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 #ifndef WM4APPRPOLYFIT3_H 00018 #define WM4APPRPOLYFIT3_H 00019 00020 #include "Wm4FoundationLIB.h" 00021 #include "Wm4System.h" 00022 00023 namespace Wm4 00024 { 00025 00026 // The samples are (x[i],y[i],w[i]) for 0 <= i < S. Think of w as a function 00027 // of x and y, say w = f(x,y). The function fits the samples with a 00028 // polynomial of degree d0 in x and degree d1 in y, say 00029 // w = sum_{i=0}^{d0} sum_{j=0}^{d1} c[i][j]*x^i*y^j 00030 // The method is a least-squares fitting algorithm. The returned array 00031 // stores the c[i][j] values according to returned[i+(d0+1)*j] = c[i][j] 00032 // for a total of (d0+1)*(d1+1) coefficients. The caller is responsible for 00033 // deleting the input arrays if they were dynamically allocated. The caller 00034 // is also responsible for deleting the returned array. 00035 // 00036 // WARNING. The fitting algorithm for polynomial terms 00037 // (1,x,x^2,...,x^d0), (1,y,y^2,...,y^d1) 00038 // is known to be nonrobust for large degrees and for large magnitude data. 00039 // One alternative is to use orthogonal polynomials 00040 // (f[0](x),...,f[d0](x)), (g[0](y),...,g[d1](y)) 00041 // and apply the least-squares algorithm to these. Another alternative is to 00042 // transform 00043 // (x',y',w') = ((x-xcen)/rng, (y-ycen)/rng, w/rng) 00044 // where xmin = min(x[i]), xmax = max(x[i]), xcen = (xmin+xmax)/2, 00045 // ymin = min(y[i]), ymax = max(y[i]), ycen = (ymin+ymax)/2, and 00046 // rng = max(xmax-xmin,ymax-ymin). Fit the (x',y',w') points, 00047 // w' = sum_{i=0}^{d0} sum_{j=0}^{d1} c'[i][j]*(x')^i*(y')^j 00048 // The original polynomial is evaluated as 00049 // w = rng * sum_{i=0}^{d0} sum_{j=0}^{d1} c'[i][j] * 00050 // ((x-xcen)/rng)^i * ((y-ycen)/rng)^j 00051 00052 template <class Real> WM4_FOUNDATION_ITEM 00053 Real* PolyFit3 (int iSamples, const Real* afX, const Real* afY, 00054 const Real* afW, int iXDegree, int iYDegree); 00055 00056 } 00057 00058 #endif