VTK-m  2.0
CellEdge.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_exec_CellEdge_h
11 #define vtk_m_exec_CellEdge_h
12 
13 #include <vtkm/CellShape.h>
14 #include <vtkm/CellTraits.h>
15 #include <vtkm/ErrorCode.h>
16 #include <vtkm/Types.h>
17 #include <vtkm/exec/FunctorBase.h>
18 
19 namespace vtkm
20 {
21 namespace exec
22 {
23 
24 namespace detail
25 {
26 
27 class CellEdgeTables
28 {
29 public:
30  static constexpr vtkm::Int32 MAX_NUM_EDGES = 12;
31 
32 public:
33  VTKM_EXEC vtkm::Int32 NumEdges(vtkm::Int32 cellShapeId) const
34  {
36  // NumEdges
37  0, // 0: CELL_SHAPE_EMPTY
38  0, // 1: CELL_SHAPE_VERTEX
39  0, // 2: Unused
40  0, // 3: CELL_SHAPE_LINE
41  0, // 4: CELL_SHAPE_POLY_LINE
42  3, // 5: CELL_SHAPE_TRIANGLE
43  0, // 6: Unused
44  -1, // 7: CELL_SHAPE_POLYGON ---special case---
45  0, // 8: Unused
46  4, // 9: CELL_SHAPE_QUAD
47  6, // 10: CELL_SHAPE_TETRA
48  0, // 11: Unused
49  12, // 12: CELL_SHAPE_HEXAHEDRON
50  9, // 13: CELL_SHAPE_WEDGE
51  8 // 14: CELL_SHAPE_PYRAMID
52  };
53  return numEdges[cellShapeId];
54  }
55 
56  VTKM_EXEC vtkm::Int32 PointsInEdge(vtkm::Int32 cellShapeId,
57  vtkm::Int32 edgeIndex,
58  vtkm::Int32 localPointIndex) const
59  {
61  pointsInEdge[vtkm::NUMBER_OF_CELL_SHAPES][MAX_NUM_EDGES][2] = {
62  // clang-format off
63  // 0: CELL_SHAPE_EMPTY
64  { { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 },
65  { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } },
66  // 1: CELL_SHAPE_VERTEX
67  { { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 },
68  { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } },
69  // 2: Unused
70  { { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 },
71  { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } },
72  // 3: CELL_SHAPE_LINE
73  { { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 },
74  { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } },
75  // 4: CELL_SHAPE_POLY_LINE
76  { { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 },
77  { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } },
78  // 5: CELL_SHAPE_TRIANGLE
79  { { 0, 1 }, { 1, 2 }, { 2, 0 }, { -1, -1 }, { -1, -1 }, { -1, -1 },
80  { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } },
81  // 6: Unused
82  { { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 },
83  { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } },
84  // 7: CELL_SHAPE_POLYGON --- special case ---
85  { { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 },
86  { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } },
87  // 8: Unused
88  { { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 },
89  { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } },
90  // 9: CELL_SHAPE_QUAD
91  { { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 0 }, { -1, -1 }, { -1, -1 },
92  { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } },
93  // 10: CELL_SHAPE_TETRA
94  { { 0, 1 }, { 1, 2 }, { 2, 0 }, { 0, 3 }, { 1, 3 }, { 2, 3 },
95  { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } },
96  // 11: Unused
97  { { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 },
98  { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } },
99  // 12: CELL_SHAPE_HEXAHEDRON
100  { { 0, 1 }, { 1, 2 }, { 3, 2 }, { 0, 3 }, { 4, 5 }, { 5, 6 },
101  { 7, 6 }, { 4, 7 }, { 0, 4 }, { 1, 5 }, { 3, 7 }, { 2, 6 } },
102  // 13: CELL_SHAPE_WEDGE
103  { { 0, 1 }, { 1, 2 }, { 2, 0 }, { 3, 4 }, { 4, 5 }, { 5, 3 },
104  { 0, 3 }, { 1, 4 }, { 2, 5 }, { -1, -1 }, { -1, -1 }, { -1, -1 } },
105  // 14: CELL_SHAPE_PYRAMID
106  { { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 0 }, { 0, 4 }, { 1, 4 },
107  { 2, 4 }, { 3, 4 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } }
108  // clang-format on
109  };
110 
111  return pointsInEdge[cellShapeId][edgeIndex][localPointIndex];
112  }
113 };
114 
115 } // namespace detail
116 
117 template <typename CellShapeTag>
118 static inline VTKM_EXEC vtkm::ErrorCode CellEdgeNumberOfEdges(vtkm::IdComponent numPoints,
119  CellShapeTag,
120  vtkm::IdComponent& numEdges)
121 {
123  {
124  numEdges = -1;
126  }
127  numEdges = detail::CellEdgeTables{}.NumEdges(CellShapeTag::Id);
129 }
130 
131 static inline VTKM_EXEC vtkm::ErrorCode CellEdgeNumberOfEdges(vtkm::IdComponent numPoints,
132  vtkm::CellShapeTagPolygon,
133  vtkm::IdComponent& numEdges)
134 {
135  if (numPoints <= 0)
136  {
137  numEdges = -1;
139  }
140  numEdges = numPoints;
142 }
143 
144 static inline VTKM_EXEC vtkm::ErrorCode CellEdgeNumberOfEdges(vtkm::IdComponent numPoints,
145  vtkm::CellShapeTagPolyLine,
146  vtkm::IdComponent& numEdges)
147 {
148  if (numPoints <= 0)
149  {
150  numEdges = -1;
152  }
153  numEdges = detail::CellEdgeTables{}.NumEdges(vtkm::CELL_SHAPE_POLY_LINE);
155 }
156 
157 static inline VTKM_EXEC vtkm::ErrorCode CellEdgeNumberOfEdges(vtkm::IdComponent numPoints,
159  vtkm::IdComponent& numEdges)
160 {
161  if (shape.Id == vtkm::CELL_SHAPE_POLYGON)
162  {
163  return CellEdgeNumberOfEdges(numPoints, vtkm::CellShapeTagPolygon(), numEdges);
164  }
165  else if (shape.Id == vtkm::CELL_SHAPE_POLY_LINE)
166  {
167  return CellEdgeNumberOfEdges(numPoints, vtkm::CellShapeTagPolyLine(), numEdges);
168  }
169  else
170  {
171  numEdges = detail::CellEdgeTables{}.NumEdges(shape.Id);
173  }
174 }
175 
176 template <typename CellShapeTag>
177 static inline VTKM_EXEC vtkm::ErrorCode CellEdgeLocalIndex(vtkm::IdComponent numPoints,
178  vtkm::IdComponent pointIndex,
179  vtkm::IdComponent edgeIndex,
180  CellShapeTag shape,
181  vtkm::IdComponent& result)
182 {
183  if ((pointIndex < 0) || (pointIndex > 1))
184  {
185  result = -1;
187  }
188  if ((edgeIndex < 0) || (edgeIndex >= detail::CellEdgeTables::MAX_NUM_EDGES))
189  {
190  result = -1;
192  }
193 
194  vtkm::IdComponent numEdges;
195  VTKM_RETURN_ON_ERROR(vtkm::exec::CellEdgeNumberOfEdges(numPoints, shape, numEdges));
196  if (edgeIndex >= numEdges)
197  {
198  result = -1;
200  }
201 
202  detail::CellEdgeTables table;
203  result = table.PointsInEdge(CellShapeTag::Id, edgeIndex, pointIndex);
205 }
206 
207 static inline VTKM_EXEC vtkm::ErrorCode CellEdgeLocalIndex(vtkm::IdComponent numPoints,
208  vtkm::IdComponent pointIndex,
209  vtkm::IdComponent edgeIndex,
210  vtkm::CellShapeTagPolygon,
211  vtkm::IdComponent& result)
212 {
213  if (numPoints < 3)
214  {
215  result = -1;
217  }
218  if ((pointIndex < 0) || (pointIndex > 1))
219  {
220  result = -1;
222  }
223  if ((edgeIndex < 0) || (edgeIndex >= numPoints))
224  {
225  result = -1;
227  }
228 
229  if (edgeIndex + pointIndex < numPoints)
230  {
231  result = edgeIndex + pointIndex;
232  }
233  else
234  {
235  result = 0;
236  }
238 }
239 
240 static inline VTKM_EXEC vtkm::ErrorCode CellEdgeLocalIndex(vtkm::IdComponent numPoints,
241  vtkm::IdComponent pointIndex,
242  vtkm::IdComponent edgeIndex,
244  vtkm::IdComponent& result)
245 {
246  if ((pointIndex < 0) || (pointIndex > 1))
247  {
248  result = -1;
250  }
251  if ((edgeIndex < 0) || (edgeIndex >= detail::CellEdgeTables::MAX_NUM_EDGES))
252  {
253  result = -1;
255  }
256 
257  if (shape.Id == vtkm::CELL_SHAPE_POLYGON)
258  {
259  return CellEdgeLocalIndex(
260  numPoints, pointIndex, edgeIndex, vtkm::CellShapeTagPolygon(), result);
261  }
262  else
263  {
264  detail::CellEdgeTables table;
265  if (edgeIndex >= table.NumEdges(shape.Id))
266  {
267  result = -1;
269  }
270 
271  result = table.PointsInEdge(shape.Id, edgeIndex, pointIndex);
273  }
274 }
275 
282 template <typename CellShapeTag, typename GlobalPointIndicesVecType>
283 static inline VTKM_EXEC vtkm::ErrorCode CellEdgeCanonicalId(
284  vtkm::IdComponent numPoints,
285  vtkm::IdComponent edgeIndex,
286  CellShapeTag shape,
287  const GlobalPointIndicesVecType& globalPointIndicesVec,
288  vtkm::Id2& result)
289 {
290  result = { -1, -1 };
291 
292  vtkm::IdComponent localIndex0;
293  VTKM_RETURN_ON_ERROR(vtkm::exec::CellEdgeLocalIndex(numPoints, 0, edgeIndex, shape, localIndex0));
294  vtkm::Id pointIndex0 = globalPointIndicesVec[localIndex0];
295 
296  vtkm::IdComponent localIndex1;
297  VTKM_RETURN_ON_ERROR(vtkm::exec::CellEdgeLocalIndex(numPoints, 1, edgeIndex, shape, localIndex1));
298  vtkm::Id pointIndex1 = globalPointIndicesVec[localIndex1];
299 
300  if (pointIndex0 < pointIndex1)
301  {
302  result = vtkm::Id2(pointIndex0, pointIndex1);
303  }
304  else
305  {
306  result = vtkm::Id2(pointIndex1, pointIndex0);
307  }
308 
310 }
311 
312 }
313 } // namespace vtkm::exec
314 
315 #endif //vtk_m_exec_CellFaces_h
vtkm::ErrorCode
ErrorCode
Definition: ErrorCode.h:19
vtkm::NUMBER_OF_CELL_SHAPES
@ NUMBER_OF_CELL_SHAPES
Definition: CellShape.h:52
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
Represents a component ID (index of component in a vector).
Definition: Types.h:168
VTKM_STATIC_CONSTEXPR_ARRAY
#define VTKM_STATIC_CONSTEXPR_ARRAY
Definition: ExportMacros.h:107
vtkm::ErrorCode::Success
@ Success
vtkm::ErrorCode::InvalidEdgeId
@ InvalidEdgeId
CellShape.h
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
ErrorCode.h
vtkm::CELL_SHAPE_POLY_LINE
@ CELL_SHAPE_POLY_LINE
Definition: CellShape.h:40
FunctorBase.h
vtkm::ErrorCode::InvalidPointId
@ InvalidPointId
vtkm::Vec
A short fixed-length array.
Definition: Types.h:767
vtkm::CellShapeTagGeneric::Id
vtkm::UInt8 Id
Definition: CellShape.h:160
VTKM_RETURN_ON_ERROR
#define VTKM_RETURN_ON_ERROR(call)
Definition: ErrorCode.h:111
vtkm::Int32
int32_t Int32
Definition: Types.h:160
CellTraits.h
vtkm::CellShapeTagGeneric
A special cell shape tag that holds a cell shape that is not known at compile time.
Definition: CellShape.h:152
vtkm::CELL_SHAPE_POLYGON
@ CELL_SHAPE_POLYGON
Definition: CellShape.h:43
vtkm::CellTraits
Information about a cell based on its tag.
Definition: CellTraits.h:46
vtkm::ErrorCode::InvalidNumberOfPoints
@ InvalidNumberOfPoints
vtkm::Id2
vtkm::Vec< vtkm::Id, 2 > Id2
Id2 corresponds to a 2-dimensional index.
Definition: Types.h:885