Add inlining pragmas.

This commit is contained in:
Sébastien Crozet 2013-06-13 16:48:28 +00:00
parent 5c777ca32d
commit 513d4d7b08
15 changed files with 311 additions and 10 deletions

View File

@ -20,6 +20,7 @@ pub struct Rotmat<M>
impl<M: Copy> Rotmat<M>
{
#[inline(always)]
fn submat(&self) -> M
{ self.submat }
}
@ -67,12 +68,15 @@ pub fn rotmat3<N: Copy + Trigonometric + Neg<N> + One + Sub<N, N> + Add<N, N> +
impl<N: Div<N, N> + Trigonometric + Neg<N> + Mul<N, N> + Add<N, N> + Copy>
Rotation<Vec1<N>> for Rotmat<Mat2<N>>
{
#[inline(always)]
fn rotation(&self) -> Vec1<N>
{ Vec1::new(-(self.submat.m12 / self.submat.m11).atan()) }
#[inline(always)]
fn rotated(&self, rot: &Vec1<N>) -> Rotmat<Mat2<N>>
{ rotmat2(rot.x) * *self }
#[inline(always)]
fn rotate(&mut self, rot: &Vec1<N>)
{ *self = self.rotated(rot) }
}
@ -81,18 +85,22 @@ 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>>
{
#[inline(always)]
fn rotation(&self) -> (Vec3<N>, N)
{ fail!("Not yet implemented.") }
#[inline(always)]
fn rotated(&self, &(axis, angle): &(Vec3<N>, N)) -> Rotmat<Mat3<N>>
{ rotmat3(&axis, angle) * *self }
#[inline(always)]
fn rotate(&mut self, rot: &(Vec3<N>, N))
{ *self = self.rotated(rot) }
}
impl<N: Copy + Rand + Trigonometric + Neg<N>> Rand for Rotmat<Mat2<N>>
{
#[inline(always)]
fn rand<R: Rng>(rng: &mut R) -> Rotmat<Mat2<N>>
{ rotmat2(rng.gen()) }
}
@ -101,57 +109,67 @@ impl<N: Copy + Rand + Trigonometric + Neg<N> + One + Sub<N, N> + Add<N, N> +
Mul<N, N>>
Rand for Rotmat<Mat3<N>>
{
#[inline(always)]
fn rand<R: Rng>(rng: &mut R) -> Rotmat<Mat3<N>>
{ rotmat3(&rng.gen(), rng.gen()) }
}
impl<M: Dim> Dim for Rotmat<M>
{
#[inline(always)]
fn dim() -> uint
{ Dim::dim::<M>() }
}
impl<M: Copy + One + Zero> One for Rotmat<M>
{
#[inline(always)]
fn one() -> Rotmat<M>
{ Rotmat { submat: One::one() } }
}
impl<M: Copy + Mul<M, M>> Mul<Rotmat<M>, Rotmat<M>> for Rotmat<M>
{
#[inline(always)]
fn mul(&self, other: &Rotmat<M>) -> Rotmat<M>
{ Rotmat { submat: self.submat.mul(&other.submat) } }
}
impl<V, M: RMul<V>> RMul<V> for Rotmat<M>
{
#[inline(always)]
fn rmul(&self, other: &V) -> V
{ self.submat.rmul(other) }
}
impl<V, M: LMul<V>> LMul<V> for Rotmat<M>
{
#[inline(always)]
fn lmul(&self, other: &V) -> V
{ self.submat.lmul(other) }
}
impl<M: Copy> DeltaTransform<M> for Rotmat<M>
{
#[inline(always)]
fn delta_transform(&self) -> M
{ self.submat }
}
impl<M: RMul<V> + Copy, V: Copy> DeltaTransformVector<V> for Rotmat<M>
{
#[inline(always)]
fn delta_transform_vector(&self, v: &V) -> V
{ self.submat.rmul(v) }
}
impl<M: Copy + Transpose> Inv for Rotmat<M>
{
#[inline(always)]
fn invert(&mut self)
{ self.transpose() }
#[inline(always)]
fn inverse(&self) -> Rotmat<M>
{ self.transposed() }
}
@ -159,21 +177,26 @@ impl<M: Copy + Transpose> Inv for Rotmat<M>
impl<M: Copy + Transpose>
Transpose for Rotmat<M>
{
#[inline(always)]
fn transposed(&self) -> Rotmat<M>
{ Rotmat { submat: self.submat.transposed() } }
#[inline(always)]
fn transpose(&mut self)
{ self.submat.transpose() }
}
impl<N: ApproxEq<N>, M: ApproxEq<N>> ApproxEq<N> for Rotmat<M>
{
#[inline(always)]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline(always)]
fn approx_eq(&self, other: &Rotmat<M>) -> bool
{ self.submat.approx_eq(&other.submat) }
#[inline(always)]
fn approx_eq_eps(&self, other: &Rotmat<M>, epsilon: &N) -> bool
{ self.submat.approx_eq_eps(&other.submat, epsilon) }
}

View File

@ -16,27 +16,34 @@ pub struct Transform<M, V>
priv subtrans : V
}
pub fn transform<M: Copy, V: Copy>(mat: &M, trans: &V)
-> Transform<M, V>
{ Transform { submat: *mat, subtrans: *trans } }
impl<M: Copy, V: Copy> Transform<M, V>
{
#[inline(always)]
pub fn new(mat: &M, trans: &V) -> Transform<M, V>
{ Transform { submat: *mat, subtrans: *trans } }
}
impl<M:Dim, V> Dim for Transform<M, V>
{
#[inline(always)]
fn dim() -> uint
{ Dim::dim::<M>() }
}
impl<M:Copy + One, V:Copy + Zero> One for Transform<M, V>
{
#[inline(always)]
fn one() -> Transform<M, V>
{ Transform { submat: One::one(), subtrans: Zero::zero() } }
}
impl<M:Copy + Zero, V:Copy + Zero> Zero for Transform<M, V>
{
#[inline(always)]
fn zero() -> Transform<M, V>
{ Transform { submat: Zero::zero(), subtrans: Zero::zero() } }
#[inline(always)]
fn is_zero(&self) -> bool
{ self.submat.is_zero() && self.subtrans.is_zero() }
}
@ -44,6 +51,7 @@ impl<M:Copy + Zero, V:Copy + Zero> Zero for Transform<M, V>
impl<M:Copy + RMul<V> + Mul<M, M>, V:Copy + Add<V, V>>
Mul<Transform<M, V>, Transform<M, V>> for Transform<M, V>
{
#[inline(always)]
fn mul(&self, other: &Transform<M, V>) -> Transform<M, V>
{
Transform { submat: self.submat * other.submat,
@ -53,24 +61,29 @@ Mul<Transform<M, V>, Transform<M, V>> for Transform<M, V>
impl<M: RMul<V>, V: Add<V, V>> RMul<V> for Transform<M, V>
{
#[inline(always)]
fn rmul(&self, other: &V) -> V
{ self.submat.rmul(other) + self.subtrans }
}
impl<M: LMul<V>, V: Add<V, V>> LMul<V> for Transform<M, V>
{
#[inline(always)]
fn lmul(&self, other: &V) -> V
{ self.submat.lmul(other) + self.subtrans }
}
impl<M: Copy, V: Copy + Translation<V>> Translation<V> for Transform<M, V>
{
#[inline(always)]
fn translation(&self) -> V
{ self.subtrans.translation() }
#[inline(always)]
fn translated(&self, t: &V) -> Transform<M, V>
{ transform(&self.submat, &self.subtrans.translated(t)) }
{ Transform::new(&self.submat, &self.subtrans.translated(t)) }
#[inline(always)]
fn translate(&mut self, t: &V)
{ self.subtrans.translate(t) }
}
@ -78,17 +91,20 @@ impl<M: Copy, V: Copy + Translation<V>> Translation<V> for Transform<M, V>
impl<M: Rotation<AV> + Copy + RMul<V> + One, V: Copy, AV>
Rotation<AV> for Transform<M, V>
{
#[inline(always)]
fn rotation(&self) -> AV
{ self.submat.rotation() }
#[inline(always)]
fn rotated(&self, rot: &AV) -> Transform<M, V>
{
// FIXME: this does not seem opitmal
let delta = One::one::<M>().rotated(rot);
transform(&self.submat.rotated(rot), &delta.rmul(&self.subtrans))
Transform::new(&self.submat.rotated(rot), &delta.rmul(&self.subtrans))
}
#[inline(always)]
fn rotate(&mut self, rot: &AV)
{
// FIXME: this does not seem opitmal
@ -100,12 +116,14 @@ Rotation<AV> for Transform<M, V>
impl<M: Copy, V> DeltaTransform<M> for Transform<M, V>
{
#[inline(always)]
fn delta_transform(&self) -> M
{ self.submat }
}
impl<M: RMul<V> + Copy, V> DeltaTransformVector<V> for Transform<M, V>
{
#[inline(always)]
fn delta_transform_vector(&self, v: &V) -> V
{ self.submat.rmul(v) }
}
@ -113,12 +131,14 @@ impl<M: RMul<V> + Copy, V> DeltaTransformVector<V> for Transform<M, V>
impl<M:Copy + Transpose + Inv + RMul<V>, V:Copy + Neg<V>>
Inv for Transform<M, V>
{
#[inline(always)]
fn invert(&mut self)
{
self.submat.invert();
self.subtrans = self.submat.rmul(&-self.subtrans);
}
#[inline(always)]
fn inverse(&self) -> Transform<M, V>
{
let mut res = *self;
@ -132,15 +152,18 @@ Inv for Transform<M, V>
impl<N: ApproxEq<N>, M:ApproxEq<N>, V:ApproxEq<N>>
ApproxEq<N> for Transform<M, V>
{
#[inline(always)]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline(always)]
fn approx_eq(&self, other: &Transform<M, V>) -> bool
{
self.submat.approx_eq(&other.submat) &&
self.subtrans.approx_eq(&other.subtrans)
}
#[inline(always)]
fn approx_eq_eps(&self, other: &Transform<M, V>, epsilon: &N) -> bool
{
self.submat.approx_eq_eps(&other.submat, epsilon) &&
@ -150,6 +173,7 @@ ApproxEq<N> for Transform<M, V>
impl<M: Rand + Copy, V: Rand + Copy> Rand for Transform<M, V>
{
#[inline(always)]
fn rand<R: Rng>(rng: &mut R) -> Transform<M, V>
{ transform(&rng.gen(), &rng.gen()) }
{ Transform::new(&rng.gen(), &rng.gen()) }
}

View File

@ -14,6 +14,7 @@ pub struct Mat1<N>
impl<N: Copy> Mat1<N>
{
#[inline(always)]
pub fn new(m11: N) -> Mat1<N>
{
Mat1
@ -23,39 +24,46 @@ impl<N: Copy> Mat1<N>
impl<N> Dim for Mat1<N>
{
#[inline(always)]
fn dim() -> uint
{ 1 }
}
impl<N:Copy + One> One for Mat1<N>
{
#[inline(always)]
fn one() -> Mat1<N>
{ return Mat1::new(One::one()) }
}
impl<N:Copy + Zero> Zero for Mat1<N>
{
#[inline(always)]
fn zero() -> Mat1<N>
{ Mat1::new(Zero::zero()) }
#[inline(always)]
fn is_zero(&self) -> bool
{ self.m11.is_zero() }
}
impl<N:Copy + Mul<N, N> + Add<N, N>> Mul<Mat1<N>, Mat1<N>> for Mat1<N>
{
#[inline(always)]
fn mul(&self, other: &Mat1<N>) -> Mat1<N>
{ Mat1::new(self.m11 * other.m11) }
}
impl<N:Copy + Add<N, N> + Mul<N, N>> RMul<Vec1<N>> for Mat1<N>
{
#[inline(always)]
fn rmul(&self, other: &Vec1<N>) -> Vec1<N>
{ Vec1::new(self.m11 * other.x) }
}
impl<N:Copy + Add<N, N> + Mul<N, N>> LMul<Vec1<N>> for Mat1<N>
{
#[inline(always)]
fn lmul(&self, other: &Vec1<N>) -> Vec1<N>
{ Vec1::new(self.m11 * other.x) }
}
@ -63,6 +71,7 @@ impl<N:Copy + Add<N, N> + Mul<N, N>> LMul<Vec1<N>> for Mat1<N>
impl<N:Copy + Mul<N, N> + Div<N, N> + Sub<N, N> + Neg<N> + Zero + One>
Inv for Mat1<N>
{
#[inline(always)]
fn inverse(&self) -> Mat1<N>
{
let mut res : Mat1<N> = *self;
@ -72,6 +81,7 @@ Inv for Mat1<N>
res
}
#[inline(always)]
fn invert(&mut self)
{
assert!(!self.m11.is_zero());
@ -82,42 +92,52 @@ Inv for Mat1<N>
impl<N:Copy> Transpose for Mat1<N>
{
#[inline(always)]
fn transposed(&self) -> Mat1<N>
{ *self }
#[inline(always)]
fn transpose(&mut self)
{ }
}
impl<N:ApproxEq<N>> ApproxEq<N> for Mat1<N>
{
#[inline(always)]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline(always)]
fn approx_eq(&self, other: &Mat1<N>) -> bool
{ self.m11.approx_eq(&other.m11) }
#[inline(always)]
fn approx_eq_eps(&self, other: &Mat1<N>, epsilon: &N) -> bool
{ self.m11.approx_eq_eps(&other.m11, epsilon) }
}
impl<N:Rand + Copy> Rand for Mat1<N>
{
#[inline(always)]
fn rand<R: Rng>(rng: &mut R) -> Mat1<N>
{ Mat1::new(rng.gen()) }
}
impl<N: Copy> Flatten<N> for Mat1<N>
{
#[inline(always)]
fn flat_size() -> uint
{ 1 }
#[inline(always)]
fn from_flattened(l: &[N], off: uint) -> Mat1<N>
{ Mat1::new(l[off]) }
#[inline(always)]
fn flatten(&self) -> ~[N]
{ ~[ self.m11 ] }
#[inline(always)]
fn flatten_to(&self, l: &mut [N], off: uint)
{ l[off] = self.m11 }
}

View File

@ -17,24 +17,28 @@ pub struct Vec1<N>
impl<N: Copy> Vec1<N>
{
#[inline(always)]
pub fn new(x: N) -> Vec1<N>
{ Vec1 {x: x} }
}
impl<N> Dim for Vec1<N>
{
#[inline(always)]
fn dim() -> uint
{ 1 }
}
impl<N:Copy + Add<N,N>> Add<Vec1<N>, Vec1<N>> for Vec1<N>
{
#[inline(always)]
fn add(&self, other: &Vec1<N>) -> Vec1<N>
{ Vec1::new(self.x + other.x) }
}
impl<N:Copy + Sub<N,N>> Sub<Vec1<N>, Vec1<N>> for Vec1<N>
{
#[inline(always)]
fn sub(&self, other: &Vec1<N>) -> Vec1<N>
{ Vec1::new(self.x - other.x) }
}
@ -42,9 +46,11 @@ impl<N:Copy + Sub<N,N>> Sub<Vec1<N>, Vec1<N>> for Vec1<N>
impl<N: Copy + Mul<N, N>>
ScalarMul<N> for Vec1<N>
{
#[inline(always)]
fn scalar_mul(&self, s: &N) -> Vec1<N>
{ Vec1 { x: self.x * *s } }
#[inline(always)]
fn scalar_mul_inplace(&mut self, s: &N)
{ self.x *= *s; }
}
@ -53,9 +59,11 @@ ScalarMul<N> for Vec1<N>
impl<N: Copy + Div<N, N>>
ScalarDiv<N> for Vec1<N>
{
#[inline(always)]
fn scalar_div(&self, s: &N) -> Vec1<N>
{ Vec1 { x: self.x / *s } }
#[inline(always)]
fn scalar_div_inplace(&mut self, s: &N)
{ self.x /= *s; }
}
@ -63,9 +71,11 @@ ScalarDiv<N> for Vec1<N>
impl<N: Copy + Add<N, N>>
ScalarAdd<N> for Vec1<N>
{
#[inline(always)]
fn scalar_add(&self, s: &N) -> Vec1<N>
{ Vec1 { x: self.x + *s } }
#[inline(always)]
fn scalar_add_inplace(&mut self, s: &N)
{ self.x += *s; }
}
@ -73,33 +83,40 @@ ScalarAdd<N> for Vec1<N>
impl<N: Copy + Sub<N, N>>
ScalarSub<N> for Vec1<N>
{
#[inline(always)]
fn scalar_sub(&self, s: &N) -> Vec1<N>
{ Vec1 { x: self.x - *s } }
#[inline(always)]
fn scalar_sub_inplace(&mut self, s: &N)
{ self.x -= *s; }
}
impl<N: Copy + Add<N, N>> Translation<Vec1<N>> for Vec1<N>
{
#[inline(always)]
fn translation(&self) -> Vec1<N>
{ *self }
#[inline(always)]
fn translated(&self, t: &Vec1<N>) -> Vec1<N>
{ self + *t }
#[inline(always)]
fn translate(&mut self, t: &Vec1<N>)
{ *self += *t }
}
impl<N:Copy + Mul<N, N>> Dot<N> for Vec1<N>
{
#[inline(always)]
fn dot(&self, other : &Vec1<N>) -> N
{ self.x * other.x }
}
impl<N:Copy + Mul<N, N> + Sub<N, N>> SubDot<N> for Vec1<N>
{
#[inline(always)]
fn sub_dot(&self, a: &Vec1<N>, b: &Vec1<N>) -> N
{ (self.x - a.x) * b.x }
}
@ -107,15 +124,19 @@ impl<N:Copy + Mul<N, N> + Sub<N, N>> SubDot<N> for Vec1<N>
impl<N:Copy + Mul<N, N> + Add<N, N> + Div<N, N> + Algebraic>
Norm<N> for Vec1<N>
{
#[inline(always)]
fn sqnorm(&self) -> N
{ self.dot(self) }
#[inline(always)]
fn norm(&self) -> N
{ self.sqnorm().sqrt() }
#[inline(always)]
fn normalized(&self) -> Vec1<N>
{ Vec1::new(self.x / self.norm()) }
#[inline(always)]
fn normalize(&mut self) -> N
{
let l = self.norm();
@ -128,60 +149,73 @@ Norm<N> for Vec1<N>
impl<N:Copy + Neg<N>> Neg<Vec1<N>> for Vec1<N>
{
#[inline(always)]
fn neg(&self) -> Vec1<N>
{ Vec1::new(-self.x) }
}
impl<N:Copy + Zero> Zero for Vec1<N>
{
#[inline(always)]
fn zero() -> Vec1<N>
{
let _0 = Zero::zero();
Vec1::new(_0)
}
#[inline(always)]
fn is_zero(&self) -> bool
{ self.x.is_zero() }
}
impl<N: Copy + One> Basis for Vec1<N>
{
#[inline(always)]
fn canonical_basis() -> ~[Vec1<N>]
{ ~[ Vec1::new(One::one()) ] } // FIXME: this should be static
#[inline(always)]
fn orthogonal_subspace_basis(&self) -> ~[Vec1<N>]
{ ~[] }
}
impl<N:ApproxEq<N>> ApproxEq<N> for Vec1<N>
{
#[inline(always)]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline(always)]
fn approx_eq(&self, other: &Vec1<N>) -> bool
{ self.x.approx_eq(&other.x) }
#[inline(always)]
fn approx_eq_eps(&self, other: &Vec1<N>, epsilon: &N) -> bool
{ self.x.approx_eq_eps(&other.x, epsilon) }
}
impl<N:Rand + Copy> Rand for Vec1<N>
{
#[inline(always)]
fn rand<R: Rng>(rng: &mut R) -> Vec1<N>
{ Vec1::new(rng.gen()) }
}
impl<N: Copy> Flatten<N> for Vec1<N>
{
#[inline(always)]
fn flat_size() -> uint
{ 1 }
#[inline(always)]
fn from_flattened(l: &[N], off: uint) -> Vec1<N>
{ Vec1::new(l[off]) }
#[inline(always)]
fn flatten(&self) -> ~[N]
{ ~[ self.x ] }
#[inline(always)]
fn flatten_to(&self, l: &mut [N], off: uint)
{ l[off] = self.x }
}

View File

@ -18,6 +18,7 @@ pub struct Mat2<N>
impl<N: Copy> Mat2<N>
{
#[inline(always)]
pub fn new(m11: N, m12: N, m21: N, m22: N) -> Mat2<N>
{
Mat2
@ -30,12 +31,14 @@ impl<N: Copy> Mat2<N>
impl<N> Dim for Mat2<N>
{
#[inline(always)]
fn dim() -> uint
{ 2 }
}
impl<N:Copy + One + Zero> One for Mat2<N>
{
#[inline(always)]
fn one() -> Mat2<N>
{
let (_0, _1) = (Zero::zero(), One::one());
@ -46,6 +49,7 @@ impl<N:Copy + One + Zero> One for Mat2<N>
impl<N:Copy + Zero> Zero for Mat2<N>
{
#[inline(always)]
fn zero() -> Mat2<N>
{
let _0 = Zero::zero();
@ -53,6 +57,7 @@ impl<N:Copy + Zero> Zero for Mat2<N>
_0, _0)
}
#[inline(always)]
fn is_zero(&self) -> bool
{
self.m11.is_zero() && self.m12.is_zero() &&
@ -62,6 +67,7 @@ impl<N:Copy + Zero> Zero for Mat2<N>
impl<N:Copy + Mul<N, N> + Add<N, N>> Mul<Mat2<N>, Mat2<N>> for Mat2<N>
{
#[inline(always)]
fn mul(&self, other: &Mat2<N>) -> Mat2<N>
{
Mat2::new(
@ -75,6 +81,7 @@ impl<N:Copy + Mul<N, N> + Add<N, N>> Mul<Mat2<N>, Mat2<N>> for Mat2<N>
impl<N:Copy + Add<N, N> + Mul<N, N>> RMul<Vec2<N>> for Mat2<N>
{
#[inline(always)]
fn rmul(&self, other: &Vec2<N>) -> Vec2<N>
{
Vec2::new(
@ -86,6 +93,7 @@ impl<N:Copy + Add<N, N> + Mul<N, N>> RMul<Vec2<N>> for Mat2<N>
impl<N:Copy + Add<N, N> + Mul<N, N>> LMul<Vec2<N>> for Mat2<N>
{
#[inline(always)]
fn lmul(&self, other: &Vec2<N>) -> Vec2<N>
{
Vec2::new(
@ -98,6 +106,7 @@ impl<N:Copy + Add<N, N> + Mul<N, N>> LMul<Vec2<N>> for Mat2<N>
impl<N:Copy + Mul<N, N> + Div<N, N> + Sub<N, N> + Neg<N> + Zero>
Inv for Mat2<N>
{
#[inline(always)]
fn inverse(&self) -> Mat2<N>
{
let mut res : Mat2<N> = *self;
@ -107,6 +116,7 @@ Inv for Mat2<N>
res
}
#[inline(always)]
fn invert(&mut self)
{
let det = self.m11 * self.m22 - self.m21 * self.m12;
@ -120,21 +130,25 @@ Inv for Mat2<N>
impl<N:Copy> Transpose for Mat2<N>
{
#[inline(always)]
fn transposed(&self) -> Mat2<N>
{
Mat2::new(self.m11, self.m21,
self.m12, self.m22)
}
#[inline(always)]
fn transpose(&mut self)
{ swap(&mut self.m21, &mut self.m12); }
}
impl<N:ApproxEq<N>> ApproxEq<N> for Mat2<N>
{
#[inline(always)]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline(always)]
fn approx_eq(&self, other: &Mat2<N>) -> bool
{
self.m11.approx_eq(&other.m11) &&
@ -144,6 +158,7 @@ impl<N:ApproxEq<N>> ApproxEq<N> for Mat2<N>
self.m22.approx_eq(&other.m22)
}
#[inline(always)]
fn approx_eq_eps(&self, other: &Mat2<N>, epsilon: &N) -> bool
{
self.m11.approx_eq_eps(&other.m11, epsilon) &&
@ -156,21 +171,26 @@ impl<N:ApproxEq<N>> ApproxEq<N> for Mat2<N>
impl<N:Rand + Copy> Rand for Mat2<N>
{
#[inline(always)]
fn rand<R: Rng>(rng: &mut R) -> Mat2<N>
{ Mat2::new(rng.gen(), rng.gen(), rng.gen(), rng.gen()) }
}
impl<N: Copy> Flatten<N> for Mat2<N>
{
#[inline(always)]
fn flat_size() -> uint
{ 4 }
#[inline(always)]
fn from_flattened(l: &[N], off: uint) -> Mat2<N>
{ Mat2::new(l[off], l[off + 1], l[off + 2], l[off + 3]) }
#[inline(always)]
fn flatten(&self) -> ~[N]
{ ~[ self.m11, self.m12, self.m21, self.m22 ] }
#[inline(always)]
fn flatten_to(&self, l: &mut [N], off: uint)
{
l[off] = self.m11;

View File

@ -21,24 +21,28 @@ pub struct Vec2<N>
impl<N: Copy> Vec2<N>
{
#[inline(always)]
pub fn new(x: N, y: N) -> Vec2<N>
{ Vec2 {x: x, y: y} }
}
impl<N> Dim for Vec2<N>
{
#[inline(always)]
fn dim() -> uint
{ 2 }
}
impl<N:Copy + Add<N,N>> Add<Vec2<N>, Vec2<N>> for Vec2<N>
{
#[inline(always)]
fn add(&self, other: &Vec2<N>) -> Vec2<N>
{ Vec2::new(self.x + other.x, self.y + other.y) }
}
impl<N:Copy + Sub<N,N>> Sub<Vec2<N>, Vec2<N>> for Vec2<N>
{
#[inline(always)]
fn sub(&self, other: &Vec2<N>) -> Vec2<N>
{ Vec2::new(self.x - other.x, self.y - other.y) }
}
@ -46,9 +50,11 @@ impl<N:Copy + Sub<N,N>> Sub<Vec2<N>, Vec2<N>> for Vec2<N>
impl<N: Copy + Mul<N, N>>
ScalarMul<N> for Vec2<N>
{
#[inline(always)]
fn scalar_mul(&self, s: &N) -> Vec2<N>
{ Vec2 { x: self.x * *s, y: self.y * *s } }
#[inline(always)]
fn scalar_mul_inplace(&mut self, s: &N)
{
self.x *= *s;
@ -60,9 +66,11 @@ ScalarMul<N> for Vec2<N>
impl<N: Copy + Div<N, N>>
ScalarDiv<N> for Vec2<N>
{
#[inline(always)]
fn scalar_div(&self, s: &N) -> Vec2<N>
{ Vec2 { x: self.x / *s, y: self.y / *s } }
#[inline(always)]
fn scalar_div_inplace(&mut self, s: &N)
{
self.x /= *s;
@ -73,9 +81,11 @@ ScalarDiv<N> for Vec2<N>
impl<N: Copy + Add<N, N>>
ScalarAdd<N> for Vec2<N>
{
#[inline(always)]
fn scalar_add(&self, s: &N) -> Vec2<N>
{ Vec2 { x: self.x + *s, y: self.y + *s } }
#[inline(always)]
fn scalar_add_inplace(&mut self, s: &N)
{
self.x += *s;
@ -86,9 +96,11 @@ ScalarAdd<N> for Vec2<N>
impl<N: Copy + Sub<N, N>>
ScalarSub<N> for Vec2<N>
{
#[inline(always)]
fn scalar_sub(&self, s: &N) -> Vec2<N>
{ Vec2 { x: self.x - *s, y: self.y - *s } }
#[inline(always)]
fn scalar_sub_inplace(&mut self, s: &N)
{
self.x -= *s;
@ -98,24 +110,29 @@ ScalarSub<N> for Vec2<N>
impl<N: Copy + Add<N, N>> Translation<Vec2<N>> for Vec2<N>
{
#[inline(always)]
fn translation(&self) -> Vec2<N>
{ *self }
#[inline(always)]
fn translated(&self, t: &Vec2<N>) -> Vec2<N>
{ self + *t }
#[inline(always)]
fn translate(&mut self, t: &Vec2<N>)
{ *self += *t; }
}
impl<N:Copy + Mul<N, N> + Add<N, N>> Dot<N> for Vec2<N>
{
#[inline(always)]
fn dot(&self, other : &Vec2<N>) -> N
{ self.x * other.x + self.y * other.y }
}
impl<N:Copy + Mul<N, N> + Add<N, N> + Sub<N, N>> SubDot<N> for Vec2<N>
{
#[inline(always)]
fn sub_dot(&self, a: &Vec2<N>, b: &Vec2<N>) -> N
{ (self.x - a.x) * b.x + (self.y - a.y) * b.y }
}
@ -123,12 +140,15 @@ impl<N:Copy + Mul<N, N> + Add<N, N> + Sub<N, N>> SubDot<N> for Vec2<N>
impl<N:Copy + Mul<N, N> + Add<N, N> + Div<N, N> + Algebraic>
Norm<N> for Vec2<N>
{
#[inline(always)]
fn sqnorm(&self) -> N
{ self.dot(self) }
#[inline(always)]
fn norm(&self) -> N
{ self.sqnorm().sqrt() }
#[inline(always)]
fn normalized(&self) -> Vec2<N>
{
let l = self.norm();
@ -136,6 +156,7 @@ Norm<N> for Vec2<N>
Vec2::new(self.x / l, self.y / l)
}
#[inline(always)]
fn normalize(&mut self) -> N
{
let l = self.norm();
@ -149,30 +170,35 @@ Norm<N> for Vec2<N>
impl<N:Copy + Mul<N, N> + Sub<N, N>> Cross<Vec1<N>> for Vec2<N>
{
#[inline(always)]
fn cross(&self, other : &Vec2<N>) -> Vec1<N>
{ Vec1::new(self.x * other.y - self.y * other.x) }
}
impl<N:Copy + Neg<N>> Neg<Vec2<N>> for Vec2<N>
{
#[inline(always)]
fn neg(&self) -> Vec2<N>
{ Vec2::new(-self.x, -self.y) }
}
impl<N:Copy + Zero> Zero for Vec2<N>
{
#[inline(always)]
fn zero() -> Vec2<N>
{
let _0 = Zero::zero();
Vec2::new(_0, _0)
}
#[inline(always)]
fn is_zero(&self) -> bool
{ self.x.is_zero() && self.y.is_zero() }
}
impl<N: Copy + One + Zero + Neg<N>> Basis for Vec2<N>
{
#[inline(always)]
fn canonical_basis() -> ~[Vec2<N>]
{
// FIXME: this should be static
@ -180,18 +206,22 @@ impl<N: Copy + One + Zero + Neg<N>> Basis for Vec2<N>
Vec2::new(Zero::zero(), One::one()) ]
}
#[inline(always)]
fn orthogonal_subspace_basis(&self) -> ~[Vec2<N>]
{ ~[ Vec2::new(-self.y, self.x) ] }
}
impl<N:ApproxEq<N>> ApproxEq<N> for Vec2<N>
{
#[inline(always)]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline(always)]
fn approx_eq(&self, other: &Vec2<N>) -> bool
{ self.x.approx_eq(&other.x) && self.y.approx_eq(&other.y) }
#[inline(always)]
fn approx_eq_eps(&self, other: &Vec2<N>, epsilon: &N) -> bool
{
self.x.approx_eq_eps(&other.x, epsilon) &&
@ -201,21 +231,26 @@ impl<N:ApproxEq<N>> ApproxEq<N> for Vec2<N>
impl<N:Rand + Copy> Rand for Vec2<N>
{
#[inline(always)]
fn rand<R: Rng>(rng: &mut R) -> Vec2<N>
{ Vec2::new(rng.gen(), rng.gen()) }
}
impl<N: Copy> Flatten<N> for Vec2<N>
{
#[inline(always)]
fn flat_size() -> uint
{ 2 }
#[inline(always)]
fn from_flattened(l: &[N], off: uint) -> Vec2<N>
{ Vec2::new(l[off], l[off + 1]) }
#[inline(always)]
fn flatten(&self) -> ~[N]
{ ~[ self.x, self.y ] }
#[inline(always)]
fn flatten_to(&self, l: &mut [N], off: uint)
{
l[off] = self.x;

View File

@ -19,6 +19,7 @@ pub struct Mat3<N>
impl<N: Copy> Mat3<N>
{
#[inline(always)]
pub fn new(m11: N, m12: N, m13: N,
m21: N, m22: N, m23: N,
m31: N, m32: N, m33: N) -> Mat3<N>
@ -34,12 +35,14 @@ impl<N: Copy> Mat3<N>
impl<N> Dim for Mat3<N>
{
#[inline(always)]
fn dim() -> uint
{ 3 }
}
impl<N:Copy + One + Zero> One for Mat3<N>
{
#[inline(always)]
fn one() -> Mat3<N>
{
let (_0, _1) = (Zero::zero(), One::one());
@ -51,6 +54,7 @@ impl<N:Copy + One + Zero> One for Mat3<N>
impl<N:Copy + Zero> Zero for Mat3<N>
{
#[inline(always)]
fn zero() -> Mat3<N>
{
let _0 = Zero::zero();
@ -59,6 +63,7 @@ impl<N:Copy + Zero> Zero for Mat3<N>
_0, _0, _0)
}
#[inline(always)]
fn is_zero(&self) -> bool
{
self.m11.is_zero() && self.m12.is_zero() && self.m13.is_zero() &&
@ -69,6 +74,7 @@ impl<N:Copy + Zero> Zero for Mat3<N>
impl<N:Copy + Mul<N, N> + Add<N, N>> Mul<Mat3<N>, Mat3<N>> for Mat3<N>
{
#[inline(always)]
fn mul(&self, other: &Mat3<N>) -> Mat3<N>
{
Mat3::new(
@ -89,6 +95,7 @@ impl<N:Copy + Mul<N, N> + Add<N, N>> Mul<Mat3<N>, Mat3<N>> for Mat3<N>
impl<N:Copy + Add<N, N> + Mul<N, N>> RMul<Vec3<N>> for Mat3<N>
{
#[inline(always)]
fn rmul(&self, other: &Vec3<N>) -> Vec3<N>
{
Vec3::new(
@ -101,6 +108,7 @@ impl<N:Copy + Add<N, N> + Mul<N, N>> RMul<Vec3<N>> for Mat3<N>
impl<N:Copy + Add<N, N> + Mul<N, N>> LMul<Vec3<N>> for Mat3<N>
{
#[inline(always)]
fn lmul(&self, other: &Vec3<N>) -> Vec3<N>
{
Vec3::new(
@ -114,6 +122,7 @@ impl<N:Copy + Add<N, N> + Mul<N, N>> LMul<Vec3<N>> for Mat3<N>
impl<N:Copy + Mul<N, N> + Div<N, N> + Sub<N, N> + Add<N, N> + Neg<N> + Zero>
Inv for Mat3<N>
{
#[inline(always)]
fn inverse(&self) -> Mat3<N>
{
let mut res = *self;
@ -123,6 +132,7 @@ Inv for Mat3<N>
res
}
#[inline(always)]
fn invert(&mut self)
{
let minor_m22_m33 = self.m22 * self.m33 - self.m32 * self.m23;
@ -153,6 +163,7 @@ Inv for Mat3<N>
impl<N:Copy> Transpose for Mat3<N>
{
#[inline(always)]
fn transposed(&self) -> Mat3<N>
{
Mat3::new(self.m11, self.m21, self.m31,
@ -160,6 +171,7 @@ impl<N:Copy> Transpose for Mat3<N>
self.m13, self.m23, self.m33)
}
#[inline(always)]
fn transpose(&mut self)
{
swap(&mut self.m12, &mut self.m21);
@ -170,9 +182,11 @@ impl<N:Copy> Transpose for Mat3<N>
impl<N:ApproxEq<N>> ApproxEq<N> for Mat3<N>
{
#[inline(always)]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline(always)]
fn approx_eq(&self, other: &Mat3<N>) -> bool
{
self.m11.approx_eq(&other.m11) &&
@ -188,6 +202,7 @@ impl<N:ApproxEq<N>> ApproxEq<N> for Mat3<N>
self.m33.approx_eq(&other.m33)
}
#[inline(always)]
fn approx_eq_eps(&self, other: &Mat3<N>, epsilon: &N) -> bool
{
self.m11.approx_eq_eps(&other.m11, epsilon) &&
@ -206,6 +221,7 @@ impl<N:ApproxEq<N>> ApproxEq<N> for Mat3<N>
impl<N:Rand + Copy> Rand for Mat3<N>
{
#[inline(always)]
fn rand<R: Rng>(rng: &mut R) -> Mat3<N>
{
Mat3::new(rng.gen(), rng.gen(), rng.gen(),
@ -216,14 +232,17 @@ impl<N:Rand + Copy> Rand for Mat3<N>
impl<N: Copy> Flatten<N> for Mat3<N>
{
#[inline(always)]
fn flat_size() -> uint
{ 9 }
#[inline(always)]
fn from_flattened(l: &[N], off: uint) -> Mat3<N>
{ Mat3::new(l[off + 0], l[off + 1], l[off + 2],
l[off + 3], l[off + 4], l[off + 5],
l[off + 6], l[off + 7], l[off + 8]) }
#[inline(always)]
fn flatten(&self) -> ~[N]
{
~[
@ -233,6 +252,7 @@ impl<N: Copy> Flatten<N> for Mat3<N>
]
}
#[inline(always)]
fn flatten_to(&self, l: &mut [N], off: uint)
{
l[off + 0] = self.m11;

View File

@ -21,24 +21,28 @@ pub struct Vec3<N>
impl<N: Copy> Vec3<N>
{
#[inline(always)]
pub fn new(x: N, y: N, z: N) -> Vec3<N>
{ Vec3 {x: x, y: y, z: z} }
}
impl<N> Dim for Vec3<N>
{
#[inline(always)]
fn dim() -> uint
{ 3 }
}
impl<N:Copy + Add<N,N>> Add<Vec3<N>, Vec3<N>> for Vec3<N>
{
#[inline(always)]
fn add(&self, other: &Vec3<N>) -> Vec3<N>
{ Vec3::new(self.x + other.x, self.y + other.y, self.z + other.z) }
}
impl<N:Copy + Sub<N,N>> Sub<Vec3<N>, Vec3<N>> for Vec3<N>
{
#[inline(always)]
fn sub(&self, other: &Vec3<N>) -> Vec3<N>
{ Vec3::new(self.x - other.x, self.y - other.y, self.z - other.z) }
}
@ -46,9 +50,11 @@ impl<N:Copy + Sub<N,N>> Sub<Vec3<N>, Vec3<N>> for Vec3<N>
impl<N: Copy + Mul<N, N>>
ScalarMul<N> for Vec3<N>
{
#[inline(always)]
fn scalar_mul(&self, s: &N) -> Vec3<N>
{ Vec3 { x: self.x * *s, y: self.y * *s, z: self.z * *s } }
#[inline(always)]
fn scalar_mul_inplace(&mut self, s: &N)
{
self.x *= *s;
@ -61,9 +67,11 @@ ScalarMul<N> for Vec3<N>
impl<N: Copy + Div<N, N>>
ScalarDiv<N> for Vec3<N>
{
#[inline(always)]
fn scalar_div(&self, s: &N) -> Vec3<N>
{ Vec3 { x: self.x / *s, y: self.y / *s, z: self.z / *s } }
#[inline(always)]
fn scalar_div_inplace(&mut self, s: &N)
{
self.x /= *s;
@ -75,9 +83,11 @@ ScalarDiv<N> for Vec3<N>
impl<N: Copy + Add<N, N>>
ScalarAdd<N> for Vec3<N>
{
#[inline(always)]
fn scalar_add(&self, s: &N) -> Vec3<N>
{ Vec3 { x: self.x + *s, y: self.y + *s, z: self.z + *s } }
#[inline(always)]
fn scalar_add_inplace(&mut self, s: &N)
{
self.x += *s;
@ -89,9 +99,11 @@ ScalarAdd<N> for Vec3<N>
impl<N: Copy + Sub<N, N>>
ScalarSub<N> for Vec3<N>
{
#[inline(always)]
fn scalar_sub(&self, s: &N) -> Vec3<N>
{ Vec3 { x: self.x - *s, y: self.y - *s, z: self.z - *s } }
#[inline(always)]
fn scalar_sub_inplace(&mut self, s: &N)
{
self.x -= *s;
@ -102,12 +114,15 @@ ScalarSub<N> for Vec3<N>
impl<N: Copy + Add<N, N>> Translation<Vec3<N>> for Vec3<N>
{
#[inline(always)]
fn translation(&self) -> Vec3<N>
{ *self }
#[inline(always)]
fn translated(&self, t: &Vec3<N>) -> Vec3<N>
{ self + *t }
#[inline(always)]
fn translate(&mut self, t: &Vec3<N>)
{ *self += *t; }
}
@ -116,18 +131,21 @@ impl<N: Copy + Add<N, N>> Translation<Vec3<N>> for Vec3<N>
impl<N:Copy + Neg<N>> Neg<Vec3<N>> for Vec3<N>
{
#[inline(always)]
fn neg(&self) -> Vec3<N>
{ Vec3::new(-self.x, -self.y, -self.z) }
}
impl<N:Copy + Mul<N, N> + Add<N, N>> Dot<N> for Vec3<N>
{
#[inline(always)]
fn dot(&self, other : &Vec3<N>) -> N
{ self.x * other.x + self.y * other.y + self.z * other.z }
}
impl<N:Copy + Mul<N, N> + Add<N, N> + Sub<N, N>> SubDot<N> for Vec3<N>
{
#[inline(always)]
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 }
}
@ -135,12 +153,15 @@ impl<N:Copy + Mul<N, N> + Add<N, N> + Sub<N, N>> SubDot<N> for Vec3<N>
impl<N:Copy + Mul<N, N> + Add<N, N> + Div<N, N> + Algebraic>
Norm<N> for Vec3<N>
{
#[inline(always)]
fn sqnorm(&self) -> N
{ self.dot(self) }
#[inline(always)]
fn norm(&self) -> N
{ self.sqnorm().sqrt() }
#[inline(always)]
fn normalized(&self) -> Vec3<N>
{
let l = self.norm();
@ -148,6 +169,7 @@ Norm<N> for Vec3<N>
Vec3::new(self.x / l, self.y / l, self.z / l)
}
#[inline(always)]
fn normalize(&mut self) -> N
{
let l = self.norm();
@ -162,6 +184,7 @@ Norm<N> for Vec3<N>
impl<N:Copy + Mul<N, N> + Sub<N, N>> Cross<Vec3<N>> for Vec3<N>
{
#[inline(always)]
fn cross(&self, other : &Vec3<N>) -> Vec3<N>
{
Vec3::new(
@ -174,12 +197,14 @@ impl<N:Copy + Mul<N, N> + Sub<N, N>> Cross<Vec3<N>> for Vec3<N>
impl<N:Copy + Zero> Zero for Vec3<N>
{
#[inline(always)]
fn zero() -> Vec3<N>
{
let _0 = Zero::zero();
Vec3::new(_0, _0, _0)
}
#[inline(always)]
fn is_zero(&self) -> bool
{ self.x.is_zero() && self.y.is_zero() && self.z.is_zero() }
}
@ -188,6 +213,7 @@ 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>
{
#[inline(always)]
fn canonical_basis() -> ~[Vec3<N>]
{
// FIXME: this should be static
@ -196,6 +222,7 @@ Basis for Vec3<N>
Vec3::new(Zero::zero(), Zero::zero(), One::one()) ]
}
#[inline(always)]
fn orthogonal_subspace_basis(&self) -> ~[Vec3<N>]
{
let a =
@ -210,9 +237,11 @@ Basis for Vec3<N>
impl<N:ApproxEq<N>> ApproxEq<N> for Vec3<N>
{
#[inline(always)]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline(always)]
fn approx_eq(&self, other: &Vec3<N>) -> bool
{
self.x.approx_eq(&other.x) &&
@ -220,6 +249,7 @@ impl<N:ApproxEq<N>> ApproxEq<N> for Vec3<N>
self.z.approx_eq(&other.z)
}
#[inline(always)]
fn approx_eq_eps(&self, other: &Vec3<N>, epsilon: &N) -> bool
{
self.x.approx_eq_eps(&other.x, epsilon) &&
@ -230,21 +260,26 @@ impl<N:ApproxEq<N>> ApproxEq<N> for Vec3<N>
impl<N:Copy + Rand> Rand for Vec3<N>
{
#[inline(always)]
fn rand<R: Rng>(rng: &mut R) -> Vec3<N>
{ Vec3::new(rng.gen(), rng.gen(), rng.gen()) }
}
impl<N: Copy> Flatten<N> for Vec3<N>
{
#[inline(always)]
fn flat_size() -> uint
{ 3 }
#[inline(always)]
fn from_flattened(l: &[N], off: uint) -> Vec3<N>
{ Vec3::new(l[off], l[off + 1], l[off + 2]) }
#[inline(always)]
fn flatten(&self) -> ~[N]
{ ~[ self.x, self.y, self.z ] }
#[inline(always)]
fn flatten_to(&self, l: &mut [N], off: uint)
{
l[off] = self.x;

View File

@ -16,12 +16,15 @@ pub struct DMat<N>
mij: ~[N]
}
#[inline(always)]
pub fn zero_mat_with_dim<N: Zero + Copy>(dim: uint) -> DMat<N>
{ DMat { dim: dim, mij: from_elem(dim * dim, Zero::zero()) } }
#[inline(always)]
pub fn is_zero_mat<N: Zero>(mat: &DMat<N>) -> bool
{ mat.mij.all(|e| e.is_zero()) }
#[inline(always)]
pub fn one_mat_with_dim<N: Copy + One + Zero>(dim: uint) -> DMat<N>
{
let mut res = zero_mat_with_dim(dim);
@ -35,9 +38,11 @@ pub fn one_mat_with_dim<N: Copy + One + Zero>(dim: uint) -> DMat<N>
impl<N: Copy> DMat<N>
{
#[inline(always)]
pub fn offset(&self, i: uint, j: uint) -> uint
{ i * self.dim + j }
#[inline(always)]
pub fn set(&mut self, i: uint, j: uint, t: &N)
{
assert!(i < self.dim);
@ -45,6 +50,7 @@ impl<N: Copy> DMat<N>
self.mij[self.offset(i, j)] = *t
}
#[inline(always)]
pub fn at(&self, i: uint, j: uint) -> N
{
assert!(i < self.dim);
@ -55,6 +61,7 @@ impl<N: Copy> DMat<N>
impl<N: Copy> Index<(uint, uint), N> for DMat<N>
{
#[inline(always)]
fn index(&self, &(i, j): &(uint, uint)) -> N
{ self.at(i, j) }
}
@ -129,6 +136,7 @@ LMul<DVec<N>> for DMat<N>
impl<N: Clone + Copy + Eq + DivisionRing>
Inv for DMat<N>
{
#[inline(always)]
fn inverse(&self) -> DMat<N>
{
let mut res : DMat<N> = self.clone();
@ -217,6 +225,7 @@ Inv for DMat<N>
impl<N:Copy> Transpose for DMat<N>
{
#[inline(always)]
fn transposed(&self) -> DMat<N>
{
let mut res = copy *self;
@ -245,9 +254,11 @@ impl<N:Copy> Transpose for DMat<N>
impl<N: ApproxEq<N>> ApproxEq<N> for DMat<N>
{
#[inline(always)]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline(always)]
fn approx_eq(&self, other: &DMat<N>) -> bool
{
let mut zip = self.mij.iter().zip(other.mij.iter());
@ -255,6 +266,7 @@ impl<N: ApproxEq<N>> ApproxEq<N> for DMat<N>
do zip.all |(a, b)| { a.approx_eq(b) }
}
#[inline(always)]
fn approx_eq_eps(&self, other: &DMat<N>, epsilon: &N) -> bool
{
let mut zip = self.mij.iter().zip(other.mij.iter());

View File

@ -17,9 +17,11 @@ pub struct DVec<N>
at: ~[N]
}
#[inline(always)]
pub fn zero_vec_with_dim<N: Zero + Copy>(dim: uint) -> DVec<N>
{ DVec { at: from_elem(dim, Zero::zero::<N>()) } }
#[inline(always)]
pub fn is_zero_vec<N: Zero>(vec: &DVec<N>) -> bool
{ vec.at.all(|e| e.is_zero()) }
@ -77,6 +79,7 @@ impl<N: Copy + DivisionRing + Algebraic + Clone + ApproxEq<N>> DVec<N>
impl<N: Copy + Add<N,N>> Add<DVec<N>, DVec<N>> for DVec<N>
{
#[inline(always)]
fn add(&self, other: &DVec<N>) -> DVec<N>
{
assert!(self.at.len() == other.at.len());
@ -86,6 +89,7 @@ impl<N: Copy + Add<N,N>> Add<DVec<N>, DVec<N>> for DVec<N>
impl<N: Copy + Sub<N,N>> Sub<DVec<N>, DVec<N>> for DVec<N>
{
#[inline(always)]
fn sub(&self, other: &DVec<N>) -> DVec<N>
{
assert!(self.at.len() == other.at.len());
@ -95,6 +99,7 @@ impl<N: Copy + Sub<N,N>> Sub<DVec<N>, DVec<N>> for DVec<N>
impl<N: Copy + Neg<N>> Neg<DVec<N>> for DVec<N>
{
#[inline(always)]
fn neg(&self) -> DVec<N>
{ DVec { at: map(self.at, |a| -a) } }
}
@ -102,6 +107,7 @@ impl<N: Copy + Neg<N>> Neg<DVec<N>> for DVec<N>
impl<N: Copy + Ring>
Dot<N> for DVec<N>
{
#[inline(always)]
fn dot(&self, other: &DVec<N>) -> N
{
assert!(self.at.len() == other.at.len());
@ -117,6 +123,7 @@ Dot<N> for DVec<N>
impl<N: Copy + Ring> SubDot<N> for DVec<N>
{
#[inline(always)]
fn sub_dot(&self, a: &DVec<N>, b: &DVec<N>) -> N
{
let mut res = Zero::zero::<N>();
@ -131,9 +138,11 @@ impl<N: Copy + Ring> SubDot<N> for DVec<N>
impl<N: Copy + Mul<N, N>>
ScalarMul<N> for DVec<N>
{
#[inline(always)]
fn scalar_mul(&self, s: &N) -> DVec<N>
{ DVec { at: map(self.at, |a| a * *s) } }
#[inline(always)]
fn scalar_mul_inplace(&mut self, s: &N)
{
for iterate(0u, self.at.len()) |i|
@ -145,9 +154,11 @@ ScalarMul<N> for DVec<N>
impl<N: Copy + Div<N, N>>
ScalarDiv<N> for DVec<N>
{
#[inline(always)]
fn scalar_div(&self, s: &N) -> DVec<N>
{ DVec { at: map(self.at, |a| a / *s) } }
#[inline(always)]
fn scalar_div_inplace(&mut self, s: &N)
{
for iterate(0u, self.at.len()) |i|
@ -158,9 +169,11 @@ ScalarDiv<N> for DVec<N>
impl<N: Copy + Add<N, N>>
ScalarAdd<N> for DVec<N>
{
#[inline(always)]
fn scalar_add(&self, s: &N) -> DVec<N>
{ DVec { at: map(self.at, |a| a + *s) } }
#[inline(always)]
fn scalar_add_inplace(&mut self, s: &N)
{
for iterate(0u, self.at.len()) |i|
@ -171,9 +184,11 @@ ScalarAdd<N> for DVec<N>
impl<N: Copy + Sub<N, N>>
ScalarSub<N> for DVec<N>
{
#[inline(always)]
fn scalar_sub(&self, s: &N) -> DVec<N>
{ DVec { at: map(self.at, |a| a - *s) } }
#[inline(always)]
fn scalar_sub_inplace(&mut self, s: &N)
{
for iterate(0u, self.at.len()) |i|
@ -183,12 +198,15 @@ ScalarSub<N> for DVec<N>
impl<N: Clone + Copy + Add<N, N>> Translation<DVec<N>> for DVec<N>
{
#[inline(always)]
fn translation(&self) -> DVec<N>
{ self.clone() }
#[inline(always)]
fn translated(&self, t: &DVec<N>) -> DVec<N>
{ self + *t }
#[inline(always)]
fn translate(&mut self, t: &DVec<N>)
{ *self = *self + *t; }
}
@ -196,12 +214,15 @@ impl<N: Clone + Copy + Add<N, N>> Translation<DVec<N>> for DVec<N>
impl<N: Copy + DivisionRing + Algebraic + Clone>
Norm<N> for DVec<N>
{
#[inline(always)]
fn sqnorm(&self) -> N
{ self.dot(self) }
#[inline(always)]
fn norm(&self) -> N
{ self.sqnorm().sqrt() }
#[inline(always)]
fn normalized(&self) -> DVec<N>
{
let mut res : DVec<N> = self.clone();
@ -211,6 +232,7 @@ Norm<N> for DVec<N>
res
}
#[inline(always)]
fn normalize(&mut self) -> N
{
let l = self.norm();
@ -224,9 +246,11 @@ Norm<N> for DVec<N>
impl<N: ApproxEq<N>> ApproxEq<N> for DVec<N>
{
#[inline(always)]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline(always)]
fn approx_eq(&self, other: &DVec<N>) -> bool
{
let mut zip = self.at.iter().zip(other.at.iter());
@ -234,6 +258,7 @@ impl<N: ApproxEq<N>> ApproxEq<N> for DVec<N>
do zip.all |(a, b)| { a.approx_eq(b) }
}
#[inline(always)]
fn approx_eq_eps(&self, other: &DVec<N>, epsilon: &N) -> bool
{
let mut zip = self.at.iter().zip(other.at.iter());

View File

@ -21,36 +21,43 @@ pub struct NMat<D, N>
impl<D: Dim, N: Copy> NMat<D, N>
{
#[inline(always)]
fn offset(i: uint, j: uint) -> uint
{ i * Dim::dim::<D>() + j }
#[inline(always)]
fn set(&mut self, i: uint, j: uint, t: &N)
{ self.mij.set(i, j, t) }
}
impl<D: Dim, N> Dim for NMat<D, N>
{
#[inline(always)]
fn dim() -> uint
{ Dim::dim::<D>() }
}
impl<D: Dim, N: Copy> Index<(uint, uint), N> for NMat<D, N>
{
#[inline(always)]
fn index(&self, &idx: &(uint, uint)) -> N
{ self.mij[idx] }
}
impl<D: Dim, N: Copy + One + Zero> One for NMat<D, N>
{
#[inline(always)]
fn one() -> NMat<D, N>
{ NMat { mij: one_mat_with_dim(Dim::dim::<D>()) } }
}
impl<D: Dim, N: Copy + Zero> Zero for NMat<D, N>
{
#[inline(always)]
fn zero() -> NMat<D, N>
{ NMat { mij: zero_mat_with_dim(Dim::dim::<D>()) } }
#[inline(always)]
fn is_zero(&self) -> bool
{ is_zero_mat(&self.mij) }
}
@ -119,15 +126,18 @@ LMul<NVec<D, N>> for NMat<D, N>
impl<D: Dim, N: Clone + Copy + Eq + DivisionRing>
Inv for NMat<D, N>
{
#[inline(always)]
fn inverse(&self) -> NMat<D, N>
{ NMat { mij: self.mij.inverse() } }
#[inline(always)]
fn invert(&mut self)
{ self.mij.invert() }
}
impl<D: Dim, N:Copy> Transpose for NMat<D, N>
{
#[inline(always)]
fn transposed(&self) -> NMat<D, N>
{
let mut res = copy *self;
@ -137,18 +147,22 @@ impl<D: Dim, N:Copy> Transpose for NMat<D, N>
res
}
#[inline(always)]
fn transpose(&mut self)
{ self.mij.transpose() }
}
impl<D, N: ApproxEq<N>> ApproxEq<N> for NMat<D, N>
{
#[inline(always)]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline(always)]
fn approx_eq(&self, other: &NMat<D, N>) -> bool
{ self.mij.approx_eq(&other.mij) }
#[inline(always)]
fn approx_eq_eps(&self, other: &NMat<D, N>, epsilon: &N) -> bool
{ self.mij.approx_eq_eps(&other.mij, epsilon) }
}
@ -172,9 +186,11 @@ impl<D: Dim, N: Rand + Zero + Copy> Rand for NMat<D, N>
impl<D: Dim, N: Zero + Copy> Flatten<N> for NMat<D, N>
{
#[inline(always)]
fn flat_size() -> uint
{ Dim::dim::<D>() * Dim::dim::<D>() }
#[inline(always)]
fn from_flattened(l: &[N], off: uint) -> NMat<D, N>
{
let dim = Dim::dim::<D>();
@ -186,6 +202,7 @@ impl<D: Dim, N: Zero + Copy> Flatten<N> for NMat<D, N>
res
}
#[inline(always)]
fn flatten(&self) -> ~[N]
{
let dim = Dim::dim::<D>();
@ -197,6 +214,7 @@ impl<D: Dim, N: Zero + Copy> Flatten<N> for NMat<D, N>
res
}
#[inline(always)]
fn flatten_to(&self, l: &mut [N], off: uint)
{
let dim = Dim::dim::<D>();

View File

@ -28,30 +28,35 @@ pub struct NVec<D, N>
impl<D: Dim, N> Dim for NVec<D, N>
{
#[inline(always)]
fn dim() -> uint
{ Dim::dim::<D>() }
}
impl<D, N: Clone> Clone for NVec<D, N>
{
#[inline(always)]
fn clone(&self) -> NVec<D, N>
{ NVec{ at: self.at.clone() } }
}
impl<D, N: Copy + Add<N,N>> Add<NVec<D, N>, NVec<D, N>> for NVec<D, N>
{
#[inline(always)]
fn add(&self, other: &NVec<D, N>) -> NVec<D, N>
{ NVec { at: self.at + other.at } }
}
impl<D, N: Copy + Sub<N,N>> Sub<NVec<D, N>, NVec<D, N>> for NVec<D, N>
{
#[inline(always)]
fn sub(&self, other: &NVec<D, N>) -> NVec<D, N>
{ NVec { at: self.at - other.at } }
}
impl<D, N: Copy + Neg<N>> Neg<NVec<D, N>> for NVec<D, N>
{
#[inline(always)]
fn neg(&self) -> NVec<D, N>
{ NVec { at: -self.at } }
}
@ -59,12 +64,14 @@ impl<D, N: Copy + Neg<N>> Neg<NVec<D, N>> for NVec<D, N>
impl<D: Dim, N: Copy + Ring>
Dot<N> for NVec<D, N>
{
#[inline(always)]
fn dot(&self, other: &NVec<D, N>) -> N
{ self.at.dot(&other.at) }
}
impl<D: Dim, N: Copy + Ring> SubDot<N> for NVec<D, N>
{
#[inline(always)]
fn sub_dot(&self, a: &NVec<D, N>, b: &NVec<D, N>) -> N
{ self.at.sub_dot(&a.at, &b.at) }
}
@ -72,9 +79,11 @@ impl<D: Dim, N: Copy + Ring> SubDot<N> for NVec<D, N>
impl<D: Dim, N: Copy + Mul<N, N>>
ScalarMul<N> for NVec<D, N>
{
#[inline(always)]
fn scalar_mul(&self, s: &N) -> NVec<D, N>
{ NVec { at: self.at.scalar_mul(s) } }
#[inline(always)]
fn scalar_mul_inplace(&mut self, s: &N)
{ self.at.scalar_mul_inplace(s) }
}
@ -83,9 +92,11 @@ ScalarMul<N> for NVec<D, N>
impl<D: Dim, N: Copy + Div<N, N>>
ScalarDiv<N> for NVec<D, N>
{
#[inline(always)]
fn scalar_div(&self, s: &N) -> NVec<D, N>
{ NVec { at: self.at.scalar_div(s) } }
#[inline(always)]
fn scalar_div_inplace(&mut self, s: &N)
{ self.at.scalar_div_inplace(s) }
}
@ -93,9 +104,11 @@ ScalarDiv<N> for NVec<D, N>
impl<D: Dim, N: Copy + Add<N, N>>
ScalarAdd<N> for NVec<D, N>
{
#[inline(always)]
fn scalar_add(&self, s: &N) -> NVec<D, N>
{ NVec { at: self.at.scalar_add(s) } }
#[inline(always)]
fn scalar_add_inplace(&mut self, s: &N)
{ self.at.scalar_add_inplace(s) }
}
@ -103,21 +116,26 @@ ScalarAdd<N> for NVec<D, N>
impl<D: Dim, N: Copy + Sub<N, N>>
ScalarSub<N> for NVec<D, N>
{
#[inline(always)]
fn scalar_sub(&self, s: &N) -> NVec<D, N>
{ NVec { at: self.at.scalar_sub(s) } }
#[inline(always)]
fn scalar_sub_inplace(&mut self, s: &N)
{ self.scalar_sub_inplace(s) }
}
impl<D: Dim, N: Clone + Copy + Add<N, N>> Translation<NVec<D, N>> for NVec<D, N>
{
#[inline(always)]
fn translation(&self) -> NVec<D, N>
{ self.clone() }
#[inline(always)]
fn translated(&self, t: &NVec<D, N>) -> NVec<D, N>
{ self + *t }
#[inline(always)]
fn translate(&mut self, t: &NVec<D, N>)
{ *self = *self + *t; }
}
@ -125,12 +143,15 @@ impl<D: Dim, N: Clone + Copy + Add<N, N>> Translation<NVec<D, N>> for NVec<D, N>
impl<D: Dim, N: Copy + DivisionRing + Algebraic + Clone>
Norm<N> for NVec<D, N>
{
#[inline(always)]
fn sqnorm(&self) -> N
{ self.dot(self) }
#[inline(always)]
fn norm(&self) -> N
{ self.sqnorm().sqrt() }
#[inline(always)]
fn normalized(&self) -> NVec<D, N>
{
let mut res : NVec<D, N> = self.clone();
@ -140,6 +161,7 @@ Norm<N> for NVec<D, N>
res
}
#[inline(always)]
fn normalize(&mut self) -> N
{ self.at.normalize() }
}
@ -148,9 +170,11 @@ impl<D: Dim,
N: Copy + DivisionRing + Algebraic + Clone + ApproxEq<N>>
Basis for NVec<D, N>
{
#[inline(always)]
fn canonical_basis() -> ~[NVec<D, N>]
{ map(DVec::canonical_basis_with_dim(Dim::dim::<D>()), |&e| NVec { at: e }) }
#[inline(always)]
fn orthogonal_subspace_basis(&self) -> ~[NVec<D, N>]
{ map(self.at.orthogonal_subspace_basis(), |&e| NVec { at: e }) }
}
@ -165,27 +189,33 @@ Basis for NVec<D, N>
impl<D: Dim, N: Copy + Zero> Zero for NVec<D, N>
{
#[inline(always)]
fn zero() -> NVec<D, N>
{ NVec { at: zero_vec_with_dim(Dim::dim::<D>()) } }
#[inline(always)]
fn is_zero(&self) -> bool
{ is_zero_vec(&self.at) }
}
impl<D, N: ApproxEq<N>> ApproxEq<N> for NVec<D, N>
{
#[inline(always)]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline(always)]
fn approx_eq(&self, other: &NVec<D, N>) -> bool
{ self.at.approx_eq(&other.at) }
#[inline(always)]
fn approx_eq_eps(&self, other: &NVec<D, N>, epsilon: &N) -> bool
{ self.at.approx_eq_eps(&other.at, epsilon) }
}
impl<D: Dim, N: Rand + Zero + Copy> Rand for NVec<D, N>
{
#[inline(always)]
fn rand<R: Rng>(rng: &mut R) -> NVec<D, N>
{
let dim = Dim::dim::<D>();
@ -200,9 +230,11 @@ impl<D: Dim, N: Rand + Zero + Copy> Rand for NVec<D, N>
impl<D: Dim, N: Zero + Copy> Flatten<N> for NVec<D, N>
{
#[inline(always)]
fn flat_size() -> uint
{ Dim::dim::<D>() }
#[inline(always)]
fn from_flattened(l: &[N], off: uint) -> NVec<D, N>
{
let dim = Dim::dim::<D>();
@ -214,6 +246,7 @@ impl<D: Dim, N: Zero + Copy> Flatten<N> for NVec<D, N>
res
}
#[inline(always)]
fn flatten(&self) -> ~[N]
{
let dim = Dim::dim::<D>();
@ -225,6 +258,7 @@ impl<D: Dim, N: Zero + Copy> Flatten<N> for NVec<D, N>
res
}
#[inline(always)]
fn flatten_to(&self, l: &mut [N], off: uint)
{
let dim = Dim::dim::<D>();

View File

@ -45,9 +45,9 @@ macro_rules! test_flatten_impl(
let v: $t = random();
let mut l: ~[$n] = vec::from_elem(42 + Flatten::flat_size::<$n, $t>(), Zero::zero::<$n>());
v.to_flattened_inplace(l, 42);
v.flatten_to(l, 42);
assert!(Flatten::from_flattened::<$n, $t>(v.to_flattened(), 0) == v);
assert!(Flatten::from_flattened::<$n, $t>(v.flatten(), 0) == v);
assert!(Flatten::from_flattened::<$n, $t>(l, 42) == v);
}
)

View File

@ -86,9 +86,9 @@ macro_rules! test_flatten_impl(
let v: $t = random();
let mut l: ~[$n] = vec::from_elem(42 + Flatten::flat_size::<$n, $t>(), Zero::zero::<$n>());
v.to_flattened_inplace(l, 42);
v.flatten_to(l, 42);
assert!(Flatten::from_flattened::<$n, $t>(v.to_flattened(), 0) == v);
assert!(Flatten::from_flattened::<$n, $t>(v.flatten(), 0) == v);
assert!(Flatten::from_flattened::<$n, $t>(l, 42) == v);
}
)

View File

@ -23,6 +23,7 @@ pub trait Rotation<V>
* - `ammount`: the rotation to apply.
* - `point`: the center of rotation.
*/
#[inline(always)]
pub fn rotate_wrt_point<M: Rotation<AV> + Translation<LV>, LV: Neg<LV>, AV>
(m: &M, ammount: &AV, center: &LV) -> M
{