Improve length tests and add distance tests

This commit is contained in:
Brendan Zabarauskas 2012-11-14 17:55:55 +10:00
parent d62cefc543
commit a9273389ea
2 changed files with 63 additions and 31 deletions

View file

@ -38,19 +38,32 @@ fn test_Vec2() {
// fuzzy_eq // fuzzy_eq
// eq // eq
assert a.length2().fuzzy_eq(&5f);
assert a.length().fuzzy_eq(&2.236068f);
let c = Vec2::new(-2.0f, -1.0f);
let d = Vec2::new( 1.0f, 0.0f);
let f3 = 0.75f;
assert c.lerp(&d, f3) == Vec2::new(0.250f, -0.250f);
// assert c.abs() == Vec2::new( 2.0f, 1.0f); // assert c.abs() == Vec2::new( 2.0f, 1.0f);
// assert c.min(&d) == Vec2::new(-2.0f, -1.0f); // assert c.min(&d) == Vec2::new(-2.0f, -1.0f);
// assert c.max(&d) == Vec2::new( 1.0f, 0.0f); // assert c.max(&d) == Vec2::new( 1.0f, 0.0f);
} }
#[test]
fn test_Vec2_geometric() {
let a = Vec2::new(5f, 12f); // (5, 12, 13) Pythagorean triple
let b0 = Vec2::new(3f, 4f); // (3, 4, 5) Pythagorean triple
let b = a.add_v(&b0);
assert a.length() == 13f;
assert a.length2() == 13f * 13f;
assert b0.length() == 5f;
assert b0.length2() == 5f * 5f;
assert a.distance(&b) == 5f;
assert a.distance2(&b) == 5f * 5f;
let c = Vec2::new(-2.0f, -1.0f);
let d = Vec2::new( 1.0f, 0.0f);
assert c.lerp(&d, 0.75f) == Vec2::new(0.250f, -0.250f);
}
#[test] #[test]
fn test_Vec3() { fn test_Vec3() {
// assert Vec3::dim == 3; // assert Vec3::dim == 3;
@ -91,19 +104,32 @@ fn test_Vec3() {
// fuzzy_eq // fuzzy_eq
// eq // eq
assert a.length2().fuzzy_eq(&14f);
assert a.length().fuzzy_eq(&3.74165738677f);
let c = Vec3::new(-2.0f, -1.0f, 1.0f);
let d = Vec3::new( 1.0f, 0.0f, 0.5f);
let f3 = 0.75f;
assert c.lerp(&d, f3) == Vec3::new(0.250f, -0.250f, 0.625f);
// assert c.abs() == Vec3::new( 2.0f, 1.0f, 1.0f); // assert c.abs() == Vec3::new( 2.0f, 1.0f, 1.0f);
// assert c.min(&d) == Vec3::new(-2.0f, -1.0f, 0.5f); // assert c.min(&d) == Vec3::new(-2.0f, -1.0f, 0.5f);
// assert c.max(&d) == Vec3::new( 1.0f, 0.0f, 1.0f); // assert c.max(&d) == Vec3::new( 1.0f, 0.0f, 1.0f);
} }
#[test]
fn test_Vec3_geometric() {
let a = Vec3::new(2f, 3f, 6f); // (2, 3, 6, 7) Pythagorean quadruple
let b0 = Vec3::new(1f, 4f, 8f); // (1, 4, 8, 9) Pythagorean quadruple
let b = a.add_v(&b0);
assert a.length() == 7f;
assert a.length2() == 7f * 7f;
assert b0.length() == 9f;
assert b0.length2() == 9f * 9f;
assert a.distance(&b) == 9f;
assert a.distance2(&b) == 9f * 9f;
let c = Vec3::new(-2.0f, -1.0f, 1.0f);
let d = Vec3::new( 1.0f, 0.0f, 0.5f);
assert c.lerp(&d, 0.75f) == Vec3::new(0.250f, -0.250f, 0.625f);
}
#[test] #[test]
fn test_Vec4() { fn test_Vec4() {
// assert Vec4::dim == 4; // assert Vec4::dim == 4;
@ -147,15 +173,28 @@ fn test_Vec4() {
// fuzzy_eq // fuzzy_eq
// eq // eq
assert a.length2().fuzzy_eq(&30f);
assert a.length().fuzzy_eq(&5.477226f);
let c = Vec4::new(-2.0f, -1.0f, 1.0f, 2.0f);
let d = Vec4::new( 1.0f, 0.0f, 0.5f, 1.0f);
let f3 = 0.75f;
assert c.lerp(&d, f3) == Vec4::new(0.250f, -0.250f, 0.625f, 1.250f);
// assert c.abs() == Vec4::new( 2.0f, 1.0f, 1.0f, 2.0f); // assert c.abs() == Vec4::new( 2.0f, 1.0f, 1.0f, 2.0f);
// assert c.min(&d) == Vec4::new(-2.0f, -1.0f, 0.5f, 1.0f); // assert c.min(&d) == Vec4::new(-2.0f, -1.0f, 0.5f, 1.0f);
// assert c.max(&d) == Vec4::new( 1.0f, 0.0f, 1.0f, 2.0f); // assert c.max(&d) == Vec4::new( 1.0f, 0.0f, 1.0f, 2.0f);
} }
#[test]
fn test_Vec4_geometric() {
let a = Vec4::new(1f, 2f, 4f, 10f); // (1, 2, 4, 10, 11) Pythagorean quintuple
let b0 = Vec4::new(1f, 2f, 8f, 10f); // (1, 2, 8, 10, 13) Pythagorean quintuple
let b = a.add_v(&b0);
assert a.length() == 11f;
assert a.length2() == 11f * 11f;
assert b0.length() == 13f;
assert b0.length2() == 13f * 13f;
assert a.distance(&b) == 13f;
assert a.distance2(&b) == 13f * 13f;
let c = Vec4::new(-2.0f, -1.0f, 1.0f, 2.0f);
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);
}

View file

@ -185,13 +185,11 @@ pub impl<T:Copy Num NumCast Exp> Vec2<T>: GeometricVector<T> {
self.length2().sqrt() self.length2().sqrt()
} }
// TODO: tests
#[inline(always)] #[inline(always)]
pure fn distance2(other: &Vec2<T>) -> T { pure fn distance2(other: &Vec2<T>) -> T {
other.sub_v(&self).length2() other.sub_v(&self).length2()
} }
// TODO: tests
#[inline(always)] #[inline(always)]
pure fn distance(other: &Vec2<T>) -> T { pure fn distance(other: &Vec2<T>) -> T {
other.distance2(&self).sqrt() other.distance2(&self).sqrt()
@ -375,13 +373,11 @@ pub impl<T:Copy Num NumCast Exp> Vec3<T>: GeometricVector<T> {
self.length2().sqrt() self.length2().sqrt()
} }
// TODO: tests
#[inline(always)] #[inline(always)]
pure fn distance2(other: &Vec3<T>) -> T { pure fn distance2(other: &Vec3<T>) -> T {
other.sub_v(&self).length2() other.sub_v(&self).length2()
} }
// TODO: tests
#[inline(always)] #[inline(always)]
pure fn distance(other: &Vec3<T>) -> T { pure fn distance(other: &Vec3<T>) -> T {
other.distance2(&self).sqrt() other.distance2(&self).sqrt()
@ -442,7 +438,6 @@ pub impl<T:Copy FuzzyEq> Vec3<T>: FuzzyEq {
pub struct Vec4<T> { x: T, y: T, z: T, w: T } pub struct Vec4<T> { x: T, y: T, z: T, w: T }
pub mod Vec4 { pub mod Vec4 {
#[inline(always)] #[inline(always)]
pub pure fn new<T>(x: T, y: T, z: T, w: T) -> Vec4<T> { pub pure fn new<T>(x: T, y: T, z: T, w: T) -> Vec4<T> {
Vec4 { x: move x, y: move y, z: move z, w: move w } Vec4 { x: move x, y: move y, z: move z, w: move w }
@ -570,13 +565,11 @@ pub impl<T:Copy Num NumCast Exp> Vec4<T>: GeometricVector<T> {
self.length2().sqrt() self.length2().sqrt()
} }
// TODO: tests
#[inline(always)] #[inline(always)]
pure fn distance2(other: &Vec4<T>) -> T { pure fn distance2(other: &Vec4<T>) -> T {
other.sub_v(&self).length2() other.sub_v(&self).length2()
} }
// TODO: tests
#[inline(always)] #[inline(always)]
pure fn distance(other: &Vec4<T>) -> T { pure fn distance(other: &Vec4<T>) -> T {
other.distance2(&self).sqrt() other.distance2(&self).sqrt()