gl3n

gl3n.linalg

Special thanks to:

  • Tomasz Stachowiak (h3r3tic): allowed me to use parts of omg.
  • Jakob Øvrum (jA_cOp): improved the code a lot!
  • Florian Boesch (__doc__): helps me to understand opengl/complex maths better, see: http://codeflow.org/.
  • #D on freenode: answered general questions about D.

Authors
David Herberth
License
MIT
Note:
All methods marked with pure are weakly pure since, they all access an instance member. All static methods are strongly pure.

struct Vector(type, int dimension_);

Base template for all vector-types.

Parameters
type all values get stored as this type
dimension specifies the dimension of the vector, can be 1, 2, 3 or 4
Examples
alias Vector!(int, 3) vec3i;
alias Vector!(float, 4) vec4;
alias Vector!(real, 2) vec2r;

alias vt = type;

Holds the internal type of the vector.


int dimension;

Holds the dimension of the vector.


vt[dimension] vector;

Holds all coordinates, length conforms dimension.


@property auto value_ptr();

Returns a pointer to the coordinates.


string as_string();
alias toString = as_string;

Returns the current vector formatted as string, useful for printing the vector.


inout inout(vt) get_(char coord)();




alias x = get_!('x');
alias y = get_!('y');
alias u = x;
alias v = y;
alias s = x;
alias t = y;
alias r = x;
alias g = y;
alias z = get_!('z');
alias b = z;
alias p = z;
alias w = get_!('w');
alias a = w;
alias q = w;

static properties to access the values.


Vector e1;
Vector e2;

canonical basis for Euclidian space


this(Args...)(Args args);
this(T)(T vec);
this()(vt value);

Constructs the vector. If a single value is passed the vector, the vector will be cleared with this value. If a vector with a higher dimension is passed the vector will hold the first values up to its dimension. If mixed types are passed they will be joined together (allowed types: vector, static array, vt).

Examples
vec4 v4 = vec4(1.0f, vec2(2.0f, 3.0f), 4.0f);
vec3 v3 = vec3(v4); // v3 = vec3(1.0f, 2.0f, 3.0f);
vec2 v2 = v3.xy; // swizzling returns a static array.
vec3 v3_2 = vec3(1.0f); // vec3 v3_2 = vec3(1.0f, 1.0f, 1.0f);

const bool ok();

Returns true if all values are not nan and finite, otherwise false.


void clear(vt value);

Sets all values of the vector to value.


void update(Vector!(vt, dimension) other);

Updates the vector with the values from other.


const Vector!(vt, s.length) opDispatch(string s)();

Implements dynamic swizzling.

Returns
a Vector

const real magnitude_squared();

Returns the squared magnitude of the vector.


const real magnitude();
alias length_squared = magnitude_squared;
alias length = magnitude;

Returns the magnitude of the vector.


void normalize();

Normalizes the vector.


const Vector normalized();

Returns a normalized copy of the current vector.


T.vt dot(T)(const T veca, const T vecb);

Calculates the product between two vectors.


T cross(T)(const T veca, const T vecb);

Calculates the cross product of two 3-dimensional vectors.


T.vt distance(T)(const T veca, const T vecb);

Calculates the distance between two vectors.


alias vec2 = Vector!(float, 2).Vector;
alias vec3 = Vector!(float, 3).Vector;
alias vec4 = Vector!(float, 4).Vector;
alias vec2d = Vector!(double, 2).Vector;
alias vec3d = Vector!(double, 3).Vector;
alias vec4d = Vector!(double, 4).Vector;
alias vec2i = Vector!(int, 2).Vector;
alias vec3i = Vector!(int, 3).Vector;
alias vec4i = Vector!(int, 4).Vector;

Pre-defined vector types, the number represents the dimension and the last letter the type (none = float, d = double, i = int).


struct Matrix(type, int rows_, int cols_) if (rows_ > 0 && cols_ > 0);

Base template for all matrix-types.

Parameters
type all values get stored as this type
rows_ rows of the matrix
cols_ columns of the matrix
Examples
alias Matrix!(float, 4, 4) mat4;
alias Matrix!(double, 3, 4) mat34d;
alias Matrix!(real, 2, 2) mat2r;

alias mt = type;

Holds the internal type of the matrix;


int rows;

Holds the number of rows;


int cols;

Holds the number of columns;


mt[cols][rows] matrix;

Holds the matrix row-major in memory.


@property auto value_ptr();

Returns the pointer to the stored values as OpenGL requires it. Note this will return a pointer to a row-major matrix, this means you've to set the transpose argument to GL_TRUE when passing it to OpenGL.

Examples
// 3rd argument = GL_TRUE
glUniformMatrix4fv(programs.main.model, 1, GL_TRUE, mat4.translation(-0.5f, -0.5f, 1.0f).value_ptr);

string as_string();
alias toString = as_string;

Returns the current matrix formatted as flat string.


string as_pretty_string();
alias toPrettyString = as_pretty_string;

Returns the current matrix as pretty formatted string.


this(Args...)(Args args);
this(T)(T mat);
this(T)(T mat);
this()(mt value);

Constructs the matrix: If a single value is passed, the matrix will be cleared with this value (each column in each row will contain this value). If a matrix with more rows and columns is passed, the matrix will be the upper left nxm matrix. If a matrix with less rows and columns is passed, the passed matrix will be stored in the upper left of an identity matrix. It's also allowed to pass vectors and scalars at a time, but the vectors dimension must match the number of columns and align correctly.

Examples
mat2 m2 = mat2(0.0f); // mat2 m2 = mat2(0.0f, 0.0f, 0.0f, 0.0f);
mat3 m3 = mat3(m2); // mat3 m3 = mat3(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
mat3 m3_2 = mat3(vec3(1.0f, 2.0f, 3.0f), 4.0f, 5.0f, 6.0f, vec3(7.0f, 8.0f, 9.0f));
mat4 m4 = mat4.identity; // just an identity matrix
mat3 m3_3 = mat3(m4); // mat3 m3_3 = mat3.identity

const bool ok();

Returns true if all values are not nan and finite, otherwise false.


void clear(mt value);

Sets all values of the matrix to value (each column in each row will contain this value).


void make_identity();

Makes the current matrix an identity matrix.


Matrix identity();

Returns a identity matrix.


void transpose();

Transposes the current matrix;


const Matrix!(mt, cols, rows) transposed();

Returns a transposed copy of the matrix.


Matrix rotation(real alpha, Vector!(mt, 3) axis);
Matrix rotation(real alpha, mt x, mt y, mt z);

Returns an identity matrix with an applied rotate_axis around an arbitrary axis (nxn matrices, n >= 3).


Matrix xrotation(real alpha);

Returns an identity matrix with an applied rotation around the x-axis (nxn matrices, n >= 3).


Matrix yrotation(real alpha);

Returns an identity matrix with an applied rotation around the y-axis (nxn matrices, n >= 3).


Matrix zrotation(real alpha);

Returns an identity matrix with an applied rotation around the z-axis (nxn matrices, n >= 3).


Matrix rotatex(real alpha);

Rotates the current matrix around the x-axis and returns this (nxn matrices, n >= 3).


Matrix rotatey(real alpha);

Rotates the current matrix around the y-axis and returns this (nxn matrices, n >= 3).


Matrix rotatez(real alpha);

Rotates the current matrix around the z-axis and returns this (nxn matrices, n >= 3).


void translation(mt[] values...);

Sets the translation of the matrix (nxn matrices, n >= 3).


void translation(Matrix mat);

Copyies the translation from mat to the current matrix (nxn matrices, n >= 3).


Matrix translation();

Returns an identity matrix with the current translation applied (nxn matrices, n >= 3)..


void scale(mt[] values...);

Sets the scale of the matrix (nxn matrices, n >= 3).


void scale(Matrix mat);

Copyies the scale from mat to the current matrix (nxn matrices, n >= 3).


Matrix scale();

Returns an identity matrix with the current scale applied (nxn matrices, n >= 3).


void rotation(Matrix!(mt, 3, 3) rot);

Copies rot into the upper left corner, the translation (nxn matrices, n >= 3).


Matrix!(mt, 3, 3) rotation();

Returns an identity matrix with the current rotation applied (nxn matrices, n >= 3).


const Matrix inverse();

Returns an inverted copy of the current matrix (nxn matrices, n <= 4).


void invert();

Inverts the current matrix (nxn matrices, n <= 4).


alias mat2 = Matrix!(float, 2, 2).Matrix;

Pre-defined matrix types, the first number represents the number of rows and the second the number of columns, if there's just one it's a nxn matrix. All of these matrices are floating-point matrices.


struct Quaternion(type);

Base template for all quaternion-types.

Parameters
type all values get stored as this type

alias qt = type;

Holds the internal type of the quaternion.


qt[4] quaternion;

Holds the w, x, y and z coordinates.


@property auto value_ptr();

Returns a pointer to the quaternion in memory, it starts with the w coordinate.


string as_string();

Returns the current vector formatted as string, useful for printing the quaternion.


alias w = get_!('w');
alias x = get_!('x');
alias y = get_!('y');
alias z = get_!('z');

static properties to access the values.


this(qt w_, qt x_, qt y_, qt z_);
this(qt w_, Vector!(qt, 3) vec);
this(Vector!(qt, 4) vec);

Constructs the quaternion. Takes a 4-dimensional vector, where vector.x = the quaternions w coordinate, or a w coordinate of type qt and a 3-dimensional vector representing the imaginary part, or 4 values of type qt.


const bool ok();

Returns true if all values are not nan and finite, otherwise false.


const real magnitude_squared();

Returns the squared magnitude of the quaternion.


const real magnitude();

Returns the magnitude of the quaternion.


Quaternion identity();

Returns an identity quaternion (w=1, x=0, y=0, z=0).


void make_identity();

Makes the current quaternion an identity quaternion.


void invert();
alias conjugate = invert;

Inverts the quaternion.


const Quaternion inverse();
alias conjugated = inverse;

Returns an inverted copy of the current quaternion.


Quaternion from_matrix(Matrix!(qt, 3, 3) matrix);

Creates a quaternion from a 3x3 matrix.

Parameters
Matrix!(qt, 3, 3) matrix 3x3 matrix (rotation)
Returns
A quaternion representing the rotation (3x3 matrix)

const Matrix!(qt, rows, cols) to_matrix(int rows, int cols)();

Returns the quaternion as matrix.

Parameters
rows number of rows of the resulting matrix (min 3)
cols number of columns of the resulting matrix (min 3)

void normalize();

Normalizes the current quaternion.


const Quaternion normalized();

Returns a normalized copy of the current quaternion.


const real yaw();

Returns the yaw.


const real pitch();

Returns the pitch.


const real roll();

Returns the roll.


Quaternion xrotation(real alpha);

Returns a quaternion with applied rotation around the x-axis.


Quaternion yrotation(real alpha);

Returns a quaternion with applied rotation around the y-axis.


Quaternion zrotation(real alpha);

Returns a quaternion with applied rotation around the z-axis.


Quaternion axis_rotation(real alpha, Vector!(qt, 3) axis);

Returns a quaternion with applied rotation around an axis.


Quaternion euler_rotation(real heading, real attitude, real bank);

Creates a quaternion from an euler rotation.


Quaternion rotatex(real alpha);

Rotates the current quaternion around the x-axis and returns this.


Quaternion rotatey(real alpha);

Rotates the current quaternion around the y-axis and returns this.


Quaternion rotatez(real alpha);

Rotates the current quaternion around the z-axis and returns this.


Quaternion rotate_axis(real alpha, Vector!(qt, 3) axis);

Rotates the current quaternion around an axis and returns this.


Quaternion rotate_euler(real heading, real attitude, real bank);

Applies an euler rotation to the current quaternion and returns this.


alias quat = Quaternion!(float).Quaternion;

Pre-defined quaternion of type float.