#![allow(dead_code)]
use std::ops::{Add, Sub, Mul, Div};
use std::cmp::PartialEq;
pub use num::{Float, Zero, One};
use super::eq::{almost_equal, equal};
#[derive(Copy, Clone)]
pub struct Vec3<T>{
x: T,
y: T,
z: T
}
impl<T> Vec3<T>{
pub fn new(x: T, y: T, z: T) -> Vec3<T> {
Vec3{x: x, y: y, z: z}
}
}
impl<T: One> One for Vec3<T>{
fn one() -> Self {
Vec3{x: T::one(), y: T::one(), z: T::one()}
}
}
impl<T: Float> Vec3<T>{
pub fn almost_equal(self, other: Vec3<T>, abs_tol: T) -> bool {
almost_equal(self.x, other.x, abs_tol) &&
almost_equal(self.y, other.y, abs_tol) &&
almost_equal(self.z, other.z, abs_tol)
}
}
impl<T: Add<Output = T>> Add for Vec3<T>{
type Output = Vec3<T>;
fn add(self, _rhs: Vec3<T>) -> Vec3<T> {
Vec3{
x: self.x + _rhs.x,
y: self.y + _rhs.y,
z: self.z + _rhs.z,
}
}
}
impl<T: Sub<Output = T>> Sub for Vec3<T>{
type Output = Vec3<T>;
fn sub(self, _rhs: Vec3<T>) -> Vec3<T> {
Vec3{
x: self.x - _rhs.x,
y: self.y - _rhs.y,
z: self.z - _rhs.z,
}
}
}
impl<T: Mul<Output = T>> Mul for Vec3<T>{
type Output = Vec3<T>;
fn mul(self, _rhs: Vec3<T>) -> Vec3<T> {
Vec3{
x: self.x * _rhs.x,
y: self.y * _rhs.y,
z: self.z * _rhs.z,
}
}
}
impl<T: Div<Output = T>> Div for Vec3<T>{
type Output = Vec3<T>;
fn div(self, _rhs: Vec3<T>) -> Vec3<T> {
Vec3{
x: self.x / _rhs.x,
y: self.y / _rhs.y,
z: self.z / _rhs.z,
}
}
}
macro_rules! impl_floats {
($($ty:ty),*) => ($(
impl PartialEq for Vec3<$ty> {
fn eq(&self, _rhs: &Vec3<$ty>) -> bool {
equal(self.x, _rhs.x) &&
equal(self.y, _rhs.y) &&
equal(self.z, _rhs.z)
}
}
impl Zero for Vec3<$ty>{
fn zero() -> Self {
Vec3{x: 0.0, y: 0.0, z: 0.0}
}
fn is_zero(&self) -> bool {
*self == Vec3::<$ty>::zero()
}
}
)*);
}
macro_rules! impl_ints {
($($ty:ty),*) => ($(
impl PartialEq for Vec3<$ty> {
fn eq(&self, _rhs: &Vec3<$ty>) -> bool {
self.x == _rhs.x &&
self.y == _rhs.y &&
self.z == _rhs.z
}
}
impl Zero for Vec3<$ty>{
fn zero() -> Self {
Vec3{x: 0, y: 0, z: 0}
}
fn is_zero(&self) -> bool {
*self == Vec3::<$ty>::zero()
}
}
)*);
}
impl_floats! { f32, f64 }
impl_ints! { i8, u8, i16, u16, i32, u32, i64, u64, isize, usize }