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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::f32;
use linalg::{self, Vector};
pub fn cos_sample_hemisphere(u: &(f32, f32)) -> Vector {
let d = concentric_sample_disk(u);
Vector::new(d.0, d.1, f32::sqrt(f32::max(0.0, 1.0 - d.0 * d.0 - d.1 * d.1)))
}
pub fn cos_hemisphere_pdf(cos_theta: f32) -> f32 { cos_theta * f32::consts::FRAC_1_PI }
pub fn concentric_sample_disk(u: &(f32, f32)) -> (f32, f32) {
let s = (2.0 * u.0 - 1.0, 2.0 * u.1 - 1.0);
let radius;
let theta;
if s.0 == 0.0 && s.1 == 0.0 {
return s;
}
if s.0 >= -s.1 {
if s.0 > s.1 {
radius = s.0;
if s.1 > 0.0 {
theta = s.1 / s.0;
} else {
theta = 8.0 + s.1 / s.0;
}
} else {
radius = s.1;
theta = 2.0 - s.0 / s.1;
}
} else if s.0 <= s.1 {
radius = -s.0;
theta = 4.0 + s.1 / s.0;
} else {
radius = -s.1;
theta = 6.0 - s.0 / s.1;
}
let theta = theta * f32::consts::FRAC_PI_4;
(radius * f32::cos(theta), radius * f32::sin(theta))
}
pub fn power_heuristic(n_f: f32, pdf_f: f32, n_g: f32, pdf_g: f32) -> f32 {
let f = n_f * pdf_f;
let g = n_g * pdf_g;
(f * f) / (f * f + g * g)
}
pub fn uniform_cone_pdf(cos_theta: f32) -> f32 {
1.0 / (f32::consts::PI * 2.0 * (1.0 - cos_theta))
}
pub fn uniform_sample_cone(samples: &(f32, f32), cos_theta_max: f32) -> Vector {
let cos_theta = linalg::lerp(samples.0, &cos_theta_max, &1.0);
let sin_theta = f32::sqrt(1.0 - cos_theta * cos_theta);
let phi = samples.1 * f32::consts::PI * 2.0;
Vector::new(f32::cos(phi) * sin_theta, f32::sin(phi) * sin_theta, cos_theta)
}
pub fn uniform_sample_cone_frame(samples: &(f32, f32), cos_theta_max: f32, w_x: &Vector,
w_y: &Vector, w_z: &Vector) -> Vector {
let cos_theta = linalg::lerp(samples.0, &cos_theta_max, &1.0);
let sin_theta = f32::sqrt(1.0 - cos_theta * cos_theta);
let phi = samples.1 * f32::consts::PI * 2.0;
f32::cos(phi) * sin_theta * *w_x + f32::sin(phi) * sin_theta * *w_y + cos_theta * *w_z
}
pub fn uniform_sample_sphere(samples: &(f32, f32)) -> Vector {
let z = 1.0 - 2.0 * samples.0;
let r = f32::sqrt(f32::max(0.0, 1.0 - z * z));
let phi = f32::consts::PI * 2.0 * samples.1;
Vector::new(f32::cos(phi) * r, f32::sin(phi) * r, z)
}