use core::cast::transmute; use core::cmp::Eq; use core::ptr::{addr_of, to_unsafe_ptr}; use core::vec::raw::buf_as_slice; use std::cmp::FuzzyEq; use dim::Dimensional; use funs::exp::Exp; use num::cast::*; use num::default_eq::DefaultEq; /// /// The base vector trait /// pub trait Vector: Dimensional, Eq, DefaultEq { /// Construct the vector from a single value, copying it to each component static pure fn from_value(value: T) -> self; } // /// A 2-dimensional vector // pub trait Vector2: Vector { // static pure fn new(x: T, y: T) -> self; // } // /// A 3-dimensional vector // pub trait Vector3: Vector { // static pure fn new(x: T, y: T, z: T) -> self; // } // /// A 4-dimensional vector // pub trait Vector4: Vector { // static pure fn new(x: T, y: T, z: T, w: T) -> self; // } /// /// A vector with numeric components /// pub trait NumericVector: Vector, Neg{ static pure fn identity() -> self; static pure fn zero() -> self; pure fn mul_t(value: T) -> self; pure fn div_t(value: T) -> self; pure fn add_v(other: &self) -> self; pure fn sub_v(other: &self) -> self; pure fn dot(other: &self) -> T; } // /// A 2-dimensional vector with numeric components // pub trait NumericVector2: Vector { // static pure fn unit_x() -> self; // static pure fn unit_y() -> self; // } /// A 3-dimensional vector with numeric components 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; } // /// A 4-dimensional vector with numeric components // 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; // } /// /// A vector with geometric properties /// pub trait GeometricVector: NumericVector { pure fn length2() -> T; pure fn length() -> T; pure fn distance2(other: &self) -> T; pure fn distance(other: &self) -> T; pure fn normalize() -> self; pure fn lerp(other: &self, amount: T) -> self; } // // Vec2 // pub struct Vec2 { x: T, y: T } pub impl Vec2/*: Vector2*/ { #[inline(always)] static pure fn new(x: T, y: T ) -> Vec2 { Vec2 { x: move x, y: move y } } } 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 } #[inline(always)] pure fn index(i: uint) -> T { unsafe { do buf_as_slice( transmute::<*Vec2, *T>( to_unsafe_ptr(&self)), 2) |slice| { slice[i] } } } #[inline(always)] pure fn to_ptr() -> *T { ptr::addr_of(&self[0]) } } pub impl Vec2: NumericVector { #[inline(always)] static pure fn identity() -> Vec2 { Vec2::new(NumCast::one(), NumCast::one()) } #[inline(always)] static pure fn zero() -> Vec2 { Vec2::new(NumCast::zero(), NumCast::zero()) } #[inline(always)] pure fn neg() -> Vec2 { Vec2::new(-self[0], -self[1]) } #[inline(always)] pure fn mul_t(value: T) -> Vec2 { Vec2::new(self[0] * value, self[1] * value) } #[inline(always)] pure fn div_t(value: T) -> Vec2 { Vec2::new(self[0] / value, self[1] / value) } #[inline(always)] pure fn add_v(other: &Vec2) -> Vec2 { Vec2::new(self[0] + other[0], self[1] + other[1]) } #[inline(always)] pure fn sub_v(other: &Vec2) -> Vec2 { Vec2::new(self[0] - other[0], self[1] - other[1]) } #[inline(always)] pure fn dot(other: &Vec2) -> T { self[0] * other[0] + self[1] * other[1] } } pub impl Vec2: GeometricVector { #[inline(always)] pure fn length2() -> T { self.dot(&self) } #[inline(always)] pure fn length() -> T { self.length2().sqrt() } #[inline(always)] pure fn distance2(other: &Vec2) -> T { other.sub_v(&self).length2() } #[inline(always)] pure fn distance(other: &Vec2) -> T { other.distance2(&self).sqrt() } #[inline(always)] pure fn normalize() -> Vec2 { let mut n: T = cast(1); n /= self.length(); return self.mul_t(n); } #[inline(always)] pure fn lerp(other: &Vec2, amount: T) -> Vec2 { self.add_v(&other.sub_v(&self).mul_t(amount)) } } pub impl Vec2: Eq { #[inline(always)] pure fn eq(other: &Vec2) -> bool { self.default_eq(other) } #[inline(always)] pure fn ne(other: &Vec2) -> bool { !(self == *other) } } pub impl Vec2: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(other: &Vec2) -> bool { self[0].fuzzy_eq(&other[0]) && self[1].fuzzy_eq(&other[1]) } } pub impl Vec2: DefaultEq { #[inline(always)] pure fn default_eq(other: &Vec2) -> bool { self[0].default_eq(&other[0]) && self[1].default_eq(&other[1]) } } // // Vec3 // pub struct Vec3 { x: T, y: T, z: T } pub impl Vec3/*: Vector3*/ { #[inline(always)] static pure fn new(x: T, y: T, z: T) -> Vec3 { Vec3 { x: move x, y: move y, z: move z } } } 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 } #[inline(always)] pure fn index(i: uint) -> T { unsafe { do buf_as_slice( transmute::<*Vec3, *T>( to_unsafe_ptr(&self)), 3) |slice| { slice[i] } } } #[inline(always)] pure fn to_ptr() -> *T { addr_of(&self[0]) } } pub impl Vec3: NumericVector { #[inline(always)] static pure fn identity() -> Vec3 { Vec3::new(NumCast::one(), NumCast::one(), NumCast::one()) } #[inline(always)] static pure fn zero() -> Vec3 { Vec3::new(NumCast::zero(), NumCast::zero(), NumCast::zero()) } #[inline(always)] pure fn neg() -> Vec3 { Vec3::new(-self[0], -self[1], -self[2]) } #[inline(always)] pure fn mul_t(value: T) -> Vec3 { Vec3::new(self[0] * value, self[1] * value, self[2] * value) } #[inline(always)] pure fn div_t(value: T) -> Vec3 { Vec3::new(self[0] / value, self[1] / value, self[2] / value) } #[inline(always)] pure fn add_v(other: &Vec3) -> Vec3{ Vec3::new(self[0] + other[0], self[1] + other[1], self[2] + other[2]) } #[inline(always)] pure fn sub_v(other: &Vec3) -> Vec3{ Vec3::new(self[0] - other[0], self[1] - other[1], self[2] - other[2]) } #[inline(always)] pure fn dot(other: &Vec3) -> T { self[0] * other[0] + self[1] * other[1] + self[2] * other[2] } } 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 { self.dot(&self) } #[inline(always)] pure fn length() -> T { self.length2().sqrt() } #[inline(always)] pure fn distance2(other: &Vec3) -> T { other.sub_v(&self).length2() } #[inline(always)] pure fn distance(other: &Vec3) -> T { other.distance2(&self).sqrt() } #[inline(always)] pure fn normalize() -> Vec3 { let mut n: T = cast(1); n /= self.length(); return self.mul_t(n); } #[inline(always)] pure fn lerp(other: &Vec3, amount: T) -> Vec3 { self.add_v(&other.sub_v(&self).mul_t(amount)) } } pub impl Vec3: Eq { #[inline(always)] pure fn eq(other: &Vec3) -> bool { self.default_eq(other) } #[inline(always)] pure fn ne(other: &Vec3) -> bool { !(self == *other) } } pub impl Vec3: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(other: &Vec3) -> bool { self[0].fuzzy_eq(&other[0]) && self[1].fuzzy_eq(&other[1]) && self[2].fuzzy_eq(&other[2]) } } pub impl Vec3: DefaultEq { #[inline(always)] pure fn default_eq(other: &Vec3) -> bool { self[0].default_eq(&other[0]) && self[1].default_eq(&other[1]) && self[2].default_eq(&other[2]) } } // // Vec4 // pub struct Vec4 { x: T, y: T, z: T, w: T } pub impl Vec4/*: Vector4*/ { #[inline(always)] 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 } } } 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 } #[inline(always)] pure fn index(i: uint) -> T { unsafe { do buf_as_slice( transmute::<*Vec4, *T>( to_unsafe_ptr(&self)), 4) |slice| { slice[i] } } } #[inline(always)] pure fn to_ptr() -> *T { addr_of(&self[0]) } } pub impl Vec4: NumericVector { #[inline(always)] static pure fn identity() -> Vec4 { Vec4::new(NumCast::one(), NumCast::one(), NumCast::one(), NumCast::one()) } #[inline(always)] static pure fn zero() -> Vec4 { Vec4::new(NumCast::zero(), NumCast::zero(), NumCast::zero(), NumCast::zero()) } #[inline(always)] pure fn neg() -> Vec4 { Vec4::new(-self[0], -self[1], -self[2], -self[3]) } #[inline(always)] pure fn mul_t(value: T) -> Vec4 { Vec4::new(self[0] * value, self[1] * value, self[2] * value, self[3] * value) } #[inline(always)] pure fn div_t(value: T) -> Vec4 { Vec4::new(self[0] / value, self[1] / value, self[2] / value, self[3] / value) } #[inline(always)] pure fn add_v(other: &Vec4) -> Vec4 { Vec4::new(self[0] + other[0], self[1] + other[1], self[2] + other[2], self[3] + other[3]) } #[inline(always)] pure fn sub_v(other: &Vec4) -> Vec4 { Vec4::new(self[0] - other[0], self[1] - other[1], self[2] - other[2], self[3] - other[3]) } #[inline(always)] pure fn dot(other: &Vec4) -> T { self[0] * other[0] + self[1] * other[1] + self[2] * other[2] + self[3] * other[3] } } pub impl Vec4: GeometricVector { #[inline(always)] pure fn length2() -> T { self.dot(&self) } #[inline(always)] pure fn length() -> T { self.length2().sqrt() } #[inline(always)] pure fn distance2(other: &Vec4) -> T { other.sub_v(&self).length2() } #[inline(always)] pure fn distance(other: &Vec4) -> T { other.distance2(&self).sqrt() } #[inline(always)] pure fn normalize() -> Vec4 { let mut n: T = cast(1); n /= self.length(); return self.mul_t(n); } #[inline(always)] pure fn lerp(other: &Vec4, amount: T) -> Vec4 { self.add_v(&other.sub_v(&self).mul_t(amount)) } } pub impl Vec4: Eq { #[inline(always)] pure fn eq(other: &Vec4) -> bool { self.default_eq(other) } #[inline(always)] pure fn ne(other: &Vec4) -> bool { !(self == *other) } } pub impl Vec4: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(other: &Vec4) -> bool { self[0].fuzzy_eq(&other[0]) && self[1].fuzzy_eq(&other[1]) && self[2].fuzzy_eq(&other[2]) && self[3].fuzzy_eq(&other[3]) } } pub impl Vec4: DefaultEq { #[inline(always)] pure fn default_eq(other: &Vec4) -> bool { self[0].default_eq(&other[0]) && self[1].default_eq(&other[1]) && self[2].default_eq(&other[2]) && self[3].default_eq(&other[3]) } }