math/numerics

sourcemath/numerics.sx

Mat4x4

pub type Mat4x4 = { m00: f64, m10: f64, m20: f64, m30: f64, m01: f64, m11: f64, m21: f64, m31: f64, m02: f64, m12: f64, m22: f64, m32: f64, m03: f64, m13: f64, m23: f64, m33: f64 }

4x4 matrix stored in column-major order

Fields

NameType
m00f64
m10f64
m20f64
m30f64
m01f64
m11f64
m21f64
m31f64
m02f64
m12f64
m22f64
m32f64
m03f64
m13f64
m23f64
m33f64

Vec3

pub type Vec3 = { x: f64, y: f64, z: f64 }

3D vector

Fields

NameType
xf64
yf64
zf64

EulerAngles

pub type EulerAngles = { roll: f64, pitch: f64, yaw: f64 }

Euler angles in radians: roll (X), pitch (Y), yaw (Z).

Fields

NameType
rollf64
pitchf64
yawf64

Quaternion

pub type Quaternion = { x: f64, y: f64, z: f64, w: f64 }

Quaternion stored as vector part (x, y, z) and scalar part (w).

Fields

NameType
xf64
yf64
zf64
wf64

wrap_signed_radians

private
fn wrap_signed_radians(angle: f64) f64

identity

pub fn (Mat4x4) identity() Mat4x4

Create identity matrix

zero

pub fn (Mat4x4) zero() Mat4x4

Create zero matrix

mul

pub fn (Mat4x4) mul(a: Mat4x4, b: Mat4x4) Mat4x4

Matrix multiplication

scale_scalar

pub fn (Mat4x4) scale_scalar(m: Mat4x4, s: f64) Mat4x4

Scalar multiplication

add

pub fn (Mat4x4) add(a: Mat4x4, b: Mat4x4) Mat4x4

Matrix addition

transpose

pub fn (Mat4x4) transpose(m: Mat4x4) Mat4x4

Matrix transpose

translate

pub fn (Mat4x4) translate(x: f64, y: f64, z: f64) Mat4x4

Create translation matrix

scale

pub fn (Mat4x4) scale(x: f64, y: f64, z: f64) Mat4x4

Create scale matrix

rotate_x

pub fn (Mat4x4) rotate_x(angle: f64) Mat4x4

Create rotation matrix around X axis

rotate_y

pub fn (Mat4x4) rotate_y(angle: f64) Mat4x4

Create rotation matrix around Y axis

rotate_z

pub fn (Mat4x4) rotate_z(angle: f64) Mat4x4

Create rotation matrix around Z axis

perspective

pub fn (Mat4x4) perspective(fov: f64, aspect: f64, near: f64, far: f64) Mat4x4

Create perspective projection matrix

ortho

pub fn (Mat4x4) ortho(left: f64, right: f64, bottom: f64, top: f64, near: f64, far: f64) Mat4x4

Create orthographic projection matrix

look_at

pub fn (Mat4x4) look_at(eye: Vec3, center: Vec3, up: Vec3) Mat4x4

Create look-at view matrix

det

pub fn (Mat4x4) det(m: Mat4x4) f64

Calculate determinant of 4x4 matrix

new

pub fn (Vec3) new(x: f64, y: f64, z: f64) Vec3

Create a new Vec3

new

pub fn (EulerAngles) new(roll: f64, pitch: f64, yaw: f64) EulerAngles

Create a new set of Euler angles in radians.

normalize

pub fn (EulerAngles) normalize(e: EulerAngles) EulerAngles

Wrap Euler angles into a signed [-PI, PI] range.

new

pub fn (Quaternion) new(x: f64, y: f64, z: f64, w: f64) Quaternion

Create a quaternion from its four components.

identity

pub fn (Quaternion) identity() Quaternion

Identity rotation quaternion.

dot

pub fn (Quaternion) dot(a: Quaternion, b: Quaternion) f64

Dot product between two quaternions.

length_squared

pub fn (Quaternion) length_squared(q: Quaternion) f64

Squared quaternion length.

length

pub fn (Quaternion) length(q: Quaternion) f64

Quaternion length.

scale

pub fn (Quaternion) scale(q: Quaternion, s: f64) Quaternion

Scale a quaternion by a scalar.

add

pub fn (Quaternion) add(a: Quaternion, b: Quaternion) Quaternion

Add two quaternions component-wise.

normalize

pub fn (Quaternion) normalize(q: Quaternion) Quaternion

Return a normalized quaternion.

conjugate

pub fn (Quaternion) conjugate(q: Quaternion) Quaternion

Conjugate of a quaternion.

inverse

pub fn (Quaternion) inverse(q: Quaternion) Quaternion

Inverse of a quaternion.

mul

pub fn (Quaternion) mul(a: Quaternion, b: Quaternion) Quaternion

Hamilton product of two quaternions.

from_axis_angle

pub fn (Quaternion) from_axis_angle(axis: Vec3, angle: f64) Quaternion

Construct a quaternion from a normalized axis and angle in radians.

from_euler

pub fn (Quaternion) from_euler(roll: f64, pitch: f64, yaw: f64) Quaternion

Construct a quaternion from roll (X), pitch (Y), yaw (Z) Euler angles in radians.

from_euler_angles

pub fn (Quaternion) from_euler_angles(e: EulerAngles) Quaternion

Construct a quaternion from Euler angles.

to_euler

pub fn (Quaternion) to_euler(q: Quaternion) EulerAngles

Convert a quaternion to roll/pitch/yaw Euler angles in radians.

rotate_vec3

pub fn (Quaternion) rotate_vec3(q: Quaternion, v: Vec3) Vec3

Rotate a vector by a quaternion.

to_mat4

pub fn (Quaternion) to_mat4(q: Quaternion) Mat4x4

Convert a quaternion into a 4x4 rotation matrix.

nlerp

pub fn (Quaternion) nlerp(a: Quaternion, b: Quaternion, t: f64) Quaternion

Normalized linear interpolation between two quaternions using the shortest path.

slerp

pub fn (Quaternion) slerp(a: Quaternion, b: Quaternion, t: f64) Quaternion

Spherical linear interpolation between two quaternions using the shortest path.

dot

pub fn (Vec3) dot(a: Vec3, b: Vec3) f64

Vector dot product

cross

pub fn (Vec3) cross(a: Vec3, b: Vec3) Vec3

Vector cross product

length

pub fn (Vec3) length(v: Vec3) f64

Vector length

normalize

pub fn (Vec3) normalize(v: Vec3) Vec3

Normalize vector

add

pub fn (Vec3) add(a: Vec3, b: Vec3) Vec3

Vector addition

sub

pub fn (Vec3) sub(a: Vec3, b: Vec3) Vec3

Vector subtraction

scale

pub fn (Vec3) scale(v: Vec3, s: f64) Vec3

Vector scalar multiplication

distance_squared

pub fn (Vec3) distance_squared(a: Vec3, b: Vec3) f64

Squared distance between two vectors.

distance

pub fn (Vec3) distance(a: Vec3, b: Vec3) f64

Distance between two vectors.

lerp

pub fn (Vec3) lerp(a: Vec3, b: Vec3, t: f64) Vec3

Linear interpolation between two vectors.