edgeI.H
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 OpenFOAM Foundation
9  Copyright (C) 2017-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "IOstreams.H"
30 
31 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
32 
33 inline int Foam::edge::compare(const edge& a, const edge& b)
34 {
35  return labelPair::compare(a, b);
36 }
37 
38 
39 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
40 
42 :
43  labelPair(-1, -1)
44 {}
45 
46 
47 inline Foam::edge::edge(const label from, const label to)
48 :
49  labelPair(from, to)
50 {}
51 
52 
53 inline Foam::edge::edge(const labelPair& pair)
54 :
55  labelPair(pair.first(), pair.second())
56 {}
57 
58 
60 :
61  labelPair(list.first(), list.last())
62 {}
63 
64 
65 inline Foam::edge::edge(const label from, const label to, const bool doSort)
66 :
67  labelPair(from, to, doSort)
68 {}
69 
70 
71 inline Foam::edge::edge(const FixedList<label, 2>& list, const bool doSort)
72 :
73  labelPair(list, doSort)
74 {}
75 
76 
78 :
79  labelPair(is)
80 {}
81 
82 
83 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
84 
86 {
87  return first();
88 }
89 
91 {
92  return first();
93 }
94 
95 
97 {
98  return second();
99 }
100 
101 
103 {
104  return second();
105 }
106 
107 
109 {
110  return (first() < second() ? first() : second());
111 }
112 
113 
115 {
116  return (first() > second() ? first() : second());
117 }
118 
119 
120 inline bool Foam::edge::valid() const
121 {
122  return (first() != second() && first() >= 0 && second() >= 0);
123 }
124 
125 
126 inline bool Foam::edge::found(const label pointLabel) const
127 {
128  // -1: always false
129  return
130  (
131  pointLabel >= 0
132  && (pointLabel == first() || pointLabel == second())
133  );
134 }
135 
136 
137 inline Foam::label Foam::edge::which(const label pointLabel) const
138 {
139  // -1: always false
140  if (pointLabel >= 0)
141  {
142  if (pointLabel == first())
143  {
144  return 0;
145  }
146  if (pointLabel == second())
147  {
148  return 1;
149  }
150  }
151 
152  return -1;
153 }
154 
155 
156 inline bool Foam::edge::connects(const edge& other) const
157 {
158  return (other.found(first()) || other.found(second()));
159 }
160 
161 
162 inline Foam::label Foam::edge::commonVertex(const edge& other) const
163 {
164  if (other.found(first()))
165  {
166  return first();
167  }
168  if (other.found(second()))
169  {
170  return second();
171  }
172 
173  // No shared vertex.
174  return -1;
175 }
176 
177 
178 inline Foam::label Foam::edge::otherVertex(const label pointLabel) const
179 {
180  if (pointLabel == first())
181  {
182  return second();
183  }
184  if (pointLabel == second())
185  {
186  return first();
187  }
188 
189  // The given vertex is not on the edge in the first place.
190  return -1;
191 }
192 
193 
195 {
196  // Cannot resize FixedList, so mark duplicates with '-1'
197  // (the lower vertex is retained)
198  // catch any '-1' (eg, if called multiple times)
199 
200  label n = 2;
201  if (first() == second() || second() < 0)
202  {
203  second() = -1;
204  --n;
205  }
206  if (first() < 0)
207  {
208  --n;
209  }
210 
211  return n;
212 }
213 
214 
216 {
217  return edge(second(), first());
218 }
219 
220 
221 inline void Foam::edge::clear()
222 {
223  first() = -1;
224  second() = -1;
225 }
226 
227 
229 {
230  label n = 2;
231  if (first() == second() || second() < 0)
232  {
233  --n;
234  }
235  if (first() < 0)
236  {
237  --n;
238  }
239 
240  return n;
241 }
242 
243 
244 inline bool Foam::edge::empty() const
245 {
246  return (first() < 0 && second() < 0);
247 }
248 
249 
250 inline bool Foam::edge::insert(const label index)
251 {
252  if (index < 0)
253  {
254  // Cannot insert invalid point labels (use direct assignment for that)
255  return false;
256  }
257 
258  if (first() < 0)
259  {
260  // Store at first, if not duplicate of second
261  if (index != second())
262  {
263  first() = index;
264  return true;
265  }
266  }
267  else if (second() < 0)
268  {
269  // Store at second, if not duplicate of first
270  if (index != first())
271  {
272  second() = index;
273  return true;
274  }
275  }
276 
277  return false;
278 }
279 
280 
281 template<class InputIterator>
283 (
284  InputIterator begIter,
285  InputIterator endIter
286 )
287 {
288  // Available slots.
289  // Don't use count() since it has special treatment for duplicates
290  const int maxChange = ((first() < 0 ? 1 : 0) + (second() < 0 ? 1 : 0));
291 
292  int changed = 0;
293  for (; changed < maxChange && begIter != endIter; ++begIter)
294  {
295  if (insert(*begIter))
296  {
297  ++changed;
298  }
299  }
300 
301  return changed;
302 }
303 
304 
305 inline Foam::label Foam::edge::insert(std::initializer_list<label> list)
306 {
307  return insert(list.begin(), list.end());
308 }
309 
310 
311 template<unsigned N>
313 {
314  return insert(list.begin(), list.end());
315 }
316 
317 
319 {
320  return insert(list.begin(), list.end());
321 }
322 
323 
325 {
326  if (index < 0)
327  {
328  // Can never remove invalid point labels!
329  return 0;
330  }
331 
332  label n = 0;
333  if (index == first())
334  {
335  first() = -1;
336  ++n;
337  }
338 
339  // Automatically handle duplicates, which should not have been there anyhow
340  if (index == second())
341  {
342  second() = -1;
343  ++n;
344  }
345 
346  return n;
347 }
348 
349 
350 template<class InputIterator>
352 (
353  InputIterator begIter,
354  InputIterator endIter
355 )
356 {
357  // Occupied slots.
358  // Don't use count() since it has special treatment for duplicates
359  const int maxChange = ((first() >= 0 ? 1 : 0) + (second() >= 0 ? 1 : 0));
360 
361  int changed = 0;
362  for (; changed < maxChange && begIter != endIter; ++begIter)
363  {
364  changed += erase(*begIter);
365  }
366 
367  return changed;
368 }
369 
370 
371 inline Foam::label Foam::edge::erase(std::initializer_list<label> list)
372 {
373  return erase(list.begin(), list.end());
374 }
375 
376 
377 template<unsigned N>
379 {
380  return erase(list.begin(), list.end());
381 }
382 
383 
385 {
386  return erase(list.begin(), list.end());
387 }
388 
389 
390 // Geometric
391 
393 {
394  #ifdef FULLDEBUG
395  if (first() < 0 || second() < 0)
396  {
398  << "negative point index on edge " << *this
399  << abort(FatalError);
400  }
401  #endif
402 
403  return 0.5*(pts[first()] + pts[second()]);
404 }
405 
406 
407 inline Foam::vector Foam::edge::vec(const UList<point>& pts) const
408 {
409  #ifdef FULLDEBUG
410  if (first() < 0 || second() < 0)
411  {
413  << "negative point index on edge " << *this
414  << abort(FatalError);
415  }
416  #endif
417 
418  return pts[second()] - pts[first()];
419 }
420 
421 
423 {
424  #ifdef FULLDEBUG
425  if (first() < 0 || second() < 0)
426  {
428  << "negative point index on edge " << *this
429  << abort(FatalError);
430  }
431  #endif
432 
433  const vector v = (pts[second()] - pts[first()]);
434  const scalar s(Foam::mag(v));
435 
436  return s < ROOTVSMALL ? Zero : v/s;
437 }
438 
439 
440 inline Foam::scalar Foam::edge::mag(const UList<point>& pts) const
441 {
442  return ::Foam::mag(vec(pts));
443 }
444 
445 
447 {
448  #ifdef FULLDEBUG
449  if (first() < 0 || second() < 0)
450  {
452  << "negative point index on edge " << *this
453  << abort(FatalError);
454  }
455  #endif
456 
457  return linePointRef(pts[first()], pts[second()]);
458 }
459 
460 
461 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
462 
464 {
465  #ifdef FULLDEBUG
466  if (i < 0 || i > 1)
467  {
469  << "Index " << i << " out of range [0,1]" << abort(FatalError);
470  }
471  #endif
472  return (i ? second() : first());
473 }
474 
475 
476 inline const Foam::label& Foam::edge::operator[](const label i) const
477 {
478  #ifdef FULLDEBUG
479  if (i < 0 || i > 1)
480  {
482  << "Index " << i << " out of range [0,1]" << abort(FatalError);
483  }
484  #endif
485  return (i ? second() : first());
486 }
487 
488 
489 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
490 
491 inline bool Foam::operator==(const edge& a, const edge& b)
492 {
493  return edge::compare(a,b) != 0;
494 }
495 
496 
497 inline bool Foam::operator!=(const edge& a, const edge& b)
498 {
499  return edge::compare(a,b) == 0;
500 }
501 
502 
503 // ************************************************************************* //
insert
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
Foam::edge::minVertex
label minVertex() const
Return the smallest point label used by the edge.
Definition: edgeI.H:108
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::edge::commonVertex
label commonVertex(const edge &other) const
Return vertex common with other edge or -1 on failure.
Definition: edgeI.H:162
Foam::edge::centre
point centre(const UList< point > &pts) const
Return centre point (centroid) of the edge.
Definition: edgeI.H:392
Foam::edge::empty
bool empty() const
Return true if edge has no valid point labels.
Definition: edgeI.H:244
s
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputSpray.H:25
Foam::edge::reverseEdge
edge reverseEdge() const
Return reverse edge as copy.
Definition: edgeI.H:215
Foam::FixedList::begin
iterator begin()
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:473
Foam::edge::mag
scalar mag(const UList< point > &pts) const
Return scalar magnitude of the edge.
Definition: edgeI.H:440
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::edge::line
linePointRef line(const UList< point > &pts) const
Return edge line.
Definition: edgeI.H:446
Foam::edge
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:63
Foam::edge::edge
edge()
Construct null with invalid point labels (-1)
Definition: edgeI.H:41
Foam::edge::insert
bool insert(const label index)
Fill any open slot with the index if it did not previously exist.
Definition: edgeI.H:250
erase
srcOptions erase("case")
Foam::edge::unitVec
vector unitVec(const UList< point > &pts) const
Return the unit vector (end - start)
Definition: edgeI.H:422
Foam::UList::begin
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:276
Foam::edge::connects
bool connects(const edge &other) const
Do the edges share a common vertex index?
Definition: edgeI.H:156
Foam::operator!=
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:235
Foam::edge::clear
void clear()
'Clears' edge by setting both ends to invalid point labels.
Definition: edgeI.H:221
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::edge::start
label start() const
Return start (first) vertex label.
Definition: edgeI.H:85
Foam::Pair::compare
static int compare(const Pair< T > &a, const Pair< T > &b)
Compare Pairs.
Definition: PairI.H:33
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:62
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::operator==
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::edge::found
bool found(const label pointLabel) const
Return true if point label is found in edge.
Definition: edgeI.H:126
Foam::edge::erase
label erase(const label index)
Remove an existing index from the edge and set its location to '-1'.
Definition: edgeI.H:324
Foam::edge::end
label end() const
Return end (last/second) vertex label.
Definition: edgeI.H:96
Foam::edge::which
label which(const label pointLabel) const
Return local index (0,1) of point label in edge -1 on failure.
Definition: edgeI.H:137
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::FixedList::end
iterator end()
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:497
Foam::edge::otherVertex
label otherVertex(const label pointLabel) const
Given the point label for one vertex, return the other one.
Definition: edgeI.H:178
Foam::linePointRef
line< point, const point & > linePointRef
Line using referred points.
Definition: linePointRef.H:47
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::edge::compare
static int compare(const edge &a, const edge &b)
Compare edges.
Definition: edgeI.H:33
Foam::Pair
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: Pair.H:54
Foam::edge::vec
vector vec(const UList< point > &pts) const
Return the vector (end - start)
Definition: edgeI.H:407
Foam::Vector< scalar >
Foam::edge::maxVertex
label maxVertex() const
Return the largest point label used by the edge.
Definition: edgeI.H:114
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
Foam::UList< label >
Foam::line
A line primitive.
Definition: line.H:59
Foam::UList::end
iterator end()
Return an iterator to end traversing the UList.
Definition: UListI.H:297
Foam::edge::valid
bool valid() const
Return true if the vertices are unique and non-negative.
Definition: edgeI.H:120
Foam::edge::count
label count() const
Return the number of unique, valid (non -1) point labels.
Definition: edgeI.H:228
Foam::edge::collapse
label collapse()
Definition: edgeI.H:194
Foam::edge::operator[]
label & operator[](const label i)
Return edge element. Index should be limited to 0/1.
Definition: edgeI.H:463