1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 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, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
28 #include "mergePoints.H"
29 #include "StaticHashTable.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
35 void Foam::edgeMesh::calcPointEdges() const
37 if (pointEdgesPtr_.valid())
39 FatalErrorIn("edgeMesh::calcPointEdges() const")
40 << "pointEdges already calculated." << abort(FatalError);
43 pointEdgesPtr_.reset(new labelListList(points_.size()));
44 labelListList& pointEdges = pointEdgesPtr_();
47 labelList nEdgesPerPoint(points_.size(), 0);
51 const edge& e = edges_[edgeI];
53 nEdgesPerPoint[e[0]]++;
54 nEdgesPerPoint[e[1]]++;
58 forAll(pointEdges, pointI)
60 pointEdges[pointI].setSize(nEdgesPerPoint[pointI]);
68 const edge& e = edges_[edgeI];
71 pointEdges[p0][nEdgesPerPoint[p0]++] = edgeI;
73 pointEdges[p1][nEdgesPerPoint[p1]++] = edgeI;
78 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
80 // construct from components
81 Foam::edgeMesh::edgeMesh(const pointField& points, const edgeList& edges)
89 Foam::edgeMesh::edgeMesh(const edgeMesh& em)
97 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
99 Foam::label Foam::edgeMesh::regions(labelList& edgeRegion) const
101 edgeRegion.setSize(edges_.size());
104 label startEdgeI = 0;
106 label currentRegion = 0;
110 while (startEdgeI < edges_.size() && edgeRegion[startEdgeI] != -1)
115 if (startEdgeI == edges_.size())
120 // Found edge that has not yet been assigned a region.
121 // Mark connected region with currentRegion starting at startEdgeI.
123 edgeRegion[startEdgeI] = currentRegion;
124 labelList edgesToVisit(1, startEdgeI);
126 while (edgesToVisit.size())
128 // neighbours of current edgesToVisit
129 DynamicList<label> newEdgesToVisit(edgesToVisit.size());
131 // Mark all point connected edges with current region.
132 forAll(edgesToVisit, i)
134 label edgeI = edgesToVisit[i];
136 // Mark connected edges
137 const edge& e = edges_[edgeI];
141 const labelList& pEdges = pointEdges()[e[fp]];
143 forAll(pEdges, pEdgeI)
145 label nbrEdgeI = pEdges[pEdgeI];
147 if (edgeRegion[nbrEdgeI] == -1)
149 edgeRegion[nbrEdgeI] = currentRegion;
150 newEdgesToVisit.append(nbrEdgeI);
156 edgesToVisit.transfer(newEdgesToVisit);
161 return currentRegion;
165 void Foam::edgeMesh::mergePoints(const scalar mergeDist)
167 pointField newPoints;
170 bool hasMerged = Foam::mergePoints
182 pointEdgesPtr_.clear();
184 points_.transfer(newPoints);
186 // Renumber and make sure e[0] < e[1] (not really nessecary)
187 forAll(edges_, edgeI)
189 edge& e = edges_[edgeI];
191 label p0 = pointMap[e[0]];
192 label p1 = pointMap[e[1]];
206 // Compact using a hashtable and commutative hash of edge.
207 StaticHashTable<label, edge, Hash<edge> > edgeToLabel
214 forAll(edges_, edgeI)
216 const edge& e = edges_[edgeI];
220 if (edgeToLabel.insert(e, newEdgeI))
227 edges_.setSize(newEdgeI);
231 StaticHashTable<label, edge, Hash<edge> >::const_iterator iter =
233 iter != edgeToLabel.end();
237 edges_[iter()] = iter.key();
243 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
245 void Foam::edgeMesh::operator=(const edgeMesh& rhs)
247 points_ = rhs.points_;
249 pointEdgesPtr_.reset(NULL);
253 // ************************************************************************* //