surfaceIntersectionFuncs.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) 2011-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 "surfaceIntersection.H"
27 #include "triSurfaceSearch.H"
28 #include "labelPairLookup.H"
29 #include "OFstream.H"
30 #include "HashSet.H"
31 #include "triSurface.H"
32 #include "pointIndexHit.H"
33 #include "meshTools.H"
34 
35 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36 
38 {
39  os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
40 }
41 
42 
44 (
45  const List<point>& pts,
46  const List<edge>& edges,
47  Ostream& os
48 )
49 {
50  forAll(pts, i)
51  {
52  writeOBJ(pts[i], os);
53  }
54  forAll(edges, i)
55  {
56  const edge& e = edges[i];
57 
58  os << "l " << e.start()+1 << ' ' << e.end()+1 << endl;
59  }
60 }
61 
62 
63 // Get minimum length of all edges connected to point
65 (
66  const triSurface& surf,
67  const label pointI
68 )
69 {
70  const labelList& pEdges = surf.pointEdges()[pointI];
71 
72  scalar minLen = GREAT;
73 
74  forAll(pEdges, pEdgeI)
75  {
76  const edge& e = surf.edges()[pEdges[pEdgeI]];
77 
78  minLen = min(minLen, e.mag(surf.localPoints()));
79  }
80 
81  return minLen;
82 }
83 
84 
85 // Get edge between fp and fp+1 on faceI.
87 (
88  const triSurface& surf,
89  const label faceI,
90  const label fp
91 )
92 {
93  const edge faceEdge = surf.localFaces()[faceI].faceEdge(fp);
94 
95  const labelList& eLabels = surf.faceEdges()[faceI];
96 
97  forAll(eLabels, eI)
98  {
99  const label edgeI = eLabels[eI];
100 
101  if (surf.edges()[edgeI] == faceEdge)
102  {
103  return edgeI;
104  }
105  }
106 
108  << "Problem:: Cannot find edge with vertices " << faceEdge
109  << " in face " << faceI
110  << abort(FatalError);
111 
112  return -1;
113 }
114 
115 
116 // Given a map remove all consecutive duplicate elements.
118 (
119  const labelList& map,
120  labelList& elems
121 )
122 {
123  bool hasDuplicate = false;
124 
125  label prevVertI = -1;
126 
127  forAll(elems, elemI)
128  {
129  label newVertI = map[elems[elemI]];
130 
131  if (newVertI == prevVertI)
132  {
133  hasDuplicate = true;
134 
135  break;
136  }
137  prevVertI = newVertI;
138  }
139 
140  if (hasDuplicate)
141  {
142  // Create copy
143  labelList oldElems(elems);
144 
145  label elemI = 0;
146 
147  // Insert first
148  elems[elemI++] = map[oldElems[0]];
149 
150  for (label vertI = 1; vertI < oldElems.size(); vertI++)
151  {
152  // Insert others only if they differ from one before
153  label newVertI = map[oldElems[vertI]];
154 
155  if (newVertI != elems.last())
156  {
157  elems[elemI++] = newVertI;
158  }
159  }
160  elems.setSize(elemI);
161  }
162 }
163 
164 
165 // Remap.
167 (
168  const labelList& map,
169  labelList& elems
170 )
171 {
172  forAll(elems, elemI)
173  {
174  elems[elemI] = map[elems[elemI]];
175  }
176 }
177 
178 
179 // Remove all duplicate and degenerate elements. Return unique elements and
180 // map from old to new.
182 (
183  const edgeList& edges,
184  labelList& map
185 )
186 {
187  HashSet<edge, Hash<edge> > uniqueEdges(10*edges.size());
188 
189  edgeList newEdges(edges.size());
190 
191  map.setSize(edges.size());
192  map = -1;
193 
194  label newEdgeI = 0;
195 
196  forAll(edges, edgeI)
197  {
198  const edge& e = edges[edgeI];
199 
200  if
201  (
202  (e.start() != e.end())
203  && (uniqueEdges.find(e) == uniqueEdges.end())
204  )
205  {
206  // Edge is -non degenerate and -not yet seen.
207  uniqueEdges.insert(e);
208 
209  map[edgeI] = newEdgeI;
210 
211  newEdges[newEdgeI++] = e;
212  }
213  }
214 
215  newEdges.setSize(newEdgeI);
216 
217  return newEdges;
218 }
219 
220 
221 // Remove all duplicate elements.
223 (
224  const labelList& elems,
225  labelList& map
226 )
227 {
228  labelHashSet uniqueElems(10*elems.size());
229 
230  labelList newElems(elems.size());
231 
232  map.setSize(elems.size());
233  map = -1;
234 
235  label newElemI = 0;
236 
237  forAll(elems, elemI)
238  {
239  label elem = elems[elemI];
240 
241  if (uniqueElems.find(elem) == uniqueElems.end())
242  {
243  // First time elem is seen
244  uniqueElems.insert(elem);
245 
246  map[elemI] = newElemI;
247 
248  newElems[newElemI++] = elem;
249  }
250  }
251 
252  newElems.setSize(newElemI);
253 
254  return newElems;
255 }
256 
257 
259 (
260  const triSurface& surf,
261  const labelListList& edgeCutVerts,
262  Ostream& os
263 ) const
264 {
265  // Dump all points (surface followed by cutPoints)
266  const pointField& pts = surf.localPoints();
267 
268  forAll(pts, pointI)
269  {
270  writeOBJ(pts[pointI], os);
271  }
272  forAll(cutPoints(), cutPointI)
273  {
274  writeOBJ(cutPoints()[cutPointI], os);
275  }
276 
277  forAll(edgeCutVerts, edgeI)
278  {
279  const labelList& extraVerts = edgeCutVerts[edgeI];
280 
281  if (extraVerts.size())
282  {
283  const edge& e = surf.edges()[edgeI];
284 
285  // Start of original edge to first extra point
286  os << "l " << e.start()+1 << ' '
287  << extraVerts[0] + surf.nPoints() + 1 << endl;
288 
289  for (label i = 1; i < extraVerts.size(); i++)
290  {
291  os << "l " << extraVerts[i-1] + surf.nPoints() + 1 << ' '
292  << extraVerts[i] + surf.nPoints() + 1 << endl;
293  }
294 
295  os << "l " << extraVerts.last() + surf.nPoints() + 1
296  << ' ' << e.end()+1 << endl;
297  }
298  }
299 }
300 
301 
302 // Return 0 (p close to start), 1(close to end) or -1.
304 (
305  const scalar startTol,
306  const scalar endTol,
307  const point& p,
308  const edge& e,
309  const pointField& points
310 )
311 {
312  if (mag(p - points[e.start()]) < startTol)
313  {
314  return 0;
315  }
316  else if (mag(p - points[e.end()]) < endTol)
317  {
318  return 1;
319  }
320  else
321  {
322  return -1;
323  }
324 }
325 
326 
327 // ************************************************************************* //
meshTools.H
labelPairLookup.H
pointIndexHit.H
p
p
Definition: pEqn.H:62
Foam::PrimitivePatch::edges
const edgeList & edges() const
Return list of edges, address into LOCAL point list.
Definition: PrimitivePatchTemplate.C:212
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
writeOBJ
void writeOBJ(Ostream &os, label &vertI, const tetPoints &tet)
Definition: Test-tetTetOverlap.C:38
Foam::surfaceIntersection::filterLabels
static labelList filterLabels(const labelList &elems, labelList &map)
Remove all duplicate elements.
Definition: surfaceIntersectionFuncs.C:223
Foam::PrimitivePatch::localPoints
const Field< PointType > & localPoints() const
Return pointField of points in patch.
Definition: PrimitivePatchTemplate.C:432
Foam::surfaceIntersection::classify
static label classify(const scalar startTol, const scalar endTol, const point &p, const edge &e, const pointField &points)
Detect if point close to edge of end. Returns -1: not close.
Definition: surfaceIntersectionFuncs.C:304
Foam::surfaceIntersection::removeDuplicates
static void removeDuplicates(const labelList &map, labelList &labels)
Remove duplicates from ordered dynamic list. Returns map from old.
Definition: surfaceIntersectionFuncs.C:118
Foam::edge
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
Foam::surfaceIntersection::filterEdges
static edgeList filterEdges(const edgeList &, labelList &map)
Definition: surfaceIntersectionFuncs.C:182
Foam::surfaceIntersection::writeOBJ
static void writeOBJ(const point &pt, Ostream &os)
Write point in obj format.
Definition: surfaceIntersectionFuncs.C:37
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::surfaceIntersection::writeIntersectedEdges
void writeIntersectedEdges(const triSurface &surf, const labelListList &edgeCutVerts, Ostream &os) const
Debugging: Dump intersected edges to stream.
Definition: surfaceIntersectionFuncs.C:259
Foam::PrimitivePatch::faceEdges
const labelListList & faceEdges() const
Return face-edge addressing.
Definition: PrimitivePatchTemplate.C:312
Foam::mag
dimensioned< scalar > mag(const dimensioned< Type > &)
Foam::HashSet
A HashTable with keys but without contents.
Definition: HashSet.H:59
OFstream.H
surfaceIntersection.H
Foam::surfaceIntersection::getEdge
static label getEdge(const triSurface &surf, const label faceI, const label fp)
Get edge label of edge between face vertices fp and fp+1.
Definition: surfaceIntersectionFuncs.C:87
Foam::label
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
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::triSurface
Triangulated surface description with patch information.
Definition: triSurface.H:57
Foam::PrimitivePatch::pointEdges
const labelListList & pointEdges() const
Return point-edge addressing.
Definition: PrimitivePatchTemplate.C:332
Foam::surfaceIntersection::inlineRemap
static void inlineRemap(const labelList &map, labelList &elems)
Apply map to elements of a labelList.
Definition: surfaceIntersectionFuncs.C:167
HashSet.H
Foam::PrimitivePatch::nPoints
label nPoints() const
Return number of points supporting patch faces.
Definition: PrimitivePatchTemplate.H:293
Foam::Vector::x
const Cmpt & x() const
Definition: VectorI.H:65
Foam::FatalError
error FatalError
Foam::Vector::z
const Cmpt & z() const
Definition: VectorI.H:77
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::e
const double e
Elementary charge.
Definition: doubleFloat.H:94
Foam::HashTable< nil, word, string::hash >::find
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Foam::List::setSize
void setSize(const label)
Reset size of List.
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::Vector< scalar >
Foam::List
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
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:116
triSurfaceSearch.H
Foam::PrimitivePatch::localFaces
const List< Face > & localFaces() const
Return patch faces addressing into local point list.
Definition: PrimitivePatchTemplate.C:372
Foam::Vector::y
const Cmpt & y() const
Definition: VectorI.H:71
Foam::surfaceIntersection::minEdgeLen
static scalar minEdgeLen(const triSurface &surf, const label pointI)
Get minimum length of all edges connected to point.
Definition: surfaceIntersectionFuncs.C:65
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::min
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)