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