VTK-m  2.0
Ray.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_Ray_h
11 #define vtk_m_rendering_raytracing_Ray_h
12 
13 #include <vtkm/cont/ArrayHandle.h>
17 
18 #include <vector>
19 
20 #define RAY_ACTIVE 0
21 #define RAY_COMPLETE 1
22 #define RAY_TERMINATED 2
23 #define RAY_EXITED_MESH 3
24 #define RAY_EXITED_DOMAIN 4
25 #define RAY_LOST 5
26 #define RAY_ABANDONED 6
27 #define RAY_TUG_EPSILON 0.001
28 
29 namespace vtkm
30 {
31 namespace rendering
32 {
33 namespace raytracing
34 {
35 
36 template <typename Precision>
37 class Ray
38 {
39 protected:
41 
42 public:
43  // composite vectors to hold array handles
44  typename //tell the compiler we have a dependent type
49 
50  typename //tell the compiler we have a dependent type
55 
56  typename //tell the compiler we have a dependent type
61 
62  typename //tell the compiler we have a dependent type
66  Dir;
67 
71 
72 
76 
80 
81  vtkm::cont::ArrayHandle<Precision> U; //barycentric coordinates
87 
89 
92 
95  vtkm::cont::ArrayHandle<vtkm::UInt8> Status; // 0 = active 1 = miss 2 = lost
96 
97  std::vector<ChannelBuffer<Precision>> Buffers;
101 
102  VTKM_CONT
103  Ray()
104  {
105  IntersectionDataEnabled = false;
106  NumRays = 0;
107  Intersection =
112 
114  buffer.Resize(NumRays);
115  Buffers.push_back(buffer);
116  DebugWidth = -1;
117  DebugHeight = -1;
118  }
119 
120 
122  {
123  template <typename Device>
124  VTKM_CONT bool operator()(Device, Ray<Precision>* self)
125  {
127  self->EnableIntersectionData(Device());
128  return true;
129  }
130  };
131 
133 
134  template <typename Device>
136  {
138  {
139  return;
140  }
141 
142  vtkm::cont::Token token;
144  IntersectionX.PrepareForOutput(NumRays, Device(), token);
145  IntersectionY.PrepareForOutput(NumRays, Device(), token);
146  IntersectionZ.PrepareForOutput(NumRays, Device(), token);
147  U.PrepareForOutput(NumRays, Device(), token);
148  V.PrepareForOutput(NumRays, Device(), token);
149  Scalar.PrepareForOutput(NumRays, Device(), token);
150 
151  NormalX.PrepareForOutput(NumRays, Device(), token);
152  NormalY.PrepareForOutput(NumRays, Device(), token);
153  NormalZ.PrepareForOutput(NumRays, Device(), token);
154  }
155 
157  {
159  {
160  return;
161  }
162 
163  IntersectionDataEnabled = false;
170 
174  }
175 
176  template <typename Device>
177  VTKM_CONT Ray(const vtkm::Int32 size, Device, bool enableIntersectionData = false)
178  {
179  NumRays = size;
180  IntersectionDataEnabled = enableIntersectionData;
181 
183  this->Buffers.push_back(buffer);
184 
185  DebugWidth = -1;
186  DebugHeight = -1;
187 
188  this->Resize(size, Device());
189  }
190 
192  {
193  template <typename Device>
194  VTKM_CONT bool operator()(Device, Ray<Precision>* self, const vtkm::Int32 size)
195  {
197  self->Resize(size, Device());
198  return true;
199  }
200  };
201 
203 
204  template <typename Device>
205  VTKM_CONT void Resize(const vtkm::Int32 size, Device)
206  {
207  NumRays = size;
208  vtkm::cont::Token token;
209 
211  {
212  IntersectionX.PrepareForOutput(NumRays, Device(), token);
213  IntersectionY.PrepareForOutput(NumRays, Device(), token);
214  IntersectionZ.PrepareForOutput(NumRays, Device(), token);
215 
216  U.PrepareForOutput(NumRays, Device(), token);
217  V.PrepareForOutput(NumRays, Device(), token);
218 
219  Scalar.PrepareForOutput(NumRays, Device(), token);
220 
221  NormalX.PrepareForOutput(NumRays, Device(), token);
222  NormalY.PrepareForOutput(NumRays, Device(), token);
223  NormalZ.PrepareForOutput(NumRays, Device(), token);
224  }
225 
226  OriginX.PrepareForOutput(NumRays, Device(), token);
227  OriginY.PrepareForOutput(NumRays, Device(), token);
228  OriginZ.PrepareForOutput(NumRays, Device(), token);
229 
230  DirX.PrepareForOutput(NumRays, Device(), token);
231  DirY.PrepareForOutput(NumRays, Device(), token);
232  DirZ.PrepareForOutput(NumRays, Device(), token);
233 
234  Distance.PrepareForOutput(NumRays, Device(), token);
235 
236  MinDistance.PrepareForOutput(NumRays, Device(), token);
237  MaxDistance.PrepareForOutput(NumRays, Device(), token);
238  Status.PrepareForOutput(NumRays, Device(), token);
239 
240  HitIdx.PrepareForOutput(NumRays, Device(), token);
241  PixelIdx.PrepareForOutput(NumRays, Device(), token);
242 
243  Intersection =
248 
249  const size_t numBuffers = this->Buffers.size();
250  for (size_t i = 0; i < numBuffers; ++i)
251  {
252  this->Buffers[i].Resize(NumRays, Device());
253  }
254  }
255 
256  VTKM_CONT
257  void AddBuffer(const vtkm::Int32 numChannels, const std::string name)
258  {
259 
260  ChannelBuffer<Precision> buffer(numChannels, this->NumRays);
261  buffer.SetName(name);
262  this->Buffers.push_back(buffer);
263  }
264 
265  VTKM_CONT
266  bool HasBuffer(const std::string name)
267  {
268  size_t numBuffers = this->Buffers.size();
269  bool found = false;
270  for (size_t i = 0; i < numBuffers; ++i)
271  {
272  if (this->Buffers[i].GetName() == name)
273  {
274  found = true;
275  break;
276  }
277  }
278  return found;
279  }
280 
281  VTKM_CONT
282  ChannelBuffer<Precision>& GetBuffer(const std::string name)
283  {
284  const size_t numBuffers = this->Buffers.size();
285  bool found = false;
286  size_t index = 0;
287  for (size_t i = 0; i < numBuffers; ++i)
288  {
289  if (this->Buffers[i].GetName() == name)
290  {
291  found = true;
292  index = i;
293  }
294  }
295  if (found)
296  {
297  return this->Buffers.at(index);
298  }
299  else
300  {
301  throw vtkm::cont::ErrorBadValue("No channel buffer with requested name: " + name);
302  }
303  }
304 
305  void PrintRay(vtkm::Id pixelId)
306  {
307  for (vtkm::Id i = 0; i < NumRays; ++i)
308  {
309  if (PixelIdx.WritePortal().Get(i) == pixelId)
310  {
311  std::cout << "Ray " << pixelId << "\n";
312  std::cout << "Origin "
313  << "[" << OriginX.WritePortal().Get(i) << "," << OriginY.WritePortal().Get(i)
314  << "," << OriginZ.WritePortal().Get(i) << "]\n";
315  std::cout << "Dir "
316  << "[" << DirX.WritePortal().Get(i) << "," << DirY.WritePortal().Get(i) << ","
317  << DirZ.WritePortal().Get(i) << "]\n";
318  }
319  }
320  }
321 
322  friend class RayOperations;
323 }; // class ray
324 }
325 }
326 } //namespace vtkm::rendering::raytracing
327 #endif //vtk_m_rendering_raytracing_Ray_h
vtkm::rendering::raytracing::Ray::HasBuffer
VTKM_CONT bool HasBuffer(const std::string name)
Definition: Ray.h:266
vtkm::rendering::raytracing::Ray::EnableIntersectionDataFunctor
Definition: Ray.h:121
vtkm::rendering::raytracing::ChannelBuffer::Resize
void Resize(const vtkm::Id newSize)
vtkm::cont::ArrayHandle< Precision >
ArrayHandle.h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::rendering::raytracing::Ray::Status
vtkm::cont::ArrayHandle< vtkm::UInt8 > Status
Definition: Ray.h:95
ArrayHandleCast.h
vtkm::rendering::raytracing::Ray::EnableIntersectionData
void EnableIntersectionData()
Definition: Ray.h:132
vtkm::rendering::raytracing::ChannelBuffer::SetName
void SetName(const std::string name)
vtkm::rendering::raytracing::Ray::U
vtkm::cont::ArrayHandle< Precision > U
Definition: Ray.h:81
ChannelBuffer.h
vtkm::rendering::raytracing::Ray::NormalZ
vtkm::cont::ArrayHandle< Precision > NormalZ
Definition: Ray.h:85
ArrayHandleCompositeVector.h
vtkm::rendering::raytracing::Ray::DebugHeight
vtkm::Id DebugHeight
Definition: Ray.h:99
vtkm::rendering::raytracing::Ray::Scalar
vtkm::cont::ArrayHandle< Precision > Scalar
Definition: Ray.h:86
vtkm::rendering::raytracing::Ray::EnableIntersectionData
void EnableIntersectionData(Device)
Definition: Ray.h:135
vtkm::cont::ArrayHandleCompositeVector
An ArrayHandle that combines components from other arrays.
Definition: ArrayHandleCompositeVector.h:402
vtkm::rendering::raytracing::Ray::OriginZ
vtkm::cont::ArrayHandle< Precision > OriginZ
Definition: Ray.h:75
vtkm::rendering::raytracing::Ray::Ray
VTKM_CONT Ray(const vtkm::Int32 size, Device, bool enableIntersectionData=false)
Definition: Ray.h:177
vtkm::rendering::raytracing::Ray::IntersectionDataEnabled
bool IntersectionDataEnabled
Definition: Ray.h:40
vtkm::rendering::raytracing::Ray::Buffers
std::vector< ChannelBuffer< Precision > > Buffers
Definition: Ray.h:97
vtkm::rendering::raytracing::Ray::NormalY
vtkm::cont::ArrayHandle< Precision > NormalY
Definition: Ray.h:84
vtkm::rendering::raytracing::Ray::IntersectionX
vtkm::cont::ArrayHandle< Precision > IntersectionX
Definition: Ray.h:68
vtkm::rendering::raytracing::ChannelBuffer
Mananges a buffer that contains many channels per value (e.g., RGBA values).
Definition: ChannelBuffer.h:45
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::rendering::raytracing::Ray::V
vtkm::cont::ArrayHandle< Precision > V
Definition: Ray.h:82
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::rendering::raytracing::Ray::MinDistance
vtkm::cont::ArrayHandle< Precision > MinDistance
Definition: Ray.h:93
vtkm::rendering::raytracing::Ray
Definition: Ray.h:37
vtkm::cont::make_ArrayHandleCompositeVector
VTKM_CONT ArrayHandleCompositeVector< ArrayTs... > make_ArrayHandleCompositeVector(const ArrayTs &... arrays)
Create a composite vector array from other arrays.
Definition: ArrayHandleCompositeVector.h:430
vtkm::rendering::raytracing::Ray::DebugWidth
vtkm::Id DebugWidth
Definition: Ray.h:98
vtkm::rendering::raytracing::Ray::DirY
vtkm::cont::ArrayHandle< Precision > DirY
Definition: Ray.h:78
vtkm::rendering::raytracing::Ray::OriginX
vtkm::cont::ArrayHandle< Precision > OriginX
Definition: Ray.h:73
vtkm::rendering::raytracing::Ray::PixelIdx
vtkm::cont::ArrayHandle< vtkm::Id > PixelIdx
Definition: Ray.h:91
vtkm::rendering::raytracing::Ray::Normal
vtkm::cont::ArrayHandleCompositeVector< vtkm::cont::ArrayHandle< Precision >, vtkm::cont::ArrayHandle< Precision >, vtkm::cont::ArrayHandle< Precision > > Normal
Definition: Ray.h:54
vtkm::rendering::raytracing::RayOperations
Definition: RayOperations.h:106
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::rendering::raytracing::Ray::Resize
VTKM_CONT void Resize(const vtkm::Int32 size, Device)
Definition: Ray.h:205
vtkm::rendering::raytracing::Ray::OriginY
vtkm::cont::ArrayHandle< Precision > OriginY
Definition: Ray.h:74
vtkm::rendering::raytracing::Ray::HitIdx
vtkm::cont::ArrayHandle< vtkm::Id > HitIdx
Definition: Ray.h:90
vtkm::cont::ArrayHandle::WritePortal
VTKM_CONT WritePortalType WritePortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:435
vtkm::rendering::raytracing::Ray::IntersectionZ
vtkm::cont::ArrayHandle< Precision > IntersectionZ
Definition: Ray.h:70
vtkm::rendering::raytracing::Ray::Intersection
vtkm::cont::ArrayHandleCompositeVector< vtkm::cont::ArrayHandle< Precision >, vtkm::cont::ArrayHandle< Precision >, vtkm::cont::ArrayHandle< Precision > > Intersection
Definition: Ray.h:48
vtkm::rendering::raytracing::Ray::PrintRay
void PrintRay(vtkm::Id pixelId)
Definition: Ray.h:305
vtkm::rendering::raytracing::Ray::GetBuffer
VTKM_CONT ChannelBuffer< Precision > & GetBuffer(const std::string name)
Definition: Ray.h:282
vtkm::rendering::raytracing::Ray::Resize
VTKM_CONT void Resize(const vtkm::Int32 size)
Definition: Ray.h:202
vtkm::rendering::raytracing::Ray::Distance
vtkm::cont::ArrayHandle< Precision > Distance
Definition: Ray.h:88
vtkm::rendering::raytracing::Ray::Dir
vtkm::cont::ArrayHandleCompositeVector< vtkm::cont::ArrayHandle< Precision >, vtkm::cont::ArrayHandle< Precision >, vtkm::cont::ArrayHandle< Precision > > Dir
Definition: Ray.h:66
vtkm::rendering::raytracing::Ray::EnableIntersectionDataFunctor::operator()
VTKM_CONT bool operator()(Device, Ray< Precision > *self)
Definition: Ray.h:124
vtkm::rendering::raytracing::Ray::IntersectionY
vtkm::cont::ArrayHandle< Precision > IntersectionY
Definition: Ray.h:69
vtkm::rendering::raytracing::Ray::DisableIntersectionData
void DisableIntersectionData()
Definition: Ray.h:156
vtkm::rendering::raytracing::Ray::NumRays
vtkm::Id NumRays
Definition: Ray.h:100
vtkm::rendering::raytracing::Ray::DirX
vtkm::cont::ArrayHandle< Precision > DirX
Definition: Ray.h:77
vtkm::cont::ErrorBadValue
This class is thrown when a VTKm function or method encounters an invalid value that inhibits progres...
Definition: ErrorBadValue.h:25
vtkm::rendering::raytracing::Ray::ResizeFunctor
Definition: Ray.h:191
vtkm::cont::ArrayHandle::PrepareForOutput
VTKM_CONT WritePortalType PrepareForOutput(vtkm::Id numberOfValues, vtkm::cont::DeviceAdapterId device, vtkm::cont::Token &token) const
Prepares (allocates) this array to be used as an output from an operation in the execution environmen...
Definition: ArrayHandle.h:613
vtkm::Int32
int32_t Int32
Definition: Types.h:160
vtkm::rendering::raytracing::Ray::DirZ
vtkm::cont::ArrayHandle< Precision > DirZ
Definition: Ray.h:79
vtkm::rendering::raytracing::Ray::Ray
VTKM_CONT Ray()
Definition: Ray.h:103
vtkm::rendering::raytracing::Ray::ResizeFunctor::operator()
VTKM_CONT bool operator()(Device, Ray< Precision > *self, const vtkm::Int32 size)
Definition: Ray.h:194
vtkm::rendering::raytracing::Ray::AddBuffer
VTKM_CONT void AddBuffer(const vtkm::Int32 numChannels, const std::string name)
Definition: Ray.h:257
vtkm::rendering::raytracing::Ray::Origin
vtkm::cont::ArrayHandleCompositeVector< vtkm::cont::ArrayHandle< Precision >, vtkm::cont::ArrayHandle< Precision >, vtkm::cont::ArrayHandle< Precision > > Origin
Definition: Ray.h:60
vtkm::rendering::raytracing::Ray::NormalX
vtkm::cont::ArrayHandle< Precision > NormalX
Definition: Ray.h:83
vtkm::cont::ArrayHandle::ReleaseResources
VTKM_CONT void ReleaseResources() const
Releases all resources in both the control and execution environments.
Definition: ArrayHandle.h:559
vtkm::rendering::raytracing::Ray::MaxDistance
vtkm::cont::ArrayHandle< Precision > MaxDistance
Definition: Ray.h:94
vtkm::cont::TryExecute
VTKM_CONT bool TryExecute(Functor &&functor, Args &&... args)
Try to execute a functor on a set of devices until one succeeds.
Definition: TryExecute.h:244
VTKM_IS_DEVICE_ADAPTER_TAG
#define VTKM_IS_DEVICE_ADAPTER_TAG(tag)
Checks that the argument is a proper device adapter tag.
Definition: DeviceAdapterTag.h:164