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

faceCollapser.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 Description
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "faceCollapser.H"
00030 #include "polyMesh.H"
00031 #include "polyTopoChange.H"
00032 #include "polyModifyPoint.H"
00033 #include "polyModifyFace.H"
00034 #include "polyRemoveFace.H"
00035 #include "SortableList.H"
00036 #include "meshTools.H"
00037 #include "OFstream.H"
00038 
00039 
00040 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00041 
00042 // Insert labelList into labelHashSet. Optional excluded element.
00043 void Foam::faceCollapser::insert
00044 (
00045     const labelList& elems,
00046     const label excludeElem,
00047     labelHashSet& set
00048 )
00049 {
00050     forAll(elems, i)
00051     {
00052         if (elems[i] != excludeElem)
00053         {
00054             set.insert(elems[i]);
00055         }
00056     }
00057 }
00058 
00059 
00060 // Find edge amongst candidate edges. FatalError if none.
00061 Foam::label Foam::faceCollapser::findEdge
00062 (
00063     const edgeList& edges,
00064     const labelList& edgeLabels,
00065     const label v0,
00066     const label v1
00067 )
00068 {
00069     forAll(edgeLabels, i)
00070     {
00071         label edgeI = edgeLabels[i];
00072 
00073         const edge& e = edges[edgeI];
00074 
00075         if
00076         (
00077             (e[0] == v0 && e[1] == v1)
00078          || (e[0] == v1 && e[1] == v0)
00079         )
00080         {
00081             return edgeI;
00082         }
00083     }
00084 
00085     FatalErrorIn("findEdge") << "Cannot find edge between vertices " << v0
00086         << " and " << v1 << " in edge labels " << edgeLabels
00087         << abort(FatalError);
00088 
00089     return -1;
00090 }
00091 
00092 
00093 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00094 
00095 // Replace vertices in face
00096 void Foam::faceCollapser::filterFace
00097 (
00098     const Map<labelList>& splitEdges,
00099     const label faceI,
00100     polyTopoChange& meshMod
00101 ) const
00102 {
00103     const face& f = mesh_.faces()[faceI];
00104     const labelList& fEdges = mesh_.faceEdges()[faceI];
00105 
00106     // Space for replaced vertices and split edges.
00107     DynamicList<label> newFace(10 * f.size());
00108 
00109     forAll(f, fp)
00110     {
00111         label v0 = f[fp];
00112 
00113         newFace.append(v0);
00114 
00115         // Look ahead one to get edge.
00116         label fp1 = f.fcIndex(fp);
00117 
00118         label v1 = f[fp1];
00119 
00120         // Get split on edge if any.
00121         label edgeI = findEdge(mesh_.edges(), fEdges, v0, v1);
00122 
00123         Map<labelList>::const_iterator edgeFnd =
00124             splitEdges.find(edgeI);
00125 
00126         if (edgeFnd != splitEdges.end())
00127         {
00128             // edgeI has been split (by introducing new vertices).
00129             // Insert new vertices in face in correct order
00130             // (splitEdges was constructed to be from edge.start() to end())
00131 
00132             const labelList& extraVerts = edgeFnd();
00133 
00134             if (v0 == mesh_.edges()[edgeI].start())
00135             {
00136                 forAll(extraVerts, i)
00137                 {
00138                     newFace.append(extraVerts[i]);
00139                 }
00140             }
00141             else
00142             {
00143                 forAllReverse(extraVerts, i)
00144                 {
00145                     newFace.append(extraVerts[i]);
00146                 }
00147             }
00148         }
00149     }
00150     face newF(newFace.shrink());
00151 
00152     //Pout<< "Modifying face:" << faceI << " from " << f << " to " << newFace
00153     //    << endl;
00154 
00155     if (newF != f)
00156     {
00157         label nei = -1;
00158 
00159         label patchI = -1;
00160 
00161         if (mesh_.isInternalFace(faceI))
00162         {
00163             nei = mesh_.faceNeighbour()[faceI];
00164         }
00165         else
00166         {
00167             patchI = mesh_.boundaryMesh().whichPatch(faceI);
00168         }
00169 
00170         // Get current zone info
00171         label zoneID = mesh_.faceZones().whichZone(faceI);
00172 
00173         bool zoneFlip = false;
00174 
00175         if (zoneID >= 0)
00176         {
00177             const faceZone& fZone = mesh_.faceZones()[zoneID];
00178 
00179             zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
00180         }
00181 
00182         meshMod.setAction
00183         (
00184             polyModifyFace
00185             (
00186                 newF,                       // modified face
00187                 faceI,                      // label of face being modified
00188                 mesh_.faceOwner()[faceI],   // owner
00189                 nei,                        // neighbour
00190                 false,                      // face flip
00191                 patchI,                     // patch for face
00192                 false,                      // remove from zone
00193                 zoneID,                     // zone for face
00194                 zoneFlip                    // face flip in zone
00195             )
00196         );
00197     }
00198 }
00199 
00200 
00201 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00202 
00203 // Construct from mesh
00204 Foam::faceCollapser::faceCollapser(const polyMesh& mesh)
00205 :
00206     mesh_(mesh)
00207 {}
00208 
00209 
00210 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00211 
00212 void Foam::faceCollapser::setRefinement
00213 (
00214     const labelList& faceLabels,
00215     const labelList& fpStart,
00216     const labelList& fpEnd,
00217     polyTopoChange& meshMod
00218 ) const
00219 {
00220     const pointField& points = mesh_.points();
00221     const edgeList& edges = mesh_.edges();
00222     const faceList& faces = mesh_.faces();
00223     const labelListList& edgeFaces = mesh_.edgeFaces();
00224 
00225 
00226     // From split edge to newly introduced point(s). Can be more than one per
00227     // edge!
00228     Map<labelList> splitEdges(faceLabels.size());
00229 
00230     // Mark faces in any way affect by modifying points/edges. Used later on
00231     // to prevent having to redo all faces.
00232     labelHashSet affectedFaces(4*faceLabels.size());
00233 
00234 
00235     //
00236     // Add/remove vertices and construct mapping
00237     //
00238 
00239     forAll(faceLabels, i)
00240     {
00241         const label faceI = faceLabels[i];
00242 
00243         const face& f = faces[faceI];
00244 
00245         const label fpA = fpStart[i];
00246         const label fpB = fpEnd[i];
00247 
00248         const point& pA = points[f[fpA]];
00249         const point& pB = points[f[fpB]];
00250 
00251         Pout<< "Face:" << f << " collapsed to fp:" << fpA << ' '  << fpB
00252             << " with points:" << pA << ' ' << pB
00253             << endl;
00254 
00255         // Create line from fpA to fpB
00256         linePointRef lineAB(pA, pB);
00257 
00258         // Get projections of all vertices onto line.
00259 
00260         // Distance(squared) to pA for every point on face.
00261         SortableList<scalar> dist(f.size());
00262 
00263         dist[fpA] = 0;
00264         dist[fpB] = magSqr(pB - pA);
00265 
00266         // Step from fpA to fpB
00267         // ~~~~~~~~~~~~~~~~~~~~
00268         // (by incrementing)
00269 
00270         label fpMin1 = fpA;
00271         label fp = f.fcIndex(fpMin1);
00272 
00273         while (fp != fpB)
00274         {
00275             // See where fp sorts. Make sure it is above fpMin1!
00276             pointHit near = lineAB.nearestDist(points[f[fp]]);
00277 
00278             scalar w = magSqr(near.rawPoint() - pA);
00279 
00280             if (w <= dist[fpMin1])
00281             {
00282                 // Offset.
00283                 w = dist[fpMin1] + 1E-6*(dist[fpB] - dist[fpA]);
00284 
00285                 point newPoint
00286                 (
00287                     pA + Foam::sqrt(w / (dist[fpB] - dist[fpA]))*(pB - pA)
00288                 );
00289 
00290                 Pout<< "Adapting position of vertex " << f[fp] << " on face "
00291                     << f << " from " << near.rawPoint() << " to " << newPoint
00292                     << endl;
00293 
00294                 near.setPoint(newPoint);
00295             }
00296 
00297             // Responsability of caller to make sure polyModifyPoint is only
00298             // called once per point. (so max only one collapse face per
00299             // edge)
00300             meshMod.setAction
00301             (
00302                 polyModifyPoint
00303                 (
00304                     f[fp],
00305                     near.rawPoint(),
00306                     false,
00307                     -1,
00308                     true
00309                 )
00310             );
00311 
00312             dist[fp] = w;
00313 
00314             // Step to next vertex.
00315             fpMin1 = fp;
00316             fp = f.fcIndex(fpMin1);
00317         }
00318 
00319         // Step from fpA to fpB
00320         // ~~~~~~~~~~~~~~~~~~~~
00321         // (by decrementing)
00322 
00323         fpMin1 = fpA;
00324         fp = f.rcIndex(fpMin1);
00325 
00326         while (fp != fpB)
00327         {
00328             // See where fp sorts. Make sure it is below fpMin1!
00329             pointHit near = lineAB.nearestDist(points[f[fp]]);
00330 
00331             scalar w = magSqr(near.rawPoint() - pA);
00332 
00333             if (w <= dist[fpMin1])
00334             {
00335                 // Offset.
00336                 w = dist[fpMin1] + 1E-6*(dist[fpB] - dist[fpA]);
00337 
00338                 point newPoint
00339                 (
00340                     pA + Foam::sqrt(w / (dist[fpB] - dist[fpA]))*(pB - pA)
00341                 );
00342 
00343                 Pout<< "Adapting position of vertex " << f[fp] << " on face "
00344                     << f << " from " << near.rawPoint() << " to " << newPoint
00345                     << endl;
00346 
00347                 near.setPoint(newPoint);
00348             }
00349 
00350             // Responsability of caller to make sure polyModifyPoint is only
00351             // called once per point. (so max only one collapse face per
00352             // edge)
00353             meshMod.setAction
00354             (
00355                 polyModifyPoint
00356                 (
00357                     f[fp],
00358                     near.rawPoint(),
00359                     false,
00360                     -1,
00361                     true
00362                 )
00363             );
00364 
00365             dist[fp] = w;
00366 
00367             // Step to previous vertex.
00368             fpMin1 = fp;
00369             fp = f.rcIndex(fpMin1);
00370         }
00371 
00372         dist.sort();
00373 
00374         // Check that fpB sorts latest.
00375         if (dist.indices()[dist.size()-1] != fpB)
00376         {
00377             OFstream str("conflictingFace.obj");
00378             meshTools::writeOBJ(str, faceList(1, f), points);
00379 
00380             FatalErrorIn("faceCollapser::setRefinement")
00381                 << "Trying to collapse face:" << faceI << " vertices:" << f
00382                 << " to edges between vertices " << f[fpA] << " and "
00383                 << f[fpB] << " but " << f[fpB] << " does not seem to be the"
00384                 << " vertex furthest away from " << f[fpA] << endl
00385                 << "Dumped conflicting face to obj file conflictingFace.obj"
00386                 << abort(FatalError);
00387         }
00388 
00389 
00390         // From fp to index in sort:
00391         Pout<< "Face:" << f << " fpA:" << fpA << " fpB:" << fpB << nl;
00392 
00393         labelList sortedFp(f.size());
00394         forAll(dist.indices(), i)
00395         {
00396             label fp = dist.indices()[i];
00397 
00398             Pout<< "   fp:" << fp << " distance:" << dist[i] << nl;
00399 
00400             sortedFp[fp] = i;
00401         }
00402 
00403         const labelList& fEdges = mesh_.faceEdges()[faceI];
00404 
00405         // Now look up all edges in the face and see if they get extra
00406         // vertices inserted and build an edge-to-intersected-points table.
00407 
00408         // Order of inserted points is in edge order (from e.start to
00409         // e.end)
00410 
00411         forAll(f, fp)
00412         {
00413             label fp1 = f.fcIndex(fp);
00414 
00415             // Get index in sorted list
00416             label sorted0 = sortedFp[fp];
00417             label sorted1 = sortedFp[fp1];
00418 
00419             // Get indices in increasing order
00420             DynamicList<label> edgePoints(f.size());
00421 
00422             // Whether edgePoints are from fp to fp1
00423             bool fpToFp1;
00424 
00425             if (sorted0 < sorted1)
00426             {
00427                 fpToFp1 = true;
00428 
00429                 for (label j = sorted0+1; j < sorted1; j++)
00430                 {
00431                     edgePoints.append(f[dist.indices()[j]]);
00432                 }
00433             }
00434             else
00435             {
00436                 fpToFp1 = false;
00437 
00438                 for (label j = sorted1+1; j < sorted0; j++)
00439                 {
00440                     edgePoints.append(f[dist.indices()[j]]);
00441                 }
00442             }
00443 
00444             if (edgePoints.size())
00445             {
00446                 edgePoints.shrink();
00447 
00448                 label edgeI = findEdge(edges, fEdges, f[fp], f[fp1]);
00449 
00450                 const edge& e = edges[edgeI];
00451 
00452                 if (fpToFp1 == (f[fp] == e.start()))
00453                 {
00454                     splitEdges.insert(edgeI, edgePoints);
00455                 }
00456                 else
00457                 {
00458                     reverse(edgePoints);
00459                     splitEdges.insert(edgeI, edgePoints);
00460                 }
00461 
00462                 // Mark all faces affected
00463                 insert(edgeFaces[edgeI], faceI, affectedFaces);
00464             }
00465         }
00466     }
00467 
00468     for
00469     (
00470         Map<labelList>::const_iterator iter = splitEdges.begin();
00471         iter != splitEdges.end();
00472         ++iter
00473     )
00474     {
00475         Pout<< "Split edge:" << iter.key()
00476             << " verts:" << mesh_.edges()[iter.key()]
00477             << " in:" << nl;
00478         const labelList& edgePoints = iter();
00479 
00480         forAll(edgePoints, i)
00481         {
00482             Pout<< "    " << edgePoints[i] << nl;
00483         }
00484     }
00485 
00486 
00487     //
00488     // Remove faces.
00489     //
00490 
00491     forAll(faceLabels, i)
00492     {
00493         const label faceI = faceLabels[i];
00494 
00495         meshMod.setAction(polyRemoveFace(faceI));
00496 
00497         // Update list of faces we still have to modify
00498         affectedFaces.erase(faceI);
00499     }
00500 
00501 
00502     //
00503     // Modify faces affected (but not removed)
00504     //
00505 
00506     for
00507     (
00508         labelHashSet::const_iterator iter = affectedFaces.begin();
00509         iter != affectedFaces.end();
00510         ++iter
00511     )
00512     {
00513         filterFace(splitEdges, iter.key(), meshMod);
00514     }
00515 }
00516 
00517 
00518 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd