Make vector constructors static functions

This commit is contained in:
Brendan Zabarauskas 2012-11-20 17:58:24 +10:00
parent 59abaf7f87
commit a06d2cff54
4 changed files with 134 additions and 186 deletions

View file

@ -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);

View file

@ -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
@ -36,80 +36,110 @@ pub type uvec4 = Vec4<u32>; /// a four-component unsigned integer ve
//
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 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 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() }
}
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() }
}
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() }
}
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() }
}
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() }
}
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() }
}
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() }
}

View file

@ -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;

View file

@ -11,7 +11,21 @@ use num::cast::*;
use num::default_eq::DefaultEq;
pub trait Vector<T>: Dimensional<T>, Eq, DefaultEq {}
pub trait Vector<T>: Dimensional<T>, Eq, DefaultEq {
static pure fn from_value(value: T) -> self;
}
// pub trait Vector2<T>: Vector<T> {
// static pure fn new(x: T, y: T) -> self;
// }
// pub trait Vector3<T>: Vector<T> {
// static pure fn new(x: T, y: T, z: T) -> self;
// }
// pub trait Vector4<T>: Vector<T> {
// static pure fn new(x: T, y: T, z: T, w: T) -> self;
// }
pub trait NumericVector<T>: Vector<T>, Neg<self>{
static pure fn identity() -> self;
@ -26,6 +40,25 @@ pub trait NumericVector<T>: Vector<T>, Neg<self>{
pure fn dot(other: &self) -> T;
}
// pub trait NumericVector2<T>: Vector<T> {
// static pure fn unit_x() -> self;
// static pure fn unit_y() -> self;
// }
pub trait NumericVector3<T>: Vector<T> {
// 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<T>: Vector<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;
// }
pub trait GeometricVector<T>: NumericVector<T> {
pure fn length2() -> T;
pure fn length() -> T;
@ -35,23 +68,6 @@ pub trait GeometricVector<T>: NumericVector<T> {
pure fn lerp(other: &self, amount: T) -> self;
}
pub trait Vector2<T>: Vector<T> {
// static pure fn new(x: T, y: T) -> self;
// static pure fn from_value(value: T) -> self;
}
pub trait Vector3<T>: Vector<T> {
// 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<T>: Vector<T> {
// 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<T>: Vector<T> {
//
pub struct Vec2<T> { x: T, y: T }
pub mod Vec2 {
pub impl<T> Vec2<T>/*: Vector2<T>*/ {
#[inline(always)]
pub pure fn new<T>(x: T, y: T) -> Vec2<T> {
static pure fn new(x: T, y: T ) -> Vec2<T> {
Vec2 { x: move x, y: move y }
}
#[inline(always)]
pub pure fn from_value<T:Copy>(value: T) -> Vec2<T> {
Vec2::new(value, value)
}
#[inline(always)]
pub pure fn zero<T:Copy NumCast>() -> Vec2<T> {
let _0 = cast(0);
Vec2::new(_0, _0)
}
#[inline(always)]
pub pure fn unit_x<T:Copy NumCast>() -> Vec2<T> {
let _0 = cast(0);
let _1 = cast(1);
Vec2::new(_1, _0)
}
#[inline(always)]
pub pure fn unit_y<T:Copy NumCast>() -> Vec2<T> {
let _0 = cast(0);
let _1 = cast(1);
Vec2::new(_0, _1)
}
#[inline(always)]
pub pure fn identity<T:Copy NumCast>() -> Vec2<T> {
let _1 = cast(1);
Vec2::new(_1, _1)
}
}
pub impl<T:Copy> Vec2<T>: Vector<T> {
#[inline(always)]
static pure fn from_value(value: T) -> Vec2<T> {
Vec2::new(value, value)
}
#[inline(always)]
static pure fn dim() -> uint { 2 }
@ -239,62 +228,19 @@ pub impl<T:Copy DefaultEq> Vec2<T>: DefaultEq {
//
pub struct Vec3<T> { x: T, y: T, z: T }
pub mod Vec3 {
pub impl<T> Vec3<T>/*: Vector3<T>*/ {
#[inline(always)]
pub pure fn new<T>(x: T, y: T, z: T) -> Vec3<T> {
static pure fn new(x: T, y: T, z: T) -> Vec3<T> {
Vec3 { x: move x, y: move y, z: move z }
}
#[inline(always)]
pub pure fn from_value<T:Copy>(value: T) -> Vec3<T> {
Vec3::new(value, value, value)
}
#[inline(always)]
pub pure fn zero<T:Copy NumCast>() -> Vec3<T> {
let _0 = cast(0);
Vec3::new(_0, _0, _0)
}
#[inline(always)]
pub pure fn unit_x<T:Copy NumCast>() -> Vec3<T> {
let _0 = cast(0);
let _1 = cast(1);
Vec3::new(_1, _0, _0)
}
#[inline(always)]
pub pure fn unit_y<T:Copy NumCast>() -> Vec3<T> {
let _0 = cast(0);
let _1 = cast(1);
Vec3::new(_0, _1, _0)
}
#[inline(always)]
pub pure fn unit_z<T:Copy NumCast>() -> Vec3<T> {
let _0 = cast(0);
let _1 = cast(1);
Vec3::new(_0, _0, _1)
}
#[inline(always)]
pub pure fn identity<T:Copy NumCast>() -> Vec3<T> {
let _1 = cast(1);
Vec3::new(_1, _1, _1)
}
}
pub impl<T:Copy Num> Vec3<T>: Vector3<T> {
#[inline(always)]
pure fn cross(other: &Vec3<T>) -> Vec3<T> {
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<T:Copy> Vec3<T>: Vector<T> {
#[inline(always)]
static pure fn from_value(value: T) -> Vec3<T> {
Vec3::new(value, value, value)
}
#[inline(always)]
static pure fn dim() -> uint { 3 }
@ -368,6 +314,15 @@ pub impl<T:Copy Num NumCast> Vec3<T>: NumericVector<T> {
}
}
pub impl<T:Copy Num> Vec3<T>: NumericVector3<T> {
#[inline(always)]
pure fn cross(other: &Vec3<T>) -> Vec3<T> {
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<T:Copy Num NumCast Exp> Vec3<T>: GeometricVector<T> {
#[inline(always)]
pure fn length2() -> T {
@ -442,59 +397,19 @@ pub impl<T:Copy DefaultEq> Vec3<T>: DefaultEq {
//
pub struct Vec4<T> { x: T, y: T, z: T, w: T }
pub mod Vec4 {
pub impl<T> Vec4<T>/*: Vector4<T>*/ {
#[inline(always)]
pub pure fn new<T>(x: T, y: T, z: T, w: T) -> Vec4<T> {
static pure fn new(x: T, y: T, z: T, w: T) -> Vec4<T> {
Vec4 { x: move x, y: move y, z: move z, w: move w }
}
#[inline(always)]
pub pure fn from_value<T:Copy>(value: T) -> Vec4<T> {
Vec4::new(value, value, value, value)
}
#[inline(always)]
pub pure fn zero<T:Copy NumCast>() -> Vec4<T> {
let _0 = cast(0);
Vec4::new(_0, _0, _0, _0)
}
#[inline(always)]
pub pure fn unit_x<T:Copy NumCast>() -> Vec4<T> {
let _0 = cast(0);
let _1 = cast(1);
Vec4::new(_1, _0, _0, _0)
}
#[inline(always)]
pub pure fn unit_y<T:Copy NumCast>() -> Vec4<T> {
let _0 = cast(0);
let _1 = cast(1);
Vec4::new(_0, _1, _0, _0)
}
#[inline(always)]
pub pure fn unit_z<T:Copy NumCast>() -> Vec4<T> {
let _0 = cast(0);
let _1 = cast(1);
Vec4::new(_0, _0, _1, _0)
}
#[inline(always)]
pub pure fn unit_w<T:Copy NumCast>() -> Vec4<T> {
let _0 = cast(0);
let _1 = cast(1);
Vec4::new(_0, _0, _0, _1)
}
#[inline(always)]
pub pure fn identity<T:Copy NumCast>() -> Vec4<T> {
let _1 = cast(1);
Vec4::new(_1, _1, _1, _1)
}
}
pub impl<T:Copy> Vec4<T>: Vector<T> {
#[inline(always)]
static pure fn from_value(value: T) -> Vec4<T> {
Vec4::new(value, value, value, value)
}
#[inline(always)]
static pure fn dim() -> uint { 4 }