VTK-m  2.0
CellIntersector.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_Cell_Intersector_h
11 #define vtk_m_rendering_raytracing_Cell_Intersector_h
12 
13 #include <vtkm/CellShape.h>
16 
17 namespace vtkm
18 {
19 namespace rendering
20 {
21 namespace raytracing
22 {
23 //
24 // Any supported element. If the cell shape is not
25 // supported it does nothing, e.g. a 2D cellkk.
26 //
27 template <typename T>
28 VTKM_EXEC_CONT inline void IntersectZoo(T xpoints[8],
29  T ypoints[8],
30  T zpoints[8],
31  const vtkm::Vec<T, 3>& dir,
32  const vtkm::Vec<T, 3>& origin,
33  T distances[6],
34  const vtkm::Int32& shapeType)
35 {
36  // Some precalc for water tight intersections
39  WaterTight intersector;
40  intersector.FindDir(dir, s, k);
41  CellTables tables;
42  const vtkm::Int32 tableOffset = tables.ZooLookUp(tables.CellTypeLookUp(shapeType), 0);
43  const vtkm::Int32 numTriangles = tables.ZooLookUp(tables.CellTypeLookUp(shapeType), 1);
44  // Decompose each face into two triangles
45  for (int i = 0; i < 6; ++i)
46  distances[i] = -1.;
47  for (int i = 0; i < numTriangles; ++i)
48  {
49  const vtkm::Int32 offset = tableOffset + i;
50  vtkm::Vec<T, 3> a, c, b;
51  a[0] = xpoints[tables.ZooTable(offset, 1)];
52  a[1] = ypoints[tables.ZooTable(offset, 1)];
53  a[2] = zpoints[tables.ZooTable(offset, 1)];
54  b[0] = xpoints[tables.ZooTable(offset, 2)];
55  b[1] = ypoints[tables.ZooTable(offset, 2)];
56  b[2] = zpoints[tables.ZooTable(offset, 2)];
57  c[0] = xpoints[tables.ZooTable(offset, 3)];
58  c[1] = ypoints[tables.ZooTable(offset, 3)];
59  c[2] = zpoints[tables.ZooTable(offset, 3)];
60  const vtkm::Int32 faceId = tables.ZooTable(offset, 0);
61  T distance = -1.f;
62 
63  T uNotUsed, vNotUsed;
64  intersector.IntersectTriSn(a, b, c, s, k, distance, uNotUsed, vNotUsed, origin);
65 
66  if (distance != -1.f)
67  {
68  if (distances[faceId] != -1.f)
69  distances[faceId] = vtkm::Min(distance, distances[faceId]);
70  else
71  distances[faceId] = distance;
72  }
73  }
74 }
75 template <typename T>
76 VTKM_EXEC_CONT inline void IntersectHex(T xpoints[8],
77  T ypoints[8],
78  T zpoints[8],
79  const vtkm::Vec<T, 3>& dir,
80  const vtkm::Vec<T, 3>& origin,
81  T distances[6])
82 {
83  // Some precalc for water tight intersections
86  WaterTight intersector;
87  intersector.FindDir(dir, s, k);
88 
89  CellTables tables;
90  // Decompose each face into two triangles
91  for (int i = 0; i < 6; ++i)
92  {
93  vtkm::Vec<T, 3> a, c, b, d;
94  a[0] = xpoints[tables.ShapesFaceList(i, 1)];
95  a[1] = ypoints[tables.ShapesFaceList(i, 1)];
96  a[2] = zpoints[tables.ShapesFaceList(i, 1)];
97  b[0] = xpoints[tables.ShapesFaceList(i, 2)];
98  b[1] = ypoints[tables.ShapesFaceList(i, 2)];
99  b[2] = zpoints[tables.ShapesFaceList(i, 2)];
100  c[0] = xpoints[tables.ShapesFaceList(i, 3)];
101  c[1] = ypoints[tables.ShapesFaceList(i, 3)];
102  c[2] = zpoints[tables.ShapesFaceList(i, 3)];
103  d[0] = xpoints[tables.ShapesFaceList(i, 4)];
104  d[1] = ypoints[tables.ShapesFaceList(i, 4)];
105  d[2] = zpoints[tables.ShapesFaceList(i, 4)];
106  T distance = -1.f;
107  distances[i] = distance; //init to -1
108 
109  T uNotUsed, vNotUsed;
110  intersector.IntersectTriSn(a, b, c, s, k, distance, uNotUsed, vNotUsed, origin);
111 
112  if (distance != -1.f)
113  distances[i] = distance;
114 
115  distance = -1.f;
116 
117  intersector.IntersectTriSn(a, c, d, s, k, distance, uNotUsed, vNotUsed, origin);
118 
119 
120 
121  if (distance != -1.f)
122  {
123  if (distances[i] != -1.f)
124  distances[i] = vtkm::Min(distance, distances[i]);
125  else
126  distances[i] = distance;
127  }
128  }
129 }
130 template <typename T>
131 VTKM_EXEC_CONT inline void IntersectTet(T xpoints[8],
132  T ypoints[8],
133  T zpoints[8],
134  const vtkm::Vec<T, 3>& dir,
135  const vtkm::Vec<T, 3>& origin,
136  T distances[6])
137 {
138  // Some precalc for water tight intersections
139  vtkm::Vec<T, 3> s;
140  vtkm::Vec3i_32 k;
141  WaterTight intersector;
142  intersector.FindDir(dir, s, k);
143 
144  CellTables tables;
145  const vtkm::Int32 tableOffset = tables.FaceLookUp(tables.CellTypeLookUp(CELL_SHAPE_TETRA), 0);
146  for (vtkm::Int32 i = 0; i < 4; ++i)
147  {
148  vtkm::Vec<T, 3> a, c, b;
149  a[0] = xpoints[tables.ShapesFaceList(i + tableOffset, 1)];
150  a[1] = ypoints[tables.ShapesFaceList(i + tableOffset, 1)];
151  a[2] = zpoints[tables.ShapesFaceList(i + tableOffset, 1)];
152  b[0] = xpoints[tables.ShapesFaceList(i + tableOffset, 2)];
153  b[1] = ypoints[tables.ShapesFaceList(i + tableOffset, 2)];
154  b[2] = zpoints[tables.ShapesFaceList(i + tableOffset, 2)];
155  c[0] = xpoints[tables.ShapesFaceList(i + tableOffset, 3)];
156  c[1] = ypoints[tables.ShapesFaceList(i + tableOffset, 3)];
157  c[2] = zpoints[tables.ShapesFaceList(i + tableOffset, 3)];
158  T distance = -1.f;
159  distances[i] = distance; //init to -1
160 
161  T uNotUsed, vNotUsed;
162 
163  intersector.IntersectTriSn(a, b, c, s, k, distance, uNotUsed, vNotUsed, origin);
164 
165  if (distance != -1.f)
166  distances[i] = distance;
167  }
168 }
169 
170 //
171 // Wedge
172 //
173 template <typename T>
174 VTKM_EXEC_CONT inline void IntersectWedge(T xpoints[8],
175  T ypoints[8],
176  T zpoints[8],
177  const vtkm::Vec<T, 3>& dir,
178  const vtkm::Vec<T, 3>& origin,
179  T distances[6])
180 {
181  // Some precalc for water tight intersections
182  vtkm::Vec<T, 3> s;
183  vtkm::Vec3i_32 k;
184  WaterTight intersector;
185  intersector.FindDir(dir, s, k);
186  // TODO: try two sepate loops to see performance impact
187  CellTables tables;
188  const vtkm::Int32 tableOffset = tables.FaceLookUp(tables.CellTypeLookUp(CELL_SHAPE_WEDGE), 0);
189  // Decompose each face into two triangles
190  for (int i = 0; i < 5; ++i)
191  {
192  vtkm::Vec<T, 3> a, c, b, d;
193  a[0] = xpoints[tables.ShapesFaceList(i + tableOffset, 1)];
194  a[1] = ypoints[tables.ShapesFaceList(i + tableOffset, 1)];
195  a[2] = zpoints[tables.ShapesFaceList(i + tableOffset, 1)];
196  b[0] = xpoints[tables.ShapesFaceList(i + tableOffset, 2)];
197  b[1] = ypoints[tables.ShapesFaceList(i + tableOffset, 2)];
198  b[2] = zpoints[tables.ShapesFaceList(i + tableOffset, 2)];
199  c[0] = xpoints[tables.ShapesFaceList(i + tableOffset, 3)];
200  c[1] = ypoints[tables.ShapesFaceList(i + tableOffset, 3)];
201  c[2] = zpoints[tables.ShapesFaceList(i + tableOffset, 3)];
202  d[0] = xpoints[tables.ShapesFaceList(i + tableOffset, 4)];
203  d[1] = ypoints[tables.ShapesFaceList(i + tableOffset, 4)];
204  d[2] = zpoints[tables.ShapesFaceList(i + tableOffset, 4)];
205  T distance = -1.f;
206  distances[i] = distance; //init to -1
207 
208  T uNotUsed, vNotUsed;
209 
210  intersector.IntersectTriSn(a, b, c, s, k, distance, uNotUsed, vNotUsed, origin);
211 
212  if (distance != -1.f)
213  distances[i] = distance;
214  //
215  //First two faces are triangles
216  //
217  if (i < 2)
218  continue;
219  distance = -1.f;
220 
221  intersector.IntersectTriSn(a, c, d, s, k, distance, uNotUsed, vNotUsed, origin);
222 
223  if (distance != -1.f)
224  {
225  if (distances[i] != -1.f)
226  distances[i] = vtkm::Min(distance, distances[i]);
227  else
228  distances[i] = distance;
229  }
230  }
231 }
232 
233 //
234 // General Template should never be instantiated
235 //
236 
237 template <int CellType>
239 {
240 public:
241  template <typename T>
242  VTKM_EXEC_CONT inline void IntersectCell(T* vtkmNotUsed(xpoints),
243  T* vtkmNotUsed(ypoints),
244  T* vtkmNotUsed(zpoints),
245  const vtkm::Vec<T, 3>& vtkmNotUsed(dir),
246  const vtkm::Vec<T, 3>& vtkmNotUsed(origin),
247  T* vtkmNotUsed(distances),
248  const vtkm::UInt8 vtkmNotUsed(cellShape = 12));
249 };
250 
251 //
252 // Hex Specialization
253 //
254 template <>
256 {
257 public:
258  template <typename T>
259  VTKM_EXEC_CONT inline void IntersectCell(T* xpoints,
260  T* ypoints,
261  T* zpoints,
262  const vtkm::Vec<T, 3>& dir,
263  const vtkm::Vec<T, 3>& origin,
264  T* distances,
265  const vtkm::UInt8 cellShape = 12) const
266  {
267  if (cellShape == 12)
268  {
269  IntersectZoo(xpoints, ypoints, zpoints, dir, origin, distances, cellShape);
270  }
271  else
272  {
273  printf("CellIntersector Hex Error: unsupported cell type. Doing nothing\n");
274  }
275  }
276 };
277 //
278 //
279 // Hex Specialization Structured
280 //
281 template <>
282 class CellIntersector<254>
283 {
284 public:
285  template <typename T>
286  VTKM_EXEC_CONT inline void IntersectCell(T* xpoints,
287  T* ypoints,
288  T* zpoints,
289  const vtkm::Vec<T, 3>& dir,
290  const vtkm::Vec<T, 3>& origin,
291  T* distances,
292  const vtkm::UInt8 cellShape = 12) const
293  {
294  if (cellShape == 12)
295  {
296  IntersectZoo(xpoints, ypoints, zpoints, dir, origin, distances, cellShape);
297  }
298  else
299  {
300  printf("CellIntersector Hex Error: unsupported cell type. Doing nothing\n");
301  }
302  }
303 };
304 
305 //
306 // Tet Specialization
307 //
308 template <>
310 {
311 public:
312  template <typename T>
313  VTKM_EXEC_CONT inline void IntersectCell(T* xpoints,
314  T* ypoints,
315  T* zpoints,
316  const vtkm::Vec<T, 3>& dir,
317  const vtkm::Vec<T, 3>& origin,
318  T distances[6],
319  const vtkm::UInt8 cellShape = 12) const
320  {
321  if (cellShape == CELL_SHAPE_TETRA)
322  {
323  IntersectTet(xpoints, ypoints, zpoints, dir, origin, distances);
324  }
325  else
326  {
327  printf("CellIntersector Tet Error: unsupported cell type. Doing nothing\n");
328  }
329  }
330 };
331 
332 //
333 // Wedge Specialization
334 //
335 template <>
337 {
338 public:
339  template <typename T>
340  VTKM_EXEC_CONT inline void IntersectCell(T* xpoints,
341  T* ypoints,
342  T* zpoints,
343  const vtkm::Vec<T, 3>& dir,
344  const vtkm::Vec<T, 3>& origin,
345  T distances[6],
346  const vtkm::UInt8 cellShape = 12) const
347  {
348  if (cellShape == CELL_SHAPE_WEDGE)
349  {
350  IntersectWedge(xpoints, ypoints, zpoints, dir, origin, distances);
351  }
352  else
353  {
354  printf("CellIntersector Wedge Error: unsupported cell type. Doing nothing\n");
355  }
356  }
357 };
358 //
359 // Zoo elements
360 //
361 template <>
362 class CellIntersector<255>
363 {
364 public:
365  template <typename T>
366  VTKM_EXEC_CONT inline void IntersectCell(T* xpoints,
367  T* ypoints,
368  T* zpoints,
369  const vtkm::Vec<T, 3>& dir,
370  const vtkm::Vec<T, 3>& origin,
371  T distances[6],
372  const vtkm::UInt8 cellShape = 0) const
373  {
374  IntersectZoo(xpoints, ypoints, zpoints, dir, origin, distances, cellShape);
375  }
376 };
377 }
378 }
379 } // namespace vtkm::rendering::raytracing
380 #endif
vtkm::rendering::raytracing::CellTables::FaceLookUp
VTKM_EXEC vtkm::Int32 FaceLookUp(vtkm::Int32 x, vtkm::Int32 y) const
Definition: CellTables.h:48
vtkm::CELL_SHAPE_WEDGE
@ CELL_SHAPE_WEDGE
Definition: CellShape.h:49
vtkm::rendering::raytracing::CellIntersector< 254 >::IntersectCell
VTKM_EXEC_CONT void IntersectCell(T *xpoints, T *ypoints, T *zpoints, const vtkm::Vec< T, 3 > &dir, const vtkm::Vec< T, 3 > &origin, T *distances, const vtkm::UInt8 cellShape=12) const
Definition: CellIntersector.h:286
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::rendering::raytracing::CellIntersector
Definition: CellIntersector.h:238
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::rendering::raytracing::IntersectHex
VTKM_EXEC_CONT void IntersectHex(T xpoints[8], T ypoints[8], T zpoints[8], const vtkm::Vec< T, 3 > &dir, const vtkm::Vec< T, 3 > &origin, T distances[6])
Definition: CellIntersector.h:76
vtkm::rendering::raytracing::IntersectTet
VTKM_EXEC_CONT void IntersectTet(T xpoints[8], T ypoints[8], T zpoints[8], const vtkm::Vec< T, 3 > &dir, const vtkm::Vec< T, 3 > &origin, T distances[6])
Definition: CellIntersector.h:131
CellTables.h
vtkm::rendering::raytracing::CellIntersector< CELL_SHAPE_TETRA >::IntersectCell
VTKM_EXEC_CONT void IntersectCell(T *xpoints, T *ypoints, T *zpoints, const vtkm::Vec< T, 3 > &dir, const vtkm::Vec< T, 3 > &origin, T distances[6], const vtkm::UInt8 cellShape=12) const
Definition: CellIntersector.h:313
vtkm::rendering::raytracing::CellTables::CellTypeLookUp
VTKM_EXEC vtkm::Int32 CellTypeLookUp(vtkm::Int32 x) const
Definition: CellTables.h:25
CellShape.h
vtkm::rendering::raytracing::WaterTight::FindDir
VTKM_EXEC void FindDir(const vtkm::Vec< Precision, 3 > &dir, vtkm::Vec< Precision, 3 > &s, vtkm::Vec< Int32, 3 > &k) const
Definition: TriangleIntersections.h:81
vtkm::rendering::raytracing::CellTables
Definition: CellTables.h:22
vtkm::CELL_SHAPE_TETRA
@ CELL_SHAPE_TETRA
Definition: CellShape.h:46
vtkm::rendering::raytracing::CellTables::ZooTable
VTKM_EXEC vtkm::Int32 ZooTable(vtkm::Int32 x, vtkm::Int32 y) const
Definition: CellTables.h:104
vtkm::rendering::raytracing::CellIntersector< CELL_SHAPE_HEXAHEDRON >::IntersectCell
VTKM_EXEC_CONT void IntersectCell(T *xpoints, T *ypoints, T *zpoints, const vtkm::Vec< T, 3 > &dir, const vtkm::Vec< T, 3 > &origin, T *distances, const vtkm::UInt8 cellShape=12) const
Definition: CellIntersector.h:259
vtkm::rendering::raytracing::WaterTight::IntersectTriSn
VTKM_EXEC void IntersectTriSn(const vtkm::Vec< Precision, 3 > &a, const vtkm::Vec< Precision, 3 > &b, const vtkm::Vec< Precision, 3 > &c, const vtkm::Vec< Precision, 3 > &s, const vtkm::Vec< Int32, 3 > &k, Precision &distance, Precision &u, Precision &v, const vtkm::Vec< Precision, 3 > &origin) const
Definition: TriangleIntersections.h:227
vtkm::CELL_SHAPE_HEXAHEDRON
@ CELL_SHAPE_HEXAHEDRON
Definition: CellShape.h:48
vtkm::UInt8
uint8_t UInt8
Definition: Types.h:157
vtkm::rendering::raytracing::IntersectWedge
VTKM_EXEC_CONT void IntersectWedge(T xpoints[8], T ypoints[8], T zpoints[8], const vtkm::Vec< T, 3 > &dir, const vtkm::Vec< T, 3 > &origin, T distances[6])
Definition: CellIntersector.h:174
vtkm::rendering::raytracing::CellIntersector< CELL_SHAPE_WEDGE >::IntersectCell
VTKM_EXEC_CONT void IntersectCell(T *xpoints, T *ypoints, T *zpoints, const vtkm::Vec< T, 3 > &dir, const vtkm::Vec< T, 3 > &origin, T distances[6], const vtkm::UInt8 cellShape=12) const
Definition: CellIntersector.h:340
vtkm::rendering::raytracing::CellTables::ShapesFaceList
VTKM_EXEC vtkm::Int32 ShapesFaceList(vtkm::Int32 x, vtkm::Int32 y) const
Definition: CellTables.h:68
vtkmNotUsed
#define vtkmNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:128
vtkm::rendering::raytracing::WaterTight
Definition: TriangleIntersections.h:77
vtkm::Vec< T, 3 >
Definition: Types.h:975
vtkm::Vec< vtkm::Int32, 3 >
vtkm::rendering::raytracing::CellIntersector::IntersectCell
VTKM_EXEC_CONT void IntersectCell(T *vtkmNotUsed(xpoints), T *vtkmNotUsed(ypoints), T *vtkmNotUsed(zpoints), const vtkm::Vec< T, 3 > &vtkmNotUsed(dir), const vtkm::Vec< T, 3 > &vtkmNotUsed(origin), T *vtkmNotUsed(distances), const vtkm::UInt8 vtkmNotUsed(cellShape=12))
vtkm::rendering::raytracing::IntersectZoo
VTKM_EXEC_CONT void IntersectZoo(T xpoints[8], T ypoints[8], T zpoints[8], const vtkm::Vec< T, 3 > &dir, const vtkm::Vec< T, 3 > &origin, T distances[6], const vtkm::Int32 &shapeType)
Definition: CellIntersector.h:28
vtkm::Int32
int32_t Int32
Definition: Types.h:160
vtkm::rendering::raytracing::CellTables::ZooLookUp
VTKM_EXEC vtkm::Int32 ZooLookUp(vtkm::Int32 x, vtkm::Int32 y) const
Definition: CellTables.h:123
TriangleIntersections.h
vtkm::rendering::raytracing::CellIntersector< 255 >::IntersectCell
VTKM_EXEC_CONT void IntersectCell(T *xpoints, T *ypoints, T *zpoints, const vtkm::Vec< T, 3 > &dir, const vtkm::Vec< T, 3 > &origin, T distances[6], const vtkm::UInt8 cellShape=0) const
Definition: CellIntersector.h:366