OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

cuttingPlane.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "cuttingPlane.H"
00028 #include "primitiveMesh.H"
00029 #include "linePointRef.H"
00030 #include "meshTools.H"
00031 
00032 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00033 
00034 // set values for what is close to zero and what is considered to
00035 // be positive (and not just rounding noise)
00037 const Foam::scalar zeroish  = Foam::SMALL;
00038 const Foam::scalar positive = Foam::SMALL * 1E3;
00040 
00041 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00042 
00043 // Find cut cells
00044 void Foam::cuttingPlane::calcCutCells
00045 (
00046     const primitiveMesh& mesh,
00047     const scalarField& dotProducts,
00048     const UList<label>& cellIdLabels
00049 )
00050 {
00051     const labelListList& cellEdges = mesh.cellEdges();
00052     const edgeList& edges = mesh.edges();
00053 
00054     label listSize = cellEdges.size();
00055     if (&cellIdLabels)
00056     {
00057         listSize = cellIdLabels.size();
00058     }
00059 
00060     cutCells_.setSize(listSize);
00061     label cutcellI(0);
00062 
00063     // Find the cut cells by detecting any cell that uses points with
00064     // opposing dotProducts.
00065     for (label listI = 0; listI < listSize; ++listI)
00066     {
00067         label cellI = listI;
00068         if (&cellIdLabels)
00069         {
00070             cellI = cellIdLabels[listI];
00071         }
00072 
00073         const labelList& cEdges = cellEdges[cellI];
00074 
00075         label nCutEdges = 0;
00076 
00077         forAll(cEdges, i)
00078         {
00079             const edge& e = edges[cEdges[i]];
00080 
00081             if
00082             (
00083                 (dotProducts[e[0]] < zeroish && dotProducts[e[1]] > positive)
00084              || (dotProducts[e[1]] < zeroish && dotProducts[e[0]] > positive)
00085             )
00086             {
00087                 nCutEdges++;
00088 
00089                 if (nCutEdges > 2)
00090                 {
00091                     cutCells_[cutcellI++] = cellI;
00092 
00093                     break;
00094                 }
00095             }
00096         }
00097     }
00098 
00099     // Set correct list size
00100     cutCells_.setSize(cutcellI);
00101 }
00102 
00103 
00104 // Determine for each edge the intersection point. Calculates
00105 // - cutPoints_ : coordinates of all intersection points
00106 // - edgePoint  : per edge -1 or the index into cutPoints
00107 void Foam::cuttingPlane::intersectEdges
00108 (
00109     const primitiveMesh& mesh,
00110     const scalarField& dotProducts,
00111     List<label>& edgePoint
00112 )
00113 {
00114     // Use the dotProducts to find out the cut edges.
00115     const edgeList& edges = mesh.edges();
00116     const pointField& points = mesh.points();
00117 
00118     // Per edge -1 or the label of the intersection point
00119     edgePoint.setSize(edges.size());
00120 
00121     DynamicList<point> dynCuttingPoints(4*cutCells_.size());
00122 
00123     forAll(edges, edgeI)
00124     {
00125         const edge& e = edges[edgeI];
00126 
00127         if
00128         (
00129             (dotProducts[e[0]] < zeroish && dotProducts[e[1]] > positive)
00130          || (dotProducts[e[1]] < zeroish && dotProducts[e[0]] > positive)
00131         )
00132         {
00133             // Edge is cut
00134             edgePoint[edgeI] = dynCuttingPoints.size();
00135 
00136             const point& p0 = points[e[0]];
00137             const point& p1 = points[e[1]];
00138 
00139             scalar alpha = lineIntersect(linePointRef(p0, p1));
00140 
00141             if (alpha < zeroish)
00142             {
00143                 dynCuttingPoints.append(p0);
00144             }
00145             else if (alpha >= 1.0)
00146             {
00147                 dynCuttingPoints.append(p1);
00148             }
00149             else
00150             {
00151                 dynCuttingPoints.append((1-alpha)*p0 + alpha*p1);
00152             }
00153         }
00154         else
00155         {
00156             edgePoint[edgeI] = -1;
00157         }
00158     }
00159 
00160     this->storedPoints().transfer(dynCuttingPoints);
00161 }
00162 
00163 
00164 // Coming from startEdgeI cross the edge to the other face
00165 // across to the next cut edge.
00166 bool Foam::cuttingPlane::walkCell
00167 (
00168     const primitiveMesh& mesh,
00169     const UList<label>& edgePoint,
00170     const label cellI,
00171     const label startEdgeI,
00172     DynamicList<label>& faceVerts
00173 )
00174 {
00175     label faceI = -1;
00176     label edgeI = startEdgeI;
00177 
00178     label nIter = 0;
00179 
00180     faceVerts.clear();
00181     do
00182     {
00183         faceVerts.append(edgePoint[edgeI]);
00184 
00185         // Cross edge to other face
00186         faceI = meshTools::otherFace(mesh, cellI, faceI, edgeI);
00187 
00188         // Find next cut edge on face.
00189         const labelList& fEdges = mesh.faceEdges()[faceI];
00190 
00191         label nextEdgeI = -1;
00192 
00193         //Note: here is where we should check for whether there are more
00194         // than 2 intersections with the face (warped/non-convex face).
00195         // If so should e.g. decompose the cells on both faces and redo
00196         // the calculation.
00197 
00198         forAll(fEdges, i)
00199         {
00200             label edge2I = fEdges[i];
00201 
00202             if (edge2I != edgeI && edgePoint[edge2I] != -1)
00203             {
00204                 nextEdgeI = edge2I;
00205                 break;
00206             }
00207         }
00208 
00209         if (nextEdgeI == -1)
00210         {
00211             // Did not find another cut edge on faceI. Do what?
00212             WarningIn("Foam::cuttingPlane::walkCell")
00213                 << "Did not find closed walk along surface of cell " << cellI
00214                 << " starting from edge " << startEdgeI
00215                 << " in " << nIter << " iterations." << nl
00216                 << "Collected cutPoints so far:" << faceVerts
00217                 << endl;
00218 
00219             return false;
00220         }
00221 
00222         edgeI = nextEdgeI;
00223 
00224         nIter++;
00225 
00226         if (nIter > 1000)
00227         {
00228             WarningIn("Foam::cuttingPlane::walkCell")
00229                 << "Did not find closed walk along surface of cell " << cellI
00230                 << " starting from edge " << startEdgeI
00231                 << " in " << nIter << " iterations." << nl
00232                 << "Collected cutPoints so far:" << faceVerts
00233                 << endl;
00234             return false;
00235         }
00236 
00237     } while (edgeI != startEdgeI);
00238 
00239 
00240     if (faceVerts.size() >= 3)
00241     {
00242         return true;
00243     }
00244     else
00245     {
00246         WarningIn("Foam::cuttingPlane::walkCell")
00247             << "Did not find closed walk along surface of cell " << cellI
00248             << " starting from edge " << startEdgeI << nl
00249             << "Collected cutPoints so far:" << faceVerts
00250             << endl;
00251 
00252         return false;
00253     }
00254 }
00255 
00256 
00257 // For every cut cell determine a walk through all? its cuts.
00258 void Foam::cuttingPlane::walkCellCuts
00259 (
00260     const primitiveMesh& mesh,
00261     const UList<label>& edgePoint
00262 )
00263 {
00264     const pointField& cutPoints = this->points();
00265 
00266     // use dynamic lists to handle triangulation and/or missed cuts
00267     DynamicList<face>  dynCutFaces(cutCells_.size());
00268     DynamicList<label> dynCutCells(cutCells_.size());
00269 
00270     // scratch space for calculating the face vertices
00271     DynamicList<label> faceVerts(10);
00272 
00273     forAll(cutCells_, i)
00274     {
00275         label cellI = cutCells_[i];
00276 
00277         // Find the starting edge to walk from.
00278         const labelList& cEdges = mesh.cellEdges()[cellI];
00279 
00280         label startEdgeI = -1;
00281 
00282         forAll(cEdges, cEdgeI)
00283         {
00284             label edgeI = cEdges[cEdgeI];
00285 
00286             if (edgePoint[edgeI] != -1)
00287             {
00288                 startEdgeI = edgeI;
00289                 break;
00290             }
00291         }
00292 
00293         // Check for the unexpected ...
00294         if (startEdgeI == -1)
00295         {
00296             FatalErrorIn("Foam::cuttingPlane::walkCellCuts")
00297                 << "Cannot find cut edge for cut cell " << cellI
00298                 << abort(FatalError);
00299         }
00300 
00301         // Walk from starting edge around the circumference of the cell.
00302         bool okCut = walkCell
00303         (
00304             mesh,
00305             edgePoint,
00306             cellI,
00307             startEdgeI,
00308             faceVerts
00309         );
00310 
00311         if (okCut)
00312         {
00313             face f(faceVerts);
00314 
00315             // Orient face to point in the same direction as the plane normal
00316             if ((f.normal(cutPoints) && normal()) < 0)
00317             {
00318                 f = f.reverseFace();
00319             }
00320 
00321             // the cut faces are usually quite ugly, so always triangulate
00322             label nTri = f.triangles(cutPoints, dynCutFaces);
00323             while (nTri--)
00324             {
00325                 dynCutCells.append(cellI);
00326             }
00327         }
00328     }
00329 
00330     this->storedFaces().transfer(dynCutFaces);
00331     cutCells_.transfer(dynCutCells);
00332 }
00333 
00334 
00335 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00336 
00337 // Construct without cutting
00338 Foam::cuttingPlane::cuttingPlane(const plane& pln)
00339 :
00340     plane(pln)
00341 {}
00342 
00343 
00344 // Construct from plane and mesh reference, restricted to a list of cells
00345 Foam::cuttingPlane::cuttingPlane
00346 (
00347     const plane& pln,
00348     const primitiveMesh& mesh,
00349     const UList<label>& cellIdLabels
00350 )
00351 :
00352     plane(pln)
00353 {
00354     reCut(mesh, cellIdLabels);
00355 }
00356 
00357 
00358 
00359 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00360 
00361 // recut mesh with existing planeDesc
00362 void Foam::cuttingPlane::reCut
00363 (
00364     const primitiveMesh& mesh,
00365     const UList<label>& cellIdLabels
00366 )
00367 {
00368     MeshStorage::clear();
00369     cutCells_.clear();
00370 
00371     scalarField dotProducts = (mesh.points() - refPoint()) & normal();
00372 
00373     // Determine cells that are (probably) cut.
00374     calcCutCells(mesh, dotProducts, cellIdLabels);
00375 
00376     // Determine cutPoints and return list of edge cuts.
00377     // per edge -1 or the label of the intersection point
00378     labelList edgePoint;
00379     intersectEdges(mesh, dotProducts, edgePoint);
00380 
00381     // Do topological walk around cell to find closed loop.
00382     walkCellCuts(mesh, edgePoint);
00383 }
00384 
00385 
00386 // remap action on triangulation
00387 void Foam::cuttingPlane::remapFaces
00388 (
00389     const UList<label>& faceMap
00390 )
00391 {
00392     // recalculate the cells cut
00393     if (&faceMap && faceMap.size())
00394     {
00395         MeshStorage::remapFaces(faceMap);
00396 
00397         List<label> newCutCells(faceMap.size());
00398         forAll(faceMap, faceI)
00399         {
00400             newCutCells[faceI] = cutCells_[faceMap[faceI]];
00401         }
00402         cutCells_.transfer(newCutCells);
00403     }
00404 }
00405 
00406 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00407 
00408 void Foam::cuttingPlane::operator=(const cuttingPlane& rhs)
00409 {
00410     // Check for assignment to self
00411     if (this == &rhs)
00412     {
00413         FatalErrorIn ("Foam::cuttingPlane::operator=(const cuttingPlane&)")
00414             << "Attempted assignment to self"
00415             << abort(FatalError);
00416     }
00417 
00418     static_cast<MeshStorage&>(*this) = rhs;
00419     static_cast<plane&>(*this) = rhs;
00420     cutCells_ = rhs.cutCells();
00421 }
00422 
00423 
00424 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd