edgeSurface.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) 2011-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 "edgeSurface.H"
29 #include "triSurface.H"
30 #include "surfaceIntersection.H"
31 #include "meshTools.H"
32 #include "OBJstream.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 
39  defineTypeNameAndDebug(edgeSurface, 0);
40 
41 } // End namespace Foam
42 
43 
44 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 // Write whole pointField and selected edges to stream
50 static void writeObjEdges
51 (
52  const UList<point>& points,
53  const edgeList& edges,
54  const labelUList& edgeLabels,
55  Ostream& os
56 )
57 {
58  for (const point& p : points)
59  {
60  os << "v " << p.x() << ' ' << p.y() << ' ' << p.z() << nl;
61  }
62 
63  for (const label edgei : edgeLabels)
64  {
65  const edge& e = edges[edgei];
66 
67  os << "l " << e.start()+1 << ' ' << e.end()+1 << nl;
68  }
69 }
70 
71 } // End namespace Foam
72 
73 
74 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
75 
76 // Pointedges in edgeSurface indices only.
77 void Foam::edgeSurface::calcPointEdges()
78 {
79  pointEdges_.setSize(points_.size());
80 
81  labelList pointNEdges(points_.size(), Zero);
82 
83  forAll(edges_, edgeI)
84  {
85  const edge& e = edges_[edgeI];
86 
87  pointNEdges[e[0]]++;
88  pointNEdges[e[1]]++;
89  }
90 
91  forAll(pointEdges_, pointi)
92  {
93  pointEdges_[pointi].setSize(pointNEdges[pointi]);
94  }
95 
96  pointNEdges = 0;
97 
98  forAll(edges_, edgeI)
99  {
100  const edge& e = edges_[edgeI];
101 
102  labelList& pEdges0 = pointEdges_[e[0]];
103  pEdges0[pointNEdges[e[0]]++] = edgeI;
104 
105  labelList& pEdges1 = pointEdges_[e[1]];
106  pEdges1[pointNEdges[e[1]]++] = edgeI;
107  }
108 }
109 
110 
111 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
112 
113 // Construct from surface and intersection description
115 (
116  const triSurface& surf,
117  const bool isFirstSurface,
118  const surfaceIntersection& inter
119 )
120 :
121  points_(surf.nPoints() + inter.cutPoints().size()),
122  nSurfacePoints_(surf.nPoints()),
123  edges_(),
124  nSurfaceEdges_(surf.nEdges()),
125  parentEdges_(0),
126  faceEdges_(surf.size()),
127  pointEdges_(points_.size())
128 {
129  // Copy points (surface ones first)
130  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131 
132  label pointi = 0;
133 
134  const pointField& surfPoints = surf.localPoints();
135 
136  forAll(surfPoints, i)
137  {
138  points_[pointi++] = surfPoints[i];
139  }
140 
141  const pointField& cutPoints = inter.cutPoints();
142 
143  forAll(cutPoints, i)
144  {
145  points_[pointi++] = cutPoints[i];
146  }
147 
148 
149  // Copy edges (surface ones first)
150  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
151 
152  DynamicList<edge> allEdges(surf.nEdges() + inter.cutEdges().size());
153  DynamicList<label> allParentEdges(surf.nEdges());
154  List<DynamicList<label>> allFaceEdges(surf.size());
155 
156 
157  // Copy surface edges (can be split!)
158 
159  const edgeList& surfEdges = surf.edges();
160 
161  forAll(surfEdges, edgeI)
162  {
163  const edge& e = surfEdges[edgeI];
164 
165  // Get additional vertices for this edge.
166  const labelList& extraVerts = inter.edgeCuts(isFirstSurface)[edgeI];
167 
168  // Store current top of allEdges.
169  label freeNewEdgeI = allEdges.size();
170 
171  if (extraVerts.empty())
172  {
173  // No cuts across this edge. Note that vertices do not need to be
174  // renumbered.
175  allEdges.append(e);
176  }
177  else
178  {
179  // Edge is cut. From e.start() to extraVerts[0],
180  // from extraVerts[i] to i+1 and finally to e.end().
181  allEdges.append
182  (
183  edge
184  (
185  e.start(),
186  extraVerts[0] + nSurfacePoints_
187  )
188  );
189 
190  for (label extraI = 1; extraI < extraVerts.size(); extraI++)
191  {
192  allEdges.append
193  (
194  edge
195  (
196  extraVerts[extraI-1] + nSurfacePoints_,
197  extraVerts[extraI] + nSurfacePoints_
198  )
199  );
200  }
201  allEdges.append
202  (
203  edge
204  (
205  extraVerts.last() + nSurfacePoints_,
206  e.end()
207  )
208  );
209  }
210 
211  // Update allFaceEdges, parentEdges_ for the newly added edges.
212 
213  // Add each edge label to all face neighbours of edgeI
214  const labelList& myFaces = surf.edgeFaces()[edgeI];
215 
216  for (label eI = freeNewEdgeI; eI < allEdges.size(); eI++)
217  {
218  allParentEdges.append(edgeI);
219 
220  forAll(myFaces, myFacei)
221  {
222  allFaceEdges[myFaces[myFacei]].append(eI);
223  }
224  }
225  }
226 
227  // Done all (possibly split) surface edges by now.
228  nSurfaceEdges_ = allEdges.size();
229 
230 
231  // Copy intersection edges
232  // (note no parentEdges)
233  const edgeList& cutEdges = inter.cutEdges();
234 
235  forAll(cutEdges, i)
236  {
237  const edge& e = cutEdges[i];
238 
239  allEdges.append(edge(e[0] + nSurfacePoints_, e[1] + nSurfacePoints_));
240  }
241 
242 
243  // Add intersection edges to faceEdges
244  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
245 
246  forAllConstIters(inter.facePairToEdgeId(), iter)
247  {
248  // The faceId from the correct surface
249  const label facei = iter.key()[isFirstSurface ? 0 : 1];
250 
251  // Edge label in intersection
252  const label edgeI = iter.val();
253 
254  // Store on face-edge addressing. (note: offset edge)
255  allFaceEdges[facei].append(edgeI + nSurfaceEdges_);
256  }
257 
258  // Transfer.
259  edges_.transfer(allEdges);
260  parentEdges_.transfer(allParentEdges);
261 
262  forAll(allFaceEdges, facei)
263  {
264  faceEdges_[facei].transfer(allFaceEdges[facei]);
265  }
266 
267 
268  // Additional addressing
269  // ~~~~~~~~~~~~~~~~~~~~~
270 
271  calcPointEdges();
272 
273 
274  if (debug & 4)
275  {
276  Pout<< "edgeSurface : Dumping faceEdges to files" << endl;
277 
278  forAll(faceEdges_, facei)
279  {
280  const labelList& fEdges = faceEdges_[facei];
281 
282  if (fEdges.size() != 3)
283  {
284  fileName faceFName("face_" + name(facei) + ".obj");
285  Pout<< "edgeSurface : Dumping faceEdges for face " << facei
286  << " to " << faceFName << endl;
287 
288  OFstream fStream(faceFName);
289  writeObjEdges(points_, edges_, fEdges, fStream);
290  }
291  }
292 
293  Pout<< "edgeSurface : Dumping edges to edges.obj" << endl;
294  OBJstream("edges.obj").write(edges_, points_);
295 
296  Pout<< "edgeSurface : Dumping intersectionEdges to"
297  << " intersectionEdges.obj" << endl;
298 
299  OFstream intEdgesStream("intersectionEdges.obj");
300  labelList edgeLabels(edges_.size() - nSurfaceEdges_);
301 
302  label i = 0;
303  for (label edgeI = nSurfaceEdges_; edgeI < edges_.size(); edgeI++)
304  {
305  edgeLabels[i++] = edgeI;
306  }
307 
308  writeObjEdges(points_, edges_, edgeLabels, intEdgesStream);
309  }
310 }
311 
312 
313 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
314 
316 (
317  const label facei,
318  const edgeList& additionalEdges
319 )
320 {
321  if (debug & 2)
322  {
323  Pout<< "Old face consisted of edges:" << endl;
324 
325  const labelList& fEdges = faceEdges_[facei];
326  forAll(fEdges, i)
327  {
328  const edge& e = edges_[fEdges[i]];
329 
330  Pout<< " " << fEdges[i] << ' ' << e
331  << points_[e.start()] << ' ' << points_[e.end()] << endl;
332  }
333  }
334 
335  // Make space for additional intersection edges (copies old ones)
336  const label oldNEdges = edges_.size();
337 
338  edges_.setSize(oldNEdges + additionalEdges.size());
339 
340  // Append new intersection edges
341  label newEdgeI = oldNEdges;
342 
343  forAll(additionalEdges, i)
344  {
345  edges_[newEdgeI] = additionalEdges[i]; // Vertices already in eSurf
346  // indices.
347  newEdgeI++;
348  }
349 
350  // Append to faceEdges.
351  labelList& fEdges = faceEdges_[facei];
352 
353  label nFEdges = fEdges.size();
354 
355  fEdges.setSize(nFEdges + additionalEdges.size());
356 
357  forAll(additionalEdges, i)
358  {
359  fEdges[nFEdges++] = oldNEdges + i;
360  }
361 
362 
363  // Update pointEdge addressing
364  calcPointEdges();
365 
366 
367  if (debug & 2)
368  {
369  const labelList& fEdges = faceEdges_[facei];
370 
371  Pout<< "New face consists of edges:" << endl;
372  forAll(fEdges, i)
373  {
374  const edge& e = edges_[fEdges[i]];
375 
376  Pout<< " " << fEdges[i] << ' ' << e
377  << points_[e.start()] << ' ' << points_[e.end()] << endl;
378  }
379  }
380 }
381 
382 
383 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
const Field< point_type > & localPoints() const
Return pointField of points in patch.
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:326
void append(const T &val)
Append an element at the end of the list.
Definition: List.H:517
List< edge > edgeList
List of edge.
Definition: edgeList.H:32
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Basic surface-surface intersection description. Constructed from two surfaces it creates a descriptio...
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
edgeSurface(const triSurface &surf, const bool isFirstSurface, const surfaceIntersection &inter)
Construct from surface and intersection description.
Definition: edgeSurface.C:108
const edgeList & cutEdges() const
The list of created edges.
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:38
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
void setSize(const label n)
Alias for resize()
Definition: List.H:316
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
const pointField & points
An edge is a list of two vertex labels. This can correspond to a directed graph edge or an edge on a ...
Definition: edge.H:59
label nPoints
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
const labelListList & edgeFaces() const
Return edge-face addressing.
const edgeList & edges() const
Return list of edges, address into LOCAL point list.
const labelListList & edgeCuts(const bool isFirstSurf) const
Access either surf1EdgeCuts (isFirstSurface = true) or.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
void addIntersectionEdges(const label facei, const edgeList &)
Add intersection edges to a face. Used for connecting.
Definition: edgeSurface.C:309
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
label nEdges() const
Number of edges in patch.
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
int debug
Static debugging option.
OBJstream os(runTime.globalPath()/outputName)
defineTypeNameAndDebug(combustionModel, 0)
const pointField & cutPoints() const
The list of cut points.
const labelPairLookup & facePairToEdgeId() const
Lookup of pairs of faces to created edges.
static void writeObjEdges(const UList< point > &points, const edgeList &edges, const labelUList &edgeLabels, Ostream &os)
Definition: edgeSurface.C:44
List< label > labelList
A List of labels.
Definition: List.H:62
volScalarField & p
Triangulated surface description with patch information.
Definition: triSurface.H:71
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Namespace for OpenFOAM.
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127