Fix axis layout and implement click raytracing
This commit is contained in:
39
src/tests/mod.rs
Normal file
39
src/tests/mod.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use cgmath::{vec3};
|
||||
|
||||
use crate::game::player::{intersect_triangle};
|
||||
|
||||
fn epsilon_eq(f1: f32, f2: f32) {
|
||||
assert!(f32::abs(f1 - f2) < f32::EPSILON, "{} == {}", f1, f2);
|
||||
}
|
||||
|
||||
fn epsilon_eq_option(o1: Option<f32>, o2: Option<f32>) {
|
||||
if let Some(f1) = o1 {
|
||||
if let Some(f2) = o2 {
|
||||
epsilon_eq(f1, f2);
|
||||
} else {
|
||||
panic!("Some({}) == None", f1);
|
||||
}
|
||||
} else if let Some(f2) = o2 {
|
||||
panic!("None == Some({})", f2);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn triangle_intersection() {
|
||||
let zero = vec3(0.0, 0.0, 0.0);
|
||||
let a = [2.0, 0.5, 0.5];
|
||||
let b = [2.0, -0.5, 0.5];
|
||||
let c = [2.0, 0.0, -0.5];
|
||||
|
||||
let dist1 = intersect_triangle(zero, vec3(1.0, 0.0, 0.0), a, b, c);
|
||||
epsilon_eq_option(dist1, Some(2.0));
|
||||
|
||||
let dist2 = intersect_triangle(zero, vec3(0.0, 0.0, 1.0), a, b, c);
|
||||
epsilon_eq_option(dist2, None);
|
||||
|
||||
let dist3 = intersect_triangle(zero, vec3(0.9950371902, 0.09950371902, 0.0), a, b, c);
|
||||
epsilon_eq_option(dist3, Some(2.0099751242));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user