00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "triangle.H"
00028 #include "triSurface.H"
00029 #include "triSurfaceTools.H"
00030 #include "triSurfaceSearch.H"
00031 #include "argList.H"
00032 #include "OFstream.H"
00033 #include "surfaceIntersection.H"
00034 #include "SortableList.H"
00035 #include "PatchTools.H"
00036
00037 using namespace Foam;
00038
00039
00040 bool validTri(const bool verbose, const triSurface& surf, const label faceI)
00041 {
00042
00043
00044 const labelledTri& f = surf[faceI];
00045
00046 if
00047 (
00048 (f[0] < 0) || (f[0] >= surf.points().size())
00049 || (f[1] < 0) || (f[1] >= surf.points().size())
00050 || (f[2] < 0) || (f[2] >= surf.points().size())
00051 )
00052 {
00053 WarningIn("validTri(const triSurface&, const label)")
00054 << "triangle " << faceI << " vertices " << f
00055 << " uses point indices outside point range 0.."
00056 << surf.points().size()-1 << endl;
00057
00058 return false;
00059 }
00060
00061 if ((f[0] == f[1]) || (f[0] == f[2]) || (f[1] == f[2]))
00062 {
00063 WarningIn("validTri(const triSurface&, const label)")
00064 << "triangle " << faceI
00065 << " uses non-unique vertices " << f
00066 << " coords:" << f.points(surf.points())
00067 << endl;
00068 return false;
00069 }
00070
00071
00072
00073 const labelList& fFaces = surf.faceFaces()[faceI];
00074
00075
00076
00077 forAll(fFaces, i)
00078 {
00079 label nbrFaceI = fFaces[i];
00080
00081 if (nbrFaceI <= faceI)
00082 {
00083
00084 continue;
00085 }
00086
00087 const labelledTri& nbrF = surf[nbrFaceI];
00088
00089 if
00090 (
00091 ((f[0] == nbrF[0]) || (f[0] == nbrF[1]) || (f[0] == nbrF[2]))
00092 && ((f[1] == nbrF[0]) || (f[1] == nbrF[1]) || (f[1] == nbrF[2]))
00093 && ((f[2] == nbrF[0]) || (f[2] == nbrF[1]) || (f[2] == nbrF[2]))
00094 )
00095 {
00096 WarningIn("validTri(const triSurface&, const label)")
00097 << "triangle " << faceI << " vertices " << f
00098 << " has the same vertices as triangle " << nbrFaceI
00099 << " vertices " << nbrF
00100 << " coords:" << f.points(surf.points())
00101 << endl;
00102
00103 return false;
00104 }
00105 }
00106 return true;
00107 }
00108
00109
00110 labelList countBins
00111 (
00112 const scalar min,
00113 const scalar max,
00114 const label nBins,
00115 const scalarField& vals
00116 )
00117 {
00118 scalar dist = nBins/(max - min);
00119
00120 labelList binCount(nBins, 0);
00121
00122 forAll(vals, i)
00123 {
00124 scalar val = vals[i];
00125
00126 label index = -1;
00127
00128 if (Foam::mag(val - min) < SMALL)
00129 {
00130 index = 0;
00131 }
00132 else if (val >= max - SMALL)
00133 {
00134 index = nBins - 1;
00135 }
00136 else
00137 {
00138 index = label((val - min)*dist);
00139
00140 if ((index < 0) || (index >= nBins))
00141 {
00142 WarningIn
00143 (
00144 "countBins(const scalar, const scalar, const label"
00145 ", const scalarField&)"
00146 ) << "value " << val << " at index " << i
00147 << " outside range " << min << " .. " << max << endl;
00148
00149 if (index < 0)
00150 {
00151 index = 0;
00152 }
00153 else
00154 {
00155 index = nBins - 1;
00156 }
00157 }
00158 }
00159 binCount[index]++;
00160 }
00161
00162 return binCount;
00163 }
00164
00165
00166
00167
00168
00169 int main(int argc, char *argv[])
00170 {
00171 argList::noParallel();
00172
00173 argList::validArgs.clear();
00174 argList::validArgs.append("surface file");
00175 argList::validOptions.insert("checkSelfIntersection", "");
00176 argList::validOptions.insert("verbose", "");
00177 argList args(argc, argv);
00178
00179 bool checkSelfIntersection = args.optionFound("checkSelfIntersection");
00180 bool verbose = args.optionFound("verbose");
00181
00182 fileName surfFileName(args.additionalArgs()[0]);
00183 Pout<< "Reading surface from " << surfFileName << " ..." << nl << endl;
00184
00185
00186
00187
00188
00189 triSurface surf(surfFileName);
00190
00191
00192 Pout<< "Statistics:" << endl;
00193 surf.writeStats(Pout);
00194 Pout<< endl;
00195
00196
00197
00198
00199
00200 {
00201 labelList regionSize(surf.patches().size(), 0);
00202
00203 forAll(surf, faceI)
00204 {
00205 label region = surf[faceI].region();
00206
00207 if (region < 0 || region >= regionSize.size())
00208 {
00209 WarningIn(args.executable())
00210 << "Triangle " << faceI << " vertices " << surf[faceI]
00211 << " has region " << region << " which is outside the range"
00212 << " of regions 0.." << surf.patches().size()-1
00213 << endl;
00214 }
00215 else
00216 {
00217 regionSize[region]++;
00218 }
00219 }
00220
00221 Pout<< "Region\tSize" << nl
00222 << "------\t----" << nl;
00223 forAll(surf.patches(), patchI)
00224 {
00225 Pout<< surf.patches()[patchI].name() << '\t'
00226 << regionSize[patchI] << nl;
00227 }
00228 Pout<< nl << endl;
00229 }
00230
00231
00232
00233
00234
00235 {
00236 DynamicList<label> illegalFaces(surf.size()/100 + 1);
00237
00238 forAll(surf, faceI)
00239 {
00240 if (!validTri(verbose, surf, faceI))
00241 {
00242 illegalFaces.append(faceI);
00243 }
00244 }
00245
00246 if (illegalFaces.size())
00247 {
00248 Pout<< "Surface has " << illegalFaces.size()
00249 << " illegal triangles." << endl;
00250
00251 OFstream str("illegalFaces");
00252 Pout<< "Dumping conflicting face labels to " << str.name() << endl
00253 << "Paste this into the input for surfaceSubset" << endl;
00254 str << illegalFaces;
00255 }
00256 else
00257 {
00258 Pout<< "Surface has no illegal triangles." << endl;
00259 }
00260 Pout<< endl;
00261 }
00262
00263
00264
00265
00266
00267
00268 {
00269 scalarField triQ(surf.size(), 0);
00270 forAll(surf, faceI)
00271 {
00272 const labelledTri& f = surf[faceI];
00273
00274 if (f[0] == f[1] || f[0] == f[2] || f[1] == f[2])
00275 {
00276
00277
00278
00279 }
00280 else
00281 {
00282 triPointRef tri
00283 (
00284 surf.points()[f[0]],
00285 surf.points()[f[1]],
00286 surf.points()[f[2]]
00287 );
00288
00289 vector ba(tri.b() - tri.a());
00290 ba /= mag(ba) + VSMALL;
00291
00292 vector ca(tri.c() - tri.a());
00293 ca /= mag(ca) + VSMALL;
00294
00295 if (mag(ba&ca) > 1-1E-3)
00296 {
00297 triQ[faceI] = SMALL;
00298 }
00299 else
00300 {
00301 triQ[faceI] = triPointRef
00302 (
00303 surf.points()[f[0]],
00304 surf.points()[f[1]],
00305 surf.points()[f[2]]
00306 ).quality();
00307 }
00308 }
00309 }
00310
00311 labelList binCount = countBins(0, 1, 20, triQ);
00312
00313 Pout<< "Triangle quality (equilateral=1, collapsed=0):"
00314 << endl;
00315
00316
00317 OSstream& os = Pout;
00318 os.width(4);
00319
00320 scalar dist = (1.0 - 0.0)/20.0;
00321 scalar min = 0;
00322 forAll(binCount, binI)
00323 {
00324 Pout<< " " << min << " .. " << min+dist << " : "
00325 << 1.0/surf.size() * binCount[binI]
00326 << endl;
00327 min += dist;
00328 }
00329 Pout<< endl;
00330
00331 label minIndex = findMin(triQ);
00332 label maxIndex = findMax(triQ);
00333
00334 Pout<< " min " << triQ[minIndex] << " for triangle " << minIndex
00335 << nl
00336 << " max " << triQ[maxIndex] << " for triangle " << maxIndex
00337 << nl
00338 << endl;
00339
00340
00341 if (triQ[minIndex] < SMALL)
00342 {
00343 WarningIn(args.executable()) << "Minimum triangle quality is "
00344 << triQ[minIndex] << ". This might give problems in"
00345 << " self-intersection testing later on." << endl;
00346 }
00347
00348
00349 {
00350 DynamicList<label> problemFaces(surf.size()/100+1);
00351
00352 forAll(triQ, faceI)
00353 {
00354 if (triQ[faceI] < 1E-11)
00355 {
00356 problemFaces.append(faceI);
00357 }
00358 }
00359 OFstream str("badFaces");
00360
00361 Pout<< "Dumping bad quality faces to " << str.name() << endl
00362 << "Paste this into the input for surfaceSubset" << nl
00363 << nl << endl;
00364
00365 str << problemFaces;
00366 }
00367 }
00368
00369
00370
00371
00372
00373 {
00374 const edgeList& edges = surf.edges();
00375 const pointField& localPoints = surf.localPoints();
00376
00377 scalarField edgeMag(edges.size());
00378
00379 forAll(edges, edgeI)
00380 {
00381 edgeMag[edgeI] = edges[edgeI].mag(localPoints);
00382 }
00383
00384 label minEdgeI = findMin(edgeMag);
00385 label maxEdgeI = findMax(edgeMag);
00386
00387 const edge& minE = edges[minEdgeI];
00388 const edge& maxE = edges[maxEdgeI];
00389
00390
00391 Pout<< "Edges:" << nl
00392 << " min " << edgeMag[minEdgeI] << " for edge " << minEdgeI
00393 << " points " << localPoints[minE[0]] << localPoints[minE[1]]
00394 << nl
00395 << " max " << edgeMag[maxEdgeI] << " for edge " << maxEdgeI
00396 << " points " << localPoints[maxE[0]] << localPoints[maxE[1]]
00397 << nl
00398 << endl;
00399 }
00400
00401
00402
00403
00404
00405 {
00406 const edgeList& edges = surf.edges();
00407 const pointField& localPoints = surf.localPoints();
00408
00409 const boundBox bb(localPoints);
00410 scalar smallDim = 1E-6 * bb.mag();
00411
00412 Pout<< "Checking for points less than 1E-6 of bounding box ("
00413 << bb.span() << " meter) apart."
00414 << endl;
00415
00416
00417 SortableList<scalar> sortedMag(mag(localPoints));
00418
00419 label nClose = 0;
00420
00421 for (label i = 1; i < sortedMag.size(); i++)
00422 {
00423 label ptI = sortedMag.indices()[i];
00424
00425 label prevPtI = sortedMag.indices()[i-1];
00426
00427 if (mag(localPoints[ptI] - localPoints[prevPtI]) < smallDim)
00428 {
00429
00430 const labelList& pEdges = surf.pointEdges()[ptI];
00431
00432 label edgeI = -1;
00433
00434 forAll(pEdges, i)
00435 {
00436 const edge& e = edges[pEdges[i]];
00437
00438 if (e[0] == prevPtI || e[1] == prevPtI)
00439 {
00440
00441 edgeI = pEdges[i];
00442
00443 break;
00444 }
00445 }
00446
00447 nClose++;
00448
00449 if (edgeI == -1)
00450 {
00451 Pout<< " close unconnected points "
00452 << ptI << ' ' << localPoints[ptI]
00453 << " and " << prevPtI << ' '
00454 << localPoints[prevPtI]
00455 << " distance:"
00456 << mag(localPoints[ptI] - localPoints[prevPtI])
00457 << endl;
00458 }
00459 else
00460 {
00461 Pout<< " small edge between points "
00462 << ptI << ' ' << localPoints[ptI]
00463 << " and " << prevPtI << ' '
00464 << localPoints[prevPtI]
00465 << " distance:"
00466 << mag(localPoints[ptI] - localPoints[prevPtI])
00467 << endl;
00468 }
00469 }
00470 }
00471
00472 Pout<< "Found " << nClose << " nearby points." << nl
00473 << endl;
00474 }
00475
00476
00477
00478
00479
00480
00481 DynamicList<label> problemFaces(surf.size()/100 + 1);
00482
00483 const labelListList& eFaces = surf.edgeFaces();
00484
00485 label nSingleEdges = 0;
00486 forAll(eFaces, edgeI)
00487 {
00488 const labelList& myFaces = eFaces[edgeI];
00489
00490 if (myFaces.size() == 1)
00491 {
00492 problemFaces.append(myFaces[0]);
00493
00494 nSingleEdges++;
00495 }
00496 }
00497
00498 label nMultEdges = 0;
00499 forAll(eFaces, edgeI)
00500 {
00501 const labelList& myFaces = eFaces[edgeI];
00502
00503 if (myFaces.size() > 2)
00504 {
00505 forAll(myFaces, myFaceI)
00506 {
00507 problemFaces.append(myFaces[myFaceI]);
00508 }
00509
00510 nMultEdges++;
00511 }
00512 }
00513 problemFaces.shrink();
00514
00515 if ((nSingleEdges != 0) || (nMultEdges != 0))
00516 {
00517 Pout<< "Surface is not closed since not all edges connected to "
00518 << "two faces:" << endl
00519 << " connected to one face : " << nSingleEdges << endl
00520 << " connected to >2 faces : " << nMultEdges << endl;
00521
00522 Pout<< "Conflicting face labels:" << problemFaces.size() << endl;
00523
00524 OFstream str("problemFaces");
00525
00526 Pout<< "Dumping conflicting face labels to " << str.name() << endl
00527 << "Paste this into the input for surfaceSubset" << endl;
00528
00529 str << problemFaces;
00530 }
00531 else
00532 {
00533 Pout<< "Surface is closed. All edges connected to two faces." << endl;
00534 }
00535 Pout<< endl;
00536
00537
00538
00539
00540
00541
00542 labelList faceZone;
00543 label numZones = surf.markZones(boolList(surf.nEdges(), false), faceZone);
00544
00545 Pout<< "Number of unconnected parts : " << numZones << endl;
00546
00547 if (numZones > 1)
00548 {
00549 Pout<< "Splitting surface into parts ..." << endl << endl;
00550
00551 fileName surfFileNameBase(surfFileName.name());
00552
00553 for(label zone = 0; zone < numZones; zone++)
00554 {
00555 boolList includeMap(surf.size(), false);
00556
00557 forAll(faceZone, faceI)
00558 {
00559 if (faceZone[faceI] == zone)
00560 {
00561 includeMap[faceI] = true;
00562 }
00563 }
00564
00565 labelList pointMap;
00566 labelList faceMap;
00567
00568 triSurface subSurf
00569 (
00570 surf.subsetMesh
00571 (
00572 includeMap,
00573 pointMap,
00574 faceMap
00575 )
00576 );
00577
00578 fileName subFileName
00579 (
00580 surfFileNameBase.lessExt()
00581 + "_"
00582 + name(zone)
00583 + ".ftr"
00584 );
00585
00586 Pout<< "writing part " << zone << " size " << subSurf.size()
00587 << " to " << subFileName << endl;
00588
00589 subSurf.write(subFileName);
00590 }
00591
00592 return 0;
00593 }
00594
00595
00596
00597
00598
00599
00600 labelHashSet borderEdge(surf.size()/1000);
00601 PatchTools::checkOrientation(surf, false, &borderEdge);
00602
00603
00604
00605
00606 labelList normalZone;
00607 label numNormalZones = PatchTools::markZones(surf, borderEdge, normalZone);
00608
00609 Pout<< endl
00610 << "Number of zones (connected area with consistent normal) : "
00611 << numNormalZones << endl;
00612
00613 if (numNormalZones > 1)
00614 {
00615 Pout<< "More than one normal orientation." << endl;
00616 }
00617 Pout<< endl;
00618
00619
00620
00621
00622
00623
00624 if (checkSelfIntersection)
00625 {
00626 Pout<< "Checking self-intersection." << endl;
00627
00628 triSurfaceSearch querySurf(surf);
00629 surfaceIntersection inter(querySurf);
00630
00631 if (inter.cutEdges().empty() && inter.cutPoints().empty())
00632 {
00633 Pout<< "Surface is not self-intersecting" << endl;
00634 }
00635 else
00636 {
00637 Pout<< "Surface is self-intersecting" << endl;
00638 Pout<< "Writing edges of intersection to selfInter.obj" << endl;
00639
00640 OFstream intStream("selfInter.obj");
00641 forAll(inter.cutPoints(), cutPointI)
00642 {
00643 const point& pt = inter.cutPoints()[cutPointI];
00644
00645 intStream << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z()
00646 << endl;
00647 }
00648 forAll(inter.cutEdges(), cutEdgeI)
00649 {
00650 const edge& e = inter.cutEdges()[cutEdgeI];
00651
00652 intStream << "l " << e.start()+1 << ' ' << e.end()+1 << endl;
00653 }
00654 }
00655 Pout<< endl;
00656 }
00657
00658
00659 Pout<< "End\n" << endl;
00660
00661 return 0;
00662 }
00663
00664
00665