Matrix

Matrix implementation, row-major right handed Basic operations:

// Scalar operations, any of *, /, +, -, % with number
m1 + 1;
m1 * 2;
// etc...
// Matrix/Matrix scalar op (on each element)
m1 + m2;
m1 - m2;
m1.scalarMultiply(m2);
m1.scalarDivide(m2);
// Unary operations
-m1; // invert all numbers
~m1; // invert matrix, aka m1^-1, where ~m1*m1=m1.identity
// Binary operations
m1 * m2; // matrix multiplication
vec1x2 * m2x1; // vector by matrix
m3x4 * vec4x1; // matrix by vector
// Casting
cast(vec1x3) mat1x3; // cast to column vector
cast(vec3x1) mat1x3; // or row vector
cast(mat1x3) vec3x1; // or event vector to mat
cast(imat2) fmat2; // and type changing casts
cast(imat2) imat4; // changing size (keeps top left)
cast(imat4) imat2; // fills new cells with zeros
// Data access:
m1[1][2]; // access to row 1 column 2
m1.data[1][2]; // or explicit access to data
struct Matrix (
T
size_t H
size_t W
) if (
isNumeric!T &&
W > 0
&&
H > 0
) {}

Constructors

this
this(T val)
this(T[size] vals)
this(T[W][H] vals)

Constructs Matrix from components. If no components present matrix will be filled with 0

this
this(quat q)

Construct matrix from quaternion

Alias This

data

Alias to allow data access

Members

Aliases

MatType
alias MatType = Matrix!(T, H, W)

Alias to matrix type. Can be used to construct matrices of same type

dataType
alias dataType = T

Alias to data type

inverse
alias inverse = invert

Inverts current matrix, alias to data = ~matrix

inversed
alias inversed = inverted

Returns inverted, alias to return ~matrix

toArray
alias toArray = buffer

Construct row-major one dimentional array from matrix.

toGlArray
alias toGlArray = glbuffer

Construct column-major one dimentional array from matrix.

Functions

adjoint
MatType adjoint()

Returns ajdoint matrix

buffer
T[W * H] buffer()

Construct row-major one dimentional array from matrix.

copyof
MatType copyof()

Returns copy of matrix

determinant
T determinant()

Returns determinant of matrix

fixNegativeZero
MatType fixNegativeZero()

Removes negative sign from numbers less then float.epsilon*2

glbuffer
T[W * H] glbuffer()

Construct column-major one dimentional array from matrix.

invert
MatType invert()

Inverts current matrix, alias to data = ~matrix

inverted
mat!(H, W) inverted()

Returns inverted, alias to return ~matrix

opBinary
MatType opBinary(Matrix!(R, H, W) b)

Scalar matrix addition and subtraction

opBinary
Matrix!(T, H, U) opBinary(Matrix!(R, W, U) b)

Matrix multiplication

opBinary
Vector!(R, H) opBinary(Vector!(R, W) b)

Vector transformation

opBinary
Vector!(R, 3) opBinary(Vector!(R, 3) b)

Vector3 transformation

opBinary
quat opBinary(quat b)

Quaternion transformation

opBinary
MatType opBinary(R b)

Scalar number operations

opBinaryRight
MatType opBinaryRight(Matrix!(R, H, W) b)

Scalar matrix addition and subtraction

opBinaryRight
Matrix!(T, U, W) opBinaryRight(Matrix!(R, U, H) b)

Matrix multiplication

opBinaryRight
Vector!(R, W) opBinaryRight(Vector!(R, H) b)

Vector transformation

opBinaryRight
Vector!(R, 3) opBinaryRight(Vector!(R, 3) b)

Vector3 transformation

opBinaryRight
quat opBinaryRight(quat b)

Quaternion transformation

opBinaryRight
MatType opBinaryRight(R b)

Scalar number operations

opCast
R opCast()

Matrix type conversion

opCast
R opCast()

Matrix resizing

opCast
R opCast()

Matrix to vector cast (column)

opCast
R opCast()

Matrix to vector cast (row)

opCast
bool opCast()

Boolean cast

opEquals
bool opEquals(Matrix!(R, H, W) b)
bool opEquals(Matrix!(R, V, U) b)
bool opEquals(R[][] b)
bool opEquals(R[] b)

opEquals x == y

opOpAssign
MatType opOpAssign(Matrix!(R, H, W) b)
MatType opOpAssign(R b)

Scalar addition and subtraction in place

opUnary
MatType opUnary()

opUnary [-, +, --, ++, *, ~] x

opUnary
Matrix!(float, W, W) opUnary()

Invert matrix

ptr
T[W]* ptr()

Returns pointer to data

resize
Matrix!(T, V, U) resize()

Resize matrix (keep left top). Alias to cast(Matrix!(T, V, U)) Matrix!(T, H, W)

scalarDivide
MatType scalarDivide(Matrix!(R, W, H) b)

Divides each element by each element of matrices

scalarMultiply
MatType scalarMultiply(Matrix!(R, W, H) b)

Multiplies each element by each element of matrices

toHash
size_t toHash()

Returns hash

toString
string toString()

Returns string representation of matrix: [1.00, 1.00,... , 1.00]

toStringPretty
string toStringPretty()

Returns string representation of matrix: 1.00, 1.00,... |\n|1.00, ... , 1.00|

transpose
Matrix!(T, W, H) transpose()

Transpose of matrix (swaps rows and columns)

Static functions

frustum
MatType frustum(T left, T right, T bottom, T top, T near, T far)

Constructs frustum matrix

identity
MatType identity()

Returns identity matrix for size

lookAt
MatType lookAt(vec!(T, 3) eye, vec!(T, 3) target, vec!(T, 3) up)

Constructs lookAt matrix

ortho
MatType ortho(T left, T right, T bottom, T top, T near, T far)

Constructs orthographic matrix

perspective
MatType perspective(int fovy, T aspect, T near, T far)
MatType perspective(T fovy, T aspect, T near, T far)

Construct perspective matrix

rotation
MatType rotation(T angle)

Constructs 2d rotation matrix

rotation
MatType rotation(T[3] axis, T angle)

Constructs 3d rotation matrix from axis and angle

rotationX
MatType rotationX(T angle)

Constructs 3d rotation matrix from angle on X axis

rotationY
MatType rotationY(T angle)

Constructs 3d rotation matrix from angle on Y axis

rotationZ
MatType rotationZ(T angle)

Constructs 3d rotation matrix from angle on Z axis

scale
MatType scale(T[2] s, ...)

Constructs 2d scale matrix

scale
MatType scale(T[3] v, ...)

Constructs 3d scale matrix

shear
MatType shear(T[2] s, ...)

Constructs 2d shear matrix

translation
MatType translation(T[2] s, ...)

Constructs 2d translation matrix

translation
MatType translation(T[3] v, ...)

Constructs 3d translation matrix

Variables

columns
enum size_t columns;

Matrix size (w)

data
T[W][H] data;

Matrix data

isSquare
enum bool isSquare;

Is matrix square (W == H)

rows
enum size_t rows;

Matrix size (h)

size
enum size_t size;

Matrix size (h*w)

Meta