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