From b47b530549ae4c637275a1f9e077d9e0eebf8c1e Mon Sep 17 00:00:00 2001 From: ganovelli Date: Thu, 13 Oct 2005 14:59:57 +0000 Subject: [PATCH] versione con svd --- vcg/space/fitting3.h | 104 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 94 insertions(+), 10 deletions(-) diff --git a/vcg/space/fitting3.h b/vcg/space/fitting3.h index a6c03bf6..357f5bb7 100644 --- a/vcg/space/fitting3.h +++ b/vcg/space/fitting3.h @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.1 2005/03/14 17:04:24 ganovelli +created + ****************************************************************************/ @@ -32,11 +35,54 @@ $Log: not supported by cvs2svn $ #include #include +#include +#include +#include namespace vcg { - // Funzione di supporto: Ritorna il vettore 1 x y z +template +bool PlaneFittingPoints( std::vector< Point3 > & samples,Plane3 &p){ + + int j; + Matrix44 m;m.SetZero(); + std::vector< Point3 > ::iterator i; + + Point3 c; c.Zero(); + for(i = samples.begin(); i != samples.end(); ++i) + c+=*i; + c/=samples.size(); + + for(i = samples.begin(); i != samples.end(); ++i){ + Point3 p = (*i)-c; + for(j = 0 ; j < 3;++j) + *(Point3*)&m[j][0] += p * p[j]; + } + + m[0][3]= m[1][3]=m[2][3]=0.0; + m[3][3]= 1.0; + m[3][0]= m[3][1]=m[3][2]=0.0; + + int n; + Matrix44 res; + Point4 e; + Point3 d; + Jacobi(m,e,res,n); + + S mx = fabs(e[0]); int mxi=0; + for(j=1; j < 3; ++j) if( (fabs(e[j]) < mx) ) {mx=fabs(e[j]); mxi = j;} + + d[0]=res[0][mxi]; + d[1]=res[1][mxi]; + d[2]=res[2][mxi]; + + p.SetOffset(c*d/d.Norm()); + p.SetDirection(d/d.Norm()); + +return true; +} + template inline double FIT_VExp( const Point3 & x, const int i ) { @@ -49,20 +95,22 @@ inline double FIT_VExp( const Point3 & x, const int i ) /** Fitting di piani: trova il piano che meglio approssima l'insieme di punti dato */ -template -bool PlaneFittingPoints( const std::vector< POINT_TYPE > & samples, Plane3 & p ) +template +bool PlaneFittingPointsOld( std::vector< Point3 > & samples, Plane3 & p ) { - typedef typename POINT_TYPE::ScalarType S; - const int N = 4; + Point3 d; + const int N = 4; S P[N][N]; // A = s' . s S U[N][N]; int i,j,k,n; - n = samples.size(); + n = (int)samples.size(); if(n<3) return false; + //printf("\n p_prima: %f %f %f %f \n",p.Offset(),p.Direction()[0],p.Direction()[1],p.Direction()[2]); + for(i=0;i & samples, Plane3 m; + for(i=0;i s;s.Zero(); +// +// s.Normalize(); +// printf("\n RES %f %f %f %f \n",s[0],s[1],s[2],s[3]); +//printf("\n GJ \n"); +// for(i=0;i & samples, Plane3(U[1][2]*U[2][3]-U[1][3],-U[2][3],1)); - p.SetOffset(-(U[0][2]*U[2][3]-U[0][3]+U[0][1]*U[1][3]-U[0][1]*U[1][2]*U[2][3])); + //printf("\n U \n"); + //for(i=0;i(U[1][2]*U[2][3]-U[1][3],-U[2][3],1).Norm(); + + p.SetDirection(Point3(U[1][2]*U[2][3]-U[1][3],-U[2][3],1)); + p.SetOffset(-(U[0][2]*U[2][3]-U[0][3]+U[0][1]*U[1][3]-U[0][1]*U[1][2]*U[2][3])/norm); + + + //printf("\n p: %f %f %f %f \n",p.Offset(),p.Direction()[0],p.Direction()[1],p.Direction()[2]); + return true; } - } // end namespace #endif \ No newline at end of file