2#include <cgv/math/vec.h>
3#include <cgv/math/functions.h>
4#include <cgv/math/det.h>
5#include <cgv/math/lin_solve.h>
20T sphere_val(
const vec<T>& sphere,
const vec<T>& x)
23 assert(sphere.size()-1 == x.size());
26 for(
unsigned i = 0; i< x.size(); i++)
27 val += sqr(x(i)-sphere(i));
29 return sqrt(val)-sphere(sphere.size()-1);
35vec<T> sphere_val(
const vec<T>& sphere,
const mat<T>& xs)
38 assert(sphere.size()-1 == xs.ncols());
41 for(
unsigned i = 0; i< xs.ncols(); i++)
42 vals(i) += sphere_val(sphere,xs.col(i));
50T sphere_val2(
const vec<T>& sphere,
const vec<T>& x)
53 assert(sphere.size()-1 == x.size());
56 for(
unsigned i = 0; i< x.size(); i++)
57 val += sqr(x(i)-sphere(i));
59 return val-sqr(sphere(sphere.size()-1));
65vec<T> sphere_val2(
const vec<T>& sphere,
const mat<T>& xs)
68 assert(sphere.size()-1 == xs.ncols());
71 for(
unsigned i = 0; i< xs.ncols(); i++)
72 vals(i) += sphere_val2(sphere,xs.col(i));
79vec<T> sphere_fit(
const vec<T>& p1)
82 sphere.set(p1(0),p1(1),p1(2),std::numeric_limits<T>::epsilon());
89vec<T> sphere_fit(
const vec<T>& p1,
const vec<T>& p2)
92 vec<T> center = (T)0.5*(p1+p2);
94 sphere.set(center(0),center(1),center(2),length(p2-center)+ std::numeric_limits<T>::epsilon());
101vec<T> sphere_fit(
const vec<T>& O,
const vec<T>& A,
const vec<T>& B)
105 T denominator = (T)2.0 * dot(cross(a , b) , cross(a , b));
106 vec<T> o = (dot(b,b) * cross(cross(a , b) , a) +
107 dot(a,a) * cross(b , cross(a , b))) / denominator;
109 T radius = length(o) + std::numeric_limits<T>::epsilon();
110 vec<T> center = O + o;
112 return vec<T>(center(0),center(1),center(2),radius);
121vec<T> sphere_fit(
const vec<T>& x1,
const vec<T>& x2,
const vec<T>& x3,
const vec<T>& x4)
124 M(0,0) = 1; M(0,1) = x1(0); M(0,2) = x1(1); M(0,3) = x1(2);
125 M(1,0) = 1; M(1,1) = x2(0); M(1,2) = x2(1); M(1,3) = x2(2);
126 M(2,0) = 1; M(2,1) = x3(0); M(2,2) = x3(1); M(2,3) = x3(2);
127 M(3,0) = 1; M(3,1) = x4(0); M(3,2) = x4(1); M(3,3) = x4(2);
134 cgv::math::solve(M,b,x);
135 vec<T> center = vec<T>(-x(1)/(T)2.0,-x(2)/(T)2.0,-x(3)/(T)2.0);
136 T radius = sqrt(dot(center,center)-x(0));
137 return vec<T>(center(0),center(1),center(2),radius);
145vec<T> recurse_mini_ball(T** P,
unsigned int p,
unsigned int b=0)
152 mb=vec<T>((T)0,(T)0,(T)0,(T)-1);
155 mb = sphere_fit(vec<T>(3,(
const T*)P[-1]));
158 mb = sphere_fit(vec<T>(3,(
const T*)P[-1]),vec<T>(3,(
const T*)P[-2]));
161 mb = sphere_fit(vec<T>(3,(
const T*)P[-1]),vec<T>(3,(
const T*)P[-2]),vec<T>(3,(
const T*)P[-3]));
164 mb = sphere_fit(vec<T>(3,(
const T*)P[-1]),vec<T>(3,(
const T*)P[-2]),vec<T>(3,(
const T*)P[-3])
165 ,vec<T>(3,(
const T*)P[-4]));
169 for(
unsigned int i = 0; i < p; i++)
171 if(sphere_val2(mb,vec<T>(3,(
const T*)P[i])) > 0)
173 for(
unsigned int j = i; j > 0; j--)
180 mb = recurse_mini_ball(P + 1, i, b + 1);
194 unsigned p = points.
ncols();
197 for(
unsigned int i = 0; i < p; i++)
198 L[i] = &(points(0,i));
202 vec<T> mb = recurse_mini_ball(L, p);
A matrix type (full column major storage) The matrix can be loaded directly into OpenGL without need ...
unsigned ncols() const
number of columns