physics/src/math.rs#
Source: lib/physics/src/math.rs
math -- the vectors, rotations and boxes of the crate, in f64, with operations that give the same bits on
every target: + - * / and a correctly rounded square root, never a fused multiply-add (Rust does not
contract a * b + c on its own); sin, cos and atan2 from rt's portable math (docs/ARM64.md, ARM2), which is
the same code on every target.
fn sqrt(x: f64) -> f64The square root, correctly rounded (IEEE 754), so the same bits on every target, NaN included:rt::fx::sqrtd, the processor's instruction where rt has it (sqrtsdon x86-64,fsqrton ARM64), elsewhere (the browser, the x86-64 Android emulator) [sqrt_portable]. A negative argument gives NaN, -0.0 gives -0.0.fn sqrt_portable(x: f64) -> f64The correctly rounded square root from f64 operations and one 64 x 64 bit product (rt::fx::sqrt_portable): Newton's method gives an estimate within a few units in the last place, the integer test (R - 1/2)^2 < X <= (R + 1/2)^2 on the significand picks the right neighbour. Equal to the processor's square root (sqrtsd) for every input, NaN included. Public for the tests.fn sincos(x: f64) -> (f64, f64)sin and cos ofx(radians) from rt's portable math: the same bits on every target (within half an ulp of the exact value)fn sin(x: f64) -> f64sin ofx(radians), the same bits on every targetfn cos(x: f64) -> f64cos ofx(radians), the same bits on every targetfn atan2(y: f64, x: f64) -> f64atan2(y, x) in radians, the same bits on every targetfn min(a: f64, b: f64) -> f64the smaller of two numbers (bwhen they are equal or one is NaN), the same on every targetfn max(a: f64, b: f64) -> f64the larger of two numbers (bwhen they are equal or one is NaN)fn clamp(x: f64, lo: f64, hi: f64) -> f64xlimited to [lo, hi] (lo when x is NaN)struct Vec3A point or a direction in metres,f64(docs/PHYSICS.md, "f64"). Converts from and to[f64; 3], render3d's positions.x: f64-- metres (or a direction's component)y: f64z: f64
impl Vec3
const ZERO: Vec3 = Vec3::new(0.0, 0.0, 0.0)(0, 0, 0)const X: Vec3 = Vec3::new(1.0, 0.0, 0.0)the unit vectors along the axes (Y is up)const Y: Vec3 = Vec3::new(0.0, 1.0, 0.0)const Z: Vec3 = Vec3::new(0.0, 0.0, 1.0)const fn new(x: f64, y: f64, z: f64) -> Vec3(x, y, z)const fn splat(v: f64) -> Vec3(v, v, v)fn dot(self, o: Vec3) -> f64the dot product, summed x, y, z in this orderfn cross(self, o: Vec3) -> Vec3the cross product (right-handed)fn length_sq(self) -> f64the squared lengthfn length(self) -> f64the length (correctly rounded root)fn try_normalize(self) -> Option<Vec3>the unit vector in this direction, None for a zero (or not finite) vectorfn normalize_or_zero(self) -> Vec3the unit vector in this direction; a zero vector stays zerofn min(self, o: Vec3) -> Vec3the smaller component by componentfn max(self, o: Vec3) -> Vec3the larger component by componentfn abs(self) -> Vec3the absolute value of each componentfn mul_elem(self, o: Vec3) -> Vec3component by componentfn lerp(self, b: Vec3, t: f64) -> Vec3a + (b - a) * tfn max_elem(self) -> f64the largest componentfn is_finite(self) -> boolevery component is finiteconst fn to_array(self) -> [f64; 3][x, y, z] (render3d's positions)fn any_perpendicular(self) -> Vec3a unit vector perpendicular to this unit vector (chosen the same way on every target)struct QuatA rotation as a unit quaternion (x, y, z, w),f64. render3d keeps rotations as[f32; 4]in the same order: [Quat::from_f32], [Quat::to_f32].x: f64-- the vector part (sin of half the angle times the axis)y: f64z: f64w: f64-- the scalar part (cos of half the angle)
impl Quat
const IDENTITY: Quat = Quat { x: 0.0, y: 0.0, z: 0.0, w: 1.0 }no rotationconst fn new(x: f64, y: f64, z: f64, w: f64) -> Quatas given (normalise it with [Quat::normalize] unless it is a unit quaternion already)fn from_axis_angle(axis: Vec3, angle: f64) -> Quatthe rotation byangleradians about the unit vectoraxis(right-handed)fn from_yaw_pitch(yaw: f64, pitch: f64) -> Quatthe rotation about +Y byyaw, then about the rotated +X bypitch(a camera's or a figure's turn)fn from_rotation_arc(from: Vec3, to: Vec3) -> Quatthe shortest rotation that turns the unit vectorfrominto the unit vectortofn from_f32(q: [f32; 4]) -> Quatfrom render3d's[f32; 4](x, y, z, w)fn to_f32(self) -> [f32; 4]to render3d's[f32; 4](x, y, z, w)fn normalize(self) -> Quatthe unit quaternion in this direction (the identity for a zero quaternion)fn conjugate(self) -> Quatthe inverse rotation of a unit quaternionfn mul(self, o: Quat) -> Quatthe product self * o, which rotates byofirst and then byselffn rotate(self, v: Vec3) -> Vec3vrotated (a unit quaternion)fn inverse_rotate(self, v: Vec3) -> Vec3vrotated back (by the conjugate)fn to_mat3(self) -> Mat3the rotation as a matrixfn is_identity(self) -> boolis this the identity, bit for bit (no rotation work needed)fn from_scaled_axis(v: Vec3) -> Quatthe rotation by |v| radians about the direction ofv(a rotation vector; zero gives the identity): exact, so a body spinning at a constant rate turns by its rate times the timefn to_scaled_axis(self) -> Vec3the rotation vector of a unit quaternion: its axis times its angle in [0, pi]. The inverse of [Quat::from_scaled_axis] for vectors up to pi long; a longer one gives the same rotation by its angle in [0, pi]fn dot(self, o: Quat) -> f64the 4D dot product (the cosine of half the angle between two unit quaternions, up to the sign)fn is_finite(self) -> boolevery component finitestruct Mat3A 3 x 3 matrix by columns (a rotation from [Quat::to_mat3])cols: [Vec3; 3]-- the columns: the images of X, Y and Z
impl Mat3
const IDENTITY: Mat3 = Mat3 { cols: [Vec3::X, Vec3::Y, Vec3::Z] }the unit matrixfn mul_vec(&self, v: Vec3) -> Vec3M vfn mul_vec_t(&self, v: Vec3) -> Vec3M^T v (the inverse of a rotation)const ZERO: Mat3 = Mat3 { cols: [Vec3::ZERO, Vec3::ZERO, Vec3::ZERO] }the zero matrixconst fn diagonal(d: Vec3) -> Mat3the matrix withdon the diagonalconst fn from_cols(x: Vec3, y: Vec3, z: Vec3) -> Mat3the matrix with these columnsfn at(&self, r: usize, c: usize) -> f64the element in rowrand columncfn transpose(&self) -> Mat3M^Tfn mul(&self, n: &Mat3) -> Mat3the product M Nfn add(&self, n: &Mat3) -> Mat3M + Nfn sub(&self, n: &Mat3) -> Mat3M - Nfn scale(&self, s: f64) -> Mat3s Mfn determinant(&self) -> f64the determinantfn inverse(&self) -> Option<Mat3>the inverse, None when the matrix is singular, not finite, or its inverse is not (and when even its rows and columns brought to one size leave a determinant that under- or overflows: a condition number beyond about 10^300)fn rotated(&self, q: Quat) -> Mat3R M R^T: a tensor of a body's frame (its inertia) in the frame rotated byqfn outer(a: Vec3, b: Vec3) -> Mat3the outer product a b^Tfn skew(v: Vec3) -> Mat3the matrix of the cross product withv(skew(v) w = v x w)fn trace(&self) -> f64the sum of the diagonalfn is_finite(&self) -> boolevery element finitestruct IsoA placement: rotate, then move (no scale). The pose of a body and of a query shape.pos: Vec3-- where the local origin liesrot: Quat-- the rotation (a unit quaternion)
impl Iso
const IDENTITY: Iso = Iso { pos: Vec3::ZERO, rot: Quat::IDENTITY }at the origin, not rotatedconst fn new(pos: Vec3, rot: Quat) -> Isorotated byrot, then moved toposconst fn at(pos: Vec3) -> Isomoved topos, not rotatedfn transform_point(&self, p: Vec3) -> Vec3a local point in world coordinatesfn inverse_transform_point(&self, p: Vec3) -> Vec3a world point in local coordinatesfn transform_vector(&self, v: Vec3) -> Vec3a local direction in world coordinatesfn inverse_transform_vector(&self, v: Vec3) -> Vec3a world direction in local coordinatesfn mul(&self, o: &Iso) -> Isoself * o: first o, then selffn inv_mul(&self, o: &Iso) -> Isothe placement oforelative to self: self^-1 * ostruct AabbAn axis-aligned box in metresmin: Vec3-- the corner with the smallest coordinatesmax: Vec3-- the corner with the largest coordinates
impl Aabb
const EMPTY: Aabb = Aabb { min: Vec3::splat(f64::INFINITY), max: Vec3::splat( ...the empty box (min above max), the start of a unionconst fn new(min: Vec3, max: Vec3) -> Aabbthe box between two cornersfn from_center(c: Vec3, h: Vec3) -> Aabbthe box aroundcwith half sizeshfn union(&self, o: &Aabb) -> Aabbthe box around bothfn add_point(&self, p: Vec3) -> Aabbthe box around this one andpfn expanded(&self, m: f64) -> Aabbgrown bymon every sidefn intersects(&self, o: &Aabb) -> boolthe boxes touch or overlapfn contains(&self, o: &Aabb) -> boololies inside (or on the border)fn contains_point(&self, p: Vec3) -> boolplies inside (or on the border)fn center(&self) -> Vec3the middlefn half_extents(&self) -> Vec3half the size on each axisfn half_area(&self) -> f64half the surface (the cost measure of the trees; 0 for the empty box)fn is_valid(&self) -> boolthe box is not empty and finitefn ray_enter(&self, o: Vec3, inv: Vec3, tmax: f64) -> Option<f64>where the ray o + t d (inv = 1 / d per axis) enters the box, if it does within [0, tmax]