fixed spacing in the headers to get rid of some warnings and some other
[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
34 #define NAN_INCLUDED_LOD_Quadric_h
35
36 #include "MT_Vector3.h"
37 #include "MT_Matrix3x3.h"
38
39
40 class LOD_Quadric {
41
42 private:
43     MT_Scalar a2, ab, ac, ad;
44     MT_Scalar     b2, bc, bd;
45     MT_Scalar         c2, cd;
46     MT_Scalar             d2;
47
48     void init(MT_Scalar a, MT_Scalar b, MT_Scalar c, MT_Scalar d);
49
50 public:
51
52     LOD_Quadric(
53         ) {
54                 Clear();
55         };
56
57     LOD_Quadric(
58                 const MT_Vector3 & vec,
59                 const MT_Scalar & offset
60         ) {
61                 a2 = vec[0] *vec[0];
62                 b2 = vec[1] *vec[1];
63                 c2 = vec[2] *vec[2];
64
65                 ab = vec[0]*vec[1];
66                 ac = vec[0]*vec[2];
67                 bc = vec[1]*vec[2];
68
69                 MT_Vector3 temp = vec*offset;
70                 ad = temp[0];
71                 bd = temp[1];
72                 cd = temp[2];
73
74                 d2 = offset*offset;
75         };
76
77                 MT_Matrix3x3 
78         Tensor(
79         ) const {
80                 // return a symmetric matrix 
81
82                 return MT_Matrix3x3(
83                         a2,ab,ac,
84                         ab,b2,bc,
85                         ac,bc,c2
86                 );
87         };
88
89
90                 MT_Vector3
91         Vector(
92         ) const {
93                 return MT_Vector3(ad, bd, cd);
94         };
95
96                 void 
97         Clear(
98                 MT_Scalar val=0.0
99         ) {
100                 a2=ab=ac=ad=b2=bc=bd=c2=cd=d2=val;
101         };
102
103                 LOD_Quadric & 
104         operator=(
105                 const LOD_Quadric& Q
106         ) {
107
108                 a2 = Q.a2;  ab = Q.ab;  ac = Q.ac;  ad = Q.ad;
109                                         b2 = Q.b2;  bc = Q.bc;  bd = Q.bd;
110                                                                 c2 = Q.c2;  cd = Q.cd;  
111                                                                                         d2 = Q.d2;
112                 return *this;
113         };
114                 
115                 LOD_Quadric& 
116         operator+=(
117                 const LOD_Quadric& Q
118         ) {
119                 a2 += Q.a2; ab += Q.ab;  ac += Q.ac;  ad += Q.ad;
120                                         b2 += Q.b2;  bc += Q.bc;  bd += Q.bd;
121                                                                  c2 += Q.c2;  cd += Q.cd;  
122                                                                                           d2 += Q.d2;
123                 return *this;
124         };
125
126                 LOD_Quadric& 
127         operator*=(
128                 const MT_Scalar & s
129         ) {
130                 a2 *= s; ab *= s;  ac *= s;  ad *= s;
131                                         b2 *= s;  bc *= s;  bd *= s;
132                                                                  c2 *= s;  cd *= s;  
133                                                                                           d2 *= s;
134                 return *this;
135         };
136
137
138                 MT_Scalar 
139         Evaluate(
140                 const MT_Vector3 &v
141         ) const {
142                 // compute the LOD_Quadric error
143
144                 return v[0]*v[0]*a2 + 2*v[0]*v[1]*ab + 2*v[0]*v[2]*ac + 2*v[0]*ad
145                   +v[1]*v[1]*b2 + 2*v[1]*v[2]*bc + 2*v[1]*bd
146                   +v[2]*v[2]*c2   + 2*v[2]*cd
147                   + d2;
148         };
149                 
150                 bool 
151         Optimize(
152                 MT_Vector3& v
153         ) const {
154                 
155                 MT_Scalar det = Tensor().determinant();
156                 if (MT_fuzzyZero(det)) {
157                         return false;
158                 }
159                 
160                 v = -((Tensor().inverse()) * Vector());
161                 return true;
162         }; 
163
164 };
165
166 #endif
167