| 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 | #include <limits.h> |
| 25 | #include <vector> |
| 26 | #include <cfloat> |
| 27 | #include <stack> |
| 28 | |
| 29 | using namespace std; |
| 30 | |
| 31 | #include "tetMesher.h" |
| 32 | #include "triangleTetIntersection.h" |
| 33 | #include "objMeshOrientable.h" |
| 34 | #include "windingNumber.h" |
| 35 | #include "performanceCounter.h" |
| 36 | |
| 37 | #ifndef M_PI |
| 38 | #define M_PI 3.1415926525897932384 |
| 39 | #endif |
| 40 | |
| 41 | DelaunayMesher::DelaunayBallCompare TetMesher::TetMeshWithRefineInfo::defaultComparor; |
| 42 | |
| 43 | TetMesher::TetMesher() : resultTetMesh(delaunay) |
| 44 | { |
| 45 | objMesh = NULL; |
| 46 | numSteinerVertices = 0; |
| 47 | } |
| 48 | |
| 49 | TetMesh * TetMesher::compute(ObjMesh * inputMesh, double refinementQuality, double alpha, double minDihedral, int maxSteinerVertices, double maxTimeSeconds) |
| 50 | { |
| 51 | // clean previous data |
| 52 | delete objMesh; |
| 53 | objMesh = NULL; |
| 54 | faceRecoveryDepth = 0; |
| 55 | //hasMissing = false; |
| 56 | numSteinerVertices = 0; |
| 57 | |
| 58 | // Deep copy the input objMesh |
| 59 | objMesh = new ObjMesh(*inputMesh); |
| 60 | objMesh->removeIsolatedVertices(); |
| 61 | |
| 62 | // Merge all groups of the objMesh |
| 63 | vector <int> gIdx; |
| 64 | for (size_t i = 0; i < objMesh->getNumGroups(); i++) |
| 65 | gIdx.push_back(i); |
| 66 | objMesh->mergeGroups(gIdx); |
| 67 | |
| 68 | // make sure the first four vertices of the triangle mesh are not in the same plane |
| 69 | if (renumberInitialVertices(objMesh) == false) |
| 70 | { |
| 71 | delete objMesh; |
| 72 | objMesh = NULL; |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | PerformanceCounter counter; |
| 77 | counter.StartCounter(); |
| 78 | initializeCDT(false); // RM: no face recovery! |
| 79 | counter.StopCounter(); |
| 80 | double cdtTime = counter.GetElapsedTime(); |
| 81 | |
| 82 | if (maxSteinerVertices == -1) |
| 83 | maxSteinerVertices = INT_MAX; |
| 84 | |
| 85 | if ((maxTimeSeconds >= 0) && (cdtTime >= maxTimeSeconds)) |
| 86 | return delaunay.getMesh(); |
| 87 | |
| 88 | double maxRefinementTime = -1.0; |
| 89 | if (maxTimeSeconds >= 0) |
| 90 | maxRefinementTime = maxTimeSeconds - cdtTime; |
| 91 | printf("Starting tet mesh refinement.\n"); |
| 92 | |
| 93 | delaunay.buildCDT(); |
| 94 | |
| 95 | // Enable the minimal dihedral angle refinement mechanism. If the minimal dihedral angle of a tet is smaller than a threshold, it needs refining. |
| 96 | if (minDihedral > 0) |
| 97 | resultTetMesh.enableAngleRefine(); |
| 98 | |
| 99 | // Insert all tets (regular balls in the delaunay) |
| 100 | for (BallCIter it = delaunay.getBallSetBegin(), end = delaunay.getBallSetEnd(); it != end; it++) |
| 101 | { |
| 102 | const DelaunayBall * ball = *it; |
| 103 | |
| 104 | if (ball->isInfinite()) |
| 105 | continue; |
| 106 | resultTetMesh.insert(ball); |
| 107 | } |
| 108 | |
| 109 | double averageEdgeLength = objMesh->computeAverageEdgeLength(); |
| 110 | |
| 111 | //cout << "Average length: " << averageEdgeLength << endl; |
| 112 | |
| 113 | numSteinerVertices = 0; |
| 114 | counter.StartCounter(); |
| 115 | //First we use edge refine rule (radius/minEdge) |
| 116 | while ((refineEdge(refinementQuality, alpha * averageEdgeLength) == 0) && (numSteinerVertices < maxSteinerVertices)) |
| 117 | { |
| 118 | printf("#"); fflush(NULL); |
| 119 | counter.StopCounter(); |
| 120 | double elapsedTime = counter.GetElapsedTime(); |
| 121 | if ((maxRefinementTime >= 0) && (elapsedTime > maxRefinementTime)) |
| 122 | break; |
| 123 | } |
| 124 | printf("\n"); |
| 125 | |
| 126 | //Use minimal dihedral angle refinement |
| 127 | if (minDihedral > 0) |
| 128 | { |
| 129 | printf("Enforcing the minimum dihedral angle condition...\n"); |
| 130 | while (refineAngle(minDihedral, alpha * averageEdgeLength) == 0 && numSteinerVertices < maxSteinerVertices) |
| 131 | { |
| 132 | printf("#"); fflush(NULL); |
| 133 | counter.StopCounter(); |
| 134 | double elapsedTime = counter.GetElapsedTime(); |
| 135 | if ((maxRefinementTime >= 0) && (elapsedTime > maxRefinementTime)) |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | printf("\n"); |
| 141 | cout << numSteinerVertices << " steiner points inserted." << endl; |
| 142 | TetMesh * ret = resultTetMesh.getTetMesh(); |
| 143 | //ret->orient(); |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | // Make sure the first four vertices of the triangle mesh are not on the same plane |
| 148 | bool TetMesher::renumberInitialVertices(ObjMesh * surfaceMesh) |
| 149 | { |
| 150 | Vec3d p0 = surfaceMesh->getPosition(0); |
| 151 | Vec3d p1 = Vec3d(0, 0, 0), p2 = Vec3d(0, 0, 0), p3 = Vec3d(0, 0, 0); |
| 152 | int v1 = -1, v2 = -1, v3 = -1; |
| 153 | double max_dis = 1e-10; |
| 154 | /*The second vertex is different from the first vertex*/ |
| 155 | for (size_t i = 1; i < surfaceMesh->getNumVertices(); i++) |
| 156 | { |
| 157 | Vec3d d10 = surfaceMesh->getPosition(i) - p0; |
| 158 | double dis = dot(d10, d10); |
| 159 | if (dis > max_dis) |
| 160 | { |
| 161 | max_dis = dot(d10, d10); |
| 162 | p1 = surfaceMesh->getPosition(i); |
| 163 | v1 = i; |
| 164 | } |
| 165 | } |
| 166 | if (v1 == -1) |
| 167 | return false; //new TetMesh(0, (double*)NULL, 0, (int*)NULL); /* The triangle mesh has only one position*/ |
| 168 | |
| 169 | p1 = surfaceMesh->getPosition(v1); |
| 170 | /*The first three vertices are not on the same straight line*/ |
| 171 | Vec3d p10 = p1 - p0; |
| 172 | p10.normalize(); |
| 173 | max_dis = 0; |
| 174 | for (size_t i = 1; i < surfaceMesh->getNumVertices(); i++) |
| 175 | { |
| 176 | if(i == (size_t)v1) |
| 177 | continue; |
| 178 | Vec3d normal = cross(p10, surfaceMesh->getPosition(i) - p0); |
| 179 | if (dot(normal, normal) > 1e-10) |
| 180 | { |
| 181 | p2 = surfaceMesh->getPosition(i); |
| 182 | double dis = min(len2(p2 - p0), len2(p2 - p1)); |
| 183 | if (dis > max_dis) |
| 184 | { |
| 185 | max_dis = dis; |
| 186 | v2 = i; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | if (v2 == -1) |
| 191 | return false; // new TetMesh(0, (double*)NULL, 0, (int*)NULL); /* The triangle mesh has only one line*/ |
| 192 | |
| 193 | /*The first four vertices are not on the same plane*/ |
| 194 | p2 = surfaceMesh->getPosition(v2); |
| 195 | Vec3d p20 = p2 - p0; |
| 196 | p20.normalize(); |
| 197 | max_dis = 0; |
| 198 | for (size_t i = 3; i < surfaceMesh->getNumVertices(); i++) |
| 199 | { |
| 200 | if (fabs(dot(p20, cross(p10, surfaceMesh->getPosition(i) - p0))) > 0) |
| 201 | { |
| 202 | p3 = surfaceMesh->getPosition(i); |
| 203 | double dis = (std::min)(len2(p3 - p0), (std::min)(len2(p2 - p0), len2(p1 - p0))); |
| 204 | if (dis > max_dis) |
| 205 | { |
| 206 | max_dis = dis; |
| 207 | v3 = i; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | if (v3 == -1) |
| 212 | return false; // new TetMesh(0, (double*)NULL, 0, (int*)NULL); /* The triangle mesh has only one plane*/ |
| 213 | |
| 214 | // Sort the first four vertices so that the volume is positive |
| 215 | p3 = surfaceMesh->getPosition(v3); |
| 216 | if (dot(p20, cross(p10, p3 - p0)) < 0) |
| 217 | swap(v2, v3); |
| 218 | |
| 219 | // Permute the objMesh |
| 220 | vector<int> renumber; |
| 221 | renumber.resize(surfaceMesh->getNumVertices()); |
| 222 | |
| 223 | for (size_t i = 0; i < renumber.size(); i++) |
| 224 | renumber[i] = i; |
| 225 | swap(renumber[1], renumber[v1]); |
| 226 | |
| 227 | int overwrite = renumber[v2]; |
| 228 | renumber[v2] = 2; |
| 229 | for (size_t i = 0; i < renumber.size(); i++) |
| 230 | if (renumber[i] == 2) |
| 231 | { |
| 232 | renumber[i] = overwrite; |
| 233 | break; |
| 234 | } |
| 235 | overwrite = renumber[v3]; |
| 236 | renumber[v3] = 3; |
| 237 | for (size_t i = 0; i < renumber.size(); i++) |
| 238 | if (renumber[i] == 3) |
| 239 | { |
| 240 | renumber[i] = overwrite; |
| 241 | break; |
| 242 | } |
| 243 | surfaceMesh->renumberVertices(renumber); |
| 244 | |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | TetMesher::~TetMesher() |
| 249 | { |
| 250 | delete objMesh; |
| 251 | } |
| 252 | |
| 253 | int TetMesher::initializeCDT(bool recovery) |
| 254 | { |
| 255 | //printf("Entering initializeCDT:\n"); fflush(NULL); |
| 256 | unsigned nv; |
| 257 | double *v = NULL; |
| 258 | const ObjMesh::Group *group = objMesh->getGroupHandle(0); |
| 259 | if (recovery) |
| 260 | { |
| 261 | //get all faces and build neighbors in the triangular mesh |
| 262 | map<pair<int, int>, vector<int> > neighborBuffer; |
| 263 | for (unsigned i = 0; i < group->getNumFaces(); i++) |
| 264 | { |
| 265 | const ObjMesh::Face face = group->getFace(i); |
| 266 | int a = face.getVertex(0).getPositionIndex(); |
| 267 | int b = face.getVertex(1).getPositionIndex(); |
| 268 | int c = face.getVertex(2).getPositionIndex(); |
| 269 | pair<int, int> edge[3] = { (a < b ? make_pair(a, b) : make_pair(b, a)), (a < c ? make_pair(a, c) : make_pair(c, a)), (c < b ? make_pair(c, b) : make_pair(b, c)) }; |
| 270 | for (int j = 0; j < 3; j++) |
| 271 | neighborBuffer[edge[j]].push_back(i); |
| 272 | } |
| 273 | |
| 274 | neighborSurface.resize(group->getNumFaces()); |
| 275 | for (map<pair<int, int>, vector<int> >::const_iterator itr = neighborBuffer.begin(); itr != neighborBuffer.end(); itr++) |
| 276 | { |
| 277 | |
| 278 | if (itr->second.size() != 2) |
| 279 | { |
| 280 | printf("The input mesh must be a 2-manifold mesh\n"); |
| 281 | exit(3); |
| 282 | } |
| 283 | |
| 284 | for (size_t i = 0; i < itr->second.size(); i++) |
| 285 | for (size_t j = i + 1; j < itr->second.size(); j++) |
| 286 | { |
| 287 | int t0 = itr->second.at(i); |
| 288 | int t1 = itr->second.at(j); |
| 289 | neighborSurface[t0].push_back(t1); |
| 290 | neighborSurface[t1].push_back(t0); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | for (size_t i = 0; i < neighborSurface.size(); i++) |
| 295 | if (neighborSurface[i].size() != 3) |
| 296 | { |
| 297 | printf("The input mesh must be a triagular mesh\n"); |
| 298 | exit(4); |
| 299 | } |
| 300 | } |
| 301 | objMesh->exportGeometry((int *) &nv, &v); |
| 302 | vector<Vec3d> objVertices; |
| 303 | for(size_t i = 0; i < nv; i++) |
| 304 | objVertices.push_back(&v[3*i]); |
| 305 | //printf("Before computeDelaunayTetrahedralization:\n"); fflush(NULL); |
| 306 | delaunay.computeDelaunayTetrahedralization(objVertices); |
| 307 | //printf("After computeDelaunayTetrahedralization:\n"); fflush(NULL); |
| 308 | free(v); |
| 309 | trianglesInTet.clear(); |
| 310 | |
| 311 | for (DelaunayMesher::BallCIter itr = delaunay.getBallSetBegin(); itr != delaunay.getBallSetEnd(); itr++) |
| 312 | { |
| 313 | const DelaunayMesher::DelaunayBall *ball = *itr; |
| 314 | if (ball->isRegular()) |
| 315 | insertTet(ball->getVertices()); |
| 316 | } |
| 317 | |
| 318 | //printf("Before segmentRecovery:\n"); fflush(NULL); |
| 319 | //segmentRecovery(); // TODO: this creates a segfault an we don't really know why... |
| 320 | //printf("After segmentRecovery:\n"); fflush(NULL); |
| 321 | flipSurface(); |
| 322 | |
| 323 | lost.clear(); |
| 324 | for (unsigned i = 0; i < group->getNumFaces(); i++) |
| 325 | { |
| 326 | const ObjMesh::Face face = group->getFace(i); |
| 327 | int a = face.getVertex(0).getPositionIndex(); |
| 328 | int b = face.getVertex(1).getPositionIndex(); |
| 329 | int c = face.getVertex(2).getPositionIndex(); |
| 330 | UTriKey key = UTriKey(a, b, c); |
| 331 | if (trianglesInTet.find(key) == trianglesInTet.end()) |
| 332 | { |
| 333 | lost.push_back(key); |
| 334 | //printf("Lost face (%d, %d %d)\n", key[0], key[1], key[2]); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | //cout << "Lost size: " << lost.size() << endl; |
| 339 | |
| 340 | //printf("\n"); |
| 341 | if (!lost.empty() && recovery) |
| 342 | faceRecovery(); |
| 343 | removeOutside(); |
| 344 | return lost.size(); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | int TetMesher::refineAngle(const double angleBound, const double minmimalDist) |
| 349 | { |
| 350 | TetMeshWithRefineInfo::AngleRefineIterator itr; |
| 351 | for ( itr = resultTetMesh.getAngleRefineBegin(); itr != resultTetMesh.getAngleRefineEnd(); itr++) |
| 352 | { |
| 353 | const DelaunayBallWithRefineInfo * delaunayBallWithRefineInfo = *itr; |
| 354 | if (delaunayBallWithRefineInfo->minDihedral > angleBound) |
| 355 | return 2; // no bad tet to refine |
| 356 | |
| 357 | Vec3d r = delaunayBallWithRefineInfo->getCenter(); |
| 358 | if (delaunayBallWithRefineInfo->visibleTo(r) == false) |
| 359 | { |
| 360 | continue; //invisible, find a new bad tet to refine |
| 361 | } |
| 362 | |
| 363 | if (isTooCloseToOtherVertices(r, minmimalDist)) |
| 364 | { |
| 365 | //cout << "Too close vertices" << endl; |
| 366 | continue; //too close to the other vertices, find a new bad tet to refine |
| 367 | } |
| 368 | |
| 369 | int added = delaunay.addOnePoint(r); |
| 370 | if (added) // one point added |
| 371 | { |
| 372 | for (DelaunayMesher::BallIter itr = delaunay.getBallToDeleteSetBegin(); itr != delaunay.getBallToDeletelSetEnd(); itr++) |
| 373 | resultTetMesh.remove(*itr); |
| 374 | for (DelaunayMesher::BallIter itr = delaunay.getBallAddedSetBegin(); itr != delaunay.getBallAddedSetEnd(); itr++) |
| 375 | resultTetMesh.insert(*itr); |
| 376 | numSteinerVertices++; |
| 377 | return 0; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | //No tet to refine due to too sparse steiner points |
| 382 | return 1; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | Ret value |
| 387 | 0: refined |
| 388 | 1: No tet to refine due to enough steiner points |
| 389 | 2: No bad tet to refine |
| 390 | */ |
| 391 | int TetMesher::refineEdge(double refinementQuality, double minmimalDist) |
| 392 | { |
| 393 | // maxSteinerVertices = 1000; |
| 394 | |
| 395 | TetMeshWithRefineInfo::EdgeRefineIterator itr; |
| 396 | for ( itr = resultTetMesh.getEdgeRefineBegin(); itr != resultTetMesh.getEdgeRefineEnd(); itr++) |
| 397 | { |
| 398 | const DelaunayBallWithRefineInfo * delaunayBallWithRefineInfo = *itr; |
| 399 | if (delaunayBallWithRefineInfo->edgeQuality < refinementQuality) |
| 400 | { |
| 401 | cout << "All tets are good quality after refining." << endl; |
| 402 | return 2; //No bad tet to refine |
| 403 | } |
| 404 | |
| 405 | Vec3d r = delaunayBallWithRefineInfo->getCenter(); |
| 406 | if (delaunayBallWithRefineInfo->contain(r) >= 0 || delaunayBallWithRefineInfo->visibleTo(r) == false) |
| 407 | continue; //invisible, find a new bad tet to refine |
| 408 | |
| 409 | if (isTooCloseToOtherVertices(r, minmimalDist)) |
| 410 | continue; //too close to other vertices, find a new bad tet to refine |
| 411 | |
| 412 | int added = delaunay.addOnePoint(r); |
| 413 | if (added) //One point added |
| 414 | { |
| 415 | for (DelaunayMesher::BallIter itr = delaunay.getBallToDeleteSetBegin(); itr != delaunay.getBallToDeletelSetEnd(); itr++) |
| 416 | resultTetMesh.remove(*itr); |
| 417 | for (DelaunayMesher::BallIter itr = delaunay.getBallAddedSetBegin(); itr != delaunay.getBallAddedSetEnd(); itr++) |
| 418 | resultTetMesh.insert(*itr); |
| 419 | numSteinerVertices++; |
| 420 | return 0; |
| 421 | } |
| 422 | } |
| 423 | return 1; |
| 424 | } |
| 425 | |
| 426 | int TetMesher::removeOutside() |
| 427 | { |
| 428 | const ObjMesh::Group *group = objMesh->getGroupHandle(0); |
| 429 | |
| 430 | set <UTriKey> surfaceTri; |
| 431 | for (unsigned i = 0; i < group->getNumFaces(); i++) |
| 432 | { |
| 433 | const ObjMesh::Face face = group->getFace(i); |
| 434 | int a = face.getVertex(0).getPositionIndex(); |
| 435 | int b = face.getVertex(1).getPositionIndex(); |
| 436 | int c = face.getVertex(2).getPositionIndex(); |
| 437 | surfaceTri.insert(UTriKey(a, b, c)); |
| 438 | } |
| 439 | /* |
| 440 | for (DelaunayMesher::BallCIter itr = delaunay.getBallSetBegin(); itr != delaunay.getBallSetEnd(); itr++) |
| 441 | { |
| 442 | if ((*itr)->isInfinite()) |
| 443 | continue; |
| 444 | for (int j = 0; j < 4; j++) |
| 445 | insertToFaceElementMap((*itr)->uFaceKey(j), *itr); |
| 446 | } |
| 447 | */ |
| 448 | set <DelaunayMesher::DelaunayBall*, DelaunayMesher::DelaunayBallCompare> removeSetCandidate; |
| 449 | set <DelaunayMesher::DelaunayBall*, DelaunayMesher::DelaunayBallCompare> removeSet; |
| 450 | |
| 451 | for (DelaunayMesher::BallCIter itr = delaunay.getBallSetBegin(); itr != delaunay.getBallSetEnd(); itr++) |
| 452 | { |
| 453 | if ((*itr)->isInfinite()) |
| 454 | { |
| 455 | removeSetCandidate.insert(*itr); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | while (!removeSetCandidate.empty()) |
| 460 | { |
| 461 | DelaunayMesher::DelaunayBall * ball = *removeSetCandidate.begin(); |
| 462 | removeSetCandidate.erase(removeSetCandidate.begin()); |
| 463 | removeSet.insert(ball); |
| 464 | if (!ball) |
| 465 | continue; |
| 466 | |
| 467 | for (int i = 0; i < 4; i++) |
| 468 | { |
| 469 | DelaunayMesher::DelaunayBall * neighbor = ball->getNeighbor(i); |
| 470 | if (surfaceTri.find(ball->uFaceKey(i)) == surfaceTri.end() && removeSet.find(neighbor) == removeSet.end() && removeSetCandidate.find(neighbor) == removeSetCandidate.end()) |
| 471 | { |
| 472 | Vec3d center = (neighbor->getPosition(0) + neighbor->getPosition(1) + neighbor->getPosition(2) + neighbor->getPosition(3)) / 4; |
| 473 | double windingNumber = fabs(WindingNumber::computeWindingNumber(objMesh, center)); |
| 474 | if (windingNumber > 0.5) |
| 475 | continue; |
| 476 | |
| 477 | removeSetCandidate.insert(ball->getNeighbor(i)); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | for (DelaunayMesher::BallCIter itr = removeSet.begin(); itr != removeSet.end(); itr++) |
| 483 | delaunay.removeBall(*itr); |
| 484 | |
| 485 | return removeSet.size(); |
| 486 | } |
| 487 | |
| 488 | |
| 489 | void TetMesher::insertFace(int f1, int f2, int f3) |
| 490 | { |
| 491 | UTriKey key = UTriKey(f1, f2, f3); |
| 492 | map<UTriKey, unsigned>::iterator itr = trianglesInTet.find(key); |
| 493 | if (itr == trianglesInTet.end()) |
| 494 | { |
| 495 | trianglesInTet[key] = 1; |
| 496 | } |
| 497 | else |
| 498 | itr->second++; |
| 499 | insertEdge(f1, f2); |
| 500 | insertEdge(f1, f3); |
| 501 | insertEdge(f3, f2); |
| 502 | } |
| 503 | |
| 504 | void TetMesher::removeFace(int f1, int f2, int f3) |
| 505 | { |
| 506 | UTriKey key = UTriKey(f1, f2, f3); |
| 507 | map<UTriKey, unsigned>::iterator itr = trianglesInTet.find(key); |
| 508 | assert(itr != trianglesInTet.end()); |
| 509 | if (itr->second == 1) |
| 510 | trianglesInTet.erase(itr); |
| 511 | else |
| 512 | itr->second--; |
| 513 | removeEdge(f1, f2); |
| 514 | removeEdge(f1, f3); |
| 515 | removeEdge(f3, f2); |
| 516 | } |
| 517 | |
| 518 | void TetMesher::insertEdge(int e1, int e2) |
| 519 | { |
| 520 | UEdgeKey key(e1, e2); |
| 521 | map<UEdgeKey, unsigned>::iterator itr = edgesInTet.find(key); |
| 522 | if (itr == edgesInTet.end()) |
| 523 | { |
| 524 | edgesInTet[key] = 1; |
| 525 | } |
| 526 | else |
| 527 | itr->second++; |
| 528 | } |
| 529 | |
| 530 | void TetMesher::removeEdge(int e1, int e2) |
| 531 | { |
| 532 | UEdgeKey key(e1, e2); |
| 533 | map<UEdgeKey, unsigned>::iterator itr = edgesInTet.find(key); |
| 534 | if (itr->second == 1) |
| 535 | edgesInTet.erase(itr); |
| 536 | else |
| 537 | itr->second--; |
| 538 | } |
| 539 | |
| 540 | UEdgeKey find_intersect(UTriKey t1, UTriKey t2) |
| 541 | { |
| 542 | bool found = false; |
| 543 | pair<int, int> ret; |
| 544 | for (int i = 0; i < 3; i++) |
| 545 | for (int j = 0; j < 3; j++) |
| 546 | if (t1[i] == t2[j]) |
| 547 | { |
| 548 | if (!found) |
| 549 | { |
| 550 | found = true; |
| 551 | ret.first = t1[i]; |
| 552 | } |
| 553 | else |
| 554 | { |
| 555 | ret.second = t1[i]; |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | return UEdgeKey(ret.first, ret.second); |
| 560 | } |
| 561 | void TetMesher::formRegion(int face, set<int> ®ion) |
| 562 | { |
| 563 | if (region.find(face) != region.end()) // The triangle is already in the region |
| 564 | return; |
| 565 | UTriKey vertexIndices = getFace(face); |
| 566 | if (trianglesInTet.find(vertexIndices) != trianglesInTet.end()) |
| 567 | return; // The triangle is not missing |
| 568 | region.insert(face); |
| 569 | for (unsigned i = 0; i < neighborSurface[face].size(); i++) |
| 570 | { |
| 571 | UTriKey neighborVertexIndices = getFace(neighborSurface[face][i]); |
| 572 | UEdgeKey commonEdge = find_intersect(vertexIndices, neighborVertexIndices); |
| 573 | if (edgesInTet.find(commonEdge) == edgesInTet.end()) // The common edge is not in the tet |
| 574 | formRegion(neighborSurface[face][i], region); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | UTriKey TetMesher::getFace(int index) |
| 579 | { |
| 580 | const ObjMesh::Face *face = objMesh->getGroupHandle(0)->getFaceHandle(index); |
| 581 | return UTriKey(face->getVertexHandle(0)->getPositionIndex(), face->getVertexHandle(1)->getPositionIndex(), face->getVertexHandle(2)->getPositionIndex()); |
| 582 | } |
| 583 | |
| 584 | void TetMesher::faceRecovery() |
| 585 | { |
| 586 | //printf("Patching\n"); |
| 587 | //tet->save("delaunay.veg"); |
| 588 | for (unsigned int face = 0; face < objMesh->getNumFaces(); face++) |
| 589 | { |
| 590 | if (trianglesInTet.find(getFace(face)) != trianglesInTet.end()) // This face is not missing |
| 591 | continue; |
| 592 | //printf("Missing\n"); |
| 593 | region.clear(); |
| 594 | formRegion(face, region); // Form a region that is connected missing triangles |
| 595 | vector<vector<Vec3d> > regionVertices; // n*3 Vec3d array |
| 596 | regionVertices.resize(region.size()); |
| 597 | int tail = 0; |
| 598 | for (set<int>::const_iterator i = region.begin(); i != region.end(); i++) |
| 599 | { |
| 600 | regionVertices[tail].push_back(objMesh->getPosition(objMesh->getGroupHandle(0)->getFaceHandle(*i)->getVertex(0).getPositionIndex())); |
| 601 | regionVertices[tail].push_back(objMesh->getPosition(objMesh->getGroupHandle(0)->getFaceHandle(*i)->getVertex(1).getPositionIndex())); |
| 602 | regionVertices[tail].push_back(objMesh->getPosition(objMesh->getGroupHandle(0)->getFaceHandle(*i)->getVertex(2).getPositionIndex())); |
| 603 | tail++; |
| 604 | } |
| 605 | |
| 606 | vector <DelaunayMesher::DelaunayBall*> intersectTet; |
| 607 | intersectTet.clear(); |
| 608 | |
| 609 | // Find all tets that intersect this region |
| 610 | for (DelaunayMesher::BallCIter itr = delaunay.getBallSetBegin(); itr != delaunay.getBallSetEnd(); itr++) |
| 611 | { |
| 612 | DelaunayMesher::DelaunayBall * ball = *itr; |
| 613 | if (ball->isInfinite()) |
| 614 | continue; // No check for infinite ball |
| 615 | Vec3d v0 = ball->getPosition(0); |
| 616 | Vec3d v1 = ball->getPosition(1); |
| 617 | Vec3d v2 = ball->getPosition(2); |
| 618 | Vec3d v3 = ball->getPosition(3); |
| 619 | for (unsigned j = 0; j < region.size(); j++) |
| 620 | { |
| 621 | if (TriangleTetIntersection::tetrahedronIntersectTriangle(v0, v1, v2, v3, regionVertices[j][0], regionVertices[j][1], regionVertices[j][2])) |
| 622 | { |
| 623 | intersectTet.push_back(ball); |
| 624 | break; |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | //printf("\n"); |
| 629 | vector<UTriKey> boundaryFace; |
| 630 | boundaryFace.clear(); |
| 631 | map<pair<int, int>, pair<int, int> > neighbor; |
| 632 | set<pair<int, int> > missingBoundary; |
| 633 | calculateTetBoundary(intersectTet, boundaryFace); |
| 634 | calculateTriangleBoundary(region, missingBoundary); |
| 635 | buildTriangleNeighbor(boundaryFace, neighbor); |
| 636 | //bool patched = false; |
| 637 | if (intersectTet.empty()) // Impossible |
| 638 | continue; |
| 639 | formTwoCavities(boundaryFace, missingBoundary, neighbor); |
| 640 | for (unsigned i = 0; i < intersectTet.size(); i++) |
| 641 | { |
| 642 | const int * v = intersectTet[i]->getVertices(); |
| 643 | removeTet(v); |
| 644 | delaunay.removeBall(intersectTet[i]); |
| 645 | } |
| 646 | for (unsigned i = 0; i < toAdd.size(); i++) |
| 647 | { |
| 648 | insertTet(toAdd[i].data()); |
| 649 | //tet->addElement(toAdd[i][0], toAdd[i][1], toAdd[i][2], toAdd[i][3]); |
| 650 | delaunay.addBall(toAdd[i].data()); |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | void TetMesher::calculateTetBoundary(std::vector<DelaunayMesher::DelaunayBall*>& intersectTet, std::vector<UTriKey>& faceVector) |
| 656 | { |
| 657 | std::set<UTriKey> faceSet; |
| 658 | faceSet.clear(); |
| 659 | for (unsigned i = 0; i < intersectTet.size(); i++) |
| 660 | { |
| 661 | const int * v = intersectTet[i]->getVertices(); |
| 662 | UTriKey face(v[0], v[1], v[2]); |
| 663 | set<UTriKey>::iterator itr; |
| 664 | itr = faceSet.find(face); |
| 665 | if (itr == faceSet.end()) |
| 666 | faceSet.insert(face); |
| 667 | else |
| 668 | faceSet.erase(itr); |
| 669 | face = UTriKey(v[3], v[1], v[2]); |
| 670 | itr = faceSet.find(face); |
| 671 | if (itr == faceSet.end()) |
| 672 | faceSet.insert(face); |
| 673 | else |
| 674 | faceSet.erase(itr); |
| 675 | face = UTriKey(v[0], v[3], v[2]); |
| 676 | itr = faceSet.find(face); |
| 677 | if (itr == faceSet.end()) |
| 678 | faceSet.insert(face); |
| 679 | else |
| 680 | faceSet.erase(itr); |
| 681 | face = UTriKey(v[0], v[1], v[3]); |
| 682 | itr = faceSet.find(face); |
| 683 | if (itr == faceSet.end()) |
| 684 | faceSet.insert(face); |
| 685 | else |
| 686 | faceSet.erase(itr); |
| 687 | } |
| 688 | faceVector.clear(); |
| 689 | for (std::set<UTriKey>::iterator i = faceSet.begin(); i != faceSet.end(); i++) |
| 690 | faceVector.push_back(*i); |
| 691 | } |
| 692 | |
| 693 | void TetMesher::calculateTriangleBoundary(set<int>& missingRegion, std::set<std::pair<int, int> >& boundary) |
| 694 | { |
| 695 | boundary.clear(); |
| 696 | for (set<int>::iterator i = missingRegion.begin(); i != missingRegion.end(); i++) |
| 697 | { |
| 698 | int v[3]; |
| 699 | const ObjMesh::Face *face = objMesh->getGroupHandle(0)->getFaceHandle(*i); |
| 700 | for (unsigned j = 0; j < 3; j++) |
| 701 | v[j] = face->getVertexHandle(j)->getPositionIndex(); |
| 702 | std::pair<int, int> edge; |
| 703 | set<std::pair<int, int> >::iterator itr; |
| 704 | edge = make_pair(v[0], v[1]); |
| 705 | if (edge.first > edge.second) |
| 706 | std::swap(edge.first, edge.second); |
| 707 | itr = boundary.find(edge); |
| 708 | if (itr == boundary.end()) |
| 709 | boundary.insert(edge); |
| 710 | else |
| 711 | boundary.erase(itr); |
| 712 | edge = make_pair(v[2], v[1]); |
| 713 | if (edge.first > edge.second) |
| 714 | std::swap(edge.first, edge.second); |
| 715 | itr = boundary.find(edge); |
| 716 | if (itr == boundary.end()) |
| 717 | boundary.insert(edge); |
| 718 | else |
| 719 | boundary.erase(itr); |
| 720 | edge = make_pair(v[0], v[2]); |
| 721 | if (edge.first > edge.second) |
| 722 | std::swap(edge.first, edge.second); |
| 723 | itr = boundary.find(edge); |
| 724 | if (itr == boundary.end()) |
| 725 | boundary.insert(edge); |
| 726 | else |
| 727 | boundary.erase(itr); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | void TetMesher::buildTriangleNeighbor(std::vector<UTriKey>& mesh, std::map<std::pair<int, int>, std::pair<int, int> >& neighbor) |
| 732 | { |
| 733 | neighbor.clear(); |
| 734 | for (unsigned i = 0; i < mesh.size(); i++) |
| 735 | { |
| 736 | int v[3]; |
| 737 | v[0] = mesh[i][0]; |
| 738 | v[1] = mesh[i][1]; |
| 739 | v[2] = mesh[i][2]; |
| 740 | std::sort(v, v + 3); |
| 741 | pair<int, int> edge; |
| 742 | map<pair<int, int>, pair<int, int> >::iterator itr; |
| 743 | edge = make_pair(v[0], v[1]); |
| 744 | itr = neighbor.find(edge); |
| 745 | if (itr == neighbor.end()) |
| 746 | neighbor[edge].first = i; |
| 747 | else |
| 748 | neighbor[edge].second = i; |
| 749 | edge = make_pair(v[1], v[2]); |
| 750 | itr = neighbor.find(edge); |
| 751 | if (itr == neighbor.end()) |
| 752 | neighbor[edge].first = i; |
| 753 | else |
| 754 | neighbor[edge].second = i; |
| 755 | edge = make_pair(v[0], v[2]); |
| 756 | itr = neighbor.find(edge); |
| 757 | if (itr == neighbor.end()) |
| 758 | neighbor[edge].first = i; |
| 759 | else |
| 760 | neighbor[edge].second = i; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | bool TetMesher::formTwoCavities(std::vector<UTriKey>& faceSet, std::set<std::pair<int, int> >& boundary, std::map<std::pair<int, int>, std::pair<int, int> >& neighborMap) |
| 765 | { |
| 766 | std::set<int> cavity1; |
| 767 | std::set<int> cavity2; |
| 768 | cavity1.clear(); |
| 769 | cavity2.clear(); |
| 770 | cavity1.insert(neighborMap[*boundary.begin()].first); |
| 771 | toAdd.clear(); |
| 772 | queue<int> q; |
| 773 | while (!q.empty()) |
| 774 | q.pop(); //q.clear() |
| 775 | q.push(neighborMap[*boundary.begin()].first); |
| 776 | while (!q.empty()) |
| 777 | { |
| 778 | int i = q.front(); |
| 779 | q.pop(); |
| 780 | int v[3]; |
| 781 | v[0] = faceSet[i][0]; |
| 782 | v[1] = faceSet[i][1]; |
| 783 | v[2] = faceSet[i][2]; |
| 784 | std::sort(v, v + 3); |
| 785 | pair<int, int> edge; |
| 786 | pair<int, int> twoNeighbors; |
| 787 | int neighbor; |
| 788 | map<pair<int, int>, pair<int, int> >::iterator itr; |
| 789 | edge = make_pair(v[0], v[1]); |
| 790 | if (boundary.find(edge) == boundary.end()) |
| 791 | { |
| 792 | twoNeighbors = neighborMap[edge]; |
| 793 | neighbor = (twoNeighbors.first == i ? twoNeighbors.second : twoNeighbors.first); |
| 794 | if (cavity1.find(neighbor) == cavity1.end()) |
| 795 | { |
| 796 | cavity1.insert(neighbor); |
| 797 | q.push(neighbor); |
| 798 | } |
| 799 | } |
| 800 | edge = make_pair(v[1], v[2]); |
| 801 | if (boundary.find(edge) == boundary.end()) |
| 802 | { |
| 803 | twoNeighbors = neighborMap[edge]; |
| 804 | neighbor = (twoNeighbors.first == i ? twoNeighbors.second : twoNeighbors.first); |
| 805 | if (cavity1.find(neighbor) == cavity1.end()) |
| 806 | { |
| 807 | cavity1.insert(neighbor); |
| 808 | q.push(neighbor); |
| 809 | } |
| 810 | } |
| 811 | edge = make_pair(v[0], v[2]); |
| 812 | if (boundary.find(edge) == boundary.end()) |
| 813 | { |
| 814 | twoNeighbors = neighborMap[edge]; |
| 815 | neighbor = (twoNeighbors.first == i ? twoNeighbors.second : twoNeighbors.first); |
| 816 | if (cavity1.find(neighbor) == cavity1.end()) |
| 817 | { |
| 818 | cavity1.insert(neighbor); |
| 819 | q.push(neighbor); |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | cavity2.insert(neighborMap[*boundary.begin()].second); |
| 824 | while (!q.empty()) |
| 825 | q.pop(); //q.clear() |
| 826 | q.push(neighborMap[*boundary.begin()].second); |
| 827 | while (!q.empty()) |
| 828 | { |
| 829 | int i = q.front(); |
| 830 | q.pop(); |
| 831 | int v[3]; |
| 832 | v[0] = faceSet[i][0]; |
| 833 | v[1] = faceSet[i][1]; |
| 834 | v[2] = faceSet[i][2]; |
| 835 | std::sort(v, v + 3); |
| 836 | pair<int, int> edge; |
| 837 | pair<int, int> twoNeighbors; |
| 838 | int neighbor; |
| 839 | map<pair<int, int>, pair<int, int> >::iterator itr; |
| 840 | edge = make_pair(v[0], v[1]); |
| 841 | if (boundary.find(edge) == boundary.end()) |
| 842 | { |
| 843 | twoNeighbors = neighborMap[edge]; |
| 844 | neighbor = (twoNeighbors.first == i ? twoNeighbors.second : twoNeighbors.first); |
| 845 | if (cavity2.find(neighbor) == cavity2.end()) |
| 846 | { |
| 847 | cavity2.insert(neighbor); |
| 848 | q.push(neighbor); |
| 849 | } |
| 850 | } |
| 851 | edge = make_pair(v[1], v[2]); |
| 852 | if (boundary.find(edge) == boundary.end()) |
| 853 | { |
| 854 | twoNeighbors = neighborMap[edge]; |
| 855 | neighbor = (twoNeighbors.first == i ? twoNeighbors.second : twoNeighbors.first); |
| 856 | if (cavity2.find(neighbor) == cavity2.end()) |
| 857 | { |
| 858 | cavity2.insert(neighbor); |
| 859 | q.push(neighbor); |
| 860 | } |
| 861 | } |
| 862 | edge = make_pair(v[0], v[2]); |
| 863 | if (boundary.find(edge) == boundary.end()) |
| 864 | { |
| 865 | twoNeighbors = neighborMap[edge]; |
| 866 | neighbor = (twoNeighbors.first == i ? twoNeighbors.second : twoNeighbors.first); |
| 867 | if (cavity2.find(neighbor) == cavity2.end()) |
| 868 | { |
| 869 | cavity2.insert(neighbor); |
| 870 | q.push(neighbor); |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | vector<UTriKey> holeBoundary; |
| 875 | holeBoundary.clear(); |
| 876 | for (set<int>::iterator i = cavity1.begin(); i != cavity1.end(); i++) |
| 877 | { |
| 878 | holeBoundary.push_back(faceSet[*i]); |
| 879 | } |
| 880 | bool ret = fillHole(holeBoundary); |
| 881 | holeBoundary.clear(); |
| 882 | for (set<int>::iterator i = cavity2.begin(); i != cavity2.end(); i++) |
| 883 | { |
| 884 | holeBoundary.push_back(faceSet[*i]); |
| 885 | } |
| 886 | ret = ret && fillHole(holeBoundary); |
| 887 | return ret; |
| 888 | } |
| 889 | |
| 890 | bool TetMesher::fillHole(std::vector<UTriKey>& holeBoundary) |
| 891 | { |
| 892 | vector<UTriKey> holeAndBottom; |
| 893 | holeAndBottom.clear(); |
| 894 | for (set<int>::iterator i = region.begin(); i != region.end(); i++) |
| 895 | { |
| 896 | const ObjMesh::Face *face = objMesh->getGroupHandle(0)->getFaceHandle(*i); |
| 897 | holeAndBottom.push_back(UTriKey(face->getVertexHandle(0)->getPositionIndex(), face->getVertexHandle(1)->getPositionIndex(), face->getVertexHandle(2)->getPositionIndex())); |
| 898 | } |
| 899 | for (vector<UTriKey>::iterator i = holeBoundary.begin(); i != holeBoundary.end(); i++) |
| 900 | { |
| 901 | holeAndBottom.push_back(UTriKey((*i)[0], (*i)[1], (*i)[2])); |
| 902 | } |
| 903 | |
| 904 | map<int, int> vertexMap; |
| 905 | vertexMap.clear(); |
| 906 | vector<int> vertex_reverse; |
| 907 | vertex_reverse.clear(); |
| 908 | ObjMesh * mesh = new ObjMesh; |
| 909 | mesh->addGroup("bottom"); |
| 910 | mesh->addGroup("hole"); |
| 911 | for (unsigned i = 0; i < holeAndBottom.size(); i++) |
| 912 | { |
| 913 | int v[3] = { holeAndBottom[i][0], holeAndBottom[i][1], holeAndBottom[i][2] }; |
| 914 | for (int j = 0; j < 3; j++) |
| 915 | { |
| 916 | if (vertexMap.find(v[j]) == vertexMap.end()) |
| 917 | { |
| 918 | mesh->addVertexPosition(objMesh->getPosition(v[j])); |
| 919 | vertexMap.insert(make_pair(v[j], vertexMap.size())); |
| 920 | vertex_reverse.push_back(v[j]); |
| 921 | } |
| 922 | v[j] = vertexMap[v[j]]; |
| 923 | } |
| 924 | } |
| 925 | ObjMesh::Group * group = (ObjMesh::Group*) mesh->getGroupHandle(1); |
| 926 | for (unsigned i = 0; i < holeBoundary.size(); i++) |
| 927 | { |
| 928 | int v[3] = { vertexMap[holeBoundary[i][0]], vertexMap[holeBoundary[i][1]], vertexMap[holeBoundary[i][2]] }; |
| 929 | ObjMesh::Face face; |
| 930 | face.addVertex(ObjMesh::Vertex(v[0])); |
| 931 | face.addVertex(ObjMesh::Vertex(v[1])); |
| 932 | face.addVertex(ObjMesh::Vertex(v[2])); |
| 933 | group->addFace(face); |
| 934 | } |
| 935 | group = (ObjMesh::Group*) mesh->getGroupHandle(0); |
| 936 | for (set<int>::iterator i = region.begin(); i != region.end(); i++) |
| 937 | { |
| 938 | const ObjMesh::Face *face = objMesh->getGroupHandle(0)->getFaceHandle(*i); |
| 939 | int v[3] = { vertexMap[face->getVertexHandle(0)->getPositionIndex()], vertexMap[face->getVertexHandle(1)->getPositionIndex()], vertexMap[face->getVertexHandle(2)->getPositionIndex()] }; |
| 940 | ObjMesh::Face _face; |
| 941 | _face.addVertex(ObjMesh::Vertex(v[0])); |
| 942 | _face.addVertex(ObjMesh::Vertex(v[1])); |
| 943 | _face.addVertex(ObjMesh::Vertex(v[2])); |
| 944 | group->addFace(_face); |
| 945 | } |
| 946 | vector<int> groupIndices; |
| 947 | groupIndices.push_back(0), groupIndices.push_back(1); |
| 948 | mesh->mergeGroups(groupIndices); |
| 949 | |
| 950 | ObjMeshOrientable * mesh_orient = NULL; |
| 951 | try |
| 952 | { |
| 953 | mesh_orient = new ObjMeshOrientable(mesh, 1, NULL, 0); |
| 954 | } |
| 955 | catch (int) |
| 956 | { |
| 957 | delete mesh; |
| 958 | delete mesh_orient; |
| 959 | throw 1; |
| 960 | } |
| 961 | |
| 962 | ObjMesh * meshToDelete = mesh; |
| 963 | mesh = mesh_orient->GenerateOrientedMesh(); |
| 964 | |
| 965 | delete mesh_orient; |
| 966 | delete meshToDelete; |
| 967 | |
| 968 | group = (ObjMesh::Group*) mesh->getGroupHandle(0); |
| 969 | if (mesh->computeVolume() < 0) |
| 970 | { |
| 971 | ObjMesh::Group group_2("hole"); |
| 972 | for (unsigned i = 0; i < group->getNumFaces(); i++) |
| 973 | { |
| 974 | const ObjMesh::Face *face = group->getFaceHandle(i); |
| 975 | ObjMesh::Face face_2; |
| 976 | face_2.addVertex(face->getVertex(0)); |
| 977 | face_2.addVertex(face->getVertex(2)); |
| 978 | face_2.addVertex(face->getVertex(1)); |
| 979 | group_2.addFace(face_2); |
| 980 | } |
| 981 | mesh->removeAllGroups(); |
| 982 | mesh->addGroup(group_2); |
| 983 | } |
| 984 | |
| 985 | const int maxFaceRecoveryDepth = 10; |
| 986 | if (faceRecoveryDepth < maxFaceRecoveryDepth) |
| 987 | { |
| 988 | TetMesher mesher; |
| 989 | mesher.faceRecoveryDepth = faceRecoveryDepth + 1; |
| 990 | mesher.objMesh = mesh; |
| 991 | mesher.initializeCDT(false); |
| 992 | for (DelaunayMesher::BallCIter itr = mesher.delaunay.getBallSetBegin(); itr != mesher.delaunay.getBallSetEnd(); itr++) |
| 993 | { |
| 994 | if ((*itr)->isInfinite()) |
| 995 | continue; |
| 996 | vector<int> tetIndex; |
| 997 | tetIndex.resize(4); |
| 998 | for (int j = 0; j < 4; j++) |
| 999 | { |
| 1000 | tetIndex[j] = vertex_reverse[(*itr)->getVtx(j)]; |
| 1001 | } |
| 1002 | toAdd.push_back(tetIndex); |
| 1003 | } |
| 1004 | return true; |
| 1005 | } |
| 1006 | else |
| 1007 | return false; |
| 1008 | } |
| 1009 | |
| 1010 | TetMesher::DelaunayBallWithRefineInfo::DelaunayBallWithRefineInfo(const DelaunayMesher::DelaunayBall& parent) |
| 1011 | : DelaunayMesher::DelaunayBall(parent) |
| 1012 | { |
| 1013 | // compute minimal dihedral |
| 1014 | if (isRegular()) |
| 1015 | { |
| 1016 | Vec3d v0 = this->parent.getVertex(v[0]); |
| 1017 | Vec3d v1 = this->parent.getVertex(v[1]); |
| 1018 | Vec3d v2 = this->parent.getVertex(v[2]); |
| 1019 | Vec3d v3 = this->parent.getVertex(v[3]); |
| 1020 | Vec3d p0 = v1 - v0; |
| 1021 | Vec3d p1 = v2 - v0; |
| 1022 | Vec3d p2 = v3 - v0; |
| 1023 | if (dot(cross(p0, p1), p2) < 0) |
| 1024 | { |
| 1025 | swap(v2, v3); |
| 1026 | swap(p1, p2); |
| 1027 | } |
| 1028 | Vec3d n0 = cross(p0, p1); |
| 1029 | Vec3d n1 = cross(p1, p2); |
| 1030 | Vec3d n2 = cross(p2, p0); |
| 1031 | |
| 1032 | Vec3d p3 = v3 - v1; |
| 1033 | Vec3d p4 = v2 - v1; |
| 1034 | Vec3d n3 = cross(p3, p4); |
| 1035 | n0.normalize(); |
| 1036 | n1.normalize(); |
| 1037 | n2.normalize(); |
| 1038 | n3.normalize(); |
| 1039 | double m = dot(n0, n1); |
| 1040 | m = min(m, dot(n0, n2)); |
| 1041 | m = min(m, dot(n0, n3)); |
| 1042 | m = min(m, dot(n1, n2)); |
| 1043 | m = min(m, dot(n1, n3)); |
| 1044 | m = min(m, dot(n2, n3)); |
| 1045 | minDihedral = acos(-m) / M_PI * 180; |
| 1046 | |
| 1047 | double minEdge2 = (std::min)((std::min)(len2(p0), len2(p1)), len2(p2)); |
| 1048 | minEdge2 = (std::min)((std::min)(len2(p3), len2(p4)), minEdge2); |
| 1049 | minEdge2 = (std::min)(len2(v3 - v2), minEdge2); |
| 1050 | edgeQuality = sqrt(len2(v0 - center) / minEdge2); |
| 1051 | } |
| 1052 | else |
| 1053 | { |
| 1054 | minDihedral = NAN; |
| 1055 | edgeQuality = NAN; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | bool TetMesher::isTooCloseToOtherVertices(const Vec3d& r, const double minimalDist) const |
| 1060 | { |
| 1061 | double minimalDist2 = minimalDist * minimalDist; |
| 1062 | for (unsigned int i = 0; i < delaunay.getNumVertices(); i++) |
| 1063 | if (len2(r - delaunay.getVertex(i)) < minimalDist2) |
| 1064 | return true; |
| 1065 | return false; |
| 1066 | } |
| 1067 | |
| 1068 | |
| 1069 | TetMesh * TetMesher::TetMeshWithRefineInfo::getTetMesh() |
| 1070 | { |
| 1071 | TetMesh * tetMesh; // The return tet mesh |
| 1072 | if (edgeRefineBalls.empty()) // No tets, almost impossible to trigger |
| 1073 | tetMesh = new TetMesh(0, (double*)NULL, 0, (int*)NULL); |
| 1074 | else |
| 1075 | { |
| 1076 | //const DelaunayMesher * delaunayMesher = &(*this->begin())->parent; |
| 1077 | int numVertices = delaunayMesher.getNumVertices(); |
| 1078 | double * vertex = (double *)malloc(3 * sizeof(double) * numVertices); |
| 1079 | for (int i = 0; i < numVertices; i++) |
| 1080 | delaunayMesher.getVertex(i).convertToArray(vertex + 3 * i); |
| 1081 | int numElements = edgeRefineBalls.size(); |
| 1082 | int * elements = (int *)malloc(4 * sizeof(int) * numElements); |
| 1083 | int i = 0; |
| 1084 | for (EdgeRefineIterator itr = edgeRefineBalls.begin(); itr != edgeRefineBalls.end(); itr++) |
| 1085 | memcpy(elements + 4 * (i++), (*itr)->getVertices(), 4 * sizeof(int)); |
| 1086 | tetMesh = new TetMesh(numVertices, vertex, numElements, elements); |
| 1087 | free(vertex); |
| 1088 | free(elements); |
| 1089 | } |
| 1090 | return tetMesh; |
| 1091 | } |
| 1092 | |
| 1093 | TetMesher::TetMeshWithRefineInfo::TetMeshWithRefineInfo(const DelaunayMesher & delaunayMesher) |
| 1094 | : enabledAngleRefine(false), delaunayMesher(delaunayMesher) {} |
| 1095 | |
| 1096 | TetMesher::TetMeshWithRefineInfo::~TetMeshWithRefineInfo() |
| 1097 | { |
| 1098 | for (EdgeRefineIterator itr = edgeRefineBalls.begin(); itr != edgeRefineBalls.end(); itr++) |
| 1099 | delete *itr; |
| 1100 | } |
| 1101 | |
| 1102 | // Insert a tet during the refine process |
| 1103 | void TetMesher::TetMeshWithRefineInfo::insert(const DelaunayMesher::DelaunayBall * delaunayBall) |
| 1104 | { |
| 1105 | DelaunayBallWithRefineInfo * delaunayBallWithRefineInfo = new DelaunayBallWithRefineInfo(*delaunayBall); |
| 1106 | edgeRefineMap.insert(make_pair(delaunayBall, edgeRefineBalls.insert(delaunayBallWithRefineInfo).first)); |
| 1107 | if (enabledAngleRefine) angleRefineMap.insert(make_pair(delaunayBall, angleRefineBalls.insert(delaunayBallWithRefineInfo).first)); |
| 1108 | } |
| 1109 | |
| 1110 | // remove a tet during the refine process |
| 1111 | void TetMesher::TetMeshWithRefineInfo::remove(const DelaunayMesher::DelaunayBall * delaunayBall) |
| 1112 | { |
| 1113 | map<const DelaunayMesher::DelaunayBall *, EdgeRefineIterator, DelaunayMesher::DelaunayBallCompare>::iterator itr = edgeRefineMap.find(delaunayBall); |
| 1114 | delete *(itr->second); |
| 1115 | edgeRefineBalls.erase(itr->second); |
| 1116 | edgeRefineMap.erase(itr); |
| 1117 | if (enabledAngleRefine) |
| 1118 | { |
| 1119 | itr = angleRefineMap.find(delaunayBall); |
| 1120 | angleRefineBalls.erase(itr->second); |
| 1121 | angleRefineMap.erase(itr); |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | // Inser a tet |
| 1126 | void TetMesher::insertTet(const int* v) |
| 1127 | { |
| 1128 | insertFace(v[0], v[1], v[2]); |
| 1129 | insertFace(v[0], v[1], v[3]); |
| 1130 | insertFace(v[0], v[3], v[2]); |
| 1131 | insertFace(v[3], v[1], v[2]); |
| 1132 | } |
| 1133 | |
| 1134 | // Remove a tet |
| 1135 | void TetMesher::removeTet(const int* v) |
| 1136 | { |
| 1137 | removeFace(v[0], v[1], v[2]); |
| 1138 | removeFace(v[0], v[1], v[3]); |
| 1139 | removeFace(v[0], v[3], v[2]); |
| 1140 | removeFace(v[3], v[1], v[2]); |
| 1141 | } |
| 1142 | |
| 1143 | int TetMesher::segmentRecovery() |
| 1144 | { |
| 1145 | vector <int> groupID; |
| 1146 | for (unsigned i = 0; i < objMesh->getNumGroups(); i++) |
| 1147 | groupID.push_back(i); |
| 1148 | objMesh->mergeGroups(groupID); |
| 1149 | |
| 1150 | // Insert all edges of the triangular surface to a queue; |
| 1151 | set <UEdgeKey> edgeSet; |
| 1152 | list <OEdgeKey> edgeStack; |
| 1153 | ObjMesh::Group & group = *objMesh->getGroupHandle(0); |
| 1154 | for (unsigned int i = 0; i < group.getNumFaces(); i++) |
| 1155 | { |
| 1156 | const ObjMesh::Face & face = group.getFace(i); |
| 1157 | int v0 = face.getVertex(0).getPositionIndex(); |
| 1158 | int v1 = face.getVertex(1).getPositionIndex(); |
| 1159 | int v2 = face.getVertex(2).getPositionIndex(); |
| 1160 | |
| 1161 | // Insert three edges into the queu, no orientation |
| 1162 | assert(v0 != v1); |
| 1163 | assert(v2 != v1); |
| 1164 | assert(v0 != v2); |
| 1165 | if (edgeSet.find(UEdgeKey(v0, v1)) == edgeSet.end()) |
| 1166 | { |
| 1167 | edgeSet.insert(UEdgeKey(v0, v1)); |
| 1168 | edgeStack.push_front(OEdgeKey(v0, v1)); |
| 1169 | } |
| 1170 | if (edgeSet.find(UEdgeKey(v1, v2)) == edgeSet.end()) |
| 1171 | { |
| 1172 | edgeSet.insert(UEdgeKey(v1, v2)); |
| 1173 | edgeStack.push_front(OEdgeKey(v1, v2)); |
| 1174 | } |
| 1175 | if (edgeSet.find(UEdgeKey(v2, v0)) == edgeSet.end()) |
| 1176 | { |
| 1177 | edgeSet.insert(UEdgeKey(v2, v0)); |
| 1178 | edgeStack.push_front(OEdgeKey(v2, v0)); |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | int level = 2; |
| 1183 | |
| 1184 | while (true) |
| 1185 | { |
| 1186 | int sum = 0; |
| 1187 | int flipped = 0; |
| 1188 | list <OEdgeKey>::iterator itr = edgeStack.begin(); |
| 1189 | while (itr != edgeStack.end()) |
| 1190 | { |
| 1191 | OEdgeKey edge = *itr; |
| 1192 | int result; |
| 1193 | if (level < 1000) |
| 1194 | { |
| 1195 | result = delaunay.segmentRecoveryUsingFlip(OEdgeKey(edge[0], edge[1]), level); |
| 1196 | if (result != 0) |
| 1197 | { |
| 1198 | fflush(NULL); |
| 1199 | result = delaunay.segmentRecoveryUsingFlip(OEdgeKey(edge[1], edge[0]), level); |
| 1200 | } |
| 1201 | } |
| 1202 | else |
| 1203 | { |
| 1204 | result = delaunay.segmentRecoveryUsingSteinerPoint(OEdgeKey(edge[0], edge[1])); |
| 1205 | result = delaunay.segmentRecoveryUsingFlip(OEdgeKey(edge[0], edge[1]), level); |
| 1206 | } |
| 1207 | if (result ) |
| 1208 | { |
| 1209 | } |
| 1210 | if (!result) |
| 1211 | flipped++; |
| 1212 | sum++; |
| 1213 | //cout << "Next edge" << " " << result << endl; |
| 1214 | //if (!result) |
| 1215 | if (!result) |
| 1216 | edgeStack.erase(itr++); |
| 1217 | else |
| 1218 | itr++; |
| 1219 | continue; |
| 1220 | } |
| 1221 | // No missing edges |
| 1222 | if (edgeStack.empty()) |
| 1223 | break; |
| 1224 | |
| 1225 | // Increase the level of flipping edges |
| 1226 | if (level < 4) |
| 1227 | level++; |
| 1228 | // Using steiner points to recover the edges |
| 1229 | else if (level < 10000) |
| 1230 | level = 10000; |
| 1231 | // Fail to recover |
| 1232 | else |
| 1233 | break; |
| 1234 | } |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | bool TetMesher::TetMeshWithRefineInfo::DelaunayBallEdgeRefineCMP |
| 1239 | ::operator ()(const DelaunayBallWithRefineInfo* const & p1, const DelaunayBallWithRefineInfo* const & p2) const |
| 1240 | { |
| 1241 | if (p1->edgeQuality > p2->edgeQuality) |
| 1242 | return true; |
| 1243 | if (p1->edgeQuality < p2->edgeQuality) |
| 1244 | return false; |
| 1245 | return defaultComparor(p1, p2); |
| 1246 | } |
| 1247 | |
| 1248 | bool TetMesher::TetMeshWithRefineInfo::DelaunayBallAngleRefineCMP |
| 1249 | ::operator ()(const DelaunayBallWithRefineInfo* const & p1, const DelaunayBallWithRefineInfo* const & p2) const |
| 1250 | { |
| 1251 | if (p1->minDihedral < p2->minDihedral) |
| 1252 | return true; |
| 1253 | if (p1->minDihedral > p2->minDihedral) |
| 1254 | return false; |
| 1255 | return defaultComparor(p1, p2); |
| 1256 | } |
| 1257 | |
| 1258 | int TetMesher::flipSurface() |
| 1259 | { |
| 1260 | //delaunay.getMesh()->save("beforeflipsurface.veg"); |
| 1261 | edgesInTet.clear(); |
| 1262 | trianglesInTet.clear(); |
| 1263 | for (DelaunayMesher::BallCIter itr = delaunay.getBallSetBegin(); itr != delaunay.getBallSetEnd(); itr++) |
| 1264 | if ((*itr)->isRegular()) |
| 1265 | insertTet((*itr)->getVertices()); |
| 1266 | |
| 1267 | set <UEdgeKey> edgeQueue; |
| 1268 | ObjMesh::Group & group = *objMesh->getGroupHandle(0); |
| 1269 | for (unsigned int i = 0; i < group.getNumFaces(); i++) |
| 1270 | { |
| 1271 | const ObjMesh::Face & face = group.getFace(i); |
| 1272 | int v0 = face.getVertex(0).getPositionIndex(); |
| 1273 | int v1 = face.getVertex(1).getPositionIndex(); |
| 1274 | int v2 = face.getVertex(2).getPositionIndex(); |
| 1275 | |
| 1276 | // Insert three edges into the queue, no orientation |
| 1277 | assert(v0 != v1); |
| 1278 | assert(v2 != v1); |
| 1279 | assert(v0 != v2); |
| 1280 | if (edgesInTet.find(UEdgeKey(v0, v1)) == edgesInTet.end()) |
| 1281 | { |
| 1282 | edgeQueue.insert(UEdgeKey(v0, v1)); |
| 1283 | } |
| 1284 | if (edgesInTet.find(UEdgeKey(v1, v2)) == edgesInTet.end()) |
| 1285 | edgeQueue.insert(UEdgeKey(v1, v2)); |
| 1286 | if (edgesInTet.find(UEdgeKey(v2, v0)) == edgesInTet.end()) |
| 1287 | edgeQueue.insert(UEdgeKey(v2, v0)); |
| 1288 | } |
| 1289 | while (true) |
| 1290 | { |
| 1291 | bool recover = false; |
| 1292 | set <UEdgeKey>::iterator itr = edgeQueue.begin(); |
| 1293 | while (itr != edgeQueue.end()) |
| 1294 | { |
| 1295 | UEdgeKey edge = *itr; |
| 1296 | int f0 = -1, f1 = -1; |
| 1297 | int v0 = -1, v1 = -1; |
| 1298 | for (int i = group.getNumFaces() - 1; i >= 0; i--) |
| 1299 | { |
| 1300 | const ObjMesh::Face & face = group.getFace(i); |
| 1301 | int p0 = face.getVertex(0).getPositionIndex(), p1 = face.getVertex(1).getPositionIndex(), p2 = face.getVertex(2).getPositionIndex(); |
| 1302 | if (p0 == edge[0] && p1 == edge[1]) |
| 1303 | { |
| 1304 | f0 = i; v0 = p2; |
| 1305 | } |
| 1306 | else if (p1 == edge[0] && p0 == edge[1]) |
| 1307 | { |
| 1308 | f1 = i; v1 = p2; |
| 1309 | } |
| 1310 | else if (p1 == edge[0] && p2 == edge[1]) |
| 1311 | { |
| 1312 | f0 = i; v0 = p0; |
| 1313 | } |
| 1314 | else if (p2 == edge[0] && p1 == edge[1]) |
| 1315 | { |
| 1316 | f1 = i; v1 = p0; |
| 1317 | } |
| 1318 | else if (p2 == edge[0] && p0 == edge[1]) |
| 1319 | { |
| 1320 | f0 = i; v0 = p1; |
| 1321 | } |
| 1322 | else if (p0 == edge[0] && p2 == edge[1]) |
| 1323 | { |
| 1324 | f1 = i; v1 = p1; |
| 1325 | } |
| 1326 | } |
| 1327 | assert(f0 != -1 && f1 != -1); |
| 1328 | if (edgesInTet.find(UEdgeKey(v0, v1)) != edgesInTet.end()) |
| 1329 | { |
| 1330 | //cout << "Flipping surface " << edge[0] << " " << edge[1] << endl; |
| 1331 | group.removeFace((std::max)(f0, f1)); |
| 1332 | group.removeFace((std::min)(f0, f1)); |
| 1333 | group.addFace(ObjMesh::Face(ObjMesh::Vertex(edge[0]), ObjMesh::Vertex(v1), ObjMesh::Vertex(v0))); |
| 1334 | group.addFace(ObjMesh::Face(ObjMesh::Vertex(edge[1]), ObjMesh::Vertex(v0), ObjMesh::Vertex(v1))); |
| 1335 | recover = true; |
| 1336 | edgeQueue.erase(itr++); |
| 1337 | } |
| 1338 | else |
| 1339 | itr++; |
| 1340 | |
| 1341 | } |
| 1342 | if (!recover) |
| 1343 | { |
| 1344 | return edgeQueue.size(); |
| 1345 | } |
| 1346 | } |
| 1347 | return 0; |
| 1348 | |
| 1349 | //objMesh->removeAllGroups(); |
| 1350 | //objMesh->addGroup(group); |
| 1351 | //objMesh->save("flipped.obj"); |
| 1352 | } |