Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/mesher/tetMesher.h

raw symbols SCIP
1 /*************************************************************************
2 * *
3 * Vega FEM Simulation Library Version 3.1 *
4 * *
5 * "mesher" library , Copyright (C) 2016 USC *
6 * All rights reserved. *
7 * *
8 * Code authors: Danyong Zhao, Yijing Li, Jernej Barbic *
9 * http://www.jernejbarbic.com/code *
10 * *
11 * Funding: National Science Foundation *
12 * *
13 * This library is free software; you can redistribute it and/or *
14 * modify it under the terms of the BSD-style license that is *
15 * included with this library in the file LICENSE.txt *
16 * *
17 * This library is distributed in the hope that it will be useful, *
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *
20 * LICENSE.TXT for more details. *
21 * *
22 *************************************************************************/
23
24 /*
25 Implements a constrained 3D Delaunay tet mesher with refinement,
26 using the methods from:
27
28 Hang Si. Adaptive tetrahedral mesh generation by constrained Delaunay refinement.
29 International Journal for Numerical Methods in Engineering, 75:856–880, 2008.
30
31 Hang Si and Klaus Gartner. 3d boundary recovery by constrained delaunay tetrahedralization.
32 International Journal for Numerical Methods in Engineering, 85(11):1341–1364, 2011.
33
34 The input is a manifold and self-intersection-free triangle mesh,
35 and the output is a quality tet mesh of the volume enclosed by the input mesh
36 that conforms to the input mesh.
37 This mesh is generated using constrained Delaunay tetrahedralization,
38 and by inserting new vertices (``Steiner vertices'') into the mesh as needed
39 to satisy the quality conditions.
40
41 Note: The tet mesher is experimental. It may generate sliver tets with some inputs.
42 */
43
44 #ifndef _TETMESHER_H_
45 #define _TETMESHER_H_
46
47 #include <vector>
48 #include <set>
49 #include <queue>
50
51 #include "vec3d.h"
52 #include "objMesh.h"
53 #include "tetMesh.h"
54 #include "delaunayMesher.h"
55
56 class TetMesher
57 {
58 public:
59
60 TetMesher();
61 virtual ~TetMesher();
62
63 // compute the tet mesh, using constrained 3D Delaunay tetrahedralization, with refinement
64 // refinementQuality is a scalar; one must use refinementQuality >= 1
65 // the smaller the value, the more the mesh will be refined
66 // minDihedralAngle: the minimal dihedral angle in a tet on the result tet mesh should not be larger than "minDihedralAngle"
67 // maxSteinerVertices controls how many vertices are added in the refinement process. < 0 means no limitation.
68 // alpha: the smallest distance between a new Steiner vertex and the existing vertices should be larger than alpha times
69 // the average edge length of the input mesh
70 // maxTimeSeconds: the routine will terminate if this computation time (in seconds) is exceeded. < 0 means no limitation.
71 TetMesh * compute(ObjMesh * surfaceMesh, double refinementQuality = 1.1, double alpha = 2.0, double minDihedralAngle = 0.0,
72 int maxSteinerVertices = -1, double maxTimeSeconds = -1.0);
73
74 inline int getNumAddedSteinerVertices() const { return numSteinerVertices; }
75
76 protected:
77
78 typedef DelaunayMesher::DelaunayBall DelaunayBall;
79 typedef DelaunayMesher::BallCIter BallCIter;
80
81
82 // Delaunay ball with information of radius/minEdge and minimal dihedral angle
83 struct DelaunayBallWithRefineInfo : public DelaunayBall
84 {
85 double minDihedral; // Not valid for infinite ball. The minimal dihedral angel of the tetrahedral. Lazy compute
86 double edgeQuality; // Not valid for infinite ball. Square of the minimal edge of the tetrahedral. Lazy compute
87 DelaunayBallWithRefineInfo(const DelaunayBall & parent);
88 protected:
89 DelaunayBallWithRefineInfo(const DelaunayBallWithRefineInfo &);
90 };
91
92 // A set of delaunay balls with information of radius/minEdge and minimal dihedral angle
93 struct TetMeshWithRefineInfo
94 {
95 static DelaunayMesher::DelaunayBallCompare defaultComparor;
96 TetMeshWithRefineInfo(const DelaunayMesher & delaunayMesher);
97 ~TetMeshWithRefineInfo();
98
99 struct DelaunayBallEdgeRefineCMP // sort radius/minEdge descend
100 {
101 bool operator () (const DelaunayBallWithRefineInfo * const & p1, const DelaunayBallWithRefineInfo * const & p2) const;
102 };
103
104
105 struct DelaunayBallAngleRefineCMP // sort dihedral angel ascend
106 {
107 bool operator () (const DelaunayBallWithRefineInfo * const & p1, const DelaunayBallWithRefineInfo * const & p2) const;
108 };
109
110 typedef std::set<const DelaunayBallWithRefineInfo *, DelaunayBallEdgeRefineCMP>::const_iterator EdgeRefineIterator;
111 typedef std::set<const DelaunayBallWithRefineInfo *, DelaunayBallAngleRefineCMP>::const_iterator AngleRefineIterator;
112 inline EdgeRefineIterator getEdgeRefineBegin() { return edgeRefineBalls.begin(); }
113 inline EdgeRefineIterator getEdgeRefineEnd() { return edgeRefineBalls.end(); }
114 inline EdgeRefineIterator getAngleRefineBegin() { return angleRefineBalls.begin(); }
115 inline EdgeRefineIterator getAngleRefineEnd() { return angleRefineBalls.end(); }
116
117 // Get Tetmesh from the set of delaunayBall.
118 // Callee will alloc the memory and caller is responsible to free the memory
119 TetMesh * getTetMesh();
120
121 // Compute the refine information such as edge quality and dihedral angle
122 // Alloc memory and deep copy inside, free memory in deconstruc function and remove function
123 void insert(const DelaunayMesher::DelaunayBall * delaunayBall);
124
125 // Remove the delaunay ball that is removed in the last refinement. Free memory inside
126 void remove(const DelaunayMesher::DelaunayBall * delaunayBall);
127
128 // Enable the minimal dihedral angle refinement
129 inline void enableAngleRefine() { enabledAngleRefine = true; }
130 //friend class TetMesher;
131 protected:
132
133 bool enabledAngleRefine; // Whether the angle refinement is enabled
134 const DelaunayMesher & delaunayMesher; // Reference to the delaunayMesher, used to get the positions of vertices
135 std::set<const DelaunayBallWithRefineInfo *, DelaunayBallEdgeRefineCMP> edgeRefineBalls; // Sort the current delaunay ball by radius/minEdge descendingly
136 std::set<const DelaunayBallWithRefineInfo *, DelaunayBallAngleRefineCMP> angleRefineBalls; // Sort the current delaunay ball by diheral angle aescendingly
137 std::map<const DelaunayMesher::DelaunayBall *, EdgeRefineIterator, DelaunayMesher::DelaunayBallCompare> edgeRefineMap; // Look up the iterator in edgeRefineSet by pointer
138 std::map<const DelaunayMesher::DelaunayBall *, AngleRefineIterator, DelaunayMesher::DelaunayBallCompare> angleRefineMap; // Look up the iterator in angleRefineSet by pointer
139 } resultTetMesh;
140
141 // construct a constrained delaunay mesh, if recovery is true, the face recovery process will be done
142 int initializeCDT(bool recovery = true);
143 // refine a constrained delaunay mesh
144
145 /*
146 Refine one tet with bad edge quality, radius/minEdge
147 Ret value
148 0: refined
149 1: No tet to refine due to enough steiner points
150 2: No bad tet to refine
151 */
152 int refineEdge(double refinementQuality, const double minmimalDist);
153
154 /*
155 Refine one tet with bad dihedral angle, too small dihedral angle
156 Ret value
157 0: refined
158 1: No tet to refine due to enough steiner points
159 2: No bad tet to refine
160 */
161 int refineAngle(const double angleBound, const double minmimalDist);
162
163 // Make sure the first four vertices of the triangle mesh are not on the same plane
164 static bool renumberInitialVertices(ObjMesh * surfaceMesh);
165
166 //remove all tetrahedrons that are outside the constrained delaunay mesh
167 int removeOutside();
168
169 // insert a tet into the tetmesh
170 void insertTet(const int * v);
171 // remove a tet from the tetmesh
172 void removeTet(const int * v);
173
174 // insert a face into the tetmesh
175 void insertFace(int f1, int f2, int f3);
176 // remove a face from the tetmesh
177 void removeFace(int f1, int f2, int f3);
178 // insert an edge into the tetmesh
179 void insertEdge(int e1, int e2);
180 // remove an edge from the tetmesh
181 void removeEdge(int e1, int e2);
182
183 // get a face from the input triangular mesh
184 UTriKey getFace(int index);
185
186 // Do face recovery process
187 void faceRecovery();
188
189 // form a missing region by one face, the boundary of the missing face is on the current tet, the triangles in the region are all missing
190 void formRegion(int face, std::set<int> &region);
191 // calculate the boundary of a tet mesh, triangles that has only one neighbor tetrahedron
192 void calculateTetBoundary(std::vector<DelaunayMesher::DelaunayBall*> &tet, std::vector<UTriKey> &boundary);
193 // calculate the boundary of a missing region, the region consists of connected triangles, boundary is a set of edges that have only one neighbor triangle
194 void calculateTriangleBoundary(std::set<int> &missingRegion, std::set<std::pair<int, int> > &boundary);
195 // build a map for triangular mesh, which two triangles are the neigbhor of an edge
196 void buildTriangleNeighbor(std::vector<UTriKey> &mesh, std::map<std::pair<int, int>, std::pair<int, int> > &neighbor);
197 // insert the missing region into the cavity formed by removing all tetrahedrons that intersect the missing region, and then form two cavities
198 bool formTwoCavities(std::vector<UTriKey> &missingRegion, std::set<std::pair<int, int> >& boundary, std::map<std::pair<int, int>, std::pair<int, int> > &neighbor);
199 // fill a hole defined by a set of boundary triangles, compute what tetrahedrons need adding
200 bool fillHole(std::vector<UTriKey> &holeBoundary);
201
202 // determine whether the point is too close to any existent vertices in the current mesh
203 bool isTooCloseToOtherVertices(const Vec3d & r, const double minimalDist) const;
204
205 // Get the steiner point in segement recovery.
206 Vec3d getSteinerPoint(const UEdgeKey & edge, const bool isAcute1, const bool isAcute2, const double lfs1, const double lfs2, const double c = 2) const;
207
208 // segment recovery
209 int segmentRecovery();
210
211 // Used to compute delaunay mesh
212 DelaunayMesher delaunay;
213 // the input triangular mesh
214 ObjMesh * objMesh;
215
216 // the set of triangles that are in the input triangular mesh but not in the volumetric mesh
217 std::vector<UTriKey> lost;
218 // the indices of triangles that are in a missing region
219 std::set<int> region;
220
221 // for the input triangular mesh, which three triangles are the neigbhors of a triangle. All triangles are described by their indices
222 std::vector<std::vector<int> > neighborSurface;
223
224 // Tetradedrons added into the tet mesh to fill a hole
225 std::vector <std::vector<int> > toAdd;
226
227 // Triangles in the volumetric mesh, key is the triangle and value is how many tets are associated with the triangle
228 std::map<UTriKey, unsigned> trianglesInTet;
229
230 // flip the input objmesh to fit the delaunay mesh in case failure in the segment recovery
231 int flipSurface();
232
233 // edges in the volumetric mesh, key is the edge and value is how many tets are associated with the edge
234 std::map<UEdgeKey, unsigned> edgesInTet;
235
236 // the number of steiner points inserted (refinement)
237 int numSteinerVertices;
238
239 // recursion depth for face recovery;
240 int faceRecoveryDepth;
241 };
242
243 #endif /* TETMESHER_H_ */
244