Create and implement MutableGeometricVector trait

This commit is contained in:
Brendan Zabarauskas 2012-12-05 08:21:40 +10:00
parent ea17c63200
commit e2857c6e7e
2 changed files with 74 additions and 3 deletions

View file

@ -81,6 +81,8 @@ fn test_Vec2_geometric() {
let b0 = Vec2::new(3f, 4f); // (3, 4, 5) Pythagorean triple
let b = a.add_v(&b0);
// TODO: test normalize and normalize_self
assert a.length() == 13f;
assert a.length2() == 13f * 13f;
@ -94,6 +96,10 @@ fn test_Vec2_geometric() {
let d = Vec2::new( 1.0f, 0.0f);
assert c.lerp(&d, 0.75f) == Vec2::new(0.250f, -0.250f);
let mut mut_c = c;
mut_c.lerp_self(&d, &0.75f);
assert mut_c == c.lerp(&d, 0.75f);
}
#[test]
@ -189,6 +195,8 @@ fn test_Vec3_geometric() {
let b0 = Vec3::new(1f, 4f, 8f); // (1, 4, 8, 9) Pythagorean quadruple
let b = a.add_v(&b0);
// TODO: test normalize and normalize_self
assert a.length() == 7f;
assert a.length2() == 7f * 7f;
@ -202,6 +210,10 @@ fn test_Vec3_geometric() {
let d = Vec3::new( 1.0f, 0.0f, 0.5f);
assert c.lerp(&d, 0.75f) == Vec3::new(0.250f, -0.250f, 0.625f);
let mut mut_c = c;
mut_c.lerp_self(&d, &0.75f);
assert mut_c == c.lerp(&d, 0.75f);
}
#[test]
@ -297,6 +309,8 @@ fn test_Vec4_geometric() {
let b0 = Vec4::new(1f, 2f, 8f, 10f); // (1, 2, 8, 10, 13) Pythagorean quintuple
let b = a.add_v(&b0);
// TODO: test normalize and normalize_self
assert a.length() == 11f;
assert a.length2() == 11f * 11f;
@ -310,4 +324,8 @@ fn test_Vec4_geometric() {
let d = Vec4::new( 1.0f, 0.0f, 0.5f, 1.0f);
assert c.lerp(&d, 0.75f) == Vec4::new(0.250f, -0.250f, 0.625f, 1.250f);
let mut mut_c = c;
mut_c.lerp_self(&d, &0.75f);
assert mut_c == c.lerp(&d, 0.75f);
}

View file

@ -77,7 +77,7 @@ pub trait NumericVector<T>: Vector<T>, Neg<self> {
pure fn div_t(&self, value: T) -> self;
/**
* Returns the sum of the vector with `other`
* Returns the sum of the vector and `other`
*/
pure fn add_v(&self, other: &self) -> self;
@ -112,12 +112,12 @@ pub trait MutableNumericVector<T>: MutableVector<&self/T>, NumericVector<T> {
fn div_self_t(&mut self, value: T);
/**
* Add a vector
* Set to the sum of the vector and `other`
*/
fn add_self_v(&mut self, other: &self);
/**
* Subtract a vector
* Set to the difference between the vector and `other`
*/
fn sub_self_v(&mut self, other: &self);
}
@ -137,6 +137,7 @@ pub trait NumericVector3<T>: NumericVector<T> {
// static pure fn unit_x() -> self;
// static pure fn unit_y() -> self;
// static pure fn unit_z() -> self;
/**
* Returns the cross product of the vector and `other`
*/
@ -198,6 +199,19 @@ pub trait GeometricVector<T>: NumericVector<T> {
pure fn lerp(&self, other: &self, amount: T) -> self;
}
pub trait MutableGeometricVector<T>: MutableNumericVector<&self/T>,
GeometricVector<T> {
/**
* Normalize the vector
*/
fn normalize_self(&mut self);
/**
* Linearly intoperlate the vector towards `other`
*/
fn lerp_self(&mut self, other: &self, amount: T);
}
@ -381,6 +395,19 @@ pub impl<T:Copy Number Exp> Vec2<T>: GeometricVector<T> {
}
}
pub impl<T:Copy Number Exp> Vec2<T>: MutableGeometricVector<&self/T> {
#[inline(always)]
fn normalize_self(&mut self) {
let mut n: T = Number::from(1);
n /= self.length();
self.mul_self_t(&n);
}
fn lerp_self(&mut self, other: &Vec2<T>, amount: &T) {
self.add_self_v(&other.sub_v(&*self).mul_t(*amount));
}
}
pub impl<T:Copy DefaultEq> Vec2<T>: Eq {
#[inline(always)]
pure fn eq(&self, other: &Vec2<T>) -> bool {
@ -622,6 +649,19 @@ pub impl<T:Copy Number Exp> Vec3<T>: GeometricVector<T> {
}
}
pub impl<T:Copy Number Exp> Vec3<T>: MutableGeometricVector<&self/T> {
#[inline(always)]
fn normalize_self(&mut self) {
let mut n: T = Number::from(1);
n /= self.length();
self.mul_self_t(&n);
}
fn lerp_self(&mut self, other: &Vec3<T>, amount: &T) {
self.add_self_v(&other.sub_v(&*self).mul_t(*amount));
}
}
pub impl<T:Copy DefaultEq> Vec3<T>: Eq {
#[inline(always)]
pure fn eq(&self, other: &Vec3<T>) -> bool {
@ -862,6 +902,19 @@ pub impl<T:Copy Number Exp> Vec4<T>: GeometricVector<T> {
}
}
pub impl<T:Copy Number Exp> Vec4<T>: MutableGeometricVector<&self/T> {
#[inline(always)]
fn normalize_self(&mut self) {
let mut n: T = Number::from(1);
n /= self.length();
self.mul_self_t(&n);
}
fn lerp_self(&mut self, other: &Vec4<T>, amount: &T) {
self.add_self_v(&other.sub_v(&*self).mul_t(*amount));
}
}
pub impl<T:Copy DefaultEq> Vec4<T>: Eq {
#[inline(always)]
pure fn eq(&self, other: &Vec4<T>) -> bool {