edgeSurface.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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 "edgeSurface.H"
27 #include "triSurface.H"
28 #include "surfaceIntersection.H"
29 #include "meshTools.H"
30 #include "OFstream.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 defineTypeNameAndDebug(edgeSurface, 0);
37 }
38 
39 
40 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
41 
42 // Write whole pointField and edges to stream
43 void Foam::edgeSurface::writeOBJ
44 (
45  const pointField& points,
46  const edgeList& edges,
47  Ostream& os
48 )
49 {
50  forAll(points, pointi)
51  {
52  const point& pt = points[pointi];
53 
54  os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
55  }
56  forAll(edges, edgeI)
57  {
58  const edge& e = edges[edgeI];
59 
60  os << "l " << e.start()+1 << ' ' << e.end()+1 << endl;
61  }
62 }
63 
64 
65 // Write whole pointField and selected edges to stream
66 void Foam::edgeSurface::writeOBJ
67 (
68  const pointField& points,
69  const edgeList& edges,
70  const labelList& edgeLabels,
71  Ostream& os
72 )
73 {
74  forAll(points, pointi)
75  {
76  const point& pt = points[pointi];
77 
78  os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
79  }
80  forAll(edgeLabels, i)
81  {
82  const edge& e = edges[edgeLabels[i]];
83 
84  os << "l " << e.start()+1 << ' ' << e.end()+1 << endl;
85  }
86 }
87 
88 
89 // Pointedges in edgeSurface indices only.
90 void Foam::edgeSurface::calcPointEdges()
91 {
92  pointEdges_.setSize(points_.size());
93 
94  labelList pointNEdges(points_.size(), 0);
95 
96  forAll(edges_, edgeI)
97  {
98  const edge& e = edges_[edgeI];
99 
100  pointNEdges[e[0]]++;
101  pointNEdges[e[1]]++;
102  }
103 
104  forAll(pointEdges_, pointi)
105  {
106  pointEdges_[pointi].setSize(pointNEdges[pointi]);
107  }
108 
109  pointNEdges = 0;
110 
111  forAll(edges_, edgeI)
112  {
113  const edge& e = edges_[edgeI];
114 
115  labelList& pEdges0 = pointEdges_[e[0]];
116  pEdges0[pointNEdges[e[0]]++] = edgeI;
117 
118  labelList& pEdges1 = pointEdges_[e[1]];
119  pEdges1[pointNEdges[e[1]]++] = edgeI;
120  }
121 }
122 
123 
124 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
125 
126 // Construct from surface and intersection description
128 (
129  const triSurface& surf,
130  const bool isFirstSurface,
131  const surfaceIntersection& inter
132 )
133 :
134  points_(surf.nPoints() + inter.cutPoints().size()),
135  nSurfacePoints_(surf.nPoints()),
136  edges_(),
137  nSurfaceEdges_(surf.nEdges()),
138  parentEdges_(0),
139  faceEdges_(surf.size()),
140  pointEdges_(points_.size())
141 {
142  // Copy points (surface ones first)
143  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144 
145  label pointi = 0;
146 
147  const pointField& surfPoints = surf.localPoints();
148 
149  forAll(surfPoints, i)
150  {
151  points_[pointi++] = surfPoints[i];
152  }
153 
154  const pointField& cutPoints = inter.cutPoints();
155 
156  forAll(cutPoints, i)
157  {
158  points_[pointi++] = cutPoints[i];
159  }
160 
161 
162  // Copy edges (surface ones first)
163  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
164 
165  DynamicList<edge> allEdges(surf.nEdges() + inter.cutEdges().size());
166  DynamicList<label> allParentEdges(surf.nEdges());
167  List<DynamicList<label>> allFaceEdges(surf.size());
168 
169 
170  // Copy surface edges (can be split!)
171 
172  const edgeList& surfEdges = surf.edges();
173 
174  forAll(surfEdges, edgeI)
175  {
176  const edge& e = surfEdges[edgeI];
177 
178  // Get additional vertices for this edge.
179  const labelList& extraVerts = inter.edgeCuts(isFirstSurface)[edgeI];
180 
181  // Store current top of allEdges.
182  label freeNewEdgeI = allEdges.size();
183 
184  if (extraVerts.empty())
185  {
186  // No cuts across this edge. Note that vertices do not need to be
187  // renumbered.
188  allEdges.append(e);
189  }
190  else
191  {
192  // Edge is cut. From e.start() to extraVerts[0],
193  // from extraVerts[i] to i+1 and finally to e.end().
194  allEdges.append
195  (
196  edge
197  (
198  e.start(),
199  extraVerts[0] + nSurfacePoints_
200  )
201  );
202 
203  for (label extraI = 1; extraI < extraVerts.size(); extraI++)
204  {
205  allEdges.append
206  (
207  edge
208  (
209  extraVerts[extraI-1] + nSurfacePoints_,
210  extraVerts[extraI] + nSurfacePoints_
211  )
212  );
213  }
214  allEdges.append
215  (
216  edge
217  (
218  extraVerts.last() + nSurfacePoints_,
219  e.end()
220  )
221  );
222  }
223 
224  // Update allFaceEdges, parentEdges_ for the newly added edges.
225 
226  // Add each edge label to all face neighbours of edgeI
227  const labelList& myFaces = surf.edgeFaces()[edgeI];
228 
229  for (label eI = freeNewEdgeI; eI < allEdges.size(); eI++)
230  {
231  allParentEdges.append(edgeI);
232 
233  forAll(myFaces, myFacei)
234  {
235  allFaceEdges[myFaces[myFacei]].append(eI);
236  }
237  }
238  }
239 
240  // Done all (possibly split) surface edges by now.
241  nSurfaceEdges_ = allEdges.size();
242 
243 
244  // Copy intersection edges
245  // (note no parentEdges)
246  const edgeList& cutEdges = inter.cutEdges();
247 
248  forAll(cutEdges, i)
249  {
250  const edge& e = cutEdges[i];
251 
252  allEdges.append(edge(e[0] + nSurfacePoints_, e[1] + nSurfacePoints_));
253  }
254 
255 
256 
257 
258  // Add intersection edges to faceEdges
259  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
260 
262  {
263  // Edge label in intersection
264  const label edgeI = iter();
265 
266  // Get the face from the correct surface
267  const FixedList<label, 2>& twoFaces = iter.key();
268 
269  label facei;
270 
271  if (isFirstSurface)
272  {
273  facei = twoFaces[0];
274  }
275  else
276  {
277  facei = twoFaces[1];
278  }
279 
280  // Store on face-edge addressing. (note: offset edge)
281  allFaceEdges[facei].append(edgeI + nSurfaceEdges_);
282  }
283 
284  // Transfer.
285  edges_.transfer(allEdges);
286  parentEdges_.transfer(allParentEdges);
287 
288  forAll(allFaceEdges, facei)
289  {
290  faceEdges_[facei].transfer(allFaceEdges[facei]);
291  }
292 
293 
294  // Additional addressing
295  // ~~~~~~~~~~~~~~~~~~~~~
296 
297  calcPointEdges();
298 
299 
300  if (debug & 4)
301  {
302  Pout<< "edgeSurface : Dumping faceEdges to files" << endl;
303 
304  forAll(faceEdges_, facei)
305  {
306  const labelList& fEdges = faceEdges_[facei];
307 
308  if (fEdges.size() != 3)
309  {
310  fileName faceFName("face_" + name(facei) + ".obj");
311  Pout<< "edgeSurface : Dumping faceEdges for face " << facei
312  << " to " << faceFName << endl;
313 
314  OFstream fStream(faceFName);
315  writeOBJ(points_, edges_, fEdges, fStream);
316  }
317  }
318 
319  Pout<< "edgeSurface : Dumping edges to edges.obj" << endl;
320  OFstream eStream("edges.obj");
321  writeOBJ(points_, edges_, eStream);
322 
323  Pout<< "edgeSurface : Dumping intersectionEdges to"
324  << " intersectionEdges.obj" << endl;
325  OFstream intEdgesStream("intersectionEdges.obj");
326 
327  labelList edgeLabels(edges_.size() - nSurfaceEdges_);
328 
329  label i = 0;
330  for (label edgeI = nSurfaceEdges_; edgeI < edges_.size(); edgeI++)
331  {
332  edgeLabels[i++] = edgeI;
333  }
334 
335  writeOBJ(points_, edges_, edgeLabels, intEdgesStream);
336  }
337 }
338 
339 
340 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
341 
343 (
344  const label facei,
345  const edgeList& additionalEdges
346 )
347 {
348  if (debug & 2)
349  {
350  Pout<< "Old face consisted of edges:" << endl;
351 
352  const labelList& fEdges = faceEdges_[facei];
353  forAll(fEdges, i)
354  {
355  const edge& e = edges_[fEdges[i]];
356 
357  Pout<< " " << fEdges[i] << ' ' << e
358  << points_[e.start()] << ' ' << points_[e.end()] << endl;
359  }
360  }
361 
362  // Make space for additional intersection edges (copies old ones)
363  const label oldNEdges = edges_.size();
364 
365  edges_.setSize(oldNEdges + additionalEdges.size());
366 
367  // Append new intersection edges
368  label newEdgeI = oldNEdges;
369 
370  forAll(additionalEdges, i)
371  {
372  edges_[newEdgeI] = additionalEdges[i]; // Vertices already in eSurf
373  // indices.
374  newEdgeI++;
375  }
376 
377  // Append to faceEdges.
378  labelList& fEdges = faceEdges_[facei];
379 
380  label nFEdges = fEdges.size();
381 
382  fEdges.setSize(nFEdges + additionalEdges.size());
383 
384  forAll(additionalEdges, i)
385  {
386  fEdges[nFEdges++] = oldNEdges + i;
387  }
388 
389 
390  // Update pointEdge addressing
391  calcPointEdges();
392 
393 
394  if (debug & 2)
395  {
396  const labelList& fEdges = faceEdges_[facei];
397 
398  Pout<< "New face consists of edges:" << endl;
399  forAll(fEdges, i)
400  {
401  const edge& e = edges_[fEdges[i]];
402 
403  Pout<< " " << fEdges[i] << ' ' << e
404  << points_[e.start()] << ' ' << points_[e.end()] << endl;
405  }
406  }
407 }
408 
409 
410 // ************************************************************************* //
label nPoints() const
Return number of points supporting patch faces.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
A class for handling file names.
Definition: fileName.H:79
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Output to file stream.
Definition: OFstream.H:82
Basic surface-surface intersection description. Constructed from two surfaces it creates a descriptio...
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
void writeOBJ(Ostream &os, const point &pt)
Write obj representation of point.
Definition: meshTools.C:203
const Field< PointType > & localPoints() const
Return pointField of points in patch.
const labelListList & edgeCuts(const bool) const
Access either surf1EdgeCuts (isFirstSurface = true) or.
edgeSurface(const triSurface &surf, const bool isFirstSurface, const surfaceIntersection &inter)
Construct from surface and intersection description.
Definition: edgeSurface.C:128
const edgeList & cutEdges() const
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
List< edge > edgeList
Definition: edgeList.H:38
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
void transfer(const FixedList< T, Size > &)
Copy (not transfer) the argument contents.
Definition: FixedListI.H:191
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:178
const labelPairLookup & facePairToEdge() const
const labelListList & edgeFaces() const
Return edge-face addressing.
const edgeList & edges() const
Return list of edges, address into LOCAL point list.
List< label > labelList
A List of labels.
Definition: labelList.H:56
const Cmpt & x() const
Definition: VectorI.H:75
void addIntersectionEdges(const label facei, const edgeList &)
Add intersection edges to a face. Used for connecting.
Definition: edgeSurface.C:343
label nEdges() const
Return number of edges in patch.
defineTypeNameAndDebug(combustionModel, 0)
const pointField & cutPoints() const
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void setSize(const label)
Reset size of List.
Definition: List.C:281
vector point
Point is a vector.
Definition: point.H:41
label end() const
Return end vertex label.
Definition: edgeI.H:92
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
T & last()
Return the last element of the list.
Definition: UListI.H:128
Triangulated surface description with patch information.
Definition: triSurface.H:66
label start() const
Return start vertex label.
Definition: edgeI.H:81
Namespace for OpenFOAM.