Given a set of N points in a 3D space, I am trying to find the best fitting plane using SVD and Eigen.
My algorithm is:
- Center data points around (0,0,0).
- Form 3xN matrix of point coordinates.
- Calculate SVD of the matrix.
- Set the smallest singular vector corresponding to the least singular value as normal of the plane.
- Set distance from origin to the plane as normal∙centroid.
I can't figure out how to use Eigen's SVD Module to find the smallest singular vector corresponding to the least singular value of point coordinates matrix.
So far I have this code (steps 1, 2 and 5 of the algorithm):
Eigen::Matrix<float, 3, 1> mean = points.rowwise().mean();
const Eigen::Matrix3Xf points_centered = points.colwise() - mean;
int setting = Eigen::ComputeThinU | Eigen::ComputeThinV;
Eigen::JacobiSVD<Eigen::Matrix3Xf> svd = points_centered.jacobiSvd(setting);
Eigen::Vector3d normal = **???**
double d = normal.dot(mean);