| 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 a quality triangular mesh of a distance field isosurface (level set), |
| 26 | using the following paper: |
| 27 | |
| 28 | Steve Oudot, Laurent Rineau, Mariette Yvinec: |
| 29 | Meshing Volumes Bounded by Smooth Surfaces |
| 30 | Proceedings of the 14th International Meshing Roundtable 2005, pp 203-219 |
| 31 | */ |
| 32 | |
| 33 | #ifndef _ISOSURFACEMESHER_H_ |
| 34 | #define _ISOSURFACEMESHER_H_ |
| 35 | |
| 36 | #include <time.h> |
| 37 | #include <vector> |
| 38 | #include <map> |
| 39 | #include <set> |
| 40 | |
| 41 | #include "distanceFieldBase.h" |
| 42 | #include "objMesh.h" |
| 43 | #include "tetMesh.h" |
| 44 | #include "triple.h" |
| 45 | #include "triangle.h" |
| 46 | #include "delaunayMesher.h" |
| 47 | |
| 48 | class IsosurfaceMesher |
| 49 | { |
| 50 | public: |
| 51 | |
| 52 | // initialize the mesher |
| 53 | // the mesher will mesh an isosurface of the distance field "field" |
| 54 | IsosurfaceMesher(DistanceFieldBase * distanceField); |
| 55 | virtual ~IsosurfaceMesher(); |
| 56 | |
| 57 | // calculate the isosurface |
| 58 | // isovalue: the isovalue of the distance field to be meshed |
| 59 | // angularBound: the angles of the triangle on the isosurface mesh should not be larger than angularBound |
| 60 | // the angular bound is passed in radians |
| 61 | // radialBound: the largest circumcircle of the triangle on the isosurface mesh should not be larger than "radialBound" |
| 62 | // numInitialSamples: number of points generated in the first step of the algorithm (internal initial mesh) |
| 63 | // Query::Type: choice of precision used in the Delaunay mesher |
| 64 | // DOUBLE: double precision |
| 65 | // RATIONAL: high-precision arithmetics |
| 66 | // FILTERED: trade-off between DOUBLE and RATIONAL |
| 67 | // epsilon: internal accuracy parameter for queryType=FILTERED, on [0,1] |
| 68 | // the larger the value, the more computation will be done in high-precision arithmetics |
| 69 | // epsilon == 0: all computation in double precision |
| 70 | // epsilon == 1: all computation in high-precision |
| 71 | // maxNumberOfIterations: the routine will terminate if this number of iterations is exceeded. < 0 means no limitation |
| 72 | // maxTimeSeconds: the routine will terminate if this computation time (in seconds) is exceeded. < 0 means no limitation |
| 73 | // return value: true if timeout occurred, false otherwise |
| 74 | bool compute(double isovalue, double angularBound, double radialBound, |
| 75 | int numInitialSamples = 20, double epsilon = 1e-6, |
| 76 | Query::Type queryType = Query::FILTERED, |
| 77 | int maxNumberOfIterations = -1, double maxTimeSeconds = -1.0); |
| 78 | |
| 79 | // get the result of compute() |
| 80 | ObjMesh * getMesh(bool enforceManifold = true); |
| 81 | |
| 82 | // =========== advanced routines =================== |
| 83 | |
| 84 | // pass a mesh representing the zero isosurface, to be remeshed via compute() |
| 85 | // only isovalue=0 is supported in this mode |
| 86 | IsosurfaceMesher(ObjMesh * detailedSurfaceMesh); |
| 87 | |
| 88 | // =========== debugging ================= |
| 89 | |
| 90 | void saveMarchingCubesMesh(bool save = true) { saveMarchingCubesObj = save; } |
| 91 | void checkDelaunayAfterComputation(bool check = true) { checkDelaunay = check; } |
| 92 | const ObjMesh * getMarchingCubesMesh() const { return marchingCubesMesh; } |
| 93 | |
| 94 | // stepping functions |
| 95 | void setStepping(double isovalue, double angularBound, double radialBound, size_t numInitialSamples = 20, double epsilon = 1e-6, Query::Type queryType = Query::FILTERED); |
| 96 | void doOneStep(); |
| 97 | bool endStepping() const; |
| 98 | |
| 99 | const DelaunayMesher & getCurrentDelaunay() const { return delaunay; } |
| 100 | |
| 101 | // if keepAllDelaunayVtx is false, only vertices that belong to at least one triangle are kept |
| 102 | // otherwise, all vertices in the Delaunay mesh are added to the resulting obj mesh |
| 103 | ObjMesh * buildCurrentSurfaceMesh(bool keepAllDelaunayVtx = false) const; |
| 104 | |
| 105 | // remove non-manifold faces and edges, orient faces |
| 106 | static bool enforceManifoldnessAndOrientNormals(ObjMesh * &objMesh); |
| 107 | protected: |
| 108 | IsosurfaceMesher(const IsosurfaceMesher &); |
| 109 | |
| 110 | // represent a face on the isosurface mesh |
| 111 | struct IsoFace |
| 112 | { |
| 113 | Vec3d isopoint; // a point on the isosurface near this face. This point is also the center of a circumsphere of the triangle face |
| 114 | double radius; // radius of the circumsphere centered at isopoint |
| 115 | double maxCosAngle; // max cosine of the angles of this triangle |
| 116 | UTriKey tri; // the triangle vtx index of this face |
| 117 | IsoFace() {} |
| 118 | IsoFace(const UTriKey & tri, const Vec3d & v0, const Vec3d & v1, const Vec3d & v2, const Vec3d & isopoint); |
| 119 | }; |
| 120 | // compare the radius of two IsoFace, used in a set to find the face with largest circumcircle radius |
| 121 | struct IsoFaceRadiusCompare |
| 122 | { |
| 123 | bool operator() (const IsoFace & a, const IsoFace & b) const; |
| 124 | }; |
| 125 | // compare the radius of two IsoFace, used in a set to find the face with largest maxCosAngle |
| 126 | struct IsoFaceAngleCompare |
| 127 | { |
| 128 | bool operator() (const IsoFace & a, const IsoFace & b) const; |
| 129 | }; |
| 130 | |
| 131 | // the set that compares IsoFace circumcircle radius, used to find the face with largest circumcircle radius |
| 132 | typedef std::set<IsoFace, IsoFaceRadiusCompare> IsoFaceRadiusSet; |
| 133 | // the set that compares IsoFace maxCosAngle, used to find the face with largest maxCosAngle |
| 134 | typedef std::set<IsoFace, IsoFaceAngleCompare> IsoFaceAngleSet; |
| 135 | |
| 136 | typedef IsoFaceRadiusSet::iterator RadiusIter; |
| 137 | typedef IsoFaceAngleSet::iterator AngleIter; |
| 138 | typedef std::pair<RadiusIter, AngleIter> IterPair; |
| 139 | typedef std::map<UTriKey, IterPair> IterPairMap; |
| 140 | |
| 141 | // DelaunayMesher related typedefs for easy interaction with DelaunayMesher |
| 142 | typedef DelaunayMesher::VoronoiEdge VoronoiEdge; |
| 143 | typedef DelaunayMesher::VoronoiEdgeMap VoronoiEdgeMap; |
| 144 | typedef DelaunayMesher::VEdgeIter VEdgeIter; |
| 145 | typedef DelaunayMesher::VEdgeCIter VEdgeCIter; |
| 146 | |
| 147 | // void intersection(const std::vector<TriangleBasic*> & triangles, const Vec3d & v, const Vec3d & direction, bool isRay, std::vector<double> & t); |
| 148 | |
| 149 | bool intersectionUsingOctree(const Vec3d & v, const Vec3d & direction, bool isRay, Vec3d & intersectionPoint); |
| 150 | |
| 151 | static void generateInitialPointSetByDiscrepancy(const ObjMesh * objMesh, std::vector<Vec3d> & point, const unsigned int numInitialSamples); |
| 152 | |
| 153 | ObjMesh * computeOneMesh(ObjMesh * objMesh, int & maxNumberOfIterations, double & maxTimeSeconds, bool & timeout); |
| 154 | bool initializeOneMesh(ObjMesh * objMesh); |
| 155 | bool addOneTriangle(double * targetRadius = NULL); // return true if it won't stop |
| 156 | |
| 157 | DistanceFieldBase * field; |
| 158 | ObjMesh * detailedSurfaceMesh; |
| 159 | double isovalue; |
| 160 | double angularBound, cosAngularBound, radialBound; |
| 161 | size_t numInitialSamples; |
| 162 | DelaunayMesher delaunay; |
| 163 | Query::Type queryType; |
| 164 | double epsilon; |
| 165 | |
| 166 | ObjMesh * splitIsosurfaceMesh; |
| 167 | std::vector<ObjMesh *> splitComponent; |
| 168 | double fieldDiagonal; |
| 169 | ObjMeshOctree<TriangleBasic> * objMeshOctree; |
| 170 | |
| 171 | // index for stepping |
| 172 | size_t splitComponentIndex; |
| 173 | int oneMeshLoopIndex; |
| 174 | |
| 175 | IsoFaceRadiusSet radiusSet; // a set stores the faces for the isosurface to be generated and order according to its circumcircle radius |
| 176 | IsoFaceAngleSet angleSet; // a set stores the faces and order according to its max cosine angle |
| 177 | IterPairMap faceIters; // map of UTriKey -> two iterators pointing to the face locations in radiusSet and angleSet |
| 178 | |
| 179 | ObjMesh * isoMesh; |
| 180 | |
| 181 | // StopWatch delaunayUpdateCounter; |
| 182 | // StopWatch octreeCounter; |
| 183 | // StopWatch intersectionCounter; |
| 184 | bool checkDelaunay; |
| 185 | bool saveMarchingCubesObj; |
| 186 | |
| 187 | ObjMesh * marchingCubesMesh; |
| 188 | |
| 189 | double ignoredSplitComponentVtxRatio; |
| 190 | |
| 191 | int globalLoopIndex; |
| 192 | }; |
| 193 | |
| 194 | #endif |
| 195 |