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-2020 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 
77 inline Foam::edge::edge
78 (
79  const labelUList& list,
80  const FixedList<label, 2>& indices
81 )
82 :
83  labelPair(list[indices.first()], list[indices.last()])
84 {}
85 
86 
88 :
89  labelPair(is)
90 {}
91 
92 
93 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
94 
95 inline Foam::label Foam::edge::start() const
96 {
97  return first();
98 }
99 
100 inline Foam::label& Foam::edge::start()
101 {
102  return first();
103 }
104 
105 
106 inline Foam::label Foam::edge::end() const
107 {
108  return second();
109 }
110 
111 
112 inline Foam::label& Foam::edge::end()
113 {
114  return second();
115 }
116 
117 
118 inline Foam::label Foam::edge::minVertex() const
119 {
120  return (first() < second() ? first() : second());
121 }
122 
123 
124 inline Foam::label Foam::edge::maxVertex() const
125 {
126  return (first() > second() ? first() : second());
127 }
128 
129 
130 inline bool Foam::edge::valid() const
131 {
132  return (first() != second() && first() >= 0 && second() >= 0);
133 }
134 
135 
136 inline bool Foam::edge::found(const label pointLabel) const
137 {
138  // -1: always false
139  return
140  (
141  pointLabel >= 0
142  && (pointLabel == first() || pointLabel == second())
143  );
144 }
145 
146 
147 inline Foam::label Foam::edge::which(const label pointLabel) const
148 {
149  // -1: always false
150  if (pointLabel >= 0)
151  {
152  if (pointLabel == first())
153  {
154  return 0;
155  }
156  if (pointLabel == second())
157  {
158  return 1;
159  }
160  }
161 
162  return -1;
163 }
164 
165 
166 inline bool Foam::edge::connects(const edge& other) const
167 {
168  return (other.found(first()) || other.found(second()));
169 }
170 
171 
172 inline Foam::label Foam::edge::commonVertex(const edge& other) const
173 {
174  if (other.found(first()))
175  {
176  return first();
177  }
178  if (other.found(second()))
179  {
180  return second();
181  }
182 
183  // No shared vertex.
184  return -1;
185 }
186 
187 
188 inline Foam::label Foam::edge::otherVertex(const label pointLabel) const
189 {
190  if (pointLabel == first())
191  {
192  return second();
193  }
194  if (pointLabel == second())
195  {
196  return first();
197  }
198 
199  // The given vertex is not on the edge in the first place.
200  return -1;
201 }
202 
203 
204 inline Foam::label Foam::edge::collapse()
205 {
206  // Cannot resize FixedList, so mark duplicates with '-1'
207  // (the lower vertex is retained)
208  // catch any '-1' (eg, if called multiple times)
209 
210  label n = 2;
211  if (first() == second() || second() < 0)
212  {
213  second() = -1;
214  --n;
215  }
216  if (first() < 0)
217  {
218  --n;
219  }
220 
221  return n;
222 }
223 
224 
226 {
227  return Foam::edge(second(), first());
228 }
229 
230 
231 inline void Foam::edge::clear()
232 {
233  first() = -1;
234  second() = -1;
235 }
236 
237 
238 inline Foam::label Foam::edge::count() const
239 {
240  label n = 2;
241  if (first() == second() || second() < 0)
242  {
243  --n;
244  }
245  if (first() < 0)
246  {
247  --n;
248  }
249 
250  return n;
251 }
252 
253 
254 inline bool Foam::edge::empty() const
255 {
256  return (first() < 0 && second() < 0);
257 }
258 
259 
260 inline bool Foam::edge::insert(const label index)
261 {
262  if (index < 0)
263  {
264  // Cannot insert invalid point labels (use direct assignment for that)
265  return false;
266  }
267 
268  if (first() < 0)
269  {
270  // Store at first, if not duplicate of second
271  if (index != second())
272  {
273  first() = index;
274  return true;
275  }
276  }
277  else if (second() < 0)
278  {
279  // Store at second, if not duplicate of first
280  if (index != first())
281  {
282  second() = index;
283  return true;
284  }
285  }
286 
287  return false;
288 }
289 
290 
291 template<class InputIterator>
292 inline Foam::label Foam::edge::insert
293 (
294  InputIterator begIter,
295  InputIterator endIter
296 )
297 {
298  // Available slots.
299  // Don't use count() since it has special treatment for duplicates
300  const int maxChange = ((first() < 0 ? 1 : 0) + (second() < 0 ? 1 : 0));
301 
302  int changed = 0;
303  for (; changed < maxChange && begIter != endIter; ++begIter)
304  {
305  if (insert(*begIter))
306  {
307  ++changed;
308  }
309  }
310 
311  return changed;
312 }
313 
314 
315 inline Foam::label Foam::edge::insert(std::initializer_list<label> list)
316 {
317  return insert(list.begin(), list.end());
318 }
319 
320 
321 template<unsigned N>
322 inline Foam::label Foam::edge::insert(const FixedList<label, N>& list)
323 {
324  return insert(list.begin(), list.end());
325 }
326 
327 
328 inline Foam::label Foam::edge::insert(const labelUList& list)
329 {
330  return insert(list.begin(), list.end());
331 }
332 
333 
334 inline Foam::label Foam::edge::erase(const label index)
335 {
336  if (index < 0)
337  {
338  // Can never remove invalid point labels!
339  return 0;
340  }
341 
342  label n = 0;
343  if (index == first())
344  {
345  first() = -1;
346  ++n;
347  }
348 
349  // Automatically handle duplicates, which should not have been there anyhow
350  if (index == second())
351  {
352  second() = -1;
353  ++n;
354  }
355 
356  return n;
357 }
358 
359 
360 template<class InputIterator>
361 inline Foam::label Foam::edge::erase
362 (
363  InputIterator begIter,
364  InputIterator endIter
365 )
366 {
367  // Occupied slots.
368  // Don't use count() since it has special treatment for duplicates
369  const int maxChange = ((first() >= 0 ? 1 : 0) + (second() >= 0 ? 1 : 0));
370 
371  int changed = 0;
372  for (; changed < maxChange && begIter != endIter; ++begIter)
373  {
374  changed += erase(*begIter);
375  }
376 
377  return changed;
378 }
379 
380 
381 inline Foam::label Foam::edge::erase(std::initializer_list<label> list)
382 {
383  return erase(list.begin(), list.end());
384 }
385 
386 
387 template<unsigned N>
388 inline Foam::label Foam::edge::erase(const FixedList<label, N>& list)
389 {
390  return erase(list.begin(), list.end());
391 }
392 
393 
394 inline Foam::label Foam::edge::erase(const labelUList& list)
395 {
396  return erase(list.begin(), list.end());
397 }
398 
399 
400 // Geometric
401 
403 {
404  #ifdef FULLDEBUG
405  if (first() < 0 || second() < 0)
406  {
408  << "negative point index on edge " << *this
409  << abort(FatalError);
410  }
411  #endif
412 
413  return 0.5*(pts[first()] + pts[second()]);
414 }
415 
416 
417 inline Foam::vector Foam::edge::vec(const UList<point>& pts) const
418 {
419  #ifdef FULLDEBUG
420  if (first() < 0 || second() < 0)
421  {
423  << "negative point index on edge " << *this
424  << abort(FatalError);
425  }
426  #endif
427 
428  return pts[second()] - pts[first()];
429 }
430 
431 
433 {
434  #ifdef FULLDEBUG
435  if (first() < 0 || second() < 0)
436  {
438  << "negative point index on edge " << *this
439  << abort(FatalError);
440  }
441  #endif
442 
443  const vector v = (pts[second()] - pts[first()]);
444  const scalar s(Foam::mag(v));
445 
446  return s < ROOTVSMALL ? Zero : v/s;
447 }
448 
449 
450 inline Foam::scalar Foam::edge::mag(const UList<point>& pts) const
451 {
452  return ::Foam::mag(vec(pts));
453 }
454 
455 
457 {
458  #ifdef FULLDEBUG
459  if (first() < 0 || second() < 0)
460  {
462  << "negative point index on edge " << *this
463  << abort(FatalError);
464  }
465  #endif
466 
467  return linePointRef(pts[first()], pts[second()]);
468 }
469 
470 
471 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
472 
473 inline Foam::label& Foam::edge::operator[](const label i)
474 {
475  #ifdef FULLDEBUG
476  if (i < 0 || i > 1)
477  {
479  << "Index " << i << " out of range [0,1]" << abort(FatalError);
480  }
481  #endif
482  return (i ? second() : first());
483 }
484 
485 
486 inline const Foam::label& Foam::edge::operator[](const label i) const
487 {
488  #ifdef FULLDEBUG
489  if (i < 0 || i > 1)
490  {
492  << "Index " << i << " out of range [0,1]" << abort(FatalError);
493  }
494  #endif
495  return (i ? second() : first());
496 }
497 
498 
499 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
500 
501 inline bool Foam::operator==(const edge& a, const edge& b)
502 {
503  return edge::compare(a,b) != 0;
504 }
505 
506 
507 inline bool Foam::operator!=(const edge& a, const edge& b)
508 {
509  return edge::compare(a,b) == 0;
510 }
511 
512 
513 // ************************************************************************* //
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:118
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:172
Foam::FixedList::end
iterator end() noexcept
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:548
Foam::edge::centre
point centre(const UList< point > &pts) const
Return centre point (centroid) of the edge.
Definition: edgeI.H:402
Foam::edge::empty
bool empty() const
Return true if edge has no valid point labels.
Definition: edgeI.H:254
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:225
Foam::UList::end
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:350
Foam::edge::mag
scalar mag(const UList< point > &pts) const
Return scalar magnitude of the edge.
Definition: edgeI.H:450
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::edge::line
linePointRef line(const UList< point > &pts) const
Return edge line.
Definition: edgeI.H:456
Foam::FixedList::last
T & last() noexcept
The last element of the list, position [N-1].
Definition: FixedListI.H:221
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()
Default construct, 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:260
erase
srcOptions erase("case")
Foam::edge::unitVec
vector unitVec(const UList< point > &pts) const
Return the unit vector (end - start)
Definition: edgeI.H:432
Foam::labelPair
Pair< label > labelPair
A pair of labels.
Definition: Pair.H:54
Foam::FixedList::first
T & first() noexcept
The first element of the list, position [0].
Definition: FixedListI.H:207
Foam::edge::connects
bool connects(const edge &other) const
Do the edges share a common vertex index?
Definition: edgeI.H:166
Foam::FixedList::begin
iterator begin() noexcept
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:524
Foam::operator!=
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:239
Foam::edge::clear
void clear()
'Clears' edge by setting both ends to invalid point labels.
Definition: edgeI.H:231
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::edge::start
label start() const
Return start (first) vertex label.
Definition: edgeI.H:95
Foam::UList::begin
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
Foam::Pair< label >::compare
static int compare(const Pair< label > &a, const Pair< label > &b)
Compare Pairs.
Definition: PairI.H:33
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:136
Foam::edge::erase
label erase(const label index)
Remove an existing index from the edge and set its location to '-1'.
Definition: edgeI.H:334
Foam::edge::end
label end() const
Return end (last/second) vertex label.
Definition: edgeI.H:106
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:147
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::edge::otherVertex
label otherVertex(const label pointLabel) const
Given the point label for one vertex, return the other one.
Definition: edgeI.H:188
Foam::linePointRef
line< point, const point & > linePointRef
A line using referred points.
Definition: linePointRef.H:47
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::edge::compare
static int compare(const edge &a, const edge &b)
Compare edges.
Definition: edgeI.H:33
Foam::Pair< label >
Foam::edge::vec
vector vec(const UList< point > &pts) const
Return the vector (end - start)
Definition: edgeI.H:417
Foam::Vector< scalar >
Foam::edge::maxVertex
label maxVertex() const
Return the largest point label used by the edge.
Definition: edgeI.H:124
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:53
Foam::edge::valid
bool valid() const
Return true if the vertices are unique and non-negative.
Definition: edgeI.H:130
Foam::edge::count
label count() const
Return the number of unique, valid (non -1) point labels.
Definition: edgeI.H:238
Foam::edge::collapse
label collapse()
Definition: edgeI.H:204
Foam::edge::operator[]
label & operator[](const label i)
Return edge element. Index should be limited to 0/1.
Definition: edgeI.H:473