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

edgeMesh.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 \*---------------------------------------------------------------------------*/
00025 
00026 #include "edgeMesh.H"
00027 #include <OpenFOAM/mergePoints.H>
00028 #include <OpenFOAM/StaticHashTable.H>
00029 
00030 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00031 
00032 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00033 
00034 void Foam::edgeMesh::calcPointEdges() const
00035 {
00036     if (pointEdgesPtr_.valid())
00037     {
00038         FatalErrorIn("edgeMesh::calcPointEdges() const")
00039             << "pointEdges already calculated." << abort(FatalError);
00040     }
00041 
00042     pointEdgesPtr_.reset(new labelListList(points_.size()));
00043     labelListList& pointEdges = pointEdgesPtr_();
00044 
00045     // Count
00046     labelList nEdgesPerPoint(points_.size(), 0);
00047 
00048     forAll(edges_, edgeI)
00049     {
00050         const edge& e = edges_[edgeI];
00051 
00052         nEdgesPerPoint[e[0]]++;
00053         nEdgesPerPoint[e[1]]++;
00054     }
00055 
00056     // Size
00057     forAll(pointEdges, pointI)
00058     {
00059         pointEdges[pointI].setSize(nEdgesPerPoint[pointI]);
00060     }
00061 
00062     // Fill
00063     nEdgesPerPoint = 0;
00064 
00065     forAll(edges_, edgeI)
00066     {
00067         const edge& e = edges_[edgeI];
00068 
00069         label p0 = e[0];
00070         pointEdges[p0][nEdgesPerPoint[p0]++] = edgeI;
00071         label p1 = e[1];
00072         pointEdges[p1][nEdgesPerPoint[p1]++] = edgeI;
00073     }
00074 }
00075 
00076 
00077 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00078 
00079 // construct from components
00080 Foam::edgeMesh::edgeMesh(const pointField& points, const edgeList& edges)
00081 :
00082     points_(points),
00083     edges_(edges)
00084 {}
00085 
00086 
00087 // construct as copy
00088 Foam::edgeMesh::edgeMesh(const edgeMesh& em)
00089 :
00090     points_(em.points_),
00091     edges_(em.edges_),
00092     pointEdgesPtr_(NULL)
00093 {}
00094 
00095 
00096 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00097 
00098 Foam::label Foam::edgeMesh::regions(labelList& edgeRegion) const
00099 {
00100     edgeRegion.setSize(edges_.size());
00101     edgeRegion = -1;
00102 
00103     label startEdgeI = 0;
00104 
00105     label currentRegion = 0;
00106 
00107     while (true)
00108     {
00109         while (startEdgeI < edges_.size() && edgeRegion[startEdgeI] != -1)
00110         {
00111             startEdgeI++;
00112         }
00113 
00114         if (startEdgeI == edges_.size())
00115         {
00116             break;
00117         }
00118 
00119         // Found edge that has not yet been assigned a region.
00120         // Mark connected region with currentRegion starting at startEdgeI.
00121 
00122         edgeRegion[startEdgeI] = currentRegion;
00123         labelList edgesToVisit(1, startEdgeI);
00124 
00125         while (edgesToVisit.size())
00126         {
00127             // neighbours of current edgesToVisit
00128             DynamicList<label> newEdgesToVisit(edgesToVisit.size());
00129 
00130             // Mark all point connected edges with current region.
00131             forAll(edgesToVisit, i)
00132             {
00133                 label edgeI = edgesToVisit[i];
00134 
00135                 // Mark connected edges
00136                 const edge& e = edges_[edgeI];
00137 
00138                 forAll(e, fp)
00139                 {
00140                     const labelList& pEdges = pointEdges()[e[fp]];
00141 
00142                     forAll(pEdges, pEdgeI)
00143                     {
00144                         label nbrEdgeI = pEdges[pEdgeI];
00145 
00146                         if (edgeRegion[nbrEdgeI] == -1)
00147                         {
00148                             edgeRegion[nbrEdgeI] = currentRegion;
00149                             newEdgesToVisit.append(nbrEdgeI);
00150                         }
00151                     }
00152                 }
00153             }
00154 
00155             edgesToVisit.transfer(newEdgesToVisit);
00156         }
00157 
00158         currentRegion++;
00159     }
00160     return currentRegion;
00161 }
00162 
00163 
00164 void Foam::edgeMesh::mergePoints(const scalar mergeDist)
00165 {
00166     pointField newPoints;
00167     labelList pointMap;
00168 
00169     bool hasMerged = Foam::mergePoints
00170     (
00171         points_,
00172         mergeDist,
00173         false,
00174         pointMap,
00175         newPoints,
00176         vector::zero
00177     );
00178 
00179     if (hasMerged)
00180     {
00181         pointEdgesPtr_.clear();
00182 
00183         points_.transfer(newPoints);
00184 
00185         // Renumber and make sure e[0] < e[1] (not really nessecary)
00186         forAll(edges_, edgeI)
00187         {
00188             edge& e = edges_[edgeI];
00189 
00190             label p0 = pointMap[e[0]];
00191             label p1 = pointMap[e[1]];
00192 
00193             if (p0 < p1)
00194             {
00195                 e[0] = p0;
00196                 e[1] = p1;
00197             }
00198             else
00199             {
00200                 e[0] = p1;
00201                 e[1] = p0;
00202             }
00203         }
00204 
00205         // Compact using a hashtable and commutative hash of edge.
00206         StaticHashTable<label, edge, Hash<edge> > edgeToLabel
00207         (
00208             2*edges_.size()
00209         );
00210 
00211         label newEdgeI = 0;
00212 
00213         forAll(edges_, edgeI)
00214         {
00215             const edge& e = edges_[edgeI];
00216 
00217             if (e[0] != e[1])
00218             {
00219                 if (edgeToLabel.insert(e, newEdgeI))
00220                 {
00221                     newEdgeI++;
00222                 }
00223             }
00224         }
00225 
00226         edges_.setSize(newEdgeI);
00227 
00228         for
00229         (
00230             StaticHashTable<label, edge, Hash<edge> >::const_iterator iter =
00231                 edgeToLabel.begin();
00232             iter != edgeToLabel.end();
00233             ++iter
00234         )
00235         {
00236             edges_[iter()] = iter.key();
00237         }
00238     }
00239 }
00240 
00241 
00242 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00243 
00244 void Foam::edgeMesh::operator=(const edgeMesh& rhs)
00245 {
00246     points_ = rhs.points_;
00247     edges_ = rhs.edges_;
00248     pointEdgesPtr_.reset(NULL);
00249 }
00250 
00251 
00252 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines