-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpdf.h
More file actions
76 lines (63 loc) · 2.25 KB
/
pdf.h
File metadata and controls
76 lines (63 loc) · 2.25 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Copyright (C) 2017-2026 Topological Manifold
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <src/com/exponent.h>
#include <cstddef>
#include <type_traits>
namespace ns::sampling
{
template <std::size_t N, typename T>
T reflected_pdf(const T pdf, const T cosine)
{
static_assert(N >= 2);
static_assert(std::is_floating_point_v<T>);
// reflected_pdf<2> * pow(sin(angle) / sin(reflected), N - 2)
// reflected_pdf<2> = pdf / 2
// cos(angle) = cosine
// cos(reflected) = 2 * square(cosine) - 1
// sin(angle) = sqrt(1 - square(cosine))
// sin(reflected) = sqrt(1 - square(2 * square(cosine) - 1))
// sin(reflected) = sqrt(4 * square(cosine) * (1 - square(cosine)))
// sin_ratio = 1 / sqrt(4 * square(cosine)) = 1 / (2 * cosine)
// pdf / (2 * pow(2 * cosine, N - 2))
// pdf / (pow(2, N - 1) * pow(cosine, N - 2))
if (cosine > 0)
{
return pdf / ((1 << (N - 1)) * power<N - 2>(cosine));
}
return 0;
}
template <std::size_t N, typename T>
T area_pdf_to_solid_angle_pdf(const T pdf, const T cosine, const T distance)
{
static_assert(N >= 2);
static_assert(std::is_floating_point_v<T>);
if (cosine > 0)
{
return pdf * power<N - 1>(distance) / cosine;
}
return 0;
}
template <std::size_t N, typename T>
T solid_angle_pdf_to_area_pdf(const T pdf, const T cosine, const T distance)
{
static_assert(N >= 2);
static_assert(std::is_floating_point_v<T>);
if (cosine > 0)
{
return pdf * cosine / power<N - 1>(distance);
}
return 0;
}
}