use prelude::*;
use super::Vector;
#[derive(Copy, Clone)]
#[cfg_attr(feature = "serialize-serde", derive(Deserialize, Serialize))]
pub struct Vec3<T = f32>(pub T, pub T, pub T);
impl<T> Vec3<T> where T: Float {
pub fn new() -> Vec3<T> {
Vec3::<T>(T::zero(), T::zero(), T::zero())
}
pub fn len(self: &Self) -> T {
(self.0*self.0 + self.1*self.1 + self.2*self.2).sqrt()
}
pub fn dot(self: &Self, other: &Vec3<T>) -> T {
self.0 * other.0 + self.1 * other.1 + self.1 * other.2
}
}
impl<T> Vector<T> for Vec3<T> where T: Copy {
fn as_vec3(&self, _: T) -> Vec3<T> {
*self
}
}
impl<T> From<[ T; 3 ]> for Vec3<T> where T: Copy {
fn from(source: [ T; 3 ]) -> Self {
Vec3(source[0], source[1], source[2])
}
}
impl From<Vec3<f32>> for [ f32; 3 ] {
fn from(source: Vec3<f32>) -> Self {
[ source.0, source.1, source.2 ]
}
}
impl From<Vec3<f64>> for [ f64; 3 ] {
fn from(source: Vec3<f64>) -> Self {
[ source.0, source.1, source.2 ]
}
}
impl<T> From<(T, T, T)> for Vec3<T> {
fn from(source: (T, T, T)) -> Self {
Vec3(source.0, source.1, source.2)
}
}
impl From<Vec3<f32>> for (f32, f32, f32) {
fn from(source: Vec3<f32>) -> Self {
(source.0, source.1, source.2)
}
}
impl From<Vec3<f64>> for (f64, f64, f64) {
fn from(source: Vec3<f64>) -> Self {
(source.0, source.1, source.2)
}
}
impl<T> Default for Vec3<T> where T: Float {
fn default() -> Self {
Vec3(T::zero(), T::zero(), T::zero())
}
}
impl<T> Neg for Vec3<T> where T: Float {
type Output = Vec3<T>;
fn neg(self) -> Vec3<T> {
Vec3::<T>(-self.0, -self.1, -self.2)
}
}
impl<T> Add for Vec3<T> where T: Float {
type Output = Vec3<T>;
fn add(self, other: Vec3<T>) -> Vec3<T> {
Vec3::<T>(self.0 + other.0, self.1 + other.1, self.2 + other.2)
}
}
impl<T> AddAssign for Vec3<T> where T: Float {
fn add_assign(self: &mut Self, other: Vec3<T>) {
*self = Vec3::<T> (
self.0 + other.0,
self.1 + other.1,
self.2 + other.2
)
}
}
impl<T> Sub for Vec3<T> where T: Float {
type Output = Vec3<T>;
fn sub(self, other: Vec3<T>) -> Vec3<T> {
Vec3::<T>(self.0 - other.0, self.1 - other.1, self.2 - other.2)
}
}
impl<T> SubAssign for Vec3<T> where T: Float {
fn sub_assign(self: &mut Self, other: Vec3<T>) {
*self = Vec3::<T> (
self.0 - other.0,
self.1 - other.1,
self.2 - other.2,
)
}
}
impl<T> Mul<Vec3<T>> for Vec3<T> where T: Float {
type Output = Vec3<T>;
fn mul(self, other: Vec3<T>) -> Vec3<T> {
Vec3::<T>(self.0 * other.0, self.1 * other.1, self.2 * other.2)
}
}
impl<T> MulAssign<Vec3<T>> for Vec3<T> where T: Float {
fn mul_assign(&mut self, other: Vec3<T>) {
*self = Vec3::<T>(self.0 * other.0, self.1 * other.1, self.2 * other.2)
}
}
impl<T> Mul<T> for Vec3<T> where T: Float {
type Output = Vec3<T>;
fn mul(self, other: T) -> Vec3<T> {
Vec3::<T>(self.0 * other, self.1 * other, self.2 * other)
}
}
impl<T> MulAssign<T> for Vec3<T> where T: Float {
fn mul_assign(&mut self, other: T) {
*self = Vec3::<T>(self.0 * other, self.1 * other, self.2 * other)
}
}
impl<T> Div<Vec3<T>> for Vec3<T> where T: Float {
type Output = Vec3<T>;
fn div(self, other: Vec3<T>) -> Vec3<T> {
Vec3::<T>(self.0 / other.0, self.1 / other.1, self.2 / other.2)
}
}
impl<T> DivAssign<Vec3<T>> for Vec3<T> where T: Float {
fn div_assign(&mut self, other: Vec3<T>) {
*self = Vec3::<T>(self.0 / other.0, self.1 / other.1, self.2 / other.2)
}
}
impl<T> Div<T> for Vec3<T> where T: Float {
type Output = Vec3<T>;
fn div(self, other: T) -> Vec3<T> {
Vec3::<T>(self.0 / other, self.1 / other, self.2 / other)
}
}
impl<T> DivAssign<T> for Vec3<T> where T: Float {
fn div_assign(&mut self, other: T) {
*self = Vec3::<T>(self.0 / other, self.1 / other, self.2 / other)
}
}
impl Mul<Vec3<f32>> for f32 {
type Output = Vec3<f32>;
fn mul(self, other: Vec3<f32>) -> Vec3<f32> {
Vec3::<f32>(self * other.0, self * other.1, self * other.2)
}
}
impl Mul<Vec3<f64>> for f64 {
type Output = Vec3<f64>;
fn mul(self, other: Vec3<f64>) -> Vec3<f64> {
Vec3::<f64>(self * other.0, self * other.1, self * other.2)
}
}
#[cfg(feature = "uniforms")]
use radiant_rs::{Uniform, AsUniform};
#[cfg(feature = "uniforms")]
impl AsUniform for Vec3<f32> {
fn as_uniform(&self) -> Uniform {
Uniform::Vec3([ self.0, self.1, self.2 ])
}
}
#[cfg(feature = "uniforms")]
impl AsUniform for Vec3<f64> {
fn as_uniform(&self) -> Uniform {
Uniform::DoubleVec3([ self.0, self.1, self.2 ])
}
}
impl<T> Debug for Vec3<T> where T: Debug {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Vec3({:?}, {:?}, {:?})", self.0, self.1, self.2)
}
}