Header file:<ply-math.h>Namespace:ply2D and 3D Math

The math library provides small POD-style types for vectors, rectangles, matrices, colors and rotations. Most types expose public fields, support inline arithmetic operators and perform runtime bounds checks in operator[] when assertions are enabled.

Scalar Helpers

The header also defines several scalar constants and free functions that are used throughout the rest of the math API.

Constants
static constexpr float Pi
static constexpr double DPi
Functions
float square(float v)
float roundNearest(float x)
float roundUp(float value)
float roundDown(float value)
float wrap(float value, float range)
u16 floatToHalf(const char* srcFloat)
float mix(float a, float b, float t)
float unmix(float a, float b, float mixed)
float stepTowards(float start, float target, float amount)
float fastSin(float rad)
float fastCos(float rad)
Float2 fastCosSin(float rad)
float easeInAndOut(float t)

Common Helpers

float square(float v)

Returns v * v.

float mix(float a,  float b,  float t)

Performs linear interpolation between a and b.

float unmix(float a,  float b,  float mixed)

Returns the interpolation parameter that would produce mixed between a and b.

float stepTowards(float start,  float target,  float amount)

Moves start toward target by at most amount without overshooting.

float wrap(float value,  float range)

Wraps value into the half-open interval [0, range). range must be positive.

Conversion And Approximation

u16 floatToHalf(const charsrcFloat)

Converts the bit pattern of a 32-bit float to a 16-bit half-float encoding.

float fastSin(float rad)

Fast sine approximation. Use it when speed matters more than exact precision.

float fastCos(float rad)

Fast cosine approximation. Use it when speed matters more than exact precision.

Float2 fastCosSin(float rad)

Returns approximate cosine and sine together as {cos(rad), sin(rad)}.

float easeInAndOut(float t)

Smoothstep-style easing curve over the range [0, 1].

Bool2

Bool2 stores the result of a per-component 2D comparison.

Bool2(bool x, bool y)

Related Helpers

bool all(const Bool2& v)
bool any(const Bool2& v)
Bool2(bool x,  bool y)

Constructs a two-component boolean vector.

bool all(const Bool2v)

Returns true only if every component is true.

bool any(const Bool2v)

Returns true if at least one component is true.

Bool3

Bool3 stores the result of a per-component 3D comparison.

Bool3(bool x, bool y, bool z)

Related Helpers

bool all(const Bool3& v)
bool any(const Bool3& v)
Bool3(bool x,  bool y,  bool z)

Constructs a three-component boolean vector.

bool all(const Bool3v)

Returns true only if every component is true.

bool any(const Bool3v)

Returns true if at least one component is true.

Bool4

Bool4 stores the result of a per-component 4D comparison.

Bool4(bool x, bool y, bool z, bool w)

Related Helpers

bool all(const Bool4& v)
bool any(const Bool4& v)
Bool4(bool x,  bool y,  bool z,  bool w)

Constructs a four-component boolean vector.

bool all(const Bool4v)

Returns true only if every component is true.

bool any(const Bool4v)

Returns true if at least one component is true.

Float2

Float2 represents a 2D vector, point or size.

Constructors and Conversion
Float2(float t)
Float2(float x, float y)
explicit operator Int2() const
Accessors
floatoperator[](u32 index)
float operator[](u32 index) const
floatr()
float r() const
floatg()
float g() const
Magnitude
float lengthSquared() const
float length() const
bool isUnitLength() const
Float2 normalized() const
Float2 safeNormalized(const Float2& fallback, float epsilon) const
Swizzle
Float2 swizzle(u32 i0, u32 i1) const
Float3 swizzle(u32 i0, u32 i1, u32 i2) const
Float4 swizzle(u32 i0, u32 i1, u32 i2, u32 i3) const

Related Helpers

float dot(const Float2& a, const Float2& b)
float cross(const Float2& a, const Float2& b)
Float2 clamp(const Float2& v, const Float2& mins, const Float2& maxs)
Float2 abs(const Float2& a)
Float2 pow(const Float2& a, const Float2& b)
Float2 min(const Float2& a, const Float2& b)
Float2 max(const Float2& a, const Float2& b)
Float2 roundUp(const Float2& value)
Float2 roundDown(const Float2& value)
Float2 roundNearest(const Float2& value)
Float2 mix(const Float2& a, const Float2& b, const Float2& t)
Float2 unmix(const Float2& a, const Float2& b, const Float2& mixed)
Float2 stepTowards(const Float2& start, const Float2& target, float amount)

Construction And Access

Float2(float x,  float y)

Constructs a vector from explicit x and y components.

floatoperator[](u32 index)
float operator[](u32 index) const

Returns the component at the specified index with runtime bounds checking.

Length And Direction

Float2 normalized() const

Returns a unit-length copy.

Float2 safeNormalized(const Float2fallback,  float epsilon) const

Returns fallback when the vector is too close to zero length.

Related Helpers

float dot(const Float2a,  const Float2b)

Measures directional similarity.

Float2 clamp(const Float2v,  const Float2mins,  const Float2maxs)

Performs per-component clamping.

Float2 min(const Float2a,  const Float2b)

Returns the per-component minimum.

Float2 max(const Float2a,  const Float2b)

Returns the per-component maximum.

Float2 abs(const Float2a)

Returns the per-component absolute value.

Float2 pow(const Float2a,  const Float2b)

Raises each component of a to the corresponding power in b.

Float2 roundUp(const Float2value)
Float2 roundDown(const Float2value)
Float2 roundNearest(const Float2value)

Rounds each component upward, downward or to the nearest integer value.

float cross(const Float2a,  const Float2b)

Returns the scalar 2D cross product a.x * b.y - a.y * b.x.

Float2 mix(const Float2a,  const Float2b,  const Float2t)

Performs per-component interpolation.

Float2 unmix(const Float2a,  const Float2b,  const Float2mixed)

Returns the per-component interpolation parameters that produce mixed.

Float2 stepTowards(const Float2start,  const Float2target,  float amount)

Moves each component toward the target by at most amount.

Float2 swizzle(u32 i0,  u32 i1) const
Float3 swizzle(u32 i0,  u32 i1,  u32 i2) const
Float4 swizzle(u32 i0,  u32 i1,  u32 i2,  u32 i3) const

Reorders components by index and returns a new vector of the requested size.

Float3

Float3 represents a 3D vector, point, direction or RGB triple.

Constructors and Conversion
Float3()
Float3(float t)
Float3(float x, float y, float z)
Float3(const Float2& v, float z)
Float3(const Color& c)
explicit operator Float2() const
explicit operator Int2() const
explicit operator Int3() const
Accessors
floatoperator[](u32 index)
float operator[](u32 index) const
floatr()
float r() const
floatg()
float g() const
floatb()
float b() const
Magnitude
float lengthSquared() const
float length() const
bool isUnitLength() const
Float3 normalized() const
Float3 safeNormalized(const Float3& fallback, float epsilon) const
Swizzle
Float2 swizzle(u32 i0, u32 i1) const
Float3 swizzle(u32 i0, u32 i1, u32 i2) const
Float4 swizzle(u32 i0, u32 i1, u32 i2, u32 i3) const

Related Helpers

float dot(const Float3& a, const Float3& b)
Float3 cross(const Float3& a, const Float3& b)
Float3 clamp(const Float3& v, const Float3& mins, const Float3& maxs)
Float3 abs(const Float3& a)
Float3 pow(const Float3& a, const Float3& b)
Float3 min(const Float3& a, const Float3& b)
Float3 max(const Float3& a, const Float3& b)
Float3 roundUp(const Float3& value)
Float3 roundDown(const Float3& value)
Float3 roundNearest(const Float3& value)
Float3 mix(const Float3& a, const Float3& b, const Float3& t)
Float3 unmix(const Float3& a, const Float3& b, const Float3& mixed)
Float3 stepTowards(const Float3& start, const Float3& target, float amount)
Float3 getNoncollinear(const Float3& unitVec)

Construction And Access

Float3(float x,  float y,  float z)
Float3(const Float2v,  float z)

Constructs a 3D vector either from three components or by extending a Float2.

floatoperator[](u32 index)
float operator[](u32 index) const

Returns the component at the specified index with runtime bounds checking.

Length And Orientation

Float3 normalized() const

Returns a unit-length copy.

Float3 safeNormalized(const Float3fallback,  float epsilon) const

Returns fallback if the vector is too close to zero length.

Related Helpers

float dot(const Float3a,  const Float3b)

Computes projection similarity.

Float3 cross(const Float3a,  const Float3b)

Returns the perpendicular 3D vector.

Float3 getNoncollinear(const Float3unitVec)

Returns a convenient axis that is not too closely aligned with unitVec.

Float3 clamp(const Float3v,  const Float3mins,  const Float3maxs)

Performs per-component clamping.

Float3 abs(const Float3a)

Returns the per-component absolute value.

Float3 pow(const Float3a,  const Float3b)

Raises each component of a to the corresponding power in b.

Float3 min(const Float3a,  const Float3b)

Returns the per-component minimum.

Float3 max(const Float3a,  const Float3b)

Returns the per-component maximum.

Float3 roundUp(const Float3value)
Float3 roundDown(const Float3value)
Float3 roundNearest(const Float3value)

Rounds each component upward, downward or to the nearest integer value.

Float3 mix(const Float3a,  const Float3b,  const Float3t)

Performs per-component interpolation.

Float3 unmix(const Float3a,  const Float3b,  const Float3mixed)

Returns the per-component interpolation parameters that produce mixed.

Float3 stepTowards(const Float3start,  const Float3target,  float amount)

Moves each component toward the target by at most amount.

Float4

Float4 is commonly used for homogeneous coordinates, RGBA values and quaternion storage.

Constructors and Conversion
Float4()
Float4(float t)
Float4(float x, float y, float z, float w)
Float4(const Float3& v, float w)
Float4(const Float2& v, float z, float w)
Float4(const Color& color)
explicit operator Float2() const
explicit operator Float3() const
explicit operator Int2() const
explicit operator Int3() const
explicit operator Int4() const
explicit operator Quaternion() const
explicit operator Color() const
Accessors
floatoperator[](u32 index)
float operator[](u32 index) const
floatr()
float r() const
floatg()
float g() const
floatb()
float b() const
floata()
float a() const
Magnitude
float lengthSquared() const
float length() const
bool isUnitLength() const
Float4 normalized() const
Float4 safeNormalized(const Float4& fallback, float epsilon) const
Swizzle
Float2 swizzle(u32 i0, u32 i1) const
Float3 swizzle(u32 i0, u32 i1, u32 i2) const
Float4 swizzle(u32 i0, u32 i1, u32 i2, u32 i3) const

Related Helpers

float dot(const Float4& a, const Float4& b)
Float4 clamp(const Float4& v, const Float4& mins, const Float4& maxs)
Float4 abs(const Float4& a)
Float4 pow(const Float4& a, const Float4& b)
Float4 min(const Float4& a, const Float4& b)
Float4 max(const Float4& a, const Float4& b)
Float4 roundUp(const Float4& value)
Float4 roundDown(const Float4& value)
Float4 roundNearest(const Float4& value)
Float4 mix(const Float4& a, const Float4& b, const Float4& t)
Float4 unmix(const Float4& a, const Float4& b, const Float4& mixed)
Float4 stepTowards(const Float4& start, const Float4& target, float amount)

Construction And Conversion

Float4(float x,  float y,  float z,  float w)

Constructs a four-component vector directly.

Float4(const Colorcolor)

Constructs a vector from normalized RGBA values derived from Color.

explicit operator Quaternion() const

Converts Float4 to a quaternion with the same four components.

explicit operator Color() const

Converts to a packed color. This asserts that all components lie in [0, 1].

Length And Component Operations

Float4 normalized() const

Returns a unit-length copy.

Related Helpers

float dot(const Float4a,  const Float4b)

Computes the four-dimensional dot product.

Float4 clamp(const Float4v,  const Float4mins,  const Float4maxs)

Performs per-component clamping.

Float4 abs(const Float4a)

Returns the per-component absolute value.

Float4 pow(const Float4a,  const Float4b)

Raises each component of a to the corresponding power in b.

Float4 min(const Float4a,  const Float4b)

Returns the per-component minimum.

Float4 max(const Float4a,  const Float4b)

Returns the per-component maximum.

Float4 roundUp(const Float4value)
Float4 roundDown(const Float4value)
Float4 roundNearest(const Float4value)

Rounds each component upward, downward or to the nearest integer value.

Float4 mix(const Float4a,  const Float4b,  const Float4t)

Performs per-component interpolation.

Float4 unmix(const Float4a,  const Float4b,  const Float4mixed)

Returns the per-component interpolation parameters that produce mixed.

Float4 stepTowards(const Float4start,  const Float4target,  float amount)

Moves each component toward the target by at most amount.

Float4 swizzle(u32 i0,  u32 i1,  u32 i2,  u32 i3) const

Reorders components by index and returns a new four-component vector.

Rect

Rect represents a 2D axis-aligned rectangle using Float2 mins and Float2 maxs.

Constructors and Conversion
Rect()
Rect(float v)
Rect(const Float2& v)
Rect(const Float2& mins, const Float2& maxs)
Rect(float minX, float minY, float maxX, float maxY)
static Rect fromSize(const Float2& mins, const Float2& size)
static Rect empty()
static Rect full()
explicit operator IntRect() const
Geometry
Float2 size() const
bool isEmpty() const
float width() const
float height() const
Float2 mix(const Float2& arg) const
Float2 unmix(const Float2& arg) const
Float2 mid() const
Rect mix(const Rect& arg) const
Rect unmix(const Rect& arg) const
Float2 clamp(const Float2& arg) const
Float2 xminYmax() const
Float2 xmaxYmin() const
bool contains(const Float2& arg) const
bool contains(const Rect& arg) const
bool intersects(const Rect& arg) const

Related Helpers

Rect makeUnion(const Rect& a, const Rect& b)
Rect intersect(const Rect& a, const Rect& b)
Rect inflate(const Rect& a, const Float2& b)
Rect roundNearest(const Rect& a)
Rect rectFromFov(float fovY, float aspect)

Construction And Queries

static Rect fromSize(const Float2mins,  const Float2size)

Constructs a rectangle from a minimum corner and a size vector.

Float2 size() const

Returns the rectangle size.

Float2 mid() const

Returns the midpoint of the rectangle.

bool contains(const Float2arg) const

Tests whether a point lies inside the rectangle using half-open bounds: mins <= p < maxs.

bool intersects(const Rectarg) const

Tests whether two rectangles overlap.

Related Helpers

Rect makeUnion(const Recta,  const Rectb)

Returns the bounding rectangle of both inputs.

Rect intersect(const Recta,  const Rectb)

Returns the overlapping region of the two rectangles.

Rect rectFromFov(float fovY,  float aspect)

Computes the view-frustum rectangle on the z = -1 plane for a perspective projection.

Rect inflate(const Recta,  const Float2b)

Expands the rectangle by the specified amount in all directions.

Rect roundNearest(const Recta)

Rounds both rectangle corners to the nearest integer values.

AABB

AABB is the 3D equivalent of Rect, using Float3 mins and Float3 maxs.

Constructors
AABB()
AABB(const Float3& v)
AABB(const Float3& mins, const Float3& maxs)
static AABB empty()
static AABB full()
static AABB fromSize(const Float3& mins, const Float3& size)
Geometry
Float3 size() const
bool isEmpty() const
float width() const
float height() const
float depth() const
Float3 mix(const Float3& arg) const
Float3 unmix(const Float3& arg) const
Float3 mid() const
AABB mix(const AABB& arg) const
AABB unmix(const AABB& arg) const
Float3 clamp(const Float3& arg) const
Float3 xminYmax() const
Float3 xmaxYmin() const
bool contains(const Float3& arg) const
bool contains(const AABB& arg) const
bool intersects(const AABB& arg) const

Related Helpers

AABB makeUnion(const AABB& a, const AABB& b)
AABB intersect(const AABB& a, const AABB& b)
AABB inflate(const AABB& a, const Float3& b)
AABB roundNearest(const AABB& a)

Construction And Queries

static AABB fromSize(const Float3mins,  const Float3size)

Constructs an axis-aligned bounding box from a minimum corner and a size vector.

Float3 size() const

Returns the box dimensions.

Float3 mid() const

Returns the box center point.

bool contains(const Float3arg) const

Tests point containment using half-open bounds.

bool intersects(const AABBarg) const

Tests whether two boxes overlap.

Related Helpers

AABB makeUnion(const AABBa,  const AABBb)

Returns the bounding box of both inputs.

AABB intersect(const AABBa,  const AABBb)

Returns the overlapping volume of the two boxes.

AABB inflate(const AABBa,  const Float3b)

Expands the box by the specified amount in all directions.

AABB roundNearest(const AABBa)

Rounds both box corners to the nearest integer values.

Int2

Int2 is the integer counterpart to Float2.

Int2()
Int2(int x)
Int2(int x, int y)
explicit operator Float2() const

Related Helpers

Int2 clamp(const Int2& v, const Int2& mins, const Int2& maxs)
Int2 abs(const Int2& a)
Int2 min(const Int2& a, const Int2& b)
Int2 max(const Int2& a, const Int2& b)
Int2(int x,  int y)

Constructs a two-component integer vector.

Related Helpers

Int2 clamp(const Int2v,  const Int2mins,  const Int2maxs)

Performs per-component integer clamping.

Int2 abs(const Int2a)

Returns the per-component absolute value.

Int2 min(const Int2a,  const Int2b)

Returns the per-component minimum.

Int2 max(const Int2a,  const Int2b)

Returns the per-component maximum.

Int3

Int3 is the integer counterpart to Float3.

Int3()
Int3(int x)
Int3(int x, int y, int z)
explicit operator Int2() const
explicit operator Float2() const
explicit operator Float3() const

Related Helpers

Int3 clamp(const Int3& v, const Int3& mins, const Int3& maxs)
Int3 abs(const Int3& a)
Int3 min(const Int3& a, const Int3& b)
Int3 max(const Int3& a, const Int3& b)
Int3(int x,  int y,  int z)

Constructs a three-component integer vector.

explicit operator Float3() const

Converts the vector to floating-point component values.

Related Helpers

Int3 clamp(const Int3v,  const Int3mins,  const Int3maxs)

Performs per-component integer clamping.

Int3 abs(const Int3a)

Returns the per-component absolute value.

Int3 min(const Int3a,  const Int3b)

Returns the per-component minimum.

Int3 max(const Int3a,  const Int3b)

Returns the per-component maximum.

Int4

Int4 is the integer counterpart to Float4.

Int4()
Int4(int x, int y, int z, int w)
explicit operator Int2() const
explicit operator Int3() const
explicit operator Float2() const
explicit operator Float3() const
explicit operator Float4() const

Related Helpers

Int4 clamp(const Int4& v, const Int4& mins, const Int4& maxs)
Int4 abs(const Int4& a)
Int4 min(const Int4& a, const Int4& b)
Int4 max(const Int4& a, const Int4& b)
Int4(int x,  int y,  int z,  int w)

Constructs a four-component integer vector.

explicit operator Float4() const

Converts the vector to floating-point component values.

Related Helpers

Int4 clamp(const Int4v,  const Int4mins,  const Int4maxs)

Performs per-component integer clamping.

Int4 abs(const Int4a)

Returns the per-component absolute value.

Int4 min(const Int4a,  const Int4b)

Returns the per-component minimum.

Int4 max(const Int4a,  const Int4b)

Returns the per-component maximum.

IntRect

IntRect is the integer counterpart to Rect.

Constructors and Conversion
IntRect()
IntRect(const Int2& v)
IntRect(const Int2& mins, const Int2& maxs)
IntRect(int minX, int minY, int maxX, int maxY)
static IntRect fromSize(const Int2& mins, const Int2& size)
static IntRect empty()
static IntRect full()
explicit operator Rect() const
Geometry
Int2 size() const
bool isEmpty() const
int width() const
int height() const
Int2 mid() const
Int2 clamp(const Int2& arg) const
Int2 xminYmax() const
Int2 xmaxYmin() const
bool contains(const Int2& arg) const
bool contains(const IntRect& arg) const
bool intersects(const IntRect& arg) const

Related Helpers

IntRect makeUnion(const IntRect& a, const IntRect& b)
IntRect intersect(const IntRect& a, const IntRect& b)
IntRect inflate(const IntRect& a, const Int2& b)

Construction And Queries

static IntRect fromSize(const Int2mins,  const Int2size)

Constructs a rectangle from an integer minimum corner and size.

Int2 size() const

Returns the integer size of the rectangle.

Int2 mid() const

Returns the integer midpoint of the rectangle.

bool contains(const Int2arg) const

Tests point containment using half-open bounds.

bool intersects(const IntRectarg) const

Tests rectangle overlap using half-open bounds.

Related Helpers

IntRect makeUnion(const IntRecta,  const IntRectb)

Returns the bounding rectangle of both inputs.

IntRect intersect(const IntRecta,  const IntRectb)

Returns the overlapping region of the two rectangles.

IntRect inflate(const IntRecta,  const Int2b)

Expands the rectangle by the specified amount in all directions.

Color

Color stores 8-bit RGBA components.

Color()
Color(StringView hex)
Color(u8 r, u8 g, u8 b, u8 a)

Related Helpers

float srgbToLinear(float s)
float linearToSrgb(float l)
Float3 srgbToLinear(const Float3& vec)
Float4 srgbToLinear(const Float4& vec)
Float3 linearToSrgb(const Float3& vec)
Float4 linearToSrgb(const Float4& vec)

Construction

Color(StringView hex)

Parses a color from a hexadecimal string representation.

Color(u8 r,  u8 g,  u8 b,  u8 a)

Constructs a packed RGBA color from 8-bit channels.

Related Helpers

float srgbToLinear(float s)

Converts a single-channel sRGB value to linear space.

float linearToSrgb(float l)

Converts a single-channel linear value to sRGB space.

Float3 srgbToLinear(const Float3vec)
Float4 srgbToLinear(const Float4vec)

Applies sRGB-to-linear conversion per component to RGB or RGBA vectors.

Float3 linearToSrgb(const Float3vec)
Float4 linearToSrgb(const Float4vec)

Applies linear-to-sRGB conversion per component to RGB or RGBA vectors.

Mat2x2

Mat2x2 represents a 2D linear transform stored in column-major form.

Mat2x2()
Mat2x2(const Float2& col0, const Float2& col1)
Float2operator[](u32 i)
const Float2operator[](u32 i) const
static Mat2x2 identity()
static Mat2x2 scale(const Float2& scale)
static Mat2x2 rotate(float radians)
static Mat2x2 fromComplex(const Float2& c)
Mat2x2 transposed() const

Related Helpers

bool operator==(const Mat2x2& a, const Mat2x2& b)
Float2 operator*(const Mat2x2& m, const Float2& v)
Mat2x2 operator*(const Mat2x2& a, const Mat2x2& b)

Factory Functions

static Mat2x2 identity()

Constructs the identity matrix.

static Mat2x2 scale(const Float2scale)

Constructs a 2D scale matrix.

static Mat2x2 rotate(float radians)

Constructs a 2D rotation matrix.

static Mat2x2 fromComplex(const Float2c)

Constructs a rotation matrix from a complex-number representation.

Related Helpers

Float2 operator*(const Mat2x2m,  const Float2v)

Applies the transform to a vector.

Mat2x2 operator*(const Mat2x2a,  const Mat2x2b)

Composes two transforms.

bool operator==(const Mat2x2a,  const Mat2x2b)

Returns true if all matrix elements are equal.

Mat3x3

Mat3x3 represents a 3D linear transform with no translation.

Mat3x3()
Mat3x3(const Float3& col0, const Float3& col1, const Float3& col2)
explicit Mat3x3(const Mat3x4& m)
explicit Mat3x3(const Mat4x4& m)
static Mat3x3 identity()
static Mat3x3 scale(const Float3& arg)
static Mat3x3 rotate(const Float3& unitAxis, float radians)
static Mat3x3 fromQuaternion(const Quaternion& q)
Float3operator[](u32 i)
const Float3operator[](u32 i) const
bool hasScale() const
Mat3x3 transposed() const

Related Helpers

bool operator==(const Mat3x3& a, const Mat3x3& b)
Float3 operator*(const Mat3x3& m, const Float3& v)
Mat3x3 operator*(const Mat3x3& a, const Mat3x3& b)
Mat3x3 makeBasis(const Float3& dstUnitFwd, const Float3& dstUp, const Float3& srcUnitFwd, const Float3& srcUnitUp)
Mat3x3 makeBasis(const Float3& dstUnitFwd, const Float3& srcFwd)

Factory Functions

static Mat3x3 rotate(const Float3unitAxis,  float radians)

Constructs a rotation matrix from an axis-angle pair.

static Mat3x3 fromQuaternion(const Quaternionq)

Constructs a rotation matrix from a quaternion.

bool hasScale() const

Returns true when the matrix contains scale or shear instead of only an orthonormal basis.

Related Helpers

Mat3x3 makeBasis(const Float3dstUnitFwd,  const Float3dstUp,  const Float3srcUnitFwd,  const Float3srcUnitUp)

Builds an orientation matrix that maps one forward/up basis to another.

Mat3x3 makeBasis(const Float3dstUnitFwd,  const Float3srcFwd)

Builds an orientation matrix from forward vectors using automatically chosen up vectors.

Float3 operator*(const Mat3x3m,  const Float3v)

Applies the linear transform to a vector.

Mat3x3 operator*(const Mat3x3a,  const Mat3x3b)

Composes two linear transforms.

bool operator==(const Mat3x3a,  const Mat3x3b)

Returns true if all matrix elements are equal.

Mat3x4

Mat3x4 stores a 3D affine transform: a 3x3 basis plus a translation column.

Mat3x4()
Mat3x4(const Float3& col0, const Float3& col1, const Float3& col2, const Float3& col3)
explicit Mat3x4(const Mat3x3& m, const Float3& pos)
explicit Mat3x4(const Mat4x4& m)
static Mat3x4 identity()
static Mat3x4 scale(const Float3& arg)
static Mat3x4 rotate(const Float3& unitAxis, float radians)
static Mat3x4 translate(const Float3& pos)
static Mat3x4 fromQuaternion(const Quaternion& q, const Float3& pos)
static Mat3x4 fromQuatPos(const QuatPos& qp)
Float3operator[](u32 i)
const Float3operator[](u32 i) const
const Mat3x3as_mat3() const
bool hasScale() const
Mat3x4 invertedOrtho() const

Related Helpers

bool operator==(const Mat3x4& a, const Mat3x4& b)
Float3 operator*(const Mat3x4& m, const Float3& v)
Float4 operator*(const Mat3x4& m, const Float4& v)
Mat3x4 operator*(const Mat3x4& a, const Mat3x4& b)

Factory Functions

static Mat3x4 translate(const Float3pos)

Constructs an affine transform representing translation only.

static Mat3x4 fromQuaternion(const Quaternionq,  const Float3pos)

Constructs an affine transform from quaternion rotation plus translation.

static Mat3x4 fromQuatPos(const QuatPosqp)

Constructs an affine transform from a QuatPos.

Related Helpers

const Mat3x3as_mat3() const

Returns the linear 3x3 portion of the transform.

Mat3x4 invertedOrtho() const

Computes a fast inverse for orthonormal affine transforms.

Float3 operator*(const Mat3x4m,  const Float3v)

Applies the affine transform to a 3D point or vector.

Float4 operator*(const Mat3x4m,  const Float4v)

Applies the affine transform to a homogeneous vector.

Mat3x4 operator*(const Mat3x4a,  const Mat3x4b)

Composes two affine transforms.

bool operator==(const Mat3x4a,  const Mat3x4b)

Returns true if all matrix elements are equal.

Mat4x4

Mat4x4 stores a full 4D matrix for homogeneous transforms and projection matrices.

Constructors
Mat4x4()
Mat4x4(float t)
Mat4x4(const Float4& col0, const Float4& col1, const Float4& col2, const Float4& col3)
explicit Mat4x4(const Mat3x3& m, const Float3& pos)
explicit Mat4x4(const Mat3x4& m)
Factory Functions
static Mat4x4 identity()
static Mat4x4 scale(const Float3& arg)
static Mat4x4 rotate(const Float3& unitAxis, float radians)
static Mat4x4 translate(const Float3& pos)
static Mat4x4 fromQuaternion(const Quaternion& q, const Float3& pos)
static Mat4x4 fromQuatPos(const QuatPos& qp)
static Mat4x4 perspectiveProjection(const Rect& frustum, float zNear, float zFar, ClipNearType clipNear)
static Mat4x4 orthographicProjection(const Rect& rect, float zNear, float zFar, ClipNearType clipNear)
Access and Operations
Float4operator[](u32 i)
const Float4operator[](u32 i) const
Mat4x4 transposed() const
Mat4x4 invertedOrtho() const

Related Helpers

bool operator==(const Mat4x4& a, const Mat4x4& b)
Float4 operator*(const Mat4x4& m, const Float4& v)
Mat4x4 operator*(const Mat4x4& a, const Mat4x4& b)
Mat4x4 operator*(const Mat3x4& a, const Mat4x4& b)
Mat4x4 operator*(const Mat4x4& a, const Mat3x4& b)

Transform Factories

static Mat4x4 translate(const Float3pos)

Constructs a 4x4 translation transform.

static Mat4x4 fromQuaternion(const Quaternionq,  const Float3pos)

Constructs a 4x4 transform from quaternion rotation plus translation.

Projection Factories

static Mat4x4 perspectiveProjection(const Rectfrustum,  float zNear,  float zFar,  ClipNearType clipNear)

Constructs a perspective projection matrix. clipNear selects whether the near clip plane maps to 0 or -1.

static Mat4x4 orthographicProjection(const Rectrect,  float zNear,  float zFar,  ClipNearType clipNear)

Constructs an orthographic projection matrix. clipNear selects whether the near clip plane maps to 0 or -1.

Related Helpers

Mat4x4 operator*(const Mat4x4a,  const Mat4x4b)

Composes two matrices.

Float4 operator*(const Mat4x4m,  const Float4v)

Transforms a homogeneous vector.

Mat4x4 operator*(const Mat3x4a,  const Mat4x4b)
Mat4x4 operator*(const Mat4x4a,  const Mat3x4b)

Multiplies affine and full 4x4 transforms in either order.

bool operator==(const Mat4x4a,  const Mat4x4b)

Returns true if all matrix elements are equal.

Complex

Complex provides static helpers for 2D rotation using a Float2 complex-number representation.

static Float2 identity()
static Float2 fromAngle(float radians)
static float getAngle(const Float2& v)
static Float2 mul(const Float2& a, const Float2& b)
static Float2 identity()

Returns the unit complex value representing no rotation.

static Float2 fromAngle(float radians)

Converts an angle in radians to the equivalent complex-number representation.

static float getAngle(const Float2v)

Converts a complex-number representation back to an angle in radians.

static Float2 mul(const Float2a,  const Float2b)

Multiplies two complex values, which composes their rotations.

Quaternion

Quaternion stores a 3D rotation using x, y, z and w.

Quaternion()
Quaternion(float x, float y, float z, float w)
Quaternion(const Float3& v, float w)
static Quaternion identity()
static Quaternion fromAxisAngle(const Float3& unitAxis, float radians)
static Quaternion fromUnitVectors(const Float3& start, const Float3& end)
static Quaternion fromOrtho(const Mat3x3& m)
static Quaternion fromOrtho(const Mat4x4& m)
explicit operator Float3() const
explicit operator Float4() const
Quaternion inverted() const
Quaternion normalized() const
bool isUnitLength() const
Quaternion negatedIfCloserTo(const Quaternion& other) const

Related Helpers

Float3 operator*(const Quaternion& q, const Float3& v)
Quaternion operator*(const Quaternion& a, const Quaternion& b)
Quaternion mix(const Quaternion& a, const Quaternion& b, float f)

Factory Functions

static Quaternion identity()
static Quaternion fromAxisAngle(const Float3unitAxis,  float radians)

Constructs the identity rotation.

static Quaternion fromAxisAngle(const Float3unitAxis,  float radians)

Constructs an axis-angle rotation.

static Quaternion fromUnitVectors(const Float3start,  const Float3end)

Constructs the rotation that maps one unit vector onto another.

Normalization And Sign

Quaternion normalized() const

Returns a normalized quaternion.

Quaternion negatedIfCloserTo(const Quaternionother) const

Flips to the equivalent sign that is closer to other, which is useful before interpolation.

Related Helpers

Float3 operator*(const Quaternionq,  const Float3v)

Rotates a vector.

Quaternion operator*(const Quaterniona,  const Quaternionb)

Composes two rotations.

Quaternion mix(const Quaterniona,  const Quaternionb,  float f)

Linearly blends between two quaternion values.

QuatPos

QuatPos combines a quaternion rotation with a translation vector.

QuatPos()
QuatPos(const Quaternion& quat, const Float3& pos)
static QuatPos identity()
static QuatPos translate(const Float3& pos)
static QuatPos rotate(const Float3& unitAxis, float radians)
static QuatPos fromOrtho(const Mat3x4& m)
static QuatPos fromOrtho(const Mat4x4& m)
QuatPos inverted() const

Related Helpers

Float3 operator*(const QuatPos& qp, const Float3& v)
QuatPos operator*(const QuatPos& a, const QuatPos& b)
QuatPos operator*(const QuatPos& a, const Quaternion& b)
QuatPos operator*(const Quaternion& a, const QuatPos& b)

Factory Functions

static QuatPos identity()

Constructs the identity rigid transform.

static QuatPos translate(const Float3pos)

Constructs a rigid transform representing pure translation.

static QuatPos rotate(const Float3unitAxis,  float radians)

Constructs a rigid transform representing pure rotation.

Related Helpers

QuatPos inverted() const

Returns the inverse rigid transform.

Float3 operator*(const QuatPosqp,  const Float3v)

Applies the transform to a point.

QuatPos operator*(const QuatPosa,  const QuatPosb)

Composes two rigid transforms.

QuatPos operator*(const QuatPosa,  const Quaternionb)
QuatPos operator*(const Quaterniona,  const QuatPosb)

Applies an additional rotation on the right or left side of a rigid transform.

Cubic Bezier Helpers

The header ends with generic helpers for sampling cubic Bezier curves.

template <typename T
T sampleCubicBezier(const T& p0, const T& p1, const T& p2, const T& p3, float t)
template <typename T
T sampleCubicBezierDerivative(const T& p0, const T& p1, const T& p2, const T& p3, float t)
template <typename TT sampleCubicBezier(const Tp0,  const Tp1,  const Tp2,  const Tp3,  float t)

Evaluates the cubic Bezier curve at parameter t.

template <typename TT sampleCubicBezierDerivative(const Tp0,  const Tp1,  const Tp2,  const Tp3,  float t)

Evaluates the derivative curve at parameter t, which is useful for tangent calculations.