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-29 01:13:44 +00:00
|
|
|
use core::sys::size_of;
|
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;
|
|
|
|
|
2013-02-09 22:42:06 +00:00
|
|
|
use std::cmp::{FuzzyEq, FUZZY_EPSILON};
|
2013-01-27 22:22:15 +00:00
|
|
|
use numeric::*;
|
2013-02-09 22:42:06 +00:00
|
|
|
use numeric::number::Number;
|
2013-01-27 22:22:15 +00:00
|
|
|
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-29 09:26:48 +00:00
|
|
|
Vector3,
|
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-03-28 09:45:43 +00:00
|
|
|
impl<T:Copy + Eq> Vector<T> for Vec2<T> {
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn from_value(value: T) -> Vec2<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(value, value)
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn to_ptr(&self) -> *T {
|
|
|
|
unsafe {
|
|
|
|
transmute::<*Vec2<T>, *T>(
|
|
|
|
to_unsafe_ptr(self)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl<T> Vector2<T> for Vec2<T> {
|
2013-01-27 23:33:02 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn new(x: T, y: T ) -> Vec2<T> {
|
|
|
|
Vec2 { x: x, y: y }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl<T:Copy + Eq> Index<uint, T> for Vec2<T> {
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn index(&self, i: uint) -> T {
|
|
|
|
unsafe { do buf_as_slice(self.to_ptr(), 2) |slice| { slice[i] } }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl<T:Copy> MutableVector<T> for Vec2<T> {
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn index_mut(&mut self, i: uint) -> &self/mut T {
|
|
|
|
match i {
|
|
|
|
0 => &mut self.x,
|
|
|
|
1 => &mut self.y,
|
2013-02-06 21:26:33 +00:00
|
|
|
_ => fail!(fmt!("index out of bounds: expected an index from 0 to 1, but found %u", i))
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[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
|
|
|
}
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
|
|
|
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> NumericVector<T> for Vec2<T> {
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn identity() -> Vec2<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(one::<T>(), one::<T>())
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn zero() -> Vec2<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(zero::<T>(), zero::<T>())
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +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
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn mul_t(&self, value: T) -> Vec2<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(self[0] * value,
|
|
|
|
self[1] * value)
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn div_t(&self, value: T) -> Vec2<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(self[0] / value,
|
|
|
|
self[1] / value)
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn add_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(self[0] + other[0],
|
|
|
|
self[1] + other[1])
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn sub_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(self[0] - other[0],
|
|
|
|
self[1] - other[1])
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 08:47:10 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn mul_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(self[0] * other[0],
|
|
|
|
self[1] * other[1])
|
2012-12-28 08:47:10 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 08:47:10 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn div_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(self[0] / other[0],
|
|
|
|
self[1] / other[1])
|
2012-12-28 08:47:10 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Neg<Vec2<T>> for Vec2<T> {
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn neg(&self) -> Vec2<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(-self[0], -self[1])
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> NumericVector2<T> for Vec2<T> {
|
2013-01-27 22:50:49 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn unit_x() -> Vec2<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(one::<T>(), zero::<T>())
|
2013-01-27 22:50:49 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-27 22:50:49 +00:00
|
|
|
#[inline(always)]
|
|
|
|
static pure fn unit_y() -> Vec2<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(zero::<T>(), one::<T>())
|
2013-01-27 22:50:49 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-27 22:50:49 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn perp_dot(&self, other: &Vec2<T>) ->T {
|
|
|
|
(self[0] * other[1]) - (self[1] * other[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableNumericVector<&self/T> for Vec2<T> {
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn neg_self(&mut self) {
|
|
|
|
*self.index_mut(0) = -*self.index_mut(0);
|
|
|
|
*self.index_mut(1) = -*self.index_mut(1);
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn mul_self_t(&mut self, value: &T) {
|
|
|
|
*self.index_mut(0) *= (*value);
|
|
|
|
*self.index_mut(1) *= (*value);
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn div_self_t(&mut self, value: &T) {
|
|
|
|
*self.index_mut(0) /= (*value);
|
|
|
|
*self.index_mut(1) /= (*value);
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn add_self_v(&mut self, other: &Vec2<T>) {
|
|
|
|
*self.index_mut(0) += other[0];
|
|
|
|
*self.index_mut(1) += other[1];
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn sub_self_v(&mut self, other: &Vec2<T>) {
|
|
|
|
*self.index_mut(0) -= other[0];
|
|
|
|
*self.index_mut(1) -= other[1];
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
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];
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-28 08:47:10 +00:00
|
|
|
#[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-03-28 09:45:43 +00:00
|
|
|
impl<T:Copy + Number> ToHomogeneous<Vec3<T>> for Vec2<T> {
|
2013-01-02 06:59:24 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn to_homogeneous(&self) -> Vec3<T> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector3::new(self.x, self.y, zero())
|
2013-01-02 06:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl<T:Copy + Float + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> EuclideanVector<T> for Vec2<T> {
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn length2(&self) -> T {
|
|
|
|
self.dot(self)
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn length(&self) -> T {
|
|
|
|
self.length2().sqrt()
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn distance2(&self, other: &Vec2<T>) -> T {
|
|
|
|
other.sub_v(self).length2()
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn distance(&self, other: &Vec2<T>) -> T {
|
|
|
|
other.distance2(self).sqrt()
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[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
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
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
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
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
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl<T:Copy + Float + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableEuclideanVector<&self/T> for Vec2<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);
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
#[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);
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-13 13:01:42 +00:00
|
|
|
fn lerp_self(&mut self, other: &Vec2<T>, amount: &T) {
|
|
|
|
self.add_self_v(&other.sub_v(&*self).mul_t(*amount));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl<T:Copy + Float + FuzzyEq<T>> FuzzyEq<T> for Vec2<T> {
|
2012-12-13 13:01:42 +00:00
|
|
|
#[inline(always)]
|
2013-02-06 21:31:52 +00:00
|
|
|
pure fn fuzzy_eq(&self, other: &Vec2<T>) -> bool {
|
2013-02-09 22:42:06 +00:00
|
|
|
self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-02-09 22:42:06 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn fuzzy_eq_eps(&self, other: &Vec2<T>, epsilon: &T) -> bool {
|
|
|
|
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
|
|
|
|
self[1].fuzzy_eq_eps(&other[1], epsilon)
|
2012-12-13 13:01:42 +00:00
|
|
|
}
|
2012-12-16 05:19:38 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl<T:Copy + Ord + Eq> OrdinalVector<T, Vec2<bool>> for Vec2<T> {
|
2012-12-16 05:19:38 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn less_than(&self, other: &Vec2<T>) -> Vec2<bool> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(self[0] < other[0],
|
|
|
|
self[1] < other[1])
|
2012-12-16 05:19:38 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-16 05:19:38 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn less_than_equal(&self, other: &Vec2<T>) -> Vec2<bool> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(self[0] <= other[0],
|
|
|
|
self[1] <= other[1])
|
2012-12-16 05:19:38 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-16 05:19:38 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn greater_than(&self, other: &Vec2<T>) -> Vec2<bool> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(self[0] > other[0],
|
|
|
|
self[1] > other[1])
|
2012-12-16 05:19:38 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-16 05:19:38 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn greater_than_equal(&self, other: &Vec2<T>) -> Vec2<bool> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(self[0] >= other[0],
|
|
|
|
self[1] >= other[1])
|
2012-12-16 05:19:38 +00:00
|
|
|
}
|
2012-12-17 05:35:03 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl<T:Copy + Eq> EquableVector<T, Vec2<bool>> for Vec2<T> {
|
2012-12-16 05:19:38 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn equal(&self, other: &Vec2<T>) -> Vec2<bool> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(self[0] == other[0],
|
|
|
|
self[1] == other[1])
|
2012-12-16 05:19:38 +00:00
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-16 05:19:38 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn not_equal(&self, other: &Vec2<T>) -> Vec2<bool> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(self[0] != other[0],
|
|
|
|
self[1] != other[1])
|
2012-12-16 05:19:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl BooleanVector for Vec2<bool> {
|
2012-12-16 05:19:38 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn any(&self) -> bool {
|
|
|
|
self[0] || self[1]
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-16 05:19:38 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn all(&self) -> bool {
|
|
|
|
self[0] && self[1]
|
|
|
|
}
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2012-12-16 05:19:38 +00:00
|
|
|
#[inline(always)]
|
2013-03-28 09:45:43 +00:00
|
|
|
pure fn not(&self) -> Vec2<bool> {
|
2013-01-29 09:26:48 +00:00
|
|
|
Vector2::new(!self[0], !self[1])
|
2012-12-16 05:19:38 +00:00
|
|
|
}
|
2013-01-29 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GLSL-style type aliases, corresponding to Section 4.1.5 of the [GLSL 4.30.6 specification]
|
|
|
|
// (http://www.opengl.org/registry/doc/GLSLangSpec.4.30.6.pdf).
|
|
|
|
|
2013-01-29 09:26:48 +00:00
|
|
|
pub type vec2 = Vec2<f32>; // a two-component single-precision floating-point vector
|
|
|
|
pub type dvec2 = Vec2<f64>; // a two-component double-precision floating-point vector
|
|
|
|
pub type bvec2 = Vec2<bool>; // a two-component Boolean vector
|
|
|
|
pub type ivec2 = Vec2<i32>; // a two-component signed integer vector
|
|
|
|
pub type uvec2 = Vec2<u32>; // a two-component unsigned integer vector
|
2013-01-29 01:13:44 +00:00
|
|
|
|
|
|
|
// Static method wrappers for GLSL-style types
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl vec2 {
|
2013-01-29 01:13:44 +00:00
|
|
|
#[inline(always)] static pure fn new(x: f32, y: f32) -> vec2 { Vector2::new(x, y) }
|
|
|
|
#[inline(always)] static pure fn from_value(v: f32) -> vec2 { Vector::from_value(v) }
|
|
|
|
#[inline(always)] static pure fn identity() -> vec2 { NumericVector::identity() }
|
|
|
|
#[inline(always)] static pure fn zero() -> vec2 { NumericVector::zero() }
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 01:13:44 +00:00
|
|
|
#[inline(always)] static pure fn unit_x() -> vec2 { NumericVector2::unit_x() }
|
|
|
|
#[inline(always)] static pure fn unit_y() -> vec2 { NumericVector2::unit_y() }
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 01:13:44 +00:00
|
|
|
#[inline(always)] static pure fn dim() -> uint { 2 }
|
|
|
|
#[inline(always)] static pure fn size_of() -> uint { size_of::<vec2>() }
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl dvec2 {
|
2013-01-29 01:13:44 +00:00
|
|
|
#[inline(always)] static pure fn new(x: f64, y: f64) -> dvec2 { Vector2::new(x, y) }
|
|
|
|
#[inline(always)] static pure fn from_value(v: f64) -> dvec2 { Vector::from_value(v) }
|
|
|
|
#[inline(always)] static pure fn identity() -> dvec2 { NumericVector::identity() }
|
|
|
|
#[inline(always)] static pure fn zero() -> dvec2 { NumericVector::zero() }
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 01:13:44 +00:00
|
|
|
#[inline(always)] static pure fn unit_x() -> dvec2 { NumericVector2::unit_x() }
|
|
|
|
#[inline(always)] static pure fn unit_y() -> dvec2 { NumericVector2::unit_y() }
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 01:13:44 +00:00
|
|
|
#[inline(always)] static pure fn dim() -> uint { 2 }
|
|
|
|
#[inline(always)] static pure fn size_of() -> uint { size_of::<dvec2>() }
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl bvec2 {
|
2013-01-29 01:13:44 +00:00
|
|
|
#[inline(always)] static pure fn new(x: bool, y: bool) -> bvec2 { Vector2::new(x, y) }
|
|
|
|
#[inline(always)] static pure fn from_value(v: bool) -> bvec2 { Vector::from_value(v) }
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 01:13:44 +00:00
|
|
|
#[inline(always)] static pure fn dim() -> uint { 2 }
|
|
|
|
#[inline(always)] static pure fn size_of() -> uint { size_of::<bvec2>() }
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl ivec2 {
|
2013-01-29 01:13:44 +00:00
|
|
|
#[inline(always)] static pure fn new(x: i32, y: i32) -> ivec2 { Vector2::new(x, y) }
|
|
|
|
#[inline(always)] static pure fn from_value(v: i32) -> ivec2 { Vector::from_value(v) }
|
|
|
|
#[inline(always)] static pure fn identity() -> ivec2 { NumericVector::identity() }
|
|
|
|
#[inline(always)] static pure fn zero() -> ivec2 { NumericVector::zero() }
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 01:13:44 +00:00
|
|
|
#[inline(always)] static pure fn unit_x() -> ivec2 { NumericVector2::unit_x() }
|
|
|
|
#[inline(always)] static pure fn unit_y() -> ivec2 { NumericVector2::unit_y() }
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 01:13:44 +00:00
|
|
|
#[inline(always)] static pure fn dim() -> uint { 2 }
|
|
|
|
#[inline(always)] static pure fn size_of() -> uint { size_of::<ivec2>() }
|
|
|
|
}
|
|
|
|
|
2013-03-28 09:45:43 +00:00
|
|
|
impl uvec2 {
|
2013-01-29 01:13:44 +00:00
|
|
|
#[inline(always)] static pure fn new(x: u32, y: u32) -> uvec2 { Vector2::new(x, y) }
|
|
|
|
#[inline(always)] static pure fn from_value(v: u32) -> uvec2 { Vector::from_value(v) }
|
|
|
|
#[inline(always)] static pure fn identity() -> uvec2 { NumericVector::identity() }
|
|
|
|
#[inline(always)] static pure fn zero() -> uvec2 { NumericVector::zero() }
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 01:13:44 +00:00
|
|
|
#[inline(always)] static pure fn unit_x() -> uvec2 { NumericVector2::unit_x() }
|
|
|
|
#[inline(always)] static pure fn unit_y() -> uvec2 { NumericVector2::unit_y() }
|
2013-03-28 09:45:43 +00:00
|
|
|
|
2013-01-29 01:13:44 +00:00
|
|
|
#[inline(always)] static pure fn dim() -> uint { 2 }
|
|
|
|
#[inline(always)] static pure fn size_of() -> uint { size_of::<uvec2>() }
|
2013-02-06 21:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type aliases named in a more 'Rustic' style
|
|
|
|
|
|
|
|
pub type vec2i = Vec2<int>;
|
|
|
|
pub type vec2i8 = Vec2<i8>;
|
|
|
|
pub type vec2i16 = Vec2<i16>;
|
|
|
|
pub type vec2i32 = Vec2<i32>;
|
|
|
|
pub type vec2i64 = Vec2<i64>;
|
|
|
|
pub type vec2f = Vec2<float>;
|
|
|
|
pub type vec2f32 = Vec2<f32>;
|
|
|
|
pub type vec2f64 = Vec2<f64>;
|
2013-02-17 08:16:41 +00:00
|
|
|
pub type vec2b = Vec2<bool>;
|