Files
physics/linear/vec3.sakura
T
2026-10-27 22:20:40 -05:00

154 lines
5.4 KiB
Plaintext

#! 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,
#! and as one `? :`, which it outruns returning early with by twice.
pub def vec3::normalized(&this): vec3 {
val l := math::sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
return l > 0 ? vec3{ this.x / l, this.y / l, this.z / l } : vec3{};
}
# 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) };
}