2012-12-13 13:01:42 +00:00
|
|
|
use core::cast::transmute;
|
2012-12-16 05:19:38 +00:00
|
|
|
use core::cmp::{Eq, Ord};
|
2012-12-13 13:01:42 +00:00
|
|
|
use core::ptr::to_unsafe_ptr;
|
2013-01-25 21:41:23 +00:00
|
|
|
use core::util::swap;
|
2012-12-13 13:01:42 +00:00
|
|
|
use core::vec::raw::buf_as_slice;
|
|
|
|
|
|
|
|
use std::cmp::FuzzyEq;
|
2013-01-27 22:22:15 +00:00
|
|
|
use numeric::*;
|
|
|
|
use numeric::number::Number::{zero,one};
|
2012-12-13 13:01:42 +00:00
|
|
|
|
2013-01-25 21:41:23 +00:00
|
|
|
use vec::{
|
|
|
|
Vec3,
|
|
|
|
Vector,
|
2013-01-27 23:33:02 +00:00
|
|
|
Vector2,
|
2013-01-25 21:41:23 +00:00
|
|
|
MutableVector,
|
|
|
|
NumericVector,
|
|
|
|
NumericVector2,
|
|
|
|
MutableNumericVector,
|
|
|
|
ToHomogeneous,
|
|
|
|
EuclideanVector,
|
|
|
|
MutableEuclideanVector,
|
|
|
|
EquableVector,
|
|
|
|
OrdinalVector,
|
|
|
|
BooleanVector,
|
|
|
|
};
|
|
|
|
|
2012-12-13 13:01:42 +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-20 05:18:23 +00:00
|
|
|
#[deriving_eq]
|
2012-12-13 13:01:42 +00:00
|
|
|
pub struct Vec2<T> { x: T, y: T }
|
|
|
|
|
2013-01-27 23:33:02 +00:00
|
|
|
pub impl<T> Vec2<T> {
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn new(x: T, y: T ) -> Vec2<T> {
|
2012-12-21 04:08:43 +00:00
|
|
|
Vec2 { x: x, y: y }
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)]
|
|
|
|
pure fn to_ptr(&self) -> *T {
|
|
|
|
unsafe {
|
|
|
|
transmute::<*Vec2<T>, *T>(
|
|
|
|
to_unsafe_ptr(self)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-27 23:33:02 +00:00
|
|
|
pub impl<T> Vec2<T>: Vector2<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
static pure fn new(x: T, y: T ) -> Vec2<T> {
|
|
|
|
Vec2 { x: x, y: y }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
pub impl<T:Copy> Vec2<T>: Index<uint, T> {
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn index(&self, i: uint) -> T {
|
|
|
|
unsafe { do buf_as_slice(self.to_ptr(), 2) |slice| { slice[i] } }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2013-01-25 21:41:23 +00:00
|
|
|
swap(self.index_mut(a),
|
|
|
|
self.index_mut(b));
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy Number> Vec2<T>: NumericVector<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
static pure fn identity() -> Vec2<T> {
|
2012-12-30 05:26:01 +00:00
|
|
|
Vec2::new(one(), one())
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
static pure fn zero() -> Vec2<T> {
|
2012-12-30 05:26:01 +00:00
|
|
|
Vec2::new(zero(), zero())
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 03:46:25 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn is_zero(&self) -> bool {
|
2012-12-30 05:26:01 +00:00
|
|
|
self[0] == zero() &&
|
|
|
|
self[1] == zero()
|
2012-12-24 03:46:25 +00:00
|
|
|
}
|
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn mul_t(&self, value: T) -> Vec2<T> {
|
|
|
|
Vec2::new(self[0] * value,
|
|
|
|
self[1] * value)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn div_t(&self, value: T) -> Vec2<T> {
|
|
|
|
Vec2::new(self[0] / value,
|
|
|
|
self[1] / value)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn add_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
|
|
|
Vec2::new(self[0] + other[0],
|
|
|
|
self[1] + other[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn sub_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
|
|
|
Vec2::new(self[0] - other[0],
|
|
|
|
self[1] - other[1])
|
|
|
|
}
|
|
|
|
|
2012-12-28 08:47:10 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn mul_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
|
|
|
Vec2::new(self[0] * other[0],
|
|
|
|
self[1] * other[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn div_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
|
|
|
Vec2::new(self[0] / other[0],
|
|
|
|
self[1] / other[1])
|
|
|
|
}
|
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn dot(&self, other: &Vec2<T>) -> T {
|
|
|
|
self[0] * other[0] +
|
|
|
|
self[1] * other[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy Number> Vec2<T>: Neg<Vec2<T>> {
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn neg(&self) -> Vec2<T> {
|
|
|
|
Vec2::new(-self[0], -self[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-27 22:50:49 +00:00
|
|
|
pub impl<T:Copy Number> Vec2<T>: NumericVector2<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
static pure fn unit_x() -> Vec2<T> {
|
|
|
|
Vec2::new(one(), zero())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
static pure fn unit_y() -> Vec2<T> {
|
|
|
|
Vec2::new(zero(), one())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn perp_dot(&self, other: &Vec2<T>) ->T {
|
|
|
|
(self[0] * other[1]) - (self[1] * other[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-13 13:01:42 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-28 08:47:10 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn mul_self_v(&mut self, other: &Vec2<T>) {
|
|
|
|
*self.index_mut(0) *= other[0];
|
|
|
|
*self.index_mut(1) *= other[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn div_self_v(&mut self, other: &Vec2<T>) {
|
|
|
|
*self.index_mut(0) /= other[0];
|
|
|
|
*self.index_mut(1) /= other[1];
|
|
|
|
}
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 06:59:24 +00:00
|
|
|
pub impl<T:Copy Number> Vec2<T>: ToHomogeneous<Vec3<T>> {
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn to_homogeneous(&self) -> Vec3<T> {
|
|
|
|
Vec3::new(self.x, self.y, zero())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-16 05:05:04 +00:00
|
|
|
pub impl<T:Copy Float> Vec2<T>: EuclideanVector<T> {
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn length2(&self) -> T {
|
|
|
|
self.dot(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn length(&self) -> T {
|
|
|
|
self.length2().sqrt()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn distance2(&self, other: &Vec2<T>) -> T {
|
|
|
|
other.sub_v(self).length2()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn distance(&self, other: &Vec2<T>) -> T {
|
|
|
|
other.distance2(self).sqrt()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2013-01-27 22:22:15 +00:00
|
|
|
pure fn angle(&self, other: &Vec2<T>) -> T {
|
|
|
|
atan2(self.perp_dot(other), self.dot(other))
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn normalize(&self) -> Vec2<T> {
|
2012-12-30 05:26:01 +00:00
|
|
|
self.mul_t(one::<T>()/self.length())
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn normalize_to(&self, length: T) -> Vec2<T> {
|
2012-12-30 05:26:01 +00:00
|
|
|
self.mul_t(length / self.length())
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn lerp(&self, other: &Vec2<T>, amount: T) -> Vec2<T> {
|
|
|
|
self.add_v(&other.sub_v(self).mul_t(amount))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-16 05:05:04 +00:00
|
|
|
pub impl<T:Copy Float> Vec2<T>: MutableEuclideanVector<&self/T> {
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn normalize_self(&mut self) {
|
2012-12-30 05:26:01 +00:00
|
|
|
let n = one::<T>() / self.length();
|
2012-12-13 13:01:42 +00:00
|
|
|
self.mul_self_t(&n);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn normalize_self_to(&mut self, length: &T) {
|
2012-12-30 05:26:01 +00:00
|
|
|
let n = length / self.length();
|
2012-12-13 13:01:42 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-16 05:19:38 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 05:35:03 +00:00
|
|
|
pub impl<T:Copy Ord> Vec2<T>: OrdinalVector<T, Vec2<bool>> {
|
2012-12-16 05:19:38 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn less_than(&self, other: &Vec2<T>) -> Vec2<bool> {
|
|
|
|
Vec2::new(self[0] < other[0],
|
|
|
|
self[1] < other[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn less_than_equal(&self, other: &Vec2<T>) -> Vec2<bool> {
|
|
|
|
Vec2::new(self[0] <= other[0],
|
|
|
|
self[1] <= other[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn greater_than(&self, other: &Vec2<T>) -> Vec2<bool> {
|
|
|
|
Vec2::new(self[0] > other[0],
|
|
|
|
self[1] > other[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn greater_than_equal(&self, other: &Vec2<T>) -> Vec2<bool> {
|
|
|
|
Vec2::new(self[0] >= other[0],
|
|
|
|
self[1] >= other[1])
|
|
|
|
}
|
2012-12-17 05:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub impl<T:Copy Eq> Vec2<T>: EquableVector<T, Vec2<bool>> {
|
2012-12-16 05:19:38 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn equal(&self, other: &Vec2<T>) -> Vec2<bool> {
|
|
|
|
Vec2::new(self[0] == other[0],
|
|
|
|
self[1] == other[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn not_equal(&self, other: &Vec2<T>) -> Vec2<bool> {
|
|
|
|
Vec2::new(self[0] != other[0],
|
|
|
|
self[1] != other[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub impl Vec2<bool>: BooleanVector {
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn any(&self) -> bool {
|
|
|
|
self[0] || self[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn all(&self) -> bool {
|
|
|
|
self[0] && self[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn not(&self) -> Vec2<bool> {
|
|
|
|
Vec2::new(!self[0], !self[1])
|
|
|
|
}
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|