c++ - Eigen and SVD to find Best Fitting Plane given a Set of Points -


given set of n points in 3d space, trying find best fitting plane using svd , eigen.

my algorithm is:

  1. center data points around (0,0,0).
  2. form 3xn matrix of point coordinates.
  3. calculate svd of matrix.
  4. set smallest singular vector corresponding least singular value normal of plane.
  5. set distance origin plane normal∙centroid.

i can't figure out how use eigen's svd module find smallest singular vector corresponding least singular value of point coordinates matrix.

so far have code (steps 1, 2 , 5 of 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); 

denoting u = svd.matrixu(), vectors u.col(0) , u.col(1) defines base of plane , u.col(2) normal plane.

u.col(0) defines direction greatest standard deviation.

you should use flag computefullu instead of computethinu have correct dimensions if points coplanar.


Comments