Add some basic tests to plane operations

This commit is contained in:
Jarrod Doyle 2023-09-18 21:48:11 +01:00
parent 8fa3c40240
commit 7de381d911
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 87 additions and 0 deletions

View File

@ -106,3 +106,90 @@ impl PlaneIntersection {
.transpose() .transpose()
} }
} }
#[cfg(test)]
mod plane_tests {
use super::*;
#[test]
fn from_point_normal() {
let point = glam::vec3(1.0, 1.0, 0.0);
let normal = glam::vec3(1.0, 1.0, 1.0);
let expected = Plane {
normal,
offset: 2.0,
};
assert_eq!(Plane::from_point_normal(point, normal), expected);
}
}
#[cfg(test)]
mod plane_intersection_tests {
use super::*;
#[test]
fn from_planes() {
let planes = vec![
Plane::from_point_normal(Vec3::X, Vec3::X),
Plane::from_point_normal(Vec3::Y, Vec3::Y),
Plane::from_point_normal(Vec3::Z, Vec3::Z),
Plane::from_point_normal(Vec3::NEG_X, Vec3::NEG_X),
];
let intersection1 = PlaneIntersection::from_planes(planes[0], planes[1], planes[2]);
let intersection2 = PlaneIntersection::from_planes(planes[0], planes[1], planes[3]);
assert!(intersection1.is_some());
assert!(intersection2.is_none());
}
#[test]
fn get_intersection_point() {
let planes = vec![
Plane::from_point_normal(Vec3::X, Vec3::X),
Plane::from_point_normal(Vec3::Y, Vec3::Y),
Plane::from_point_normal(Vec3::Z, Vec3::Z),
Plane::from_point_normal(glam::vec3(1.0, 1.0, 0.0), Vec3::ONE),
];
let intersection1 = PlaneIntersection::from_planes(planes[0], planes[1], planes[2]);
let intersection2 = PlaneIntersection::from_planes(planes[0], planes[1], planes[3]);
assert_eq!(intersection1.unwrap().get_intersection_point(), Vec3::ONE);
assert_eq!(
intersection2.unwrap().get_intersection_point(),
glam::vec3(1.0, 1.0, 0.0)
);
}
#[test]
fn in_half_space_mat() {
let intersection = PlaneIntersection::from_planes(
Plane::from_point_normal(Vec3::X, Vec3::X),
Plane::from_point_normal(Vec3::Y, Vec3::Y),
Plane::from_point_normal(Vec3::Z, Vec3::Z),
)
.unwrap();
let p1 = Plane::from_point_normal(Vec3::NEG_ONE, Vec3::NEG_ONE);
let p2 = Plane::from_point_normal(Vec3::ZERO, Vec3::ONE);
assert!(intersection.in_halfspace_mat(&p1));
assert!(!intersection.in_halfspace_mat(&p2));
}
#[test]
fn in_half_space() {
let intersection = PlaneIntersection::from_planes(
Plane::from_point_normal(Vec3::X, Vec3::X),
Plane::from_point_normal(Vec3::Y, Vec3::Y),
Plane::from_point_normal(Vec3::Z, Vec3::Z),
)
.unwrap();
let p1 = Plane::from_point_normal(Vec3::NEG_ONE, Vec3::NEG_ONE);
let p2 = Plane::from_point_normal(Vec3::ZERO, Vec3::ONE);
assert!(intersection.in_halfspace(&p1));
assert!(!intersection.in_halfspace(&p2));
}
}