Visual Computing Library
Loading...
Searching...
No Matches
tri_poly_index_bimap.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_SPACE_COMPLEX_TRI_POLY_INDEX_BIMAP_H
24#define VCL_SPACE_COMPLEX_TRI_POLY_INDEX_BIMAP_H
25
26#include <vclib/types.h>
27
28#include <vector>
29
30namespace vcl {
31
50{
51 std::vector<uint> mTriToPoly;
52 std::vector<uint> mPolyToTri;
53
54public:
58 TriPolyIndexBiMap() = default;
59
67 uint polygon(uint triangleIndex) const
68 {
69 assert(triangleIndex < mTriToPoly.size());
70 return mTriToPoly[triangleIndex];
71 }
72
83 uint triangleBegin(uint polygonIndex) const
84 {
85 assert(polygonIndex < mPolyToTri.size());
86 return mPolyToTri[polygonIndex];
87 }
88
113 {
114 // be sure that the current polygon index is valid and the polygon
115 // has not been deleted in the mesh
116 assert(polygonIndex < mPolyToTri.size());
117 assert(mPolyToTri[polygonIndex] != UINT_NULL);
118
119 // we need to manage the case in which the polygon was deleted: some
120 // values in the mPolyToTri vector could be UINT_NULL. In this case,
121 // we just need to jump to the next polygon index having a valid value
122
123 // look for the next polygon index having value != UINT_NULL
124 uint pnext = polygonIndex + 1;
125 while (pnext < mPolyToTri.size() && mPolyToTri[pnext] == UINT_NULL) {
126 pnext++;
127 }
128
129 if (pnext < mPolyToTri.size()) { // there is a next polygon index
130 return mPolyToTri[pnext] - mPolyToTri[polygonIndex];
131 }
132 else {
133 // total number of triangles minus the first triangle index of the
134 // polygon
135 return mTriToPoly.size() - mPolyToTri[polygonIndex];
136 }
137 }
138
142 void clear()
143 {
144 mTriToPoly.clear();
145 mPolyToTri.clear();
146 }
147
155 void reserve(uint nTriangles, uint nPolygons)
156 {
157 mTriToPoly.reserve(nTriangles);
158 mPolyToTri.reserve(nPolygons);
159 }
160
176 {
177 // add the index of the polygon associated to the triangle
178 if (triangleIndex >= mTriToPoly.size()) {
179 mTriToPoly.resize(triangleIndex + 1, UINT_NULL);
180 }
181 mTriToPoly[triangleIndex] = polygonIndex;
182
183 // add the index of the triangle associated to the polygon,
184 // but only if it is the first triangle index of the polygon!
185 if (polygonIndex >= mPolyToTri.size()) {
186 mPolyToTri.resize(polygonIndex + 1, UINT_NULL);
187 }
188 if (mPolyToTri[polygonIndex] == UINT_NULL ||
189 triangleIndex < mPolyToTri[polygonIndex])
190 mPolyToTri[polygonIndex] = triangleIndex;
191 }
192
197 uint triangleNumber() const { return mTriToPoly.size(); }
198
203 uint polygonNumber() const { return mPolyToTri.size(); }
204};
205
206} // namespace vcl
207
208#endif // VCL_SPACE_COMPLEX_TRI_POLY_INDEX_BIMAP_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
The TriPolyIndexBiMap class allows to store a bidirectional mapping between a Polygon Mesh and a Tria...
Definition tri_poly_index_bimap.h:50
TriPolyIndexBiMap()=default
Creates an empty BiMap.
void reserve(uint nTriangles, uint nPolygons)
Reserves enogh memory for the BiMap. Allows fast insertions.
Definition tri_poly_index_bimap.h:155
uint triangleNumber() const
Returns the number of triangles stored in the BiMap.
Definition tri_poly_index_bimap.h:197
void insert(uint triangleIndex, uint polygonIndex)
Performs an insertion into the BiMap, and associates:
Definition tri_poly_index_bimap.h:175
void clear()
Clears the BiMap.
Definition tri_poly_index_bimap.h:142
uint polygon(uint triangleIndex) const
Returns the index of the polygon mapped to the triangle having the index given as input argument.
Definition tri_poly_index_bimap.h:67
uint triangleBegin(uint polygonIndex) const
Returns the smallest index of set of triangles mapped to the polygon having the index given as input ...
Definition tri_poly_index_bimap.h:83
uint polygonNumber() const
Returns the number of polygons stored in the BiMap.
Definition tri_poly_index_bimap.h:203
uint triangleNumber(uint polygonIndex) const
Returns the number of (consecutive index) triangles mapped to a polygon.
Definition tri_poly_index_bimap.h:112
constexpr uint UINT_NULL
The UINT_NULL value represent a null value of uint that is the maximum value that can be represented ...
Definition base.h:48