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

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