Used a better type parametter name.

This commit is contained in:
Sébastien Crozet 2013-06-09 23:36:47 +00:00
parent bd5f1eae88
commit c02edb9d09
20 changed files with 511 additions and 511 deletions

View File

@ -24,7 +24,7 @@ impl<M: Copy> Rotmat<M>
{ self.submat }
}
pub fn rotmat2<T: Copy + Trigonometric + Neg<T>>(angle: T) -> Rotmat<Mat2<T>>
pub fn rotmat2<N: Copy + Trigonometric + Neg<N>>(angle: N) -> Rotmat<Mat2<N>>
{
let coa = angle.cos();
let sia = angle.sin();
@ -33,11 +33,11 @@ pub fn rotmat2<T: Copy + Trigonometric + Neg<T>>(angle: T) -> Rotmat<Mat2<T>>
{ submat: Mat2::new(coa, -sia, sia, coa) }
}
pub fn rotmat3<T: Copy + Trigonometric + Neg<T> + One + Sub<T, T> + Add<T, T> +
Mul<T, T>>
(axis: &Vec3<T>, angle: T) -> Rotmat<Mat3<T>>
pub fn rotmat3<N: Copy + Trigonometric + Neg<N> + One + Sub<N, N> + Add<N, N> +
Mul<N, N>>
(axis: &Vec3<N>, angle: N) -> Rotmat<Mat3<N>>
{
let _1 = One::one::<T>();
let _1 = One::one::<N>();
let ux = axis.x;
let uy = axis.y;
let uz = axis.z;
@ -64,44 +64,44 @@ pub fn rotmat3<T: Copy + Trigonometric + Neg<T> + One + Sub<T, T> + Add<T, T> +
}
}
impl<T: Div<T, T> + Trigonometric + Neg<T> + Mul<T, T> + Add<T, T> + Copy>
Rotation<Vec1<T>> for Rotmat<Mat2<T>>
impl<N: Div<N, N> + Trigonometric + Neg<N> + Mul<N, N> + Add<N, N> + Copy>
Rotation<Vec1<N>> for Rotmat<Mat2<N>>
{
fn rotation(&self) -> Vec1<T>
fn rotation(&self) -> Vec1<N>
{ Vec1::new(-(self.submat.m12 / self.submat.m11).atan()) }
fn rotated(&self, rot: &Vec1<T>) -> Rotmat<Mat2<T>>
fn rotated(&self, rot: &Vec1<N>) -> Rotmat<Mat2<N>>
{ rotmat2(rot.x) * *self }
fn rotate(&mut self, rot: &Vec1<T>)
fn rotate(&mut self, rot: &Vec1<N>)
{ *self = self.rotated(rot) }
}
impl<T: Div<T, T> + Trigonometric + Neg<T> + Mul<T, T> + Add<T, T> + Copy +
One + Sub<T, T>>
Rotation<(Vec3<T>, T)> for Rotmat<Mat3<T>>
impl<N: Div<N, N> + Trigonometric + Neg<N> + Mul<N, N> + Add<N, N> + Copy +
One + Sub<N, N>>
Rotation<(Vec3<N>, N)> for Rotmat<Mat3<N>>
{
fn rotation(&self) -> (Vec3<T>, T)
fn rotation(&self) -> (Vec3<N>, N)
{ fail!("Not yet implemented.") }
fn rotated(&self, &(axis, angle): &(Vec3<T>, T)) -> Rotmat<Mat3<T>>
fn rotated(&self, &(axis, angle): &(Vec3<N>, N)) -> Rotmat<Mat3<N>>
{ rotmat3(&axis, angle) * *self }
fn rotate(&mut self, rot: &(Vec3<T>, T))
fn rotate(&mut self, rot: &(Vec3<N>, N))
{ *self = self.rotated(rot) }
}
impl<T: Copy + Rand + Trigonometric + Neg<T>> Rand for Rotmat<Mat2<T>>
impl<N: Copy + Rand + Trigonometric + Neg<N>> Rand for Rotmat<Mat2<N>>
{
fn rand<R: Rng>(rng: &mut R) -> Rotmat<Mat2<T>>
fn rand<R: Rng>(rng: &mut R) -> Rotmat<Mat2<N>>
{ rotmat2(rng.gen()) }
}
impl<T: Copy + Rand + Trigonometric + Neg<T> + One + Sub<T, T> + Add<T, T> +
Mul<T, T>>
Rand for Rotmat<Mat3<T>>
impl<N: Copy + Rand + Trigonometric + Neg<N> + One + Sub<N, N> + Add<N, N> +
Mul<N, N>>
Rand for Rotmat<Mat3<N>>
{
fn rand<R: Rng>(rng: &mut R) -> Rotmat<Mat3<T>>
fn rand<R: Rng>(rng: &mut R) -> Rotmat<Mat3<N>>
{ rotmat3(&rng.gen(), rng.gen()) }
}
@ -160,14 +160,14 @@ Transpose for Rotmat<M>
{ self.submat.transpose() }
}
impl<T: ApproxEq<T>, M: ApproxEq<T>> ApproxEq<T> for Rotmat<M>
impl<N: ApproxEq<N>, M: ApproxEq<N>> ApproxEq<N> for Rotmat<M>
{
fn approx_epsilon() -> T
{ ApproxEq::approx_epsilon::<T, T>() }
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
fn approx_eq(&self, other: &Rotmat<M>) -> bool
{ self.submat.approx_eq(&other.submat) }
fn approx_eq_eps(&self, other: &Rotmat<M>, epsilon: &T) -> bool
fn approx_eq_eps(&self, other: &Rotmat<M>, epsilon: &N) -> bool
{ self.submat.approx_eq_eps(&other.submat, epsilon) }
}

View File

@ -123,11 +123,11 @@ Inv for Transform<M, V>
}
}
impl<T: ApproxEq<T>, M:ApproxEq<T>, V:ApproxEq<T>>
ApproxEq<T> for Transform<M, V>
impl<N: ApproxEq<N>, M:ApproxEq<N>, V:ApproxEq<N>>
ApproxEq<N> for Transform<M, V>
{
fn approx_epsilon() -> T
{ ApproxEq::approx_epsilon::<T, T>() }
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
fn approx_eq(&self, other: &Transform<M, V>) -> bool
{
@ -135,7 +135,7 @@ ApproxEq<T> for Transform<M, V>
self.subtrans.approx_eq(&other.subtrans)
}
fn approx_eq_eps(&self, other: &Transform<M, V>, epsilon: &T) -> bool
fn approx_eq_eps(&self, other: &Transform<M, V>, epsilon: &N) -> bool
{
self.submat.approx_eq_eps(&other.submat, epsilon) &&
self.subtrans.approx_eq_eps(&other.subtrans, epsilon)

View File

@ -8,63 +8,63 @@ use traits::workarounds::rlmul::{RMul, LMul};
use dim1::vec1::Vec1;
#[deriving(Eq, ToStr)]
pub struct Mat1<T>
{ m11: T }
pub struct Mat1<N>
{ m11: N }
impl<T: Copy> Mat1<T>
impl<N: Copy> Mat1<N>
{
pub fn new(m11: T) -> Mat1<T>
pub fn new(m11: N) -> Mat1<N>
{
Mat1
{ m11: m11 }
}
}
impl<T> Dim for Mat1<T>
impl<N> Dim for Mat1<N>
{
fn dim() -> uint
{ 1 }
}
impl<T:Copy + One> One for Mat1<T>
impl<N:Copy + One> One for Mat1<N>
{
fn one() -> Mat1<T>
fn one() -> Mat1<N>
{ return Mat1::new(One::one()) }
}
impl<T:Copy + Zero> Zero for Mat1<T>
impl<N:Copy + Zero> Zero for Mat1<N>
{
fn zero() -> Mat1<T>
fn zero() -> Mat1<N>
{ Mat1::new(Zero::zero()) }
fn is_zero(&self) -> bool
{ self.m11.is_zero() }
}
impl<T:Copy + Mul<T, T> + Add<T, T>> Mul<Mat1<T>, Mat1<T>> for Mat1<T>
impl<N:Copy + Mul<N, N> + Add<N, N>> Mul<Mat1<N>, Mat1<N>> for Mat1<N>
{
fn mul(&self, other: &Mat1<T>) -> Mat1<T>
fn mul(&self, other: &Mat1<N>) -> Mat1<N>
{ Mat1::new(self.m11 * other.m11) }
}
impl<T:Copy + Add<T, T> + Mul<T, T>> RMul<Vec1<T>> for Mat1<T>
impl<N:Copy + Add<N, N> + Mul<N, N>> RMul<Vec1<N>> for Mat1<N>
{
fn rmul(&self, other: &Vec1<T>) -> Vec1<T>
fn rmul(&self, other: &Vec1<N>) -> Vec1<N>
{ Vec1::new(self.m11 * other.x) }
}
impl<T:Copy + Add<T, T> + Mul<T, T>> LMul<Vec1<T>> for Mat1<T>
impl<N:Copy + Add<N, N> + Mul<N, N>> LMul<Vec1<N>> for Mat1<N>
{
fn lmul(&self, other: &Vec1<T>) -> Vec1<T>
fn lmul(&self, other: &Vec1<N>) -> Vec1<N>
{ Vec1::new(self.m11 * other.x) }
}
impl<T:Copy + Mul<T, T> + Div<T, T> + Sub<T, T> + Neg<T> + Zero + One>
Inv for Mat1<T>
impl<N:Copy + Mul<N, N> + Div<N, N> + Sub<N, N> + Neg<N> + Zero + One>
Inv for Mat1<N>
{
fn inverse(&self) -> Mat1<T>
fn inverse(&self) -> Mat1<N>
{
let mut res : Mat1<T> = *self;
let mut res : Mat1<N> = *self;
res.invert();
@ -75,33 +75,33 @@ Inv for Mat1<T>
{
assert!(!self.m11.is_zero());
self.m11 = One::one::<T>() / self.m11
self.m11 = One::one::<N>() / self.m11
}
}
impl<T:Copy> Transpose for Mat1<T>
impl<N:Copy> Transpose for Mat1<N>
{
fn transposed(&self) -> Mat1<T>
fn transposed(&self) -> Mat1<N>
{ *self }
fn transpose(&mut self)
{ }
}
impl<T:ApproxEq<T>> ApproxEq<T> for Mat1<T>
impl<N:ApproxEq<N>> ApproxEq<N> for Mat1<N>
{
fn approx_epsilon() -> T
{ ApproxEq::approx_epsilon::<T, T>() }
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
fn approx_eq(&self, other: &Mat1<T>) -> bool
fn approx_eq(&self, other: &Mat1<N>) -> bool
{ self.m11.approx_eq(&other.m11) }
fn approx_eq_eps(&self, other: &Mat1<T>, epsilon: &T) -> bool
fn approx_eq_eps(&self, other: &Mat1<N>, epsilon: &N) -> bool
{ self.m11.approx_eq_eps(&other.m11, epsilon) }
}
impl<T:Rand + Copy> Rand for Mat1<T>
impl<N:Rand + Copy> Rand for Mat1<N>
{
fn rand<R: Rng>(rng: &mut R) -> Mat1<T>
fn rand<R: Rng>(rng: &mut R) -> Mat1<N>
{ Mat1::new(rng.gen()) }
}

View File

@ -10,112 +10,112 @@ use traits::sub_dot::SubDot;
use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
#[deriving(Eq, ToStr)]
pub struct Vec1<T>
{ x : T }
pub struct Vec1<N>
{ x : N }
impl<T: Copy> Vec1<T>
impl<N: Copy> Vec1<N>
{
pub fn new(x: T) -> Vec1<T>
pub fn new(x: N) -> Vec1<N>
{ Vec1 {x: x} }
}
impl<T> Dim for Vec1<T>
impl<N> Dim for Vec1<N>
{
fn dim() -> uint
{ 1 }
}
impl<T:Copy + Add<T,T>> Add<Vec1<T>, Vec1<T>> for Vec1<T>
impl<N:Copy + Add<N,N>> Add<Vec1<N>, Vec1<N>> for Vec1<N>
{
fn add(&self, other: &Vec1<T>) -> Vec1<T>
fn add(&self, other: &Vec1<N>) -> Vec1<N>
{ Vec1::new(self.x + other.x) }
}
impl<T:Copy + Sub<T,T>> Sub<Vec1<T>, Vec1<T>> for Vec1<T>
impl<N:Copy + Sub<N,N>> Sub<Vec1<N>, Vec1<N>> for Vec1<N>
{
fn sub(&self, other: &Vec1<T>) -> Vec1<T>
fn sub(&self, other: &Vec1<N>) -> Vec1<N>
{ Vec1::new(self.x - other.x) }
}
impl<T: Copy + Mul<T, T>>
ScalarMul<T> for Vec1<T>
impl<N: Copy + Mul<N, N>>
ScalarMul<N> for Vec1<N>
{
fn scalar_mul(&self, s: &T) -> Vec1<T>
fn scalar_mul(&self, s: &N) -> Vec1<N>
{ Vec1 { x: self.x * *s } }
fn scalar_mul_inplace(&mut self, s: &T)
fn scalar_mul_inplace(&mut self, s: &N)
{ self.x *= *s; }
}
impl<T: Copy + Div<T, T>>
ScalarDiv<T> for Vec1<T>
impl<N: Copy + Div<N, N>>
ScalarDiv<N> for Vec1<N>
{
fn scalar_div(&self, s: &T) -> Vec1<T>
fn scalar_div(&self, s: &N) -> Vec1<N>
{ Vec1 { x: self.x / *s } }
fn scalar_div_inplace(&mut self, s: &T)
fn scalar_div_inplace(&mut self, s: &N)
{ self.x /= *s; }
}
impl<T: Copy + Add<T, T>>
ScalarAdd<T> for Vec1<T>
impl<N: Copy + Add<N, N>>
ScalarAdd<N> for Vec1<N>
{
fn scalar_add(&self, s: &T) -> Vec1<T>
fn scalar_add(&self, s: &N) -> Vec1<N>
{ Vec1 { x: self.x + *s } }
fn scalar_add_inplace(&mut self, s: &T)
fn scalar_add_inplace(&mut self, s: &N)
{ self.x += *s; }
}
impl<T: Copy + Sub<T, T>>
ScalarSub<T> for Vec1<T>
impl<N: Copy + Sub<N, N>>
ScalarSub<N> for Vec1<N>
{
fn scalar_sub(&self, s: &T) -> Vec1<T>
fn scalar_sub(&self, s: &N) -> Vec1<N>
{ Vec1 { x: self.x - *s } }
fn scalar_sub_inplace(&mut self, s: &T)
fn scalar_sub_inplace(&mut self, s: &N)
{ self.x -= *s; }
}
impl<T: Copy + Add<T, T>> Translation<Vec1<T>> for Vec1<T>
impl<N: Copy + Add<N, N>> Translation<Vec1<N>> for Vec1<N>
{
fn translation(&self) -> Vec1<T>
fn translation(&self) -> Vec1<N>
{ *self }
fn translated(&self, t: &Vec1<T>) -> Vec1<T>
fn translated(&self, t: &Vec1<N>) -> Vec1<N>
{ self + *t }
fn translate(&mut self, t: &Vec1<T>)
fn translate(&mut self, t: &Vec1<N>)
{ *self += *t }
}
impl<T:Copy + Mul<T, T>> Dot<T> for Vec1<T>
impl<N:Copy + Mul<N, N>> Dot<N> for Vec1<N>
{
fn dot(&self, other : &Vec1<T>) -> T
fn dot(&self, other : &Vec1<N>) -> N
{ self.x * other.x }
}
impl<T:Copy + Mul<T, T> + Sub<T, T>> SubDot<T> for Vec1<T>
impl<N:Copy + Mul<N, N> + Sub<N, N>> SubDot<N> for Vec1<N>
{
fn sub_dot(&self, a: &Vec1<T>, b: &Vec1<T>) -> T
fn sub_dot(&self, a: &Vec1<N>, b: &Vec1<N>) -> N
{ (self.x - a.x) * b.x }
}
impl<T:Copy + Mul<T, T> + Add<T, T> + Div<T, T> + Algebraic>
Norm<T> for Vec1<T>
impl<N:Copy + Mul<N, N> + Add<N, N> + Div<N, N> + Algebraic>
Norm<N> for Vec1<N>
{
fn sqnorm(&self) -> T
fn sqnorm(&self) -> N
{ self.dot(self) }
fn norm(&self) -> T
fn norm(&self) -> N
{ self.sqnorm().sqrt() }
fn normalized(&self) -> Vec1<T>
fn normalized(&self) -> Vec1<N>
{ Vec1::new(self.x / self.norm()) }
fn normalize(&mut self) -> T
fn normalize(&mut self) -> N
{
let l = self.norm();
@ -125,15 +125,15 @@ Norm<T> for Vec1<T>
}
}
impl<T:Copy + Neg<T>> Neg<Vec1<T>> for Vec1<T>
impl<N:Copy + Neg<N>> Neg<Vec1<N>> for Vec1<N>
{
fn neg(&self) -> Vec1<T>
fn neg(&self) -> Vec1<N>
{ Vec1::new(-self.x) }
}
impl<T:Copy + Zero> Zero for Vec1<T>
impl<N:Copy + Zero> Zero for Vec1<N>
{
fn zero() -> Vec1<T>
fn zero() -> Vec1<N>
{
let _0 = Zero::zero();
Vec1::new(_0)
@ -143,29 +143,29 @@ impl<T:Copy + Zero> Zero for Vec1<T>
{ self.x.is_zero() }
}
impl<T: Copy + One> Basis for Vec1<T>
impl<N: Copy + One> Basis for Vec1<N>
{
fn canonical_basis() -> ~[Vec1<T>]
fn canonical_basis() -> ~[Vec1<N>]
{ ~[ Vec1::new(One::one()) ] } // FIXME: this should be static
fn orthogonal_subspace_basis(&self) -> ~[Vec1<T>]
fn orthogonal_subspace_basis(&self) -> ~[Vec1<N>]
{ ~[] }
}
impl<T:ApproxEq<T>> ApproxEq<T> for Vec1<T>
impl<N:ApproxEq<N>> ApproxEq<N> for Vec1<N>
{
fn approx_epsilon() -> T
{ ApproxEq::approx_epsilon::<T, T>() }
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
fn approx_eq(&self, other: &Vec1<T>) -> bool
fn approx_eq(&self, other: &Vec1<N>) -> bool
{ self.x.approx_eq(&other.x) }
fn approx_eq_eps(&self, other: &Vec1<T>, epsilon: &T) -> bool
fn approx_eq_eps(&self, other: &Vec1<N>, epsilon: &N) -> bool
{ self.x.approx_eq_eps(&other.x, epsilon) }
}
impl<T:Rand + Copy> Rand for Vec1<T>
impl<N:Rand + Copy> Rand for Vec1<N>
{
fn rand<R: Rng>(rng: &mut R) -> Vec1<T>
fn rand<R: Rng>(rng: &mut R) -> Vec1<N>
{ Vec1::new(rng.gen()) }
}

View File

@ -9,15 +9,15 @@ use traits::workarounds::rlmul::{RMul, LMul};
use dim2::vec2::Vec2;
#[deriving(Eq, ToStr)]
pub struct Mat2<T>
pub struct Mat2<N>
{
m11: T, m12: T,
m21: T, m22: T
m11: N, m12: N,
m21: N, m22: N
}
impl<T: Copy> Mat2<T>
impl<N: Copy> Mat2<N>
{
pub fn new(m11: T, m12: T, m21: T, m22: T) -> Mat2<T>
pub fn new(m11: N, m12: N, m21: N, m22: N) -> Mat2<N>
{
Mat2
{
@ -27,15 +27,15 @@ impl<T: Copy> Mat2<T>
}
}
impl<T> Dim for Mat2<T>
impl<N> Dim for Mat2<N>
{
fn dim() -> uint
{ 2 }
}
impl<T:Copy + One + Zero> One for Mat2<T>
impl<N:Copy + One + Zero> One for Mat2<N>
{
fn one() -> Mat2<T>
fn one() -> Mat2<N>
{
let (_0, _1) = (Zero::zero(), One::one());
return Mat2::new(_1, _0,
@ -43,9 +43,9 @@ impl<T:Copy + One + Zero> One for Mat2<T>
}
}
impl<T:Copy + Zero> Zero for Mat2<T>
impl<N:Copy + Zero> Zero for Mat2<N>
{
fn zero() -> Mat2<T>
fn zero() -> Mat2<N>
{
let _0 = Zero::zero();
return Mat2::new(_0, _0,
@ -59,9 +59,9 @@ impl<T:Copy + Zero> Zero for Mat2<T>
}
}
impl<T:Copy + Mul<T, T> + Add<T, T>> Mul<Mat2<T>, Mat2<T>> for Mat2<T>
impl<N:Copy + Mul<N, N> + Add<N, N>> Mul<Mat2<N>, Mat2<N>> for Mat2<N>
{
fn mul(&self, other: &Mat2<T>) -> Mat2<T>
fn mul(&self, other: &Mat2<N>) -> Mat2<N>
{
Mat2::new(
self.m11 * other.m11 + self.m12 * other.m21,
@ -72,9 +72,9 @@ impl<T:Copy + Mul<T, T> + Add<T, T>> Mul<Mat2<T>, Mat2<T>> for Mat2<T>
}
}
impl<T:Copy + Add<T, T> + Mul<T, T>> RMul<Vec2<T>> for Mat2<T>
impl<N:Copy + Add<N, N> + Mul<N, N>> RMul<Vec2<N>> for Mat2<N>
{
fn rmul(&self, other: &Vec2<T>) -> Vec2<T>
fn rmul(&self, other: &Vec2<N>) -> Vec2<N>
{
Vec2::new(
self.m11 * other.x + self.m12 * other.y,
@ -83,9 +83,9 @@ impl<T:Copy + Add<T, T> + Mul<T, T>> RMul<Vec2<T>> for Mat2<T>
}
}
impl<T:Copy + Add<T, T> + Mul<T, T>> LMul<Vec2<T>> for Mat2<T>
impl<N:Copy + Add<N, N> + Mul<N, N>> LMul<Vec2<N>> for Mat2<N>
{
fn lmul(&self, other: &Vec2<T>) -> Vec2<T>
fn lmul(&self, other: &Vec2<N>) -> Vec2<N>
{
Vec2::new(
self.m11 * other.x + self.m21 * other.y,
@ -94,12 +94,12 @@ impl<T:Copy + Add<T, T> + Mul<T, T>> LMul<Vec2<T>> for Mat2<T>
}
}
impl<T:Copy + Mul<T, T> + Div<T, T> + Sub<T, T> + Neg<T> + Zero>
Inv for Mat2<T>
impl<N:Copy + Mul<N, N> + Div<N, N> + Sub<N, N> + Neg<N> + Zero>
Inv for Mat2<N>
{
fn inverse(&self) -> Mat2<T>
fn inverse(&self) -> Mat2<N>
{
let mut res : Mat2<T> = *self;
let mut res : Mat2<N> = *self;
res.invert();
@ -117,9 +117,9 @@ Inv for Mat2<T>
}
}
impl<T:Copy> Transpose for Mat2<T>
impl<N:Copy> Transpose for Mat2<N>
{
fn transposed(&self) -> Mat2<T>
fn transposed(&self) -> Mat2<N>
{
Mat2::new(self.m11, self.m21,
self.m12, self.m22)
@ -129,12 +129,12 @@ impl<T:Copy> Transpose for Mat2<T>
{ swap(&mut self.m21, &mut self.m12); }
}
impl<T:ApproxEq<T>> ApproxEq<T> for Mat2<T>
impl<N:ApproxEq<N>> ApproxEq<N> for Mat2<N>
{
fn approx_epsilon() -> T
{ ApproxEq::approx_epsilon::<T, T>() }
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
fn approx_eq(&self, other: &Mat2<T>) -> bool
fn approx_eq(&self, other: &Mat2<N>) -> bool
{
self.m11.approx_eq(&other.m11) &&
self.m12.approx_eq(&other.m12) &&
@ -143,7 +143,7 @@ impl<T:ApproxEq<T>> ApproxEq<T> for Mat2<T>
self.m22.approx_eq(&other.m22)
}
fn approx_eq_eps(&self, other: &Mat2<T>, epsilon: &T) -> bool
fn approx_eq_eps(&self, other: &Mat2<N>, epsilon: &N) -> bool
{
self.m11.approx_eq_eps(&other.m11, epsilon) &&
self.m12.approx_eq_eps(&other.m12, epsilon) &&
@ -153,8 +153,8 @@ impl<T:ApproxEq<T>> ApproxEq<T> for Mat2<T>
}
}
impl<T:Rand + Copy> Rand for Mat2<T>
impl<N:Rand + Copy> Rand for Mat2<N>
{
fn rand<R: Rng>(rng: &mut R) -> Mat2<T>
fn rand<R: Rng>(rng: &mut R) -> Mat2<N>
{ Mat2::new(rng.gen(), rng.gen(), rng.gen(), rng.gen()) }
}

View File

@ -12,43 +12,43 @@ use traits::translation::Translation;
use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
#[deriving(Eq, ToStr)]
pub struct Vec2<T>
pub struct Vec2<N>
{
x : T,
y : T
x : N,
y : N
}
impl<T: Copy> Vec2<T>
impl<N: Copy> Vec2<N>
{
pub fn new(x: T, y: T) -> Vec2<T>
pub fn new(x: N, y: N) -> Vec2<N>
{ Vec2 {x: x, y: y} }
}
impl<T> Dim for Vec2<T>
impl<N> Dim for Vec2<N>
{
fn dim() -> uint
{ 2 }
}
impl<T:Copy + Add<T,T>> Add<Vec2<T>, Vec2<T>> for Vec2<T>
impl<N:Copy + Add<N,N>> Add<Vec2<N>, Vec2<N>> for Vec2<N>
{
fn add(&self, other: &Vec2<T>) -> Vec2<T>
fn add(&self, other: &Vec2<N>) -> Vec2<N>
{ Vec2::new(self.x + other.x, self.y + other.y) }
}
impl<T:Copy + Sub<T,T>> Sub<Vec2<T>, Vec2<T>> for Vec2<T>
impl<N:Copy + Sub<N,N>> Sub<Vec2<N>, Vec2<N>> for Vec2<N>
{
fn sub(&self, other: &Vec2<T>) -> Vec2<T>
fn sub(&self, other: &Vec2<N>) -> Vec2<N>
{ Vec2::new(self.x - other.x, self.y - other.y) }
}
impl<T: Copy + Mul<T, T>>
ScalarMul<T> for Vec2<T>
impl<N: Copy + Mul<N, N>>
ScalarMul<N> for Vec2<N>
{
fn scalar_mul(&self, s: &T) -> Vec2<T>
fn scalar_mul(&self, s: &N) -> Vec2<N>
{ Vec2 { x: self.x * *s, y: self.y * *s } }
fn scalar_mul_inplace(&mut self, s: &T)
fn scalar_mul_inplace(&mut self, s: &N)
{
self.x *= *s;
self.y *= *s;
@ -56,86 +56,86 @@ ScalarMul<T> for Vec2<T>
}
impl<T: Copy + Div<T, T>>
ScalarDiv<T> for Vec2<T>
impl<N: Copy + Div<N, N>>
ScalarDiv<N> for Vec2<N>
{
fn scalar_div(&self, s: &T) -> Vec2<T>
fn scalar_div(&self, s: &N) -> Vec2<N>
{ Vec2 { x: self.x / *s, y: self.y / *s } }
fn scalar_div_inplace(&mut self, s: &T)
fn scalar_div_inplace(&mut self, s: &N)
{
self.x /= *s;
self.y /= *s;
}
}
impl<T: Copy + Add<T, T>>
ScalarAdd<T> for Vec2<T>
impl<N: Copy + Add<N, N>>
ScalarAdd<N> for Vec2<N>
{
fn scalar_add(&self, s: &T) -> Vec2<T>
fn scalar_add(&self, s: &N) -> Vec2<N>
{ Vec2 { x: self.x + *s, y: self.y + *s } }
fn scalar_add_inplace(&mut self, s: &T)
fn scalar_add_inplace(&mut self, s: &N)
{
self.x += *s;
self.y += *s;
}
}
impl<T: Copy + Sub<T, T>>
ScalarSub<T> for Vec2<T>
impl<N: Copy + Sub<N, N>>
ScalarSub<N> for Vec2<N>
{
fn scalar_sub(&self, s: &T) -> Vec2<T>
fn scalar_sub(&self, s: &N) -> Vec2<N>
{ Vec2 { x: self.x - *s, y: self.y - *s } }
fn scalar_sub_inplace(&mut self, s: &T)
fn scalar_sub_inplace(&mut self, s: &N)
{
self.x -= *s;
self.y -= *s;
}
}
impl<T: Copy + Add<T, T>> Translation<Vec2<T>> for Vec2<T>
impl<N: Copy + Add<N, N>> Translation<Vec2<N>> for Vec2<N>
{
fn translation(&self) -> Vec2<T>
fn translation(&self) -> Vec2<N>
{ *self }
fn translated(&self, t: &Vec2<T>) -> Vec2<T>
fn translated(&self, t: &Vec2<N>) -> Vec2<N>
{ self + *t }
fn translate(&mut self, t: &Vec2<T>)
fn translate(&mut self, t: &Vec2<N>)
{ *self += *t; }
}
impl<T:Copy + Mul<T, T> + Add<T, T>> Dot<T> for Vec2<T>
impl<N:Copy + Mul<N, N> + Add<N, N>> Dot<N> for Vec2<N>
{
fn dot(&self, other : &Vec2<T>) -> T
fn dot(&self, other : &Vec2<N>) -> N
{ self.x * other.x + self.y * other.y }
}
impl<T:Copy + Mul<T, T> + Add<T, T> + Sub<T, T>> SubDot<T> for Vec2<T>
impl<N:Copy + Mul<N, N> + Add<N, N> + Sub<N, N>> SubDot<N> for Vec2<N>
{
fn sub_dot(&self, a: &Vec2<T>, b: &Vec2<T>) -> T
fn sub_dot(&self, a: &Vec2<N>, b: &Vec2<N>) -> N
{ (self.x - a.x) * b.x + (self.y - a.y) * b.y }
}
impl<T:Copy + Mul<T, T> + Add<T, T> + Div<T, T> + Algebraic>
Norm<T> for Vec2<T>
impl<N:Copy + Mul<N, N> + Add<N, N> + Div<N, N> + Algebraic>
Norm<N> for Vec2<N>
{
fn sqnorm(&self) -> T
fn sqnorm(&self) -> N
{ self.dot(self) }
fn norm(&self) -> T
fn norm(&self) -> N
{ self.sqnorm().sqrt() }
fn normalized(&self) -> Vec2<T>
fn normalized(&self) -> Vec2<N>
{
let l = self.norm();
Vec2::new(self.x / l, self.y / l)
}
fn normalize(&mut self) -> T
fn normalize(&mut self) -> N
{
let l = self.norm();
@ -146,21 +146,21 @@ Norm<T> for Vec2<T>
}
}
impl<T:Copy + Mul<T, T> + Sub<T, T>> Cross<Vec1<T>> for Vec2<T>
impl<N:Copy + Mul<N, N> + Sub<N, N>> Cross<Vec1<N>> for Vec2<N>
{
fn cross(&self, other : &Vec2<T>) -> Vec1<T>
fn cross(&self, other : &Vec2<N>) -> Vec1<N>
{ Vec1::new(self.x * other.y - self.y * other.x) }
}
impl<T:Copy + Neg<T>> Neg<Vec2<T>> for Vec2<T>
impl<N:Copy + Neg<N>> Neg<Vec2<N>> for Vec2<N>
{
fn neg(&self) -> Vec2<T>
fn neg(&self) -> Vec2<N>
{ Vec2::new(-self.x, -self.y) }
}
impl<T:Copy + Zero> Zero for Vec2<T>
impl<N:Copy + Zero> Zero for Vec2<N>
{
fn zero() -> Vec2<T>
fn zero() -> Vec2<N>
{
let _0 = Zero::zero();
Vec2::new(_0, _0)
@ -170,36 +170,36 @@ impl<T:Copy + Zero> Zero for Vec2<T>
{ self.x.is_zero() && self.y.is_zero() }
}
impl<T: Copy + One + Zero + Neg<T>> Basis for Vec2<T>
impl<N: Copy + One + Zero + Neg<N>> Basis for Vec2<N>
{
fn canonical_basis() -> ~[Vec2<T>]
fn canonical_basis() -> ~[Vec2<N>]
{
// FIXME: this should be static
~[ Vec2::new(One::one(), Zero::zero()),
Vec2::new(Zero::zero(), One::one()) ]
}
fn orthogonal_subspace_basis(&self) -> ~[Vec2<T>]
fn orthogonal_subspace_basis(&self) -> ~[Vec2<N>]
{ ~[ Vec2::new(-self.y, self.x) ] }
}
impl<T:ApproxEq<T>> ApproxEq<T> for Vec2<T>
impl<N:ApproxEq<N>> ApproxEq<N> for Vec2<N>
{
fn approx_epsilon() -> T
{ ApproxEq::approx_epsilon::<T, T>() }
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
fn approx_eq(&self, other: &Vec2<T>) -> bool
fn approx_eq(&self, other: &Vec2<N>) -> bool
{ self.x.approx_eq(&other.x) && self.y.approx_eq(&other.y) }
fn approx_eq_eps(&self, other: &Vec2<T>, epsilon: &T) -> bool
fn approx_eq_eps(&self, other: &Vec2<N>, epsilon: &N) -> bool
{
self.x.approx_eq_eps(&other.x, epsilon) &&
self.y.approx_eq_eps(&other.y, epsilon)
}
}
impl<T:Rand + Copy> Rand for Vec2<T>
impl<N:Rand + Copy> Rand for Vec2<N>
{
fn rand<R: Rng>(rng: &mut R) -> Vec2<T>
fn rand<R: Rng>(rng: &mut R) -> Vec2<N>
{ Vec2::new(rng.gen(), rng.gen()) }
}

View File

@ -9,18 +9,18 @@ use traits::workarounds::rlmul::{RMul, LMul};
use dim3::vec3::Vec3;
#[deriving(Eq, ToStr)]
pub struct Mat3<T>
pub struct Mat3<N>
{
m11: T, m12: T, m13: T,
m21: T, m22: T, m23: T,
m31: T, m32: T, m33: T
m11: N, m12: N, m13: N,
m21: N, m22: N, m23: N,
m31: N, m32: N, m33: N
}
impl<T: Copy> Mat3<T>
impl<N: Copy> Mat3<N>
{
pub fn new(m11: T, m12: T, m13: T,
m21: T, m22: T, m23: T,
m31: T, m32: T, m33: T) -> Mat3<T>
pub fn new(m11: N, m12: N, m13: N,
m21: N, m22: N, m23: N,
m31: N, m32: N, m33: N) -> Mat3<N>
{
Mat3
{
@ -31,15 +31,15 @@ impl<T: Copy> Mat3<T>
}
}
impl<T> Dim for Mat3<T>
impl<N> Dim for Mat3<N>
{
fn dim() -> uint
{ 3 }
}
impl<T:Copy + One + Zero> One for Mat3<T>
impl<N:Copy + One + Zero> One for Mat3<N>
{
fn one() -> Mat3<T>
fn one() -> Mat3<N>
{
let (_0, _1) = (Zero::zero(), One::one());
return Mat3::new(_1, _0, _0,
@ -48,9 +48,9 @@ impl<T:Copy + One + Zero> One for Mat3<T>
}
}
impl<T:Copy + Zero> Zero for Mat3<T>
impl<N:Copy + Zero> Zero for Mat3<N>
{
fn zero() -> Mat3<T>
fn zero() -> Mat3<N>
{
let _0 = Zero::zero();
return Mat3::new(_0, _0, _0,
@ -66,9 +66,9 @@ impl<T:Copy + Zero> Zero for Mat3<T>
}
}
impl<T:Copy + Mul<T, T> + Add<T, T>> Mul<Mat3<T>, Mat3<T>> for Mat3<T>
impl<N:Copy + Mul<N, N> + Add<N, N>> Mul<Mat3<N>, Mat3<N>> for Mat3<N>
{
fn mul(&self, other: &Mat3<T>) -> Mat3<T>
fn mul(&self, other: &Mat3<N>) -> Mat3<N>
{
Mat3::new(
self.m11 * other.m11 + self.m12 * other.m21 + self.m13 * other.m31,
@ -86,9 +86,9 @@ impl<T:Copy + Mul<T, T> + Add<T, T>> Mul<Mat3<T>, Mat3<T>> for Mat3<T>
}
}
impl<T:Copy + Add<T, T> + Mul<T, T>> RMul<Vec3<T>> for Mat3<T>
impl<N:Copy + Add<N, N> + Mul<N, N>> RMul<Vec3<N>> for Mat3<N>
{
fn rmul(&self, other: &Vec3<T>) -> Vec3<T>
fn rmul(&self, other: &Vec3<N>) -> Vec3<N>
{
Vec3::new(
self.m11 * other.x + self.m12 * other.y + self.m13 * other.z,
@ -98,9 +98,9 @@ impl<T:Copy + Add<T, T> + Mul<T, T>> RMul<Vec3<T>> for Mat3<T>
}
}
impl<T:Copy + Add<T, T> + Mul<T, T>> LMul<Vec3<T>> for Mat3<T>
impl<N:Copy + Add<N, N> + Mul<N, N>> LMul<Vec3<N>> for Mat3<N>
{
fn lmul(&self, other: &Vec3<T>) -> Vec3<T>
fn lmul(&self, other: &Vec3<N>) -> Vec3<N>
{
Vec3::new(
self.m11 * other.x + self.m21 * other.y + self.m31 * other.z,
@ -110,10 +110,10 @@ impl<T:Copy + Add<T, T> + Mul<T, T>> LMul<Vec3<T>> for Mat3<T>
}
}
impl<T:Copy + Mul<T, T> + Div<T, T> + Sub<T, T> + Add<T, T> + Neg<T> + Zero>
Inv for Mat3<T>
impl<N:Copy + Mul<N, N> + Div<N, N> + Sub<N, N> + Add<N, N> + Neg<N> + Zero>
Inv for Mat3<N>
{
fn inverse(&self) -> Mat3<T>
fn inverse(&self) -> Mat3<N>
{
let mut res = *self;
@ -150,9 +150,9 @@ Inv for Mat3<T>
}
}
impl<T:Copy> Transpose for Mat3<T>
impl<N:Copy> Transpose for Mat3<N>
{
fn transposed(&self) -> Mat3<T>
fn transposed(&self) -> Mat3<N>
{
Mat3::new(self.m11, self.m21, self.m31,
self.m12, self.m22, self.m32,
@ -167,12 +167,12 @@ impl<T:Copy> Transpose for Mat3<T>
}
}
impl<T:ApproxEq<T>> ApproxEq<T> for Mat3<T>
impl<N:ApproxEq<N>> ApproxEq<N> for Mat3<N>
{
fn approx_epsilon() -> T
{ ApproxEq::approx_epsilon::<T, T>() }
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
fn approx_eq(&self, other: &Mat3<T>) -> bool
fn approx_eq(&self, other: &Mat3<N>) -> bool
{
self.m11.approx_eq(&other.m11) &&
self.m12.approx_eq(&other.m12) &&
@ -187,7 +187,7 @@ impl<T:ApproxEq<T>> ApproxEq<T> for Mat3<T>
self.m33.approx_eq(&other.m33)
}
fn approx_eq_eps(&self, other: &Mat3<T>, epsilon: &T) -> bool
fn approx_eq_eps(&self, other: &Mat3<N>, epsilon: &N) -> bool
{
self.m11.approx_eq_eps(&other.m11, epsilon) &&
self.m12.approx_eq_eps(&other.m12, epsilon) &&
@ -203,9 +203,9 @@ impl<T:ApproxEq<T>> ApproxEq<T> for Mat3<T>
}
}
impl<T:Rand + Copy> Rand for Mat3<T>
impl<N:Rand + Copy> Rand for Mat3<N>
{
fn rand<R: Rng>(rng: &mut R) -> Mat3<T>
fn rand<R: Rng>(rng: &mut R) -> Mat3<N>
{
Mat3::new(rng.gen(), rng.gen(), rng.gen(),
rng.gen(), rng.gen(), rng.gen(),

View File

@ -11,44 +11,44 @@ use traits::translation::Translation;
use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
#[deriving(Eq, ToStr)]
pub struct Vec3<T>
pub struct Vec3<N>
{
x : T,
y : T,
z : T
x : N,
y : N,
z : N
}
impl<T: Copy> Vec3<T>
impl<N: Copy> Vec3<N>
{
pub fn new(x: T, y: T, z: T) -> Vec3<T>
pub fn new(x: N, y: N, z: N) -> Vec3<N>
{ Vec3 {x: x, y: y, z: z} }
}
impl<T> Dim for Vec3<T>
impl<N> Dim for Vec3<N>
{
fn dim() -> uint
{ 3 }
}
impl<T:Copy + Add<T,T>> Add<Vec3<T>, Vec3<T>> for Vec3<T>
impl<N:Copy + Add<N,N>> Add<Vec3<N>, Vec3<N>> for Vec3<N>
{
fn add(&self, other: &Vec3<T>) -> Vec3<T>
fn add(&self, other: &Vec3<N>) -> Vec3<N>
{ Vec3::new(self.x + other.x, self.y + other.y, self.z + other.z) }
}
impl<T:Copy + Sub<T,T>> Sub<Vec3<T>, Vec3<T>> for Vec3<T>
impl<N:Copy + Sub<N,N>> Sub<Vec3<N>, Vec3<N>> for Vec3<N>
{
fn sub(&self, other: &Vec3<T>) -> Vec3<T>
fn sub(&self, other: &Vec3<N>) -> Vec3<N>
{ Vec3::new(self.x - other.x, self.y - other.y, self.z - other.z) }
}
impl<T: Copy + Mul<T, T>>
ScalarMul<T> for Vec3<T>
impl<N: Copy + Mul<N, N>>
ScalarMul<N> for Vec3<N>
{
fn scalar_mul(&self, s: &T) -> Vec3<T>
fn scalar_mul(&self, s: &N) -> Vec3<N>
{ Vec3 { x: self.x * *s, y: self.y * *s, z: self.z * *s } }
fn scalar_mul_inplace(&mut self, s: &T)
fn scalar_mul_inplace(&mut self, s: &N)
{
self.x *= *s;
self.y *= *s;
@ -57,13 +57,13 @@ ScalarMul<T> for Vec3<T>
}
impl<T: Copy + Div<T, T>>
ScalarDiv<T> for Vec3<T>
impl<N: Copy + Div<N, N>>
ScalarDiv<N> for Vec3<N>
{
fn scalar_div(&self, s: &T) -> Vec3<T>
fn scalar_div(&self, s: &N) -> Vec3<N>
{ Vec3 { x: self.x / *s, y: self.y / *s, z: self.z / *s } }
fn scalar_div_inplace(&mut self, s: &T)
fn scalar_div_inplace(&mut self, s: &N)
{
self.x /= *s;
self.y /= *s;
@ -71,13 +71,13 @@ ScalarDiv<T> for Vec3<T>
}
}
impl<T: Copy + Add<T, T>>
ScalarAdd<T> for Vec3<T>
impl<N: Copy + Add<N, N>>
ScalarAdd<N> for Vec3<N>
{
fn scalar_add(&self, s: &T) -> Vec3<T>
fn scalar_add(&self, s: &N) -> Vec3<N>
{ Vec3 { x: self.x + *s, y: self.y + *s, z: self.z + *s } }
fn scalar_add_inplace(&mut self, s: &T)
fn scalar_add_inplace(&mut self, s: &N)
{
self.x += *s;
self.y += *s;
@ -85,13 +85,13 @@ ScalarAdd<T> for Vec3<T>
}
}
impl<T: Copy + Sub<T, T>>
ScalarSub<T> for Vec3<T>
impl<N: Copy + Sub<N, N>>
ScalarSub<N> for Vec3<N>
{
fn scalar_sub(&self, s: &T) -> Vec3<T>
fn scalar_sub(&self, s: &N) -> Vec3<N>
{ Vec3 { x: self.x - *s, y: self.y - *s, z: self.z - *s } }
fn scalar_sub_inplace(&mut self, s: &T)
fn scalar_sub_inplace(&mut self, s: &N)
{
self.x -= *s;
self.y -= *s;
@ -99,55 +99,55 @@ ScalarSub<T> for Vec3<T>
}
}
impl<T: Copy + Add<T, T>> Translation<Vec3<T>> for Vec3<T>
impl<N: Copy + Add<N, N>> Translation<Vec3<N>> for Vec3<N>
{
fn translation(&self) -> Vec3<T>
fn translation(&self) -> Vec3<N>
{ *self }
fn translated(&self, t: &Vec3<T>) -> Vec3<T>
fn translated(&self, t: &Vec3<N>) -> Vec3<N>
{ self + *t }
fn translate(&mut self, t: &Vec3<T>)
fn translate(&mut self, t: &Vec3<N>)
{ *self += *t; }
}
impl<T:Copy + Neg<T>> Neg<Vec3<T>> for Vec3<T>
impl<N:Copy + Neg<N>> Neg<Vec3<N>> for Vec3<N>
{
fn neg(&self) -> Vec3<T>
fn neg(&self) -> Vec3<N>
{ Vec3::new(-self.x, -self.y, -self.z) }
}
impl<T:Copy + Mul<T, T> + Add<T, T>> Dot<T> for Vec3<T>
impl<N:Copy + Mul<N, N> + Add<N, N>> Dot<N> for Vec3<N>
{
fn dot(&self, other : &Vec3<T>) -> T
fn dot(&self, other : &Vec3<N>) -> N
{ self.x * other.x + self.y * other.y + self.z * other.z }
}
impl<T:Copy + Mul<T, T> + Add<T, T> + Sub<T, T>> SubDot<T> for Vec3<T>
impl<N:Copy + Mul<N, N> + Add<N, N> + Sub<N, N>> SubDot<N> for Vec3<N>
{
fn sub_dot(&self, a: &Vec3<T>, b: &Vec3<T>) -> T
fn sub_dot(&self, a: &Vec3<N>, b: &Vec3<N>) -> N
{ (self.x - a.x) * b.x + (self.y - a.y) * b.y + (self.z - a.z) * b.z }
}
impl<T:Copy + Mul<T, T> + Add<T, T> + Div<T, T> + Algebraic>
Norm<T> for Vec3<T>
impl<N:Copy + Mul<N, N> + Add<N, N> + Div<N, N> + Algebraic>
Norm<N> for Vec3<N>
{
fn sqnorm(&self) -> T
fn sqnorm(&self) -> N
{ self.dot(self) }
fn norm(&self) -> T
fn norm(&self) -> N
{ self.sqnorm().sqrt() }
fn normalized(&self) -> Vec3<T>
fn normalized(&self) -> Vec3<N>
{
let l = self.norm();
Vec3::new(self.x / l, self.y / l, self.z / l)
}
fn normalize(&mut self) -> T
fn normalize(&mut self) -> N
{
let l = self.norm();
@ -159,9 +159,9 @@ Norm<T> for Vec3<T>
}
}
impl<T:Copy + Mul<T, T> + Sub<T, T>> Cross<Vec3<T>> for Vec3<T>
impl<N:Copy + Mul<N, N> + Sub<N, N>> Cross<Vec3<N>> for Vec3<N>
{
fn cross(&self, other : &Vec3<T>) -> Vec3<T>
fn cross(&self, other : &Vec3<N>) -> Vec3<N>
{
Vec3::new(
self.y * other.z - self.z * other.y,
@ -171,9 +171,9 @@ impl<T:Copy + Mul<T, T> + Sub<T, T>> Cross<Vec3<T>> for Vec3<T>
}
}
impl<T:Copy + Zero> Zero for Vec3<T>
impl<N:Copy + Zero> Zero for Vec3<N>
{
fn zero() -> Vec3<T>
fn zero() -> Vec3<N>
{
let _0 = Zero::zero();
Vec3::new(_0, _0, _0)
@ -183,11 +183,11 @@ impl<T:Copy + Zero> Zero for Vec3<T>
{ self.x.is_zero() && self.y.is_zero() && self.z.is_zero() }
}
impl<T: Copy + One + Zero + Neg<T> + Ord + Mul<T, T> + Sub<T, T> + Add<T, T> +
Div<T, T> + Algebraic>
Basis for Vec3<T>
impl<N: Copy + One + Zero + Neg<N> + Ord + Mul<N, N> + Sub<N, N> + Add<N, N> +
Div<N, N> + Algebraic>
Basis for Vec3<N>
{
fn canonical_basis() -> ~[Vec3<T>]
fn canonical_basis() -> ~[Vec3<N>]
{
// FIXME: this should be static
~[ Vec3::new(One::one(), Zero::zero(), Zero::zero()),
@ -195,7 +195,7 @@ Basis for Vec3<T>
Vec3::new(Zero::zero(), Zero::zero(), One::one()) ]
}
fn orthogonal_subspace_basis(&self) -> ~[Vec3<T>]
fn orthogonal_subspace_basis(&self) -> ~[Vec3<N>]
{
let a =
if (abs(self.x) > abs(self.y))
@ -207,19 +207,19 @@ Basis for Vec3<T>
}
}
impl<T:ApproxEq<T>> ApproxEq<T> for Vec3<T>
impl<N:ApproxEq<N>> ApproxEq<N> for Vec3<N>
{
fn approx_epsilon() -> T
{ ApproxEq::approx_epsilon::<T, T>() }
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
fn approx_eq(&self, other: &Vec3<T>) -> bool
fn approx_eq(&self, other: &Vec3<N>) -> bool
{
self.x.approx_eq(&other.x) &&
self.y.approx_eq(&other.y) &&
self.z.approx_eq(&other.z)
}
fn approx_eq_eps(&self, other: &Vec3<T>, epsilon: &T) -> bool
fn approx_eq_eps(&self, other: &Vec3<N>, epsilon: &N) -> bool
{
self.x.approx_eq_eps(&other.x, epsilon) &&
self.y.approx_eq_eps(&other.y, epsilon) &&
@ -227,8 +227,8 @@ impl<T:ApproxEq<T>> ApproxEq<T> for Vec3<T>
}
}
impl<T:Copy + Rand> Rand for Vec3<T>
impl<N:Copy + Rand> Rand for Vec3<N>
{
fn rand<R: Rng>(rng: &mut R) -> Vec3<T>
fn rand<R: Rng>(rng: &mut R) -> Vec3<N>
{ Vec3::new(rng.gen(), rng.gen(), rng.gen()) }
}

View File

@ -10,22 +10,22 @@ use traits::workarounds::rlmul::{RMul, LMul};
use ndim::dvec::{DVec, zero_vec_with_dim};
#[deriving(Eq, ToStr, Clone)]
pub struct DMat<T>
pub struct DMat<N>
{
dim: uint, // FIXME: handle more than just square matrices
mij: ~[T]
mij: ~[N]
}
pub fn zero_mat_with_dim<T: Zero + Copy>(dim: uint) -> DMat<T>
pub fn zero_mat_with_dim<N: Zero + Copy>(dim: uint) -> DMat<N>
{ DMat { dim: dim, mij: from_elem(dim * dim, Zero::zero()) } }
pub fn is_zero_mat<T: Zero>(mat: &DMat<T>) -> bool
pub fn is_zero_mat<N: Zero>(mat: &DMat<N>) -> bool
{ mat.mij.all(|e| e.is_zero()) }
pub fn one_mat_with_dim<T: Copy + One + Zero>(dim: uint) -> DMat<T>
pub fn one_mat_with_dim<N: Copy + One + Zero>(dim: uint) -> DMat<N>
{
let mut res = zero_mat_with_dim(dim);
let _1 = One::one::<T>();
let _1 = One::one::<N>();
for iterate(0u, dim) |i|
{ res.set(i, i, &_1); }
@ -33,19 +33,19 @@ pub fn one_mat_with_dim<T: Copy + One + Zero>(dim: uint) -> DMat<T>
res
}
impl<T: Copy> DMat<T>
impl<N: Copy> DMat<N>
{
pub fn offset(&self, i: uint, j: uint) -> uint
{ i * self.dim + j }
pub fn set(&mut self, i: uint, j: uint, t: &T)
pub fn set(&mut self, i: uint, j: uint, t: &N)
{
assert!(i < self.dim);
assert!(j < self.dim);
self.mij[self.offset(i, j)] = *t
}
pub fn at(&self, i: uint, j: uint) -> T
pub fn at(&self, i: uint, j: uint) -> N
{
assert!(i < self.dim);
assert!(j < self.dim);
@ -53,16 +53,16 @@ impl<T: Copy> DMat<T>
}
}
impl<T: Copy> Index<(uint, uint), T> for DMat<T>
impl<N: Copy> Index<(uint, uint), N> for DMat<N>
{
fn index(&self, &(i, j): &(uint, uint)) -> T
fn index(&self, &(i, j): &(uint, uint)) -> N
{ self.at(i, j) }
}
impl<T: Copy + Mul<T, T> + Add<T, T> + Zero>
Mul<DMat<T>, DMat<T>> for DMat<T>
impl<N: Copy + Mul<N, N> + Add<N, N> + Zero>
Mul<DMat<N>, DMat<N>> for DMat<N>
{
fn mul(&self, other: &DMat<T>) -> DMat<T>
fn mul(&self, other: &DMat<N>) -> DMat<N>
{
assert!(self.dim == other.dim);
@ -73,7 +73,7 @@ Mul<DMat<T>, DMat<T>> for DMat<T>
{
for iterate(0u, dim) |j|
{
let mut acc = Zero::zero::<T>();
let mut acc = Zero::zero::<N>();
for iterate(0u, dim) |k|
{ acc += self.at(i, k) * other.at(k, j); }
@ -86,15 +86,15 @@ Mul<DMat<T>, DMat<T>> for DMat<T>
}
}
impl<T: Copy + Add<T, T> + Mul<T, T> + Zero>
RMul<DVec<T>> for DMat<T>
impl<N: Copy + Add<N, N> + Mul<N, N> + Zero>
RMul<DVec<N>> for DMat<N>
{
fn rmul(&self, other: &DVec<T>) -> DVec<T>
fn rmul(&self, other: &DVec<N>) -> DVec<N>
{
assert!(self.dim == other.at.len());
let dim = self.dim;
let mut res : DVec<T> = zero_vec_with_dim(dim);
let mut res : DVec<N> = zero_vec_with_dim(dim);
for iterate(0u, dim) |i|
{
@ -106,15 +106,15 @@ RMul<DVec<T>> for DMat<T>
}
}
impl<T: Copy + Add<T, T> + Mul<T, T> + Zero>
LMul<DVec<T>> for DMat<T>
impl<N: Copy + Add<N, N> + Mul<N, N> + Zero>
LMul<DVec<N>> for DMat<N>
{
fn lmul(&self, other: &DVec<T>) -> DVec<T>
fn lmul(&self, other: &DVec<N>) -> DVec<N>
{
assert!(self.dim == other.at.len());
let dim = self.dim;
let mut res : DVec<T> = zero_vec_with_dim(dim);
let mut res : DVec<N> = zero_vec_with_dim(dim);
for iterate(0u, dim) |i|
{
@ -126,12 +126,12 @@ LMul<DVec<T>> for DMat<T>
}
}
impl<T: Clone + Copy + Eq + DivisionRing>
Inv for DMat<T>
impl<N: Clone + Copy + Eq + DivisionRing>
Inv for DMat<N>
{
fn inverse(&self) -> DMat<T>
fn inverse(&self) -> DMat<N>
{
let mut res : DMat<T> = self.clone();
let mut res : DMat<N> = self.clone();
res.invert();
@ -141,8 +141,8 @@ Inv for DMat<T>
fn invert(&mut self)
{
let dim = self.dim;
let mut res = one_mat_with_dim::<T>(dim);
let _0T = Zero::zero::<T>();
let mut res = one_mat_with_dim::<N>(dim);
let _0T = Zero::zero::<N>();
// inversion using Gauss-Jordan elimination
for iterate(0u, dim) |k|
@ -215,9 +215,9 @@ Inv for DMat<T>
}
}
impl<T:Copy> Transpose for DMat<T>
impl<N:Copy> Transpose for DMat<N>
{
fn transposed(&self) -> DMat<T>
fn transposed(&self) -> DMat<N>
{
let mut res = copy *self;
@ -243,19 +243,19 @@ impl<T:Copy> Transpose for DMat<T>
}
}
impl<T: ApproxEq<T>> ApproxEq<T> for DMat<T>
impl<N: ApproxEq<N>> ApproxEq<N> for DMat<N>
{
fn approx_epsilon() -> T
{ ApproxEq::approx_epsilon::<T, T>() }
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
fn approx_eq(&self, other: &DMat<T>) -> bool
fn approx_eq(&self, other: &DMat<N>) -> bool
{
let mut zip = self.mij.iter().zip(other.mij.iter());
do zip.all |(a, b)| { a.approx_eq(b) }
}
fn approx_eq_eps(&self, other: &DMat<T>, epsilon: &T) -> bool
fn approx_eq_eps(&self, other: &DMat<N>, epsilon: &N) -> bool
{
let mut zip = self.mij.iter().zip(other.mij.iter());

View File

@ -12,27 +12,27 @@ use traits::translation::Translation;
use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
#[deriving(Eq, ToStr, Clone)]
pub struct DVec<T>
pub struct DVec<N>
{
at: ~[T]
at: ~[N]
}
pub fn zero_vec_with_dim<T: Zero + Copy>(dim: uint) -> DVec<T>
{ DVec { at: from_elem(dim, Zero::zero::<T>()) } }
pub fn zero_vec_with_dim<N: Zero + Copy>(dim: uint) -> DVec<N>
{ DVec { at: from_elem(dim, Zero::zero::<N>()) } }
pub fn is_zero_vec<T: Zero>(vec: &DVec<T>) -> bool
pub fn is_zero_vec<N: Zero>(vec: &DVec<N>) -> bool
{ vec.at.all(|e| e.is_zero()) }
// FIXME: is Clone needed?
impl<T: Copy + DivisionRing + Algebraic + Clone + ApproxEq<T>> DVec<T>
impl<N: Copy + DivisionRing + Algebraic + Clone + ApproxEq<N>> DVec<N>
{
pub fn canonical_basis_with_dim(dim: uint) -> ~[DVec<T>]
pub fn canonical_basis_with_dim(dim: uint) -> ~[DVec<N>]
{
let mut res : ~[DVec<T>] = ~[];
let mut res : ~[DVec<N>] = ~[];
for iterate(0u, dim) |i|
{
let mut basis_element : DVec<T> = zero_vec_with_dim(dim);
let mut basis_element : DVec<N> = zero_vec_with_dim(dim);
basis_element.at[i] = One::one();
@ -42,16 +42,16 @@ impl<T: Copy + DivisionRing + Algebraic + Clone + ApproxEq<T>> DVec<T>
res
}
pub fn orthogonal_subspace_basis(&self) -> ~[DVec<T>]
pub fn orthogonal_subspace_basis(&self) -> ~[DVec<N>]
{
// compute the basis of the orthogonal subspace using Gram-Schmidt
// orthogonalization algorithm
let dim = self.at.len();
let mut res : ~[DVec<T>] = ~[];
let mut res : ~[DVec<N>] = ~[];
for iterate(0u, dim) |i|
{
let mut basis_element : DVec<T> = zero_vec_with_dim(self.at.len());
let mut basis_element : DVec<N> = zero_vec_with_dim(self.at.len());
basis_element.at[i] = One::one();
@ -75,38 +75,38 @@ impl<T: Copy + DivisionRing + Algebraic + Clone + ApproxEq<T>> DVec<T>
}
}
impl<T: Copy + Add<T,T>> Add<DVec<T>, DVec<T>> for DVec<T>
impl<N: Copy + Add<N,N>> Add<DVec<N>, DVec<N>> for DVec<N>
{
fn add(&self, other: &DVec<T>) -> DVec<T>
fn add(&self, other: &DVec<N>) -> DVec<N>
{
assert!(self.at.len() == other.at.len());
DVec { at: map_zip(self.at, other.at, | a, b | { *a + *b }) }
}
}
impl<T: Copy + Sub<T,T>> Sub<DVec<T>, DVec<T>> for DVec<T>
impl<N: Copy + Sub<N,N>> Sub<DVec<N>, DVec<N>> for DVec<N>
{
fn sub(&self, other: &DVec<T>) -> DVec<T>
fn sub(&self, other: &DVec<N>) -> DVec<N>
{
assert!(self.at.len() == other.at.len());
DVec { at: map_zip(self.at, other.at, | a, b | *a - *b) }
}
}
impl<T: Copy + Neg<T>> Neg<DVec<T>> for DVec<T>
impl<N: Copy + Neg<N>> Neg<DVec<N>> for DVec<N>
{
fn neg(&self) -> DVec<T>
fn neg(&self) -> DVec<N>
{ DVec { at: map(self.at, |a| -a) } }
}
impl<T: Copy + Ring>
Dot<T> for DVec<T>
impl<N: Copy + Ring>
Dot<N> for DVec<N>
{
fn dot(&self, other: &DVec<T>) -> T
fn dot(&self, other: &DVec<N>) -> N
{
assert!(self.at.len() == other.at.len());
let mut res = Zero::zero::<T>();
let mut res = Zero::zero::<N>();
for iterate(0u, self.at.len()) |i|
{ res += self.at[i] * other.at[i]; }
@ -115,11 +115,11 @@ Dot<T> for DVec<T>
}
}
impl<T: Copy + Ring> SubDot<T> for DVec<T>
impl<N: Copy + Ring> SubDot<N> for DVec<N>
{
fn sub_dot(&self, a: &DVec<T>, b: &DVec<T>) -> T
fn sub_dot(&self, a: &DVec<N>, b: &DVec<N>) -> N
{
let mut res = Zero::zero::<T>();
let mut res = Zero::zero::<N>();
for iterate(0u, self.at.len()) |i|
{ res += (self.at[i] - a.at[i]) * b.at[i]; }
@ -128,13 +128,13 @@ impl<T: Copy + Ring> SubDot<T> for DVec<T>
}
}
impl<T: Copy + Mul<T, T>>
ScalarMul<T> for DVec<T>
impl<N: Copy + Mul<N, N>>
ScalarMul<N> for DVec<N>
{
fn scalar_mul(&self, s: &T) -> DVec<T>
fn scalar_mul(&self, s: &N) -> DVec<N>
{ DVec { at: map(self.at, |a| a * *s) } }
fn scalar_mul_inplace(&mut self, s: &T)
fn scalar_mul_inplace(&mut self, s: &N)
{
for iterate(0u, self.at.len()) |i|
{ self.at[i] *= *s; }
@ -142,76 +142,76 @@ ScalarMul<T> for DVec<T>
}
impl<T: Copy + Div<T, T>>
ScalarDiv<T> for DVec<T>
impl<N: Copy + Div<N, N>>
ScalarDiv<N> for DVec<N>
{
fn scalar_div(&self, s: &T) -> DVec<T>
fn scalar_div(&self, s: &N) -> DVec<N>
{ DVec { at: map(self.at, |a| a / *s) } }
fn scalar_div_inplace(&mut self, s: &T)
fn scalar_div_inplace(&mut self, s: &N)
{
for iterate(0u, self.at.len()) |i|
{ self.at[i] /= *s; }
}
}
impl<T: Copy + Add<T, T>>
ScalarAdd<T> for DVec<T>
impl<N: Copy + Add<N, N>>
ScalarAdd<N> for DVec<N>
{
fn scalar_add(&self, s: &T) -> DVec<T>
fn scalar_add(&self, s: &N) -> DVec<N>
{ DVec { at: map(self.at, |a| a + *s) } }
fn scalar_add_inplace(&mut self, s: &T)
fn scalar_add_inplace(&mut self, s: &N)
{
for iterate(0u, self.at.len()) |i|
{ self.at[i] += *s; }
}
}
impl<T: Copy + Sub<T, T>>
ScalarSub<T> for DVec<T>
impl<N: Copy + Sub<N, N>>
ScalarSub<N> for DVec<N>
{
fn scalar_sub(&self, s: &T) -> DVec<T>
fn scalar_sub(&self, s: &N) -> DVec<N>
{ DVec { at: map(self.at, |a| a - *s) } }
fn scalar_sub_inplace(&mut self, s: &T)
fn scalar_sub_inplace(&mut self, s: &N)
{
for iterate(0u, self.at.len()) |i|
{ self.at[i] -= *s; }
}
}
impl<T: Clone + Copy + Add<T, T>> Translation<DVec<T>> for DVec<T>
impl<N: Clone + Copy + Add<N, N>> Translation<DVec<N>> for DVec<N>
{
fn translation(&self) -> DVec<T>
fn translation(&self) -> DVec<N>
{ self.clone() }
fn translated(&self, t: &DVec<T>) -> DVec<T>
fn translated(&self, t: &DVec<N>) -> DVec<N>
{ self + *t }
fn translate(&mut self, t: &DVec<T>)
fn translate(&mut self, t: &DVec<N>)
{ *self = *self + *t; }
}
impl<T: Copy + DivisionRing + Algebraic + Clone>
Norm<T> for DVec<T>
impl<N: Copy + DivisionRing + Algebraic + Clone>
Norm<N> for DVec<N>
{
fn sqnorm(&self) -> T
fn sqnorm(&self) -> N
{ self.dot(self) }
fn norm(&self) -> T
fn norm(&self) -> N
{ self.sqnorm().sqrt() }
fn normalized(&self) -> DVec<T>
fn normalized(&self) -> DVec<N>
{
let mut res : DVec<T> = self.clone();
let mut res : DVec<N> = self.clone();
res.normalize();
res
}
fn normalize(&mut self) -> T
fn normalize(&mut self) -> N
{
let l = self.norm();
@ -222,19 +222,19 @@ Norm<T> for DVec<T>
}
}
impl<T: ApproxEq<T>> ApproxEq<T> for DVec<T>
impl<N: ApproxEq<N>> ApproxEq<N> for DVec<N>
{
fn approx_epsilon() -> T
{ ApproxEq::approx_epsilon::<T, T>() }
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
fn approx_eq(&self, other: &DVec<T>) -> bool
fn approx_eq(&self, other: &DVec<N>) -> bool
{
let mut zip = self.at.iter().zip(other.at.iter());
do zip.all |(a, b)| { a.approx_eq(b) }
}
fn approx_eq_eps(&self, other: &DVec<T>, epsilon: &T) -> bool
fn approx_eq_eps(&self, other: &DVec<N>, epsilon: &N) -> bool
{
let mut zip = self.at.iter().zip(other.at.iter());

View File

@ -15,58 +15,58 @@ use ndim::nvec::NVec;
// It can be anything implementing the Dim trait. However, to avoid confusion,
// using d0, d1, d2, d3 and d4 tokens are prefered.
#[deriving(Eq, ToStr)]
pub struct NMat<D, T>
{ mij: DMat<T> }
pub struct NMat<D, N>
{ mij: DMat<N> }
impl<D: Dim, T: Copy> NMat<D, T>
impl<D: Dim, N: Copy> NMat<D, N>
{
fn offset(i: uint, j: uint) -> uint
{ i * Dim::dim::<D>() + j }
fn set(&mut self, i: uint, j: uint, t: &T)
fn set(&mut self, i: uint, j: uint, t: &N)
{ self.mij.set(i, j, t) }
}
impl<D: Dim, T> Dim for NMat<D, T>
impl<D: Dim, N> Dim for NMat<D, N>
{
fn dim() -> uint
{ Dim::dim::<D>() }
}
impl<D: Dim, T: Copy> Index<(uint, uint), T> for NMat<D, T>
impl<D: Dim, N: Copy> Index<(uint, uint), N> for NMat<D, N>
{
fn index(&self, &idx: &(uint, uint)) -> T
fn index(&self, &idx: &(uint, uint)) -> N
{ self.mij[idx] }
}
impl<D: Dim, T: Copy + One + Zero> One for NMat<D, T>
impl<D: Dim, N: Copy + One + Zero> One for NMat<D, N>
{
fn one() -> NMat<D, T>
fn one() -> NMat<D, N>
{ NMat { mij: one_mat_with_dim(Dim::dim::<D>()) } }
}
impl<D: Dim, T: Copy + Zero> Zero for NMat<D, T>
impl<D: Dim, N: Copy + Zero> Zero for NMat<D, N>
{
fn zero() -> NMat<D, T>
fn zero() -> NMat<D, N>
{ NMat { mij: zero_mat_with_dim(Dim::dim::<D>()) } }
fn is_zero(&self) -> bool
{ is_zero_mat(&self.mij) }
}
impl<D: Dim, T: Copy + Mul<T, T> + Add<T, T> + Zero>
Mul<NMat<D, T>, NMat<D, T>> for NMat<D, T>
impl<D: Dim, N: Copy + Mul<N, N> + Add<N, N> + Zero>
Mul<NMat<D, N>, NMat<D, N>> for NMat<D, N>
{
fn mul(&self, other: &NMat<D, T>) -> NMat<D, T>
fn mul(&self, other: &NMat<D, N>) -> NMat<D, N>
{
let dim = Dim::dim::<D>();
let mut res = Zero::zero::<NMat<D, T>>();
let mut res = Zero::zero::<NMat<D, N>>();
for iterate(0u, dim) |i|
{
for iterate(0u, dim) |j|
{
let mut acc: T = Zero::zero();
let mut acc: N = Zero::zero();
for iterate(0u, dim) |k|
{ acc += self[(i, k)] * other[(k, j)]; }
@ -79,13 +79,13 @@ Mul<NMat<D, T>, NMat<D, T>> for NMat<D, T>
}
}
impl<D: Dim, T: Copy + Add<T, T> + Mul<T, T> + Zero>
RMul<NVec<D, T>> for NMat<D, T>
impl<D: Dim, N: Copy + Add<N, N> + Mul<N, N> + Zero>
RMul<NVec<D, N>> for NMat<D, N>
{
fn rmul(&self, other: &NVec<D, T>) -> NVec<D, T>
fn rmul(&self, other: &NVec<D, N>) -> NVec<D, N>
{
let dim = Dim::dim::<D>();
let mut res : NVec<D, T> = Zero::zero();
let mut res : NVec<D, N> = Zero::zero();
for iterate(0u, dim) |i|
{
@ -97,13 +97,13 @@ RMul<NVec<D, T>> for NMat<D, T>
}
}
impl<D: Dim, T: Copy + Add<T, T> + Mul<T, T> + Zero>
LMul<NVec<D, T>> for NMat<D, T>
impl<D: Dim, N: Copy + Add<N, N> + Mul<N, N> + Zero>
LMul<NVec<D, N>> for NMat<D, N>
{
fn lmul(&self, other: &NVec<D, T>) -> NVec<D, T>
fn lmul(&self, other: &NVec<D, N>) -> NVec<D, N>
{
let dim = Dim::dim::<D>();
let mut res : NVec<D, T> = Zero::zero();
let mut res : NVec<D, N> = Zero::zero();
for iterate(0u, dim) |i|
{
@ -115,19 +115,19 @@ LMul<NVec<D, T>> for NMat<D, T>
}
}
impl<D: Dim, T: Clone + Copy + Eq + DivisionRing>
Inv for NMat<D, T>
impl<D: Dim, N: Clone + Copy + Eq + DivisionRing>
Inv for NMat<D, N>
{
fn inverse(&self) -> NMat<D, T>
fn inverse(&self) -> NMat<D, N>
{ NMat { mij: self.mij.inverse() } }
fn invert(&mut self)
{ self.mij.invert() }
}
impl<D: Dim, T:Copy> Transpose for NMat<D, T>
impl<D: Dim, N:Copy> Transpose for NMat<D, N>
{
fn transposed(&self) -> NMat<D, T>
fn transposed(&self) -> NMat<D, N>
{
let mut res = copy *self;
@ -140,24 +140,24 @@ impl<D: Dim, T:Copy> Transpose for NMat<D, T>
{ self.mij.transpose() }
}
impl<D, T: ApproxEq<T>> ApproxEq<T> for NMat<D, T>
impl<D, N: ApproxEq<N>> ApproxEq<N> for NMat<D, N>
{
fn approx_epsilon() -> T
{ ApproxEq::approx_epsilon::<T, T>() }
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
fn approx_eq(&self, other: &NMat<D, T>) -> bool
fn approx_eq(&self, other: &NMat<D, N>) -> bool
{ self.mij.approx_eq(&other.mij) }
fn approx_eq_eps(&self, other: &NMat<D, T>, epsilon: &T) -> bool
fn approx_eq_eps(&self, other: &NMat<D, N>, epsilon: &N) -> bool
{ self.mij.approx_eq_eps(&other.mij, epsilon) }
}
impl<D: Dim, T: Rand + Zero + Copy> Rand for NMat<D, T>
impl<D: Dim, N: Rand + Zero + Copy> Rand for NMat<D, N>
{
fn rand<R: Rng>(rng: &mut R) -> NMat<D, T>
fn rand<R: Rng>(rng: &mut R) -> NMat<D, N>
{
let dim = Dim::dim::<D>();
let mut res : NMat<D, T> = Zero::zero();
let mut res : NMat<D, N> = Zero::zero();
for iterate(0u, dim) |i|
{

View File

@ -21,174 +21,174 @@ use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub}
// FIXME: it might be possible to implement type-level integers and use them
// here?
#[deriving(Eq, ToStr)]
pub struct NVec<D, T>
{ at: DVec<T> }
pub struct NVec<D, N>
{ at: DVec<N> }
impl<D: Dim, T> Dim for NVec<D, T>
impl<D: Dim, N> Dim for NVec<D, N>
{
fn dim() -> uint
{ Dim::dim::<D>() }
}
impl<D, T: Clone> Clone for NVec<D, T>
impl<D, N: Clone> Clone for NVec<D, N>
{
fn clone(&self) -> NVec<D, T>
fn clone(&self) -> NVec<D, N>
{ NVec{ at: self.at.clone() } }
}
impl<D, T: Copy + Add<T,T>> Add<NVec<D, T>, NVec<D, T>> for NVec<D, T>
impl<D, N: Copy + Add<N,N>> Add<NVec<D, N>, NVec<D, N>> for NVec<D, N>
{
fn add(&self, other: &NVec<D, T>) -> NVec<D, T>
fn add(&self, other: &NVec<D, N>) -> NVec<D, N>
{ NVec { at: self.at + other.at } }
}
impl<D, T: Copy + Sub<T,T>> Sub<NVec<D, T>, NVec<D, T>> for NVec<D, T>
impl<D, N: Copy + Sub<N,N>> Sub<NVec<D, N>, NVec<D, N>> for NVec<D, N>
{
fn sub(&self, other: &NVec<D, T>) -> NVec<D, T>
fn sub(&self, other: &NVec<D, N>) -> NVec<D, N>
{ NVec { at: self.at - other.at } }
}
impl<D, T: Copy + Neg<T>> Neg<NVec<D, T>> for NVec<D, T>
impl<D, N: Copy + Neg<N>> Neg<NVec<D, N>> for NVec<D, N>
{
fn neg(&self) -> NVec<D, T>
fn neg(&self) -> NVec<D, N>
{ NVec { at: -self.at } }
}
impl<D: Dim, T: Copy + Ring>
Dot<T> for NVec<D, T>
impl<D: Dim, N: Copy + Ring>
Dot<N> for NVec<D, N>
{
fn dot(&self, other: &NVec<D, T>) -> T
fn dot(&self, other: &NVec<D, N>) -> N
{ self.at.dot(&other.at) }
}
impl<D: Dim, T: Copy + Ring> SubDot<T> for NVec<D, T>
impl<D: Dim, N: Copy + Ring> SubDot<N> for NVec<D, N>
{
fn sub_dot(&self, a: &NVec<D, T>, b: &NVec<D, T>) -> T
fn sub_dot(&self, a: &NVec<D, N>, b: &NVec<D, N>) -> N
{ self.at.sub_dot(&a.at, &b.at) }
}
impl<D: Dim, T: Copy + Mul<T, T>>
ScalarMul<T> for NVec<D, T>
impl<D: Dim, N: Copy + Mul<N, N>>
ScalarMul<N> for NVec<D, N>
{
fn scalar_mul(&self, s: &T) -> NVec<D, T>
fn scalar_mul(&self, s: &N) -> NVec<D, N>
{ NVec { at: self.at.scalar_mul(s) } }
fn scalar_mul_inplace(&mut self, s: &T)
fn scalar_mul_inplace(&mut self, s: &N)
{ self.at.scalar_mul_inplace(s) }
}
impl<D: Dim, T: Copy + Div<T, T>>
ScalarDiv<T> for NVec<D, T>
impl<D: Dim, N: Copy + Div<N, N>>
ScalarDiv<N> for NVec<D, N>
{
fn scalar_div(&self, s: &T) -> NVec<D, T>
fn scalar_div(&self, s: &N) -> NVec<D, N>
{ NVec { at: self.at.scalar_div(s) } }
fn scalar_div_inplace(&mut self, s: &T)
fn scalar_div_inplace(&mut self, s: &N)
{ self.at.scalar_div_inplace(s) }
}
impl<D: Dim, T: Copy + Add<T, T>>
ScalarAdd<T> for NVec<D, T>
impl<D: Dim, N: Copy + Add<N, N>>
ScalarAdd<N> for NVec<D, N>
{
fn scalar_add(&self, s: &T) -> NVec<D, T>
fn scalar_add(&self, s: &N) -> NVec<D, N>
{ NVec { at: self.at.scalar_add(s) } }
fn scalar_add_inplace(&mut self, s: &T)
fn scalar_add_inplace(&mut self, s: &N)
{ self.at.scalar_add_inplace(s) }
}
impl<D: Dim, T: Copy + Sub<T, T>>
ScalarSub<T> for NVec<D, T>
impl<D: Dim, N: Copy + Sub<N, N>>
ScalarSub<N> for NVec<D, N>
{
fn scalar_sub(&self, s: &T) -> NVec<D, T>
fn scalar_sub(&self, s: &N) -> NVec<D, N>
{ NVec { at: self.at.scalar_sub(s) } }
fn scalar_sub_inplace(&mut self, s: &T)
fn scalar_sub_inplace(&mut self, s: &N)
{ self.scalar_sub_inplace(s) }
}
impl<D: Dim, T: Clone + Copy + Add<T, T>> Translation<NVec<D, T>> for NVec<D, T>
impl<D: Dim, N: Clone + Copy + Add<N, N>> Translation<NVec<D, N>> for NVec<D, N>
{
fn translation(&self) -> NVec<D, T>
fn translation(&self) -> NVec<D, N>
{ self.clone() }
fn translated(&self, t: &NVec<D, T>) -> NVec<D, T>
fn translated(&self, t: &NVec<D, N>) -> NVec<D, N>
{ self + *t }
fn translate(&mut self, t: &NVec<D, T>)
fn translate(&mut self, t: &NVec<D, N>)
{ *self = *self + *t; }
}
impl<D: Dim, T: Copy + DivisionRing + Algebraic + Clone>
Norm<T> for NVec<D, T>
impl<D: Dim, N: Copy + DivisionRing + Algebraic + Clone>
Norm<N> for NVec<D, N>
{
fn sqnorm(&self) -> T
fn sqnorm(&self) -> N
{ self.dot(self) }
fn norm(&self) -> T
fn norm(&self) -> N
{ self.sqnorm().sqrt() }
fn normalized(&self) -> NVec<D, T>
fn normalized(&self) -> NVec<D, N>
{
let mut res : NVec<D, T> = self.clone();
let mut res : NVec<D, N> = self.clone();
res.normalize();
res
}
fn normalize(&mut self) -> T
fn normalize(&mut self) -> N
{ self.at.normalize() }
}
impl<D: Dim,
T: Copy + DivisionRing + Algebraic + Clone + ApproxEq<T>>
Basis for NVec<D, T>
N: Copy + DivisionRing + Algebraic + Clone + ApproxEq<N>>
Basis for NVec<D, N>
{
fn canonical_basis() -> ~[NVec<D, T>]
fn canonical_basis() -> ~[NVec<D, N>]
{ map(DVec::canonical_basis_with_dim(Dim::dim::<D>()), |&e| NVec { at: e }) }
fn orthogonal_subspace_basis(&self) -> ~[NVec<D, T>]
fn orthogonal_subspace_basis(&self) -> ~[NVec<D, N>]
{ map(self.at.orthogonal_subspace_basis(), |&e| NVec { at: e }) }
}
// FIXME: I dont really know how te generalize the cross product int
// n-dimensions…
// impl<T: Copy + Mul<T, T> + Sub<T, T>> Cross<T> for NVec<D, T>
// impl<N: Copy + Mul<N, N> + Sub<N, N>> Cross<N> for NVec<D, N>
// {
// fn cross(&self, other: &NVec<D, T>) -> T
// fn cross(&self, other: &NVec<D, N>) -> N
// { self.x * other.y - self.y * other.x }
// }
impl<D: Dim, T: Copy + Zero> Zero for NVec<D, T>
impl<D: Dim, N: Copy + Zero> Zero for NVec<D, N>
{
fn zero() -> NVec<D, T>
fn zero() -> NVec<D, N>
{ NVec { at: zero_vec_with_dim(Dim::dim::<D>()) } }
fn is_zero(&self) -> bool
{ is_zero_vec(&self.at) }
}
impl<D, T: ApproxEq<T>> ApproxEq<T> for NVec<D, T>
impl<D, N: ApproxEq<N>> ApproxEq<N> for NVec<D, N>
{
fn approx_epsilon() -> T
{ ApproxEq::approx_epsilon::<T, T>() }
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
fn approx_eq(&self, other: &NVec<D, T>) -> bool
fn approx_eq(&self, other: &NVec<D, N>) -> bool
{ self.at.approx_eq(&other.at) }
fn approx_eq_eps(&self, other: &NVec<D, T>, epsilon: &T) -> bool
fn approx_eq_eps(&self, other: &NVec<D, N>, epsilon: &N) -> bool
{ self.at.approx_eq_eps(&other.at, epsilon) }
}
impl<D: Dim, T: Rand + Zero + Copy> Rand for NVec<D, T>
impl<D: Dim, N: Rand + Zero + Copy> Rand for NVec<D, N>
{
fn rand<R: Rng>(rng: &mut R) -> NVec<D, T>
fn rand<R: Rng>(rng: &mut R) -> NVec<D, N>
{
let dim = Dim::dim::<D>();
let mut res : NVec<D, T> = Zero::zero();
let mut res : NVec<D, N> = Zero::zero();
for iterate(0u, dim) |i|
{ res.at.at[i] = rng.gen() }

View File

@ -1,5 +1,5 @@
/**
* Trait of objects having a dimension (in term of spacial dimension).
* Trait of objects having a spacial dimension.
*/
pub trait Dim {
/// The dimension of the object.

View File

@ -7,4 +7,4 @@ use traits::ring::Ring;
pub trait DivisionRing : Ring + Div<Self, Self>
{ }
impl<T: Ring + Div<T, T>> DivisionRing for T;
impl<N: Ring + Div<N, N>> DivisionRing for N;

View File

@ -1,8 +1,8 @@
/**
* Trait of objects having a dot product (also called inner product).
*/
pub trait Dot<T>
pub trait Dot<N>
{
/// Computes the dot (inner) product of two objects.
fn dot(&self, &Self) -> T;
fn dot(&self, &Self) -> N;
}

View File

@ -1,20 +1,20 @@
/**
* Trait of objects having a L² norm and which can be normalized.
*/
pub trait Norm<T>
pub trait Norm<N>
{
/// Computes the norm a an object.
fn norm(&self) -> T;
fn norm(&self) -> N;
/**
* Computes the squared norm of an object. Usually faster than computing the
* norm itself.
*/
fn sqnorm(&self) -> T;
fn sqnorm(&self) -> N;
/// Gets the normalized version of the argument.
fn normalized(&self) -> Self;
/// In-place version of `normalized`.
fn normalize(&mut self) -> T;
fn normalize(&mut self) -> N;
}

View File

@ -10,5 +10,5 @@ pub trait Ring :
Sub<Self, Self> + Add<Self, Self> + Neg<Self> + Mul<Self, Self> + One + Zero
{ }
impl<T: Sub<T, T> + Add<T, T> + Neg<T> + Mul<T, T> + One + Zero>
Ring for T;
impl<N: Sub<N, N> + Add<N, N> + Neg<N> + Mul<N, N> + One + Zero>
Ring for N;

View File

@ -1,4 +1,4 @@
pub trait SubDot<T>
pub trait SubDot<N>
{
/**
* Short-cut to compute the projecton of a point on a vector, but without
@ -8,5 +8,5 @@ pub trait SubDot<T>
* (a - b).dot(c)
*
*/
fn sub_dot(&self, b: &Self, c: &Self) -> T;
fn sub_dot(&self, b: &Self, c: &Self) -> N;
}

View File

@ -6,7 +6,7 @@ use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv};
/// structure, the elements of which have addition, substraction, negation,
/// scalar multiplication (the scalar being a element of a `DivisionRing`), and
/// has a distinct element (`Zero`) neutral wrt the addition.
pub trait VectorSpace<T: DivisionRing>
pub trait VectorSpace<N: DivisionRing>
: Sub<Self, Self> + Add<Self, Self> + Neg<Self> + Zero +
ScalarMul<T> + ScalarDiv<T>
ScalarMul<N> + ScalarDiv<N>
{ }

View File

@ -5,17 +5,17 @@
* exemple does not compile (end with an ICE):
*
* ~~~
* trait Mul<V, T> for T
* trait Mul<V2, T> for T
* trait Mul<V, N> for N
* trait Mul<V2, N> for N
* ~~~
*/
pub trait ScalarMul<T>
pub trait ScalarMul<N>
{
/// Gets the result of a multiplication by a scalar.
fn scalar_mul(&self, &T) -> Self;
fn scalar_mul(&self, &N) -> Self;
/// In-place version of `scalar_mul`.
fn scalar_mul_inplace(&mut self, &T);
fn scalar_mul_inplace(&mut self, &N);
}
/**
@ -25,17 +25,17 @@ pub trait ScalarMul<T>
* exemple does not compile (end with an ICE):
*
* ~~~
* trait Div<V, T> for T
* trait Div<V2, T> for T
* trait Div<V, N> for N
* trait Div<V2, N> for N
* ~~~
*/
pub trait ScalarDiv<T>
pub trait ScalarDiv<N>
{
/// Gets the result of a division by a scalar.
fn scalar_div(&self, &T) -> Self;
fn scalar_div(&self, &N) -> Self;
/// In-place version of `scalar_div`.
fn scalar_div_inplace(&mut self, &T);
fn scalar_div_inplace(&mut self, &N);
}
/**
@ -45,17 +45,17 @@ pub trait ScalarDiv<T>
* exemple does not compile (end with an ICE):
*
* ~~~
* trait Add<V, T> for T
* trait Add<V2, T> for T
* trait Add<V, N> for N
* trait Add<V2, N> for N
* ~~~
*/
pub trait ScalarAdd<T>
pub trait ScalarAdd<N>
{
/// Gets the result of an addition by a scalar.
fn scalar_add(&self, &T) -> Self;
fn scalar_add(&self, &N) -> Self;
/// In-place version of `scalar_add`.
fn scalar_add_inplace(&mut self, &T);
fn scalar_add_inplace(&mut self, &N);
}
/**
@ -65,15 +65,15 @@ pub trait ScalarAdd<T>
* exemple does not compile (end with an ICE):
*
* ~~~
* trait Sub<V, T> for T
* trait Sub<V2, T> for T
* trait Sub<V, N> for N
* trait Sub<V2, N> for N
* ~~~
*/
pub trait ScalarSub<T>
pub trait ScalarSub<N>
{
/// Gets the result of a subtraction by a scalar.
fn scalar_sub(&self, &T) -> Self;
fn scalar_sub(&self, &N) -> Self;
/// In-place version of `scalar_sub`.
fn scalar_sub_inplace(&mut self, &T);
fn scalar_sub_inplace(&mut self, &N);
}