VTK-m  2.2
TriangulateTables.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_worklet_internal_TriangulateTables_h
11 #define vtk_m_worklet_internal_TriangulateTables_h
12 
13 #include <vtkm/CellShape.h>
14 #include <vtkm/Types.h>
15 
17 
18 #include <vtkm/cont/ArrayHandle.h>
19 
20 namespace vtkm
21 {
22 namespace worklet
23 {
24 namespace internal
25 {
26 
27 using TriangulateArrayHandle =
29 
30 static vtkm::IdComponent TriangleCountData[vtkm::NUMBER_OF_CELL_SHAPES] = {
31  0, // 0 = vtkm::CELL_SHAPE_EMPTY_CELL
32  0, // 1 = vtkm::CELL_SHAPE_VERTEX
33  0, // 2 = vtkm::CELL_SHAPE_POLY_VERTEX
34  0, // 3 = vtkm::CELL_SHAPE_LINE
35  0, // 4 = vtkm::CELL_SHAPE_POLY_LINE
36  1, // 5 = vtkm::CELL_SHAPE_TRIANGLE
37  0, // 6 = vtkm::CELL_SHAPE_TRIANGLE_STRIP
38  -1, // 7 = vtkm::CELL_SHAPE_POLYGON
39  0, // 8 = vtkm::CELL_SHAPE_PIXEL
40  2, // 9 = vtkm::CELL_SHAPE_QUAD
41  0, // 10 = vtkm::CELL_SHAPE_TETRA
42  0, // 11 = vtkm::CELL_SHAPE_VOXEL
43  0, // 12 = vtkm::CELL_SHAPE_HEXAHEDRON
44  0, // 13 = vtkm::CELL_SHAPE_WEDGE
45  0 // 14 = vtkm::CELL_SHAPE_PYRAMID
46 };
47 
48 static vtkm::IdComponent TriangleOffsetData[vtkm::NUMBER_OF_CELL_SHAPES] = {
49  -1, // 0 = vtkm::CELL_SHAPE_EMPTY_CELL
50  -1, // 1 = vtkm::CELL_SHAPE_VERTEX
51  -1, // 2 = vtkm::CELL_SHAPE_POLY_VERTEX
52  -1, // 3 = vtkm::CELL_SHAPE_LINE
53  -1, // 4 = vtkm::CELL_SHAPE_POLY_LINE
54  0, // 5 = vtkm::CELL_SHAPE_TRIANGLE
55  -1, // 6 = vtkm::CELL_SHAPE_TRIANGLE_STRIP
56  -1, // 7 = vtkm::CELL_SHAPE_POLYGON
57  -1, // 8 = vtkm::CELL_SHAPE_PIXEL
58  1, // 9 = vtkm::CELL_SHAPE_QUAD
59  -1, // 10 = vtkm::CELL_SHAPE_TETRA
60  -1, // 11 = vtkm::CELL_SHAPE_VOXEL
61  -1, // 12 = vtkm::CELL_SHAPE_HEXAHEDRON
62  -1, // 13 = vtkm::CELL_SHAPE_WEDGE
63  -1 // 14 = vtkm::CELL_SHAPE_PYRAMID
64 };
65 
66 static vtkm::IdComponent TriangleIndexData[] = {
67  // vtkm::CELL_SHAPE_TRIANGLE
68  0,
69  1,
70  2,
71  // vtkm::CELL_SHAPE_QUAD
72  0,
73  1,
74  2,
75  0,
76  2,
77  3
78 };
79 
80 class TriangulateTablesExecutionObject
81 {
82 public:
83  using PortalType = TriangulateArrayHandle::ReadPortalType;
84 
85  VTKM_CONT
86  TriangulateTablesExecutionObject(const TriangulateArrayHandle& counts,
87  const TriangulateArrayHandle& offsets,
88  const TriangulateArrayHandle& indices,
90  vtkm::cont::Token& token)
91  : Counts(counts.PrepareForInput(device, token))
92  , Offsets(offsets.PrepareForInput(device, token))
93  , Indices(indices.PrepareForInput(device, token))
94  {
95  }
96 
97  template <typename CellShape>
98  VTKM_EXEC vtkm::IdComponent GetCount(CellShape shape, vtkm::IdComponent numPoints) const
99  {
100  if (shape.Id == vtkm::CELL_SHAPE_POLYGON)
101  {
102  return numPoints - 2;
103  }
104  else
105  {
106  return this->Counts.Get(shape.Id);
107  }
108  }
109 
110  template <typename CellShape>
111  VTKM_EXEC vtkm::IdComponent3 GetIndices(CellShape shape, vtkm::IdComponent triangleIndex) const
112  {
113  vtkm::IdComponent3 triIndices;
114  if (shape.Id == vtkm::CELL_SHAPE_POLYGON)
115  {
116  triIndices[0] = 0;
117  triIndices[1] = triangleIndex + 1;
118  triIndices[2] = triangleIndex + 2;
119  }
120  else
121  {
122  vtkm::IdComponent offset = 3 * (this->Offsets.Get(shape.Id) + triangleIndex);
123  triIndices[0] = this->Indices.Get(offset + 0);
124  triIndices[1] = this->Indices.Get(offset + 1);
125  triIndices[2] = this->Indices.Get(offset + 2);
126  }
127  return triIndices;
128  }
129 
130 private:
131  PortalType Counts;
132  PortalType Offsets;
133  PortalType Indices;
134 };
135 
136 class TriangulateTables : public vtkm::cont::ExecutionObjectBase
137 {
138 public:
139  VTKM_CONT TriangulateTablesExecutionObject PrepareForExecution(vtkm::cont::DeviceAdapterId device,
140  vtkm::cont::Token& token) const
141  {
142  return TriangulateTablesExecutionObject(
143  this->Counts, this->Offsets, this->Indices, device, token);
144  }
145 
146  VTKM_CONT
147  TriangulateTables()
148  : Counts(vtkm::cont::make_ArrayHandle(vtkm::worklet::internal::TriangleCountData,
150  vtkm::CopyFlag::Off))
151  , Offsets(vtkm::cont::make_ArrayHandle(vtkm::worklet::internal::TriangleOffsetData,
153  vtkm::CopyFlag::Off))
154  , Indices(vtkm::cont::make_ArrayHandle(vtkm::worklet::internal::TriangleIndexData,
155  vtkm::Id(9),
156  vtkm::CopyFlag::Off))
157  {
158  }
159 
160 private:
161  TriangulateArrayHandle Counts;
162  TriangulateArrayHandle Offsets;
163  TriangulateArrayHandle Indices;
164 };
165 
166 static vtkm::IdComponent TetrahedronCountData[vtkm::NUMBER_OF_CELL_SHAPES] = {
167  0, // 0 = vtkm::CELL_SHAPE_EMPTY_CELL
168  0, // 1 = vtkm::CELL_SHAPE_VERTEX
169  0, // 2 = vtkm::CELL_SHAPE_POLY_VERTEX
170  0, // 3 = vtkm::CELL_SHAPE_LINE
171  0, // 4 = vtkm::CELL_SHAPE_POLY_LINE
172  0, // 5 = vtkm::CELL_SHAPE_TRIANGLE
173  0, // 6 = vtkm::CELL_SHAPE_TRIANGLE_STRIP
174  0, // 7 = vtkm::CELL_SHAPE_POLYGON
175  0, // 8 = vtkm::CELL_SHAPE_PIXEL
176  0, // 9 = vtkm::CELL_SHAPE_QUAD
177  1, // 10 = vtkm::CELL_SHAPE_TETRA
178  0, // 11 = vtkm::CELL_SHAPE_VOXEL
179  5, // 12 = vtkm::CELL_SHAPE_HEXAHEDRON
180  3, // 13 = vtkm::CELL_SHAPE_WEDGE
181  2 // 14 = vtkm::CELL_SHAPE_PYRAMID
182 };
183 
184 static vtkm::IdComponent TetrahedronOffsetData[vtkm::NUMBER_OF_CELL_SHAPES] = {
185  -1, // 0 = vtkm::CELL_SHAPE_EMPTY_CELL
186  -1, // 1 = vtkm::CELL_SHAPE_VERTEX
187  -1, // 2 = vtkm::CELL_SHAPE_POLY_VERTEX
188  -1, // 3 = vtkm::CELL_SHAPE_LINE
189  -1, // 4 = vtkm::CELL_SHAPE_POLY_LINE
190  -1, // 5 = vtkm::CELL_SHAPE_TRIANGLE
191  -1, // 6 = vtkm::CELL_SHAPE_TRIANGLE_STRIP
192  -1, // 7 = vtkm::CELL_SHAPE_POLYGON
193  -1, // 8 = vtkm::CELL_SHAPE_PIXEL
194  -1, // 9 = vtkm::CELL_SHAPE_QUAD
195  0, // 10 = vtkm::CELL_SHAPE_TETRA
196  -1, // 11 = vtkm::CELL_SHAPE_VOXEL
197  1, // 12 = vtkm::CELL_SHAPE_HEXAHEDRON
198  6, // 13 = vtkm::CELL_SHAPE_WEDGE
199  9 // 14 = vtkm::CELL_SHAPE_PYRAMID
200 };
201 
202 static vtkm::IdComponent TetrahedronIndexData[] = {
203  // vtkm::CELL_SHAPE_TETRA
204  0,
205  1,
206  2,
207  3,
208  // vtkm::CELL_SHAPE_HEXAHEDRON
209  0,
210  1,
211  3,
212  4,
213  1,
214  4,
215  5,
216  6,
217  1,
218  4,
219  6,
220  3,
221  1,
222  3,
223  6,
224  2,
225  3,
226  6,
227  7,
228  4,
229  // vtkm::CELL_SHAPE_WEDGE
230  0,
231  1,
232  2,
233  4,
234  3,
235  4,
236  5,
237  2,
238  0,
239  2,
240  3,
241  4,
242  // vtkm::CELL_SHAPE_PYRAMID
243  0,
244  1,
245  2,
246  4,
247  0,
248  2,
249  3,
250  4
251 };
252 
253 class TetrahedralizeTablesExecutionObject
254 {
255 public:
256  using PortalType = typename TriangulateArrayHandle::ReadPortalType;
257  template <typename Device>
258  VTKM_CONT TetrahedralizeTablesExecutionObject PrepareForExecution(Device) const
259  {
260  return *this;
261  }
262 
263  VTKM_CONT
264  TetrahedralizeTablesExecutionObject(const TriangulateArrayHandle& counts,
265  const TriangulateArrayHandle& offsets,
266  const TriangulateArrayHandle& indices,
268  vtkm::cont::Token& token)
269  : Counts(counts.PrepareForInput(device, token))
270  , Offsets(offsets.PrepareForInput(device, token))
271  , Indices(indices.PrepareForInput(device, token))
272  {
273  }
274 
275  template <typename CellShape>
276  VTKM_EXEC vtkm::IdComponent GetCount(CellShape shape) const
277  {
278  return this->Counts.Get(shape.Id);
279  }
280 
281  template <typename CellShape>
282  VTKM_EXEC vtkm::IdComponent4 GetIndices(CellShape shape, vtkm::IdComponent tetrahedronIndex) const
283  {
284  vtkm::IdComponent4 tetIndices;
285  vtkm::IdComponent offset = 4 * (this->Offsets.Get(shape.Id) + tetrahedronIndex);
286  tetIndices[0] = this->Indices.Get(offset + 0);
287  tetIndices[1] = this->Indices.Get(offset + 1);
288  tetIndices[2] = this->Indices.Get(offset + 2);
289  tetIndices[3] = this->Indices.Get(offset + 3);
290  return tetIndices;
291  }
292 
293 private:
294  PortalType Counts;
295  PortalType Offsets;
296  PortalType Indices;
297 };
298 
299 class TetrahedralizeTables : public vtkm::cont::ExecutionObjectBase
300 {
301 public:
302  VTKM_CONT
303  TetrahedralizeTables()
304  : Counts(vtkm::cont::make_ArrayHandle(vtkm::worklet::internal::TetrahedronCountData,
306  vtkm::CopyFlag::Off))
307  , Offsets(vtkm::cont::make_ArrayHandle(vtkm::worklet::internal::TetrahedronOffsetData,
309  vtkm::CopyFlag::Off))
310  , Indices(vtkm::cont::make_ArrayHandle(vtkm::worklet::internal::TetrahedronIndexData,
311  vtkm::Id(44),
312  vtkm::CopyFlag::Off))
313  {
314  }
315 
316  VTKM_CONT TetrahedralizeTablesExecutionObject
317  PrepareForExecution(vtkm::cont::DeviceAdapterId device, vtkm::cont::Token& token) const
318  {
319  return TetrahedralizeTablesExecutionObject(
320  this->Counts, this->Offsets, this->Indices, device, token);
321  }
322 
323 private:
324  TriangulateArrayHandle Counts;
325  TriangulateArrayHandle Offsets;
326  TriangulateArrayHandle Indices;
327 };
328 
329 }
330 }
331 }
332 
333 #endif //vtk_m_worklet_internal_TriangulateTables_h
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:300
ArrayHandle.h
vtkm::NUMBER_OF_CELL_SHAPES
@ NUMBER_OF_CELL_SHAPES
Definition: CellShape.h:70
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
CellShape.h
vtkm::cont::ArrayHandle::ReadPortalType
typename StorageType::ReadPortalType ReadPortalType
The type of portal used when accessing data in a read-only mode.
Definition: ArrayHandle.h:312
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::cont::ExecutionObjectBase
Base ExecutionObjectBase for execution objects to inherit from so that you can use an arbitrary objec...
Definition: ExecutionObjectBase.h:31
vtkm::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:58
vtkm::Vec
A short fixed-length array.
Definition: Types.h:357
vtkm::cont::LogLevel::Off
@ Off
A placeholder used to silence all logging.
vtkm::CopyFlag
CopyFlag
Identifier used to specify whether a function should deep copy data.
Definition: Flags.h:17
vtkm::cont::make_ArrayHandle
vtkm::cont::ArrayHandleBasic< T > make_ArrayHandle(const T *array, vtkm::Id numberOfValues, vtkm::CopyFlag copy)
A convenience function for creating an ArrayHandle from a standard C array.
Definition: ArrayHandleBasic.h:270
vtkm::CELL_SHAPE_POLYGON
@ CELL_SHAPE_POLYGON
A general polygon shape.
Definition: CellShape.h:53
ExecutionObjectBase.h