From a06d2cff54b6a9a94a0b512aa15561cdc941b3ff Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Tue, 20 Nov 2012 17:58:24 +1000 Subject: [PATCH] Make vector constructors static functions --- src/funs/test/test_transform.rs | 9 +- src/gltypes.rs | 62 +++++++--- src/test/test_vec.rs | 36 +++--- src/vec.rs | 213 ++++++++++---------------------- 4 files changed, 134 insertions(+), 186 deletions(-) diff --git a/src/funs/test/test_transform.rs b/src/funs/test/test_transform.rs index c25cbfb..d12e325 100644 --- a/src/funs/test/test_transform.rs +++ b/src/funs/test/test_transform.rs @@ -6,7 +6,8 @@ use vec::{Vec3, Vec4}; fn test_mat4_from_rotation() { { let pos = Vec4::new(1f32, 0f32, 0f32, 1f32); - let tform = mat4_from_rotation(180f32, Vec3::unit_z()); + // let tform = mat4_from_rotation(180f32, Vec3::unit_z()); + let tform = mat4_from_rotation(180f32, Vec3::new(0f32, 0f32, 1f32)); let newpos = tform.mul_v(&pos); let expected = Vec4::new(-1f32, 0f32, 0f32, 1f32); @@ -16,8 +17,10 @@ fn test_mat4_from_rotation() { { let pos = Vec4::new(4f32, 0f32, 0f32, 1f32); - let tform_a = mat4_from_rotation(90f32, Vec3::unit_y()); - let tform_b = mat4_from_rotation(90f32, -Vec3::unit_y()); + // let tform_a = mat4_from_rotation(90f32, Vec3::unit_y()); + // let tform_b = mat4_from_rotation(90f32, -Vec3::unit_y()); + let tform_a = mat4_from_rotation(90f32, Vec3::new(0f32, 1f32, 0f32)); + let tform_b = mat4_from_rotation(90f32, -Vec3::new(0f32, 1f32, 0f32)); let newpos_a = tform_a.mul_v(&pos); let newpos_b = tform_b.mul_v(&pos); diff --git a/src/gltypes.rs b/src/gltypes.rs index 309ffa4..2d5489a 100644 --- a/src/gltypes.rs +++ b/src/gltypes.rs @@ -2,7 +2,7 @@ pub use mat::{Mat2, Mat3, Mat4}; pub use vec::{Vec2, Vec3, Vec4}; pub use quat::Quat; -use vec::NumericVector; +use vec::{Vector, NumericVector}; use mat::{NumericMatrix, NumericMatrix_NxN}; // Vector aliases @@ -35,81 +35,111 @@ pub type uvec4 = Vec4; /// a four-component unsigned integer ve // as opposed to: let v: dvec4 = NumericVector::identity(); // -pub impl vec2 { - #[inline(always)] static pure fn identity() -> vec2 { NumericVector::identity() } - #[inline(always)] static pure fn zero() -> vec2 { NumericVector::zero() } +pub impl vec2 { + #[inline(always)] static pure fn new(x: f32, y: f32) -> vec2 { Vec2::new(x, y) } + #[inline(always)] static pure fn from_value(v: f32) -> vec2 { Vector::from_value(v) } + #[inline(always)] static pure fn identity() -> vec2 { NumericVector::identity() } + #[inline(always)] static pure fn zero() -> vec2 { NumericVector::zero() } } -pub impl vec3 { - #[inline(always)] static pure fn identity() -> vec3 { NumericVector::identity() } - #[inline(always)] static pure fn zero() -> vec3 { NumericVector::zero() } +pub impl vec3 { + #[inline(always)] static pure fn new(x: f32, y: f32, z: f32) -> vec3 { Vec3::new(x, y, z) } + #[inline(always)] static pure fn from_value(v: f32) -> vec3 { Vector::from_value(v) } + #[inline(always)] static pure fn identity() -> vec3 { NumericVector::identity() } + #[inline(always)] static pure fn zero() -> vec3 { NumericVector::zero() } } -pub impl vec4 { - #[inline(always)] static pure fn identity() -> vec4 { NumericVector::identity() } - #[inline(always)] static pure fn zero() -> vec4 { NumericVector::zero() } +pub impl vec4 { + #[inline(always)] static pure fn new(x: f32, y: f32, z: f32, w: f32) -> vec4 { Vec4::new(x, y, z, w) } + #[inline(always)] static pure fn from_value(v: f32) -> vec4 { Vector::from_value(v) } + #[inline(always)] static pure fn identity() -> vec4 { NumericVector::identity() } + #[inline(always)] static pure fn zero() -> vec4 { NumericVector::zero() } } pub impl dvec2 { + #[inline(always)] static pure fn new(x: f64, y: f64) -> dvec2 { Vec2::new(x, y) } + #[inline(always)] static pure fn from_value(v: f64) -> dvec2 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> dvec2 { NumericVector::identity() } - #[inline(always)] static pure fn zero() -> dvec2 { NumericVector::zero() } + #[inline(always)] static pure fn zero() -> dvec2 { NumericVector::zero() } } pub impl dvec3 { + #[inline(always)] static pure fn new(x: f64, y: f64, z: f64) -> dvec3 { Vec3::new(x, y, z) } + #[inline(always)] static pure fn from_value(v: f64) -> dvec3 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> dvec3 { NumericVector::identity() } - #[inline(always)] static pure fn zero() -> dvec3 { NumericVector::zero() } + #[inline(always)] static pure fn zero() -> dvec3 { NumericVector::zero() } } pub impl dvec4 { + #[inline(always)] static pure fn new(x: f64, y: f64, z: f64, w: f64) -> dvec4 { Vec4::new(x, y, z, w) } + #[inline(always)] static pure fn from_value(v: f64) -> dvec4 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> dvec4 { NumericVector::identity() } - #[inline(always)] static pure fn zero() -> dvec4 { NumericVector::zero() } + #[inline(always)] static pure fn zero() -> dvec4 { NumericVector::zero() } } pub impl bvec2 { + #[inline(always)] static pure fn new(x: bool, y: bool) -> bvec2 { Vec2::new(x, y) } + #[inline(always)] static pure fn from_value(v: bool) -> bvec2 { Vector::from_value(v) } // #[inline(always)] static pure fn identity() -> bvec2 { NumericVector::identity() } - // #[inline(always)] static pure fn zero() -> bvec2 { NumericVector::zero() } + // #[inline(always)] static pure fn zero() -> bvec2 { NumericVector::zero() } } pub impl bvec3 { + #[inline(always)] static pure fn new(x: bool, y: bool, z: bool) -> bvec3 { Vec3::new(x, y, z) } + #[inline(always)] static pure fn from_value(v: bool) -> bvec3 { Vector::from_value(v) } // #[inline(always)] static pure fn identity() -> bvec3 { NumericVector::identity() } - // #[inline(always)] static pure fn zero() -> bvec3 { NumericVector::zero() } + // #[inline(always)] static pure fn zero() -> bvec3 { NumericVector::zero() } } pub impl bvec4 { + #[inline(always)] static pure fn new(x: bool, y: bool, z: bool, w: bool) -> bvec4 { Vec4::new(x, y, z, w) } + #[inline(always)] static pure fn from_value(v: bool) -> bvec4 { Vector::from_value(v) } // #[inline(always)] static pure fn identity() -> bvec4 { NumericVector::identity() } - // #[inline(always)] static pure fn zero() -> bvec4 { NumericVector::zero() } + // #[inline(always)] static pure fn zero() -> bvec4 { NumericVector::zero() } } pub impl ivec2 { + #[inline(always)] static pure fn new(x: i32, y: i32) -> ivec2 { Vec2::new(x, y) } + #[inline(always)] static pure fn from_value(v: i32) -> ivec2 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> ivec2 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> ivec2 { NumericVector::zero() } } pub impl ivec3 { + #[inline(always)] static pure fn new(x: i32, y: i32, z: i32) -> ivec3 { Vec3::new(x, y, z) } + #[inline(always)] static pure fn from_value(v: i32) -> ivec3 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> ivec3 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> ivec3 { NumericVector::zero() } } pub impl ivec4 { + #[inline(always)] static pure fn new(x: i32, y: i32, z: i32, w: i32) -> ivec4 { Vec4::new(x, y, z, w) } + #[inline(always)] static pure fn from_value(v: i32) -> ivec4 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> ivec4 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> ivec4 { NumericVector::zero() } } pub impl uvec2 { + #[inline(always)] static pure fn new(x: u32, y: u32) -> uvec2 { Vec2::new(x, y) } + #[inline(always)] static pure fn from_value(v: u32) -> uvec2 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> uvec2 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> uvec2 { NumericVector::zero() } } pub impl uvec3 { + #[inline(always)] static pure fn new(x: u32, y: u32, z: u32) -> uvec3 { Vec3::new(x, y, z) } + #[inline(always)] static pure fn from_value(v: u32) -> uvec3 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> uvec3 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> uvec3 { NumericVector::zero() } } pub impl uvec4 { + #[inline(always)] static pure fn new(x: u32, y: u32, z: u32, w: u32) -> uvec4 { Vec4::new(x, y, z, w) } + #[inline(always)] static pure fn from_value(v: u32) -> uvec4 { Vector::from_value(v) } #[inline(always)] static pure fn identity() -> uvec4 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> uvec4 { NumericVector::zero() } } diff --git a/src/test/test_vec.rs b/src/test/test_vec.rs index db7950f..1d2b94d 100644 --- a/src/test/test_vec.rs +++ b/src/test/test_vec.rs @@ -13,12 +13,12 @@ fn test_Vec2() { let f2 = 0.5f; assert Vec2::new(1f, 2f) == a; - assert Vec2::from_value(1f32) == Vec2::new(1f32, 1f32); + // assert Vec2::from_value(1f32) == Vec2::new(1f32, 1f32); - assert Vec2::zero() == Vec2::new(0f, 0f); - assert Vec2::unit_x() == Vec2::new(1f, 0f); - assert Vec2::unit_y() == Vec2::new(0f, 1f); - assert Vec2::identity() == Vec2::new(1f, 1f); + // assert Vec2::zero() == Vec2::new(0f, 0f); + // assert Vec2::unit_x() == Vec2::new(1f, 0f); + // assert Vec2::unit_y() == Vec2::new(0f, 1f); + // assert Vec2::identity() == Vec2::new(1f, 1f); assert a.x == 1f; assert a.y == 2f; @@ -74,13 +74,13 @@ fn test_Vec3() { let f2 = 0.5f; assert Vec3::new(1f, 2f, 3f) == a; - assert Vec3::from_value(1f32) == Vec3::new(1f32, 1f32, 1f32); + // assert Vec3::from_value(1f32) == Vec3::new(1f32, 1f32, 1f32); - assert Vec3::zero() == Vec3::new(0f, 0f, 0f); - assert Vec3::unit_x() == Vec3::new(1f, 0f, 0f); - assert Vec3::unit_y() == Vec3::new(0f, 1f, 0f); - assert Vec3::unit_z() == Vec3::new(0f, 0f, 1f); - assert Vec3::identity() == Vec3::new(1f, 1f, 1f); + // assert Vec3::zero() == Vec3::new(0f, 0f, 0f); + // assert Vec3::unit_x() == Vec3::new(1f, 0f, 0f); + // assert Vec3::unit_y() == Vec3::new(0f, 1f, 0f); + // assert Vec3::unit_z() == Vec3::new(0f, 0f, 1f); + // assert Vec3::identity() == Vec3::new(1f, 1f, 1f); assert a.x == 1f; assert a.y == 2f; @@ -140,14 +140,14 @@ fn test_Vec4() { let f2 = 0.5f; assert Vec4::new(1f, 2f, 3f, 4f) == a; - assert Vec4::from_value(1f32) == Vec4::new(1f32, 1f32, 1f32, 1f32); + // assert Vec4::from_value(1f32) == Vec4::new(1f32, 1f32, 1f32, 1f32); - assert Vec4::zero() == Vec4::new(0f, 0f, 0f, 0f); - assert Vec4::unit_x() == Vec4::new(1f, 0f, 0f, 0f); - assert Vec4::unit_y() == Vec4::new(0f, 1f, 0f, 0f); - assert Vec4::unit_z() == Vec4::new(0f, 0f, 1f, 0f); - assert Vec4::unit_w() == Vec4::new(0f, 0f, 0f, 1f); - assert Vec4::identity() == Vec4::new(1f, 1f, 1f, 1f); + // assert Vec4::zero() == Vec4::new(0f, 0f, 0f, 0f); + // assert Vec4::unit_x() == Vec4::new(1f, 0f, 0f, 0f); + // assert Vec4::unit_y() == Vec4::new(0f, 1f, 0f, 0f); + // assert Vec4::unit_z() == Vec4::new(0f, 0f, 1f, 0f); + // assert Vec4::unit_w() == Vec4::new(0f, 0f, 0f, 1f); + // assert Vec4::identity() == Vec4::new(1f, 1f, 1f, 1f); assert a.x == 1f; assert a.y == 2f; diff --git a/src/vec.rs b/src/vec.rs index 8a87c65..44da77b 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -11,7 +11,21 @@ use num::cast::*; use num::default_eq::DefaultEq; -pub trait Vector: Dimensional, Eq, DefaultEq {} +pub trait Vector: Dimensional, Eq, DefaultEq { + static pure fn from_value(value: T) -> self; +} + +// pub trait Vector2: Vector { +// static pure fn new(x: T, y: T) -> self; +// } + +// pub trait Vector3: Vector { +// static pure fn new(x: T, y: T, z: T) -> self; +// } + +// pub trait Vector4: Vector { +// static pure fn new(x: T, y: T, z: T, w: T) -> self; +// } pub trait NumericVector: Vector, Neg{ static pure fn identity() -> self; @@ -26,6 +40,25 @@ pub trait NumericVector: Vector, Neg{ pure fn dot(other: &self) -> T; } +// pub trait NumericVector2: Vector { +// static pure fn unit_x() -> self; +// static pure fn unit_y() -> self; +// } + +pub trait NumericVector3: Vector { +// static pure fn unit_x() -> self; +// static pure fn unit_y() -> self; +// static pure fn unit_z() -> self; + pure fn cross(other: &self) -> self; +} + +// pub trait NumericVector4: Vector { +// static pure fn unit_x() -> self; +// static pure fn unit_y() -> self; +// static pure fn unit_z() -> self; +// static pure fn unit_w() -> self; +// } + pub trait GeometricVector: NumericVector { pure fn length2() -> T; pure fn length() -> T; @@ -35,23 +68,6 @@ pub trait GeometricVector: NumericVector { pure fn lerp(other: &self, amount: T) -> self; } -pub trait Vector2: Vector { - // static pure fn new(x: T, y: T) -> self; - // static pure fn from_value(value: T) -> self; -} - -pub trait Vector3: Vector { - // static pure fn new(x: T, y: T, z: T) -> self; - // static pure fn from_value(value: T) -> self; - - pure fn cross(other: &self) -> self; -} - -pub trait Vector4: Vector { - // pub static pure fn new(x: T, y: T, z: T, w: T) -> self; - // pub static pure fn from_value(value: T) -> self; -} - @@ -61,46 +77,19 @@ pub trait Vector4: Vector { // pub struct Vec2 { x: T, y: T } -pub mod Vec2 { - +pub impl Vec2/*: Vector2*/ { #[inline(always)] - pub pure fn new(x: T, y: T) -> Vec2 { + static pure fn new(x: T, y: T ) -> Vec2 { Vec2 { x: move x, y: move y } } - - #[inline(always)] - pub pure fn from_value(value: T) -> Vec2 { - Vec2::new(value, value) - } - - #[inline(always)] - pub pure fn zero() -> Vec2 { - let _0 = cast(0); - Vec2::new(_0, _0) - } - - #[inline(always)] - pub pure fn unit_x() -> Vec2 { - let _0 = cast(0); - let _1 = cast(1); - Vec2::new(_1, _0) - } - - #[inline(always)] - pub pure fn unit_y() -> Vec2 { - let _0 = cast(0); - let _1 = cast(1); - Vec2::new(_0, _1) - } - - #[inline(always)] - pub pure fn identity() -> Vec2 { - let _1 = cast(1); - Vec2::new(_1, _1) - } } pub impl Vec2: Vector { + #[inline(always)] + static pure fn from_value(value: T) -> Vec2 { + Vec2::new(value, value) + } + #[inline(always)] static pure fn dim() -> uint { 2 } @@ -239,62 +228,19 @@ pub impl Vec2: DefaultEq { // pub struct Vec3 { x: T, y: T, z: T } -pub mod Vec3 { - +pub impl Vec3/*: Vector3*/ { #[inline(always)] - pub pure fn new(x: T, y: T, z: T) -> Vec3 { + static pure fn new(x: T, y: T, z: T) -> Vec3 { Vec3 { x: move x, y: move y, z: move z } } - - #[inline(always)] - pub pure fn from_value(value: T) -> Vec3 { - Vec3::new(value, value, value) - } - - #[inline(always)] - pub pure fn zero() -> Vec3 { - let _0 = cast(0); - Vec3::new(_0, _0, _0) - } - - #[inline(always)] - pub pure fn unit_x() -> Vec3 { - let _0 = cast(0); - let _1 = cast(1); - Vec3::new(_1, _0, _0) - } - - #[inline(always)] - pub pure fn unit_y() -> Vec3 { - let _0 = cast(0); - let _1 = cast(1); - Vec3::new(_0, _1, _0) - } - - #[inline(always)] - pub pure fn unit_z() -> Vec3 { - let _0 = cast(0); - let _1 = cast(1); - Vec3::new(_0, _0, _1) - } - - #[inline(always)] - pub pure fn identity() -> Vec3 { - let _1 = cast(1); - Vec3::new(_1, _1, _1) - } -} - -pub impl Vec3: Vector3 { - #[inline(always)] - pure fn cross(other: &Vec3) -> Vec3 { - Vec3::new((self[1] * other[2]) - (self[2] * other[1]), - (self[2] * other[0]) - (self[0] * other[2]), - (self[0] * other[1]) - (self[1] * other[0])) - } } pub impl Vec3: Vector { + #[inline(always)] + static pure fn from_value(value: T) -> Vec3 { + Vec3::new(value, value, value) + } + #[inline(always)] static pure fn dim() -> uint { 3 } @@ -368,6 +314,15 @@ pub impl Vec3: NumericVector { } } +pub impl Vec3: NumericVector3 { + #[inline(always)] + pure fn cross(other: &Vec3) -> Vec3 { + Vec3::new((self[1] * other[2]) - (self[2] * other[1]), + (self[2] * other[0]) - (self[0] * other[2]), + (self[0] * other[1]) - (self[1] * other[0])) + } +} + pub impl Vec3: GeometricVector { #[inline(always)] pure fn length2() -> T { @@ -442,59 +397,19 @@ pub impl Vec3: DefaultEq { // pub struct Vec4 { x: T, y: T, z: T, w: T } -pub mod Vec4 { +pub impl Vec4/*: Vector4*/ { #[inline(always)] - pub pure fn new(x: T, y: T, z: T, w: T) -> Vec4 { + static pure fn new(x: T, y: T, z: T, w: T) -> Vec4 { Vec4 { x: move x, y: move y, z: move z, w: move w } } - - #[inline(always)] - pub pure fn from_value(value: T) -> Vec4 { - Vec4::new(value, value, value, value) - } - - #[inline(always)] - pub pure fn zero() -> Vec4 { - let _0 = cast(0); - Vec4::new(_0, _0, _0, _0) - } - - #[inline(always)] - pub pure fn unit_x() -> Vec4 { - let _0 = cast(0); - let _1 = cast(1); - Vec4::new(_1, _0, _0, _0) - } - - #[inline(always)] - pub pure fn unit_y() -> Vec4 { - let _0 = cast(0); - let _1 = cast(1); - Vec4::new(_0, _1, _0, _0) - } - - #[inline(always)] - pub pure fn unit_z() -> Vec4 { - let _0 = cast(0); - let _1 = cast(1); - Vec4::new(_0, _0, _1, _0) - } - - #[inline(always)] - pub pure fn unit_w() -> Vec4 { - let _0 = cast(0); - let _1 = cast(1); - Vec4::new(_0, _0, _0, _1) - } - - #[inline(always)] - pub pure fn identity() -> Vec4 { - let _1 = cast(1); - Vec4::new(_1, _1, _1, _1) - } } pub impl Vec4: Vector { + #[inline(always)] + static pure fn from_value(value: T) -> Vec4 { + Vec4::new(value, value, value, value) + } + #[inline(always)] static pure fn dim() -> uint { 4 }