Moved constants to submodules

This commit is contained in:
Brendan Zabarauskas 2012-10-30 00:11:27 +10:00
parent 5d8319fff0
commit c80b95da59
6 changed files with 104 additions and 84 deletions

View file

@ -2,7 +2,7 @@ use std::cmp::FuzzyEq;
use cmp::Eq; use cmp::Eq;
use math::Sqrt; use math::Sqrt;
use quaternion::Quat; use quaternion::Quat;
use vector::*; use vector::{Vec2, Vec3, Vec4};
// //
// NxN Matrix // NxN Matrix
@ -59,10 +59,10 @@ pub trait Matrix4<V3, V4> {
// //
pub struct Mat2 { data:[Vec2 * 2] } pub struct Mat2 { data:[Vec2 * 2] }
pub const mat2_zero :Mat2 = Mat2 { data: [ vec2_zero, pub const mat2_zero :Mat2 = Mat2 { data: [ Vec2::zero,
vec2_zero ] }; Vec2::zero ] };
pub const mat2_identity :Mat2 = Mat2 { data: [ vec2_unit_x, pub const mat2_identity :Mat2 = Mat2 { data: [ Vec2::unit_x,
vec2_unit_y ] }; Vec2::unit_y ] };
// //
// Mat2 Constructor // Mat2 Constructor
@ -82,6 +82,13 @@ pub pure fn Mat2_v(col0: &Vec2, col1: &Vec2) -> Mat2 {
Mat2 { data: [ *col0, *col1 ] } Mat2 { data: [ *col0, *col1 ] }
} }
pub mod Mat2 {
pub const zero :Mat2 = Mat2 { data: [ Vec2::zero,
Vec2::zero ] };
pub const identity :Mat2 = Mat2 { data: [ Vec2::unit_x,
Vec2::unit_y ] };
}
// //
// Matrix2x2 Implementation // Matrix2x2 Implementation
// //
@ -157,7 +164,7 @@ pub impl Mat2: Matrix<float, Vec2> {
#[inline] #[inline]
pure fn is_identity() -> bool { pure fn is_identity() -> bool {
self.fuzzy_eq(&mat2_identity) self.fuzzy_eq(&Mat2::identity)
} }
#[inline] #[inline]
@ -174,7 +181,7 @@ pub impl Mat2: Matrix<float, Vec2> {
#[inline] #[inline]
pure fn is_rotated() -> bool { pure fn is_rotated() -> bool {
!self.fuzzy_eq(&mat2_identity) !self.fuzzy_eq(&Mat2::identity)
} }
} }
@ -222,13 +229,6 @@ pub impl Mat2: FuzzyEq {
// //
pub struct Mat3 { data:[Vec3 * 3] } pub struct Mat3 { data:[Vec3 * 3] }
pub const mat3_zero :Mat3 = Mat3 { data: [ vec3_zero,
vec3_zero,
vec3_zero ] };
pub const mat3_identity :Mat3 = Mat3 { data: [ vec3_unit_x,
vec3_unit_y,
vec3_unit_z ] };
// //
// Mat3 Constructor // Mat3 Constructor
// //
@ -249,6 +249,15 @@ pub pure fn Mat3_v(col0: &Vec3, col1: &Vec3, col2: &Vec3) -> Mat3 {
Mat3 { data: [ *col0, *col1, *col2 ] } Mat3 { data: [ *col0, *col1, *col2 ] }
} }
pub mod Mat3 {
pub const zero :Mat3 = Mat3 { data: [ Vec3::zero,
Vec3::zero,
Vec3::zero ] };
pub const identity :Mat3 = Mat3 { data: [ Vec3::unit_x,
Vec3::unit_y,
Vec3::unit_z ] };
}
// //
// Matrix3x3 Implementation // Matrix3x3 Implementation
// //
@ -337,7 +346,7 @@ pub impl Mat3: Matrix<float, Vec3> {
#[inline] #[inline]
pure fn is_identity() -> bool { pure fn is_identity() -> bool {
self.fuzzy_eq(&mat3_identity) self.fuzzy_eq(&Mat3::identity)
} }
#[inline] #[inline]
@ -366,7 +375,7 @@ pub impl Mat3: Matrix<float, Vec3> {
#[inline] #[inline]
pure fn is_rotated() -> bool { pure fn is_rotated() -> bool {
!self.fuzzy_eq(&mat3_identity) !self.fuzzy_eq(&Mat3::identity)
} }
} }
@ -472,15 +481,6 @@ pub impl Mat3: FuzzyEq {
// //
pub struct Mat4 { data:[Vec4 * 4] } pub struct Mat4 { data:[Vec4 * 4] }
pub const mat4_zero :Mat4 = Mat4 { data: [ vec4_zero,
vec4_zero,
vec4_zero,
vec4_zero ] };
pub const mat4_identity :Mat4 = Mat4 { data: [ vec4_unit_x,
vec4_unit_y,
vec4_unit_z,
vec4_unit_w ] };
// //
// Mat4 Constructor // Mat4 Constructor
// //
@ -503,6 +503,17 @@ pub pure fn Mat4_v(col0: &Vec4, col1: &Vec4, col2: &Vec4, col3: &Vec4) -> Mat4 {
Mat4 { data: [ *col0, *col1, *col2, *col3 ] } Mat4 { data: [ *col0, *col1, *col2, *col3 ] }
} }
pub mod Mat4 {
pub const zero :Mat4 = Mat4 { data: [ Vec4::zero,
Vec4::zero,
Vec4::zero,
Vec4::zero ] };
pub const identity :Mat4 = Mat4 { data: [ Vec4::unit_x,
Vec4::unit_y,
Vec4::unit_z,
Vec4::unit_w ] };
}
// //
// Matrix4x4 Implementation // Matrix4x4 Implementation
// //
@ -606,7 +617,7 @@ pub impl Mat4: Matrix<float, Vec4> {
#[inline] #[inline]
pure fn is_identity() -> bool { pure fn is_identity() -> bool {
self.fuzzy_eq(&mat4_identity) self.fuzzy_eq(&Mat4::identity)
} }
#[inline] #[inline]
@ -649,7 +660,7 @@ pub impl Mat4: Matrix<float, Vec4> {
#[inline] #[inline]
pure fn is_rotated() -> bool { pure fn is_rotated() -> bool {
!self.fuzzy_eq(&mat4_identity) !self.fuzzy_eq(&Mat4::identity)
} }
} }

View file

@ -43,9 +43,6 @@ pub trait Quaternion<T> {
// //
pub struct Quat { w: float, x: float, y: float, z: float } pub struct Quat { w: float, x: float, y: float, z: float }
pub const quat_zero :Quat = Quat { w: 0f, x: 0f, y: 0f, z: 0f };
pub const quat_identity :Quat = Quat { w: 1f, x: 0f, y: 0f, z: 0f };
// //
// Quat Constructor // Quat Constructor
// //
@ -54,6 +51,11 @@ pub pure fn Quat(w: float, x: float, y: float, z: float) -> Quat {
Quat { w: w, x: x, y: y, z: z } Quat { w: w, x: x, y: y, z: z }
} }
pub mod Quat {
pub const zero :Quat = Quat { w: 0f, x: 0f, y: 0f, z: 0f };
pub const identity :Quat = Quat { w: 1f, x: 0f, y: 0f, z: 0f };
}
// //
// Quaternion Implementation // Quaternion Implementation
// //

View file

@ -49,10 +49,10 @@ fn test_Mat2() {
// fuzzy_eq // fuzzy_eq
// eq // eq
assert mat2_identity.is_identity(); assert Mat2::identity.is_identity();
assert mat2_identity.is_symmetric(); assert Mat2::identity.is_symmetric();
assert mat2_identity.is_diagonal(); assert Mat2::identity.is_diagonal();
assert !mat2_identity.is_rotated(); assert !Mat2::identity.is_rotated();
assert !a.is_identity(); assert !a.is_identity();
assert !a.is_symmetric(); assert !a.is_symmetric();
@ -126,10 +126,10 @@ fn test_Mat3() {
// fuzzy_eq // fuzzy_eq
// eq // eq
assert mat3_identity.is_identity(); assert Mat3::identity.is_identity();
assert mat3_identity.is_symmetric(); assert Mat3::identity.is_symmetric();
assert mat3_identity.is_diagonal(); assert Mat3::identity.is_diagonal();
assert !mat3_identity.is_rotated(); assert !Mat3::identity.is_rotated();
assert !a.is_identity(); assert !a.is_identity();
assert !a.is_symmetric(); assert !a.is_symmetric();
@ -224,10 +224,10 @@ fn test_Mat4() {
// fuzzy_eq // fuzzy_eq
// eq // eq
assert mat4_identity.is_identity(); assert Mat4::identity.is_identity();
assert mat4_identity.is_symmetric(); assert Mat4::identity.is_symmetric();
assert mat4_identity.is_diagonal(); assert Mat4::identity.is_diagonal();
assert !mat4_identity.is_rotated(); assert !Mat4::identity.is_rotated();
assert !a.is_identity(); assert !a.is_identity();
assert !a.is_symmetric(); assert !a.is_symmetric();

View file

@ -13,8 +13,8 @@ fn test_Quat() {
assert a == Quat(1f, 2f, 3f, 4f); assert a == Quat(1f, 2f, 3f, 4f);
assert quat_zero == Quat(0f, 0f, 0f, 0f); assert Quat::zero == Quat(0f, 0f, 0f, 0f);
assert quat_identity == Quat(1f, 0f, 0f, 0f); assert Quat::identity == Quat(1f, 0f, 0f, 0f);
assert a.w == 1f; assert a.w == 1f;
assert a.x == 2f; assert a.x == 2f;

View file

@ -14,10 +14,10 @@ fn test_Vec2() {
assert a == Vec2(1f, 2f); assert a == Vec2(1f, 2f);
assert vec2_zero == Vec2(0f, 0f); assert Vec2::zero == Vec2(0f, 0f);
assert vec2_unit_x == Vec2(1f, 0f); assert Vec2::unit_x == Vec2(1f, 0f);
assert vec2_unit_y == Vec2(0f, 1f); assert Vec2::unit_y == Vec2(0f, 1f);
assert vec2_identity == Vec2(1f, 1f); assert Vec2::identity == Vec2(1f, 1f);
assert a.x == 1f; assert a.x == 1f;
assert a.y == 2f; assert a.y == 2f;
@ -63,11 +63,11 @@ fn test_Vec3() {
assert a == Vec3(1f, 2f, 3f); assert a == Vec3(1f, 2f, 3f);
assert vec3_zero == Vec3(0f, 0f, 0f); assert Vec3::zero == Vec3(0f, 0f, 0f);
assert vec3_unit_x == Vec3(1f, 0f, 0f); assert Vec3::unit_x == Vec3(1f, 0f, 0f);
assert vec3_unit_y == Vec3(0f, 1f, 0f); assert Vec3::unit_y == Vec3(0f, 1f, 0f);
assert vec3_unit_z == Vec3(0f, 0f, 1f); assert Vec3::unit_z == Vec3(0f, 0f, 1f);
assert vec3_identity == Vec3(1f, 1f, 1f); assert Vec3::identity == Vec3(1f, 1f, 1f);
assert a.x == 1f; assert a.x == 1f;
assert a.y == 2f; assert a.y == 2f;
@ -117,12 +117,12 @@ fn test_Vec4() {
assert a == Vec4(1f, 2f, 3f, 4f); assert a == Vec4(1f, 2f, 3f, 4f);
assert vec4_zero == Vec4(0f, 0f, 0f, 0f); assert Vec4::zero == Vec4(0f, 0f, 0f, 0f);
assert vec4_unit_x == Vec4(1f, 0f, 0f, 0f); assert Vec4::unit_x == Vec4(1f, 0f, 0f, 0f);
assert vec4_unit_y == Vec4(0f, 1f, 0f, 0f); assert Vec4::unit_y == Vec4(0f, 1f, 0f, 0f);
assert vec4_unit_z == Vec4(0f, 0f, 1f, 0f); assert Vec4::unit_z == Vec4(0f, 0f, 1f, 0f);
assert vec4_unit_w == Vec4(0f, 0f, 0f, 1f); assert Vec4::unit_w == Vec4(0f, 0f, 0f, 1f);
assert vec4_identity == Vec4(1f, 1f, 1f, 1f); assert Vec4::identity == Vec4(1f, 1f, 1f, 1f);
assert a.x == 1f; assert a.x == 1f;
assert a.y == 2f; assert a.y == 2f;

View file

@ -49,10 +49,6 @@ pub trait Vector3<T> {
// //
pub struct Vec2 { x: float, y: float } pub struct Vec2 { x: float, y: float }
pub const vec2_zero :Vec2 = Vec2 { x: 0f, y: 0f };
pub const vec2_unit_x :Vec2 = Vec2 { x: 1f, y: 0f };
pub const vec2_unit_y :Vec2 = Vec2 { x: 0f, y: 1f };
pub const vec2_identity :Vec2 = Vec2 { x: 1f, y: 1f };
// //
// Constructor // Constructor
@ -62,6 +58,13 @@ pub pure fn Vec2(x: float, y: float) -> Vec2 {
Vec2 { x: x, y: y } Vec2 { x: x, y: y }
} }
pub mod Vec2 {
pub const zero :Vec2 = Vec2 { x: 0f, y: 0f };
pub const unit_x :Vec2 = Vec2 { x: 1f, y: 0f };
pub const unit_y :Vec2 = Vec2 { x: 0f, y: 1f };
pub const identity :Vec2 = Vec2 { x: 1f, y: 1f };
}
pub impl Vec2: Vector<float> { pub impl Vec2: Vector<float> {
#[inline] #[inline]
static pure fn dim() -> uint { 2 } static pure fn dim() -> uint { 2 }
@ -166,8 +169,8 @@ pub impl Vec2: Index<uint, float> {
pub impl Vec2: Abs { pub impl Vec2: Abs {
#[inline] #[inline]
pure fn abs() -> Vec2 { pure fn abs() -> Vec2 {
Vec2(abs(self[0]), Vec2(abs(&self[0]),
abs(self[1])) abs(&self[1]))
} }
} }
@ -214,12 +217,6 @@ pub impl Vec2: ToStr {
// //
pub struct Vec3 { x: float, y: float, z: float } pub struct Vec3 { x: float, y: float, z: float }
pub const vec3_zero :Vec3 = Vec3 { x: 0f, y: 0f, z: 0f };
pub const vec3_unit_x :Vec3 = Vec3 { x: 1f, y: 0f, z: 0f };
pub const vec3_unit_y :Vec3 = Vec3 { x: 0f, y: 1f, z: 0f };
pub const vec3_unit_z :Vec3 = Vec3 { x: 0f, y: 0f, z: 1f };
pub const vec3_identity :Vec3 = Vec3 { x: 1f, y: 1f, z: 1f };
// //
// Constructor // Constructor
// //
@ -228,6 +225,14 @@ pub pure fn Vec3(x: float, y: float, z: float) -> Vec3 {
Vec3 { x: x, y: y, z: z } Vec3 { x: x, y: y, z: z }
} }
pub mod Vec3 {
pub const zero :Vec3 = Vec3 { x: 0f, y: 0f, z: 0f };
pub const unit_x :Vec3 = Vec3 { x: 1f, y: 0f, z: 0f };
pub const unit_y :Vec3 = Vec3 { x: 0f, y: 1f, z: 0f };
pub const unit_z :Vec3 = Vec3 { x: 0f, y: 0f, z: 1f };
pub const identity :Vec3 = Vec3 { x: 1f, y: 1f, z: 1f };
}
pub impl Vec3: Vector3<float> { pub impl Vec3: Vector3<float> {
#[inline] #[inline]
fn cross(other: &Vec3) -> Vec3 { fn cross(other: &Vec3) -> Vec3 {
@ -351,9 +356,9 @@ pub impl Vec3: Index<uint, float> {
pub impl Vec3: Abs { pub impl Vec3: Abs {
#[inline] #[inline]
pure fn abs() -> Vec3 { pure fn abs() -> Vec3 {
Vec3(abs(self[0]), Vec3(abs(&self[0]),
abs(self[1]), abs(&self[1]),
abs(self[2])) abs(&self[2]))
} }
} }
@ -401,12 +406,14 @@ pub impl Vec3: ToStr {
// //
pub struct Vec4 { x: float, y: float, z: float, w: float } pub struct Vec4 { x: float, y: float, z: float, w: float }
pub const vec4_zero :Vec4 = Vec4 { x: 0f, y: 0f, z: 0f, w: 0f }; pub mod Vec4 {
pub const vec4_unit_x :Vec4 = Vec4 { x: 1f, y: 0f, z: 0f, w: 0f }; pub const zero :Vec4 = Vec4 { x: 0f, y: 0f, z: 0f, w: 0f };
pub const vec4_unit_y :Vec4 = Vec4 { x: 0f, y: 1f, z: 0f, w: 0f }; pub const unit_x :Vec4 = Vec4 { x: 1f, y: 0f, z: 0f, w: 0f };
pub const vec4_unit_z :Vec4 = Vec4 { x: 0f, y: 0f, z: 1f, w: 0f }; pub const unit_y :Vec4 = Vec4 { x: 0f, y: 1f, z: 0f, w: 0f };
pub const vec4_unit_w :Vec4 = Vec4 { x: 0f, y: 0f, z: 0f, w: 1f }; pub const unit_z :Vec4 = Vec4 { x: 0f, y: 0f, z: 1f, w: 0f };
pub const vec4_identity :Vec4 = Vec4 { x: 1f, y: 1f, z: 1f, w: 1f }; pub const unit_w :Vec4 = Vec4 { x: 0f, y: 0f, z: 0f, w: 1f };
pub const identity :Vec4 = Vec4 { x: 1f, y: 1f, z: 1f, w: 1f };
}
// //
// Constructor // Constructor
@ -542,10 +549,10 @@ pub impl Vec4: Index<uint, float> {
pub impl Vec4: Abs { pub impl Vec4: Abs {
#[inline] #[inline]
pure fn abs() -> Vec4 { pure fn abs() -> Vec4 {
Vec4(abs(self[0]), Vec4(abs(&self[0]),
abs(self[1]), abs(&self[1]),
abs(self[2]), abs(&self[2]),
abs(self[3])) abs(&self[3]))
} }
} }