Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-14 15:41:09 -06:00
commit 11d94dba85
84 files changed
+18825

No files matched your search

+640
View File
@@ -0,0 +1,640 @@
#! Contains the tests of `physics3d`: poses; hulls of boxes, of places, and of
#! triangles, their faces facing out; shapes, their boxes, and what they hold;
#! manifolds between spheres, capsules, and hulls, a box resting on another at
#! four points, on its edge at two, on its corner at one, and edge across edge
#! at one; rays meeting each shape; a tree of boxes, sound as it's changed,
#! finding what a brute search finds; and a mesh of triangles, met but for the
#! edges its triangles share flat, and rays meeting it.
import std::testing;
import std::containers;
import std::math;
import malunal::geometry;
import malunal::linear;
import malunal::physics3d;
# Whether two numbers are within a hundred-thousandth or so of one another.
def near(a: f32, b: f32): bool {
return math::abs(a - b) < 1e-4;
}
def near(a: vec3, b: vec3): bool {
return near(a.x, b.x) && near(a.y, b.y) && near(a.z, b.z);
}
def quarter_about(axis: vec3): quat {
return quat::axis_angle(axis, math::pi as f32 / 2);
}
@test def turns_and_places_by_poses(t: &mut tester): void {
# x turned a quarter about y, counterclockwise as y points at the viewer, is
# -z.
val at := pose::of(vec3{ 3, 1, 0 }, quarter_about(vec3::up));
t.expects(near(at.apply(vec3{ 1, 0, 0 }), vec3{ 3, 1, -1 }) && near(at.unapply(vec3{ 3, 1, -1 }), vec3{ 1, 0, 0 }));
t.expects(near(at.rotate(vec3{ 1, 0, 0 }), vec3{ 0, 0, -1 }) && near(at.unrotate(vec3{ 0, 0, -1 }), vec3{ 1, 0, 0 }));
# A pose within another, found again within it, and undone.
val inner := pose::of(vec3{ -2, 0.5, 1 }, quat::axis_angle(vec3{ 1, 1, 0 }, 0.7));
val both := at * inner;
val p := vec3{ 1, 2, 3 };
t.expects(near(both.apply(p), at.apply(inner.apply(p))));
val found := at.unapply_pose(both);
t.expects(near(found.at, inner.at) && near(found.apply(p), inner.apply(p)));
t.expects(near(at.inverse().apply(at.apply(p)), p) && near((at * at.inverse()).apply(p), p));
}
# Each face's corners on its plane, counterclockwise about its normal, and
# every corner of the hull behind it.
def faces_out(h: &hull): bool {
for f in 0..h.faces {
val n := h.normal(f);
val offset := h.offset(f);
return false if !near(n.length, 1);
val size := h.face_size(f);
for k in 0..size {
val a := h.point(h.corner(f, k));
val b := h.point(h.corner(f, (k + 1) % size));
val c := h.point(h.corner(f, (k + 2) % size));
return false if !near(n.dot(a), offset) || (b - a).cross(c - b).dot(n) < -1e-4;
}
for i in 0..h.count {
return false if n.dot(h.point(i)) > offset + 1e-4;
}
}
return true;
}
@test def makes_hulls(t: &mut tester): void {
# A box's faces, +x, -x, +y, -y, +z, and -z, each edge between two at right
# angles.
val box := hull::box(vec3{ 2, 1, 0.5 });
t.expects(box.count == 8 && box.faces == 6 && box.edges == 12 && faces_out(&box) && !box.is_flat);
t.expects(near(box.normal(0), vec3{ 1, 0, 0 }) && near(box.offset(0), 2) && near(box.normal(3), vec3{ 0, -1, 0 }) && near(box.offset(4), 0.5));
var square := true;
for e in 0..box.edges {
val sides := box.edge_faces(e);
square = square && sides[0] != sides[1] && near(box.normal(sides[0]).dot(box.normal(sides[1])), 0);
}
t.expects(square && box.point(box.support(vec3{ 1, 1, 1 })) == vec3{ 2, 1, 0.5 });
# Turned and moved, its faces still face out.
val turned := hull::box(vec3{ 2, 1, 0.5 }, vec3{ 5, 0, 0 }, quat::axis_angle(vec3{ 1, 2, 3 }, 0.9));
t.expects(faces_out(&turned) && near(turned.center, vec3{ 5, 0, 0 }));
# Around a cube's corners, with places within it, on its faces, and along
# its edges: its 8 corners, 6 square faces, and 12 edges.
val places: vec3[12] = [
vec3{ 0.3, -0.2, 0.5 }, vec3{ -1, -1, -1 }, vec3{ 1, -1, -1 }, vec3{ -1, 1, -1 }, vec3{ 1, 1, -1 }, vec3{ 0, 0, 0 },
vec3{ -1, -1, 1 }, vec3{ 1, -1, 1 }, vec3{ -1, 1, 1 }, vec3{ 1, 1, 1 }, vec3{ 1, 0, 0 }, vec3{ 1, 1, 0 },
];
val cube := hull::of(places[:]).expect("A cube is made.");
t.expects(cube.count == 8 && cube.faces == 6 && cube.edges == 12 && faces_out(&cube));
var quads := true;
for f in 0..cube.faces {
quads = quads && cube.face_size(f) == 4;
}
t.expects(quads && near(cube.center, vec3::zero));
# A tetrahedron, and an icosahedron's 12 corners, of 20 triangles.
val corners: vec3[4] = [vec3{ 0, 0, 0 }, vec3{ 1, 0, 0 }, vec3{ 0, 1, 0 }, vec3{ 0, 0, 1 }];
val tetrahedron := hull::of(corners[:]).expect("A tetrahedron is made.");
t.expects(tetrahedron.count == 4 && tetrahedron.faces == 4 && tetrahedron.edges == 6 && faces_out(&tetrahedron));
val phi := (1 + math::sqrt(5f)) / 2;
var ico := vector::<vec3>::new();
for i in 0..4u {
val a: f32 = (i & 1) != 0 ? 1 : -1;
val b: f32 = (i & 2) != 0 ? phi : -phi;
ico.append(vec3{ 0, a, b });
ico.append(vec3{ a, b, 0 });
ico.append(vec3{ b, 0, a });
}
val twenty := hull::of(ico.span).expect("An icosahedron is made.");
t.expects(twenty.count == 12 && twenty.faces == 20 && twenty.edges == 30 && faces_out(&twenty));
# Flat places hold no volume, and 40 corners are too many.
val flat: vec3[5] = [vec3{ 0, 0, 0 }, vec3{ 1, 0, 0 }, vec3{ 1, 1, 0 }, vec3{ 0, 1, 0 }, vec3{ 0.5, 0.5, 0 }];
t.expects(hull::of(flat[:]) is none && hull::of(corners[0:3]) is none);
var ball := vector::<vec3>::new();
for i in 0..40u {
val y := 1 - 2 * (i as f32 + 0.5) / 40;
val r := math::sqrt(1 - y * y);
val theta := i as f32 * 2.39996;
ball.append(vec3{ r * math::cos(theta), y, r * math::sin(theta) });
}
t.expects(hull::of(ball.span) is none);
# A triangle is flat: a face each way, and each edge between them.
val triangle := hull::triangle(vec3{ 0, 0, 0 }, vec3{ 1, 0, 0 }, vec3{ 0, 0, -1 });
t.expects(triangle.is_flat && triangle.count == 3 && triangle.faces == 2 && triangle.edges == 3);
t.expects(near(triangle.normal(0), vec3{ 0, 1, 0 }) && near(triangle.normal(1), vec3{ 0, -1, 0 }));
}
@test def makes_shapes(t: &mut tester): void {
t.expects(shape::capsule(vec3{ 1, 1, 1 }, vec3{ 1, 1, 1 }, 0.5).kind == shape_kind::sphere);
val pill := shape::pill(1, 3);
t.expects(pill.kind == shape_kind::capsule && near(pill.a, vec3{ 0, -1, 0 }) && near(pill.b, vec3{ 0, 1, 0 }) && near(pill.radius, 0.5));
# The box holding a shape where a pose places it.
val box := shape::box(vec3{ 2, 1, 0.5 });
val turned := box.bounds(pose::of(vec3{ 10, 0, 0 }, quarter_about(vec3::back)));
t.expects(near(turned.min, vec3{ 9, -2, -0.5 }) && near(turned.max, vec3{ 11, 2, 0.5 }));
val held := pill.bounds(pose::of(vec3{ 0, 5, 0 }));
t.expects(near(held.min, vec3{ -0.5, 3.5, -0.5 }) && near(held.max, vec3{ 0.5, 6.5, 0.5 }));
# What they hold.
t.expects(pill.contains(pose::identity, vec3{ 0.4, 1.2, 0 }) && !pill.contains(pose::identity, vec3{ 0.45, 1.4, 0 }));
t.expects(box.contains(pose::identity, vec3{ 1.9, 0.9, 0.4 }) && !box.contains(pose::identity, vec3{ 2.1, 0, 0 }));
t.expects(box.contains(pose::of(vec3{ 10, 0, 0 }, quarter_about(vec3::back)), vec3{ 10.9, 1.9, 0 }));
t.expects(shape::hull_of(corners_of_cube()[:]) is some(s) && s.kind == shape_kind::hull);
}
def corners_of_cube(): vec3[8] {
return [
vec3{ -1, -1, -1 }, vec3{ 1, -1, -1 }, vec3{ -1, 1, -1 }, vec3{ 1, 1, -1 },
vec3{ -1, -1, 1 }, vec3{ 1, -1, 1 }, vec3{ -1, 1, 1 }, vec3{ 1, 1, 1 },
];
}
@test def collides_spheres_and_capsules(t: &mut tester): void {
val ball := shape::sphere(0.5);
val other := shape::sphere(1);
val met := collide(&ball, pose::identity, &other, pose::of(vec3{ 1.2, 0, 0 })).expect("They meet.");
t.expects(met.count == 1 && near(met.normal, vec3{ 1, 0, 0 }) && near(met.points[0].depth, 0.3) && near(met.points[0].point, vec3{ 0.35, 0, 0 }));
t.expects(collide(&ball, pose::identity, &other, pose::of(vec3{ 1.6, 0, 0 })) is none);
t.expects(collide(&ball, pose::identity, &other, pose::of(vec3{ 1.5, 0, 0 })) is some(m) && near(m.depth, 0));
# Within a margin, apart, at a depth below 0.
t.expects(collide(&ball, pose::identity, &other, pose::of(vec3{ 1.6, 0, 0 }), 0.2) is some(m) && near(m.depth, -0.1));
# Capsules lying along one another meet at two points; crossing, at one.
val lying := shape::capsule(vec3{ -0.5, 0, 0 }, vec3{ 0.5, 0, 0 }, 0.5);
val beside := collide(&lying, pose::identity, &lying, pose::of(vec3{ 0.2, 0.8, 0 })).expect("They lie together.");
t.expects(beside.count == 2 && near(beside.normal, vec3{ 0, 1, 0 }) && near(beside.points[0].depth, 0.2) && near(beside.points[1].depth, 0.2));
t.expects(near(beside.points[0].point, vec3{ -0.3, 0.4, 0 }) && near(beside.points[1].point, vec3{ 0.5, 0.4, 0 }));
val crossing := collide(&lying, pose::identity, &lying, pose::of(vec3{ 0, 0.8, 0 }, quarter_about(vec3::up))).expect("They cross.");
t.expects(crossing.count == 1 && near(crossing.normal, vec3{ 0, 1, 0 }) && near(crossing.depth, 0.2) && near(crossing.points[0].point, vec3{ 0, 0.4, 0 }));
# A ball by a capsule's side.
val pill := shape::capsule(vec3{ -1, 0, 0 }, vec3{ 1, 0, 0 }, 0.25);
val side := collide(&pill, pose::identity, &ball, pose::of(vec3{ 0.3, 0, 0.7 })).expect("It meets the capsule's side.");
t.expects(near(side.normal, vec3{ 0, 0, 1 }) && near(side.depth, 0.05));
}
@test def collides_spheres_and_capsules_with_hulls(t: &mut tester): void {
val slab := shape::box(vec3{ 1, 0.5, 1 });
val ball := shape::sphere(0.5);
# On the box's face, by its edge, by its corner, and within it.
val above := collide(&slab, pose::identity, &ball, pose::of(vec3{ 0, 0.9, 0 })).expect("It rests on the box.");
t.expects(near(above.normal, vec3{ 0, 1, 0 }) && near(above.depth, 0.1) && near(above.points[0].point, vec3{ 0, 0.45, 0 }));
val edge := collide(&slab, pose::identity, &ball, pose::of(vec3{ 1.3, 0.8, 0 })).expect("It meets the edge.");
val diagonal := math::sqrt(0.5f);
t.expects(near(edge.normal, vec3{ diagonal, diagonal, 0 }) && near(edge.depth, 0.5 - math::sqrt(0.18f)));
val corner := collide(&slab, pose::identity, &ball, pose::of(vec3{ 1.2, 0.7, 1.2 })).expect("It meets the corner.");
val third := math::sqrt(1f / 3f);
t.expects(near(corner.normal, vec3{ third, third, third }) && near(corner.depth, 0.5 - math::sqrt(0.12f)));
t.expects(collide(&slab, pose::identity, &ball, pose::of(vec3{ 1.3, 0.8, 1.3 })) is none);
val small := shape::sphere(0.1);
val within := collide(&slab, pose::identity, &small, pose::of(vec3{ 0.8, 0, 0 })).expect("It's within the box.");
t.expects(near(within.normal, vec3{ 1, 0, 0 }) && near(within.depth, 0.3));
val back := collide(&small, pose::of(vec3{ 0.8, 0, 0 }), &slab, pose::identity).expect("It's within the box.");
t.expects(near(back.normal, vec3{ -1, 0, 0 }) && near(back.depth, 0.3));
# The box turned, the ball over its face.
val tilted := collide(&slab, pose::of(vec3::zero, quarter_about(vec3::back)), &ball, pose::of(vec3{ -0.9, 0, 0 })).expect("It meets the turned box.");
t.expects(near(tilted.normal, vec3{ -1, 0, 0 }) && near(tilted.depth, 0.1));
# A capsule lying on the box meets it at two points; standing by its edge,
# at one; and stood within it, by the face it's nearest.
val lying := shape::capsule(vec3{ -0.5, 0, 0 }, vec3{ 0.5, 0, 0 }, 0.5);
val flat := collide(&slab, pose::identity, &lying, pose::of(vec3{ 0, 0.9, 0 })).expect("The capsule lies on the box.");
t.expects(flat.count == 2 && near(flat.normal, vec3{ 0, 1, 0 }) && near(flat.points[0].depth, 0.1) && near(flat.points[0].point.y, 0.45));
val standing := shape::capsule(vec3{ 0, 0, 0 }, vec3{ 0, 1, 0 }, 0.5);
val by := collide(&slab, pose::identity, &standing, pose::of(vec3{ 1.3, 0.8, 0 })).expect("The capsule's end meets the edge.");
t.expects(by.count == 1 && near(by.normal, vec3{ diagonal, diagonal, 0 }) && near(by.depth, 0.5 - math::sqrt(0.18f)));
t.expects(!overlaps(&slab, pose::identity, &standing, pose::of(vec3{ 1.4, 0.9, 0 })));
val stood := collide(&slab, pose::identity, &standing, pose::of(vec3{ 0, 0.3, 0 })).expect("The capsule stands within the box.");
t.expects(stood.count == 1 && near(stood.normal, vec3{ 0, 1, 0 }) && near(stood.depth, 0.7) && near(stood.points[0].point, vec3{ 0, 0.15, 0 }));
# The other way around, the normal is turned around.
val under := collide(&lying, pose::of(vec3{ 0, 0.9, 0 }), &slab, pose::identity).expect("The box is under the capsule.");
t.expects(under.count == 2 && near(under.normal, vec3{ 0, -1, 0 }));
}
@test def collides_hulls(t: &mut tester): void {
val slab := shape::box(vec3{ 2, 0.5, 2 });
val crate := shape::box(vec3{ 0.5, 0.5, 0.5 });
# A crate resting flat meets the slab at its four corners, midway between
# their surfaces.
val resting := collide(&slab, pose::identity, &crate, pose::of(vec3{ 0, 0.9, 0 })).expect("The crate rests on the slab.");
t.expects(resting.count == 4 && near(resting.normal, vec3{ 0, 1, 0 }));
var corners := true;
for i in 0..4u {
val p := resting.points[i];
corners = corners && near(p.depth, 0.1) && near(p.point.y, 0.45) && near(math::abs(p.point.x), 0.5) && near(math::abs(p.point.z), 0.5);
}
t.expects(corners);
t.expects(collide(&slab, pose::identity, &crate, pose::of(vec3{ 0, 1.1, 0 })) is none);
t.expects(collide(&slab, pose::identity, &crate, pose::of(vec3{ 0, 1.1, 0 }), 0.2) is some(m) && m.count == 4 && near(m.depth, -0.1));
val flipped := collide(&crate, pose::of(vec3{ 0, 0.9, 0 }), &slab, pose::identity).expect("The slab is under the crate.");
t.expects(flipped.count == 4 && near(flipped.normal, vec3{ 0, -1, 0 }) && near(flipped.depth, 0.1));
# Turned about y, still at four.
val turned := collide(&slab, pose::identity, &crate, pose::of(vec3{ 0, 0.9, 0 }, quat::axis_angle(vec3::up, 0.7))).expect("The turned crate rests on the slab.");
t.expects(turned.count == 4 && near(turned.normal, vec3{ 0, 1, 0 }) && near(turned.depth, 0.1));
# On its edge, at the edge's two ends; on its corner, at the corner.
val half_diagonal := math::sqrt(0.5f);
val on_edge := collide(&slab, pose::identity, &crate, pose::of(vec3{ 0, 0.4 + half_diagonal, 0 }, quat::axis_angle(vec3::back, math::pi as f32 / 4))).expect("Its edge meets the slab.");
t.expects(on_edge.count == 2 && near(on_edge.normal, vec3{ 0, 1, 0 }) && near(on_edge.points[0].depth, 0.1) && near(on_edge.points[1].depth, 0.1));
t.expects(near(math::abs(on_edge.points[0].point.z), 0.5) && near(on_edge.points[0].point.x, 0));
val pointing := quat::axis_angle(vec3::right, math::atan(math::sqrt(0.5f))) * quat::axis_angle(vec3::back, math::pi as f32 / 4);
val half_cube := math::sqrt(0.75f);
val on_corner := collide(&slab, pose::identity, &crate, pose::of(vec3{ 0, 0.4 + half_cube, 0 }, pointing)).expect("Its corner meets the slab.");
t.expects(on_corner.count == 1 && near(on_corner.normal, vec3{ 0, 1, 0 }) && near(on_corner.depth, 0.1) && near(on_corner.points[0].point, vec3{ 0, 0.45, 0 }));
# Edge across edge, at one point between them, the normal along neither's
# face.
val ridge := pose::of(vec3::zero, quat::axis_angle(vec3::right, math::pi as f32 / 4));
val keel := pose::of(vec3{ 0, 2 * half_diagonal - 0.1, 0 }, quat::axis_angle(vec3::back, math::pi as f32 / 4));
val crossed := collide(&crate, ridge, &crate, keel).expect("The edges cross.");
t.expects(crossed.count == 1 && near(crossed.normal, vec3{ 0, 1, 0 }) && near(crossed.depth, 0.1) && near(crossed.points[0].point, vec3{ 0, half_diagonal - 0.05, 0 }));
t.expects(!overlaps(&crate, ridge, &crate, pose::of(vec3{ 0, 2 * half_diagonal + 0.05, 0 }, keel.turn)));
# A hull of places meets as a box does.
val cube := shape::hull_of(corners_of_cube()[:]).expect("A cube is made.");
val stacked := collide(&slab, pose::identity, &cube, pose::of(vec3{ 0, 1.4, 0 })).expect("The cube rests on the slab.");
t.expects(stacked.count == 4 && near(stacked.normal, vec3{ 0, 1, 0 }) && near(stacked.depth, 0.1));
}
@test def casts_rays_at_shapes(t: &mut tester): void {
val up := vec3{ 0, 1, 0 };
val ball := shape::sphere(1);
val struck := ball.ray_cast(pose::of(vec3{ 3, 0, 0 }), vec3::zero, vec3{ 1, 0, 0 }, 10).expect("It meets the ball.");
t.expects(near(struck.distance, 2) && near(struck.point, vec3{ 2, 0, 0 }) && near(struck.normal, vec3{ -1, 0, 0 }));
t.expects(ball.ray_cast(pose::of(vec3{ 3, 0, 0 }), vec3::zero, up, 10) is none);
t.expects(ball.ray_cast(pose::of(vec3{ 3, 0, 0 }), vec3::zero, vec3{ 1, 0, 0 }, 1.5) is none);
val inside := ball.ray_cast(pose::of(vec3{ 3, 0, 0 }), vec3{ 3, 0, 0 }, up, 10).expect("It begins within the ball.");
t.expects(inside.distance == 0 && near(inside.normal, vec3{ 0, -1, 0 }));
# A box, turned a quarter, its long side up and down.
val box := shape::box(vec3{ 1, 0.5, 0.5 });
val roof := box.ray_cast(pose::of(vec3{ 0, 5, 0 }, quarter_about(vec3::back)), vec3::zero, up, 10).expect("It meets the box.");
t.expects(near(roof.distance, 4) && near(roof.normal, vec3{ 0, -1, 0 }) && near(roof.point, vec3{ 0, 4, 0 }));
t.expects(box.ray_cast(pose::of(vec3{ 0, 5, 0 }), vec3::zero, up, 4) is none);
t.expects(box.ray_cast(pose::of(vec3{ 0, 5, 0 }), vec3{ 2, 0, 0 }, up, 10) is none);
# A capsule, by its side, along and across it, and by its round end.
val pill := shape::capsule(vec3{ -1, 3, 0 }, vec3{ 1, 3, 0 }, 0.5);
val side := pill.ray_cast(pose::identity, vec3::zero, up, 10).expect("It meets the capsule's side.");
t.expects(near(side.distance, 2.5) && near(side.normal, vec3{ 0, -1, 0 }));
val across := pill.ray_cast(pose::identity, vec3{ 0.2, 3, -5 }, vec3{ 0, 0, 1 }, 10).expect("It meets the capsule's side.");
t.expects(near(across.distance, 4.5) && near(across.normal, vec3{ 0, 0, -1 }));
val end := pill.ray_cast(pose::identity, vec3{ 3, 3, 0 }, vec3{ -1, 0, 0 }, 10).expect("It meets the capsule's end.");
t.expects(near(end.distance, 1.5) && near(end.normal, vec3{ 1, 0, 0 }));
t.expects(pill.ray_cast(pose::identity, vec3{ 0, 4, 0 }, vec3{ 1, 0, 0 }, 10) is none);
}
# Numbers from 0 to 1, each of the last, as an LCG makes them.
struct dice {
var state: u64;
}
def dice::next(&mut this): f32 {
this.state = math::wrapping_add(math::wrapping_mul(this.state, 6364136223846793005), 1442695040888963407);
return (this.state >> 40) as f32 / (1u << 24) as f32;
}
# A box somewhere within 100 of the origin, as large as 3.
def some_box(seed: &mut dice): bounds {
val at := vec3{ seed.next() * 200 - 100, seed.next() * 200 - 100, seed.next() * 200 - 100 };
return bounds::around(at, vec3{ 0.1 + seed.next() * 1.4, 0.1 + seed.next() * 1.4, 0.1 + seed.next() * 1.4 });
}
# How far along a ray it meets a box, if within reach.
def hit_within(box: bounds, from: vec3, way: vec3, reach: f32): maybe<f32> {
val r := ray{ from, way };
if r.hit(box) is some(at) {
return at if at <= reach;
}
return nothing{};
}
@test def sorts_boxes_in_a_tree(t: &mut tester): void {
var seed := dice{ state = 7 };
var tree := aabb_tree::new(0.25);
var boxes := vector::<bounds>::new();
var leaves := vector::<u32>::new();
for i in 0..500u {
val box := some_box(&mut seed);
boxes.append(box);
leaves.append(tree.insert(box, i));
}
t.expects(tree.length == 500 && tree.is_sound() && tree.height <= 18);
# Moved a little, a leaf stays; moved further, it's put again. Half are
# removed.
t.expects(!tree.move(leaves[0], bounds{ boxes[0].min + vec3{ 0.1, 0, 0 }, boxes[0].max + vec3{ 0.1, 0, 0 } }));
for i in 0..500u {
if i % 2 == 0 {
boxes[i] = some_box(&mut seed);
_ = tree.move(leaves[i], boxes[i]);
} else if i % 4 == 1 {
tree.remove(leaves[i]);
}
}
t.expects(tree.length == 375 && tree.is_sound());
# What a box meets is what a brute search finds, of the leaves' grown boxes.
for k in 0..20u {
val asked := bounds::around(vec3{ seed.next() * 200 - 100, seed.next() * 200 - 100, seed.next() * 200 - 100 }, vec3{ 20, 20, 20 });
var found := vector::<u64>::new();
tree.query(asked, def(leaf: u32, data: u64): bool {
found.append(data);
return true;
});
var expected: u64 = 0;
for i in 0..500u {
if i % 4 != 1 && tree.box_of(leaves[i]).overlaps(asked) {
expected++;
}
}
t.expects(found.length == expected);
}
# The nearest box along a ray, the search narrowing as each is met.
val from := vec3{ -120, 3, -2 };
val way := vec3{ 1, 0.05, 0.02 }.normalized();
var nearest: f32 = 1000;
tree.ray_cast(from, way, 1000, def(leaf: u32, data: u64, reach: f32): f32 {
if hit_within(boxes[data], from, way, reach) is some(at) {
nearest = math::min(nearest, at);
return at;
}
return reach;
});
var expected: f32 = 1000;
for i in 0..500u {
if i % 4 != 1 && hit_within(boxes[i], from, way, 1000) is some(at) {
expected = math::min(expected, at);
}
}
t.expects(near(nearest, expected));
for i in 0..500u {
if i % 4 != 1 {
tree.remove(leaves[i]);
}
}
t.expects(tree.length == 0 && tree.is_sound() && tree.height == 0);
}
# A rotation of some axis and angle.
def some_turn(seed: &mut dice): quat {
val axis := vec3{ seed.next() - 0.5, seed.next() - 0.5, seed.next() - 0.5 };
return quat::axis_angle(axis.length_squared > 1e-4 ? axis : vec3::up, seed.next() * 6.3);
}
# A hull's corners and face normals, and its edges' directions, in space.
struct placed_hull {
var points: vector<vec3>;
var normals: vector<vec3>;
var edges: vector<vec3>;
}
def placed_of(h: &hull, at: pose): placed_hull {
var made := placed_hull{ points = vector::<vec3>::new(), normals = vector::<vec3>::new(), edges = vector::<vec3>::new() };
for i in 0..h.count {
made.points.append(at.apply(h.point(i)));
}
for f in 0..h.faces {
made.normals.append(at.rotate(h.normal(f)));
}
for e in 0..h.edges {
val ends := h.edge(e);
made.edges.append(at.rotate(h.point(ends[1]) - h.point(ends[0])));
}
return made;
}
# How far apart two hulls are along an axis, below 0 where they overlap.
def apart_along(a: &placed_hull, b: &placed_hull, axis: vec3): f32 {
var a_low := 1e30f;
var a_high := -1e30f;
var b_low := 1e30f;
var b_high := -1e30f;
for i in 0..a.points.length {
a_low = math::min(a_low, axis.dot(a.points[i]));
a_high = math::max(a_high, axis.dot(a.points[i]));
}
for i in 0..b.points.length {
b_low = math::min(b_low, axis.dot(b.points[i]));
b_high = math::max(b_high, axis.dot(b.points[i]));
}
return math::max(b_low - a_high, a_low - b_high);
}
# How far apart two hulls are, by every axis that may part them: each face's
# normal, and the cross of every pair of edges, none passed over.
def apart_by_every_axis(a: &placed_hull, b: &placed_hull): f32 {
var most := -1e30f;
for i in 0..a.normals.length {
most = math::max(most, apart_along(a, b, a.normals[i]));
}
for i in 0..b.normals.length {
most = math::max(most, apart_along(a, b, b.normals[i]));
}
for i in 0..a.edges.length {
for j in 0..b.edges.length {
val axis := a.edges[i].cross(b.edges[j]);
continue if axis.length_squared <= 1e-8 * a.edges[i].length_squared * b.edges[j].length_squared;
most = math::max(most, apart_along(a, b, axis.normalized()));
}
}
return most;
}
# Places within a box of so many each way from the origin.
def some_places(seed: &mut dice, count: u32, reach: f32): vector<vec3> {
var made := vector::<vec3>::new();
for i in 0..count {
made.append(vec3{ (seed.next() * 2 - 1) * reach, (seed.next() * 2 - 1) * reach, (seed.next() * 2 - 1) * reach });
}
return move(made);
}
# How far a place is from a box about the origin, turned and placed by a pose:
# below 0, how deep within it.
def from_box(half: vec3, at: pose, p: vec3): f32 {
val local := at.unapply(p);
val out := local.abs() - half;
val outside := out.max(vec3::zero).length;
return outside if outside > 0;
return math::max(out.x, math::max(out.y, out.z));
}
@test def agrees_with_a_full_search(t: &mut tester): void {
var seed := dice{ state = 11 };
# Boxes and hulls of places, turned every way, as near as to meet about
# half the time: met where no axis parts them, as deep as the axis parting
# them least, but where a face is taken over another or an edge as good,
# a few percent deeper.
var tested: u32 = 0;
var met: u32 = 0;
var agreed: u32 = 0;
var deep_enough: u32 = 0;
for i in 0..400u {
var a := shape::box(vec3{ 0.2 + seed.next(), 0.2 + seed.next(), 0.2 + seed.next() });
var b := shape::box(vec3{ 0.2 + seed.next(), 0.2 + seed.next(), 0.2 + seed.next() });
if i % 2 == 1 {
a = shape::hull_of(some_places(&mut seed, 12, 1).span).expect("A hull is made.");
b = shape::hull_of(some_places(&mut seed, 12, 1).span).expect("A hull is made.");
}
val at_a := pose::of(vec3::zero, some_turn(&mut seed));
val at_b := pose::of(vec3{ seed.next() * 3 - 1.5, seed.next() * 3 - 1.5, seed.next() * 3 - 1.5 }, some_turn(&mut seed));
val apart := apart_by_every_axis(&placed_of(a.as_hull(), at_a), &placed_of(b.as_hull(), at_b));
continue if math::abs(apart) < 1e-4;
tested++;
val found := collide(&a, at_a, &b, at_b);
if (found is some(_)) == (apart < 0) {
agreed++;
}
if found is some(m) {
met++;
if math::abs(m.depth + apart) <= 2e-3 + 0.075 * math::abs(apart) && m.count >= 1 && near(m.normal.length, 1) {
deep_enough++;
}
}
}
t.expects(tested > 350 && agreed == tested && met > 100 && deep_enough == met);
# Spheres and capsules against boxes, against their nearest places: a
# capsule's of a thousand along its segment.
var spheres_agreed: u32 = 0;
var capsules_agreed: u32 = 0;
for i in 0..200u {
val half := vec3{ 0.2 + seed.next(), 0.2 + seed.next(), 0.2 + seed.next() };
val box := shape::box(half);
val at_box := pose::of(vec3::zero, some_turn(&mut seed));
val r := 0.1 + seed.next() * 0.6;
val center := vec3{ seed.next() * 3 - 1.5, seed.next() * 3 - 1.5, seed.next() * 3 - 1.5 };
val ball := shape::sphere(r);
val gap := from_box(half, at_box, center) - r;
if math::abs(gap) < 1e-4 {
spheres_agreed++;
} else if collide(&box, at_box, &ball, pose::of(center)) is some(m) {
if gap < 0 && near(m.depth, -gap) && near(m.normal.length, 1) {
spheres_agreed++;
}
} else if gap > 0 {
spheres_agreed++;
}
val pill := shape::pill(r * 2, r * 2 + 0.2 + seed.next());
val at_pill := pose::of(center, some_turn(&mut seed));
var nearest := 1e30f;
for k in 0..=1000u {
val along := pill.a.lerp(pill.b, k as f32 / 1000);
nearest = math::min(nearest, from_box(half, at_box, at_pill.apply(along)));
}
# Where its segment passes within the box, only whether they meet.
val pill_gap := nearest - r;
if math::abs(pill_gap) < 2e-3 {
capsules_agreed++;
} else if collide(&box, at_box, &pill, at_pill) is some(m) {
if pill_gap < 0 && (nearest <= 0 || math::abs(m.depth + pill_gap) <= 2e-3) {
capsules_agreed++;
}
} else if pill_gap > 0 {
capsules_agreed++;
}
}
t.expects(spheres_agreed == 200 && capsules_agreed == 200);
}
# A floor of 8 by 8 squares, each of two triangles, from -4 to 4 along x and
# z, and a wall up its +x side, 2 high, facing in.
def room(): triangle_mesh {
var points := vector::<vec3>::new();
var indices := vector::<u32>::new();
for i in 0..9u {
for j in 0..9u {
points.append(vec3{ i as f32 - 4, 0, j as f32 - 4 });
}
}
for i in 0..8u {
for j in 0..8u {
val p00 := (i * 9 + j) as u32;
val p01 := p00 + 1;
val p10 := p00 + 9;
val p11 := p10 + 1;
for each in [p00, p01, p10, p10, p01, p11] {
indices.append(each);
}
}
}
val w := points.length as u32;
points.append(vec3{ 4, 0, -4 });
points.append(vec3{ 4, 0, 4 });
points.append(vec3{ 4, 2, -4 });
points.append(vec3{ 4, 2, 4 });
for each in [w, w + 1, w + 2, w + 2, w + 1, w + 3] {
indices.append(each);
}
# A triangle of no area, and one of a place past the rest, left out.
val skipped: u32[6] = [0, 1, 2, 0, 1, 500];
for i in 0..6u {
indices.append(skipped[i]);
}
return triangle_mesh::new(points.span, indices.span);
}
# The manifolds a shape meets a mesh with.
def met(m: &triangle_mesh, at: pose, s: &shape, at_s: pose): vector<manifold> {
var found := vector::<manifold>::new();
m.collide(at, s, at_s, 0, def(triangle: u32, each: manifold): bool {
found.append(each);
return true;
});
return found;
}
# Whether each manifold pushes along a direction, as deep as `depth`.
def all_along(found: &vector<manifold>, normal: vec3, depth: f32): bool {
for i in 0..found.length {
return false if !near(found[i].normal, normal) || !near(found[i].depth, depth);
}
return !found.empty;
}
@test def meets_a_mesh_of_triangles(t: &mut tester): void {
val m := room();
t.expects(m.length == 130 && near(m.normal(0), vec3{ 0, 1, 0 }) && near(m.normal(128), vec3{ -1, 0, 0 }));
# The floor's edges between its own triangles are flat, so they don't push;
# those along its sides, of one triangle alone, do.
t.expects(!m.is_active(9, 1) && !m.is_active(9, 2) && m.is_active(0, 0) && m.is_active(128, 0));
# A crate on the floor, over the corner of four squares: pushed up by each
# triangle it's over.
val crate := shape::box(vec3{ 0.5, 0.5, 0.5 });
val resting := met(&m, pose::identity, &crate, pose::of(vec3{ 0.1, 0.45, -0.2 }));
t.expects(resting.length >= 4 && all_along(&resting, vec3{ 0, 1, 0 }, 0.05));
# A ball sunk into it beside a seam: the triangles past the seam meet it by
# their edges, flat, so they push it up as their faces do.
val ball := shape::sphere(0.5);
val sunk := met(&m, pose::identity, &ball, pose::of(vec3{ 0.1, 0.3, 0.35 }));
t.expects(sunk.length >= 3 && all_along(&sunk, vec3{ 0, 1, 0 }, 0.2));
# Against the wall, pushed back from it alone; in the corner, by both.
t.expects(all_along(&met(&m, pose::identity, &ball, pose::of(vec3{ 3.7, 1, 0 })), vec3{ -1, 0, 0 }, 0.2));
val cornered := met(&m, pose::identity, &ball, pose::of(vec3{ 3.7, 0.4, 0.5 }));
var up: u64 = 0;
var back: u64 = 0;
for i in 0..cornered.length {
if near(cornered[i].normal, vec3{ 0, 1, 0 }) && near(cornered[i].depth, 0.1) {
up++;
} else if near(cornered[i].normal, vec3{ -1, 0, 0 }) && near(cornered[i].depth, 0.2) {
back++;
}
}
t.expects(up >= 1 && back >= 1 && up + back == cornered.length);
t.expects(m.overlaps(pose::identity, &ball, pose::of(vec3{ 0, 0.4, 0 })) && !m.overlaps(pose::identity, &ball, pose::of(vec3{ 0, 0.6, 0 })));
# The mesh placed elsewhere is met there.
val moved := pose::of(vec3{ 20, 0, 0 });
t.expects(met(&m, moved, &ball, pose::of(vec3{ 20, 0.3, 0 })).length >= 1 && met(&m, moved, &ball, pose::of(vec3{ 0, 0.3, 0 })).empty);
# Rays down to the floor, up at it from below, and into the wall.
val down := m.ray_cast(pose::identity, vec3{ 0.3, 5, 0.2 }, vec3{ 0, -1, 0 }, 10).expect("It meets the floor.");
t.expects(near(down.distance, 5) && near(down.normal, vec3{ 0, 1, 0 }) && near(down.point, vec3{ 0.3, 0, 0.2 }));
val from_below := m.ray_cast(pose::identity, vec3{ 0.3, -1, 0.2 }, vec3{ 0, 1, 0 }, 10).expect("It meets the floor from below.");
t.expects(near(from_below.distance, 1) && near(from_below.normal, vec3{ 0, -1, 0 }));
val wall := m.ray_cast(pose::identity, vec3{ 0, 1, 0 }, vec3{ 1, 0, 0 }, 10).expect("It meets the wall.");
t.expects(near(wall.distance, 4) && near(wall.normal, vec3{ -1, 0, 0 }) && wall.triangle >= 128);
t.expects(m.ray_cast(pose::identity, vec3{ 0, 1, 0 }, vec3{ 1, 0, 0 }, 3) is none);
t.expects(m.ray_cast(moved, vec3{ 20.3, 5, 0.2 }, vec3{ 0, -1, 0 }, 10) is some(h) && near(h.point, vec3{ 20.3, 0, 0.2 }));
}