From 9b69ea222ce5519254f2dd15600100a1a4dc35c4 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 28 Jan 2013 09:50:49 +1100 Subject: [PATCH] Add vector constants for axis units --- src/gltypes.rs | 60 +++++++++++++++++++++++++++++++++++- src/vec.rs | 82 +++++++++++++++++++++++++------------------------- src/vec2.rs | 24 ++++++++++----- src/vec3.rs | 33 ++++++++++++++------ src/vec4.rs | 23 ++++++++++++++ 5 files changed, 164 insertions(+), 58 deletions(-) diff --git a/src/gltypes.rs b/src/gltypes.rs index d73eea4..5ca7705 100644 --- a/src/gltypes.rs +++ b/src/gltypes.rs @@ -22,8 +22,18 @@ use core::sys::size_of; +use vec::{ + Vector, + NumericVector, + NumericVector2, + NumericVector3, + NumericVector4, + Vec2, + Vec3, + Vec4, +}; + use mat::{Matrix, Mat2, Mat3, Mat4}; -use vec::{Vector, NumericVector, Vec2, Vec3, Vec4}; use quat::{/*Quaternion, */Quat}; use numeric::*; @@ -61,6 +71,9 @@ pub impl vec2 { #[inline(always)] static pure fn identity() -> vec2 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> vec2 { NumericVector::zero() } + #[inline(always)] static pure fn unit_x() -> vec2 { NumericVector2::unit_x() } + #[inline(always)] static pure fn unit_y() -> vec2 { NumericVector2::unit_y() } + #[inline(always)] static pure fn dim() -> uint { 2 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } @@ -71,6 +84,10 @@ pub impl vec3 { #[inline(always)] static pure fn identity() -> vec3 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> vec3 { NumericVector::zero() } + #[inline(always)] static pure fn unit_x() -> vec3 { NumericVector3::unit_x() } + #[inline(always)] static pure fn unit_y() -> vec3 { NumericVector3::unit_y() } + #[inline(always)] static pure fn unit_z() -> vec3 { NumericVector3::unit_z() } + #[inline(always)] static pure fn dim() -> uint { 3 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } @@ -81,6 +98,11 @@ pub impl vec4 { #[inline(always)] static pure fn identity() -> vec4 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> vec4 { NumericVector::zero() } + #[inline(always)] static pure fn unit_x() -> vec4 { NumericVector4::unit_x() } + #[inline(always)] static pure fn unit_y() -> vec4 { NumericVector4::unit_y() } + #[inline(always)] static pure fn unit_z() -> vec4 { NumericVector4::unit_z() } + #[inline(always)] static pure fn unit_w() -> vec4 { NumericVector4::unit_w() } + #[inline(always)] static pure fn dim() -> uint { 4 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } @@ -92,6 +114,9 @@ pub impl dvec2 { #[inline(always)] static pure fn identity() -> dvec2 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> dvec2 { NumericVector::zero() } + #[inline(always)] static pure fn unit_x() -> dvec2 { NumericVector2::unit_x() } + #[inline(always)] static pure fn unit_y() -> dvec2 { NumericVector2::unit_y() } + #[inline(always)] static pure fn dim() -> uint { 2 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } @@ -102,6 +127,10 @@ pub impl dvec3 { #[inline(always)] static pure fn identity() -> dvec3 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> dvec3 { NumericVector::zero() } + #[inline(always)] static pure fn unit_x() -> dvec3 { NumericVector3::unit_x() } + #[inline(always)] static pure fn unit_y() -> dvec3 { NumericVector3::unit_y() } + #[inline(always)] static pure fn unit_z() -> dvec3 { NumericVector3::unit_z() } + #[inline(always)] static pure fn dim() -> uint { 3 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } @@ -112,6 +141,11 @@ pub impl dvec4 { #[inline(always)] static pure fn identity() -> dvec4 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> dvec4 { NumericVector::zero() } + #[inline(always)] static pure fn unit_x() -> dvec4 { NumericVector4::unit_x() } + #[inline(always)] static pure fn unit_y() -> dvec4 { NumericVector4::unit_y() } + #[inline(always)] static pure fn unit_z() -> dvec4 { NumericVector4::unit_z() } + #[inline(always)] static pure fn unit_w() -> dvec4 { NumericVector4::unit_w() } + #[inline(always)] static pure fn dim() -> uint { 4 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } @@ -150,6 +184,9 @@ pub impl ivec2 { #[inline(always)] static pure fn identity() -> ivec2 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> ivec2 { NumericVector::zero() } + #[inline(always)] static pure fn unit_x() -> ivec2 { NumericVector2::unit_x() } + #[inline(always)] static pure fn unit_y() -> ivec2 { NumericVector2::unit_y() } + #[inline(always)] static pure fn dim() -> uint { 2 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } @@ -160,6 +197,10 @@ pub impl ivec3 { #[inline(always)] static pure fn identity() -> ivec3 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> ivec3 { NumericVector::zero() } + #[inline(always)] static pure fn unit_x() -> ivec3 { NumericVector3::unit_x() } + #[inline(always)] static pure fn unit_y() -> ivec3 { NumericVector3::unit_y() } + #[inline(always)] static pure fn unit_z() -> ivec3 { NumericVector3::unit_z() } + #[inline(always)] static pure fn dim() -> uint { 3 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } @@ -170,6 +211,11 @@ pub impl ivec4 { #[inline(always)] static pure fn identity() -> ivec4 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> ivec4 { NumericVector::zero() } + #[inline(always)] static pure fn unit_x() -> ivec4 { NumericVector4::unit_x() } + #[inline(always)] static pure fn unit_y() -> ivec4 { NumericVector4::unit_y() } + #[inline(always)] static pure fn unit_z() -> ivec4 { NumericVector4::unit_z() } + #[inline(always)] static pure fn unit_w() -> ivec4 { NumericVector4::unit_w() } + #[inline(always)] static pure fn dim() -> uint { 4 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } @@ -181,6 +227,9 @@ pub impl uvec2 { #[inline(always)] static pure fn identity() -> uvec2 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> uvec2 { NumericVector::zero() } + #[inline(always)] static pure fn unit_x() -> uvec2 { NumericVector2::unit_x() } + #[inline(always)] static pure fn unit_y() -> uvec2 { NumericVector2::unit_y() } + #[inline(always)] static pure fn dim() -> uint { 2 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } @@ -191,6 +240,10 @@ pub impl uvec3 { #[inline(always)] static pure fn identity() -> uvec3 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> uvec3 { NumericVector::zero() } + #[inline(always)] static pure fn unit_x() -> uvec3 { NumericVector3::unit_x() } + #[inline(always)] static pure fn unit_y() -> uvec3 { NumericVector3::unit_y() } + #[inline(always)] static pure fn unit_z() -> uvec3 { NumericVector3::unit_z() } + #[inline(always)] static pure fn dim() -> uint { 3 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } @@ -201,6 +254,11 @@ pub impl uvec4 { #[inline(always)] static pure fn identity() -> uvec4 { NumericVector::identity() } #[inline(always)] static pure fn zero() -> uvec4 { NumericVector::zero() } + #[inline(always)] static pure fn unit_x() -> uvec4 { NumericVector4::unit_x() } + #[inline(always)] static pure fn unit_y() -> uvec4 { NumericVector4::unit_y() } + #[inline(always)] static pure fn unit_z() -> uvec4 { NumericVector4::unit_z() } + #[inline(always)] static pure fn unit_w() -> uvec4 { NumericVector4::unit_w() } + #[inline(always)] static pure fn dim() -> uint { 4 } #[inline(always)] static pure fn size_of() -> uint { size_of::() } } diff --git a/src/vec.rs b/src/vec.rs index ba8dd72..809121f 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -135,6 +135,47 @@ pub trait NumericVector: Vector Neg { pure fn dot(&self, other: &self) -> T; } +/** + * A 2-dimensional vector with numeric components + */ +pub trait NumericVector2: NumericVector { + static pure fn unit_x() -> self; + static pure fn unit_y() -> self; + + /** + * # Return value + * + * The perp dot product of the vector and `other` + */ + pure fn perp_dot(&self, other: &self) -> T; +} + +/** + * A 3-dimensional vector with numeric components + */ +pub trait NumericVector3: NumericVector { + static pure fn unit_x() -> self; + static pure fn unit_y() -> self; + static pure fn unit_z() -> self; + + /** + * # Return value + * + * The cross product of the vector and `other` + */ + pure fn cross(&self, other: &self) -> self; +} + +/** + * A 4-dimensional vector with numeric components + */ +pub trait NumericVector4: NumericVector { + static pure fn unit_x() -> self; + static pure fn unit_y() -> self; + static pure fn unit_z() -> self; + static pure fn unit_w() -> self; +} + /** * A mutable vector with numeric components */ @@ -176,37 +217,6 @@ pub trait MutableNumericVector: MutableVector<&self/T> fn div_self_v(&mut self, other: &self); } -/** - * A 2-dimensional vector with numeric components - */ -pub trait NumericVector2: NumericVector { - // static pure fn unit_x() -> self; - // static pure fn unit_y() -> self; - - /** - * # Return value - * - * The perp dot product of the vector and `other` - */ - pure fn perp_dot(&self, other: &self) -> T; -} - -/** - * A 3-dimensional vector with numeric components - */ -pub trait NumericVector3: NumericVector { - // static pure fn unit_x() -> self; - // static pure fn unit_y() -> self; - // static pure fn unit_z() -> self; - - /** - * # Return value - * - * The cross product of the vector and `other` - */ - pure fn cross(&self, other: &self) -> self; -} - /** * A mutable 3-dimensional vector with numeric components */ @@ -217,16 +227,6 @@ pub trait MutableNumericVector3: MutableNumericVector<&self/T> { fn cross_self(&mut self, other: &self); } -/** - * A 4-dimensional vector with numeric components - */ -pub trait NumericVector4: NumericVector { - // 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 ToHomogeneous { /** * Convert to a homogenous coordinate diff --git a/src/vec2.rs b/src/vec2.rs index 611eb5e..2f07cea 100644 --- a/src/vec2.rs +++ b/src/vec2.rs @@ -153,6 +153,23 @@ pub impl Vec2: Neg> { } } +pub impl Vec2: NumericVector2 { + #[inline(always)] + static pure fn unit_x() -> Vec2 { + Vec2::new(one(), zero()) + } + + #[inline(always)] + static pure fn unit_y() -> Vec2 { + Vec2::new(zero(), one()) + } + + #[inline(always)] + pure fn perp_dot(&self, other: &Vec2) ->T { + (self[0] * other[1]) - (self[1] * other[0]) + } +} + pub impl Vec2: MutableNumericVector<&self/T> { #[inline(always)] fn neg_self(&mut self) { @@ -197,13 +214,6 @@ pub impl Vec2: MutableNumericVector<&self/T> { } } -pub impl Vec2: NumericVector2 { - #[inline(always)] - pure fn perp_dot(&self, other: &Vec2) ->T { - (self[0] * other[1]) - (self[1] * other[0]) - } -} - pub impl Vec2: ToHomogeneous> { #[inline(always)] pure fn to_homogeneous(&self) -> Vec3 { diff --git a/src/vec3.rs b/src/vec3.rs index aa40b09..6fb327a 100644 --- a/src/vec3.rs +++ b/src/vec3.rs @@ -164,6 +164,30 @@ pub impl Vec3: Neg> { } } +pub impl Vec3: NumericVector3 { + #[inline(always)] + static pure fn unit_x() -> Vec3 { + Vec3::new(one(), zero(), zero()) + } + + #[inline(always)] + static pure fn unit_y() -> Vec3 { + Vec3::new(zero(), one(), zero()) + } + + #[inline(always)] + static pure fn unit_z() -> Vec3 { + Vec3::new(zero(), zero(), one()) + } + + #[inline(always)] + pure fn cross(&self, 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: MutableNumericVector<&self/T> { #[inline(always)] fn neg_self(&mut self) { @@ -215,15 +239,6 @@ pub impl Vec3: MutableNumericVector<&self/T> { } } -pub impl Vec3: NumericVector3 { - #[inline(always)] - pure fn cross(&self, 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: MutableNumericVector3<&self/T> { #[inline(always)] fn cross_self(&mut self, other: &Vec3) { diff --git a/src/vec4.rs b/src/vec4.rs index cf3d9d4..d600692 100644 --- a/src/vec4.rs +++ b/src/vec4.rs @@ -12,6 +12,7 @@ use vec::{ Vector, MutableVector, NumericVector, + NumericVector4, MutableNumericVector, ToHomogeneous, EuclideanVector, @@ -171,6 +172,28 @@ pub impl Vec4: Neg> { } } +pub impl Vec4: NumericVector4 { + #[inline(always)] + static pure fn unit_x() -> Vec4 { + Vec4::new(one(), zero(), zero(), zero()) + } + + #[inline(always)] + static pure fn unit_y() -> Vec4 { + Vec4::new(zero(), one(), zero(), zero()) + } + + #[inline(always)] + static pure fn unit_z() -> Vec4 { + Vec4::new(zero(), zero(), one(), zero()) + } + + #[inline(always)] + static pure fn unit_w() -> Vec4 { + Vec4::new(zero(), zero(), zero(), one()) + } +} + pub impl Vec4: MutableNumericVector<&self/T> { #[inline(always)] fn neg_self(&mut self) {