Files
physics/tests/geometry.sakura
T
2026-11-14 15:41:09 -06:00

179 lines
10 KiB
Plaintext

#! Contains the tests of `geometry`: planes, and which side of one something
#! lies on; boxes and spheres holding places and meeting; rays meeting each
#! shape; a camera's frustum reaching what it sees; and the same bits whether
#! numbers are worked as lanes or one at a time, which `malunal.geometry.tests`
#! and `malunal.geometry.lanes.tests` each check.
import std::testing;
import std::json;
import std::math;
import malunal::linear;
import malunal::geometry;
# Whether two numbers are within a millionth or so of one another.
def near(a: f32, b: f32): bool {
return math::abs(a - b) < 1e-5;
}
def near(a: vec3, b: vec3): bool {
return near(a.x, b.x) && near(a.y, b.y) && near(a.z, b.z);
}
# How far along a ray it meets a shape, or -1 where it doesn't.
def hit_at(found: maybe<f32>): f32 {
return found is some(t) ? t : -1;
}
@test def lays_out_its_shapes(t: &mut tester): void {
t.expects(size_of::<ray>() == 32 && size_of::<plane>() == 20 && size_of::<bounds>() == 32 && size_of::<sphere>() == 20);
t.expects(size_of::<frustum>() == 128 && align_of::<frustum>() == 4 && align_of::<bounds>() == 4);
}
@test def writes_and_reads_its_shapes(t: &mut tester): void {
# Each is written by its fields' names, its vectors as `linear` writes them.
val box := bounds::around(vec3{ 1, 2, 3 }, vec3{ 0.5, 0.5, 0.5 });
val text := json::to_text(&box) else (_) {
t.expects(false);
return;
};
t.expects(text.span == "{\"min\":{\"x\":0.5,\"y\":1.5,\"z\":2.5},\"max\":{\"x\":1.5,\"y\":2.5,\"z\":3.5}}");
t.expects(json::from_text::<bounds>(text.span).value == box);
val ball := sphere{ vec3{ 0, 1, 0 }, 0.75 };
t.expects(json::from_text::<sphere>(json::to_text(&ball).value.span).value == ball);
val floor := plane::through(vec3{ 0, 2, 0 }, vec3::up);
t.expects(json::from_text::<plane>(json::to_text(&floor).value.span).value == floor);
val look := ray::toward(vec3::zero, vec3{ 0, 0, -4 });
t.expects(json::from_text::<ray>(json::to_text(&look).value.span).value == look);
}
@test def measures_planes(t: &mut tester): void {
val floor := plane::through(vec3{ 0, 2, 0 }, vec3{ 0, 5, 0 });
t.expects(floor.normal == vec3::up && floor.distance == 2);
t.expects(floor.distance_to(vec3{ 3, 5, -1 }) == 3 && floor.distance_to(vec3::zero) == -2);
t.expects(floor.closest(vec3{ 3, 5, -1 }) == vec3{ 3, 2, -1 } && floor.flipped().distance_to(vec3::zero) == 2);
# Turning counterclockwise as seen from +z, it faces +z.
val wall := plane::of_points(vec3{ 0, 0, 1 }, vec3{ 1, 0, 1 }, vec3{ 0, 1, 1 });
t.expects(wall.normal == vec3::back && wall.distance == 1);
t.expects(floor.side_of(bounds::around(vec3{ 0, 4, 0 }, vec3::one)) == side::front);
t.expects(floor.side_of(bounds::around(vec3{ 0, 2.5, 0 }, vec3::one)) == side::across);
t.expects(floor.side_of(bounds::around(vec3{ 5, -1, 5 }, vec3::one)) == side::back);
# A sphere touching it is on both sides.
t.expects(floor.side_of(sphere{ vec3::zero, 2 }) == side::across && floor.side_of(sphere{ vec3{ 0, -1, 0 }, 2 }) == side::back);
}
@test def holds_and_meets_boxes_and_spheres(t: &mut tester): void {
val b := bounds{ vec3{ -1, 0, -1 }, vec3{ 1, 2, 1 } };
t.expects(b.center == vec3{ 0, 1, 0 } && b.size == vec3{ 2, 2, 2 } && b.half_size == vec3::one);
t.expects(b.contains(vec3{ 1, 2, 1 }) && !b.contains(vec3{ 1, 2.5, 1 }));
t.expects(b.contains(bounds::around(b.center, vec3{ 0.5, 0.5, 0.5 })) && !b.contains(bounds::around(b.center, vec3{ 2, 0.5, 0.5 })));
# Touching at a corner, they meet.
t.expects(b.overlaps(bounds{ vec3{ 1, 2, 1 }, vec3{ 3, 3, 3 } }) && !b.overlaps(bounds{ vec3{ 1.5, 0, 0 }, vec3{ 3, 1, 1 } }));
t.expects(b.closest(vec3{ 5, 1, -3 }) == vec3{ 1, 1, -1 } && b.closest(vec3{ 0, 1, 0 }) == vec3{ 0, 1, 0 });
val points: vec3[3] = [vec3{ 1, 5, -2 }, vec3{ -3, 0, 4 }, vec3{ 2, 1, 0 }];
t.expects(bounds::of(points[:]) == bounds{ vec3{ -3, 0, -2 }, vec3{ 2, 5, 4 } } && bounds::of(points[0:0]) == bounds::blank);
t.expects(bounds::blank.empty && !b.empty && bounds::blank.including(vec3::one) == bounds{ vec3::one, vec3::one });
t.expects(b.merged(bounds::around(vec3{ 4, 0, 0 }, vec3::one)) == bounds{ vec3{ -1, -1, -1 }, vec3{ 5, 2, 1 } });
# A quarter turn about z takes the box's reach along y to x, and along x to y.
val quarter := quat::axis_angle(vec3::back, math::pi as f32 / 2);
val turned := bounds::around(vec3::zero, vec3{ 1, 2, 3 }).transformed(mat4::compose(vec3{ 10, 0, 0 }, quarter, vec3::one));
t.expects(near(turned.min, vec3{ 8, -1, -3 }) && near(turned.max, vec3{ 12, 1, 3 }));
t.expects(bounds::blank.transformed(mat4::identity).empty);
val s := sphere{ vec3::zero, 2 };
t.expects(s.contains(vec3{ 0, 2, 0 }) && !s.contains(vec3{ 1.5, 1.5, 0 }));
# Touching, they meet.
t.expects(s.overlaps(sphere{ vec3{ 3, 0, 0 }, 1 }) && !s.overlaps(sphere{ vec3{ 3, 0, 0.5 }, 0.9 }));
t.expects(s.overlaps(b) && b.overlaps(s) && !s.overlaps(bounds::around(vec3{ 3, 3, 0 }, vec3::one)));
t.expects(bounds::of(s) == bounds::around(vec3::zero, vec3{ 2, 2, 2 }));
t.expects(sphere::of(b).center == b.center && near(sphere::of(b).radius, math::sqrt(3f)));
}
@test def casts_rays(t: &mut tester): void {
val r := ray::toward(vec3{ 0, 0, 10 }, vec3::zero);
t.expects(r.direction == vec3::forward && r.at(4) == vec3{ 0, 0, 6 });
t.expects(r.closest(vec3{ 3, 0, 5 }) == vec3{ 0, 0, 5 } && r.closest(vec3{ 0, 0, 20 }) == r.origin);
# A wall ahead is met from in front, and one behind, and a floor it runs beside, aren't.
t.expects(hit_at(r.hit(plane::through(vec3{ 0, 0, 1 }, vec3::back))) == 9);
t.expects(hit_at(r.hit(plane::through(vec3{ 0, 0, 1 }, vec3::forward))) == 9);
t.expects(hit_at(r.hit(plane::through(vec3{ 0, 0, 11 }, vec3::back))) == -1 && hit_at(r.hit(plane::through(vec3::zero, vec3::up))) == -1);
# A sphere ahead is met at its surface, one begun within at 0, and one beside or behind not at all.
t.expects(hit_at(r.hit(sphere{ vec3::zero, 2 })) == 8 && hit_at(ray{ vec3::zero, vec3::up }.hit(sphere{ vec3::zero, 2 })) == 0);
t.expects(hit_at(r.hit(sphere{ vec3{ 3, 0, 0 }, 2 })) == -1 && hit_at(r.hit(sphere{ vec3{ 0, 0, 12 }, 1 })) == -1);
# A box ahead is met at its face, one begun within at 0, and one beside or behind not at all.
val b := bounds::around(vec3::zero, vec3::one);
t.expects(hit_at(r.hit(b)) == 9 && hit_at(ray{ vec3::zero, vec3{ 1, 2, 3 } }.hit(b)) == 0);
t.expects(hit_at(r.hit(bounds::around(vec3{ 3, 0, 0 }, vec3::one))) == -1 && hit_at(r.hit(bounds::around(vec3{ 0, 0, 12 }, vec3::one))) == -1);
# Slanting in, it meets the box where it crosses x = -1.
t.expects(near(hit_at(ray::toward(vec3{ -5, -5, 0 }, vec3::zero).hit(b)), 4 * math::sqrt(2f)));
}
@test def culls_by_a_cameras_frustum(t: &mut tester): void {
# Looking from z = 10 at the origin, a quarter turn wide, from 1 to 100 away.
val f := frustum::of(mat4::perspective(math::pi as f32 / 2, 1, 1, 100) * mat4::look_at(vec3{ 0, 0, 10 }, vec3::zero));
# Its planes face in, so the place it looks at is in front of each.
for p in f.planes {
t.expects(p.distance_to(vec3::zero) > 0 && near(p.normal.length, 1));
}
# The far plane is the difference of two rows nearly alike, so as near as an `f32` of 90 gets.
t.expects(near(f.planes[4].distance_to(vec3{ 0, 0, 9 }), 0) && math::abs(f.planes[5].distance_to(vec3{ 0, 0, -90 })) < 1e-3);
t.expects(f.contains(vec3::zero) && f.contains(vec3{ 9, 0, 0 }));
t.expects(!f.contains(vec3{ 0, 0, 9.5 }) && !f.contains(vec3{ 0, 0, -95 }) && !f.contains(vec3{ 11, 0, 0 }) && !f.contains(vec3{ 0, -11, 0 }));
# A box or sphere reaching in from beside is seen, and one wholly beside or behind isn't.
t.expects(f.overlaps(bounds::around(vec3{ 11, 0, 0 }, vec3{ 2, 1, 1 })) && !f.overlaps(bounds::around(vec3{ 14, 0, 0 }, vec3{ 2, 1, 1 })));
t.expects(f.overlaps(bounds::around(vec3{ 0, 0, 9.5 }, vec3::one)) && !f.overlaps(bounds::around(vec3{ 0, 0, 12 }, vec3::one)));
t.expects(f.overlaps(sphere{ vec3{ 0, 0, 9.5 }, 1 }) && !f.overlaps(sphere{ vec3{ 0, 0, 11 }, 1 }));
t.expects(f.overlaps(sphere{ vec3{ 0, 11, 0 }, 1.5 }) && !f.overlaps(sphere{ vec3{ 0, 13, 0 }, 1.5 }));
# Made of its own planes, it's the same.
val again := frustum::of_planes(f.planes);
for i in 0..6u {
t.expects(again.planes[i] == f.planes[i]);
}
# A projection alone gives the frustum in the camera's space, an orthographic one a box.
val o := frustum::of(mat4::orthographic(-10, 10, -10, 10, 0, 50));
t.expects(o.contains(vec3{ 9.5, -9.5, -49 }) && !o.contains(vec3{ 10.5, 0, -1 }) && !o.contains(vec3{ 0, 0, 1 }) && !o.contains(vec3{ 0, 0, -51 }));
}
# The bits of each number, folded together, as FNV-1a folds bytes, wrapping
# as it does.
def fold(hash: u64, x: f32): u64 {
var held := x;
val bits := ((&mut held) as *mut u32)[0] as u64;
return math::wrapping_mul(hash ^ bits, 0x100000001B3);
}
def fold(hash: u64, v: vec3): u64 {
return fold(fold(fold(hash, v.x), v.y), v.z);
}
def fold(hash: u64, yes: bool): u64 {
return math::wrapping_mul(hash ^ (yes ? 1u : 2u), 0x100000001B3);
}
@test def gives_the_same_bits_as_lanes_or_not(t: &mut tester): void {
var h: u64 = 0xCBF29CE484222325;
val f := frustum::of(mat4::perspective(1.1, 1.5, 0.5, 60) * mat4::look_at(vec3{ 3, 4, 12 }, vec3{ 0, 1, 0 }));
val turn := quat::axis_angle(vec3{ 0.2, 1, -0.4 }, 0.37);
var p := vec3{ 0.3, -1.7, 2.9 };
for i in 0..64u {
p = turn * (p * 1.07 + vec3{ 0.11, -0.05, 0.2 });
val b := bounds::around(p, (turn * vec3{ 0.5, 1.25, 0.75 }).abs());
val s := sphere{ p * 0.5, 1.5 };
val r := ray::toward(vec3{ 20, -3, 7 }, p);
val wall := plane::of_points(p, p + vec3::right, p + turn * vec3::up);
val moved := b.transformed(mat4::compose(p, turn, vec3{ 1, 2, 0.5 }));
h = fold(fold(fold(h, moved.min), moved.max), b.closest(s.center));
h = fold(fold(fold(h, hit_at(r.hit(b))), hit_at(r.hit(s))), hit_at(r.hit(wall)));
h = fold(fold(fold(h, r.closest(s.center)), wall.distance_to(s.center)), wall.closest(vec3::zero));
h = fold(fold(fold(h, f.contains(p)), f.overlaps(b)), f.overlaps(s));
h = fold(fold(fold(h, b.overlaps(s)), s.overlaps(moved)), b.overlaps(moved));
h = fold(fold(h, wall.side_of(b) == side::across), wall.side_of(s) == side::front);
}
val corners: vec3[3] = [p, p * 2, -p];
h = fold(fold(h, sphere::of(bounds::of(corners[:])).radius), f.planes[3].normal);
t.expects(h == expected_bits);
}
# What `gives_the_same_bits_as_lanes_or_not` folds, as the machine works it a
# number at a time.
const expected_bits: u64 = 16338969092856802616;