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