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 "meshTools.H"
00028 #include "polyMesh.H"
00029 #include "hexMatcher.H"
00030
00031
00032
00033
00034 bool Foam::meshTools::visNormal
00035 (
00036 const vector& n,
00037 const vectorField& faceNormals,
00038 const labelList& faceLabels
00039 )
00040 {
00041 forAll(faceLabels, i)
00042 {
00043 if ((faceNormals[faceLabels[i]] & n) < SMALL)
00044 {
00045
00046 return false;
00047 }
00048 }
00049
00050 return true;
00051 }
00052
00053
00054 Foam::vectorField Foam::meshTools::calcBoxPointNormals(const primitivePatch& pp)
00055 {
00056 vectorField octantNormal(8);
00057 octantNormal[mXmYmZ] = vector(-1, -1, -1);
00058 octantNormal[pXmYmZ] = vector(1, -1, -1);
00059 octantNormal[mXpYmZ] = vector(-1, 1, -1);
00060 octantNormal[pXpYmZ] = vector(1, 1, -1);
00061
00062 octantNormal[mXmYpZ] = vector(-1, -1, 1);
00063 octantNormal[pXmYpZ] = vector(1, -1, 1);
00064 octantNormal[mXpYpZ] = vector(-1, 1, 1);
00065 octantNormal[pXpYpZ] = vector(1, 1, 1);
00066
00067 octantNormal /= mag(octantNormal);
00068
00069
00070 vectorField pn(pp.nPoints());
00071
00072 const vectorField& faceNormals = pp.faceNormals();
00073 const vectorField& pointNormals = pp.pointNormals();
00074 const labelListList& pointFaces = pp.pointFaces();
00075
00076 forAll(pointFaces, pointI)
00077 {
00078 const labelList& pFaces = pointFaces[pointI];
00079
00080 if (visNormal(pointNormals[pointI], faceNormals, pFaces))
00081 {
00082 pn[pointI] = pointNormals[pointI];
00083 }
00084 else
00085 {
00086 WarningIn
00087 (
00088 "Foam::meshTools::calcBoxPointNormals(const primitivePatch& pp)"
00089 ) << "Average point normal not visible for point:"
00090 << pp.meshPoints()[pointI] << endl;
00091
00092 label visOctant =
00093 mXmYmZMask
00094 | pXmYmZMask
00095 | mXpYmZMask
00096 | pXpYmZMask
00097 | mXmYpZMask
00098 | pXmYpZMask
00099 | mXpYpZMask
00100 | pXpYpZMask;
00101
00102 forAll(pFaces, i)
00103 {
00104 const vector& n = faceNormals[pFaces[i]];
00105
00106 if (n.x() > SMALL)
00107 {
00108
00109 visOctant &= ~mXmYmZMask;
00110 visOctant &= ~mXmYpZMask;
00111 visOctant &= ~mXpYmZMask;
00112 visOctant &= ~mXpYpZMask;
00113 }
00114 else if (n.x() < -SMALL)
00115 {
00116
00117 visOctant &= ~pXmYmZMask;
00118 visOctant &= ~pXmYpZMask;
00119 visOctant &= ~pXpYmZMask;
00120 visOctant &= ~pXpYpZMask;
00121 }
00122
00123 if (n.y() > SMALL)
00124 {
00125 visOctant &= ~mXmYmZMask;
00126 visOctant &= ~mXmYpZMask;
00127 visOctant &= ~pXmYmZMask;
00128 visOctant &= ~pXmYpZMask;
00129 }
00130 else if (n.y() < -SMALL)
00131 {
00132 visOctant &= ~mXpYmZMask;
00133 visOctant &= ~mXpYpZMask;
00134 visOctant &= ~pXpYmZMask;
00135 visOctant &= ~pXpYpZMask;
00136 }
00137
00138 if (n.z() > SMALL)
00139 {
00140 visOctant &= ~mXmYmZMask;
00141 visOctant &= ~mXpYmZMask;
00142 visOctant &= ~pXmYmZMask;
00143 visOctant &= ~pXpYmZMask;
00144 }
00145 else if (n.z() < -SMALL)
00146 {
00147 visOctant &= ~mXmYpZMask;
00148 visOctant &= ~mXpYpZMask;
00149 visOctant &= ~pXmYpZMask;
00150 visOctant &= ~pXpYpZMask;
00151 }
00152 }
00153
00154 label visI = -1;
00155
00156 label mask = 1;
00157
00158 for (label octant = 0; octant < 8; octant++)
00159 {
00160 if (visOctant & mask)
00161 {
00162
00163
00164 visI = octant;
00165
00166 break;
00167 }
00168 mask <<= 1;
00169 }
00170
00171 if (visI != -1)
00172 {
00173
00174 pn[pointI] = octantNormal[visI];
00175 }
00176 else
00177 {
00178 pn[pointI] = vector::zero;
00179
00180 WarningIn
00181 (
00182 "Foam::meshTools::calcBoxPointNormals"
00183 "(const primitivePatch& pp)"
00184 ) << "No visible octant for point:" << pp.meshPoints()[pointI]
00185 << " cooord:" << pp.points()[pp.meshPoints()[pointI]] << nl
00186 << "Normal set to " << pn[pointI] << endl;
00187 }
00188 }
00189 }
00190
00191 return pn;
00192 }
00193
00194
00195 Foam::vector Foam::meshTools::normEdgeVec
00196 (
00197 const primitiveMesh& mesh,
00198 const label edgeI
00199 )
00200 {
00201 vector eVec = mesh.edges()[edgeI].vec(mesh.points());
00202
00203 eVec /= mag(eVec);
00204
00205 return eVec;
00206 }
00207
00208
00209 void Foam::meshTools::writeOBJ
00210 (
00211 Ostream& os,
00212 const point& pt
00213 )
00214 {
00215 os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
00216 }
00217
00218
00219 void Foam::meshTools::writeOBJ
00220 (
00221 Ostream& os,
00222 const faceList& faces,
00223 const pointField& points,
00224 const labelList& faceLabels
00225 )
00226 {
00227 Map<label> foamToObj(4*faceLabels.size());
00228
00229 label vertI = 0;
00230
00231 forAll(faceLabels, i)
00232 {
00233 const face& f = faces[faceLabels[i]];
00234
00235 forAll(f, fp)
00236 {
00237 if (foamToObj.insert(f[fp], vertI))
00238 {
00239 writeOBJ(os, points[f[fp]]);
00240 vertI++;
00241 }
00242 }
00243
00244 os << 'l';
00245 forAll(f, fp)
00246 {
00247 os << ' ' << foamToObj[f[fp]]+1;
00248 }
00249 os << ' ' << foamToObj[f[0]]+1 << endl;
00250 }
00251 }
00252
00253
00254 void Foam::meshTools::writeOBJ
00255 (
00256 Ostream& os,
00257 const faceList& faces,
00258 const pointField& points
00259 )
00260 {
00261 labelList allFaces(faces.size());
00262 forAll(allFaces, i)
00263 {
00264 allFaces[i] = i;
00265 }
00266 writeOBJ(os, faces, points, allFaces);
00267 }
00268
00269
00270 void Foam::meshTools::writeOBJ
00271 (
00272 Ostream& os,
00273 const cellList& cells,
00274 const faceList& faces,
00275 const pointField& points,
00276 const labelList& cellLabels
00277 )
00278 {
00279 labelHashSet usedFaces(4*cellLabels.size());
00280
00281 forAll(cellLabels, i)
00282 {
00283 const cell& cFaces = cells[cellLabels[i]];
00284
00285 forAll(cFaces, j)
00286 {
00287 usedFaces.insert(cFaces[j]);
00288 }
00289 }
00290
00291 writeOBJ(os, faces, points, usedFaces.toc());
00292 }
00293
00294
00295 bool Foam::meshTools::edgeOnCell
00296 (
00297 const primitiveMesh& mesh,
00298 const label cellI,
00299 const label edgeI
00300 )
00301 {
00302 return findIndex(mesh.edgeCells(edgeI), cellI) != -1;
00303 }
00304
00305
00306 bool Foam::meshTools::edgeOnFace
00307 (
00308 const primitiveMesh& mesh,
00309 const label faceI,
00310 const label edgeI
00311 )
00312 {
00313 return findIndex(mesh.faceEdges(faceI), edgeI) != -1;
00314 }
00315
00316
00317
00318 bool Foam::meshTools::faceOnCell
00319 (
00320 const primitiveMesh& mesh,
00321 const label cellI,
00322 const label faceI
00323 )
00324 {
00325 if (mesh.isInternalFace(faceI))
00326 {
00327 if
00328 (
00329 (mesh.faceOwner()[faceI] == cellI)
00330 || (mesh.faceNeighbour()[faceI] == cellI)
00331 )
00332 {
00333 return true;
00334 }
00335 }
00336 else
00337 {
00338 if (mesh.faceOwner()[faceI] == cellI)
00339 {
00340 return true;
00341 }
00342 }
00343 return false;
00344 }
00345
00346
00347 Foam::label Foam::meshTools::findEdge
00348 (
00349 const edgeList& edges,
00350 const labelList& candidates,
00351 const label v0,
00352 const label v1
00353 )
00354 {
00355 forAll(candidates, i)
00356 {
00357 label edgeI = candidates[i];
00358
00359 const edge& e = edges[edgeI];
00360
00361 if ((e[0] == v0 && e[1] == v1) || (e[0] == v1 && e[1] == v0))
00362 {
00363 return edgeI;
00364 }
00365 }
00366 return -1;
00367 }
00368
00369
00370 Foam::label Foam::meshTools::findEdge
00371 (
00372 const primitiveMesh& mesh,
00373 const label v0,
00374 const label v1
00375 )
00376 {
00377 const edgeList& edges = mesh.edges();
00378
00379 const labelList& v0Edges = mesh.pointEdges()[v0];
00380
00381 forAll(v0Edges, i)
00382 {
00383 label edgeI = v0Edges[i];
00384
00385 const edge& e = edges[edgeI];
00386
00387 if ((e.start() == v1) || (e.end() == v1))
00388 {
00389 return edgeI;
00390 }
00391 }
00392 return -1;
00393 }
00394
00395
00396 Foam::label Foam::meshTools::getSharedEdge
00397 (
00398 const primitiveMesh& mesh,
00399 const label f0,
00400 const label f1
00401 )
00402 {
00403 const labelList& f0Edges = mesh.faceEdges()[f0];
00404 const labelList& f1Edges = mesh.faceEdges()[f1];
00405
00406 forAll(f0Edges, f0EdgeI)
00407 {
00408 label edge0 = f0Edges[f0EdgeI];
00409
00410 forAll(f1Edges, f1EdgeI)
00411 {
00412 label edge1 = f1Edges[f1EdgeI];
00413
00414 if (edge0 == edge1)
00415 {
00416 return edge0;
00417 }
00418 }
00419 }
00420 FatalErrorIn
00421 (
00422 "meshTools::getSharedEdge(const primitiveMesh&, const label"
00423 ", const label)"
00424 ) << "Faces " << f0 << " and " << f1 << " do not share an edge"
00425 << abort(FatalError);
00426
00427 return -1;
00428
00429 }
00430
00431
00432 Foam::label Foam::meshTools::getSharedFace
00433 (
00434 const primitiveMesh& mesh,
00435 const label cell0I,
00436 const label cell1I
00437 )
00438 {
00439 const cell& cFaces = mesh.cells()[cell0I];
00440
00441 forAll(cFaces, cFaceI)
00442 {
00443 label faceI = cFaces[cFaceI];
00444
00445 if
00446 (
00447 mesh.isInternalFace(faceI)
00448 && (
00449 mesh.faceOwner()[faceI] == cell1I
00450 || mesh.faceNeighbour()[faceI] == cell1I
00451 )
00452 )
00453 {
00454 return faceI;
00455 }
00456 }
00457
00458
00459 FatalErrorIn
00460 (
00461 "meshTools::getSharedFace(const primitiveMesh&, const label"
00462 ", const label)"
00463 ) << "No common face for"
00464 << " cell0I:" << cell0I << " faces:" << cFaces
00465 << " cell1I:" << cell1I << " faces:"
00466 << mesh.cells()[cell1I]
00467 << abort(FatalError);
00468
00469 return -1;
00470 }
00471
00472
00473
00474 void Foam::meshTools::getEdgeFaces
00475 (
00476 const primitiveMesh& mesh,
00477 const label cellI,
00478 const label edgeI,
00479 label& face0,
00480 label& face1
00481 )
00482 {
00483 const labelList& eFaces = mesh.edgeFaces(edgeI);
00484
00485 face0 = -1;
00486 face1 = -1;
00487
00488 forAll(eFaces, eFaceI)
00489 {
00490 label faceI = eFaces[eFaceI];
00491
00492 if (faceOnCell(mesh, cellI, faceI))
00493 {
00494 if (face0 == -1)
00495 {
00496 face0 = faceI;
00497 }
00498 else
00499 {
00500 face1 = faceI;
00501
00502 return;
00503 }
00504 }
00505 }
00506
00507 if ((face0 == -1) || (face1 == -1))
00508 {
00509 FatalErrorIn
00510 (
00511 "meshTools::getEdgeFaces(const primitiveMesh&, const label"
00512 ", const label, label&, label&"
00513 ) << "Can not find faces using edge " << mesh.edges()[edgeI]
00514 << " on cell " << cellI << abort(FatalError);
00515 }
00516 }
00517
00518
00519
00520 Foam::label Foam::meshTools::otherEdge
00521 (
00522 const primitiveMesh& mesh,
00523 const labelList& edgeLabels,
00524 const label thisEdgeI,
00525 const label thisVertI
00526 )
00527 {
00528 forAll(edgeLabels, edgeLabelI)
00529 {
00530 label edgeI = edgeLabels[edgeLabelI];
00531
00532 if (edgeI != thisEdgeI)
00533 {
00534 const edge& e = mesh.edges()[edgeI];
00535
00536 if ((e.start() == thisVertI) || (e.end() == thisVertI))
00537 {
00538 return edgeI;
00539 }
00540 }
00541 }
00542
00543 FatalErrorIn
00544 (
00545 "meshTools::otherEdge(const primitiveMesh&, const labelList&"
00546 ", const label, const label)"
00547 ) << "Can not find edge in "
00548 << UIndirectList<edge>(mesh.edges(), edgeLabels)()
00549 << " connected to edge "
00550 << thisEdgeI << " with vertices " << mesh.edges()[thisEdgeI]
00551 << " on side " << thisVertI << abort(FatalError);
00552
00553 return -1;
00554 }
00555
00556
00557
00558 Foam::label Foam::meshTools::otherFace
00559 (
00560 const primitiveMesh& mesh,
00561 const label cellI,
00562 const label faceI,
00563 const label edgeI
00564 )
00565 {
00566 label face0;
00567 label face1;
00568
00569 getEdgeFaces(mesh, cellI, edgeI, face0, face1);
00570
00571 if (face0 == faceI)
00572 {
00573 return face1;
00574 }
00575 else
00576 {
00577 return face0;
00578 }
00579 }
00580
00581
00582
00583 Foam::label Foam::meshTools::otherCell
00584 (
00585 const primitiveMesh& mesh,
00586 const label otherCellI,
00587 const label faceI
00588 )
00589 {
00590 if (!mesh.isInternalFace(faceI))
00591 {
00592 FatalErrorIn
00593 (
00594 "meshTools::otherCell(const primitiveMesh&, const label"
00595 ", const label)"
00596 ) << "Face " << faceI << " is not internal"
00597 << abort(FatalError);
00598 }
00599
00600 label newCellI = mesh.faceOwner()[faceI];
00601
00602 if (newCellI == otherCellI)
00603 {
00604 newCellI = mesh.faceNeighbour()[faceI];
00605 }
00606 return newCellI;
00607 }
00608
00609
00610
00611
00612 Foam::label Foam::meshTools::walkFace
00613 (
00614 const primitiveMesh& mesh,
00615 const label faceI,
00616 const label startEdgeI,
00617 const label startVertI,
00618 const label nEdges
00619 )
00620 {
00621 const labelList& fEdges = mesh.faceEdges(faceI);
00622
00623 label edgeI = startEdgeI;
00624
00625 label vertI = startVertI;
00626
00627 for (label iter = 0; iter < nEdges; iter++)
00628 {
00629 edgeI = otherEdge(mesh, fEdges, edgeI, vertI);
00630
00631 vertI = mesh.edges()[edgeI].otherVertex(vertI);
00632 }
00633
00634 return edgeI;
00635 }
00636
00637
00638 void Foam::meshTools::constrainToMeshCentre
00639 (
00640 const polyMesh& mesh,
00641 point& pt
00642 )
00643 {
00644 const Vector<label>& dirs = mesh.geometricD();
00645
00646 const point& min = mesh.bounds().min();
00647 const point& max = mesh.bounds().max();
00648
00649 for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
00650 {
00651 if (dirs[cmpt] == -1)
00652 {
00653 pt[cmpt] = 0.5*(min[cmpt] + max[cmpt]);
00654 }
00655 }
00656 }
00657
00658
00659 void Foam::meshTools::constrainToMeshCentre
00660 (
00661 const polyMesh& mesh,
00662 pointField& pts
00663 )
00664 {
00665 const Vector<label>& dirs = mesh.geometricD();
00666
00667 const point& min = mesh.bounds().min();
00668 const point& max = mesh.bounds().max();
00669
00670 bool isConstrained = false;
00671 for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
00672 {
00673 if (dirs[cmpt] == -1)
00674 {
00675 isConstrained = true;
00676 break;
00677 }
00678 }
00679
00680 if (isConstrained)
00681 {
00682 forAll(pts, i)
00683 {
00684 for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
00685 {
00686 if (dirs[cmpt] == -1)
00687 {
00688 pts[i][cmpt] = 0.5*(min[cmpt] + max[cmpt]);
00689 }
00690 }
00691 }
00692 }
00693 }
00694
00695
00696
00697 void Foam::meshTools::constrainDirection
00698 (
00699 const polyMesh& mesh,
00700 const Vector<label>& dirs,
00701 vector& d
00702 )
00703 {
00704 for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
00705 {
00706 if (dirs[cmpt] == -1)
00707 {
00708 d[cmpt] = 0.0;
00709 }
00710 }
00711 }
00712
00713
00714 void Foam::meshTools::constrainDirection
00715 (
00716 const polyMesh& mesh,
00717 const Vector<label>& dirs,
00718 vectorField& d
00719 )
00720 {
00721 bool isConstrained = false;
00722 for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
00723 {
00724 if (dirs[cmpt] == -1)
00725 {
00726 isConstrained = true;
00727 break;
00728 }
00729 }
00730
00731 if (isConstrained)
00732 {
00733 forAll(d, i)
00734 {
00735 for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
00736 {
00737 if (dirs[cmpt] == -1)
00738 {
00739 d[i][cmpt] = 0.0;
00740 }
00741 }
00742 }
00743 }
00744 }
00745
00746
00747 void Foam::meshTools::getParallelEdges
00748 (
00749 const primitiveMesh& mesh,
00750 const label cellI,
00751 const label e0,
00752 label& e1,
00753 label& e2,
00754 label& e3
00755 )
00756 {
00757
00758 label faceI = meshTools::otherFace(mesh, cellI, -1, e0);
00759
00760
00761 e1 = meshTools::walkFace(mesh, faceI, e0, mesh.edges()[e0].end(), 2);
00762
00763 faceI = meshTools::otherFace(mesh, cellI, faceI, e1);
00764
00765 e2 = meshTools::walkFace(mesh, faceI, e1, mesh.edges()[e1].end(), 2);
00766
00767 faceI = meshTools::otherFace(mesh, cellI, faceI, e2);
00768
00769 e3 = meshTools::walkFace(mesh, faceI, e2, mesh.edges()[e2].end(), 2);
00770 }
00771
00772
00773 Foam::vector Foam::meshTools::edgeToCutDir
00774 (
00775 const primitiveMesh& mesh,
00776 const label cellI,
00777 const label startEdgeI
00778 )
00779 {
00780 if (!hexMatcher().isA(mesh, cellI))
00781 {
00782 FatalErrorIn
00783 (
00784 "Foam::meshTools::getCutDir(const label, const label)"
00785 ) << "Not a hex : cell:" << cellI << abort(FatalError);
00786 }
00787
00788
00789 vector avgVec(normEdgeVec(mesh, startEdgeI));
00790
00791 label edgeI = startEdgeI;
00792
00793 label faceI = -1;
00794
00795 for (label i = 0; i < 3; i++)
00796 {
00797
00798 faceI = meshTools::otherFace(mesh, cellI, faceI, edgeI);
00799
00800 vector eVec(normEdgeVec(mesh, edgeI));
00801
00802 if ((eVec & avgVec) > 0)
00803 {
00804 avgVec += eVec;
00805 }
00806 else
00807 {
00808 avgVec -= eVec;
00809 }
00810
00811 label vertI = mesh.edges()[edgeI].end();
00812
00813 edgeI = meshTools::walkFace(mesh, faceI, edgeI, vertI, 2);
00814 }
00815
00816 avgVec /= mag(avgVec) + VSMALL;
00817
00818 return avgVec;
00819 }
00820
00821
00822
00823 Foam::label Foam::meshTools::cutDirToEdge
00824 (
00825 const primitiveMesh& mesh,
00826 const label cellI,
00827 const vector& cutDir
00828 )
00829 {
00830 if (!hexMatcher().isA(mesh, cellI))
00831 {
00832 FatalErrorIn
00833 (
00834 "Foam::meshTools::getCutDir(const label, const vector&)"
00835 ) << "Not a hex : cell:" << cellI << abort(FatalError);
00836 }
00837
00838 const labelList& cEdges = mesh.cellEdges()[cellI];
00839
00840 labelHashSet doneEdges(2*cEdges.size());
00841
00842 scalar maxCos = -GREAT;
00843 label maxEdgeI = -1;
00844
00845 for (label i = 0; i < 4; i++)
00846 {
00847 forAll(cEdges, cEdgeI)
00848 {
00849 label e0 = cEdges[cEdgeI];
00850
00851 if (!doneEdges.found(e0))
00852 {
00853 vector avgDir(edgeToCutDir(mesh, cellI, e0));
00854
00855 scalar cosAngle = mag(avgDir & cutDir);
00856
00857 if (cosAngle > maxCos)
00858 {
00859 maxCos = cosAngle;
00860 maxEdgeI = e0;
00861 }
00862
00863
00864 label e1, e2, e3;
00865 getParallelEdges(mesh, cellI, e0, e1, e2, e3);
00866
00867 doneEdges.insert(e0);
00868 doneEdges.insert(e1);
00869 doneEdges.insert(e2);
00870 doneEdges.insert(e3);
00871 }
00872 }
00873 }
00874
00875 forAll(cEdges, cEdgeI)
00876 {
00877 if (!doneEdges.found(cEdges[cEdgeI]))
00878 {
00879 FatalErrorIn
00880 (
00881 "meshTools::cutDirToEdge(const label, const vector&)"
00882 ) << "Cell:" << cellI << " edges:" << cEdges << endl
00883 << "Edge:" << cEdges[cEdgeI] << " not yet handled"
00884 << abort(FatalError);
00885 }
00886 }
00887
00888 if (maxEdgeI == -1)
00889 {
00890 FatalErrorIn
00891 (
00892 "meshTools::cutDirToEdge(const label, const vector&)"
00893 ) << "Problem : did not find edge aligned with " << cutDir
00894 << " on cell " << cellI << abort(FatalError);
00895 }
00896
00897 return maxEdgeI;
00898 }
00899
00900
00901