MatrixBase
This class is the base that is inherited by all matrix, vector, and related expression types. Most of the Eigen API is contained in this class, and its base classes. Other important classes for the Eigen API are Matrix, and VectorwiseOp.
该类在Eigen中是一个基类,使用模板不指定变量的具体类型的时侯可以使用该类
如:
//打印输入Eigen中定义的矩阵、向量等类型的x的第一行
template<typename Derived>
void printFirstRow(const Eigen::MatrixBase<Derived>& x)
{
cout << x.row(0) << endl;
}
可以使用模板找到行数,列数,定义新的类型
template<class H_type>
void dosomething(Eigen::MatrixBase<H_type>){
H_type S;
Eigen::Matrix<double,R_type::RowsAtCompileTime,R_type::ColsAtCompileTime> K;
enum {
rows = Eigen::MatrixBase < H_type > ::RowsAtCompileTime,
cols = Eigen::MatrixBase < H_type > ::ColsAtCompileTime
};
Eigen::Matrix<double,rows,cols>T;
}
Assert
在C++中assert(x)用来调试,如果x计算值为false则中断,输出调试信息,Eigen中也有
EIGEN_STATIC_ASSERT_FIXED_SIZE(TYPE) \\ passes if TYPE is fixed size.
EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(TYPE) \\ passes if TYPE is dynamic size.
EIGEN_STATIC_ASSERT_LVALUE(Derived) \\ failes if Derived is read-only.
EIGEN_STATIC_ASSERT_ARRAYXPR(Derived) \\ passes if Derived is an array expression.
EIGEN_STATIC_ASSERT_SAME_XPR_KIND(Derived1, Derived2) \\ failes if the two expressions are an array one and a matrix one.
EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE) \\ passes if TYPE must be a vector type.
EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(TYPE, SIZE) \\ passes if TYPE must be a vector of the given size.
EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(TYPE, ROWS, COLS) \\ passes if TYPE must be a matrix with given rows and columns.
EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(TYPE0,TYPE1) \\ fails if the two vector expression types must have different sizes.
EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(TYPE0,TYPE1) \\ fails if the two matrix expression types must have different sizes.
EIGEN_STATIC_ASSERT_SIZE_1x1(TYPE) \\ fails if TYPE cannot be an 1x1 expression.
对齐
在生成定长的Matrix或Vector对象时,需要开辟内存,调用默认构造函数,内存位数没对齐就会导致程序运行出错,对于这种情况需要自己定义new的operator,但是Eigen中定义了语句“EIGEN_MAKE_ALIGNED_OPERATOR_NEW”放入类中(结尾没有“;”)这样操作之后不用担心指针对齐问题
such as
class Foo
{
...
Eigen::Vector2d v;
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
...
};
...
Foo *foo = new Foo;
quaternion
Eigen::quaternion不同于其它matrix,按照类似于向量类型输出需要q.coeffs(),其包括x,y,z,w四个元素,赋值或是调用需用类似q.x()的用法
Eigen::Quaterniond q;
q.x() = 1;
q.y() = 2;
q.z() = 3;
q.w() = 4;
std::cout << q.coeffs() << std::endl;
normalize() 和 normalized()
normalize()对使用它的变量单位化,无返回
normalized() 返回使用它变量的单位化后的值,但是使用它的变量无变化
//q1(1,2,3,4), q2(3,2,1,4)
cout << q2.coeffs() << endl;
q2.normalize();
cout << q2.coeffs() << endl;
Eigen::Quaternion<double> q3;
q3 = q1.normalized();
cout << q1.coeffs() << endl;
cout << q3.coeffs() << endl;
out:
3
2
1
4
0.547723
0.365148
0.182574
0.730297
1
2
3
4
0.182574
0.365148
0.547723
0.730297