use super::*;
#[test]
fn test_y_intercept() {
let plane = Plane2 {
normal: Vec2(0.0, 1.0),
distance: -3.0,
};
assert_eq!(plane.y_intercept(), Some(3.0));
assert_eq!(plane.x_intercept(), None); }
#[test]
fn test_x_intercept() {
let plane = Plane2 {
normal: Vec2(2.0, 0.0),
distance: -4.0,
};
assert_eq!(plane.x_intercept(), Some(2.0));
assert_eq!(plane.y_intercept(), None); }
#[test]
fn test_intercept() {
let plane = Plane2 {
normal: Vec2(1.0, 1.0),
distance: -3.0,
};
assert_eq!(plane.y_intercept(), Some(3.0));
assert_eq!(plane.x_intercept(), Some(3.0));
}
#[test]
fn test_intersect_basic() {
let p1 = Plane2 {
normal: Vec2(1.0, 0.0),
distance: 0.0,
};
let p2 = Plane2 {
normal: Vec2(0.0, 1.0),
distance: 0.0,
};
let intersection = p1.intersect(p2).unwrap();
assert_eq!(intersection.x, 0.0);
assert_eq!(intersection.y, 0.0);
}
#[test]
fn test_intersect_parallel() {
let p1 = Plane2 {
normal: Vec2(1.0, 0.0),
distance: -1.0,
};
let p2 = Plane2 {
normal: Vec2(1.0, 0.0),
distance: -2.0,
};
assert!(p1.intersect(p2).is_none());
}
#[test]
fn test_intersect_random_planes() {
let mut rng = urandom::new();
let epsilon = f32::EPSILON as f64;
for _ in 0..1000 {
let x1 = rng.range(-256.0..256.0).round();
let y1 = rng.range(-256.0..256.0).round();
let x2 = rng.range(-256.0..256.0).round();
let y2 = rng.range(-256.0..256.0).round();
let d1 = rng.range(-256.0..256.0);
let d2 = rng.range(-256.0..256.0);
let norm1 = Vec2(x1, y1).norm();
let norm2 = Vec2(x2, y2).norm();
if norm1.cross(norm2).abs() < epsilon {
continue;
}
let plane1 = Plane2 {
normal: norm1,
distance: d1,
};
let plane2 = Plane2 {
normal: norm2,
distance: d2,
};
let intersection = plane1.intersect(plane2);
let p = intersection.expect("Planes should intersect");
let dist1 = plane1.distance(p);
let dist2 = plane2.distance(p);
assert!(dist1.abs() < epsilon, "Point {p} not on plane1 {plane1:?}: {dist1}");
assert!(dist2.abs() < epsilon, "Point {p} not on plane2 {plane2:?}: {dist2}");
}
}
#[test]
fn test_trace_random_planes() {
let mut rng = urandom::new();
let epsilon = f32::EPSILON as f64;
for _ in 0..1000 {
let normal = Vec2(rng.next_f64(), rng.next_f64()).norm();
let distance: f64 = rng.next();
let plane = Plane2 { normal, distance };
let origin = normal * (distance + rng.next_f64());
let direction = (normal + Vec2(rng.range(-0.5..0.5), rng.range(-0.5..0.5))).norm();
let mut ray = Ray2 { origin, direction, distance: Interval(0.0, f64::INFINITY) };
let hit = ray.trace(&plane);
assert!(hit.is_none(), "Ray should not hit the plane when moving away from it");
ray.direction = -ray.direction;
let hit = ray.trace(&plane).expect("Ray should hit the plane when moving towards it");
let d = plane.distance(hit.point);
assert!(d.abs() < epsilon, "Hit point should be on the plane: {d}");
}
}