Skip to content

Commit b18663b

Browse files
committed
SelfAdjointSolver always produce real and sorted eigenvalues
1 parent 087eb94 commit b18663b

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

include/pca.hpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,21 @@ static inline PCAResult<real_t> pca_from_pointcloud(const PointCloud<real_t>& cl
7979
Eigen::SelfAdjointEigenSolver<Eigen::Matrix<real_t, 3, 3>> es(cov);
8080

8181
// Sort the values and vectors in order of increasing eigenvalue
82-
const auto ev = es.eigenvalues().real();
82+
const auto ev = es.eigenvalues();
8383

84-
std::array<Eigen::Index, 3> indices = {0, 1, 2};
84+
// Clamp eigen values
85+
Vec3<real_t> val = {(std::max(ev(0), real_t(0.))), (std::max(ev(1), real_t(0.))), (std::max(ev(2), real_t(0.)))};
8586

86-
std::sort(
87-
std::begin(indices), std::end(indices), [&](Eigen::Index i1, Eigen::Index i2) { return ev(i1) > ev(i2); });
88-
89-
Vec3<real_t> val = {
90-
(std::max(ev(indices[0]), real_t(0.))), (std::max(ev(indices[1]), real_t(0.))),
91-
(std::max(ev(indices[2]), real_t(0.)))};
92-
Vec3<real_t> v0 = es.eigenvectors().col(indices[0]).real();
93-
Vec3<real_t> v1 = es.eigenvectors().col(indices[1]).real();
94-
Vec3<real_t> v2 = es.eigenvectors().col(indices[2]).real();
87+
Vec3<real_t> v0 = es.eigenvectors().col(0);
88+
Vec3<real_t> v1 = es.eigenvectors().col(1);
89+
Vec3<real_t> v2 = es.eigenvectors().col(2);
9590

9691
// To standardize the orientation of eigenvectors, we choose to enforce all eigenvectors
9792
// to be expressed in the Z+ half-space.
98-
// Only the third eigenvector (v2) needs to be reoriented because it is the
93+
// Only the third eigenvector (v2 the surface normal) needs to be reoriented because it is the
9994
// only one used in further computations.
10095
// TODO: In case we want to orient normal, this should be improved
101-
if (v2(2) < real_t(0.)) { v2 = real_t(-1.) * v2; }
96+
if (v2(2) < real_t(0.)) { v2 *= real_t(-1.); };
10297
return {val, v0, v1, v2};
10398
};
10499

0 commit comments

Comments
 (0)