17#include <Eigen/Eigenvalues>
19#include <lagrange/utils/assert.h>
20#include <lagrange/legacy/inline.h>
46template <
typename Scalar>
51 Eigen::Matrix<Scalar, Eigen::Dynamic, 1> center;
54 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> components;
58 Eigen::Matrix<Scalar, Eigen::Dynamic, 1> weights;
68template <
typename Derived1>
70 const Eigen::MatrixBase<Derived1>& points,
71 const bool should_shift_centeroid,
72 const bool should_normalize)
75 using Scalar =
typename Derived1::Scalar;
77 Eigen::Matrix<Scalar, Derived1::ColsAtCompileTime, Derived1::ColsAtCompileTime>;
78 using RowVectorXS = Eigen::Matrix<Scalar, 1, Derived1::ColsAtCompileTime>;
87 if (should_shift_centeroid) {
88 center = points.colwise().mean();
90 center = RowVectorXS::Zero(points.cols());
93 if (should_normalize) {
97 ((points.rowwise() - center).transpose() * (points.rowwise() - center)) / points.rows();
99 covariance = ((points.rowwise() - center).transpose() * (points.rowwise() - center));
104 Eigen::SelfAdjointEigenSolver<MatrixXS> eigs(covariance);
109 ComputePointcloudPCAOutput<Scalar> output;
110 output.center = center.transpose();
111 output.components = eigs.eigenvectors();
112 output.weights = eigs.eigenvalues();
115 if (output.components.determinant() < 0.) {
116 output.components.col(0) *= -1;
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
Main namespace for Lagrange.
Definition: AABBIGL.h:30
PointcloudPCAOutput< Scalar > compute_pointcloud_pca(span< const Scalar > points, ComputePointcloudPCAOptions options={})
Finds the principal components for a pointcloud.
Definition: compute_pointcloud_pca.cpp:23
Definition: compute_pointcloud_pca.h:48