Skip to content

Commit dad1c64

Browse files
committedSep 28, 2022
Slight cleanup + changed name of cook torrence
1 parent f0a964a commit dad1c64

File tree

6 files changed

+65
-67
lines changed

6 files changed

+65
-67
lines changed
 

‎implementations/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ rayon = "1.5.1"
1313
rt_core = {path = "../rt_core"}
1414

1515
[dev-dependencies]
16+
chrono = "0.4.19"
1617
statrs = "0.16.0"

‎implementations/src/materials/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use proc::Scatter;
22
use rt_core::{Float, Hit, Ray, Scatter, Vec3};
33

4-
pub mod cook_torrence;
54
pub mod emissive;
6-
pub mod lambertain;
5+
pub mod lambertian;
76
pub mod phong;
87
pub mod reflect;
98
pub mod refract;
9+
pub mod trowbridge_reitz;
1010

1111
pub use crate::{
1212
materials::{
13-
cook_torrence::CookTorrence, emissive::Emit, lambertain::Lambertian, phong::Phong,
14-
reflect::Reflect, refract::Refract,
13+
emissive::Emit, lambertian::Lambertian, phong::Phong, reflect::Reflect, refract::Refract,
14+
trowbridge_reitz::TrowbridgeReitz,
1515
},
1616
textures::Texture,
1717
};
@@ -21,7 +21,7 @@ pub enum AllMaterials<T: Texture> {
2121
Emit(Emit<T>),
2222
Lambertian(Lambertian<T>),
2323
Phong(Phong<T>),
24-
CookTorrence(CookTorrence<T>),
24+
TrowbridgeReitz(TrowbridgeReitz<T>),
2525
Reflect(Reflect<T>),
2626
Refract(Refract<T>),
2727
}

‎implementations/src/materials/cook_torrence.rs ‎implementations/src/materials/trowbridge_reitz.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rt_core::{Float, Hit, Ray, Scatter, Vec3};
88
use std::{f32::INFINITY, sync::Arc};
99

1010
#[derive(Debug)]
11-
pub struct CookTorrence<T: Texture> {
11+
pub struct TrowbridgeReitz<T: Texture> {
1212
pub texture: Arc<T>,
1313
pub alpha: Float,
1414
pub ior: Vec3,
@@ -21,7 +21,7 @@ use std::f64::consts::PI;
2121
#[cfg(not(feature = "f64"))]
2222
use std::f32::consts::PI;
2323

24-
impl<T> CookTorrence<T>
24+
impl<T> TrowbridgeReitz<T>
2525
where
2626
T: Texture,
2727
{
@@ -83,7 +83,7 @@ where
8383
}
8484
}
8585

86-
impl<T> Scatter for CookTorrence<T>
86+
impl<T> Scatter for TrowbridgeReitz<T>
8787
where
8888
T: Texture,
8989
{

‎implementations/tests/bxdf.rs

+40-15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
mod common;
2-
use common::bsdf_testing;
3-
4-
use rt_core::Hit;
5-
use rt_core::Ray;
1+
use crate::common::*;
2+
use rt_core::*;
63

7-
use rt_core::{Scatter, Vec3};
4+
mod common;
85

9-
fn test_bxdf<B>(bxdf: B) -> bool
6+
fn test_bxdf<B>(bxdf: B, bxdf_name: String) -> bool
107
where
118
B: Scatter,
129
{
@@ -26,27 +23,55 @@ where
2623
};
2724

2825
let pdf = |wo: Vec3, wi: Vec3| bxdf.scattering_pdf(&hit, wo, wi);
29-
bsdf_testing(&sample, &pdf)
26+
for i in 0..CHI_TESTS {
27+
let wo = generate_wo();
28+
let freq_table = samped_frequency_distribution(&sample, wo, THETA_RES, PHI_RES, SAMPLES);
29+
let expected_freq_table: Vec<Float> =
30+
integrate_frequency_table(&pdf, wo, THETA_RES, PHI_RES)
31+
.into_iter()
32+
.map(|x| x * SAMPLES as Float)
33+
.collect();
34+
if i == 0 {
35+
dump_tables(
36+
wo,
37+
&freq_table
38+
.iter()
39+
.map(|&v| v as Float)
40+
.collect::<Vec<Float>>(),
41+
&expected_freq_table,
42+
THETA_RES,
43+
PHI_RES,
44+
&bxdf_name,
45+
);
46+
}
47+
let (df, chi_squared) = chi_squared(freq_table, expected_freq_table, SAMPLES);
48+
let p = chi2_probability(df as f64, chi_squared as f64);
49+
let threshold = 1.0 - (1.0 - CHI2_THRESHOLD).powf(1.0 / CHI_TESTS as Float);
50+
if p < threshold as f64 || p.is_infinite() {
51+
return false;
52+
}
53+
}
54+
true
3055
}
3156

32-
#[test] // change name
33-
fn ggx_cook_torrence() {
34-
let bxdf = implementations::cook_torrence::CookTorrence::new(
57+
#[test]
58+
fn trowbridge_reitz() {
59+
let bxdf = implementations::trowbridge_reitz::TrowbridgeReitz::new(
3560
&std::sync::Arc::new(implementations::SolidColour::new(Vec3::new(0.0, 0.0, 0.0))),
3661
0.3,
3762
3.2 * Vec3::one(),
3863
1.0,
3964
);
4065

41-
assert!(test_bxdf(bxdf))
66+
assert!(test_bxdf(bxdf, "ggx".to_string()))
4267
}
4368

44-
#[test] // change name
69+
#[test]
4570
fn lambertian() {
46-
let bxdf = implementations::lambertain::Lambertian::new(
71+
let bxdf = implementations::lambertian::Lambertian::new(
4772
&std::sync::Arc::new(implementations::SolidColour::new(Vec3::new(0.0, 0.0, 0.0))),
4873
0.3,
4974
);
5075

51-
assert!(test_bxdf(bxdf))
76+
assert!(test_bxdf(bxdf, "lambertain".to_string()))
5277
}

‎implementations/tests/common/mod.rs

+16-44
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use std::{
99

1010
pub mod int;
1111

12-
const SAMPLES: usize = 10_000_000;
13-
const THETA_RES: usize = 80;
14-
const PHI_RES: usize = 2 * THETA_RES;
15-
const CHI2_THRESHOLD: Float = 0.01;
16-
const CHI_TESTS: usize = 1;
12+
pub const SAMPLES: usize = 10_000_000;
13+
pub const THETA_RES: usize = 80;
14+
pub const PHI_RES: usize = 2 * THETA_RES;
15+
pub const CHI2_THRESHOLD: Float = 0.01;
16+
pub const CHI_TESTS: usize = 1;
1717

1818
use int::*;
1919

@@ -29,7 +29,7 @@ pub fn to_vec(sin_theta: Float, cos_theta: Float, phi: Float) -> Vec3 {
2929
Vec3::new(phi.cos() * sin_theta, phi.sin() * sin_theta, cos_theta)
3030
}
3131

32-
fn chi2_probability(dof: f64, distance: f64) -> f64 {
32+
pub fn chi2_probability(dof: f64, distance: f64) -> f64 {
3333
assert!(
3434
(gamma_lr(dof * 0.5, distance * 0.5) + gamma_ur(dof * 0.5, distance * 0.5) - 1.0).abs()
3535
< 0.0001
@@ -122,7 +122,7 @@ pub fn random_float() -> Float {
122122
rng.gen()
123123
}
124124

125-
fn generate_wo() -> Vec3 {
125+
pub fn generate_wo() -> Vec3 {
126126
let cos_theta = random_float();
127127
let phi = TAU as Float * random_float();
128128

@@ -176,12 +176,13 @@ pub fn chi_squared(
176176
(df, chi_squared)
177177
}
178178

179-
fn dump_tables(
179+
pub fn dump_tables(
180180
wo: Vec3,
181181
freq_table: &[Float],
182182
expected_freq_table: &[Float],
183183
theta_res: usize,
184184
phi_res: usize,
185+
bxdf_name: &str,
185186
) {
186187
let enumerate = |file: &mut File, func: fn(Float, Float) -> Float| {
187188
(0..theta_res * phi_res).for_each(|index| {
@@ -198,7 +199,13 @@ fn dump_tables(
198199
});
199200
};
200201

201-
let mut file = File::create("chi_test.m").unwrap();
202+
let time = chrono::Local::now();
203+
204+
let mut file = File::create(format!(
205+
"chi_test_{bxdf_name}@{}.m",
206+
time.format("%Y-%m-%d:%H:%M")
207+
))
208+
.unwrap();
202209

203210
file.write_all(format!("% wo = {wo}\nfrequencies = [ ").as_bytes())
204211
.unwrap();
@@ -229,38 +236,3 @@ title('expected frequencies');",
229236
)
230237
.unwrap();
231238
}
232-
233-
pub fn bsdf_testing<S, P>(sample: &S, pdf: &P) -> bool
234-
where
235-
S: Fn(Vec3) -> Vec3,
236-
P: Fn(Vec3, Vec3) -> Float,
237-
{
238-
for i in 0..CHI_TESTS {
239-
let wo = generate_wo();
240-
let freq_table = samped_frequency_distribution(&sample, wo, THETA_RES, PHI_RES, SAMPLES);
241-
let expected_freq_table: Vec<Float> =
242-
integrate_frequency_table(pdf, wo, THETA_RES, PHI_RES)
243-
.into_iter()
244-
.map(|x| x * SAMPLES as Float)
245-
.collect();
246-
if i == 0 {
247-
dump_tables(
248-
wo,
249-
&freq_table
250-
.iter()
251-
.map(|&v| v as Float)
252-
.collect::<Vec<Float>>(),
253-
&expected_freq_table,
254-
THETA_RES,
255-
PHI_RES,
256-
);
257-
}
258-
let (df, chi_squared) = chi_squared(freq_table, expected_freq_table, SAMPLES);
259-
let p = chi2_probability(df as f64, chi_squared as f64);
260-
let threshold = 1.0 - (1.0 - CHI2_THRESHOLD).powf(1.0 / CHI_TESTS as Float);
261-
if p < threshold as f64 || p.is_infinite() {
262-
return false;
263-
}
264-
}
265-
true
266-
}

0 commit comments

Comments
 (0)
Please sign in to comment.