Initial Commit
This commit is contained in:
14 files changed
+1470
No files matched your search
@@ -0,0 +1,175 @@
|
||||
#! 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 }));
|
||||
}
|
||||
|
||||
@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 = 3034663111268630348;
|
||||
Reference in new issue
Block a user