Documentation 6b54b78464
Loading...
Searching...
No Matches
CVertexMap.hpp
Go to the documentation of this file.
1/*!
2 * \file CVertexMap.hpp
3 * \brief An index to index lookup vector.
4 * \author P. Gomes
5 * \version 8.5.0 "Harrier"
6 *
7 * SU2 Project Website: https://su2code.github.io
8 *
9 * The SU2 Project is maintained by the SU2 Foundation
10 * (http://su2foundation.org)
11 *
12 * Copyright 2012-2026, SU2 Contributors (cf. AUTHORS.md)
13 *
14 * SU2 is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * SU2 is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with SU2. If not, see <http://www.gnu.org/licenses/>.
26 */
27
28#pragma once
29
30#include "C2DContainer.hpp"
31#include <type_traits>
32#include <cassert>
33
34/*!
35 * \class CVertexMap
36 * \ingroup Containers
37 * \brief A lookup type map, maps indices in a large range to indices in a smaller one.
38 *
39 * The usage case is:
40 * 1 - Initialize via Reset for the large range size.
41 * 2 - Use SetIsVertex to define what large range indices have small range correspondence.
42 * 3 - Call Build, vertices are given a sequential index in increasing order of iPoint,
43 * not in order of calls to SetIsVertex. Keep this in mind when allocating vertex data.
44 * 4 - Use GetVertexIndex to convert a point index to vertex index.
45 *
46 * Only consider using this class if you have no reasonable way to keep track of vertices.
47 * For example if you need to assume every point might be a vertex, but you do not want to
48 * allocate data for the entire set of points.
49 *
50 * \note For efficiency use the smallest type that can fit the maximum number of vertices.
51 */
52template <typename T = unsigned long>
54 static_assert(std::is_unsigned<T>::value && std::is_integral<T>::value,
55 "Vertex map requires an unsigned integral type (e.g. unsigned).");
56
57 private:
58 su2vector<T> Map; /*!< \brief Map from range 0-(nPoint-1) to 1-nVertex. */
59 bool isValid = false; /*!< \brief Set to true when it is safe to use the accessors. */
60 T nVertex = 0; /*!< \brief Number of vertices. */
61
62 public:
63 /*!
64 * \brief Check if the current mapping is valid.
65 */
66 inline bool GetIsValid() const { return isValid; }
67
68 /*!
69 * \brief Get the number of vertices currently mapped.
70 */
71 inline T GetnVertex() const { return nVertex; }
72
73 /*!
74 * \brief Reset the map for size nPoint, marks every point as not-a-vertex.
75 */
76 void Reset(unsigned long nPoint) {
77 Map.resize(nPoint) = 0;
78 nVertex = 0;
79 isValid = true;
80 }
81
82 /*!
83 * \brief Set the vertex status of a point.
84 */
85 inline bool SetIsVertex(unsigned long iPoint, bool isVertex) {
86 /*--- Invalidate map if change is requested as that destroys it. ---*/
87 if (isVertex != bool(Map(iPoint))) {
88 isValid = false;
89 Map(iPoint) = T(isVertex);
90 }
91 return isValid;
92 }
93
94 /*!
95 * \brief Get wheter a point is marked as vertex.
96 */
97 inline bool GetIsVertex(unsigned long iPoint) const { return (Map(iPoint) != 0); }
98
99 /*!
100 * \brief Build the point to vertex map.
101 */
102 T Build() {
103 if (!isValid) {
104 /*--- The map is 1 based, the accessors correct
105 accordingly when returning vertex indices. ---*/
106 nVertex = 0;
107
108 for (unsigned long iPoint = 0; iPoint < Map.size(); ++iPoint)
109 if (Map(iPoint) != 0) Map(iPoint) = ++nVertex;
110
111 isValid = true;
112 }
113 return nVertex;
114 }
115
116 /*!
117 * \brief Convert a point index to vertex index.
118 * \param[in,out] iVertex - On entry point index, on exit vertex index.
119 * \return True if conversion is successful (i.e. point is vertex).
120 */
121 inline bool GetVertexIndex(unsigned long& iVertex) const {
122 assert(isValid && "Vertex map is not in valid state.");
123 iVertex = Map(iVertex);
124 if (iVertex == 0) return false; // not a vertex
125 iVertex--; // decrement for 0 based
126 return true; // is a vertex
127 }
128};
A templated vector/matrix object.
A templated matrix/vector-like object.
Definition C2DContainer.hpp:374
C2DContainer & resize(const Index_t size1, const Index_t size2=1) noexcept
Request a change of size.
Definition C2DContainer.hpp:541
size_t size() const noexcept
Definition C2DContainer.hpp:222
A lookup type map, maps indices in a large range to indices in a smaller one.
Definition CVertexMap.hpp:53
void Reset(unsigned long nPoint)
Reset the map for size nPoint, marks every point as not-a-vertex.
Definition CVertexMap.hpp:76
bool GetIsValid() const
Check if the current mapping is valid.
Definition CVertexMap.hpp:66
bool GetIsVertex(unsigned long iPoint) const
Get wheter a point is marked as vertex.
Definition CVertexMap.hpp:97
T GetnVertex() const
Get the number of vertices currently mapped.
Definition CVertexMap.hpp:71
bool SetIsVertex(unsigned long iPoint, bool isVertex)
Set the vertex status of a point.
Definition CVertexMap.hpp:85
T Build()
Build the point to vertex map.
Definition CVertexMap.hpp:102
bool GetVertexIndex(unsigned long &iVertex) const
Convert a point index to vertex index.
Definition CVertexMap.hpp:121