2

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.

ubisum
  • 179
  • 3
  • 12
  • Template arguments are required to be compile time constant - invariably. Neither `P` nor `cols_of_P` is a compile time coinstant expression. – Pixelchemist Aug 05 '15 at 16:06

1 Answers1

2

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)
Phil
  • 5,822
  • 2
  • 31
  • 60