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 WM4APPRQUADRATICFIT3_H 00018 #define WM4APPRQUADRATICFIT3_H 00019 00020 #include "Wm4FoundationLIB.h" 00021 #include "Wm4Vector3.h" 00022 00023 namespace Wm4 00024 { 00025 00026 // Quadratic fit is 00027 // 00028 // 0 = C[0] + C[1]*X + C[2]*Y + C[3]*Z + C[4]*X^2 + C[5]*Y^2 00029 // + C[6]*Z^2 + C[7]*X*Y + C[8]*X*Z + C[9]*Y*Z 00030 // 00031 // subject to Length(C) = 1. Minimize E(C) = C^t M C with Length(C) = 1 00032 // and M = (sum_i V_i)(sum_i V_i)^t where 00033 // 00034 // V = (1, X, Y, Z, X^2, Y^2, Z^2, X*Y, X*Z, Y*Z) 00035 // 00036 // Minimum value is the smallest eigenvalue of M and C is a corresponding 00037 // unit length eigenvector. 00038 // 00039 // Input: 00040 // n = number of points to fit 00041 // p[0..n-1] = array of points to fit 00042 // 00043 // Output: 00044 // c[0..9] = coefficients of quadratic fit (the eigenvector) 00045 // return value of function is nonnegative and a measure of the fit 00046 // (the minimum eigenvalue; 0 = exact fit, positive otherwise) 00047 00048 // Canonical forms. The quadratic equation can be factored into 00049 // P^T A P + B^T P + K = 0 where P = (X,Y,Z), K = C[0], B = (C[1],C[2],C[3]), 00050 // and A is a 3x3 symmetric matrix with A00 = C[4], A11 = C[5], A22 = C[6], 00051 // A01 = C[7]/2, A02 = C[8]/2, and A12 = C[9]/2. Matrix A = R^T D R where 00052 // R is orthogonal and D is diagonal (using an eigendecomposition). Define 00053 // V = R P = (v0,v1,v2), E = R B = (e0,e1,e2), D = diag(d0,d1,d2), and f = K 00054 // to obtain 00055 // 00056 // d0 v0^2 + d1 v1^2 + d2 v^2 + e0 v0 + e1 v1 + e2 v2 + f = 0 00057 // 00058 // Characterization depends on the signs of the d_i. 00059 template <class Real> WM4_FOUNDATION_ITEM 00060 Real QuadraticFit3 (int iQuantity, const Vector3<Real>* akPoint, 00061 Real afCoeff[10]); 00062 00063 // If you think your points are nearly spherical, use this. Sphere is of form 00064 // C'[0]+C'[1]*X+C'[2]*Y+C'[3]*Z+C'[4]*(X^2+Y^2+Z^2) where Length(C') = 1. 00065 // Function returns C = (C'[0]/C'[4],C'[1]/C'[4],C'[2]/C'[4],C'[3]/C'[4]), so 00066 // fitted sphere is C[0]+C[1]*X+C[2]*Y+C[3]*Z+X^2+Y^2+Z^2. Center is 00067 // (xc,yc,zc) = -0.5*(C[1],C[2],C[3]) and radius is rad = 00068 // sqrt(xc*xc+yc*yc+zc*zc-C[0]). 00069 template <class Real> WM4_FOUNDATION_ITEM 00070 Real QuadraticSphereFit3 (int iQuantity, const Vector3<Real>* akPoint, 00071 Vector3<Real>& rkCenter, Real& rfRadius); 00072 00073 } 00074 00075 #endif