2012-10-29 05:47:53 +00:00
|
|
|
use cast::transmute;
|
2012-10-29 11:53:25 +00:00
|
|
|
use vec::raw::buf_as_slice;
|
2012-10-29 05:47:53 +00:00
|
|
|
use ptr::to_unsafe_ptr;
|
2012-11-01 01:34:38 +00:00
|
|
|
use cmp::{Eq, Ord};
|
|
|
|
use num::from_int;
|
2012-10-29 05:47:53 +00:00
|
|
|
use std::cmp::FuzzyEq;
|
2012-11-01 01:34:38 +00:00
|
|
|
|
|
|
|
use math::*;
|
2012-09-07 10:48:47 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// N-dimensional Vector
|
|
|
|
//
|
2012-09-29 08:41:48 +00:00
|
|
|
pub trait Vector<T> {
|
2012-09-08 05:54:31 +00:00
|
|
|
static pure fn dim() -> uint;
|
2012-09-07 10:48:47 +00:00
|
|
|
|
2012-10-30 04:35:02 +00:00
|
|
|
pure fn add_t(value: T) -> self;
|
|
|
|
pure fn sub_t(value: T) -> self;
|
|
|
|
pure fn mul_t(value: T) -> self;
|
|
|
|
pure fn div_t(value: T) -> self;
|
2012-09-07 10:48:47 +00:00
|
|
|
|
2012-10-01 06:23:49 +00:00
|
|
|
pure fn add_v(other: &self) -> self;
|
|
|
|
pure fn sub_v(other: &self) -> self;
|
2012-09-07 10:48:47 +00:00
|
|
|
|
2012-10-01 06:23:49 +00:00
|
|
|
pure fn dot(other: &self) -> T;
|
2012-09-07 15:18:18 +00:00
|
|
|
|
2012-09-07 10:48:47 +00:00
|
|
|
pure fn magnitude2() -> T;
|
|
|
|
pure fn magnitude() -> T;
|
|
|
|
pure fn normalize() -> self;
|
|
|
|
|
2012-10-01 06:23:49 +00:00
|
|
|
pure fn lerp(other: &self, value: T) -> self;
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-09-29 08:41:48 +00:00
|
|
|
pub trait Vector3<T> {
|
2012-10-01 06:23:49 +00:00
|
|
|
fn cross(other: &self) -> self;
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2012-09-07 18:49:52 +00:00
|
|
|
// Vec2
|
2012-09-07 10:48:47 +00:00
|
|
|
//
|
2012-11-01 01:34:38 +00:00
|
|
|
pub struct Vec2<T> { x: T, y: T }
|
2012-09-07 10:48:47 +00:00
|
|
|
|
2012-09-08 05:54:31 +00:00
|
|
|
|
2012-09-07 10:48:47 +00:00
|
|
|
//
|
2012-09-07 18:49:52 +00:00
|
|
|
// Constructor
|
2012-09-07 10:48:47 +00:00
|
|
|
//
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pub pure fn Vec2<T:Copy>(x: T, y: T) -> Vec2<T> {
|
|
|
|
Vec2 { x: move x, y: move y }
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 14:11:27 +00:00
|
|
|
pub mod Vec2 {
|
2012-11-01 01:34:38 +00:00
|
|
|
#[inline(always)] pub pure fn zero <T: Num>() -> Vec2<T> { Vec2 { x: from_int(0), y: from_int(0) } }
|
|
|
|
#[inline(always)] pub pure fn unit_x <T: Num>() -> Vec2<T> { Vec2 { x: from_int(1), y: from_int(0) } }
|
|
|
|
#[inline(always)] pub pure fn unit_y <T: Num>() -> Vec2<T> { Vec2 { x: from_int(0), y: from_int(1) } }
|
|
|
|
#[inline(always)] pub pure fn identity <T: Num>() -> Vec2<T> { Vec2 { x: from_int(1), y: from_int(1) } }
|
2012-10-29 14:11:27 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy Num Sqrt> Vec2<T>: Vector<T> {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-09-08 05:54:31 +00:00
|
|
|
static pure fn dim() -> uint { 2 }
|
2012-09-07 10:48:47 +00:00
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn add_t(value: T) -> Vec2<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec2(self[0] + value,
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] + value)
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn sub_t(value: T) -> Vec2<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec2(self[0] - value,
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] - value)
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn mul_t(value: T) -> Vec2<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec2(self[0] * value,
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] * value)
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn div_t(value: T) -> Vec2<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec2(self[0] / value,
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] / value)
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn add_v(other: &Vec2<T>) -> Vec2<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec2(self[0] + other[0],
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] + other[1])
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn sub_v(other: &Vec2<T>) -> Vec2<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec2(self[0] - other[0],
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] - other[1])
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn dot(other: &Vec2<T>) -> T {
|
2012-09-07 15:18:18 +00:00
|
|
|
self[0] * other[0] +
|
|
|
|
self[1] * other[1]
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn magnitude2() -> T {
|
2012-09-07 10:48:47 +00:00
|
|
|
self[0] * self[0] +
|
|
|
|
self[1] * self[1]
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn magnitude() -> T {
|
2012-09-10 02:55:15 +00:00
|
|
|
self.magnitude2().sqrt()
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn normalize() -> Vec2<T> {
|
|
|
|
let mut n: T = from_int(1);
|
|
|
|
n /= self.magnitude();
|
2012-10-30 04:35:02 +00:00
|
|
|
return self.mul_t(n);
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
2012-09-07 18:45:07 +00:00
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn lerp(other: &Vec2<T>, value: T) -> Vec2<T> {
|
2012-10-30 04:35:02 +00:00
|
|
|
self.add_v(&other.sub_v(&self).mul_t(value))
|
2012-09-07 18:45:07 +00:00
|
|
|
}
|
2012-11-01 01:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy> Vec2<T>: Index<uint, T> {
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn index(i: uint) -> T {
|
|
|
|
unsafe { do buf_as_slice(
|
|
|
|
transmute::<*Vec2<T>, *T>(
|
|
|
|
to_unsafe_ptr(&self)), 2) |slice| { slice[i] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy MinMax> Vec2<T>: MinMax {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn min(other: &Vec2<T>) -> Vec2<T> {
|
2012-10-06 05:08:02 +00:00
|
|
|
Vec2(min(&self[0], &other[0]),
|
|
|
|
min(&self[1], &other[1]))
|
2012-09-08 05:54:31 +00:00
|
|
|
}
|
2012-09-07 18:45:07 +00:00
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn max(other: &Vec2<T>) -> Vec2<T> {
|
2012-10-06 05:08:02 +00:00
|
|
|
Vec2(max(&self[0], &other[0]),
|
|
|
|
max(&self[1], &other[1]))
|
2012-09-08 05:54:31 +00:00
|
|
|
}
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy Abs> Vec2<T>: Abs {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn abs() -> Vec2<T> {
|
2012-10-29 14:11:27 +00:00
|
|
|
Vec2(abs(&self[0]),
|
|
|
|
abs(&self[1]))
|
2012-10-28 06:59:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy Neg<T>> Vec2<T>: Neg<Vec2<T>> {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn neg() -> Vec2<T> {
|
2012-09-29 10:25:49 +00:00
|
|
|
Vec2(-self[0], -self[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
// TODO: make work for T:Integer
|
|
|
|
pub impl<T:Copy FuzzyEq> Vec2<T>: Eq {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn eq(other: &Vec2<T>) -> bool {
|
2012-09-29 10:25:49 +00:00
|
|
|
self.fuzzy_eq(other)
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn ne(other: &Vec2<T>) -> bool {
|
2012-09-29 10:25:49 +00:00
|
|
|
!(self == *other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
impl<T:Copy Eq> Vec2<T>: ExactEq {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn exact_eq(other: &Vec2<T>) -> bool {
|
2012-10-30 02:45:18 +00:00
|
|
|
self[0] == other[0] &&
|
|
|
|
self[1] == other[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy FuzzyEq> Vec2<T>: FuzzyEq {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn fuzzy_eq(other: &Vec2<T>) -> bool {
|
2012-09-29 10:25:49 +00:00
|
|
|
self[0].fuzzy_eq(&other[0]) &&
|
|
|
|
self[1].fuzzy_eq(&other[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy> Vec2<T>: ToPtr<T> {
|
2012-10-30 03:24:50 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn to_ptr() -> *T {
|
2012-10-30 03:24:50 +00:00
|
|
|
to_unsafe_ptr(&self[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 10:48:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2012-09-07 18:49:52 +00:00
|
|
|
// Vec3
|
2012-09-07 10:48:47 +00:00
|
|
|
//
|
2012-11-01 01:34:38 +00:00
|
|
|
pub struct Vec3<T> { x: T, y: T, z: T }
|
2012-09-07 10:48:47 +00:00
|
|
|
|
|
|
|
//
|
2012-09-07 18:49:52 +00:00
|
|
|
// Constructor
|
2012-09-07 10:48:47 +00:00
|
|
|
//
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pub pure fn Vec3<T:Copy>(x: T, y: T, z: T) -> Vec3<T> {
|
|
|
|
Vec3 { x: move x, y: move y, z: move z }
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 14:11:27 +00:00
|
|
|
pub mod Vec3 {
|
2012-11-01 01:34:38 +00:00
|
|
|
#[inline(always)] pub pure fn zero <T: Num>() -> Vec3<T> { Vec3 { x: from_int(0), y: from_int(0), z: from_int(0) } }
|
|
|
|
#[inline(always)] pub pure fn unit_x <T: Num>() -> Vec3<T> { Vec3 { x: from_int(1), y: from_int(0), z: from_int(0) } }
|
|
|
|
#[inline(always)] pub pure fn unit_y <T: Num>() -> Vec3<T> { Vec3 { x: from_int(0), y: from_int(1), z: from_int(0) } }
|
|
|
|
#[inline(always)] pub pure fn unit_z <T: Num>() -> Vec3<T> { Vec3 { x: from_int(0), y: from_int(0), z: from_int(1) } }
|
|
|
|
#[inline(always)] pub pure fn identity <T: Num>() -> Vec3<T> { Vec3 { x: from_int(1), y: from_int(1), z: from_int(1) } }
|
2012-10-29 14:11:27 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy Num> Vec3<T>: Vector3<T> {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
fn cross(other: &Vec3<T>) -> Vec3<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec3((self[1] * other[2]) - (self[2] * other[1]),
|
2012-09-07 10:48:47 +00:00
|
|
|
(self[2] * other[0]) - (self[0] * other[2]),
|
|
|
|
(self[0] * other[1]) - (self[1] * other[0]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy Num Sqrt> Vec3<T>: Vector<T> {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-09-08 05:54:31 +00:00
|
|
|
static pure fn dim() -> uint { 3 }
|
2012-09-07 10:48:47 +00:00
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn add_t(value: T) -> Vec3<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec3(self[0] + value,
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] + value,
|
|
|
|
self[2] + value)
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn sub_t(value: T) -> Vec3<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec3(self[0] - value,
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] - value,
|
|
|
|
self[2] - value)
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn mul_t(value: T) -> Vec3<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec3(self[0] * value,
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] * value,
|
|
|
|
self[2] * value)
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn div_t(value: T) -> Vec3<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec3(self[0] / value,
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] / value,
|
|
|
|
self[2] / value)
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn add_v(other: &Vec3<T>) -> Vec3<T>{
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec3(self[0] + other[0],
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] + other[1],
|
|
|
|
self[2] + other[2])
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn sub_v(other: &Vec3<T>) -> Vec3<T>{
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec3(self[0] - other[0],
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] - other[1],
|
|
|
|
self[2] - other[2])
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn dot(other: &Vec3<T>) -> T {
|
2012-09-07 15:18:18 +00:00
|
|
|
self[0] * other[0] +
|
|
|
|
self[1] * other[1] +
|
|
|
|
self[2] * other[2]
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn magnitude2() -> T {
|
2012-09-07 10:48:47 +00:00
|
|
|
self[0] * self[0] +
|
|
|
|
self[1] * self[1] +
|
|
|
|
self[2] * self[2]
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn magnitude() -> T {
|
2012-09-10 02:55:15 +00:00
|
|
|
self.magnitude2().sqrt()
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn normalize() -> Vec3<T> {
|
|
|
|
let mut n: T = from_int(1);
|
|
|
|
n /= self.magnitude();
|
2012-10-30 04:35:02 +00:00
|
|
|
return self.mul_t(n);
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
2012-09-07 18:45:07 +00:00
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn lerp(other: &Vec3<T>, value: T) -> Vec3<T> {
|
2012-10-30 04:35:02 +00:00
|
|
|
self.add_v(&other.sub_v(&self).mul_t(value))
|
2012-09-07 18:45:07 +00:00
|
|
|
}
|
2012-11-01 01:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy> Vec3<T>: Index<uint, T> {
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn index(i: uint) -> T {
|
|
|
|
unsafe { do buf_as_slice(
|
|
|
|
transmute::<*Vec3<T>, *T>(
|
|
|
|
to_unsafe_ptr(&self)), 3) |slice| { slice[i] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy MinMax> Vec3<T>: MinMax {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn min(other: &Vec3<T>) -> Vec3<T> {
|
2012-10-06 05:08:02 +00:00
|
|
|
Vec3(min(&self[0], &other[0]),
|
|
|
|
min(&self[1], &other[1]),
|
|
|
|
min(&self[2], &other[2]))
|
2012-09-08 05:54:31 +00:00
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn max(other: &Vec3<T>) -> Vec3<T> {
|
2012-10-06 05:08:02 +00:00
|
|
|
Vec3(max(&self[0], &other[0]),
|
|
|
|
max(&self[1], &other[1]),
|
|
|
|
max(&self[2], &other[2]))
|
2012-09-08 05:54:31 +00:00
|
|
|
}
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy Abs> Vec3<T>: Abs {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn abs() -> Vec3<T> {
|
2012-10-29 14:11:27 +00:00
|
|
|
Vec3(abs(&self[0]),
|
|
|
|
abs(&self[1]),
|
|
|
|
abs(&self[2]))
|
2012-10-28 06:59:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy Neg<T>> Vec3<T>: Neg<Vec3<T>> {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn neg() -> Vec3<T> {
|
2012-09-29 10:25:49 +00:00
|
|
|
Vec3(-self[0], -self[1], -self[2])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
// TODO: make work for T:Integer
|
|
|
|
pub impl<T:Copy FuzzyEq> Vec3<T>: Eq {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn eq(other: &Vec3<T>) -> bool {
|
2012-09-29 10:25:49 +00:00
|
|
|
self.fuzzy_eq(other)
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn ne(other: &Vec3<T>) -> bool {
|
2012-09-29 10:25:49 +00:00
|
|
|
!(self == *other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy Eq> Vec3<T>: ExactEq {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn exact_eq(other: &Vec3<T>) -> bool {
|
2012-10-30 02:45:18 +00:00
|
|
|
self[0] == other[0] &&
|
|
|
|
self[1] == other[1] &&
|
|
|
|
self[2] == other[2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy FuzzyEq> Vec3<T>: FuzzyEq {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn fuzzy_eq(other: &Vec3<T>) -> bool {
|
2012-09-29 10:25:49 +00:00
|
|
|
self[0].fuzzy_eq(&other[0]) &&
|
|
|
|
self[1].fuzzy_eq(&other[1]) &&
|
|
|
|
self[2].fuzzy_eq(&other[2])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy> Vec3<T>: ToPtr<T> {
|
2012-10-30 03:24:50 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn to_ptr() -> *T {
|
2012-10-30 03:24:50 +00:00
|
|
|
to_unsafe_ptr(&self[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 10:48:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2012-09-07 18:49:52 +00:00
|
|
|
// Vec4
|
2012-09-07 10:48:47 +00:00
|
|
|
//
|
2012-11-01 01:34:38 +00:00
|
|
|
pub struct Vec4<T> { x: T, y: T, z: T, w: T }
|
2012-09-07 10:48:47 +00:00
|
|
|
|
2012-10-29 14:11:27 +00:00
|
|
|
pub mod Vec4 {
|
2012-11-01 01:34:38 +00:00
|
|
|
#[inline(always)] pub pure fn zero <T: Num>() -> Vec4<T> { Vec4 { x: from_int(0), y: from_int(0), z: from_int(0), w: from_int(0) } }
|
|
|
|
#[inline(always)] pub pure fn unit_x <T: Num>() -> Vec4<T> { Vec4 { x: from_int(1), y: from_int(0), z: from_int(0), w: from_int(0) } }
|
|
|
|
#[inline(always)] pub pure fn unit_y <T: Num>() -> Vec4<T> { Vec4 { x: from_int(0), y: from_int(1), z: from_int(0), w: from_int(0) } }
|
|
|
|
#[inline(always)] pub pure fn unit_z <T: Num>() -> Vec4<T> { Vec4 { x: from_int(0), y: from_int(0), z: from_int(1), w: from_int(0) } }
|
|
|
|
#[inline(always)] pub pure fn unit_w <T: Num>() -> Vec4<T> { Vec4 { x: from_int(0), y: from_int(0), z: from_int(0), w: from_int(1) } }
|
|
|
|
#[inline(always)] pub pure fn identity <T: Num>() -> Vec4<T> { Vec4 { x: from_int(1), y: from_int(1), z: from_int(1), w: from_int(1) } }
|
2012-10-29 14:11:27 +00:00
|
|
|
}
|
2012-09-07 10:48:47 +00:00
|
|
|
|
|
|
|
//
|
2012-09-07 18:49:52 +00:00
|
|
|
// Constructor
|
2012-09-07 10:48:47 +00:00
|
|
|
//
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pub pure fn Vec4<T:Copy>(x: T, y: T, z: T, w: T) -> Vec4<T> {
|
|
|
|
Vec4 { x: move x, y: move y, z: move z, w: move w }
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy Num Sqrt> Vec4<T>: Vector<T> {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-09-08 05:54:31 +00:00
|
|
|
static pure fn dim() -> uint { 4 }
|
2012-09-07 10:48:47 +00:00
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn add_t(value: T) -> Vec4<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec4(self[0] + value,
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] + value,
|
|
|
|
self[2] + value,
|
|
|
|
self[3] + value)
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn sub_t(value: T) -> Vec4<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec4(self[0] - value,
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] - value,
|
|
|
|
self[2] - value,
|
|
|
|
self[3] - value)
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn mul_t(value: T) -> Vec4<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec4(self[0] * value,
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] * value,
|
|
|
|
self[2] * value,
|
|
|
|
self[3] * value)
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn div_t(value: T) -> Vec4<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec4(self[0] / value,
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] / value,
|
|
|
|
self[2] / value,
|
|
|
|
self[3] / value)
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn add_v(other: &Vec4<T>) -> Vec4<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec4(self[0] + other[0],
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] + other[1],
|
|
|
|
self[2] + other[2],
|
|
|
|
self[3] + other[3])
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn sub_v(other: &Vec4<T>) -> Vec4<T> {
|
2012-09-24 02:54:24 +00:00
|
|
|
Vec4(self[0] - other[0],
|
2012-09-07 10:48:47 +00:00
|
|
|
self[1] - other[1],
|
|
|
|
self[2] - other[2],
|
|
|
|
self[3] - other[3])
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn dot(other: &Vec4<T>) -> T {
|
2012-09-07 15:18:18 +00:00
|
|
|
self[0] * other[0] +
|
|
|
|
self[1] * other[1] +
|
|
|
|
self[2] * other[2] +
|
|
|
|
self[3] * other[3]
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn magnitude2() -> T {
|
2012-09-07 10:48:47 +00:00
|
|
|
self[0] * self[0] +
|
|
|
|
self[1] * self[1] +
|
|
|
|
self[2] * self[2] +
|
2012-09-07 18:45:07 +00:00
|
|
|
self[3] * self[3]
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn magnitude() -> T {
|
2012-09-10 02:55:15 +00:00
|
|
|
self.magnitude2().sqrt()
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn normalize() -> Vec4<T> {
|
|
|
|
let mut n: T = from_int(1);
|
|
|
|
n /= self.magnitude();
|
2012-10-30 04:35:02 +00:00
|
|
|
return self.mul_t(n);
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
2012-09-07 18:45:07 +00:00
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn lerp(other: &Vec4<T>, value: T) -> Vec4<T> {
|
2012-10-30 04:35:02 +00:00
|
|
|
self.add_v(&other.sub_v(&self).mul_t(value))
|
2012-09-07 18:45:07 +00:00
|
|
|
}
|
2012-11-01 01:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy> Vec4<T>: Index<uint, T> {
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn index(i: uint) -> T {
|
|
|
|
unsafe {
|
|
|
|
do buf_as_slice(
|
|
|
|
transmute::<*Vec4<T>, *T>(
|
|
|
|
to_unsafe_ptr(&self)), 4) |slice| { slice[i] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy MinMax> Vec4<T>: MinMax {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn min(other: &Vec4<T>) -> Vec4<T> {
|
2012-10-06 05:08:02 +00:00
|
|
|
Vec4(min(&self[0], &other[0]),
|
|
|
|
min(&self[1], &other[1]),
|
|
|
|
min(&self[2], &other[2]),
|
|
|
|
min(&self[3], &other[3]))
|
2012-09-08 05:54:31 +00:00
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn max(other: &Vec4<T>) -> Vec4<T> {
|
2012-10-06 05:08:02 +00:00
|
|
|
Vec4(max(&self[0], &other[0]),
|
|
|
|
max(&self[1], &other[1]),
|
|
|
|
max(&self[2], &other[2]),
|
|
|
|
max(&self[3], &other[3]))
|
2012-09-08 05:54:31 +00:00
|
|
|
}
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy Abs> Vec4<T>: Abs {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn abs() -> Vec4<T> {
|
2012-10-29 14:11:27 +00:00
|
|
|
Vec4(abs(&self[0]),
|
|
|
|
abs(&self[1]),
|
|
|
|
abs(&self[2]),
|
|
|
|
abs(&self[3]))
|
2012-10-28 06:59:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy Neg<T>> Vec4<T>: Neg<Vec4<T>> {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn neg() -> Vec4<T> {
|
2012-09-29 10:25:49 +00:00
|
|
|
Vec4(-self[0], -self[1], -self[2], -self[3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy FuzzyEq> Vec4<T>: Eq {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn eq(other: &Vec4<T>) -> bool {
|
2012-09-29 10:25:49 +00:00
|
|
|
self.fuzzy_eq(other)
|
|
|
|
}
|
|
|
|
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn ne(other: &Vec4<T>) -> bool {
|
2012-09-29 10:25:49 +00:00
|
|
|
!(self == *other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
// TODO: make work for T:Integer
|
|
|
|
pub impl<T:Copy Eq> Vec4<T>: ExactEq {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn exact_eq(other: &Vec4<T>) -> bool {
|
2012-10-30 02:45:18 +00:00
|
|
|
self[0] == other[0] &&
|
|
|
|
self[1] == other[1] &&
|
|
|
|
self[2] == other[2] &&
|
|
|
|
self[3] == other[3]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy FuzzyEq> Vec4<T>: FuzzyEq {
|
2012-10-30 02:55:20 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn fuzzy_eq(other: &Vec4<T>) -> bool {
|
2012-09-29 10:25:49 +00:00
|
|
|
self[0].fuzzy_eq(&other[0]) &&
|
|
|
|
self[1].fuzzy_eq(&other[1]) &&
|
|
|
|
self[2].fuzzy_eq(&other[2]) &&
|
|
|
|
self[3].fuzzy_eq(&other[3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:34:38 +00:00
|
|
|
pub impl<T:Copy> Vec4<T>: ToPtr<T> {
|
2012-10-30 03:24:50 +00:00
|
|
|
#[inline(always)]
|
2012-11-01 01:34:38 +00:00
|
|
|
pure fn to_ptr() -> *T {
|
2012-10-30 03:24:50 +00:00
|
|
|
to_unsafe_ptr(&self[0])
|
|
|
|
}
|
2012-09-07 10:48:47 +00:00
|
|
|
}
|