Reverting change to decimation to fix compatibility with
[blender.git] / intern / decimation / intern / LOD_Quadric.h
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 #ifndef NAN_INCLUDED_LOD_Quadric_h
33 #define NAN_INCLUDED_LOD_Quadric_h
34
35 #include "MT_Vector3.h"
36 #include "MT_Matrix3x3.h"
37
38
39 class LOD_Quadric {
40
41 private:
42     MT_Scalar a2, ab, ac, ad;
43     MT_Scalar     b2, bc, bd;
44     MT_Scalar         c2, cd;
45     MT_Scalar             d2;
46
47     void init(MT_Scalar a, MT_Scalar b, MT_Scalar c, MT_Scalar d);
48
49 public:
50
51     LOD_Quadric(
52         ) {
53                 Clear();
54         };
55
56     LOD_Quadric(
57                 const MT_Vector3 & vec,
58                 const MT_Scalar & offset
59         ) {
60                 a2 = vec[0] *vec[0];
61                 b2 = vec[1] *vec[1];
62                 c2 = vec[2] *vec[2];
63
64                 ab = vec[0]*vec[1];
65                 ac = vec[0]*vec[2];
66                 bc = vec[1]*vec[2];
67
68                 MT_Vector3 temp = vec*offset;
69                 ad = temp[0];
70                 bd = temp[1];
71                 cd = temp[2];
72
73                 d2 = offset*offset;
74         };
75
76                 MT_Matrix3x3 
77         Tensor(
78         ) const {
79                 // return a symmetric matrix 
80
81                 return MT_Matrix3x3(
82                         a2,ab,ac,
83                         ab,b2,bc,
84                         ac,bc,c2
85                 );
86         };
87
88
89                 MT_Vector3
90         Vector(
91         ) const {
92                 return MT_Vector3(ad, bd, cd);
93         };
94
95                 void 
96         Clear(
97                 MT_Scalar val=0.0
98         ) {
99                 a2=ab=ac=ad=b2=bc=bd=c2=cd=d2=val;
100         };
101
102                 LOD_Quadric & 
103         operator=(
104                 const LOD_Quadric& Q
105         ) {
106
107                 a2 = Q.a2;  ab = Q.ab;  ac = Q.ac;  ad = Q.ad;
108                                         b2 = Q.b2;  bc = Q.bc;  bd = Q.bd;
109                                                                 c2 = Q.c2;  cd = Q.cd;  
110                                                                                         d2 = Q.d2;
111                 return *this;
112         };
113                 
114                 LOD_Quadric& 
115         operator+=(
116                 const LOD_Quadric& Q
117         ) {
118                 a2 += Q.a2; ab += Q.ab;  ac += Q.ac;  ad += Q.ad;
119                                         b2 += Q.b2;  bc += Q.bc;  bd += Q.bd;
120                                                                  c2 += Q.c2;  cd += Q.cd;  
121                                                                                           d2 += Q.d2;
122                 return *this;
123         };
124
125                 LOD_Quadric& 
126         operator*=(
127                 const MT_Scalar & s
128         ) {
129                 a2 *= s; ab *= s;  ac *= s;  ad *= s;
130                                         b2 *= s;  bc *= s;  bd *= s;
131                                                                  c2 *= s;  cd *= s;  
132                                                                                           d2 *= s;
133                 return *this;
134         };
135
136
137                 MT_Scalar 
138         Evaluate(
139                 const MT_Vector3 &v
140         ) const {
141                 // compute the LOD_Quadric error
142
143                 return v[0]*v[0]*a2 + 2*v[0]*v[1]*ab + 2*v[0]*v[2]*ac + 2*v[0]*ad
144                   +v[1]*v[1]*b2 + 2*v[1]*v[2]*bc + 2*v[1]*bd
145                   +v[2]*v[2]*c2   + 2*v[2]*cd
146                   + d2;
147         };
148                 
149                 bool 
150         Optimize(
151                 MT_Vector3& v
152         ) const {
153                 
154                 MT_Scalar det = Tensor().determinant();
155                 if (MT_fuzzyZero(det)) {
156                         return false;
157                 }
158                 
159                 v = -((Tensor().inverse()) * Vector());
160                 return true;
161         }; 
162
163 };
164
165 #endif
166