Initial Commit
This commit is contained in:
64 files changed
+13417
No files matched your search
@@ -0,0 +1,43 @@
|
||||
#! 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, and works it as lanes
|
||||
#! at once: adding, scaling, dots, lengths, and `normalized`, and what a matrix
|
||||
#! makes of a vector or another matrix, each of its columns scaled by a number
|
||||
#! splat into every lane, and added. What gathers one vector's numbers into
|
||||
#! other lanes, as a cross product and a quaternions' product would, it works a
|
||||
#! number at a time, as `simd::shuffle` gathers them a lane at a time. The
|
||||
#! virtual machine, which works lanes one at a time, works everything a number
|
||||
#! at a time, in the order the lanes do, so both ways give the same bits.
|
||||
#!
|
||||
#! A product is rounded before it's added, not fused with the sum as `fma` does:
|
||||
#! a column waiting on the fused sum before it is slower than one adding
|
||||
#! products worked at once, and the virtual machine would work each fused sum
|
||||
#! as `std::math` does without FMA3, many instructions for one.
|
||||
|
||||
module malunal::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,104 @@
|
||||
#! 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.
|
||||
|
||||
import std::serial;
|
||||
module malunal::linear;
|
||||
|
||||
#! A matrix of three by three, column after column: where it takes x, y, and z.
|
||||
@serial
|
||||
pub struct mat3 {
|
||||
@serial pub var x_axis: vec3;
|
||||
@serial pub var y_axis: vec3;
|
||||
@serial 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.
|
||||
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.
|
||||
pub def mat3::mul(&this, v: &vec3): vec3 {
|
||||
if lanes_held() {
|
||||
return of_lanes::<vec3>(lanes_of(&this.x_axis) * f32x4::splat(v.x) + lanes_of(&this.y_axis) * f32x4::splat(v.y)
|
||||
+ lanes_of(&this.z_axis) * f32x4::splat(v.z));
|
||||
}
|
||||
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);
|
||||
# Scaled by 0 where nothing undoes it, so it returns once.
|
||||
val k := det != 0 ? 1 / det : 0;
|
||||
return mat3{ a * k, b * k, c * k }.transposed();
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
#! 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;
|
||||
import std::serial;
|
||||
module malunal::linear;
|
||||
|
||||
#! A matrix of four by four, column after column. `std::serial` writes it as a
|
||||
#! sequence of its 16 numbers, in the same order.
|
||||
pub struct mat4 {
|
||||
pub var m: f32[16];
|
||||
}
|
||||
|
||||
pub def mat4::encode(&this, out: &mut serial::encoder): void {
|
||||
out.begin_array(16);
|
||||
for i in 0..16u {
|
||||
serial::encode(&this.m[i], out);
|
||||
}
|
||||
out.end_array();
|
||||
}
|
||||
|
||||
pub def mat4::decode(from: &serial::decoder, at: u32, given: serial::options): result<mat4, serial::error> {
|
||||
if from.kind_of(at) != serial::kind::array || from.count(at) != 16 {
|
||||
return serial::error::at(serial::error_kind::wrong_kind, from.place(at));
|
||||
}
|
||||
var made := mat4{};
|
||||
var item: u32 = 0;
|
||||
for i in 0..16u {
|
||||
item = i == 0 ? from.element(at, 0) : from.next_element(at, item);
|
||||
val read := serial::decode::<f32>(from, item, given) else (e) { return e; };
|
||||
made.m[i] = read;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! 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 a vector, as a projection makes a place of clip
|
||||
#! space: each column, scaled by one of its numbers, added.
|
||||
pub def mat4::mul(&this, v: &vec4): vec4 {
|
||||
if lanes_held() {
|
||||
return of_lanes::<vec4>(lanes_of(&this.m[0]) * f32x4::splat(v.x) + lanes_of(&this.m[4]) * f32x4::splat(v.y)
|
||||
+ lanes_of(&this.m[8]) * f32x4::splat(v.z) + lanes_of(&this.m[12]) * f32x4::splat(v.w));
|
||||
}
|
||||
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 {
|
||||
if lanes_held() {
|
||||
return of_lanes::<vec3>(lanes_of(&this.m[0]) * f32x4::splat(p.x) + lanes_of(&this.m[4]) * f32x4::splat(p.y)
|
||||
+ lanes_of(&this.m[8]) * f32x4::splat(p.z) + lanes_of(&this.m[12]));
|
||||
}
|
||||
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 {
|
||||
if lanes_held() {
|
||||
return of_lanes::<vec3>(lanes_of(&this.m[0]) * f32x4::splat(v.x) + lanes_of(&this.m[4]) * f32x4::splat(v.y)
|
||||
+ lanes_of(&this.m[8]) * f32x4::splat(v.z));
|
||||
}
|
||||
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
|
||||
#! the determinants of two by two its first two columns make, and its last
|
||||
#! two, each cofactor of three of them, which is half the work of the
|
||||
#! cofactors written out, a number at a time.
|
||||
pub def mat4::inverse(&this): mat4 {
|
||||
val a := &this.m;
|
||||
val s0 := a[0] * a[5] - a[4] * a[1];
|
||||
val s1 := a[0] * a[6] - a[4] * a[2];
|
||||
val s2 := a[0] * a[7] - a[4] * a[3];
|
||||
val s3 := a[1] * a[6] - a[5] * a[2];
|
||||
val s4 := a[1] * a[7] - a[5] * a[3];
|
||||
val s5 := a[2] * a[7] - a[6] * a[3];
|
||||
val c5 := a[10] * a[15] - a[14] * a[11];
|
||||
val c4 := a[9] * a[15] - a[13] * a[11];
|
||||
val c3 := a[9] * a[14] - a[13] * a[10];
|
||||
val c2 := a[8] * a[15] - a[12] * a[11];
|
||||
val c1 := a[8] * a[14] - a[12] * a[10];
|
||||
val c0 := a[8] * a[13] - a[12] * a[9];
|
||||
val det := s0 * c5 - s1 * c4 + s2 * c3 + s3 * c2 - s4 * c1 + s5 * c0;
|
||||
# Scaled by 0 where nothing undoes it, so it returns once.
|
||||
val k := det != 0 ? 1 / det : 0;
|
||||
return mat4{ m = [
|
||||
(a[5] * c5 - a[6] * c4 + a[7] * c3) * k,
|
||||
(a[2] * c4 - a[1] * c5 - a[3] * c3) * k,
|
||||
(a[13] * s5 - a[14] * s4 + a[15] * s3) * k,
|
||||
(a[10] * s4 - a[9] * s5 - a[11] * s3) * k,
|
||||
(a[6] * c2 - a[4] * c5 - a[7] * c1) * k,
|
||||
(a[0] * c5 - a[2] * c2 + a[3] * c1) * k,
|
||||
(a[14] * s2 - a[12] * s5 - a[15] * s1) * k,
|
||||
(a[8] * s5 - a[10] * s2 + a[11] * s1) * k,
|
||||
(a[4] * c4 - a[5] * c2 + a[7] * c0) * k,
|
||||
(a[1] * c2 - a[0] * c4 - a[3] * c0) * k,
|
||||
(a[12] * s4 - a[13] * s2 + a[15] * s0) * k,
|
||||
(a[9] * s2 - a[8] * s4 - a[11] * s0) * k,
|
||||
(a[5] * c1 - a[4] * c3 - a[6] * c0) * k,
|
||||
(a[0] * c3 - a[1] * c1 + a[2] * c0) * k,
|
||||
(a[13] * s1 - a[12] * s3 - a[14] * s0) * k,
|
||||
(a[8] * s3 - a[9] * s1 + a[10] * s0) * k,
|
||||
] };
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
#! 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;
|
||||
import std::serial;
|
||||
module malunal::linear;
|
||||
|
||||
#! A rotation, as a quaternion of length 1.
|
||||
@serial
|
||||
pub struct quat {
|
||||
@serial pub var x: f32;
|
||||
@serial pub var y: f32;
|
||||
@serial pub var z: f32;
|
||||
@serial 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 are scaled by
|
||||
#! the vector's numbers and added as lanes, which an executable works three
|
||||
#! times as fast as the crosses of the rotation's axis.
|
||||
pub def quat::mul(&this, v: &vec3): vec3 {
|
||||
return mat3::rotation(this) * 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 := this.length;
|
||||
if lanes_held() {
|
||||
return l > 0 ? of_lanes::<quat>(lanes_of(this) / f32x4::splat(l)) : quat::identity;
|
||||
}
|
||||
return l > 0 ? quat{ this.x / l, this.y / l, this.z / l, this.w / l } : quat::identity;
|
||||
}
|
||||
|
||||
#! 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;
|
||||
import std::serial;
|
||||
module malunal::linear;
|
||||
|
||||
#! A vector of two: a place or a direction in a plane.
|
||||
@serial
|
||||
pub struct vec2 {
|
||||
@serial pub var x: f32;
|
||||
@serial 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 := math::sqrt(this.x * this.x + this.y * this.y);
|
||||
return l > 0 ? vec2{ this.x / l, this.y / l } : vec2{};
|
||||
}
|
||||
|
||||
#! 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,138 @@
|
||||
#! 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;
|
||||
import std::serial;
|
||||
module malunal::linear;
|
||||
|
||||
#! A vector of three: a place or a direction in space.
|
||||
@serial
|
||||
pub struct vec3 {
|
||||
@serial pub var x: f32;
|
||||
@serial pub var y: f32;
|
||||
@serial 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 { return (other - this).length; }
|
||||
|
||||
#! The vector at a length of 1, or none where it has no length.
|
||||
pub def vec3::normalized(&this): vec3 {
|
||||
val l := this.length;
|
||||
return l > 0 ? this / l : vec3{};
|
||||
}
|
||||
|
||||
#! The vector a fraction of the way to another.
|
||||
pub def vec3::lerp(&this, to: vec3, t: f32): vec3 {
|
||||
return this + (to - this) * 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,116 @@
|
||||
#! 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;
|
||||
import std::serial;
|
||||
module malunal::linear;
|
||||
|
||||
#! A vector of four, as a point of space with its `w`.
|
||||
@serial
|
||||
pub struct vec4 {
|
||||
@serial pub var x: f32;
|
||||
@serial pub var y: f32;
|
||||
@serial pub var z: f32;
|
||||
@serial 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.
|
||||
pub def vec4::normalized(&this): vec4 {
|
||||
val l := this.length;
|
||||
return l > 0 ? this / l : vec4{};
|
||||
}
|
||||
|
||||
#! The vector a fraction of the way to another.
|
||||
pub def vec4::lerp(&this, to: vec4, t: f32): vec4 {
|
||||
return this + (to - this) * 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