DelaunayMesh.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2012-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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
19  for more details.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "DelaunayMesh.H"
27 #include "labelPair.H"
28 #include "PrintTable.H"
29 #include "pointIOField.H"
30 #include "scalarIOField.H"
31 #include "labelIOField.H"
32 #include "pointConversion.H"
33 
34 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
35 
36 template<class Triangulation>
38 :
39  Triangulation(),
40  vertexCount_(0),
41  cellCount_(0),
42  runTime_(runTime)
43 {}
44 
45 
46 template<class Triangulation>
48 (
49  const Time& runTime,
50  const word& meshName
51 )
52 :
53  Triangulation(),
54  vertexCount_(0),
55  cellCount_(0),
56  runTime_(runTime)
57 {
58  Info<< "Reading " << meshName << " from " << runTime.timeName() << endl;
59 
60  pointIOField pts
61  (
62  IOobject
63  (
64  "points",
65  runTime.timeName(),
66  meshName/polyMesh::meshSubDir,
67  runTime,
70  )
71  );
72 
73  if (pts.headerOk())
74  {
75  labelIOField types
76  (
77  IOobject
78  (
79  "types",
80  runTime.timeName(),
81  meshName,
82  runTime,
85  )
86  );
87 
88 // Do not read in indices
89 // labelIOField indices
90 // (
91 // IOobject
92 // (
93 // "indices",
94 // runTime.timeName(),
95 // meshName,
96 // runTime,
97 // IOobject::MUST_READ,
98 // IOobject::NO_WRITE
99 // )
100 // );
101 
102  labelIOField processorIndices
103  (
104  IOobject
105  (
106  "processorIndices",
107  runTime.timeName(),
108  meshName,
109  runTime,
112  )
113  );
114 
115  List<Vb> pointsToInsert(pts.size());
116 
117  forAll(pointsToInsert, pI)
118  {
119  pointsToInsert[pI] =
120  Vb
121  (
122  toPoint(pts[pI]),
123  pI,
124  static_cast<indexedVertexEnum::vertexType>(types[pI]),
125  processorIndices[pI]
126  );
127  }
128 
129  rangeInsertWithInfo
130  (
131  pointsToInsert.begin(),
132  pointsToInsert.end(),
133  false,
134  false
135  );
136 
137  vertexCount_ = Triangulation::number_of_vertices();
138  }
139 }
140 
141 
142 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
143 
144 template<class Triangulation>
146 {}
147 
148 
149 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
150 
151 
152 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
153 
154 
155 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
156 
157 template<class Triangulation>
159 {
160  Info<< "Clearing triangulation" << endl;
161 
162  DynamicList<Vb> vertices;
163 
164  for
165  (
166  Finite_vertices_iterator vit = Triangulation::finite_vertices_begin();
167  vit != Triangulation::finite_vertices_end();
168  ++vit
169  )
170  {
171  if (vit->fixed())
172  {
173  vertices.append
174  (
175  Vb
176  (
177  vit->point(),
178  vit->index(),
179  vit->type(),
180  vit->procIndex()
181  )
182  );
183 
184  vertices.last().fixed() = vit->fixed();
185  }
186  }
187 
188  this->clear();
189 
190  resetVertexCount();
191  resetCellCount();
192 
193  insertPoints(vertices, false);
194 
195  Info<< "Inserted " << vertexCount() << " fixed points" << endl;
196 }
197 
198 
199 template<class Triangulation>
201 (
202  const List<Vb>& vertices,
203  const bool reIndex
204 )
205 {
206  return rangeInsertWithInfo
207  (
208  vertices.begin(),
209  vertices.end(),
210  false,
211  reIndex
212  );
213 }
214 
215 
216 template<class Triangulation>
218 operator()
219 (
220  const Point_3& p,
221  const Point_3& q
222 ) const
223 {
224  return typename Gt::Less_x_3()(*(p.first), *(q.first));
225 }
226 
227 template<class Triangulation>
229 operator()
230 (
231  const Point_3& p,
232  const Point_3& q
233 ) const
234 {
235  return typename Gt::Less_y_3()(*(p.first), *(q.first));
236 }
237 
238 template<class Triangulation>
240 operator()
241 (
242  const Point_3& p,
243  const Point_3& q
244 ) const
245 {
246  return typename Gt::Less_z_3()(*(p.first), *(q.first));
247 }
248 
249 template<class Triangulation>
252 const
253 {
254  return Less_x_3();
255 }
256 
257 template<class Triangulation>
260 const
261 {
262  return Less_y_3();
263 }
264 
265 template<class Triangulation>
268 const
269 {
270  return Less_z_3();
271 }
272 
273 
274 template<class Triangulation>
275 template<class PointIterator>
277 (
278  PointIterator begin,
279  PointIterator end,
280  bool printErrors,
281  bool reIndex
282 )
283 {
284  typedef DynamicList
285  <
286  std::pair
287  <
288  const typename Triangulation::Point*,
289  label
290  >
291  > vectorPairPointIndex;
292 
293  vectorPairPointIndex points;
294 
295  label count = 0;
296  for (PointIterator it = begin; it != end; ++it)
297  {
298  points.append
299  (
300  std::make_pair(&(it->point()), count++)
301  );
302  }
303 
304  std::random_shuffle(points.begin(), points.end());
305 
306  spatial_sort
307  (
308  points.begin(),
309  points.end(),
310  Traits_for_spatial_sort()
311  );
312 
313  Vertex_handle hint;
314 
315  Map<label> oldToNewIndex(points.size());
316 
317  for
318  (
319  typename vectorPairPointIndex::const_iterator p = points.begin();
320  p != points.end();
321  ++p
322  )
323  {
324  const size_t checkInsertion = Triangulation::number_of_vertices();
325 
326  hint = this->insert(*(p->first), hint);
327 
328  const Vb& vert = *(begin + p->second);
329 
330  if (checkInsertion != Triangulation::number_of_vertices() - 1)
331  {
332  if (printErrors)
333  {
334  Vertex_handle nearV =
335  Triangulation::nearest_vertex(*(p->first));
336 
337  Pout<< "Failed insertion : " << vert.info()
338  << " nearest : " << nearV->info();
339  }
340  }
341  else
342  {
343  const label oldIndex = vert.index();
344  hint->index() = getNewVertexIndex();
345 
346  if (reIndex)
347  {
348  oldToNewIndex.insert(oldIndex, hint->index());
349  }
350 
351  hint->type() = vert.type();
352  hint->procIndex() = vert.procIndex();
353  hint->targetCellSize() = vert.targetCellSize();
354  hint->alignment() = vert.alignment();
355  }
356  }
357 
358  return oldToNewIndex;
359 }
360 
361 
362 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
363 
364 #include "DelaunayMeshIO.C"
365 
366 // ************************************************************************* //
Map< label > rangeInsertWithInfo(PointIterator begin, PointIterator end, bool printErrors=false, bool reIndex=true)
Function inserting points into a triangulation and setting the.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
The vertex and cell classes must have an index defined.
Definition: DelaunayMesh.H:60
tUEqn clear()
vectorIOField pointIOField
pointIOField is a vectorIOField.
Definition: pointIOField.H:42
~DelaunayMesh()
Destructor.
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:309
vertexType & type()
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
Foam::label & index()
Foam::scalar & targetCellSize()
An indexed form of CGAL::Triangulation_vertex_base_3<K> used to keep track of the Delaunay vertices i...
Definition: indexedVertex.H:51
CGAL::indexedVertex< K > Vb
static char meshName[]
Definition: globalFoam.H:7
const pointField & points
timeIndices insert(timeIndex, timeDirs[timeI].value())
void reset()
Clear the entire triangulation.
PointFrompoint toPoint(const Foam::point &p)
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53
int procIndex() const
Foam::tensor & alignment()
Foam::InfoProxy< indexedVertex< Gt, Vb > > info() const
Return info proxy.
messageStream Info
Map< label > insertPoints(const List< Vb > &vertices, const bool reIndex)
Insert the list of vertices (calls rangeInsertWithInfo)
volScalarField & p
IOField< label > labelIOField
labelField with IO.
Definition: labelIOField.H:42
A HashTable to objects of type <T> with a label key.
Definition: Map.H:49