-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.cpp
367 lines (281 loc) · 12.3 KB
/
mesh.cpp
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "mesh.h"
#include "math.h"
Mesh::Mesh() {
qDebug() << "✓✓ Mesh constructor (Empty)";
}
Mesh::Mesh(OBJFile* loadedOBJFile) {
qDebug() << "✓✓ Mesh constructor (OBJ)";
// Convert loaded OBJ file to HalfEdge mesh
unsigned int numVertices, numHalfEdges, numFaces;
unsigned int k, m, n;
numVertices = loadedOBJFile->vertexCoords.size();
numHalfEdges = 0;
for (k=0; k < static_cast<unsigned int>(loadedOBJFile->faceValences.size()); k++) {
numHalfEdges += loadedOBJFile->faceValences[k];
}
numFaces = loadedOBJFile->faceValences.size();
// Note - resize() invokes the Vertex() constructor, reserve() does not.
vertices.reserve(numVertices);
// If boundaries are present, reserve twice as much = worst case scenario
halfEdges.reserve(2*numHalfEdges);
faces.reserve(numFaces);
// Add Vertices
for (k = 0; k < numVertices; k++) {
// Coords (x,y,z), Out, Valence, Index
vertices.append(Vertex(loadedOBJFile->vertexCoords[k],
nullptr,
0,
k));
// Out and valence are unknown at this point.
}
qDebug() << " # Vertices" << vertices.capacity() << vertices.size();
unsigned int indexH = 0;
unsigned int currentIndex = 0;
// Initialize every entry of PotentialTwins with an empty QVector (using resize() )
//potentialTwins.resize(loadedOBJFile->vertexCoords.size());
QVector<QVector<unsigned int>> potentialTwins(loadedOBJFile->vertexCoords.size());
// Add Faces and most of the HalfEdges
for (m = 0; m < numFaces; m++) {
// Side, Val, Index
faces.append(Face(nullptr,
loadedOBJFile->faceValences[m],
m));
for (n = 0; n < loadedOBJFile->faceValences[m]; n++) {
// Target, Next, Prev, Twin, Poly, Index
halfEdges.append(HalfEdge(&vertices[loadedOBJFile->faceCoordInd[currentIndex+n]],
nullptr,
nullptr,
nullptr,
&faces[m],
indexH));
// Next, Prev and Twin of the above HalfEdge have to be assigned later! Starting below...
if (n > 0) {
halfEdges[indexH-1].next = &halfEdges[indexH];
halfEdges[indexH].prev = &halfEdges[indexH-1];
// Append index of HalfEdge to list of OutgoingHalfEdges of its TailVertex.
potentialTwins[loadedOBJFile->faceCoordInd[currentIndex+n-1]].append(indexH);
}
indexH++;
}
// HalfEdges[indexH-1] is the most recent addition.
faces[m].side = &halfEdges[indexH-1];
halfEdges[indexH-1].next = &halfEdges[indexH-n];
halfEdges[indexH-n].prev = &halfEdges[indexH-1];
potentialTwins[loadedOBJFile->faceCoordInd[currentIndex+n-1]].append(indexH-n);
currentIndex += loadedOBJFile->faceValences[m];
}
qDebug() << " # Faces" << faces.capacity() << faces.size();
qDebug() << " # HalfEdges" << halfEdges.capacity() << halfEdges.size();
// Outs and Valences of vertices
for (k = 0; k < vertices.size(); k++) {
if (potentialTwins[k].size() == 0) {
qWarning() << " ! Isolated Vertex? PotentialTwins empty for Index" << k;
dispVertInfo(k);
continue;
}
vertices[k].out = &halfEdges[potentialTwins[k][0]];
// Not the correct valence when on the boundary! Fixed below.
vertices[k].val = potentialTwins[k].size();
}
setTwins(numHalfEdges, indexH, potentialTwins);
qDebug() << " # Updated HalfEdges" << halfEdges.capacity() << halfEdges.size();
computeLimitMesh(*this);
computeQuadPatches(*this);
}
Mesh::~Mesh() {
qDebug() << "✗✗ Mesh destructor";
qDebug() << " # Vertices:" << vertices.size();
qDebug() << " # HalfEdges:" << halfEdges.size();
qDebug() << " # Faces:" << faces.size();
vertices.clear();
vertices.squeeze();
halfEdges.clear();
halfEdges.squeeze();
faces.clear();
faces.squeeze();
}
void Mesh::extractAttributes() {
HalfEdge* currentEdge;
vertexCoords.clear();
limitCoords.clear();
vertexCoords.reserve(vertices.size());
limitCoords.reserve(vertices.size());
for (int k = 0; k < vertices.size(); k++) {
vertexCoords.append(vertices[k].coords);
}
for (int k = 0; k < vertices.size(); ++k) {
limitCoords.append(vertices[k].limitCoords);
}
vertexNormals.clear();
vertexNormals.reserve(vertices.size());
limitNormals.clear();
limitNormals.reserve(vertices.size());
for (int k = 0; k < faces.size(); k++) {
setFaceNormal(&faces[k]);
}
for (int k = 0; k < vertices.size(); k++) {
vertexNormals.append( computeVertexNormal(&vertices[k]) );
limitNormals.append( computeLimitNormal(&vertices[k]) );
}
tessPatchIndices.clear();
tessPatchIndices.reserve(vertices.size() * 16);
for (int k = 0; k < tessPatches.size(); ++k) {
for (int c = 0; c < 16; ++c) {
tessPatchIndices.push_back(tessPatches[k].vertIndices[c]);
}
}
polyIndices.clear();
polyIndices.reserve(halfEdges.size() + faces.size());
for(int k = 0; k < faces.size(); k++) {
currentEdge = faces[k].side;
for (int m = 0; m < faces[k].val; m++) {
polyIndices.append(currentEdge->target->index);
currentEdge = currentEdge->next;
}
//append MAX_INT to signify end of face
polyIndices.append(INT_MAX);
}
}
void Mesh::setTwins(unsigned int numHalfEdges, unsigned int indexH, QVector<QVector<unsigned int>>& potentialTwins) {
unsigned int m, n;
unsigned int hTail, hHead, len;
QSet<unsigned int> twinless;
// Assign Twins
for (m = 0; m < numHalfEdges; m++) {
if (halfEdges[m].twin == nullptr) {
hTail = halfEdges[m].prev->target->index;
hHead = halfEdges[m].target->index;
len = halfEdges[m].target->val;
for(n = 0; n < len; n++) {
if (halfEdges[potentialTwins[hHead][n]].target->index == hTail) {
//qDebug() << "Found Twin!";
halfEdges[m].twin = &halfEdges[potentialTwins[hHead][n]];
halfEdges[potentialTwins[hHead][n]].twin = &halfEdges[m];
break;
}
}
if (n == len) {
// Twin not found...
twinless.insert(m);
}
}
}
if (twinless.size() > 0) {
qDebug() << " * There are" << twinless.size() << "HalfEdges without Twin (i.e. the model contains boundaries)";
// The mesh is not closed
//qDebug() << Twinless.values();
HalfEdge* initialEdge;
HalfEdge* currentEdge;
unsigned int startBoundaryLoop;
while (twinless.size() > 0) {
// Select a HalfEdge without Twin. The Twin that we will create is part of a boundary edge loop
qDebug() << " → Processing new Boundary Edge Loop";
initialEdge = &halfEdges[*twinless.begin()];
twinless.remove(initialEdge->index);
// Target, Next, Prev, Twin, Poly, Index
halfEdges.append(HalfEdge( initialEdge->prev->target,
nullptr,
nullptr,
initialEdge,
nullptr,
indexH ));
startBoundaryLoop = indexH;
// Twin of initialEdge should be assigned AFTER the central while loop!
indexH++;
// Use a sketch to properly understand these steps (assume counter-clockwise HalfEdges) :)
currentEdge = initialEdge->prev;
while (currentEdge->twin != nullptr) {
currentEdge = currentEdge->twin->prev;
}
// Trace the current boundary loop
while (currentEdge != initialEdge) {
twinless.remove(currentEdge->index);
// Target, Next, Prev, Twin, Poly, Index
halfEdges.append(HalfEdge( currentEdge->prev->target,
nullptr,
&halfEdges[indexH-1],
currentEdge,
nullptr,
indexH ));
halfEdges[indexH-1].next = &halfEdges[indexH];
currentEdge->target->val += 1;
currentEdge->twin = &halfEdges[indexH];
indexH++;
currentEdge = currentEdge->prev;
while (currentEdge->twin != nullptr) {
currentEdge = currentEdge->twin->prev;
}
}
halfEdges[startBoundaryLoop].prev = &halfEdges[indexH-1];
halfEdges[indexH-1].next = &halfEdges[startBoundaryLoop];
initialEdge->target->val += 1;
// Set Twin of initialEdge!
initialEdge->twin = &halfEdges[startBoundaryLoop];
}
}
}
void Mesh::setFaceNormal(Face* currentFace) {
QVector3D faceNormal;
QVector3D faceLimitNormal;
HalfEdge* currentEdge;
faceNormal = QVector3D();
faceLimitNormal = QVector3D();
currentEdge = currentFace->side;
for(int k = 0; k < currentFace->val; k++) {
faceNormal += QVector3D::crossProduct(
currentEdge->next->target->coords - currentEdge->target->coords,
currentEdge->twin->target->coords - currentEdge->target->coords );
faceLimitNormal += QVector3D::crossProduct(
currentEdge->next->target->limitCoords - currentEdge->target->limitCoords,
currentEdge->twin->target->limitCoords - currentEdge->target->limitCoords );
currentEdge = currentEdge->next;
}
currentFace->normal = faceNormal / faceNormal.length();
currentFace->limitNormal = faceLimitNormal / faceLimitNormal.length();
}
QVector3D Mesh::computeVertexNormal(Vertex* currentVertex) {
QVector3D vertexNormal;
HalfEdge* currentEdge;
float faceAngle;
vertexNormal = QVector3D();
currentEdge = currentVertex->out;
for (int k = 0; k < currentVertex->val; k++) {
faceAngle = acos( fmax(-1.0, QVector3D::dotProduct(
(currentEdge->target->coords - currentVertex->coords).normalized(),
(currentEdge->prev->twin->target->coords - currentVertex->coords).normalized() ) ) );
if (currentEdge->polygon) {
vertexNormal += faceAngle * currentEdge->polygon->normal;
}
currentEdge = currentEdge->twin->next;
}
return vertexNormal;
}
QVector3D Mesh::computeLimitNormal(Vertex* currentVertex) {
QVector3D vertexNormal;
HalfEdge* currentEdge;
float faceAngle;
vertexNormal = QVector3D();
currentEdge = currentVertex->out;
for (int k = 0; k < currentVertex->val; k++) {
faceAngle = acos( fmax(-1.0, QVector3D::dotProduct(
(currentEdge->target->limitCoords - currentVertex->limitCoords).normalized(),
(currentEdge->prev->twin->target->limitCoords - currentVertex->limitCoords).normalized() ) ) );
if (currentEdge->polygon) {
vertexNormal += faceAngle * currentEdge->polygon->limitNormal;
}
currentEdge = currentEdge->twin->next;
}
return vertexNormal;
}
void Mesh::dispVertInfo(unsigned short vertIndex) {
Vertex* dispVert = &vertices[vertIndex];
qDebug() << "Vertex at Index =" << dispVert->index << "Coords =" << dispVert->coords << "Out =" << dispVert->out << "Val =" << dispVert->val;
}
void Mesh::dispHalfEdgeInfo(unsigned short edgeIndex) {
HalfEdge* dispEdge = &halfEdges[edgeIndex];
qDebug() << "HalfEdge at Index =" << dispEdge->index << "Target =" << dispEdge->target << "Next =" << dispEdge->next << "Prev =" << dispEdge->prev << "Twin =" << dispEdge->twin << "Poly =" << dispEdge->polygon;
}
void Mesh::dispFaceInfo(unsigned short faceIndex){
Face* dispFace = &faces[faceIndex];
qDebug() << "Face at Index =" << dispFace->index << "Side =" << dispFace->side << "Val =" << dispFace->val;
}