Add matrix unittests

This commit is contained in:
Brendan Zabarauskas 2012-09-08 11:54:32 +10:00
parent e9b407b6a5
commit 04bdd00576
2 changed files with 273 additions and 44 deletions

View file

@ -21,10 +21,10 @@ trait Matrix<T:Num Ord FuzzyEq, V:Vector<T>> {
pure fn neg() -> self; pure fn neg() -> self;
pure fn add_m(&&other:self) -> self;
pure fn sub_m(&&other:self) -> self;
pure fn mul_f(&&value:T) -> self; pure fn mul_f(&&value:T) -> self;
pure fn mul_v(&&other:V) -> V; pure fn mul_v(&&other:V) -> V;
pure fn add_m(&&other:self) -> self;
pure fn sub_m(&&other:self) -> self;
pure fn mul_m(&&other:self) -> self; pure fn mul_m(&&other:self) -> self;
// pure fn invert(&&other:self) -> self; // pure fn invert(&&other:self) -> self;
@ -131,18 +131,6 @@ impl mat2: Matrix<float, vec2> {
mat2_v(-self[0], -self[1]) mat2_v(-self[0], -self[1])
} }
#[inline(always)]
pure fn add_m(&&other:mat2) -> mat2 {
mat2_v(self[0].add_v(other[0]),
self[1].add_v(other[1]))
}
#[inline(always)]
pure fn sub_m(&&other:mat2) -> mat2 {
mat2_v(self[0].add_v(other[0]),
self[1].add_v(other[1]))
}
#[inline(always)] #[inline(always)]
pure fn mul_f(&&value:float) -> mat2 { pure fn mul_f(&&value:float) -> mat2 {
mat2_v(self[0].mul_f(value), mat2_v(self[0].mul_f(value),
@ -155,6 +143,18 @@ impl mat2: Matrix<float, vec2> {
self[0][1]*other[0] + self[1][1]*other[1]) self[0][1]*other[0] + self[1][1]*other[1])
} }
#[inline(always)]
pure fn add_m(&&other:mat2) -> mat2 {
mat2_v(self[0].add_v(other[0]),
self[1].add_v(other[1]))
}
#[inline(always)]
pure fn sub_m(&&other:mat2) -> mat2 {
mat2_v(self[0].sub_v(other[0]),
self[1].sub_v(other[1]))
}
#[inline(always)] #[inline(always)]
pure fn mul_m(&&other:mat2) -> mat2 { pure fn mul_m(&&other:mat2) -> mat2 {
mat2(self[0][0]*other[0][0] + self[1][0]*other[0][1], mat2(self[0][0]*other[0][0] + self[1][0]*other[0][1],
@ -293,20 +293,6 @@ impl mat3: Matrix<float, vec3> {
mat3_v(-self[0], -self[1], -self[2]) mat3_v(-self[0], -self[1], -self[2])
} }
#[inline(always)]
pure fn add_m(&&other:mat3) -> mat3 {
mat3_v(self[0].add_v(other[0]),
self[1].add_v(other[1]),
self[2].add_v(other[2]))
}
#[inline(always)]
pure fn sub_m(&&other:mat3) -> mat3 {
mat3_v(self[0].add_v(other[0]),
self[1].add_v(other[1]),
self[2].add_v(other[2]))
}
#[inline(always)] #[inline(always)]
pure fn mul_f(&&value:float) -> mat3 { pure fn mul_f(&&value:float) -> mat3 {
mat3_v(self[0].mul_f(value), mat3_v(self[0].mul_f(value),
@ -321,6 +307,20 @@ impl mat3: Matrix<float, vec3> {
self[0][2]*other[0] + self[1][2]*other[1] + self[2][2]*other[2]) self[0][2]*other[0] + self[1][2]*other[1] + self[2][2]*other[2])
} }
#[inline(always)]
pure fn add_m(&&other:mat3) -> mat3 {
mat3_v(self[0].add_v(other[0]),
self[1].add_v(other[1]),
self[2].add_v(other[2]))
}
#[inline(always)]
pure fn sub_m(&&other:mat3) -> mat3 {
mat3_v(self[0].sub_v(other[0]),
self[1].sub_v(other[1]),
self[2].sub_v(other[2]))
}
#[inline(always)] #[inline(always)]
pure fn mul_m(&&other:mat3) -> mat3 { pure fn mul_m(&&other:mat3) -> mat3 {
mat3(self[0][0]*other[0][0] + self[1][0]*other[0][1] + self[2][0]*other[0][2], mat3(self[0][0]*other[0][0] + self[1][0]*other[0][1] + self[2][0]*other[0][2],
@ -542,22 +542,6 @@ impl mat4: Matrix<float, vec4> {
mat4_v(-self[0], -self[1], -self[2], -self[3]) mat4_v(-self[0], -self[1], -self[2], -self[3])
} }
#[inline(always)]
pure fn add_m(&&other:mat4) -> mat4 {
mat4_v(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)]
pure fn sub_m(&&other:mat4) -> mat4 {
mat4_v(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)] #[inline(always)]
pure fn mul_f(&&value:float) -> mat4 { pure fn mul_f(&&value:float) -> mat4 {
mat4_v(self[0].mul_f(value), mat4_v(self[0].mul_f(value),
@ -574,6 +558,22 @@ impl mat4: Matrix<float, vec4> {
self[0][3]*other[0] + self[1][3]*other[1] + self[2][3]*other[2] + self[3][3]*other[3]) self[0][3]*other[0] + self[1][3]*other[1] + self[2][3]*other[2] + self[3][3]*other[3])
} }
#[inline(always)]
pure fn add_m(&&other:mat4) -> mat4 {
mat4_v(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)]
pure fn sub_m(&&other:mat4) -> mat4 {
mat4_v(self[0].sub_v(other[0]),
self[1].sub_v(other[1]),
self[2].sub_v(other[2]),
self[3].sub_v(other[3]))
}
#[inline(always)] #[inline(always)]
pure fn mul_m(&&other:mat4) -> mat4 { pure fn mul_m(&&other:mat4) -> mat4 {
mat4(self[0][0]*other[0][0] + self[1][0]*other[0][1] + self[2][0]*other[0][2] + self[3][0]*other[0][3], mat4(self[0][0]*other[0][0] + self[1][0]*other[0][1] + self[2][0]*other[0][2] + self[3][0]*other[0][3],

View file

@ -1,16 +1,245 @@
import om3d::mat::*;
import om3d::vec::*;
// TODO // TODO
#[test] #[test]
fn test_mat2() { fn test_mat2() {
let a = mat2 { data: [ vec2 { data: [ 1f, 3f ] },
vec2 { data: [ 2f, 4f ] } ] };
let b = mat2 { data: [ vec2 { data: [ 2f, 4f ] },
vec2 { data: [ 3f, 5f ] } ] };
let v1 = vec2(1f, 2f);
let f1 = 0.5f;
assert a == mat2(1f, 3f,
2f, 4f);
assert a == mat2_v(vec2(1f, 3f),
vec2(2f, 4f));
assert a[0] == vec2(1f, 3f);
assert a[1] == vec2(2f, 4f);
assert a.row(0) == vec2(1f, 2f);
assert a.row(1) == vec2(3f, 4f);
assert a.col(0) == vec2(1f, 3f);
assert a.col(1) == vec2(2f, 4f);
assert a.neg() == mat2(-1f, -3f,
-2f, -4f);
assert -a == a.neg();
assert a.mul_f(f1) == mat2(0.5f, 1.5f,
1.0f, 2.0f);
assert a.mul_v(v1) == vec2(5f, 11f);
assert a.add_m(b) == mat2(3f, 7f,
5f, 9f);
assert a.sub_m(b) == mat2(-1f, -1f,
-1f, -1f);
assert a.mul_m(b) == mat2(10.0, 22.0,
13.0, 29.0);
assert a.transpose() == mat2(1f, 2f,
3f, 4f);
// exact_eq
// fuzzy_eq
// eq
assert mat2_identity().is_identity();
assert mat2_identity().is_symmetric();
assert mat2_identity().is_diagonal();
assert !mat2_identity().is_rotated();
assert !a.is_identity();
assert !a.is_symmetric();
assert !a.is_diagonal();
assert a.is_rotated();
let c = mat2(2f, 1f,
1f, 2f);
assert !c.is_identity();
assert c.is_symmetric();
assert !c.is_diagonal();
assert c.is_rotated();
} }
#[test] #[test]
fn test_mat3() { fn test_mat3() {
let a = mat3 { data: [ vec3 { data: [ 1f, 4f, 7f ] },
vec3 { data: [ 2f, 5f, 8f ] },
vec3 { data: [ 3f, 6f, 9f ] } ] };
let b = mat3 { data: [ vec3 { data: [ 2f, 5f, 8f ] },
vec3 { data: [ 3f, 6f, 9f ] },
vec3 { data: [ 4f, 7f, 10f ] } ] };
let v1 = vec3(1f, 2f, 3f);
let f1 = 0.5f;
assert a == mat3(1f, 4f, 7f,
2f, 5f, 8f,
3f, 6f, 9f);
assert a == mat3_v(vec3(1f, 4f, 7f),
vec3(2f, 5f, 8f),
vec3(3f, 6f, 9f));
assert a[0] == vec3(1f, 4f, 7f);
assert a[1] == vec3(2f, 5f, 8f);
assert a[2] == vec3(3f, 6f, 9f);
assert a.row(0) == vec3(1f, 2f, 3f);
assert a.row(1) == vec3(4f, 5f, 6f);
assert a.row(2) == vec3(7f, 8f, 9f);
assert a.col(0) == vec3(1f, 4f, 7f);
assert a.col(1) == vec3(2f, 5f, 8f);
assert a.col(2) == vec3(3f, 6f, 9f);
assert a.neg() == mat3(-1f, -4f, -7f,
-2f, -5f, -8f,
-3f, -6f, -9f);
assert -a == a.neg();
assert a.mul_f(f1) == mat3(0.5f, 2.0f, 3.5f,
1.0f, 2.5f, 4.0f,
1.5f, 3.0f, 4.5f);
assert a.mul_v(v1) == vec3(14f, 32f, 50f);
assert a.add_m(b) == mat3(3f, 9f, 15f,
5f, 11f, 17f,
7f, 13f, 19f);
assert a.sub_m(b) == mat3(-1f, -1f, -1f,
-1f, -1f, -1f,
-1f, -1f, -1f);
assert a.mul_m(b) == mat3(36f, 81f, 126f,
42f, 96f, 150f,
48f, 111f, 174f);
assert a.transpose() == mat3(1f, 2f, 3f,
4f, 5f, 6f,
7f, 8f, 9f);
// exact_eq
// fuzzy_eq
// eq
assert mat3_identity().is_identity();
assert mat3_identity().is_symmetric();
assert mat3_identity().is_diagonal();
assert !mat3_identity().is_rotated();
assert !a.is_identity();
assert !a.is_symmetric();
assert !a.is_diagonal();
assert a.is_rotated();
let c = mat3(3f, 2f, 1f,
2f, 3f, 2f,
1f, 2f, 3f);
assert !c.is_identity();
assert c.is_symmetric();
assert !c.is_diagonal();
assert c.is_rotated();
assert a.to_mat4() == mat4(1f, 4f, 7f, 0f,
2f, 5f, 8f, 0f,
3f, 6f, 9f, 0f,
0f, 0f, 0f, 1f);
// to_quaternion
} }
#[test] #[test]
fn test_mat4() { fn test_mat4() {
let a = mat4 { data: [ vec4 { data: [ 1f, 5f, 9f, 13f ] },
vec4 { data: [ 2f, 6f, 10f, 14f ] },
vec4 { data: [ 3f, 7f, 11f, 15f ] },
vec4 { data: [ 4f, 8f, 12f, 16f ] } ] };
let b = mat4 { data: [ vec4 { data: [ 2f, 6f, 10f, 14f ] },
vec4 { data: [ 3f, 7f, 11f, 15f ] },
vec4 { data: [ 4f, 8f, 12f, 16f ] },
vec4 { data: [ 5f, 9f, 13f, 17f ] } ] };
let v1 = vec4(1f, 2f, 3f, 4f);
let f1 = 0.5f;
assert a == mat4(1f, 5f, 9f, 13f,
2f, 6f, 10f, 14f,
3f, 7f, 11f, 15f,
4f, 8f, 12f, 16f);
assert a == mat4_v(vec4(1f, 5f, 9f, 13f),
vec4(2f, 6f, 10f, 14f),
vec4(3f, 7f, 11f, 15f),
vec4(4f, 8f, 12f, 16f));
assert a[0] == vec4(1f, 5f, 9f, 13f);
assert a[1] == vec4(2f, 6f, 10f, 14f);
assert a[2] == vec4(3f, 7f, 11f, 15f);
assert a[3] == vec4(4f, 8f, 12f, 16f);
assert a.row(0) == vec4( 1f, 2f, 3f, 4f);
assert a.row(1) == vec4( 5f, 6f, 7f, 8f);
assert a.row(2) == vec4( 9f, 10f, 11f, 12f);
assert a.row(3) == vec4(13f, 14f, 15f, 16f);
assert a.col(0) == vec4(1f, 5f, 9f, 13f);
assert a.col(1) == vec4(2f, 6f, 10f, 14f);
assert a.col(2) == vec4(3f, 7f, 11f, 15f);
assert a.col(3) == vec4(4f, 8f, 12f, 16f);
assert a.neg() == mat4(-1f, -5f, -9f, -13f,
-2f, -6f, -10f, -14f,
-3f, -7f, -11f, -15f,
-4f, -8f, -12f, -16f);
assert -a == a.neg();
assert a.mul_f(f1) == mat4(0.5f, 2.5f, 4.5f, 6.5f,
1.0f, 3.0f, 5.0f, 7.0f,
1.5f, 3.5f, 5.5f, 7.5f,
2.0f, 4.0f, 6.0f, 8.0f);
assert a.mul_v(v1) == vec4(30.0, 70.0, 110.0, 150.0);
assert a.add_m(b) == mat4(3f, 11f, 19f, 27f,
5f, 13f, 21f, 29f,
7f, 15f, 23f, 31f,
9f, 17f, 25f, 33f);
assert a.sub_m(b) == mat4(-1f, -1f, -1f, -1f,
-1f, -1f, -1f, -1f,
-1f, -1f, -1f, -1f,
-1f, -1f, -1f, -1f);
assert a.mul_m(b) == mat4(100f, 228f, 356f, 484f,
110f, 254f, 398f, 542f,
120f, 280f, 440f, 600f,
130f, 306f, 482f, 658f);
assert a.transpose() == mat4( 1f, 2f, 3f, 4f,
5f, 6f, 7f, 8f,
9f, 10f, 11f, 12f,
13f, 14f, 15f, 16f);
// exact_eq
// fuzzy_eq
// eq
assert mat4_identity().is_identity();
assert mat4_identity().is_symmetric();
assert mat4_identity().is_diagonal();
assert !mat4_identity().is_rotated();
assert !a.is_identity();
assert !a.is_symmetric();
assert !a.is_diagonal();
assert a.is_rotated();
let c = mat4(4f, 3f, 2f, 1f,
3f, 4f, 3f, 2f,
2f, 3f, 4f, 3f,
1f, 2f, 3f, 4f);
assert !c.is_identity();
assert c.is_symmetric();
assert !c.is_diagonal();
assert c.is_rotated();
} }