我试图取代使用本征在我的代码目前使用的矩阵库。我有几个班这样一个添加自定义方法的基础基质类。在这个例子中,我更换了父亲类征:
#include <iostream>
#include <eigen3/Eigen/Dense>
class MyVectorType: public Eigen::Matrix<double, 3, 1> {
public:
MyVectorType(void) :
Eigen::Matrix<double, 3, 1>() {
}
typedef Eigen::Matrix<double, 3, 1> Base;
// This constructor allows you to construct MyVectorType from Eigen expressions
template<typename OtherDerived>
MyVectorType(const Eigen::MatrixBase<OtherDerived>& other) :
Eigen::Matrix<double, 3, 1>(other) {
}
// This method allows you to assign Eigen expressions to MyVectorType
template<typename OtherDerived>
MyVectorType & operator=(const Eigen::MatrixBase<OtherDerived>& other) {
this->Base::operator=(other);
return *this;
}
void customMethod() {
//bla bla....
}
};
最大的问题是,是不是很容易的方法来管理自定义类。例:
void foo(MyVectorType& a) {
....
a.customMethod();
}
void foo(Eigen::Ref<MyVectorType::Base> a) {
....
a.customMethod(); <---can't call customMethod here
}
Eigen::Matrix<double, -1, -1, 0, 15, 15> m(3,1);
foo(m); <---can't call it with m;
Eigen::Map<Matrix<double, 3, 1> > map(m.data(), 3, 1);
Eigen::Ref<Matrix<double, 3, 1> > ref(map);
foo(ref); <---it works but I can't call custom method
通常本征提供参考模板类,但我不能使用自定义类使用它,因为如果我使用参考,我将无法在这个例子中,我应该征::参考在我的例子调用customMethod内FOO。避免使用参考的是一个大问题,因为使用地图和文献征对象是非常重要的动态矩阵转换为固定的一个并执行其他转换操作。最后一个问题:什么是在这种情况下,使用本征的最佳策略是什么?
至少有三种方法:
MyVectorType
的,使customMethod
通过MatrixBase
机制plugin的成员。MyVectorType
的,使customMethod
免费的功能。Eigen::Ref<MyVectorType>
它的构造Ref<MyVectorType::Base>
,添加customMethod
方法,并通过调用内部的自由功能customMethod_impl
因式分解的两种方法。