VTK  9.3.1
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 {
59 VTK_ABI_NAMESPACE_BEGIN
60 
61 static constexpr vtkTypeUInt64 CELLID_MASK = 0x0fffffffffffffffull;
62 static constexpr vtkTypeUInt64 SHIFTED_TYPE_INDEX_MASK = 0xf000000000000000ull;
63 static constexpr vtkTypeUInt64 TARGET_MASK = 0x3ull << 62;
64 static constexpr vtkTypeUInt64 TYPE_VARIANT_MASK = 0x3ull << 60;
65 
66 // Enumeration of internal cell array targets.
67 enum 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.
76 enum 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.
88 static 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.
109 static 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.
117 static 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
121  GenTargetVar(Target::Lines, TypeVariant::Var1), // 3 | VTK_LINE
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)
127  GenTargetVar(Target::Polys, TypeVariant::Var2), // 9 | VTK_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.
132 struct 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 
180 private:
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
193 class VTKCOMMONDATAMODEL_EXPORT CellMap : public vtkObject
194 {
195 public:
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.
247  TaggedCellId& InsertCell(vtkIdType globalCellId, VTKCellType cellType)
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 
283 protected:
284  CellMap();
285  ~CellMap() override;
286 
287  std::vector<TaggedCellId> Map;
288 
289 private:
290  CellMap(const CellMap&) = delete;
291  CellMap& operator=(const CellMap&) = delete;
292 };
293 
294 VTK_ABI_NAMESPACE_END
295 } // end namespace vtkPolyData_detail
296 
297 #endif // vtkPolyDataInternals.h
298 
299 // VTK-HeaderTest-Exclude: vtkPolyDataInternals.h
static constexpr unsigned char GenTargetVar(Target target, TypeVariant var) noexcept
void InsertCell(vtkIdType globalCellId, vtkIdType cellId, VTKCellType cellType)
const TaggedCellId & GetTag(vtkIdType cellId) const
abstract base class for most VTK objects
Definition: vtkObject.h:51
void SetCapacity(vtkIdType numCells)
static constexpr vtkTypeUInt64 CELLID_MASK
unsigned char GetCellType() const noexcept
static constexpr vtkTypeUInt64 TYPE_VARIANT_MASK
TaggedCellId & InsertNextCell(VTKCellType cellType)
static bool ValidateCellId(vtkIdType cellId) noexcept
void SetCellId(vtkIdType cellId) noexcept
int vtkIdType
Definition: vtkType.h:315
VTKCellType
Definition: vtkCellType.h:34
void InsertNextCell(vtkIdType cellId, VTKCellType cellType)
unsigned long GetActualMemorySize() const
void SetNumberOfCells(vtkIdType numCells)
boost::graph_traits< vtkGraph * >::vertex_descriptor target(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
TaggedCellId & GetTag(vtkIdType cellId)
static constexpr vtkTypeUInt64 TARGET_MASK
static vtkTypeUInt64 Encode(vtkIdType cellId, VTKCellType type) noexcept
std::vector< TaggedCellId > Map
static constexpr vtkTypeUInt64 SHIFTED_TYPE_INDEX_MASK
static constexpr unsigned char TargetVarTable[10]
static constexpr unsigned char TypeTable[16]
static bool ValidateCellType(VTKCellType cellType) noexcept
vtkIdType GetCellId() const noexcept
TaggedCellId & InsertCell(vtkIdType globalCellId, VTKCellType cellType)