2012-12-13 13:01:42 +00:00
|
|
|
use core::cmp::Eq;
|
2012-11-15 02:23:39 +00:00
|
|
|
use std::cmp::FuzzyEq;
|
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
use vec::Vec3;
|
2012-12-14 06:22:45 +00:00
|
|
|
use quat::Quat;
|
2012-12-13 13:01:42 +00:00
|
|
|
|
2013-01-29 01:13:44 +00:00
|
|
|
pub use mat2::{Mat2, mat2, dmat2};
|
|
|
|
pub use mat3::{Mat3, mat3, dmat3};
|
|
|
|
pub use mat4::{Mat4, mat4, dmat4};
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-02 22:15:40 +00:00
|
|
|
/**
|
2012-12-03 22:31:26 +00:00
|
|
|
* The base square matrix trait
|
2012-12-05 01:51:18 +00:00
|
|
|
*
|
|
|
|
* # Type parameters
|
|
|
|
*
|
|
|
|
* * `T` - The type of the elements of the matrix. Should be a floating point type.
|
|
|
|
* * `V` - The type of the row and column vectors. Should have components of a
|
|
|
|
* floating point type and have the same number of dimensions as the
|
|
|
|
* number of rows and columns in the matrix.
|
2012-12-02 22:15:40 +00:00
|
|
|
*/
|
2013-03-28 09:45:43 +00:00
|
|
|
pub trait Matrix<T,V>: Index<uint, V> + Eq + Neg<Self> {
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The column vector at `i`
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2012-12-02 22:15:40 +00:00
|
|
|
pure fn col(&self, i: uint) -> V;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The row vector at `i`
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2012-12-02 22:15:40 +00:00
|
|
|
pure fn row(&self, i: uint) -> V;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
/**
|
|
|
|
* Construct a diagonal matrix with the major diagonal set to `value`
|
|
|
|
*/
|
2013-01-29 10:23:22 +00:00
|
|
|
static pure fn from_value(value: T) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The identity matrix
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2013-01-29 10:23:22 +00:00
|
|
|
static pure fn identity() -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* A matrix with all elements set to zero
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2013-01-29 10:23:22 +00:00
|
|
|
static pure fn zero() -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The scalar multiplication of this matrix and `value`
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2013-01-29 10:23:22 +00:00
|
|
|
pure fn mul_t(&self, value: T) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The matrix vector product of the matrix and `vec`
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2012-12-03 22:06:00 +00:00
|
|
|
pure fn mul_v(&self, vec: &V) -> V;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The matrix addition of the matrix and `other`
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2013-01-29 10:23:22 +00:00
|
|
|
pure fn add_m(&self, other: &Self) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The difference between the matrix and `other`
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2013-01-29 10:23:22 +00:00
|
|
|
pure fn sub_m(&self, other: &Self) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The matrix product of the matrix and `other`
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2013-01-29 10:23:22 +00:00
|
|
|
pure fn mul_m(&self, other: &Self) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The matrix dot product of the matrix and `other`
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2013-01-29 10:23:22 +00:00
|
|
|
pure fn dot(&self, other: &Self) -> T;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The determinant of the matrix
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2012-12-03 01:08:36 +00:00
|
|
|
pure fn determinant(&self) -> T;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The sum of the main diagonal of the matrix
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn trace(&self) -> T;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
|
|
|
* Returns the inverse of the matrix
|
2013-03-28 09:45:43 +00:00
|
|
|
*
|
2012-12-04 01:56:10 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
2012-12-05 08:09:53 +00:00
|
|
|
* * `Some(m)` - if the inversion was successful, where `m` is the inverted matrix
|
|
|
|
* * `None` - if the inversion was unsuccessful (because the matrix was not invertable)
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2013-01-29 10:23:22 +00:00
|
|
|
pure fn inverse(&self) -> Option<Self>;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The transposed matrix
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2013-01-29 10:23:22 +00:00
|
|
|
pure fn transpose(&self) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* Check to see if the matrix is an identity matrix
|
|
|
|
*
|
|
|
|
* # Return value
|
2013-03-28 09:45:43 +00:00
|
|
|
*
|
2012-12-05 08:09:53 +00:00
|
|
|
* `true` if the matrix is approximately equal to the identity matrix
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_identity(&self) -> bool;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* Check to see if the matrix is diagonal
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* `true` all the elements outside the main diagonal are approximately
|
|
|
|
* equal to zero.
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_diagonal(&self) -> bool;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* Check to see if the matrix is rotated
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* `true` if the matrix is not approximately equal to the identity matrix.
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_rotated(&self) -> bool;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* Check to see if the matrix is symmetric
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* `true` if the matrix is approximately equal to its transpose).
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2012-12-02 22:15:40 +00:00
|
|
|
pure fn is_symmetric(&self) -> bool;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* Check to see if the matrix is invertable
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* `true` if the matrix is invertable
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_invertible(&self) -> bool;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-12 01:29:35 +00:00
|
|
|
/**
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* A pointer to the first element of the matrix
|
|
|
|
*/
|
|
|
|
pure fn to_ptr(&self) -> *T;
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
/**
|
|
|
|
* A 2 x 2 matrix
|
|
|
|
*/
|
|
|
|
pub trait Matrix2<T,V>: Matrix<T,V> {
|
|
|
|
static pure fn new(c0r0: T, c0r1: T,
|
2013-01-29 10:23:22 +00:00
|
|
|
c1r0: T, c1r1: T) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 10:23:22 +00:00
|
|
|
static pure fn from_cols(c0: V, c1: V) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 10:23:22 +00:00
|
|
|
static pure fn from_angle(radians: T) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
pure fn to_mat3(&self) -> Mat3<T>;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
pure fn to_mat4(&self) -> Mat4<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A 3 x 3 matrix
|
|
|
|
*/
|
|
|
|
pub trait Matrix3<T,V>: Matrix<T,V> {
|
|
|
|
static pure fn new(c0r0:T, c0r1:T, c0r2:T,
|
|
|
|
c1r0:T, c1r1:T, c1r2:T,
|
2013-01-29 10:23:22 +00:00
|
|
|
c2r0:T, c2r1:T, c2r2:T) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 10:23:22 +00:00
|
|
|
static pure fn from_cols(c0: V, c1: V, c2: V) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 10:23:22 +00:00
|
|
|
static pure fn from_angle_x(radians: T) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 10:23:22 +00:00
|
|
|
static pure fn from_angle_y(radians: T) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 10:23:22 +00:00
|
|
|
static pure fn from_angle_z(radians: T) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 10:23:22 +00:00
|
|
|
static pure fn from_angle_xyz(radians_x: T, radians_y: T, radians_z: T) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 10:23:22 +00:00
|
|
|
static pure fn from_angle_axis(radians: T, axis: &Vec3<T>) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 10:23:22 +00:00
|
|
|
static pure fn from_axes(x: V, y: V, z: V) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 10:23:22 +00:00
|
|
|
static pure fn look_at(dir: &Vec3<T>, up: &Vec3<T>) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
pure fn to_mat4(&self) -> Mat4<T>;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
pure fn to_quat(&self) -> Quat<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A 4 x 4 matrix
|
|
|
|
*/
|
|
|
|
pub trait Matrix4<T,V>: Matrix<T,V> {
|
|
|
|
static pure fn new(c0r0: T, c0r1: T, c0r2: T, c0r3: T,
|
|
|
|
c1r0: T, c1r1: T, c1r2: T, c1r3: T,
|
|
|
|
c2r0: T, c2r1: T, c2r2: T, c2r3: T,
|
2013-01-29 10:23:22 +00:00
|
|
|
c3r0: T, c3r1: T, c3r2: T, c3r3: T) -> Self;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 10:23:22 +00:00
|
|
|
static pure fn from_cols(c0: V, c1: V, c2: V, c3: V) -> Self;
|
2013-01-29 09:26:48 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 01:38:30 +00:00
|
|
|
/**
|
|
|
|
* A mutable matrix
|
|
|
|
*/
|
2012-12-04 11:42:32 +00:00
|
|
|
pub trait MutableMatrix<T,V>: Matrix<T,V> {
|
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* A mutable reference to the column at `i`
|
2012-12-04 11:42:32 +00:00
|
|
|
*/
|
|
|
|
fn col_mut(&mut self, i: uint) -> &self/mut V;
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-04 11:42:32 +00:00
|
|
|
/**
|
|
|
|
* Swap two columns of the matrix in place
|
|
|
|
*/
|
|
|
|
fn swap_cols(&mut self, a: uint, b: uint);
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-04 11:42:32 +00:00
|
|
|
/**
|
2013-03-28 09:45:43 +00:00
|
|
|
* Swap two rows of the matrix in place
|
2012-12-04 11:42:32 +00:00
|
|
|
*/
|
|
|
|
fn swap_rows(&mut self, a: uint, b: uint);
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-05 03:49:59 +00:00
|
|
|
/**
|
2013-03-28 09:45:43 +00:00
|
|
|
* Sets the matrix to `other`
|
2012-12-05 03:49:59 +00:00
|
|
|
*/
|
2013-01-29 10:23:22 +00:00
|
|
|
fn set(&mut self, other: &Self);
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-04 11:42:32 +00:00
|
|
|
/**
|
|
|
|
* Sets the matrix to the identity matrix
|
|
|
|
*/
|
|
|
|
fn to_identity(&mut self);
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-04 11:42:32 +00:00
|
|
|
/**
|
|
|
|
* Sets each element of the matrix to zero
|
|
|
|
*/
|
|
|
|
fn to_zero(&mut self);
|
2012-12-05 03:49:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Multiplies the matrix by a scalar
|
|
|
|
*/
|
|
|
|
fn mul_self_t(&mut self, value: T);
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-05 03:49:59 +00:00
|
|
|
/**
|
|
|
|
* Add the matrix `other` to `self`
|
|
|
|
*/
|
2013-01-29 10:23:22 +00:00
|
|
|
fn add_self_m(&mut self, other: &Self);
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-05 03:49:59 +00:00
|
|
|
/**
|
|
|
|
* Subtract the matrix `other` from `self`
|
|
|
|
*/
|
2013-01-29 10:23:22 +00:00
|
|
|
fn sub_self_m(&mut self, other: &Self);
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-05 03:49:59 +00:00
|
|
|
/**
|
|
|
|
* Sets the matrix to its inverse
|
2013-03-28 09:45:43 +00:00
|
|
|
*
|
2012-12-05 03:49:59 +00:00
|
|
|
* # Failure
|
|
|
|
*
|
|
|
|
* Fails if the matrix is not invertable. Make sure you check with the
|
|
|
|
* `is_invertible` method before you attempt this!
|
|
|
|
*/
|
|
|
|
fn invert_self(&mut self);
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-05 03:49:59 +00:00
|
|
|
/**
|
|
|
|
* Sets the matrix to its transpose
|
|
|
|
*/
|
|
|
|
fn transpose_self(&mut self);
|
2013-01-29 09:26:48 +00:00
|
|
|
}
|