VTK-m  2.0
Sampler.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_Sampler_h
11 #define vtk_m_rendering_raytracing_Sampler_h
12 #include <vtkm/Math.h>
13 #include <vtkm/VectorAnalysis.h>
14 namespace vtkm
15 {
16 namespace rendering
17 {
18 namespace raytracing
19 {
20 
21 template <vtkm::Int32 Base>
22 VTKM_EXEC void Halton2D(const vtkm::Int32& sampleNum, vtkm::Vec2f_32& coord)
23 {
24  //generate base2 halton
25  vtkm::Float32 x = 0.0f;
26  vtkm::Float32 xadd = 1.0f;
27  vtkm::UInt32 b2 = 1 + static_cast<vtkm::UInt32>(sampleNum);
28  while (b2 != 0)
29  {
30  xadd *= 0.5f;
31  if ((b2 & 1) != 0)
32  x += xadd;
33  b2 >>= 1;
34  }
35 
36  vtkm::Float32 y = 0.0f;
37  vtkm::Float32 yadd = 1.0f;
38  vtkm::Int32 bn = 1 + sampleNum;
39  while (bn != 0)
40  {
41  yadd *= 1.0f / (vtkm::Float32)Base;
42  y += (vtkm::Float32)(bn % Base) * yadd;
43  bn /= Base;
44  }
45 
46  coord[0] = x;
47  coord[1] = y;
48 } // Halton2D
49 
52 {
53  //generate orthoganal basis about normal
54  int kz = 0;
55  if (vtkm::Abs(normal[0]) > vtkm::Abs(normal[1]))
56  {
57  if (vtkm::Abs(normal[0]) > vtkm::Abs(normal[2]))
58  kz = 0;
59  else
60  kz = 2;
61  }
62  else
63  {
64  if (vtkm::Abs(normal[1]) > vtkm::Abs(normal[2]))
65  kz = 1;
66  else
67  kz = 2;
68  }
69  vtkm::Vec3f_32 notNormal;
70  notNormal[0] = 0.f;
71  notNormal[1] = 0.f;
72  notNormal[2] = 0.f;
73  notNormal[kz] = 1.f;
74 
75  vtkm::Vec3f_32 xAxis = vtkm::Cross(normal, notNormal);
76  vtkm::Normalize(xAxis);
77  vtkm::Vec3f_32 yAxis = vtkm::Cross(normal, xAxis);
78  vtkm::Normalize(yAxis);
79 
80  vtkm::Vec2f_32 xy;
81  Halton2D<3>(sampleNum, xy);
82  const vtkm::Float32 r = Sqrt(xy[0]);
83  const vtkm::Float32 theta = 2 * static_cast<vtkm::Float32>(vtkm::Pi()) * xy[1];
84 
85  vtkm::Vec3f_32 direction(0.f, 0.f, 0.f);
86  direction[0] = r * vtkm::Cos(theta);
87  direction[1] = r * vtkm::Sin(theta);
88  direction[2] = vtkm::Sqrt(vtkm::Max(0.0f, 1.f - xy[0]));
89 
90  vtkm::Vec3f_32 sampleDir;
91  sampleDir[0] = vtkm::dot(direction, xAxis);
92  sampleDir[1] = vtkm::dot(direction, yAxis);
93  sampleDir[2] = vtkm::dot(direction, normal);
94  return sampleDir;
95 }
96 }
97 }
98 } // namespace vtkm::rendering::raytracing
99 #endif
vtkm::Sqrt
VTKM_EXEC_CONT vtkm::Float32 Sqrt(vtkm::Float32 x)
Compute the square root of x.
Definition: Math.h:958
vtkm::rendering::raytracing::CosineWeightedHemisphere
VTKM_EXEC vtkm::Vec3f_32 CosineWeightedHemisphere(const vtkm::Int32 &sampleNum, const vtkm::Vec3f_32 &normal)
Definition: Sampler.h:51
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::Cos
VTKM_EXEC_CONT vtkm::Float32 Cos(vtkm::Float32 x)
Compute the cosine of x.
Definition: Math.h:269
vtkm::Normalize
VTKM_EXEC_CONT void Normalize(T &x)
Changes a vector to be normal.
Definition: VectorAnalysis.h:168
vtkm::rendering::raytracing::Halton2D
VTKM_EXEC void Halton2D(const vtkm::Int32 &sampleNum, vtkm::Vec2f_32 &coord)
Definition: Sampler.h:22
vtkm::Sin
VTKM_EXEC_CONT vtkm::Float32 Sin(vtkm::Float32 x)
Compute the sine of x.
Definition: Math.h:210
VectorAnalysis.h
Math.h
vtkm::Cross
VTKM_EXEC_CONT vtkm::Vec< typename detail::FloatingPointReturnType< T >::Type, 3 > Cross(const vtkm::Vec< T, 3 > &x, const vtkm::Vec< T, 3 > &y)
Find the cross product of two vectors.
Definition: VectorAnalysis.h:177
vtkm::Vec< vtkm::Float32, 2 >
vtkm::UInt32
uint32_t UInt32
Definition: Types.h:161
vtkm::Float32
float Float32
Definition: Types.h:154
vtkm::Int32
int32_t Int32
Definition: Types.h:160