2012-11-15 02:23:39 +00:00
|
|
|
use core::cast::transmute;
|
|
|
|
use core::cmp::Eq;
|
2012-11-22 00:38:39 +00:00
|
|
|
use core::ptr::to_unsafe_ptr;
|
2012-11-25 12:05:47 +00:00
|
|
|
use core::sys::size_of;
|
2012-11-15 02:23:39 +00:00
|
|
|
use core::vec::raw::buf_as_slice;
|
|
|
|
|
|
|
|
use std::cmp::FuzzyEq;
|
|
|
|
|
2012-12-01 04:55:45 +00:00
|
|
|
use dim::{Dimensional, ToPtr};
|
2012-11-26 06:48:46 +00:00
|
|
|
use funs::exponential::Exp;
|
2012-12-03 01:12:57 +00:00
|
|
|
use num::kinds::Number;
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
2012-12-05 01:51:18 +00:00
|
|
|
* The base generic vector trait.
|
|
|
|
*
|
|
|
|
* # Type parameters
|
|
|
|
*
|
|
|
|
* * `T` - The type of the components. This is intended to support boolean,
|
|
|
|
* integer, unsigned integer, and floating point types.
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-12-10 00:38:41 +00:00
|
|
|
pub trait Vector<T>: Dimensional<T> ToPtr<T> Eq {
|
2012-12-05 01:51:18 +00:00
|
|
|
/**
|
|
|
|
* Construct the vector from a single value, copying it to each component
|
|
|
|
*/
|
2012-11-20 07:58:24 +00:00
|
|
|
static pure fn from_value(value: T) -> self;
|
2012-12-04 11:42:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait MutableVector<T>: Vector<T> {
|
|
|
|
/**
|
|
|
|
* Get a mutable reference to the component at `i`
|
|
|
|
*/
|
|
|
|
fn index_mut(&mut self, i: uint) -> &self/mut T;
|
2012-12-04 07:58:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Swap two components of the vector in place
|
|
|
|
*/
|
|
|
|
fn swap(&mut self, a: uint, b: uint);
|
2012-11-20 07:58:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
|
|
|
* A generic 2-dimensional vector
|
|
|
|
*/
|
2012-11-21 04:08:36 +00:00
|
|
|
pub trait Vector2<T>: Vector<T> {
|
2012-12-03 16:32:40 +00:00
|
|
|
// static pure fn new(x: T, y: T) -> self;
|
2012-11-21 04:08:36 +00:00
|
|
|
}
|
2012-11-20 07:58:24 +00:00
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
|
|
|
* A generic 3-dimensional vector
|
|
|
|
*/
|
2012-11-21 04:08:36 +00:00
|
|
|
pub trait Vector3<T>: Vector<T> {
|
2012-12-03 16:32:40 +00:00
|
|
|
// static pure fn new(x: T, y: T, z: T) -> self;
|
2012-11-21 04:08:36 +00:00
|
|
|
}
|
2012-11-20 07:58:24 +00:00
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
|
|
|
* A generic 4-dimensional vector
|
|
|
|
*/
|
2012-11-21 04:08:36 +00:00
|
|
|
pub trait Vector4<T>: Vector<T> {
|
2012-12-05 01:51:18 +00:00
|
|
|
// static pure fn new(x: T, y: T, z: T, w: T) -> self;
|
2012-11-21 04:08:36 +00:00
|
|
|
}
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
|
|
|
* A vector with numeric components
|
|
|
|
*/
|
2012-12-10 00:38:41 +00:00
|
|
|
pub trait NumericVector<T>: Vector<T> Neg<self> {
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
2012-12-05 02:10:05 +00:00
|
|
|
* The standard basis vector
|
|
|
|
*
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
2012-12-05 02:10:05 +00:00
|
|
|
*
|
|
|
|
* A vector with each component set to one
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-20 06:57:32 +00:00
|
|
|
static pure fn identity() -> self;
|
2012-12-03 16:32:40 +00:00
|
|
|
|
|
|
|
/**
|
2012-12-05 02:10:05 +00:00
|
|
|
* The null vector
|
|
|
|
*
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
2012-12-05 02:10:05 +00:00
|
|
|
*
|
|
|
|
* A vector with each component set to zero
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-20 06:57:32 +00:00
|
|
|
static pure fn zero() -> self;
|
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The scalar multiplication of the vector and `value`
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn mul_t(&self, value: T) -> self;
|
2012-12-03 16:32:40 +00:00
|
|
|
|
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The scalar division of the vector and `value`
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn div_t(&self, value: T) -> self;
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The sum of the vector and `other`
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn add_v(&self, other: &self) -> self;
|
2012-12-03 16:32:40 +00:00
|
|
|
|
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The difference between the vector and `other`
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn sub_v(&self, other: &self) -> self;
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The dot product of the vector and `other`
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn dot(&self, other: &self) -> T;
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 13:32:34 +00:00
|
|
|
/**
|
|
|
|
* A mutable vector with numeric components
|
|
|
|
*/
|
2012-12-10 00:38:41 +00:00
|
|
|
pub trait MutableNumericVector<T>: MutableVector<&self/T> NumericVector<T> {
|
2012-12-04 13:32:34 +00:00
|
|
|
/**
|
|
|
|
* Negate the vector
|
|
|
|
*/
|
|
|
|
fn neg_self(&mut self);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multiply the vector by a scalar
|
|
|
|
*/
|
|
|
|
fn mul_self_t(&mut self, value: T);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Divide the vector by a scalar
|
|
|
|
*/
|
|
|
|
fn div_self_t(&mut self, value: T);
|
|
|
|
|
|
|
|
/**
|
2012-12-04 22:21:40 +00:00
|
|
|
* Set to the sum of the vector and `other`
|
2012-12-04 13:32:34 +00:00
|
|
|
*/
|
|
|
|
fn add_self_v(&mut self, other: &self);
|
|
|
|
|
|
|
|
/**
|
2012-12-04 22:21:40 +00:00
|
|
|
* Set to the difference between the vector and `other`
|
2012-12-04 13:32:34 +00:00
|
|
|
*/
|
|
|
|
fn sub_self_v(&mut self, other: &self);
|
|
|
|
}
|
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
|
|
|
* A 2-dimensional vector with numeric components
|
|
|
|
*/
|
2012-11-21 04:08:36 +00:00
|
|
|
pub trait NumericVector2<T>: NumericVector<T> {
|
2012-12-03 22:31:26 +00:00
|
|
|
// static pure fn unit_x() -> self;
|
|
|
|
// static pure fn unit_y() -> self;
|
2012-11-21 04:08:36 +00:00
|
|
|
}
|
2012-11-20 07:58:24 +00:00
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
|
|
|
* A 3-dimensional vector with numeric components
|
|
|
|
*/
|
2012-11-21 04:08:36 +00:00
|
|
|
pub trait NumericVector3<T>: NumericVector<T> {
|
2012-12-03 22:31:26 +00:00
|
|
|
// static pure fn unit_x() -> self;
|
|
|
|
// static pure fn unit_y() -> self;
|
|
|
|
// static pure fn unit_z() -> self;
|
2012-12-04 22:21:40 +00:00
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The cross product of the vector and `other`
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn cross(&self, other: &self) -> self;
|
2012-11-20 07:58:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 15:34:41 +00:00
|
|
|
/**
|
|
|
|
* A mutable 3-dimensional vector with numeric components
|
|
|
|
*/
|
|
|
|
pub trait MutableNumericVector3<T>: MutableNumericVector<&self/T> {
|
|
|
|
/**
|
|
|
|
* Set to the cross product of the vector and `other`
|
|
|
|
*/
|
|
|
|
fn cross_self(&mut self, other: &self);
|
|
|
|
}
|
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
|
|
|
* A 4-dimensional vector with numeric components
|
|
|
|
*/
|
2012-11-21 04:08:36 +00:00
|
|
|
pub trait NumericVector4<T>: NumericVector<T> {
|
2012-12-03 22:31:26 +00:00
|
|
|
// static pure fn unit_x() -> self;
|
|
|
|
// static pure fn unit_y() -> self;
|
|
|
|
// static pure fn unit_z() -> self;
|
|
|
|
// static pure fn unit_w() -> self;
|
2012-11-21 04:08:36 +00:00
|
|
|
}
|
2012-11-20 07:58:24 +00:00
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
2012-12-05 02:10:05 +00:00
|
|
|
* A Euclidean (or Affine) vector
|
|
|
|
*
|
|
|
|
* # Type parameters
|
|
|
|
*
|
|
|
|
* * `T` - The type of the components. This should be a floating point type.
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-12-05 02:10:05 +00:00
|
|
|
pub trait EuclideanVector<T>: NumericVector<T> {
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The squared length of the vector. This is useful for comparisons where
|
|
|
|
* the exact length does not need to be calculated.
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn length2(&self) -> T;
|
2012-12-03 16:32:40 +00:00
|
|
|
|
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The length of the vector
|
|
|
|
*
|
|
|
|
* # Performance notes
|
|
|
|
*
|
|
|
|
* For instances where the exact length of the vector does not need to be
|
|
|
|
* known, for example for quaternion-quaternion length comparisons,
|
|
|
|
* it is advisable to use the `length2` method instead.
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn length(&self) -> T;
|
2012-12-03 16:32:40 +00:00
|
|
|
|
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The squared distance between the vector and `other`.
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn distance2(&self, other: &self) -> T;
|
2012-12-03 16:32:40 +00:00
|
|
|
|
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The distance between the vector and `other`
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn distance(&self, other: &self) -> T;
|
2012-12-03 16:32:40 +00:00
|
|
|
|
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The normalized vector
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn normalize(&self) -> self;
|
2012-12-03 16:32:40 +00:00
|
|
|
|
|
|
|
/**
|
2012-12-03 22:31:26 +00:00
|
|
|
* Linearly intoperlate between the vector and `other`
|
2012-12-05 08:09:53 +00:00
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The intoperlated vector
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn lerp(&self, other: &self, amount: T) -> self;
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 02:10:05 +00:00
|
|
|
/**
|
|
|
|
* A mutable Euclidean (or Affine) vector
|
|
|
|
*
|
|
|
|
* # Type parameters
|
|
|
|
*
|
|
|
|
* * `T` - The type of the components. This should be a floating point type.
|
|
|
|
*/
|
2012-12-10 00:38:41 +00:00
|
|
|
pub trait MutableEuclideanVector<T>: MutableNumericVector<&self/T>
|
2012-12-05 02:10:05 +00:00
|
|
|
EuclideanVector<T> {
|
2012-12-04 22:21:40 +00:00
|
|
|
/**
|
|
|
|
* Normalize the vector
|
|
|
|
*/
|
|
|
|
fn normalize_self(&mut self);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Linearly intoperlate the vector towards `other`
|
|
|
|
*/
|
|
|
|
fn lerp_self(&mut self, other: &self, amount: T);
|
|
|
|
}
|
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* A 2-dimensional vector
|
|
|
|
*
|
|
|
|
* # Type parameters
|
|
|
|
*
|
|
|
|
* * `T` - The type of the components. This is intended to support boolean,
|
|
|
|
* integer, unsigned integer, and floating point types.
|
|
|
|
*
|
|
|
|
* # Fields
|
|
|
|
*
|
|
|
|
* * `x` - the first component of the vector
|
|
|
|
* * `y` - the second component of the vector
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
pub struct Vec2<T> { x: T, y: T }
|
|
|
|
|
2012-11-20 07:58:24 +00:00
|
|
|
pub impl<T> Vec2<T>/*: Vector2<T>*/ {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-20 07:58:24 +00:00
|
|
|
static pure fn new(x: T, y: T ) -> Vec2<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec2 { x: move x, y: move y }
|
|
|
|
}
|
2012-11-20 07:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy> Vec2<T>: Vector<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-20 07:58:24 +00:00
|
|
|
static pure fn from_value(value: T) -> Vec2<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec2::new(value, value)
|
|
|
|
}
|
2012-12-01 04:19:21 +00:00
|
|
|
}
|
|
|
|
|
2012-12-01 04:55:45 +00:00
|
|
|
pub impl<T> Vec2<T>: Dimensional<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn dim() -> uint { 2 }
|
|
|
|
|
2012-11-25 12:05:47 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn size_of() -> uint { size_of::<Vec2<T>>() }
|
2012-12-01 04:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy> Vec2<T>: Index<uint, T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-12-08 00:00:50 +00:00
|
|
|
pure fn index(&self, i: uint) -> T {
|
2012-12-04 07:50:15 +00:00
|
|
|
unsafe { do buf_as_slice(self.to_ptr(), 2) |slice| { slice[i] } }
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-01 04:55:45 +00:00
|
|
|
|
|
|
|
pub impl<T:Copy> Vec2<T>: ToPtr<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn to_ptr(&self) -> *T {
|
2012-12-04 07:50:15 +00:00
|
|
|
unsafe {
|
|
|
|
transmute::<*Vec2<T>, *T>(
|
2012-12-08 00:00:50 +00:00
|
|
|
to_unsafe_ptr(self)
|
2012-12-04 07:50:15 +00:00
|
|
|
)
|
|
|
|
}
|
2012-12-01 04:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-04 11:42:24 +00:00
|
|
|
|
|
|
|
pub impl<T:Copy> Vec2<T>: MutableVector<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn index_mut(&mut self, i: uint) -> &self/mut T {
|
|
|
|
match i {
|
|
|
|
0 => &mut self.x,
|
|
|
|
1 => &mut self.y,
|
|
|
|
_ => fail(fmt!("index out of bounds: expected an index from 0 to 1, but found %u", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn swap(&mut self, a: uint, b: uint) {
|
|
|
|
util::swap(self.index_mut(a),
|
|
|
|
self.index_mut(b));
|
|
|
|
}
|
|
|
|
}
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Number> Vec2<T>: NumericVector<T> {
|
2012-11-20 06:57:32 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn identity() -> Vec2<T> {
|
2012-12-03 05:39:32 +00:00
|
|
|
Vec2::new(Number::one(),
|
|
|
|
Number::one())
|
2012-11-20 06:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
static pure fn zero() -> Vec2<T> {
|
2012-12-03 05:39:32 +00:00
|
|
|
Vec2::new(Number::zero(),
|
|
|
|
Number::zero())
|
2012-11-20 06:57:32 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn mul_t(&self, value: T) -> Vec2<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec2::new(self[0] * value,
|
|
|
|
self[1] * value)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn div_t(&self, value: T) -> Vec2<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec2::new(self[0] / value,
|
|
|
|
self[1] / value)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn add_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec2::new(self[0] + other[0],
|
|
|
|
self[1] + other[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn sub_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec2::new(self[0] - other[0],
|
|
|
|
self[1] - other[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn dot(&self, other: &Vec2<T>) -> T {
|
2012-11-15 02:23:39 +00:00
|
|
|
self[0] * other[0] +
|
|
|
|
self[1] * other[1]
|
|
|
|
}
|
|
|
|
}
|
2012-12-01 04:19:21 +00:00
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Number> Vec2<T>: Neg<Vec2<T>> {
|
2012-12-01 04:19:21 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn neg(&self) -> Vec2<T> {
|
|
|
|
Vec2::new(-self[0], -self[1])
|
|
|
|
}
|
|
|
|
}
|
2012-12-04 13:32:34 +00:00
|
|
|
|
|
|
|
pub impl<T:Copy Number> Vec2<T>: MutableNumericVector<&self/T> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn neg_self(&mut self) {
|
|
|
|
*self.index_mut(0) = -*self.index_mut(0);
|
|
|
|
*self.index_mut(1) = -*self.index_mut(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn mul_self_t(&mut self, value: &T) {
|
|
|
|
*self.index_mut(0) *= (*value);
|
|
|
|
*self.index_mut(1) *= (*value);
|
|
|
|
}
|
2012-11-15 02:23:39 +00:00
|
|
|
|
2012-12-04 13:32:34 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn div_self_t(&mut self, value: &T) {
|
|
|
|
*self.index_mut(0) /= (*value);
|
|
|
|
*self.index_mut(1) /= (*value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn add_self_v(&mut self, other: &Vec2<T>) {
|
|
|
|
*self.index_mut(0) += other[0];
|
|
|
|
*self.index_mut(1) += other[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn sub_self_v(&mut self, other: &Vec2<T>) {
|
|
|
|
*self.index_mut(0) -= other[0];
|
|
|
|
*self.index_mut(1) -= other[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 02:10:05 +00:00
|
|
|
pub impl<T:Copy Number Exp> Vec2<T>: EuclideanVector<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn length2(&self) -> T {
|
|
|
|
self.dot(self)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn length(&self) -> T {
|
2012-11-15 02:23:39 +00:00
|
|
|
self.length2().sqrt()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn distance2(&self, other: &Vec2<T>) -> T {
|
|
|
|
other.sub_v(self).length2()
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn distance(&self, other: &Vec2<T>) -> T {
|
|
|
|
other.distance2(self).sqrt()
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn normalize(&self) -> Vec2<T> {
|
2012-12-03 06:28:32 +00:00
|
|
|
let mut n: T = Number::from(1);
|
2012-11-15 02:23:39 +00:00
|
|
|
n /= self.length();
|
|
|
|
return self.mul_t(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn lerp(&self, other: &Vec2<T>, amount: T) -> Vec2<T> {
|
|
|
|
self.add_v(&other.sub_v(self).mul_t(amount))
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 02:10:05 +00:00
|
|
|
pub impl<T:Copy Number Exp> Vec2<T>: MutableEuclideanVector<&self/T> {
|
2012-12-04 22:21:40 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn normalize_self(&mut self) {
|
|
|
|
let mut n: T = Number::from(1);
|
|
|
|
n /= self.length();
|
|
|
|
self.mul_self_t(&n);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lerp_self(&mut self, other: &Vec2<T>, amount: &T) {
|
|
|
|
self.add_self_v(&other.sub_v(&*self).mul_t(*amount));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-06 16:19:48 +00:00
|
|
|
pub impl<T:Copy Eq> Vec2<T>: Eq {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-29 03:14:42 +00:00
|
|
|
pure fn eq(&self, other: &Vec2<T>) -> bool {
|
2012-12-06 16:19:48 +00:00
|
|
|
self[0] == other[0] &&
|
|
|
|
self[1] == other[1]
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-29 03:14:42 +00:00
|
|
|
pure fn ne(&self, other: &Vec2<T>) -> bool {
|
|
|
|
!(self == other)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy FuzzyEq> Vec2<T>: FuzzyEq {
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn fuzzy_eq(other: &Vec2<T>) -> bool {
|
|
|
|
self[0].fuzzy_eq(&other[0]) &&
|
|
|
|
self[1].fuzzy_eq(&other[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* A 3-dimensional vector
|
|
|
|
*
|
|
|
|
* # Type parameters
|
|
|
|
*
|
|
|
|
* * `T` - The type of the components. This is intended to support boolean,
|
|
|
|
* integer, unsigned integer, and floating point types.
|
|
|
|
*
|
|
|
|
* # Fields
|
|
|
|
*
|
|
|
|
* * `x` - the first component of the vector
|
|
|
|
* * `y` - the second component of the vector
|
|
|
|
* * `z` - the third component of the vector
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
pub struct Vec3<T> { x: T, y: T, z: T }
|
|
|
|
|
2012-11-20 07:58:24 +00:00
|
|
|
pub impl<T> Vec3<T>/*: Vector3<T>*/ {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-20 07:58:24 +00:00
|
|
|
static pure fn new(x: T, y: T, z: T) -> Vec3<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec3 { x: move x, y: move y, z: move z }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-20 07:58:24 +00:00
|
|
|
pub impl<T:Copy> Vec3<T>: Vector<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-20 07:58:24 +00:00
|
|
|
static pure fn from_value(value: T) -> Vec3<T> {
|
|
|
|
Vec3::new(value, value, value)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
2012-12-01 04:19:21 +00:00
|
|
|
}
|
|
|
|
|
2012-12-01 04:55:45 +00:00
|
|
|
pub impl<T> Vec3<T>: Dimensional<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn dim() -> uint { 3 }
|
|
|
|
|
2012-11-25 12:05:47 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn size_of() -> uint { size_of::<Vec3<T>>() }
|
2012-12-01 04:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy> Vec3<T>: Index<uint, T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-12-08 00:00:50 +00:00
|
|
|
pure fn index(&self, i: uint) -> T {
|
2012-12-04 07:50:15 +00:00
|
|
|
unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } }
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-01 04:55:45 +00:00
|
|
|
pub impl<T:Copy> Vec3<T>: ToPtr<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn to_ptr(&self) -> *T {
|
2012-12-04 07:50:15 +00:00
|
|
|
unsafe {
|
|
|
|
transmute::<*Vec3<T>, *T>(
|
2012-12-08 00:00:50 +00:00
|
|
|
to_unsafe_ptr(self)
|
2012-12-04 07:50:15 +00:00
|
|
|
)
|
|
|
|
}
|
2012-12-01 04:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 11:42:24 +00:00
|
|
|
pub impl<T:Copy> Vec3<T>: MutableVector<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn index_mut(&mut self, i: uint) -> &self/mut T {
|
|
|
|
match i {
|
|
|
|
0 => &mut self.x,
|
|
|
|
1 => &mut self.y,
|
|
|
|
2 => &mut self.z,
|
|
|
|
_ => fail(fmt!("index out of bounds: expected an index from 0 to 2, but found %u", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn swap(&mut self, a: uint, b: uint) {
|
|
|
|
util::swap(self.index_mut(a),
|
|
|
|
self.index_mut(b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Number> Vec3<T>: NumericVector<T> {
|
2012-11-20 06:57:32 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn identity() -> Vec3<T> {
|
2012-12-03 05:39:32 +00:00
|
|
|
Vec3::new(Number::one(),
|
|
|
|
Number::one(),
|
|
|
|
Number::one())
|
2012-11-20 06:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
static pure fn zero() -> Vec3<T> {
|
2012-12-03 05:39:32 +00:00
|
|
|
Vec3::new(Number::zero(),
|
|
|
|
Number::zero(),
|
|
|
|
Number::zero())
|
2012-11-20 06:57:32 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn mul_t(&self, value: T) -> Vec3<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec3::new(self[0] * value,
|
|
|
|
self[1] * value,
|
|
|
|
self[2] * value)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn div_t(&self, value: T) -> Vec3<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec3::new(self[0] / value,
|
|
|
|
self[1] / value,
|
|
|
|
self[2] / value)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn add_v(&self, other: &Vec3<T>) -> Vec3<T>{
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec3::new(self[0] + other[0],
|
|
|
|
self[1] + other[1],
|
|
|
|
self[2] + other[2])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn sub_v(&self, other: &Vec3<T>) -> Vec3<T>{
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec3::new(self[0] - other[0],
|
|
|
|
self[1] - other[1],
|
|
|
|
self[2] - other[2])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn dot(&self, other: &Vec3<T>) -> T {
|
2012-11-15 02:23:39 +00:00
|
|
|
self[0] * other[0] +
|
|
|
|
self[1] * other[1] +
|
|
|
|
self[2] * other[2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Number> Vec3<T>: Neg<Vec3<T>> {
|
2012-12-01 04:19:21 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn neg(&self) -> Vec3<T> {
|
|
|
|
Vec3::new(-self[0], -self[1], -self[2])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 13:32:34 +00:00
|
|
|
pub impl<T:Copy Number> Vec3<T>: MutableNumericVector<&self/T> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn neg_self(&mut self) {
|
|
|
|
*self.index_mut(0) = -*self.index_mut(0);
|
|
|
|
*self.index_mut(1) = -*self.index_mut(1);
|
|
|
|
*self.index_mut(2) = -*self.index_mut(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn mul_self_t(&mut self, value: &T) {
|
|
|
|
*self.index_mut(0) *= (*value);
|
|
|
|
*self.index_mut(1) *= (*value);
|
|
|
|
*self.index_mut(2) *= (*value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn div_self_t(&mut self, value: &T) {
|
|
|
|
*self.index_mut(0) /= (*value);
|
|
|
|
*self.index_mut(1) /= (*value);
|
|
|
|
*self.index_mut(2) /= (*value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn add_self_v(&mut self, other: &Vec3<T>) {
|
|
|
|
*self.index_mut(0) += other[0];
|
|
|
|
*self.index_mut(1) += other[1];
|
|
|
|
*self.index_mut(2) += other[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn sub_self_v(&mut self, other: &Vec3<T>) {
|
|
|
|
*self.index_mut(0) -= other[0];
|
|
|
|
*self.index_mut(1) -= other[1];
|
|
|
|
*self.index_mut(2) -= other[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 15:34:41 +00:00
|
|
|
pub impl<T:Copy Number> Vec3<T>: NumericVector3<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn cross(&self, 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 Number> Vec3<T>: MutableNumericVector3<&self/T> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn cross_self(&mut self, other: &Vec3<T>) {
|
|
|
|
*self = self.cross(other);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 02:10:05 +00:00
|
|
|
pub impl<T:Copy Number Exp> Vec3<T>: EuclideanVector<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn length2(&self) -> T {
|
|
|
|
self.dot(self)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn length(&self) -> T {
|
2012-11-15 02:23:39 +00:00
|
|
|
self.length2().sqrt()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn distance2(&self, other: &Vec3<T>) -> T {
|
|
|
|
other.sub_v(self).length2()
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn distance(&self, other: &Vec3<T>) -> T {
|
|
|
|
other.distance2(self).sqrt()
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn normalize(&self) -> Vec3<T> {
|
2012-12-03 06:28:32 +00:00
|
|
|
let mut n: T = Number::from(1);
|
2012-11-15 02:23:39 +00:00
|
|
|
n /= self.length();
|
|
|
|
return self.mul_t(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn lerp(&self, other: &Vec3<T>, amount: T) -> Vec3<T> {
|
|
|
|
self.add_v(&other.sub_v(self).mul_t(amount))
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 02:10:05 +00:00
|
|
|
pub impl<T:Copy Number Exp> Vec3<T>: MutableEuclideanVector<&self/T> {
|
2012-12-04 22:21:40 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn normalize_self(&mut self) {
|
|
|
|
let mut n: T = Number::from(1);
|
|
|
|
n /= self.length();
|
|
|
|
self.mul_self_t(&n);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lerp_self(&mut self, other: &Vec3<T>, amount: &T) {
|
|
|
|
self.add_self_v(&other.sub_v(&*self).mul_t(*amount));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-06 16:19:48 +00:00
|
|
|
pub impl<T:Copy Eq> Vec3<T>: Eq {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-29 03:14:42 +00:00
|
|
|
pure fn eq(&self, other: &Vec3<T>) -> bool {
|
2012-12-06 16:19:48 +00:00
|
|
|
self[0] == other[0] &&
|
|
|
|
self[1] == other[1] &&
|
|
|
|
self[2] == other[2]
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-29 03:14:42 +00:00
|
|
|
pure fn ne(&self, other: &Vec3<T>) -> bool {
|
|
|
|
!(self == other)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy FuzzyEq> Vec3<T>: FuzzyEq {
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn fuzzy_eq(other: &Vec3<T>) -> bool {
|
|
|
|
self[0].fuzzy_eq(&other[0]) &&
|
|
|
|
self[1].fuzzy_eq(&other[1]) &&
|
|
|
|
self[2].fuzzy_eq(&other[2])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-03 16:32:40 +00:00
|
|
|
/**
|
2012-12-05 08:09:53 +00:00
|
|
|
* A 4-dimensional vector
|
|
|
|
*
|
|
|
|
* # Type parameters
|
|
|
|
*
|
|
|
|
* * `T` - The type of the components. This is intended to support boolean,
|
|
|
|
* integer, unsigned integer, and floating point types.
|
|
|
|
*
|
|
|
|
* # Fields
|
|
|
|
*
|
|
|
|
* * `x` - the first component of the vector
|
|
|
|
* * `y` - the second component of the vector
|
|
|
|
* * `z` - the third component of the vector
|
|
|
|
* * `w` - the fourth component of the vector
|
2012-12-03 16:32:40 +00:00
|
|
|
*/
|
2012-11-15 02:23:39 +00:00
|
|
|
pub struct Vec4<T> { x: T, y: T, z: T, w: T }
|
|
|
|
|
2012-11-20 07:58:24 +00:00
|
|
|
pub impl<T> Vec4<T>/*: Vector4<T>*/ {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-20 07:58:24 +00:00
|
|
|
static pure fn new(x: T, y: T, z: T, w: T) -> Vec4<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec4 { x: move x, y: move y, z: move z, w: move w }
|
|
|
|
}
|
2012-11-20 07:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy> Vec4<T>: Vector<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-20 07:58:24 +00:00
|
|
|
static pure fn from_value(value: T) -> Vec4<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec4::new(value, value, value, value)
|
|
|
|
}
|
2012-12-01 04:19:21 +00:00
|
|
|
}
|
|
|
|
|
2012-12-01 04:55:45 +00:00
|
|
|
pub impl<T> Vec4<T>: Dimensional<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn dim() -> uint { 4 }
|
|
|
|
|
2012-11-25 12:05:47 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn size_of() -> uint { size_of::<Vec4<T>>() }
|
2012-12-01 04:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy> Vec4<T>: Index<uint, T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-12-08 00:00:50 +00:00
|
|
|
pure fn index(&self, i: uint) -> T {
|
2012-12-04 07:50:15 +00:00
|
|
|
unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } }
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-01 04:55:45 +00:00
|
|
|
pub impl<T:Copy> Vec4<T>: ToPtr<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn to_ptr(&self) -> *T {
|
2012-12-04 07:50:15 +00:00
|
|
|
unsafe {
|
|
|
|
transmute::<*Vec4<T>, *T>(
|
2012-12-08 00:00:50 +00:00
|
|
|
to_unsafe_ptr(self)
|
2012-12-04 07:50:15 +00:00
|
|
|
)
|
|
|
|
}
|
2012-12-01 04:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 11:42:24 +00:00
|
|
|
pub impl<T:Copy> Vec4<T>: MutableVector<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn index_mut(&mut self, i: uint) -> &self/mut T {
|
|
|
|
match i {
|
|
|
|
0 => &mut self.x,
|
|
|
|
1 => &mut self.y,
|
|
|
|
2 => &mut self.z,
|
|
|
|
3 => &mut self.w,
|
|
|
|
_ => fail(fmt!("index out of bounds: expected an index from 0 to 3, but found %u", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn swap(&mut self, a: uint, b: uint) {
|
|
|
|
util::swap(self.index_mut(a),
|
|
|
|
self.index_mut(b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Number> Vec4<T>: NumericVector<T> {
|
2012-11-20 06:57:32 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn identity() -> Vec4<T> {
|
2012-12-03 05:39:32 +00:00
|
|
|
Vec4::new(Number::one(),
|
|
|
|
Number::one(),
|
|
|
|
Number::one(),
|
|
|
|
Number::one())
|
2012-11-20 06:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
static pure fn zero() -> Vec4<T> {
|
2012-12-03 05:39:32 +00:00
|
|
|
Vec4::new(Number::zero(),
|
|
|
|
Number::zero(),
|
|
|
|
Number::zero(),
|
|
|
|
Number::zero())
|
2012-11-20 06:57:32 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn mul_t(&self, value: T) -> Vec4<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec4::new(self[0] * value,
|
|
|
|
self[1] * value,
|
|
|
|
self[2] * value,
|
|
|
|
self[3] * value)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn div_t(&self, value: T) -> Vec4<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec4::new(self[0] / value,
|
|
|
|
self[1] / value,
|
|
|
|
self[2] / value,
|
|
|
|
self[3] / value)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn add_v(&self, other: &Vec4<T>) -> Vec4<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec4::new(self[0] + other[0],
|
|
|
|
self[1] + other[1],
|
|
|
|
self[2] + other[2],
|
|
|
|
self[3] + other[3])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn sub_v(&self, other: &Vec4<T>) -> Vec4<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
Vec4::new(self[0] - other[0],
|
|
|
|
self[1] - other[1],
|
|
|
|
self[2] - other[2],
|
|
|
|
self[3] - other[3])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn dot(&self, other: &Vec4<T>) -> T {
|
2012-11-15 02:23:39 +00:00
|
|
|
self[0] * other[0] +
|
|
|
|
self[1] * other[1] +
|
|
|
|
self[2] * other[2] +
|
|
|
|
self[3] * other[3]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:10:14 +00:00
|
|
|
pub impl<T:Copy Number> Vec4<T>: Neg<Vec4<T>> {
|
2012-12-01 04:19:21 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn neg(&self) -> Vec4<T> {
|
|
|
|
Vec4::new(-self[0], -self[1], -self[2], -self[3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 13:32:34 +00:00
|
|
|
pub impl<T:Copy Number> Vec4<T>: MutableNumericVector<&self/T> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn neg_self(&mut self) {
|
|
|
|
*self.index_mut(0) = -*self.index_mut(0);
|
|
|
|
*self.index_mut(1) = -*self.index_mut(1);
|
|
|
|
*self.index_mut(2) = -*self.index_mut(2);
|
|
|
|
*self.index_mut(3) = -*self.index_mut(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn mul_self_t(&mut self, value: &T) {
|
|
|
|
*self.index_mut(0) *= (*value);
|
|
|
|
*self.index_mut(1) *= (*value);
|
|
|
|
*self.index_mut(2) *= (*value);
|
|
|
|
*self.index_mut(3) *= (*value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn div_self_t(&mut self, value: &T) {
|
|
|
|
*self.index_mut(0) /= (*value);
|
|
|
|
*self.index_mut(1) /= (*value);
|
|
|
|
*self.index_mut(2) /= (*value);
|
|
|
|
*self.index_mut(3) /= (*value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn add_self_v(&mut self, other: &Vec4<T>) {
|
|
|
|
*self.index_mut(0) += other[0];
|
|
|
|
*self.index_mut(1) += other[1];
|
|
|
|
*self.index_mut(2) += other[2];
|
|
|
|
*self.index_mut(3) += other[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn sub_self_v(&mut self, other: &Vec4<T>) {
|
|
|
|
*self.index_mut(0) -= other[0];
|
|
|
|
*self.index_mut(1) -= other[1];
|
|
|
|
*self.index_mut(2) -= other[2];
|
|
|
|
*self.index_mut(3) -= other[3];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 02:10:05 +00:00
|
|
|
pub impl<T:Copy Number Exp> Vec4<T>: EuclideanVector<T> {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn length2(&self) -> T {
|
|
|
|
self.dot(self)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn length(&self) -> T {
|
2012-11-15 02:23:39 +00:00
|
|
|
self.length2().sqrt()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn distance2(&self, other: &Vec4<T>) -> T {
|
|
|
|
other.sub_v(self).length2()
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn distance(&self, other: &Vec4<T>) -> T {
|
|
|
|
other.distance2(self).sqrt()
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn normalize(&self) -> Vec4<T> {
|
2012-12-03 06:28:32 +00:00
|
|
|
let mut n: T = Number::from(1);
|
2012-11-15 02:23:39 +00:00
|
|
|
n /= self.length();
|
|
|
|
return self.mul_t(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-30 03:13:20 +00:00
|
|
|
pure fn lerp(&self, other: &Vec4<T>, amount: T) -> Vec4<T> {
|
|
|
|
self.add_v(&other.sub_v(self).mul_t(amount))
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 02:10:05 +00:00
|
|
|
pub impl<T:Copy Number Exp> Vec4<T>: MutableEuclideanVector<&self/T> {
|
2012-12-04 22:21:40 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn normalize_self(&mut self) {
|
|
|
|
let mut n: T = Number::from(1);
|
|
|
|
n /= self.length();
|
|
|
|
self.mul_self_t(&n);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lerp_self(&mut self, other: &Vec4<T>, amount: &T) {
|
|
|
|
self.add_self_v(&other.sub_v(&*self).mul_t(*amount));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-06 16:19:48 +00:00
|
|
|
pub impl<T:Copy Eq> Vec4<T>: Eq {
|
2012-11-15 02:23:39 +00:00
|
|
|
#[inline(always)]
|
2012-11-29 03:14:42 +00:00
|
|
|
pure fn eq(&self, other: &Vec4<T>) -> bool {
|
2012-12-06 16:19:48 +00:00
|
|
|
self[0] == other[0] &&
|
|
|
|
self[1] == other[1] &&
|
|
|
|
self[2] == other[2] &&
|
|
|
|
self[3] == other[3]
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-11-29 03:14:42 +00:00
|
|
|
pure fn ne(&self, other: &Vec4<T>) -> bool {
|
|
|
|
!(self == other)
|
2012-11-15 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy FuzzyEq> Vec4<T>: FuzzyEq {
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn fuzzy_eq(other: &Vec4<T>) -> 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])
|
|
|
|
}
|
|
|
|
}
|