diff --git a/csg/src/plane.rs b/csg/src/plane.rs index ebaf997..0afc3f5 100644 --- a/csg/src/plane.rs +++ b/csg/src/plane.rs @@ -106,3 +106,90 @@ impl PlaneIntersection { .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)); + } +}