ubisum
ubisum

Reputation: 179

Eigen3: extract a submatrix with a non constant number of columns

I wrote the following function in C++, using Eigen3:

MatrixXf transformPoints(MatrixXf X, MatrixXf P)
{
    // create a new matrix to host points
    MatrixXf tempMatrix = MatrixXf::Zero(4, P.cols());

    // extract rotational and traslational parts from X
    MatrixXf rotmat = tempMatrix.block<2,2>(0,0);
    Vector2f traMat = tempMatrix.block<2,1>(0,2);

    // separate points from normals
    // int cols_of_P = P.cols();
    MatrixXf points = tempMatrix.block<2,P.cols()>(0,0);
    MatrixXf normals = tempMatrix.block<2,P.cols()>(2,0);
}

In my idea, on last two rows, I should be able to extract a submatrix from p, whose number of column is not known a priori, but it depends on P's size. I get the following error:

home/ubisum/fuerte_workspace/thesis_pack/src/least_squares_utilities.h: In function ‘Eigen::MatrixXf least_squares::transformPoints(Eigen::MatrixXf, Eigen::MatrixXf)’:
/home/ubisum/fuerte_workspace/thesis_pack/src/least_squares_utilities.h:18:47: error: ‘P’ cannot appear in a constant-expression
/home/ubisum/fuerte_workspace/thesis_pack/src/least_squares_utilities.h:18:49: error: ‘.’ cannot appear in a constant-expression
/home/ubisum/fuerte_workspace/thesis_pack/src/least_squares_utilities.h:18:54: error: a function call cannot appear in a constant-expression
/home/ubisum/fuerte_workspace/thesis_pack/src/least_squares_utilities.h:18:60: error: no matching function for call to ‘Eigen::Matrix<float, -0x00000000000000001, -0x00000000000000001>::block(int, int)’

I even tried the following modification:

int cols_of_P = P.cols();
MatrixXf points = tempMatrix.block<2,cols_of_P>(0,0);
MatrixXf normals = tempMatrix.block<2,cols_of_P>(2,0);

but nothing changed. Can you help me?

Thank you.

Upvotes: 2

Views: 679

Answers (1)

Phil
Phil

Reputation: 6164

Look at the documentation for Using Block Operations.

There are two versions, whose syntax is as follows:

Block operation with block of size (p,q), starting at (i,j)

Version constructing a dynamic-size block expression

matrix.block(i,j,p,q);

Version constructing a fixed-size block expression

matrix.block<p,q>(i,j);

For your case use:

tempMatrix.block(0, 0, 2, 2)

Upvotes: 2

Related Questions