2#include <cgv/math/vec.h>
3#include <cgv/math/point_operations.h>
4#include <cgv/math/eig.h>
5#include <cgv/math/ray.h>
19T plane_val(
const vec<T>& plane,
const vec<T>& x)
22 assert(plane.size()-1 == x.size());
25 for(
unsigned i = 0;i< x.size(); i++)
28 return val+plane(plane.size()-1);
33vec<T> plane_val(
const vec<T>& plane,
const mat<T>& xs)
36 assert(plane.size()-1 == xs.nrows());
38 vec<T> vals(xs.ncols());
39 for(
unsigned i = 0;i< xs.ncols(); i++)
40 vals(i) = plane_val(plane,xs.col(i));
49vec<T> plane_fit(
const vec<T>& p1,
const vec<T>& p2,
const vec<T>& p3)
54 vec<T> nml = cross(normalize(p2-p1),normalize(p3-p1));
59 plane.set(nml(0),nml(1),nml(2),-dot(p1,nml));
65vec<T> tls_plane_fit(
const mat<T>& points)
72 covmat_and_mean(points,covmat, mean);
77 vec<T> nml = normalize(v.col(2));
79 plane.set(nml(0),nml(1),nml(2),-dot(mean,nml));
93vec<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)
95 assert(points.nrows == 3);
98 vec<unsigned int> ind;
99 vec<T> p1,p2,p3,dists(points.ncols());
101 unsigned n = points.ncols;
102 unsigned max_iter = num_ransac_iterations(3,p_out,psurety);
103 unsigned num_inlier = (unsigned)::ceil((1-p_out)*points.ncols());
105 T error = std::numeric_limits<T>::max();
107 for(
unsigned i = 0; i < max_iter; i++)
113 p1 = points.col(ind(0));
114 p2 = points.col(ind(1));
115 p3 = points.col(ind(2));
117 while(fabs(length(cross(q2-q1,q3-q1)))<0.001);
120 vec<T> pl = plane_fit(p1,p2,p3);
126 for(
unsigned i = 0; i < points.ncols(); i++)
128 dists(i) = std::abs(plane_val(pl,points.col(i)));
142 if(loransac && n_inlier > 3)
144 mat<T> inliers(3,n_inlier);
147 for(
unsigned i = 0; i < points.ncols(); i++)
152 inliers.set_col(j,points.col(i));
156 pl = plane_fit(inliers)
159 for(
unsigned i = 0; i < points.ncols(); i++)
161 dists(i) = std::abs(plane_val(pl,points.col(i)));
182 if(n_inlier >= num_inlier)
184 p_out = (T)1-(T)n_inlier/(T)points.ncols();
185 max_iter = num_ransac_iterations(3,p_out,psurety);
201int ray_plane_intersection_nd(
const ray<T>& ray,
const vec<T>& plane, T& out_t) {
204 for(
unsigned i = 0; i < ray.origin.dim(); i++) {
205 t += plane[i] * ray.origin[i];
206 s += plane[i] * ray.direction[i];
211 t += plane[ray.origin.dim()];
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