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.

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.

  • 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

    1. alias Vector!(int, 3) vec3i; alias Vector!(float, 4) vec4; alias Vector!(real, 2) vec2r;

    • vt

      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();
      alias toString = as_string;

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

    • Declaration

      inout @property ref inout(vt) get_(char coord)();

    • x

      Declaration

      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.

    • e1

      Declaration

      enum Vector e1;
      enum Vector e2;

      canonical basis for Euclidian space

    • Declaration

      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

      1. 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, otherwise false.

    • Declaration

      void clear(vt value);

      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();
      alias length_squared = magnitude_squared;
      alias length = 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.

  • dot

    Declaration

    pure nothrow @safe T.vt dot(T)(const T veca, const T vecb) if (is_vector!T);

    Calculates the product between two vectors.

  • Declaration

    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.

  • Declaration

    pure nothrow @safe T.vt distance(T)(const T veca, const T vecb) if (is_vector!T);

    Calculates the distance between two vectors.

  • Declaration

    pure nothrow @safe T reflect(T)(const T vec, const T norm) if (is_vector!T);

    reflect a vector using a surface normal

  • Declaration

    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).

  • 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

    1. alias Matrix!(float, 4, 4) mat4; alias Matrix!(double, 3, 4) mat34d; alias Matrix!(real, 2, 2) mat2r;

    • mt

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

      1. // 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();
      alias toString = as_string;

      Returns the current matrix formatted as flat string.

    • Declaration

      @property string as_pretty_string();
      alias toPrettyString = as_pretty_string;

      Returns the current matrix as pretty formatted string.

    • Declaration

      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

      1. 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, otherwise false.

    • Declaration

      void clear(mt value);

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

    • 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(mt x, mt y, mt z);
      static Matrix translation(Vector!(mt, 3) v);

      Returns a translation matrix (3x3 and 4x4 matrices).

    • Declaration

      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).

    • Declaration

      static Matrix scaling(mt x, mt y, mt z);

      Returns a scaling matrix (3x3 and 4x4 matrices);

    • Declaration

      Matrix scale(mt x, mt y, mt z);

      Applys a scale to the current matrix and returns this (3x3 and 4x4 matrices).

    • Declaration

      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).

    • Declaration

      static Matrix xrotation(real alpha);

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

    • Declaration

      static Matrix yrotation(real alpha);

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

    • Declaration

      static Matrix zrotation(real alpha);

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

    • Declaration

      Matrix rotatex(real alpha);

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

    • Declaration

      Matrix rotatey(real alpha);

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

    • Declaration

      Matrix rotatez(real alpha);

      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(Matrix mat);

      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(Matrix mat);

      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 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

    • qt

      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.

    • w

      Declaration

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

      static properties to access the values.

    • Declaration

      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.

    • Declaration

      const @property bool isFinite();

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

    • 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();
      alias conjugate = invert;

      Inverts the quaternion.

    • Declaration

      const @property Quaternion inverse();
      alias conjugated = 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

      vec3 to_axis_angle();

      Returns the quaternion as a vec3 (axis / angle representation).

    • Declaration

      void normalize();

      Normalizes the current quaternion.

    • Declaration

      const Quaternion normalized();

      Returns a normalized copy of the current quaternion.

    • yaw

      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(real alpha);

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

    • Declaration

      static Quaternion yrotation(real alpha);

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

    • Declaration

      static Quaternion zrotation(real alpha);

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

    • Declaration

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

      Returns a quaternion with applied rotation around an axis.

    • Declaration

      static Quaternion euler_rotation(real roll, real pitch, real yaw);

      Creates a quaternion from an euler rotation.

    • Declaration

      Quaternion rotatex(real alpha);

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

    • Declaration

      Quaternion rotatey(real alpha);

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

    • Declaration

      Quaternion rotatez(real alpha);

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

    • Declaration

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

      Rotates the current quaternion around an axis and returns this.

    • Declaration

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

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

  • Declaration

    alias quat = Quaternion!float.Quaternion;

    Pre-defined quaternion of type float.