Skip to content

Phase out slice for dense matrices#2259

Merged
alecjacobson merged 10 commits intomainfrom
alecjacobson/phase-out-slice
Sep 1, 2023
Merged

Phase out slice for dense matrices#2259
alecjacobson merged 10 commits intomainfrom
alecjacobson/phase-out-slice

Conversation

@alecjacobson
Copy link
Copy Markdown
Contributor

Fixes #2258

As mentioned in #2258, Eigen v3.4 supports slicing for dense matrices making many of the igl::slice overloads obsolete. I've removed all of their uses within libigl internally and in the tests and tutorial so that now dense slices show up in the static library. They're still available for now (e.g., via header only).

This will break code relying on these in the static library, but the fixes are straight forward (and result in cleaner + faster code! thanks, eigen!).

I've also added a igl::find overload to make using binary masks easy.

There's a nasty gotcha in Eigen's slicing, I'll document here:

Eigen::MatrixXd A(4,2);
A<< 1,2,
  3,4,
  5,6,
  7,8;
Eigen::Array<bool,Eigen::Dynamic,1> M(4);
M<<0,1,0,1;
// Correctly select rows using mask M
Eigen::MatrixXd B = A(igl::find(M),Eigen::all);
// Unfortunately, this also compiles:
Eigen::MatrixXd C = A(M,Eigen::all);

std::cout<<igl::matlab_format(B,"B")<<std::endl;
std::cout<<igl::matlab_format(C,"C")<<std::endl;

this results in

B = [
  3 4
  7 8
];
C = [
  1 2
  3 4
  1 2
  3 4
];

We can see that the true and false values of the Eigen::Array<bool,…> M have been interpreted as indices 0 and 1 🙃

Remembering to useigl::find is a fine solution as long as ... well ... you remember. I wish Eigen would throw a warning if you use bool scalar types as a slicing index.

@alecjacobson alecjacobson merged commit b774e1b into main Sep 1, 2023
@alecjacobson alecjacobson deleted the alecjacobson/phase-out-slice branch September 1, 2023 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Phase out igl::slice for dense matrices

1 participant