2#include <cgv/math/vec.h>
3#include <cgv/math/point_operations.h>
4#include <cgv/math/eig.h>
18T plane_val(
const vec<T>& plane,
const vec<T>& x)
21 assert(plane.size()-1 == x.size());
24 for(
unsigned i = 0;i< x.size(); i++)
27 return val+plane(plane.size()-1);
32vec<T> plane_val(
const vec<T>& plane,
const mat<T>& xs)
35 assert(plane.size()-1 == xs.nrows());
37 vec<T> vals(xs.ncols());
38 for(
unsigned i = 0;i< xs.ncols(); i++)
39 vals(i) = plane_val(plane,xs.col(i));
48vec<T> plane_fit(
const vec<T>& p1,
const vec<T>& p2,
const vec<T>& p3)
53 vec<T> nml = cross(normalize(p2-p1),normalize(p3-p1));
58 plane.set(nml(0),nml(1),nml(2),-dot(p1,nml));
64vec<T> tls_plane_fit(
const mat<T>& points)
71 covmat_and_mean(points,covmat, mean);
76 vec<T> nml = normalize(v.col(2));
78 plane.set(nml(0),nml(1),nml(2),-dot(mean,nml));
92vec<T> ransac_plane_fit(
const mat<T>& points,
const T p_out=0.8,
const T d_max=0.001,
const T p_surety = 0.99,
bool msac=
true)
94 assert(points.nrows == 3);
97 vec<unsigned int> ind;
98 vec<T> p1,p2,p3,dists(points.ncols());
100 unsigned n = points.ncols;
101 unsigned max_iter = num_ransac_iterations(3,p_out,psurety);
102 unsigned num_inlier = (unsigned)::ceil((1-p_out)*points.ncols());
104 T error = std::numeric_limits<T>::max();
106 for(
unsigned i = 0; i < max_iter; i++)
112 p1 = points.col(ind(0));
113 p2 = points.col(ind(1));
114 p3 = points.col(ind(2));
116 while(fabs(length(cross(q2-q1,q3-q1)))<0.001);
119 vec<T> pl = plane_fit(p1,p2,p3);
125 for(
unsigned i = 0; i < points.ncols(); i++)
127 dists(i) = std::abs(plane_val(pl,points.col(i)));
141 if(loransac && n_inlier > 3)
143 mat<T> inliers(3,n_inlier);
146 for(
unsigned i = 0; i < points.ncols(); i++)
151 inliers.set_col(j,points.col(i));
155 pl = plane_fit(inliers)
158 for(
unsigned i = 0; i < points.ncols(); i++)
160 dists(i) = std::abs(plane_val(pl,points.col(i)));
181 if(n_inlier >= num_inlier)
183 p_out = (T)1-(T)n_inlier/(T)points.ncols();
184 max_iter = num_ransac_iterations(3,p_out,psurety);
High quality random number generator, which is a little bit slower than typical random number generat...
void uniform_nchoosek(unsigned n, unsigned k, vec< unsigned > &indices)
creates a vector of k unique indices drawn from 0 to n-1