1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
27 #include <OpenFOAM/mergePoints.H>
28 #include <OpenFOAM/StaticHashTable.H>
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 void Foam::edgeMesh::calcPointEdges() const
36 if (pointEdgesPtr_.valid())
38 FatalErrorIn("edgeMesh::calcPointEdges() const")
39 << "pointEdges already calculated." << abort(FatalError);
42 pointEdgesPtr_.reset(new labelListList(points_.size()));
43 labelListList& pointEdges = pointEdgesPtr_();
46 labelList nEdgesPerPoint(points_.size(), 0);
50 const edge& e = edges_[edgeI];
52 nEdgesPerPoint[e[0]]++;
53 nEdgesPerPoint[e[1]]++;
57 forAll(pointEdges, pointI)
59 pointEdges[pointI].setSize(nEdgesPerPoint[pointI]);
67 const edge& e = edges_[edgeI];
70 pointEdges[p0][nEdgesPerPoint[p0]++] = edgeI;
72 pointEdges[p1][nEdgesPerPoint[p1]++] = edgeI;
77 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
79 // construct from components
80 Foam::edgeMesh::edgeMesh(const pointField& points, const edgeList& edges)
88 Foam::edgeMesh::edgeMesh(const edgeMesh& em)
96 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
98 Foam::label Foam::edgeMesh::regions(labelList& edgeRegion) const
100 edgeRegion.setSize(edges_.size());
103 label startEdgeI = 0;
105 label currentRegion = 0;
109 while (startEdgeI < edges_.size() && edgeRegion[startEdgeI] != -1)
114 if (startEdgeI == edges_.size())
119 // Found edge that has not yet been assigned a region.
120 // Mark connected region with currentRegion starting at startEdgeI.
122 edgeRegion[startEdgeI] = currentRegion;
123 labelList edgesToVisit(1, startEdgeI);
125 while (edgesToVisit.size())
127 // neighbours of current edgesToVisit
128 DynamicList<label> newEdgesToVisit(edgesToVisit.size());
130 // Mark all point connected edges with current region.
131 forAll(edgesToVisit, i)
133 label edgeI = edgesToVisit[i];
135 // Mark connected edges
136 const edge& e = edges_[edgeI];
140 const labelList& pEdges = pointEdges()[e[fp]];
142 forAll(pEdges, pEdgeI)
144 label nbrEdgeI = pEdges[pEdgeI];
146 if (edgeRegion[nbrEdgeI] == -1)
148 edgeRegion[nbrEdgeI] = currentRegion;
149 newEdgesToVisit.append(nbrEdgeI);
155 edgesToVisit.transfer(newEdgesToVisit);
160 return currentRegion;
164 void Foam::edgeMesh::mergePoints(const scalar mergeDist)
166 pointField newPoints;
169 bool hasMerged = Foam::mergePoints
181 pointEdgesPtr_.clear();
183 points_.transfer(newPoints);
185 // Renumber and make sure e[0] < e[1] (not really nessecary)
186 forAll(edges_, edgeI)
188 edge& e = edges_[edgeI];
190 label p0 = pointMap[e[0]];
191 label p1 = pointMap[e[1]];
205 // Compact using a hashtable and commutative hash of edge.
206 StaticHashTable<label, edge, Hash<edge> > edgeToLabel
213 forAll(edges_, edgeI)
215 const edge& e = edges_[edgeI];
219 if (edgeToLabel.insert(e, newEdgeI))
226 edges_.setSize(newEdgeI);
230 StaticHashTable<label, edge, Hash<edge> >::const_iterator iter =
232 iter != edgeToLabel.end();
236 edges_[iter()] = iter.key();
242 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
244 void Foam::edgeMesh::operator=(const edgeMesh& rhs)
246 points_ = rhs.points_;
248 pointEdgesPtr_.reset(NULL);
252 // ************************ vim: set sw=4 sts=4 et: ************************ //