Eigen相關(MatrixBase, Assert, ALIGNED. quaternion, normalize()和normalized)

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章