1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkCellTypes.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15 /**
16 * @class vtkCellTypes
17 * @brief object provides direct access to cells in vtkCellArray and type information
18 *
19 * This class is a supplemental object to vtkCellArray to allow random access
20 * into cells as well as representing cell type information. The "location"
21 * field is the location in the vtkCellArray list in terms of an integer
22 * offset. An integer offset was used instead of a pointer for easy storage
23 * and inter-process communication. The type information is defined in the
24 * file vtkCellType.h.
25 *
26 * @warning
27 * Sometimes this class is used to pass type information independent of the
28 * random access (i.e., location) information. For example, see
29 * vtkDataSet::GetCellTypes(). If you use the class in this way, you can use
30 * a location value of -1.
31 *
32 * @sa
33 * vtkCellArray vtkCellLinks
34 */
35
36 #ifndef vtkCellTypes_h
37 #define vtkCellTypes_h
38
39 #include "vtkCommonDataModelModule.h" // For export macro
40 #include "vtkObject.h"
41
42 #include "vtkCellType.h" // Needed for VTK_EMPTY_CELL
43 #include "vtkIdTypeArray.h" // Needed for inline methods
44 #include "vtkIntArray.h" // Needed for inline methods
45 #include "vtkUnsignedCharArray.h" // Needed for inline methods
46
47 class VTKCOMMONDATAMODEL_EXPORT vtkCellTypes : public vtkObject
48 {
49 public:
50 static vtkCellTypes* New();
51 vtkTypeMacro(vtkCellTypes, vtkObject);
52 void PrintSelf(ostream& os, vtkIndent indent) override;
53
54 /**
55 * Allocate memory for this array. Delete old storage only if necessary.
56 */
57 int Allocate(vtkIdType sz = 512, vtkIdType ext = 1000);
58
59 /**
60 * Add a cell at specified id.
61 */
62 void InsertCell(vtkIdType id, unsigned char type, vtkIdType loc);
63
64 /**
65 * Add a cell to the object in the next available slot.
66 */
67 vtkIdType InsertNextCell(unsigned char type, vtkIdType loc);
68
69 /**
70 * Specify a group of cell types.
71 */
72 void SetCellTypes(
73 vtkIdType ncells, vtkUnsignedCharArray* cellTypes, vtkIdTypeArray* cellLocations);
74
75 /**
76 * Specify a group of cell types. This version is provided to maintain
77 * backwards compatibility and does a copy of the cellLocations
78 */
79 void SetCellTypes(vtkIdType ncells, vtkUnsignedCharArray* cellTypes, vtkIntArray* cellLocations);
80
81 /**
82 * Return the location of the cell in the associated vtkCellArray.
83 */
GetCellLocation(vtkIdType cellId)84 vtkIdType GetCellLocation(vtkIdType cellId) { return this->LocationArray->GetValue(cellId); }
85
86 /**
87 * Delete cell by setting to nullptr cell type.
88 */
DeleteCell(vtkIdType cellId)89 void DeleteCell(vtkIdType cellId) { this->TypeArray->SetValue(cellId, VTK_EMPTY_CELL); }
90
91 /**
92 * Return the number of types in the list.
93 */
GetNumberOfTypes()94 vtkIdType GetNumberOfTypes() { return (this->MaxId + 1); }
95
96 /**
97 * Return 1 if type specified is contained in list; 0 otherwise.
98 */
99 int IsType(unsigned char type);
100
101 /**
102 * Add the type specified to the end of the list. Range checking is performed.
103 */
InsertNextType(unsigned char type)104 vtkIdType InsertNextType(unsigned char type) { return this->InsertNextCell(type, -1); }
105
106 /**
107 * Return the type of cell.
108 */
GetCellType(vtkIdType cellId)109 unsigned char GetCellType(vtkIdType cellId) { return this->TypeArray->GetValue(cellId); }
110
111 /**
112 * Reclaim any extra memory.
113 */
114 void Squeeze();
115
116 /**
117 * Initialize object without releasing memory.
118 */
119 void Reset();
120
121 /**
122 * Return the memory in kibibytes (1024 bytes) consumed by this cell type array.
123 * Used to support streaming and reading/writing data. The value
124 * returned is guaranteed to be greater than or equal to the memory
125 * required to actually represent the data represented by this object.
126 * The information returned is valid only after the pipeline has
127 * been updated.
128 */
129 unsigned long GetActualMemorySize();
130
131 /**
132 * Standard DeepCopy method. Since this object contains no reference
133 * to other objects, there is no ShallowCopy.
134 */
135 void DeepCopy(vtkCellTypes* src);
136
137 /**
138 * Given an int (as defined in vtkCellType.h) identifier for a class
139 * return it's classname.
140 */
141 static const char* GetClassNameFromTypeId(int typeId);
142
143 /**
144 * Given a data object classname, return it's int identified (as
145 * defined in vtkCellType.h)
146 */
147 static int GetTypeIdFromClassName(const char* classname);
148
149 /**
150 * This convenience method is a fast check to determine if a cell type
151 * represents a linear or nonlinear cell. This is generally much more
152 * efficient than getting the appropriate vtkCell and checking its IsLinear
153 * method.
154 */
155 static int IsLinear(unsigned char type);
156
157 ///@{
158 /**
159 * Methods for obtaining the arrays representing types and locations.
160 */
GetCellTypesArray()161 vtkUnsignedCharArray* GetCellTypesArray() { return this->TypeArray; }
GetCellLocationsArray()162 vtkIdTypeArray* GetCellLocationsArray() { return this->LocationArray; }
163 ///@}
164
165 protected:
166 vtkCellTypes();
167 ~vtkCellTypes() override;
168
169 vtkUnsignedCharArray* TypeArray; // pointer to types array
170 vtkIdTypeArray* LocationArray; // pointer to array of offsets
171 vtkIdType Size; // allocated size of data
172 vtkIdType MaxId; // maximum index inserted thus far
173 vtkIdType Extend; // grow array by this point
174
175 private:
176 vtkCellTypes(const vtkCellTypes&) = delete;
177 void operator=(const vtkCellTypes&) = delete;
178 };
179
180 //----------------------------------------------------------------------------
IsType(unsigned char type)181 inline int vtkCellTypes::IsType(unsigned char type)
182 {
183 vtkIdType numTypes = this->GetNumberOfTypes();
184
185 for (vtkIdType i = 0; i < numTypes; i++)
186 {
187 if (type == this->GetCellType(i))
188 {
189 return 1;
190 }
191 }
192 return 0;
193 }
194
195 //-----------------------------------------------------------------------------
IsLinear(unsigned char type)196 inline int vtkCellTypes::IsLinear(unsigned char type)
197 {
198 return ((type <= 20) || (type == VTK_CONVEX_POINT_SET) || (type == VTK_POLYHEDRON));
199 }
200
201 #endif
202