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-2023 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(const label from, const label to);
83 
84  //- Construct from pair of vertex labels
85  inline edge(const labelPair& pair);
86 
87  //- Construct from list of vertex labels
88  inline edge(const FixedList<label, 2>& list);
89 
90  //- Construct from two vertex labels, sorted with first less-than second
91  inline edge(const label from, const label to, const bool doSort);
92 
93  //- Construct from list, sorted with first less-than second
94  inline edge(const FixedList<label, 2>& list, const bool doSort);
95 
96  //- Copy construct from a subset of vertex labels
97  inline edge
98  (
99  const UList<label>& list,
100  const FixedList<label, 2>& indices
101  );
102 
103  //- Construct from Istream
104  inline edge(Istream& is);
105 
106 
107  // Member Functions
108 
109  // Access
110 
111  //- The first vertex
112  label a() const noexcept { return labelPair::first(); }
113 
114  //- The second vertex
115  label b() const noexcept { return labelPair::second(); }
116 
117  //- The first vertex
118  label& a() noexcept { return labelPair::first(); }
119 
120  //- The second vertex
121  label& b() noexcept { return labelPair::second(); }
122 
123  //- The start (first) vertex label
124  label start() const noexcept { return labelPair::first(); }
125 
126  //- The end (last/second) vertex label
127  label end() const noexcept { return labelPair::second(); }
128 
129  //- The start (first) vertex label
130  label& start() noexcept { return labelPair::first(); }
131 
132  //- The end (second/last) vertex label
133  label& end() noexcept { return labelPair::second(); }
134 
135 
136  //- Return reverse edge as copy.
137  // No special handling of negative vertex labels.
138  inline edge reverseEdge() const;
139 
140 
141  // Queries
143  //- Return the smallest vertex label used by the edge
144  // No special handling of negative vertex labels.
145  inline label minVertex() const noexcept;
146 
147  //- Return the largest vertex label used by the edge
148  // No special handling of negative vertex labels.
149  inline label maxVertex() const noexcept;
150 
151  //- True if the vertices are unique and non-negative.
152  inline bool good() const noexcept;
153 
154  //- Return true if the vertex label is contained in the edge.
155  // Always false for a negative vertex label.
156  inline bool contains(const label vertex) const noexcept;
158  //- Return local index (0,1) of vertex label in edge -1 on failure
159  // Always return -1 for a negative vertex label.
160  inline label which(const label vertex) const;
161 
162  //- True if the edge has at least one vertex in common with other
163  inline bool connected(const edge& other) const;
164 
165  //- Return vertex common with other edge or -1 on failure
166  // Negative vertex labels are never considered common between edges.
167  inline label commonVertex(const edge& other) const;
168 
169  //- Given one vertex label, return the other one.
170  // No special treatment for negative vertex labels.
171  inline label otherVertex(const label vertex) const;
172 
173  //- Do the edges share a common vertex index?
174  // Negative vertex labels never connect.
175  bool connects(const edge& other) const { return connected(other); }
176 
177 
178  // Editing
179 
180  //- 'Collapse' edge by marking duplicate vertex labels as '-1',
181  //- the lower vertex is retained.
182  // Return the effective size after collapsing.
183  inline label collapse();
184 
185 
186  // Hash-like Functions
187 
188  //- Return the number of unique, valid (non -1) vertex labels.
189  // Similar to a HashTable::size().
190  inline label count() const;
191 
192  //- Return true if edge has no valid vertex labels.
193  inline bool empty() const noexcept;
194 
195  //- 'Clears' edge by setting both ends to invalid vertex labels.
196  inline void clear();
197 
198 
199  //- Fill any open slot with the vertex label
200  //- (if not previously contained in the edge).
201  // Returns true on success. A negative vertex label never inserts.
202  // Similar to a HashTable::insert().
203  inline bool insert(const label vertex);
204 
205  //- Insert values, using begin/end iterators.
206  template<class InputIterator>
207  inline label insert(InputIterator begIter, InputIterator endIter);
208 
209  //- Fill open slots with the indices if they did not previously exist.
210  // Returns true on success. Negative labels never insert.
211  // Return the number of slots filled.
212  // Similar to a HashTable::insert().
213  inline label insert(std::initializer_list<label> list);
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  template<unsigned N>
220  inline label insert(const FixedList<label, N>& list);
221 
222  //- Fill open slots with the indices if they did not previously exist.
223  // Returns true on success. Negative labels never insert.
224  // Return the number of slots filled.
225  // Similar to a HashTable::insert().
226  inline label insert(const labelUList& list);
227 
228 
229  //- Remove an existing vertex from the edge and set its location
230  //- to '-1'. A negative vertex label never removes.
231  // Returns the number of changes.
232  // Similar to a HashTable::erase().
233  inline label erase(const label vertex);
234 
235  //- Remove values, using begin/end iterators.
236  template<class InputIterator>
237  inline label erase(InputIterator begIter, InputIterator endIter);
238 
239  //- Remove existing indices from the edge and set locations to '-1'.
240  // Returns the number of changes.
241  inline label erase(std::initializer_list<label> list);
242 
243  //- Remove existing indices from the edge and set locations to '-1'.
244  // Returns the number of changes.
245  template<unsigned N>
246  inline label erase(const FixedList<label, N>& list);
247 
248  //- Remove existing indices from the edge and set locations to '-1'.
249  // Returns the number of changes.
250  inline label erase(const labelUList& list);
251 
252 
253  // Geometric Functions
254 
255  //- Return centre point (centroid) of the edge.
256  // No special handling of negative vertex labels.
257  inline point centre(const UList<point>& pts) const;
258 
259  //- Return the vector (from first to second).
260  // No special handling of negative vertex labels.
261  inline vector vec(const UList<point>& pts) const;
262 
263  //- Return the unit vector (from first to second).
264  // No special handling of negative vertex labels.
265  inline vector unitVec(const UList<point>& pts) const;
266 
267  //- The length (L2-norm) of the edge vector.
268  // No special handling of negative vertex labels.
269  inline scalar mag(const UList<point>& pts) const;
270 
271  //- The length (L2-norm) squared of the edge vector.
272  // No special handling of negative vertex labels.
273  inline scalar magSqr(const UList<point>& pts) const;
274 
275  //- The enclosing (bounding) box for the edge
276  inline Pair<point> box(const UList<point>& pts) const;
277 
278  //- Return edge line
279  // No special handling of negative vertex labels.
280  inline linePointRef line(const UList<point>& pts) const;
281 
282 
283  // Comparison
284 
285  //- Compare edges
286  // \return
287  // - 0: different
288  // - +1: identical values and order used
289  // - -1: identical values, but in different order
290  static inline int compare(const edge& a, const edge& b);
291 
292 
293  // Member Operators
294 
295  //- Return edge element. Index should be limited to 0/1.
296  inline label& operator[](const label i);
297 
298  //- Return constant edge element. Index should be limited to 0/1.
299  inline const label& operator[](const label i) const;
300 
301 
302  // Hashing
303 
304  //- The (commutative) hash value for edge, hashes lower value first
305  inline unsigned hash_code(unsigned seed=0) const
306  {
308  if (second() < first())
309  {
310  return op(first(), op(second(), seed));
311  }
312  else
313  {
314  return op(second(), op(first(), seed));
315  }
316  }
317 
318  //- Hashing functor for edge (commutative)
319  // Also useful for inheritance in sub-classes
320  struct hasher
321  {
322  unsigned operator()(const edge& obj, unsigned seed=0) const
323  {
324  return obj.hash_code(seed);
325  }
326  };
327 
328 
329  // Housekeeping
330 
331  //- Same as contains()
332  bool found(label vertex) const { return contains(vertex); }
333 
334  //- Same as good()
335  bool valid() const noexcept { return good(); }
336 
337  //- Deprecated(2021-04) hashing functor. Use hasher()
338  // \deprecated(2021-04) - use hasher() functor
339  template<class Unused=bool>
340  struct Hash : edge::hasher
341  {
342  FOAM_DEPRECATED_FOR(2021-04, "hasher()") Hash() {}
343  };
344 };
345 
346 
347 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
348 
349 //- Contiguous data for edge (a pair of labels)
350 template<> struct is_contiguous<edge> : std::true_type {};
351 
352 //- Contiguous label data for edge (a pair of labels)
353 template<> struct is_contiguous_label<edge> : std::true_type {};
354 
355 //- Hashing for edge uses commutative (incremental) hash
356 template<> struct Hash<edge> : edge::hasher {};
357 
358 
359 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
360 
361 //- Return reverse of an edge
362 inline edge reverse(const edge& e)
363 {
364  return edge(e.second(), e.first());
365 }
366 
367 
368 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
369 
370 //- Compare edges for equal content, ignoring orientation
371 inline bool operator==(const edge& a, const edge& b);
372 
373 //- Compare edges for non-equal content, ignoring orientation
374 inline bool operator!=(const edge& a, const edge& b);
375 
376 
377 
378 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
379 
380 } // End namespace Foam
381 
382 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
383 
384 #include "edgeI.H"
385 
386 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
387 
388 #endif
389 
390 // ************************************************************************* //
label count() const
Return the number of unique, valid (non -1) vertex labels.
Definition: edgeI.H:208
unsigned operator()(const edge &obj, unsigned seed=0) const
Definition: edge.H:449
const label & first() const noexcept
Access the first element.
Definition: Pair.H:137
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:419
bool valid() const noexcept
Same as good()
Definition: edge.H:466
edge reverseEdge() const
Return reverse edge as copy.
Definition: edgeI.H:195
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:429
label b() const noexcept
The second vertex.
Definition: edge.H:132
bool connected(const edge &other) const
True if the edge has at least one vertex in common with other.
Definition: edgeI.H:136
bool empty() const noexcept
Return true if edge has no valid vertex labels.
Definition: edgeI.H:224
bool connects(const edge &other) const
Do the edges share a common vertex index?
Definition: edge.H:232
vector unitVec(const UList< point > &pts) const
Return the unit vector (from first to second).
Definition: edgeI.H:402
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:304
label start() const noexcept
The start (first) vertex label.
Definition: edge.H:147
void clear()
&#39;Clears&#39; edge by setting both ends to invalid vertex labels.
Definition: edgeI.H:201
label minVertex() const noexcept
Return the smallest vertex label used by the edge.
Definition: edgeI.H:88
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:142
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:117
label collapse()
&#39;Collapse&#39; edge by marking duplicate vertex labels as &#39;-1&#39;, the lower vertex is retained.
Definition: edgeI.H:174
void reverse(UList< T > &list, const label n)
Reverse the first n elements of the list.
Definition: UListI.H:520
vector vec(const UList< point > &pts) const
Return the vector (from first to second).
Definition: edgeI.H:387
Pair< point > box(const UList< point > &pts) const
The enclosing (bounding) box for the edge.
Definition: edgeI.H:432
const direction noexcept
Definition: Scalar.H:258
bool good() const noexcept
True if the vertices are unique and non-negative.
Definition: edgeI.H:100
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
label a() const noexcept
The first vertex.
Definition: edge.H:127
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:158
const Vector< label > N(dict.get< Vector< label >>("N"))
bool found(label vertex) const
Same as contains()
Definition: edge.H:461
FOAM_DEPRECATED_FOR(2021-04, "hasher()") Hash()
Definition: edge.H:476
bool insert(const label vertex)
Fill any open slot with the vertex label (if not previously contained in the edge).
Definition: edgeI.H:230
label maxVertex() const noexcept
Return the largest vertex label used by the edge.
Definition: edgeI.H:94
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:34
scalar magSqr(const UList< point > &pts) const
The length (L2-norm) squared of the edge vector.
Definition: edgeI.H:425
bool contains(const label vertex) const noexcept
Return true if the vertex label is contained in the edge.
Definition: edgeI.H:106
point centre(const UList< point > &pts) const
Return centre point (centroid) of the edge.
Definition: edgeI.H:372
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:297
label end() const noexcept
The end (last/second) vertex label.
Definition: edge.H:152
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
const label & second() const noexcept
Access the second element.
Definition: Pair.H:147
Namespace for OpenFOAM.
static int compare(const edge &a, const edge &b)
Compare edges.
Definition: edgeI.H:26
const pointField & pts