CV2DIO.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2013-2016 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "CV2D.H"
29 #include "OFstream.H"
30 
31 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
32 
33 void Foam::CV2D::writePoints(const fileName& fName, bool internalOnly) const
34 {
35  Info<< "Writing points to " << fName << nl << endl;
36  OFstream str(fName);
37 
38  for
39  (
40  Triangulation::Finite_vertices_iterator vit = finite_vertices_begin();
41  vit != finite_vertices_end();
42  ++vit
43  )
44  {
45  if (!internalOnly || vit->internalOrBoundaryPoint())
46  {
47  meshTools::writeOBJ(str, toPoint3D(vit->point()));
48  }
49  }
50 }
51 
52 
53 void Foam::CV2D::writeTriangles(const fileName& fName, bool internalOnly) const
54 {
55  Info<< "Writing triangles to " << fName << nl << endl;
56  OFstream str(fName);
57 
58  labelList vertexMap(number_of_vertices(), -2);
59  label verti = 0;
60 
61  for
62  (
63  Triangulation::Finite_vertices_iterator vit = finite_vertices_begin();
64  vit != finite_vertices_end();
65  ++vit
66  )
67  {
68  if (!internalOnly || !vit->farPoint())
69  {
70  vertexMap[vit->index()] = verti++;
71  meshTools::writeOBJ(str, toPoint3D(vit->point()));
72  }
73  }
74 
75  for
76  (
77  Triangulation::Finite_faces_iterator fit = finite_faces_begin();
78  fit != finite_faces_end();
79  ++fit
80  )
81  {
82  if
83  (
84  !internalOnly
85  || (
86  fit->vertex(0)->internalOrBoundaryPoint()
87  || fit->vertex(1)->internalOrBoundaryPoint()
88  || fit->vertex(2)->internalOrBoundaryPoint()
89  )
90  )
91  {
92  str << "f";
93  for (label i = 0; i < 3; ++i)
94  {
95  str << " " << vertexMap[fit->vertex(i)->index()] + 1;
96  }
97  str << nl;
98  }
99  }
100 }
101 
102 
103 void Foam::CV2D::writeFaces(const fileName& fName, bool internalOnly) const
104 {
105  Info<< "Writing dual faces to " << fName << nl << endl;
106  OFstream str(fName);
107 
108  label dualVerti = 0;
109 
110  for
111  (
112  Triangulation::Finite_faces_iterator fit = finite_faces_begin();
113  fit != finite_faces_end();
114  ++fit
115  )
116  {
117  if
118  (
119  !internalOnly
120  || (
121  fit->vertex(0)->internalOrBoundaryPoint()
122  || fit->vertex(1)->internalOrBoundaryPoint()
123  || fit->vertex(2)->internalOrBoundaryPoint()
124  )
125  )
126  {
127  fit->faceIndex() = dualVerti++;
128  meshTools::writeOBJ(str, toPoint3D(circumcenter(fit)));
129  }
130  else
131  {
132  fit->faceIndex() = -1;
133  }
134  }
135 
136  for
137  (
138  Triangulation::Finite_vertices_iterator vit = finite_vertices_begin();
139  vit != finite_vertices_end();
140  ++vit
141  )
142  {
143  if (!internalOnly || vit->internalOrBoundaryPoint())
144  {
145  Face_circulator fcStart = incident_faces(vit);
146  Face_circulator fc = fcStart;
147 
148  str<< 'f';
149 
150  do
151  {
152  if (!is_infinite(fc))
153  {
154  if (fc->faceIndex() < 0)
155  {
157  << "Dual face uses vertex defined by a triangle"
158  " defined by an external point"
159  << exit(FatalError);
160  }
161 
162  str<< ' ' << fc->faceIndex() + 1;
163  }
164  } while (++fc != fcStart);
165 
166  str<< nl;
167  }
168  }
169 }
170 
171 
173 (
175  labelList& patchSizes,
176  EdgeMap<label>& mapEdgesRegion,
177  EdgeMap<label>& indirectPatchEdge
178 ) const
179 {
180  label nPatches = qSurf_.patchNames().size() + 1;
181  label defaultPatchIndex = qSurf_.patchNames().size();
182 
184  patchSizes.setSize(nPatches, 0);
185  mapEdgesRegion.clear();
186 
187  const wordList& existingPatches = qSurf_.patchNames();
188 
189  forAll(existingPatches, sP)
190  {
191  patchNames[sP] = existingPatches[sP];
192  }
193 
194  patchNames[defaultPatchIndex] = "CV2D_default_patch";
195 
196  for
197  (
198  Triangulation::Finite_edges_iterator eit = finite_edges_begin();
199  eit != finite_edges_end();
200  ++eit
201  )
202  {
203  Face_handle fOwner = eit->first;
204  Face_handle fNeighbor = fOwner->neighbor(eit->second);
205 
206  Vertex_handle vA = fOwner->vertex(cw(eit->second));
207  Vertex_handle vB = fOwner->vertex(ccw(eit->second));
208 
209  if
210  (
211  (vA->internalOrBoundaryPoint() && !vB->internalOrBoundaryPoint())
212  || (vB->internalOrBoundaryPoint() && !vA->internalOrBoundaryPoint())
213  )
214  {
215  Foam::point ptA = toPoint3D(vA->point());
216  Foam::point ptB = toPoint3D(vB->point());
217 
218  label patchIndex = qSurf_.findPatch(ptA, ptB);
219 
220  if (patchIndex == -1)
221  {
222  patchIndex = defaultPatchIndex;
223 
225  << "Dual face found that is not on a surface "
226  << "patch. Adding to CV2D_default_patch."
227  << endl;
228  }
229 
230  edge e(fOwner->faceIndex(), fNeighbor->faceIndex());
231  patchSizes[patchIndex]++;
232  mapEdgesRegion.insert(e, patchIndex);
233 
234  if (!pointPair(*vA, *vB))
235  {
236  indirectPatchEdge.insert(e, 1);
237  }
238  }
239  }
240 }
241 
242 
244 (
245  point2DField& dualPoints,
246  faceList& dualFaces,
248  labelList& patchSizes,
249  EdgeMap<label>& mapEdgesRegion,
250  EdgeMap<label>& indirectPatchEdge
251 ) const
252 {
253  // Dual points stored in triangle order.
254  dualPoints.setSize(number_of_faces());
255  label dualVerti = 0;
256 
257  for
258  (
259  Triangulation::Finite_faces_iterator fit = finite_faces_begin();
260  fit != finite_faces_end();
261  ++fit
262  )
263  {
264  if
265  (
266  fit->vertex(0)->internalOrBoundaryPoint()
267  || fit->vertex(1)->internalOrBoundaryPoint()
268  || fit->vertex(2)->internalOrBoundaryPoint()
269  )
270  {
271  fit->faceIndex() = dualVerti;
272 
273  dualPoints[dualVerti++] = toPoint2D(circumcenter(fit));
274  }
275  else
276  {
277  fit->faceIndex() = -1;
278  }
279  }
280 
281  dualPoints.setSize(dualVerti);
282 
283  extractPatches(patchNames, patchSizes, mapEdgesRegion, indirectPatchEdge);
284 
285  forAll(patchNames, patchi)
286  {
287  Info<< "Patch " << patchNames[patchi]
288  << " has size " << patchSizes[patchi] << endl;
289  }
290 
291  // Create dual faces
292  // ~~~~~~~~~~~~~~~~~
293 
294  dualFaces.setSize(number_of_vertices());
295  label dualFacei = 0;
296  labelList faceVerts(maxNvert);
297 
298  for
299  (
300  Triangulation::Finite_vertices_iterator vit = finite_vertices_begin();
301  vit != finite_vertices_end();
302  ++vit
303  )
304  {
305  if (vit->internalOrBoundaryPoint())
306  {
307  Face_circulator fcStart = incident_faces(vit);
308  Face_circulator fc = fcStart;
309  label verti = 0;
310 
311  do
312  {
313  if (!is_infinite(fc))
314  {
315  if (fc->faceIndex() < 0)
316  {
318  << "Dual face uses vertex defined by a triangle"
319  " defined by an external point"
320  << exit(FatalError);
321  }
322 
323  // Look up the index of the triangle
324  faceVerts[verti++] = fc->faceIndex();
325  }
326  } while (++fc != fcStart);
327 
328  if (faceVerts.size() > 2)
329  {
330  dualFaces[dualFacei++] =
331  face(labelList::subList(faceVerts, verti));
332  }
333  else
334  {
335  Info<< "From triangle point:" << vit->index()
336  << " coord:" << toPoint2D(vit->point())
337  << " generated illegal dualFace:" << faceVerts
338  << endl;
339  }
340  }
341  }
342 
343  dualFaces.setSize(dualFacei);
344 }
345 
346 
347 void Foam::CV2D::writePatch(const fileName& fName) const
348 {
349  point2DField dual2DPoints;
350  faceList dualFaces;
352  labelList patchSizes;
353  EdgeMap<label> mapEdgesRegion;
354  EdgeMap<label> indirectPatchEdge;
355 
356  calcDual
357  (
358  dual2DPoints,
359  dualFaces,
360  patchNames,
361  patchSizes,
362  mapEdgesRegion,
363  indirectPatchEdge
364  );
365 
366  pointField dualPoints(dual2DPoints.size());
367  forAll(dualPoints, ip)
368  {
369  dualPoints[ip] = toPoint3D(dual2DPoints[ip]);
370  }
371 
372  // Dump as primitive patch to be read by extrudeMesh.
373  OFstream str(fName);
374 
375  Info<< "Writing patch to be used with extrudeMesh to file " << fName
376  << endl;
377 
378  str << dualPoints << nl << dualFaces << nl;
379 }
380 
381 
382 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
CGAL::pointPair
bool pointPair(const indexedVertex< Gt, Vb > &v0, const indexedVertex< Gt, Vb > &v1)
Definition: indexedVertexI.H:185
Foam::meshTools::writeOBJ
void writeOBJ(Ostream &os, const point &pt)
Write obj representation of a point.
Definition: meshTools.C:203
Foam::CV2D::writeTriangles
void writeTriangles(const fileName &fName, bool internalOnly) const
Write triangles as .obj file.
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::point2DField
vector2DField point2DField
point2DField is a vector2DField.
Definition: point2DFieldFwd.H:44
Foam::CV2D::extractPatches
void extractPatches(wordList &patchNames, labelList &patchSizes, EdgeMap< label > &mapEdgesRegion, EdgeMap< label > &indirectPatchEdge) const
Extract patch names and sizes.
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
OFstream.H
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:62
Foam::CV2D::writePoints
void writePoints(const fileName &fName, bool internalOnly) const
Write internal points to .obj file.
nPatches
const label nPatches
Definition: printMeshSummary.H:30
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::List::setSize
void setSize(const label n)
Alias for resize()
Definition: List.H:222
patchNames
wordList patchNames(nPatches)
Foam::List::subList
SubList< T > subList
Declare type of subList.
Definition: List.H:112
Foam::FatalError
error FatalError
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::faceList
List< face > faceList
A List of faces.
Definition: faceListFwd.H:47
Foam::Vector
Templated 3D Vector derived from VectorSpace adding construction from 3 components,...
Definition: Vector.H:62
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::CV2D::calcDual
void calcDual(point2DField &dualPoints, faceList &dualFaces, wordList &patchNames, labelList &patchSizes, EdgeMap< label > &mapEdgesRegion, EdgeMap< label > &indirectPatchEdge) const
Calculates dual points (circumcentres of tets) and faces.
Foam::CV2D::writePatch
void writePatch(const fileName &fName) const
Write patch.
Foam::CV2D::toPoint3D
Foam::point toPoint3D(const point2D &) const
Definition: CV2DI.H:142
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
CV2D.H
Foam::CV2D::writeFaces
void writeFaces(const fileName &fName, bool internalOnly) const
Write dual faces as .obj file.