Wm4ApprPolyFit3.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 "Wm4ApprPolyFit3.h"
00019 #include "Wm4LinearSystem.h"
00020 
00021 namespace Wm4
00022 {
00023 //----------------------------------------------------------------------------
00024 template <class Real>
00025 Real* PolyFit3 (int iSamples, const Real* afX, const Real* afY,
00026     const Real* afW, int iXDegree, int iYDegree)
00027 {
00028     int iXBound = iXDegree + 1;
00029     int iYBound = iYDegree + 1;
00030     int iQuantity = iXBound*iYBound;
00031     Real* afCoeff = WM4_NEW Real[iQuantity];
00032 
00033     int i0, j0, i1, j1, iS;
00034 
00035     // powers of x, y
00036     Real** aafXP;
00037     Real** aafYP;
00038     Allocate<Real>(2*iXDegree+1,iSamples,aafXP);
00039     Allocate<Real>(2*iYDegree+1,iSamples,aafYP);
00040     for (iS = 0; iS < iSamples; iS++)
00041     {
00042         aafXP[iS][0] = (Real)1.0;
00043         for (i0 = 1; i0 <= 2*iXDegree; i0++)
00044         {
00045             aafXP[iS][i0] = afX[iS]*aafXP[iS][i0-1];
00046         }
00047 
00048         aafYP[iS][0] = (Real)1.0;
00049         for (j0 = 1; j0 <= 2*iYDegree; j0++)
00050         {
00051             aafYP[iS][j0] = afY[iS]*aafYP[iS][j0-1];
00052         }
00053     }
00054 
00055     // Vandermonde matrix and right-hand side of linear system
00056     GMatrix<Real> kA(iQuantity,iQuantity);
00057     Real* afB = WM4_NEW Real[iQuantity];
00058 
00059     for (j0 = 0; j0 <= iYDegree; j0++)
00060     {
00061         for (i0 = 0; i0 <= iXDegree; i0++)
00062         {
00063             int iIndex0 = i0+iXBound*j0;
00064             Real fSum = (Real)0.0;
00065             for (iS = 0; iS < iSamples; iS++)
00066             {
00067                 fSum += afW[iS]*aafXP[iS][i0]*aafYP[iS][j0];
00068             }
00069 
00070             afB[iIndex0] = fSum;
00071 
00072             for (j1 = 0; j1 <= iYDegree; j1++)
00073             {
00074                 for (i1 = 0; i1 <= iXDegree; i1++)
00075                 {
00076                     int iIndex1 = i1+iXBound*j1;
00077                     fSum = (Real)0.0;
00078                     for (iS = 0; iS < iSamples; iS++)
00079                     {
00080                         fSum += aafXP[iS][i0+i1]*aafYP[iS][j0+j1];
00081                     }
00082 
00083                     kA(iIndex0,iIndex1) = fSum;
00084                 }
00085             }
00086         }
00087     }
00088 
00089     // solve for the polynomial coefficients
00090     bool bHasSolution = LinearSystem<Real>().Solve(kA,afB,afCoeff);
00091     if (!bHasSolution) throw std::exception();
00092     assert(bHasSolution);
00093     (void)bHasSolution;  // avoid compiler warning in release build
00094     WM4_DELETE[] afB;
00095     Deallocate<Real>(aafXP);
00096     Deallocate<Real>(aafYP);
00097 
00098     return afCoeff;
00099 }
00100 //----------------------------------------------------------------------------
00101 
00102 //----------------------------------------------------------------------------
00103 // explicit instantiation
00104 //----------------------------------------------------------------------------
00105 template WM4_FOUNDATION_ITEM
00106 float* PolyFit3<float> (int, const float*, const float*, const float*,
00107     int, int);
00108 
00109 template WM4_FOUNDATION_ITEM
00110 double* PolyFit3<double> (int, const double*, const double*, const double*,
00111     int, int);
00112 //----------------------------------------------------------------------------
00113 }

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