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