JFLib

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) -> f64 The 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 (sqrtsd on x86-64, fsqrt on 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) -> f64 The 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 of x (radians) from rt's portable math: the same bits on every target (within half an ulp of the exact value)

  • fn sin(x: f64) -> f64 sin of x (radians), the same bits on every target

  • fn cos(x: f64) -> f64 cos of x (radians), the same bits on every target

  • fn atan2(y: f64, x: f64) -> f64 atan2(y, x) in radians, the same bits on every target

  • fn min(a: f64, b: f64) -> f64 the smaller of two numbers (b when they are equal or one is NaN), the same on every target

  • fn max(a: f64, b: f64) -> f64 the larger of two numbers (b when they are equal or one is NaN)

  • fn clamp(x: f64, lo: f64, hi: f64) -> f64 x limited to [lo, hi] (lo when x is NaN)

  • struct Vec3 A 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: f64
    • z: 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) -> f64 the dot product, summed x, y, z in this order

  • fn cross(self, o: Vec3) -> Vec3 the cross product (right-handed)

  • fn length_sq(self) -> f64 the squared length

  • fn length(self) -> f64 the length (correctly rounded root)

  • fn try_normalize(self) -> Option<Vec3> the unit vector in this direction, None for a zero (or not finite) vector

  • fn normalize_or_zero(self) -> Vec3 the unit vector in this direction; a zero vector stays zero

  • fn min(self, o: Vec3) -> Vec3 the smaller component by component

  • fn max(self, o: Vec3) -> Vec3 the larger component by component

  • fn abs(self) -> Vec3 the absolute value of each component

  • fn mul_elem(self, o: Vec3) -> Vec3 component by component

  • fn lerp(self, b: Vec3, t: f64) -> Vec3 a + (b - a) * t

  • fn max_elem(self) -> f64 the largest component

  • fn is_finite(self) -> bool every component is finite

  • const fn to_array(self) -> [f64; 3] [x, y, z] (render3d's positions)

  • fn any_perpendicular(self) -> Vec3 a unit vector perpendicular to this unit vector (chosen the same way on every target)

  • struct Quat A 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: f64
    • z: f64
    • w: 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 rotation

  • const fn new(x: f64, y: f64, z: f64, w: f64) -> Quat as given (normalise it with [Quat::normalize] unless it is a unit quaternion already)

  • fn from_axis_angle(axis: Vec3, angle: f64) -> Quat the rotation by angle radians about the unit vector axis (right-handed)

  • fn from_yaw_pitch(yaw: f64, pitch: f64) -> Quat the rotation about +Y by yaw, then about the rotated +X by pitch (a camera's or a figure's turn)

  • fn from_rotation_arc(from: Vec3, to: Vec3) -> Quat the shortest rotation that turns the unit vector from into the unit vector to

  • fn from_f32(q: [f32; 4]) -> Quat from 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) -> Quat the unit quaternion in this direction (the identity for a zero quaternion)

  • fn conjugate(self) -> Quat the inverse rotation of a unit quaternion

  • fn mul(self, o: Quat) -> Quat the product self * o, which rotates by o first and then by self

  • fn rotate(self, v: Vec3) -> Vec3 v rotated (a unit quaternion)

  • fn inverse_rotate(self, v: Vec3) -> Vec3 v rotated back (by the conjugate)

  • fn to_mat3(self) -> Mat3 the rotation as a matrix

  • fn is_identity(self) -> bool is this the identity, bit for bit (no rotation work needed)

  • fn from_scaled_axis(v: Vec3) -> Quat the rotation by |v| radians about the direction of v (a rotation vector; zero gives the identity): exact, so a body spinning at a constant rate turns by its rate times the time

  • fn to_scaled_axis(self) -> Vec3 the 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) -> f64 the 4D dot product (the cosine of half the angle between two unit quaternions, up to the sign)

  • fn is_finite(self) -> bool every component finite

  • struct Mat3 A 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 matrix

  • fn mul_vec(&self, v: Vec3) -> Vec3 M v

  • fn mul_vec_t(&self, v: Vec3) -> Vec3 M^T v (the inverse of a rotation)

  • const ZERO: Mat3 = Mat3 { cols: [Vec3::ZERO, Vec3::ZERO, Vec3::ZERO] } the zero matrix

  • const fn diagonal(d: Vec3) -> Mat3 the matrix with d on the diagonal

  • const fn from_cols(x: Vec3, y: Vec3, z: Vec3) -> Mat3 the matrix with these columns

  • fn at(&self, r: usize, c: usize) -> f64 the element in row r and column c

  • fn transpose(&self) -> Mat3 M^T

  • fn mul(&self, n: &Mat3) -> Mat3 the product M N

  • fn add(&self, n: &Mat3) -> Mat3 M + N

  • fn sub(&self, n: &Mat3) -> Mat3 M - N

  • fn scale(&self, s: f64) -> Mat3 s M

  • fn determinant(&self) -> f64 the determinant

  • fn 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) -> Mat3 R M R^T: a tensor of a body's frame (its inertia) in the frame rotated by q

  • fn outer(a: Vec3, b: Vec3) -> Mat3 the outer product a b^T

  • fn skew(v: Vec3) -> Mat3 the matrix of the cross product with v (skew(v) w = v x w)

  • fn trace(&self) -> f64 the sum of the diagonal

  • fn is_finite(&self) -> bool every element finite

  • struct Iso A placement: rotate, then move (no scale). The pose of a body and of a query shape.

    • pos: Vec3 -- where the local origin lies
    • rot: Quat -- the rotation (a unit quaternion)

impl Iso

  • const IDENTITY: Iso = Iso { pos: Vec3::ZERO, rot: Quat::IDENTITY } at the origin, not rotated

  • const fn new(pos: Vec3, rot: Quat) -> Iso rotated by rot, then moved to pos

  • const fn at(pos: Vec3) -> Iso moved to pos, not rotated

  • fn transform_point(&self, p: Vec3) -> Vec3 a local point in world coordinates

  • fn inverse_transform_point(&self, p: Vec3) -> Vec3 a world point in local coordinates

  • fn transform_vector(&self, v: Vec3) -> Vec3 a local direction in world coordinates

  • fn inverse_transform_vector(&self, v: Vec3) -> Vec3 a world direction in local coordinates

  • fn mul(&self, o: &Iso) -> Iso self * o: first o, then self

  • fn inv_mul(&self, o: &Iso) -> Iso the placement of o relative to self: self^-1 * o

  • struct Aabb An axis-aligned box in metres

    • min: Vec3 -- the corner with the smallest coordinates
    • max: 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 union
  • const fn new(min: Vec3, max: Vec3) -> Aabb the box between two corners
  • fn from_center(c: Vec3, h: Vec3) -> Aabb the box around c with half sizes h
  • fn union(&self, o: &Aabb) -> Aabb the box around both
  • fn add_point(&self, p: Vec3) -> Aabb the box around this one and p
  • fn expanded(&self, m: f64) -> Aabb grown by m on every side
  • fn intersects(&self, o: &Aabb) -> bool the boxes touch or overlap
  • fn contains(&self, o: &Aabb) -> bool o lies inside (or on the border)
  • fn contains_point(&self, p: Vec3) -> bool p lies inside (or on the border)
  • fn center(&self) -> Vec3 the middle
  • fn half_extents(&self) -> Vec3 half the size on each axis
  • fn half_area(&self) -> f64 half the surface (the cost measure of the trees; 0 for the empty box)
  • fn is_valid(&self) -> bool the box is not empty and finite
  • fn 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]

Source: docs/API.md in the JFLib repository.