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

edgeSurface.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 "edgeSurface.H"
00030 #include "triSurface.H"
00031 #include "surfaceIntersection.H"
00032 #include "meshTools.H"
00033 #include "OFstream.H"
00034 
00035 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00036 
00037 defineTypeNameAndDebug(Foam::edgeSurface, 0);
00038 
00039 
00040 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00041 
00042 // Write whole pointField and edges to stream
00043 void Foam::edgeSurface::writeOBJ
00044 (
00045     const pointField& points,
00046     const edgeList& edges,
00047     Ostream& os
00048 )
00049 {
00050     forAll(points, pointI)
00051     {
00052         const point& pt = points[pointI];
00053 
00054         os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
00055     }
00056     forAll(edges, edgeI)
00057     {
00058         const edge& e = edges[edgeI];
00059 
00060         os << "l " << e.start()+1 << ' ' << e.end()+1 << endl;
00061     }
00062 }
00063 
00064 
00065 // Write whole pointField and selected edges to stream
00066 void Foam::edgeSurface::writeOBJ
00067 (
00068     const pointField& points,
00069     const edgeList& edges,
00070     const labelList& edgeLabels,
00071     Ostream& os
00072 )
00073 {
00074     forAll(points, pointI)
00075     {
00076         const point& pt = points[pointI];
00077 
00078         os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
00079     }
00080     forAll(edgeLabels, i)
00081     {
00082         const edge& e = edges[edgeLabels[i]];
00083 
00084         os << "l " << e.start()+1 << ' ' << e.end()+1 << endl;
00085     }
00086 }
00087 
00088 
00089 // Pointedges in edgeSurface indices only.
00090 void Foam::edgeSurface::calcPointEdges()
00091 {
00092     pointEdges_.setSize(points_.size());
00093 
00094     labelList pointNEdges(points_.size(), 0);
00095 
00096     forAll(edges_, edgeI)
00097     {
00098         const edge& e = edges_[edgeI];
00099 
00100         pointNEdges[e[0]]++;
00101         pointNEdges[e[1]]++;
00102     }
00103 
00104     forAll(pointEdges_, pointI)
00105     {
00106         pointEdges_[pointI].setSize(pointNEdges[pointI]);
00107     }
00108 
00109     pointNEdges = 0;
00110 
00111     forAll(edges_, edgeI)
00112     {
00113         const edge& e = edges_[edgeI];
00114 
00115         labelList& pEdges0 = pointEdges_[e[0]];
00116         pEdges0[pointNEdges[e[0]]++] = edgeI;
00117 
00118         labelList& pEdges1 = pointEdges_[e[1]];
00119         pEdges1[pointNEdges[e[1]]++] = edgeI;
00120     }
00121 }
00122 
00123 
00124 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00125 
00126 // Construct from surface and intersection description
00127 Foam::edgeSurface::edgeSurface
00128 (
00129     const triSurface& surf,
00130     const bool isFirstSurface,
00131     const surfaceIntersection& inter
00132 )
00133 :
00134     points_(surf.nPoints() + inter.cutPoints().size()),
00135     nSurfacePoints_(surf.nPoints()),
00136     edges_(),
00137     nSurfaceEdges_(surf.nEdges()),
00138     parentEdges_(0),
00139     faceEdges_(surf.size()),
00140     pointEdges_(points_.size())
00141 {
00142     // Copy points (surface ones first)
00143     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00144 
00145     label pointI = 0;
00146 
00147     const pointField& surfPoints = surf.localPoints();
00148 
00149     forAll(surfPoints, i)
00150     {
00151         points_[pointI++] = surfPoints[i];
00152     }
00153 
00154     const pointField& cutPoints = inter.cutPoints();
00155 
00156     forAll(cutPoints, i)
00157     {
00158         points_[pointI++] = cutPoints[i];
00159     }
00160 
00161 
00162     // Copy edges (surface ones first)
00163     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00164 
00165     DynamicList<edge> allEdges(surf.nEdges() + inter.cutEdges().size());
00166     DynamicList<label> allParentEdges(surf.nEdges());
00167     List<DynamicList<label> > allFaceEdges(surf.size());
00168 
00169 
00170     // Copy surface edges (can be split!)
00171 
00172     const edgeList& surfEdges = surf.edges();
00173 
00174     forAll(surfEdges, edgeI)
00175     {
00176         const edge& e = surfEdges[edgeI];
00177 
00178         // Get additional vertices for this edge.
00179         const labelList& extraVerts = inter.edgeCuts(isFirstSurface)[edgeI];
00180 
00181         // Store current top of allEdges.
00182         label freeNewEdgeI = allEdges.size();
00183 
00184         if (extraVerts.empty())
00185         {
00186             // No cuts across this edge. Note that vertices do not need to be
00187             // renumbered.
00188             allEdges.append(e);
00189         }
00190         else
00191         {
00192             // Edge is cut. From e.start() to extraVerts[0],
00193             // from extraVerts[i] to i+1 and finally to e.end().
00194             allEdges.append
00195             (
00196                 edge
00197                 (
00198                     e.start(),
00199                     extraVerts[0] + nSurfacePoints_
00200                 )
00201             );
00202 
00203             for (label extraI = 1; extraI < extraVerts.size(); extraI++)
00204             {
00205                 allEdges.append
00206                 (
00207                     edge
00208                     (
00209                         extraVerts[extraI-1] + nSurfacePoints_,
00210                         extraVerts[extraI] + nSurfacePoints_
00211                     )
00212                 );
00213             }
00214             allEdges.append
00215             (
00216                 edge
00217                 (
00218                     extraVerts[extraVerts.size()-1] + nSurfacePoints_,
00219                     e.end()
00220                 )
00221             );
00222         }
00223 
00224         // Update allFaceEdges, parentEdges_ for the newly added edges.
00225 
00226         // Add each edge label to all face neighbours of edgeI
00227         const labelList& myFaces = surf.edgeFaces()[edgeI];
00228 
00229         for (label eI = freeNewEdgeI; eI < allEdges.size(); eI++)
00230         {
00231             allParentEdges.append(edgeI);
00232 
00233             forAll(myFaces, myFaceI)
00234             {
00235                 allFaceEdges[myFaces[myFaceI]].append(eI);
00236             }
00237         }
00238     }
00239 
00240     // Done all (possibly split) surface edges by now.
00241     nSurfaceEdges_ = allEdges.size();
00242 
00243 
00244     // Copy intersection edges
00245     // (note no parentEdges)
00246     const edgeList& cutEdges = inter.cutEdges();
00247 
00248     forAll(cutEdges, i)
00249     {
00250         const edge& e = cutEdges[i];
00251 
00252         allEdges.append(edge(e[0] + nSurfacePoints_, e[1] + nSurfacePoints_));
00253     }
00254 
00255 
00256 
00257 
00258     // Add intersection edges to faceEdges
00259     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00260 
00261     for
00262     (
00263         labelPairLookup::const_iterator iter = inter.facePairToEdge().begin();
00264         iter != inter.facePairToEdge().end();
00265         ++iter
00266     )
00267     {
00268         // Edge label in intersection
00269         label edgeI = iter();
00270 
00271         // Get the face from the correct surface
00272         const FixedList<label, 2>& twoFaces = iter.key();
00273 
00274         label faceI;
00275 
00276         if (isFirstSurface)
00277         {
00278             faceI = twoFaces[0];
00279         }
00280         else
00281         {
00282             faceI = twoFaces[1];
00283         }
00284 
00285         // Store on face-edge addressing. (note: offset edge)
00286         allFaceEdges[faceI].append(edgeI + nSurfaceEdges_);
00287     }
00288 
00289     // Transfer.
00290     edges_.transfer(allEdges);
00291     parentEdges_.transfer(allParentEdges);
00292 
00293     forAll(allFaceEdges, faceI)
00294     {
00295         faceEdges_[faceI].transfer(allFaceEdges[faceI]);
00296     }
00297 
00298 
00299     // Additional addressing
00300     // ~~~~~~~~~~~~~~~~~~~~~
00301 
00302     calcPointEdges();
00303 
00304 
00305     if (debug & 4)
00306     {
00307         Pout<< "edgeSurface : Dumping faceEdges to files" << endl;
00308 
00309         forAll(faceEdges_, faceI)
00310         {
00311             const labelList& fEdges = faceEdges_[faceI];
00312 
00313             if (fEdges.size() != 3)
00314             {
00315                 fileName faceFName("face_" + name(faceI) + ".obj");
00316                 Pout<< "edgeSurface : Dumping faceEdges for face " << faceI
00317                     << " to " << faceFName << endl;
00318 
00319                 OFstream fStream(faceFName);
00320                 writeOBJ(points_, edges_, fEdges, fStream);
00321             }
00322         }
00323 
00324         Pout<< "edgeSurface : Dumping edges to edges.obj" << endl;
00325         OFstream eStream("edges.obj");
00326         writeOBJ(points_, edges_, eStream);
00327 
00328         Pout<< "edgeSurface : Dumping intersectionEdges to"
00329             << " intersectionEdges.obj" << endl;
00330         OFstream intEdgesStream("intersectionEdges.obj");
00331 
00332         labelList edgeLabels(edges_.size() - nSurfaceEdges_);
00333 
00334         label i = 0;
00335         for(label edgeI = nSurfaceEdges_; edgeI < edges_.size(); edgeI++)
00336         {
00337             edgeLabels[i++] = edgeI;
00338         }
00339 
00340         writeOBJ(points_, edges_, edgeLabels, intEdgesStream);
00341     }
00342 }
00343 
00344 
00345 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00346 
00347 void Foam::edgeSurface::addIntersectionEdges
00348 (
00349     const label faceI,
00350     const edgeList& additionalEdges
00351 )
00352 {
00353     if (debug & 2)
00354     {
00355         Pout<< "Old face consisted of edges:" << endl;
00356 
00357         const labelList& fEdges = faceEdges_[faceI];
00358         forAll(fEdges, i)
00359         {
00360             const edge& e = edges_[fEdges[i]];
00361 
00362             Pout<< "    " << fEdges[i] << ' ' << e
00363                 << points_[e.start()] << ' ' << points_[e.end()] << endl;
00364         }
00365     }
00366 
00367     // Make space for additional intersection edges (copies old ones)
00368     const label oldNEdges = edges_.size();
00369 
00370     edges_.setSize(oldNEdges + additionalEdges.size());
00371 
00372     // Append new intersection edges
00373     label newEdgeI = oldNEdges;
00374 
00375     forAll(additionalEdges, i)
00376     {
00377         edges_[newEdgeI] = additionalEdges[i];  // Vertices already in eSurf
00378                                                 // indices.
00379         newEdgeI++;
00380     }
00381 
00382     // Append to faceEdges.
00383     labelList& fEdges = faceEdges_[faceI];
00384 
00385     label nFEdges = fEdges.size();
00386 
00387     fEdges.setSize(nFEdges + additionalEdges.size());
00388 
00389     forAll(additionalEdges, i)
00390     {
00391         fEdges[nFEdges++] = oldNEdges + i;
00392     }
00393 
00394 
00395     // Update pointEdge addressing
00396     calcPointEdges();
00397 
00398 
00399     if (debug & 2)
00400     {
00401         const labelList& fEdges = faceEdges_[faceI];
00402 
00403         Pout<< "New face consists of edges:" << endl;
00404         forAll(fEdges, i)
00405         {
00406             const edge& e = edges_[fEdges[i]];
00407 
00408             Pout<< "    " << fEdges[i] << ' ' << e
00409                 << points_[e.start()] << ' ' << points_[e.end()] << endl;
00410         }
00411     }
00412 }
00413 
00414 
00415 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd