如何使用 Boost 从 numpy ndarray 中提取 Eigen MatrixXd



在Boost的文档中,extract函数用于将内置的python数据类型转换为c ++数据类型。我有一个返回numpy ndarray的python函数,在我的c ++代码中,我想将其转换为Eigen MatrixXd。所以,我想知道是否可以将非内置的python类型提取到非内置的c ++类型。

你需要编写numpy ndarray和c ++ Eigen:::MatrixXd之间的转换,并将其注册到boost python注册表中。

看起来像这样

namespace py = boost::python
template <typename MatrixType>
struct pyToEigen {
pyToEigen() {
py::converter::registry::push_back(&convertible, &construct, py::type_id<MatrixType>());
}
static void* convertible(PyObject* obj_ptr) {
// check for if object is convetible here
}
static void construct(PyObject* obj_ptr, py::converter::rvalue_from_python_stage1_data* data) {
// Construct here
}
}:

我写了一个仅标题库来完成这项工作:https://github.com/vsamy/pygen-converter 您只需要调用pygen::converter(Converters::All, true)即可带来几种不同的转换。

这种方法的主要缺点之一是它复制了所有内容。

最新更新