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