Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
simplexify.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22
23#ifndef VULPES_GEOMETRY_SIMPLEXIFY_HPP_
24#define VULPES_GEOMETRY_SIMPLEXIFY_HPP_
25
26#include <memory>
27#include <vector>
28
29#include <Eigen/Core>
30
31#include <tetgen.h>
32#include <triangle.h>
33
36
37namespace vulpes {
38namespace geometry {
39
50 public:
55 using float_t = triangle::REAL;
60 using index_t = int32_t;
61 static constexpr int32_t dim = 2;
62 using PointType = const Eigen::Vector<float_t, dim>;
63 using PointsType = const Eigen::Matrix<float_t, dim, -1>;
64 using SimplexesType = const Eigen::Matrix<index_t, dim + 1, -1>;
65 using SubsimplexesType = const Eigen::Matrix<index_t, dim, -1>;
66 using PointsViewType = Eigen::Map<PointsType>;
67 using SimplexesViewType = Eigen::Map<SimplexesType>;
68 using SubsimplexesViewType = Eigen::Map<SubsimplexesType>;
69
70 static const triangle::behavior s_triangle_default_options;
71
77 static constexpr index_t s_buffer_size = 256;
78
86 explicit Triangulation(
88 triangle::behavior options = s_triangle_default_options);
95
101 index_t getNumTriangles() const { return m_out.numberoftriangles; }
107 index_t getNumPoints() const { return m_out.numberofpoints; }
116 const PointsViewType& getPointList() const { return *m_point_list; }
126
133
137 const auto& cell() const { return m_cell; }
138
139 protected:
144
148 struct triangle::triangulateio m_in;
152 struct triangle::triangulateio m_out;
153
158 std::vector<index_t> m_segmentlist;
163 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7,
164 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15,
165 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
166 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30,
167 30, 31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37,
168 38, 38, 39, 39, 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 45,
169 45, 46, 46, 47, 47, 48, 48, 49, 49, 50, 50, 51, 51, 52, 52,
170 53, 53, 54, 54, 55, 55, 56, 56, 57, 57, 58, 58, 59, 59, 60,
171 60, 61, 61, 62, 62, 63, 63, 64, 64, 65, 65, 66, 66, 67, 67,
172 68, 68, 69, 69, 70, 70, 71, 71, 72, 72, 73, 73, 74, 74, 75,
173 75, 76, 76, 77, 77, 78, 78, 79, 79, 80, 80, 81, 81, 82, 82,
174 83, 83, 84, 84, 85, 85, 86, 86, 87, 87, 88, 88, 89, 89, 90,
175 90, 91, 91, 92, 92, 93, 93, 94, 94, 95, 95, 96, 96, 97, 97,
176 98, 98, 99, 99, 100, 100, 101, 101, 102, 102, 103, 103, 104, 104, 105,
177 105, 106, 106, 107, 107, 108, 108, 109, 109, 110, 110, 111, 111, 112, 112,
178 113, 113, 114, 114, 115, 115, 116, 116, 117, 117, 118, 118, 119, 119, 120,
179 120, 121, 121, 122, 122, 123, 123, 124, 124, 125, 125, 126, 126, 127, 127,
180 0};
181
188 std::unique_ptr<PointsViewType> m_point_list;
195 std::unique_ptr<SimplexesViewType> m_triangle_list;
202 std::unique_ptr<SubsimplexesViewType> m_surface_segment_list;
203};
204
215 public:
220 using float_t = tetgen::REAL;
225 using index_t = int32_t;
226 static constexpr int32_t dim = 3;
227 using PointType = const Eigen::Vector<float_t, dim>;
228 using PointsType = const Eigen::Matrix<float_t, dim, -1>;
229 using SimplexesType = const Eigen::Matrix<index_t, dim + 1, -1>;
230 using SubsimplexesType = const Eigen::Matrix<index_t, dim, -1>;
231 using PointsViewType = Eigen::Map<PointsType>;
232 using SimplexesViewType = Eigen::Map<SimplexesType>;
233 using SubsimplexesViewType = Eigen::Map<SubsimplexesType>;
234
235 static const tetgen::tetgenbehavior s_tetgen_default_options;
240 static constexpr index_t s_static_buffer_size = 256;
241
255 explicit Tetrahedralization(
257 bool reconstruct_boundary = false,
258 tetgen::tetgenbehavior options = s_tetgen_default_options);
259
265 index_t getNumPoints() const { return m_out.numberofpoints; }
271 index_t getNumTetrahedra() const { return m_out.numberoftetrahedra; }
280 const PointsViewType& getPointList() const { return *m_point_list; }
290 return *m_tetrahedra_list;
291 }
292
296 const auto& cell() const { return m_cell; }
297
305 const auto getTriangleListFacet(index_t face_id) const {
306 return Eigen::Map<const Eigen::Matrix<index_t, dim, -1>>(
307 m_faces_subsimplexes.data() +
308 static_cast<std::ptrdiff_t>(
309 3 * m_num_subfaces_per_face_cumsum[face_id]),
310 3, m_num_subfaces_per_face[face_id]);
311 }
312
313 protected:
323
327 tetgen::tetgenio m_in;
331 tetgen::tetgenio m_out;
332
338 std::vector<tetgen::tetgenio::facet> m_facelist;
344 std::vector<tetgen::tetgenio::polygon> m_polygons;
350 std::vector<int32_t> m_facetmarkerlist;
355 static tetgen::tetgenio::facet s_facelist[s_static_buffer_size];
360 static tetgen::tetgenio::polygon s_polygons[s_static_buffer_size];
361
366 std::unique_ptr<PointsViewType> m_point_list;
371 std::unique_ptr<SimplexesViewType> m_tetrahedra_list;
372
376 std::vector<index_t> m_num_subfaces_per_face;
382 std::vector<index_t> m_num_subfaces_per_face_cumsum;
390 Eigen::Matrix<index_t, 3, -1> m_faces_subsimplexes;
391};
392
408template <
409 std::size_t Dim,
410 typename = std::enable_if_t<(Dim == 2) || (Dim == 3)>>
412 public:
421 std::conditional_t<Dim == 2, Triangulation, Tetrahedralization>;
422
423 using float_t = SimplexifyEngine::float_t;
424 using index_t = SimplexifyEngine::index_t;
425 static constexpr int32_t dim = SimplexifyEngine::dim;
426 using PointsType = SimplexifyEngine::PointsType;
427 using SimplexesType = SimplexifyEngine::SimplexesType;
428 using PointsViewType = SimplexifyEngine::PointsViewType;
429 using SimplexesViewType = SimplexifyEngine::SimplexesViewType;
430
441 template <typename T = void, typename = std::enable_if_t<Dim == 2, T>>
442 explicit Simplexify(
443 const geometry::Cell<2, float_t, index_t>& cell, bool /*unused*/ = false)
444 : m_engine(cell) {}
445
458 template <typename T = void, typename = std::enable_if_t<Dim == 3, T>>
459 explicit Simplexify(
461 bool reconstruct_boundaries = false)
462 : m_engine(cell, reconstruct_boundaries) {}
463
468 auto getSubsimplexesListFacet(index_t face_id) const {
469 if constexpr (Dim == 2) {
470 // this supposes that the first points in m_out are a copy of the points
471 // in m_in
472 return Eigen::Vector2i{face_id, (face_id + 1) % m_engine.cell().nfaces()};
473 }
474 if constexpr (Dim == 3) {
475 return m_engine.getTriangleListFacet(face_id);
476 }
477 };
478
484 index_t getNumPoints() const { return m_engine.getNumPoints(); }
485
496 if constexpr (Dim == 2) {
497 return m_engine.getNumTriangles();
498 } else if constexpr (Dim == 3) {
499 return m_engine.getNumTetrahedra();
500 } else {
501 static_assert(Dim * 0, "Not Implemented");
502 return -1;
503 }
504 }
505
514 const PointsViewType& getPointList() const { return m_engine.getPointList(); }
515
529 if constexpr (Dim == 2) {
530 return m_engine.getTriangleList();
531 } else if constexpr (Dim == 3) {
532 return m_engine.getTetrahedronList();
533 } else {
534 static_assert(Dim * 0, "Not Implemented");
535 }
536 }
537
538 protected:
540};
541
542} // namespace geometry
543} // namespace vulpes
544
545#endif // VULPES_GEOMETRY_SIMPLEXIFY_HPP_
Definition cell.hpp:98
Definition cell.hpp:151
const PointsViewType & getPointList() const
Gets the list of points in the simplexification.
Definition simplexify.hpp:514
const SimplexesViewType & getSimplexesList() const
Gets the list of simplices in the simplexification.
Definition simplexify.hpp:528
static constexpr int32_t dim
Definition simplexify.hpp:425
SimplexifyEngine::PointsType PointsType
Definition simplexify.hpp:426
index_t getNumPoints() const
Gets the number of points in the simplexification.
Definition simplexify.hpp:484
auto getSubsimplexesListFacet(index_t face_id) const
List the sub-simplexes (segments in 2D or triangles in 3D) that make a certain face in the simplixifi...
Definition simplexify.hpp:468
SimplexifyEngine::SimplexesType SimplexesType
Definition simplexify.hpp:427
SimplexifyEngine::SimplexesViewType SimplexesViewType
Definition simplexify.hpp:429
SimplexifyEngine m_engine
Definition simplexify.hpp:539
std::conditional_t< Dim==2, Triangulation, Tetrahedralization > SimplexifyEngine
The engine used for simplex generation, determined by the dimension.
Definition simplexify.hpp:420
SimplexifyEngine::float_t float_t
Definition simplexify.hpp:423
SimplexifyEngine::PointsViewType PointsViewType
Definition simplexify.hpp:428
index_t getNumSimplexes() const
Gets the number of simplices in the simplexification.
Definition simplexify.hpp:495
SimplexifyEngine::index_t index_t
Definition simplexify.hpp:424
Simplexify(const geometry::Cell< 3, float_t, index_t > &cell, bool reconstruct_boundaries=false)
Constructs a simplexification for the given geometric cell.
Definition simplexify.hpp:459
Simplexify(const geometry::Cell< 2, float_t, index_t > &cell, bool=false)
Constructs a simplexification for the given geometric cell.
Definition simplexify.hpp:442
const auto getTriangleListFacet(index_t face_id) const
Gets the list of triangles of the boundary of the tetrahedralization for a certain face.
Definition simplexify.hpp:305
Eigen::Matrix< index_t, 3, -1 > m_faces_subsimplexes
Triangles in each face of the cell.
Definition simplexify.hpp:390
Tetrahedralization(const geometry::Cell3D< float_t, index_t > &cell, bool reconstruct_boundary=false, tetgen::tetgenbehavior options=s_tetgen_default_options)
Constructs a Tetrahedralization object for a given 3D cell.
Definition simplexify.cpp:176
static tetgen::tetgenio::polygon s_polygons[s_static_buffer_size]
Static buffer for storing the list of polygons in the tetrahedralization.
Definition simplexify.hpp:360
tetgen::REAL float_t
Defines the floating-point type used in the TetGen library.
Definition simplexify.hpp:220
tetgen::tetgenio m_out
Output structure for the TetGen library.
Definition simplexify.hpp:331
Eigen::Map< SubsimplexesType > SubsimplexesViewType
Definition simplexify.hpp:233
Eigen::Map< SimplexesType > SimplexesViewType
Definition simplexify.hpp:232
std::vector< tetgen::tetgenio::facet > m_facelist
Dynamic buffer for storing the list of facets in the tetrahedralization. This is used only if the sta...
Definition simplexify.hpp:338
const PointsViewType & getPointList() const
Gets the list of points in the tetrahedralization.
Definition simplexify.hpp:280
const Eigen::Matrix< index_t, dim+1, -1 > SimplexesType
Definition simplexify.hpp:229
static constexpr int32_t dim
Definition simplexify.hpp:226
std::vector< index_t > m_num_subfaces_per_face_cumsum
Cumulative number of triangles in each face of the cell.
Definition simplexify.hpp:382
tetgen::tetgenio m_in
Input structure for the TetGen library.
Definition simplexify.hpp:327
const Eigen::Matrix< index_t, dim, -1 > SubsimplexesType
Definition simplexify.hpp:230
std::unique_ptr< SimplexesViewType > m_tetrahedra_list
Unique pointer to an Eigen::Map representing the list of tetrahedra in the tetrahedralization....
Definition simplexify.hpp:371
index_t getNumPoints() const
Gets the number of points in the tetrahedralization.
Definition simplexify.hpp:265
const geometry::Cell3D< float_t, index_t > & m_cell
The input 3D geometric cell for tetrahedralization.
Definition simplexify.hpp:322
const Eigen::Matrix< float_t, dim, -1 > PointsType
Definition simplexify.hpp:228
int32_t index_t
Defines the integer type used in the TetGen library.
Definition simplexify.hpp:225
std::vector< index_t > m_num_subfaces_per_face
Number of triangles in each face of the cell.
Definition simplexify.hpp:376
Eigen::Map< PointsType > PointsViewType
Definition simplexify.hpp:231
static tetgen::tetgenio::facet s_facelist[s_static_buffer_size]
Static buffer for storing the list of facets in the tetrahedralization.
Definition simplexify.hpp:355
const SimplexesViewType & getTetrahedronList() const
Gets the list of tetrahedra in the tetrahedralization.
Definition simplexify.hpp:289
const Eigen::Vector< float_t, dim > PointType
Definition simplexify.hpp:227
std::vector< tetgen::tetgenio::polygon > m_polygons
Dynamic buffer for storing the list of polygons in the tetrahedralization. This is used only if the s...
Definition simplexify.hpp:344
static const tetgen::tetgenbehavior s_tetgen_default_options
Definition simplexify.hpp:235
const auto & cell() const
Gets a reference to the cell.
Definition simplexify.hpp:296
static constexpr index_t s_static_buffer_size
Static constant defining the size of the static buffer for avoiding dynamic allocation of facet and p...
Definition simplexify.hpp:240
index_t getNumTetrahedra() const
Gets the number of tetrahedra in the tetrahedralization.
Definition simplexify.hpp:271
std::vector< int32_t > m_facetmarkerlist
Boundary markers are tags (integers) used mainly to identify which entities are associated with which...
Definition simplexify.hpp:350
std::unique_ptr< PointsViewType > m_point_list
Unique pointer to an Eigen::Map representing the list of points in the tetrahedralization....
Definition simplexify.hpp:366
void reconstructBoundaries()
Find out which faces (triangles) of the tetrahedrons that compose the tetrahedralization are on the b...
Definition simplexify.cpp:249
struct triangle::triangulateio m_out
Output structure for the Triangle library.
Definition simplexify.hpp:152
int32_t index_t
Defines the integer type used in the Triangle library.
Definition simplexify.hpp:60
const SimplexesViewType & getTriangleList() const
Gets the list of triangles in the triangulation.
Definition simplexify.hpp:125
std::unique_ptr< PointsViewType > m_point_list
Unique pointer to an Eigen::Map representing the list of points in the triangulation.
Definition simplexify.hpp:188
triangle::REAL float_t
Defines the floating-point type used in the Triangle library.
Definition simplexify.hpp:55
const PointsViewType & getPointList() const
Gets the list of points in the triangulation.
Definition simplexify.hpp:116
std::unique_ptr< SubsimplexesViewType > m_surface_segment_list
Unique pointer to an Eigen::Map representing the list of edges in the boundary of the triangulation.
Definition simplexify.hpp:202
index_t getNumTriangles() const
Gets the number of triangles in the triangulation.
Definition simplexify.hpp:101
const Eigen::Matrix< float_t, dim, -1 > PointsType
Definition simplexify.hpp:63
const auto & cell() const
Gets a reference to the cell.
Definition simplexify.hpp:137
std::vector< index_t > m_segmentlist
Dynamic buffer where we store the segment list. It is used only if the static buffer is not large eno...
Definition simplexify.hpp:158
static constexpr int32_t dim
Definition simplexify.hpp:61
Eigen::Map< SubsimplexesType > SubsimplexesViewType
Definition simplexify.hpp:68
index_t m_static_segmentlist[s_buffer_size]
Static buffer where we store the segment list.
Definition simplexify.hpp:162
std::unique_ptr< SimplexesViewType > m_triangle_list
Unique pointer to an Eigen::Map representing the list of triangles in the triangulation.
Definition simplexify.hpp:195
const geometry::Cell2D< float_t > & m_cell
The input 2D geometric cell for triangulation.
Definition simplexify.hpp:143
const Eigen::Matrix< index_t, dim+1, -1 > SimplexesType
Definition simplexify.hpp:64
Triangulation(const geometry::Cell2D< float_t > &cell, triangle::behavior options=s_triangle_default_options)
Constructs a Triangulation object for a given 2D cell.
Definition simplexify.cpp:47
const Eigen::Vector< float_t, dim > PointType
Definition simplexify.hpp:62
static constexpr index_t s_buffer_size
Static constant defining the size of the static buffer for avoiding dynamic allocation of segment lis...
Definition simplexify.hpp:77
Eigen::Map< SimplexesType > SimplexesViewType
Definition simplexify.hpp:67
struct triangle::triangulateio m_in
Input structure for the Triangle library.
Definition simplexify.hpp:148
~Triangulation()
Destructor for the Triangulation class.
Definition simplexify.cpp:132
const Eigen::Matrix< index_t, dim, -1 > SubsimplexesType
Definition simplexify.hpp:65
Eigen::Map< PointsType > PointsViewType
Definition simplexify.hpp:66
index_t getNumPoints() const
Gets the number of points in the triangulation.
Definition simplexify.hpp:107
const SubsimplexesViewType & getSurfaceSegmentList() const
Gets the list of edges of the boundary of the triangulation.
Definition simplexify.hpp:130
static const triangle::behavior s_triangle_default_options
Definition simplexify.hpp:70
Definition abs_agg_model.cpp:40
std::conditional_t< Dim==2, Cell2D< FloatType, IndexType >, Cell3D< FloatType, IndexType > > Cell
Definition cell.hpp:267
Definition cell_manager.hpp:38