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