gl3n.linalg
gl3n.linalg
Discussion
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.
License
MIT
Note: All methods marked with pure are weakly pure since, they all access an instance member. All static methods are strongly pure.
-
Declaration
structVector(type, int dimension_);Base template for all vector-types.
Parameters
typeall values get stored as this type
dimensionspecifies 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;
-
Declaration
aliasvt= type;Holds the internal type of the vector.
-
Declaration
static const intdimension;Holds the
dimensionof the vector. -
Declaration
vt[dimension]vector;Holds all coordinates, length conforms dimension.
-
Declaration
const @property autovalue_ptr();Returns a pointer to the coordinates.
-
Declaration
@property stringas_string();
aliastoString= as_string;Returns the current vector formatted as string, useful for printing the vector.
-
Declaration
inout @property ref inout(vt)get_(char coord)(); -
Declaration
aliasx= get_!'x';
aliasu= x;
aliass= x;
aliasr= x;
aliasy= get_!'y';
aliasv= y;
aliast= y;
aliasg= y;
aliasz= get_!'z';
aliasb= z;
aliasp= z;
aliasw= get_!'w';
aliasa= w;
aliasq= w;static properties to access the values.
-
Declaration
enum Vectore1;
enum Vectore2;canonical basis for Euclidian space
-
Declaration
this(Args...)(Argsargs);
this(T)(Tvec) if (is_vector!T && is(T.vt : vt) && (T.dimension >= dimension));
this()(vtvalue);Constructs the vector. If a single
valueis passed the vector, the vector will be cleared with thisvalue. 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);
-
Declaration
const @property boolisFinite();Returns
trueif all values are not nan and finite, otherwisefalse. -
Declaration
voidclear(vtvalue);Sets all values of the vector to
value. -
Declaration
voidupdate(Vector!(vt, dimension)other);Updates the vector with the values from
other. -
Declaration
const @property Vector!(vt, s.length)opDispatch(string s)();Implements dynamic swizzling.
Return Value
a Vector
-
Declaration
const @property realmagnitude_squared();Returns the squared magnitude of the vector.
-
Declaration
const @property realmagnitude();
aliaslength_squared= magnitude_squared;
aliaslength= magnitude;Returns the
magnitudeof the vector. -
Declaration
voidnormalize();Normalizes the vector.
-
Declaration
const @property Vectornormalized();Returns a
normalizedcopy of the current vector.
-
Declaration
pure nothrow @safe T.vtdot(T)(const Tveca, const Tvecb) if (is_vector!T);Calculates the product between two vectors.
-
Declaration
pure nothrow @safe Tcross(T)(const Tveca, const Tvecb) if (is_vector!T && (T.dimension == 3));Calculates the
crossproduct of two 3-dimensional vectors. -
Declaration
pure nothrow @safe T.vtdistance(T)(const Tveca, const Tvecb) if (is_vector!T);Calculates the
distancebetween two vectors. -
Declaration
pure nothrow @safe Treflect(T)(const Tvec, const Tnorm) if (is_vector!T);reflecta vector using a surface normal -
Declaration
aliasvec2= Vector!(float, 2).Vector;
aliasvec3= Vector!(float, 3).Vector;
aliasvec4= Vector!(float, 4).Vector;
aliasvec2d= Vector!(double, 2).Vector;
aliasvec3d= Vector!(double, 3).Vector;
aliasvec4d= Vector!(double, 4).Vector;
aliasvec2i= Vector!(int, 2).Vector;
aliasvec3i= Vector!(int, 3).Vector;
aliasvec4i= 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).
-
Declaration
structMatrix(type, int rows_, int cols_) if (rows_ > 0 && (cols_ > 0));Base template for all matrix-types.
Parameters
typeall 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;
-
Declaration
aliasmt= type;Holds the internal type of the matrix;
-
Declaration
static const introws;Holds the number of
rows; -
Declaration
static const intcols;Holds the number of columns;
-
Declaration
mt[cols][rows]matrix;Holds the
matrixrow-major in memory. -
Declaration
const @property autovalue_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);
-
Declaration
@property stringas_string();
aliastoString= as_string;Returns the current matrix formatted as flat string.
-
Declaration
@property stringas_pretty_string();
aliastoPrettyString= as_pretty_string;Returns the current matrix as pretty formatted string.
-
Declaration
this(Args...)(Argsargs);
this(T)(Tmat) if (is_matrix!T && (T.cols >= cols) && (T.rows >= rows));
this(T)(Tmat) if (is_matrix!T && (T.cols < cols) && (T.rows < rows));
this()(mtvalue);Constructs the matrix: If a single
valueis passed, the matrix will be cleared with thisvalue(each column in each row will contain thisvalue). 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
-
Declaration
const @property boolisFinite();Returns
trueif all values are not nan and finite, otherwisefalse. -
Declaration
voidclear(mtvalue);Sets all values of the matrix to
value(each column in each row will contain thisvalue). -
Declaration
voidmake_identity();Makes the current matrix an identity matrix.
-
Declaration
static @property Matrixidentity();Returns a
identitymatrix. -
Declaration
voidtranspose();Transposes the current matrix;
-
Declaration
const @property Matrix!(mt, cols, rows)transposed();Returns a
transposedcopy of the matrix. -
Declaration
static Matrixtranslation(mtx, mty, mtz);
static Matrixtranslation(Vector!(mt, 3)v);Returns a
translationmatrix (3x3 and 4x4 matrices). -
Declaration
Matrixtranslate(mtx, mty, mtz);
Matrixtranslate(Vector!(mt, 3)v);Applys a translation on the current matrix and returns this (3x3 and 4x4 matrices).
-
Declaration
static Matrixscaling(mtx, mty, mtz);Returns a
scalingmatrix (3x3 and 4x4 matrices); -
Declaration
Matrixscale(mtx, mty, mtz);Applys a
scaleto the current matrix and returns this (3x3 and 4x4 matrices). -
Declaration
static Matrixrotation(realalpha, Vector!(mt, 3)axis);
static Matrixrotation(realalpha, mtx, mty, mtz);Returns an identity matrix with an applied rotate_axis around an arbitrary
axis(nxn matrices, n >= 3). -
Declaration
static Matrixxrotation(realalpha);Returns an identity matrix with an applied rotation around the x-axis (nxn matrices, n >= 3).
-
Declaration
static Matrixyrotation(realalpha);Returns an identity matrix with an applied rotation around the y-axis (nxn matrices, n >= 3).
-
Declaration
static Matrixzrotation(realalpha);Returns an identity matrix with an applied rotation around the z-axis (nxn matrices, n >= 3).
-
Declaration
Matrixrotatex(realalpha);Rotates the current matrix around the x-axis and returns this (nxn matrices, n >= 3).
-
Declaration
Matrixrotatey(realalpha);Rotates the current matrix around the y-axis and returns this (nxn matrices, n >= 3).
-
Declaration
Matrixrotatez(realalpha);Rotates the current matrix around the z-axis and returns this (nxn matrices, n >= 3).
-
Declaration
voidset_translation(mt[]values...);Sets the translation of the matrix (nxn matrices, n >= 3).
-
Declaration
voidset_translation(Matrixmat);Copyies the translation from
matto the current matrix (nxn matrices, n >= 3). -
Declaration
Matrixget_translation();Returns an identity matrix with the current translation applied (nxn matrices, n >= 3)..
-
Declaration
voidset_scale(mt[]values...);Sets the scale of the matrix (nxn matrices, n >= 3).
-
Declaration
voidset_scale(Matrixmat);Copyies the scale from
matto the current matrix (nxn matrices, n >= 3). -
Declaration
Matrixget_scale();Returns an identity matrix with the current scale applied (nxn matrices, n >= 3).
-
Declaration
voidset_rotation(Matrix!(mt, 3, 3)rot);Copies
rotinto the upper left corner, the translation (nxn matrices, n >= 3). -
Declaration
Matrix!(mt, 3, 3)get_rotation();Returns an identity matrix with the current rotation applied (nxn matrices, n >= 3).
-
Declaration
const @property Matrixinverse();Returns an inverted copy of the current matrix (nxn matrices, 2 >= n <= 4).
-
Declaration
voidinvert();Inverts the current matrix (nxn matrices, 2 >= n <= 4).
-
Declaration
aliasmat2= 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.
-
Declaration
structQuaternion(type);Base template for all quaternion-types.
Parameters
typeall values get stored as this type
-
Declaration
aliasqt= type;Holds the internal type of the quaternion.
-
Declaration
qt[4]quaternion;Holds the w, x, y and z coordinates.
-
Declaration
const @property autovalue_ptr();Returns a pointer to the quaternion in memory, it starts with the w coordinate.
-
Declaration
@property stringas_string();Returns the current vector formatted as string, useful for printing the quaternion.
-
Declaration
aliasw= get_!'w';
aliasx= get_!'x';
aliasy= get_!'y';
aliasz= get_!'z';static properties to access the values.
-
Declaration
this(qtw_, qtx_, qty_, qtz_);
this(qtw_, 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.
-
Declaration
const @property boolisFinite();Returns
trueif all values are not nan and finite, otherwisefalse. -
Declaration
const @property realmagnitude_squared();Returns the squared magnitude of the quaternion.
-
Declaration
const @property realmagnitude();Returns the
magnitudeof the quaternion. -
Declaration
static @property Quaternionidentity();Returns an
identityquaternion (w=1, x=0, y=0, z=0). -
Declaration
voidmake_identity();Makes the current quaternion an identity quaternion.
-
Declaration
voidinvert();
aliasconjugate= invert;Inverts the quaternion.
-
Declaration
const @property Quaternioninverse();
aliasconjugated= inverse;Returns an inverted copy of the current quaternion.
-
Declaration
static Quaternionfrom_matrix(Matrix!(qt, 3, 3)matrix);Creates a quaternion from a 3x3
matrix.Parameters
Matrix!(qt, 3, 3)matrix3x3
matrix(rotation)Return Value
A quaternion representing the rotation (3x3
matrix) -
Declaration
const Matrix!(qt, rows, cols)to_matrix(int rows, int cols)() if (rows >= 3 && (cols >= 3));Returns the quaternion as matrix.
Parameters
rowsnumber of rows of the resulting matrix (min 3)
colsnumber of columns of the resulting matrix (min 3)
-
Declaration
vec3to_axis_angle();Returns the quaternion as a vec3 (axis / angle representation).
-
Declaration
voidnormalize();Normalizes the current quaternion.
-
Declaration
const Quaternionnormalized();Returns a
normalizedcopy of the current quaternion. -
Declaration
const @property realyaw();Returns the
yaw. -
Declaration
const @property realpitch();Returns the
pitch. -
Declaration
const @property realroll();Returns the
roll. -
Declaration
static Quaternionxrotation(realalpha);Returns a quaternion with applied rotation around the x-axis.
-
Declaration
static Quaternionyrotation(realalpha);Returns a quaternion with applied rotation around the y-axis.
-
Declaration
static Quaternionzrotation(realalpha);Returns a quaternion with applied rotation around the z-axis.
-
Declaration
static Quaternionaxis_rotation(realalpha, Vector!(qt, 3)axis);Returns a quaternion with applied rotation around an
axis. -
Declaration
static Quaternioneuler_rotation(realroll, realpitch, realyaw);Creates a quaternion from an euler rotation.
-
Declaration
Quaternionrotatex(realalpha);Rotates the current quaternion around the x-axis and returns this.
-
Declaration
Quaternionrotatey(realalpha);Rotates the current quaternion around the y-axis and returns this.
-
Declaration
Quaternionrotatez(realalpha);Rotates the current quaternion around the z-axis and returns this.
-
Declaration
Quaternionrotate_axis(realalpha, Vector!(qt, 3)axis);Rotates the current quaternion around an
axisand returns this. -
Declaration
Quaternionrotate_euler(realheading, realattitude, realbank);Applies an euler rotation to the current quaternion and returns this.
-
-
Declaration
aliasquat= Quaternion!float.Quaternion;Pre-defined quaternion of type float.