cgmath/src/mat.rs

273 lines
5.7 KiB
Rust
Raw Normal View History

2012-12-13 13:01:42 +00:00
use core::cmp::Eq;
use std::cmp::FuzzyEq;
2012-12-08 02:59:10 +00:00
use angle::Angle;
2012-12-14 06:22:45 +00:00
use quat::Quat;
2012-12-13 13:01:42 +00:00
pub mod mat2;
pub mod mat3;
pub mod mat4;
pub use mat2::Mat2;
pub use mat3::Mat3;
pub use mat4::Mat4;
/**
2012-12-03 22:31:26 +00:00
* The base square matrix trait
*
* # 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-12 01:29:35 +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
*/
pure fn col(&self, i: uint) -> V;
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
*/
pure fn row(&self, i: uint) -> V;
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
*/
static pure fn identity() -> self;
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
*/
2012-11-20 06:57:32 +00:00
static pure fn zero() -> self;
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
*/
pure fn mul_t(&self, value: T) -> self;
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;
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
*/
pure fn add_m(&self, other: &self) -> self;
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
*/
pure fn sub_m(&self, other: &self) -> self;
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
*/
pure fn mul_m(&self, other: &self) -> self;
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
*/
pure fn dot(&self, other: &self) -> T;
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;
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
*/
pure fn trace(&self) -> T;
2012-12-03 22:31:26 +00:00
/**
* Returns the inverse of the matrix
*
* # 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
*/
2012-12-03 22:12:22 +00:00
pure fn inverse(&self) -> Option<self>;
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
*/
pure fn transpose(&self) -> self;
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
*
* `true` if the matrix is approximately equal to the identity matrix
2012-12-03 22:31:26 +00:00
*/
pure fn is_identity(&self) -> bool;
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
*/
pure fn is_diagonal(&self) -> bool;
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
*/
pure fn is_rotated(&self) -> bool;
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
*/
pure fn is_symmetric(&self) -> bool;
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
*/
pure fn is_invertible(&self) -> bool;
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;
}
/**
* A mutable matrix
*/
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`
*/
fn col_mut(&mut self, i: uint) -> &self/mut V;
/**
* Swap two columns of the matrix in place
*/
fn swap_cols(&mut self, a: uint, b: uint);
/**
* Swap two rows of the matrix in place
*/
fn swap_rows(&mut self, a: uint, b: uint);
2012-12-05 03:49:59 +00:00
/**
* Sets the matrix to `other`
*/
fn set(&mut self, other: &self);
/**
* Sets the matrix to the identity matrix
*/
fn to_identity(&mut self);
/**
* 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);
/**
* Add the matrix `other` to `self`
*/
fn add_self_m(&mut self, other: &self);
/**
* Subtract the matrix `other` from `self`
*/
fn sub_self_m(&mut self, other: &self);
/**
* Sets the matrix to its inverse
*
* # 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);
/**
* Sets the matrix to its transpose
*/
fn transpose_self(&mut self);
}
2012-12-03 22:31:26 +00:00
/**
* A 2 x 2 matrix
2012-12-03 22:31:26 +00:00
*/
pub trait Matrix2<T,V>: Matrix<T,V> {
pure fn to_mat3(&self) -> Mat3<T>;
pure fn to_mat4(&self) -> Mat4<T>;
}
2012-12-03 22:31:26 +00:00
/**
* A 3 x 3 matrix
2012-12-03 22:31:26 +00:00
*/
2012-12-14 06:22:45 +00:00
pub trait Matrix3<T,V>: Matrix<T,V> {
2012-12-13 13:01:42 +00:00
static pure fn from_axis_angle<A:Angle<T>>(axis: &V, theta: A) -> Mat3<T>;
pure fn to_mat4(&self) -> Mat4<T>;
2012-12-14 06:22:45 +00:00
/**
* Convert the matrix to a quaternion
*/
pure fn to_Quat() -> Quat<T>;
}
2012-12-03 22:31:26 +00:00
/**
* A 4 x 4 matrix
2012-12-03 22:31:26 +00:00
*/
pub trait Matrix4<T,V>: Matrix<T,V> {
2012-12-13 13:01:42 +00:00
}