Wm4ApprLineFit3.cpp

Go to the documentation of this file.
00001 // Wild Magic Source Code
00002 // David Eberly
00003 // http://www.geometrictools.com
00004 // Copyright (c) 1998-2007
00005 //
00006 // This library is free software; you can redistribute it and/or modify it
00007 // under the terms of the GNU Lesser General Public License as published by
00008 // the Free Software Foundation; either version 2.1 of the License, or (at
00009 // your option) any later version.  The license is available for reading at
00010 // either of the locations:
00011 //     http://www.gnu.org/copyleft/lgpl.html
00012 //     http://www.geometrictools.com/License/WildMagicLicense.pdf
00013 // The license applies to versions 0 through 4 of Wild Magic.
00014 //
00015 // Version: 4.0.0 (2006/06/28)
00016 
00017 #include "Wm4FoundationPCH.h"
00018 #include "Wm4ApprLineFit3.h"
00019 #include "Wm4Eigen.h"
00020 
00021 namespace Wm4
00022 {
00023 //----------------------------------------------------------------------------
00024 template <class Real>
00025 Line3<Real> OrthogonalLineFit3 (int iQuantity, const Vector3<Real>* akPoint)
00026 {
00027     Line3<Real> kLine(Vector3<Real>::ZERO,Vector3<Real>::ZERO);
00028 
00029     // compute the mean of the points
00030     kLine.Origin = akPoint[0];
00031     int i;
00032     for (i = 1; i < iQuantity; i++)
00033     {
00034         kLine.Origin += akPoint[i];
00035     }
00036     Real fInvQuantity = ((Real)1.0)/iQuantity;
00037     kLine.Origin *= fInvQuantity;
00038 
00039     // compute the covariance matrix of the points
00040     Real fSumXX = (Real)0.0, fSumXY = (Real)0.0, fSumXZ = (Real)0.0;
00041     Real fSumYY = (Real)0.0, fSumYZ = (Real)0.0, fSumZZ = (Real)0.0;
00042     for (i = 0; i < iQuantity; i++) 
00043     {
00044         Vector3<Real> kDiff = akPoint[i] - kLine.Origin;
00045         fSumXX += kDiff.X()*kDiff.X();
00046         fSumXY += kDiff.X()*kDiff.Y();
00047         fSumXZ += kDiff.X()*kDiff.Z();
00048         fSumYY += kDiff.Y()*kDiff.Y();
00049         fSumYZ += kDiff.Y()*kDiff.Z();
00050         fSumZZ += kDiff.Z()*kDiff.Z();
00051     }
00052 
00053     fSumXX *= fInvQuantity;
00054     fSumXY *= fInvQuantity;
00055     fSumXZ *= fInvQuantity;
00056     fSumYY *= fInvQuantity;
00057     fSumYZ *= fInvQuantity;
00058     fSumZZ *= fInvQuantity;
00059 
00060     // set up the eigensolver
00061     Eigen<Real> kES(3);
00062     kES(0,0) = fSumYY+fSumZZ;
00063     kES(0,1) = -fSumXY;
00064     kES(0,2) = -fSumXZ;
00065     kES(1,0) = kES(0,1);
00066     kES(1,1) = fSumXX+fSumZZ;
00067     kES(1,2) = -fSumYZ;
00068     kES(2,0) = kES(0,2);
00069     kES(2,1) = kES(1,2);
00070     kES(2,2) = fSumXX+fSumYY;
00071 
00072     // compute eigenstuff, smallest eigenvalue is in last position
00073     kES.DecrSortEigenStuff3();
00074 
00075     // unit-length direction for best-fit line
00076     kES.GetEigenvector(2,kLine.Direction);
00077 
00078     return kLine;
00079 }
00080 //----------------------------------------------------------------------------
00081 
00082 //----------------------------------------------------------------------------
00083 // explicit instantiation
00084 //----------------------------------------------------------------------------
00085 template WM4_FOUNDATION_ITEM
00086 Line3<float> OrthogonalLineFit3<float> (int, const Vector3<float>*);
00087 
00088 template WM4_FOUNDATION_ITEM
00089 Line3<double> OrthogonalLineFit3<double> (int, const Vector3<double>*);
00090 //----------------------------------------------------------------------------
00091 }

Generated on Wed Nov 23 19:01:02 2011 for FreeCAD by  doxygen 1.6.1