Search code examples
c++eigen

Row wise cross product Eigen


I am trying to do cross product between every row of one Eigen::MatrixXd dir with corresponding row of Eigen::MatrixXd v0v2 and save the result in another Eigen::MatrixXd pvec.

Initialization of pvec : Eigen::MatrixXd pvec(v0v2.rows(), 3);

I have tried this dirty method:

for(size_t i = 0; i < v0v2.rows(); i++){ pvec.row(i) = dir.row(i).cross(v0v2.row(i)); }

I get this error : THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE

I thought this would be because of column-major/row-major issue so I added .transpose() but that doesn't help either. I could always do a dirtier personal cross product element wise something like this:

Vec3 crossProduct(const Vec3<T> &v) const { return Vec3<T>(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x); } but I would like to do an Eigen method. Any help would be great!


Solution

  • The arguments of .cross must be known at compile-time to be of size 3. Try declaring your matrices as Eigen::Matrix<double, Eigen::Dynamic, 3> dir(N, 3);, etc.