use cast::transmute; use cmp::{Eq, Ord}; use num::from_int; use vec::raw::buf_as_slice; use ptr::to_unsafe_ptr; use std::cmp::FuzzyEq; use funs::exp::Exp; use ncast::*; use math::*; // GLSL equivalent type aliases pub type vec2 = Vec2; /// a two-component single-precision floating-point vector pub type vec3 = Vec3; /// a three-component single-precision floating-point vector pub type vec4 = Vec4; /// a four-component single-precision floating-point vector pub type dvec2 = Vec2; /// a two-component double-precision floating-point vector pub type dvec3 = Vec3; /// a three-component double-precision floating-point vector pub type dvec4 = Vec4; /// a four-component double-precision floating-point vector pub type bvec2 = Vec2; /// a two-component Boolean vector pub type bvec3 = Vec3; /// a three-component Boolean vector pub type bvec4 = Vec4; /// a four-component Boolean vector pub type ivec2 = Vec2; /// a two-component signed integer vector pub type ivec3 = Vec3; /// a three-component signed integer vector pub type ivec4 = Vec4; /// a four-component signed integer vector pub type uvec2 = Vec2; /// a two-component unsigned integer vector pub type uvec3 = Vec3; /// a three-component unsigned integer vector pub type uvec4 = Vec4; /// a four-component unsigned integer vector pub trait Vector { static pure fn dim() -> uint; } pub trait NumericVector { 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; } pub trait GeometricVector { pure fn length2() -> T; pure fn length() -> T; pure fn normalize() -> self; pure fn lerp(other: &self, value: T) -> self; } pub trait Vector2 { // static pure fn new(x: T, y: T) -> self; // static pure fn from_value(value: T) -> self; } pub trait Vector3 { // 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 { // pub static pure fn new(x: T, y: T, z: T, w: T) -> self; // pub static pure fn from_value(value: T) -> self; } // // Vec2 // pub struct Vec2 { x: T, y: T } pub mod Vec2 { #[inline(always)] pub 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 dim() -> uint { 2 } } pub impl Vec2: NumericVector { #[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[0] * self[0] + self[1] * self[1] } #[inline(always)] pure fn length() -> T { self.length2().sqrt() } #[inline(always)] pure fn normalize() -> Vec2 { let mut n: T = from_int(1); n /= self.length(); return self.mul_t(n); } #[inline(always)] pure fn lerp(other: &Vec2, value: T) -> Vec2 { self.add_v(&other.sub_v(&self).mul_t(value)) } } pub impl Vec2: Index { #[inline(always)] pure fn index(i: uint) -> T { unsafe { do buf_as_slice( transmute::<*Vec2, *T>( to_unsafe_ptr(&self)), 2) |slice| { slice[i] } } } } pub impl> Vec2: Neg> { #[inline(always)] pure fn neg() -> Vec2 { Vec2::new(-self[0], -self[1]) } } // TODO: make work for T:Integer pub impl Vec2: Eq { #[inline(always)] pure fn eq(other: &Vec2) -> bool { self.fuzzy_eq(other) } #[inline(always)] pure fn ne(other: &Vec2) -> bool { !(self == *other) } } impl Vec2: ExactEq { #[inline(always)] pure fn exact_eq(other: &Vec2) -> bool { self[0] == other[0] && self[1] == other[1] } } 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: ToPtr { #[inline(always)] pure fn to_ptr() -> *T { to_unsafe_ptr(&self[0]) } } // // Vec3 // pub struct Vec3 { x: T, y: T, z: T } pub mod Vec3 { #[inline(always)] pub 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 dim() -> uint { 3 } } pub impl Vec3: NumericVector { #[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: GeometricVector { #[inline(always)] pure fn length2() -> T { self[0] * self[0] + self[1] * self[1] + self[2] * self[2] } #[inline(always)] pure fn length() -> T { self.length2().sqrt() } #[inline(always)] pure fn normalize() -> Vec3 { let mut n: T = from_int(1); n /= self.length(); return self.mul_t(n); } #[inline(always)] pure fn lerp(other: &Vec3, value: T) -> Vec3 { self.add_v(&other.sub_v(&self).mul_t(value)) } } pub impl Vec3: Index { #[inline(always)] pure fn index(i: uint) -> T { unsafe { do buf_as_slice( transmute::<*Vec3, *T>( to_unsafe_ptr(&self)), 3) |slice| { slice[i] } } } } pub impl> Vec3: Neg> { #[inline(always)] pure fn neg() -> Vec3 { Vec3::new(-self[0], -self[1], -self[2]) } } // TODO: make work for T:Integer pub impl Vec3: Eq { #[inline(always)] pure fn eq(other: &Vec3) -> bool { self.fuzzy_eq(other) } #[inline(always)] pure fn ne(other: &Vec3) -> bool { !(self == *other) } } pub impl Vec3: ExactEq { #[inline(always)] pure fn exact_eq(other: &Vec3) -> bool { self[0] == other[0] && self[1] == other[1] && self[2] == other[2] } } 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: ToPtr { #[inline(always)] pure fn to_ptr() -> *T { to_unsafe_ptr(&self[0]) } } // // Vec4 // pub struct Vec4 { x: T, y: T, z: T, w: T } pub mod Vec4 { #[inline(always)] pub 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 dim() -> uint { 4 } } pub impl Vec4: NumericVector { #[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[0] * self[0] + self[1] * self[1] + self[2] * self[2] + self[3] * self[3] } #[inline(always)] pure fn length() -> T { self.length2().sqrt() } #[inline(always)] pure fn normalize() -> Vec4 { let mut n: T = from_int(1); n /= self.length(); return self.mul_t(n); } #[inline(always)] pure fn lerp(other: &Vec4, value: T) -> Vec4 { self.add_v(&other.sub_v(&self).mul_t(value)) } } pub impl Vec4: Index { #[inline(always)] pure fn index(i: uint) -> T { unsafe { do buf_as_slice( transmute::<*Vec4, *T>( to_unsafe_ptr(&self)), 4) |slice| { slice[i] } } } } pub impl> Vec4: Neg> { #[inline(always)] pure fn neg() -> Vec4 { Vec4::new(-self[0], -self[1], -self[2], -self[3]) } } pub impl Vec4: Eq { #[inline(always)] pure fn eq(other: &Vec4) -> bool { self.fuzzy_eq(other) } #[inline(always)] pure fn ne(other: &Vec4) -> bool { !(self == *other) } } // TODO: make work for T:Integer pub impl Vec4: ExactEq { #[inline(always)] pure fn exact_eq(other: &Vec4) -> bool { self[0] == other[0] && self[1] == other[1] && self[2] == other[2] && self[3] == other[3] } } 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: ToPtr { #[inline(always)] pure fn to_ptr() -> *T { to_unsafe_ptr(&self[0]) } }