Skip to content

atl/math.hpp

Math helpers and the value types Vec3, Quat, Color and Uuid. Their math runs in WASM.

inline double sqrt(double x);

Square root.

Parameters

  • x (double)

Returns double

inline double abs(double x);

Absolute value.

Parameters

  • x (double)

Returns double

inline double sin(double x);

Trigonometry and pow, computed by Lua's math library.

Parameters

  • x (double)

Returns double

inline double cos(double x);

Trigonometry and pow, computed by Lua's math library.

Parameters

  • x (double)

Returns double

inline double tan(double x);

Trigonometry and pow, computed by Lua's math library.

Parameters

  • x (double)

Returns double

inline double asin(double x);

Trigonometry and pow, computed by Lua's math library.

Parameters

  • x (double)

Returns double

inline double acos(double x);

Trigonometry and pow, computed by Lua's math library.

Parameters

  • x (double)

Returns double

inline double atan2(double y, double x);

Trigonometry and pow, computed by Lua's math library.

Parameters

  • y (double)
  • x (double)

Returns double

inline double pow(double x, double y);

Trigonometry and pow, computed by Lua's math library.

Parameters

  • x (double)
  • y (double)

Returns double

inline double floor(double x);

Rounds down / up to a whole number.

Parameters

  • x (double)

Returns double

inline double ceil(double x);

Rounds down / up to a whole number.

Parameters

  • x (double)

Returns double

inline constexpr double PI = 3.14159265358979323846;

Pi.

inline constexpr double DEG = PI / 180.0;

Degrees to radians: 90 * sm::DEG.

inline double clamp(double x, double lo, double hi);

x limited to the range lo..hi.

Parameters

  • x (double)
  • lo (double)
  • hi (double)

Returns double

inline double lerp(double a, double b, double t);

Linear interpolation from a (t = 0) to b (t = 1).

Parameters

  • a (double)
  • b (double)
  • t (double)

Returns double

inline String toString(Vec3 v);

Text forms for format() and logging.

Parameters

  • v (Vec3)

Returns String

inline String toString(Quat q);

Text forms for format() and logging.

Parameters

  • q (Quat)

Returns String

inline String toString(Color c);

Text forms for format() and logging.

Parameters

  • c (Color)

Returns String

inline String toString(const Uuid &u);

Text forms for format() and logging.

Parameters

  • u (const Uuid &)

Returns String

A 3D vector (sm.vec3). Plain value type; arithmetic happens in WASM.

struct Vec3
double x = 0, y = 0, z = 0;
constexpr Vec3() = defaul;

The zero vector.

constexpr Vec3(double x, double y, double z);

A vector from its components.

Parameters

  • x (double)
  • y (double)
  • z (double)
static constexpr Vec3 zero();

(0, 0, 0) and (1, 1, 1).

Returns Vec3

static constexpr Vec3 one();

(0, 0, 0) and (1, 1, 1).

Returns Vec3

constexpr Vec3 operator+(Vec3 o) const;

Component-wise arithmetic, and scaling by a number.

Parameters

  • o (Vec3)

Returns Vec3

constexpr Vec3 operator-(Vec3 o) const;

Component-wise arithmetic, and scaling by a number.

Parameters

  • o (Vec3)

Returns Vec3

constexpr Vec3 operator*(Vec3 o) const;

Component-wise arithmetic, and scaling by a number.

Parameters

  • o (Vec3)

Returns Vec3

constexpr Vec3 operator/(Vec3 o) const;

Component-wise arithmetic, and scaling by a number.

Parameters

  • o (Vec3)

Returns Vec3

constexpr Vec3 operator*(double s) const;

Component-wise arithmetic, and scaling by a number.

Parameters

  • s (double)

Returns Vec3

constexpr Vec3 operator/(double s) const;

Component-wise arithmetic, and scaling by a number.

Parameters

  • s (double)

Returns Vec3

constexpr Vec3 operator-() const;

Component-wise arithmetic, and scaling by a number.

Returns Vec3

Vec3 &operator+=(Vec3 o);

Parameters

  • o (Vec3)

Returns Vec3 &

Vec3 &operator-=(Vec3 o);

Parameters

  • o (Vec3)

Returns Vec3 &

Vec3 &operator*=(double s);

Parameters

  • s (double)

Returns Vec3 &

Vec3 &operator/=(double s);

Parameters

  • s (double)

Returns Vec3 &

constexpr bool operator==(Vec3 o) const;

Exact equality of all components.

Parameters

  • o (Vec3)

Returns bool

constexpr double dot(Vec3 o) const;

Dot product.

Parameters

  • o (Vec3)

Returns double

constexpr Vec3 cross(Vec3 o) const;

Cross product.

Parameters

  • o (Vec3)

Returns Vec3

constexpr double length2() const;

Squared length (cheaper than length()).

Returns double

double length() const;

Length.

Returns double

double distance(Vec3 o) const;

Distance to another point.

Parameters

  • o (Vec3)

Returns double

Vec3 normalize() const;

Unit vector in the same direction; a zero vector stays zero.

Returns Vec3

constexpr double distance2(Vec3 o) const;

Squared distance to another point.

Parameters

  • o (Vec3)

Returns double

Vec3 safeNormalize(Vec3 fallback) const;

Unit vector in the same direction, or fallback for a zero vector.

Parameters

  • fallback (Vec3)

Returns Vec3

constexpr Vec3 lerp(Vec3 to, double t) const;

Linear interpolation towards to.

Parameters

  • to (Vec3)
  • t (double)

Returns Vec3

bool fuzzyEquals(Vec3 o, double eps = 1e-6) const;

True when the two vectors are within eps of each other.

Parameters

  • o (Vec3)
  • eps (double)

Returns bool

Vec3 abs() const;

Component-wise absolute value, floor and ceil.

Returns Vec3

Vec3 floor() const;

Component-wise absolute value, floor and ceil.

Returns Vec3

Vec3 ceil() const;

Component-wise absolute value, floor and ceil.

Returns Vec3

constexpr Vec3 min(Vec3 o) const;

Component-wise minimum and maximum.

Parameters

  • o (Vec3)

Returns Vec3

constexpr Vec3 max(Vec3 o) const;

Component-wise minimum and maximum.

Parameters

  • o (Vec3)

Returns Vec3

Vec3 rotate(double radians, Vec3 axis) const;

This vector rotated radians around axis (same convention as Quat::angleAxis).

Parameters

  • radians (double)
  • axis (Vec3)

Returns Vec3

Vec3 rotateX(double radians) const;

Rotation around a world axis.

Parameters

  • radians (double)

Returns Vec3

Vec3 rotateY(double radians) const;

Rotation around a world axis.

Parameters

  • radians (double)

Returns Vec3

Vec3 rotateZ(double radians) const;

Rotation around a world axis.

Parameters

  • radians (double)

Returns Vec3

Vec3 closestAxis() const;

The axis-aligned unit vector closest to this direction.

Returns Vec3

static constexpr Vec3 bezier2(Vec3 c0, Vec3 c1, Vec3 c2, double t);

Point on a quadratic Bezier curve at t (0..1).

Parameters

  • c0 (Vec3)
  • c1 (Vec3)
  • c2 (Vec3)
  • t (double)

Returns Vec3

static constexpr Vec3 bezier3(Vec3 c0, Vec3 c1, Vec3 c2, Vec3 c3, double t);

Point on a cubic Bezier curve at t (0..1).

Parameters

  • c0 (Vec3)
  • c1 (Vec3)
  • c2 (Vec3)
  • c3 (Vec3)
  • t (double)

Returns Vec3

A rotation quaternion (sm.quat). Plain value type; w = 1 is no rotation.

struct Quat
double x = 0, y = 0, z = 0, w = 1;
constexpr Quat() = defaul;

The identity rotation.

constexpr Quat(double x, double y, double z, double w);

A quaternion from its components.

Parameters

  • x (double)
  • y (double)
  • z (double)
  • w (double)
static constexpr Quat identity();

No rotation.

Returns Quat

static Quat angleAxis(double radians, Vec3 axis);

A rotation of radians around axis.

Parameters

  • radians (double)
  • axis (Vec3)

Returns Quat

static Quat fromEuler(Vec3 degrees);

A rotation from Euler angles in degrees (sm.quat.fromEuler).

Parameters

  • degrees (Vec3)

Returns Quat

Lua: sm.quat.fromEuler

static Quat lookRotation(Vec3 at, Vec3 up);

A rotation facing at with up as up (sm.quat.lookRotation).

Parameters

  • at (Vec3)
  • up (Vec3)

Returns Quat

Lua: sm.quat.lookRotation

static Quat fromTo(Vec3 from, Vec3 to);

The shortest rotation taking direction from to direction to (sm.vec3.getRotation).

Parameters

  • from (Vec3)
  • to (Vec3)

Returns Quat

static constexpr double HALF_SQRT2 = 0.70710678118654752440;

Fixed 90 and 180 degree rotations (sm.quat.rotX90() and so on).

static constexpr Quat rotX90();

Fixed 90 and 180 degree rotations (sm.quat.rotX90() and so on).

Returns Quat

static constexpr Quat rotY90();

Fixed 90 and 180 degree rotations (sm.quat.rotX90() and so on).

Returns Quat

static constexpr Quat rotZ90();

Fixed 90 and 180 degree rotations (sm.quat.rotX90() and so on).

Returns Quat

static constexpr Quat rotNegX90();

Fixed 90 and 180 degree rotations (sm.quat.rotX90() and so on).

Returns Quat

static constexpr Quat rotNegY90();

Fixed 90 and 180 degree rotations (sm.quat.rotX90() and so on).

Returns Quat

static constexpr Quat rotNegZ90();

Fixed 90 and 180 degree rotations (sm.quat.rotX90() and so on).

Returns Quat

static constexpr Quat rotX180();

Fixed 90 and 180 degree rotations (sm.quat.rotX90() and so on).

Returns Quat

static constexpr Quat rotY180();

Fixed 90 and 180 degree rotations (sm.quat.rotX90() and so on).

Returns Quat

static constexpr Quat rotZ180();

Fixed 90 and 180 degree rotations (sm.quat.rotX90() and so on).

Returns Quat

Vec3 at() const;

The rotated forward, up and right directions (Quat:getAt/getUp/getRight).

Returns Vec3

Vec3 up() const;

The rotated forward, up and right directions (Quat:getAt/getUp/getRight).

Returns Vec3

Vec3 right() const;

The rotated forward, up and right directions (Quat:getAt/getUp/getRight).

Returns Vec3

Quat round90() const;

Snapped to the nearest 90 degree steps.

Returns Quat

Lua: sm.quat.round90

Quat safeNormalize(Quat fallback) const;

Normalized, or fallback if the quaternion has zero length.

Parameters

  • fallback (Quat)

Returns Quat

constexpr Quat operator*(Quat o) const;

Combines rotations: (a * b) applies b, then a.

Parameters

  • o (Quat)

Returns Quat

constexpr Vec3 operator*(Vec3 v) const;

Rotates a vector.

Parameters

  • v (Vec3)

Returns Vec3

constexpr bool operator==(Quat o) const;

Exact equality of all components.

Parameters

  • o (Quat)

Returns bool

constexpr Quat inverse() const;

The opposite rotation.

Returns Quat

Quat normalize() const;

Unit length; a zero quaternion becomes the identity.

Returns Quat

Quat slerp(Quat to, double t) const;

Spherical interpolation towards to along the shortest path.

Parameters

  • to (Quat)
  • t (double)

Returns Quat

An RGBA color (sm.color) with channels from 0 to 1. Defaults to white.

struct Color
double r = 1, g = 1, b = 1, a = 1;
constexpr Color() = defaul;

White.

constexpr Color(double r, double g, double b, double a = 1);

A color from its channels.

Parameters

  • r (double)
  • g (double)
  • b (double)
  • a (double)
static constexpr Color rgb(uint32_t hex);

A color from 0xRRGGBB, e.g. Color::rgb(0x4a90d9).

Parameters

  • hex (uint32_t)

Returns Color

static constexpr Color rgba(uint32_t hex);

A color from 0xRRGGBBAA.

Parameters

  • hex (uint32_t)

Returns Color

static constexpr Color hex(StringView s);

A color from "#rrggbb", "rrggbb", "#rrggbbaa" or "rrggbbaa".

Parameters

  • s (StringView)

Returns Color

constexpr Color lerp(Color to, double t) const;

Linear interpolation towards to.

Parameters

  • to (Color)
  • t (double)

Returns Color

constexpr bool operator==(Color o) const;

Exact equality of all channels.

Parameters

  • o (Color)

Returns bool

String hexString() const;

"rrggbbaa", as Color:getHexStr().

Returns String

String tag() const;

"#rrggbb", the color tag used in chat and GUI text.

Returns String

String guiString() const;

The engine's GUI color string, as Color:getGuiColorStr().

Returns String

uint32_t uintRgba() const;

0xRRGGBBAA, as Color:getUintRgba().

Returns uint32_t

A uuid (sm.uuid), stored as its 36-character text.

struct Uuid
char text[37] = "00000000-0000-0000-0000-000000000000";
constexpr Uuid() = defaul;

The nil uuid (all zeros).

constexpr Uuid(const char *s);

Parses the text form; missing characters become '0'.

Parameters

  • s (const char *)
constexpr Uuid(StringView s);

Parses the text form; missing characters become '0'.

Parameters

  • s (StringView)
static Uuid random();

A random uuid.

Returns Uuid

Lua: sm.uuid.generateRandom

static Uuid named(const Uuid &ns, StringView name);

A version 5 uuid: the same name in the same ns always gives the same uuid.

Parameters

  • ns (const Uuid &): A uuid namespace for the name. The namespace makes sure any equal name from different namespaces do not collide.
  • name (StringView): A name, to generate a uuid from. Provided the same namespace and name, the uuid will be the same.

Returns Uuid

Lua: sm.uuid.generateNamed

static constexpr Uuid nil();

The nil uuid.

Returns Uuid

bool isNil() const;

True for the nil uuid.

Returns bool

StringView view() const;

The 36-character text.

Returns StringView

constexpr bool operator==(const Uuid &o) const;

Compares the text.

Parameters

  • o (const Uuid &)

Returns bool