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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/**
* $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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 *****
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "LOD_QuadricEditor.h"
#include "LOD_ExternNormalEditor.h"
// Creation
///////////
using namespace std;
LOD_QuadricEditor::
LOD_QuadricEditor(
LOD_ManMesh2 &mesh
) :
m_quadrics(NULL),
m_mesh(mesh)
{
};
LOD_QuadricEditor *
LOD_QuadricEditor::
New(
LOD_ManMesh2 &mesh
){
//same number of quadrics as vertices in the mesh
MEM_SmartPtr<LOD_QuadricEditor> output(new LOD_QuadricEditor(mesh));
if (output == NULL) {
return NULL;
}
return output.Release();
}
// Property editor interface
////////////////////////////
void
LOD_QuadricEditor::
Remove(
std::vector<LOD_VertexInd> &sorted_vertices
){
vector<LOD_Quadric> & quadrics = *m_quadrics;
vector<LOD_VertexInd>::const_iterator it_start = sorted_vertices.begin();
vector<LOD_VertexInd>::const_iterator it_end = sorted_vertices.end();
for (; it_start != it_end; ++it_start) {
if (quadrics.size() > 0) {
LOD_Quadric temp = quadrics[*it_start];
quadrics[*it_start] = quadrics.back();
quadrics.back() = temp;
quadrics.pop_back();
}
}
};
// Editor specific methods
//////////////////////////
bool
LOD_QuadricEditor::
BuildQuadrics(
LOD_ExternNormalEditor& normal_editor,
bool preserve_boundaries
){
if (m_quadrics != NULL) delete(m_quadrics);
m_quadrics =new vector<LOD_Quadric> (m_mesh.VertexSet().size());
if (m_quadrics == NULL) return false;
// iterate through the face set of the mesh
// compute a quadric based upon that face and
// add it to each of it's vertices quadrics.
const vector<LOD_TriFace> &faces = m_mesh.FaceSet();
const vector<LOD_Vertex> &verts = m_mesh.VertexSet();
vector<LOD_Edge> &edges = m_mesh.EdgeSet();
const vector<MT_Vector3> &normals = normal_editor.Normals();
vector<MT_Vector3>::const_iterator normal_it = normals.begin();
vector<LOD_TriFace>::const_iterator face_it = faces.begin();
vector<LOD_TriFace>::const_iterator face_end = faces.end();
vector<LOD_Quadric> & quadrics = *m_quadrics;
for (; face_it != face_end; ++face_it, ++normal_it) {
MT_Vector3 normal = *normal_it;
MT_Scalar offset = -normal.dot(verts[face_it->m_verts[0]].pos);
LOD_Quadric q(normal,offset);
quadrics[face_it->m_verts[0]] += q;
quadrics[face_it->m_verts[1]] += q;
quadrics[face_it->m_verts[2]] += q;
}
if (preserve_boundaries) {
// iterate through the edge set and add a boundary quadric to
// each of the boundary edges vertices.
vector<LOD_Edge>::const_iterator edge_it = edges.begin();
vector<LOD_Edge>::const_iterator edge_end = edges.end();
for (; edge_it != edge_end; ++edge_it) {
if (edge_it->BoundaryEdge()) {
// compute a plane perpendicular to the edge and the normal
// of the edges single polygon.
const MT_Vector3 & v0 = verts[edge_it->m_verts[0]].pos;
const MT_Vector3 & v1 = verts[edge_it->m_verts[1]].pos;
MT_Vector3 edge_vector = v1 - v0;
LOD_FaceInd edge_face = edge_it->OpFace(LOD_EdgeInd::Empty());
edge_vector = edge_vector.cross(normals[edge_face]);
if (!edge_vector.fuzzyZero()) {
edge_vector.normalize();
LOD_Quadric boundary_q(edge_vector, - edge_vector.dot(v0));
boundary_q *= 100;
quadrics[edge_it->m_verts[0]] += boundary_q;
quadrics[edge_it->m_verts[1]] += boundary_q;
}
}
}
}
// initiate the heap keys of the edges by computing the edge costs.
vector<LOD_Edge>::iterator edge_it = edges.begin();
vector<LOD_Edge>::const_iterator edge_end = edges.end();
for (; edge_it != edge_end; ++edge_it) {
MT_Vector3 target = TargetVertex(*edge_it);
LOD_Edge &e = *edge_it;
LOD_Quadric q0 = quadrics[e.m_verts[0]];
const LOD_Quadric &q1 = quadrics[e.m_verts[1]];
e.HeapKey() = -float(q0.Evaluate(target) + q1.Evaluate(target));
}
return true;
};
MT_Vector3
LOD_QuadricEditor::
TargetVertex(
LOD_Edge & e
){
// compute an edge contration target for edge ei
// this is computed by summing it's vertices quadrics and
// optimizing the result.
vector<LOD_Vertex> &verts = m_mesh.VertexSet();
vector<LOD_Quadric> &quadrics = *m_quadrics;
LOD_VertexInd v0 = e.m_verts[0];
LOD_VertexInd v1 = e.m_verts[1];
LOD_Quadric q0 = quadrics[v0];
q0 += quadrics[v1];
MT_Vector3 result;
if (q0.Optimize(result)) {
return result;
} else {
// the quadric was degenerate -> just take the average of
// v0 and v1
return ((verts[v0].pos + verts[v1].pos) * 0.5);
}
};
void
LOD_QuadricEditor::
ComputeEdgeCosts(
vector<LOD_EdgeInd> &edges
){
// for each we compute the target vertex and then compute
// the quadric error e = Q1(v') + Q2(v')
vector<LOD_Edge> &edge_set = m_mesh.EdgeSet();
vector<LOD_Quadric> &quadrics = *m_quadrics;
vector<LOD_EdgeInd>::const_iterator edge_it = edges.begin();
vector<LOD_EdgeInd>::const_iterator edge_end = edges.end();
for (; edge_it != edge_end; ++edge_it) {
MT_Vector3 target = TargetVertex(edge_set[*edge_it]);
LOD_Edge &e = edge_set[*edge_it];
LOD_Quadric q0 = quadrics[e.m_verts[0]];
const LOD_Quadric &q1 = quadrics[e.m_verts[1]];
e.HeapKey() = -float(q0.Evaluate(target) + q1.Evaluate(target));
}
};
|