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-2021 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 edge_H
49 #define edge_H
50 
51 #include "labelPair.H"
52 #include "linePointRef.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  //- Return first vertex label
111  using labelPair::first;
112 
113  //- Return last (second) vertex label
114  using labelPair::last;
115 
116  //- Return second (last) vertex label
117  using labelPair::second;
118 
119 
120  //- Return start (first) vertex label
121  inline label start() const;
122 
123  //- Return start (first) vertex label
124  inline label& start();
125 
126  //- Return end (last/second) vertex label
127  inline label end() const;
128 
129  //- Return end (last/second) vertex label
130  inline label& end();
131 
132  //- Return reverse edge as copy.
133  // No special handling of negative point labels.
134  inline edge reverseEdge() const;
135 
136 
137  // Queries
138 
139  //- Return the smallest point label used by the edge
140  // No special handling of negative point labels.
141  inline label minVertex() const;
142 
143  //- Return the largest point label used by the edge
144  // No special handling of negative point labels.
145  inline label maxVertex() const;
146 
147  //- Return true if the vertices are unique and non-negative.
148  inline bool valid() const;
149 
150  //- Return true if point label is found in edge.
151  // Always false for a negative label.
152  inline bool found(const label pointLabel) const;
153 
154  //- Return local index (0,1) of point label in edge -1 on failure
155  // Always return -1 for a negative label.
156  inline label which(const label pointLabel) const;
157 
158  //- Do the edges share a common vertex index?
159  // Negative point labels never connect.
160  inline bool connects(const edge& other) const;
161 
162  //- Return vertex common with other edge or -1 on failure
163  // Negative point labels are never considered common between edges.
164  inline label commonVertex(const edge& other) const;
165 
166  //- Given the point label for one vertex, return the other one.
167  // No special treatment for negative point labels.
168  inline label otherVertex(const label pointLabel) const;
169 
170 
171  // Editing
172 
173  //- 'Collapse' edge by marking duplicate point labels as '-1',
174  //- the lower vertex is retained.
175  // Return the effective size after collapsing.
176  inline label collapse();
177 
178 
179  // Hash-like Functions
180 
181  //- Return the number of unique, valid (non -1) point labels.
182  // Similar to a HashTable::size().
183  inline label count() const;
184 
185  //- Return true if edge has no valid point labels.
186  inline bool empty() const;
187 
188  //- 'Clears' edge by setting both ends to invalid point labels.
189  inline void clear();
190 
191 
192  //- Fill any open slot with the index if it did not previously exist.
193  // Returns true on success. A negative label never inserts.
194  // Similar to a HashTable::insert().
195  inline bool insert(const label index);
196 
197  //- Insert values, using begin/end iterators.
198  template<class InputIterator>
199  inline label insert(InputIterator begIter, InputIterator endIter);
200 
201  //- Fill open slots with the indices if they did not previously exist.
202  // Returns true on success. Negative labels never insert.
203  // Return the number of slots filled.
204  // Similar to a HashTable::insert().
205  inline label insert(std::initializer_list<label> list);
206 
207  //- Fill open slots with the indices if they did not previously exist.
208  // Returns true on success. Negative labels never insert.
209  // Return the number of slots filled.
210  // Similar to a HashTable::insert().
211  template<unsigned N>
212  inline label insert(const FixedList<label, N>& list);
213 
214  //- Fill open slots with the indices if they did not previously exist.
215  // Returns true on success. Negative labels never insert.
216  // Return the number of slots filled.
217  // Similar to a HashTable::insert().
218  inline label insert(const labelUList& list);
219 
220 
221  //- Remove an existing index from the edge and set its location to '-1'.
222  // Returns the number of changes. A negative label never removes.
223  // Similar to a HashTable::erase().
224  inline label erase(const label index);
225 
226  //- Remove values, using begin/end iterators.
227  template<class InputIterator>
228  inline label erase(InputIterator begIter, InputIterator endIter);
229 
230  //- Remove existing indices from the edge and set locations to '-1'.
231  // Returns the number of changes.
232  inline label erase(std::initializer_list<label> list);
233 
234  //- Remove existing indices from the edge and set locations to '-1'.
235  // Returns the number of changes.
236  template<unsigned N>
237  inline label erase(const FixedList<label, N>& list);
238 
239  //- Remove existing indices from the edge and set locations to '-1'.
240  // Returns the number of changes.
241  inline label erase(const labelUList& list);
242 
243 
244  // Geometric Functions
245 
246  //- Return centre point (centroid) of the edge.
247  // No special handling of negative point labels.
248  inline point centre(const UList<point>& pts) const;
249 
250  //- Return the vector (end - start)
251  // No special handling of negative point labels.
252  inline vector vec(const UList<point>& pts) const;
253 
254  //- Return the unit vector (end - start)
255  // No special handling of negative point labels.
256  inline vector unitVec(const UList<point>& pts) const;
257 
258  //- Return scalar magnitude of the edge.
259  // No special handling of negative point labels.
260  inline scalar mag(const UList<point>& pts) const;
261 
262  //- Return edge line
263  // No special handling of negative point labels.
264  inline linePointRef line(const UList<point>& pts) const;
265 
266 
267  // Comparison
268 
269  //- Compare edges
270  // \return
271  // - 0: different
272  // - +1: identical values and order used
273  // - -1: identical values, but in different order
274  static inline int compare(const edge& a, const edge& b);
275 
276 
277  // Member Operators
278 
279  //- Return edge element. Index should be limited to 0/1.
280  inline label& operator[](const label i);
281 
282  //- Return constant edge element. Index should be limited to 0/1.
283  inline const label& operator[](const label i) const;
284 
285 
286  // Hashing
287 
288  //- The (commutative) hash value for edge, hashes lower value first
289  inline unsigned hash_code(unsigned seed=0) const
290  {
292  if (second() < first())
293  {
294  return op(first(), op(second(), seed));
295  }
296  else
297  {
298  return op(second(), op(first(), seed));
299  }
300  }
301 
302  //- Hashing functor for edge (commutative)
303  // Also useful for inheritance in sub-classes
304  struct hasher
305  {
306  unsigned operator()(const edge& obj, unsigned seed=0) const
307  {
308  return obj.hash_code(seed);
309  }
310  };
311 
312  //- Deprecated(2021-04) hashing functor. Use hasher()
313  // \deprecated(2021-04) - use hasher() functor
314  template<class Unused=bool>
315  struct Hash : edge::hasher
316  {
317  FOAM_DEPRECATED_FOR(2021-04, "hasher()") Hash() {}
318  };
319 };
320 
321 
322 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
323 
324 //- Contiguous data for edge (a pair of labels)
325 template<> struct is_contiguous<edge> : std::true_type {};
326 
327 //- Contiguous label data for edge (a pair of labels)
328 template<> struct is_contiguous_label<edge> : std::true_type {};
329 
330 //- Hashing for edge uses commutative (incremental) hash
331 template<> struct Hash<edge> : edge::hasher {};
332 
333 
334 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
335 
336 //- Return reverse of an edge
337 inline edge reverse(const edge& e)
338 {
339  return edge(e.second(), e.first());
340 }
341 
342 
343 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
344 
345 //- Compare edges for equal content, ignoring orientation
346 inline bool operator==(const edge& a, const edge& b);
347 
348 //- Compare edges for non-equal content, ignoring orientation
349 inline bool operator!=(const edge& a, const edge& b);
350 
351 
352 
353 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
354 
355 } // End namespace Foam
356 
357 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
358 
359 #include "edgeI.H"
360 
361 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
362 
363 #endif
364 
365 // ************************************************************************* //
Foam::Pair< label >::second
const label & second() const noexcept
Return second element, which is also the last element.
Definition: PairI.H:122
Foam::reverse
void reverse(UList< T > &list, const label n)
Definition: UListI.H:449
Foam::edge::minVertex
label minVertex() const
Return the smallest point label used by the edge.
Definition: edgeI.H:118
Foam::edge::hash_code
unsigned hash_code(unsigned seed=0) const
The (commutative) hash value for edge, hashes lower value first.
Definition: edge.H:288
Foam::edge::typeName
static const char *const typeName
Definition: edge.H:71
edgeI.H
Foam::edge::commonVertex
label commonVertex(const edge &other) const
Return vertex common with other edge or -1 on failure.
Definition: edgeI.H:172
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
Foam::edge::reverseEdge
edge reverseEdge() const
Return reverse edge as copy.
Definition: edgeI.H:225
Foam::edge::mag
scalar mag(const UList< point > &pts) const
Return scalar magnitude of the edge.
Definition: edgeI.H:450
Foam::edge::line
linePointRef line(const UList< point > &pts) const
Return edge line.
Definition: edgeI.H:456
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
Foam::edge::unitVec
vector unitVec(const UList< point > &pts) const
Return the unit vector (end - start)
Definition: edgeI.H:432
Foam::is_contiguous_label
A template class to specify if a data type is composed solely of Foam::label elements.
Definition: contiguous.H:83
Foam::edge::connects
bool connects(const edge &other) const
Do the edges share a common vertex index?
Definition: edgeI.H:166
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
Foam::edge::start
label start() const
Return start (first) vertex label.
Definition: edgeI.H:95
Foam::Hash
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:53
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::Hash
Deprecated(2021-04) hashing functor. Use hasher()
Definition: edge.H:314
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::Pair< label >::other
const label & other(const label &a) const
Return other element.
Definition: PairI.H:136
Foam::edge::Hash::FOAM_DEPRECATED_FOR
FOAM_DEPRECATED_FOR(2021-04, "hasher()") Hash()
Definition: edge.H:316
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
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::edge::hasher
Hashing functor for edge (commutative)
Definition: edge.H:303
pointField.H
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::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
Foam::UList< label >
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
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::hasher::operator()
unsigned operator()(const edge &obj, unsigned seed=0) const
Definition: edge.H:305
Foam::is_contiguous
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:75
labelPair.H
linePointRef.H
Foam::edge::operator[]
label & operator[](const label i)
Return edge element. Index should be limited to 0/1.
Definition: edgeI.H:473