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
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;
-
Declaration
alias
vt
= type;Holds the internal type of the vector.
-
Declaration
static const int
dimension
;Holds the
dimension
of the vector. -
Declaration
vt[dimension]
vector
;Holds all coordinates, length conforms dimension.
-
Declaration
const @property auto
value_ptr
();Returns a pointer to the coordinates.
-
Declaration
@property string
as_string
();
aliastoString
= as_string;Returns the current vector formatted as string, useful for printing the vector.
-
Declaration
alias
x
= 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 Vector
e1
;
enum Vectore2
;canonical basis for Euclidian space
-
Declaration
this(Args...)(Args
args
);
this(T)(Tvec
) if (is_vector!T && is(T.vt : vt) && T.dimension >= dimension);
this()(vtvalue
);Constructs the vector.
Discussion
If a single
value
is 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 bool
isFinite
();Returns
true
if all values are not nan and finite, otherwisefalse
. -
Declaration
void
clear
(vtvalue
);Sets all values of the vector to
value
. -
Declaration
void
update
(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 real
magnitude_squared
();Returns the squared magnitude of the vector.
-
Declaration
const @property real
magnitude
();
aliaslength_squared
= magnitude_squared;
aliaslength
= magnitude;Returns the
magnitude
of the vector. -
Declaration
void
normalize
();Normalizes the vector.
-
Declaration
const @property Vector
normalized
();Returns a
normalized
copy of the current vector.
-
Declaration
pure nothrow @safe T.vt
dot
(T)(const Tveca
, const Tvecb
) if (is_vector!T);Calculates the product between two vectors.
-
Declaration
pure nothrow @safe T
cross
(T)(const Tveca
, const Tvecb
) if (is_vector!T && T.dimension == 3);Calculates the
cross
product of two 3-dimensional vectors. -
Declaration
pure nothrow @safe T.vt
distance
(T)(const Tveca
, const Tvecb
) if (is_vector!T);Calculates the
distance
between two vectors. -
Declaration
pure nothrow @safe T
reflect
(T)(const Tvec
, const Tnorm
) if (is_vector!T);reflect
a vector using a surface normal -
Declaration
alias
vec2
= 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
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;
-
Declaration
alias
mt
= type;Holds the internal type of the matrix;
-
Declaration
static const int
rows
;Holds the number of
rows
; -
Declaration
static const int
cols
;Holds the number of columns;
-
Declaration
mt[cols][rows]
matrix
;Holds the
matrix
row-major in memory. -
Declaration
const @property auto
value_ptr
();Returns the pointer to the stored values as OpenGL requires it.
Discussion
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 string
as_string
();
aliastoString
= as_string;Returns the current matrix formatted as flat string.
-
Declaration
@property string
as_pretty_string
();
aliastoPrettyString
= as_pretty_string;Returns the current matrix as pretty formatted string.
-
Declaration
this(Args...)(Args
args
);
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:
Discussion
If a single
value
is 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 bool
isFinite
();Returns
true
if all values are not nan and finite, otherwisefalse
. -
Declaration
void
clear
(mtvalue
);Sets all values of the matrix to
value
(each column in each row will contain thisvalue
). -
Declaration
void
make_identity
();Makes the current matrix an identity matrix.
-
Declaration
static @property Matrix
identity
();Returns a
identity
matrix. -
Declaration
void
transpose
();Transposes the current matrix;
-
Declaration
const @property Matrix!(mt, cols, rows)
transposed
();Returns a
transposed
copy of the matrix. -
Declaration
static Matrix
translation
(mtx
, mty
, mtz
);
static Matrixtranslation
(Vector!(mt, 3)v
);Returns a
translation
matrix (3x3 and 4x4 matrices). -
Declaration
Matrix
translate
(mtx
, mty
, mtz
);
Matrixtranslate
(Vector!(mt, 3)v
);Applys a translation on the current matrix and returns this (3x3 and 4x4 matrices).
-
Declaration
static Matrix
scaling
(mtx
, mty
, mtz
);Returns a
scaling
matrix (3x3 and 4x4 matrices); -
Declaration
Matrix
scale
(mtx
, mty
, mtz
);Applys a
scale
to the current matrix and returns this (3x3 and 4x4 matrices). -
Declaration
static Matrix
rotation
(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 Matrix
xrotation
(realalpha
);Returns an identity matrix with an applied rotation around the x-axis (nxn matrices, n >= 3).
-
Declaration
static Matrix
yrotation
(realalpha
);Returns an identity matrix with an applied rotation around the y-axis (nxn matrices, n >= 3).
-
Declaration
static Matrix
zrotation
(realalpha
);Returns an identity matrix with an applied rotation around the z-axis (nxn matrices, n >= 3).
-
Declaration
Matrix
rotatex
(realalpha
);Rotates the current matrix around the x-axis and returns this (nxn matrices, n >= 3).
-
Declaration
Matrix
rotatey
(realalpha
);Rotates the current matrix around the y-axis and returns this (nxn matrices, n >= 3).
-
Declaration
Matrix
rotatez
(realalpha
);Rotates the current matrix around the z-axis and returns this (nxn matrices, n >= 3).
-
Declaration
void
set_translation
(mt[]values
...);Sets the translation of the matrix (nxn matrices, n >= 3).
-
Declaration
void
set_translation
(Matrixmat
);Copyies the translation from
mat
to the current matrix (nxn matrices, n >= 3). -
Declaration
Matrix
get_translation
();Returns an identity matrix with the current translation applied (nxn matrices, n >= 3)..
-
Declaration
void
set_scale
(mt[]values
...);Sets the scale of the matrix (nxn matrices, n >= 3).
-
Declaration
void
set_scale
(Matrixmat
);Copyies the scale from
mat
to the current matrix (nxn matrices, n >= 3). -
Declaration
Matrix
get_scale
();Returns an identity matrix with the current scale applied (nxn matrices, n >= 3).
-
Declaration
void
set_rotation
(Matrix!(mt, 3, 3)rot
);Copies
rot
into 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 Matrix
inverse
();Returns an inverted copy of the current matrix (nxn matrices, 2 >= n <= 4).
-
Declaration
void
invert
();Inverts the current matrix (nxn matrices, 2 >= n <= 4).
-
Declaration
alias
mat2
= Matrix!(float, 2, 2).Matrix;Pre-defined matrix types, the first number represents the number of rows
Discussion
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
struct
Quaternion
(type);Base template for all quaternion-types.
Parameters
type
all values get stored as this type
-
Declaration
alias
qt
= type;Holds the internal type of the quaternion.
-
Declaration
qt[4]
quaternion
;Holds the w, x, y and z coordinates.
-
Declaration
const @property auto
value_ptr
();Returns a pointer to the quaternion in memory, it starts with the w coordinate.
-
Declaration
@property string
as_string
();Returns the current vector formatted as string, useful for printing the quaternion.
-
Declaration
alias
w
= get_!'w
';
aliasx
= get_!'x
';
aliasy
= get_!'y
';
aliasz
= get_!'z
';static properties to access the values.
-
Declaration
this(qt
w_
, qtx_
, qty_
, qtz_
);
this(qtw_
, Vector!(qt, 3)vec
);
this(Vector!(qt, 4)vec
);Constructs the quaternion.
Discussion
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 bool
isFinite
();Returns
true
if all values are not nan and finite, otherwisefalse
. -
Declaration
const @property real
magnitude_squared
();Returns the squared magnitude of the quaternion.
-
Declaration
const @property real
magnitude
();Returns the
magnitude
of the quaternion. -
Declaration
static @property Quaternion
identity
();Returns an
identity
quaternion (w=1, x=0, y=0, z=0). -
Declaration
void
make_identity
();Makes the current quaternion an identity quaternion.
-
Declaration
void
invert
();
aliasconjugate
= invert;Inverts the quaternion.
-
Declaration
const @property Quaternion
inverse
();
aliasconjugated
= inverse;Returns an inverted copy of the current quaternion.
-
Declaration
static Quaternion
from_matrix
(Matrix!(qt, 3, 3)matrix
);Creates a quaternion from a 3x3
matrix
.Parameters
Matrix!(qt, 3, 3)
matrix
3x3
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
rows
number of rows of the resulting matrix (min 3)
cols
number of columns of the resulting matrix (min 3)
-
Declaration
void
normalize
();Normalizes the current quaternion.
-
Declaration
const Quaternion
normalized
();Returns a
normalized
copy of the current quaternion. -
Declaration
const @property real
yaw
();Returns the
yaw
. -
Declaration
const @property real
pitch
();Returns the
pitch
. -
Declaration
const @property real
roll
();Returns the
roll
. -
Declaration
static Quaternion
xrotation
(realalpha
);Returns a quaternion with applied rotation around the x-axis.
-
Declaration
static Quaternion
yrotation
(realalpha
);Returns a quaternion with applied rotation around the y-axis.
-
Declaration
static Quaternion
zrotation
(realalpha
);Returns a quaternion with applied rotation around the z-axis.
-
Declaration
static Quaternion
axis_rotation
(realalpha
, Vector!(qt, 3)axis
);Returns a quaternion with applied rotation around an
axis
. -
Declaration
static Quaternion
euler_rotation
(realroll
, realpitch
, realyaw
);Creates a quaternion from an euler rotation.
-
Declaration
Quaternion
rotatex
(realalpha
);Rotates the current quaternion around the x-axis and returns this.
-
Declaration
Quaternion
rotatey
(realalpha
);Rotates the current quaternion around the y-axis and returns this.
-
Declaration
Quaternion
rotatez
(realalpha
);Rotates the current quaternion around the z-axis and returns this.
-
Declaration
Quaternion
rotate_axis
(realalpha
, Vector!(qt, 3)axis
);Rotates the current quaternion around an
axis
and returns this. -
Declaration
Quaternion
rotate_euler
(realheading
, realattitude
, realbank
);Applies an euler rotation to the current quaternion and returns this.
-
-
Declaration
alias
quat
= Quaternion!float.Quaternion;Pre-defined quaternion of type float.