Math module

Fixed-size vectors/colors, matrices, transforms, rays, orthonormal bases, and other mathy things.

Darts provides many commonly-used vector math data types, including n-dimensional vectors (Vec), rays (Ray), boxes (Box), and n x n matrices (Mat). Behind the scenes, darts uses the excellent linalg library for this.

Each of these structures is templated based on both the dimension and the underlying datatype. Darts defines typedefs for low dimensions and common datatypes. The pattern for the typedefs is: DataDimType, where Data is Vec, Ray, or Box, and Type is a single-character shorthand for the underlying datatype:

  • f: for 32-bit single-precision float
  • d: for a 64-bit double-precision floating point number
  • i: for an integer, int32_t
  • u: for an unsigned integer, uint32_t
  • c: for a char, uint8_t

For instance, a vector of three 32-bit floating point values has a typedef Vec3f, and a 2D box of integer values would be Box2i.

RGB and RGBA colors can be represented by Color3 and Color4, which are just aliases for Vec3 and Vec4. There are typedefs for Color3Type and Color4Type.

For the matrix (Mat) the pattern is MatDimDimType. For instance, Mat33f for a 3x3 matrix of floats, or Mat44d for a 4x4 matrix of doubles.

Darts also provides two data type specifically designed for working in 3D: a 3D orthonormal basis class (#ONB), and linear 3D transformation (Transform) represented using a 4x4 matrix.

Classes

template<int N, typename T>
struct Box
An N-D axis-aligned bounding box consisting of two N-D points min and max.
template<size_t N, typename T>
struct Ray
Simple ray segment data structure.
struct Transform
Homogeneous coordinate transformation.

Functions

template<typename T>
auto coordinate_system(const Vec3<T>& z) -> std::pair<Vec3<T>, Vec3<T>>
Construct an orthonormal coordinate system given one vector a.

Basic darts vector/matrix/color types

template<int N, class T>
using Vec = la::vec<T, N>
Generic N dimensional vector.
template<class T, int M, int N>
using Mat = la::mat<T, M, N>
Generic M x N matrix.
template<int N, class T>
using Color = la::vec<T, N>
Generic N dimensional color.
template<class T>
using Color3 = Color<3, T>
RGB color of type T.
template<class T>
using Color4 = Color<4, T>
RGBA color of type T.

Interpolation utilities

template<typename T>
auto mod(T a, T b) -> T
Always-positive modulo operation.

Color utilities

Working with colors

auto to_sRGB(const Color3f& c) -> Color3f
Convert from linear RGB to sRGB.
auto to_linear_RGB(const Color3f& c) -> Color3f
Convert from sRGB to linear RGB.
auto luminance(const Color3f& c) -> float
Return the associated luminance.
auto viridis(float t) -> Color3f
Evaluate the Matplotlib viridis colormap.
auto inferno(float t) -> Color3f
Evaluate the Matplotlib inferno colormap.
auto magma(float t) -> Color3f
Evaluate the Matplotlib magma colormap.
auto plasma(float t) -> Color3f
Evaluate the Matplotlib plasma colormap.

Math constants

A few useful constants

#define M_PI
$\pi$
#define INV_PI
$\frac{1}{\pi}$
#define INV_TWOPI
$\frac{1}{2\pi}$
#define INV_FOURPI
$\frac{1}{4\pi}$
#define SQRT_TWO
$\sqrt{2}$
#define INV_SQRT_TWO
$\frac{1}{\sqrt{2}}$

Typedef documentation

template<int N, class T>
using Vec = la::vec<T, N>

Generic N dimensional vector.

template<class T, int M, int N>
using Mat = la::mat<T, M, N>

Generic M x N matrix.

template<int N, class T>
using Color = la::vec<T, N>

Generic N dimensional color.

template<class T>
using Color3 = Color<3, T>

RGB color of type T.

template<class T>
using Color4 = Color<4, T>

RGBA color of type T.

Function documentation

template<typename T>
std::pair<Vec3<T>, Vec3<T>> coordinate_system(const Vec3<T>& z)

Construct an orthonormal coordinate system given one vector a.

Parameters
in The coordinate system's local z axis direction.
Returns The local x and y-axes orthogonal to z.

template<typename T>
T mod(T a, T b)

Always-positive modulo operation.

Color3f to_sRGB(const Color3f& c)

Convert from linear RGB to sRGB.

Color3f to_linear_RGB(const Color3f& c)

Convert from sRGB to linear RGB.

float luminance(const Color3f& c)

Return the associated luminance.

Color3f viridis(float t)

Evaluate the Matplotlib viridis colormap.

Color3f inferno(float t)

Evaluate the Matplotlib inferno colormap.

Color3f magma(float t)

Evaluate the Matplotlib magma colormap.

Color3f plasma(float t)

Evaluate the Matplotlib plasma colormap.

Define documentation

#define M_PI

$\pi$

#define INV_PI

$\frac{1}{\pi}$

#define INV_TWOPI

$\frac{1}{2\pi}$

#define INV_FOURPI

$\frac{1}{4\pi}$

#define SQRT_TWO

$\sqrt{2}$

#define INV_SQRT_TWO

$\frac{1}{\sqrt{2}}$