#! 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::(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(); }