Initial Commit
This commit is contained in:
13 files changed
+1393
No files matched your search
@@ -0,0 +1,38 @@
|
||||
#! Contains `linear`: vectors of two, three, and four `f32`s, rotations as
|
||||
#! quaternions, and matrices of three by three and four by four, the numbers of
|
||||
#! space as games, their renderers, and their physics work it.
|
||||
#!
|
||||
#! Space is right-handed: x to the right, y up, and z toward the viewer, so a
|
||||
#! camera looks down its own -z. A matrix is column-major, as GLSL reads one,
|
||||
#! and multiplies a vector on its right: `m * v`. Projections give a clip space
|
||||
#! of y up and depth from 0 at the near plane to 1 at the far one, as Vulkan's
|
||||
#! is with its y flipped. Angles are radians.
|
||||
#!
|
||||
#! Each type is of plain `f32` fields, so it's laid out as C and the GPU lay
|
||||
#! out its numbers, and the virtual machine reads a field as quickly as any.
|
||||
#! `vec3`, `vec4`, and `quat` are 16 bytes, `vec3` holding a fourth number no one
|
||||
#! names, so an executable optimized with `-O2`, which holds `simd` vectors in
|
||||
#! registers, reads each as one `f32x4` from where it is: what works whole
|
||||
#! vectors, as adding, scaling, and dots do, the products of 4 by 4 matrices,
|
||||
#! and a rotation turning a vector by its matrix's columns, it works as lanes
|
||||
#! at once. What gathers one vector's numbers into
|
||||
#! lanes, as a cross product, or splats them, as one vector a matrix turns, it
|
||||
#! outruns lanes with a number at a time, as the bench shows, so that's how
|
||||
#! those are worked. The virtual machine, which works lanes one at a time,
|
||||
#! works everything a number at a time. Both ways give the same bits.
|
||||
|
||||
module linear;
|
||||
|
||||
|
||||
# The lanes of a value of four `f32`s, as `vec3`, `vec4`, and `quat` are, read
|
||||
# from where it is, which needs no alignment.
|
||||
def lanes_of<T>(v: &T): f32x4 {
|
||||
return ((v as *T) as *f32x4)[0];
|
||||
}
|
||||
|
||||
# The value of four `f32`s whose numbers are lanes.
|
||||
def of_lanes<T>(lanes: f32x4): T where T: zeroable {
|
||||
var made: T;
|
||||
((&mut made) as *mut f32x4)[0] = lanes;
|
||||
return made;
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
#! Contains `mat3`, a matrix of three by three: a rotation, a scale, or what
|
||||
#! turns and stretches a direction without moving it, as an inertia tensor
|
||||
#! does. Its columns are `vec3`s, each 16 bytes, so it's laid out as std140
|
||||
#! lays out a GLSL `mat3`, and its columns are added and scaled as lanes.
|
||||
|
||||
module linear;
|
||||
|
||||
#! A matrix of three by three, column after column: where it takes x, y, and z.
|
||||
pub struct mat3 {
|
||||
pub var x_axis: vec3;
|
||||
pub var y_axis: vec3;
|
||||
pub var z_axis: vec3;
|
||||
}
|
||||
|
||||
#! The matrix that changes nothing.
|
||||
pub val mat3::identity := mat3{ vec3::right, vec3::up, vec3::back };
|
||||
|
||||
#! Turning by a rotation.
|
||||
pub def mat3::rotation(by: quat): mat3 {
|
||||
val q := by;
|
||||
val xx := q.x * q.x;
|
||||
val yy := q.y * q.y;
|
||||
val zz := q.z * q.z;
|
||||
val xy := q.x * q.y;
|
||||
val xz := q.x * q.z;
|
||||
val yz := q.y * q.z;
|
||||
val wx := q.w * q.x;
|
||||
val wy := q.w * q.y;
|
||||
val wz := q.w * q.z;
|
||||
return mat3{
|
||||
vec3{ 1 - 2 * (yy + zz), 2 * (xy + wz), 2 * (xz - wy) },
|
||||
vec3{ 2 * (xy - wz), 1 - 2 * (xx + zz), 2 * (yz + wx) },
|
||||
vec3{ 2 * (xz + wy), 2 * (yz - wx), 1 - 2 * (xx + yy) },
|
||||
};
|
||||
}
|
||||
|
||||
#! Scaling along each axis.
|
||||
pub def mat3::scaling(by: vec3): mat3 {
|
||||
return mat3{ vec3{ by.x, 0, 0 }, vec3{ 0, by.y, 0 }, vec3{ 0, 0, by.z } };
|
||||
}
|
||||
|
||||
#! The matrix doing another, then this: each of the other's columns, as this
|
||||
#! turns and scales it. A number at a time, which an executable outruns lanes
|
||||
#! with for three columns.
|
||||
pub def mat3::mul(&this, other: &mat3): mat3 {
|
||||
return mat3{ this * other.x_axis, this * other.y_axis, this * other.z_axis };
|
||||
}
|
||||
|
||||
#! A direction the matrix turns and scales: each column, scaled by one of its
|
||||
#! numbers, added. A number at a time, which an executable outruns lanes with
|
||||
#! for one vector.
|
||||
pub def mat3::mul(&this, v: &vec3): vec3 {
|
||||
return vec3{
|
||||
this.x_axis.x * v.x + this.y_axis.x * v.y + this.z_axis.x * v.z,
|
||||
this.x_axis.y * v.x + this.y_axis.y * v.y + this.z_axis.y * v.z,
|
||||
this.x_axis.z * v.x + this.y_axis.z * v.y + this.z_axis.z * v.z,
|
||||
};
|
||||
}
|
||||
|
||||
pub def mat3::mul(&this, by: &f32): mat3 {
|
||||
return mat3{ this.x_axis * by, this.y_axis * by, this.z_axis * by };
|
||||
}
|
||||
|
||||
pub def mat3::add(&this, other: &mat3): mat3 {
|
||||
return mat3{ this.x_axis + other.x_axis, this.y_axis + other.y_axis, this.z_axis + other.z_axis };
|
||||
}
|
||||
|
||||
pub def mat3::sub(&this, other: &mat3): mat3 {
|
||||
return mat3{ this.x_axis - other.x_axis, this.y_axis - other.y_axis, this.z_axis - other.z_axis };
|
||||
}
|
||||
|
||||
pub def mat3::equals(&this, other: &mat3): bool {
|
||||
return this.x_axis == other.x_axis && this.y_axis == other.y_axis && this.z_axis == other.z_axis;
|
||||
}
|
||||
|
||||
#! Its rows as its columns, which for a rotation is the rotation back.
|
||||
pub def mat3::transposed(&this): mat3 {
|
||||
return mat3{
|
||||
vec3{ this.x_axis.x, this.y_axis.x, this.z_axis.x },
|
||||
vec3{ this.x_axis.y, this.y_axis.y, this.z_axis.y },
|
||||
vec3{ this.x_axis.z, this.y_axis.z, this.z_axis.z },
|
||||
};
|
||||
}
|
||||
|
||||
#! How much it scales a volume, negative where it mirrors one.
|
||||
pub val mat3::determinant: f32 {
|
||||
get => this.x_axis.dot(this.y_axis.cross(this.z_axis));
|
||||
}
|
||||
|
||||
#! The matrix undoing this, or none where nothing can, as a scale of zero: each
|
||||
#! row the cross product of the other two columns, over the determinant.
|
||||
pub def mat3::inverse(&this): mat3 {
|
||||
val a := this.y_axis.cross(this.z_axis);
|
||||
val b := this.z_axis.cross(this.x_axis);
|
||||
val c := this.x_axis.cross(this.y_axis);
|
||||
val det := this.x_axis.dot(a);
|
||||
return mat3{} if det == 0;
|
||||
val k := 1 / det;
|
||||
return mat3{ a * k, b * k, c * k }.transposed();
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
#! Contains `mat4`, a matrix of four by four: what moves, turns, and scales a
|
||||
#! place, or projects it, as a camera does. Its numbers are column after
|
||||
#! column, so a column is 16 bytes, read as one `f32x4`, and its products are
|
||||
#! each column scaled and added at once.
|
||||
|
||||
import std::math;
|
||||
module linear;
|
||||
|
||||
#! A matrix of four by four, column after column.
|
||||
pub struct mat4 {
|
||||
pub var m: f32[16];
|
||||
}
|
||||
|
||||
#! The matrix that changes nothing.
|
||||
pub val mat4::identity := mat4{ m = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] };
|
||||
|
||||
#! Moving by a vector.
|
||||
pub def mat4::translation(by: vec3): mat4 {
|
||||
var made := mat4::identity;
|
||||
made.m[12] = by.x;
|
||||
made.m[13] = by.y;
|
||||
made.m[14] = by.z;
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Scaling along each axis.
|
||||
pub def mat4::scaling(by: vec3): mat4 {
|
||||
var made := mat4::identity;
|
||||
made.m[0] = by.x;
|
||||
made.m[5] = by.y;
|
||||
made.m[10] = by.z;
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Turning by a rotation.
|
||||
pub def mat4::rotation(by: quat): mat4 {
|
||||
return mat4::compose(vec3::zero, by, vec3::one);
|
||||
}
|
||||
|
||||
#! Scaling, then turning, then moving: what a transform does.
|
||||
pub def mat4::compose(translation: vec3, rotation: quat, scale: vec3): mat4 {
|
||||
val q := rotation;
|
||||
val xx := q.x * q.x;
|
||||
val yy := q.y * q.y;
|
||||
val zz := q.z * q.z;
|
||||
val xy := q.x * q.y;
|
||||
val xz := q.x * q.z;
|
||||
val yz := q.y * q.z;
|
||||
val wx := q.w * q.x;
|
||||
val wy := q.w * q.y;
|
||||
val wz := q.w * q.z;
|
||||
return mat4{ m = [
|
||||
(1 - 2 * (yy + zz)) * scale.x, 2 * (xy + wz) * scale.x, 2 * (xz - wy) * scale.x, 0,
|
||||
2 * (xy - wz) * scale.y, (1 - 2 * (xx + zz)) * scale.y, 2 * (yz + wx) * scale.y, 0,
|
||||
2 * (xz + wy) * scale.z, 2 * (yz - wx) * scale.z, (1 - 2 * (xx + yy)) * scale.z, 0,
|
||||
translation.x, translation.y, translation.z, 1,
|
||||
] };
|
||||
}
|
||||
|
||||
#! A perspective projection, as a camera's: its field of view up and down, the
|
||||
#! width over the height it's drawn at, and the nearest and furthest it draws.
|
||||
pub def mat4::perspective(fov_y: f32, aspect: f32, near: f32, far: f32): mat4 {
|
||||
val f := 1 / math::tan(fov_y / 2);
|
||||
var made := mat4{};
|
||||
made.m[0] = f / aspect;
|
||||
made.m[5] = f;
|
||||
made.m[10] = far / (near - far);
|
||||
made.m[11] = -1;
|
||||
made.m[14] = near * far / (near - far);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! An orthographic projection of a box: its edges, and how near and far it
|
||||
#! reaches down -z.
|
||||
pub def mat4::orthographic(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32): mat4 {
|
||||
var made := mat4::identity;
|
||||
made.m[0] = 2 / (right - left);
|
||||
made.m[5] = 2 / (top - bottom);
|
||||
made.m[10] = 1 / (near - far);
|
||||
made.m[12] = -(right + left) / (right - left);
|
||||
made.m[13] = -(top + bottom) / (top - bottom);
|
||||
made.m[14] = near / (near - far);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The view of a camera at a place looking at another, y as near to `up` as it
|
||||
#! can be: what moves the world into the camera's space.
|
||||
pub def mat4::look_at(eye: vec3, target: vec3, up: vec3 = vec3::up): mat4 {
|
||||
return mat4::compose(eye, quat::looking(target - eye, up), vec3::one).inverse();
|
||||
}
|
||||
|
||||
#! The matrix doing another, then this: each of the other's columns, each of
|
||||
#! this one's columns scaled by one of its numbers, added.
|
||||
pub def mat4::mul(&this, other: &mat4): mat4 {
|
||||
var made := mat4{};
|
||||
if lanes_held() {
|
||||
val c0 := lanes_of(&this.m[0]);
|
||||
val c1 := lanes_of(&this.m[4]);
|
||||
val c2 := lanes_of(&this.m[8]);
|
||||
val c3 := lanes_of(&this.m[12]);
|
||||
val out := (&mut made) as *mut f32x4;
|
||||
for c in 0..4u {
|
||||
out[c] = c0 * f32x4::splat(other.m[c * 4]) + c1 * f32x4::splat(other.m[c * 4 + 1])
|
||||
+ c2 * f32x4::splat(other.m[c * 4 + 2]) + c3 * f32x4::splat(other.m[c * 4 + 3]);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
for c in 0..4u {
|
||||
for r in 0..4u {
|
||||
made.m[c * 4 + r] = this.m[r] * other.m[c * 4] + this.m[4 + r] * other.m[c * 4 + 1]
|
||||
+ this.m[8 + r] * other.m[c * 4 + 2] + this.m[12 + r] * other.m[c * 4 + 3];
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# What the matrix makes of one vector is worked a number at a time, which an
|
||||
# executable outruns lanes with for one.
|
||||
|
||||
pub def mat4::mul(&this, v: &vec4): vec4 {
|
||||
return vec4{
|
||||
this.m[0] * v.x + this.m[4] * v.y + this.m[8] * v.z + this.m[12] * v.w,
|
||||
this.m[1] * v.x + this.m[5] * v.y + this.m[9] * v.z + this.m[13] * v.w,
|
||||
this.m[2] * v.x + this.m[6] * v.y + this.m[10] * v.z + this.m[14] * v.w,
|
||||
this.m[3] * v.x + this.m[7] * v.y + this.m[11] * v.z + this.m[15] * v.w,
|
||||
};
|
||||
}
|
||||
|
||||
pub def mat4::equals(&this, other: &mat4): bool {
|
||||
for i in 0..16u {
|
||||
return false if this.m[i] != other.m[i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! A place moved by the matrix, which moves, turns, and scales but doesn't
|
||||
#! project.
|
||||
pub def mat4::transform_point(&this, p: vec3): vec3 {
|
||||
return vec3{
|
||||
this.m[0] * p.x + this.m[4] * p.y + this.m[8] * p.z + this.m[12],
|
||||
this.m[1] * p.x + this.m[5] * p.y + this.m[9] * p.z + this.m[13],
|
||||
this.m[2] * p.x + this.m[6] * p.y + this.m[10] * p.z + this.m[14],
|
||||
};
|
||||
}
|
||||
|
||||
#! A direction turned and scaled by the matrix, which a move doesn't change.
|
||||
pub def mat4::transform_vector(&this, v: vec3): vec3 {
|
||||
return vec3{
|
||||
this.m[0] * v.x + this.m[4] * v.y + this.m[8] * v.z,
|
||||
this.m[1] * v.x + this.m[5] * v.y + this.m[9] * v.z,
|
||||
this.m[2] * v.x + this.m[6] * v.y + this.m[10] * v.z,
|
||||
};
|
||||
}
|
||||
|
||||
#! Where the matrix moves the origin to.
|
||||
pub val mat4::translation_of: vec3 {
|
||||
get => vec3{ this.m[12], this.m[13], this.m[14] };
|
||||
}
|
||||
|
||||
#! A column, from 0.
|
||||
pub def mat4::column(&this, which: u64): vec4 {
|
||||
return vec4{ this.m[which * 4], this.m[which * 4 + 1], this.m[which * 4 + 2], this.m[which * 4 + 3] };
|
||||
}
|
||||
|
||||
#! What it does to directions: its first three columns' first three numbers.
|
||||
pub def mat4::linear_part(&this): mat3 {
|
||||
return mat3{
|
||||
vec3{ this.m[0], this.m[1], this.m[2] },
|
||||
vec3{ this.m[4], this.m[5], this.m[6] },
|
||||
vec3{ this.m[8], this.m[9], this.m[10] },
|
||||
};
|
||||
}
|
||||
|
||||
pub def mat4::transposed(&this): mat4 {
|
||||
var made := mat4{};
|
||||
for c in 0..4u {
|
||||
for r in 0..4u {
|
||||
made.m[c * 4 + r] = this.m[r * 4 + c];
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The matrix undoing this, or none where nothing can, as a scale of zero: of
|
||||
#! cofactors, a number at a time, which outruns gathering them into lanes.
|
||||
pub def mat4::inverse(&this): mat4 {
|
||||
val a := &this.m;
|
||||
var inv: f32[16];
|
||||
inv[0] = a[5] * a[10] * a[15] - a[5] * a[11] * a[14] - a[9] * a[6] * a[15] + a[9] * a[7] * a[14] + a[13] * a[6] * a[11] - a[13] * a[7] * a[10];
|
||||
inv[4] = -a[4] * a[10] * a[15] + a[4] * a[11] * a[14] + a[8] * a[6] * a[15] - a[8] * a[7] * a[14] - a[12] * a[6] * a[11] + a[12] * a[7] * a[10];
|
||||
inv[8] = a[4] * a[9] * a[15] - a[4] * a[11] * a[13] - a[8] * a[5] * a[15] + a[8] * a[7] * a[13] + a[12] * a[5] * a[11] - a[12] * a[7] * a[9];
|
||||
inv[12] = -a[4] * a[9] * a[14] + a[4] * a[10] * a[13] + a[8] * a[5] * a[14] - a[8] * a[6] * a[13] - a[12] * a[5] * a[10] + a[12] * a[6] * a[9];
|
||||
inv[1] = -a[1] * a[10] * a[15] + a[1] * a[11] * a[14] + a[9] * a[2] * a[15] - a[9] * a[3] * a[14] - a[13] * a[2] * a[11] + a[13] * a[3] * a[10];
|
||||
inv[5] = a[0] * a[10] * a[15] - a[0] * a[11] * a[14] - a[8] * a[2] * a[15] + a[8] * a[3] * a[14] + a[12] * a[2] * a[11] - a[12] * a[3] * a[10];
|
||||
inv[9] = -a[0] * a[9] * a[15] + a[0] * a[11] * a[13] + a[8] * a[1] * a[15] - a[8] * a[3] * a[13] - a[12] * a[1] * a[11] + a[12] * a[3] * a[9];
|
||||
inv[13] = a[0] * a[9] * a[14] - a[0] * a[10] * a[13] - a[8] * a[1] * a[14] + a[8] * a[2] * a[13] + a[12] * a[1] * a[10] - a[12] * a[2] * a[9];
|
||||
inv[2] = a[1] * a[6] * a[15] - a[1] * a[7] * a[14] - a[5] * a[2] * a[15] + a[5] * a[3] * a[14] + a[13] * a[2] * a[7] - a[13] * a[3] * a[6];
|
||||
inv[6] = -a[0] * a[6] * a[15] + a[0] * a[7] * a[14] + a[4] * a[2] * a[15] - a[4] * a[3] * a[14] - a[12] * a[2] * a[7] + a[12] * a[3] * a[6];
|
||||
inv[10] = a[0] * a[5] * a[15] - a[0] * a[7] * a[13] - a[4] * a[1] * a[15] + a[4] * a[3] * a[13] + a[12] * a[1] * a[7] - a[12] * a[3] * a[5];
|
||||
inv[14] = -a[0] * a[5] * a[14] + a[0] * a[6] * a[13] + a[4] * a[1] * a[14] - a[4] * a[2] * a[13] - a[12] * a[1] * a[6] + a[12] * a[2] * a[5];
|
||||
inv[3] = -a[1] * a[6] * a[11] + a[1] * a[7] * a[10] + a[5] * a[2] * a[11] - a[5] * a[3] * a[10] - a[9] * a[2] * a[7] + a[9] * a[3] * a[6];
|
||||
inv[7] = a[0] * a[6] * a[11] - a[0] * a[7] * a[10] - a[4] * a[2] * a[11] + a[4] * a[3] * a[10] + a[8] * a[2] * a[7] - a[8] * a[3] * a[6];
|
||||
inv[11] = -a[0] * a[5] * a[11] + a[0] * a[7] * a[9] + a[4] * a[1] * a[11] - a[4] * a[3] * a[9] - a[8] * a[1] * a[7] + a[8] * a[3] * a[5];
|
||||
inv[15] = a[0] * a[5] * a[10] - a[0] * a[6] * a[9] - a[4] * a[1] * a[10] + a[4] * a[2] * a[9] + a[8] * a[1] * a[6] - a[8] * a[2] * a[5];
|
||||
val det := a[0] * inv[0] + a[1] * inv[4] + a[2] * inv[8] + a[3] * inv[12];
|
||||
return mat4{} if det == 0;
|
||||
val k := 1 / det;
|
||||
for i in 0..16u {
|
||||
inv[i] *= k;
|
||||
}
|
||||
return mat4{ m = inv };
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
#! Contains `quat`, a rotation as a quaternion of length 1: made of an axis and
|
||||
#! an angle, of angles about each axis, or of a direction to look in; turning
|
||||
#! vectors, following one another, undone, and turned between at an even rate.
|
||||
|
||||
import std::math;
|
||||
module linear;
|
||||
|
||||
#! A rotation, as a quaternion of length 1.
|
||||
pub struct quat {
|
||||
pub var x: f32;
|
||||
pub var y: f32;
|
||||
pub var z: f32;
|
||||
pub var w: f32 = 1;
|
||||
}
|
||||
|
||||
#! No rotation.
|
||||
pub val quat::identity := quat{ 0, 0, 0, 1 };
|
||||
|
||||
#! Its numbers as lanes, for code of its own to work several at once.
|
||||
pub def quat::lanes(&this): f32x4 { return lanes_of(this); }
|
||||
|
||||
#! The quaternion of lanes.
|
||||
pub def quat::of_lanes(lanes: f32x4): quat { return of_lanes::<quat>(lanes); }
|
||||
|
||||
#! A rotation about an axis, by an angle, counterclockwise as the axis points
|
||||
#! at the viewer.
|
||||
pub def quat::axis_angle(axis: vec3, angle: f32): quat {
|
||||
val a := axis.normalized();
|
||||
val s := math::sin(angle / 2);
|
||||
return quat{ a.x * s, a.y * s, a.z * s, math::cos(angle / 2) };
|
||||
}
|
||||
|
||||
#! A rotation by angles about each axis, as a camera's is: about z, its roll,
|
||||
#! then about x, its pitch, then about y, its yaw.
|
||||
pub def quat::euler(x: f32, y: f32, z: f32): quat {
|
||||
return quat::axis_angle(vec3::up, y) * quat::axis_angle(vec3::right, x) * quat::axis_angle(vec3::back, z);
|
||||
}
|
||||
|
||||
#! The rotation turning -z, where a camera looks, to a direction, and y as near
|
||||
#! to `up` as it can be.
|
||||
pub def quat::looking(direction: vec3, up: vec3 = vec3::up): quat {
|
||||
val f := direction.normalized();
|
||||
var r := f.cross(up).normalized();
|
||||
if r.length == 0 {
|
||||
# Looking along `up`, any right will do.
|
||||
r = f.cross(math::abs(f.x) < 0.9 ? vec3::right : vec3::up).normalized();
|
||||
}
|
||||
val u := r.cross(f);
|
||||
return quat::of_basis(r, u, -f);
|
||||
}
|
||||
|
||||
#! The rotation whose matrix's columns are three axes, at right angles, each of
|
||||
#! length 1, as `mat3::rotation` gives them.
|
||||
pub def quat::of_basis(x: vec3, y: vec3, z: vec3): quat {
|
||||
val trace := x.x + y.y + z.z;
|
||||
if trace > 0 {
|
||||
val s := math::sqrt(trace + 1) * 2;
|
||||
return quat{ (y.z - z.y) / s, (z.x - x.z) / s, (x.y - y.x) / s, s / 4 };
|
||||
}
|
||||
if x.x > y.y && x.x > z.z {
|
||||
val s := math::sqrt(1 + x.x - y.y - z.z) * 2;
|
||||
return quat{ s / 4, (y.x + x.y) / s, (z.x + x.z) / s, (y.z - z.y) / s };
|
||||
}
|
||||
if y.y > z.z {
|
||||
val s := math::sqrt(1 + y.y - x.x - z.z) * 2;
|
||||
return quat{ (y.x + x.y) / s, s / 4, (z.y + y.z) / s, (z.x - x.z) / s };
|
||||
}
|
||||
val s := math::sqrt(1 + z.z - x.x - y.y) * 2;
|
||||
return quat{ (z.x + x.z) / s, (z.y + y.z) / s, s / 4, (x.y - y.x) / s };
|
||||
}
|
||||
|
||||
#! The rotation of this after another: `a * b` turns by `b`, then by `a`. A
|
||||
#! number at a time, which an executable outruns gathering the other's numbers
|
||||
#! into lanes with.
|
||||
pub def quat::mul(&this, o: &quat): quat {
|
||||
return quat{
|
||||
o.x * this.w + o.w * this.x + o.z * this.y - o.y * this.z,
|
||||
o.y * this.w - o.z * this.x + o.w * this.y + o.x * this.z,
|
||||
o.z * this.w + o.y * this.x - o.x * this.y + o.w * this.z,
|
||||
o.w * this.w - o.x * this.x - o.y * this.y - o.z * this.z,
|
||||
};
|
||||
}
|
||||
|
||||
#! A vector turned by the rotation, by its matrix: whose columns, scaled by the
|
||||
#! vector's numbers and added, are worked as lanes, which an executable works
|
||||
#! twice as fast as the crosses of the rotation's axis.
|
||||
pub def quat::mul(&this, v: &vec3): vec3 {
|
||||
val r := mat3::rotation(this);
|
||||
if lanes_held() {
|
||||
return of_lanes::<vec3>(lanes_of(&r.x_axis) * f32x4::splat(v.x) + lanes_of(&r.y_axis) * f32x4::splat(v.y)
|
||||
+ lanes_of(&r.z_axis) * f32x4::splat(v.z));
|
||||
}
|
||||
return r * v;
|
||||
}
|
||||
|
||||
pub def quat::equals(&this, other: &quat): bool {
|
||||
return this.x == other.x && this.y == other.y && this.z == other.z && this.w == other.w;
|
||||
}
|
||||
|
||||
pub def quat::dot(&this, other: quat): f32 {
|
||||
if lanes_held() {
|
||||
val p := lanes_of(this) * lanes_of(&other);
|
||||
return p[0] + p[1] + p[2] + p[3];
|
||||
}
|
||||
return this.x * other.x + this.y * other.y + this.z * other.z + this.w * other.w;
|
||||
}
|
||||
|
||||
pub val quat::length: f32 {
|
||||
get => math::sqrt(this.dot(this));
|
||||
}
|
||||
|
||||
#! The rotation back.
|
||||
pub def quat::inverse(&this): quat {
|
||||
return quat{ -this.x, -this.y, -this.z, this.w };
|
||||
}
|
||||
|
||||
#! The quaternion at a length of 1, as rounding keeps a rotation turned many
|
||||
#! times.
|
||||
pub def quat::normalized(&this): quat {
|
||||
val l := math::sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
|
||||
return quat::identity if l == 0;
|
||||
val k := 1 / l;
|
||||
return quat{ this.x * k, this.y * k, this.z * k, this.w * k };
|
||||
}
|
||||
|
||||
#! The rotation a fraction of the way to another, turning at an even rate by
|
||||
#! the shorter way.
|
||||
pub def quat::slerp(&this, to: quat, t: f32): quat {
|
||||
var cos := this.dot(to);
|
||||
var end := to;
|
||||
if cos < 0 {
|
||||
cos = -cos;
|
||||
end = quat{ -to.x, -to.y, -to.z, -to.w };
|
||||
}
|
||||
var a := 1 - t;
|
||||
var b := t;
|
||||
if cos < 0.9995 {
|
||||
val angle := math::acos(cos);
|
||||
val s := math::sin(angle);
|
||||
a = math::sin((1 - t) * angle) / s;
|
||||
b = math::sin(t * angle) / s;
|
||||
}
|
||||
val made := quat{ this.x * a + end.x * b, this.y * a + end.y * b, this.z * a + end.z * b, this.w * a + end.w * b };
|
||||
return made.normalized();
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
#! Contains `vec2`, a vector of two: a place or a direction in a plane, as of
|
||||
#! a window's pixels, a glyph's outline, or a texture. It's 8 bytes, half the
|
||||
#! lanes of an `f32x4`, so it's worked a number at a time.
|
||||
|
||||
import std::math;
|
||||
module linear;
|
||||
|
||||
#! A vector of two: a place or a direction in a plane.
|
||||
pub struct vec2 {
|
||||
pub var x: f32;
|
||||
pub var y: f32;
|
||||
}
|
||||
|
||||
pub val vec2::zero := vec2{ 0, 0 };
|
||||
pub val vec2::one := vec2{ 1, 1 };
|
||||
|
||||
pub def vec2::add(&this, other: &vec2): vec2 { return vec2{ this.x + other.x, this.y + other.y }; }
|
||||
pub def vec2::sub(&this, other: &vec2): vec2 { return vec2{ this.x - other.x, this.y - other.y }; }
|
||||
pub def vec2::mul(&this, by: &f32): vec2 { return vec2{ this.x * by, this.y * by }; }
|
||||
pub def vec2::mul(&this, other: &vec2): vec2 { return vec2{ this.x * other.x, this.y * other.y }; }
|
||||
pub def vec2::div(&this, by: &f32): vec2 { return vec2{ this.x / by, this.y / by }; }
|
||||
pub def vec2::div(&this, other: &vec2): vec2 { return vec2{ this.x / other.x, this.y / other.y }; }
|
||||
pub def vec2::negate(&this): vec2 { return vec2{ -this.x, -this.y }; }
|
||||
pub def vec2::equals(&this, other: &vec2): bool { return this.x == other.x && this.y == other.y; }
|
||||
|
||||
pub def vec2::dot(&this, other: vec2): f32 { return this.x * other.x + this.y * other.y; }
|
||||
|
||||
#! The z of the cross product of the two as vectors of three in the plane z is
|
||||
#! 0: positive where `other` is counterclockwise of this.
|
||||
pub def vec2::cross(&this, other: vec2): f32 { return this.x * other.y - this.y * other.x; }
|
||||
|
||||
#! The vector turned a quarter counterclockwise.
|
||||
pub def vec2::perpendicular(&this): vec2 { return vec2{ -this.y, this.x }; }
|
||||
|
||||
pub val vec2::length: f32 {
|
||||
get => math::sqrt(this.x * this.x + this.y * this.y);
|
||||
}
|
||||
|
||||
#! The length squared, which needs no square root, to compare lengths by.
|
||||
pub val vec2::length_squared: f32 {
|
||||
get => this.x * this.x + this.y * this.y;
|
||||
}
|
||||
|
||||
pub def vec2::distance(&this, other: vec2): f32 { return (other - this).length; }
|
||||
|
||||
#! The vector at a length of 1, or none where it has no length.
|
||||
pub def vec2::normalized(&this): vec2 {
|
||||
val l := this.length;
|
||||
return vec2{} if l == 0;
|
||||
val k := 1 / l;
|
||||
return vec2{ this.x * k, this.y * k };
|
||||
}
|
||||
|
||||
#! The vector a fraction of the way to another.
|
||||
pub def vec2::lerp(&this, to: vec2, t: f32): vec2 {
|
||||
return vec2{ this.x + (to.x - this.x) * t, this.y + (to.y - this.y) * t };
|
||||
}
|
||||
|
||||
#! The lesser of each pair of numbers, as `math::min` gives it.
|
||||
pub def vec2::min(&this, other: vec2): vec2 { return vec2{ math::min(this.x, other.x), math::min(this.y, other.y) }; }
|
||||
|
||||
#! The greater of each pair of numbers, as `math::max` gives it.
|
||||
pub def vec2::max(&this, other: vec2): vec2 { return vec2{ math::max(this.x, other.x), math::max(this.y, other.y) }; }
|
||||
|
||||
#! Each number's distance from zero.
|
||||
pub def vec2::abs(&this): vec2 { return vec2{ math::abs(this.x), math::abs(this.y) }; }
|
||||
@@ -0,0 +1,154 @@
|
||||
#! Contains `vec3`, a vector of three: a place or a direction in space. It holds
|
||||
#! a fourth number no one names, so it's 16 bytes, an `f32x4`'s, which an
|
||||
#! executable holding vectors in registers works at once.
|
||||
|
||||
import std::math;
|
||||
module linear;
|
||||
|
||||
#! A vector of three: a place or a direction in space.
|
||||
pub struct vec3 {
|
||||
pub var x: f32;
|
||||
pub var y: f32;
|
||||
pub var z: f32;
|
||||
# Unnamed, so the vector is four lanes; what its lane holds means nothing.
|
||||
var _w: f32;
|
||||
}
|
||||
|
||||
pub val vec3::zero := vec3{ 0, 0, 0 };
|
||||
pub val vec3::one := vec3{ 1, 1, 1 };
|
||||
pub val vec3::right := vec3{ 1, 0, 0 };
|
||||
pub val vec3::left := vec3{ -1, 0, 0 };
|
||||
pub val vec3::up := vec3{ 0, 1, 0 };
|
||||
pub val vec3::down := vec3{ 0, -1, 0 };
|
||||
#! Toward the viewer: a camera looks the other way.
|
||||
pub val vec3::back := vec3{ 0, 0, 1 };
|
||||
#! Away from the viewer, where a camera looks.
|
||||
pub val vec3::forward := vec3{ 0, 0, -1 };
|
||||
|
||||
#! Its numbers as lanes, the fourth meaning nothing, for code of its own to
|
||||
#! work several at once.
|
||||
pub def vec3::lanes(&this): f32x4 { return lanes_of(this); }
|
||||
|
||||
#! The vector of the first three of lanes.
|
||||
pub def vec3::of_lanes(lanes: f32x4): vec3 { return of_lanes::<vec3>(lanes); }
|
||||
|
||||
pub def vec3::add(&this, other: &vec3): vec3 {
|
||||
return of_lanes::<vec3>(lanes_of(this) + lanes_of(other)) if lanes_held();
|
||||
return vec3{ this.x + other.x, this.y + other.y, this.z + other.z };
|
||||
}
|
||||
|
||||
pub def vec3::sub(&this, other: &vec3): vec3 {
|
||||
return of_lanes::<vec3>(lanes_of(this) - lanes_of(other)) if lanes_held();
|
||||
return vec3{ this.x - other.x, this.y - other.y, this.z - other.z };
|
||||
}
|
||||
|
||||
pub def vec3::mul(&this, by: &f32): vec3 {
|
||||
return of_lanes::<vec3>(lanes_of(this) * f32x4::splat(by)) if lanes_held();
|
||||
return vec3{ this.x * by, this.y * by, this.z * by };
|
||||
}
|
||||
|
||||
pub def vec3::mul(&this, other: &vec3): vec3 {
|
||||
return of_lanes::<vec3>(lanes_of(this) * lanes_of(other)) if lanes_held();
|
||||
return vec3{ this.x * other.x, this.y * other.y, this.z * other.z };
|
||||
}
|
||||
|
||||
pub def vec3::div(&this, by: &f32): vec3 {
|
||||
return of_lanes::<vec3>(lanes_of(this) / f32x4::splat(by)) if lanes_held();
|
||||
return vec3{ this.x / by, this.y / by, this.z / by };
|
||||
}
|
||||
|
||||
pub def vec3::div(&this, other: &vec3): vec3 {
|
||||
if lanes_held() {
|
||||
# The unnamed lanes divide 0 by 0, which is kept from the vector.
|
||||
var made := of_lanes::<vec3>(lanes_of(this) / lanes_of(other));
|
||||
made._w = 0;
|
||||
return made;
|
||||
}
|
||||
return vec3{ this.x / other.x, this.y / other.y, this.z / other.z };
|
||||
}
|
||||
|
||||
pub def vec3::negate(&this): vec3 {
|
||||
return of_lanes::<vec3>(-lanes_of(this)) if lanes_held();
|
||||
return vec3{ -this.x, -this.y, -this.z };
|
||||
}
|
||||
|
||||
pub def vec3::equals(&this, other: &vec3): bool {
|
||||
return this.x == other.x && this.y == other.y && this.z == other.z;
|
||||
}
|
||||
|
||||
pub def vec3::dot(&this, other: vec3): f32 {
|
||||
if lanes_held() {
|
||||
val p := lanes_of(this) * lanes_of(&other);
|
||||
return p[0] + p[1] + p[2];
|
||||
}
|
||||
return this.x * other.x + this.y * other.y + this.z * other.z;
|
||||
}
|
||||
|
||||
#! The vector at right angles to both, by the right hand: x cross y is z. Its
|
||||
#! lanes would be shuffled, which no instruction does, so it's a number at a
|
||||
#! time.
|
||||
pub def vec3::cross(&this, other: vec3): vec3 {
|
||||
return vec3{
|
||||
this.y * other.z - this.z * other.y,
|
||||
this.z * other.x - this.x * other.z,
|
||||
this.x * other.y - this.y * other.x,
|
||||
};
|
||||
}
|
||||
|
||||
pub val vec3::length: f32 {
|
||||
get => math::sqrt(this.dot(this));
|
||||
}
|
||||
|
||||
#! The length squared, which needs no square root, to compare lengths by.
|
||||
pub val vec3::length_squared: f32 {
|
||||
get => this.dot(this);
|
||||
}
|
||||
|
||||
pub def vec3::distance(&this, other: vec3): f32 {
|
||||
if lanes_held() {
|
||||
val d := lanes_of(&other) - lanes_of(this);
|
||||
val p := d * d;
|
||||
return math::sqrt(p[0] + p[1] + p[2]);
|
||||
}
|
||||
val d := vec3{ other.x - this.x, other.y - this.y, other.z - this.z };
|
||||
return math::sqrt(d.x * d.x + d.y * d.y + d.z * d.z);
|
||||
}
|
||||
|
||||
#! The vector at a length of 1, or none where it has no length. A number at a
|
||||
#! time, which an executable outruns lanes with, the square root between them.
|
||||
pub def vec3::normalized(&this): vec3 {
|
||||
val l := math::sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
||||
return vec3{} if l == 0;
|
||||
val k := 1 / l;
|
||||
return vec3{ this.x * k, this.y * k, this.z * k };
|
||||
}
|
||||
|
||||
# The methods below work their lanes themselves rather than calling the
|
||||
# operators', since an operator called within one is left a call.
|
||||
|
||||
#! The vector a fraction of the way to another.
|
||||
pub def vec3::lerp(&this, to: vec3, t: f32): vec3 {
|
||||
if lanes_held() {
|
||||
val from := lanes_of(this);
|
||||
return of_lanes::<vec3>(from + (lanes_of(&to) - from) * f32x4::splat(t));
|
||||
}
|
||||
return vec3{ this.x + (to.x - this.x) * t, this.y + (to.y - this.y) * t, this.z + (to.z - this.z) * t };
|
||||
}
|
||||
|
||||
#! The lesser of each pair of numbers, as `math::min` gives it.
|
||||
pub def vec3::min(&this, other: vec3): vec3 {
|
||||
return of_lanes::<vec3>(lanes_of(this).min(&lanes_of(&other))) if lanes_held();
|
||||
return vec3{ math::min(this.x, other.x), math::min(this.y, other.y), math::min(this.z, other.z) };
|
||||
}
|
||||
|
||||
#! The greater of each pair of numbers, as `math::max` gives it.
|
||||
pub def vec3::max(&this, other: vec3): vec3 {
|
||||
return of_lanes::<vec3>(lanes_of(this).max(&lanes_of(&other))) if lanes_held();
|
||||
return vec3{ math::max(this.x, other.x), math::max(this.y, other.y), math::max(this.z, other.z) };
|
||||
}
|
||||
|
||||
#! Each number's distance from zero.
|
||||
pub def vec3::abs(&this): vec3 {
|
||||
return of_lanes::<vec3>(lanes_of(this).abs()) if lanes_held();
|
||||
return vec3{ math::abs(this.x), math::abs(this.y), math::abs(this.z) };
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
#! Contains `vec4`, a vector of four: a place of space with its `w`, as a
|
||||
#! matrix projects one, or four numbers worked alike, as a color's. It's an
|
||||
#! `f32x4`'s 16 bytes, which an executable holding vectors in registers works at
|
||||
#! once.
|
||||
|
||||
import std::math;
|
||||
module linear;
|
||||
|
||||
#! A vector of four, as a point of space with its `w`.
|
||||
pub struct vec4 {
|
||||
pub var x: f32;
|
||||
pub var y: f32;
|
||||
pub var z: f32;
|
||||
pub var w: f32;
|
||||
}
|
||||
|
||||
pub val vec4::zero := vec4{ 0, 0, 0, 0 };
|
||||
pub val vec4::one := vec4{ 1, 1, 1, 1 };
|
||||
|
||||
#! Its numbers as lanes, for code of its own to work several at once.
|
||||
pub def vec4::lanes(&this): f32x4 { return lanes_of(this); }
|
||||
|
||||
#! The vector of lanes.
|
||||
pub def vec4::of_lanes(lanes: f32x4): vec4 { return of_lanes::<vec4>(lanes); }
|
||||
|
||||
pub def vec4::add(&this, other: &vec4): vec4 {
|
||||
return of_lanes::<vec4>(lanes_of(this) + lanes_of(other)) if lanes_held();
|
||||
return vec4{ this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w };
|
||||
}
|
||||
|
||||
pub def vec4::sub(&this, other: &vec4): vec4 {
|
||||
return of_lanes::<vec4>(lanes_of(this) - lanes_of(other)) if lanes_held();
|
||||
return vec4{ this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w };
|
||||
}
|
||||
|
||||
pub def vec4::mul(&this, by: &f32): vec4 {
|
||||
return of_lanes::<vec4>(lanes_of(this) * f32x4::splat(by)) if lanes_held();
|
||||
return vec4{ this.x * by, this.y * by, this.z * by, this.w * by };
|
||||
}
|
||||
|
||||
pub def vec4::mul(&this, other: &vec4): vec4 {
|
||||
return of_lanes::<vec4>(lanes_of(this) * lanes_of(other)) if lanes_held();
|
||||
return vec4{ this.x * other.x, this.y * other.y, this.z * other.z, this.w * other.w };
|
||||
}
|
||||
|
||||
pub def vec4::div(&this, by: &f32): vec4 {
|
||||
return of_lanes::<vec4>(lanes_of(this) / f32x4::splat(by)) if lanes_held();
|
||||
return vec4{ this.x / by, this.y / by, this.z / by, this.w / by };
|
||||
}
|
||||
|
||||
pub def vec4::div(&this, other: &vec4): vec4 {
|
||||
return of_lanes::<vec4>(lanes_of(this) / lanes_of(other)) if lanes_held();
|
||||
return vec4{ this.x / other.x, this.y / other.y, this.z / other.z, this.w / other.w };
|
||||
}
|
||||
|
||||
pub def vec4::negate(&this): vec4 {
|
||||
return of_lanes::<vec4>(-lanes_of(this)) if lanes_held();
|
||||
return vec4{ -this.x, -this.y, -this.z, -this.w };
|
||||
}
|
||||
|
||||
pub def vec4::equals(&this, other: &vec4): bool {
|
||||
return this.x == other.x && this.y == other.y && this.z == other.z && this.w == other.w;
|
||||
}
|
||||
|
||||
pub def vec4::dot(&this, other: vec4): f32 {
|
||||
if lanes_held() {
|
||||
val p := lanes_of(this) * lanes_of(&other);
|
||||
return p[0] + p[1] + p[2] + p[3];
|
||||
}
|
||||
return this.x * other.x + this.y * other.y + this.z * other.z + this.w * other.w;
|
||||
}
|
||||
|
||||
pub val vec4::length: f32 {
|
||||
get => math::sqrt(this.dot(this));
|
||||
}
|
||||
|
||||
#! The length squared, which needs no square root, to compare lengths by.
|
||||
pub val vec4::length_squared: f32 {
|
||||
get => this.dot(this);
|
||||
}
|
||||
|
||||
#! The vector at a length of 1, or none where it has no length. A number at a
|
||||
#! time, which an executable outruns lanes with, the square root between them.
|
||||
pub def vec4::normalized(&this): vec4 {
|
||||
val l := math::sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
|
||||
return vec4{} if l == 0;
|
||||
val k := 1 / l;
|
||||
return vec4{ this.x * k, this.y * k, this.z * k, this.w * k };
|
||||
}
|
||||
|
||||
# The methods below work their lanes themselves rather than calling the
|
||||
# operators', since an operator called within one is left a call.
|
||||
|
||||
#! The vector a fraction of the way to another.
|
||||
pub def vec4::lerp(&this, to: vec4, t: f32): vec4 {
|
||||
if lanes_held() {
|
||||
val from := lanes_of(this);
|
||||
return of_lanes::<vec4>(from + (lanes_of(&to) - from) * f32x4::splat(t));
|
||||
}
|
||||
return vec4{
|
||||
this.x + (to.x - this.x) * t, this.y + (to.y - this.y) * t, this.z + (to.z - this.z) * t, this.w + (to.w - this.w) * t,
|
||||
};
|
||||
}
|
||||
|
||||
#! The lesser of each pair of numbers, as `math::min` gives it.
|
||||
pub def vec4::min(&this, other: vec4): vec4 {
|
||||
return of_lanes::<vec4>(lanes_of(this).min(&lanes_of(&other))) if lanes_held();
|
||||
return vec4{ math::min(this.x, other.x), math::min(this.y, other.y), math::min(this.z, other.z), math::min(this.w, other.w) };
|
||||
}
|
||||
|
||||
#! The greater of each pair of numbers, as `math::max` gives it.
|
||||
pub def vec4::max(&this, other: vec4): vec4 {
|
||||
return of_lanes::<vec4>(lanes_of(this).max(&lanes_of(&other))) if lanes_held();
|
||||
return vec4{ math::max(this.x, other.x), math::max(this.y, other.y), math::max(this.z, other.z), math::max(this.w, other.w) };
|
||||
}
|
||||
|
||||
#! Each number's distance from zero.
|
||||
pub def vec4::abs(&this): vec4 {
|
||||
return of_lanes::<vec4>(lanes_of(this).abs()) if lanes_held();
|
||||
return vec4{ math::abs(this.x), math::abs(this.y), math::abs(this.z), math::abs(this.w) };
|
||||
}
|
||||
|
||||
#! Its first three.
|
||||
pub val vec4::xyz: vec3 {
|
||||
get => vec3{ this.x, this.y, this.z };
|
||||
}
|
||||
Reference in new issue
Block a user