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-------------------------------------------------------------------------------
11License
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
33inline 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
47inline Foam::edge::edge(const label from, const label to)
48:
49 labelPair(from, to)
50{}
51
52
53inline 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
65inline Foam::edge::edge(const label from, const label to, const bool doSort)
66:
67 labelPair(from, to, doSort)
68{}
69
70
71inline Foam::edge::edge(const FixedList<label, 2>& list, const bool doSort)
72:
73 labelPair(list, doSort)
74{}
75
76
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
95inline Foam::label Foam::edge::start() const
96{
97 return first();
98}
99
100inline Foam::label& Foam::edge::start()
101{
102 return first();
103}
104
105
106inline Foam::label Foam::edge::end() const
107{
108 return second();
109}
110
111
112inline Foam::label& Foam::edge::end()
113{
114 return second();
115}
116
117
118inline Foam::label Foam::edge::minVertex() const
119{
120 return (first() < second() ? first() : second());
121}
122
123
124inline Foam::label Foam::edge::maxVertex() const
125{
126 return (first() > second() ? first() : second());
127}
128
129
130inline bool Foam::edge::valid() const
131{
132 return (first() != second() && first() >= 0 && second() >= 0);
133}
134
135
136inline 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
147inline 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
166inline bool Foam::edge::connects(const edge& other) const
167{
168 return (other.found(first()) || other.found(second()));
169}
170
171
172inline 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
188inline 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
204inline 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
231inline void Foam::edge::clear()
232{
233 first() = -1;
234 second() = -1;
235}
236
237
238inline 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
254inline bool Foam::edge::empty() const
255{
256 return (first() < 0 && second() < 0);
257}
258
259
260inline 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
291template<class InputIterator>
292inline 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
315inline Foam::label Foam::edge::insert(std::initializer_list<label> list)
316{
317 return insert(list.begin(), list.end());
318}
319
320
321template<unsigned N>
322inline Foam::label Foam::edge::insert(const FixedList<label, N>& list)
323{
324 return insert(list.begin(), list.end());
325}
326
327
328inline Foam::label Foam::edge::insert(const labelUList& list)
329{
330 return insert(list.begin(), list.end());
331}
332
333
334inline 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
360template<class InputIterator>
361inline 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
381inline Foam::label Foam::edge::erase(std::initializer_list<label> list)
382{
383 return erase(list.begin(), list.end());
384}
385
386
387template<unsigned N>
388inline Foam::label Foam::edge::erase(const FixedList<label, N>& list)
389{
390 return erase(list.begin(), list.end());
391}
392
393
394inline 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
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
450inline 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
473inline 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
486inline 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
501inline bool Foam::operator==(const edge& a, const edge& b)
502{
503 return edge::compare(a,b) != 0;
504}
505
506
507inline bool Foam::operator!=(const edge& a, const edge& b)
508{
509 return edge::compare(a,b) == 0;
510}
511
512
513// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
bool found
label n
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: FixedList.H:81
iterator end() noexcept
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:548
iterator begin() noexcept
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:524
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
scalar centre() const
Mid-point location, zero for an empty list.
Definition: PDRblockI.H:67
static int compare(const Pair< label > &a, const Pair< label > &b)
Compare Pairs.
Definition: PairI.H:31
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:350
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:66
static int compare(const edge &a, const edge &b)
Compare edges.
Definition: edgeI.H:33
bool valid() const
Return true if the vertices are unique and non-negative.
Definition: edgeI.H:130
bool insert(const label index)
Fill any open slot with the index if it did not previously exist.
Definition: edgeI.H:260
label otherVertex(const label pointLabel) const
Given the point label for one vertex, return the other one.
Definition: edgeI.H:188
edge reverseEdge() const
Return reverse edge as copy.
Definition: edgeI.H:225
label end() const
Return end (last/second) vertex label.
Definition: edgeI.H:106
bool empty() const
Return true if edge has no valid point labels.
Definition: edgeI.H:254
label which(const label pointLabel) const
Return local index (0,1) of point label in edge -1 on failure.
Definition: edgeI.H:147
edge()
Default construct, with invalid point labels (-1)
Definition: edgeI.H:41
label collapse()
Definition: edgeI.H:204
label commonVertex(const edge &other) const
Return vertex common with other edge or -1 on failure.
Definition: edgeI.H:172
label minVertex() const
Return the smallest point label used by the edge.
Definition: edgeI.H:118
label & operator[](const label i)
Return edge element. Index should be limited to 0/1.
Definition: edgeI.H:473
void clear()
'Clears' edge by setting both ends to invalid point labels.
Definition: edgeI.H:231
label count() const
Return the number of unique, valid (non -1) point labels.
Definition: edgeI.H:238
bool found(const label pointLabel) const
Return true if point label is found in edge.
Definition: edgeI.H:136
bool connects(const edge &other) const
Do the edges share a common vertex index?
Definition: edgeI.H:166
label maxVertex() const
Return the largest point label used by the edge.
Definition: edgeI.H:124
label erase(const label index)
Remove an existing index from the edge and set its location to '-1'.
Definition: edgeI.H:334
label start() const
Return start (first) vertex label.
Definition: edgeI.H:95
Computes the magnitude of an input field.
Definition: mag.H:153
A line primitive.
Definition: line.H:68
Point unitVec() const
Return the unit vector (start-to-end)
Definition: lineI.H:119
Point vec() const
Return start-to-end vector.
Definition: lineI.H:112
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
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))
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:239
line< point, const point & > linePointRef
A line using referred points.
Definition: linePointRef.H:47
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
errorManip< error > abort(error &err)
Definition: errorManip.H:144
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
error FatalError
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
srcOptions erase("case")
volScalarField & b
Definition: createFields.H:27