edge.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-2015 OpenFOAM Foundation
9  Copyright (C) 2017-2025 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 Class
28  Foam::edge
29 
30 Description
31  An edge is a list of two vertex labels.
32  This can correspond to a directed graph edge or an edge on a mesh.
33 
34  The edge is implemented as a Pair/FixedList of labels.
35  As well as geometrically relevant methods, it also provides methods
36  similar to HashSet for additional convenience.
37  Valid vertex labels are always non-negative (eg, since they correspond to
38  addressing within the mesh). The value '-1' is used to tag invalid
39  point labels that correspond conceptually to open 'slots', which
40  can be filled with a HashSet-like functionality.
41 
42 SourceFiles
43  edge.C
44  edgeI.H
45 
46 \*---------------------------------------------------------------------------*/
47 
48 #ifndef Foam_edge_H
49 #define Foam_edge_H
50 
51 #include "labelPair.H"
52 #include "line.H"
53 #include "pointField.H"
54 
55 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 
57 namespace Foam
58 {
59 
60 /*---------------------------------------------------------------------------*\
61  Class edge Declaration
62 \*---------------------------------------------------------------------------*/
63 
64 class edge
65 :
66  public labelPair
67 {
68 public:
69 
70  // Static Data Members
71 
72  //- The typeName ("edge")
73  static const char* const typeName;
74 
75 
76  // Constructors
77 
78  //- Default construct, with invalid vertex labels (-1)
79  inline edge();
80 
81  //- Construct from two vertex labels
82  inline edge(label from, label to);
83 
84  //- Construct from two vertex labels
85  inline edge(const labelPair& pair);
86 
87  //- Construct from two vertex labels
88  inline edge(const FixedList<label, 2>& pair);
89 
90  //- Copy construct from a subset of vertex labels
91  inline edge
92  (
93  const UList<label>& list,
94  const FixedList<label, 2>& indices
95  );
96 
97  //- Construct from Istream
98  inline edge(Istream& is);
99 
100 
101  // Static Functions
102 
103  //- Create (in ascending order) from two vertex labels
104  inline static edge sorted(label from, label to);
105 
106  //- Create (in ascending order) from two vertex labels
107  inline static edge sorted(const labelPair& pair);
108 
109  //- Create (in ascending order) from two vertex labels
110  inline static edge sorted(const FixedList<label, 2>& pair);
111 
112 
113  // Member Functions
114 
115  // Access
116 
117  //- The first vertex
118  label a() const noexcept { return labelPair::first(); }
119 
120  //- The first vertex
121  label& a() noexcept { return labelPair::first(); }
122 
123  //- The second vertex
124  label b() const noexcept { return labelPair::second(); }
125 
126  //- The second vertex
127  label& b() noexcept { return labelPair::second(); }
128 
129  //- The start (first) vertex label
130  label start() const noexcept { return labelPair::first(); }
131 
132  //- The start (first) vertex label
133  label& start() noexcept { return labelPair::first(); }
134 
135  //- The end (second/last) vertex label
136  label& end() noexcept { return labelPair::second(); }
137 
138  //- The end (last/second) vertex label
139  label end() const noexcept { return labelPair::second(); }
141 
142  //- Return reverse edge as copy.
143  // No special handling of negative vertex labels.
144  inline edge reverseEdge() const;
146 
147  // Queries
148 
149  //- Return the smallest vertex label used by the edge
150  // No special handling of negative vertex labels.
151  inline label min() const noexcept;
152 
153  //- Return the largest vertex label used by the edge
154  // No special handling of negative vertex labels.
155  inline label max() const noexcept;
156 
157  //- True if the vertices are unique and non-negative.
158  inline bool good() const noexcept;
159 
160  //- Return true if the vertex label is contained in the edge.
161  // Always false for a negative vertex label.
162  inline bool contains(const label vertex) const noexcept;
163 
164  //- Return local index (0,1) of vertex label in edge -1 on failure
165  // Always return -1 for a negative vertex label.
166  inline label which(const label vertex) const;
167 
168  //- True if the edge has at least one vertex in common with other
169  inline bool connected(const edge& other) const;
171  //- Return vertex common with other edge or -1 on failure
172  // Negative vertex labels are never considered common between edges.
173  inline label commonVertex(const edge& other) const;
174 
175  //- Given one vertex label, return the other one.
176  // No special treatment for negative vertex labels.
177  inline label otherVertex(const label vertex) const;
178 
179  //- Do the edges share a common vertex index?
180  // Negative vertex labels never connect.
181  bool connects(const edge& other) const { return connected(other); }
182 
183 
184  // Editing
185 
186  //- 'Collapse' edge by marking duplicate vertex labels as '-1',
187  //- the lower vertex is retained.
188  // Return the effective size after collapsing.
189  inline label collapse();
190 
191 
192  // Hash-like Functions
193 
194  //- Return the number of unique, valid (non -1) vertex labels.
195  // Similar to a HashTable::size().
196  inline label count() const;
197 
198  //- Return true if edge has no valid vertex labels.
199  inline bool empty() const noexcept;
200 
201  //- 'Clears' edge by setting both ends to invalid vertex labels.
202  inline void clear();
203 
204 
205  //- Fill any open slot with the vertex label
206  //- (if not previously contained in the edge).
207  // Returns true on success. A negative vertex label never inserts.
208  // Similar to a HashTable::insert().
209  inline bool insert(const label vertex);
210 
211  //- Insert values, using begin/end iterators.
212  template<class InputIterator>
213  inline label insert(InputIterator begIter, InputIterator endIter);
214 
215  //- Fill open slots with the indices if they did not previously exist.
216  // Returns true on success. Negative labels never insert.
217  // Return the number of slots filled.
218  // Similar to a HashTable::insert().
219  inline label insert(std::initializer_list<label> list);
220 
221  //- Fill open slots with the indices if they did not previously exist.
222  // Returns true on success. Negative labels never insert.
223  // Return the number of slots filled.
224  // Similar to a HashTable::insert().
225  template<unsigned N>
226  inline label insert(const FixedList<label, N>& list);
227 
228  //- Fill open slots with the indices if they did not previously exist.
229  // Returns true on success. Negative labels never insert.
230  // Return the number of slots filled.
231  // Similar to a HashTable::insert().
232  inline label insert(const labelUList& list);
233 
234 
235  //- Remove an existing vertex from the edge and set its location
236  //- to '-1'. A negative vertex label never removes.
237  // Returns the number of changes.
238  // Similar to a HashTable::erase().
239  inline label erase(const label vertex);
241  //- Remove values, using begin/end iterators.
242  template<class InputIterator>
243  inline label erase(InputIterator begIter, InputIterator endIter);
244 
245  //- Remove existing indices from the edge and set locations to '-1'.
246  // Returns the number of changes.
247  inline label erase(std::initializer_list<label> list);
248 
249  //- Remove existing indices from the edge and set locations to '-1'.
250  // Returns the number of changes.
251  template<unsigned N>
252  inline label erase(const FixedList<label, N>& list);
253 
254  //- Remove existing indices from the edge and set locations to '-1'.
255  // Returns the number of changes.
256  inline label erase(const labelUList& list);
257 
258 
259  // Geometric Functions
260 
261  //- Return centre point (centroid) of the edge.
262  // No special handling of negative vertex labels.
263  inline point centre(const UList<point>& pts) const;
264 
265  //- Return the vector (from first to second).
266  // No special handling of negative vertex labels.
267  inline vector vec(const UList<point>& pts) const;
268 
269  //- Return the unit vector (from first to second).
270  // No special handling of negative vertex labels.
271  inline vector unitVec(const UList<point>& pts) const;
272 
273  //- The length (L2-norm) of the edge vector.
274  // No special handling of negative vertex labels.
275  inline scalar mag(const UList<point>& pts) const;
276 
277  //- The length (L2-norm) squared of the edge vector.
278  // No special handling of negative vertex labels.
279  inline scalar magSqr(const UList<point>& pts) const;
280 
281  //- The enclosing (bounding) box for the edge
282  inline Pair<point> box(const UList<point>& pts) const;
283 
284  //- Return edge line
285  // No special handling of negative vertex labels.
286  inline linePointRef line(const UList<point>& pts) const;
287 
288 
289  // Comparison
290 
291  //- Compare edges
292  // \return
293  // - 0: different
294  // - +1: identical values and order used
295  // - -1: identical values, but in different order
296  static inline int compare(const edge& a, const edge& b);
297 
298 
299  // Member Operators
300 
301  //- Return edge element. Index should be limited to 0/1.
302  inline label& operator[](const label i);
303 
304  //- Return constant edge element. Index should be limited to 0/1.
305  inline const label& operator[](const label i) const;
306 
307 
308  // Hashing
309 
310  //- The (commutative) hash value for edge, hashes lower value first
311  inline unsigned hash_code(unsigned seed=0) const
312  {
314  if (second() < first())
315  {
316  return op(first(), op(second(), seed));
317  }
318  else
319  {
320  return op(second(), op(first(), seed));
321  }
322  }
323 
324  //- Hashing functor for edge (commutative)
325  // Also useful for inheritance in sub-classes
326  struct hasher
327  {
328  unsigned operator()(const edge& obj, unsigned seed=0) const
329  {
330  return obj.hash_code(seed);
331  }
332  };
333 
334 
335  // Housekeeping
336 
337  //- Construct from two vertex labels, sorted with first less-than second
338  FOAM_DEPRECATED_FOR(2025-04, "edge::sorted() factory")
339  edge(label from, label to, bool doSort) : labelPair(from, to)
340  {
341  if (doSort) labelPair::sort();
342  }
343 
344  //- Construct from list, sorted with first less-than second
345  FOAM_DEPRECATED_FOR(2025-04, "edge::sorted() factory")
346  edge(const FixedList<label, 2>& pair, bool doSort) : labelPair(pair)
347  {
348  if (doSort) labelPair::sort();
349  }
350 
351  //- Same as contains()
352  bool found(label vertex) const { return contains(vertex); }
353 
354  //- Same as good()
355  bool valid() const noexcept { return good(); }
356 
357  //- Same as min()
358  label minVert() const noexcept { return edge::min(); }
359 
360  //- Same as max()
361  label maxVert() const noexcept { return edge::max(); }
362 
363  //- Deprecated(2021-04) hashing functor. Use hasher()
364  // \deprecated(2021-04) - use hasher() functor
365  template<class Unused=bool>
366  struct Hash : edge::hasher
367  {
368  FOAM_DEPRECATED_FOR(2021-04, "hasher()") Hash() {}
369  };
370 };
371 
372 
373 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
374 
375 //- Contiguous data for edge (a pair of labels)
376 template<> struct is_contiguous<edge> : std::true_type {};
377 
378 //- Contiguous label data for edge (a pair of labels)
379 template<> struct is_contiguous_label<edge> : std::true_type {};
380 
381 //- Hashing for edge uses commutative (incremental) hash
382 template<> struct Hash<edge> : edge::hasher {};
383 
384 
385 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
386 
387 //- Return reverse of an edge
388 inline edge reverse(const edge& e)
389 {
390  return edge(e.second(), e.first());
391 }
392 
393 
394 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
395 
396 //- Compare edges for equal content, ignoring orientation
397 inline bool operator==(const edge& a, const edge& b);
398 
399 //- Compare edges for non-equal content, ignoring orientation
400 inline bool operator!=(const edge& a, const edge& b);
401 
402 
403 
404 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
405 
406 } // End namespace Foam
407 
408 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
409 
410 #include "edgeI.H"
411 
412 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
413 
414 #endif
415 
416 // ************************************************************************* //
label count() const
Return the number of unique, valid (non -1) vertex labels.
Definition: edgeI.H:224
unsigned operator()(const edge &obj, unsigned seed=0) const
Definition: edge.H:457
const label & first() const noexcept
Access the first element.
Definition: Pair.H:137
void sort()
Sort so that first() is less-than second()
Definition: PairI.H:151
A line primitive.
Definition: line.H:52
scalar mag(const UList< point > &pts) const
The length (L2-norm) of the edge vector.
Definition: edgeI.H:435
bool valid() const noexcept
Same as good()
Definition: edge.H:492
edge reverseEdge() const
Return reverse edge as copy.
Definition: edgeI.H:211
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
unsigned hash_code(unsigned seed=0) const
The (commutative) hash value for edge, hashes lower value first.
Definition: edge.H:437
label b() const noexcept
The second vertex.
Definition: edge.H:145
bool connected(const edge &other) const
True if the edge has at least one vertex in common with other.
Definition: edgeI.H:152
bool empty() const noexcept
Return true if edge has no valid vertex labels.
Definition: edgeI.H:240
bool connects(const edge &other) const
Do the edges share a common vertex index?
Definition: edge.H:240
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
vector unitVec(const UList< point > &pts) const
Return the unit vector (from first to second).
Definition: edgeI.H:418
const label & other(const label &a) const
Return other element.
Definition: PairI.H:113
label erase(const label vertex)
Remove an existing vertex from the edge and set its location to &#39;-1&#39;. A negative vertex label never r...
Definition: edgeI.H:320
label start() const noexcept
The start (first) vertex label.
Definition: edge.H:155
void clear()
&#39;Clears&#39; edge by setting both ends to invalid vertex labels.
Definition: edgeI.H:217
label minVert() const noexcept
Same as min()
Definition: edge.H:497
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
label commonVertex(const edge &other) const
Return vertex common with other edge or -1 on failure.
Definition: edgeI.H:158
An edge is a list of two vertex labels. This can correspond to a directed graph edge or an edge on a ...
Definition: edge.H:59
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
label which(const label vertex) const
Return local index (0,1) of vertex label in edge -1 on failure.
Definition: edgeI.H:133
label collapse()
&#39;Collapse&#39; edge by marking duplicate vertex labels as &#39;-1&#39;, the lower vertex is retained.
Definition: edgeI.H:190
void reverse(UList< T > &list, const label n)
Reverse the first n elements of the list.
Definition: UListI.H:514
vector vec(const UList< point > &pts) const
Return the vector (from first to second).
Definition: edgeI.H:403
Pair< point > box(const UList< point > &pts) const
The enclosing (bounding) box for the edge.
Definition: edgeI.H:448
const direction noexcept
Definition: scalarImpl.H:255
bool good() const noexcept
True if the vertices are unique and non-negative.
Definition: edgeI.H:116
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
Pair< label > labelPair
A pair of labels.
Definition: Pair.H:51
label a() const noexcept
The first vertex.
Definition: edge.H:135
static const char *const typeName
The typeName ("edge")
Definition: edge.H:70
label otherVertex(const label vertex) const
Given one vertex label, return the other one.
Definition: edgeI.H:174
const Vector< label > N(dict.get< Vector< label >>("N"))
bool found(label vertex) const
Same as contains()
Definition: edge.H:487
FOAM_DEPRECATED_FOR(2021-04, "hasher()") Hash()
Definition: edge.H:512
bool insert(const label vertex)
Fill any open slot with the vertex label (if not previously contained in the edge).
Definition: edgeI.H:246
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:47
edge()
Default construct, with invalid vertex labels (-1)
Definition: edgeI.H:62
scalar magSqr(const UList< point > &pts) const
The length (L2-norm) squared of the edge vector.
Definition: edgeI.H:441
bool contains(const label vertex) const noexcept
Return true if the vertex label is contained in the edge.
Definition: edgeI.H:122
point centre(const UList< point > &pts) const
Return centre point (centroid) of the edge.
Definition: edgeI.H:388
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:297
static edge sorted(label from, label to)
Create (in ascending order) from two vertex labels.
Definition: edgeI.H:32
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
const label & second() const noexcept
Access the second element.
Definition: Pair.H:147
label max() const noexcept
Return the largest vertex label used by the edge.
Definition: edgeI.H:110
label maxVert() const noexcept
Same as max()
Definition: edge.H:502
label min() const noexcept
Return the smallest vertex label used by the edge.
Definition: edgeI.H:104
Namespace for OpenFOAM.
FixedList()=default
Default construct.
label & end() noexcept
The end (second/last) vertex label.
Definition: edge.H:165
static int compare(const edge &a, const edge &b)
Compare edges.
Definition: edgeI.H:26
const pointField & pts