Refined ScalaOp trait.
This commit is contained in:
parent
d13924bfee
commit
01d37a1b56
|
@ -5,6 +5,7 @@ use traits::dot::Dot;
|
|||
use traits::dim::Dim;
|
||||
use traits::basis::Basis;
|
||||
use traits::norm::Norm;
|
||||
use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
|
||||
|
||||
#[deriving(Eq)]
|
||||
pub struct Vec1<T>
|
||||
|
@ -31,6 +32,47 @@ impl<T:Copy + Sub<T,T>> Sub<Vec1<T>, Vec1<T>> for Vec1<T>
|
|||
{ Vec1(self.x - other.x) }
|
||||
}
|
||||
|
||||
impl<T: Copy + Mul<T, T>>
|
||||
ScalarMul<T> for Vec1<T>
|
||||
{
|
||||
fn scalar_mul(&self, s: &T) -> Vec1<T>
|
||||
{ Vec1 { x: self.x * *s } }
|
||||
|
||||
fn scalar_mul_inplace(&mut self, s: &T)
|
||||
{ self.x *= *s; }
|
||||
}
|
||||
|
||||
|
||||
impl<T: Copy + Quot<T, T>>
|
||||
ScalarDiv<T> for Vec1<T>
|
||||
{
|
||||
fn scalar_div(&self, s: &T) -> Vec1<T>
|
||||
{ Vec1 { x: self.x / *s } }
|
||||
|
||||
fn scalar_div_inplace(&mut self, s: &T)
|
||||
{ self.x /= *s; }
|
||||
}
|
||||
|
||||
impl<T: Copy + Add<T, T>>
|
||||
ScalarAdd<T> for Vec1<T>
|
||||
{
|
||||
fn scalar_add(&self, s: &T) -> Vec1<T>
|
||||
{ Vec1 { x: self.x + *s } }
|
||||
|
||||
fn scalar_add_inplace(&mut self, s: &T)
|
||||
{ self.x += *s; }
|
||||
}
|
||||
|
||||
impl<T: Copy + Sub<T, T>>
|
||||
ScalarSub<T> for Vec1<T>
|
||||
{
|
||||
fn scalar_sub(&self, s: &T) -> Vec1<T>
|
||||
{ Vec1 { x: self.x - *s } }
|
||||
|
||||
fn scalar_sub_inplace(&mut self, s: &T)
|
||||
{ self.x -= *s; }
|
||||
}
|
||||
|
||||
impl<T:Copy + Mul<T, T> + Add<T, T> + Algebraic> Dot<T> for Vec1<T>
|
||||
{
|
||||
fn dot(&self, other : &Vec1<T>) -> T
|
||||
|
|
|
@ -7,6 +7,7 @@ use traits::cross::Cross;
|
|||
use traits::basis::Basis;
|
||||
use traits::norm::Norm;
|
||||
use dim1::vec1::Vec1;
|
||||
use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
|
||||
|
||||
#[deriving(Eq)]
|
||||
pub struct Vec2<T>
|
||||
|
@ -36,6 +37,59 @@ impl<T:Copy + Sub<T,T>> Sub<Vec2<T>, Vec2<T>> for Vec2<T>
|
|||
{ Vec2(self.x - other.x, self.y - other.y) }
|
||||
}
|
||||
|
||||
impl<T: Copy + Mul<T, T>>
|
||||
ScalarMul<T> for Vec2<T>
|
||||
{
|
||||
fn scalar_mul(&self, s: &T) -> Vec2<T>
|
||||
{ Vec2 { x: self.x * *s, y: self.y * *s } }
|
||||
|
||||
fn scalar_mul_inplace(&mut self, s: &T)
|
||||
{
|
||||
self.x *= *s;
|
||||
self.y *= *s;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<T: Copy + Quot<T, T>>
|
||||
ScalarDiv<T> for Vec2<T>
|
||||
{
|
||||
fn scalar_div(&self, s: &T) -> Vec2<T>
|
||||
{ Vec2 { x: self.x / *s, y: self.y / *s } }
|
||||
|
||||
fn scalar_div_inplace(&mut self, s: &T)
|
||||
{
|
||||
self.x /= *s;
|
||||
self.y /= *s;
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy + Add<T, T>>
|
||||
ScalarAdd<T> for Vec2<T>
|
||||
{
|
||||
fn scalar_add(&self, s: &T) -> Vec2<T>
|
||||
{ Vec2 { x: self.x + *s, y: self.y + *s } }
|
||||
|
||||
fn scalar_add_inplace(&mut self, s: &T)
|
||||
{
|
||||
self.x += *s;
|
||||
self.y += *s;
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy + Sub<T, T>>
|
||||
ScalarSub<T> for Vec2<T>
|
||||
{
|
||||
fn scalar_sub(&self, s: &T) -> Vec2<T>
|
||||
{ Vec2 { x: self.x - *s, y: self.y - *s } }
|
||||
|
||||
fn scalar_sub_inplace(&mut self, s: &T)
|
||||
{
|
||||
self.x -= *s;
|
||||
self.y -= *s;
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy + Mul<T, T> + Add<T, T> + Algebraic> Dot<T> for Vec2<T>
|
||||
{
|
||||
fn dot(&self, other : &Vec2<T>) -> T
|
||||
|
|
|
@ -6,6 +6,7 @@ use traits::dot::Dot;
|
|||
use traits::cross::Cross;
|
||||
use traits::basis::Basis;
|
||||
use traits::norm::Norm;
|
||||
use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
|
||||
|
||||
#[deriving(Eq)]
|
||||
pub struct Vec3<T>
|
||||
|
@ -36,6 +37,64 @@ impl<T:Copy + Sub<T,T>> Sub<Vec3<T>, Vec3<T>> for Vec3<T>
|
|||
{ Vec3(self.x - other.x, self.y - other.y, self.z - other.z) }
|
||||
}
|
||||
|
||||
impl<T: Copy + Mul<T, T>>
|
||||
ScalarMul<T> for Vec3<T>
|
||||
{
|
||||
fn scalar_mul(&self, s: &T) -> Vec3<T>
|
||||
{ Vec3 { x: self.x * *s, y: self.y * *s, z: self.z * *s } }
|
||||
|
||||
fn scalar_mul_inplace(&mut self, s: &T)
|
||||
{
|
||||
self.x *= *s;
|
||||
self.y *= *s;
|
||||
self.z *= *s;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<T: Copy + Quot<T, T>>
|
||||
ScalarDiv<T> for Vec3<T>
|
||||
{
|
||||
fn scalar_div(&self, s: &T) -> Vec3<T>
|
||||
{ Vec3 { x: self.x / *s, y: self.y / *s, z: self.z / *s } }
|
||||
|
||||
fn scalar_div_inplace(&mut self, s: &T)
|
||||
{
|
||||
self.x /= *s;
|
||||
self.y /= *s;
|
||||
self.z /= *s;
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy + Add<T, T>>
|
||||
ScalarAdd<T> for Vec3<T>
|
||||
{
|
||||
fn scalar_add(&self, s: &T) -> Vec3<T>
|
||||
{ Vec3 { x: self.x + *s, y: self.y + *s, z: self.z + *s } }
|
||||
|
||||
fn scalar_add_inplace(&mut self, s: &T)
|
||||
{
|
||||
self.x += *s;
|
||||
self.y += *s;
|
||||
self.z += *s;
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy + Sub<T, T>>
|
||||
ScalarSub<T> for Vec3<T>
|
||||
{
|
||||
fn scalar_sub(&self, s: &T) -> Vec3<T>
|
||||
{ Vec3 { x: self.x - *s, y: self.y - *s, z: self.z - *s } }
|
||||
|
||||
fn scalar_sub_inplace(&mut self, s: &T)
|
||||
{
|
||||
self.x -= *s;
|
||||
self.y -= *s;
|
||||
self.z -= *s;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<T:Copy + Neg<T>> Neg<Vec3<T>> for Vec3<T>
|
||||
{
|
||||
fn neg(&self) -> Vec3<T>
|
||||
|
|
|
@ -32,7 +32,7 @@ pub use traits::norm::Norm;
|
|||
|
||||
pub use traits::workarounds::rlmul::{RMul, LMul};
|
||||
pub use traits::workarounds::trigonometric::Trigonometric;
|
||||
pub use traits::workarounds::scalar_op::ScalarOp;
|
||||
pub use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
|
||||
|
||||
mod dim2
|
||||
{
|
||||
|
|
|
@ -6,7 +6,7 @@ use traits::dim::Dim;
|
|||
use traits::dot::Dot;
|
||||
use traits::norm::Norm;
|
||||
use traits::basis::Basis;
|
||||
use traits::workarounds::scalar_op::ScalarOp;
|
||||
use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
|
||||
|
||||
// D is a phantom parameter, used only as a dimensional token.
|
||||
// Its allows use to encode the vector dimension at the type-level.
|
||||
|
@ -65,38 +65,51 @@ Dot<T> for NVec<D, T>
|
|||
}
|
||||
}
|
||||
|
||||
impl<D: Dim, T: Copy + Mul<T, T> + Quot<T, T> + Add<T, T> + Sub<T, T>>
|
||||
ScalarOp<T> for NVec<D, T>
|
||||
impl<D: Dim, T: Copy + Mul<T, T>>
|
||||
ScalarMul<T> for NVec<D, T>
|
||||
{
|
||||
fn scalar_mul(&self, s: &T) -> NVec<D, T>
|
||||
{ NVec { at: map(self.at, |a| a * *s) } }
|
||||
|
||||
fn scalar_div(&self, s: &T) -> NVec<D, T>
|
||||
{ NVec { at: map(self.at, |a| a / *s) } }
|
||||
|
||||
fn scalar_add(&self, s: &T) -> NVec<D, T>
|
||||
{ NVec { at: map(self.at, |a| a + *s) } }
|
||||
|
||||
fn scalar_sub(&self, s: &T) -> NVec<D, T>
|
||||
{ NVec { at: map(self.at, |a| a - *s) } }
|
||||
|
||||
fn scalar_mul_inplace(&mut self, s: &T)
|
||||
{
|
||||
for uint::range(0u, Dim::dim::<D>()) |i|
|
||||
{ self.at[i] *= *s; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<D: Dim, T: Copy + Quot<T, T>>
|
||||
ScalarDiv<T> for NVec<D, T>
|
||||
{
|
||||
fn scalar_div(&self, s: &T) -> NVec<D, T>
|
||||
{ NVec { at: map(self.at, |a| a / *s) } }
|
||||
|
||||
fn scalar_div_inplace(&mut self, s: &T)
|
||||
{
|
||||
for uint::range(0u, Dim::dim::<D>()) |i|
|
||||
{ self.at[i] /= *s; }
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Dim, T: Copy + Add<T, T>>
|
||||
ScalarAdd<T> for NVec<D, T>
|
||||
{
|
||||
fn scalar_add(&self, s: &T) -> NVec<D, T>
|
||||
{ NVec { at: map(self.at, |a| a + *s) } }
|
||||
|
||||
fn scalar_add_inplace(&mut self, s: &T)
|
||||
{
|
||||
for uint::range(0u, Dim::dim::<D>()) |i|
|
||||
{ self.at[i] += *s; }
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Dim, T: Copy + Sub<T, T>>
|
||||
ScalarSub<T> for NVec<D, T>
|
||||
{
|
||||
fn scalar_sub(&self, s: &T) -> NVec<D, T>
|
||||
{ NVec { at: map(self.at, |a| a - *s) } }
|
||||
|
||||
fn scalar_sub_inplace(&mut self, s: &T)
|
||||
{
|
||||
|
|
|
@ -1,12 +1,23 @@
|
|||
pub trait ScalarOp<T>
|
||||
pub trait ScalarMul<T>
|
||||
{
|
||||
fn scalar_mul(&self, &T) -> Self;
|
||||
fn scalar_div(&self, &T) -> Self;
|
||||
fn scalar_add(&self, &T) -> Self;
|
||||
fn scalar_sub(&self, &T) -> Self;
|
||||
|
||||
fn scalar_mul_inplace(&mut self, &T);
|
||||
}
|
||||
|
||||
pub trait ScalarDiv<T>
|
||||
{
|
||||
fn scalar_div(&self, &T) -> Self;
|
||||
fn scalar_div_inplace(&mut self, &T);
|
||||
}
|
||||
|
||||
pub trait ScalarAdd<T>
|
||||
{
|
||||
fn scalar_add(&self, &T) -> Self;
|
||||
fn scalar_add_inplace(&mut self, &T);
|
||||
}
|
||||
|
||||
pub trait ScalarSub<T>
|
||||
{
|
||||
fn scalar_sub(&self, &T) -> Self;
|
||||
fn scalar_sub_inplace(&mut self, &T);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue