-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathHalfedgeBuilder.cpp
427 lines (371 loc) · 14.8 KB
/
HalfedgeBuilder.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* ========================================================================= *
* *
* Luca Castelli Aleardi *
* Copyright (c) 2019, Ecole Polytechnique *
* Department of Computer Science *
* All rights reserved. *
* *
*---------------------------------------------------------------------------*
* This file is part of the course material developed for *
* INF574 Digital Representation and Analysis of Shapes (2019/20) *
* ========================================================================= */
// #include <bits/stdc++.h>
#include <chrono>
#ifndef HALFEDGE_DS_HEADER
#define HALFEDGE_DS_HEADER
#include "HalfedgeDS.cpp"
#include <unordered_map>
// #include <insertedHalfedges>
#endif
//using namespace Eigen;
//using namespace std;
struct VertexPair
{
int first, second;
VertexPair(const int &v1, const int &v2)
{
first = v1;
second = v2;
}
bool operator==(const VertexPair &p) const
{
if (first == p.first && second == p.second)
return true;
else if (first == p.second && second == p.first)
return true;
else
return false;
}
//friend ostream &operator<<(ostream &os, const VertexPair &dt);
};
/*ostream &operator<<(ostream &out, const VertexPair &p)
{
out << "(" << p.first;
out << "," << p.second << ")";
return out;
}*/
class VertexPairHashFunction
{
public:
// Use sum of lengths of first and last names
// as hash function.
size_t operator()(const VertexPair &p) const
{
return p.first * p.second;
}
};
/**
* @author Luca Castelli Aleardi (2019)
* This class provides functions for creating an array-based implementation of the ahlf-edge data structure
**/
class HalfedgeBuilder
{
public:
HalfedgeBuilder()
{
}
/**
* Efficient construction of the Half-edge DS for triangle meshes <br>
* <br>
* Remarks: <br>
* -) it works only for triangle meshes <br>
* -) it does not store the geometry (point locations) but only the combinatorial structure <br>
* -) this version does NOT store face/edge incidence relations
*/
HalfedgeDS createMesh(int nV, const MatrixXi &F)
{
int nF = F.rows(); // number of triangle faces
int nE; // total number of half-edges: including both inner and boundary edges
int nB; // number of boundary edges
int d = 3; // the degree is 3 for all faces (in a triangle mesh)
unordered_map<VertexPair, int, VertexPairHashFunction> insertedHalfedges; // allows to efficiently retrieve an inserted half-edge
cout << "Building an array-based implementation of an halfedge representation from a shared-vertex representation" << endl;
auto start = std::chrono::high_resolution_clock::now(); // for measuring time performances
// preliminary step: iterate over all faces to count inner and boundary edges (boundary edges are counted once)
cout << "Counting edges...";
int innerEdges = 0;
nE = 0;
for (int i = 0; i < nF; i++)
{
int edge;
for (int j = 0; j < d; j++)
{
MatrixXi f = F.row(i); // the incidence relations of the i-th face
VertexPair pair0(f(j), f((j + 1) % d));
//cout << "f" << i << ", " << counter << ": processing halfedge " << pair0;
edge = i * d + j;
if (insertedHalfedges.find(pair0) == insertedHalfedges.end())
{
insertedHalfedges[pair0] = edge;
nE = nE + 2; // count all half-edges: each edge must be counted twice
//cout << " inserted, " << insertedHalfedges.size() << endl;
}
else
innerEdges++; // count inner edges: they are shared by an edge
}
}
nB = (nE / 2) - innerEdges;
cout << "done" << endl;
cout << "\tn=" << nV << ", e=" << (nE / 2) << ", f=" << nF;
cout << ", \tboundary edges=" << nB << ", inner edges=" << innerEdges << endl;
HalfedgeDS result(nV, nE); // the resulting triangle mesh
int i;
int edgeCounter = 0;
//cout << "Setting the target vertex and the incident face...";
for (i = 0; i < nF; i++)
{
// creating and storing the 'd' half-edges in a face
for (int j = 0; j < d; j++)
{
// setting incident (target) vertex
MatrixXi f = F.row(i); // the incidence relations of the i-th face
result.setVertex(edgeCounter, f((j + 1) % d));
// setting the face contaning the hal-edge
result.setFace(edgeCounter, i);
edgeCounter++;
}
}
//cout << "done" << endl;
// setting incidence relations between half-edges in the same face
//cout << "Setting 'next' references...";
int indNext; // index of next half-edge
for (int j = 0; j < edgeCounter; j++)
{
if (j % 3 != 2) // only works for triangular meshes
indNext = (j + 1);
else
indNext = (j - 2);
result.setNext(j, indNext); // set the 'next' half-edge in the same face
result.setPrev(indNext, j); // set the 'previous' half-edge in the same face
}
//cout << "done" << endl;
// setting opposite half-edge
insertedHalfedges.clear(); // reset the hash map (the map must be empty)
int edge;
int nInsertedHalfedges = 0;
//cout << "Setting 'opposite' references...";
int counter = 0;
for (i = 0; i < nF; i++)
{
for (int j = 0; j < d; j++)
{
MatrixXi f = F.row(i); // the incidence relations of the i-th face
VertexPair pair0(f(j), f((j + 1) % d));
//cout << "f" << i << ", " << counter << ": processing halfedge " << pair0;
edge = i * d + j;
if (insertedHalfedges.find(pair0) == insertedHalfedges.end())
{
insertedHalfedges[pair0] = edge;
//cout << " inserted, " << insertedHalfedges.size() << endl;
nInsertedHalfedges++;
}
else
{
int eOpposite = insertedHalfedges[pair0];
//cout << " not inserted, opposite " << eOpposite << endl;
result.setOpposite(edge, eOpposite);
result.setOpposite(eOpposite, edge);
}
counter++;
}
}
//cout << "done (" << nInsertedHalfedges << " inserted edges)" << endl;
// for vertices: set the incident half-edge (having the vertex as target)
//cout << "Setting half-edges incident to edges...";
int vertex, oppPrev;
for (edge = 0; edge < nE; edge++)
{
vertex = result.getTarget(edge);
if (vertex != -1)
result.setEdge(vertex, edge);
}
//cout << "done" << endl;
if (nB > 0) // process boundary edges if any
addBoundaryEdges(result);
auto finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = finish - start;
std::cout << "Construction time: " << elapsed.count() << " s\n";
return result;
}
/**
* Efficient construction of the Half-edge DS for triangle meshes <br>
* <br>
* Remarks: <br>
* -) it works only for triangle meshes <br>
* -) it does not store the geometry (point locations) but only the combinatorial structure <br>
* -) this version stores face/edge incidence relations
*/
HalfedgeDS createMeshWithFaces(int nV, const MatrixXi &F)
{
int nF = F.rows(); // number of triangle faces
int nE; // total number of half-edges: including both inner and boundary edges
int nB; // number of boundary edges
int d = 3; // the degree is 3 for all faces (in a triangle mesh)
unordered_map<VertexPair, int, VertexPairHashFunction> insertedHalfedges; // allows to efficiently retrieve an inserted half-edge
cout << "Building an array-based implementation of an halfedge representation from a shared-vertex representation" << endl;
auto start = std::chrono::high_resolution_clock::now(); // for measuring time performances
// preliminary step: iterate over all faces to count inner and boundary edges (boundary edges are counted once)
cout << "Counting edges...";
int innerEdges = 0;
nE = 0;
for (int i = 0; i < nF; i++)
{
int edge;
for (int j = 0; j < d; j++)
{
MatrixXi f = F.row(i); // the incidence relations of the i-th face
VertexPair pair0(f(j), f((j + 1) % d));
//cout << "f" << i << ", " << counter << ": processing halfedge " << pair0;
edge = i * d + j;
if (insertedHalfedges.find(pair0) == insertedHalfedges.end())
{
insertedHalfedges[pair0] = edge;
nE = nE + 2; // count all half-edges: each edge must be counted twice
//cout << " inserted, " << insertedHalfedges.size() << endl;
}
else
innerEdges++; // count inner edges: they are shared by an edge
}
}
nB = (nE / 2) - innerEdges;
cout << "done" << endl;
cout << "\tn=" << nV << ", e=" << (nE / 2) << ", f=" << nF;
cout << ", \tboundary edges=" << nB << ", inner edges=" << innerEdges << endl;
HalfedgeDS result(nV, nE, nF); // the resulting triangle mesh
int i;
int edgeCounter = 0;
//cout << "Setting the target vertex and the incident face...";
for (i = 0; i < nF; i++)
{
// creating and storing the 'd' half-edges in a face
for (int j = 0; j < d; j++)
{
// setting incident (target) vertex
MatrixXi f = F.row(i); // the incidence relations of the i-th face
result.setVertex(edgeCounter, f((j + 1) % d));
result.setFace(edgeCounter, i); // setting the face contaning the halfedge
if (j == 0) // set the first halfedge incident to the face
result.setEdgeInFace(i, edgeCounter);
edgeCounter++;
}
}
//cout << "done" << endl;
// setting incidence relations between half-edges in the same face
//cout << "Setting 'next' references...";
int indNext; // index of next half-edge
for (int j = 0; j < edgeCounter; j++)
{
if (j % 3 != 2) // only works for triangular meshes
indNext = (j + 1);
else
indNext = (j - 2);
result.setNext(j, indNext); // set the 'next' half-edge in the same face
result.setPrev(indNext, j); // set the 'previous' half-edge in the same face
}
//cout << "done" << endl;
// setting opposite half-edge
insertedHalfedges.clear(); // reset the hash map (the map must be empty)
int edge;
int nInsertedHalfedges = 0;
//cout << "Setting 'opposite' references...";
int counter = 0;
for (i = 0; i < nF; i++)
{
for (int j = 0; j < d; j++)
{
MatrixXi f = F.row(i); // the incidence relations of the i-th face
VertexPair pair0(f(j), f((j + 1) % d));
//cout << "f" << i << ", " << counter << ": processing halfedge " << pair0;
edge = i * d + j;
if (insertedHalfedges.find(pair0) == insertedHalfedges.end())
{
insertedHalfedges[pair0] = edge;
//cout << " inserted, " << insertedHalfedges.size() << endl;
nInsertedHalfedges++;
}
else
{
int eOpposite = insertedHalfedges[pair0];
//cout << " not inserted, opposite " << eOpposite << endl;
result.setOpposite(edge, eOpposite);
result.setOpposite(eOpposite, edge);
}
counter++;
}
}
//cout << "done (" << nInsertedHalfedges << " inserted edges)" << endl;
// for vertices: set the incident half-edge (having the vertex as target)
//cout << "Setting half-edges incident to edges...";
int vertex, oppPrev;
for (edge = 0; edge < nE; edge++)
{
vertex = result.getTarget(edge);
if (vertex != -1)
result.setEdge(vertex, edge);
}
//cout << "done" << endl;
if (nB > 0) // process boundary edges if any
addBoundaryEdges(result);
auto finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = finish - start;
std::cout << "Construction time: " << elapsed.count() << " s\n";
return result;
}
private:
/**
* Add and set boundary half-edges to the representation
* Remark: it allows to deal with meshes having boundaries
*/
void addBoundaryEdges(HalfedgeDS mesh)
{
int nE = mesh.sizeOfHalfedges(); // total number of halfedges the data structure must store
list<int> boundaryEdges{}; // a list storing boundary edges to process
int nInsertedHalfedges = 0; // counts the number of inserted halfedges
int bCount = 0; // count the number of boundaries
int edgeCounter; // counter for enumerating half-edges
for (int e = 0; e < nE; e++) // store all boundary edges
{ // Remark: boundary edges are those for which the 'opposite' is not defined, but the 'next' is already defines
if (mesh.getOpposite(e) == -1 && mesh.getNext(e) != -1)
boundaryEdges.push_back(e);
}
edgeCounter = nE - boundaryEdges.size();
while (boundaryEdges.empty() == false) // set opposite references of all boundary edges
{
int firstEdge = boundaryEdges.front(); // get a boundary edge
boundaryEdges.pop_front(); // remove the boundary edge
int opFirstEdge = edgeCounter; // the index of the half-edge to be added
edgeCounter++; // increment the counter (for adding the new half-edge)
mesh.setOpposite(opFirstEdge, firstEdge);
mesh.setOpposite(firstEdge, opFirstEdge);
mesh.setVertex(opFirstEdge, mesh.getTarget(mesh.getNext((mesh.getNext(firstEdge)))));
mesh.setFace(opFirstEdge, -1); // not required: just for the sake of clarity
}
for (int e = 0; e < nE; e++) // iterate over all edges
{ // Remark: boundary edges are those for which the 'opposite' is not defined, but the 'next' is already defines
if (mesh.getFace(e) == -1)
{
int next = getNextBoundaryHalfedge(mesh, e);
mesh.setNext(e, next);
mesh.setPrev(next, e);
}
}
}
/**
* Given a boundary exterior half-edge exterior 'e', returns the next boundary half-edge around the same boundary cycle
* Warning: the mesh is supposed to be manifold (boundary cycles are disjoint)
*/
int getNextBoundaryHalfedge(HalfedgeDS he, int e)
{
if (he.getFace(e) != -1) // the starting edge must be a boundary (exterior) half-edge: its incidence face is not defined
return -1; // error
// turn around the target vertex of 'e', starting from the half-edge pEdge
int pEdge = he.getOpposite(e);
while (he.getFace(pEdge) != -1)
{
//System.out.print(" pEdge:"+printEdge(pEdge));
pEdge = he.getOpposite(he.getNext(he.getNext(pEdge)));
}
//System.out.println("next boundary edge:"+printEdge(pEdge));
return pEdge;
}
};