Use object-oriented style for trait methods

Wherever sensible the geometric traits now take `&self` arguments, so that
implementations can be invoked as instance methods instead of static methods.
This makes some idioms easier to express and generally allows for greater
flexibility as the restructured methods can still be invoked like static
methods.

Fixes #39.
This commit is contained in:
Eduard Bopp 2014-12-01 18:50:11 +01:00
parent 3b44441ce9
commit edaeab72ac
16 changed files with 246 additions and 317 deletions

View File

@ -326,13 +326,13 @@ pub fn center<N: BaseFloat, P: FloatPnt<N, V>, V>(a: &P, b: &P) -> P {
/// Returns the distance between two points. /// Returns the distance between two points.
#[inline(always)] #[inline(always)]
pub fn dist<N: BaseFloat, P: FloatPnt<N, V>, V: Norm<N>>(a: &P, b: &P) -> N { pub fn dist<N: BaseFloat, P: FloatPnt<N, V>, V: Norm<N>>(a: &P, b: &P) -> N {
FloatPnt::<N, V>::dist(a, b) a.dist(b)
} }
/// Returns the squared distance between two points. /// Returns the squared distance between two points.
#[inline(always)] #[inline(always)]
pub fn sqdist<N: BaseFloat, P: FloatPnt<N, V>, V: Norm<N>>(a: &P, b: &P) -> N { pub fn sqdist<N: BaseFloat, P: FloatPnt<N, V>, V: Norm<N>>(a: &P, b: &P) -> N {
FloatPnt::<N, V>::sqdist(a, b) a.sqdist(b)
} }
/* /*

View File

@ -355,9 +355,8 @@ impl<N: Clone + Add<N, N> + Mul<N, N> + Zero> Mul<DMat<N>, DVec<N>> for DVec<N>
impl<N: Clone + BaseNum + Zero + One> Inv for DMat<N> { impl<N: Clone + BaseNum + Zero + One> Inv for DMat<N> {
#[inline] #[inline]
fn inv_cpy(m: &DMat<N>) -> Option<DMat<N>> { fn inv_cpy(&self) -> Option<DMat<N>> {
let mut res : DMat<N> = m.clone(); let mut res: DMat<N> = self.clone();
if res.inv() { if res.inv() {
Some(res) Some(res)
} }
@ -442,21 +441,21 @@ impl<N: Clone + BaseNum + Zero + One> Inv for DMat<N> {
impl<N: Clone> Transpose for DMat<N> { impl<N: Clone> Transpose for DMat<N> {
#[inline] #[inline]
fn transpose_cpy(m: &DMat<N>) -> DMat<N> { fn transpose_cpy(&self) -> DMat<N> {
if m.nrows == m.ncols { if self.nrows == self.ncols {
let mut res = m.clone(); let mut res = self.clone();
res.transpose(); res.transpose();
res res
} }
else { else {
let mut res = unsafe { DMat::new_uninitialized(m.ncols, m.nrows) }; let mut res = unsafe { DMat::new_uninitialized(self.ncols, self.nrows) };
for i in range(0u, m.nrows) { for i in range(0u, self.nrows) {
for j in range(0u, m.ncols) { for j in range(0u, self.ncols) {
unsafe { unsafe {
res.unsafe_set((j, i), m.unsafe_at((i, j))) res.unsafe_set((j, i), self.unsafe_at((i, j)))
} }
} }
} }
@ -487,14 +486,14 @@ impl<N: Clone> Transpose for DMat<N> {
} }
impl<N: BaseNum + Cast<f64> + Zero + Clone> Mean<DVec<N>> for DMat<N> { impl<N: BaseNum + Cast<f64> + Zero + Clone> Mean<DVec<N>> for DMat<N> {
fn mean(m: &DMat<N>) -> DVec<N> { fn mean(&self) -> DVec<N> {
let mut res: DVec<N> = DVec::new_zeros(m.ncols); let mut res: DVec<N> = DVec::new_zeros(self.ncols);
let normalizer: N = Cast::from(1.0f64 / Cast::from(m.nrows)); let normalizer: N = Cast::from(1.0f64 / Cast::from(self.nrows));
for i in range(0u, m.nrows) { for i in range(0u, self.nrows) {
for j in range(0u, m.ncols) { for j in range(0u, self.ncols) {
unsafe { unsafe {
let acc = res.unsafe_at(j) + m.unsafe_at((i, j)) * normalizer; let acc = res.unsafe_at(j) + self.unsafe_at((i, j)) * normalizer;
res.unsafe_set(j, acc); res.unsafe_set(j, acc);
} }
} }
@ -506,23 +505,23 @@ impl<N: BaseNum + Cast<f64> + Zero + Clone> Mean<DVec<N>> for DMat<N> {
impl<N: Clone + BaseNum + Cast<f64> + Div<N, N>> Cov<DMat<N>> for DMat<N> { impl<N: Clone + BaseNum + Cast<f64> + Div<N, N>> Cov<DMat<N>> for DMat<N> {
// FIXME: this could be heavily optimized, removing all temporaries by merging loops. // FIXME: this could be heavily optimized, removing all temporaries by merging loops.
fn cov(m: &DMat<N>) -> DMat<N> { fn cov(&self) -> DMat<N> {
assert!(m.nrows > 1); assert!(self.nrows > 1);
let mut centered = unsafe { DMat::new_uninitialized(m.nrows, m.ncols) }; let mut centered = unsafe { DMat::new_uninitialized(self.nrows, self.ncols) };
let mean = Mean::mean(m); let mean = self.mean();
// FIXME: use the rows iterator when available // FIXME: use the rows iterator when available
for i in range(0u, m.nrows) { for i in range(0u, self.nrows) {
for j in range(0u, m.ncols) { for j in range(0u, self.ncols) {
unsafe { unsafe {
centered.unsafe_set((i, j), m.unsafe_at((i, j)) - mean.unsafe_at(j)); centered.unsafe_set((i, j), self.unsafe_at((i, j)) - mean.unsafe_at(j));
} }
} }
} }
// FIXME: return a triangular matrix? // FIXME: return a triangular matrix?
let fnormalizer: f64 = Cast::from(m.nrows() - 1); let fnormalizer: f64 = Cast::from(self.nrows() - 1);
let normalizer: N = Cast::from(fnormalizer); let normalizer: N = Cast::from(fnormalizer);
// FIXME: this will do 2 allocations for temporaries! // FIXME: this will do 2 allocations for temporaries!
(Transpose::transpose_cpy(&centered) * centered) / normalizer (Transpose::transpose_cpy(&centered) * centered) / normalizer
@ -604,16 +603,8 @@ impl<N: ApproxEq<N>> ApproxEq<N> for DMat<N> {
} }
#[inline] #[inline]
fn approx_eq(a: &DMat<N>, b: &DMat<N>) -> bool { fn approx_eq_eps(&self, other: &DMat<N>, epsilon: &N) -> bool {
let zip = a.mij.iter().zip(b.mij.iter()); let zip = self.mij.iter().zip(other.mij.iter());
zip.all(|(a, b)| ApproxEq::approx_eq(a, b))
}
#[inline]
fn approx_eq_eps(a: &DMat<N>, b: &DMat<N>, epsilon: &N) -> bool {
let zip = a.mij.iter().zip(b.mij.iter());
zip.all(|(a, b)| ApproxEq::approx_eq_eps(a, b, epsilon)) zip.all(|(a, b)| ApproxEq::approx_eq_eps(a, b, epsilon))
} }
} }

View File

@ -217,36 +217,26 @@ macro_rules! dvec_impl(
impl<N: BaseNum + Clone> Dot<N> for $dvec<N> { impl<N: BaseNum + Clone> Dot<N> for $dvec<N> {
#[inline] #[inline]
fn dot(a: &$dvec<N>, b: &$dvec<N>) -> N { fn dot(&self, other: &$dvec<N>) -> N {
assert!(a.len() == b.len()); assert!(self.len() == other.len());
let mut res: N = ::zero(); let mut res: N = ::zero();
for i in range(0u, self.len()) {
for i in range(0u, a.len()) { res = res + unsafe { self.unsafe_at(i) * other.unsafe_at(i) };
res = res + unsafe { a.unsafe_at(i) * b.unsafe_at(i) };
} }
res res
} }
} }
impl<N: BaseFloat + Clone> Norm<N> for $dvec<N> { impl<N: BaseFloat + Clone> Norm<N> for $dvec<N> {
#[inline] #[inline]
fn sqnorm(v: &$dvec<N>) -> N { fn sqnorm(&self) -> N {
Dot::dot(v, v) Dot::dot(self, self)
} }
#[inline] #[inline]
fn norm(v: &$dvec<N>) -> N { fn normalize_cpy(&self) -> $dvec<N> {
Norm::sqnorm(v).sqrt() let mut res : $dvec<N> = self.clone();
}
#[inline]
fn normalize_cpy(v: &$dvec<N>) -> $dvec<N> {
let mut res : $dvec<N> = v.clone();
let _ = res.normalize(); let _ = res.normalize();
res res
} }
@ -269,16 +259,8 @@ macro_rules! dvec_impl(
} }
#[inline] #[inline]
fn approx_eq(a: &$dvec<N>, b: &$dvec<N>) -> bool { fn approx_eq_eps(&self, other: &$dvec<N>, epsilon: &N) -> bool {
let zip = a.as_slice().iter().zip(b.as_slice().iter()); let zip = self.as_slice().iter().zip(other.as_slice().iter());
zip.all(|(a, b)| ApproxEq::approx_eq(a, b))
}
#[inline]
fn approx_eq_eps(a: &$dvec<N>, b: &$dvec<N>, epsilon: &N) -> bool {
let zip = a.as_slice().iter().zip(b.as_slice().iter());
zip.all(|(a, b)| ApproxEq::approx_eq_eps(a, b, epsilon)) zip.all(|(a, b)| ApproxEq::approx_eq_eps(a, b, epsilon))
} }
} }

View File

@ -113,8 +113,8 @@ macro_rules! translation_impl(
} }
#[inline] #[inline]
fn append_translation_cpy(iso: &$t<N>, t: &$tv<N>) -> $t<N> { fn append_translation_cpy(&self, t: &$tv<N>) -> $t<N> {
$t::new_with_rotmat(*t + iso.translation, iso.rotation.clone()) $t::new_with_rotmat(*t + self.translation, self.rotation.clone())
} }
#[inline] #[inline]
@ -123,8 +123,8 @@ macro_rules! translation_impl(
} }
#[inline] #[inline]
fn prepend_translation_cpy(iso: &$t<N>, t: &$tv<N>) -> $t<N> { fn prepend_translation_cpy(&self, t: &$tv<N>) -> $t<N> {
$t::new_with_rotmat(iso.translation + iso.rotation * *t, iso.rotation.clone()) $t::new_with_rotmat(self.translation + self.rotation * *t, self.rotation.clone())
} }
#[inline] #[inline]
@ -173,10 +173,10 @@ macro_rules! rotation_impl(
} }
#[inline] #[inline]
fn append_rotation_cpy(t: &$t<N>, rot: &$tav<N>) -> $t<N> { fn append_rotation_cpy(&self, rot: &$tav<N>) -> $t<N> {
let delta = $trot::new(rot.clone()); let delta = $trot::new(rot.clone());
$t::new_with_rotmat(delta * t.translation, delta * t.rotation) $t::new_with_rotmat(delta * self.translation, delta * self.rotation)
} }
#[inline] #[inline]
@ -187,10 +187,10 @@ macro_rules! rotation_impl(
} }
#[inline] #[inline]
fn prepend_rotation_cpy(t: &$t<N>, rot: &$tav<N>) -> $t<N> { fn prepend_rotation_cpy(&self, rot: &$tav<N>) -> $t<N> {
let delta = $trot::new(rot.clone()); let delta = $trot::new(rot.clone());
$t::new_with_rotmat(t.translation.clone(), t.rotation * delta) $t::new_with_rotmat(self.translation.clone(), self.rotation * delta)
} }
#[inline] #[inline]
@ -234,16 +234,16 @@ macro_rules! transformation_impl(
*self = *t * *self *self = *t * *self
} }
fn append_transformation_cpy(iso: &$t<N>, t: &$t<N>) -> $t<N> { fn append_transformation_cpy(&self, t: &$t<N>) -> $t<N> {
*t * *iso *t * *self
} }
fn prepend_transformation(&mut self, t: &$t<N>) { fn prepend_transformation(&mut self, t: &$t<N>) {
*self = *self * *t *self = *self * *t
} }
fn prepend_transformation_cpy(iso: &$t<N>, t: &$t<N>) -> $t<N> { fn prepend_transformation_cpy(&self, t: &$t<N>) -> $t<N> {
*iso * *t *self * *t
} }
fn set_transformation(&mut self, t: $t<N>) { fn set_transformation(&mut self, t: $t<N>) {
@ -276,17 +276,14 @@ macro_rules! inv_impl(
fn inv(&mut self) -> bool { fn inv(&mut self) -> bool {
self.rotation.inv(); self.rotation.inv();
self.translation = self.rotation * -self.translation; self.translation = self.rotation * -self.translation;
// always succeed // always succeed
true true
} }
#[inline] #[inline]
fn inv_cpy(m: &$t<N>) -> Option<$t<N>> { fn inv_cpy(&self) -> Option<$t<N>> {
let mut res = m.clone(); let mut res = self.clone();
res.inv(); res.inv();
// always succeed // always succeed
Some(res) Some(res)
} }
@ -320,15 +317,9 @@ macro_rules! approx_eq_impl(
} }
#[inline] #[inline]
fn approx_eq(a: &$t<N>, b: &$t<N>) -> bool { fn approx_eq_eps(&self, other: &$t<N>, epsilon: &N) -> bool {
ApproxEq::approx_eq(&a.rotation, &b.rotation) && ApproxEq::approx_eq_eps(&self.rotation, &other.rotation, epsilon) &&
ApproxEq::approx_eq(&a.translation, &b.translation) ApproxEq::approx_eq_eps(&self.translation, &other.translation, epsilon)
}
#[inline]
fn approx_eq_eps(a: &$t<N>, b: &$t<N>, epsilon: &N) -> bool {
ApproxEq::approx_eq_eps(&a.rotation, &b.rotation, epsilon) &&
ApproxEq::approx_eq_eps(&a.translation, &b.translation, epsilon)
} }
} }
) )

View File

@ -513,9 +513,8 @@ macro_rules! inv_impl(
impl<N: Clone + BaseNum> impl<N: Clone + BaseNum>
Inv for $t<N> { Inv for $t<N> {
#[inline] #[inline]
fn inv_cpy(m: &$t<N>) -> Option<$t<N>> { fn inv_cpy(&self) -> Option<$t<N>> {
let mut res : $t<N> = m.clone(); let mut res : $t<N> = self.clone();
if res.inv() { if res.inv() {
Some(res) Some(res)
} }
@ -596,11 +595,9 @@ macro_rules! transpose_impl(
($t: ident, $dim: expr) => ( ($t: ident, $dim: expr) => (
impl<N: Clone> Transpose for $t<N> { impl<N: Clone> Transpose for $t<N> {
#[inline] #[inline]
fn transpose_cpy(m: &$t<N>) -> $t<N> { fn transpose_cpy(&self) -> $t<N> {
let mut res = m.clone(); let mut res = self.clone();
res.transpose(); res.transpose();
res res
} }
@ -625,16 +622,8 @@ macro_rules! approx_eq_impl(
} }
#[inline] #[inline]
fn approx_eq(a: &$t<N>, b: &$t<N>) -> bool { fn approx_eq_eps(&self, other: &$t<N>, epsilon: &N) -> bool {
let zip = a.iter().zip(b.iter()); let zip = self.iter().zip(other.iter());
zip.all(|(a, b)| ApproxEq::approx_eq(a, b))
}
#[inline]
fn approx_eq_eps(a: &$t<N>, b: &$t<N>, epsilon: &N) -> bool {
let zip = a.iter().zip(b.iter());
zip.all(|(a, b)| ApproxEq::approx_eq_eps(a, b, epsilon)) zip.all(|(a, b)| ApproxEq::approx_eq_eps(a, b, epsilon))
} }
} }
@ -686,15 +675,13 @@ macro_rules! outer_impl(
($t: ident, $m: ident) => ( ($t: ident, $m: ident) => (
impl<N: Clone + Mul<N, N> + Zero> Outer<$m<N>> for $t<N> { impl<N: Clone + Mul<N, N> + Zero> Outer<$m<N>> for $t<N> {
#[inline] #[inline]
fn outer(a: &$t<N>, b: &$t<N>) -> $m<N> { fn outer(&self, other: &$t<N>) -> $m<N> {
let mut res: $m<N> = ::zero(); let mut res: $m<N> = ::zero();
for i in range(0u, Dim::dim(None::<$t<N>>)) { for i in range(0u, Dim::dim(None::<$t<N>>)) {
for j in range(0u, Dim::dim(None::<$t<N>>)) { for j in range(0u, Dim::dim(None::<$t<N>>)) {
res.set((i, j), a.at(i) * b.at(j)) res.set((i, j), self.at(i) * other.at(j))
} }
} }
res res
} }
} }
@ -705,8 +692,8 @@ macro_rules! eigen_qr_impl(
($t: ident, $v: ident) => ( ($t: ident, $v: ident) => (
impl<N> EigenQR<N, $v<N>> for $t<N> impl<N> EigenQR<N, $v<N>> for $t<N>
where N: BaseNum + One + Zero + BaseFloat + ApproxEq<N> + Clone { where N: BaseNum + One + Zero + BaseFloat + ApproxEq<N> + Clone {
fn eigen_qr(m: &$t<N>, eps: &N, niter: uint) -> ($t<N>, $v<N>) { fn eigen_qr(&self, eps: &N, niter: uint) -> ($t<N>, $v<N>) {
linalg::eigen_qr(m, eps, niter) linalg::eigen_qr(self, eps, niter)
} }
} }
) )

View File

@ -66,9 +66,8 @@ impl<N: Neg<N>> Quat<N> {
impl<N: BaseFloat + ApproxEq<N> + Clone> Inv for Quat<N> { impl<N: BaseFloat + ApproxEq<N> + Clone> Inv for Quat<N> {
#[inline] #[inline]
fn inv_cpy(m: &Quat<N>) -> Option<Quat<N>> { fn inv_cpy(&self) -> Option<Quat<N>> {
let mut res = m.clone(); let mut res = self.clone();
if res.inv() { if res.inv() {
Some(res) Some(res)
} }
@ -98,14 +97,14 @@ impl<N: BaseFloat + ApproxEq<N> + Clone> Inv for Quat<N> {
impl<N: BaseFloat> Norm<N> for Quat<N> { impl<N: BaseFloat> Norm<N> for Quat<N> {
#[inline] #[inline]
fn sqnorm(q: &Quat<N>) -> N { fn sqnorm(&self) -> N {
q.w * q.w + q.i * q.i + q.j * q.j + q.k * q.k self.w * self.w + self.i * self.i + self.j * self.j + self.k * self.k
} }
#[inline] #[inline]
fn normalize_cpy(v: &Quat<N>) -> Quat<N> { fn normalize_cpy(&self) -> Quat<N> {
let n = Norm::norm(v); let n = self.norm();
Quat::new(v.w / n, v.i / n, v.j / n, v.k / n) Quat::new(self.w / n, self.i / n, self.j / n, self.k / n)
} }
#[inline] #[inline]
@ -261,9 +260,8 @@ impl<N: BaseNum + Clone> One for UnitQuat<N> {
impl<N: Clone + Neg<N>> Inv for UnitQuat<N> { impl<N: Clone + Neg<N>> Inv for UnitQuat<N> {
#[inline] #[inline]
fn inv_cpy(m: &UnitQuat<N>) -> Option<UnitQuat<N>> { fn inv_cpy(&self) -> Option<UnitQuat<N>> {
let mut cpy = m.clone(); let mut cpy = self.clone();
cpy.inv(); cpy.inv();
Some(cpy) Some(cpy)
} }
@ -290,13 +288,8 @@ impl<N: ApproxEq<N>> ApproxEq<N> for UnitQuat<N> {
} }
#[inline] #[inline]
fn approx_eq(a: &UnitQuat<N>, b: &UnitQuat<N>) -> bool { fn approx_eq_eps(&self, other: &UnitQuat<N>, eps: &N) -> bool {
ApproxEq::approx_eq(&a.q, &b.q) ApproxEq::approx_eq_eps(&self.q, &other.q, eps)
}
#[inline]
fn approx_eq_eps(a: &UnitQuat<N>, b: &UnitQuat<N>, eps: &N) -> bool {
ApproxEq::approx_eq_eps(&a.q, &b.q, eps)
} }
} }
@ -379,8 +372,8 @@ impl<N: BaseFloat + Clone> Rotation<Vec3<N>> for UnitQuat<N> {
} }
#[inline] #[inline]
fn append_rotation_cpy(t: &UnitQuat<N>, amount: &Vec3<N>) -> UnitQuat<N> { fn append_rotation_cpy(&self, amount: &Vec3<N>) -> UnitQuat<N> {
*t * UnitQuat::new(amount.clone()) *self * UnitQuat::new(amount.clone())
} }
#[inline] #[inline]
@ -389,8 +382,8 @@ impl<N: BaseFloat + Clone> Rotation<Vec3<N>> for UnitQuat<N> {
} }
#[inline] #[inline]
fn prepend_rotation_cpy(t: &UnitQuat<N>, amount: &Vec3<N>) -> UnitQuat<N> { fn prepend_rotation_cpy(&self, amount: &Vec3<N>) -> UnitQuat<N> {
UnitQuat::new(amount.clone()) * *t UnitQuat::new(amount.clone()) * *self
} }
#[inline] #[inline]

View File

@ -46,8 +46,8 @@ impl<N: BaseFloat + Clone> Rotation<Vec1<N>> for Rot2<N> {
} }
#[inline] #[inline]
fn append_rotation_cpy(t: &Rot2<N>, rot: &Vec1<N>) -> Rot2<N> { fn append_rotation_cpy(&self, rot: &Vec1<N>) -> Rot2<N> {
Rot2::new(rot.clone()) * *t Rot2::new(rot.clone()) * *self
} }
#[inline] #[inline]
@ -56,8 +56,8 @@ impl<N: BaseFloat + Clone> Rotation<Vec1<N>> for Rot2<N> {
} }
#[inline] #[inline]
fn prepend_rotation_cpy(t: &Rot2<N>, rot: &Vec1<N>) -> Rot2<N> { fn prepend_rotation_cpy(&self, rot: &Vec1<N>) -> Rot2<N> {
*t * Rot2::new(rot.clone()) *self * Rot2::new(rot.clone())
} }
#[inline] #[inline]
@ -249,8 +249,8 @@ Rotation<Vec3<N>> for Rot3<N> {
} }
#[inline] #[inline]
fn append_rotation_cpy(t: &Rot3<N>, axisangle: &Vec3<N>) -> Rot3<N> { fn append_rotation_cpy(&self, axisangle: &Vec3<N>) -> Rot3<N> {
Rot3::new(axisangle.clone()) * *t Rot3::new(axisangle.clone()) * *self
} }
#[inline] #[inline]
@ -259,8 +259,8 @@ Rotation<Vec3<N>> for Rot3<N> {
} }
#[inline] #[inline]
fn prepend_rotation_cpy(t: &Rot3<N>, axisangle: &Vec3<N>) -> Rot3<N> { fn prepend_rotation_cpy(&self, axisangle: &Vec3<N>) -> Rot3<N> {
*t * Rot3::new(axisangle.clone()) *self * Rot3::new(axisangle.clone())
} }
#[inline] #[inline]
@ -351,7 +351,7 @@ Rotation<Vec4<N>> for Rot4<N> {
} }
#[inline] #[inline]
fn append_rotation_cpy(_: &Rot4<N>, _: &Vec4<N>) -> Rot4<N> { fn append_rotation_cpy(&self, _: &Vec4<N>) -> Rot4<N> {
panic!("Not yet implemented") panic!("Not yet implemented")
} }
@ -361,7 +361,7 @@ Rotation<Vec4<N>> for Rot4<N> {
} }
#[inline] #[inline]
fn prepend_rotation_cpy(_: &Rot4<N>, _: &Vec4<N>) -> Rot4<N> { fn prepend_rotation_cpy(&self, _: &Vec4<N>) -> Rot4<N> {
panic!("Not yet implemented") panic!("Not yet implemented")
} }

View File

@ -157,9 +157,9 @@ macro_rules! inv_impl(
} }
#[inline] #[inline]
fn inv_cpy(m: &$t<N>) -> Option<$t<N>> { fn inv_cpy(&self) -> Option<$t<N>> {
// always succeed // always succeed
Some(Transpose::transpose_cpy(m)) Some(self.transpose_cpy())
} }
} }
) )
@ -169,8 +169,8 @@ macro_rules! transpose_impl(
($t: ident) => ( ($t: ident) => (
impl<N: Clone> Transpose for $t<N> { impl<N: Clone> Transpose for $t<N> {
#[inline] #[inline]
fn transpose_cpy(m: &$t<N>) -> $t<N> { fn transpose_cpy(&self) -> $t<N> {
$t { submat: Transpose::transpose_cpy(&m.submat) } $t { submat: Transpose::transpose_cpy(&self.submat) }
} }
#[inline] #[inline]
@ -257,13 +257,13 @@ macro_rules! approx_eq_impl(
} }
#[inline] #[inline]
fn approx_eq(a: &$t<N>, b: &$t<N>) -> bool { fn approx_eq(&self, other: &$t<N>) -> bool {
ApproxEq::approx_eq(&a.submat, &b.submat) ApproxEq::approx_eq(&self.submat, &other.submat)
} }
#[inline] #[inline]
fn approx_eq_eps(a: &$t<N>, b: &$t<N>, epsilon: &N) -> bool { fn approx_eq_eps(&self, other: &$t<N>, epsilon: &N) -> bool {
ApproxEq::approx_eq_eps(&a.submat, &b.submat, epsilon) ApproxEq::approx_eq_eps(&self.submat, &other.submat, epsilon)
} }
} }
) )

View File

@ -12,7 +12,7 @@ impl One for mat::Identity {
} }
impl Inv for mat::Identity { impl Inv for mat::Identity {
fn inv_cpy(_: &mat::Identity) -> Option<mat::Identity> { fn inv_cpy(&self) -> Option<mat::Identity> {
Some(mat::Identity::new()) Some(mat::Identity::new())
} }
@ -30,7 +30,7 @@ impl<T: Clone> Mul<T, T> for mat::Identity {
impl Transpose for mat::Identity { impl Transpose for mat::Identity {
#[inline] #[inline]
fn transpose_cpy(_: &mat::Identity) -> mat::Identity { fn transpose_cpy(&self) -> mat::Identity {
mat::Identity::new() mat::Identity::new()
} }
@ -56,7 +56,7 @@ impl<V: Zero> Translation<V> for mat::Identity {
} }
#[inline] #[inline]
fn append_translation_cpy(_: &mat::Identity, _: &V) -> mat::Identity { fn append_translation_cpy(&self, _: &V) -> mat::Identity {
panic!("Attempted to translate the identity matrix.") panic!("Attempted to translate the identity matrix.")
} }
@ -66,7 +66,7 @@ impl<V: Zero> Translation<V> for mat::Identity {
} }
#[inline] #[inline]
fn prepend_translation_cpy(_: &mat::Identity, _: &V) -> mat::Identity { fn prepend_translation_cpy(&self, _: &V) -> mat::Identity {
panic!("Attempted to translate the identity matrix.") panic!("Attempted to translate the identity matrix.")
} }
@ -105,7 +105,7 @@ impl<V: Zero> Rotation<V> for mat::Identity {
} }
#[inline] #[inline]
fn append_rotation_cpy(_: &mat::Identity, _: &V) -> mat::Identity { fn append_rotation_cpy(&self, _: &V) -> mat::Identity {
panic!("Attempted to rotate the identity matrix.") panic!("Attempted to rotate the identity matrix.")
} }
@ -115,7 +115,7 @@ impl<V: Zero> Rotation<V> for mat::Identity {
} }
#[inline] #[inline]
fn prepend_rotation_cpy(_: &mat::Identity, _: &V) -> mat::Identity { fn prepend_rotation_cpy(&self, _: &V) -> mat::Identity {
panic!("Attempted to rotate the identity matrix.") panic!("Attempted to rotate the identity matrix.")
} }
@ -161,7 +161,7 @@ impl<M: One> Transformation<M> for mat::Identity {
} }
#[inline] #[inline]
fn append_transformation_cpy(_: &mat::Identity, _: &M) -> mat::Identity { fn append_transformation_cpy(&self, _: &M) -> mat::Identity {
panic!("Attempted to transform the identity matrix.") panic!("Attempted to transform the identity matrix.")
} }
@ -171,7 +171,7 @@ impl<M: One> Transformation<M> for mat::Identity {
} }
#[inline] #[inline]
fn prepend_transformation_cpy(_: &mat::Identity, _: &M) -> mat::Identity { fn prepend_transformation_cpy(&self, _: &M) -> mat::Identity {
panic!("Attempted to transform the identity matrix.") panic!("Attempted to transform the identity matrix.")
} }

View File

@ -7,9 +7,8 @@ use traits::structure::{Row, Col, BaseNum};
// some specializations: // some specializations:
impl<N: BaseNum + ApproxEq<N> + Clone> Inv for Mat1<N> { impl<N: BaseNum + ApproxEq<N> + Clone> Inv for Mat1<N> {
#[inline] #[inline]
fn inv_cpy(m: &Mat1<N>) -> Option<Mat1<N>> { fn inv_cpy(&self) -> Option<Mat1<N>> {
let mut res = m.clone(); let mut res = self.clone();
if res.inv() { if res.inv() {
Some(res) Some(res)
} }
@ -34,9 +33,8 @@ impl<N: BaseNum + ApproxEq<N> + Clone> Inv for Mat1<N> {
impl<N: BaseNum + ApproxEq<N> + Clone> Inv for Mat2<N> { impl<N: BaseNum + ApproxEq<N> + Clone> Inv for Mat2<N> {
#[inline] #[inline]
fn inv_cpy(m: &Mat2<N>) -> Option<Mat2<N>> { fn inv_cpy(&self) -> Option<Mat2<N>> {
let mut res = m.clone(); let mut res = self.clone();
if res.inv() { if res.inv() {
Some(res) Some(res)
} }
@ -64,9 +62,8 @@ impl<N: BaseNum + ApproxEq<N> + Clone> Inv for Mat2<N> {
impl<N: BaseNum + ApproxEq<N> + Clone> Inv for Mat3<N> { impl<N: BaseNum + ApproxEq<N> + Clone> Inv for Mat3<N> {
#[inline] #[inline]
fn inv_cpy(m: &Mat3<N>) -> Option<Mat3<N>> { fn inv_cpy(&self) -> Option<Mat3<N>> {
let mut res = m.clone(); let mut res = self.clone();
if res.inv() { if res.inv() {
Some(res) Some(res)
} }
@ -108,26 +105,26 @@ impl<N: BaseNum + ApproxEq<N> + Clone> Inv for Mat3<N> {
impl<N: BaseNum + Clone> Det<N> for Mat1<N> { impl<N: BaseNum + Clone> Det<N> for Mat1<N> {
#[inline] #[inline]
fn det(m: &Mat1<N>) -> N { fn det(&self) -> N {
m.m11.clone() self.m11.clone()
} }
} }
impl<N: BaseNum> Det<N> for Mat2<N> { impl<N: BaseNum> Det<N> for Mat2<N> {
#[inline] #[inline]
fn det(m: &Mat2<N>) -> N { fn det(&self) -> N {
m.m11 * m.m22 - m.m21 * m.m12 self.m11 * self.m22 - self.m21 * self.m12
} }
} }
impl<N: BaseNum> Det<N> for Mat3<N> { impl<N: BaseNum> Det<N> for Mat3<N> {
#[inline] #[inline]
fn det(m: &Mat3<N>) -> N { fn det(&self) -> N {
let minor_m12_m23 = m.m22 * m.m33 - m.m32 * m.m23; let minor_m12_m23 = self.m22 * self.m33 - self.m32 * self.m23;
let minor_m11_m23 = m.m21 * m.m33 - m.m31 * m.m23; let minor_m11_m23 = self.m21 * self.m33 - self.m31 * self.m23;
let minor_m11_m22 = m.m21 * m.m32 - m.m31 * m.m22; let minor_m11_m22 = self.m21 * self.m32 - self.m31 * self.m22;
m.m11 * minor_m12_m23 - m.m12 * minor_m11_m23 + m.m13 * minor_m11_m22 self.m11 * minor_m12_m23 - self.m12 * minor_m11_m23 + self.m13 * minor_m11_m22
} }
} }

View File

@ -5,37 +5,37 @@ use structs::mat::Mat3;
impl<N: Mul<N, N> + Sub<N, N>> Cross<Vec1<N>> for Vec2<N> { impl<N: Mul<N, N> + Sub<N, N>> Cross<Vec1<N>> for Vec2<N> {
#[inline] #[inline]
fn cross(a: &Vec2<N>, b: &Vec2<N>) -> Vec1<N> { fn cross(&self, other: &Vec2<N>) -> Vec1<N> {
Vec1::new(a.x * b.y - a.y * b.x) Vec1::new(self.x * other.y - self.y * other.x)
} }
} }
// FIXME: instead of returning a Vec2, define a Mat2x1 matrix? // FIXME: instead of returning a Vec2, define a Mat2x1 matrix?
impl<N: Neg<N> + Clone> CrossMatrix<Vec2<N>> for Vec2<N> { impl<N: Neg<N> + Clone> CrossMatrix<Vec2<N>> for Vec2<N> {
#[inline] #[inline]
fn cross_matrix(v: &Vec2<N>) -> Vec2<N> { fn cross_matrix(&self) -> Vec2<N> {
Vec2::new(-v.y, v.x.clone()) Vec2::new(-self.y, self.x.clone())
} }
} }
impl<N: Mul<N, N> + Sub<N, N>> Cross<Vec3<N>> for Vec3<N> { impl<N: Mul<N, N> + Sub<N, N>> Cross<Vec3<N>> for Vec3<N> {
#[inline] #[inline]
fn cross(a: &Vec3<N>, b: &Vec3<N>) -> Vec3<N> { fn cross(&self, other: &Vec3<N>) -> Vec3<N> {
Vec3::new( Vec3::new(
a.y * b.z - a.z * b.y, self.y * other.z - self.z * other.y,
a.z * b.x - a.x * b.z, self.z * other.x - self.x * other.z,
a.x * b.y - a.y * b.x self.x * other.y - self.y * other.x
) )
} }
} }
impl<N: Neg<N> + Zero + Clone> CrossMatrix<Mat3<N>> for Vec3<N> { impl<N: Neg<N> + Zero + Clone> CrossMatrix<Mat3<N>> for Vec3<N> {
#[inline] #[inline]
fn cross_matrix(v: &Vec3<N>) -> Mat3<N> { fn cross_matrix(&self) -> Mat3<N> {
Mat3::new( Mat3::new(
::zero(), -v.z , v.y.clone(), ::zero(), -self.z, self.y.clone(),
v.z.clone() , ::zero(), -v.x, self.z.clone(), ::zero(), -self.x,
-v.y , v.x.clone() , ::zero() -self.y, self.x.clone(), ::zero()
) )
} }
} }

View File

@ -121,7 +121,7 @@ impl<N: Neg<N>> Neg<vec::Vec0<N>> for vec::Vec0<N> {
impl<N: BaseNum> Dot<N> for vec::Vec0<N> { impl<N: BaseNum> Dot<N> for vec::Vec0<N> {
#[inline] #[inline]
fn dot(_: &vec::Vec0<N>, _: &vec::Vec0<N>) -> N { fn dot(&self, _: &vec::Vec0<N>) -> N {
::zero() ::zero()
} }
} }
@ -157,8 +157,8 @@ impl<N: Clone + Add<N, N> + Neg<N>> Translation<vec::Vec0<N>> for vec::Vec0<N> {
} }
#[inline] #[inline]
fn append_translation_cpy(vec: &vec::Vec0<N>, t: &vec::Vec0<N>) -> vec::Vec0<N> { fn append_translation_cpy(&self, t: &vec::Vec0<N>) -> vec::Vec0<N> {
*t + vec *t + self
} }
#[inline] #[inline]
@ -167,8 +167,8 @@ impl<N: Clone + Add<N, N> + Neg<N>> Translation<vec::Vec0<N>> for vec::Vec0<N> {
} }
#[inline] #[inline]
fn prepend_translation_cpy(vec: &vec::Vec0<N>, t: &vec::Vec0<N>) -> vec::Vec0<N> { fn prepend_translation_cpy(&self, t: &vec::Vec0<N>) -> vec::Vec0<N> {
*vec + *t *self + *t
} }
#[inline] #[inline]
@ -178,17 +178,17 @@ impl<N: Clone + Add<N, N> + Neg<N>> Translation<vec::Vec0<N>> for vec::Vec0<N> {
impl<N: BaseFloat> Norm<N> for vec::Vec0<N> { impl<N: BaseFloat> Norm<N> for vec::Vec0<N> {
#[inline] #[inline]
fn sqnorm(_: &vec::Vec0<N>) -> N { fn sqnorm(&self) -> N {
::zero() ::zero()
} }
#[inline] #[inline]
fn norm(_: &vec::Vec0<N>) -> N { fn norm(&self) -> N {
::zero() ::zero()
} }
#[inline] #[inline]
fn normalize_cpy(_: &vec::Vec0<N>) -> vec::Vec0<N> { fn normalize_cpy(&self) -> vec::Vec0<N> {
::zero() ::zero()
} }
@ -205,12 +205,7 @@ impl<N: ApproxEq<N>> ApproxEq<N> for vec::Vec0<N> {
} }
#[inline] #[inline]
fn approx_eq(_: &vec::Vec0<N>, _: &vec::Vec0<N>) -> bool { fn approx_eq_eps(&self, _: &vec::Vec0<N>, _: &N) -> bool {
true
}
#[inline]
fn approx_eq_eps(_: &vec::Vec0<N>, _: &vec::Vec0<N>, _: &N) -> bool {
true true
} }
} }

View File

@ -82,26 +82,26 @@ macro_rules! ord_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => ( ($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: BaseFloat + Clone> POrd for $t<N> { impl<N: BaseFloat + Clone> POrd for $t<N> {
#[inline] #[inline]
fn inf(a: &$t<N>, b: &$t<N>) -> $t<N> { fn inf(&self, other: &$t<N>) -> $t<N> {
$t::new(a.$comp0.min(b.$comp0.clone()) $t::new(self.$comp0.min(other.$comp0.clone())
$(, a.$compN.min(b.$compN))*) $(, self.$compN.min(other.$compN))*)
} }
#[inline] #[inline]
fn sup(a: &$t<N>, b: &$t<N>) -> $t<N> { fn sup(&self, other: &$t<N>) -> $t<N> {
$t::new(a.$comp0.max(b.$comp0.clone()) $t::new(self.$comp0.max(other.$comp0.clone())
$(, a.$compN.max(b.$compN.clone()))*) $(, self.$compN.max(other.$compN.clone()))*)
} }
#[inline] #[inline]
#[allow(unused_mut)] // otherwise there will be a warning for is_eq or Vec1. #[allow(unused_mut)] // otherwise there will be a warning for is_eq or Vec1.
fn partial_cmp(a: &$t<N>, b: &$t<N>) -> POrdering { fn partial_cmp(&self, other: &$t<N>) -> POrdering {
let is_lt = a.$comp0 < b.$comp0; let is_lt = self.$comp0 < other.$comp0;
let mut is_eq = a.$comp0 == b.$comp0; let mut is_eq = self.$comp0 == other.$comp0;
if is_lt { // < if is_lt { // <
$( $(
if a.$compN > b.$compN { if self.$compN > other.$compN {
return POrdering::NotComparable return POrdering::NotComparable
} }
)* )*
@ -110,10 +110,10 @@ macro_rules! ord_impl(
} }
else { // >= else { // >=
$( $(
if a.$compN < b.$compN { if self.$compN < other.$compN {
return POrdering::NotComparable return POrdering::NotComparable
} }
else if a.$compN > b.$compN { else if self.$compN > other.$compN {
is_eq = false; is_eq = false;
} }
@ -129,23 +129,23 @@ macro_rules! ord_impl(
} }
#[inline] #[inline]
fn partial_lt(a: &$t<N>, b: &$t<N>) -> bool { fn partial_lt(&self, other: &$t<N>) -> bool {
a.$comp0 < b.$comp0 $(&& a.$compN < b.$compN)* self.$comp0 < other.$comp0 $(&& self.$compN < other.$compN)*
} }
#[inline] #[inline]
fn partial_le(a: &$t<N>, b: &$t<N>) -> bool { fn partial_le(&self, other: &$t<N>) -> bool {
a.$comp0 <= b.$comp0 $(&& a.$compN <= b.$compN)* self.$comp0 <= other.$comp0 $(&& self.$compN <= other.$compN)*
} }
#[inline] #[inline]
fn partial_gt(a: &$t<N>, b: &$t<N>) -> bool { fn partial_gt(&self, other: &$t<N>) -> bool {
a.$comp0 > b.$comp0 $(&& a.$compN > b.$compN)* self.$comp0 > other.$comp0 $(&& self.$compN > other.$compN)*
} }
#[inline] #[inline]
fn partial_ge(a: &$t<N>, b: &$t<N>) -> bool { fn partial_ge(&self, other: &$t<N>) -> bool {
a.$comp0 >= b.$comp0 $(&& a.$compN >= b.$compN)* self.$comp0 >= other.$comp0 $(&& self.$compN >= other.$compN)*
} }
} }
) )
@ -493,8 +493,8 @@ macro_rules! dot_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => ( ($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: BaseNum> Dot<N> for $t<N> { impl<N: BaseNum> Dot<N> for $t<N> {
#[inline] #[inline]
fn dot(a: &$t<N>, b: &$t<N>) -> N { fn dot(&self, other: &$t<N>) -> N {
a.$comp0 * b.$comp0 $(+ a.$compN * b.$compN )* self.$comp0 * other.$comp0 $(+ self.$compN * other.$compN )*
} }
} }
) )
@ -595,8 +595,8 @@ macro_rules! translation_impl(
} }
#[inline] #[inline]
fn append_translation_cpy(transform: &$t<N>, t: &$t<N>) -> $t<N> { fn append_translation_cpy(&self, t: &$t<N>) -> $t<N> {
*t + *transform *t + *self
} }
#[inline] #[inline]
@ -605,8 +605,8 @@ macro_rules! translation_impl(
} }
#[inline] #[inline]
fn prepend_translation_cpy(transform: &$t<N>, t: &$t<N>) -> $t<N> { fn prepend_translation_cpy(&self, t: &$t<N>) -> $t<N> {
*transform + *t *self + *t
} }
#[inline] #[inline]
@ -621,21 +621,14 @@ macro_rules! norm_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => ( ($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Clone + BaseFloat> Norm<N> for $t<N> { impl<N: Clone + BaseFloat> Norm<N> for $t<N> {
#[inline] #[inline]
fn sqnorm(v: &$t<N>) -> N { fn sqnorm(&self) -> N {
Dot::dot(v, v) Dot::dot(self, self)
} }
#[inline] #[inline]
fn norm(v: &$t<N>) -> N { fn normalize_cpy(&self) -> $t<N> {
Norm::sqnorm(v).sqrt() let mut res : $t<N> = self.clone();
}
#[inline]
fn normalize_cpy(v: &$t<N>) -> $t<N> {
let mut res : $t<N> = v.clone();
let _ = res.normalize(); let _ = res.normalize();
res res
} }
@ -661,15 +654,15 @@ macro_rules! approx_eq_impl(
} }
#[inline] #[inline]
fn approx_eq(a: &$t<N>, b: &$t<N>) -> bool { fn approx_eq(&self, other: &$t<N>) -> bool {
ApproxEq::approx_eq(&a.$comp0, &b.$comp0) ApproxEq::approx_eq(&self.$comp0, &other.$comp0)
$(&& ApproxEq::approx_eq(&a.$compN, &b.$compN))* $(&& ApproxEq::approx_eq(&self.$compN, &other.$compN))*
} }
#[inline] #[inline]
fn approx_eq_eps(a: &$t<N>, b: &$t<N>, eps: &N) -> bool { fn approx_eq_eps(&self, other: &$t<N>, eps: &N) -> bool {
ApproxEq::approx_eq_eps(&a.$comp0, &b.$comp0, eps) ApproxEq::approx_eq_eps(&self.$comp0, &other.$comp0, eps)
$(&& ApproxEq::approx_eq_eps(&a.$compN, &b.$compN, eps))* $(&& ApproxEq::approx_eq_eps(&self.$compN, &other.$compN, eps))*
} }
} }
) )

View File

@ -17,13 +17,13 @@ pub trait Translation<V> {
fn append_translation(&mut self, &V); fn append_translation(&mut self, &V);
/// Appends the translation `amount` to a copy of `t`. /// Appends the translation `amount` to a copy of `t`.
fn append_translation_cpy(t: &Self, amount: &V) -> Self; fn append_translation_cpy(&self, amount: &V) -> Self;
/// Prepends a translation to this object. /// Prepends a translation to this object.
fn prepend_translation(&mut self, &V); fn prepend_translation(&mut self, &V);
/// Prepends the translation `amount` to a copy of `t`. /// Prepends the translation `amount` to a copy of `t`.
fn prepend_translation_cpy(t: &Self, amount: &V) -> Self; fn prepend_translation_cpy(&self, amount: &V) -> Self;
/// Sets the translation. /// Sets the translation.
fn set_translation(&mut self, V); fn set_translation(&mut self, V);
@ -52,13 +52,13 @@ pub trait Rotation<V> {
fn append_rotation(&mut self, &V); fn append_rotation(&mut self, &V);
/// Appends the rotation `amount` to a copy of `t`. /// Appends the rotation `amount` to a copy of `t`.
fn append_rotation_cpy(t: &Self, amount: &V) -> Self; fn append_rotation_cpy(&self, amount: &V) -> Self;
/// Prepends a rotation to this object. /// Prepends a rotation to this object.
fn prepend_rotation(&mut self, &V); fn prepend_rotation(&mut self, &V);
/// Prepends the rotation `amount` to a copy of `t`. /// Prepends the rotation `amount` to a copy of `t`.
fn prepend_rotation_cpy(t: &Self, amount: &V) -> Self; fn prepend_rotation_cpy(&self, amount: &V) -> Self;
/// Sets the rotation of `self`. /// Sets the rotation of `self`.
fn set_rotation(&mut self, V); fn set_rotation(&mut self, V);
@ -90,8 +90,8 @@ pub trait RotationWithTranslation<LV: Neg<LV>, AV>: Rotation<AV> + Translation<L
/// * `amount` - the rotation to apply. /// * `amount` - the rotation to apply.
/// * `point` - the center of rotation. /// * `point` - the center of rotation.
#[inline] #[inline]
fn append_rotation_wrt_point_cpy(t: &Self, amount: &AV, center: &LV) -> Self { fn append_rotation_wrt_point_cpy(&self, amount: &AV, center: &LV) -> Self {
let mut res = Translation::append_translation_cpy(t, &-*center); let mut res = Translation::append_translation_cpy(self, &-*center);
res.append_rotation(amount); res.append_rotation(amount);
res.append_translation(center); res.append_translation(center);
@ -119,8 +119,8 @@ pub trait RotationWithTranslation<LV: Neg<LV>, AV>: Rotation<AV> + Translation<L
/// * `t` - the object to be rotated. /// * `t` - the object to be rotated.
/// * `amount` - the rotation to apply. /// * `amount` - the rotation to apply.
#[inline] #[inline]
fn append_rotation_wrt_center_cpy(t: &Self, amount: &AV) -> Self { fn append_rotation_wrt_center_cpy(&self, amount: &AV) -> Self {
RotationWithTranslation::append_rotation_wrt_point_cpy(t, amount, &t.translation()) RotationWithTranslation::append_rotation_wrt_point_cpy(self, amount, &self.translation())
} }
/// Applies a rotation centered on the translation of `m`. /// Applies a rotation centered on the translation of `m`.
@ -174,13 +174,13 @@ pub trait Transformation<M> {
fn append_transformation(&mut self, &M); fn append_transformation(&mut self, &M);
/// Appends the transformation `amount` to a copy of `t`. /// Appends the transformation `amount` to a copy of `t`.
fn append_transformation_cpy(t: &Self, amount: &M) -> Self; fn append_transformation_cpy(&self, amount: &M) -> Self;
/// Prepends a transformation to this object. /// Prepends a transformation to this object.
fn prepend_transformation(&mut self, &M); fn prepend_transformation(&mut self, &M);
/// Prepends the transformation `amount` to a copy of `t`. /// Prepends the transformation `amount` to a copy of `t`.
fn prepend_transformation_cpy(t: &Self, amount: &M) -> Self; fn prepend_transformation_cpy(&self, amount: &M) -> Self;
/// Sets the transformation of `self`. /// Sets the transformation of `self`.
fn set_transformation(&mut self, M); fn set_transformation(&mut self, M);
@ -201,24 +201,24 @@ pub trait Transform<V> {
pub trait Dot<N> { pub trait Dot<N> {
/// Computes the dot (inner) product of two vectors. /// Computes the dot (inner) product of two vectors.
#[inline] #[inline]
fn dot(&Self, &Self) -> N; fn dot(&self, other: &Self) -> N;
} }
/// Traits of objects having an euclidian norm. /// Traits of objects having an euclidian norm.
pub trait Norm<N: BaseFloat> { pub trait Norm<N: BaseFloat> {
/// Computes the norm of `self`. /// Computes the norm of `self`.
#[inline] #[inline]
fn norm(v: &Self) -> N { fn norm(&self) -> N {
Norm::sqnorm(v).sqrt() self.sqnorm().sqrt()
} }
/// Computes the squared norm of `self`. /// Computes the squared norm of `self`.
/// ///
/// This is usually faster than computing the norm itself. /// This is usually faster than computing the norm itself.
fn sqnorm(&Self) -> N; fn sqnorm(&self) -> N;
/// Gets the normalized version of a copy of `v`. /// Gets the normalized version of a copy of `v`.
fn normalize_cpy(v: &Self) -> Self; fn normalize_cpy(&self) -> Self;
/// Normalizes `self`. /// Normalizes `self`.
fn normalize(&mut self) -> N; fn normalize(&mut self) -> N;
@ -229,7 +229,7 @@ pub trait Norm<N: BaseFloat> {
*/ */
pub trait Cross<V> { pub trait Cross<V> {
/// Computes the cross product between two elements (usually vectors). /// Computes the cross product between two elements (usually vectors).
fn cross(&Self, other: &Self) -> V; fn cross(&self, other: &Self) -> V;
} }
/** /**
@ -238,7 +238,7 @@ pub trait Cross<V> {
pub trait CrossMatrix<M> { pub trait CrossMatrix<M> {
/// The matrix associated to any cross product with this vector. I.e. `v.cross(anything)` = /// The matrix associated to any cross product with this vector. I.e. `v.cross(anything)` =
/// `v.cross_matrix().rmul(anything)`. /// `v.cross_matrix().rmul(anything)`.
fn cross_matrix(&Self) -> M; fn cross_matrix(&self) -> M;
} }
/// Traits of objects which can be put in homogeneous coordinates form. /// Traits of objects which can be put in homogeneous coordinates form.

View File

@ -71,55 +71,55 @@ impl POrdering {
/// Pointwise ordering operations. /// Pointwise ordering operations.
pub trait POrd { pub trait POrd {
/// Returns the infimum of `a` and `b`. /// Returns the infimum of this value and another
fn inf(a: &Self, b: &Self) -> Self; fn inf(&self, other: &Self) -> Self;
/// Returns the supremum of `a` and `b`. /// Returns the supremum of this value and another
fn sup(a: &Self, b: &Self) -> Self; fn sup(&self, other: &Self) -> Self;
/// Compare `a` and `b` using a partial ordering relation. /// Compare `self` and `other` using a partial ordering relation.
fn partial_cmp(a: &Self, b: &Self) -> POrdering; fn partial_cmp(&self, other: &Self) -> POrdering;
/// Returns `true` iff `a` and `b` are comparable and `a <= b`. /// Returns `true` iff `self` and `other` are comparable and `self <= other`.
#[inline] #[inline]
fn partial_le(a: &Self, b: &Self) -> bool { fn partial_le(&self, other: &Self) -> bool {
POrd::partial_cmp(a, b).is_le() POrd::partial_cmp(self, other).is_le()
} }
/// Returns `true` iff `a` and `b` are comparable and `a < b`. /// Returns `true` iff `self` and `other` are comparable and `self < other`.
#[inline] #[inline]
fn partial_lt(a: &Self, b: &Self) -> bool { fn partial_lt(&self, other: &Self) -> bool {
POrd::partial_cmp(a, b).is_lt() POrd::partial_cmp(self, other).is_lt()
} }
/// Returns `true` iff `a` and `b` are comparable and `a >= b`. /// Returns `true` iff `self` and `other` are comparable and `self >= other`.
#[inline] #[inline]
fn partial_ge(a: &Self, b: &Self) -> bool { fn partial_ge(&self, other: &Self) -> bool {
POrd::partial_cmp(a, b).is_ge() POrd::partial_cmp(self, other).is_ge()
} }
/// Returns `true` iff `a` and `b` are comparable and `a > b`. /// Returns `true` iff `self` and `other` are comparable and `self > other`.
#[inline] #[inline]
fn partial_gt(a: &Self, b: &Self) -> bool { fn partial_gt(&self, other: &Self) -> bool {
POrd::partial_cmp(a, b).is_gt() POrd::partial_cmp(self, other).is_gt()
} }
/// Return the minimum of `a` and `b` if they are comparable. /// Return the minimum of `self` and `other` if they are comparable.
#[inline] #[inline]
fn partial_min<'a>(a: &'a Self, b: &'a Self) -> Option<&'a Self> { fn partial_min<'a>(&'a self, other: &'a Self) -> Option<&'a Self> {
match POrd::partial_cmp(a, b) { match POrd::partial_cmp(self, other) {
POrdering::PartialLess | POrdering::PartialEqual => Some(a), POrdering::PartialLess | POrdering::PartialEqual => Some(self),
POrdering::PartialGreater => Some(b), POrdering::PartialGreater => Some(other),
POrdering::NotComparable => None POrdering::NotComparable => None
} }
} }
/// Return the maximum of `a` and `b` if they are comparable. /// Return the maximum of `self` and `other` if they are comparable.
#[inline] #[inline]
fn partial_max<'a>(a: &'a Self, b: &'a Self) -> Option<&'a Self> { fn partial_max<'a>(&'a self, other: &'a Self) -> Option<&'a Self> {
match POrd::partial_cmp(a, b) { match POrd::partial_cmp(self, other) {
POrdering::PartialGreater | POrdering::PartialEqual => Some(a), POrdering::PartialGreater | POrdering::PartialEqual => Some(self),
POrdering::PartialLess => Some(b), POrdering::PartialLess => Some(other),
POrdering::NotComparable => None POrdering::NotComparable => None
} }
} }
@ -127,9 +127,9 @@ pub trait POrd {
/// Clamp `value` between `min` and `max`. Returns `None` if `value` is not comparable to /// Clamp `value` between `min` and `max`. Returns `None` if `value` is not comparable to
/// `min` or `max`. /// `min` or `max`.
#[inline] #[inline]
fn partial_clamp<'a>(value: &'a Self, min: &'a Self, max: &'a Self) -> Option<&'a Self> { fn partial_clamp<'a>(&'a self, min: &'a Self, max: &'a Self) -> Option<&'a Self> {
let v_min = POrd::partial_cmp(value, min); let v_min = self.partial_cmp(min);
let v_max = POrd::partial_cmp(value, max); let v_max = self.partial_cmp(max);
if v_min.is_not_comparable() || v_max.is_not_comparable() { if v_min.is_not_comparable() || v_max.is_not_comparable() {
None None
@ -142,7 +142,7 @@ pub trait POrd {
Some(max) Some(max)
} }
else { else {
Some(value) Some(self)
} }
} }
} }
@ -154,12 +154,12 @@ pub trait ApproxEq<Eps> {
fn approx_epsilon(unused_self: Option<Self>) -> Eps; fn approx_epsilon(unused_self: Option<Self>) -> Eps;
/// Tests approximate equality using a custom epsilon. /// Tests approximate equality using a custom epsilon.
fn approx_eq_eps(a: &Self, other: &Self, epsilon: &Eps) -> bool; fn approx_eq_eps(&self, other: &Self, epsilon: &Eps) -> bool;
/// Tests approximate equality. /// Tests approximate equality.
#[inline] #[inline]
fn approx_eq(a: &Self, b: &Self) -> bool { fn approx_eq(&self, other: &Self) -> bool {
ApproxEq::approx_eq_eps(a, b, &ApproxEq::approx_epsilon(None::<Self>)) self.approx_eq_eps(other, &ApproxEq::approx_epsilon(None::<Self>))
} }
} }
@ -170,8 +170,8 @@ impl ApproxEq<f32> for f32 {
} }
#[inline] #[inline]
fn approx_eq_eps(a: &f32, b: &f32, epsilon: &f32) -> bool { fn approx_eq_eps(&self, other: &f32, epsilon: &f32) -> bool {
::abs(&(*a - *b)) < *epsilon ::abs(&(*self - *other)) < *epsilon
} }
} }
@ -182,8 +182,8 @@ impl ApproxEq<f64> for f64 {
} }
#[inline] #[inline]
fn approx_eq_eps(a: &f64, b: &f64, approx_epsilon: &f64) -> bool { fn approx_eq_eps(&self, other: &f64, approx_epsilon: &f64) -> bool {
::abs(&(*a - *b)) < *approx_epsilon ::abs(&(*self - *other)) < *approx_epsilon
} }
} }
@ -198,7 +198,7 @@ pub trait Absolute<A> {
/// Trait of objects having an inverse. Typically used to implement matrix inverse. /// Trait of objects having an inverse. Typically used to implement matrix inverse.
pub trait Inv { pub trait Inv {
/// Returns the inverse of `m`. /// Returns the inverse of `m`.
fn inv_cpy(m: &Self) -> Option<Self>; fn inv_cpy(&self) -> Option<Self>;
/// In-place version of `inverse`. /// In-place version of `inverse`.
fn inv(&mut self) -> bool; fn inv(&mut self) -> bool;
@ -207,13 +207,13 @@ pub trait Inv {
/// Trait of objects having a determinant. Typically used by square matrices. /// Trait of objects having a determinant. Typically used by square matrices.
pub trait Det<N> { pub trait Det<N> {
/// Returns the determinant of `m`. /// Returns the determinant of `m`.
fn det(m: &Self) -> N; fn det(&self) -> N;
} }
/// Trait of objects which can be transposed. /// Trait of objects which can be transposed.
pub trait Transpose { pub trait Transpose {
/// Computes the transpose of a matrix. /// Computes the transpose of a matrix.
fn transpose_cpy(m: &Self) -> Self; fn transpose_cpy(&self) -> Self;
/// In-place version of `transposed`. /// In-place version of `transposed`.
fn transpose(&mut self); fn transpose(&mut self);
@ -222,7 +222,7 @@ pub trait Transpose {
/// Traits of objects having an outer product. /// Traits of objects having an outer product.
pub trait Outer<M> { pub trait Outer<M> {
/// Computes the outer product: `a * b` /// Computes the outer product: `a * b`
fn outer(a: &Self, b: &Self) -> M; fn outer(&self, other: &Self) -> M;
} }
/// Trait for computing the covariance of a set of data. /// Trait for computing the covariance of a set of data.
@ -231,14 +231,14 @@ pub trait Cov<M> {
/// ///
/// * For matrices, observations are stored in its rows. /// * For matrices, observations are stored in its rows.
/// * For vectors, observations are stored in its components (thus are 1-dimensional). /// * For vectors, observations are stored in its components (thus are 1-dimensional).
fn cov(m: &Self) -> M; fn cov(&self) -> M;
/// Computes the covariance of the obsevations stored by `m`: /// Computes the covariance of the obsevations stored by `m`:
/// ///
/// * For matrices, observations are stored in its rows. /// * For matrices, observations are stored in its rows.
/// * For vectors, observations are stored in its components (thus are 1-dimensional). /// * For vectors, observations are stored in its components (thus are 1-dimensional).
fn cov_to(m: &Self, out: &mut M) { fn cov_to(&self, out: &mut M) {
*out = Cov::cov(m) *out = self.cov()
} }
} }
@ -248,13 +248,13 @@ pub trait Mean<N> {
/// ///
/// * For matrices, observations are stored in its rows. /// * For matrices, observations are stored in its rows.
/// * For vectors, observations are stored in its components (thus are 1-dimensional). /// * For vectors, observations are stored in its components (thus are 1-dimensional).
fn mean(v: &Self) -> N; fn mean(&self) -> N;
} }
/// Trait for computing the eigenvector and eigenvalues of a square matrix usin the QR algorithm. /// Trait for computing the eigenvector and eigenvalues of a square matrix usin the QR algorithm.
pub trait EigenQR<N, V>: SquareMat<N, V> { pub trait EigenQR<N, V>: SquareMat<N, V> {
/// Computes the eigenvectors and eigenvalues of this matrix. /// Computes the eigenvectors and eigenvalues of this matrix.
fn eigen_qr(m: &Self, eps: &N, niter: uint) -> (Self, V); fn eigen_qr(&self, eps: &N, niter: uint) -> (Self, V);
} }
// XXX: those two traits should not exist since there is generalized operator overloading of Add // XXX: those two traits should not exist since there is generalized operator overloading of Add

View File

@ -270,14 +270,14 @@ pub trait NumPnt<N, V>:
pub trait FloatPnt<N: BaseFloat, V: Norm<N>>: NumPnt<N, V> { pub trait FloatPnt<N: BaseFloat, V: Norm<N>>: NumPnt<N, V> {
/// Computes the square distance between two points. /// Computes the square distance between two points.
#[inline] #[inline]
fn sqdist(a: &Self, b: &Self) -> N { fn sqdist(&self, other: &Self) -> N {
Norm::sqnorm(&(*a - *b)) (*self - *other).sqnorm()
} }
/// Computes the distance between two points. /// Computes the distance between two points.
#[inline] #[inline]
fn dist(a: &Self, b: &Self) -> N { fn dist(&self, other: &Self) -> N {
Norm::norm(&(*a - *b)) (*self - *other).norm()
} }
} }