| 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 | Compute 3D Delaunay tetrahedralization of a set of input points in 3D. |
| 26 | The tetrahedralization can be updated incrementally by adding new points. |
| 27 | */ |
| 28 | |
| 29 | #ifndef _DELAUNAYMESHER_H_ |
| 30 | #define _DELAUNAYMESHER_H_ |
| 31 | |
| 32 | #include <stddef.h> |
| 33 | #include <cassert> |
| 34 | #include <iostream> |
| 35 | #include <list> |
| 36 | #include <map> |
| 37 | #include <set> |
| 38 | #include <utility> |
| 39 | #include <vector> |
| 40 | #include "objMeshOctree.h" |
| 41 | #include "vec3d.h" |
| 42 | #include "vegalong.h" |
| 43 | #include "triangle.h" |
| 44 | #include "triple.h" |
| 45 | #include "tetMesh.h" |
| 46 | #include "query.h" |
| 47 | #include "tetKey.h" |
| 48 | #include "triKey.h" |
| 49 | |
| 50 | class TetMesh; |
| 51 | class VerticesQuery; |
| 52 | |
| 53 | class DelaunayMesher |
| 54 | { |
| 55 | public: |
| 56 | DelaunayMesher(); |
| 57 | virtual ~DelaunayMesher(); |
| 58 | |
| 59 | /*************************************************************************** |
| 60 | * Basic Functions for Delaunay Tetrahedralization * |
| 61 | ***************************************************************************/ |
| 62 | // compute Delaunay tetrahedralization of a set of input points |
| 63 | // must have points.size() >= 4 |
| 64 | // Query::Type: choice of precision used in the Delaunay mesher |
| 65 | // DOUBLE: double precision |
| 66 | // RATIONAL: high-precision arithmetics |
| 67 | // FILTERED: trade-off between DOUBLE and RATIONAL |
| 68 | // epsilon: internal accuracy parameter for queryType=FILTERED, on [0,1] |
| 69 | // the larger the value, the more computation will be done in high-precision arithmetics |
| 70 | // epsilon == 0: all computation in double precision |
| 71 | // epsilon == 1: all computation in high-precision |
| 72 | // returns true upon success, and false upon failure (degeneracy) |
| 73 | // use getMesh() to get the result |
| 74 | bool computeDelaunayTetrahedralization(const std::vector<Vec3d> & points, |
| 75 | double epsilon = 1e-9, Query::Type queryType = Query::FILTERED); |
| 76 | |
| 77 | // return the current Delaunay mesh |
| 78 | TetMesh * getMesh() const; |
| 79 | |
| 80 | // after computeDelaunayTetrahedralization, insert one additional point into the tetrahedralization |
| 81 | bool addOnePoint(const Vec3d & p); |
| 82 | size_t getNumVertices() const { return inputVertices.size(); } // get the total number of input vertices |
| 83 | const Vec3d & getVertex(int index) const { return inputVertices[index]; } // get the position of an input vertex |
| 84 | |
| 85 | // clear the mesh to empty (one can then call computeDelaunayTetrahedralization on a new input) |
| 86 | void clear(); |
| 87 | |
| 88 | // ********************************************************************************** |
| 89 | // All functionality below here mostly exists to support our isomesher and tetMesher |
| 90 | // We do not expect functions below here to be of general-purpose use |
| 91 | // ********************************************************************************** |
| 92 | |
| 93 | /*************************************************************************** |
| 94 | * Functions for CDT (Constrained Delaunay Tetrahedralization) * |
| 95 | * * |
| 96 | ***************************************************************************/ |
| 97 | |
| 98 | // initialize the Constrained Delaunay Tetrahedralization algorithm; to conform to the surface faces of "mesh" |
| 99 | // the tet mesh "mesh" must be manifold and free of self-intersections |
| 100 | // further calls to this->addOnePoint() will incrementally rebuild the tetrahedralization while preserving the constrained Delaunay property |
| 101 | bool initializeCDT(TetMesh * mesh, double epsilon = 1e-6, Query::Type queryType = Query::DOUBLE); |
| 102 | |
| 103 | // Must do remove outside and all infite balls first |
| 104 | int buildCDT(); |
| 105 | // get the surface mesh of the tet mesh "mesh" passed to initializeCDT (used in the Constrained Delaunay Tetrahedralization) |
| 106 | inline const ObjMesh * getCDTBoundaryMesh() const { return boundaryMesh; } |
| 107 | |
| 108 | /*************************************************************************** |
| 109 | * Voronoi Edge Query Functions * |
| 110 | ***************************************************************************/ |
| 111 | |
| 112 | // if input is true, DelaunayMesher will record Voronoi Edge update information for each subsequent operation (computeDelaunayTetrahedralization or addOnePoint) |
| 113 | // get last Voronoi Edge update by calling getDeletedVoronoiEdges() and getAddedVoronoiEdges |
| 114 | void computeVoronoiEdgeModification(bool compute = true) { computeVEdgeModification = compute; } |
| 115 | |
| 116 | // A struct for Voronoi Edge |
| 117 | // A Voronoi Edge is the line connecting two adjacent tet centers in Delaunay, or an infinite line starting at one border tet center, |
| 118 | // and pointing outside perpendicular to a border face on this tet |
| 119 | struct VoronoiEdge |
| 120 | { |
| 121 | bool finite; //whether this edge has finite length |
| 122 | Vec3d start; // start point of the edge |
| 123 | Vec3d end; // end point if it's not infinite, otherwise undefined |
| 124 | Vec3d direction; // direction of the edge if it's infinite, otherwise undefined |
| 125 | VoronoiEdge() : finite(true), start(0.0), end(0.0), direction(0.0) {} |
| 126 | VoronoiEdge(bool isFinite, const Vec3d & start, const Vec3d & other); |
| 127 | bool isFinite() const { return finite; } |
| 128 | }; |
| 129 | |
| 130 | typedef std::map<UTriKey, VoronoiEdge> VoronoiEdgeMap; |
| 131 | typedef VoronoiEdgeMap::iterator VEdgeIter; |
| 132 | typedef VoronoiEdgeMap::const_iterator VEdgeCIter; |
| 133 | |
| 134 | // vEdgeDeleted and vEdgeAdded will hold Voronoi edges only for last operation on Delaunay |
| 135 | // e.g. when addOnePoint() is called, vEdgeDeleted is reset and only stores the edges deleted by the last addOnePoint() call |
| 136 | const VoronoiEdgeMap & getDeletedVoronoiEdges() const { return vEdgeDeleted; } |
| 137 | const VoronoiEdgeMap & getAddedVoronoiEdges() const { return vEdgeAdded; } |
| 138 | |
| 139 | /*************************************************************************** |
| 140 | * Delaunay Ball Query Functions * |
| 141 | ***************************************************************************/ |
| 142 | |
| 143 | // Delaunay Balls are used for representing Delaunay |
| 144 | typedef vegaunsignedlong label_t; |
| 145 | struct DelaunayBall; |
| 146 | struct DelaunayBallCompare |
| 147 | { |
| 148 | bool operator() (const DelaunayBall * const & p1, const DelaunayBall * const & p2) const { return p1->label < p2->label; } |
| 149 | }; |
| 150 | |
| 151 | typedef std::set<DelaunayBall *, DelaunayBallCompare> BallSet; |
| 152 | typedef BallSet::iterator BallIter; |
| 153 | typedef BallSet::const_iterator BallCIter; |
| 154 | |
| 155 | // used to traverse all the balls |
| 156 | inline BallCIter getBallSetBegin() const { return balls.begin(); } |
| 157 | inline BallCIter getBallSetEnd() const { return balls.end(); } |
| 158 | |
| 159 | // used to traverse all the ballsToDelete |
| 160 | inline BallCIter getBallToDeleteSetBegin() const { return ballsToDelete.begin(); } |
| 161 | inline BallCIter getBallToDeletelSetEnd() const { return ballsToDelete.end(); } |
| 162 | |
| 163 | // used to traverse all the ballsAdded |
| 164 | inline BallCIter getBallAddedSetBegin() const { return ballsAdded.begin(); } |
| 165 | inline BallCIter getBallAddedSetEnd() const { return ballsAdded.end(); } |
| 166 | |
| 167 | // There're two kinds of Delaunay Balls: regular and infinite |
| 168 | // Regular ball (constructor input parameter v0 >= 0) is built by four vertices and represent a tetrahedron. |
| 169 | // Infinite ball (constructor input parameter v0 < 0) is a ball with infinite radius representing the half space separated by a border face of the Delaunay |
| 170 | // the other three vtx indices of one infinite ball give the border face triangle on the convex hull, their orientation points inside the convex hull |
| 171 | // Infinite ball's center is at infinity, on a line perpendicular to the border face and intersecting the face at its circumcenter |
| 172 | // In short, regular ball represents the space inside one tetrahedron, infinite ball represents the empty space outside one border face on the convex hull |
| 173 | struct DelaunayBall : public OTetKey |
| 174 | { |
| 175 | // get vertex of this ball |
| 176 | // if it's infinite ball, getVtx(0) returns -1 |
| 177 | inline int getVtx(int ind) const { return v[ind]; } |
| 178 | // get the array stores the 4 vtx indices |
| 179 | inline const int * getVertices() const {return v;} |
| 180 | // get the position of vertices |
| 181 | inline Vec3d getPosition(int ind) const { return parent.getVertex(v[ind]); } |
| 182 | |
| 183 | inline bool isRegular() const { return v[0] >= 0; } |
| 184 | inline bool isInfinite() const { return v[0] < 0; } |
| 185 | |
| 186 | // get ball center if it's regular ball |
| 187 | inline const Vec3d & getCenter() const { return center; } |
| 188 | |
| 189 | // for regular ball, return >0 for outside, ==0 for on the ball, <0 for inside |
| 190 | // for infinite ball, return >0 for on the inner side of the border face (or the plane the face lies on), ==0 for on the plane, <0 for on the outside of the border face |
| 191 | int contain(const Vec3d & p) const; |
| 192 | // Used only in CDT. Always return false for infinite ball |
| 193 | // For regular ball, if atleast one of the four ball vtx cannot be seen from p because the CDT boundary mesh obscures the sight, return false. Otherwise return true |
| 194 | // This can be tested by creating a line segment from p to one ball vtx and test intersection of this line segment with boundary mesh |
| 195 | // Notice that if the ball vtx is a mesh vtx of the boundary mesh, this does not counted as one valid intersection in this case |
| 196 | bool visibleTo(const Vec3d & p) const; |
| 197 | inline DelaunayBall * getNeighbor(const int ind) const { return nbr[ind]; } |
| 198 | |
| 199 | protected: |
| 200 | |
| 201 | // ==================== Member Vars ================================= |
| 202 | |
| 203 | const DelaunayMesher & parent; // pointer to DelaunayMesher |
| 204 | |
| 205 | Vec3d center; //for regular ball, the center |
| 206 | |
| 207 | // neighboring balls to this ball; there are always four neighbors |
| 208 | // regular ball: for every face of this tet, there is a neighboring tet across that face (may be an infinite ball) |
| 209 | // infinite ball: the border face (triangle) has three neighbors, one for each face edge; neighbors of the ball are the regular ball across the triangle, and the three infintie balls corresponding to the convex hull neighboring triangles |
| 210 | DelaunayBall * nbr[4]; |
| 211 | |
| 212 | label_t label; //label unique to every Delaunay Ball |
| 213 | |
| 214 | // ==================== End Member Vars ============================= |
| 215 | |
| 216 | DelaunayBall(int v0, int v1, int v2, int v3, DelaunayMesher * parent, label_t label); |
| 217 | |
| 218 | //return value: |
| 219 | //1 means point is outside the ball |
| 220 | //0 means point is on the ball |
| 221 | //-1 means point is inside the ball |
| 222 | int contains(int vtx) const; |
| 223 | |
| 224 | // A regular face is one face in Delaunay. Two Delaunay Balls (regular vs. regular, or regular vs. infinite) share one regular face |
| 225 | // An irregular face is one line segment (f.v[1], f.v[2]) on a face. |
| 226 | // Two infinite balls share an irregular face. This is actually two border faces share one triangle edge. |
| 227 | inline static bool isFaceRegular(const UTriKey & f) { return f[0] >= 0; } |
| 228 | inline static bool isFaceRegular(const OTriKey & f) { return f[0] >= 0; } |
| 229 | |
| 230 | // get the indices forming the infinite ball border face triangle |
| 231 | const int * getInfiniteBallTriangle() const { assert(!isRegular()); return v+1; } |
| 232 | |
| 233 | // if uface[i] == key, return i, otherwise return -1 |
| 234 | int getNeighborIndex(const UTriKey & key) const; |
| 235 | // if oface[i] == key, return i, otherwise return -1 |
| 236 | int getNeighborIndex(const OTriKey & key) const; |
| 237 | |
| 238 | inline int getVtxOpposeFace(const OTriKey & key) const { return v[getNeighborIndex(key)]; } |
| 239 | DelaunayBall * getNeighborByFace(const OTriKey & key) const { return nbr[getNeighborIndex(key)]; } |
| 240 | // if uface[i] == key, set DelaunayBall::nbr[i] to be nbr |
| 241 | void setNeighbor(const UTriKey & key, DelaunayBall * nbr); |
| 242 | |
| 243 | // get the Voronoi Edge connecting this Delaunay ball and one neighbor ball at nbr[nbrIndex] |
| 244 | VoronoiEdge getVoronoiEdge(int nbrIndex) const; |
| 245 | |
| 246 | friend class DelaunayMesher; |
| 247 | |
| 248 | friend std::ostream & operator << (std::ostream & o, const DelaunayBall & ball); |
| 249 | |
| 250 | }; |
| 251 | |
| 252 | // recover an edge by flipping, level means the maximal levels of recursion the recover process can have |
| 253 | int segmentRecoveryUsingFlip(const OEdgeKey & edge, int level); |
| 254 | // recover an edge by adding steiner points |
| 255 | int segmentRecoveryUsingSteinerPoint(const OEdgeKey & edge); |
| 256 | |
| 257 | // simply add a ball for the delaunayMesher, we won't check whether it is still a delaunay mesh |
| 258 | // v is an array of the four indices of the vertices in the delaunay mesh |
| 259 | DelaunayMesher::DelaunayBall * addBall(const int * v); |
| 260 | // simply remove a ball for the delaunayMesher, a hole may exist |
| 261 | void removeBall(DelaunayBall * ball); |
| 262 | |
| 263 | /*************************************************************************** |
| 264 | * Debugging Functions * |
| 265 | ***************************************************************************/ |
| 266 | |
| 267 | // check whether each tet satisfyies the Delaunay criterion by looping over every tet-vtx pair |
| 268 | // It's slow. For debugging purpose. |
| 269 | bool checkDelaunay() const; |
| 270 | |
| 271 | protected: |
| 272 | // First, the apex of the tet, second, the shared face of the tet, third, the pointer to the tet |
| 273 | typedef std::list<triple <int, OTriKey, DelaunayBall*> > TetAroundEdge; |
| 274 | |
| 275 | // Fliping a face, the face is shared by ball0 and ball1 |
| 276 | // ball0 and ball1 will be removed and three new balls will be added to the delaunay mesh |
| 277 | // It can return one of the three added balls by setting requestNewBallIdx and newBallAroundEdge |
| 278 | // By setting allowFlat, sometimes the three added balls may be flat and then removed by the next flip32. |
| 279 | int flip23(const OTriKey & face, DelaunayBall * ball0, DelaunayBall * ball1, int requestNewBallIdx = -1, DelaunayBall ** newBallAroundEdge = NULL, int allowFlat = -1); |
| 280 | // Fliping a edge, there must be 3 tets around the edge |
| 281 | int flip32(const OEdgeKey & edge, const TetAroundEdge & tetsAroundEdge); |
| 282 | |
| 283 | // remove an edge by flipping, level means the maximal levels of recursion |
| 284 | int segmentRemovalUsingFlip(const OEdgeKey & edge, int level = 1); |
| 285 | |
| 286 | |
| 287 | // Find two delaunay balls that have the face |
| 288 | int getTwoBallsByFace(const OTriKey & face, std::pair<DelaunayBall*, DelaunayBall *> & twoBalls); |
| 289 | |
| 290 | |
| 291 | // Find one delaunay ball that has the face |
| 292 | DelaunayBall * getOneBallByFace(const OTriKey & face); |
| 293 | |
| 294 | // Find tets that have the edge |
| 295 | int getTetsAroundEdge(const OEdgeKey & edge, TetAroundEdge & tetsAroundEdge); |
| 296 | |
| 297 | // Clear the counter of some tets |
| 298 | int clearCounter(const DelaunayMesher::TetAroundEdge & tetsAroundEdge); |
| 299 | |
| 300 | // The origin of the return ball should be start |
| 301 | int getOneBallBySegment(const int start, const int end); |
| 302 | |
| 303 | //void debug() const; |
| 304 | |
| 305 | // double getCheckContainingTime() const { return checkContainingWatch.getElapsedTime(); } |
| 306 | // double getUpdateTime() const { return updateWatch.getElapsedTime(); } |
| 307 | |
| 308 | DelaunayMesher::DelaunayBall * addBall(const int v0, const int v1, const int v2, const int v3); |
| 309 | //build geometry query object |
| 310 | void buildQuery(); |
| 311 | |
| 312 | // compute circumcenter of a tet |
| 313 | static Vec3d circumcenter(const Vec3d & a, const Vec3d & b, const Vec3d & c, const Vec3d & d); |
| 314 | |
| 315 | // get all the Delaunay balls containing the point getDelaunayVtx(i) |
| 316 | void getBallsContainingPoint(int i, BallSet & balls); |
| 317 | |
| 318 | // used to construct neighboring information for new Delaunay balls |
| 319 | // search face in neighboringStructure. If face is found, ball is neighbor to neighboringStructure[face], build related neighboring data |
| 320 | int buildNeighbor(const UTriKey & face, DelaunayBall * ball, std::map<UTriKey, DelaunayBall *> & neighboringStructure); |
| 321 | inline int buildNeighbor(int v0, int v1, int v2, DelaunayBall * ball, std::map<UTriKey, DelaunayBall *> & neighboringStructure) {return buildNeighbor(UTriKey(v0, v1, v2), ball, neighboringStructure);} |
| 322 | |
| 323 | // get only one ball containing the point getDelaunayVtx(i) |
| 324 | DelaunayBall * getOneBallContainingPoint(int i); |
| 325 | |
| 326 | DelaunayBall * createBall(int v0, int v1, int v2, int v3); |
| 327 | |
| 328 | //DelaunayMesher(const DelaunayMesher &); |
| 329 | |
| 330 | // initialize the first four vertices for Delaunay |
| 331 | // return false if failed |
| 332 | bool initialize(int a, int b, int c, int d); |
| 333 | |
| 334 | bool update(int i); // update Delaunay for the new point at index i |
| 335 | |
| 336 | Vec3d getFaceNormal(const OTriKey & face) const; |
| 337 | |
| 338 | static inline double det(const Vec3d & a, const Vec3d & b, const Vec3d & c) { return dot(a, cross(b, c)); } |
| 339 | |
| 340 | static bool isTetMeshFaceManifold(const TetMesh * tetMesh); // check whether any tet mesh face is at most shared by two tets |
| 341 | |
| 342 | double epsilon; |
| 343 | Query::Type queryType; |
| 344 | std::vector<Vec3d> inputVertices; |
| 345 | std::set<Vec3d> verticesSet; |
| 346 | |
| 347 | BallSet balls; |
| 348 | BallSet ballsToDelete; |
| 349 | BallSet ballsAdded; |
| 350 | |
| 351 | VerticesQuery * query; |
| 352 | |
| 353 | |
| 354 | std::map<UTriKey, DelaunayBall *> neighboringStructure; // Used for modifying balls |
| 355 | |
| 356 | // Look up by the index of the vertex, only return one of the balls that contains the vertex |
| 357 | std::vector <DelaunayBall*> vertex2ball; |
| 358 | |
| 359 | std::set <UEdgeKey> recoveredEdge; |
| 360 | |
| 361 | // ============ DEBUG CODE ==================== |
| 362 | // typedef std::map<label_t, DelaunayBall *> BallMap; |
| 363 | // typedef BallMap::iterator BallMapIter; |
| 364 | // typedef BallMap::const_iterator BallMapCIter; |
| 365 | // BallMap ballMap; |
| 366 | //// std::map<label_t, label_t> ballConnections; |
| 367 | // |
| 368 | // struct Operation { |
| 369 | // std::vector<label_t> deletedBalls; |
| 370 | // std::vector<label_t> addedBalls; |
| 371 | // }; |
| 372 | // |
| 373 | // std::vector<Operation> opStack; |
| 374 | // TetMeshManifold tetManifold; |
| 375 | // ============ END DEBUG CODE ================== |
| 376 | |
| 377 | bool computeVEdgeModification; |
| 378 | VoronoiEdgeMap vEdgeDeleted; |
| 379 | VoronoiEdgeMap vEdgeAdded; |
| 380 | |
| 381 | // data for CDT |
| 382 | ObjMesh * boundaryMesh; |
| 383 | //std::vector<UTriKey> boundaryTriangles; |
| 384 | ObjMeshOctree<TriangleBasic> * boundaryOctree; |
| 385 | |
| 386 | label_t nextBallLabel; |
| 387 | }; |
| 388 | |
| 389 | #endif |
| 390 |