Skip to content

API Reference

Attila Szabo edited this page Dec 22, 2023 · 1 revision

Data types

Vector types start with a capital V, followed by the dimension (2, 3, or 4) and the element type (d, f, l, or i).

dim float64 float32 int64 int32
2 V2d V2f V2l V2i
3 V3d V3f V3l V3i
4 V4d V4f V4l V4i

Matrix types start with a capital M, followed by the number of rows (2, 3, or 4), the number of columns (2, 3, or 4) and the element type (d, f, l, or i). See the following table for all available types.

dim float64 float32 int64 int32
2x2 M22d M22f M22l M22i
2x3 M23d M23f M23l M23i
3x3 M33d M33f M33l M33i
3x4 M34d M34f M34l M34i
4x4 M44d M44f M44l M44i

Construction

Vectors

example value
V3d.Zero (0.0, 0.0, 0.0)
V3d.One (1.0, 1.0, 1.0)
V3d.XAxis (1.0, 0.0, 0.0)
V3d.YAxis (0.0, 1.0, 0.0)
V3d.ZAxis (0.0, 0.0, 1.0)
V4d.WAxis (0.0, 0.0, 0.0, 1.0)
V3d.IOI (1.0, 0.0, 1.0), where I is 1.0, and O is 0.0
V3d.NOP (-1.0, 0.0, 1.0), where P is +1.0, and N is -1.0
V3d() (0.0, 0.0, 0.0)
V3d(3.5) (3.5, 3.5, 3.5)
V2d(1.2, 3.4) (1.2, 3.4)
V3d(V2d(1.2, 3.4), 5.6) (1.2, 3.4, 5.6)
V3d.MaxValue (double.MaxValue, double.MaxValue, double.MaxValue)
V3d.MinValue (double.MinValue, double.MinValue, double.MinValue)
V3d.NegativeInfinity (double.NegativeInfinity, double.NegativeInfinity, double.NegativeInfinity)
V3d.PositiveInfinity (double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity)
V3d.NaN (double.NaN, double.NaN, double.NaN)

Matrices

example description
M44d.Zero all elements zero
M44d.Identity identity matrix
M44d() all elements zero

Transformation Matrices

example description
M44d.Translation(dx, dy, dz) translation
M44d.Scale(s) uniform scale
M44d.Scale(sx, sy, sz) non-uniform scale
M44d.RotationX(angleInRadians) rotation around x-axis
M44d.RotationY(angleInRadians) rotation around y-axis
M44d.RotationZ(angleInRadians) rotation around z-axis
M44d.Rotation(axis, angleInRadians) rotation around specified axis
M44d.Rotation(yaw, pitch, roll) rotation given by yaw, pitch, and roll in radians
M44d.ShearXY(factorX, factorY) shear along x and y
M44d.ShearXZ(factorX, factorZ) shear along x and z
M44d.ShearYZ(factorY, factorZ) shear along y and z

Accessors

Vectors

let v = V4d(2.0, 4.0, 8.0, 1.0)

v.X     // 2.0
v.Y     // 4.0
v.Z     // 8.0
v.W     // 1.0

Matrices

Elements

M00 M01 M02 M03
M10 M11 M12 M13
M20 M21 M22 M23
M30 M31 M32 M33

Columns

C0 C1 C2 C3

Rows

R0
R1
R2
R3

Examples

let m = M33d(1.0, 2.0, 3.0,
             4.0, 5.0, 6.0,
             0.0, 0.0, 1.0)

m.M01   // 2.0
m.M12   // 6.0

m.R0  // V3d(1.0, 2.0, 3.0); row0
m.R1  // V3d(4.0, 5.0, 6.0); row1
m.C0  // V3d(1.0, 4.0, 0.0); column0
m.C2  // V3d(3.0, 6.0, 1.0); column1

Operations

Arithmetic Operators

a + b        // component-wise addition
a - b        // component-wise subtraction
a * b        // component-wise multiplication
a / b        // component-wise division
a % b        // component-wise modulo

Relational Operators

a == b               // component-wise comparison
a != b
a.AllSmaller(b)      // component-wise smaller-than (true if all comparisons are true)
a.AnySmaller(b)      // component-wise smaller-than (true if at least one comparison is true)
...                  // Smaller, SmallerEqual, Greater, GreaterEqual

Properties vs Methods

Properties by default DO NOT change the underlying data (vector), whereas methods usually DO. E.g. v.Normalized returns a normalized copy of v but does not touch v itself, and v.Normalize() normalizes v itself (and returns this so one can chain method calls). ATTENTION: since all vector and matrix types are value types returning this returns a copy and DOES NOT reference the original struct.

Properties

v.Length
v.LengthSquared
v.Negated
v.Normalized
v.Reciprocal   // returns component-wise reciprocal (1/x)

Methods

v.Negate()
v.Normalize()
V3d.DotProduct(a, b)
V3d.CrossProduct(a, b)

Scalar/Vector

let v = V3d(1.0, 2.0, 4.0)

-v              // (-1.0, -2.0, -3.0)
v * 0.2         // (0.2, 0.4, 0.8)
0.2 * v         // (0.2, 0.4, 0.8)
v / 2.0         // (0.5, 1.0, 2.0)
2.0 / v         // (2.0, 1.0, 0.5)
v + 0.5         // (1.5, 2.5, 4.5)
0.5 + v         // (1.5, 2.5, 4.5)
v - 0.5         // (0.5, 1.5, 3.5)
0.5 - v         // (-0.5, -1.5, -3.5)

Vector/Vector

let u = V3d(10.0, 20.0, 40.0)
let v = V3d(5.0, 10.0, 20.0)

u + v           // (15.0, 30.0, 60.0)
u - v           // (5.0, 10.0, 20.0)
u * v           // (50.0, 200.0, 800.0)
u / v           // (2.0, 2.0, 2.0)

Dot product

let u = V3d(1.0, 0.0, 0.0)
let v = V3d(0.0, 1.0, 0.0)

u.Dot(v)        // 0.0
V3d.Dot(u, v)   // 0.0

Cross product

let u = V3d(1.0, 0.0, 0.0)
let v = V3d(0.0, 1.0, 0.0)

u.Cross(v)        // (0.0, 0.0, 1.0)
V3d.Cross(u, v)   // (0.0, 0.0, 1.0)

Normalization

let v = V2d(5.0, 0.0)

v.Normalized      // (1.0, 0.0)

Length and Norms

let v = V2d(3.0, 4.0)

v.Length          // 5.0
v.LengthSquared   // 25.0
v.Norm1           // 7.0; 1-norm or Manhattan-norm
v.Norm2           // 25.0; 2-norm or Euclidean-norm; same as a.Length
v.NormMax         // 4.0; maximum-norm
v.NormMin         // 3.0; minimum-norm

Abs, Floor, Ceil

let v = V3d(1.6, -2.3, 3.1)

v.Abs    // (1.6, 2.3, 3.1)
v.Floor  // (1.0, -3.0, 3.0)
v.Ceil   // (2.0, -2.0, 4.0)

Swizzle

let v = V3d(3.0, 4.0, 5.0)

v.YX              // V2d(4.0, 3.0))
v.XZIY            // V4d(3.0, 5.0, 1.0, 4.0)
v.XXX             // V3d(3.0, 3.0, 3.0)
v.XYO             // V3d(3.0, 4.0, 0.0)

Various

let v = V4d(2.0, 4.0, 8.0, 1.0)
let m = M33d(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 0.0, 0.0, 1.0)

m.Adjoint           // [[5, -2, -3], [-4, 1, 6], [0, 0, -3]]
m.Det               // -3.0
m.InvTransformDir   
m.InvTransformPos
m.InvTransformPosProj
m.Inverse           // [[-1.66..., 0.66..., 1], [1.33..., -0.33..., -2], [0, 0, 1]]
m.Invertible        // true
m.IsIdentity
m.IsOrthogonal
m.IsOrthonormal
m.LuInverse() 
m.QrInverse()
m.Norm1             // 22.0
m.Norm2             // 9.5916630466254382
m.NormMax           // 6.0
m.NormMin           // 0.0
m.Singular          // false
m.Trace             // 7.0
m.Transform
m.TransformDir
m.TransformPos
m.TransformPosProj
m.TransformPosProjFull
m.Transposed        // [[1, 4, 0], [2, 5, 0], [3, 6, 1]]
m.TransposedTransform
m.TransposedTransformDir
m.TransposedTransformPos

Transforming Vectors and Points

Matrices and vectors of the same dimension can be multiplied directly:

V4d v;
M44d m;
V4d r = m * v; // matrix * vector -> vector

In order to multiply 3-dim vectors by a 4x4 matrix (which is often the case in computer graphics) you have to use one of the Transform methods provided by matrix types:

V3d p, v;
M44d m;
V3d a = m.TransformPos(p);  // w-component assumed to be 1 (location)
V3d b = m.TransformDir(v);  // w-component assumed to be 0 (direction)

A Trafo3d is a Container for two M44d fields, a Matrix and its Inverse

Since it is often a lot easier to maintain the inverse of a matrix as opposed to computing the inverse as necessary, a special object that contains both, a matrix and its inverse, has been created. Thus a Trafo3d contains two M44d members, one called Forward, one called Backward .

Note also, that the multiplication of two M44d structs follows mathematical rules:

M44d m1;
M44d m2;
M44d m = m2 * m1; // transform first by m1, then by m2 !!!

whereas, the multiplication of two Trafo3d objects is defined differently:

Trafo3d t1;
Trafo3d t2;
Trafo3d t = t1 * t2; // transform first by t1, then by t2 !!!

Colors

  • C3b
  • C3us
  • C3ui
  • C3f
  • C3d
  • C4b
  • C4us
  • C4ui
  • C4f
  • C4d
  • CieLabf
  • CieLuvf
  • CieXYZf
  • CieYxyf
  • CMYKf
  • HSLf
  • HSVf
  • Yuvf

Intersection Tests

2D

Line2d Ray2d Box2d Plane2d Polygon2d Triangle2d Quad2d
Line2d
Ray2d
Box2d
Plane2d
Polygon2d
Triangle2d
Quad2d

3D

Line3d Ray3d Box3d Plane3d Polygon3d Hull3d Triangle3d Quad3d Sphere3d Cylinder3d
Line3d
Ray3d
Box3d
Plane3d
Polygon3d
Hull3d
Triangle3d
Quad3d
Sphere3d
Cylinder3d

Shapes

2D

  • Line2d
  • Ray2d
  • Plane2d
  • Box2d[ilfd]
  • Polygon2d
  • Triangle2d
  • Quad2d
  • Circle2d

3D

  • Line3d
  • Ray3d
  • Plane3d
  • Box3d[ilfd]
  • Polygon3d
  • Triangle3d
  • Hull3d
  • Quad3d
  • Sphere3d
  • Cylinder3d