Samplers-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Samplers-Impl.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Sampler functions for CubbyFlow.
6 > Created Time: 2017/06/21
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 
10 // Copyright (c) 1998-2014, Matt Pharr and Greg Humphreys.
11 // All rights reserved.
12 
13 // Redistribution and use in source and binary forms, with or without
14 // modification, are permitted provided that the following conditions are met:
15 
16 // Redistributions of source code must retain the above copyright notice, this
17 // list of conditions and the following disclaimer.
18 // Redistributions in binary form must reproduce the above copyright notice,
19 // this list of conditions and the following disclaimer in the documentation
20 // and/or other materials provided with the distribution.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 #ifndef CUBBYFLOW_SAMPLERS_IMPL_H
34 #define CUBBYFLOW_SAMPLERS_IMPL_H
35 
36 namespace CubbyFlow
37 {
38  template <typename T>
39  inline Vector3<T> UniformSampleCone(T u1, T u2, const Vector3<T>& axis, T angle)
40  {
41  T cosAngle_2 = std::cos(angle / 2);
42  T y = 1 - (1 - cosAngle_2) * u1;
43  T r = std::sqrt(std::max<T>(0, 1 - y * y));
44  T phi = static_cast<T>(2.0 * PI<T>()) * u2;
45  T x = r * std::cos(phi);
46  T z = r * std::sin(phi);
47  auto ts = axis.Tangential();
48 
49  return std::get<0>(ts) * x + axis * y + std::get<1>(ts) * z;
50  }
51 
52  template <typename T>
53  inline Vector3<T> UniformSampleHemisphere(T u1, T u2, const Vector3<T>& normal)
54  {
55  T y = u1;
56  T r = std::sqrt(std::max<T>(0, 1 - y * y));
57  T phi = static_cast<T>(2.0 * PI<T>()) * u2;
58  T x = r * std::cos(phi);
59  T z = r * std::sin(phi);
60  auto ts = normal.Tangential();
61 
62  return std::get<0>(ts) * x + normal * y + std::get<1>(ts) * z;
63  }
64 
65  template <typename T>
66  inline Vector3<T> CosineWeightedSampleHemisphere(T u1, T u2, const Vector3<T>& normal)
67  {
68  T phi = static_cast<T>(2.0 * PI<T>()) * u1;
69  T y = std::sqrt(u2);
70  T theta = std::acos(y);
71  T x = std::cos(phi) * std::sin(theta);
72  T z = std::sin(phi) * std::sin(theta);
73  Vector3<T> t = Tangential(normal);
74  auto ts = normal.Tangential();
75 
76  return std::get<0>(ts) * x + normal * y + std::get<1>(ts) * z;
77  }
78 
79  template <typename T>
80  inline Vector3<T> UniformSampleSphere(T u1, T u2)
81  {
82  T y = 1 - 2 * u1;
83  T r = std::sqrt(std::max<T>(0, 1 - y * y));
84  T phi = static_cast<T>(2.0 * PI<T>()) * u2;
85  T x = r * std::cos(phi);
86  T z = r * std::sin(phi);
87 
88  return Vector3<T>(x, y, z);
89  }
90 
91  template <typename T>
92  inline Vector2<T> UniformSampleDisk(T u1, T u2)
93  {
94  T r = std::sqrt(u1);
95  T theta = static_cast<T>(2.0 * PI<T>()) * u2;
96 
97  return Vector2<T>(r * std::cos(theta), r * std::sin(theta));
98  }
99 }
100 
101 #endif
3-D vector class.
Definition: Vector3.h:26
Vector3< T > UniformSampleSphere(T u1, T u2)
Returns randomly a point on a sphere.
Definition: Samplers-Impl.h:80
Vector3< T > CosineWeightedSampleHemisphere(T u1, T u2, const Vector3< T > &normal)
Returns weighted sampled point on a hemisphere.
Definition: Samplers-Impl.h:66
Definition: pybind11Utils.h:24
std::tuple< Vector< T, 3 >, Vector< T, 3 > > Tangential() const
Returns the tangential vector for this vector.
Definition: Vector3-Impl.h:352
2-D vector class.
Definition: Vector2.h:26
Vector2< T > UniformSampleDisk(T u1, T u2)
Returns randomly a point on a disk.
Definition: Samplers-Impl.h:92
Vector3< T > UniformSampleCone(T u1, T u2, const Vector3< T > &axis, T angle)
Returns randomly sampled direction within a cone.
Definition: Samplers-Impl.h:39
Vector3< T > UniformSampleHemisphere(T u1, T u2, const Vector3< T > &normal)
Returns randomly sampled point within a unit hemisphere.
Definition: Samplers-Impl.h:53