VTK  9.4.20241223
vtkPolyDataInternals.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3
45#ifndef vtkPolyDataInternals_h
46#define vtkPolyDataInternals_h
47
48#include "vtkCommonDataModelModule.h" // For export macro
49
50#include "vtkCellType.h"
51#include "vtkObject.h"
52#include "vtkType.h"
53
54#include <cstdlib> // for std::size_t
55#include <vector> // for CellMap implementation.
56
58{
59VTK_ABI_NAMESPACE_BEGIN
60
61static constexpr vtkTypeUInt64 CELLID_MASK = 0x0fffffffffffffffull;
62static constexpr vtkTypeUInt64 SHIFTED_TYPE_INDEX_MASK = 0xf000000000000000ull;
63static constexpr vtkTypeUInt64 TARGET_MASK = 0x3ull << 62;
64static constexpr vtkTypeUInt64 TYPE_VARIANT_MASK = 0x3ull << 60;
65
66// Enumeration of internal cell array targets.
67enum class Target : vtkTypeUInt64
68{
69 Verts = 0x0ull << 62,
70 Lines = 0x1ull << 62,
71 Polys = 0x2ull << 62,
72 Strips = 0x3ull << 62,
73};
74
75// Enumeration of type variants.
76enum class TypeVariant : vtkTypeUInt64
77{
78 Dead = 0x0ull << 60,
79 Var1 = 0x1ull << 60,
80 Var2 = 0x2ull << 60,
81 Var3 = 0x3ull << 60,
82};
83
84// Lookup table to convert a type index (TaggedCellId.GetTypeIndex()) into
85// a VTK cell type.
86// The type index is the highest four bits of the encoded value, eg. the
87// target and type variant information.
88static constexpr unsigned char TypeTable[16] = {
89 VTK_EMPTY_CELL, // 0000b | Verts | Dead
90 VTK_VERTEX, // 0001b | Verts | Var1
91 VTK_POLY_VERTEX, // 0010b | Verts | Var2
92 VTK_EMPTY_CELL, // 0011b | Verts | Var3
93 VTK_EMPTY_CELL, // 0100b | Lines | Dead
94 VTK_LINE, // 0101b | Lines | Var1
95 VTK_POLY_LINE, // 0110b | Lines | Var2
96 VTK_EMPTY_CELL, // 0111b | Lines | Var3
97 VTK_EMPTY_CELL, // 1000b | Polys | Dead
98 VTK_TRIANGLE, // 1001b | Polys | Var1
99 VTK_QUAD, // 1010b | Polys | Var2
100 VTK_POLYGON, // 1011b | Polys | Var3
101 VTK_EMPTY_CELL, // 1100b | Strips | Dead
102 VTK_TRIANGLE_STRIP, // 1101b | Strips | Var1
103 VTK_EMPTY_CELL, // 1110b | Strips | Var2
104 VTK_EMPTY_CELL, // 1111b | Strips | Var3
105};
106
107// Convenience method to concatenate a target and type variant into the low
108// four bits of a single byte. Used to build the TargetVarTable.
109static constexpr unsigned char GenTargetVar(Target target, TypeVariant var) noexcept
110{
111 return static_cast<unsigned char>(
112 (static_cast<vtkTypeUInt64>(target) | static_cast<vtkTypeUInt64>(var)) >> 60);
113}
114
115// Lookup table that maps a VTK cell type (eg. VTK_TRIANGLE) into a target +
116// type variant byte.
117static constexpr unsigned char TargetVarTable[10] = {
118 GenTargetVar(Target::Verts, TypeVariant::Dead), // 0 | VTK_EMPTY_CELL
119 GenTargetVar(Target::Verts, TypeVariant::Var1), // 1 | VTK_VERTEX
120 GenTargetVar(Target::Verts, TypeVariant::Var2), // 2 | VTK_POLY_VERTEX
122 GenTargetVar(Target::Lines, TypeVariant::Var2), // 4 | VTK_POLY_LINE
123 GenTargetVar(Target::Polys, TypeVariant::Var1), // 5 | VTK_TRIANGLE
124 GenTargetVar(Target::Strips, TypeVariant::Var1), // 6 | VTK_TRIANGLE_STRIP
125 GenTargetVar(Target::Polys, TypeVariant::Var3), // 7 | VTK_POLYGON
126 GenTargetVar(Target::Polys, TypeVariant::Var2), // 8 | VTK_PIXEL (treat as quad)
128};
129
130// Thin wrapper around a vtkTypeUInt64 that encodes a target cell array,
131// cell type, deleted status, and 60-bit cell id.
132struct VTKCOMMONDATAMODEL_EXPORT TaggedCellId
133{
134 // Encode a cell id and a VTK cell type (eg. VTK_TRIANGLE) into a
135 // vtkTypeUInt64.
136 static vtkTypeUInt64 Encode(vtkIdType cellId, VTKCellType type) noexcept
137 {
138 const size_t typeIndex = static_cast<size_t>(type);
139 return ((static_cast<vtkTypeUInt64>(cellId) & CELLID_MASK) |
140 (static_cast<vtkTypeUInt64>(TargetVarTable[typeIndex]) << 60));
141 }
142
143 TaggedCellId() noexcept = default;
144
145 // Create a TaggedCellId from a cellId and cell type (e.g. VTK_TRIANGLE).
146 TaggedCellId(vtkIdType cellId, VTKCellType cellType) noexcept
147 : Value(Encode(cellId, cellType))
148 {
149 }
150
151 TaggedCellId(const TaggedCellId&) noexcept = default;
152 TaggedCellId(TaggedCellId&&) noexcept = default;
153 TaggedCellId& operator=(const TaggedCellId&) noexcept = default;
154 TaggedCellId& operator=(TaggedCellId&&) noexcept = default;
155
156 // Get an enum value describing the internal vtkCellArray target used to
157 // store this cell.
158 Target GetTarget() const noexcept { return static_cast<Target>(this->Value & TARGET_MASK); }
159
160 // Get the VTK cell type value (eg. VTK_TRIANGLE) as a single byte.
161 unsigned char GetCellType() const noexcept { return TypeTable[this->GetTypeIndex()]; }
162
163 // Get the cell id used by the target vtkCellArray to store this cell.
164 vtkIdType GetCellId() const noexcept { return static_cast<vtkIdType>(this->Value & CELLID_MASK); }
165
166 // Update the cell id. Most useful with the CellMap::InsertNextCell(type)
167 // signature.
168 void SetCellId(vtkIdType cellId) noexcept
169 {
170 this->Value &= SHIFTED_TYPE_INDEX_MASK;
171 this->Value |= (static_cast<vtkTypeUInt64>(cellId) & CELLID_MASK);
172 }
173
174 // Mark this cell as deleted.
175 void MarkDeleted() noexcept { this->Value &= ~TYPE_VARIANT_MASK; }
176
177 // Returns true if the cell has been deleted.
178 bool IsDeleted() const noexcept { return (this->Value & TYPE_VARIANT_MASK) == 0; }
179
180private:
181 vtkTypeUInt64 Value;
182
183 // These shouldn't be needed outside of this struct. You're probably looking
184 // for GetCellType() instead.
185 TypeVariant GetTypeVariant() const noexcept
186 {
187 return static_cast<TypeVariant>(this->Value & TYPE_VARIANT_MASK);
188 }
189 std::size_t GetTypeIndex() const noexcept { return static_cast<std::size_t>(this->Value >> 60); }
190};
191
192// Thin wrapper around a std::vector<TaggedCellId> to allow shallow copying, etc
193class VTKCOMMONDATAMODEL_EXPORT CellMap : public vtkObject
194{
195public:
196 static CellMap* New();
197 vtkTypeMacro(CellMap, vtkObject);
198
199 static bool ValidateCellType(VTKCellType cellType) noexcept
200 {
201 // 1-9 excluding 8 (VTK_PIXEL):
202 return cellType > 0 && cellType <= 10 && cellType != VTK_PIXEL;
203 }
204
205 static bool ValidateCellId(vtkIdType cellId) noexcept
206 {
207 // is positive, won't truncate:
208 return (
209 (static_cast<vtkTypeUInt64>(cellId) & CELLID_MASK) == static_cast<vtkTypeUInt64>(cellId));
210 }
211
212 void DeepCopy(CellMap* other)
213 {
214 if (other)
215 {
216 this->Map = other->Map;
217 }
218 else
219 {
220 this->Map.clear();
221 }
222 }
223
224 void SetCapacity(vtkIdType numCells) { this->Map.reserve(static_cast<std::size_t>(numCells)); }
225
227 {
228 this->Map.resize(static_cast<std::size_t>(numCells));
229 }
230
231 TaggedCellId& GetTag(vtkIdType cellId) { return this->Map[static_cast<std::size_t>(cellId)]; }
232
233 const TaggedCellId& GetTag(vtkIdType cellId) const
234 {
235 return this->Map[static_cast<std::size_t>(cellId)];
236 }
237
238 // Caller must ValidateCellType first.
239 void InsertCell(vtkIdType globalCellId, vtkIdType cellId, VTKCellType cellType)
240 {
241 this->Map[globalCellId] = TaggedCellId(cellId, cellType);
242 }
243
244 // Caller must ValidateCellType and ValidateCellId first.
245 // useful for reusing the target lookup from cellType and then calling
246 // TaggedCellId::SetCellId later.
248 {
249 this->Map[globalCellId] = TaggedCellId(vtkIdType(0), cellType);
250 return this->Map[globalCellId];
251 }
252
253 // Caller must ValidateCellType first.
254 void InsertNextCell(vtkIdType cellId, VTKCellType cellType)
255 {
256 this->Map.emplace_back(cellId, cellType);
257 }
258
259 // Caller must ValidateCellType and ValidateCellId first.
260 // useful for reusing the target lookup from cellType and then calling
261 // TaggedCellId::SetCellId later.
263 {
264 this->Map.emplace_back(vtkIdType(0), cellType);
265 return this->Map.back();
266 }
267
268 vtkIdType GetNumberOfCells() const { return static_cast<vtkIdType>(this->Map.size()); }
269
270 void Reset() { this->Map.clear(); }
271
272 void Squeeze()
273 {
274 this->Map.shrink_to_fit(); // yaaaaay c++11
275 }
276
277 // In rounded-up kibibytes, as is VTK's custom:
278 unsigned long GetActualMemorySize() const
279 {
280 return static_cast<unsigned long>(sizeof(TaggedCellId) * this->Map.capacity() + 1023) / 1024;
281 }
282
283protected:
285 ~CellMap() override;
286
287 std::vector<TaggedCellId> Map;
288
289private:
290 CellMap(const CellMap&) = delete;
291 CellMap& operator=(const CellMap&) = delete;
292};
293
294VTK_ABI_NAMESPACE_END
295} // end namespace vtkPolyData_detail
296
297#endif // vtkPolyDataInternals.h
298
299// VTK-HeaderTest-Exclude: vtkPolyDataInternals.h
abstract base class for most VTK objects
Definition vtkObject.h:162
static bool ValidateCellId(vtkIdType cellId) noexcept
TaggedCellId & InsertCell(vtkIdType globalCellId, VTKCellType cellType)
unsigned long GetActualMemorySize() const
static CellMap * New()
void InsertCell(vtkIdType globalCellId, vtkIdType cellId, VTKCellType cellType)
void SetNumberOfCells(vtkIdType numCells)
void SetCapacity(vtkIdType numCells)
const TaggedCellId & GetTag(vtkIdType cellId) const
std::vector< TaggedCellId > Map
static bool ValidateCellType(VTKCellType cellType) noexcept
TaggedCellId & InsertNextCell(VTKCellType cellType)
void InsertNextCell(vtkIdType cellId, VTKCellType cellType)
TaggedCellId & GetTag(vtkIdType cellId)
static constexpr unsigned char TypeTable[16]
static constexpr vtkTypeUInt64 CELLID_MASK
static constexpr vtkTypeUInt64 SHIFTED_TYPE_INDEX_MASK
static constexpr unsigned char GenTargetVar(Target target, TypeVariant var) noexcept
static constexpr vtkTypeUInt64 TARGET_MASK
static constexpr vtkTypeUInt64 TYPE_VARIANT_MASK
static constexpr unsigned char TargetVarTable[10]
unsigned char GetCellType() const noexcept
TaggedCellId() noexcept=default
TaggedCellId(TaggedCellId &&) noexcept=default
vtkIdType GetCellId() const noexcept
void SetCellId(vtkIdType cellId) noexcept
TaggedCellId(const TaggedCellId &) noexcept=default
static vtkTypeUInt64 Encode(vtkIdType cellId, VTKCellType type) noexcept
boost::graph_traits< vtkGraph * >::vertex_descriptor target(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
VTKCellType
Definition vtkCellType.h:35
@ VTK_TRIANGLE_STRIP
Definition vtkCellType.h:43
@ VTK_PIXEL
Definition vtkCellType.h:45
@ VTK_POLY_LINE
Definition vtkCellType.h:41
@ VTK_TRIANGLE
Definition vtkCellType.h:42
@ VTK_POLYGON
Definition vtkCellType.h:44
@ VTK_EMPTY_CELL
Definition vtkCellType.h:37
@ VTK_LINE
Definition vtkCellType.h:40
@ VTK_QUAD
Definition vtkCellType.h:46
@ VTK_VERTEX
Definition vtkCellType.h:38
@ VTK_POLY_VERTEX
Definition vtkCellType.h:39
int vtkIdType
Definition vtkType.h:315