Expand description
A linear algebra library for gamedev and graphics.
§Conventions
§Angles
All angles are in radians unless the function name explicitly states otherwise.
The exceptions are focal_length_to_fov and fov_to_focal_length, which
work in degrees to match common camera/lens tooling conventions.
§Matrix Layout
Matrices are stored in row-major order in memory. The element m[row * cols + col]
accesses the element at a given row and column.
§Handedness
The library does not enforce a single coordinate system handedness. Functions that
depend on handedness — such as perspective projection — are provided in both variants
and indicate their convention explicitly in the function name (e.g. _lh_yup for
left-handed y-up, _rh_yup for right-handed y-up).
Modules§
- mat
- multi dimensional row-major matrix with generic mat2, mat3, mat34 and mat4 implementations there is a mat43 type for mat34 transposes but it does not support the full functionality
- num
- base traits and operations for scalar numbers, signed numbers, integers and floats
- prelude
- export float types and all functions for qick and easy integration
- quat
- generic quaternion for varying floating point precision
- swizz
- module containing vector swizzling traits
- vec
- multi dimensional column vector with generic vec2, vec3 and vec4 implementations
Enums§
- Classification
- classification for tests vs planes (behind, infront or intersects)
Functions§
- aabb_
vs_ aabb - returns true if the aabb defined by
aabb_min1toaabb_max1intersectsaabb_min2-aabb_max2 - aabb_
vs_ frustum - returns true if the aabb defined by
aabb_pos(centre) andaabb_extentis inside the frustum defined by 6 planes packed as vec4’s.xyz = normal, .w = plane distance - aabb_
vs_ obb - returns true if the aabb defined by
aabb_mintoaabb_maxoverlaps obb defined by matrixobb, where the matrix transforms an aabb with -1 to 1 extents into an obb - aabb_
vs_ plane - returns the classification of the 3D aabb defined as
aabb_mintoaabb_maxvs the plane defined by point on planexand normaln - aabb_
vs_ sphere - returns true if the aabb defined by
aabb_mintoaabb_maxintersects the sphere (or circle) centred atswith radiusr - abs
- returns the absolute (positive) value of
a - acos
- returns the arc-cosine of
vwhere the valuevis in radians - almost_
identity - returns soft clipping (in a cubic fashion) of
x; let m be the threshold (anything above m stays unchanged), and n the value things will take when the signal is zero - approx
- returns true if value
ais approximately equal to valuebwithin the specified epsiloneps - asin
- returns the arc-sine of
vwhere the valuevis in radians - atan
- returns the arc-tangent of
vwhere the valuevis in radians - atan2
- returns the arc-tangent of
vwhere the valuevis in radians - azimuth_
altitude_ to_ xyz - returns an
xyzdirectional unit vector converted fromazimuthandaltitudeangles in radians - barycentric
- returns the barycentric coordinate
(u, v, w)of pointpinside trianglet1-t2-t3 - capsule_
vs_ capsule - returns true if the capsule
cp0-cp1with radiuscr0overlaps the capsulecp2-cp3with radiuscr1 - capsule_
vs_ plane - returns the classification of a capsule defined by line
c1-c2with radiusrvs a plane defined by point on planexand normaln - ceil
- returns the ceil’d value of
a(round up to nearest integer) - chebyshev_
normalize - returns a Chebyshev normalized unit vector of
a(where the normalization projects onto the unit cube) - clamp
- returns the value
xclamped to the range ofminandmax - closest_
point_ on_ aabb - returns the closest point on the aabb defined by
aabb_minandaabb_maxto pointp, if the point is inside the aabb it will returnp - closest_
point_ on_ cone - returns the closest point to
pon the cone defined bycpposition, with directioncvheighthand radiusr - closest_
point_ on_ convex_ hull - returns the clostest point to
pon the edge of the convex hull defined by point list ‘hull’ with clockwise winding - closest_
point_ on_ line_ segment - returns the closest point on the line
l1-l2to pointp - closest_
point_ on_ obb - returns the closest point to point
pon the obb defined bymatwhich will transform an aabb with extents -1 to 1 into an obb - closest_
point_ on_ plane - returns the closest point on the plane to point
pwhere the plane is defined by point on planexand normaln - closest_
point_ on_ polygon - returns the clostest point to
pon the edge of the polygon defined by point listpoly. - closest_
point_ on_ ray - returns the closest point to
pon the the ray starting atr0with directionrv - closest_
point_ on_ sphere - returns the closest point from
pon sphere or circle centred atswith radiusr - closest_
point_ on_ triangle - returns the closest point to
pon the triangle defined byt1-t2-t3 - cone_
vs_ plane - return the classification of cone defined by position
cp, directioncvwith heighthand radius at the base ofrvs the plane defined by pointxand normaln - convex_
hull_ from_ points - returns a convex hull wound clockwise from point cloud
points - convex_
hull_ vs_ convex_ hull - returns true if the convex hull
convex0overlapsconvex1where convex hull is an array of vertices forming a 2D convex polygon - copysign
- returns the value
awith the same sign as second parametersign - cos
- returns the cosine of
vwhere the valuevis in radians - cosh
- returns the hyperbolic cosine of
vwhere the valuevis in radians - cross
- returns the vector cross product of
a x b, makes sense only for 3 or 7 dimensional vectors - cube
- returns value
acubed (raised to the power 3) - cubic_
interpolate - returns the cubic interpolation of bezier control points
p1-p2-p3-p4with percentage t - cubic_
pulse - returns a cubic pulse (y position on a graph for
x); equivalent to:smoothstep(c-w,c,x)-smoothstep(c,c+w,x) - cubic_
tangent - returns the tangent of bezier control points
p1-p2-p3-p4with percentage t - deg_
to_ rad - returns the radian value converted from value
awhich is specified in degrees - dist
- returns scalar distance between 2 points
aandb(magnitude of the vector between the 2 points) - dist2
- returns scalar squared distance between 2 points
aandbto avoid using sqrt - distance
- returns scalar distance between 2 points
aandb(magnitude of the vector between the 2 points) - distance_
on_ line - returns the distance parameter t of point
pprojected along the linel1-l2, the value is not clamped to the line segment extents - distance_
on_ ray - returns the distance parameter t of point
pprojected along the rayr0with directionrv, the value is not clamped to 0 or the start of the ray - dot
- returns the vector dot product between
a . b - exp
- returns the base-e exponential function of
v, which is e raised to the powerv - exp2
- returns 2 raised to the given power
v - exp_
impulse - returns an exponential impulse (y position on a graph for
x);kcontrols the stretching of the function - exp_
step - returns an exponential step (y position on a graph for
x);kis control parameter,nis power which gives sharper curves. - exp_
sustained_ impulse - returns an exponential sustained impulse (y position on a graph for
x); control on the width of attack withkand release withf - floor
- returns the floored value of
a(round down to nearest integer) - fmod
- returns the floating point remainder of
a / b - focal_
length_ to_ fov - returns field of view in degrees converted from focal length with the specified aperture_width
- fov_
to_ focal_ length - returns focal length converted from field of view
fovin degrees with the specified aperture_width - frac
- returns the fractional part of value
a - gain
- returns gain (y position on a graph for
x); remapping the unit interval into the unit interval by expanding the sides and compressing the center - get_
triangle_ normal - returns the normalized unit vector normal of triangle
t1-t2-t3 - gjk_2d
- returns true if the 2d convex hull
convex0overlaps withconvex1using the gjk algorithm - gjk_3d
- returns true if the 3D convex hull
convex0overlaps withconvex1using the gjk algorithm - gjk_
mesh_ support_ function - finds support vertices for gjk based on convex meshses where
convex0andconvex1are an array of vertices that form a convex hull - hsv_
to_ rgb - returns an rgb value in 0-1 range converted from
hsvin 0-1 range - integral_
smoothstep - returns the integral smoothstep of
xit’s derivative is never larger than 1 - length
- returns the scalar magnitude or length of vector
a - lerp
- returns a value interpolated between edges
e0ande1by percentaget - line_
segment_ vs_ line_ segment - returns the intersection point if the line segment
l1-l2intersects withs1-s2 - line_
segment_ vs_ plane - returns the intersection point of the line segment
l1-l2and plane defined by point on planexand normaln, returnsNoneif no intersection exists - line_
vs_ line - returns the intersection point if the infinite line
l1-l2intersects withs1-s2 - line_
vs_ plane - returns the intersection point of the infinite line defined as point on line
l0and directionlvwith the plane defined by point on planexand normaln - log
- returns the logarithm of
vinbase - log2
- returns the binary (base-2) logarithm of
v - log10
- returns the common (base-10) logarithm of
v - mad
- returns fused multiply add
m * a + b - mag
- returns the scalar magnitude or length of vector
a - mag2
- returns the scalar magnitude or length of vector
asquared to avoid using sqrt - map_
to_ range - remap
vwithinin_start->in_endrange to the new rangeout_start->out_end - max
- returns the maximum of
aandb - min
- returns the minimum of
aandb - min_max
- returns the tuple (min, max) of the value
aandmin_maxtuple (min, max) - modf
- returns the value
vbroken down into a tuple (fractional, integer) parts - morton_
1 - returns the number even bits extracted from
xas set bits in the return; value0b010101returns0b111 - morton_
2 - returns the number of bits divisible by 3 in
x. value0b001001001returns0b111 - morton_
to_ xy - returns the
x,ygrid position for morten order indexd - morton_
to_ xyz - returns the
x,y,zgrid position for morten order indexd - morton_
xy - returns the morten order index from
x,yposition - morton_
xyz - returns the morten order index from
x,y,zposition - nlerp
- returns a value interpolated between edges
e0ande1by percentagetwith the result being normalised - normalize
- returns a normalized unit vector of
a - obb_
vs_ obb - returns true if the aabb defined by
obb0overlapsobb1where the obb’s are defined by a matrix and the matrix transforms an aabb with -1 to 1 extents into an obb - parabola
- returns a parabola (y position on a graph for
x); usekto control its shape - pcurve
- returns a power curve (y position on a graph for
x); this is a generalziation of the parabola - perp
- returns the perpendicular vector of
aperforming anti-clockwise rotation by 90 degrees - plane_
distance - returns the distance to the plane define by a point on the plane
xand normal of the planen - plane_
from_ normal_ and_ point - returns a plane packed into Vec4 in the form
.xyz = plane normal, .w = plane distance (constant)fromx(point on plane) andn(planes normal) - point_
aabb_ distance - returns the distance that point
pis from an aabb defined byaabb_mintoaabb_max - point_
cone_ distance - returns the distance from point
pto the cone defined by positioncp, with heighthand radius at the base ofr - point_
convex_ hull_ distance - returns the distance from point
pto the edge of the convex hull defined by point list ‘hull’ with clockwise winding - point_
inside_ aabb - returns true if point
pis inside the aabb defined byaabb_minandaabb_max - point_
inside_ cone - returns true if point
pis inside cone defined by positioncpfacing directioncvwith heighthand radiusr - point_
inside_ convex_ hull - returns true if the point
pis inside the 2D convex hull defined by point listhullwith clockwise winding - point_
inside_ frustum - returns true if the point
pis inside the frustum defined by 6 planes packed as vec4’s.xyz = normal, .w = plane distance - point_
inside_ obb - returns true if the point
pis inside the obb defined bymatwhich will transform an aabb with extents -1 to 1 into an obb - point_
inside_ polygon - returns true if point
pis inside the polygon defined by point listpoly - point_
inside_ sphere - returns true if sphere (or circle) with centre
sand radiusrcontains pointp - point_
inside_ triangle - returns true if the point
pis inside the triangle defined byt1-t2-t3 - point_
line_ segment_ distance - returns the distance that point
pis from the line segment defined byl1-l2 - point_
obb_ distance - returns the unsigned distance from point
pto the obb defined by matrixobb, where the matrix transforms a unit cube from -1 to 1 into an obb - point_
plane_ distance - returns the distance to the plane from point
pwhere the plane is defined by point on planexand normaln - point_
polygon_ distance - returns the distance from point
pto the edge of the polygon defined by point list ‘poly’ - point_
sphere_ distance - returns the unsigned distance from point
p0to the sphere (or 2d circle) centred ats0with radiusr - point_
triangle_ distance - returns the distance point
pis from a triangle defined byt1-t2-t3 - point_
vs_ plane - returns the classification of point
pvs the plane defined by point on planexand normaln - poly_
impulse - returns a quadratic impulse (y position on a graph for
x);nis the degree of the polynomial andkcontrols the stretching of the function - powf
- returns value
araised to the floating point power of valueb - powi
- returns value
araised to the integer power of valueb - project_
to_ ndc - returns the 3D normalized device coordinate of point
pprojected byview_projectionmatrix, performing homogenous divide - project_
to_ sc - returns the 2D screen coordinate of 3D point
pprojected withview_projection, performing homogenous divide andviewportcorrection assumes screen coordinates are vup in the y-axis y.0 = bottom y.height = top - project_
to_ sc_ vdown - returns the 2D screen coordinate of 3D point
pprojected withview_projection, performing homogenous divide andviewportcorrection coordinates are vdown in the y-axis vdown = y.0 = top y.height = bottom - quad_
impulse - returns an quadratic impulse (y position on a graph for
x);kcontrols the stretching of the function - rad_
to_ deg - returns the degree value converted from value
awhich is specified in radians - ray_
vs_ aabb - returns the intersection point of the ray with origin
r0and directionrvwith the aabb defined byaabb_minandaabb_max - ray_
vs_ capsule - returns the intersection point of ray wih origin
r0and directionrvagainst the capsule with linec0-c1and radiuscr - ray_
vs_ cylinder - returns true if there is an intersection between ray wih origin
r0and directionrvagainst the cylinder with linec0-c1and radiuscrthe intersection point is return as anOptionif it exists. - ray_
vs_ line_ segment - returns the intersection point if the ray with origin
r0and directionrvintersects the line segmentl1-l2 - ray_
vs_ obb - returns the intersection of the 3D ray with origin
r0and directionrvwith the obb defined bymat - ray_
vs_ plane - returns the intersection point of the ray defined as origin of ray
r0and directionrvwith the plane defined by point on planexand normaln - ray_
vs_ sphere - returns the intersection point of ray with origin
r0and directionrvagainst the sphere (or circle) centred ats0with radiusr - ray_
vs_ triangle - returns the intersection point of ray
r0and normalized directionrvwith trianglet0-t1-t2 - recip
- returns the reciprocal of value
a - rgb_
to_ hsv - returns a hsv value in 0-1 range converted from
rgbin 0-1 range - rgba8_
to_ vec4 - returns a vec4 of rgba in 0-1 range from a packed
rgbawhich is inside u32 (4 bytes, 0xRRGGBBAA) - rotate_
2d - returns the vec2 rotated anti-clockwise rotation by radian
angle - round
- returns the value
arounded to closest integer floor ifa < 0.5or ceil ifa >= 0.5 - rsqrt
- returns the reciprocal square root of value
a - saturate
- returns the saturated value of
xinto to the 0-1 range, this is the same asclamp(x, 0.0, 1.0) - scalar_
triple - returns the scalar triple product of
a x b x c, makes sense only for 3 dimensional vectors - shortest_
line_ segment_ between_ line_ and_ line_ segment - returns the shortest line segment between 2 line segments
p1-p2andp3-p4as an option tuple where.0is the point on line segment 1 and.1is the point on line segment 2 - shortest_
line_ segment_ between_ line_ segments - returns the shortest line segment between 2 line segments
p1-p2andp3-p4as an option tuple where.0is the point on line segment 1 and.1is the point on line segment 2 - shortest_
line_ segment_ between_ lines - returns the shortest line segment between 2 lines
p1-p2andp3-p4as an option tuple where.0is the point on line segment 1 and.1is the point on line segment 2 - signum
- returns -1 if value
ais negative, 1 if positive and 0 if zero (integers) - sin
- returns the sine of
vwhere the valuevis in radians - sin_cos
- returns a tuple of (sin(v), cos(v)) of
vwhere the valuevis in radian - sinc
- returns a sin curve (y position on a graph for
x); can be used for some bouncing behaviors. givekdifferent integer values to tweak the amount of bounces - sinh
- returns the hyperbolic sine of v where the value v is in radians
- slerp
- returns a value spherically interpolated between edges
e0ande1by percentaget - smooth_
start2 - returns value
tbetween the rangecanddwith offsetbcreating smooth easing at the start (t^2) - smooth_
start3 - returns value
tbetween the rangecanddwith offsetbcreating smooth easing at the start (t^3) - smooth_
start4 - returns value
tbetween the rangecanddwith offsetbcreating smooth easing at the start (t^4) - smooth_
start5 - returns value
tbetween the rangecanddwith offsetbcreating smooth easing at the start (t^5) - smooth_
stop2 - returns value
tbetween the rangecanddwith offsetbcreating smooth easing at the end of t (t^2) - smooth_
stop3 - returns value
tbetween the rangecanddwith offsetbcreating smooth easing at the end of t (t^3) - smooth_
stop4 - returns value
tbetween the rangecanddwith offsetbcreating smooth easing at the end of t (t^4) - smooth_
stop5 - returns value
tbetween the rangecanddwith offsetbcreating smooth easing at the end of t (t^5) - smoothstep
- returns the hermite interpolated value between edge
e0ande1by percentaget - sphere_
vs_ capsule - returns true if the sphere or circle at centre
s1with radiusr1intersects capsulec0-c1with radiuscr - sphere_
vs_ frustum - returns true if the sphere with centre
sand radiusris inside the frustum defined by 6 planes packed as vec4’s.xyz = normal, .w = plane distance - sphere_
vs_ obb - returns true if the sphere with centre
s0and radiusr0overlaps obb defined by matrixobb, where the matrix transforms a unit cube with extents -1 to 1 into an obb - sphere_
vs_ plane - returns the classification of sphere defined by centre
sand radiusrvs the plane defined by point on planexand normaln - sphere_
vs_ sphere - returns true if the sphere or circle at centre
s1with radiusr1intersectss2-r2 - sqr
- returns value
asquared (raised to the power 2) - sqrt
- returns the square root of value
a - step
- returns 1 if
a > bor 0 otherwise - tan
- returns the tangent of
vwhere the valuevis in radians - tanh
- returns the hyperbolic tangent of
vwhere the valuevis in radians - trunc
- truncates value
a- removing the fractional part, truncating to an integer - unproject_
ndc - returns the unprojected 3D world position of point
pwhich is specified in normalized device coordinates - unproject_
sc - returns the unprojected 3D world position of screen coordinate
passumes screen coordinates are vup in the y-axis y.0 = bottom y.height = top - unproject_
sc_ vdown - returns the unprojected 3D world position of screen coordinate
pcoordinates are vdown in the y-axis vdown = y.0 = top y.height = bottom - vec4_
to_ rgba8 - returns a packed u32 containing rgba8 (4 bytes, R8G8B8A8) converted from a Vec4
vof rgba in 0-1 range - vector_
triple - returns the vector triple product of
a x b x c, mainly used for 3D vectors, but with a 2D specialisation leveraging z-up - xyz_
to_ azimuth_ altitude - returns
(azimuth, altitude)in radians converted from directional unit vectorxyz