2012-11-15 02:23:39 +00:00
|
|
|
use core::cast::transmute;
|
2012-11-22 01:09:04 +00:00
|
|
|
use core::cmp::{Eq, Ord};
|
2012-11-15 02:23:39 +00:00
|
|
|
use core::ptr::to_unsafe_ptr;
|
2012-11-25 12:05:47 +00:00
|
|
|
use core::sys::size_of;
|
2012-11-15 02:23:39 +00:00
|
|
|
use core::vec::raw::buf_as_slice;
|
|
|
|
|
|
|
|
use std::cmp::FuzzyEq;
|
|
|
|
|
2012-12-08 02:59:10 +00:00
|
|
|
use angle::Angle;
|
2012-11-26 06:48:46 +00:00
|
|
|
use funs::common::*;
|
|
|
|
use funs::exponential::*;
|
2012-12-08 02:59:10 +00:00
|
|
|
use funs::triganomic::{sin, cos};
|
2012-12-11 23:55:42 +00:00
|
|
|
use num::types::{Float, Number};
|
2012-11-15 02:23:39 +00:00
|
|
|
use quat::{Quat, ToQuat};
|
2012-11-20 06:57:32 +00:00
|
|
|
use vec::{NumericVector, Vec2, Vec3, Vec4};
|
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
|
|
|
*/
|
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
|
|
|
*/
|
2012-12-02 22:15:40 +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
|
|
|
*/
|
2012-12-02 22:15:40 +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
|
|
|
*/
|
2012-12-02 22:15:40 +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
|
|
|
*/
|
2012-11-30 03:13:20 +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
|
|
|
*/
|
2012-11-30 03:13:20 +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
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn sub_m(&self, other: &self) -> self;
|
2012-11-28 22:49:31 +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
|
|
|
*/
|
2012-11-30 03:13:20 +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
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn dot(&self, other: &self) -> T;
|
2012-11-15 02:23:39 +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;
|
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;
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
|
|
|
* Returns the inverse of the matrix
|
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
|
|
|
*/
|
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
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn transpose(&self) -> self;
|
2012-11-15 02:23:39 +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
|
|
|
|
*
|
|
|
|
* `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;
|
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;
|
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;
|
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;
|
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;
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2012-12-04 11:42:32 +00:00
|
|
|
/**
|
|
|
|
* 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-04 11:42:32 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 01:38:30 +00:00
|
|
|
* A 2 x 2 matrix
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2012-12-02 22:15:40 +00:00
|
|
|
pub trait Matrix2<T,V>: Matrix<T,V> {
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn to_mat3(&self) -> Mat3<T>;
|
|
|
|
pure fn to_mat4(&self) -> Mat4<T>;
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 01:38:30 +00:00
|
|
|
* A 3 x 3 matrix
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2012-12-02 22:15:40 +00:00
|
|
|
pub trait Matrix3<T,V>: Matrix<T,V> {
|
2012-12-08 02:59:10 +00:00
|
|
|
static pure fn from_axis_angle<A:Angle<T>>(axis: &Vec3<T>, theta: A) -> Mat3<T>;
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn to_mat4(&self) -> Mat4<T>;
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 22:31:26 +00:00
|
|
|
/**
|
2012-12-05 01:38:30 +00:00
|
|
|
* A 4 x 4 matrix
|
2012-12-03 22:31:26 +00:00
|
|
|
*/
|
2012-12-02 22:15:40 +00:00
|
|
|
pub trait Matrix4<T,V>: Matrix<T,V> {
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-02 22:15:40 +00:00
|
|
|
/**
|
|
|
|
* A 2 x 2 column major matrix
|
2012-12-05 01:38:30 +00:00
|
|
|
*
|
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.
|
|
|
|
*
|
2012-12-05 01:38:30 +00:00
|
|
|
* # Fields
|
|
|
|
*
|
|
|
|
* * `x` - the first column vector of the matrix
|
|
|
|
* * `y` - the second column vector of the matrix
|
|
|
|
* * `z` - the third column vector of the matrix
|
2012-12-02 22:15:40 +00:00
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
pub struct Mat2<T> { x: Vec2<T>, y: Vec2<T> }
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat2<T> {
|
2012-12-04 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Construct a 2 x 2 matrix
|
2012-12-05 01:38:30 +00:00
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * `c0r0`, `c0r1` - the first column of the matrix
|
|
|
|
* * `c1r0`, `c1r1` - the second column of the matrix
|
|
|
|
*
|
2012-12-04 00:58:19 +00:00
|
|
|
* ~~~
|
2012-12-04 04:34:09 +00:00
|
|
|
* c0 c1
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r0 | c0r0 | c1r0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r1 | c0r1 | c1r1 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+
|
|
|
|
* ~~~
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-20 12:27:17 +00:00
|
|
|
static pure fn new(c0r0: T, c0r1: T,
|
2012-11-15 02:23:39 +00:00
|
|
|
c1r0: T, c1r1: T) -> Mat2<T> {
|
|
|
|
Mat2::from_cols(Vec2::new(move c0r0, move c0r1),
|
|
|
|
Vec2::new(move c1r0, move c1r1))
|
|
|
|
}
|
2012-12-04 00:58:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a 2 x 2 matrix from column vectors
|
2012-12-05 01:38:30 +00:00
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * `c0` - the first column vector of the matrix
|
|
|
|
* * `c1` - the second column vector of the matrix
|
|
|
|
*
|
2012-12-04 00:58:19 +00:00
|
|
|
* ~~~
|
2012-12-04 04:34:09 +00:00
|
|
|
* c0 c1
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r0 | c0.x | c1.x |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r1 | c0.y | c1.y |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+
|
|
|
|
* ~~~
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-20 12:27:17 +00:00
|
|
|
static pure fn from_cols(c0: Vec2<T>, c1: Vec2<T>) -> Mat2<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat2 { x: move c0,
|
|
|
|
y: move c1 }
|
|
|
|
}
|
|
|
|
|
2012-12-04 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Construct a 2 x 2 diagonal matrix with the major diagonal set to `value`
|
2012-12-05 01:38:30 +00:00
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * `value` - the value to set the major diagonal to
|
|
|
|
*
|
2012-12-04 00:58:19 +00:00
|
|
|
* ~~~
|
2012-12-04 04:34:09 +00:00
|
|
|
* c0 c1
|
2012-12-04 00:58:19 +00:00
|
|
|
* +-----+-----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r0 | val | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +-----+-----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r1 | 0 | val |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +-----+-----+
|
|
|
|
* ~~~
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-20 12:27:17 +00:00
|
|
|
static pure fn from_value(value: T) -> Mat2<T> {
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0 = Number::from(0);
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat2::new(value, _0,
|
|
|
|
_0, value)
|
|
|
|
}
|
2012-12-01 12:49:24 +00:00
|
|
|
|
|
|
|
// FIXME: An interim solution to the issues with static functions
|
|
|
|
#[inline(always)]
|
|
|
|
static pure fn identity() -> Mat2<T> {
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0 = Number::from(0);
|
|
|
|
let _1 = Number::from(1);
|
2012-12-01 12:49:24 +00:00
|
|
|
Mat2::new(_1, _0,
|
|
|
|
_0, _1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: An interim solution to the issues with static functions
|
|
|
|
#[inline(always)]
|
|
|
|
static pure fn zero() -> Mat2<T> {
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0 = Number::from(0);
|
2012-12-01 12:49:24 +00:00
|
|
|
Mat2::new(_0, _0,
|
|
|
|
_0, _0)
|
|
|
|
}
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat2<T>: Matrix<T, Vec2<T>> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn col(&self, i: uint) -> Vec2<T> { self[i] }
|
2012-11-15 02:23:39 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn row(&self, i: uint) -> Vec2<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec2::new(self[0][i],
|
|
|
|
self[1][i])
|
|
|
|
}
|
2012-12-01 04:19:21 +00:00
|
|
|
|
2012-12-04 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Returns the multiplicative identity matrix
|
|
|
|
* ~~~
|
2012-12-04 04:34:09 +00:00
|
|
|
* c0 c1
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r0 | 1 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r1 | 0 | 1 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+
|
|
|
|
* ~~~
|
|
|
|
*/
|
2012-12-01 04:19:21 +00:00
|
|
|
#[inline(always)]
|
2012-12-02 22:15:40 +00:00
|
|
|
static pure fn identity() -> Mat2<T> {
|
2012-12-03 06:19:53 +00:00
|
|
|
let _0 = Number::from(0);
|
|
|
|
let _1 = Number::from(1);
|
2012-12-02 22:15:40 +00:00
|
|
|
Mat2::new(_1, _0,
|
|
|
|
_0, _1)
|
2012-11-21 08:35:26 +00:00
|
|
|
}
|
2012-12-02 22:15:40 +00:00
|
|
|
|
2012-12-04 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Returns the additive identity matrix
|
|
|
|
* ~~~
|
2012-12-04 04:34:09 +00:00
|
|
|
* c0 c1
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r0 | 0 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r1 | 0 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+
|
|
|
|
* ~~~
|
|
|
|
*/
|
2012-11-20 06:57:32 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn zero() -> Mat2<T> {
|
2012-12-03 06:19:53 +00:00
|
|
|
let _0 = Number::from(0);
|
2012-12-01 12:49:24 +00:00
|
|
|
Mat2::new(_0, _0,
|
|
|
|
_0, _0)
|
2012-11-20 06:57:32 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn mul_t(&self, value: T) -> Mat2<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat2::from_cols(self[0].mul_t(value),
|
|
|
|
self[1].mul_t(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-12-03 22:06:00 +00:00
|
|
|
pure fn mul_v(&self, vec: &Vec2<T>) -> Vec2<T> {
|
|
|
|
Vec2::new(self.row(0).dot(vec),
|
|
|
|
self.row(1).dot(vec))
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn add_m(&self, other: &Mat2<T>) -> Mat2<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat2::from_cols(self[0].add_v(&other[0]),
|
|
|
|
self[1].add_v(&other[1]))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn sub_m(&self, other: &Mat2<T>) -> Mat2<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat2::from_cols(self[0].sub_v(&other[0]),
|
|
|
|
self[1].sub_v(&other[1]))
|
|
|
|
}
|
2012-11-20 06:57:32 +00:00
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn mul_m(&self, other: &Mat2<T>) -> Mat2<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat2::new(self.row(0).dot(&other.col(0)), self.row(1).dot(&other.col(0)),
|
|
|
|
self.row(0).dot(&other.col(1)), self.row(1).dot(&other.col(1)))
|
|
|
|
}
|
2012-11-21 23:24:43 +00:00
|
|
|
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn dot(&self, other: &Mat2<T>) -> T {
|
|
|
|
other.transpose().mul_m(self).trace()
|
2012-11-21 23:24:43 +00:00
|
|
|
}
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-03 01:08:36 +00:00
|
|
|
pure fn determinant(&self) -> T {
|
2012-11-21 08:08:08 +00:00
|
|
|
self[0][0] * self[1][1] - self[1][0] * self[0][1]
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn trace(&self) -> T {
|
2012-11-21 21:27:44 +00:00
|
|
|
self[0][0] + self[1][1]
|
|
|
|
}
|
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-12-03 22:12:22 +00:00
|
|
|
pure fn inverse(&self) -> Option<Mat2<T>> {
|
2012-12-03 01:08:36 +00:00
|
|
|
let d = self.determinant();
|
2012-12-13 01:10:53 +00:00
|
|
|
if d.fuzzy_eq(&Number::from(0)) {
|
2012-11-15 02:23:39 +00:00
|
|
|
None
|
|
|
|
} else {
|
2012-11-21 08:08:08 +00:00
|
|
|
Some(Mat2::new( self[1][1]/d, -self[0][1]/d,
|
|
|
|
-self[1][0]/d, self[0][0]/d))
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn transpose(&self) -> Mat2<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat2::new(self[0][0], self[1][0],
|
|
|
|
self[0][1], self[1][1])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_identity(&self) -> bool {
|
2012-12-03 22:23:13 +00:00
|
|
|
// self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here!
|
|
|
|
self.fuzzy_eq(&Mat2::identity())
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_diagonal(&self) -> bool {
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0 = Number::from(0);
|
2012-12-03 22:23:13 +00:00
|
|
|
self[0][1].fuzzy_eq(&_0) &&
|
|
|
|
self[1][0].fuzzy_eq(&_0)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_rotated(&self) -> bool {
|
2012-12-03 22:23:13 +00:00
|
|
|
// !self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here!
|
|
|
|
!self.fuzzy_eq(&Mat2::identity())
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2012-12-02 22:15:40 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn is_symmetric(&self) -> bool {
|
2012-12-03 22:23:13 +00:00
|
|
|
self[0][1].fuzzy_eq(&self[1][0]) &&
|
|
|
|
self[1][0].fuzzy_eq(&self[0][1])
|
2012-12-02 22:15:40 +00:00
|
|
|
}
|
2012-11-15 02:23:39 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_invertible(&self) -> bool {
|
2012-12-13 01:10:53 +00:00
|
|
|
!self.determinant().fuzzy_eq(&Number::from(0))
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2012-12-12 01:29:35 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn to_ptr(&self) -> *T {
|
|
|
|
unsafe {
|
|
|
|
transmute::<*Mat2<T>, *T>(
|
|
|
|
to_unsafe_ptr(self)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 03:49:59 +00:00
|
|
|
pub impl<T:Copy Float Sign> Mat2<T>: MutableMatrix<T, Vec2<T>> {
|
2012-12-04 11:42:32 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn col_mut(&mut self, i: uint) -> &self/mut Vec2<T> {
|
|
|
|
match i {
|
|
|
|
0 => &mut self.x,
|
|
|
|
1 => &mut self.y,
|
|
|
|
_ => fail(fmt!("index out of bounds: expected an index from 0 to 1, but found %u", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn swap_cols(&mut self, a: uint, b: uint) {
|
|
|
|
util::swap(self.col_mut(a),
|
|
|
|
self.col_mut(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn swap_rows(&mut self, a: uint, b: uint) {
|
|
|
|
self.x.swap(a, b);
|
|
|
|
self.y.swap(a, b);
|
|
|
|
}
|
|
|
|
|
2012-12-05 03:49:59 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn set(&mut self, other: &Mat2<T>) {
|
|
|
|
(*self) = (*other);
|
|
|
|
}
|
|
|
|
|
2012-12-04 11:42:32 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn to_identity(&mut self) {
|
2012-12-05 03:49:59 +00:00
|
|
|
(*self) = Mat2::identity();
|
2012-12-04 11:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn to_zero(&mut self) {
|
2012-12-05 03:49:59 +00:00
|
|
|
(*self) = Mat2::zero();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn mul_self_t(&mut self, value: T) {
|
|
|
|
self.col_mut(0).mul_self_t(&value);
|
|
|
|
self.col_mut(1).mul_self_t(&value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn add_self_m(&mut self, other: &Mat2<T>) {
|
|
|
|
self.col_mut(0).add_self_v(&other[0]);
|
|
|
|
self.col_mut(1).add_self_v(&other[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn sub_self_m(&mut self, other: &Mat2<T>) {
|
|
|
|
self.col_mut(0).sub_self_v(&other[0]);
|
|
|
|
self.col_mut(1).sub_self_v(&other[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn invert_self(&mut self) {
|
|
|
|
match self.inverse() {
|
|
|
|
Some(m) => (*self) = m,
|
|
|
|
None => fail(~"Couldn't invert the matrix!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn transpose_self(&mut self) {
|
|
|
|
util::swap(self.col_mut(0).index_mut(1), self.col_mut(1).index_mut(0));
|
|
|
|
util::swap(self.col_mut(1).index_mut(0), self.col_mut(0).index_mut(1));
|
2012-12-04 11:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat2<T>: Matrix2<T, Vec2<T>> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn to_mat3(&self) -> Mat3<T> {
|
|
|
|
Mat3::from_Mat2(self)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn to_mat4(&self) -> Mat4<T> {
|
|
|
|
Mat4::from_Mat2(self)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-02 22:15:40 +00:00
|
|
|
pub impl<T:Copy> Mat2<T>: Index<uint, Vec2<T>> {
|
|
|
|
#[inline(always)]
|
2012-12-08 00:00:50 +00:00
|
|
|
pure fn index(&self, i: uint) -> Vec2<T> {
|
2012-12-02 22:15:40 +00:00
|
|
|
unsafe { do buf_as_slice(
|
|
|
|
transmute::<*Mat2<T>, *Vec2<T>>(
|
2012-12-08 00:00:50 +00:00
|
|
|
to_unsafe_ptr(self)), 2) |slice| { slice[i] }
|
2012-12-02 22:15:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat2<T>: Neg<Mat2<T>> {
|
2012-12-02 22:15:40 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn neg(&self) -> Mat2<T> {
|
|
|
|
Mat2::from_cols(-self[0], -self[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat2<T>: Eq {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-29 03:14:42 +00:00
|
|
|
pure fn eq(&self, other: &Mat2<T>) -> bool {
|
2012-11-29 03:49:57 +00:00
|
|
|
self[0] == other[0] &&
|
|
|
|
self[1] == other[1]
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-29 03:14:42 +00:00
|
|
|
pure fn ne(&self, other: &Mat2<T>) -> bool {
|
|
|
|
!(self == other)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat2<T>: FuzzyEq {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn fuzzy_eq(other: &Mat2<T>) -> bool {
|
|
|
|
self[0].fuzzy_eq(&other[0]) &&
|
|
|
|
self[1].fuzzy_eq(&other[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-02 22:15:40 +00:00
|
|
|
/**
|
|
|
|
* A 3 x 3 column major matrix
|
2012-12-05 01:38:30 +00:00
|
|
|
*
|
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.
|
|
|
|
*
|
2012-12-05 01:38:30 +00:00
|
|
|
* # Fields
|
|
|
|
*
|
|
|
|
* * `x` - the first column vector of the matrix
|
|
|
|
* * `y` - the second column vector of the matrix
|
|
|
|
* * `z` - the third column vector of the matrix
|
2012-12-02 22:15:40 +00:00
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
pub struct Mat3<T> { x: Vec3<T>, y: Vec3<T>, z: Vec3<T> }
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat3<T> {
|
2012-12-04 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Construct a 3 x 3 matrix
|
2012-12-05 01:38:30 +00:00
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * `c0r0`, `c0r1`, `c0r2` - the first column of the matrix
|
|
|
|
* * `c1r0`, `c1r1`, `c1r2` - the second column of the matrix
|
|
|
|
* * `c2r0`, `c2r1`, `c2r2` - the third column of the matrix
|
|
|
|
*
|
2012-12-04 00:58:19 +00:00
|
|
|
* ~~~
|
2012-12-04 04:34:09 +00:00
|
|
|
* c0 c1 c2
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r0 | c0r0 | c1r0 | c2r0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r1 | c0r1 | c1r1 | c2r1 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r2 | c0r2 | c1r2 | c2r2 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+
|
|
|
|
* ~~~
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-20 12:27:17 +00:00
|
|
|
static pure fn new(c0r0:T, c0r1:T, c0r2:T,
|
2012-11-15 02:23:39 +00:00
|
|
|
c1r0:T, c1r1:T, c1r2:T,
|
|
|
|
c2r0:T, c2r1:T, c2r2:T) -> Mat3<T> {
|
|
|
|
Mat3::from_cols(Vec3::new(move c0r0, move c0r1, move c0r2),
|
|
|
|
Vec3::new(move c1r0, move c1r1, move c1r2),
|
|
|
|
Vec3::new(move c2r0, move c2r1, move c2r2))
|
|
|
|
}
|
|
|
|
|
2012-12-04 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Construct a 3 x 3 matrix from column vectors
|
2012-12-05 01:38:30 +00:00
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * `c0` - the first column vector of the matrix
|
|
|
|
* * `c1` - the second column vector of the matrix
|
|
|
|
* * `c2` - the third column vector of the matrix
|
|
|
|
*
|
2012-12-04 00:58:19 +00:00
|
|
|
* ~~~
|
2012-12-04 04:34:09 +00:00
|
|
|
* c0 c1 c2
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r0 | c0.x | c1.y | c2.z |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r1 | c0.x | c1.y | c2.z |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r2 | c0.x | c1.y | c2.z |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+
|
|
|
|
* ~~~
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-20 12:27:17 +00:00
|
|
|
static pure fn from_cols(c0: Vec3<T>, c1: Vec3<T>, c2: Vec3<T>) -> Mat3<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat3 { x: move c0,
|
|
|
|
y: move c1,
|
|
|
|
z: move c2 }
|
|
|
|
}
|
|
|
|
|
2012-12-04 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Construct a 3 x 3 diagonal matrix with the major diagonal set to `value`
|
2012-12-05 01:38:30 +00:00
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * `value` - the value to set the major diagonal to
|
|
|
|
*
|
2012-12-04 00:58:19 +00:00
|
|
|
* ~~~
|
2012-12-04 04:34:09 +00:00
|
|
|
* c0 c1 c2
|
2012-12-04 00:58:19 +00:00
|
|
|
* +-----+-----+-----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r0 | val | 0 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +-----+-----+-----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r1 | 0 | val | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +-----+-----+-----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r2 | 0 | 0 | val |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +-----+-----+-----+
|
|
|
|
* ~~~
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-20 12:27:17 +00:00
|
|
|
static pure fn from_value(value: T) -> Mat3<T> {
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0 = Number::from(0);
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat3::new(value, _0, _0,
|
|
|
|
_0, value, _0,
|
|
|
|
_0, _0, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-20 12:27:17 +00:00
|
|
|
static pure fn from_Mat2(m: &Mat2<T>) -> Mat3<T> {
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0 = Number::from(0);
|
|
|
|
let _1 = Number::from(1);
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat3::new(m[0][0], m[0][1], _0,
|
|
|
|
m[1][0], m[1][1], _0,
|
|
|
|
_0, _0, _1)
|
|
|
|
}
|
2012-12-01 12:49:24 +00:00
|
|
|
|
|
|
|
// FIXME: An interim solution to the issues with static functions
|
|
|
|
#[inline(always)]
|
|
|
|
static pure fn identity() -> Mat3<T> {
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0 = Number::from(0);
|
|
|
|
let _1 = Number::from(1);
|
2012-12-01 12:49:24 +00:00
|
|
|
Mat3::new(_1, _0, _0,
|
|
|
|
_0, _1, _0,
|
|
|
|
_0, _0, _1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: An interim solution to the issues with static functions
|
|
|
|
#[inline(always)]
|
|
|
|
static pure fn zero() -> Mat3<T> {
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0 = Number::from(0);
|
2012-12-01 12:49:24 +00:00
|
|
|
Mat3::new(_0, _0, _0,
|
|
|
|
_0, _0, _0,
|
|
|
|
_0, _0, _0)
|
|
|
|
}
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat3<T>: Matrix<T, Vec3<T>> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn col(&self, i: uint) -> Vec3<T> { self[i] }
|
2012-11-15 02:23:39 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn row(&self, i: uint) -> Vec3<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec3::new(self[0][i],
|
|
|
|
self[1][i],
|
|
|
|
self[2][i])
|
|
|
|
}
|
2012-12-01 04:19:21 +00:00
|
|
|
|
2012-12-04 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Returns the multiplicative identity matrix
|
|
|
|
* ~~~
|
2012-12-04 04:34:09 +00:00
|
|
|
* c0 c1 c2
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r0 | 1 | 0 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r1 | 0 | 1 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r2 | 0 | 0 | 1 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+
|
|
|
|
* ~~~
|
|
|
|
*/
|
2012-12-01 04:19:21 +00:00
|
|
|
#[inline(always)]
|
2012-12-02 22:15:40 +00:00
|
|
|
static pure fn identity() -> Mat3<T> {
|
2012-12-04 04:41:21 +00:00
|
|
|
let _0 = Number::from(0);
|
|
|
|
let _1 = Number::from(1);
|
2012-12-02 22:15:40 +00:00
|
|
|
Mat3::new(_1, _0, _0,
|
|
|
|
_0, _1, _0,
|
|
|
|
_0, _0, _1)
|
2012-11-21 08:35:26 +00:00
|
|
|
}
|
2012-12-02 22:15:40 +00:00
|
|
|
|
2012-12-04 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Returns the additive identity matrix
|
|
|
|
* ~~~
|
2012-12-04 04:34:09 +00:00
|
|
|
* c0 c1 c2
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r0 | 0 | 0 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r1 | 0 | 0 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r2 | 0 | 0 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+
|
|
|
|
* ~~~
|
|
|
|
*/
|
2012-11-20 06:57:32 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn zero() -> Mat3<T> {
|
2012-12-04 04:41:21 +00:00
|
|
|
let _0 = Number::from(0);
|
2012-12-01 12:49:24 +00:00
|
|
|
Mat3::new(_0, _0, _0,
|
|
|
|
_0, _0, _0,
|
|
|
|
_0, _0, _0)
|
2012-11-20 06:57:32 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn mul_t(&self, value: T) -> Mat3<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat3::from_cols(self[0].mul_t(value),
|
|
|
|
self[1].mul_t(value),
|
|
|
|
self[2].mul_t(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-12-03 22:06:00 +00:00
|
|
|
pure fn mul_v(&self, vec: &Vec3<T>) -> Vec3<T> {
|
|
|
|
Vec3::new(self.row(0).dot(vec),
|
|
|
|
self.row(1).dot(vec),
|
|
|
|
self.row(2).dot(vec))
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn add_m(&self, other: &Mat3<T>) -> Mat3<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat3::from_cols(self[0].add_v(&other[0]),
|
|
|
|
self[1].add_v(&other[1]),
|
|
|
|
self[2].add_v(&other[2]))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn sub_m(&self, other: &Mat3<T>) -> Mat3<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat3::from_cols(self[0].sub_v(&other[0]),
|
|
|
|
self[1].sub_v(&other[1]),
|
|
|
|
self[2].sub_v(&other[2]))
|
|
|
|
}
|
2012-11-20 06:57:32 +00:00
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn mul_m(&self, other: &Mat3<T>) -> Mat3<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat3::new(self.row(0).dot(&other.col(0)), self.row(1).dot(&other.col(0)), self.row(2).dot(&other.col(0)),
|
|
|
|
self.row(0).dot(&other.col(1)), self.row(1).dot(&other.col(1)), self.row(2).dot(&other.col(1)),
|
|
|
|
self.row(0).dot(&other.col(2)), self.row(1).dot(&other.col(2)), self.row(2).dot(&other.col(2)))
|
|
|
|
}
|
|
|
|
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn dot(&self, other: &Mat3<T>) -> T {
|
|
|
|
other.transpose().mul_m(self).trace()
|
2012-11-21 23:24:43 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 01:08:36 +00:00
|
|
|
pure fn determinant(&self) -> T {
|
2012-11-15 02:23:39 +00:00
|
|
|
self.col(0).dot(&self.col(1).cross(&self.col(2)))
|
|
|
|
}
|
|
|
|
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn trace(&self) -> T {
|
2012-11-21 21:27:44 +00:00
|
|
|
self[0][0] + self[1][1] + self[2][2]
|
|
|
|
}
|
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
// #[inline(always)]
|
2012-12-03 22:12:22 +00:00
|
|
|
pure fn inverse(&self) -> Option<Mat3<T>> {
|
2012-12-03 01:08:36 +00:00
|
|
|
let d = self.determinant();
|
2012-12-13 01:10:53 +00:00
|
|
|
if d.fuzzy_eq(&Number::from(0)) {
|
2012-11-15 02:23:39 +00:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Mat3::from_cols(self[1].cross(&self[2]).div_t(d),
|
|
|
|
self[2].cross(&self[0]).div_t(d),
|
2012-12-13 01:10:53 +00:00
|
|
|
self[0].cross(&self[1]).div_t(d)).transpose())
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn transpose(&self) -> Mat3<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat3::new(self[0][0], self[1][0], self[2][0],
|
|
|
|
self[0][1], self[1][1], self[2][1],
|
|
|
|
self[0][2], self[1][2], self[2][2])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_identity(&self) -> bool {
|
2012-12-03 22:23:13 +00:00
|
|
|
// self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here!
|
|
|
|
self.fuzzy_eq(&Mat3::identity())
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_diagonal(&self) -> bool {
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0 = Number::from(0);
|
2012-12-03 22:23:13 +00:00
|
|
|
self[0][1].fuzzy_eq(&_0) &&
|
|
|
|
self[0][2].fuzzy_eq(&_0) &&
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-03 22:23:13 +00:00
|
|
|
self[1][0].fuzzy_eq(&_0) &&
|
|
|
|
self[1][2].fuzzy_eq(&_0) &&
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-03 22:23:13 +00:00
|
|
|
self[2][0].fuzzy_eq(&_0) &&
|
|
|
|
self[2][1].fuzzy_eq(&_0)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_rotated(&self) -> bool {
|
2012-12-03 22:23:13 +00:00
|
|
|
// !self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here!
|
|
|
|
!self.fuzzy_eq(&Mat3::identity())
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2012-12-02 22:15:40 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn is_symmetric(&self) -> bool {
|
2012-12-03 22:23:13 +00:00
|
|
|
self[0][1].fuzzy_eq(&self[1][0]) &&
|
|
|
|
self[0][2].fuzzy_eq(&self[2][0]) &&
|
2012-12-02 22:15:40 +00:00
|
|
|
|
2012-12-03 22:23:13 +00:00
|
|
|
self[1][0].fuzzy_eq(&self[0][1]) &&
|
|
|
|
self[1][2].fuzzy_eq(&self[2][1]) &&
|
2012-12-02 22:15:40 +00:00
|
|
|
|
2012-12-03 22:23:13 +00:00
|
|
|
self[2][0].fuzzy_eq(&self[0][2]) &&
|
|
|
|
self[2][1].fuzzy_eq(&self[1][2])
|
2012-12-02 22:15:40 +00:00
|
|
|
}
|
2012-11-15 02:23:39 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_invertible(&self) -> bool {
|
2012-12-13 01:10:53 +00:00
|
|
|
!self.determinant().fuzzy_eq(&Number::zero())
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2012-12-12 01:29:35 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn to_ptr(&self) -> *T {
|
|
|
|
unsafe {
|
|
|
|
transmute::<*Mat3<T>, *T>(
|
|
|
|
to_unsafe_ptr(self)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 03:49:59 +00:00
|
|
|
pub impl<T:Copy Float Sign> Mat3<T>: MutableMatrix<T, Vec3<T>> {
|
2012-12-04 11:42:32 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn col_mut(&mut self, i: uint) -> &self/mut Vec3<T> {
|
|
|
|
match i {
|
|
|
|
0 => &mut self.x,
|
|
|
|
1 => &mut self.y,
|
|
|
|
2 => &mut self.z,
|
|
|
|
_ => fail(fmt!("index out of bounds: expected an index from 0 to 2, but found %u", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn swap_cols(&mut self, a: uint, b: uint) {
|
|
|
|
util::swap(self.col_mut(a),
|
|
|
|
self.col_mut(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn swap_rows(&mut self, a: uint, b: uint) {
|
|
|
|
self.x.swap(a, b);
|
|
|
|
self.y.swap(a, b);
|
|
|
|
self.z.swap(a, b);
|
|
|
|
}
|
|
|
|
|
2012-12-05 03:49:59 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn set(&mut self, other: &Mat3<T>) {
|
|
|
|
(*self) = (*other);
|
|
|
|
}
|
|
|
|
|
2012-12-04 11:42:32 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn to_identity(&mut self) {
|
2012-12-05 03:49:59 +00:00
|
|
|
(*self) = Mat3::identity();
|
2012-12-04 11:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn to_zero(&mut self) {
|
2012-12-05 03:49:59 +00:00
|
|
|
(*self) = Mat3::zero();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn mul_self_t(&mut self, value: T) {
|
|
|
|
self.col_mut(0).mul_self_t(&value);
|
|
|
|
self.col_mut(1).mul_self_t(&value);
|
|
|
|
self.col_mut(2).mul_self_t(&value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn add_self_m(&mut self, other: &Mat3<T>) {
|
|
|
|
self.col_mut(0).add_self_v(&other[0]);
|
|
|
|
self.col_mut(1).add_self_v(&other[1]);
|
|
|
|
self.col_mut(2).add_self_v(&other[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn sub_self_m(&mut self, other: &Mat3<T>) {
|
|
|
|
self.col_mut(0).sub_self_v(&other[0]);
|
|
|
|
self.col_mut(1).sub_self_v(&other[1]);
|
|
|
|
self.col_mut(2).sub_self_v(&other[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn invert_self(&mut self) {
|
|
|
|
match self.inverse() {
|
|
|
|
Some(m) => (*self) = m,
|
|
|
|
None => fail(~"Couldn't invert the matrix!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn transpose_self(&mut self) {
|
|
|
|
util::swap(self.col_mut(0).index_mut(1), self.col_mut(1).index_mut(0));
|
|
|
|
util::swap(self.col_mut(0).index_mut(2), self.col_mut(2).index_mut(0));
|
|
|
|
|
|
|
|
util::swap(self.col_mut(1).index_mut(0), self.col_mut(0).index_mut(1));
|
|
|
|
util::swap(self.col_mut(1).index_mut(2), self.col_mut(2).index_mut(1));
|
|
|
|
|
|
|
|
util::swap(self.col_mut(2).index_mut(0), self.col_mut(0).index_mut(2));
|
|
|
|
util::swap(self.col_mut(2).index_mut(1), self.col_mut(1).index_mut(2));
|
2012-12-04 11:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat3<T>: Matrix3<T, Vec3<T>> {
|
2012-12-08 02:59:10 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn from_axis_angle<A:Angle<T>>(axis: &Vec3<T>, theta: A) -> Mat3<T> {
|
|
|
|
let c: T = cos(&theta.to_radians());
|
|
|
|
let s: T = sin(&theta.to_radians());
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0: T = Number::from(0);
|
|
|
|
let _1: T = Number::from(1);
|
2012-12-08 02:59:10 +00:00
|
|
|
let _1_c: T = _1 - c;
|
|
|
|
|
|
|
|
let x = axis.x;
|
|
|
|
let y = axis.y;
|
|
|
|
let z = axis.z;
|
|
|
|
|
|
|
|
Mat3::new(_1_c * x * x + c, _1_c * x * y + s * z, _1_c * x * z - s * y,
|
|
|
|
_1_c * x * y - s * z, _1_c * y * y + c, _1_c * y * z + s * x,
|
|
|
|
_1_c * x * z + s * y, _1_c * y * z - s * x, _1_c * z * z + c)
|
|
|
|
}
|
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn to_mat4(&self) -> Mat4<T> {
|
|
|
|
Mat4::from_Mat3(self)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 06:19:53 +00:00
|
|
|
pub impl<T:Copy Float Exp> Mat3<T>: ToQuat<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
pure fn to_Quat() -> Quat<T> {
|
|
|
|
// Implemented using a mix of ideas from jMonkeyEngine and Ken Shoemake's
|
|
|
|
// paper on Quaternions: http://www.cs.ucr.edu/~vbz/resources/Quatut.pdf
|
|
|
|
|
2012-12-03 06:19:53 +00:00
|
|
|
let mut s;
|
|
|
|
let w, x, y, z;
|
|
|
|
let trace = self.trace();
|
|
|
|
|
|
|
|
let _1: T = Number::from(1.0);
|
|
|
|
let half: T = Number::from(0.5);
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-04 04:41:21 +00:00
|
|
|
if trace >= Number::from(0) {
|
2012-12-03 06:19:53 +00:00
|
|
|
s = (_1 + trace).sqrt();
|
|
|
|
w = half * s;
|
|
|
|
s = half / s;
|
|
|
|
x = (self[1][2] - self[2][1]) * s;
|
|
|
|
y = (self[2][0] - self[0][2]) * s;
|
|
|
|
z = (self[0][1] - self[1][0]) * s;
|
2012-11-15 02:23:39 +00:00
|
|
|
} else if (self[0][0] > self[1][1]) && (self[0][0] > self[2][2]) {
|
2012-12-03 06:19:53 +00:00
|
|
|
s = (half + (self[0][0] - self[1][1] - self[2][2])).sqrt();
|
|
|
|
w = half * s;
|
|
|
|
s = half / s;
|
|
|
|
x = (self[0][1] - self[1][0]) * s;
|
|
|
|
y = (self[2][0] - self[0][2]) * s;
|
|
|
|
z = (self[1][2] - self[2][1]) * s;
|
2012-11-15 02:23:39 +00:00
|
|
|
} else if self[1][1] > self[2][2] {
|
2012-12-03 06:19:53 +00:00
|
|
|
s = (half + (self[1][1] - self[0][0] - self[2][2])).sqrt();
|
|
|
|
w = half * s;
|
|
|
|
s = half / s;
|
|
|
|
x = (self[0][1] - self[1][0]) * s;
|
|
|
|
y = (self[1][2] - self[2][1]) * s;
|
|
|
|
z = (self[2][0] - self[0][2]) * s;
|
2012-11-15 02:23:39 +00:00
|
|
|
} else {
|
2012-12-03 06:19:53 +00:00
|
|
|
s = (half + (self[2][2] - self[0][0] - self[1][1])).sqrt();
|
|
|
|
w = half * s;
|
|
|
|
s = half / s;
|
|
|
|
x = (self[2][0] - self[0][2]) * s;
|
|
|
|
y = (self[1][2] - self[2][1]) * s;
|
|
|
|
z = (self[0][1] - self[1][0]) * s;
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 06:19:53 +00:00
|
|
|
Quat::new(w, x, y, z)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-02 22:15:40 +00:00
|
|
|
pub impl<T:Copy> Mat3<T>: Index<uint, Vec3<T>> {
|
|
|
|
#[inline(always)]
|
2012-12-08 00:00:50 +00:00
|
|
|
pure fn index(&self, i: uint) -> Vec3<T> {
|
2012-12-02 22:15:40 +00:00
|
|
|
unsafe { do buf_as_slice(
|
|
|
|
transmute::<*Mat3<T>, *Vec3<T>>(
|
2012-12-08 00:00:50 +00:00
|
|
|
to_unsafe_ptr(self)), 3) |slice| { slice[i] }
|
2012-12-02 22:15:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat3<T>: Neg<Mat3<T>> {
|
2012-12-02 22:15:40 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn neg(&self) -> Mat3<T> {
|
|
|
|
Mat3::from_cols(-self[0], -self[1], -self[2])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat3<T>: Eq {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-29 03:14:42 +00:00
|
|
|
pure fn eq(&self, other: &Mat3<T>) -> bool {
|
2012-11-29 03:49:57 +00:00
|
|
|
self[0] == other[0] &&
|
|
|
|
self[1] == other[1] &&
|
|
|
|
self[2] == other[2]
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-29 03:14:42 +00:00
|
|
|
pure fn ne(&self, other: &Mat3<T>) -> bool {
|
|
|
|
!(self == other)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat3<T>: FuzzyEq {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn fuzzy_eq(other: &Mat3<T>) -> bool {
|
|
|
|
self[0].fuzzy_eq(&other[0]) &&
|
|
|
|
self[1].fuzzy_eq(&other[1]) &&
|
|
|
|
self[2].fuzzy_eq(&other[2])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-02 22:15:40 +00:00
|
|
|
/**
|
|
|
|
* A 4 x 4 column major matrix
|
2012-12-05 01:38:30 +00:00
|
|
|
*
|
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.
|
|
|
|
*
|
2012-12-05 01:38:30 +00:00
|
|
|
* # Fields
|
|
|
|
*
|
|
|
|
* * `x` - the first column vector of the matrix
|
|
|
|
* * `y` - the second column vector of the matrix
|
|
|
|
* * `z` - the third column vector of the matrix
|
|
|
|
* * `w` - the fourth column vector of the matrix
|
2012-12-02 22:15:40 +00:00
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
pub struct Mat4<T> { x: Vec4<T>, y: Vec4<T>, z: Vec4<T>, w: Vec4<T> }
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat4<T> {
|
2012-12-04 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Construct a 4 x 4 matrix
|
2012-12-05 01:38:30 +00:00
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * `c0r0`, `c0r1`, `c0r2`, `c0r3` - the first column of the matrix
|
|
|
|
* * `c1r0`, `c1r1`, `c1r2`, `c1r3` - the second column of the matrix
|
|
|
|
* * `c2r0`, `c2r1`, `c2r2`, `c2r3` - the third column of the matrix
|
|
|
|
* * `c3r0`, `c3r1`, `c3r2`, `c3r3` - the fourth column of the matrix
|
|
|
|
*
|
2012-12-04 00:58:19 +00:00
|
|
|
* ~~~
|
2012-12-04 04:34:09 +00:00
|
|
|
* c0 c1 c2 c3
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r0 | c0r0 | c1r0 | c2r0 | c3r0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r1 | c0r1 | c1r1 | c2r1 | c3r1 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r2 | c0r2 | c1r2 | c2r2 | c3r2 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r3 | c0r3 | c1r3 | c2r3 | c3r3 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+------+
|
|
|
|
* ~~~
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-20 12:27:17 +00:00
|
|
|
static pure fn new(c0r0: T, c0r1: T, c0r2: T, c0r3: T,
|
2012-11-15 02:23:39 +00:00
|
|
|
c1r0: T, c1r1: T, c1r2: T, c1r3: T,
|
|
|
|
c2r0: T, c2r1: T, c2r2: T, c2r3: T,
|
|
|
|
c3r0: T, c3r1: T, c3r2: T, c3r3: T) -> Mat4<T> {
|
|
|
|
Mat4::from_cols(Vec4::new(move c0r0, move c0r1, move c0r2, move c0r3),
|
|
|
|
Vec4::new(move c1r0, move c1r1, move c1r2, move c1r3),
|
|
|
|
Vec4::new(move c2r0, move c2r1, move c2r2, move c2r3),
|
|
|
|
Vec4::new(move c3r0, move c3r1, move c3r2, move c3r3))
|
|
|
|
}
|
|
|
|
|
2012-12-04 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Construct a 4 x 4 matrix from column vectors
|
2012-12-05 01:38:30 +00:00
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * `c0` - the first column vector of the matrix
|
|
|
|
* * `c1` - the second column vector of the matrix
|
|
|
|
* * `c2` - the third column vector of the matrix
|
|
|
|
* * `c3` - the fourth column vector of the matrix
|
|
|
|
*
|
2012-12-04 00:58:19 +00:00
|
|
|
* ~~~
|
2012-12-04 04:34:09 +00:00
|
|
|
* c0 c1 c2 c3
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r0 | c0.x | c1.x | c2.x | c3.x |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r1 | c0.y | c1.y | c2.y | c3.y |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r2 | c0.z | c1.z | c2.z | c3.z |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+------+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r3 | c0.w | c1.w | c2.w | c3.w |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +------+------+------+------+
|
|
|
|
* ~~~
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-20 12:27:17 +00:00
|
|
|
static pure fn from_cols(c0: Vec4<T>, c1: Vec4<T>, c2: Vec4<T>, c3: Vec4<T>) -> Mat4<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat4 { x: move c0,
|
|
|
|
y: move c1,
|
|
|
|
z: move c2,
|
|
|
|
w: move c3 }
|
|
|
|
}
|
|
|
|
|
2012-12-04 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Construct a 4 x 4 diagonal matrix with the major diagonal set to `value`
|
2012-12-05 01:38:30 +00:00
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * `value` - the value to set the major diagonal to
|
|
|
|
*
|
2012-12-04 00:58:19 +00:00
|
|
|
* ~~~
|
2012-12-04 04:34:09 +00:00
|
|
|
* c0 c1 c2 c3
|
2012-12-04 00:58:19 +00:00
|
|
|
* +-----+-----+-----+-----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r0 | val | 0 | 0 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +-----+-----+-----+-----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r1 | 0 | val | 0 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +-----+-----+-----+-----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r2 | 0 | 0 | val | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +-----+-----+-----+-----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r3 | 0 | 0 | 0 | val |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +-----+-----+-----+-----+
|
|
|
|
* ~~~
|
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-20 12:27:17 +00:00
|
|
|
static pure fn from_value(value: T) -> Mat4<T> {
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0 = Number::from(0);
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat4::new(value, _0, _0, _0,
|
|
|
|
_0, value, _0, _0,
|
|
|
|
_0, _0, value, _0,
|
|
|
|
_0, _0, _0, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-20 12:27:17 +00:00
|
|
|
static pure fn from_Mat2(m: &Mat2<T>) -> Mat4<T> {
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0 = Number::from(0);
|
|
|
|
let _1 = Number::from(1);
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat4::new(m[0][0], m[0][1], _0, _0,
|
|
|
|
m[1][0], m[1][1], _0, _0,
|
|
|
|
_0, _0, _1, _0,
|
|
|
|
_0, _0, _0, _1)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-20 12:27:17 +00:00
|
|
|
static pure fn from_Mat3(m: &Mat3<T>) -> Mat4<T> {
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0 = Number::from(0);
|
|
|
|
let _1 = Number::from(1);
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat4::new(m[0][0], m[0][1], m[0][2], _0,
|
|
|
|
m[1][0], m[1][1], m[1][2], _0,
|
|
|
|
m[2][0], m[2][1], m[2][2], _0,
|
|
|
|
_0, _0, _0, _1)
|
|
|
|
}
|
2012-12-01 12:49:24 +00:00
|
|
|
|
|
|
|
// FIXME: An interim solution to the issues with static functions
|
|
|
|
#[inline(always)]
|
|
|
|
static pure fn identity() -> Mat4<T> {
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0 = Number::from(0);
|
|
|
|
let _1 = Number::from(1);
|
2012-12-01 12:49:24 +00:00
|
|
|
Mat4::new(_1, _0, _0, _0,
|
|
|
|
_0, _1, _0, _0,
|
|
|
|
_0, _0, _1, _0,
|
|
|
|
_0, _0, _0, _1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: An interim solution to the issues with static functions
|
|
|
|
#[inline(always)]
|
|
|
|
static pure fn zero() -> Mat4<T> {
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0 = Number::from(0);
|
2012-12-01 12:49:24 +00:00
|
|
|
Mat4::new(_0, _0, _0, _0,
|
|
|
|
_0, _0, _0, _0,
|
|
|
|
_0, _0, _0, _0,
|
|
|
|
_0, _0, _0, _0)
|
|
|
|
}
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float Sign> Mat4<T>: Matrix<T, Vec4<T>> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn col(&self, i: uint) -> Vec4<T> { self[i] }
|
2012-11-15 02:23:39 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn row(&self, i: uint) -> Vec4<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec4::new(self[0][i],
|
|
|
|
self[1][i],
|
|
|
|
self[2][i],
|
|
|
|
self[3][i])
|
|
|
|
}
|
2012-12-01 04:19:21 +00:00
|
|
|
|
2012-12-04 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Returns the multiplicative identity matrix
|
|
|
|
* ~~~
|
2012-12-04 04:34:09 +00:00
|
|
|
* c0 c1 c2 c3
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r0 | 1 | 0 | 0 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r1 | 0 | 1 | 0 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r2 | 0 | 0 | 1 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r3 | 0 | 0 | 0 | 1 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+----+
|
|
|
|
* ~~~
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
|
|
|
static pure fn identity() -> Mat4<T> {
|
2012-12-04 04:41:21 +00:00
|
|
|
let _0 = Number::from(0);
|
|
|
|
let _1 = Number::from(1);
|
2012-12-04 00:58:19 +00:00
|
|
|
Mat4::new(_1, _0, _0, _0,
|
|
|
|
_0, _1, _0, _0,
|
|
|
|
_0, _0, _1, _0,
|
|
|
|
_0, _0, _0, _1)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the additive identity matrix
|
|
|
|
* ~~~
|
2012-12-04 04:34:09 +00:00
|
|
|
* c0 c1 c2 c3
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r0 | 0 | 0 | 0 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r1 | 0 | 0 | 0 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r2 | 0 | 0 | 0 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+----+
|
2012-12-04 04:34:09 +00:00
|
|
|
* r3 | 0 | 0 | 0 | 0 |
|
2012-12-04 00:58:19 +00:00
|
|
|
* +----+----+----+----+
|
|
|
|
* ~~~
|
|
|
|
*/
|
2012-11-20 06:57:32 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn zero() -> Mat4<T> {
|
2012-12-04 04:41:21 +00:00
|
|
|
let _0 = Number::from(0);
|
2012-12-01 12:49:24 +00:00
|
|
|
Mat4::new(_0, _0, _0, _0,
|
|
|
|
_0, _0, _0, _0,
|
|
|
|
_0, _0, _0, _0,
|
|
|
|
_0, _0, _0, _0)
|
2012-11-20 06:57:32 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn mul_t(&self, value: T) -> Mat4<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat4::from_cols(self[0].mul_t(value),
|
|
|
|
self[1].mul_t(value),
|
|
|
|
self[2].mul_t(value),
|
|
|
|
self[3].mul_t(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-12-03 22:06:00 +00:00
|
|
|
pure fn mul_v(&self, vec: &Vec4<T>) -> Vec4<T> {
|
|
|
|
Vec4::new(self.row(0).dot(vec),
|
|
|
|
self.row(1).dot(vec),
|
|
|
|
self.row(2).dot(vec),
|
|
|
|
self.row(3).dot(vec))
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn add_m(&self, other: &Mat4<T>) -> Mat4<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat4::from_cols(self[0].add_v(&other[0]),
|
|
|
|
self[1].add_v(&other[1]),
|
|
|
|
self[2].add_v(&other[2]),
|
|
|
|
self[3].add_v(&other[3]))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn sub_m(&self, other: &Mat4<T>) -> Mat4<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat4::from_cols(self[0].sub_v(&other[0]),
|
|
|
|
self[1].sub_v(&other[1]),
|
|
|
|
self[2].sub_v(&other[2]),
|
|
|
|
self[3].sub_v(&other[3]))
|
|
|
|
}
|
2012-12-02 22:15:40 +00:00
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn mul_m(&self, other: &Mat4<T>) -> Mat4<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
// Surprisingly when building with optimisation turned on this is actually
|
|
|
|
// faster than writing out the matrix multiplication in expanded form.
|
|
|
|
// If you don't believe me, see ./test/performance/matrix_mul.rs
|
|
|
|
Mat4::new(self.row(0).dot(&other.col(0)), self.row(1).dot(&other.col(0)), self.row(2).dot(&other.col(0)), self.row(3).dot(&other.col(0)),
|
|
|
|
self.row(0).dot(&other.col(1)), self.row(1).dot(&other.col(1)), self.row(2).dot(&other.col(1)), self.row(3).dot(&other.col(1)),
|
|
|
|
self.row(0).dot(&other.col(2)), self.row(1).dot(&other.col(2)), self.row(2).dot(&other.col(2)), self.row(3).dot(&other.col(2)),
|
|
|
|
self.row(0).dot(&other.col(3)), self.row(1).dot(&other.col(3)), self.row(2).dot(&other.col(3)), self.row(3).dot(&other.col(3)))
|
|
|
|
}
|
|
|
|
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn dot(&self, other: &Mat4<T>) -> T {
|
|
|
|
other.transpose().mul_m(self).trace()
|
2012-11-21 23:24:43 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 01:08:36 +00:00
|
|
|
pure fn determinant(&self) -> T {
|
2012-11-15 02:23:39 +00:00
|
|
|
self[0][0]*Mat3::new(self[1][1], self[2][1], self[3][1],
|
|
|
|
self[1][2], self[2][2], self[3][2],
|
2012-12-03 01:08:36 +00:00
|
|
|
self[1][3], self[2][3], self[3][3]).determinant() -
|
2012-11-15 02:23:39 +00:00
|
|
|
self[1][0]*Mat3::new(self[0][1], self[2][1], self[3][1],
|
|
|
|
self[0][2], self[2][2], self[3][2],
|
2012-12-03 01:08:36 +00:00
|
|
|
self[0][3], self[2][3], self[3][3]).determinant() +
|
2012-11-15 02:23:39 +00:00
|
|
|
self[2][0]*Mat3::new(self[0][1], self[1][1], self[3][1],
|
|
|
|
self[0][2], self[1][2], self[3][2],
|
2012-12-03 01:08:36 +00:00
|
|
|
self[0][3], self[1][3], self[3][3]).determinant() -
|
2012-11-15 02:23:39 +00:00
|
|
|
self[3][0]*Mat3::new(self[0][1], self[1][1], self[2][1],
|
|
|
|
self[0][2], self[1][2], self[2][2],
|
2012-12-03 01:08:36 +00:00
|
|
|
self[0][3], self[1][3], self[2][3]).determinant()
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn trace(&self) -> T {
|
2012-11-21 21:27:44 +00:00
|
|
|
self[0][0] + self[1][1] + self[2][2] + self[3][3]
|
|
|
|
}
|
|
|
|
|
2012-12-10 00:48:02 +00:00
|
|
|
pure fn inverse(&self) -> Option<Mat4<T>> {
|
2012-12-03 01:08:36 +00:00
|
|
|
let d = self.determinant();
|
2012-12-13 01:10:53 +00:00
|
|
|
if d.fuzzy_eq(&Number::from(0)) {
|
2012-11-15 02:23:39 +00:00
|
|
|
None
|
|
|
|
} else {
|
2012-12-10 01:47:36 +00:00
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
// Gauss Jordan Elimination with partial pivoting
|
2012-12-08 00:09:03 +00:00
|
|
|
// So take this matrix, A, augmented with the identity
|
|
|
|
// and essentially reduce [A|I]
|
2012-12-05 01:38:30 +00:00
|
|
|
|
2012-12-08 00:09:03 +00:00
|
|
|
let mut A = *self;
|
|
|
|
// let mut I: Mat4<T> = Matrix::identity(); // FIXME: there's something wrong with static functions here!
|
|
|
|
let mut I = Mat4::identity();
|
2012-12-10 01:47:36 +00:00
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
for uint::range(0, 4) |j| {
|
2012-12-08 00:09:03 +00:00
|
|
|
// Find largest element in col j
|
2012-11-15 02:23:39 +00:00
|
|
|
let mut i1 = j;
|
|
|
|
for uint::range(j + 1, 4) |i| {
|
2012-12-08 00:09:03 +00:00
|
|
|
if abs(&A[j][i]) > abs(&A[j][i1]) {
|
2012-11-15 02:23:39 +00:00
|
|
|
i1 = i;
|
|
|
|
}
|
|
|
|
}
|
2012-12-10 00:48:02 +00:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
// Swap columns i1 and j in A and I to
|
|
|
|
// put pivot on diagonal
|
|
|
|
A.swap_cols(i1, j);
|
|
|
|
I.swap_cols(i1, j);
|
2012-12-10 01:47:36 +00:00
|
|
|
|
2012-12-10 00:48:02 +00:00
|
|
|
// Scale col j to have a unit diagonal
|
|
|
|
I.col_mut(j).div_self_t(&A[j][j]);
|
|
|
|
A.col_mut(j).div_self_t(&A[j][j]);
|
2012-12-10 01:47:36 +00:00
|
|
|
|
2012-12-10 00:48:02 +00:00
|
|
|
// Eliminate off-diagonal elems in col j of A,
|
|
|
|
// doing identical ops to I
|
|
|
|
for uint::range(0, 4) |i| {
|
|
|
|
if i != j {
|
2012-12-10 01:47:36 +00:00
|
|
|
I.col_mut(i).sub_self_v(&I[j].mul_t(A[i][j]));
|
|
|
|
A.col_mut(i).sub_self_v(&A[j].mul_t(A[i][j]));
|
2012-12-10 00:48:02 +00:00
|
|
|
}
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-08 00:09:03 +00:00
|
|
|
Some(I)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn transpose(&self) -> Mat4<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Mat4::new(self[0][0], self[1][0], self[2][0], self[3][0],
|
|
|
|
self[0][1], self[1][1], self[2][1], self[3][1],
|
|
|
|
self[0][2], self[1][2], self[2][2], self[3][2],
|
|
|
|
self[0][3], self[1][3], self[2][3], self[3][3])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_identity(&self) -> bool {
|
2012-12-03 22:23:13 +00:00
|
|
|
// self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here!
|
|
|
|
self.fuzzy_eq(&Mat4::identity())
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_diagonal(&self) -> bool {
|
2012-12-13 01:10:53 +00:00
|
|
|
let _0 = Number::from(0);
|
2012-12-03 22:23:13 +00:00
|
|
|
self[0][1].fuzzy_eq(&_0) &&
|
|
|
|
self[0][2].fuzzy_eq(&_0) &&
|
|
|
|
self[0][3].fuzzy_eq(&_0) &&
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-03 22:23:13 +00:00
|
|
|
self[1][0].fuzzy_eq(&_0) &&
|
|
|
|
self[1][2].fuzzy_eq(&_0) &&
|
|
|
|
self[1][3].fuzzy_eq(&_0) &&
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-03 22:23:13 +00:00
|
|
|
self[2][0].fuzzy_eq(&_0) &&
|
|
|
|
self[2][1].fuzzy_eq(&_0) &&
|
|
|
|
self[2][3].fuzzy_eq(&_0) &&
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-03 22:23:13 +00:00
|
|
|
self[3][0].fuzzy_eq(&_0) &&
|
|
|
|
self[3][1].fuzzy_eq(&_0) &&
|
|
|
|
self[3][2].fuzzy_eq(&_0)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_rotated(&self) -> bool {
|
2012-12-03 22:23:13 +00:00
|
|
|
// !self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here!
|
|
|
|
!self.fuzzy_eq(&Mat4::identity())
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2012-12-02 22:15:40 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn is_symmetric(&self) -> bool {
|
2012-12-03 22:23:13 +00:00
|
|
|
self[0][1].fuzzy_eq(&self[1][0]) &&
|
|
|
|
self[0][2].fuzzy_eq(&self[2][0]) &&
|
|
|
|
self[0][3].fuzzy_eq(&self[3][0]) &&
|
2012-12-02 22:15:40 +00:00
|
|
|
|
2012-12-03 22:23:13 +00:00
|
|
|
self[1][0].fuzzy_eq(&self[0][1]) &&
|
|
|
|
self[1][2].fuzzy_eq(&self[2][1]) &&
|
|
|
|
self[1][3].fuzzy_eq(&self[3][1]) &&
|
2012-12-02 22:15:40 +00:00
|
|
|
|
2012-12-03 22:23:13 +00:00
|
|
|
self[2][0].fuzzy_eq(&self[0][2]) &&
|
|
|
|
self[2][1].fuzzy_eq(&self[1][2]) &&
|
|
|
|
self[2][3].fuzzy_eq(&self[3][2]) &&
|
2012-12-02 22:15:40 +00:00
|
|
|
|
2012-12-03 22:23:13 +00:00
|
|
|
self[3][0].fuzzy_eq(&self[0][3]) &&
|
|
|
|
self[3][1].fuzzy_eq(&self[1][3]) &&
|
|
|
|
self[3][2].fuzzy_eq(&self[2][3])
|
2012-12-02 22:15:40 +00:00
|
|
|
}
|
2012-11-15 02:23:39 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn is_invertible(&self) -> bool {
|
2012-12-13 01:10:53 +00:00
|
|
|
!self.determinant().fuzzy_eq(&Number::zero())
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2012-12-12 01:29:35 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn to_ptr(&self) -> *T {
|
|
|
|
unsafe {
|
|
|
|
transmute::<*Mat4<T>, *T>(
|
|
|
|
to_unsafe_ptr(self)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 03:49:59 +00:00
|
|
|
pub impl<T:Copy Float Sign> Mat4<T>: MutableMatrix<T, Vec4<T>> {
|
2012-12-04 11:42:32 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn col_mut(&mut self, i: uint) -> &self/mut Vec4<T> {
|
|
|
|
match i {
|
|
|
|
0 => &mut self.x,
|
|
|
|
1 => &mut self.y,
|
|
|
|
2 => &mut self.z,
|
|
|
|
3 => &mut self.w,
|
|
|
|
_ => fail(fmt!("index out of bounds: expected an index from 0 to 3, but found %u", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn swap_cols(&mut self, a: uint, b: uint) {
|
|
|
|
util::swap(self.col_mut(a),
|
|
|
|
self.col_mut(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn swap_rows(&mut self, a: uint, b: uint) {
|
|
|
|
self.x.swap(a, b);
|
|
|
|
self.y.swap(a, b);
|
|
|
|
self.z.swap(a, b);
|
|
|
|
self.w.swap(a, b);
|
|
|
|
}
|
|
|
|
|
2012-12-05 03:49:59 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn set(&mut self, other: &Mat4<T>) {
|
|
|
|
(*self) = (*other);
|
|
|
|
}
|
|
|
|
|
2012-12-04 11:42:32 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn to_identity(&mut self) {
|
2012-12-05 03:49:59 +00:00
|
|
|
(*self) = Mat4::identity();
|
2012-12-04 11:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn to_zero(&mut self) {
|
2012-12-05 03:49:59 +00:00
|
|
|
(*self) = Mat4::zero();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn mul_self_t(&mut self, value: T) {
|
|
|
|
self.col_mut(0).mul_self_t(&value);
|
|
|
|
self.col_mut(1).mul_self_t(&value);
|
|
|
|
self.col_mut(2).mul_self_t(&value);
|
|
|
|
self.col_mut(3).mul_self_t(&value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn add_self_m(&mut self, other: &Mat4<T>) {
|
|
|
|
self.col_mut(0).add_self_v(&other[0]);
|
|
|
|
self.col_mut(1).add_self_v(&other[1]);
|
|
|
|
self.col_mut(2).add_self_v(&other[2]);
|
|
|
|
self.col_mut(3).add_self_v(&other[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn sub_self_m(&mut self, other: &Mat4<T>) {
|
|
|
|
self.col_mut(0).sub_self_v(&other[0]);
|
|
|
|
self.col_mut(1).sub_self_v(&other[1]);
|
|
|
|
self.col_mut(2).sub_self_v(&other[2]);
|
|
|
|
self.col_mut(3).sub_self_v(&other[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn invert_self(&mut self) {
|
|
|
|
match self.inverse() {
|
|
|
|
Some(m) => (*self) = m,
|
|
|
|
None => fail(~"Couldn't invert the matrix!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn transpose_self(&mut self) {
|
|
|
|
util::swap(self.col_mut(0).index_mut(1), self.col_mut(1).index_mut(0));
|
|
|
|
util::swap(self.col_mut(0).index_mut(2), self.col_mut(2).index_mut(0));
|
|
|
|
util::swap(self.col_mut(0).index_mut(3), self.col_mut(3).index_mut(0));
|
|
|
|
|
|
|
|
util::swap(self.col_mut(1).index_mut(0), self.col_mut(0).index_mut(1));
|
|
|
|
util::swap(self.col_mut(1).index_mut(2), self.col_mut(2).index_mut(1));
|
|
|
|
util::swap(self.col_mut(1).index_mut(3), self.col_mut(3).index_mut(1));
|
|
|
|
|
|
|
|
util::swap(self.col_mut(2).index_mut(0), self.col_mut(0).index_mut(2));
|
|
|
|
util::swap(self.col_mut(2).index_mut(1), self.col_mut(1).index_mut(2));
|
|
|
|
util::swap(self.col_mut(2).index_mut(3), self.col_mut(3).index_mut(2));
|
|
|
|
|
|
|
|
util::swap(self.col_mut(3).index_mut(0), self.col_mut(0).index_mut(3));
|
|
|
|
util::swap(self.col_mut(3).index_mut(1), self.col_mut(1).index_mut(3));
|
|
|
|
util::swap(self.col_mut(3).index_mut(2), self.col_mut(2).index_mut(3));
|
2012-12-04 11:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-02 22:15:40 +00:00
|
|
|
pub impl<T> Mat4<T>: Matrix4<T, Vec4<T>> {
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat4<T>: Neg<Mat4<T>> {
|
2012-12-02 22:15:40 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn neg(&self) -> Mat4<T> {
|
|
|
|
Mat4::from_cols(-self[0], -self[1], -self[2], -self[3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy> Mat4<T>: Index<uint, Vec4<T>> {
|
|
|
|
#[inline(always)]
|
2012-12-08 00:00:50 +00:00
|
|
|
pure fn index(&self, i: uint) -> Vec4<T> {
|
2012-12-02 22:15:40 +00:00
|
|
|
unsafe { do buf_as_slice(
|
|
|
|
transmute::<*Mat4<T>, *Vec4<T>>(
|
2012-12-08 00:00:50 +00:00
|
|
|
to_unsafe_ptr(self)), 4) |slice| { slice[i] }
|
2012-12-02 22:15:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat4<T>: Eq {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-29 03:14:42 +00:00
|
|
|
pure fn eq(&self, other: &Mat4<T>) -> bool {
|
2012-11-29 03:49:57 +00:00
|
|
|
self[0] == other[0] &&
|
|
|
|
self[1] == other[1] &&
|
|
|
|
self[2] == other[2] &&
|
|
|
|
self[3] == other[3]
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-29 03:14:42 +00:00
|
|
|
pure fn ne(&self, other: &Mat4<T>) -> bool {
|
|
|
|
!(self == other)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Float> Mat4<T>: FuzzyEq {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn fuzzy_eq(other: &Mat4<T>) -> bool {
|
|
|
|
self[0].fuzzy_eq(&other[0]) &&
|
|
|
|
self[1].fuzzy_eq(&other[1]) &&
|
|
|
|
self[2].fuzzy_eq(&other[2]) &&
|
|
|
|
self[3].fuzzy_eq(&other[3])
|
|
|
|
}
|
|
|
|
}
|