TTK
Loading...
Searching...
No Matches
Mesh.h
Go to the documentation of this file.
1
12
13#pragma once
14
15// core includes
16#include <Triangulation.h>
17
18// local includes
19#include "FTRCommon.h"
20#include "FTRDataTypes.h"
21
22// c++ includes
23#include <vector>
24
25namespace ttk {
26 namespace ftr {
27 // bit field havind 2 value
28 // struct edgeScheme {
29 // unsigned int scheme:1;
30 // };
31 // keep the internal order of boundary vertices of an edge
32 // 0 : 0 1
33 // 1 : 1 0
34 using edgeScheme = char;
35
36 // bit field having 2^3 = 8 value.
37 // only 6 are used
38 //
39 // keep the internal order of edges composing a triangle
40 // 0 : 1 2 3
41 // 1 : 1 3 2
42 // 2 : 2 1 3
43 // 3 : 2 3 1
44 // 4 : 3 1 2
45 // 5 : 3 2 1
46 struct triScheme {
47 unsigned int scheme : 3;
48
49 triScheme() = default;
50
51 triScheme(const char u) : scheme(u) {
52 }
53
54 // direct access to data
55 operator char() const {
56 return scheme;
57 }
58 };
59
60 template <typename triangulationType>
61 class Mesh : public Allocable {
62 private:
63 triangulationType *tri_{};
64 idVertex nbVerts_{};
65 idEdge nbEdges_{};
66 idCell nbTriangles_{};
67 std::vector<edgeScheme> edgesSortId_{};
68 std::vector<triScheme> trianglesSortId_{};
69
70 public:
71 // Init
72 explicit Mesh(triangulationType *tri) : tri_{tri} {
73 }
74
75 Mesh() = default;
76
77 void setTriangulation(triangulationType *tri) {
78 tri_ = tri;
79 }
80
81 triangulationType *getTriangulation() {
82 return tri_;
83 }
84
85 void alloc() override {
86 edgesSortId_.resize(nbEdges_);
87 trianglesSortId_.resize(nbTriangles_);
88 }
89
90 void init() override {
91 }
92
93 void preprocess() {
94 tri_->preconditionVertexNeighbors();
95 tri_->preconditionVertexEdges();
96 tri_->preconditionVertexTriangles();
97 tri_->preconditionVertexStars();
98 tri_->preconditionTriangleEdges();
99
100 nbVerts_ = tri_->getNumberOfVertices();
101 nbEdges_ = tri_->getNumberOfEdges();
102 nbTriangles_ = tri_->getNumberOfTriangles();
103 }
104
105 // Facade Triangulation
106
108 return tri_->getDimensionality();
109 }
110
112 return nbVerts_;
113 }
114
115 inline idEdge getNumberOfEdges() const {
116 return nbEdges_;
117 }
118
120 return nbTriangles_;
121 }
122
123 inline idCell getNumberOfCells() const {
124 return tri_->getNumberOfCells();
125 }
126
128 return tri_->getVertexNeighborNumber(v);
129 }
130
131 inline void getVertexNeighbor(const idVertex v,
132 const idVertex local_v,
133 idVertex &res) {
134 tri_->getVertexNeighbor(v, local_v, res);
135 }
136
137 inline idEdge getVertexEdgeNumber(const idEdge e) const {
138 return tri_->getVertexEdgeNumber(e);
139 }
140
141 inline void getVertexEdge(const idVertex v,
142 const idEdge local_e,
143 idEdge &res) const {
144 tri_->getVertexEdge(v, local_e, res);
145 }
146
148 return tri_->getVertexTriangleNumber(v);
149 }
150
151 inline void getVertexTriangle(const idVertex v,
152 const idCell local_c,
153 idCell &res) const {
154 tri_->getVertexTriangle(v, local_c, res);
155 }
156
157 inline void getEdgeVertex(const idEdge e,
158 const char local_id,
159 idVertex &res) const {
160 tri_->getEdgeVertex(e, local_id, res);
161 }
162
163 inline idCell getEdgeTriangleNumber(const idEdge e) const {
164 return tri_->getEdgeTriangleNumber(e);
165 }
166
167 inline void getEdgeTriangle(const idEdge e,
168 const idCell local_t,
169 idCell &res) const {
170 tri_->getEdgeTriangle(e, local_t, res);
171 }
172
173 inline idEdge getTriangleEdgeNumber(const idCell t) const {
174 return tri_->getTriangleEdgeNumber(t);
175 }
176
177 inline void getTriangleEdge(const idCell t,
178 const char local_id,
179 idEdge &res) const {
180 tri_->getTriangleEdge(t, local_id, res);
181 }
182
183 // Extend Triangulation by pre-sorting scalars
184 // This is done by keeping id of the internal scheme of the simplex to
185 // reorder it instantly
186
187 // precompute
188
189 void preSortEdges(const VertCompFN &lowerThan);
190
191 void preSortTriangles(const VertCompFN &lowerThan);
192
193 // get simplex
194
196 const bool increasingOrder) const;
197
198 void getOrderedEdge(const idEdge e,
199 const bool increasingOrder,
200 orderedEdge &oEdge) const;
201
203 const bool increasingOrder) const;
204
205 void getOrderedTriangle(const idCell t,
206 const bool increasingOrder,
207 orderedTriangle &oTri) const;
208
209 bool compareEdges(const idEdge e0,
210 const idEdge e1,
211 const VertCompFN &lowerThan) const;
212
213 bool compareLinks(const linkEdge &l0,
214 const linkEdge &l1,
215 const VertCompFN &lowerTan) const;
216
217 // tools
218
219 std::string printEdges() const;
220
221 std::string printEdge(const idEdge e) const;
222 };
223
224 template <typename triangulationType>
226// Can't be parallel on a vector of bool !!
227#ifdef TTK_ENABLE_OPENMP
228#pragma omp parallel for schedule(static)
229#endif
230 for(idEdge ei = 0; ei < nbEdges_; ++ei) {
231 idVertex v0, v1;
232 tri_->getEdgeVertex(ei, 0, v0);
233 tri_->getEdgeVertex(ei, 1, v1);
234
235 edgesSortId_[ei] = lowerThan(v0, v1);
236 }
237 }
238
239 template <typename triangulationType>
240 void
242// require edges to be already sorted
243//
244// Not sure we can parallelie on a vector of bitfields
245#ifdef TTK_ENABLE_OPENMP
246#pragma omp parallel for schedule(static)
247#endif
248 for(idCell ti = 0; ti < nbTriangles_; ++ti) {
249 idEdge e0, e1, e2;
250 getTriangleEdge(ti, 0, e0);
251 getTriangleEdge(ti, 1, e1);
252 getTriangleEdge(ti, 2, e2);
253
254 if(compareEdges(e0, e1, lowerThan)) {
255 // 1 2 3
256 // 1 3 2
257 // 2 3 1
258 if(compareEdges(e1, e2, lowerThan)) {
259 // 0 : 1 2 3
260 trianglesSortId_[ti] = 0;
261 } else if(compareEdges(e0, e2, lowerThan)) {
262 // 1 : 1 3 2
263 trianglesSortId_[ti] = 1;
264 } else {
265 // 3 : 2 3 1
266 trianglesSortId_[ti] = 3;
267 }
268 } else {
269 // 2 1 3
270 // 3 1 2
271 // 3 2 1
272 if(compareEdges(e0, e2, lowerThan)) {
273 // 2 : 2 1 3
274 trianglesSortId_[ti] = 2;
275 } else if(compareEdges(e1, e2, lowerThan)) {
276 // 4 : 3 1 2
277 trianglesSortId_[ti] = 4;
278 } else {
279 // 5 : 3 2 1
280 trianglesSortId_[ti] = 5;
281 }
282 }
283 }
284 }
285
286 template <typename triangulationType>
288 const idEdge e, const bool increasingOrder) const {
289 idVertex v0, v1;
290 tri_->getEdgeVertex(e, 0, v0);
291 tri_->getEdgeVertex(e, 1, v1);
292 if(edgesSortId_[e] == static_cast<edgeScheme>(increasingOrder)) {
293 return std::make_tuple(v0, v1);
294 } else {
295 return std::make_tuple(v1, v0);
296 }
297 }
298
299 template <typename triangulationType>
301 const bool increasingOrder,
302 orderedEdge &oEdge) const {
303 idVertex v0, v1;
304 tri_->getEdgeVertex(e, 0, v0);
305 tri_->getEdgeVertex(e, 1, v1);
306 if(edgesSortId_[e] == static_cast<edgeScheme>(increasingOrder)) {
307 std::get<0>(oEdge) = v0;
308 std::get<1>(oEdge) = v1;
309 } else {
310 std::get<0>(oEdge) = v1;
311 std::get<1>(oEdge) = v0;
312 }
313 }
314
315 template <typename triangulationType>
317 const idCell ti, const bool increasingOrder) const {
318 idEdge e0, e1, e2;
319 getTriangleEdge(ti, 0, e0);
320 getTriangleEdge(ti, 1, e1);
321 getTriangleEdge(ti, 2, e2);
322
323 switch(trianglesSortId_[ti]) {
324 case 0:
325 if(increasingOrder)
326 return std::make_tuple(e0, e1, e2);
327 else
328 return std::make_tuple(e2, e1, e0);
329
330 case 1:
331 if(increasingOrder)
332 return std::make_tuple(e0, e2, e1);
333 else
334 return std::make_tuple(e1, e2, e0);
335
336 case 2:
337 if(increasingOrder)
338 return std::make_tuple(e1, e0, e2);
339 else
340 return std::make_tuple(e2, e0, e1);
341
342 case 3:
343 if(increasingOrder)
344 return std::make_tuple(e2, e0, e1);
345 else
346 return std::make_tuple(e1, e0, e2);
347
348 case 4:
349 if(increasingOrder)
350 return std::make_tuple(e1, e2, e0);
351 else
352 return std::make_tuple(e0, e2, e1);
353
354 case 5:
355 if(increasingOrder)
356 return std::make_tuple(e2, e1, e0);
357 else
358 return std::make_tuple(e0, e1, e2);
359 }
360
361#ifndef TTK_ENABLE_KAMIKAZE
362 std::cerr << "[Mesh]: error, unknownk case in triangleSortId_ : "
363 << static_cast<unsigned>(trianglesSortId_[ti]) << " at " << ti
364 << std::endl;
365#endif
366
367 return std::make_tuple(nullEdge, nullEdge, nullEdge);
368 }
369
370 template <typename triangulationType>
372 const idCell ti,
373 const bool increasingOrder,
374 orderedTriangle &oTriangle) const {
375 idEdge e0, e1, e2;
376 getTriangleEdge(ti, 0, e0);
377 getTriangleEdge(ti, 1, e1);
378 getTriangleEdge(ti, 2, e2);
379
380 switch(trianglesSortId_[ti]) {
381 case 0:
382 if(increasingOrder) {
383 std::get<0>(oTriangle) = e0;
384 std::get<1>(oTriangle) = e1;
385 std::get<2>(oTriangle) = e2;
386 } else {
387 std::get<0>(oTriangle) = e2;
388 std::get<1>(oTriangle) = e1;
389 std::get<2>(oTriangle) = e0;
390 }
391 break;
392
393 case 1:
394 if(increasingOrder) {
395 std::get<0>(oTriangle) = e0;
396 std::get<1>(oTriangle) = e2;
397 std::get<2>(oTriangle) = e1;
398 } else {
399 std::get<0>(oTriangle) = e1;
400 std::get<1>(oTriangle) = e2;
401 std::get<2>(oTriangle) = e0;
402 }
403 break;
404
405 case 2:
406 if(increasingOrder) {
407 std::get<0>(oTriangle) = e1;
408 std::get<1>(oTriangle) = e0;
409 std::get<2>(oTriangle) = e2;
410 } else {
411 std::get<0>(oTriangle) = e2;
412 std::get<1>(oTriangle) = e0;
413 std::get<2>(oTriangle) = e1;
414 }
415 break;
416
417 case 3:
418 if(increasingOrder) {
419 std::get<0>(oTriangle) = e2;
420 std::get<1>(oTriangle) = e0;
421 std::get<2>(oTriangle) = e1;
422 } else {
423 std::get<0>(oTriangle) = e1;
424 std::get<1>(oTriangle) = e0;
425 std::get<2>(oTriangle) = e2;
426 }
427 break;
428
429 case 4:
430 if(increasingOrder) {
431 std::get<0>(oTriangle) = e1;
432 std::get<1>(oTriangle) = e2;
433 std::get<2>(oTriangle) = e0;
434 } else {
435 std::get<0>(oTriangle) = e0;
436 std::get<1>(oTriangle) = e2;
437 std::get<2>(oTriangle) = e1;
438 }
439 break;
440
441 case 5:
442 if(increasingOrder) {
443 std::get<0>(oTriangle) = e2;
444 std::get<1>(oTriangle) = e1;
445 std::get<2>(oTriangle) = e0;
446 } else {
447 std::get<0>(oTriangle) = e0;
448 std::get<1>(oTriangle) = e1;
449 std::get<2>(oTriangle) = e2;
450 }
451 break;
452 default:
453#ifndef TTK_ENABLE_KAMIKAZE
454 std::cerr << "[Mesh]: error, unknownk case in triangleSortId_ : "
455 << static_cast<unsigned>(trianglesSortId_[ti]) << " at "
456 << ti << std::endl;
457#endif
458 break;
459 }
460 }
461
462 // tools
463
464 template <typename triangulationType>
466 const idEdge e0, const idEdge e1, const VertCompFN &lowerThan) const {
467 idVertex e0v0, e0v1;
468 if(edgesSortId_[e0] == 0) {
469 getEdgeVertex(e0, 0, e0v0);
470 getEdgeVertex(e0, 1, e0v1);
471 } else {
472 getEdgeVertex(e0, 1, e0v0);
473 getEdgeVertex(e0, 0, e0v1);
474 }
475
476 idVertex e1v0, e1v1;
477 if(edgesSortId_[e1] == 0) {
478 getEdgeVertex(e1, 0, e1v0);
479 getEdgeVertex(e1, 1, e1v1);
480 } else {
481 getEdgeVertex(e1, 1, e1v0);
482 getEdgeVertex(e1, 0, e1v1);
483 }
484
485 if(e0v0 == e1v0)
486 return lowerThan(e0v1, e1v1);
487 return lowerThan(e0v0, e1v0);
488 }
489
490 template <typename triangulationType>
491 bool
493 const linkEdge &l1,
494 const VertCompFN &lowerThan) const {
495 if(std::get<0>(l0) == std::get<0>(l1)) {
496 return compareEdges(std::get<1>(l0), std::get<1>(l1), lowerThan);
497 }
498 return compareEdges(std::get<0>(l0), std::get<0>(l1), lowerThan);
499 }
500
501 template <typename triangulationType>
503 std::stringstream res;
504 res << " edges: " << std::endl;
505 for(idEdge i = 0; i < nbEdges_; ++i) {
506 idVertex v0, v1;
507 getEdgeVertex(i, 0, v0);
508 getEdgeVertex(i, 1, v1);
509 res << i << " : " << v0 << " " << v1 << std::endl;
510 }
511
512 return res.str();
513 }
514
515 template <typename triangulationType>
516 std::string Mesh<triangulationType>::printEdge(const idEdge e) const {
517 std::stringstream res;
518 idVertex v0, v1;
519 getEdgeVertex(e, 0, v0);
520 getEdgeVertex(e, 1, v1);
521 res << v0 << " " << v1;
522 return res.str();
523 }
524
525 } // namespace ftr
526} // namespace ttk
TTK FTRGraph mesh related operations.
Definition Mesh.h:61
bool compareEdges(const idEdge e0, const idEdge e1, const VertCompFN &lowerThan) const
Definition Mesh.h:465
orderedTriangle getOrderedTriangle(const idCell t, const bool increasingOrder) const
Definition Mesh.h:316
idEdge getNumberOfEdges() const
Definition Mesh.h:115
std::string printEdges() const
Definition Mesh.h:502
idEdge getVertexEdgeNumber(const idEdge e) const
Definition Mesh.h:137
void setTriangulation(triangulationType *tri)
Definition Mesh.h:77
void getVertexTriangle(const idVertex v, const idCell local_c, idCell &res) const
Definition Mesh.h:151
void getTriangleEdge(const idCell t, const char local_id, idEdge &res) const
Definition Mesh.h:177
Mesh()=default
void getVertexNeighbor(const idVertex v, const idVertex local_v, idVertex &res)
Definition Mesh.h:131
idVertex getVertexNeighborNumber(const idVertex v) const
Definition Mesh.h:127
std::string printEdge(const idEdge e) const
Definition Mesh.h:516
idCell getNumberOfTriangles() const
Definition Mesh.h:119
void preprocess()
Definition Mesh.h:93
SimplexId getDimensionality() const
Definition Mesh.h:107
void preSortTriangles(const VertCompFN &lowerThan)
Definition Mesh.h:241
idCell getVertexTriangleNumber(const idVertex v) const
Definition Mesh.h:147
Mesh(triangulationType *tri)
Definition Mesh.h:72
bool compareLinks(const linkEdge &l0, const linkEdge &l1, const VertCompFN &lowerTan) const
Definition Mesh.h:492
void getVertexEdge(const idVertex v, const idEdge local_e, idEdge &res) const
Definition Mesh.h:141
idVertex getNumberOfVertices() const
Definition Mesh.h:111
orderedEdge getOrderedEdge(const idEdge e, const bool increasingOrder) const
Definition Mesh.h:287
void alloc() override
Definition Mesh.h:85
idEdge getTriangleEdgeNumber(const idCell t) const
Definition Mesh.h:173
void preSortEdges(const VertCompFN &lowerThan)
Definition Mesh.h:225
void init() override
Definition Mesh.h:90
triangulationType * getTriangulation()
Definition Mesh.h:81
idCell getEdgeTriangleNumber(const idEdge e) const
Definition Mesh.h:163
void getEdgeTriangle(const idEdge e, const idCell local_t, idCell &res) const
Definition Mesh.h:167
idCell getNumberOfCells() const
Definition Mesh.h:123
void getEdgeVertex(const idEdge e, const char local_id, idVertex &res) const
Definition Mesh.h:157
std::tuple< idVertex, idVertex > orderedEdge
Edge represented by its 2 vertices, lower then upper.
std::function< bool(const idVertex, const idVertex)> VertCompFN
std::tuple< idEdge, idEdge, idEdge > orderedTriangle
Triangle represented by its 3 edges Edges are sorted by their starting vertex (see orderedEdge)
SimplexId idCell
Cell index in vect_cellList_.
char edgeScheme
Definition Mesh.h:34
SimplexId idVertex
Vertex index in scalars_.
SimplexId idEdge
Edge index in vect_edgeList_.
std::pair< idEdge, idEdge > linkEdge
The Topology ToolKit.
int SimplexId
Identifier type for simplices of any dimension.
Definition DataTypes.h:22
unsigned int scheme
Definition Mesh.h:47
triScheme(const char u)
Definition Mesh.h:51