cgmath/src/vec2.rs

440 lines
12 KiB
Rust
Raw Normal View History

2012-12-13 13:01:42 +00:00
use core::cast::transmute;
use core::cmp::{Eq, Ord};
2012-12-13 13:01:42 +00:00
use core::ptr::to_unsafe_ptr;
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,
Vector2,
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 }
impl<T:Copy + Eq> Vector<T> for Vec2<T> {
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:37:25 +00:00
fn from_value(value: T) -> Vec2<T> {
Vector2::new(value, value)
2012-12-13 13:01:42 +00:00
}
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn to_ptr(&self) -> *T {
2012-12-13 13:01:42 +00:00
unsafe {
transmute::<*Vec2<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
impl<T> Vector2<T> for Vec2<T> {
#[inline(always)]
2013-03-28 10:37:25 +00:00
fn new(x: T, y: T ) -> Vec2<T> {
Vec2 { x: x, y: y }
}
}
impl<T:Copy + Eq> Index<uint, T> for Vec2<T> {
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn index(&self, i: uint) -> T {
2012-12-13 13:01:42 +00:00
unsafe { do buf_as_slice(self.to_ptr(), 2) |slice| { slice[i] } }
}
}
impl<T:Copy> MutableVector<T> for Vec2<T> {
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:39:19 +00:00
fn index_mut(&mut self, i: uint) -> &'self mut T {
2012-12-13 13:01:42 +00:00
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
}
}
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
}
}
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)]
2013-03-28 10:37:25 +00:00
fn identity() -> Vec2<T> {
Vector2::new(one::<T>(), one::<T>())
2012-12-13 13:01:42 +00:00
}
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:37:25 +00:00
fn zero() -> Vec2<T> {
Vector2::new(zero::<T>(), zero::<T>())
2012-12-13 13:01:42 +00:00
}
2012-12-24 03:46:25 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn is_zero(&self) -> bool {
self[0] == zero() &&
self[1] == zero()
2012-12-24 03:46:25 +00:00
}
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn mul_t(&self, value: T) -> Vec2<T> {
Vector2::new(self[0] * value,
self[1] * value)
2012-12-13 13:01:42 +00:00
}
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn div_t(&self, value: T) -> Vec2<T> {
Vector2::new(self[0] / value,
self[1] / value)
2012-12-13 13:01:42 +00:00
}
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn add_v(&self, other: &Vec2<T>) -> Vec2<T> {
Vector2::new(self[0] + other[0],
self[1] + other[1])
2012-12-13 13:01:42 +00:00
}
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn sub_v(&self, other: &Vec2<T>) -> Vec2<T> {
Vector2::new(self[0] - other[0],
self[1] - other[1])
2012-12-13 13:01:42 +00:00
}
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn mul_v(&self, other: &Vec2<T>) -> Vec2<T> {
Vector2::new(self[0] * other[0],
self[1] * other[1])
}
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn div_v(&self, other: &Vec2<T>) -> Vec2<T> {
Vector2::new(self[0] / other[0],
self[1] / other[1])
}
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn dot(&self, other: &Vec2<T>) -> T {
2012-12-13 13:01:42 +00:00
self[0] * other[0] +
self[1] * other[1]
}
}
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)]
2013-03-28 10:35:51 +00:00
fn neg(&self) -> Vec2<T> {
Vector2::new(-self[0], -self[1])
2012-12-13 13:01:42 +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)]
2013-03-28 10:37:25 +00:00
fn unit_x() -> Vec2<T> {
Vector2::new(one::<T>(), zero::<T>())
2013-01-27 22:50:49 +00:00
}
2013-01-27 22:50:49 +00:00
#[inline(always)]
2013-03-28 10:37:25 +00:00
fn unit_y() -> Vec2<T> {
Vector2::new(zero::<T>(), one::<T>())
2013-01-27 22:50:49 +00:00
}
2013-01-27 22:50:49 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn perp_dot(&self, other: &Vec2<T>) ->T {
2013-01-27 22:50:49 +00:00
(self[0] * other[1]) - (self[1] * other[0])
}
}
2013-03-28 10:39:19 +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);
}
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);
}
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);
}
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];
}
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];
}
#[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
}
impl<T:Copy + Number> ToHomogeneous<Vec3<T>> for Vec2<T> {
2013-01-02 06:59:24 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn to_homogeneous(&self) -> Vec3<T> {
Vector3::new(self.x, self.y, zero())
2013-01-02 06:59:24 +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)]
2013-03-28 10:35:51 +00:00
fn length2(&self) -> T {
2012-12-13 13:01:42 +00:00
self.dot(self)
}
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn length(&self) -> T {
2012-12-13 13:01:42 +00:00
self.length2().sqrt()
}
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn distance2(&self, other: &Vec2<T>) -> T {
2012-12-13 13:01:42 +00:00
other.sub_v(self).length2()
}
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn distance(&self, other: &Vec2<T>) -> T {
2012-12-13 13:01:42 +00:00
other.distance2(self).sqrt()
}
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn angle(&self, other: &Vec2<T>) -> T {
2013-01-27 22:22:15 +00:00
atan2(self.perp_dot(other), self.dot(other))
2012-12-13 13:01:42 +00:00
}
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn normalize(&self) -> Vec2<T> {
self.mul_t(one::<T>()/self.length())
2012-12-13 13:01:42 +00:00
}
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn normalize_to(&self, length: T) -> Vec2<T> {
self.mul_t(length / self.length())
2012-12-13 13:01:42 +00:00
}
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn lerp(&self, other: &Vec2<T>, amount: T) -> Vec2<T> {
2012-12-13 13:01:42 +00:00
self.add_v(&other.sub_v(self).mul_t(amount))
}
}
2013-03-28 10:39:19 +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) {
let n = one::<T>() / self.length();
2012-12-13 13:01:42 +00:00
self.mul_self_t(&n);
}
2012-12-13 13:01:42 +00:00
#[inline(always)]
fn normalize_self_to(&mut self, length: &T) {
let n = length / self.length();
2012-12-13 13:01:42 +00:00
self.mul_self_t(&n);
}
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));
}
}
impl<T:Copy + Float + FuzzyEq<T>> FuzzyEq<T> for Vec2<T> {
2012-12-13 13:01:42 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
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-02-09 22:42:06 +00:00
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn fuzzy_eq_eps(&self, other: &Vec2<T>, epsilon: &T) -> bool {
2013-02-09 22:42:06 +00:00
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
self[1].fuzzy_eq_eps(&other[1], epsilon)
2012-12-13 13:01:42 +00:00
}
}
impl<T:Copy + Ord + Eq> OrdinalVector<T, Vec2<bool>> for Vec2<T> {
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn less_than(&self, other: &Vec2<T>) -> Vec2<bool> {
Vector2::new(self[0] < other[0],
self[1] < other[1])
}
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn less_than_equal(&self, other: &Vec2<T>) -> Vec2<bool> {
Vector2::new(self[0] <= other[0],
self[1] <= other[1])
}
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn greater_than(&self, other: &Vec2<T>) -> Vec2<bool> {
Vector2::new(self[0] > other[0],
self[1] > other[1])
}
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn greater_than_equal(&self, other: &Vec2<T>) -> Vec2<bool> {
Vector2::new(self[0] >= other[0],
self[1] >= other[1])
}
}
impl<T:Copy + Eq> EquableVector<T, Vec2<bool>> for Vec2<T> {
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn equal(&self, other: &Vec2<T>) -> Vec2<bool> {
Vector2::new(self[0] == other[0],
self[1] == other[1])
}
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn not_equal(&self, other: &Vec2<T>) -> Vec2<bool> {
Vector2::new(self[0] != other[0],
self[1] != other[1])
}
}
impl BooleanVector for Vec2<bool> {
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn any(&self) -> bool {
self[0] || self[1]
}
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn all(&self) -> bool {
self[0] && self[1]
}
#[inline(always)]
2013-03-28 10:35:51 +00:00
fn not(&self) -> Vec2<bool> {
Vector2::new(!self[0], !self[1])
}
}
// 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).
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
// Static method wrappers for GLSL-style types
impl vec2 {
2013-03-28 10:37:25 +00:00
#[inline(always)] fn new(x: f32, y: f32) -> vec2 { Vector2::new(x, y) }
#[inline(always)] fn from_value(v: f32) -> vec2 { Vector::from_value(v) }
#[inline(always)] fn identity() -> vec2 { NumericVector::identity() }
#[inline(always)] fn zero() -> vec2 { NumericVector::zero() }
2013-03-28 10:37:25 +00:00
#[inline(always)] fn unit_x() -> vec2 { NumericVector2::unit_x() }
#[inline(always)] fn unit_y() -> vec2 { NumericVector2::unit_y() }
2013-03-28 10:37:25 +00:00
#[inline(always)] fn dim() -> uint { 2 }
#[inline(always)] fn size_of() -> uint { size_of::<vec2>() }
}
impl dvec2 {
2013-03-28 10:37:25 +00:00
#[inline(always)] fn new(x: f64, y: f64) -> dvec2 { Vector2::new(x, y) }
#[inline(always)] fn from_value(v: f64) -> dvec2 { Vector::from_value(v) }
#[inline(always)] fn identity() -> dvec2 { NumericVector::identity() }
#[inline(always)] fn zero() -> dvec2 { NumericVector::zero() }
2013-03-28 10:37:25 +00:00
#[inline(always)] fn unit_x() -> dvec2 { NumericVector2::unit_x() }
#[inline(always)] fn unit_y() -> dvec2 { NumericVector2::unit_y() }
2013-03-28 10:37:25 +00:00
#[inline(always)] fn dim() -> uint { 2 }
#[inline(always)] fn size_of() -> uint { size_of::<dvec2>() }
}
impl bvec2 {
2013-03-28 10:37:25 +00:00
#[inline(always)] fn new(x: bool, y: bool) -> bvec2 { Vector2::new(x, y) }
#[inline(always)] fn from_value(v: bool) -> bvec2 { Vector::from_value(v) }
2013-03-28 10:37:25 +00:00
#[inline(always)] fn dim() -> uint { 2 }
#[inline(always)] fn size_of() -> uint { size_of::<bvec2>() }
}
impl ivec2 {
2013-03-28 10:37:25 +00:00
#[inline(always)] fn new(x: i32, y: i32) -> ivec2 { Vector2::new(x, y) }
#[inline(always)] fn from_value(v: i32) -> ivec2 { Vector::from_value(v) }
#[inline(always)] fn identity() -> ivec2 { NumericVector::identity() }
#[inline(always)] fn zero() -> ivec2 { NumericVector::zero() }
2013-03-28 10:37:25 +00:00
#[inline(always)] fn unit_x() -> ivec2 { NumericVector2::unit_x() }
#[inline(always)] fn unit_y() -> ivec2 { NumericVector2::unit_y() }
2013-03-28 10:37:25 +00:00
#[inline(always)] fn dim() -> uint { 2 }
#[inline(always)] fn size_of() -> uint { size_of::<ivec2>() }
}
impl uvec2 {
2013-03-28 10:37:25 +00:00
#[inline(always)] fn new(x: u32, y: u32) -> uvec2 { Vector2::new(x, y) }
#[inline(always)] fn from_value(v: u32) -> uvec2 { Vector::from_value(v) }
#[inline(always)] fn identity() -> uvec2 { NumericVector::identity() }
#[inline(always)] fn zero() -> uvec2 { NumericVector::zero() }
2013-03-28 10:37:25 +00:00
#[inline(always)] fn unit_x() -> uvec2 { NumericVector2::unit_x() }
#[inline(always)] fn unit_y() -> uvec2 { NumericVector2::unit_y() }
2013-03-28 10:37:25 +00:00
#[inline(always)] fn dim() -> uint { 2 }
#[inline(always)] 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>;
pub type vec2b = Vec2<bool>;