-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampling.hpp
47 lines (37 loc) · 1.31 KB
/
sampling.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#pragma once
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#pragma region
#include "vector.hpp"
#pragma endregion
//-----------------------------------------------------------------------------
// Declarations and Definitions
//-----------------------------------------------------------------------------
namespace pathtracing
{
inline const Vector3 UniformSampleOnSphere(
double u1, double u2) noexcept
{
const double cos_theta = 1.0 - 2.0 * u1;
const double sin_theta = sqrt(std::max(0.0, 1.0 - cos_theta * cos_theta));
const double phi = 2.0 * g_pi * u2;
return Vector3(cos(phi) * sin_theta, sin(phi) * sin_theta, cos_theta);
}
inline const Vector3 UniformSampleOnHemisphere(
double u1, double u2) noexcept
{
// u1 := cos_theta
const double sin_theta = sqrt(std::max(0.0, 1.0 - u1 * u1));
const double phi = 2.0 * g_pi * u2;
return Vector3(cos(phi) * sin_theta, sin(phi) * sin_theta, u1);
}
inline const Vector3 CosineWeightedSampleOnHemisphere(
double u1, double u2) noexcept
{
const double cos_theta = sqrt(1.0 - u1);
const double sin_theta = sqrt(u1);
const double phi = 2.0 * g_pi * u2;
return Vector3(cos(phi) * sin_theta, sin(phi) * sin_theta, cos_theta);
}
} // namespace pathtracing