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