Files
physics/linear/vec4.sakura
T

125 lines
4.6 KiB
Plaintext
Raw Normal View History

2026-10-27 22:08:25 -05:00
#! 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,
#! and as one `? :`, which it outruns returning early with by twice.
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 l > 0 ? vec4{ this.x / l, this.y / l, this.z / l, this.w / l } : vec4{};
}
# 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 };
}