-
Notifications
You must be signed in to change notification settings - Fork 11
/
geodesic_mesh.h
422 lines (366 loc) · 11.6 KB
/
geodesic_mesh.h
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
#ifndef GEODESIC_MESH
#define GEODESIC_MESH
#include "stdafx.h"
#include "geodesic_mesh_elements.h"
#include "geodesic_memory.h"
#include "geodesic_constants_and_simple_functions.h"
namespace geodesic{
class Mesh
{
public:
Mesh(){};
~Mesh(){};
template<class Points, class Faces>
void initialize_mesh_data(unsigned num_vertices,
Points& p,
unsigned num_faces,
Faces& tri); //build mesh from regular point-triangle representation
template<class Points, class Faces>
void initialize_mesh_data(Points& p, Faces& tri); //build mesh from regular point-triangle representation
std::vector<Vertex>& vertices(){return m_vertices;};
std::vector<Edge>& edges() {return m_edges;};
std::vector<Face>& faces() {return m_faces;};
private:
void build_adjacencies(); //build internal structure of the mesh
bool verify(); //verifies connectivity of the mesh and prints some debug info
typedef void* void_pointer;
void_pointer allocate_pointers(unsigned n)
{
return m_pointer_allocator.allocate(n);
}
std::vector<Vertex> m_vertices;
std::vector<Edge> m_edges;
std::vector<Face> m_faces;
SimlpeMemoryAllocator<void_pointer> m_pointer_allocator; //fast memory allocating for Face/Vertex/Edge cross-references
};
template<class Points, class Faces>
void Mesh::initialize_mesh_data(Points& p, Faces& tri) //build mesh from regular point-triangle representation
{
assert(p.size() % 3 == 0);
unsigned const num_vertices = p.size() / 3;
assert(tri.size() % 3 == 0);
unsigned const num_faces = tri.size() / 3;
initialize_mesh_data(num_vertices, p, num_faces, tri);
}
template<class Points, class Faces>
void Mesh::initialize_mesh_data(unsigned num_vertices,
Points& p,
unsigned num_faces,
Faces& tri)
{
unsigned const approximate_number_of_internal_pointers = (num_vertices + num_faces) * 4;
unsigned const max_number_of_pointer_blocks = 100;
m_pointer_allocator.reset(approximate_number_of_internal_pointers,
max_number_of_pointer_blocks);
m_vertices.resize(num_vertices);
for(unsigned i = 0; i < num_vertices; ++i) //copy coordinates to vertices
{
Vertex& v = m_vertices[i];
v.id() = i;
unsigned shift = 3 * i;
v.x() = p[shift];
v.y() = p[shift + 1];
v.z() = p[shift + 2];
}
m_faces.resize(num_faces);
for(unsigned i = 0; i < num_faces; ++i) //copy adjacent vertices to polygons/faces
{
Face& f = m_faces[i];
f.id() = i;
f.adjacent_vertices().set_allocation(allocate_pointers(3),3); //allocate three units of memory
unsigned shift = 3 * i;
for(unsigned j = 0; j < 3; ++j)
{
unsigned vertex_index = tri[shift + j];
//unsigned vertex_index = tri[shift + j] - 1;// The vertex index may start from 1
assert(vertex_index < num_vertices);
f.adjacent_vertices()[j] = &m_vertices[vertex_index];
}
}
build_adjacencies(); //build the structure of the mesh
}
inline void Mesh::build_adjacencies()
{
// Vertex->adjacent Faces
std::vector<unsigned> count(m_vertices.size()); //count adjacent vertices
for(unsigned i = 0; i < m_faces.size(); ++i)
{
Face& f = m_faces[i];
for(unsigned j = 0; j < 3; ++j)
{
unsigned vertex_id = f.adjacent_vertices()[j]->id();
assert(vertex_id < m_vertices.size());
count[vertex_id]++;
}
}
for(unsigned i = 0; i < m_vertices.size(); ++i) //reserve space
{
Vertex& v = m_vertices[i];
unsigned num_adjacent_faces = count[i];
v.adjacent_faces().set_allocation(allocate_pointers(num_adjacent_faces), //allocate three units of memory
num_adjacent_faces);
}
std::fill(count.begin(), count.end(), 0);
for(unsigned i = 0; i < m_faces.size(); ++i)
{
Face& f = m_faces[i];
for(unsigned j = 0; j < 3; ++j)
{
vertex_pointer v = f.adjacent_vertices()[j];
v->adjacent_faces()[count[v->id()]++] = &f;
}
}
//find all edges
//i.e. find all half-edges, sort and combine them into edges
std::vector<HalfEdge> half_edges(m_faces.size() * 3);
unsigned k = 0;
for(unsigned i = 0; i < m_faces.size(); ++i)
{
Face& f = m_faces[i];
for(unsigned j = 0; j < 3; ++j)
{
half_edges[k].face_id = i;
unsigned vertex_id_1 = f.adjacent_vertices()[j]->id();
unsigned vertex_id_2 = f.adjacent_vertices()[(j+1) % 3]->id();
half_edges[k].vertex_0 = std::min(vertex_id_1, vertex_id_2);
half_edges[k].vertex_1 = std::max(vertex_id_1, vertex_id_2);
k++;
}
}
std::sort(half_edges.begin(), half_edges.end());
unsigned number_of_edges = 1;
for(unsigned i = 1; i < half_edges.size(); ++i)
{
if(half_edges[i] != half_edges[i-1])
{
++number_of_edges;
}
else
{
if(i<half_edges.size()-1) //sanity check: there should be at most two equal half-edges
{ //if it fails, most likely the input data are messed up
assert(half_edges[i] != half_edges[i+1]);
}
}
}
// Edges->adjacent Vertices and Faces
m_edges.resize(number_of_edges);
unsigned edge_id = 0;
for(unsigned i = 0; i < half_edges.size();)
{
Edge& e = m_edges[edge_id];
e.id() = edge_id++;
e.adjacent_vertices().set_allocation(allocate_pointers(2),2); //allocate two units of memory
e.adjacent_vertices()[0] = &m_vertices[half_edges[i].vertex_0];
e.adjacent_vertices()[1] = &m_vertices[half_edges[i].vertex_1];
e.length() = e.adjacent_vertices()[0]->distance(e.adjacent_vertices()[1]);
assert(e.length() > 1e-100); //algorithm works well with non-degenerate meshes only
if(i != half_edges.size()-1 && half_edges[i] == half_edges[i+1]) //double edge
{
e.adjacent_faces().set_allocation(allocate_pointers(2),2);
e.adjacent_faces()[0] = &m_faces[half_edges[i].face_id];
e.adjacent_faces()[1] = &m_faces[half_edges[i+1].face_id];
i += 2;
}
else //single edge
{
e.adjacent_faces().set_allocation(allocate_pointers(1),1); //one adjucent faces
e.adjacent_faces()[0] = &m_faces[half_edges[i].face_id];
i += 1;
}
}
// Vertices->adjacent Edges
std::fill(count.begin(), count.end(), 0);
for(unsigned i = 0; i < m_edges.size(); ++i)
{
Edge& e = m_edges[i];
assert(e.adjacent_vertices().size()==2);
count[e.adjacent_vertices()[0]->id()]++;
count[e.adjacent_vertices()[1]->id()]++;
}
for(unsigned i = 0; i < m_vertices.size(); ++i)
{
m_vertices[i].adjacent_edges().set_allocation(allocate_pointers(count[i]),
count[i]);
}
std::fill(count.begin(), count.end(), 0);
for(unsigned i = 0; i < m_edges.size(); ++i)
{
Edge& e = m_edges[i];
for(unsigned j = 0; j < 2; ++j)
{
vertex_pointer v = e.adjacent_vertices()[j];
v->adjacent_edges()[count[v->id()]++] = &e;
}
}
// Faces->adjacent Edges
for(unsigned i = 0; i < m_faces.size(); ++i)
{
m_faces[i].adjacent_edges().set_allocation(allocate_pointers(3),3);
}
count.resize(m_faces.size());
std::fill(count.begin(), count.end(), 0);
for(unsigned i = 0; i < m_edges.size(); ++i)
{
Edge& e = m_edges[i];
for(unsigned j = 0; j < e.adjacent_faces().size(); ++j)
{
face_pointer f = e.adjacent_faces()[j];
assert(count[f->id()]<3);
f->adjacent_edges()[count[f->id()]++] = &e;
}
}
// compute angles for the faces
for(unsigned i = 0; i < m_faces.size(); ++i)
{
Face& f = m_faces[i];
double abc[3];
double sum = 0;
for(unsigned j = 0; j < 3; ++j) //compute angle adjacent to the vertex j
{
for(unsigned k = 0; k < 3; ++k)
{
vertex_pointer v = f.adjacent_vertices()[(j + k)%3];
abc[k] = f.opposite_edge(v)->length();
}
double angle = angle_from_edges(abc[0], abc[1], abc[2]);
assert(angle>1e-5); //algorithm works well with non-degenerate meshes only
if (angle < 1e-5)
std::cout<< "degenerate mesh" << std::endl;
f.corner_angles()[j] = angle;
sum += angle;
}
assert(std::abs(sum - M_PI) < 1e-5); //algorithm works well with non-degenerate meshes only
}
// define m_turn_around_flag for vertices
std::vector<double> total_vertex_angle(m_vertices.size());
for(unsigned i = 0; i < m_faces.size(); ++i)
{
Face& f = m_faces[i];
for(unsigned j = 0; j < 3; ++j)
{
vertex_pointer v = f.adjacent_vertices()[j];
total_vertex_angle[v->id()] += f.corner_angles()[j];
}
}
for(unsigned i = 0; i < m_vertices.size(); ++i)
{
Vertex& v = m_vertices[i];
v.saddle_or_boundary() = (total_vertex_angle[v.id()] > 2.0*M_PI - 1e-5);
}
for(unsigned i = 0; i < m_edges.size(); ++i)
{
Edge& e = m_edges[i];
if(e.is_boundary())
{
e.adjacent_vertices()[0]->saddle_or_boundary() = true;
e.adjacent_vertices()[1]->saddle_or_boundary() = true;
}
}
assert(verify());
}
inline bool Mesh::verify() //verifies connectivity of the mesh and prints some debug info
{
std::cout << std::endl;
// make sure that all vertices are mentioned at least once.
// though the loose vertex is not a bug, it most likely indicates that something is wrong with the mesh
std::vector<bool> map(m_vertices.size(), false);
for(unsigned i = 0; i < m_edges.size(); ++i)
{
edge_pointer e = &m_edges[i];
map[e->adjacent_vertices()[0]->id()] = true;
map[e->adjacent_vertices()[1]->id()] = true;
}
assert(std::find(map.begin(), map.end(), false) == map.end());
//make sure that the mesh is connected trough its edges
//if mesh has more than one connected component, it is most likely a bug
std::vector<face_pointer> stack(1,&m_faces[0]);
stack.reserve(m_faces.size());
map.resize(m_faces.size());
std::fill(map.begin(), map.end(), false);
map[0] = true;
while(!stack.empty())
{
face_pointer f = stack.back();
stack.pop_back();
for(unsigned i = 0; i < 3; ++i)
{
edge_pointer e = f->adjacent_edges()[i];
face_pointer f_adjacent = e->opposite_face(f);
if(f_adjacent && !map[f_adjacent->id()])
{
map[f_adjacent->id()] = true;
stack.push_back(f_adjacent);
}
}
}
assert(std::find(map.begin(), map.end(), false) == map.end());
//print some mesh statistics that can be useful in debugging
std::cout << "mesh has " << m_vertices.size()
<< " vertices, " << m_faces.size()
<< " faces, " << m_edges.size()
<< " edges\n";
unsigned total_boundary_edges = 0;
double longest_edge = 0;
double shortest_edge = 1e100;
for(unsigned i = 0; i < m_edges.size(); ++i)
{
Edge& e = m_edges[i];
total_boundary_edges += e.is_boundary() ? 1 : 0;
longest_edge = std::max(longest_edge, e.length());
shortest_edge = std::min(shortest_edge, e.length());
}
std::cout << total_boundary_edges << " edges are boundary edges\n";
std::cout << "shortest/longest edges are "
<< shortest_edge << "/"
<< longest_edge << " = "
<< shortest_edge/longest_edge
<< std::endl;
double minx = 1e100;
double maxx = -1e100;
double miny = 1e100;
double maxy = -1e100;
double minz = 1e100;
double maxz = -1e100;
for(unsigned i = 0; i < m_vertices.size(); ++i)
{
Vertex& v = m_vertices[i];
minx = std::min(minx, v.x());
maxx = std::max(maxx, v.x());
miny = std::min(miny, v.y());
maxy = std::max(maxy, v.y());
minz = std::min(minz, v.z());
maxz = std::max(maxz, v.z());
}
std::cout << "enclosing XYZ box:"
<<" X[" << minx << "," << maxx << "]"
<<" Y[" << miny << "," << maxy << "]"
<<" Z[" << minz << "," << maxz << "]"
<< std::endl;
double dx = maxx - minx;
double dy = maxy - miny;
double dz = maxz - minz;
std::cout << "approximate diameter of the mesh is "
<< sqrt(dx*dx + dy*dy + dz*dz)
<< std::endl;
double min_angle = 1e100;
double max_angle = -1e100;
for(unsigned i = 0; i < m_faces.size(); ++i)
{
Face& f = m_faces[i];
for(unsigned j = 0; j < 3; ++j)
{
double angle = f.corner_angles()[j];
min_angle = std::min(min_angle, angle);
max_angle = std::max(max_angle, angle);
}
}
std::cout << "min/max face angles are "
<< min_angle/M_PI*180.0 << "/"
<< max_angle/M_PI*180.0
<< " degrees\n";
std::cout << std::endl;
return true;
}
} //geodesic
#endif