cgmath/src/vec3.rs
2012-12-13 23:01:42 +10:00

273 lines
7 KiB
Rust

use core::cast::transmute;
use core::cmp::Eq;
use core::ptr::to_unsafe_ptr;
use core::vec::raw::buf_as_slice;
use std::cmp::FuzzyEq;
use angle::Radians;
use funs::exponential::Exp;
use funs::triganomic::{InvTrig, atan2};
use num::types::Number;
/**
* 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
*/
pub struct Vec3<T> { x: T, y: T, z: T }
pub impl<T> Vec3<T>/*: Vector3<T>*/ {
#[inline(always)]
static pure fn new(x: T, y: T, z: T) -> Vec3<T> {
Vec3 { x: move x, y: move y, z: move z }
}
}
pub impl<T:Copy> Vec3<T>: Vector<T> {
#[inline(always)]
static pure fn from_value(value: T) -> Vec3<T> {
Vec3::new(value, value, value)
}
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*Vec3<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy> Vec3<T>: Index<uint, T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } }
}
}
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));
}
}
pub impl<T:Copy Number> Vec3<T>: NumericVector<T> {
#[inline(always)]
static pure fn identity() -> Vec3<T> {
Vec3::new(Number::one(),
Number::one(),
Number::one())
}
#[inline(always)]
static pure fn zero() -> Vec3<T> {
Vec3::new(Number::zero(),
Number::zero(),
Number::zero())
}
#[inline(always)]
pure fn mul_t(&self, value: T) -> Vec3<T> {
Vec3::new(self[0] * value,
self[1] * value,
self[2] * value)
}
#[inline(always)]
pure fn div_t(&self, value: T) -> Vec3<T> {
Vec3::new(self[0] / value,
self[1] / value,
self[2] / value)
}
#[inline(always)]
pure fn add_v(&self, other: &Vec3<T>) -> Vec3<T>{
Vec3::new(self[0] + other[0],
self[1] + other[1],
self[2] + other[2])
}
#[inline(always)]
pure fn sub_v(&self, other: &Vec3<T>) -> Vec3<T>{
Vec3::new(self[0] - other[0],
self[1] - other[1],
self[2] - other[2])
}
#[inline(always)]
pure fn dot(&self, other: &Vec3<T>) -> T {
self[0] * other[0] +
self[1] * other[1] +
self[2] * other[2]
}
}
pub impl<T:Copy Number> Vec3<T>: Neg<Vec3<T>> {
#[inline(always)]
pure fn neg(&self) -> Vec3<T> {
Vec3::new(-self[0], -self[1], -self[2])
}
}
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];
}
}
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);
}
}
pub impl<T:Copy Number Exp InvTrig> Vec3<T>: EuclideanVector<T> {
#[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: &Vec3<T>) -> T {
other.sub_v(self).length2()
}
#[inline(always)]
pure fn distance(&self, other: &Vec3<T>) -> T {
other.distance2(self).sqrt()
}
#[inline(always)]
pure fn angle(&self, other: &Vec3<T>) -> Radians<T> {
atan2(&self.cross(other).length(), &self.dot(other))
}
#[inline(always)]
pure fn normalize(&self) -> Vec3<T> {
let mut n: T = Number::from(1);
n /= self.length();
return self.mul_t(n);
}
#[inline(always)]
pure fn normalize_to(&self, length: T) -> Vec3<T> {
let mut n: T = length / self.length();
return self.mul_t(n);
}
#[inline(always)]
pure fn lerp(&self, other: &Vec3<T>, amount: T) -> Vec3<T> {
self.add_v(&other.sub_v(self).mul_t(amount))
}
}
pub impl<T:Copy Number Exp InvTrig> Vec3<T>: MutableEuclideanVector<&self/T> {
#[inline(always)]
fn normalize_self(&mut self) {
let mut n: T = Number::from(1);
n /= self.length();
self.mul_self_t(&n);
}
#[inline(always)]
fn normalize_self_to(&mut self, length: &T) {
let mut n: T = length / 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));
}
}
pub impl<T:Copy Eq> Vec3<T>: Eq {
#[inline(always)]
pure fn eq(&self, other: &Vec3<T>) -> bool {
self[0] == other[0] &&
self[1] == other[1] &&
self[2] == other[2]
}
#[inline(always)]
pure fn ne(&self, other: &Vec3<T>) -> bool {
!(self == other)
}
}
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])
}
}