VTK-m  2.0
MeshConnectivity.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_rendering_raytracing_MeshConnectivity
11 #define vtk_m_rendering_raytracing_MeshConnectivity
12 
13 #include <sstream>
14 #include <vtkm/CellShape.h>
16 #include <vtkm/exec/Variant.h>
22 
23 namespace vtkm
24 {
25 namespace rendering
26 {
27 namespace raytracing
28 {
29 
31 {
32 protected:
36 
37 public:
38  VTKM_CONT
39  MeshConnectivityStructured(const vtkm::Id3& cellDims, const vtkm::Id3& pointDims)
40  : CellDims(cellDims)
41  , PointDims(pointDims)
42  {
43  }
44 
46  vtkm::Id GetConnectingCell(const vtkm::Id& cellId, const vtkm::Id& face) const
47  {
48  //TODO: there is probably a better way to do this.
49  vtkm::Id3 logicalCellId;
50  logicalCellId[0] = cellId % CellDims[0];
51  logicalCellId[1] = (cellId / CellDims[0]) % CellDims[1];
52  logicalCellId[2] = cellId / (CellDims[0] * CellDims[1]);
53  if (face == 0)
54  logicalCellId[1] -= 1;
55  if (face == 2)
56  logicalCellId[1] += 1;
57  if (face == 1)
58  logicalCellId[0] += 1;
59  if (face == 3)
60  logicalCellId[0] -= 1;
61  if (face == 4)
62  logicalCellId[2] -= 1;
63  if (face == 5)
64  logicalCellId[2] += 1;
65  vtkm::Id nextCell =
66  (logicalCellId[2] * CellDims[1] + logicalCellId[1]) * CellDims[0] + logicalCellId[0];
67  bool validCell = true;
68  if (logicalCellId[0] >= CellDims[0])
69  validCell = false;
70  if (logicalCellId[1] >= CellDims[1])
71  validCell = false;
72  if (logicalCellId[2] >= CellDims[2])
73  validCell = false;
74  vtkm::Id minId = vtkm::Min(logicalCellId[0], vtkm::Min(logicalCellId[1], logicalCellId[2]));
75  if (minId < 0)
76  validCell = false;
77  if (!validCell)
78  nextCell = -1;
79  return nextCell;
80  }
81 
83  vtkm::Int32 GetCellIndices(vtkm::Id cellIndices[8], const vtkm::Id& cellIndex) const
84  {
85  vtkm::Id3 cellId;
86  cellId[0] = cellIndex % CellDims[0];
87  cellId[1] = (cellIndex / CellDims[0]) % CellDims[1];
88  cellId[2] = cellIndex / (CellDims[0] * CellDims[1]);
89  cellIndices[0] = (cellId[2] * PointDims[1] + cellId[1]) * PointDims[0] + cellId[0];
90  cellIndices[1] = cellIndices[0] + 1;
91  cellIndices[2] = cellIndices[1] + PointDims[0];
92  cellIndices[3] = cellIndices[2] - 1;
93  cellIndices[4] = cellIndices[0] + PointDims[0] * PointDims[1];
94  cellIndices[5] = cellIndices[4] + 1;
95  cellIndices[6] = cellIndices[5] + PointDims[0];
96  cellIndices[7] = cellIndices[6] - 1;
97  return 8;
98  }
99 
100  VTKM_EXEC
102  {
104  }
105 }; // MeshConnStructured
106 
108 {
109 protected:
112  using IdConstPortal = typename IdHandle::ReadPortalType;
113  using UCharConstPortal = typename UCharHandle::ReadPortalType;
114 
115  // Constant Portals for the execution Environment
116  //FaceConn
119  //Cell Set
123 
124 public:
125  VTKM_CONT
126  MeshConnectivityUnstructured(const IdHandle& faceConnectivity,
127  const IdHandle& faceOffsets,
128  const IdHandle& cellConn,
129  const IdHandle& cellOffsets,
130  const UCharHandle& shapes,
132  vtkm::cont::Token& token)
133  : FaceConnPortal(faceConnectivity.PrepareForInput(device, token))
134  , FaceOffsetsPortal(faceOffsets.PrepareForInput(device, token))
135  , CellConnPortal(cellConn.PrepareForInput(device, token))
136  , CellOffsetsPortal(cellOffsets.PrepareForInput(device, token))
137  , ShapesPortal(shapes.PrepareForInput(device, token))
138  {
139  }
140 
142  vtkm::Id GetConnectingCell(const vtkm::Id& cellId, const vtkm::Id& face) const
143  {
144  BOUNDS_CHECK(FaceOffsetsPortal, cellId);
145  vtkm::Id cellStartIndex = FaceOffsetsPortal.Get(cellId);
146  BOUNDS_CHECK(FaceConnPortal, cellStartIndex + face);
147  return FaceConnPortal.Get(cellStartIndex + face);
148  }
149 
150  //----------------------------------------------------------------------------
151  VTKM_EXEC
152  vtkm::Int32 GetCellIndices(vtkm::Id cellIndices[8], const vtkm::Id& cellId) const
153  {
154  const vtkm::Int32 shapeId = static_cast<vtkm::Int32>(ShapesPortal.Get(cellId));
155  CellTables tables;
156  const vtkm::Int32 numIndices = tables.FaceLookUp(tables.CellTypeLookUp(shapeId), 2);
157  BOUNDS_CHECK(CellOffsetsPortal, cellId);
158  const vtkm::Id cellOffset = CellOffsetsPortal.Get(cellId);
159 
160  for (vtkm::Int32 i = 0; i < numIndices; ++i)
161  {
162  BOUNDS_CHECK(CellConnPortal, cellOffset + i);
163  cellIndices[i] = CellConnPortal.Get(cellOffset + i);
164  }
165  return numIndices;
166  }
167 
168  //----------------------------------------------------------------------------
169  VTKM_EXEC
170  vtkm::UInt8 GetCellShape(const vtkm::Id& cellId) const
171  {
172  BOUNDS_CHECK(ShapesPortal, cellId);
173  return ShapesPortal.Get(cellId);
174  }
175 
176 }; // MeshConnUnstructured
177 
179 {
180 protected:
182  using IdConstPortal = typename IdHandle::ReadPortalType;
183 
185  using CountingPortal = typename CountingHandle::ReadPortalType;
186  // Constant Portals for the execution Environment
190 
194 
195 public:
196  VTKM_CONT
198  const IdHandle& cellConn,
199  const CountingHandle& cellOffsets,
200  vtkm::Int32 shapeId,
201  vtkm::Int32 numIndices,
202  vtkm::Int32 numFaces,
204  vtkm::cont::Token& token)
205  : FaceConnPortal(faceConn.PrepareForInput(device, token))
206  , CellConnectivityPortal(cellConn.PrepareForInput(device, token))
207  , CellOffsetsPortal(cellOffsets.PrepareForInput(device, token))
208  , ShapeId(shapeId)
209  , NumIndices(numIndices)
210  , NumFaces(numFaces)
211  {
212  }
213 
214  //----------------------------------------------------------------------------
215  // Execution Environment Methods
216  //----------------------------------------------------------------------------
217  VTKM_EXEC
218  vtkm::Id GetConnectingCell(const vtkm::Id& cellId, const vtkm::Id& face) const
219  {
221  vtkm::Id cellStartIndex = cellId * NumFaces;
222  BOUNDS_CHECK(FaceConnPortal, cellStartIndex + face);
223  return FaceConnPortal.Get(cellStartIndex + face);
224  }
225 
226  VTKM_EXEC
227  vtkm::Int32 GetCellIndices(vtkm::Id cellIndices[8], const vtkm::Id& cellId) const
228  {
230  const vtkm::Id cellOffset = CellOffsetsPortal.Get(cellId);
231 
232  for (vtkm::Int32 i = 0; i < NumIndices; ++i)
233  {
234  BOUNDS_CHECK(CellConnectivityPortal, cellOffset + i);
235  cellIndices[i] = CellConnectivityPortal.Get(cellOffset + i);
236  }
237 
238  return NumIndices;
239  }
240 
241  //----------------------------------------------------------------------------
242  VTKM_EXEC
244  {
245  return vtkm::UInt8(ShapeId);
246  }
247 
248 }; //MeshConn Single type specialization
249 
252 {
253  using ConnectivityType = vtkm::exec::
254  Variant<MeshConnectivityStructured, MeshConnectivityUnstructured, MeshConnectivitySingleType>;
256 
257 public:
258  // Constructor for structured connectivity
259  VTKM_CONT MeshConnectivity(const vtkm::Id3& cellDims, const vtkm::Id3& pointDims)
260  : Connectivity(MeshConnectivityStructured(cellDims, pointDims))
261  {
262  }
263 
264  // Constructor for unstructured connectivity
266  const vtkm::cont::ArrayHandle<vtkm::Id>& faceOffsets,
267  const vtkm::cont::ArrayHandle<vtkm::Id>& cellConn,
268  const vtkm::cont::ArrayHandle<vtkm::Id>& cellOffsets,
271  vtkm::cont::Token& token)
272  : Connectivity(MeshConnectivityUnstructured(faceConnectivity,
273  faceOffsets,
274  cellConn,
275  cellOffsets,
276  shapes,
277  device,
278  token))
279  {
280  }
281 
282  // Constructor for unstructured connectivity with single cell type
284  const vtkm::cont::ArrayHandle<vtkm::Id>& cellConn,
286  vtkm::Int32 shapeId,
287  vtkm::Int32 numIndices,
288  vtkm::Int32 numFaces,
290  vtkm::cont::Token& token)
291  : Connectivity(MeshConnectivitySingleType(faceConn,
292  cellConn,
293  cellOffsets,
294  shapeId,
295  numIndices,
296  numFaces,
297  device,
298  token))
299  {
300  }
301 
303  vtkm::Id GetConnectingCell(const vtkm::Id& cellId, const vtkm::Id& face) const
304  {
305  return this->Connectivity.CastAndCall(
306  [=](auto conn) { return conn.GetConnectingCell(cellId, face); });
307  }
308 
310  vtkm::Int32 GetCellIndices(vtkm::Id cellIndices[8], const vtkm::Id& cellId) const
311  {
312  return this->Connectivity.CastAndCall(
313  [=](auto conn) { return conn.GetCellIndices(cellIndices, cellId); });
314  }
315 
317  vtkm::UInt8 GetCellShape(const vtkm::Id& cellId) const
318  {
319  return this->Connectivity.CastAndCall([=](auto conn) { return conn.GetCellShape(cellId); });
320  }
321 };
322 
323 }
324 }
325 } //namespace vtkm::rendering::raytracing
326 
327 
328 #endif // MeshConnectivity
vtkm::rendering::raytracing::MeshConnectivityUnstructured::IdHandle
typename vtkm::cont::ArrayHandle< vtkm::Id > IdHandle
Definition: MeshConnectivity.h:110
vtkm::rendering::raytracing::MeshConnectivityStructured::CellDims
vtkm::Id3 CellDims
Definition: MeshConnectivity.h:34
vtkm::rendering::raytracing::CellTables::FaceLookUp
VTKM_EXEC vtkm::Int32 FaceLookUp(vtkm::Int32 x, vtkm::Int32 y) const
Definition: CellTables.h:48
vtkm::cont::ArrayHandle< vtkm::Id4 >
vtkm::rendering::raytracing::MeshConnectivityUnstructured::ShapesPortal
UCharConstPortal ShapesPortal
Definition: MeshConnectivity.h:122
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::rendering::raytracing::MeshConnectivitySingleType::CountingPortal
typename CountingHandle::ReadPortalType CountingPortal
Definition: MeshConnectivity.h:185
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::rendering::raytracing::MeshConnectivityStructured::Id4Handle
vtkm::cont::ArrayHandle< vtkm::Id4 > Id4Handle
Definition: MeshConnectivity.h:33
vtkm::rendering::raytracing::MeshConnectivityStructured::PointDims
vtkm::Id3 PointDims
Definition: MeshConnectivity.h:35
vtkm::rendering::raytracing::MeshConnectivityUnstructured::GetConnectingCell
VTKM_EXEC_CONT vtkm::Id GetConnectingCell(const vtkm::Id &cellId, const vtkm::Id &face) const
Definition: MeshConnectivity.h:142
CellTables.h
Variant.h
vtkm::rendering::raytracing::MeshConnectivity::ConnectivityType
vtkm::exec::Variant< MeshConnectivityStructured, MeshConnectivityUnstructured, MeshConnectivitySingleType > ConnectivityType
Definition: MeshConnectivity.h:254
vtkm::rendering::raytracing::MeshConnectivityUnstructured::FaceOffsetsPortal
IdConstPortal FaceOffsetsPortal
Definition: MeshConnectivity.h:118
vtkm::rendering::raytracing::CellTables::CellTypeLookUp
VTKM_EXEC vtkm::Int32 CellTypeLookUp(vtkm::Int32 x) const
Definition: CellTables.h:25
CellShape.h
vtkm::rendering::raytracing::MeshConnectivityUnstructured::FaceConnPortal
IdConstPortal FaceConnPortal
Definition: MeshConnectivity.h:117
vtkm::rendering::raytracing::MeshConnectivitySingleType::ShapeId
vtkm::Int32 ShapeId
Definition: MeshConnectivity.h:191
vtkm::rendering::raytracing::MeshConnectivityUnstructured::UCharConstPortal
typename UCharHandle::ReadPortalType UCharConstPortal
Definition: MeshConnectivity.h:113
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::rendering::raytracing::MeshConnectivity::GetCellShape
VTKM_EXEC_CONT vtkm::UInt8 GetCellShape(const vtkm::Id &cellId) const
Definition: MeshConnectivity.h:317
vtkm::rendering::raytracing::MeshConnectivitySingleType::FaceConnPortal
IdConstPortal FaceConnPortal
Definition: MeshConnectivity.h:187
vtkm::rendering::raytracing::MeshConnectivitySingleType::GetConnectingCell
VTKM_EXEC vtkm::Id GetConnectingCell(const vtkm::Id &cellId, const vtkm::Id &face) const
Definition: MeshConnectivity.h:218
vtkm::rendering::raytracing::CellTables
Definition: CellTables.h:22
vtkm::rendering::raytracing::MeshConnectivity::GetCellIndices
VTKM_EXEC_CONT vtkm::Int32 GetCellIndices(vtkm::Id cellIndices[8], const vtkm::Id &cellId) const
Definition: MeshConnectivity.h:310
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::rendering::raytracing::MeshConnectivity
General version of mesh connectivity that can be used for all supported mesh types.
Definition: MeshConnectivity.h:251
vtkm::rendering::raytracing::MeshConnectivitySingleType::IdConstPortal
typename IdHandle::ReadPortalType IdConstPortal
Definition: MeshConnectivity.h:182
Logger.h
vtkm::rendering::raytracing::MeshConnectivityUnstructured::CellConnPortal
IdConstPortal CellConnPortal
Definition: MeshConnectivity.h:120
vtkm::rendering::raytracing::MeshConnectivitySingleType::IdHandle
typename vtkm::cont::ArrayHandle< vtkm::Id > IdHandle
Definition: MeshConnectivity.h:181
vtkm::cont::ArrayHandleCounting< vtkm::Id >
vtkm::rendering::raytracing::MeshConnectivitySingleType::NumIndices
vtkm::Int32 NumIndices
Definition: MeshConnectivity.h:192
vtkm::rendering::raytracing::MeshConnectivityUnstructured::GetCellIndices
VTKM_EXEC vtkm::Int32 GetCellIndices(vtkm::Id cellIndices[8], const vtkm::Id &cellId) const
Definition: MeshConnectivity.h:152
vtkm::rendering::raytracing::MeshConnectivitySingleType::MeshConnectivitySingleType
VTKM_CONT MeshConnectivitySingleType(const IdHandle &faceConn, const IdHandle &cellConn, const CountingHandle &cellOffsets, vtkm::Int32 shapeId, vtkm::Int32 numIndices, vtkm::Int32 numFaces, vtkm::cont::DeviceAdapterId device, vtkm::cont::Token &token)
Definition: MeshConnectivity.h:197
vtkm::rendering::raytracing::MeshConnectivityStructured::MeshConnectivityStructured
VTKM_CONT MeshConnectivityStructured(const vtkm::Id3 &cellDims, const vtkm::Id3 &pointDims)
Definition: MeshConnectivity.h:39
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::rendering::raytracing::MeshConnectivitySingleType::CountingHandle
typename vtkm::cont::ArrayHandleCounting< vtkm::Id > CountingHandle
Definition: MeshConnectivity.h:184
vtkm::CELL_SHAPE_HEXAHEDRON
@ CELL_SHAPE_HEXAHEDRON
Definition: CellShape.h:48
vtkm::UInt8
uint8_t UInt8
Definition: Types.h:157
vtkmNotUsed
#define vtkmNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:128
BOUNDS_CHECK
#define BOUNDS_CHECK(HANDLE, INDEX)
Definition: RayTracingTypeDefs.h:31
vtkm::rendering::raytracing::MeshConnectivitySingleType::GetCellShape
VTKM_EXEC vtkm::UInt8 GetCellShape(const vtkm::Id &vtkmNotUsed(cellId)) const
Definition: MeshConnectivity.h:243
vtkm::rendering::raytracing::MeshConnectivitySingleType::NumFaces
vtkm::Int32 NumFaces
Definition: MeshConnectivity.h:193
vtkm::rendering::raytracing::MeshConnectivitySingleType
Definition: MeshConnectivity.h:178
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
vtkm::Vec< vtkm::Id, 3 >
vtkm::rendering::raytracing::MeshConnectivityUnstructured::CellOffsetsPortal
IdConstPortal CellOffsetsPortal
Definition: MeshConnectivity.h:121
vtkm::rendering::raytracing::MeshConnectivityUnstructured::IdConstPortal
typename IdHandle::ReadPortalType IdConstPortal
Definition: MeshConnectivity.h:112
vtkm::rendering::raytracing::MeshConnectivityStructured::GetCellIndices
VTKM_EXEC_CONT vtkm::Int32 GetCellIndices(vtkm::Id cellIndices[8], const vtkm::Id &cellIndex) const
Definition: MeshConnectivity.h:83
vtkm::rendering::raytracing::MeshConnectivityUnstructured::UCharHandle
typename vtkm::cont::ArrayHandle< vtkm::UInt8 > UCharHandle
Definition: MeshConnectivity.h:111
vtkm::rendering::raytracing::MeshConnectivityStructured::GetConnectingCell
VTKM_EXEC_CONT vtkm::Id GetConnectingCell(const vtkm::Id &cellId, const vtkm::Id &face) const
Definition: MeshConnectivity.h:46
vtkm::rendering::raytracing::MeshConnectivity::MeshConnectivity
VTKM_CONT MeshConnectivity(const vtkm::cont::ArrayHandle< vtkm::Id > &faceConnectivity, const vtkm::cont::ArrayHandle< vtkm::Id > &faceOffsets, const vtkm::cont::ArrayHandle< vtkm::Id > &cellConn, const vtkm::cont::ArrayHandle< vtkm::Id > &cellOffsets, const vtkm::cont::ArrayHandle< vtkm::UInt8 > &shapes, vtkm::cont::DeviceAdapterId device, vtkm::cont::Token &token)
Definition: MeshConnectivity.h:265
vtkm::rendering::raytracing::MeshConnectivityUnstructured::GetCellShape
VTKM_EXEC vtkm::UInt8 GetCellShape(const vtkm::Id &cellId) const
Definition: MeshConnectivity.h:170
ErrorBadValue.h
vtkm::rendering::raytracing::MeshConnectivityStructured
Definition: MeshConnectivity.h:30
vtkm::Int32
int32_t Int32
Definition: Types.h:160
vtkm::rendering::raytracing::MeshConnectivityUnstructured
Definition: MeshConnectivity.h:107
vtkm::rendering::raytracing::MeshConnectivity::MeshConnectivity
VTKM_CONT MeshConnectivity(const vtkm::Id3 &cellDims, const vtkm::Id3 &pointDims)
Definition: MeshConnectivity.h:259
vtkm::rendering::raytracing::MeshConnectivityUnstructured::MeshConnectivityUnstructured
VTKM_CONT MeshConnectivityUnstructured(const IdHandle &faceConnectivity, const IdHandle &faceOffsets, const IdHandle &cellConn, const IdHandle &cellOffsets, const UCharHandle &shapes, vtkm::cont::DeviceAdapterId device, vtkm::cont::Token &token)
Definition: MeshConnectivity.h:126
vtkm::rendering::raytracing::MeshConnectivity::GetConnectingCell
VTKM_EXEC_CONT vtkm::Id GetConnectingCell(const vtkm::Id &cellId, const vtkm::Id &face) const
Definition: MeshConnectivity.h:303
vtkm::rendering::raytracing::MeshConnectivitySingleType::GetCellIndices
VTKM_EXEC vtkm::Int32 GetCellIndices(vtkm::Id cellIndices[8], const vtkm::Id &cellId) const
Definition: MeshConnectivity.h:227
vtkm::rendering::raytracing::MeshConnectivitySingleType::CellConnectivityPortal
IdConstPortal CellConnectivityPortal
Definition: MeshConnectivity.h:188
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
vtkm::rendering::raytracing::MeshConnectivity::MeshConnectivity
VTKM_CONT MeshConnectivity(const vtkm::cont::ArrayHandle< vtkm::Id > &faceConn, const vtkm::cont::ArrayHandle< vtkm::Id > &cellConn, const vtkm::cont::ArrayHandleCounting< vtkm::Id > &cellOffsets, vtkm::Int32 shapeId, vtkm::Int32 numIndices, vtkm::Int32 numFaces, vtkm::cont::DeviceAdapterId device, vtkm::cont::Token &token)
Definition: MeshConnectivity.h:283
vtkm::rendering::raytracing::MeshConnectivity::Connectivity
ConnectivityType Connectivity
Definition: MeshConnectivity.h:255
TriangleIntersector.h
Ray.h
vtkm::rendering::raytracing::MeshConnectivitySingleType::CellOffsetsPortal
CountingPortal CellOffsetsPortal
Definition: MeshConnectivity.h:189
vtkm::rendering::raytracing::MeshConnectivityStructured::GetCellShape
VTKM_EXEC vtkm::UInt8 GetCellShape(const vtkm::Id &vtkmNotUsed(cellId)) const
Definition: MeshConnectivity.h:101
BoundingVolumeHierarchy.h