#![allow(dead_code)]
use std::ops::{Add, Sub, Mul, Div};
use std::cmp::{PartialEq, PartialOrd, Ordering};
pub use num::{Zero, One};
use num;
use super::float::Float;
use std::fmt;
use clamp::Clamp;
#[derive(Copy, Clone, Debug)]
pub struct Vec3<T>{
x: T,
y: T,
z: T
}
impl<T> Vec3<T>{
pub fn new(x: T, y: T, z: T) -> Self {
Vec3{x: x, y: y, z: z}
}
}
pub type Point3<T> = Vec3<T>;
impl<T: fmt::Display> fmt::Display for Vec3<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Vec3({}, {}, {})", self.x, self.y, self.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<N: num::Float>(self, other: Self, abs_tol: N) -> bool {
self.x.almost_equal(other.x, abs_tol) &&
self.y.almost_equal(other.y, abs_tol) &&
self.z.almost_equal(other.z, abs_tol)
}
pub fn is_nan(self) -> bool {
self.x.is_nan() &&
self.y.is_nan() &&
self.z.is_nan()
}
}
impl<T: Add<Output = T>> Add for Vec3<T>{
type Output = Vec3<T>;
fn add(self, _rhs: Self) -> Self {
Vec3{
x: self.x + _rhs.x,
y: self.y + _rhs.y,
z: self.z + _rhs.z,
}
}
}
impl<T: Add<Output = T> + Copy> Vec3<T> {
pub fn add_scalar(self, _rhs: T) -> Self {
Vec3{
x: self.x + _rhs,
y: self.y + _rhs,
z: self.z + _rhs,
}
}
}
impl<T: Sub<Output = T>> Sub for Vec3<T>{
type Output = Self;
fn sub(self, _rhs: Self) -> Self {
Vec3{
x: self.x - _rhs.x,
y: self.y - _rhs.y,
z: self.z - _rhs.z,
}
}
}
impl<T: Sub<Output = T> + Copy> Vec3<T> {
pub fn sub_scalar(self, _rhs: T) -> Self {
Vec3{
x: self.x - _rhs,
y: self.y - _rhs,
z: self.z - _rhs,
}
}
}
impl<T: Mul<Output = T>> Mul for Vec3<T>{
type Output = Self;
fn mul(self, _rhs: Self) -> Self {
Vec3{
x: self.x * _rhs.x,
y: self.y * _rhs.y,
z: self.z * _rhs.z,
}
}
}
impl<T: Mul<Output = T> + Copy> Vec3<T> {
pub fn mul_scalar(self, _rhs: T) -> Self {
Vec3{
x: self.x * _rhs,
y: self.y * _rhs,
z: self.z * _rhs,
}
}
}
impl<T: Div<Output = T>> Div for Vec3<T>{
type Output = Vec3<T>;
fn div(self, _rhs: Self) -> Self {
Vec3{
x: self.x / _rhs.x,
y: self.y / _rhs.y,
z: self.z / _rhs.z,
}
}
}
impl<T: Div<Output = T> + Copy> Vec3<T> {
pub fn div_scalar(self, _rhs: T) -> Self {
Vec3{
x: self.x / _rhs,
y: self.y / _rhs,
z: self.z / _rhs,
}
}
}
impl<T: Clamp<Elem = T> + Copy> Clamp for Vec3<T>{
type Elem = T;
fn clamp(self, min: T, max: T) -> Self {
Vec3{
x: self.x.clamp(min, max),
y: self.y.clamp(min, max),
z: self.z.clamp(min, max),
}
}
}
impl<T> AsRef<Vec3<T>> for Vec3<T> {
fn as_ref(&self) -> &Self {
self
}
}
impl<T:PartialOrd> Vec3<T> {
pub fn any_less<O:AsRef<Self>>(&self, other: O) -> bool {
let o = other.as_ref();
self.x < o.x || self.y < o.y || self.z < o.z
}
pub fn any_greater<O:AsRef<Self>>(&self, other: O) -> bool {
let o = other.as_ref();
self.x > o.x || self.y > o.y || self.z > o.z
}
}
impl<T: PartialEq> PartialEq for Vec3<T> {
fn eq(&self, _rhs: &Self) -> bool {
self.x == _rhs.x &&
self.y == _rhs.y &&
self.z == _rhs.z
}
}
impl<T: PartialOrd> PartialOrd for Vec3<T>{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.x < other.x && self.y < other.y && self.z < other.z {
Some(Ordering::Less)
} else if self.x > other.x && self.y > other.y && self.z > other.z {
Some(Ordering::Greater)
} else if self == other {
Some(Ordering::Equal)
} else {
None
}
}
}
impl<T: Zero> Zero for Vec3<T>{
fn zero() -> Self {
Vec3{x: Zero::zero(), y: Zero::zero(), z: Zero::zero()}
}
fn is_zero(&self) -> bool {
self.x.is_zero() &&
self.y.is_zero() &&
self.z.is_zero()
}
}