FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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