nalgebra/src/dim3/vec3.rs

88 lines
1.7 KiB
Rust
Raw Normal View History

use core::num::{Zero, Algebraic};
2013-05-15 08:18:13 +08:00
use traits::dim::Dim;
2013-05-14 19:35:01 +08:00
use traits::dot::Dot;
2013-05-15 08:18:13 +08:00
use traits::cross::Cross;
2013-05-14 19:35:01 +08:00
#[deriving(Eq)]
pub struct Vec3<T>
{
x : T,
y : T,
z : T
}
pub fn Vec3<T:Copy>(x: T, y: T, z: T) -> Vec3<T>
{ Vec3 {x: x, y: y, z: z} }
2013-05-15 08:18:13 +08:00
impl<T> Dim for Vec3<T>
{
fn dim() -> uint
{ 3 }
}
2013-05-14 19:35:01 +08:00
impl<T:Copy + Add<T,T>> Add<Vec3<T>, Vec3<T>> for Vec3<T>
{
fn add(&self, other: &Vec3<T>) -> Vec3<T>
{ Vec3(self.x + other.x, self.y + other.y, self.z + other.z) }
}
impl<T:Copy + Sub<T,T>> Sub<Vec3<T>, Vec3<T>> for Vec3<T>
{
fn sub(&self, other: &Vec3<T>) -> Vec3<T>
{ Vec3(self.x - other.x, self.y - other.y, self.z - other.z) }
}
impl<T:Copy + Neg<T>> Neg<Vec3<T>> for Vec3<T>
{
fn neg(&self) -> Vec3<T>
{ Vec3(-self.x, -self.y, -self.z) }
}
impl<T:Copy + Mul<T, T> + Add<T, T> + Algebraic> Dot<T> for Vec3<T>
2013-05-14 19:35:01 +08:00
{
fn dot(&self, other : &Vec3<T>) -> T
{ self.x * other.x + self.y * other.y + self.z * other.z }
fn sqnorm(&self) -> T
{ self.dot(self) }
fn norm(&self) -> T
{ self.sqnorm().sqrt() }
}
2013-05-15 08:18:13 +08:00
impl<T:Copy + Mul<T, T> + Sub<T, T>> Cross<Vec3<T>> for Vec3<T>
{
fn cross(&self, other : &Vec3<T>) -> Vec3<T>
{
Vec3(
self.y * other.z - self.z * other.y,
self.z * other.x - self.x * other.z,
self.x * other.y - self.y * other.x
)
}
}
impl<T:Copy + Zero> Zero for Vec3<T>
{
fn zero() -> Vec3<T>
{
let _0 = Zero::zero();
Vec3(_0, _0, _0)
}
fn is_zero(&self) -> bool
{ self.x.is_zero() && self.y.is_zero() && self.z.is_zero() }
2013-05-15 08:18:13 +08:00
}
impl<T:ToStr> ToStr for Vec3<T>
{
fn to_str(&self) -> ~str
{
~"Vec3 "
+ "{ x : " + self.x.to_str()
+ ", y : " + self.y.to_str()
+ ", z : " + self.z.to_str()
+ " }"
}
}