Replaced data arrays with component fields

This commit is contained in:
Brendan Zabarauskas 2012-10-28 14:26:37 +10:00
parent 0cef6b55b8
commit 2438675384
6 changed files with 120 additions and 186 deletions

View file

@ -375,9 +375,9 @@ pub impl Mat3: Matrix<float, Vec3> {
pub impl Mat3: Matrix3<Vec3> {
#[inline]
pure fn scale(vec: &Vec3) -> Mat3 {
self.mul_m(&Mat3(vec.x(), 0f, 0f,
0f, vec.y(), 0f,
0f, 0f, vec.z()))
self.mul_m(&Mat3(vec.x, 0f, 0f,
0f, vec.y, 0f,
0f, 0f, vec.z))
}
#[inline]
@ -658,9 +658,9 @@ pub impl Mat4: Matrix<float, Vec4> {
pub impl Mat4: Matrix4<Vec3, Vec4> {
#[inline]
pure fn scale(vec: &Vec3) -> Mat4 {
self.mul_m(&Mat4(vec.x(), 0f, 0f, 0f,
0f, vec.y(), 0f, 0f,
0f, 0f, vec.z(), 0f,
self.mul_m(&Mat4(vec.x, 0f, 0f, 0f,
0f, vec.y, 0f, 0f,
0f, 0f, vec.z, 0f,
0f, 0f, 0f, 1f))
}
@ -669,9 +669,9 @@ pub impl Mat4: Matrix4<Vec3, Vec4> {
Mat4_v(&self[0],
&self[1],
&self[2],
&Vec4(self[3][0] + vec.x(),
self[3][1] + vec.y(),
self[3][2] + vec.z(),
&Vec4(self[3][0] + vec.x,
self[3][1] + vec.y,
self[3][2] + vec.z,
self[3][3]))
}
}

View file

@ -14,11 +14,6 @@ use vec::Vec3;
pub trait Quaternion<T> {
pure fn dim() -> uint;
pure fn w() -> T;
pure fn x() -> T;
pure fn y() -> T;
pure fn z() -> T;
pure fn mul_f(value: T) -> self;
pure fn div_f(value: T) -> self;
@ -47,17 +42,17 @@ pub trait Quaternion<T> {
//
// Quat struct definition
//
pub struct Quat { data:[float * 4] }
pub struct Quat { w: float, x: float, y: float, z: float }
pub const quat_zero :Quat = Quat { data: [ 0f, 0f, 0f, 0f ] };
pub const quat_identity :Quat = Quat { data: [ 1f, 0f, 0f, 0f ] };
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
//
#[inline]
pub pure fn Quat(w: float, x: float, y: float, z: float) -> Quat {
Quat { data: [ w, x, y, z ] }
Quat { w: w, x: x, y: y, z: z }
}
//
@ -67,11 +62,6 @@ pub impl Quat: Quaternion<float> {
#[inline]
pure fn dim() -> uint { 4 }
#[inline] pure fn w() -> float { self.data[0] }
#[inline] pure fn x() -> float { self.data[1] }
#[inline] pure fn y() -> float { self.data[2] }
#[inline] pure fn z() -> float { self.data[3] }
#[inline]
pure fn mul_f(value: float) -> Quat {
Quat(self[0] * value,
@ -106,10 +96,10 @@ pub impl Quat: Quaternion<float> {
#[inline]
pure fn mul_q(other: &Quat) -> Quat {
Quat(self.w()*other.w() - self.x()*other.x() - self.y()*other.y() - self.z()*other.z(),
self.w()*other.x() + self.x()*other.w() + self.y()*other.z() - self.z()*other.y(),
self.w()*other.y() + self.y()*other.w() + self.z()*other.x() - self.x()*other.z(),
self.w()*other.z() + self.z()*other.w() + self.x()*other.y() - self.y()*other.x())
Quat(self.w * other.w - self.x * other.x - self.y * other.y - self.z * other.z,
self.w * other.x + self.x * other.w + self.y * other.z - self.z * other.y,
self.w * other.y + self.y * other.w + self.z * other.x - self.x * other.z,
self.w * other.z + self.z * other.w + self.x * other.y - self.y * other.x)
}
#[inline]
@ -122,7 +112,7 @@ pub impl Quat: Quaternion<float> {
#[inline]
pure fn conjugate() -> Quat {
Quat(self.w(), -self.x(), -self.y(), -self.z())
Quat(self.w, -self.x, -self.y, -self.z)
}
#[inline]
@ -132,10 +122,10 @@ pub impl Quat: Quaternion<float> {
#[inline]
pure fn magnitude2() -> float {
self.w() * self.w() +
self.x() * self.x() +
self.y() * self.y() +
self.z() * self.z()
self.w * self.w +
self.x * self.x +
self.y * self.y +
self.z * self.z
}
#[inline]
@ -145,21 +135,21 @@ pub impl Quat: Quaternion<float> {
#[inline]
pure fn to_Mat3() -> Mat3 {
let x2 = self.x() + self.x();
let y2 = self.y() + self.y();
let z2 = self.z() + self.z();
let x2 = self.x + self.x;
let y2 = self.y + self.y;
let z2 = self.z + self.z;
let xx2 = x2 * self.x();
let xy2 = x2 * self.y();
let xz2 = x2 * self.z();
let xx2 = x2 * self.x;
let xy2 = x2 * self.y;
let xz2 = x2 * self.z;
let yy2 = y2 * self.y();
let yz2 = y2 * self.z();
let zz2 = z2 * self.z();
let yy2 = y2 * self.y;
let yz2 = y2 * self.z;
let zz2 = z2 * self.z;
let wy2 = y2 * self.w();
let wz2 = z2 * self.w();
let wx2 = x2 * self.w();
let wy2 = y2 * self.w;
let wz2 = z2 * self.w;
let wx2 = x2 * self.w;
return Mat3(1f - yy2 - zz2, xy2 - wz2, xz2 + wy2,
xy2 + wz2, 1f - xx2 - zz2, yz2 - wx2,
@ -175,7 +165,13 @@ pub impl Quat: Quaternion<float> {
pub impl Quat: Index<uint, float> {
#[inline]
pure fn index(i: uint) -> float {
self.data[i]
match i {
0 => self.w,
1 => self.x,
2 => self.y,
3 => self.z,
_ => fail(~"Vector index out of bounds.")
}
}
}
@ -213,6 +209,6 @@ pub impl Quat: FuzzyEq {
//
pub impl Quat: ToStr {
pure fn to_str() -> ~str {
fmt!("Quat[ %f, %f, %f, %f ]", self.w(), self.x(), self.y(), self.z())
fmt!("Quat[ %f, %f, %f, %f ]", self.w, self.x, self.y, self.z)
}
}

View file

@ -41,47 +41,10 @@ pub trait Vector<T> {
pub trait Vector2<T> {
// static pure fn _new(x: float, y: float) -> self;
// This is where I wish rust had properties ;)
pure fn x() -> T;
pure fn y() -> T;
// static pure fn unit_x() -> self;
// static pure fn unit_y() -> self;
}
pub trait Vector3<T> {
// error: duplicate function definition
// static pure fn _new(x: float, y: float, z: float) -> self;
pure fn x() -> T;
pure fn y() -> T;
pure fn z() -> T;
// static pure fn unit_x() -> self;
// static pure fn unit_y() -> self;
// static pure fn unit_z() -> self;
fn cross(other: &self) -> self;
}
pub trait Vector4<T> {
// error: duplicate function definition
// static pure fn _new(x: float, y: float, z: float, w: float) -> self;
pure fn x() -> T;
pure fn y() -> T;
pure fn z() -> T;
pure fn w() -> T;
// static pure fn unit_x() -> self;
// static pure fn unit_y() -> self;
// static pure fn unit_z() -> self;
// static pure fn unit_w() -> self;
}
@ -90,33 +53,19 @@ pub trait Vector4<T> {
//
// Vec2
//
pub struct Vec2 { data:[float * 2] }
pub struct Vec2 { x: float, y: float }
pub const vec2_zero :Vec2 = Vec2 { data: [ 0f, 0f ] };
pub const vec2_unit_x :Vec2 = Vec2 { data: [ 1f, 0f ] };
pub const vec2_unit_y :Vec2 = Vec2 { data: [ 0f, 1f ] };
pub const vec2_identity :Vec2 = Vec2 { data: [ 1f, 1f ] };
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
//
#[inline]
pub pure fn Vec2(x: float, y: float) -> Vec2 {
Vec2 { data: [ x, y ] }
}
pub impl Vec2: Vector2<float> {
// #[inline]
// static pure fn _new(x: float, y: float) -> Vec2 {
// Vec2 { data: [ x, y ] }
// }
#[inline] pure fn x() -> float { self.data[0] }
#[inline] pure fn y() -> float { self.data[1] }
// #[inline] static pure fn unit_x() -> Vec2 { Vec2(1f, 0f) }
// #[inline] static pure fn unit_y() -> Vec2 { Vec2(0f, 1f) }
// #[inline] static pure fn unit_z() -> Vec2 { Vec2(0f, 0f) }
Vec2 { x: x, y: y }
}
pub impl Vec2: Vector<float> {
@ -218,7 +167,11 @@ pub impl Vec2: Vector<float> {
pub impl Vec2: Index<uint, float> {
#[inline]
pure fn index(i: uint) -> float {
self.data[i]
match i {
0 => self.x,
1 => self.y,
_ => fail(~"Vector index out of bounds.")
}
}
}
@ -263,32 +216,23 @@ pub impl Vec2: ToStr {
//
// Vec3
//
pub struct Vec3 { data:[float * 3] }
pub struct Vec3 { x: float, y: float, z: float }
pub const vec3_zero :Vec3 = Vec3 { data: [ 0f, 0f, 0f ] };
pub const vec3_unit_x :Vec3 = Vec3 { data: [ 1f, 0f, 0f ] };
pub const vec3_unit_y :Vec3 = Vec3 { data: [ 0f, 1f, 0f ] };
pub const vec3_unit_z :Vec3 = Vec3 { data: [ 0f, 0f, 1f ] };
pub const vec3_identity :Vec3 = Vec3 { data: [ 1f, 1f, 1f ] };
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
//
#[inline]
pub pure fn Vec3(x: float, y: float, z: float) -> Vec3 {
Vec3 { data: [ x, y, z ] }
Vec3 { x: x, y: y, z: z }
}
pub impl Vec3: Vector3<float> {
// #[inline]
// static pure fn _new(x: float, y: float, z: float) -> Vec3 {
// Vec2 { data: [ x, y, z ] }
// }
#[inline] pure fn x() -> float { self.data[0] }
#[inline] pure fn y() -> float { self.data[1] }
#[inline] pure fn z() -> float { self.data[2] }
#[inline]
fn cross(other: &Vec3) -> Vec3 {
Vec3((self[1] * other[2]) - (self[2] * other[1]),
@ -412,7 +356,12 @@ pub impl Vec3: Vector<float> {
pub impl Vec3: Index<uint, float> {
#[inline]
pure fn index(i: uint) -> float {
self.data[i]
match i {
0 => self.x,
1 => self.y,
2 => self.z,
_ => fail(~"Vector index out of bounds.")
}
}
}
@ -458,38 +407,21 @@ pub impl Vec3: ToStr {
//
// Vec4
//
pub struct Vec4 { data:[float * 4] }
pub struct Vec4 { x: float, y: float, z: float, w: float }
pub const vec4_zero :Vec4 = Vec4 { data: [ 0f, 0f, 0f, 0f ] };
pub const vec4_unit_x :Vec4 = Vec4 { data: [ 1f, 0f, 0f, 0f ] };
pub const vec4_unit_y :Vec4 = Vec4 { data: [ 0f, 1f, 0f, 0f ] };
pub const vec4_unit_z :Vec4 = Vec4 { data: [ 0f, 0f, 1f, 0f ] };
pub const vec4_unit_w :Vec4 = Vec4 { data: [ 0f, 0f, 0f, 1f ] };
pub const vec4_identity :Vec4 = Vec4 { data: [ 1f, 1f, 1f, 1f ] };
pub const vec4_zero :Vec4 = Vec4 { x: 0f, y: 0f, z: 0f, w: 0f };
pub const vec4_unit_x :Vec4 = Vec4 { x: 1f, y: 0f, z: 0f, w: 0f };
pub const vec4_unit_y :Vec4 = Vec4 { x: 0f, y: 1f, z: 0f, w: 0f };
pub const vec4_unit_z :Vec4 = Vec4 { x: 0f, y: 0f, z: 1f, w: 0f };
pub const vec4_unit_w :Vec4 = Vec4 { x: 0f, y: 0f, z: 0f, w: 1f };
pub const vec4_identity :Vec4 = Vec4 { x: 1f, y: 1f, z: 1f, w: 1f };
//
// Constructor
//
#[inline]
pub pure fn Vec4(x: float, y: float, z: float, w: float) -> Vec4 {
Vec4 { data: [ x, y, z, w ] }
}
pub impl Vec4: Vector4<float> {
// #[inline]
// static pure fn _new(x: float, y: float, z: float, w: float) -> Vec3 {
// Vec2 { data: [ x, y, z, w ] }
// }
#[inline] pure fn x() -> float { self.data[0] }
#[inline] pure fn y() -> float { self.data[1] }
#[inline] pure fn z() -> float { self.data[2] }
#[inline] pure fn w() -> float { self.data[3] }
// #[inline] static pure fn unit_x() -> Vec4 { Vec4(1f, 0f, 0f, 0f) }
// #[inline] static pure fn unit_y() -> Vec4 { Vec4(0f, 1f, 0f, 0f) }
// #[inline] static pure fn unit_z() -> Vec4 { Vec4(0f, 0f, 1f, 0f) }
// #[inline] static pure fn unit_w() -> Vec4 { Vec4(0f, 0f, 0f, 1f) }
Vec4 { x: x, y: y, z: z, w: w }
}
pub impl Vec4: Vector<float> {
@ -615,7 +547,13 @@ pub impl Vec4: Vector<float> {
pub impl Vec4: Index<uint, float> {
#[inline]
pure fn index(i: uint) -> float {
self.data[i]
match i {
0 => self.x,
1 => self.y,
2 => self.z,
3 => self.w,
_ => fail(~"Vector index out of bounds.")
}
}
}

View file

@ -5,10 +5,10 @@ use omath::vec::*;
#[test]
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 a = Mat2 { data: [ Vec2 { x: 1f, y: 3f },
Vec2 { x: 2f, y: 4f } ] };
let b = Mat2 { data: [ Vec2 { x: 2f, y: 4f },
Vec2 { x: 3f, y: 5f } ] };
let v1 = Vec2(1f, 2f);
let f1 = 0.5f;
@ -69,12 +69,12 @@ fn test_Mat2() {
#[test]
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 a = Mat3 { data: [ Vec3 { x: 1f, y: 4f, z: 7f },
Vec3 { x: 2f, y: 5f, z: 8f },
Vec3 { x: 3f, y: 6f, z: 9f } ] };
let b = Mat3 { data: [ Vec3 { x: 2f, y: 5f, z: 8f },
Vec3 { x: 3f, y: 6f, z: 9f },
Vec3 { x: 4f, y: 7f, z: 10f } ] };
let v1 = Vec3(1f, 2f, 3f);
let f1 = 0.5f;
@ -154,14 +154,14 @@ fn test_Mat3() {
#[test]
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 a = Mat4 { data: [ Vec4 { x: 1f, y: 5f, z: 9f, w: 13f },
Vec4 { x: 2f, y: 6f, z: 10f, w: 14f },
Vec4 { x: 3f, y: 7f, z: 11f, w: 15f },
Vec4 { x: 4f, y: 8f, z: 12f, w: 16f } ] };
let b = Mat4 { data: [ Vec4 { x: 2f, y: 6f, z: 10f, w: 14f },
Vec4 { x: 3f, y: 7f, z: 11f, w: 15f },
Vec4 { x: 4f, y: 8f, z: 12f, w: 16f },
Vec4 { x: 5f, y: 9f, z: 13f, w: 17f } ] };
let v1 = Vec4(1f, 2f, 3f, 4f);
let f1 = 0.5f;

View file

@ -6,7 +6,7 @@ use omath::vec::*;
#[test]
fn test_Quat() {
let a = Quat { data: [ 1f, 2f, 3f, 4f ] };
let a = Quat { w: 1f, x: 2f, y: 3f, z: 4f };
// let b = Quat { data: [ 5f, 6f, 7f, 8f ] };
// let f1 = 1.5f;
// let f2 = 0.5f;
@ -16,14 +16,14 @@ fn test_Quat() {
assert quat_zero == Quat(0f, 0f, 0f, 0f);
assert quat_identity == Quat(1f, 0f, 0f, 0f);
assert a.w == 1f;
assert a.x == 2f;
assert a.y == 3f;
assert a.z == 4f;
assert a[0] == 1f;
assert a[1] == 2f;
assert a[2] == 3f;
assert a[3] == 4f;
assert a.w() == 1f;
assert a.x() == 2f;
assert a.y() == 3f;
assert a.z() == 4f;
// TODO
}

View file

@ -7,8 +7,8 @@ use omath::vec::*;
fn test_Vec2() {
// assert Vec2::dim == 2;
let a = Vec2 { data: [ 1f, 2f ] };
let b = Vec2 { data: [ 3f, 4f ] };
let a = Vec2 { x: 1f, y: 2f };
let b = Vec2 { x: 3f, y: 4f };
let f1 = 1.5f;
let f2 = 0.5f;
@ -19,10 +19,10 @@ fn test_Vec2() {
assert vec2_unit_y == Vec2(0f, 1f);
assert vec2_identity == Vec2(1f, 1f);
assert a.x == 1f;
assert a.y == 2f;
assert a[0] == 1f;
assert a[1] == 2f;
assert a.x() == 1f;
assert a.y() == 2f;
assert -a == Vec2(-1f, -2f);
assert a.neg() == Vec2(-1f, -2f);
@ -56,8 +56,8 @@ fn test_Vec2() {
fn test_Vec3() {
// assert Vec3::dim == 3;
let a = Vec3 { data: [ 1f, 2f, 3f ] };
let b = Vec3 { data: [ 4f, 5f, 6f ] };
let a = Vec3 { x: 1f, y: 2f, z: 3f };
let b = Vec3 { x: 4f, y: 5f, z: 6f };
let f1 = 1.5f;
let f2 = 0.5f;
@ -69,12 +69,12 @@ fn test_Vec3() {
assert vec3_unit_z == Vec3(0f, 0f, 1f);
assert vec3_identity == Vec3(1f, 1f, 1f);
assert a.x == 1f;
assert a.y == 2f;
assert a.z == 3f;
assert a[0] == 1f;
assert a[1] == 2f;
assert a[2] == 3f;
assert a.x() == 1f;
assert a.y() == 2f;
assert a.z() == 3f;
assert a.cross(&b) == Vec3(-3f, 6f, -3f);
@ -110,8 +110,8 @@ fn test_Vec3() {
fn test_Vec4() {
// assert Vec4::dim == 4;
let a = Vec4 { data: [ 1f, 2f, 3f, 4f ] };
let b = Vec4 { data: [ 5f, 6f, 7f, 8f ] };
let a = Vec4 { x: 1f, y: 2f, z: 3f, w: 4f };
let b = Vec4 { x: 5f, y: 6f, z: 7f, w: 8f };
let f1 = 1.5f;
let f2 = 0.5f;
@ -124,14 +124,14 @@ fn test_Vec4() {
assert vec4_unit_w == Vec4(0f, 0f, 0f, 1f);
assert vec4_identity == Vec4(1f, 1f, 1f, 1f);
assert a.x == 1f;
assert a.y == 2f;
assert a.z == 3f;
assert a.w == 4f;
assert a[0] == 1f;
assert a[1] == 2f;
assert a[2] == 3f;
assert a[3] == 4f;
assert a.x() == 1f;
assert a.y() == 2f;
assert a.z() == 3f;
assert a.w() == 4f;
assert -a == Vec4(-1f, -2f, -3f, -4f);
assert a.neg() == Vec4(-1f, -2f, -3f, -4f);