gl3n.linalg

gl3n.linalg

Special thanks to:

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.

Params:
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.

static const int dimension;
Holds the dimension of the vector.

vt[dimension] vector;
Holds all coordinates, length conforms dimension.

const @property auto value_ptr();
Returns a pointer to the coordinates.

@property string as_string();
alias toString = as_string;
Returns the current vector formatted as string, useful for printing the vector.

alias x = get_!'x';
alias u = x;
alias s = x;
alias r = x;
alias y = get_!'y';
alias v = y;
alias t = y;
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.

enum Vector e1;
enum Vector e2;
canonical basis for Euclidian space

this(Args...)(Args args);
this(T)(T vec) if (is_vector!T && is(T.vt : vt) && T.dimension >= dimension);
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 @property bool isFinite();
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 @property Vector!(vt, s.length) opDispatch(string s)();
Implements dynamic swizzling.

Returns:
a Vector

const @property real magnitude_squared();
Returns the squared magnitude of the vector.

const @property real magnitude();
alias length_squared = magnitude_squared;
alias length = magnitude;
Returns the magnitude of the vector.

void normalize();
Normalizes the vector.

const @property Vector normalized();
Returns a normalized copy of the current vector.

pure nothrow @safe T.vt dot(T)(const T veca, const T vecb) if (is_vector!T);
Calculates the product between two vectors.

pure nothrow @safe T cross(T)(const T veca, const T vecb) if (is_vector!T && T.dimension == 3);
Calculates the cross product of two 3-dimensional vectors.

pure nothrow @safe T.vt distance(T)(const T veca, const T vecb) if (is_vector!T);
Calculates the distance between two vectors.

pure nothrow @safe T reflect(T)(const T vec, const T norm) if (is_vector!T);
reflect a vector using a surface normal

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.

Params:
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;

static const int rows;
Holds the number of rows;

static const int cols;
Holds the number of columns;

mt[cols][rows] matrix;
Holds the matrix row-major in memory.

const @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);



@property string as_string();
alias toString = as_string;
Returns the current matrix formatted as flat string.

@property 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) if (is_matrix!T && T.cols >= cols && T.rows >= rows);
this(T)(T mat) if (is_matrix!T && T.cols < cols && T.rows < rows);
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 @property bool isFinite();
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.

static @property Matrix identity();
Returns a identity matrix.

void transpose();
Transposes the current matrix;

const @property Matrix!(mt, cols, rows) transposed();
Returns a transposed copy of the matrix.

static Matrix translation(mt x, mt y, mt z);
static Matrix translation(Vector!(mt, 3) v);
Returns a translation matrix (3x3 and 4x4 matrices).

Matrix translate(mt x, mt y, mt z);
Matrix translate(Vector!(mt, 3) v);
Applys a translation on the current matrix and returns this (3x3 and 4x4 matrices).

static Matrix scaling(mt x, mt y, mt z);
Returns a scaling matrix (3x3 and 4x4 matrices);

Matrix scale(mt x, mt y, mt z);
Applys a scale to the current matrix and returns this (3x3 and 4x4 matrices).

static Matrix rotation(real alpha, Vector!(mt, 3) axis);
static 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).

static Matrix xrotation(real alpha);
Returns an identity matrix with an applied rotation around the x-axis (nxn matrices, n >= 3).

static Matrix yrotation(real alpha);
Returns an identity matrix with an applied rotation around the y-axis (nxn matrices, n >= 3).

static 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 set_translation(mt[] values...);
Sets the translation of the matrix (nxn matrices, n >= 3).

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

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

void set_scale(mt[] values...);
Sets the scale of the matrix (nxn matrices, n >= 3).

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

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

void set_rotation(Matrix!(mt, 3, 3) rot);
Copies rot into the upper left corner, the translation (nxn matrices, n >= 3).

Matrix!(mt, 3, 3) get_rotation();
Returns an identity matrix with the current rotation applied (nxn matrices, n >= 3).

const @property Matrix inverse();
Returns an inverted copy of the current matrix (nxn matrices, 2 >= n <= 4).

void invert();
Inverts the current matrix (nxn matrices, 2 >= 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.

Params:
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.

const @property auto value_ptr();
Returns a pointer to the quaternion in memory, it starts with the w coordinate.

@property 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 @property bool isFinite();
Returns true if all values are not nan and finite, otherwise false.

const @property real magnitude_squared();
Returns the squared magnitude of the quaternion.

const @property real magnitude();
Returns the magnitude of the quaternion.

static @property 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 @property Quaternion inverse();
alias conjugated = inverse;
Returns an inverted copy of the current quaternion.

static Quaternion from_matrix(Matrix!(qt, 3, 3) matrix);
Creates a quaternion from a 3x3 matrix.

Params:
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)() if (rows >= 3 && cols >= 3);
Returns the quaternion as matrix.

Params:
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 @property real yaw();
Returns the yaw.

const @property real pitch();
Returns the pitch.

const @property real roll();
Returns the roll.

static Quaternion xrotation(real alpha);
Returns a quaternion with applied rotation around the x-axis.

static Quaternion yrotation(real alpha);
Returns a quaternion with applied rotation around the y-axis.

static Quaternion zrotation(real alpha);
Returns a quaternion with applied rotation around the z-axis.

static Quaternion axis_rotation(real alpha, Vector!(qt, 3) axis);
Returns a quaternion with applied rotation around an axis.

static Quaternion euler_rotation(real roll, real pitch, real yaw);
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.


Page generated by Ddoc.