Blender  V2.59
LOD_Quadric.h
Go to the documentation of this file.
00001 /*
00002  * $Id: LOD_Quadric.h 35147 2011-02-25 10:47:28Z jesterking $
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00020  * All rights reserved.
00021  *
00022  * The Original Code is: all of this file.
00023  *
00024  * Contributor(s): none yet.
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #ifndef NAN_INCLUDED_LOD_Quadric_h
00035 #define NAN_INCLUDED_LOD_Quadric_h
00036 
00037 #include "MT_Vector3.h"
00038 #include "MT_Matrix3x3.h"
00039 
00040 
00041 class LOD_Quadric {
00042 
00043 private:
00044     MT_Scalar a2, ab, ac, ad;
00045     MT_Scalar     b2, bc, bd;
00046     MT_Scalar         c2, cd;
00047     MT_Scalar             d2;
00048 
00049     void init(MT_Scalar a, MT_Scalar b, MT_Scalar c, MT_Scalar d);
00050 
00051 public:
00052 
00053     LOD_Quadric(
00054         ) {
00055                 Clear();
00056         };
00057 
00058     LOD_Quadric(
00059                 const MT_Vector3 & vec,
00060                 const MT_Scalar & offset
00061         ) {
00062                 a2 = vec[0] *vec[0];
00063                 b2 = vec[1] *vec[1];
00064                 c2 = vec[2] *vec[2];
00065 
00066                 ab = vec[0]*vec[1];
00067                 ac = vec[0]*vec[2];
00068                 bc = vec[1]*vec[2];
00069 
00070                 MT_Vector3 temp = vec*offset;
00071                 ad = temp[0];
00072                 bd = temp[1];
00073                 cd = temp[2];
00074 
00075                 d2 = offset*offset;
00076         };
00077 
00078                 MT_Matrix3x3 
00079         Tensor(
00080         ) const {
00081                 // return a symmetric matrix 
00082 
00083                 return MT_Matrix3x3(
00084                         a2,ab,ac,
00085                         ab,b2,bc,
00086                         ac,bc,c2
00087                 );
00088         };
00089 
00090 
00091                 MT_Vector3
00092         Vector(
00093         ) const {
00094                 return MT_Vector3(ad, bd, cd);
00095         };
00096 
00097                 void 
00098         Clear(
00099                 MT_Scalar val=0.0
00100         ) {
00101                 a2=ab=ac=ad=b2=bc=bd=c2=cd=d2=val;
00102         };
00103 
00104                 LOD_Quadric & 
00105         operator=(
00106                 const LOD_Quadric& Q
00107         ) {
00108 
00109                 a2 = Q.a2;  ab = Q.ab;  ac = Q.ac;  ad = Q.ad;
00110                                         b2 = Q.b2;  bc = Q.bc;  bd = Q.bd;
00111                                                                 c2 = Q.c2;  cd = Q.cd;  
00112                                                                                         d2 = Q.d2;
00113                 return *this;
00114         };
00115                 
00116                 LOD_Quadric& 
00117         operator+=(
00118                 const LOD_Quadric& Q
00119         ) {
00120                 a2 += Q.a2; ab += Q.ab;  ac += Q.ac;  ad += Q.ad;
00121                                         b2 += Q.b2;  bc += Q.bc;  bd += Q.bd;
00122                                                                  c2 += Q.c2;  cd += Q.cd;  
00123                                                                                           d2 += Q.d2;
00124                 return *this;
00125         };
00126 
00127                 LOD_Quadric& 
00128         operator*=(
00129                 const MT_Scalar & s
00130         ) {
00131                 a2 *= s; ab *= s;  ac *= s;  ad *= s;
00132                                         b2 *= s;  bc *= s;  bd *= s;
00133                                                                  c2 *= s;  cd *= s;  
00134                                                                                           d2 *= s;
00135                 return *this;
00136         };
00137 
00138 
00139                 MT_Scalar 
00140         Evaluate(
00141                 const MT_Vector3 &v
00142         ) const {
00143                 // compute the LOD_Quadric error
00144 
00145                 return v[0]*v[0]*a2 + 2*v[0]*v[1]*ab + 2*v[0]*v[2]*ac + 2*v[0]*ad
00146                   +v[1]*v[1]*b2 + 2*v[1]*v[2]*bc + 2*v[1]*bd
00147                   +v[2]*v[2]*c2   + 2*v[2]*cd
00148                   + d2;
00149         };
00150                 
00151                 bool 
00152         Optimize(
00153                 MT_Vector3& v
00154         ) const {
00155                 
00156                 MT_Scalar det = Tensor().determinant();
00157                 if (MT_fuzzyZero(det)) {
00158                         return false;
00159                 }
00160                 
00161                 v = -((Tensor().inverse()) * Vector());
00162                 return true;
00163         }; 
00164 
00165 };
00166 
00167 #endif
00168