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

geomCellLooper.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 "geomCellLooper.H"
00028 #include "polyMesh.H"
00029 #include "DynamicList.H"
00030 #include "plane.H"
00031 #include "meshTools.H"
00032 #include "SortableList.H"
00033 #include "triSurfaceTools.H"
00034 #include "HashSet.H"
00035 #include "ListOps.H"
00036 #include "transform.H"
00037 
00038 #include "addToRunTimeSelectionTable.H"
00039 
00040 
00041 // Extension factor of edges to make sure we catch intersections through
00042 // edge endpoints
00043 const Foam::scalar Foam::geomCellLooper::pointEqualTol_ = 1E-3;
00044 
00045 
00046 // Snap cuts through edges onto edge endpoints. Fraction of edge length.
00047 Foam::scalar Foam::geomCellLooper::snapTol_ = 0.1;
00048 
00049 
00050 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00051 
00052 namespace Foam
00053 {
00054 
00055 defineTypeNameAndDebug(geomCellLooper, 0);
00056 addToRunTimeSelectionTable(cellLooper, geomCellLooper, word);
00057 
00058 }
00059 
00060 
00061 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00062 
00063 Foam::scalar Foam::geomCellLooper::minEdgeLen(const label vertI) const
00064 {
00065     scalar minLen = GREAT;
00066 
00067     const labelList& pEdges = mesh().pointEdges()[vertI];
00068 
00069     forAll(pEdges, pEdgeI)
00070     {
00071         const edge& e = mesh().edges()[pEdges[pEdgeI]];
00072 
00073         minLen = min(minLen, e.mag(mesh().points()));
00074     }
00075     return minLen;
00076 }
00077 
00078 
00079 // Cut edge with plane. Return true and set weight to fraction between
00080 // edge-start and edge-end
00081 bool Foam::geomCellLooper::cutEdge
00082 (
00083     const plane& cutPlane,
00084     const label edgeI,
00085     scalar& weight
00086 ) const
00087 {
00088     const pointField& pts = mesh().points();
00089 
00090     const edge& e = mesh().edges()[edgeI];
00091 
00092     scalar s = cutPlane.normalIntersect(pts[e.start()], e.vec(pts));
00093 
00094     if ((s > -pointEqualTol_) && (s < 1 + pointEqualTol_))
00095     {
00096         weight = s;
00097 
00098         return true;
00099     }
00100     else
00101     {
00102         // Make sure we don't use this value
00103         weight = -GREAT;
00104 
00105         return false;
00106     }
00107 }
00108 
00109 
00110 // If edge close enough to endpoint snap to endpoint.
00111 Foam::label Foam::geomCellLooper::snapToVert
00112 (
00113     const scalar tol,
00114     const label edgeI,
00115     const scalar weight
00116 ) const
00117 {
00118     const edge& e = mesh().edges()[edgeI];
00119 
00120     if (weight < tol)
00121     {
00122         return e.start();
00123     }
00124     else if (weight > (1-tol))
00125     {
00126         return e.end();
00127     }
00128     else
00129     {
00130         return -1;
00131     }
00132 }
00133 
00134 
00135 void Foam::geomCellLooper::getBase(const vector& n, vector& e0, vector& e1)
00136  const
00137 {
00138     // Guess for vector normal to n.
00139     vector base(1,0,0);
00140 
00141     scalar nComp = n & base;
00142 
00143     if (mag(nComp) > 0.8)
00144     {
00145         // Was bad guess. Try with different vector.
00146 
00147         base.x() = 0;
00148         base.y() = 1;
00149 
00150         nComp = n & base;
00151 
00152         if (mag(nComp) > 0.8)
00153         {
00154             base.y() = 0;
00155             base.z() = 1;
00156 
00157             nComp = n & base;
00158         }
00159     }
00160 
00161 
00162     // Use component normal to n as base vector.
00163     e0 = base - nComp*n;
00164 
00165     e0 /= mag(e0) + VSMALL;
00166 
00167     e1 = n ^ e0;
00168 
00169     //Pout<< "Coord system:" << endl
00170     //    << "    n  : " << n << ' ' << mag(n) << endl
00171     //    << "    e0 : " << e0 << ' ' << mag(e0) << endl
00172     //    << "    e1 : " << e1 << ' ' << mag(e1) << endl
00173     //    << endl;
00174 }
00175 
00176 
00177 // Return true if the cut edge at loop[index] is preceded by cuts through
00178 // the edge end points.
00179 bool Foam::geomCellLooper::edgeEndsCut
00180 (
00181     const labelList& loop,
00182     const label index
00183 ) const
00184 {
00185     label edgeI = getEdge(loop[index]);
00186 
00187     const edge& e = mesh().edges()[edgeI];
00188 
00189     const label prevCut = loop[loop.rcIndex(index)];
00190     const label nextCut = loop[loop.fcIndex(index)];
00191 
00192     if (!isEdge(prevCut) && !isEdge(nextCut))
00193     {
00194         // Cuts before and after are both vertices. Check if both
00195         // the edge endpoints
00196         label v0 = getVertex(prevCut);
00197         label v1 = getVertex(nextCut);
00198 
00199         if
00200         (
00201             (v0 == e[0] && v1 == e[1])
00202          || (v0 == e[1] && v1 == e[0])
00203         )
00204         {
00205             return true;
00206         }
00207     }
00208     return false;
00209 }
00210 
00211 
00212 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00213 
00214 // Construct from components
00215 Foam::geomCellLooper::geomCellLooper(const polyMesh& mesh)
00216 :
00217     cellLooper(mesh)
00218 {}
00219 
00220 
00221 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00222 
00223 Foam::geomCellLooper::~geomCellLooper()
00224 {}
00225 
00226 
00227 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00228 
00229 bool Foam::geomCellLooper::cut
00230 (
00231     const vector& refDir,
00232     const label cellI,
00233     const boolList& vertIsCut,
00234     const boolList& edgeIsCut,
00235     const scalarField& edgeWeight,
00236 
00237     labelList& loop,
00238     scalarField& loopWeights
00239 ) const
00240 {
00241     // Cut through cell centre normal to refDir.
00242     return cut
00243     (
00244         plane(mesh().cellCentres()[cellI], refDir),
00245         cellI,
00246         vertIsCut,
00247         edgeIsCut,
00248         edgeWeight,
00249         loop,
00250         loopWeights
00251     );
00252 }
00253 
00254 
00255 bool Foam::geomCellLooper::cut
00256 (
00257     const plane& cutPlane,
00258     const label cellI,
00259     const boolList&,
00260     const boolList&,
00261     const scalarField&,
00262 
00263     labelList& loop,
00264     scalarField& loopWeights
00265 ) const
00266 {
00267     const pointField& points = mesh().points();
00268     const edgeList& edges = mesh().edges();
00269 
00270     // Find all cuts through edges.
00271     // Special cases:
00272     // - edge cut close to endpoint. Snap to endpoint.
00273     // - edge endpoint close to plane (but edge not cut). Snap to endpoint.
00274     // - both endpoints close to plane. Edge parallel to plane. No need to
00275     //   cut to edge.
00276     // Note: any snap-to-point check must be based on all edges using a point
00277     // since otherwise cut through close to point but on neighbouring edge
00278     // might not be snapped.
00279 
00280     // Size overly big.
00281     label nEstCuts = 2*mesh().cells()[cellI].size();
00282 
00283     DynamicList<label> localLoop(nEstCuts);
00284     DynamicList<scalar> localLoopWeights(nEstCuts);
00285 
00286     // Points checked. Used to make sure we don't cut edge and edge endpoints
00287     // at the same time.
00288     labelHashSet checkedPoints(nEstCuts);
00289 
00290     const labelList& cellEdges = mesh().cellEdges()[cellI];
00291 
00292     forAll(cellEdges, i)
00293     {
00294         label edgeI = cellEdges[i];
00295 
00296         const edge& e = edges[edgeI];
00297 
00298         bool useStart = false;
00299 
00300         bool useEnd = false;
00301 
00302         //
00303         // Check distance of endpoints to cutPlane
00304         //
00305 
00306         if (!checkedPoints.found(e.start()))
00307         {
00308             checkedPoints.insert(e.start());
00309 
00310             scalar typStartLen = pointEqualTol_ * minEdgeLen(e.start());
00311 
00312             // Check distance of startPt to plane.
00313             if (cutPlane.distance(points[e.start()]) < typStartLen)
00314             {
00315                 // Use point.
00316                 localLoop.append(vertToEVert(e.start()));
00317                 localLoopWeights.append(-GREAT);
00318 
00319                 useStart = true;
00320             }
00321         }
00322         if (!checkedPoints.found(e.end()))
00323         {
00324             checkedPoints.insert(e.end());
00325 
00326             scalar typEndLen = pointEqualTol_ * minEdgeLen(e.end());
00327 
00328             // Check distance of endPt to plane.
00329             if (cutPlane.distance(points[e.end()]) < typEndLen)
00330             {
00331                 // Use point.
00332                 localLoop.append(vertToEVert(e.end()));
00333                 localLoopWeights.append(-GREAT);
00334 
00335                 useEnd = true;
00336             }
00337         }
00338 
00339         //
00340         // Check cut of edge
00341         //
00342 
00343         if (!useEnd && !useStart)
00344         {
00345             // Edge end points not close to plane so edge not close to
00346             // plane. Cut edge.
00347             scalar cutWeight;
00348 
00349             if (cutEdge(cutPlane, edgeI, cutWeight))
00350             {
00351                 // Snap edge cut to vertex.
00352                 label cutVertI = snapToVert(snapTol_, edgeI, cutWeight);
00353 
00354                 if (cutVertI == -1)
00355                 {
00356                     // Proper cut of edge
00357                     localLoop.append(edgeToEVert(edgeI));
00358                     localLoopWeights.append(cutWeight);
00359                 }
00360                 else
00361                 {
00362                     // Cut through edge end point. Might be duplicate
00363                     // since connected edge might also be snapped to same
00364                     // endpoint so only insert if unique.
00365                     label cut = vertToEVert(cutVertI);
00366 
00367                     if (findIndex(localLoop, cut) == -1)
00368                     {
00369                         localLoop.append(vertToEVert(cutVertI));
00370                         localLoopWeights.append(-GREAT);
00371                     }
00372                 }
00373             }
00374         }
00375     }
00376 
00377     if (localLoop.size() <= 2)
00378     {
00379         return false;
00380     }
00381 
00382     localLoop.shrink();
00383     localLoopWeights.shrink();
00384 
00385 
00386     // Get points on loop and centre of loop
00387     pointField loopPoints(localLoop.size());
00388     point ctr(vector::zero);
00389 
00390     forAll(localLoop, i)
00391     {
00392         loopPoints[i] = coord(localLoop[i], localLoopWeights[i]);
00393         ctr += loopPoints[i];
00394     }
00395     ctr /= localLoop.size();
00396 
00397 
00398     // Get base vectors of coordinate system normal to refDir
00399     vector e0, e1;
00400     getBase(cutPlane.normal(), e0, e1);
00401 
00402 
00403     // Get sorted angles from point on loop to centre of loop.
00404     SortableList<scalar> sortedAngles(localLoop.size());
00405 
00406     forAll(sortedAngles, i)
00407     {
00408         vector toCtr(loopPoints[i] - ctr);
00409         toCtr /= mag(toCtr);
00410 
00411         sortedAngles[i] = pseudoAngle(e0, e1, toCtr);
00412     }
00413     sortedAngles.sort();
00414 
00415     loop.setSize(localLoop.size());
00416     loopWeights.setSize(loop.size());
00417 
00418 
00419     // Fill loop and loopweights according to sorted angles
00420 
00421     const labelList& indices = sortedAngles.indices();
00422 
00423     forAll(indices, i)
00424     {
00425         loop[i] = localLoop[indices[i]];
00426         loopWeights[i] = localLoopWeights[indices[i]];
00427     }
00428 
00429 
00430     // Check for cut edges along already cut vertices.
00431     bool filterLoop = false;
00432 
00433     forAll(loop, i)
00434     {
00435         label cut = loop[i];
00436 
00437         if (isEdge(cut) && edgeEndsCut(loop, i))
00438         {
00439             filterLoop = true;
00440         }
00441     }
00442 
00443     if (filterLoop)
00444     {
00445         // Found edges in loop where both end points are also in loop. This
00446         // is superfluous information so we can remove it.
00447 
00448         labelList filteredLoop(loop.size());
00449         scalarField filteredLoopWeights(loopWeights.size());
00450         label filterI = 0;
00451 
00452         forAll(loop, i)
00453         {
00454             label cut = loop[i];
00455 
00456             if (isEdge(cut) && edgeEndsCut(loop, i))
00457             {
00458                 // Superfluous cut. Edge end points cut and edge itself as well.
00459             }
00460             else
00461             {
00462                 filteredLoop[filterI] = loop[i];
00463                 filteredLoopWeights[filterI] = loopWeights[i];
00464                 filterI++;
00465             }
00466         }
00467         filteredLoop.setSize(filterI);
00468         filteredLoopWeights.setSize(filterI);
00469 
00470         loop.transfer(filteredLoop);
00471         loopWeights.transfer(filteredLoopWeights);
00472     }
00473 
00474     if (debug&2)
00475     {
00476         Pout<< "cell:" << cellI << endl;
00477         forAll(loop, i)
00478         {
00479             Pout<< "At angle:" << sortedAngles[i] << endl
00480                 << "    cut:";
00481 
00482             writeCut(Pout, loop[i], loopWeights[i]);
00483 
00484             Pout<< "  coord:" << coord(loop[i], loopWeights[i]);
00485 
00486             Pout<< endl;
00487         }
00488     }
00489 
00490     return true;
00491 }
00492 
00493 
00494 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd