Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 04:25:49 -05:00
commit 68f041cec4
22 files changed
+2437

No files matched your search

+159
View File
@@ -0,0 +1,159 @@
#! 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 `geometry.tests` and
#! `geometry.lanes.tests` each check.
import std::testing;
import std::math;
import linear;
import 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 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::empty);
t.expects(bounds::empty.is_empty && !b.is_empty && bounds::empty.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::empty.transformed(mat4::identity).is_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.
def fold(hash: u64, x: f32): u64 {
var held := x;
val bits := ((&mut held) as *mut u32)[0] as u64;
return (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 (hash ^ (yes ? 1 : 2)) * 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;
+179
View File
@@ -0,0 +1,179 @@
#! Contains the tests of `linear`: vectors of two, three, and four; rotations;
#! matrices of three and four; projections; values read as lanes where they lie
#! unaligned; and the same bits whether numbers are worked as lanes or one at a
#! time, which `linear.tests` and `linear.lanes.tests` each check.
import std::testing;
import std::math;
import linear;
# 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);
}
def near(a: &mat4, b: &mat4): bool {
for i in 0..16u {
return false if !near(a.m[i], b.m[i]);
}
return true;
}
def near(a: &mat3, b: &mat3): bool {
return near(a.x_axis, b.x_axis) && near(a.y_axis, b.y_axis) && near(a.z_axis, b.z_axis);
}
@test def lays_out_its_numbers_as_lanes(t: &mut tester): void {
t.expects(size_of::<vec2>() == 8 && size_of::<vec3>() == 16 && size_of::<vec4>() == 16 && size_of::<quat>() == 16);
t.expects(size_of::<mat3>() == 48 && size_of::<mat4>() == 64);
# Each is of `f32`s, so a field of one is wherever the struct holding it puts it.
t.expects(align_of::<vec3>() == 4 && align_of::<mat4>() == 4);
val v := vec3{ 1, 2, 3 };
t.expects(v.x == 1 && v.y == 2 && v.z == 3 && vec3{ y = 5 } == vec3{ 0, 5, 0 });
}
@test def adds_scales_and_measures_vectors(t: &mut tester): void {
val a := vec3{ 1, 2, 3 };
val b := vec3{ 4, -5, 6 };
t.expects(a + b == vec3{ 5, -3, 9 } && a - b == vec3{ -3, 7, -3 } && -a == vec3{ -1, -2, -3 });
t.expects(a * 2 == vec3{ 2, 4, 6 } && a * b == vec3{ 4, -10, 18 } && b / 2 == vec3{ 2, -2.5, 3 });
t.expects(b / a == vec3{ 4, -2.5, 2 });
t.expects(a.dot(b) == 12 && a.length_squared == 14 && near(a.length, math::sqrt(14f)));
t.expects(near(a.normalized().length, 1) && vec3::zero.normalized() == vec3::zero);
t.expects(near(vec3{ 0, 3, 4 }.distance(vec3::zero), 5));
t.expects(a.lerp(b, 0.5) == vec3{ 2.5, -1.5, 4.5 });
t.expects(a.min(b) == vec3{ 1, -5, 3 } && a.max(b) == vec3{ 4, 2, 6 } && b.abs() == vec3{ 4, 5, 6 });
t.expects(vec3::right.cross(vec3::up) == vec3::back && vec3::up.cross(vec3::right) == vec3::forward);
val c := vec2{ 3, 4 };
t.expects(c + vec2::one == vec2{ 4, 5 } && c * 2 == vec2{ 6, 8 } && c.length == 5 && c.dot(vec2{ 1, 1 }) == 7);
t.expects(vec2{ 1, 0 }.cross(vec2{ 0, 1 }) == 1 && vec2{ 1, 0 }.perpendicular() == vec2{ 0, 1 });
t.expects(near(c.normalized().length, 1) && c.lerp(vec2::zero, 0.5) == vec2{ 1.5, 2 });
val d := vec4{ 1, 2, 3, 4 };
t.expects(d + vec4::one == vec4{ 2, 3, 4, 5 } && d * 0.5 == vec4{ 0.5, 1, 1.5, 2 } && d.dot(d) == 30);
t.expects(d.xyz == a && -d == vec4{ -1, -2, -3, -4 } && near(d.normalized().length, 1));
t.expects(d.min(vec4{ 2, 2, 2, 2 }) == vec4{ 1, 2, 2, 2 } && (d - vec4{ 5, 5, 5, 5 }).abs() == vec4{ 4, 3, 2, 1 });
}
@test def turns_and_moves_vectors(t: &mut tester): void {
val quarter: f32 = math::pi as f32 / 2;
# A quarter turn about y takes x to -z, and about z takes x to y.
t.expects(near(quat::axis_angle(vec3::up, quarter) * vec3::right, vec3{ 0, 0, -1 }));
t.expects(near(quat::axis_angle(vec3::back, quarter) * vec3::right, vec3::up));
# Turning by a rotation and then back changes nothing.
val q := quat::euler(0.3, -1.1, 0.7);
t.expects(near(q.length, 1) && near(q.inverse() * (q * vec3{ 1, 2, 3 }), vec3{ 1, 2, 3 }));
# Turning by one rotation after another is turning by their product.
val r := quat::axis_angle(vec3{ 1, 1, 0 }, 0.4);
t.expects(near((q * r) * vec3{ 1, 2, 3 }, q * (r * vec3{ 1, 2, 3 })));
# A matrix composed of a transform does what the transform does.
val m := mat4::compose(vec3{ 5, 0, -2 }, q, vec3{ 2, 2, 2 });
t.expects(near(m.transform_point(vec3{ 1, 0, 0 }), vec3{ 5, 0, -2 } + q * vec3{ 2, 0, 0 }));
t.expects(near(m.transform_vector(vec3{ 1, 0, 0 }), q * vec3{ 2, 0, 0 }));
t.expects(near(&(m * m.inverse()), &mat4::identity));
val p := m * vec4{ 1, 0, 0, 1 };
t.expects(near(p.xyz, m.transform_point(vec3{ 1, 0, 0 })) && near(p.w, 1));
# Halfway between no turn and a third of one is a sixth.
val half := quat::identity.slerp(quat::axis_angle(vec3::up, 4 * quarter / 3), 0.5);
t.expects(near(half * vec3::right, vec3{ 0.5, 0, -math::sqrt(3f) / 2 }));
# Looking at a place turns -z toward it.
t.expects(near(quat::looking(vec3{ 1, 0, 0 }) * vec3::forward, vec3::right));
}
@test def projects_into_clip_space(t: &mut tester): void {
# The near plane is depth 0, the far one 1, and y points up.
val p := mat4::perspective(math::pi as f32 / 2, 2, 1, 100);
val near_point := p * vec4{ 0, 1, -1, 1 };
t.expects(near(near_point.z / near_point.w, 0) && near(near_point.y / near_point.w, 1));
val far_point := p * vec4{ 0, 0, -100, 1 };
t.expects(near(far_point.z / far_point.w, 1));
val o := mat4::orthographic(0, 200, 100, 0, -1, 1);
val corner := o * vec4{ 200, 100, 0, 1 };
t.expects(near(corner.x, 1) && near(corner.y, -1) && near(corner.z, 0.5));
# A camera looking at the origin from +z sees it straight ahead.
val view := mat4::look_at(vec3{ 0, 0, 10 }, vec3::zero);
t.expects(near(view.transform_point(vec3::zero), vec3{ 0, 0, -10 }));
# A projection is undone, and a scale of zero can't be.
val clip := p * view;
t.expects(near(&(clip * clip.inverse()), &mat4::identity) && near(&(clip.inverse() * clip), &mat4::identity));
t.expects(mat4::scaling(vec3::zero).inverse() == mat4{});
}
@test def works_matrices_of_three(t: &mut tester): void {
val q := quat::euler(0.3, -1.1, 0.7);
val r := mat3::rotation(q);
# A rotation's matrix turns as the rotation does, and its transpose back.
t.expects(near(r * vec3{ 1, 2, 3 }, q * vec3{ 1, 2, 3 }) && near(&(r * r.transposed()), &mat3::identity));
t.expects(near(r.determinant, 1) && near(&r.inverse(), &r.transposed()));
# The rotation of a rotation's matrix is the rotation.
val back := quat::of_basis(r.x_axis, r.y_axis, r.z_axis);
t.expects(near(back * vec3{ 1, 2, 3 }, q * vec3{ 1, 2, 3 }));
val s := mat3::scaling(vec3{ 2, 4, 8 });
t.expects(s.determinant == 64 && near(&(s * s.inverse()), &mat3::identity) && mat3::scaling(vec3::zero).inverse() == mat3{});
t.expects(near(&(r * s), &mat4::compose(vec3::zero, q, vec3{ 2, 4, 8 }).linear_part()));
t.expects(s + s == s * 2 && (s - s) == mat3{});
}
# A value lying where a struct puts it, four bytes past a 16-byte boundary.
struct placed {
var tag: u32;
var at: vec3;
var turn: quat;
var by: mat4;
}
@test def reads_values_where_they_lie(t: &mut tester): void {
var p := placed{ tag = 7, at = vec3{ 1, 2, 3 }, turn = quat::axis_angle(vec3::back, math::pi as f32 / 2), by = mat4::translation(vec3{ 1, 1, 1 }) };
p.at = p.at * 2 + vec3::one;
t.expects(p.tag == 7 && p.at == vec3{ 3, 5, 7 });
t.expects(near(p.turn * vec3::right, vec3::up) && near(p.by.transform_point(p.at), vec3{ 4, 6, 8 }));
t.expects(near(&(p.by * p.by), &mat4::translation(vec3{ 2, 2, 2 })));
# Its lanes, and a vector of lanes.
val l := p.at.lanes();
t.expects(l[0] == 3 && l[1] == 5 && l[2] == 7 && vec3::of_lanes(l) == p.at);
t.expects(vec4::of_lanes(f32x4::of([1.0, 2.0, 3.0, 4.0])) == vec4{ 1, 2, 3, 4 });
}
# The bits of each number, folded together, as FNV-1a folds bytes.
def fold(hash: u64, x: f32): u64 {
var held := x;
val bits := ((&mut held) as *mut u32)[0] as u64;
return (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, q: quat): u64 {
return fold(fold(fold(fold(hash, q.x), q.y), q.z), q.w);
}
@test def gives_the_same_bits_as_lanes_or_not(t: &mut tester): void {
var h: u64 = 0xCBF29CE484222325;
var v := vec3{ 0.1, -2.3, 4.5 };
var q := quat::euler(0.2, 0.9, -0.4);
var m := mat4::compose(vec3{ 1, 2, 3 }, q, vec3{ 1.5, 0.5, 2 });
val turn := quat::axis_angle(vec3{ 0.3, 1, -0.2 }, 0.05);
for i in 0..64u {
v = (v + vec3{ 0.25, 0.5, -0.125 }) * 0.9;
v = q * v;
v = m.transform_point(v) - m.transform_vector(v).normalized();
q = (q * turn).normalized();
m = m * mat4::rotation(turn);
h = fold(fold(fold(h, v), q), v.dot(vec3{ 1, 2, 3 }));
h = fold(h, (m * vec4{ v.x, v.y, v.z, 1 }).w);
h = fold(h, (mat3::rotation(q) * v).lerp(v.min(vec3::one).abs(), 0.25));
}
h = fold(fold(h, q.slerp(quat::identity, 0.3)), m.inverse().m[7]);
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 = 2409136494821621507;
+10
View File
@@ -0,0 +1,10 @@
#! Contains the `main` that runs the tests of `linear` and `geometry`, each in a
#! copy of the program of its own. `blossom test` runs them, and `-- --only TEXT`
#! those whose name holds the text.
import system;
import std::testing;
def main(args: span<strview>): void {
exit(tester::run(args));
}