如何使用 pybind11 在 Eigen::Quaternion 和 numpy ndarray

问题描述

我正在尝试封装一个将 Eigen::Quaternion 作为 Python 使用参数的 C++ 函数。 该函数定义为:

void func(const Eigen::Quaternion<double> &rotation) {...}

我正在使用 pybind11 为 python 包装它,在我的 pybind11 中我有

#include <pybind11/eigen.h> // I have this included

PYBIND11_MODULE(example,m)
{
    m.def("func",&func,"example function");
}

一切看起来都不错,它可以编译,但是当我调用它时:

func(np.array([0,1]))

我有错误

func():不兼容的函数参数。以下参数类型 支持: 1. (arg0: Eigen::Quaternion) -> None

是否进行了一些谷歌搜索,但无法找到有关是否可以将 Eigen::Quaternion 投射到/从 numpy 数组以及应该使用什么形状的数组的答案?我认为四元数可以从一个 4 元素的 numpy ndarray 中投射出来,但似乎不是,有人知道怎么做吗?

解决方法

我认为问题在于 eigen quat 需要双精度数,而您已经用整数构建了您的 np.array。

func(np.array([0.0,0.0,1.0]))

可能会起作用吗?