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 "isoSurfaceCell.H"
00028 #include "dictionary.H"
00029 #include "polyMesh.H"
00030 #include "mergePoints.H"
00031 #include "tetMatcher.H"
00032 #include "syncTools.H"
00033 #include "addToRunTimeSelectionTable.H"
00034 #include "faceTriangulation.H"
00035
00036
00037
00038 namespace Foam
00039 {
00040 defineTypeNameAndDebug(isoSurfaceCell, 0);
00041 }
00042
00043
00044
00045 Foam::scalar Foam::isoSurfaceCell::isoFraction
00046 (
00047 const scalar s0,
00048 const scalar s1
00049 ) const
00050 {
00051 scalar d = s1-s0;
00052
00053 if (mag(d) > VSMALL)
00054 {
00055 return (iso_-s0)/d;
00056 }
00057 else
00058 {
00059 return -1.0;
00060 }
00061 }
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 Foam::isoSurfaceCell::cellCutType Foam::isoSurfaceCell::calcCutType
00083 (
00084 const PackedBoolList& isTet,
00085 const scalarField& cellValues,
00086 const scalarField& pointValues,
00087 const label cellI
00088 ) const
00089 {
00090 const cell& cFaces = mesh_.cells()[cellI];
00091
00092 if (isTet.get(cellI) == 1)
00093 {
00094 forAll(cFaces, cFaceI)
00095 {
00096 const face& f = mesh_.faces()[cFaces[cFaceI]];
00097
00098 for (label fp = 1; fp < f.size() - 1; fp++)
00099 {
00100 triFace tri(f[0], f[fp], f[f.fcIndex(fp)]);
00101
00102 bool aLower = (pointValues[tri[0]] < iso_);
00103 bool bLower = (pointValues[tri[1]] < iso_);
00104 bool cLower = (pointValues[tri[2]] < iso_);
00105
00106 if (aLower == bLower && aLower == cLower)
00107 {}
00108 else
00109 {
00110 return CUT;
00111 }
00112 }
00113 }
00114 return NOTCUT;
00115 }
00116 else
00117 {
00118 bool cellLower = (cellValues[cellI] < iso_);
00119
00120
00121 bool edgeCut = false;
00122
00123 forAll(cFaces, cFaceI)
00124 {
00125 const face& f = mesh_.faces()[cFaces[cFaceI]];
00126
00127
00128 forAll(f, fp)
00129 {
00130 if ((pointValues[f[fp]] < iso_) != cellLower)
00131 {
00132 edgeCut = true;
00133 break;
00134 }
00135 }
00136
00137 if (edgeCut)
00138 {
00139 break;
00140 }
00141
00142 for (label fp = 1; fp < f.size() - 1; fp++)
00143 {
00144 triFace tri(f[0], f[fp], f[f.fcIndex(fp)]);
00145
00146
00147
00148
00149
00150 bool aLower = (pointValues[tri[0]] < iso_);
00151 bool bLower = (pointValues[tri[1]] < iso_);
00152 bool cLower = (pointValues[tri[2]] < iso_);
00153
00154 if (aLower == bLower && aLower == cLower)
00155 {}
00156 else
00157 {
00158 edgeCut = true;
00159 break;
00160 }
00161 }
00162
00163 if (edgeCut)
00164 {
00165 break;
00166 }
00167 }
00168
00169 if (edgeCut)
00170 {
00171
00172
00173
00174
00175 const labelList& cPoints = mesh_.cellPoints(cellI);
00176
00177 label nPyrCuts = 0;
00178
00179 forAll(cPoints, i)
00180 {
00181 if ((pointValues[cPoints[i]] < iso_) != cellLower)
00182 {
00183 nPyrCuts++;
00184 }
00185 }
00186
00187 if (nPyrCuts == cPoints.size())
00188 {
00189 return SPHERE;
00190 }
00191 else
00192 {
00193 return CUT;
00194 }
00195 }
00196 else
00197 {
00198 return NOTCUT;
00199 }
00200 }
00201 }
00202
00203
00204 void Foam::isoSurfaceCell::calcCutTypes
00205 (
00206 const PackedBoolList& isTet,
00207 const scalarField& cVals,
00208 const scalarField& pVals
00209 )
00210 {
00211 cellCutType_.setSize(mesh_.nCells());
00212 nCutCells_ = 0;
00213 forAll(mesh_.cells(), cellI)
00214 {
00215 cellCutType_[cellI] = calcCutType(isTet, cVals, pVals, cellI);
00216
00217 if (cellCutType_[cellI] == CUT)
00218 {
00219 nCutCells_++;
00220 }
00221 }
00222
00223 if (debug)
00224 {
00225 Pout<< "isoSurfaceCell : detected " << nCutCells_
00226 << " candidate cut cells." << endl;
00227 }
00228 }
00229
00230
00231
00232
00233 Foam::labelPair Foam::isoSurfaceCell::findCommonPoints
00234 (
00235 const labelledTri& tri0,
00236 const labelledTri& tri1
00237 )
00238 {
00239 labelPair common(-1, -1);
00240
00241 label fp0 = 0;
00242 label fp1 = findIndex(tri1, tri0[fp0]);
00243
00244 if (fp1 == -1)
00245 {
00246 fp0 = 1;
00247 fp1 = findIndex(tri1, tri0[fp0]);
00248 }
00249
00250 if (fp1 != -1)
00251 {
00252
00253
00254
00255 label fp0p1 = tri0.fcIndex(fp0);
00256 label fp1p1 = tri1.fcIndex(fp1);
00257 label fp1m1 = tri1.rcIndex(fp1);
00258
00259 if (tri0[fp0p1] == tri1[fp1p1] || tri0[fp0p1] == tri1[fp1m1])
00260 {
00261 common[0] = tri0[fp0];
00262 common[1] = tri0[fp0p1];
00263 }
00264 }
00265 return common;
00266 }
00267
00268
00269
00270 Foam::point Foam::isoSurfaceCell::calcCentre(const triSurface& s)
00271 {
00272 vector sum = vector::zero;
00273
00274 forAll(s, i)
00275 {
00276 sum += s[i].centre(s.points());
00277 }
00278 return sum/s.size();
00279 }
00280
00281
00282
00283
00284 Foam::pointIndexHit Foam::isoSurfaceCell::collapseSurface
00285 (
00286 pointField& localPoints,
00287 DynamicList<labelledTri, 64>& localTris
00288 )
00289 {
00290 pointIndexHit info(false, vector::zero, localTris.size());
00291
00292 if (localTris.size() == 1)
00293 {
00294 const labelledTri& tri = localTris[0];
00295 info.setPoint(tri.centre(localPoints));
00296 info.setHit();
00297 }
00298 else if (localTris.size() == 2)
00299 {
00300
00301 const labelledTri& tri0 = localTris[0];
00302 const labelledTri& tri1 = localTris[0];
00303
00304 labelPair shared = findCommonPoints(tri0, tri1);
00305
00306 if (shared[0] != -1)
00307 {
00308 info.setPoint
00309 (
00310 0.5
00311 * (
00312 tri0.centre(localPoints)
00313 + tri1.centre(localPoints)
00314 )
00315 );
00316 info.setHit();
00317 }
00318 }
00319 else if (localTris.size())
00320 {
00321
00322 triSurface surf
00323 (
00324 localTris,
00325 geometricSurfacePatchList(0),
00326 localPoints,
00327 true
00328 );
00329 localTris.clearStorage();
00330
00331 labelList faceZone;
00332 label nZones = surf.markZones
00333 (
00334 boolList(surf.nEdges(), false),
00335 faceZone
00336 );
00337
00338 if (nZones == 1)
00339 {
00340 info.setPoint(calcCentre(surf));
00341 info.setHit();
00342 }
00343 }
00344
00345 return info;
00346 }
00347
00348
00349 void Foam::isoSurfaceCell::calcSnappedCc
00350 (
00351 const PackedBoolList& isTet,
00352 const scalarField& cVals,
00353 const scalarField& pVals,
00354
00355 DynamicList<point>& snappedPoints,
00356 labelList& snappedCc
00357 ) const
00358 {
00359 const pointField& cc = mesh_.cellCentres();
00360 const pointField& pts = mesh_.points();
00361
00362 snappedCc.setSize(mesh_.nCells());
00363 snappedCc = -1;
00364
00365
00366 DynamicList<point, 64> localPoints(64);
00367 DynamicList<labelledTri, 64> localTris(64);
00368 Map<label> pointToLocal(64);
00369
00370 forAll(mesh_.cells(), cellI)
00371 {
00372 if (cellCutType_[cellI] == CUT && isTet.get(cellI) == 0)
00373 {
00374 scalar cVal = cVals[cellI];
00375
00376 const cell& cFaces = mesh_.cells()[cellI];
00377
00378 localPoints.clear();
00379 localTris.clear();
00380 pointToLocal.clear();
00381
00382
00383
00384
00385 forAll(cFaces, cFaceI)
00386 {
00387 const face& f = mesh_.faces()[cFaces[cFaceI]];
00388
00389 forAll(f, fp)
00390 {
00391 label pointI = f[fp];
00392
00393 scalar s = isoFraction(cVal, pVals[pointI]);
00394
00395 if (s >= 0.0 && s <= 0.5)
00396 {
00397 if (pointToLocal.insert(pointI, localPoints.size()))
00398 {
00399 localPoints.append((1.0-s)*cc[cellI]+s*pts[pointI]);
00400 }
00401 }
00402 }
00403 }
00404
00405 if (localPoints.size() == 1)
00406 {
00407
00408 snappedCc[cellI] = snappedPoints.size();
00409 snappedPoints.append(localPoints[0]);
00410
00411
00412
00413
00414
00415
00416 }
00417 else if (localPoints.size() == 2)
00418 {
00419
00420 snappedCc[cellI] = snappedPoints.size();
00421 snappedPoints.append(0.5*(localPoints[0]+localPoints[1]));
00422
00423
00424
00425
00426
00427
00428 }
00429 else if (localPoints.size())
00430 {
00431
00432 forAll(cFaces, cFaceI)
00433 {
00434 const face& f = mesh_.faces()[cFaces[cFaceI]];
00435
00436
00437
00438
00439
00440 for (label fp = 1; fp < f.size() - 1; fp++)
00441 {
00442 triFace tri(f[0], f[fp], f[f.fcIndex(fp)]);
00443
00444
00445
00446
00447
00448
00449 FixedList<scalar, 3> s(3);
00450 s[0] = isoFraction(cVal, pVals[tri[0]]);
00451 s[1] = isoFraction(cVal, pVals[tri[1]]);
00452 s[2] = isoFraction(cVal, pVals[tri[2]]);
00453
00454 if
00455 (
00456 (s[0] >= 0.0 && s[0] <= 0.5)
00457 && (s[1] >= 0.0 && s[1] <= 0.5)
00458 && (s[2] >= 0.0 && s[2] <= 0.5)
00459 )
00460 {
00461 localTris.append
00462 (
00463 labelledTri
00464 (
00465 pointToLocal[tri[0]],
00466 pointToLocal[tri[1]],
00467 pointToLocal[tri[2]],
00468 0
00469 )
00470 );
00471 }
00472 }
00473 }
00474
00475 pointField surfPoints;
00476 surfPoints.transfer(localPoints);
00477 pointIndexHit info = collapseSurface(surfPoints, localTris);
00478
00479 if (info.hit())
00480 {
00481 snappedCc[cellI] = snappedPoints.size();
00482 snappedPoints.append(info.hitPoint());
00483
00484
00485
00486
00487
00488
00489 }
00490 }
00491 }
00492 }
00493 }
00494
00495
00496
00497 void Foam::isoSurfaceCell::genPointTris
00498 (
00499 const scalarField& cellValues,
00500 const scalarField& pointValues,
00501 const label pointI,
00502 const face& f,
00503 const label cellI,
00504 DynamicList<point, 64>& localTriPoints
00505 ) const
00506 {
00507 const pointField& cc = mesh_.cellCentres();
00508 const pointField& pts = mesh_.points();
00509
00510 for (label fp = 1; fp < f.size() - 1; fp++)
00511 {
00512 triFace tri(f[0], f[fp], f[f.fcIndex(fp)]);
00513
00514
00515
00516
00517
00518 label index = findIndex(tri, pointI);
00519
00520 if (index == -1)
00521 {
00522 continue;
00523 }
00524
00525
00526 label b = tri[tri.fcIndex(index)];
00527 label c = tri[tri.rcIndex(index)];
00528
00529
00530 FixedList<scalar, 3> s(3);
00531 s[0] = isoFraction(pointValues[pointI], pointValues[b]);
00532 s[1] = isoFraction(pointValues[pointI], pointValues[c]);
00533 s[2] = isoFraction(pointValues[pointI], cellValues[cellI]);
00534
00535 if
00536 (
00537 (s[0] >= 0.0 && s[0] <= 0.5)
00538 && (s[1] >= 0.0 && s[1] <= 0.5)
00539 && (s[2] >= 0.0 && s[2] <= 0.5)
00540 )
00541 {
00542 localTriPoints.append((1.0-s[0])*pts[pointI] + s[0]*pts[b]);
00543 localTriPoints.append((1.0-s[1])*pts[pointI] + s[1]*pts[c]);
00544 localTriPoints.append((1.0-s[2])*pts[pointI] + s[2]*cc[cellI]);
00545 }
00546 }
00547 }
00548
00549
00550
00551 void Foam::isoSurfaceCell::genPointTris
00552 (
00553 const scalarField& pointValues,
00554 const label pointI,
00555 const label cellI,
00556 DynamicList<point, 64>& localTriPoints
00557 ) const
00558 {
00559 const pointField& pts = mesh_.points();
00560 const cell& cFaces = mesh_.cells()[cellI];
00561
00562 FixedList<label, 4> tet;
00563
00564 label face0 = cFaces[0];
00565 const face& f0 = mesh_.faces()[face0];
00566
00567 if (mesh_.faceOwner()[face0] != cellI)
00568 {
00569 tet[0] = f0[0];
00570 tet[1] = f0[1];
00571 tet[2] = f0[2];
00572 }
00573 else
00574 {
00575 tet[0] = f0[0];
00576 tet[1] = f0[2];
00577 tet[2] = f0[1];
00578 }
00579
00580
00581 const face& f1 = mesh_.faces()[cFaces[1]];
00582
00583 forAll(f1, fp)
00584 {
00585 label p1 = f1[fp];
00586
00587 if (p1 != tet[0] && p1 != tet[1] && p1 != tet[2])
00588 {
00589 tet[3] = p1;
00590 break;
00591 }
00592 }
00593
00594
00595
00596
00597 label i0 = findIndex(tet, pointI);
00598 label i1 = tet.fcIndex(i0);
00599 label i2 = tet.fcIndex(i1);
00600 label i3 = tet.fcIndex(i2);
00601
00602
00603 FixedList<scalar, 3> s(3);
00604 s[0] = isoFraction(pointValues[pointI], pointValues[tet[i1]]);
00605 s[1] = isoFraction(pointValues[pointI], pointValues[tet[i2]]);
00606 s[2] = isoFraction(pointValues[pointI], pointValues[tet[i3]]);
00607
00608 if
00609 (
00610 (s[0] >= 0.0 && s[0] <= 0.5)
00611 && (s[1] >= 0.0 && s[1] <= 0.5)
00612 && (s[2] >= 0.0 && s[2] <= 0.5)
00613 )
00614 {
00615 localTriPoints.append((1.0-s[0])*pts[pointI] + s[0]*pts[tet[i1]]);
00616 localTriPoints.append((1.0-s[1])*pts[pointI] + s[1]*pts[tet[i2]]);
00617 localTriPoints.append((1.0-s[2])*pts[pointI] + s[2]*pts[tet[i3]]);
00618 }
00619 }
00620
00621
00622 void Foam::isoSurfaceCell::calcSnappedPoint
00623 (
00624 const PackedBoolList& isBoundaryPoint,
00625 const PackedBoolList& isTet,
00626 const scalarField& cVals,
00627 const scalarField& pVals,
00628
00629 DynamicList<point>& snappedPoints,
00630 labelList& snappedPoint
00631 ) const
00632 {
00633 const point greatPoint(VGREAT, VGREAT, VGREAT);
00634 pointField collapsedPoint(mesh_.nPoints(), greatPoint);
00635
00636
00637
00638 DynamicList<point, 64> localTriPoints(100);
00639 labelHashSet localPointCells(100);
00640
00641 forAll(mesh_.pointFaces(), pointI)
00642 {
00643 if (isBoundaryPoint.get(pointI) == 1)
00644 {
00645 continue;
00646 }
00647
00648 const labelList& pFaces = mesh_.pointFaces()[pointI];
00649
00650 bool anyCut = false;
00651
00652 forAll(pFaces, i)
00653 {
00654 label faceI = pFaces[i];
00655
00656 if
00657 (
00658 cellCutType_[mesh_.faceOwner()[faceI]] == CUT
00659 || (
00660 mesh_.isInternalFace(faceI)
00661 && cellCutType_[mesh_.faceNeighbour()[faceI]] == CUT
00662 )
00663 )
00664 {
00665 anyCut = true;
00666 break;
00667 }
00668 }
00669
00670 if (!anyCut)
00671 {
00672 continue;
00673 }
00674
00675
00676 localPointCells.clear();
00677 localTriPoints.clear();
00678
00679 forAll(pFaces, pFaceI)
00680 {
00681 label faceI = pFaces[pFaceI];
00682 const face& f = mesh_.faces()[faceI];
00683 label own = mesh_.faceOwner()[faceI];
00684
00685
00686 if (isTet.get(own) == 1)
00687 {
00688 if (localPointCells.insert(own))
00689 {
00690 genPointTris(pVals, pointI, own, localTriPoints);
00691 }
00692 }
00693 else
00694 {
00695 genPointTris(cVals, pVals, pointI, f, own, localTriPoints);
00696 }
00697
00698 if (mesh_.isInternalFace(faceI))
00699 {
00700 label nei = mesh_.faceNeighbour()[faceI];
00701
00702 if (isTet.get(nei) == 1)
00703 {
00704 if (localPointCells.insert(nei))
00705 {
00706 genPointTris(pVals, pointI, nei, localTriPoints);
00707 }
00708 }
00709 else
00710 {
00711 genPointTris(cVals, pVals, pointI, f, nei, localTriPoints);
00712 }
00713 }
00714 }
00715
00716 if (localTriPoints.size() == 3)
00717 {
00718
00719 pointField points;
00720 points.transfer(localTriPoints);
00721 collapsedPoint[pointI] = sum(points)/points.size();
00722
00723
00724
00725
00726 }
00727 else if (localTriPoints.size())
00728 {
00729
00730
00731
00732 labelList triMap;
00733 labelList triPointReverseMap;
00734 triSurface surf
00735 (
00736 stitchTriPoints
00737 (
00738 false,
00739 localTriPoints,
00740 triPointReverseMap,
00741 triMap
00742 )
00743 );
00744
00745 labelList faceZone;
00746 label nZones = surf.markZones
00747 (
00748 boolList(surf.nEdges(), false),
00749 faceZone
00750 );
00751
00752 if (nZones == 1)
00753 {
00754 collapsedPoint[pointI] = calcCentre(surf);
00755
00756
00757
00758 }
00759 }
00760 }
00761
00762 syncTools::syncPointList
00763 (
00764 mesh_,
00765 collapsedPoint,
00766 minEqOp<point>(),
00767 greatPoint,
00768 true
00769 );
00770
00771 snappedPoint.setSize(mesh_.nPoints());
00772 snappedPoint = -1;
00773
00774 forAll(collapsedPoint, pointI)
00775 {
00776 if (collapsedPoint[pointI] != greatPoint)
00777 {
00778 snappedPoint[pointI] = snappedPoints.size();
00779 snappedPoints.append(collapsedPoint[pointI]);
00780 }
00781 }
00782 }
00783
00784
00785
00786
00787 Foam::triSurface Foam::isoSurfaceCell::stitchTriPoints
00788 (
00789 const bool checkDuplicates,
00790 const List<point>& triPoints,
00791 labelList& triPointReverseMap,
00792 labelList& triMap
00793 ) const
00794 {
00795 label nTris = triPoints.size()/3;
00796
00797 if ((triPoints.size() % 3) != 0)
00798 {
00799 FatalErrorIn("isoSurfaceCell::stitchTriPoints(..)")
00800 << "Problem: number of points " << triPoints.size()
00801 << " not a multiple of 3." << abort(FatalError);
00802 }
00803
00804 pointField newPoints;
00805 mergePoints
00806 (
00807 triPoints,
00808 mergeDistance_,
00809 false,
00810 triPointReverseMap,
00811 newPoints
00812 );
00813
00814
00815 if (debug)
00816 {
00817 pointField newNewPoints;
00818 labelList oldToNew;
00819 bool hasMerged = mergePoints
00820 (
00821 newPoints,
00822 mergeDistance_,
00823 true,
00824 oldToNew,
00825 newNewPoints
00826 );
00827
00828 if (hasMerged)
00829 {
00830 FatalErrorIn("isoSurfaceCell::stitchTriPoints(..)")
00831 << "Merged points contain duplicates"
00832 << " when merging with distance " << mergeDistance_ << endl
00833 << "merged:" << newPoints.size() << " re-merged:"
00834 << newNewPoints.size()
00835 << abort(FatalError);
00836 }
00837 }
00838
00839
00840 List<labelledTri> tris;
00841 {
00842 DynamicList<labelledTri> dynTris(nTris);
00843 label rawPointI = 0;
00844 DynamicList<label> newToOldTri(nTris);
00845
00846 for (label oldTriI = 0; oldTriI < nTris; oldTriI++)
00847 {
00848 labelledTri tri
00849 (
00850 triPointReverseMap[rawPointI],
00851 triPointReverseMap[rawPointI+1],
00852 triPointReverseMap[rawPointI+2],
00853 0
00854 );
00855 rawPointI += 3;
00856
00857 if ((tri[0] != tri[1]) && (tri[0] != tri[2]) && (tri[1] != tri[2]))
00858 {
00859 newToOldTri.append(oldTriI);
00860 dynTris.append(tri);
00861 }
00862 }
00863
00864 triMap.transfer(newToOldTri);
00865 tris.transfer(dynTris);
00866 }
00867
00868
00869
00870
00871
00872
00873 if (checkDuplicates)
00874 {
00875 if (debug)
00876 {
00877 Pout<< "isoSurfaceCell : merged from " << nTris
00878 << " down to " << tris.size() << " triangles." << endl;
00879 }
00880
00881 pointField centres(tris.size());
00882 forAll(tris, triI)
00883 {
00884 centres[triI] = tris[triI].centre(newPoints);
00885 }
00886
00887 pointField mergedCentres;
00888 labelList oldToMerged;
00889 bool hasMerged = mergePoints
00890 (
00891 centres,
00892 mergeDistance_,
00893 false,
00894 oldToMerged,
00895 mergedCentres
00896 );
00897
00898 if (debug)
00899 {
00900 Pout<< "isoSurfaceCell : detected "
00901 << centres.size()-mergedCentres.size()
00902 << " duplicate triangles." << endl;
00903 }
00904
00905 if (hasMerged)
00906 {
00907
00908 label newTriI = 0;
00909 DynamicList<label> newToOldTri(tris.size());
00910 labelList newToMaster(mergedCentres.size(), -1);
00911 forAll(tris, triI)
00912 {
00913 label mergedI = oldToMerged[triI];
00914
00915 if (newToMaster[mergedI] == -1)
00916 {
00917 newToMaster[mergedI] = triI;
00918 newToOldTri.append(triMap[triI]);
00919 tris[newTriI++] = tris[triI];
00920 }
00921 }
00922
00923 triMap.transfer(newToOldTri);
00924 tris.setSize(newTriI);
00925 }
00926 }
00927
00928 return triSurface(tris, geometricSurfacePatchList(0), newPoints, true);
00929 }
00930
00931
00932
00933 bool Foam::isoSurfaceCell::validTri(const triSurface& surf, const label faceI)
00934 {
00935
00936
00937 const labelledTri& f = surf[faceI];
00938
00939 if
00940 (
00941 (f[0] < 0) || (f[0] >= surf.points().size())
00942 || (f[1] < 0) || (f[1] >= surf.points().size())
00943 || (f[2] < 0) || (f[2] >= surf.points().size())
00944 )
00945 {
00946 WarningIn("validTri(const triSurface&, const label)")
00947 << "triangle " << faceI << " vertices " << f
00948 << " uses point indices outside point range 0.."
00949 << surf.points().size()-1 << endl;
00950
00951 return false;
00952 }
00953
00954 if ((f[0] == f[1]) || (f[0] == f[2]) || (f[1] == f[2]))
00955 {
00956 WarningIn("validTri(const triSurface&, const label)")
00957 << "triangle " << faceI
00958 << " uses non-unique vertices " << f
00959 << endl;
00960 return false;
00961 }
00962
00963
00964
00965 const labelList& fFaces = surf.faceFaces()[faceI];
00966
00967
00968
00969 forAll(fFaces, i)
00970 {
00971 label nbrFaceI = fFaces[i];
00972
00973 if (nbrFaceI <= faceI)
00974 {
00975
00976 continue;
00977 }
00978
00979 const labelledTri& nbrF = surf[nbrFaceI];
00980
00981 if
00982 (
00983 ((f[0] == nbrF[0]) || (f[0] == nbrF[1]) || (f[0] == nbrF[2]))
00984 && ((f[1] == nbrF[0]) || (f[1] == nbrF[1]) || (f[1] == nbrF[2]))
00985 && ((f[2] == nbrF[0]) || (f[2] == nbrF[1]) || (f[2] == nbrF[2]))
00986 )
00987 {
00988 WarningIn("validTri(const triSurface&, const label)")
00989 << "triangle " << faceI << " vertices " << f
00990 << " has the same vertices as triangle " << nbrFaceI
00991 << " vertices " << nbrF
00992 << endl;
00993
00994 return false;
00995 }
00996 }
00997 return true;
00998 }
00999
01000
01001 void Foam::isoSurfaceCell::calcAddressing
01002 (
01003 const triSurface& surf,
01004 List<FixedList<label, 3> >& faceEdges,
01005 labelList& edgeFace0,
01006 labelList& edgeFace1,
01007 Map<labelList>& edgeFacesRest
01008 ) const
01009 {
01010 const pointField& points = surf.points();
01011
01012 pointField edgeCentres(3*surf.size());
01013 label edgeI = 0;
01014 forAll(surf, triI)
01015 {
01016 const labelledTri& tri = surf[triI];
01017 edgeCentres[edgeI++] = 0.5*(points[tri[0]]+points[tri[1]]);
01018 edgeCentres[edgeI++] = 0.5*(points[tri[1]]+points[tri[2]]);
01019 edgeCentres[edgeI++] = 0.5*(points[tri[2]]+points[tri[0]]);
01020 }
01021
01022 pointField mergedCentres;
01023 labelList oldToMerged;
01024 bool hasMerged = mergePoints
01025 (
01026 edgeCentres,
01027 mergeDistance_,
01028 false,
01029 oldToMerged,
01030 mergedCentres
01031 );
01032
01033 if (debug)
01034 {
01035 Pout<< "isoSurfaceCell : detected "
01036 << mergedCentres.size()
01037 << " edges on " << surf.size() << " triangles." << endl;
01038 }
01039
01040 if (!hasMerged)
01041 {
01042 return;
01043 }
01044
01045
01046
01047 faceEdges.setSize(surf.size());
01048 edgeI = 0;
01049 forAll(surf, triI)
01050 {
01051 faceEdges[triI][0] = oldToMerged[edgeI++];
01052 faceEdges[triI][1] = oldToMerged[edgeI++];
01053 faceEdges[triI][2] = oldToMerged[edgeI++];
01054 }
01055
01056
01057
01058 edgeFace0.setSize(mergedCentres.size());
01059 edgeFace0 = -1;
01060 edgeFace1.setSize(mergedCentres.size());
01061 edgeFace1 = -1;
01062 edgeFacesRest.clear();
01063
01064 forAll(oldToMerged, oldEdgeI)
01065 {
01066 label triI = oldEdgeI / 3;
01067 label edgeI = oldToMerged[oldEdgeI];
01068
01069 if (edgeFace0[edgeI] == -1)
01070 {
01071 edgeFace0[edgeI] = triI;
01072 }
01073 else if (edgeFace1[edgeI] == -1)
01074 {
01075 edgeFace1[edgeI] = triI;
01076 }
01077 else
01078 {
01079
01080
01081
01082
01083
01084 Map<labelList>::iterator iter = edgeFacesRest.find(edgeI);
01085
01086 if (iter != edgeFacesRest.end())
01087 {
01088 labelList& eFaces = iter();
01089 label sz = eFaces.size();
01090 eFaces.setSize(sz+1);
01091 eFaces[sz] = triI;
01092 }
01093 else
01094 {
01095 edgeFacesRest.insert(edgeI, labelList(1, triI));
01096 }
01097 }
01098 }
01099 }
01100
01101
01102 void Foam::isoSurfaceCell::walkOrientation
01103 (
01104 const triSurface& surf,
01105 const List<FixedList<label, 3> >& faceEdges,
01106 const labelList& edgeFace0,
01107 const labelList& edgeFace1,
01108 const label seedTriI,
01109 labelList& flipState
01110 )
01111 {
01112
01113 DynamicList<label> changedFaces(surf.size());
01114
01115 changedFaces.append(seedTriI);
01116
01117 while (changedFaces.size())
01118 {
01119 DynamicList<label> newChangedFaces(changedFaces.size());
01120
01121 forAll(changedFaces, i)
01122 {
01123 label triI = changedFaces[i];
01124 const labelledTri& tri = surf[triI];
01125 const FixedList<label, 3>& fEdges = faceEdges[triI];
01126
01127 forAll(fEdges, fp)
01128 {
01129 label edgeI = fEdges[fp];
01130
01131
01132 label p0 = tri[fp];
01133 label p1 = tri[tri.fcIndex(fp)];
01134
01135 label nbrI =
01136 (
01137 edgeFace0[edgeI] != triI
01138 ? edgeFace0[edgeI]
01139 : edgeFace1[edgeI]
01140 );
01141
01142 if (nbrI != -1 && flipState[nbrI] == -1)
01143 {
01144 const labelledTri& nbrTri = surf[nbrI];
01145
01146
01147 label nbrFp = findIndex(nbrTri, p0);
01148 label nbrP1 = nbrTri[nbrTri.rcIndex(nbrFp)];
01149
01150 bool sameOrientation = (p1 == nbrP1);
01151
01152 if (flipState[triI] == 0)
01153 {
01154 flipState[nbrI] = (sameOrientation ? 0 : 1);
01155 }
01156 else
01157 {
01158 flipState[nbrI] = (sameOrientation ? 1 : 0);
01159 }
01160 newChangedFaces.append(nbrI);
01161 }
01162 }
01163 }
01164
01165 changedFaces.transfer(newChangedFaces);
01166 }
01167 }
01168
01169
01170 void Foam::isoSurfaceCell::orientSurface
01171 (
01172 triSurface& surf,
01173 const List<FixedList<label, 3> >& faceEdges,
01174 const labelList& edgeFace0,
01175 const labelList& edgeFace1,
01176 const Map<labelList>& edgeFacesRest
01177 )
01178 {
01179
01180
01181
01182 labelList flipState(surf.size(), -1);
01183
01184 label seedTriI = 0;
01185
01186 while (true)
01187 {
01188
01189 for
01190 (
01191 ;
01192 seedTriI < surf.size() && flipState[seedTriI] != -1;
01193 seedTriI++
01194 )
01195 {}
01196
01197 if (seedTriI == surf.size())
01198 {
01199 break;
01200 }
01201
01202
01203
01204 flipState[seedTriI] = 0;
01205
01206 walkOrientation
01207 (
01208 surf,
01209 faceEdges,
01210 edgeFace0,
01211 edgeFace1,
01212 seedTriI,
01213 flipState
01214 );
01215 }
01216
01217
01218 surf.clearOut();
01219 forAll(surf, triI)
01220 {
01221 if (flipState[triI] == 1)
01222 {
01223 labelledTri tri(surf[triI]);
01224
01225 surf[triI][0] = tri[0];
01226 surf[triI][1] = tri[2];
01227 surf[triI][2] = tri[1];
01228 }
01229 else if (flipState[triI] == -1)
01230 {
01231 FatalErrorIn
01232 (
01233 "isoSurfaceCell::orientSurface(triSurface&, const label)"
01234 ) << "problem" << abort(FatalError);
01235 }
01236 }
01237 }
01238
01239
01240
01241 bool Foam::isoSurfaceCell::danglingTriangle
01242 (
01243 const FixedList<label, 3>& fEdges,
01244 const labelList& edgeFace1
01245 )
01246 {
01247 label nOpen = 0;
01248 forAll(fEdges, i)
01249 {
01250 if (edgeFace1[fEdges[i]] == -1)
01251 {
01252 nOpen++;
01253 }
01254 }
01255
01256 if (nOpen == 1 || nOpen == 2 || nOpen == 3)
01257 {
01258 return true;
01259 }
01260 else
01261 {
01262 return false;
01263 }
01264 }
01265
01266
01267
01268 Foam::label Foam::isoSurfaceCell::markDanglingTriangles
01269 (
01270 const List<FixedList<label, 3> >& faceEdges,
01271 const labelList& edgeFace0,
01272 const labelList& edgeFace1,
01273 const Map<labelList>& edgeFacesRest,
01274 boolList& keepTriangles
01275 )
01276 {
01277 keepTriangles.setSize(faceEdges.size());
01278 keepTriangles = true;
01279
01280 label nDangling = 0;
01281
01282
01283 forAllConstIter(Map<labelList>, edgeFacesRest, iter)
01284 {
01285
01286
01287
01288 label edgeI = iter.key();
01289 const labelList& otherEdgeFaces = iter();
01290
01291
01292 if (danglingTriangle(faceEdges[edgeFace0[edgeI]], edgeFace1))
01293 {
01294 keepTriangles[edgeFace0[edgeI]] = false;
01295 nDangling++;
01296 }
01297 if (danglingTriangle(faceEdges[edgeFace1[edgeI]], edgeFace1))
01298 {
01299 keepTriangles[edgeFace1[edgeI]] = false;
01300 nDangling++;
01301 }
01302 forAll(otherEdgeFaces, i)
01303 {
01304 label triI = otherEdgeFaces[i];
01305 if (danglingTriangle(faceEdges[triI], edgeFace1))
01306 {
01307 keepTriangles[triI] = false;
01308 nDangling++;
01309 }
01310 }
01311 }
01312 return nDangling;
01313 }
01314
01315
01316 Foam::triSurface Foam::isoSurfaceCell::subsetMesh
01317 (
01318 const triSurface& s,
01319 const labelList& newToOldFaces,
01320 labelList& oldToNewPoints,
01321 labelList& newToOldPoints
01322 )
01323 {
01324 const boolList include
01325 (
01326 createWithValues<boolList>
01327 (
01328 s.size(),
01329 false,
01330 newToOldFaces,
01331 true
01332 )
01333 );
01334
01335 newToOldPoints.setSize(s.points().size());
01336 oldToNewPoints.setSize(s.points().size());
01337 oldToNewPoints = -1;
01338 {
01339 label pointI = 0;
01340
01341 forAll(include, oldFacei)
01342 {
01343 if (include[oldFacei])
01344 {
01345
01346 const labelledTri& tri = s[oldFacei];
01347
01348 forAll(tri, fp)
01349 {
01350 label oldPointI = tri[fp];
01351
01352 if (oldToNewPoints[oldPointI] == -1)
01353 {
01354 oldToNewPoints[oldPointI] = pointI;
01355 newToOldPoints[pointI++] = oldPointI;
01356 }
01357 }
01358 }
01359 }
01360 newToOldPoints.setSize(pointI);
01361 }
01362
01363
01364 pointField newPoints(newToOldPoints.size());
01365 forAll(newToOldPoints, i)
01366 {
01367 newPoints[i] = s.points()[newToOldPoints[i]];
01368 }
01369
01370 List<labelledTri> newTriangles(newToOldFaces.size());
01371
01372 forAll(newToOldFaces, i)
01373 {
01374
01375 const labelledTri& tri = s[newToOldFaces[i]];
01376
01377 newTriangles[i][0] = oldToNewPoints[tri[0]];
01378 newTriangles[i][1] = oldToNewPoints[tri[1]];
01379 newTriangles[i][2] = oldToNewPoints[tri[2]];
01380 newTriangles[i].region() = tri.region();
01381 }
01382
01383
01384 return triSurface(newTriangles, s.patches(), newPoints, true);
01385 }
01386
01387
01388
01389
01390 Foam::isoSurfaceCell::isoSurfaceCell
01391 (
01392 const polyMesh& mesh,
01393 const scalarField& cVals,
01394 const scalarField& pVals,
01395 const scalar iso,
01396 const bool regularise,
01397 const scalar mergeTol
01398 )
01399 :
01400 mesh_(mesh),
01401 iso_(iso),
01402 mergeDistance_(mergeTol*mesh.bounds().mag())
01403 {
01404
01405 PackedBoolList isTet(mesh_.nCells());
01406 {
01407 tetMatcher tet;
01408
01409 forAll(isTet, cellI)
01410 {
01411 if (tet.isA(mesh_, cellI))
01412 {
01413 isTet.set(cellI, 1);
01414 }
01415 }
01416 }
01417
01418
01419
01420 PackedBoolList isBoundaryPoint(mesh_.nPoints());
01421 const polyBoundaryMesh& patches = mesh_.boundaryMesh();
01422 forAll(patches, patchI)
01423 {
01424 const polyPatch& pp = patches[patchI];
01425
01426 if (!pp.coupled())
01427 {
01428 label faceI = pp.start();
01429 forAll(pp, i)
01430 {
01431 const face& f = mesh_.faces()[faceI++];
01432
01433 forAll(f, fp)
01434 {
01435 isBoundaryPoint.set(f[fp], 1);
01436 }
01437 }
01438 }
01439 }
01440
01441
01442
01443 calcCutTypes(isTet, cVals, pVals);
01444
01445 DynamicList<point> snappedPoints(nCutCells_);
01446
01447
01448 labelList snappedCc;
01449 if (regularise)
01450 {
01451 calcSnappedCc
01452 (
01453 isTet,
01454 cVals,
01455 pVals,
01456 snappedPoints,
01457 snappedCc
01458 );
01459 }
01460 else
01461 {
01462 snappedCc.setSize(mesh_.nCells());
01463 snappedCc = -1;
01464 }
01465
01466 if (debug)
01467 {
01468 Pout<< "isoSurfaceCell : shifted " << snappedPoints.size()
01469 << " cell centres to intersection." << endl;
01470 }
01471
01472 snappedPoints.shrink();
01473 label nCellSnaps = snappedPoints.size();
01474
01475
01476 labelList snappedPoint;
01477 if (regularise)
01478 {
01479 calcSnappedPoint
01480 (
01481 isBoundaryPoint,
01482 isTet,
01483 cVals,
01484 pVals,
01485 snappedPoints,
01486 snappedPoint
01487 );
01488 }
01489 else
01490 {
01491 snappedPoint.setSize(mesh_.nPoints());
01492 snappedPoint = -1;
01493 }
01494
01495 if (debug)
01496 {
01497 Pout<< "isoSurfaceCell : shifted " << snappedPoints.size()-nCellSnaps
01498 << " vertices to intersection." << endl;
01499 }
01500
01501
01502
01503 DynamicList<point> triPoints(nCutCells_);
01504 DynamicList<label> triMeshCells(nCutCells_);
01505
01506 generateTriPoints
01507 (
01508 cVals,
01509 pVals,
01510
01511 mesh_.cellCentres(),
01512 mesh_.points(),
01513
01514 snappedPoints,
01515 snappedCc,
01516 snappedPoint,
01517
01518 triPoints,
01519 triMeshCells
01520 );
01521
01522 if (debug)
01523 {
01524 Pout<< "isoSurfaceCell : generated " << triMeshCells.size()
01525 << " unmerged triangles." << endl;
01526 }
01527
01528
01529 labelList triMap;
01530 triSurface::operator=
01531 (
01532 stitchTriPoints
01533 (
01534 true,
01535 triPoints,
01536 triPointMergeMap_,
01537 triMap
01538 )
01539 );
01540
01541 if (debug)
01542 {
01543 Pout<< "isoSurfaceCell : generated " << triMap.size()
01544 << " merged triangles." << endl;
01545 }
01546
01547 meshCells_.setSize(triMap.size());
01548 forAll(triMap, i)
01549 {
01550 meshCells_[i] = triMeshCells[triMap[i]];
01551 }
01552
01553 if (debug)
01554 {
01555 Pout<< "isoSurfaceCell : checking " << size()
01556 << " triangles for validity." << endl;
01557
01558 forAll(*this, triI)
01559 {
01560
01561 validTri(*this, triI);
01562 }
01563 }
01564
01565
01566 List<FixedList<label, 3> > faceEdges;
01567 labelList edgeFace0, edgeFace1;
01568 Map<labelList> edgeFacesRest;
01569
01570
01571 while (true)
01572 {
01573
01574 calcAddressing(*this, faceEdges, edgeFace0, edgeFace1, edgeFacesRest);
01575
01576
01577 boolList keepTriangles;
01578 label nDangling = markDanglingTriangles
01579 (
01580 faceEdges,
01581 edgeFace0,
01582 edgeFace1,
01583 edgeFacesRest,
01584 keepTriangles
01585 );
01586
01587 if (debug)
01588 {
01589 Pout<< "isoSurfaceCell : detected " << nDangling
01590 << " dangling triangles." << endl;
01591 }
01592
01593 if (nDangling == 0)
01594 {
01595 break;
01596 }
01597
01598
01599 labelList subsetTriMap(findIndices(keepTriangles, true));
01600
01601 labelList subsetPointMap;
01602 labelList reversePointMap;
01603 triSurface::operator=
01604 (
01605 subsetMesh
01606 (
01607 *this,
01608 subsetTriMap,
01609 reversePointMap,
01610 subsetPointMap
01611 )
01612 );
01613 meshCells_ = labelField(meshCells_, subsetTriMap);
01614 inplaceRenumber(reversePointMap, triPointMergeMap_);
01615 }
01616
01617 orientSurface(*this, faceEdges, edgeFace0, edgeFace1, edgeFacesRest);
01618
01619
01620 }
01621
01622
01627
01628
01629
01630
01631
01632
01633
01634
01635
01636
01637
01638
01639
01640
01641
01642
01643
01644
01645
01646
01647
01648
01649
01650
01651
01652
01653
01654
01655
01656
01657
01658
01659
01660
01661
01662
01663
01664
01665
01666
01667
01668
01669
01670
01671
01672
01673
01674
01675
01676
01677
01678
01679
01680
01681
01682
01683
01684
01685
01686
01687
01688
01689
01690
01691
01692
01693
01694
01695
01696
01697
01698
01699
01700
01701
01702
01703
01704
01705
01706
01707
01708
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723
01724
01725
01726
01727
01728
01729
01730
01731
01732
01733
01734
01735
01736
01737
01738
01739
01741
01742