Blender V2.61 - r43446

LOD_Quadric.h

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