Add to_identity and to_zero methods

This commit is contained in:
Brendan Zabarauskas 2012-12-04 12:51:38 +10:00
parent c7571e4d50
commit 07bed0a9c1
2 changed files with 95 additions and 0 deletions

View file

@ -33,11 +33,21 @@ pub trait Matrix<T,V>: Dimensional<V>, ToPtr<T>, Eq, Neg<self> {
*/
static pure fn identity() -> self;
/**
* Sets the matrix to the identity matrix
*/
fn to_identity(&mut self);
/**
* Returns a matrix with all elements set to zero
*/
static pure fn zero() -> self;
/**
* Sets each element of the matrix to zero
*/
fn to_zero(&mut self);
/**
* Returns the scalar multiplication of this matrix and `value`
*/
@ -259,6 +269,11 @@ pub impl<T:Copy Float> Mat2<T>: Matrix<T, Vec2<T>> {
_0, _1)
}
#[inline(always)]
fn to_identity(&mut self) {
*self = Mat2::identity();
}
/**
* Returns the additive identity matrix
* ~~~
@ -277,6 +292,11 @@ pub impl<T:Copy Float> Mat2<T>: Matrix<T, Vec2<T>> {
_0, _0)
}
#[inline(always)]
fn to_zero(&mut self) {
*self = Mat2::zero();
}
#[inline(always)]
pure fn mul_t(&self, value: T) -> Mat2<T> {
Mat2::from_cols(self[0].mul_t(value),
@ -570,6 +590,11 @@ pub impl<T:Copy Float> Mat3<T>: Matrix<T, Vec3<T>> {
_0, _0, _1)
}
#[inline(always)]
fn to_identity(&mut self) {
*self = Mat3::identity();
}
/**
* Returns the additive identity matrix
* ~~~
@ -591,6 +616,11 @@ pub impl<T:Copy Float> Mat3<T>: Matrix<T, Vec3<T>> {
_0, _0, _0)
}
#[inline(always)]
fn to_zero(&mut self) {
*self = Mat3::zero();
}
#[inline(always)]
pure fn mul_t(&self, value: T) -> Mat3<T> {
Mat3::from_cols(self[0].mul_t(value),
@ -974,6 +1004,11 @@ pub impl<T:Copy Float Sign> Mat4<T>: Matrix<T, Vec4<T>> {
_0, _0, _0, _1)
}
#[inline(always)]
fn to_identity(&mut self) {
*self = Mat4::identity();
}
/**
* Returns the additive identity matrix
* ~~~
@ -998,6 +1033,11 @@ pub impl<T:Copy Float Sign> Mat4<T>: Matrix<T, Vec4<T>> {
_0, _0, _0, _0)
}
#[inline(always)]
fn to_zero(&mut self) {
*self = Mat4::zero();
}
#[inline(always)]
pure fn mul_t(&self, value: T) -> Mat4<T> {
Mat4::from_cols(self[0].mul_t(value),

View file

@ -9,6 +9,9 @@ fn test_Mat2() {
y: Vec2 { x: 2f, y: 4f } };
let b = Mat2 { x: Vec2 { x: 2f, y: 4f },
y: Vec2 { x: 3f, y: 5f } };
let mut mut_a = a;
let v1 = Vec2::new(1f, 2f);
let f1 = 0.5f;
@ -29,6 +32,19 @@ fn test_Mat2() {
assert a.col(0) == Vec2::new(1f, 3f);
assert a.col(1) == Vec2::new(2f, 4f);
assert Mat2::identity() == Mat2::new(1f, 0f,
0f, 1f);
mut_a.to_identity();
assert mut_a.is_identity();
mut_a = a;
assert Mat2::zero() == Mat2::new(0f, 0f,
0f, 0f);
mut_a.to_zero();
assert mut_a == Mat2::new(0f, 0f,
0f, 0f);
assert a.determinant() == -2f;
assert a.trace() == 5f;
@ -105,6 +121,9 @@ fn test_Mat3() {
let b = Mat3 { x: Vec3 { x: 2f, y: 5f, z: 8f },
y: Vec3 { x: 3f, y: 6f, z: 9f },
z: Vec3 { x: 4f, y: 7f, z: 10f } };
let mut mut_a = a;
let v1 = Vec3::new(1f, 2f, 3f);
let f1 = 0.5f;
@ -136,6 +155,21 @@ fn test_Mat3() {
assert a.col(0) == Vec3::new(1f, 4f, 7f);
assert a.col(1) == Vec3::new(2f, 5f, 8f);
assert a.col(2) == Vec3::new(3f, 6f, 9f);
assert Mat3::identity() == Mat3::new(1f, 0f, 0f,
0f, 1f, 0f,
0f, 0f, 1f);
mut_a.to_identity();
assert mut_a.is_identity();
mut_a = a;
assert Mat3::zero() == Mat3::new(0f, 0f, 0f,
0f, 0f, 0f,
0f, 0f, 0f);
mut_a.to_zero();
assert mut_a == Mat3::new(0f, 0f, 0f,
0f, 0f, 0f,
0f, 0f, 0f);
assert a.determinant() == 0f;
assert a.trace() == 15f;
@ -228,6 +262,9 @@ fn test_Mat4() {
y: Vec4 { x: 2f, y: 3f, z: 2f, w: 2f },
z: Vec4 { x: 1f, y: 2f, z: 3f, w: 3f },
w: Vec4 { x: 0f, y: 1f, z: 1f, w: 0f } };
let mut mut_a = a;
let v1 = Vec4::new(1f, 2f, 3f, 4f);
let f1 = 0.5f;
@ -273,6 +310,24 @@ fn test_Mat4() {
assert a.col(1) == Vec4::new(2f, 6f, 10f, 14f);
assert a.col(2) == Vec4::new(3f, 7f, 11f, 15f);
assert a.col(3) == Vec4::new(4f, 8f, 12f, 16f);
assert Mat4::identity() == Mat4::new(1f, 0f, 0f, 0f,
0f, 1f, 0f, 0f,
0f, 0f, 1f, 0f,
0f, 0f, 0f, 1f);
mut_a.to_identity();
assert mut_a.is_identity();
mut_a = a;
assert Mat4::zero() == Mat4::new(0f, 0f, 0f, 0f,
0f, 0f, 0f, 0f,
0f, 0f, 0f, 0f,
0f, 0f, 0f, 0f);
mut_a.to_zero();
assert mut_a == Mat4::new(0f, 0f, 0f, 0f,
0f, 0f, 0f, 0f,
0f, 0f, 0f, 0f,
0f, 0f, 0f, 0f);
assert a.determinant() == 0f;
assert a.trace() == 34f;