19 #include <vnl/algo/vnl_svd.h>
20 #include <vnl/algo/vnl_matrix_inverse.h>
21 #include <vnl/algo/vnl_symmetric_eigensystem.h>
22 #include <vnl/algo/vnl_rpoly_roots.h>
23 #include <vnl/algo/vnl_levenberg_marquardt.h>
35 void f(
const vnl_vector<double>& x, vnl_vector<double>& fx);
43 a_point c(x[1],x[2],x[3]);
44 a_point d(x[4],x[5],x[6]);
45 for (
int i = 0; i< fx.size(); i++)
49 fx[i]=(
cloud_[i]-v0).norm()-r;
57 o <<
"****************" << std::endl;
58 o <<
"a_shape_cylinder:" << std::endl;
59 o <<
"****************" << std::endl;
60 o <<
"This is a 'cylinder' fitting class" << std::endl;
101 vnl_matrix<double> mat(9,9);
102 vnl_matrix<double>
b(9,1);
103 for (
int i=0;i<9;i++)
105 double x =
pt[i].
x();
106 double y =
pt[i].
y();
107 double z =
pt[i].
z();
119 vnl_matrix<double> inv_mat = vnl_matrix_inverse<double>(mat).as_matrix();
120 vnl_matrix<double> x = inv_mat*
b;
121 for (
int i=0;i<9;i++)
123 vnl_double_3x3
A = this->
A();
125 vnl_double_3 b2 = this->
b();
127 vnl_double_3 b3 = -b2/2.;
128 vnl_svd<double> svd(
A.as_matrix());
129 svd.zero_out_relative(1.e-6);
130 vnl_double_3 c = svd.solve(b3.as_vector());
131 double d = 1.-dot_product(b2,c)-dot_product(c*
A,c);
134 double n1 = svd.W(0);
135 double n2 = svd.W(1);
136 double n3 = svd.W(2);
142 std::cerr <<
"svd " <<
n1 <<
" " <<
n2 <<
" " <<
n3 << std::endl;
143 if ((
n2>0.1)&&(
n3>0.01))
145 std::cerr <<
"failed! not a cylinder!" << std::endl;
146 for (
short i=0;i<9;i++)
para_[i]=0.;
149 center_.set(0.,0.,0.);
153 std::cerr <<
"passed! this is a cylinder" << std::endl;
156 double r = (svd.W(0)+svd.W(1))/2.;
160 r = sqrt(fabs(1./r));
161 radius_ = sqrt(fabs(1./r));
162 vnl_double_3x3 A2 = svd.recompose();
172 std::cerr <<
"dddir " << dir_ << std::endl;
178 double r = this->
radius();
179 a_point c = this->
center();
180 a_point
n = this->
dir();
182 a_point c1 = c+(
n*
v)*
n;
183 a_point v2 = (
pt-c1).normalise();
192 std::cerr <<
"rr " << radius_ <<
" " << center_ <<
" " << dir_ << std::endl;
197 double d = (
pt-pt0).norm();
204 std::cerr <<
"thres: " << iter << std::endl;
210 std::vector<a_point> pts2;
211 int cs0 = pts.size();
216 vnl_svd<double> svd(
Ap_.as_matrix());
217 double r = (svd.W(0)+svd.W(1))/2.;
218 r = sqrt(fabs(1./r));
221 vnl_vector<double> x(7);
229 vnl_levenberg_marquardt levmarq(f);
232 center_ = a_point(x[1],x[2],x[3]);
233 dir_ = a_point(x[4],x[5],x[6]);
236 std::cerr <<
"Least Squares Fitting (Levenberg Marquardt)" << std::endl
237 <<
"min: " << levmarq.get_end_error() <<
" at " << x << std::endl;
238 levmarq.diagnose_outcome();
246 std::cout <<
"radius: " << radius_ << std::endl;
247 std::cout <<
"center: " << center_ << std::endl;
251 for (
short i=0;i<3;i++)
dir[i]=-
dir[i];
253 std::cout <<
"direction: " <<
dir << std::endl;
254 std::cout <<
"azimuth: " << atan2(
dir[1],
dir[0])*180./3.1415926535 <<
"°" << std::endl;
256 std::cout <<
"elevation: " << atan2(
dir[2],
b)*180./3.1415926535 <<
"°" << std::endl;
const std::vector< a_point > & cloud_
void f(const vnl_vector< double > &x, vnl_vector< double > &fx)
a_shape_cylinder_function(const std::vector< a_point > &cloud)
void export_triangles(const unsigned int nseg, const std::vector< a_point > &pts) const
void p9pts(const a_point *pt)
int threshold_cloud(const std::vector< a_point > &pts, std::vector< a_point > &pts2)
static const std::string help()
void export_points(const unsigned int nseg, const std::vector< a_point > &pts) const
void export_matrices() const
void fit_cloud(std::vector< a_point > &pts, short nl=1)
a_point closest_point(const a_point pt) const
vnl_vector< double > para_
static const std::string help()
int best_fitting_cloud(const std::vector< a_point > &pts, std::vector< a_point > &pts2)
double v(const uint32_t step, const uint32_t n)