Use our own traits instead of the deprecated ones.

We might switch to bjz/num-rs eventually (#32), but in the mean time we just re-declare the
deprecrated traits in nalgebra.
This commit is contained in:
Sébastien Crozet 2014-11-16 14:04:15 +01:00
parent 39b9d20717
commit 77348f668f
29 changed files with 444 additions and 475 deletions

View File

@ -108,18 +108,17 @@ extern crate serialize;
#[cfg(test)] #[cfg(test)]
extern crate test; extern crate test;
use std::num::{Zero, One};
use std::cmp; use std::cmp;
pub use traits::{PartialLess, PartialEqual, PartialGreater, NotComparable}; pub use traits::{PartialLess, PartialEqual, PartialGreater, NotComparable};
pub use traits::{ pub use traits::{
Absolute, Absolute,
AbsoluteRotate, AbsoluteRotate,
NumPnt,
NumVec,
ApproxEq, ApproxEq,
Axpy, Axpy,
Basis, Basis,
BaseFloat, BaseFloat,
BaseNum,
Bounded,
Cast, Cast,
Col, Col,
ColSlice, RowSlice, ColSlice, RowSlice,
@ -143,6 +142,9 @@ pub use traits::{
Mat, Mat,
Mean, Mean,
Norm, Norm,
NumPnt,
NumVec,
One,
Orig, Orig,
Outer, Outer,
POrd, POrd,
@ -160,7 +162,8 @@ pub use traits::{
Translate, Translation, Translate, Translation,
Transpose, Transpose,
UniformSphereSample, UniformSphereSample,
VecAsPnt VecAsPnt,
Zero
}; };
pub use structs::{ pub use structs::{
@ -336,6 +339,12 @@ pub fn zero<T: Zero>() -> T {
Zero::zero() Zero::zero()
} }
/// Tests is a value is iqual to zero.
#[inline(always)]
pub fn is_zero<T: Zero>(val: &T) -> bool {
val.is_zero()
}
/// Create a one-valued value. /// Create a one-valued value.
/// ///
/// This is the same as `std::num::one()`. /// This is the same as `std::num::one()`.

View File

@ -1,4 +1,3 @@
use std::num::Zero;
use traits::operations::{Transpose, ApproxEq}; use traits::operations::{Transpose, ApproxEq};
use traits::structure::{ColSlice, Eye, Indexable, Diag, SquareMat, BaseFloat}; use traits::structure::{ColSlice, Eye, Indexable, Diag, SquareMat, BaseFloat};
use traits::geometry::Norm; use traits::geometry::Norm;
@ -52,7 +51,7 @@ pub fn qr<N, V, M>(m: &M) -> (M, M)
for ite in range(0u, iterations) { for ite in range(0u, iterations) {
let mut v = r.col_slice(ite, ite, rows); let mut v = r.col_slice(ite, ite, rows);
let alpha = let alpha =
if unsafe { v.unsafe_at(ite) } >= Zero::zero() { if unsafe { v.unsafe_at(ite) } >= ::zero() {
-Norm::norm(&v) -Norm::norm(&v)
} }
else { else {
@ -62,7 +61,7 @@ pub fn qr<N, V, M>(m: &M) -> (M, M)
let x = v.unsafe_at(0); let x = v.unsafe_at(0);
v.unsafe_set(0, x - alpha); v.unsafe_set(0, x - alpha);
} }
if !v.normalize().is_zero() { if !::is_zero(&v.normalize()) {
let qk: M = householder_matrix(rows, ite, v); let qk: M = householder_matrix(rows, ite, v);
r = qk * r; r = qk * r;
q = q * Transpose::transpose_cpy(&qk); q = q * Transpose::transpose_cpy(&qk);

View File

@ -1,27 +0,0 @@
// use lower_triangular::LowerTriangularMat;
//
// /// Choleski factorization.
// pub trait Chol<N> {
// /// Choleski LL* factorization for a symetric definite positive matrix.
// fn chol(self) -> LowerTriangularMat<N>;
// }
//
// impl<N> Chol<N> for DMat<N> {
// fn chol(self) -> LowerTriangularMat<N> {
// }
// }
//
// impl<N> Chol<N> for LowerTriangularMat<N> {
// fn chol(self) -> LowerTriangularMat<N> {
// }
// }
//
// impl<N> Chol<N> for UpperTriangularMat<N> {
// fn chol(self) -> LowerTriangularMat<N> {
// }
// }
//
// impl<N> Chol<N> for DiagonalMat<N> {
// fn chol(self) -> LowerTriangularMat<N> {
// }
// }

View File

@ -1,6 +0,0 @@
use lower_triangular::LowerTriangularMat;
pub trait Crout<N> {
/// Crout LDL* factorization for a symmetric definite indefinite matrix.
fn crout(self, &mut DiagonalMat<N>) -> LowerTriangularMat<N>;
}

View File

@ -5,12 +5,11 @@
use std::cmp; use std::cmp;
use std::rand::Rand; use std::rand::Rand;
use std::rand; use std::rand;
use std::num::{One, Zero, Num};
use traits::operations::ApproxEq; use traits::operations::ApproxEq;
use std::mem; use std::mem;
use structs::dvec::{DVec, DVecMulRhs}; use structs::dvec::{DVec, DVecMulRhs};
use traits::operations::{Inv, Transpose, Mean, Cov}; use traits::operations::{Inv, Transpose, Mean, Cov};
use traits::structure::{Cast, ColSlice, RowSlice, Diag, Eye, Indexable, Shape}; use traits::structure::{Cast, ColSlice, RowSlice, Diag, Eye, Indexable, Shape, Zero, One, BaseNum};
use std::fmt::{Show, Formatter, Result}; use std::fmt::{Show, Formatter, Result};
@ -55,7 +54,7 @@ impl<N: Zero + Clone> DMat<N> {
/// components. /// components.
#[inline] #[inline]
pub fn new_zeros(nrows: uint, ncols: uint) -> DMat<N> { pub fn new_zeros(nrows: uint, ncols: uint) -> DMat<N> {
DMat::from_elem(nrows, ncols, Zero::zero()) DMat::from_elem(nrows, ncols, ::zero())
} }
/// Tests if all components of the matrix are zeroes. /// Tests if all components of the matrix are zeroes.
@ -67,7 +66,7 @@ impl<N: Zero + Clone> DMat<N> {
#[inline] #[inline]
pub fn reset(&mut self) { pub fn reset(&mut self) {
for mij in self.mij.iter_mut() { for mij in self.mij.iter_mut() {
*mij = Zero::zero(); *mij = ::zero();
} }
} }
} }
@ -84,7 +83,7 @@ impl<N: One + Clone> DMat<N> {
/// Builds a matrix filled with a given constant. /// Builds a matrix filled with a given constant.
#[inline] #[inline]
pub fn new_ones(nrows: uint, ncols: uint) -> DMat<N> { pub fn new_ones(nrows: uint, ncols: uint) -> DMat<N> {
DMat::from_elem(nrows, ncols, One::one()) DMat::from_elem(nrows, ncols, ::one())
} }
} }
@ -191,7 +190,7 @@ impl<N: One + Zero + Clone> Eye for DMat<N> {
let mut res = DMat::new_zeros(dim, dim); let mut res = DMat::new_zeros(dim, dim);
for i in range(0u, dim) { for i in range(0u, dim) {
let _1: N = One::one(); let _1: N = ::one();
res[(i, i)] = _1; res[(i, i)] = _1;
} }
@ -302,7 +301,7 @@ impl<N: Clone + Mul<N, N> + Add<N, N> + Zero> DMatMulRhs<N, DMat<N>> for DMat<N>
for i in range(0u, left.nrows) { for i in range(0u, left.nrows) {
for j in range(0u, right.ncols) { for j in range(0u, right.ncols) {
let mut acc: N = Zero::zero(); let mut acc: N = ::zero();
unsafe { unsafe {
for k in range(0u, left.ncols) { for k in range(0u, left.ncols) {
@ -327,7 +326,7 @@ DMatMulRhs<N, DVec<N>> for DVec<N> {
let mut res : DVec<N> = unsafe { DVec::new_uninitialized(left.nrows) }; let mut res : DVec<N> = unsafe { DVec::new_uninitialized(left.nrows) };
for i in range(0u, left.nrows) { for i in range(0u, left.nrows) {
let mut acc: N = Zero::zero(); let mut acc: N = ::zero();
for j in range(0u, left.ncols) { for j in range(0u, left.ncols) {
unsafe { unsafe {
@ -351,7 +350,7 @@ DVecMulRhs<N, DVec<N>> for DMat<N> {
let mut res : DVec<N> = unsafe { DVec::new_uninitialized(right.ncols) }; let mut res : DVec<N> = unsafe { DVec::new_uninitialized(right.ncols) };
for i in range(0u, right.ncols) { for i in range(0u, right.ncols) {
let mut acc: N = Zero::zero(); let mut acc: N = ::zero();
for j in range(0u, right.nrows) { for j in range(0u, right.nrows) {
unsafe { unsafe {
@ -366,8 +365,7 @@ DVecMulRhs<N, DVec<N>> for DMat<N> {
} }
} }
impl<N: Clone + Num> impl<N: Clone + BaseNum + Zero + One> Inv for DMat<N> {
Inv for DMat<N> {
#[inline] #[inline]
fn inv_cpy(m: &DMat<N>) -> Option<DMat<N>> { fn inv_cpy(m: &DMat<N>) -> Option<DMat<N>> {
let mut res : DMat<N> = m.clone(); let mut res : DMat<N> = m.clone();
@ -395,7 +393,7 @@ Inv for DMat<N> {
let mut n0 = k; // index of a non-zero entry let mut n0 = k; // index of a non-zero entry
while n0 != dim { while n0 != dim {
if unsafe { self.unsafe_at((n0, k)) } != Zero::zero() { if unsafe { self.unsafe_at((n0, k)) } != ::zero() {
break; break;
} }
@ -500,7 +498,7 @@ impl<N: Clone> Transpose for DMat<N> {
} }
} }
impl<N: Num + Cast<f64> + 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(m: &DMat<N>) -> DVec<N> {
let mut res: DVec<N> = DVec::new_zeros(m.ncols); let mut res: DVec<N> = DVec::new_zeros(m.ncols);
let normalizer: N = Cast::from(1.0f64 / Cast::from(m.nrows)); let normalizer: N = Cast::from(1.0f64 / Cast::from(m.nrows));
@ -518,7 +516,7 @@ impl<N: Num + Cast<f64> + Clone> Mean<DVec<N>> for DMat<N> {
} }
} }
impl<N: Clone + Num + Cast<f64> + DMatDivRhs<N, DMat<N>>> Cov<DMat<N>> for DMat<N> { impl<N: Clone + BaseNum + Cast<f64> + DMatDivRhs<N, DMat<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(m: &DMat<N>) -> DMat<N> {
assert!(m.nrows > 1); assert!(m.nrows > 1);

View File

@ -2,14 +2,13 @@
#![allow(missing_docs)] // we hide doc to not have to document the $trhs double dispatch trait. #![allow(missing_docs)] // we hide doc to not have to document the $trhs double dispatch trait.
use std::num::{Zero, One, Num};
use std::rand::Rand; use std::rand::Rand;
use std::rand; use std::rand;
use std::slice::{Items, MutItems}; use std::slice::{Items, MutItems};
use traits::operations::ApproxEq; use traits::operations::ApproxEq;
use std::iter::FromIterator; use std::iter::FromIterator;
use traits::geometry::{Dot, Norm}; use traits::geometry::{Dot, Norm};
use traits::structure::{Iterable, IterableMut, Indexable, Shape, BaseFloat}; use traits::structure::{Iterable, IterableMut, Indexable, Shape, BaseFloat, BaseNum, Zero, One};
/// Heap allocated, dynamically sized vector. /// Heap allocated, dynamically sized vector.
#[deriving(Eq, PartialEq, Show, Clone)] #[deriving(Eq, PartialEq, Show, Clone)]
@ -87,7 +86,7 @@ pub struct DVec1<N> {
} }
small_dvec_impl!(DVec1, 1, DVec1MulRhs, DVec1DivRhs, DVec1AddRhs, DVec1SubRhs, 0) small_dvec_impl!(DVec1, 1, DVec1MulRhs, DVec1DivRhs, DVec1AddRhs, DVec1SubRhs, 0)
small_dvec_from_impl!(DVec1, 1, Zero::zero()) small_dvec_from_impl!(DVec1, 1, ::zero())
/// Stack-allocated, dynamically sized vector with a maximum size of 2. /// Stack-allocated, dynamically sized vector with a maximum size of 2.
@ -97,7 +96,7 @@ pub struct DVec2<N> {
} }
small_dvec_impl!(DVec2, 2, DVec2MulRhs, DVec2DivRhs, DVec2AddRhs, DVec2SubRhs, 0, 1) small_dvec_impl!(DVec2, 2, DVec2MulRhs, DVec2DivRhs, DVec2AddRhs, DVec2SubRhs, 0, 1)
small_dvec_from_impl!(DVec2, 2, Zero::zero(), Zero::zero()) small_dvec_from_impl!(DVec2, 2, ::zero(), ::zero())
/// Stack-allocated, dynamically sized vector with a maximum size of 3. /// Stack-allocated, dynamically sized vector with a maximum size of 3.
@ -107,7 +106,7 @@ pub struct DVec3<N> {
} }
small_dvec_impl!(DVec3, 3, DVec3MulRhs, DVec3DivRhs, DVec3AddRhs, DVec3SubRhs, 0, 1, 2) small_dvec_impl!(DVec3, 3, DVec3MulRhs, DVec3DivRhs, DVec3AddRhs, DVec3SubRhs, 0, 1, 2)
small_dvec_from_impl!(DVec3, 3, Zero::zero(), Zero::zero(), Zero::zero()) small_dvec_from_impl!(DVec3, 3, ::zero(), ::zero(), ::zero())
/// Stack-allocated, dynamically sized vector with a maximum size of 4. /// Stack-allocated, dynamically sized vector with a maximum size of 4.
@ -117,7 +116,7 @@ pub struct DVec4<N> {
} }
small_dvec_impl!(DVec4, 4, DVec4MulRhs, DVec4DivRhs, DVec4AddRhs, DVec4SubRhs, 0, 1, 2, 3) small_dvec_impl!(DVec4, 4, DVec4MulRhs, DVec4DivRhs, DVec4AddRhs, DVec4SubRhs, 0, 1, 2, 3)
small_dvec_from_impl!(DVec4, 4, Zero::zero(), Zero::zero(), Zero::zero(), Zero::zero()) small_dvec_from_impl!(DVec4, 4, ::zero(), ::zero(), ::zero(), ::zero())
/// Stack-allocated, dynamically sized vector with a maximum size of 5. /// Stack-allocated, dynamically sized vector with a maximum size of 5.
@ -127,7 +126,7 @@ pub struct DVec5<N> {
} }
small_dvec_impl!(DVec5, 5, DVec5MulRhs, DVec5DivRhs, DVec5AddRhs, DVec5SubRhs, 0, 1, 2, 3, 4) small_dvec_impl!(DVec5, 5, DVec5MulRhs, DVec5DivRhs, DVec5AddRhs, DVec5SubRhs, 0, 1, 2, 3, 4)
small_dvec_from_impl!(DVec5, 5, Zero::zero(), Zero::zero(), Zero::zero(), Zero::zero(), Zero::zero()) small_dvec_from_impl!(DVec5, 5, ::zero(), ::zero(), ::zero(), ::zero(), ::zero())
/// Stack-allocated, dynamically sized vector with a maximum size of 6. /// Stack-allocated, dynamically sized vector with a maximum size of 6.
@ -137,4 +136,4 @@ pub struct DVec6<N> {
} }
small_dvec_impl!(DVec6, 6, DVec6MulRhs, DVec6DivRhs, DVec6AddRhs, DVec6SubRhs, 0, 1, 2, 3, 4, 5) small_dvec_impl!(DVec6, 6, DVec6MulRhs, DVec6DivRhs, DVec6AddRhs, DVec6SubRhs, 0, 1, 2, 3, 4, 5)
small_dvec_from_impl!(DVec6, 6, Zero::zero(), Zero::zero(), Zero::zero(), Zero::zero(), Zero::zero(), Zero::zero()) small_dvec_from_impl!(DVec6, 6, ::zero(), ::zero(), ::zero(), ::zero(), ::zero(), ::zero())

View File

@ -19,7 +19,7 @@ macro_rules! dvec_impl(
/// * `dim` - The dimension of the vector. /// * `dim` - The dimension of the vector.
#[inline] #[inline]
pub fn new_zeros(dim: uint) -> $dvec<N> { pub fn new_zeros(dim: uint) -> $dvec<N> {
$dvec::from_elem(dim, Zero::zero()) $dvec::from_elem(dim, ::zero())
} }
/// Tests if all components of the vector are zeroes. /// Tests if all components of the vector are zeroes.
@ -106,7 +106,7 @@ macro_rules! dvec_impl(
/// * `dim` - The dimension of the vector. /// * `dim` - The dimension of the vector.
#[inline] #[inline]
pub fn new_ones(dim: uint) -> $dvec<N> { pub fn new_ones(dim: uint) -> $dvec<N> {
$dvec::from_elem(dim, One::one()) $dvec::from_elem(dim, ::one())
} }
} }
@ -142,7 +142,7 @@ macro_rules! dvec_impl(
for i in range(0u, dim) { for i in range(0u, dim) {
let mut basis_element : $dvec<N> = $dvec::new_zeros(dim); let mut basis_element : $dvec<N> = $dvec::new_zeros(dim);
basis_element.set(i, One::one()); basis_element.set(i, ::one());
res.push(basis_element); res.push(basis_element);
} }
@ -161,7 +161,7 @@ macro_rules! dvec_impl(
for i in range(0u, dim) { for i in range(0u, dim) {
let mut basis_element : $dvec<N> = $dvec::new_zeros(self.len()); let mut basis_element : $dvec<N> = $dvec::new_zeros(self.len());
basis_element.set(i, One::one()); basis_element.set(i, ::one());
if res.len() == dim - 1 { if res.len() == dim - 1 {
break; break;
@ -175,7 +175,7 @@ macro_rules! dvec_impl(
elt = elt - *v * Dot::dot(&elt, v) elt = elt - *v * Dot::dot(&elt, v)
}; };
if !ApproxEq::approx_eq(&Norm::sqnorm(&elt), &Zero::zero()) { if !ApproxEq::approx_eq(&Norm::sqnorm(&elt), &::zero()) {
res.push(Norm::normalize_cpy(&elt)); res.push(Norm::normalize_cpy(&elt));
} }
} }
@ -225,12 +225,12 @@ macro_rules! dvec_impl(
} }
} }
impl<N: Num + 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(a: &$dvec<N>, b: &$dvec<N>) -> N {
assert!(a.len() == b.len()); assert!(a.len() == b.len());
let mut res: N = Zero::zero(); let mut res: N = ::zero();
for i in range(0u, a.len()) { for i in range(0u, a.len()) {
res = res + unsafe { a.unsafe_at(i) * b.unsafe_at(i) }; res = res + unsafe { a.unsafe_at(i) * b.unsafe_at(i) };

View File

@ -2,11 +2,10 @@
#![allow(missing_docs)] #![allow(missing_docs)]
use std::num::{Zero, One, Num};
use std::rand::{Rand, Rng}; use std::rand::{Rand, Rng};
use structs::mat::{Mat3, Mat4, Mat5}; use structs::mat::{Mat3, Mat4, Mat5};
use traits::structure::{Cast, Dim, Col, BaseFloat}; use traits::structure::{Cast, Dim, Col, BaseFloat, BaseNum, One};
use traits::operations::{Inv, ApproxEq, Absolute}; use traits::operations::{Inv, ApproxEq};
use traits::geometry::{RotationMatrix, Rotation, Rotate, AbsoluteRotate, Transform, Transformation, use traits::geometry::{RotationMatrix, Rotation, Rotate, AbsoluteRotate, Transform, Transformation,
Translate, Translation, ToHomogeneous}; Translate, Translation, ToHomogeneous};

View File

@ -2,7 +2,7 @@
macro_rules! iso_impl( macro_rules! iso_impl(
($t: ident, $submat: ident, $subvec: ident, $subrotvec: ident) => ( ($t: ident, $submat: ident, $subvec: ident, $subrotvec: ident) => (
impl<N: Clone + BaseFloat + Num> $t<N> { impl<N: Clone + BaseFloat + BaseNum> $t<N> {
/// Creates a new isometry from a rotation matrix and a vector. /// Creates a new isometry from a rotation matrix and a vector.
#[inline] #[inline]
pub fn new(translation: $subvec<N>, rotation: $subrotvec<N>) -> $t<N> { pub fn new(translation: $subvec<N>, rotation: $subrotvec<N>) -> $t<N> {
@ -26,7 +26,7 @@ macro_rules! iso_impl(
macro_rules! rotation_matrix_impl( macro_rules! rotation_matrix_impl(
($t: ident, $trot: ident, $tlv: ident, $tav: ident) => ( ($t: ident, $trot: ident, $tlv: ident, $tav: ident) => (
impl<N: Cast<f64> + BaseFloat + Num + Clone> impl<N: Cast<f64> + BaseFloat + BaseNum + Clone>
RotationMatrix<N, $tlv<N>, $tav<N>, $trot<N>> for $t<N> { RotationMatrix<N, $tlv<N>, $tav<N>, $trot<N>> for $t<N> {
#[inline] #[inline]
fn to_rot_mat(&self) -> $trot<N> { fn to_rot_mat(&self) -> $trot<N> {
@ -53,7 +53,7 @@ macro_rules! one_impl(
impl<N: BaseFloat + Clone> One for $t<N> { impl<N: BaseFloat + Clone> One for $t<N> {
#[inline] #[inline]
fn one() -> $t<N> { fn one() -> $t<N> {
$t::new_with_rotmat(Zero::zero(), One::one()) $t::new_with_rotmat(::zero(), ::one())
} }
} }
) )
@ -74,7 +74,7 @@ macro_rules! iso_mul_iso_impl(
macro_rules! iso_mul_pnt_impl( macro_rules! iso_mul_pnt_impl(
($t: ident, $tv: ident, $tmul: ident) => ( ($t: ident, $tv: ident, $tmul: ident) => (
impl<N: Num + Clone> $tmul<N, $tv<N>> for $tv<N> { impl<N: BaseNum + Clone> $tmul<N, $tv<N>> for $tv<N> {
#[inline] #[inline]
fn binop(left: &$t<N>, right: &$tv<N>) -> $tv<N> { fn binop(left: &$t<N>, right: &$tv<N>) -> $tv<N> {
left.rotation * *right + left.translation left.rotation * *right + left.translation
@ -85,7 +85,7 @@ macro_rules! iso_mul_pnt_impl(
macro_rules! pnt_mul_iso_impl( macro_rules! pnt_mul_iso_impl(
($t: ident, $tv: ident, $tmul: ident) => ( ($t: ident, $tv: ident, $tmul: ident) => (
impl<N: Clone + Num> $tmul<N, $tv<N>> for $t<N> { impl<N: Clone + BaseNum> $tmul<N, $tv<N>> for $t<N> {
#[inline] #[inline]
fn binop(left: &$tv<N>, right: &$t<N>) -> $tv<N> { fn binop(left: &$tv<N>, right: &$t<N>) -> $tv<N> {
(*left + right.translation) * right.rotation (*left + right.translation) * right.rotation
@ -204,7 +204,7 @@ macro_rules! rotation_impl(
macro_rules! rotate_impl( macro_rules! rotate_impl(
($t: ident, $tv: ident) => ( ($t: ident, $tv: ident) => (
impl<N: Num + Clone> Rotate<$tv<N>> for $t<N> { impl<N: BaseNum + Clone> Rotate<$tv<N>> for $t<N> {
#[inline] #[inline]
fn rotate(&self, v: &$tv<N>) -> $tv<N> { fn rotate(&self, v: &$tv<N>) -> $tv<N> {
self.rotation.rotate(v) self.rotation.rotate(v)
@ -276,7 +276,7 @@ macro_rules! transform_impl(
} }
} }
impl<N: Num + Clone> $trhs<N> for $tp<N> { impl<N: BaseNum + Clone> $trhs<N> for $tp<N> {
#[inline] #[inline]
fn transform(t: &$t<N>, p: &$tp<N>) -> $tp<N> { fn transform(t: &$t<N>, p: &$tp<N>) -> $tp<N> {
t.rotation.transform(p) + t.translation t.rotation.transform(p) + t.translation
@ -292,7 +292,7 @@ macro_rules! transform_impl(
macro_rules! inv_impl( macro_rules! inv_impl(
($t: ident) => ( ($t: ident) => (
impl<N: Clone + Num> Inv for $t<N> { impl<N: Clone + BaseNum> Inv for $t<N> {
#[inline] #[inline]
fn inv(&mut self) -> bool { fn inv(&mut self) -> bool {
self.rotation.inv(); self.rotation.inv();
@ -317,7 +317,7 @@ macro_rules! inv_impl(
macro_rules! to_homogeneous_impl( macro_rules! to_homogeneous_impl(
($t: ident, $th: ident) => ( ($t: ident, $th: ident) => (
impl<N: Num + Clone> ToHomogeneous<$th<N>> for $t<N> { impl<N: BaseNum + Clone> ToHomogeneous<$th<N>> for $t<N> {
fn to_homogeneous(m: &$t<N>) -> $th<N> { fn to_homogeneous(m: &$t<N>) -> $th<N> {
let mut res = ToHomogeneous::to_homogeneous(&m.rotation); let mut res = ToHomogeneous::to_homogeneous(&m.rotation);

View File

@ -1,153 +0,0 @@
use rand::Rand;
use rand;
use std::num::{One, Zero};
use std::vec;
/// A structure optimized to store lower triangular matrices.
pub struct LowerTriangularMat<N> {
priv dim: uint,
priv mij: ~[N]
}
/// Trait to be implemented by objects which can be left-multiplied by a lower triangular array.
pub trait LowerTriangularMatMulRhs<N, Res> {
/// Apply the muliplicitaion.
fn binop(left: &LowerTriangularMat<N>, right: &Self) -> Res;
}
impl<N, Rhs: LowerTriangularMatMulRhs<N, Res>, Res> Mul<Rhs, Res> for LowerTriangularMat<N> {
#[inline(always)]
fn mul(&self, other: &Rhs) -> Res {
LowerTriangularMatMulRhs::binop(self, other)
}
}
impl<N> LowerTriangularMat<N> {
/// Creates a lower triangular matrix without initializing its arguments.
#[inline]
pub unsafe fn new_uninitialized(dim: uint) -> LowerTriangularMat<N> {
let mut vec = vec::with_capacity(dim * (dim + 1) / 2);
vec::raw::set_len(&mut vec, dim * (dim + 1) / 2);
LowerTriangularMat {
dim: dim,
mij: vec
}
}
}
impl<N: Zero + Clone> LowerTriangularMat<N> {
/// Creates a lower triangular matrix filled with zeros.
#[inline]
pub fn new_zeros(dim: uint) -> LowerTriangularMat<N> {
LowerTriangularMat::from_elem(dim, Zero::zero())
}
/// Tests if every entry of the matrix are exactly zeros.
#[inline]
pub fn is_zero(&self) -> bool {
self.mij.iter().all(|e| e.is_zero())
}
}
impl<N: Rand> LowerTriangularMat<N> {
/// Creates a lower triangular matrix filled with random elements.
#[inline]
pub fn new_random(dim: uint) -> LowerTriangularMat<N> {
LowerTriangularMat::from_fn(dim, |_, _| rand::random())
}
}
impl<N: One + Clone> LowerTriangularMat<N> {
/// Creates a lower triangular matrix filled with ones.
#[inline]
pub fn new_ones(dim: uint) -> LowerTriangularMat<N> {
LowerTriangularMat::from_elem(dim, One::one())
}
}
impl<N: Clone> LowerTriangularMat<N> {
/// Creates a lower triangular matrix filled with a given value.
#[inline]
pub fn from_elem(dim: uint, val: N) -> LowerTriangularMat<N> {
LowerTriangularMat {
dim: dim,
mij: vec::from_elem(dim * (dim + 1) / 2, val)
}
}
}
impl<N> LowerTriangularMat<N> {
/// Creates a lower triangular matrix filled by a function.
#[inline(always)]
pub fn from_fn(dim: uint, f: &fn(uint, uint) -> N) -> LowerTriangularMat<N> {
let mij = do vec::from_fn(dim * (dim + 1) / 2) |i| {
let l = (((1.0f64 + 8.0f64 * i as f64).sqrt() - 1.) / 2.0f64).floor() as uint;
let c = i - l * (l + 1) / 2;
f(l, c)
};
LowerTriangularMat {
dim: dim,
mij: mij
}
}
#[inline]
fn offset(&self, i: uint, j: uint) -> uint {
i * (i + 1) / 2 + j + 1
}
/// Transforms this matrix into an array. This consumes the matrix and is O(1).
#[inline]
pub fn to_array(self) -> ~[N] {
self.mij
}
}
impl<N: Zero + Clone> LowerTriangularMat<N> {
/// Changes the value of a component of the matrix.
/// Fails if the indices point outside of the lower-triangular part of the matrix.
///
/// # Arguments
/// * `row` - 0-based index of the line to be changed
/// * `col` - 0-based index of the column to be changed
#[inline]
pub fn set(&mut self, row: uint, col: uint, val: N) {
assert!(row < self.dim);
assert!(col < self.dim);
assert!(col <= row);
unsafe { self.set_fast(row, col, val) }
}
/// Just like `set` without bounds checking.
#[inline]
pub unsafe fn set_fast(&mut self, row: uint, col: uint, val: N) {
let offset = self.offset(row, col);
*self.mij.unsafe_mut_ref(offset) = val
}
/// Reads the value of a component of the matrix.
/// Fails if the indices point outside of the lower-triangular part of the matrix.
///
/// # Arguments
/// * `row` - 0-based index of the line to be read
/// * `col` - 0-based index of the column to be read
#[inline]
pub fn at(&self, row: uint, col: uint) -> N {
assert!(row < self.dim);
assert!(col < self.dim);
unsafe { self.at_fast(row, col) }
}
/// Just like `at` without bounds checking.
#[inline]
pub unsafe fn at_fast(&self, row: uint, col: uint) -> N {
if col > row {
Zero::zero()
}
vec::raw::get(self.mij, self.offset(row, col))
}
}

View File

@ -3,7 +3,6 @@
#![allow(missing_docs)] // we allow missing to avoid having to document the mij components. #![allow(missing_docs)] // we allow missing to avoid having to document the mij components.
use std::mem; use std::mem;
use std::num::{One, Zero, Num};
use traits::operations::ApproxEq; use traits::operations::ApproxEq;
use std::slice::{Items, MutItems}; use std::slice::{Items, MutItems};
use structs::vec::{Vec1, Vec2, Vec3, Vec4, Vec5, Vec6, use structs::vec::{Vec1, Vec2, Vec3, Vec4, Vec5, Vec6,
@ -12,7 +11,7 @@ use structs::pnt::{Pnt1, Pnt4, Pnt5, Pnt6, Pnt1MulRhs, Pnt4MulRhs, Pnt5MulRhs, P
use structs::dvec::{DVec1, DVec2, DVec3, DVec4, DVec5, DVec6}; use structs::dvec::{DVec1, DVec2, DVec3, DVec4, DVec5, DVec6};
use traits::structure::{Cast, Row, Col, Iterable, IterableMut, Dim, Indexable, use traits::structure::{Cast, Row, Col, Iterable, IterableMut, Dim, Indexable,
Eye, ColSlice, RowSlice, Diag, Shape, BaseFloat}; Eye, ColSlice, RowSlice, Diag, Shape, BaseFloat, BaseNum, Zero, One};
use traits::operations::{Absolute, Transpose, Inv, Outer, EigenQR}; use traits::operations::{Absolute, Transpose, Inv, Outer, EigenQR};
use traits::geometry::{ToHomogeneous, FromHomogeneous, Orig}; use traits::geometry::{ToHomogeneous, FromHomogeneous, Orig};
use linalg; use linalg;
@ -31,7 +30,7 @@ impl Identity {
} }
/// Square matrix of dimension 1. /// Square matrix of dimension 1.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)]
pub struct Mat1<N> { pub struct Mat1<N> {
pub m11: N pub m11: N
} }
@ -106,7 +105,8 @@ mat_sub_scalar_impl!(Mat1, uint, Mat1SubRhs, m11)
mat_sub_scalar_impl!(Mat1, int, Mat1SubRhs, m11) mat_sub_scalar_impl!(Mat1, int, Mat1SubRhs, m11)
absolute_impl!(Mat1, m11) absolute_impl!(Mat1, m11)
one_impl!(Mat1, One::one) zero_impl!(Mat1, m11)
one_impl!(Mat1, ::one)
iterable_impl!(Mat1, 1) iterable_impl!(Mat1, 1)
iterable_mut_impl!(Mat1, 1) iterable_mut_impl!(Mat1, 1)
at_fast_impl!(Mat1, 1) at_fast_impl!(Mat1, 1)
@ -114,8 +114,8 @@ dim_impl!(Mat1, 1)
indexable_impl!(Mat1, 1) indexable_impl!(Mat1, 1)
index_impl!(Mat1, 1) index_impl!(Mat1, 1)
mat_mul_mat_impl!(Mat1, Mat1MulRhs, 1) mat_mul_mat_impl!(Mat1, Mat1MulRhs, 1)
mat_mul_vec_impl!(Mat1, Vec1, Mat1MulRhs, 1, Zero::zero) mat_mul_vec_impl!(Mat1, Vec1, Mat1MulRhs, 1, ::zero)
vec_mul_mat_impl!(Mat1, Vec1, Vec1MulRhs, 1, Zero::zero) vec_mul_mat_impl!(Mat1, Vec1, Vec1MulRhs, 1, ::zero)
mat_mul_pnt_impl!(Mat1, Pnt1, Mat1MulRhs, 1, Orig::orig) mat_mul_pnt_impl!(Mat1, Pnt1, Mat1MulRhs, 1, Orig::orig)
pnt_mul_mat_impl!(Mat1, Pnt1, Pnt1MulRhs, 1, Orig::orig) pnt_mul_mat_impl!(Mat1, Pnt1, Pnt1MulRhs, 1, Orig::orig)
// (specialized) inv_impl!(Mat1, 1) // (specialized) inv_impl!(Mat1, 1)
@ -132,7 +132,7 @@ outer_impl!(Vec1, Mat1)
eigen_qr_impl!(Mat1, Vec1) eigen_qr_impl!(Mat1, Vec1)
/// Square matrix of dimension 2. /// Square matrix of dimension 2.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)]
pub struct Mat2<N> { pub struct Mat2<N> {
pub m11: N, pub m21: N, pub m11: N, pub m21: N,
pub m12: N, pub m22: N pub m12: N, pub m22: N
@ -211,8 +211,10 @@ mat_sub_scalar_impl!(Mat2, int, Mat2SubRhs, m11, m12, m21, m22)
absolute_impl!(Mat2, m11, m12, absolute_impl!(Mat2, m11, m12,
m21, m22) m21, m22)
one_impl!(Mat2, One::one, Zero::zero, zero_impl!(Mat2, m11, m12,
Zero::zero, One::one) m21, m22)
one_impl!(Mat2, ::one, ::zero,
::zero, ::one)
iterable_impl!(Mat2, 2) iterable_impl!(Mat2, 2)
iterable_mut_impl!(Mat2, 2) iterable_mut_impl!(Mat2, 2)
dim_impl!(Mat2, 2) dim_impl!(Mat2, 2)
@ -236,7 +238,7 @@ outer_impl!(Vec2, Mat2)
eigen_qr_impl!(Mat2, Vec2) eigen_qr_impl!(Mat2, Vec2)
/// Square matrix of dimension 3. /// Square matrix of dimension 3.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)]
pub struct Mat3<N> { pub struct Mat3<N> {
pub m11: N, pub m21: N, pub m31: N, pub m11: N, pub m21: N, pub m31: N,
pub m12: N, pub m22: N, pub m32: N, pub m12: N, pub m22: N, pub m32: N,
@ -328,9 +330,14 @@ absolute_impl!(Mat3,
m21, m22, m23, m21, m22, m23,
m31, m32, m33 m31, m32, m33
) )
one_impl!(Mat3, One::one , Zero::zero, Zero::zero, zero_impl!(Mat3,
Zero::zero, One::one , Zero::zero, m11, m12, m13,
Zero::zero, Zero::zero, One::one) m21, m22, m23,
m31, m32, m33
)
one_impl!(Mat3, ::one , ::zero, ::zero,
::zero, ::one , ::zero,
::zero, ::zero, ::one)
iterable_impl!(Mat3, 3) iterable_impl!(Mat3, 3)
iterable_mut_impl!(Mat3, 3) iterable_mut_impl!(Mat3, 3)
dim_impl!(Mat3, 3) dim_impl!(Mat3, 3)
@ -354,7 +361,7 @@ outer_impl!(Vec3, Mat3)
eigen_qr_impl!(Mat3, Vec3) eigen_qr_impl!(Mat3, Vec3)
/// Square matrix of dimension 4. /// Square matrix of dimension 4.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)]
pub struct Mat4<N> { pub struct Mat4<N> {
pub m11: N, pub m21: N, pub m31: N, pub m41: N, pub m11: N, pub m21: N, pub m31: N, pub m41: N,
pub m12: N, pub m22: N, pub m32: N, pub m42: N, pub m12: N, pub m22: N, pub m32: N, pub m42: N,
@ -497,10 +504,16 @@ absolute_impl!(Mat4,
m31, m32, m33, m34, m31, m32, m33, m34,
m41, m42, m43, m44 m41, m42, m43, m44
) )
one_impl!(Mat4, One::one , Zero::zero, Zero::zero, Zero::zero, zero_impl!(Mat4,
Zero::zero, One::one , Zero::zero, Zero::zero, m11, m12, m13, m14,
Zero::zero, Zero::zero, One::one , Zero::zero, m21, m22, m23, m24,
Zero::zero, Zero::zero, Zero::zero, One::one) m31, m32, m33, m34,
m41, m42, m43, m44
)
one_impl!(Mat4, ::one , ::zero, ::zero, ::zero,
::zero, ::one , ::zero, ::zero,
::zero, ::zero, ::one , ::zero,
::zero, ::zero, ::zero, ::one)
iterable_impl!(Mat4, 4) iterable_impl!(Mat4, 4)
iterable_mut_impl!(Mat4, 4) iterable_mut_impl!(Mat4, 4)
dim_impl!(Mat4, 4) dim_impl!(Mat4, 4)
@ -508,8 +521,8 @@ indexable_impl!(Mat4, 4)
index_impl!(Mat4, 4) index_impl!(Mat4, 4)
at_fast_impl!(Mat4, 4) at_fast_impl!(Mat4, 4)
mat_mul_mat_impl!(Mat4, Mat4MulRhs, 4) mat_mul_mat_impl!(Mat4, Mat4MulRhs, 4)
mat_mul_vec_impl!(Mat4, Vec4, Mat4MulRhs, 4, Zero::zero) mat_mul_vec_impl!(Mat4, Vec4, Mat4MulRhs, 4, ::zero)
vec_mul_mat_impl!(Mat4, Vec4, Vec4MulRhs, 4, Zero::zero) vec_mul_mat_impl!(Mat4, Vec4, Vec4MulRhs, 4, ::zero)
mat_mul_pnt_impl!(Mat4, Pnt4, Mat4MulRhs, 4, Orig::orig) mat_mul_pnt_impl!(Mat4, Pnt4, Mat4MulRhs, 4, Orig::orig)
pnt_mul_mat_impl!(Mat4, Pnt4, Pnt4MulRhs, 4, Orig::orig) pnt_mul_mat_impl!(Mat4, Pnt4, Pnt4MulRhs, 4, Orig::orig)
inv_impl!(Mat4, 4) inv_impl!(Mat4, 4)
@ -526,7 +539,7 @@ outer_impl!(Vec4, Mat4)
eigen_qr_impl!(Mat4, Vec4) eigen_qr_impl!(Mat4, Vec4)
/// Square matrix of dimension 5. /// Square matrix of dimension 5.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)]
pub struct Mat5<N> { pub struct Mat5<N> {
pub m11: N, pub m21: N, pub m31: N, pub m41: N, pub m51: N, pub m11: N, pub m21: N, pub m31: N, pub m41: N, pub m51: N,
pub m12: N, pub m22: N, pub m32: N, pub m42: N, pub m52: N, pub m12: N, pub m22: N, pub m32: N, pub m42: N, pub m52: N,
@ -568,12 +581,19 @@ absolute_impl!(Mat5,
m41, m42, m43, m44, m45, m41, m42, m43, m44, m45,
m51, m52, m53, m54, m55 m51, m52, m53, m54, m55
) )
zero_impl!(Mat5,
m11, m12, m13, m14, m15,
m21, m22, m23, m24, m25,
m31, m32, m33, m34, m35,
m41, m42, m43, m44, m45,
m51, m52, m53, m54, m55
)
one_impl!(Mat5, one_impl!(Mat5,
One::one , Zero::zero, Zero::zero, Zero::zero, Zero::zero, ::one , ::zero, ::zero, ::zero, ::zero,
Zero::zero, One::one , Zero::zero, Zero::zero, Zero::zero, ::zero, ::one , ::zero, ::zero, ::zero,
Zero::zero, Zero::zero, One::one , Zero::zero, Zero::zero, ::zero, ::zero, ::one , ::zero, ::zero,
Zero::zero, Zero::zero, Zero::zero, One::one , Zero::zero, ::zero, ::zero, ::zero, ::one , ::zero,
Zero::zero, Zero::zero, Zero::zero, Zero::zero, One::one ::zero, ::zero, ::zero, ::zero, ::one
) )
add_impl!(Mat5, Mat5AddRhs, add_impl!(Mat5, Mat5AddRhs,
m11, m12, m13, m14, m15, m11, m12, m13, m14, m15,
@ -696,8 +716,8 @@ indexable_impl!(Mat5, 5)
index_impl!(Mat5, 5) index_impl!(Mat5, 5)
at_fast_impl!(Mat5, 5) at_fast_impl!(Mat5, 5)
mat_mul_mat_impl!(Mat5, Mat5MulRhs, 5) mat_mul_mat_impl!(Mat5, Mat5MulRhs, 5)
mat_mul_vec_impl!(Mat5, Vec5, Mat5MulRhs, 5, Zero::zero) mat_mul_vec_impl!(Mat5, Vec5, Mat5MulRhs, 5, ::zero)
vec_mul_mat_impl!(Mat5, Vec5, Vec5MulRhs, 5, Zero::zero) vec_mul_mat_impl!(Mat5, Vec5, Vec5MulRhs, 5, ::zero)
mat_mul_pnt_impl!(Mat5, Pnt5, Mat5MulRhs, 5, Orig::orig) mat_mul_pnt_impl!(Mat5, Pnt5, Mat5MulRhs, 5, Orig::orig)
pnt_mul_mat_impl!(Mat5, Pnt5, Pnt5MulRhs, 5, Orig::orig) pnt_mul_mat_impl!(Mat5, Pnt5, Pnt5MulRhs, 5, Orig::orig)
inv_impl!(Mat5, 5) inv_impl!(Mat5, 5)
@ -714,7 +734,7 @@ outer_impl!(Vec5, Mat5)
eigen_qr_impl!(Mat5, Vec5) eigen_qr_impl!(Mat5, Vec5)
/// Square matrix of dimension 6. /// Square matrix of dimension 6.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)]
pub struct Mat6<N> { pub struct Mat6<N> {
pub m11: N, pub m21: N, pub m31: N, pub m41: N, pub m51: N, pub m61: N, pub m11: N, pub m21: N, pub m31: N, pub m41: N, pub m51: N, pub m61: N,
pub m12: N, pub m22: N, pub m32: N, pub m42: N, pub m52: N, pub m62: N, pub m12: N, pub m22: N, pub m32: N, pub m42: N, pub m52: N, pub m62: N,
@ -921,13 +941,17 @@ absolute_impl!(Mat6, m11, m12, m13, m14, m15, m16, m21, m22, m23, m24, m25, m26,
m31, m32, m33, m34, m35, m36, m41, m42, m43, m44, m45, m46, m51, m52, m53, m54, m55, m56, m31, m32, m33, m34, m35, m36, m41, m42, m43, m44, m45, m46, m51, m52, m53, m54, m55, m56,
m61, m62, m63, m64, m65, m66) m61, m62, m63, m64, m65, m66)
zero_impl!(Mat6, m11, m12, m13, m14, m15, m16, m21, m22, m23, m24, m25, m26,
m31, m32, m33, m34, m35, m36, m41, m42, m43, m44, m45, m46, m51, m52, m53, m54, m55, m56,
m61, m62, m63, m64, m65, m66)
one_impl!(Mat6, one_impl!(Mat6,
One::one , Zero::zero, Zero::zero, Zero::zero, Zero::zero, Zero::zero, ::one , ::zero, ::zero, ::zero, ::zero, ::zero,
Zero::zero, One::one , Zero::zero, Zero::zero, Zero::zero, Zero::zero, ::zero, ::one , ::zero, ::zero, ::zero, ::zero,
Zero::zero, Zero::zero, One::one , Zero::zero, Zero::zero, Zero::zero, ::zero, ::zero, ::one , ::zero, ::zero, ::zero,
Zero::zero, Zero::zero, Zero::zero, One::one , Zero::zero, Zero::zero, ::zero, ::zero, ::zero, ::one , ::zero, ::zero,
Zero::zero, Zero::zero, Zero::zero, Zero::zero, One::one , Zero::zero, ::zero, ::zero, ::zero, ::zero, ::one , ::zero,
Zero::zero, Zero::zero, Zero::zero, Zero::zero, Zero::zero, One::one ::zero, ::zero, ::zero, ::zero, ::zero, ::one
) )
iterable_impl!(Mat6, 6) iterable_impl!(Mat6, 6)
iterable_mut_impl!(Mat6, 6) iterable_mut_impl!(Mat6, 6)
@ -936,8 +960,8 @@ indexable_impl!(Mat6, 6)
index_impl!(Mat6, 6) index_impl!(Mat6, 6)
at_fast_impl!(Mat6, 6) at_fast_impl!(Mat6, 6)
mat_mul_mat_impl!(Mat6, Mat6MulRhs, 6) mat_mul_mat_impl!(Mat6, Mat6MulRhs, 6)
mat_mul_vec_impl!(Mat6, Vec6, Mat6MulRhs, 6, Zero::zero) mat_mul_vec_impl!(Mat6, Vec6, Mat6MulRhs, 6, ::zero)
vec_mul_mat_impl!(Mat6, Vec6, Vec6MulRhs, 6, Zero::zero) vec_mul_mat_impl!(Mat6, Vec6, Vec6MulRhs, 6, ::zero)
mat_mul_pnt_impl!(Mat6, Pnt6, Mat6MulRhs, 6, Orig::orig) mat_mul_pnt_impl!(Mat6, Pnt6, Mat6MulRhs, 6, Orig::orig)
pnt_mul_mat_impl!(Mat6, Pnt6, Pnt6MulRhs, 6, Orig::orig) pnt_mul_mat_impl!(Mat6, Pnt6, Pnt6MulRhs, 6, Orig::orig)
inv_impl!(Mat6, 6) inv_impl!(Mat6, 6)

View File

@ -104,8 +104,8 @@ macro_rules! eye_impl(
impl<N: Zero + One> Eye for $t<N> { impl<N: Zero + One> Eye for $t<N> {
fn new_identity(dim: uint) -> $t<N> { fn new_identity(dim: uint) -> $t<N> {
assert!(dim == $ndim); assert!(dim == $ndim);
let mut eye: $t<N> = Zero::zero(); let mut eye: $t<N> = ::zero();
$(eye.$comp_diagN = One::one();)+ $(eye.$comp_diagN = ::one();)+
eye eye
} }
} }
@ -162,7 +162,7 @@ macro_rules! iterable_mut_impl(
macro_rules! one_impl( macro_rules! one_impl(
($t: ident, $value0: expr $(, $valueN: expr)* ) => ( ($t: ident, $value0: expr $(, $valueN: expr)* ) => (
impl<N: Clone + Num> One for $t<N> { impl<N: Clone + BaseNum> One for $t<N> {
#[inline] #[inline]
fn one() -> $t<N> { fn one() -> $t<N> {
$t::new($value0() $(, $valueN() )*) $t::new($value0() $(, $valueN() )*)
@ -171,6 +171,25 @@ macro_rules! one_impl(
) )
) )
macro_rules! zero_impl(
($t: ident, $comp0: ident $(, $compN: ident)* ) => (
impl<N: Zero> Zero for $t<N> {
#[inline]
fn zero() -> $t<N> {
$t {
$comp0: ::zero()
$(, $compN: ::zero() )*
}
}
#[inline]
fn is_zero(&self) -> bool {
::is_zero(&self.$comp0) $(&& ::is_zero(&self.$compN) )*
}
}
)
)
macro_rules! dim_impl( macro_rules! dim_impl(
($t: ident, $dim: expr) => ( ($t: ident, $dim: expr) => (
impl<N> Dim for $t<N> { impl<N> Dim for $t<N> {
@ -276,7 +295,7 @@ macro_rules! row_impl(
#[inline] #[inline]
fn row(&self, row: uint) -> $tv<N> { fn row(&self, row: uint) -> $tv<N> {
let mut res: $tv<N> = Zero::zero(); let mut res: $tv<N> = ::zero();
for (i, e) in res.iter_mut().enumerate() { for (i, e) in res.iter_mut().enumerate() {
*e = self.at((row, i)); *e = self.at((row, i));
@ -317,7 +336,7 @@ macro_rules! col_impl(
#[inline] #[inline]
fn col(&self, col: uint) -> $tv<N> { fn col(&self, col: uint) -> $tv<N> {
let mut res: $tv<N> = Zero::zero(); let mut res: $tv<N> = ::zero();
for (i, e) in res.iter_mut().enumerate() { for (i, e) in res.iter_mut().enumerate() {
*e = self.at((i, col)); *e = self.at((i, col));
@ -334,7 +353,7 @@ macro_rules! diag_impl(
impl<N: Clone + Zero> Diag<$tv<N>> for $t<N> { impl<N: Clone + Zero> Diag<$tv<N>> for $t<N> {
#[inline] #[inline]
fn from_diag(diag: &$tv<N>) -> $t<N> { fn from_diag(diag: &$tv<N>) -> $t<N> {
let mut res: $t<N> = Zero::zero(); let mut res: $t<N> = ::zero();
res.set_diag(diag); res.set_diag(diag);
@ -350,7 +369,7 @@ macro_rules! diag_impl(
#[inline] #[inline]
fn diag(&self) -> $tv<N> { fn diag(&self) -> $tv<N> {
let mut diag: $tv<N> = Zero::zero(); let mut diag: $tv<N> = ::zero();
for i in range(0, $dim) { for i in range(0, $dim) {
unsafe { diag.unsafe_set(i, self.unsafe_at((i, i))) } unsafe { diag.unsafe_set(i, self.unsafe_at((i, i))) }
@ -364,15 +383,15 @@ macro_rules! diag_impl(
macro_rules! mat_mul_mat_impl( macro_rules! mat_mul_mat_impl(
($t: ident, $trhs: ident, $dim: expr) => ( ($t: ident, $trhs: ident, $dim: expr) => (
impl<N: Clone + Num> $trhs<N, $t<N>> for $t<N> { impl<N: Clone + BaseNum> $trhs<N, $t<N>> for $t<N> {
#[inline] #[inline]
fn binop(left: &$t<N>, right: &$t<N>) -> $t<N> { fn binop(left: &$t<N>, right: &$t<N>) -> $t<N> {
// careful! we need to comute other * self here (self is the rhs). // careful! we need to comute other * self here (self is the rhs).
let mut res: $t<N> = Zero::zero(); let mut res: $t<N> = ::zero();
for i in range(0u, $dim) { for i in range(0u, $dim) {
for j in range(0u, $dim) { for j in range(0u, $dim) {
let mut acc: N = Zero::zero(); let mut acc: N = ::zero();
unsafe { unsafe {
for k in range(0u, $dim) { for k in range(0u, $dim) {
@ -392,7 +411,7 @@ macro_rules! mat_mul_mat_impl(
macro_rules! vec_mul_mat_impl( macro_rules! vec_mul_mat_impl(
($t: ident, $v: ident, $trhs: ident, $dim: expr, $zero: expr) => ( ($t: ident, $v: ident, $trhs: ident, $dim: expr, $zero: expr) => (
impl<N: Clone + Num> $trhs<N, $v<N>> for $t<N> { impl<N: Clone + BaseNum> $trhs<N, $v<N>> for $t<N> {
#[inline] #[inline]
fn binop(left: &$v<N>, right: &$t<N>) -> $v<N> { fn binop(left: &$v<N>, right: &$t<N>) -> $v<N> {
let mut res : $v<N> = $zero(); let mut res : $v<N> = $zero();
@ -414,7 +433,7 @@ macro_rules! vec_mul_mat_impl(
macro_rules! mat_mul_vec_impl( macro_rules! mat_mul_vec_impl(
($t: ident, $v: ident, $trhs: ident, $dim: expr, $zero: expr) => ( ($t: ident, $v: ident, $trhs: ident, $dim: expr, $zero: expr) => (
impl<N: Clone + Num> $trhs<N, $v<N>> for $v<N> { impl<N: Clone + BaseNum> $trhs<N, $v<N>> for $v<N> {
#[inline] #[inline]
fn binop(left: &$t<N>, right: &$v<N>) -> $v<N> { fn binop(left: &$t<N>, right: &$v<N>) -> $v<N> {
let mut res : $v<N> = $zero(); let mut res : $v<N> = $zero();
@ -448,7 +467,7 @@ macro_rules! mat_mul_pnt_impl(
macro_rules! inv_impl( macro_rules! inv_impl(
($t: ident, $dim: expr) => ( ($t: ident, $dim: expr) => (
impl<N: Clone + Num> 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(m: &$t<N>) -> Option<$t<N>> {
@ -463,7 +482,7 @@ macro_rules! inv_impl(
} }
fn inv(&mut self) -> bool { fn inv(&mut self) -> bool {
let mut res: $t<N> = One::one(); let mut res: $t<N> = ::one();
// inversion using Gauss-Jordan elimination // inversion using Gauss-Jordan elimination
for k in range(0u, $dim) { for k in range(0u, $dim) {
@ -474,7 +493,7 @@ macro_rules! inv_impl(
let mut n0 = k; // index of a non-zero entry let mut n0 = k; // index of a non-zero entry
while n0 != $dim { while n0 != $dim {
if self.at((n0, k)) != Zero::zero() { if self.at((n0, k)) != ::zero() {
break; break;
} }
@ -581,10 +600,10 @@ macro_rules! approx_eq_impl(
macro_rules! to_homogeneous_impl( macro_rules! to_homogeneous_impl(
($t: ident, $t2: ident, $dim: expr, $dim2: expr) => ( ($t: ident, $t2: ident, $dim: expr, $dim2: expr) => (
impl<N: Num + Clone> ToHomogeneous<$t2<N>> for $t<N> { impl<N: BaseNum + Clone> ToHomogeneous<$t2<N>> for $t<N> {
#[inline] #[inline]
fn to_homogeneous(m: &$t<N>) -> $t2<N> { fn to_homogeneous(m: &$t<N>) -> $t2<N> {
let mut res: $t2<N> = One::one(); let mut res: $t2<N> = ::one();
for i in range(0u, $dim) { for i in range(0u, $dim) {
for j in range(0u, $dim) { for j in range(0u, $dim) {
@ -600,10 +619,10 @@ macro_rules! to_homogeneous_impl(
macro_rules! from_homogeneous_impl( macro_rules! from_homogeneous_impl(
($t: ident, $t2: ident, $dim: expr, $dim2: expr) => ( ($t: ident, $t2: ident, $dim: expr, $dim2: expr) => (
impl<N: Num + Clone> FromHomogeneous<$t2<N>> for $t<N> { impl<N: BaseNum + Clone> FromHomogeneous<$t2<N>> for $t<N> {
#[inline] #[inline]
fn from(m: &$t2<N>) -> $t<N> { fn from(m: &$t2<N>) -> $t<N> {
let mut res: $t<N> = One::one(); let mut res: $t<N> = ::one();
for i in range(0u, $dim2) { for i in range(0u, $dim2) {
for j in range(0u, $dim2) { for j in range(0u, $dim2) {
@ -625,7 +644,7 @@ macro_rules! outer_impl(
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(a: &$t<N>, b: &$t<N>) -> $m<N> {
let mut res: $m<N> = Zero::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>>)) {
@ -642,7 +661,7 @@ macro_rules! outer_impl(
macro_rules! eigen_qr_impl( 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: Num + 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(m: &$t<N>, eps: &N, niter: uint) -> ($t<N>, $v<N>) {
linalg::eigen_qr(m, eps, niter) linalg::eigen_qr(m, eps, niter)
} }

View File

@ -1,4 +1,3 @@
use std::num::{Zero, One };
use std::num; use std::num;
use traits::structure::BaseFloat; use traits::structure::BaseFloat;
use structs::{Pnt3, Vec3, Mat4}; use structs::{Pnt3, Vec3, Mat4};
@ -25,9 +24,9 @@ pub struct OrthoMat3<N> {
impl<N: BaseFloat> Ortho3<N> { impl<N: BaseFloat> Ortho3<N> {
/// Creates a new 3D orthographic projection. /// Creates a new 3D orthographic projection.
pub fn new(width: N, height: N, znear: N, zfar: N) -> Ortho3<N> { pub fn new(width: N, height: N, znear: N, zfar: N) -> Ortho3<N> {
assert!(!(zfar - znear).is_zero()); assert!(!::is_zero(&(zfar - znear)));
assert!(!width.is_zero()); assert!(!::is_zero(&width));
assert!(!height.is_zero()); assert!(!::is_zero(&height));
Ortho3 { Ortho3 {
width: width, width: width,
@ -115,11 +114,11 @@ impl<N: BaseFloat + Clone> Ortho3<N> {
impl<N: BaseFloat> OrthoMat3<N> { impl<N: BaseFloat> OrthoMat3<N> {
/// Creates a new orthographic projection matrix from the width, heihgt, znear and zfar planes of the view cuboid. /// Creates a new orthographic projection matrix from the width, heihgt, znear and zfar planes of the view cuboid.
pub fn new(width: N, height: N, znear: N, zfar: N) -> OrthoMat3<N> { pub fn new(width: N, height: N, znear: N, zfar: N) -> OrthoMat3<N> {
assert!(!(zfar - znear).is_zero()); assert!(!::is_zero(&(zfar - znear)));
assert!(!width.is_zero()); assert!(!::is_zero(&width));
assert!(!height.is_zero()); assert!(!::is_zero(&height));
let mat: Mat4<N> = One::one(); let mat: Mat4<N> = ::one();
let mut res = OrthoMat3 { mat: mat }; let mut res = OrthoMat3 { mat: mat };
res.set_width(width); res.set_width(width);
@ -160,26 +159,26 @@ impl<N: BaseFloat> OrthoMat3<N> {
/// The near plane offset of the view cuboid. /// The near plane offset of the view cuboid.
#[inline] #[inline]
pub fn znear(&self) -> N { pub fn znear(&self) -> N {
(self.mat.m34 + One::one()) / self.mat.m33 (self.mat.m34 + ::one()) / self.mat.m33
} }
/// The far plane offset of the view cuboid. /// The far plane offset of the view cuboid.
#[inline] #[inline]
pub fn zfar(&self) -> N { pub fn zfar(&self) -> N {
(self.mat.m34 - One::one()) / self.mat.m33 (self.mat.m34 - ::one()) / self.mat.m33
} }
/// Sets the width of the view cuboid. /// Sets the width of the view cuboid.
#[inline] #[inline]
pub fn set_width(&mut self, width: N) { pub fn set_width(&mut self, width: N) {
assert!(!width.is_zero()); assert!(!::is_zero(&width));
self.mat.m11 = num::cast::<f64, N>(2.0).unwrap() / width; self.mat.m11 = num::cast::<f64, N>(2.0).unwrap() / width;
} }
/// Sets the height of the view cuboid. /// Sets the height of the view cuboid.
#[inline] #[inline]
pub fn set_height(&mut self, height: N) { pub fn set_height(&mut self, height: N) {
assert!(!height.is_zero()); assert!(!::is_zero(&height));
self.mat.m22 = num::cast::<f64, N>(2.0).unwrap() / height; self.mat.m22 = num::cast::<f64, N>(2.0).unwrap() / height;
} }
@ -200,7 +199,7 @@ impl<N: BaseFloat> OrthoMat3<N> {
/// Sets the near and far plane offsets of the view cuboid. /// Sets the near and far plane offsets of the view cuboid.
#[inline] #[inline]
pub fn set_znear_and_zfar(&mut self, znear: N, zfar: N) { pub fn set_znear_and_zfar(&mut self, znear: N, zfar: N) {
assert!(!(zfar - znear).is_zero()); assert!(!::is_zero(&(zfar - znear)));
self.mat.m33 = -num::cast::<f64, N>(2.0).unwrap() / (zfar - znear); self.mat.m33 = -num::cast::<f64, N>(2.0).unwrap() / (zfar - znear);
self.mat.m34 = -(zfar + znear) / (zfar - znear); self.mat.m34 = -(zfar + znear) / (zfar - znear);
} }

View File

@ -1,4 +1,3 @@
use std::num::{Zero, One};
use traits::structure::BaseFloat; use traits::structure::BaseFloat;
use structs::{Pnt3, Vec3, Mat4}; use structs::{Pnt3, Vec3, Mat4};
@ -24,8 +23,8 @@ pub struct PerspMat3<N> {
impl<N: BaseFloat> Persp3<N> { impl<N: BaseFloat> Persp3<N> {
/// Creates a new 3D perspective projection. /// Creates a new 3D perspective projection.
pub fn new(aspect: N, fov: N, znear: N, zfar: N) -> Persp3<N> { pub fn new(aspect: N, fov: N, znear: N, zfar: N) -> Persp3<N> {
assert!(!(zfar - znear).is_zero()); assert!(!::is_zero(&(zfar - znear)));
assert!(!aspect.is_zero()); assert!(!::is_zero(&aspect));
Persp3 { Persp3 {
aspect: aspect, aspect: aspect,
@ -121,17 +120,17 @@ impl<N: BaseFloat + Clone> Persp3<N> {
impl<N: BaseFloat> PerspMat3<N> { impl<N: BaseFloat> PerspMat3<N> {
/// Creates a new persepctive matrix from the aspect ratio, field of view, and near/far planes. /// Creates a new persepctive matrix from the aspect ratio, field of view, and near/far planes.
pub fn new(aspect: N, fov: N, znear: N, zfar: N) -> PerspMat3<N> { pub fn new(aspect: N, fov: N, znear: N, zfar: N) -> PerspMat3<N> {
assert!(!(znear - zfar).is_zero()); assert!(!::is_zero(&(znear - zfar)));
assert!(!aspect.is_zero()); assert!(!::is_zero(&aspect));
let mat: Mat4<N> = One::one(); let mat: Mat4<N> = ::one();
let mut res = PerspMat3 { mat: mat }; let mut res = PerspMat3 { mat: mat };
res.set_fov(fov); res.set_fov(fov);
res.set_aspect(aspect); res.set_aspect(aspect);
res.set_znear_and_zfar(znear, zfar); res.set_znear_and_zfar(znear, zfar);
res.mat.m44 = Zero::zero(); res.mat.m44 = ::zero();
res.mat.m43 = One::one(); res.mat.m43 = ::one();
res res
} }
@ -161,7 +160,7 @@ impl<N: BaseFloat> PerspMat3<N> {
/// Gets the field of view of the view frustrum. /// Gets the field of view of the view frustrum.
#[inline] #[inline]
pub fn fov(&self) -> N { pub fn fov(&self) -> N {
let _1: N = One::one(); let _1: N = ::one();
let _2 = _1 + _1; let _2 = _1 + _1;
(_1 / self.mat.m22).atan() * _2 (_1 / self.mat.m22).atan() * _2
@ -170,7 +169,7 @@ impl<N: BaseFloat> PerspMat3<N> {
/// Gets the near plane offset of the view frustrum. /// Gets the near plane offset of the view frustrum.
#[inline] #[inline]
pub fn znear(&self) -> N { pub fn znear(&self) -> N {
let _1: N = One::one(); let _1: N = ::one();
let _2 = _1 + _1; let _2 = _1 + _1;
let ratio = (self.mat.m33 + _1) / (self.mat.m33 - _1); let ratio = (self.mat.m33 + _1) / (self.mat.m33 - _1);
@ -180,7 +179,7 @@ impl<N: BaseFloat> PerspMat3<N> {
/// Gets the far plane offset of the view frustrum. /// Gets the far plane offset of the view frustrum.
#[inline] #[inline]
pub fn zfar(&self) -> N { pub fn zfar(&self) -> N {
let _1: N = One::one(); let _1: N = ::one();
let _2 = _1 + _1; let _2 = _1 + _1;
let ratio = (self.mat.m33 + _1) / (self.mat.m33 - _1); let ratio = (self.mat.m33 + _1) / (self.mat.m33 - _1);
@ -193,14 +192,14 @@ impl<N: BaseFloat> PerspMat3<N> {
/// frustrum. /// frustrum.
#[inline] #[inline]
pub fn set_aspect(&mut self, aspect: N) { pub fn set_aspect(&mut self, aspect: N) {
assert!(!aspect.is_zero()); assert!(!::is_zero(&aspect));
self.mat.m11 = -self.mat.m22 / aspect; self.mat.m11 = -self.mat.m22 / aspect;
} }
/// Updates this projection with a new field of view of the view frustrum. /// Updates this projection with a new field of view of the view frustrum.
#[inline] #[inline]
pub fn set_fov(&mut self, fov: N) { pub fn set_fov(&mut self, fov: N) {
let _1: N = One::one(); let _1: N = ::one();
let _2 = _1 + _1; let _2 = _1 + _1;
let old_m22 = self.mat.m22.clone(); let old_m22 = self.mat.m22.clone();
@ -225,7 +224,7 @@ impl<N: BaseFloat> PerspMat3<N> {
/// Updates this projection matrix with new near and far plane offsets of the view frustrum. /// Updates this projection matrix with new near and far plane offsets of the view frustrum.
#[inline] #[inline]
pub fn set_znear_and_zfar(&mut self, znear: N, zfar: N) { pub fn set_znear_and_zfar(&mut self, znear: N, zfar: N) {
let _1: N = One::one(); let _1: N = ::one();
let _2 = _1 + _1; let _2 = _1 + _1;
self.mat.m33 = -(zfar + znear) / (znear - zfar); self.mat.m33 = -(zfar + znear) / (znear - zfar);
@ -235,7 +234,7 @@ impl<N: BaseFloat> PerspMat3<N> {
/// Projects a point. /// Projects a point.
#[inline] #[inline]
pub fn project_pnt(&self, p: &Pnt3<N>) -> Pnt3<N> { pub fn project_pnt(&self, p: &Pnt3<N>) -> Pnt3<N> {
let _1: N = One::one(); let _1: N = ::one();
let inv_denom = _1 / p.z; let inv_denom = _1 / p.z;
Pnt3::new( Pnt3::new(
self.mat.m11 * p.x * inv_denom, self.mat.m11 * p.x * inv_denom,
@ -247,7 +246,7 @@ impl<N: BaseFloat> PerspMat3<N> {
/// Projects a vector. /// Projects a vector.
#[inline] #[inline]
pub fn project_vec(&self, p: &Vec3<N>) -> Vec3<N> { pub fn project_vec(&self, p: &Vec3<N>) -> Vec3<N> {
let _1: N = One::one(); let _1: N = ::one();
let inv_denom = _1 / p.z; let inv_denom = _1 / p.z;
Vec3::new( Vec3::new(
self.mat.m11 * p.x * inv_denom, self.mat.m11 * p.x * inv_denom,

View File

@ -3,14 +3,13 @@
#![allow(missing_docs)] // we allow missing to avoid having to document the point components. #![allow(missing_docs)] // we allow missing to avoid having to document the point components.
use std::mem; use std::mem;
use std::num::{Zero, One, Bounded, Num};
use std::slice::{Items, MutItems}; use std::slice::{Items, MutItems};
use std::iter::{Iterator, FromIterator}; use std::iter::{Iterator, FromIterator};
use traits::operations::{ApproxEq, POrd, POrdering, PartialLess, PartialEqual, use traits::operations::{ApproxEq, POrd, POrdering, PartialLess, PartialEqual,
PartialGreater, NotComparable, Axpy, ScalarAdd, ScalarSub, ScalarMul, PartialGreater, NotComparable, Axpy, ScalarAdd, ScalarSub, ScalarMul,
ScalarDiv}; ScalarDiv};
use traits::structure::{Cast, Dim, Indexable, Iterable, IterableMut, PntAsVec, Shape, use traits::structure::{Cast, Dim, Indexable, Iterable, IterableMut, PntAsVec, Shape,
NumPnt, FloatPnt, BaseFloat}; NumPnt, FloatPnt, BaseFloat, BaseNum, Zero, One, Bounded};
use traits::geometry::{Orig, FromHomogeneous, ToHomogeneous}; use traits::geometry::{Orig, FromHomogeneous, ToHomogeneous};
use structs::vec::{Vec1, Vec2, Vec3, Vec4, Vec5, Vec6}; use structs::vec::{Vec1, Vec2, Vec3, Vec4, Vec5, Vec6};

View File

@ -6,8 +6,8 @@ macro_rules! orig_impl(
#[inline] #[inline]
fn orig() -> $t<N> { fn orig() -> $t<N> {
$t { $t {
$comp0: Zero::zero() $comp0: ::zero()
$(, $compN: Zero::zero() )* $(, $compN: ::zero() )*
} }
} }
@ -106,7 +106,7 @@ macro_rules! pnt_to_homogeneous_impl(
res.$comp0 = v.$comp0.clone(); res.$comp0 = v.$comp0.clone();
$( res.$compN = v.$compN.clone(); )* $( res.$compN = v.$compN.clone(); )*
res.$extra = One::one(); res.$extra = ::one();
res res
} }
@ -132,11 +132,11 @@ macro_rules! pnt_from_homogeneous_impl(
macro_rules! num_float_pnt_impl( macro_rules! num_float_pnt_impl(
($t: ident, $tv: ident $(,$trhs: ident)*) => ( ($t: ident, $tv: ident $(,$trhs: ident)*) => (
impl<N> NumPnt<N, $tv<N>> for $t<N> impl<N> NumPnt<N, $tv<N>> for $t<N>
where N: Num $(+ $trhs<N, $t<N>>)* { where N: BaseNum + Zero $(+ $trhs<N, $t<N>>)* {
} }
impl<N> FloatPnt<N, $tv<N>> for $t<N> impl<N> FloatPnt<N, $tv<N>> for $t<N>
where N: Num + One + Zero + ApproxEq<N> + BaseFloat $(+ $trhs<N, $t<N>>)* { where N: BaseNum + One + Zero + ApproxEq<N> + BaseFloat $(+ $trhs<N, $t<N>>)* {
} }
) )
) )

View File

@ -3,7 +3,6 @@
#![allow(missing_docs)] // we allow missing to avoid having to document the dispatch trait. #![allow(missing_docs)] // we allow missing to avoid having to document the dispatch trait.
use std::mem; use std::mem;
use std::num::{Zero, One, Bounded, Num};
use std::num; use std::num;
use std::rand::{Rand, Rng}; use std::rand::{Rand, Rng};
use std::slice::{Items, MutItems}; use std::slice::{Items, MutItems};
@ -11,11 +10,12 @@ use structs::{Vec3, Pnt3, Rot3, Mat3, Vec3MulRhs, Pnt3MulRhs};
use traits::operations::{ApproxEq, Inv, POrd, POrdering, NotComparable, PartialLess, use traits::operations::{ApproxEq, Inv, POrd, POrdering, NotComparable, PartialLess,
PartialGreater, PartialEqual, Axpy, ScalarAdd, ScalarSub, ScalarMul, PartialGreater, PartialEqual, Axpy, ScalarAdd, ScalarSub, ScalarMul,
ScalarDiv}; ScalarDiv};
use traits::structure::{Cast, Indexable, Iterable, IterableMut, Dim, Shape, BaseFloat}; use traits::structure::{Cast, Indexable, Iterable, IterableMut, Dim, Shape, BaseFloat, BaseNum, Zero,
One, Bounded};
use traits::geometry::{Norm, Cross, Rotation, Rotate, Transform}; use traits::geometry::{Norm, Cross, Rotation, Rotate, Transform};
/// A quaternion. /// A quaternion.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)]
pub struct Quat<N> { pub struct Quat<N> {
/// The scalar component of the quaternion. /// The scalar component of the quaternion.
pub w: N, pub w: N,
@ -82,7 +82,7 @@ impl<N: BaseFloat + ApproxEq<N> + Clone> Inv for Quat<N> {
fn inv(&mut self) -> bool { fn inv(&mut self) -> bool {
let sqnorm = Norm::sqnorm(self); let sqnorm = Norm::sqnorm(self);
if ApproxEq::approx_eq(&sqnorm, &Zero::zero()) { if ApproxEq::approx_eq(&sqnorm, &::zero()) {
false false
} }
else { else {
@ -152,8 +152,8 @@ impl<N: BaseFloat> UnitQuat<N> {
pub fn new(axisangle: Vec3<N>) -> UnitQuat<N> { pub fn new(axisangle: Vec3<N>) -> UnitQuat<N> {
let sqang = Norm::sqnorm(&axisangle); let sqang = Norm::sqnorm(&axisangle);
if sqang.is_zero() { if ::is_zero(&sqang) {
One::one() ::one()
} }
else { else {
let ang = sqang.sqrt(); let ang = sqang.sqrt();
@ -251,11 +251,11 @@ impl<N> UnitQuat<N> {
} }
} }
impl<N: Num + Clone> One for UnitQuat<N> { impl<N: BaseNum + Clone> One for UnitQuat<N> {
#[inline] #[inline]
fn one() -> UnitQuat<N> { fn one() -> UnitQuat<N> {
unsafe { unsafe {
UnitQuat::new_with_unit_quat(Quat::new(One::one(), Zero::zero(), Zero::zero(), Zero::zero())) UnitQuat::new_with_unit_quat(Quat::new(::one(), ::zero(), ::zero(), ::zero()))
} }
} }
} }
@ -308,17 +308,17 @@ impl<N: BaseFloat + ApproxEq<N> + Clone> Div<UnitQuat<N>, UnitQuat<N>> for UnitQ
} }
} }
impl<N: Num + Clone> UnitQuatMulRhs<N, UnitQuat<N>> for UnitQuat<N> { impl<N: BaseNum + Clone> UnitQuatMulRhs<N, UnitQuat<N>> for UnitQuat<N> {
#[inline] #[inline]
fn binop(left: &UnitQuat<N>, right: &UnitQuat<N>) -> UnitQuat<N> { fn binop(left: &UnitQuat<N>, right: &UnitQuat<N>) -> UnitQuat<N> {
UnitQuat { q: left.q * right.q } UnitQuat { q: left.q * right.q }
} }
} }
impl<N: Num + Clone> UnitQuatMulRhs<N, Vec3<N>> for Vec3<N> { impl<N: BaseNum + Clone> UnitQuatMulRhs<N, Vec3<N>> for Vec3<N> {
#[inline] #[inline]
fn binop(left: &UnitQuat<N>, right: &Vec3<N>) -> Vec3<N> { fn binop(left: &UnitQuat<N>, right: &Vec3<N>) -> Vec3<N> {
let _2: N = num::one::<N>() + num::one(); let _2: N = ::one::<N>() + ::one();
let mut t = Cross::cross(left.q.vector(), right); let mut t = Cross::cross(left.q.vector(), right);
t.x = t.x * _2; t.x = t.x * _2;
t.y = t.y * _2; t.y = t.y * _2;
@ -330,14 +330,14 @@ impl<N: Num + Clone> UnitQuatMulRhs<N, Vec3<N>> for Vec3<N> {
} }
} }
impl<N: Num + Clone> UnitQuatMulRhs<N, Pnt3<N>> for Pnt3<N> { impl<N: BaseNum + Clone> UnitQuatMulRhs<N, Pnt3<N>> for Pnt3<N> {
#[inline] #[inline]
fn binop(left: &UnitQuat<N>, right: &Pnt3<N>) -> Pnt3<N> { fn binop(left: &UnitQuat<N>, right: &Pnt3<N>) -> Pnt3<N> {
::orig::<Pnt3<N>>() + *left * *right.as_vec() ::orig::<Pnt3<N>>() + *left * *right.as_vec()
} }
} }
impl<N: Num + Clone> Vec3MulRhs<N, Vec3<N>> for UnitQuat<N> { impl<N: BaseNum + Clone> Vec3MulRhs<N, Vec3<N>> for UnitQuat<N> {
#[inline] #[inline]
fn binop(left: &Vec3<N>, right: &UnitQuat<N>) -> Vec3<N> { fn binop(left: &Vec3<N>, right: &UnitQuat<N>) -> Vec3<N> {
let mut inv_quat = right.clone(); let mut inv_quat = right.clone();
@ -347,7 +347,7 @@ impl<N: Num + Clone> Vec3MulRhs<N, Vec3<N>> for UnitQuat<N> {
} }
} }
impl<N: Num + Clone> Pnt3MulRhs<N, Pnt3<N>> for UnitQuat<N> { impl<N: BaseNum + Clone> Pnt3MulRhs<N, Pnt3<N>> for UnitQuat<N> {
#[inline] #[inline]
fn binop(left: &Pnt3<N>, right: &UnitQuat<N>) -> Pnt3<N> { fn binop(left: &Pnt3<N>, right: &UnitQuat<N>) -> Pnt3<N> {
::orig::<Pnt3<N>>() + *left.as_vec() * *right ::orig::<Pnt3<N>>() + *left.as_vec() * *right
@ -357,12 +357,12 @@ impl<N: Num + Clone> Pnt3MulRhs<N, Pnt3<N>> for UnitQuat<N> {
impl<N: BaseFloat + Clone> Rotation<Vec3<N>> for UnitQuat<N> { impl<N: BaseFloat + Clone> Rotation<Vec3<N>> for UnitQuat<N> {
#[inline] #[inline]
fn rotation(&self) -> Vec3<N> { fn rotation(&self) -> Vec3<N> {
let _2 = num::one::<N>() + num::one(); let _2 = ::one::<N>() + ::one();
let mut v = self.q.vector().clone(); let mut v = self.q.vector().clone();
let ang = _2 * v.normalize().atan2(self.q.w); let ang = _2 * v.normalize().atan2(self.q.w);
if ang.is_zero() { if ::is_zero(&ang) {
num::zero() ::zero()
} }
else { else {
Vec3::new(v.x * ang, v.y * ang, v.z * ang) Vec3::new(v.x * ang, v.y * ang, v.z * ang)
@ -400,7 +400,7 @@ impl<N: BaseFloat + Clone> Rotation<Vec3<N>> for UnitQuat<N> {
} }
} }
impl<N: Num + Clone> Rotate<Vec3<N>> for UnitQuat<N> { impl<N: BaseNum + Clone> Rotate<Vec3<N>> for UnitQuat<N> {
#[inline] #[inline]
fn rotate(&self, v: &Vec3<N>) -> Vec3<N> { fn rotate(&self, v: &Vec3<N>) -> Vec3<N> {
*self * *v *self * *v
@ -412,7 +412,7 @@ impl<N: Num + Clone> Rotate<Vec3<N>> for UnitQuat<N> {
} }
} }
impl<N: Num + Clone> Rotate<Pnt3<N>> for UnitQuat<N> { impl<N: BaseNum + Clone> Rotate<Pnt3<N>> for UnitQuat<N> {
#[inline] #[inline]
fn rotate(&self, p: &Pnt3<N>) -> Pnt3<N> { fn rotate(&self, p: &Pnt3<N>) -> Pnt3<N> {
*self * *p *self * *p
@ -424,7 +424,7 @@ impl<N: Num + Clone> Rotate<Pnt3<N>> for UnitQuat<N> {
} }
} }
impl<N: Num + Clone> Transform<Vec3<N>> for UnitQuat<N> { impl<N: BaseNum + Clone> Transform<Vec3<N>> for UnitQuat<N> {
#[inline] #[inline]
fn transform(&self, v: &Vec3<N>) -> Vec3<N> { fn transform(&self, v: &Vec3<N>) -> Vec3<N> {
*self * *v *self * *v
@ -436,7 +436,7 @@ impl<N: Num + Clone> Transform<Vec3<N>> for UnitQuat<N> {
} }
} }
impl<N: Num + Clone> Transform<Pnt3<N>> for UnitQuat<N> { impl<N: BaseNum + Clone> Transform<Pnt3<N>> for UnitQuat<N> {
#[inline] #[inline]
fn transform(&self, p: &Pnt3<N>) -> Pnt3<N> { fn transform(&self, p: &Pnt3<N>) -> Pnt3<N> {
*self * *p *self * *p
@ -520,6 +520,7 @@ vec_sub_scalar_impl!(Quat, i16, QuatSubRhs, w, i, j, k)
vec_sub_scalar_impl!(Quat, i8, QuatSubRhs, w, i, j, k) vec_sub_scalar_impl!(Quat, i8, QuatSubRhs, w, i, j, k)
vec_sub_scalar_impl!(Quat, uint, QuatSubRhs, w, i, j, k) vec_sub_scalar_impl!(Quat, uint, QuatSubRhs, w, i, j, k)
vec_sub_scalar_impl!(Quat, int, QuatSubRhs, w, i, j, k) vec_sub_scalar_impl!(Quat, int, QuatSubRhs, w, i, j, k)
zero_one_impl!(Quat, w, i, j, k)
approx_eq_impl!(Quat, w, i, j, k) approx_eq_impl!(Quat, w, i, j, k)
from_iterator_impl!(Quat, iterator, iterator, iterator, iterator) from_iterator_impl!(Quat, iterator, iterator, iterator, iterator)
bounded_impl!(Quat, w, i, j, k) bounded_impl!(Quat, w, i, j, k)

View File

@ -2,11 +2,10 @@
#![allow(missing_docs)] #![allow(missing_docs)]
use std::num::{Zero, One, Num};
use std::rand::{Rand, Rng}; use std::rand::{Rand, Rng};
use traits::geometry::{Rotate, Rotation, AbsoluteRotate, RotationMatrix, Transform, ToHomogeneous, use traits::geometry::{Rotate, Rotation, AbsoluteRotate, RotationMatrix, Transform, ToHomogeneous,
Norm, Cross}; Norm, Cross};
use traits::structure::{Cast, Dim, Row, Col, BaseFloat}; use traits::structure::{Cast, Dim, Row, Col, BaseFloat, BaseNum, Zero, One};
use traits::operations::{Absolute, Inv, Transpose, ApproxEq}; use traits::operations::{Absolute, Inv, Transpose, ApproxEq};
use structs::vec::{Vec1, Vec2, Vec3, Vec4, Vec2MulRhs, Vec3MulRhs, Vec4MulRhs}; use structs::vec::{Vec1, Vec2, Vec3, Vec4, Vec2MulRhs, Vec3MulRhs, Vec4MulRhs};
use structs::pnt::{Pnt2, Pnt3, Pnt4, Pnt2MulRhs, Pnt3MulRhs, Pnt4MulRhs}; use structs::pnt::{Pnt2, Pnt3, Pnt4, Pnt2MulRhs, Pnt3MulRhs, Pnt4MulRhs};
@ -104,13 +103,13 @@ impl<N: Clone + BaseFloat> Rot3<N> {
/// * `axisangle` - A vector representing the rotation. Its magnitude is the amount of rotation /// * `axisangle` - A vector representing the rotation. Its magnitude is the amount of rotation
/// in radian. Its direction is the axis of rotation. /// in radian. Its direction is the axis of rotation.
pub fn new(axisangle: Vec3<N>) -> Rot3<N> { pub fn new(axisangle: Vec3<N>) -> Rot3<N> {
if Norm::sqnorm(&axisangle).is_zero() { if ::is_zero(&Norm::sqnorm(&axisangle)) {
One::one() ::one()
} }
else { else {
let mut axis = axisangle; let mut axis = axisangle;
let angle = axis.normalize(); let angle = axis.normalize();
let _1: N = One::one(); let _1: N = ::one();
let ux = axis.x.clone(); let ux = axis.x.clone();
let uy = axis.y.clone(); let uy = axis.y.clone();
let uz = axis.z.clone(); let uz = axis.z.clone();
@ -209,14 +208,14 @@ impl<N: Clone + BaseFloat + Cast<f64>>
Rotation<Vec3<N>> for Rot3<N> { Rotation<Vec3<N>> for Rot3<N> {
#[inline] #[inline]
fn rotation(&self) -> Vec3<N> { fn rotation(&self) -> Vec3<N> {
let angle = ((self.submat.m11 + self.submat.m22 + self.submat.m33 - One::one()) / Cast::from(2.0)).acos(); let angle = ((self.submat.m11 + self.submat.m22 + self.submat.m33 - ::one()) / Cast::from(2.0)).acos();
if angle != angle { if angle != angle {
// FIXME: handle that correctly // FIXME: handle that correctly
Zero::zero() ::zero()
} }
else if angle.is_zero() { else if ::is_zero(&angle) {
Zero::zero() ::zero()
} }
else { else {
let m32_m23 = self.submat.m32 - self.submat.m23; let m32_m23 = self.submat.m32 - self.submat.m23;
@ -225,10 +224,10 @@ Rotation<Vec3<N>> for Rot3<N> {
let denom = (m32_m23 * m32_m23 + m13_m31 * m13_m31 + m21_m12 * m21_m12).sqrt(); let denom = (m32_m23 * m32_m23 + m13_m31 * m13_m31 + m21_m12 * m21_m12).sqrt();
if denom.is_zero() { if ::is_zero(&denom) {
// XXX: handle that properly // XXX: handle that properly
// panic!("Internal error: singularity.") // panic!("Internal error: singularity.")
Zero::zero() ::zero()
} }
else { else {
let a_d = angle / denom; let a_d = angle / denom;

View File

@ -34,7 +34,7 @@ macro_rules! rotate_impl(
} }
} }
impl<N: Num + Clone> $trhs<N> for $tv<N> { impl<N: BaseNum + Clone> $trhs<N> for $tv<N> {
#[inline] #[inline]
fn rotate(t: &$t<N>, v: &$tv<N>) -> $tv<N> { fn rotate(t: &$t<N>, v: &$tv<N>) -> $tv<N> {
*t * *v *t * *v
@ -46,7 +46,7 @@ macro_rules! rotate_impl(
} }
} }
impl<N: Num + Clone> $trhs<N> for $tp<N> { impl<N: BaseNum + Clone> $trhs<N> for $tp<N> {
#[inline] #[inline]
fn rotate(t: &$t<N>, p: &$tp<N>) -> $tp<N> { fn rotate(t: &$t<N>, p: &$tp<N>) -> $tp<N> {
*t * *p *t * *p
@ -83,7 +83,7 @@ macro_rules! transform_impl(
} }
} }
impl<N: Num + Clone> $trhs<N> for $tv<N> { impl<N: BaseNum + Clone> $trhs<N> for $tv<N> {
#[inline] #[inline]
fn transform(t: &$t<N>, v: &$tv<N>) -> $tv<N> { fn transform(t: &$t<N>, v: &$tv<N>) -> $tv<N> {
t.rotate(v) t.rotate(v)
@ -95,7 +95,7 @@ macro_rules! transform_impl(
} }
} }
impl<N: Num + Clone> $trhs<N> for $tp<N> { impl<N: BaseNum + Clone> $trhs<N> for $tp<N> {
#[inline] #[inline]
fn transform(t: &$t<N>, p: &$tp<N>) -> $tp<N> { fn transform(t: &$t<N>, p: &$tp<N>) -> $tp<N> {
t.rotate(p) t.rotate(p)
@ -122,7 +122,7 @@ macro_rules! dim_impl(
macro_rules! rotation_matrix_impl( macro_rules! rotation_matrix_impl(
($t: ident, $tlv: ident, $tav: ident) => ( ($t: ident, $tlv: ident, $tav: ident) => (
impl<N: Zero + Num + Cast<f64> + BaseFloat> RotationMatrix<N, $tlv<N>, $tav<N>, $t<N>> for $t<N> { impl<N: Zero + BaseNum + Cast<f64> + BaseFloat> RotationMatrix<N, $tlv<N>, $tav<N>, $t<N>> for $t<N> {
#[inline] #[inline]
fn to_rot_mat(&self) -> $t<N> { fn to_rot_mat(&self) -> $t<N> {
self.clone() self.clone()
@ -133,10 +133,10 @@ macro_rules! rotation_matrix_impl(
macro_rules! one_impl( macro_rules! one_impl(
($t: ident) => ( ($t: ident) => (
impl<N: Num + Clone> One for $t<N> { impl<N: BaseNum + Clone> One for $t<N> {
#[inline] #[inline]
fn one() -> $t<N> { fn one() -> $t<N> {
$t { submat: One::one() } $t { submat: ::one() }
} }
} }
) )
@ -144,7 +144,7 @@ macro_rules! one_impl(
macro_rules! rot_mul_rot_impl( macro_rules! rot_mul_rot_impl(
($t: ident, $mulrhs: ident) => ( ($t: ident, $mulrhs: ident) => (
impl<N: Num + Clone> $mulrhs<N, $t<N>> for $t<N> { impl<N: BaseNum + Clone> $mulrhs<N, $t<N>> for $t<N> {
#[inline] #[inline]
fn binop(left: &$t<N>, right: &$t<N>) -> $t<N> { fn binop(left: &$t<N>, right: &$t<N>) -> $t<N> {
$t { submat: left.submat * right.submat } $t { submat: left.submat * right.submat }
@ -155,7 +155,7 @@ macro_rules! rot_mul_rot_impl(
macro_rules! rot_mul_vec_impl( macro_rules! rot_mul_vec_impl(
($t: ident, $tv: ident, $mulrhs: ident) => ( ($t: ident, $tv: ident, $mulrhs: ident) => (
impl<N: Num + Clone> $mulrhs<N, $tv<N>> for $tv<N> { impl<N: BaseNum + Clone> $mulrhs<N, $tv<N>> for $tv<N> {
#[inline] #[inline]
fn binop(left: &$t<N>, right: &$tv<N>) -> $tv<N> { fn binop(left: &$t<N>, right: &$tv<N>) -> $tv<N> {
left.submat * *right left.submat * *right
@ -172,7 +172,7 @@ macro_rules! rot_mul_pnt_impl(
macro_rules! vec_mul_rot_impl( macro_rules! vec_mul_rot_impl(
($t: ident, $tv: ident, $mulrhs: ident) => ( ($t: ident, $tv: ident, $mulrhs: ident) => (
impl<N: Num + Clone> $mulrhs<N, $tv<N>> for $t<N> { impl<N: BaseNum + Clone> $mulrhs<N, $tv<N>> for $t<N> {
#[inline] #[inline]
fn binop(left: &$tv<N>, right: &$t<N>) -> $tv<N> { fn binop(left: &$tv<N>, right: &$t<N>) -> $tv<N> {
*left * right.submat *left * right.submat
@ -281,7 +281,7 @@ macro_rules! index_impl(
macro_rules! to_homogeneous_impl( macro_rules! to_homogeneous_impl(
($t: ident, $tm: ident) => ( ($t: ident, $tm: ident) => (
impl<N: Num + Clone> ToHomogeneous<$tm<N>> for $t<N> { impl<N: BaseNum + Clone> ToHomogeneous<$tm<N>> for $t<N> {
#[inline] #[inline]
fn to_homogeneous(m: &$t<N>) -> $tm<N> { fn to_homogeneous(m: &$t<N>) -> $tm<N> {
ToHomogeneous::to_homogeneous(&m.submat) ToHomogeneous::to_homogeneous(&m.submat)

View File

@ -5,21 +5,21 @@ use extra::complex::Cmplx;
use traits::operations::{Absolute, Inv}; use traits::operations::{Absolute, Inv};
use traits::structure::{Dim}; use traits::structure::{Dim};
impl<N: Clone + Num> Absolute<Cmplx<N>> for Cmplx<N> { impl<N: Clone + BaseNum> Absolute<Cmplx<N>> for Cmplx<N> {
#[inline] #[inline]
fn absolute(&self) -> Cmplx<N> { fn absolute(&self) -> Cmplx<N> {
Cmplx::new(self.re.clone(), self.im.clone()) Cmplx::new(self.re.clone(), self.im.clone())
} }
} }
impl<N: Clone + Num + NumCast + Zero> Inv for Cmplx<N> { impl<N: Clone + BaseNum + BaseNumCast + Zero> Inv for Cmplx<N> {
#[inline] #[inline]
fn inverse(&self) -> Option<Cmplx<N>> { fn inverse(&self) -> Option<Cmplx<N>> {
if self.is_zero() { if self.is_zero() {
None None
} }
else { else {
let _1: N = NumCast::from(1.0); let _1: N = BaseNumCast::from(1.0);
let divisor = _1 / (self.re * self.re - self.im * self.im); let divisor = _1 / (self.re * self.re - self.im * self.im);
Some(Cmplx::new(self.re * divisor, -self.im * divisor)) Some(Cmplx::new(self.re * divisor, -self.im * divisor))
@ -32,7 +32,7 @@ impl<N: Clone + Num + NumCast + Zero> Inv for Cmplx<N> {
false false
} }
else { else {
let _1: N = NumCast::from(1.0); let _1: N = BaseNumCast::from(1.0);
let divisor = _1 / (self.re * self.re - self.im * self.im); let divisor = _1 / (self.re * self.re - self.im * self.im);
self.re = self.re * divisor; self.re = self.re * divisor;

View File

@ -1,7 +1,8 @@
use std::num::{One, Zero};
use structs::mat; use structs::mat;
use traits::operations::{Inv, Transpose}; use traits::operations::{Inv, Transpose};
use traits::geometry::{Translation, Translate, Rotation, Rotate, Transformation, Transform, AbsoluteRotate}; use traits::structure::{Zero, One};
use traits::geometry::{Translation, Translate, Rotation, Rotate, Transformation, Transform,
AbsoluteRotate};
impl One for mat::Identity { impl One for mat::Identity {
#[inline] #[inline]
@ -41,12 +42,12 @@ impl Transpose for mat::Identity {
impl<V: Zero> Translation<V> for mat::Identity { impl<V: Zero> Translation<V> for mat::Identity {
#[inline] #[inline]
fn translation(&self) -> V { fn translation(&self) -> V {
Zero::zero() ::zero()
} }
#[inline] #[inline]
fn inv_translation(&self) -> V { fn inv_translation(&self) -> V {
Zero::zero() ::zero()
} }
#[inline] #[inline]
@ -90,12 +91,12 @@ impl<V: Clone> Translate<V> for mat::Identity {
impl<V: Zero> Rotation<V> for mat::Identity { impl<V: Zero> Rotation<V> for mat::Identity {
#[inline] #[inline]
fn rotation(&self) -> V { fn rotation(&self) -> V {
Zero::zero() ::zero()
} }
#[inline] #[inline]
fn inv_rotation(&self) -> V { fn inv_rotation(&self) -> V {
Zero::zero() ::zero()
} }
#[inline] #[inline]
@ -146,12 +147,12 @@ impl<V: Clone> AbsoluteRotate<V> for mat::Identity {
impl<M: One> Transformation<M> for mat::Identity { impl<M: One> Transformation<M> for mat::Identity {
#[inline] #[inline]
fn transformation(&self) -> M { fn transformation(&self) -> M {
One::one() ::one()
} }
#[inline] #[inline]
fn inv_transformation(&self) -> M { fn inv_transformation(&self) -> M {
One::one() ::one()
} }
#[inline] #[inline]

View File

@ -1,12 +1,11 @@
use std::num::{Zero, One, Num};
use structs::vec::{Vec2, Vec3, Vec2MulRhs, Vec3MulRhs}; use structs::vec::{Vec2, Vec3, Vec2MulRhs, Vec3MulRhs};
use structs::pnt::{Pnt2, Pnt3, Pnt2MulRhs, Pnt3MulRhs}; use structs::pnt::{Pnt2, Pnt3, Pnt2MulRhs, Pnt3MulRhs};
use structs::mat::{Mat1, Mat2, Mat3, Mat3MulRhs, Mat2MulRhs}; use structs::mat::{Mat1, Mat2, Mat3, Mat3MulRhs, Mat2MulRhs};
use traits::operations::{Inv, Det, ApproxEq}; use traits::operations::{Inv, Det, ApproxEq};
use traits::structure::{Row, Col, BaseFloat}; use traits::structure::{Row, Col, BaseNum};
// some specializations: // some specializations:
impl<N: Num + 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(m: &Mat1<N>) -> Option<Mat1<N>> {
let mut res = m.clone(); let mut res = m.clone();
@ -21,11 +20,11 @@ impl<N: Num + ApproxEq<N> + Clone> Inv for Mat1<N> {
#[inline] #[inline]
fn inv(&mut self) -> bool { fn inv(&mut self) -> bool {
if ApproxEq::approx_eq(&self.m11, &Zero::zero()) { if ApproxEq::approx_eq(&self.m11, &::zero()) {
false false
} }
else { else {
let _1: N = One::one(); let _1: N = ::one();
self.m11 = _1 / Det::det(self); self.m11 = _1 / Det::det(self);
true true
@ -33,7 +32,7 @@ impl<N: Num + ApproxEq<N> + Clone> Inv for Mat1<N> {
} }
} }
impl<N: Num + 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(m: &Mat2<N>) -> Option<Mat2<N>> {
let mut res = m.clone(); let mut res = m.clone();
@ -50,7 +49,7 @@ impl<N: Num + ApproxEq<N> + Clone> Inv for Mat2<N> {
fn inv(&mut self) -> bool { fn inv(&mut self) -> bool {
let det = Det::det(self); let det = Det::det(self);
if ApproxEq::approx_eq(&det, &Zero::zero()) { if ApproxEq::approx_eq(&det, &::zero()) {
false false
} }
else { else {
@ -63,7 +62,7 @@ impl<N: Num + ApproxEq<N> + Clone> Inv for Mat2<N> {
} }
} }
impl<N: Num + 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(m: &Mat3<N>) -> Option<Mat3<N>> {
let mut res = m.clone(); let mut res = m.clone();
@ -84,7 +83,7 @@ impl<N: Num + ApproxEq<N> + Clone> Inv for Mat3<N> {
let det = self.m11 * minor_m12_m23 - self.m12 * minor_m11_m23 + self.m13 * minor_m11_m22; let det = self.m11 * minor_m12_m23 - self.m12 * minor_m11_m23 + self.m13 * minor_m11_m22;
if ApproxEq::approx_eq(&det, &Zero::zero()) { if ApproxEq::approx_eq(&det, &::zero()) {
false false
} }
else { else {
@ -107,21 +106,21 @@ impl<N: Num + ApproxEq<N> + Clone> Inv for Mat3<N> {
} }
} }
impl<N: Num + 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(m: &Mat1<N>) -> N {
m.m11.clone() m.m11.clone()
} }
} }
impl<N: Num> Det<N> for Mat2<N> { impl<N: BaseNum> Det<N> for Mat2<N> {
#[inline] #[inline]
fn det(m: &Mat2<N>) -> N { fn det(m: &Mat2<N>) -> N {
m.m11 * m.m22 - m.m21 * m.m12 m.m11 * m.m22 - m.m21 * m.m12
} }
} }
impl<N: Num> Det<N> for Mat3<N> { impl<N: BaseNum> Det<N> for Mat3<N> {
#[inline] #[inline]
fn det(m: &Mat3<N>) -> N { fn det(m: &Mat3<N>) -> N {
let minor_m12_m23 = m.m22 * m.m33 - m.m32 * m.m23; let minor_m12_m23 = m.m22 * m.m33 - m.m32 * m.m23;

View File

@ -1,5 +1,4 @@
use std::num::{Zero, One, Num}; use traits::structure::{Cast, Row, Basis, BaseFloat, Zero, One};
use traits::structure::{Cast, Row, Basis, BaseFloat};
use traits::geometry::{Norm, Cross, CrossMatrix, UniformSphereSample}; use traits::geometry::{Norm, Cross, CrossMatrix, UniformSphereSample};
use structs::vec::{Vec1, Vec2, Vec3, Vec4}; use structs::vec::{Vec1, Vec2, Vec3, Vec4};
use structs::mat::Mat3; use structs::mat::Mat3;
@ -34,9 +33,9 @@ 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(v: &Vec3<N>) -> Mat3<N> {
Mat3::new( Mat3::new(
Zero::zero(), -v.z , v.y.clone(), ::zero(), -v.z , v.y.clone(),
v.z.clone() , Zero::zero(), -v.x, v.z.clone() , ::zero(), -v.x,
-v.y , v.x.clone() , Zero::zero() -v.y , v.x.clone() , ::zero()
) )
} }
} }
@ -71,7 +70,7 @@ impl<N: Clone> Row<Vec1<N>> for Vec2<N> {
impl<N: One> Basis for Vec1<N> { impl<N: One> Basis for Vec1<N> {
#[inline(always)] #[inline(always)]
fn canonical_basis(f: |Vec1<N>| -> bool) { fn canonical_basis(f: |Vec1<N>| -> bool) {
f(Vec1::new(One::one())); f(Vec1::new(::one()));
} }
#[inline(always)] #[inline(always)]
@ -80,7 +79,7 @@ impl<N: One> Basis for Vec1<N> {
#[inline] #[inline]
fn canonical_basis_element(i: uint) -> Option<Vec1<N>> { fn canonical_basis_element(i: uint) -> Option<Vec1<N>> {
if i == 0 { if i == 0 {
Some(Vec1::new(One::one())) Some(Vec1::new(::one()))
} }
else { else {
None None
@ -91,8 +90,8 @@ impl<N: One> Basis for Vec1<N> {
impl<N: Clone + One + Zero + Neg<N>> Basis for Vec2<N> { impl<N: Clone + One + Zero + Neg<N>> Basis for Vec2<N> {
#[inline(always)] #[inline(always)]
fn canonical_basis(f: |Vec2<N>| -> bool) { fn canonical_basis(f: |Vec2<N>| -> bool) {
if !f(Vec2::new(One::one(), Zero::zero())) { return }; if !f(Vec2::new(::one(), ::zero())) { return };
f(Vec2::new(Zero::zero(), One::one())); f(Vec2::new(::zero(), ::one()));
} }
#[inline] #[inline]
@ -103,10 +102,10 @@ impl<N: Clone + One + Zero + Neg<N>> Basis for Vec2<N> {
#[inline] #[inline]
fn canonical_basis_element(i: uint) -> Option<Vec2<N>> { fn canonical_basis_element(i: uint) -> Option<Vec2<N>> {
if i == 0 { if i == 0 {
Some(Vec2::new(One::one(), Zero::zero())) Some(Vec2::new(::one(), ::zero()))
} }
else if i == 1 { else if i == 1 {
Some(Vec2::new(Zero::zero(), One::one())) Some(Vec2::new(::zero(), ::one()))
} }
else { else {
None None
@ -117,19 +116,19 @@ impl<N: Clone + One + Zero + Neg<N>> Basis for Vec2<N> {
impl<N: BaseFloat> Basis for Vec3<N> { impl<N: BaseFloat> Basis for Vec3<N> {
#[inline(always)] #[inline(always)]
fn canonical_basis(f: |Vec3<N>| -> bool) { fn canonical_basis(f: |Vec3<N>| -> bool) {
if !f(Vec3::new(One::one(), Zero::zero(), Zero::zero())) { return }; if !f(Vec3::new(::one(), ::zero(), ::zero())) { return };
if !f(Vec3::new(Zero::zero(), One::one(), Zero::zero())) { return }; if !f(Vec3::new(::zero(), ::one(), ::zero())) { return };
f(Vec3::new(Zero::zero(), Zero::zero(), One::one())); f(Vec3::new(::zero(), ::zero(), ::one()));
} }
#[inline(always)] #[inline(always)]
fn orthonormal_subspace_basis(n: &Vec3<N>, f: |Vec3<N>| -> bool) { fn orthonormal_subspace_basis(n: &Vec3<N>, f: |Vec3<N>| -> bool) {
let a = let a =
if n.x.clone().abs() > n.y.clone().abs() { if n.x.clone().abs() > n.y.clone().abs() {
Norm::normalize_cpy(&Vec3::new(n.z.clone(), Zero::zero(), -n.x)) Norm::normalize_cpy(&Vec3::new(n.z.clone(), ::zero(), -n.x))
} }
else { else {
Norm::normalize_cpy(&Vec3::new(Zero::zero(), -n.z, n.y.clone())) Norm::normalize_cpy(&Vec3::new(::zero(), -n.z, n.y.clone()))
}; };
if !f(Cross::cross(&a, n)) { return }; if !f(Cross::cross(&a, n)) { return };
@ -139,13 +138,13 @@ impl<N: BaseFloat> Basis for Vec3<N> {
#[inline] #[inline]
fn canonical_basis_element(i: uint) -> Option<Vec3<N>> { fn canonical_basis_element(i: uint) -> Option<Vec3<N>> {
if i == 0 { if i == 0 {
Some(Vec3::new(One::one(), Zero::zero(), Zero::zero())) Some(Vec3::new(::one(), ::zero(), ::zero()))
} }
else if i == 1 { else if i == 1 {
Some(Vec3::new(Zero::zero(), One::one(), Zero::zero())) Some(Vec3::new(::zero(), ::one(), ::zero()))
} }
else if i == 2 { else if i == 2 {
Some(Vec3::new(Zero::zero(), Zero::zero(), One::one())) Some(Vec3::new(::zero(), ::zero(), ::one()))
} }
else { else {
None None
@ -227,7 +226,7 @@ static SAMPLES_3_F64: [Vec3<f64>, ..42] = [
impl<N: One + Clone> UniformSphereSample for Vec1<N> { impl<N: One + Clone> UniformSphereSample for Vec1<N> {
#[inline(always)] #[inline(always)]
fn sample(f: |Vec1<N>| -> ()) { fn sample(f: |Vec1<N>| -> ()) {
f(One::one()) f(::one())
} }
} }

View File

@ -1,12 +1,24 @@
use std::mem; use std::mem;
use std::num::{Zero, One, Bounded, Num};
use std::slice::{Items, MutItems}; use std::slice::{Items, MutItems};
use std::iter::{Iterator, FromIterator}; use std::iter::{Iterator, FromIterator};
use traits::operations::ApproxEq; use traits::operations::ApproxEq;
use traits::structure::{Iterable, IterableMut, Indexable, Basis, Dim, Shape, BaseFloat}; use traits::structure::{Iterable, IterableMut, Indexable, Basis, Dim, Shape, BaseFloat, BaseNum,
Zero, One, Bounded};
use traits::geometry::{Translation, Dot, Norm}; use traits::geometry::{Translation, Dot, Norm};
use structs::vec; use structs::vec;
impl<N> Zero for vec::Vec0<N> {
#[inline]
fn zero() -> vec::Vec0<N> {
vec::Vec0
}
#[inline]
fn is_zero(&self) -> bool {
true
}
}
impl<N> Index<uint, N> for vec::Vec0<N> { impl<N> Index<uint, N> for vec::Vec0<N> {
#[inline] #[inline]
fn index(&self, _: &uint) -> &N { fn index(&self, _: &uint) -> &N {
@ -107,10 +119,10 @@ impl<N: Neg<N>> Neg<vec::Vec0<N>> for vec::Vec0<N> {
} }
} }
impl<N: Num> 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(_: &vec::Vec0<N>, _: &vec::Vec0<N>) -> N {
Zero::zero() ::zero()
} }
} }
@ -167,22 +179,22 @@ 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(_: &vec::Vec0<N>) -> N {
Zero::zero() ::zero()
} }
#[inline] #[inline]
fn norm(_: &vec::Vec0<N>) -> N { fn norm(_: &vec::Vec0<N>) -> N {
Zero::zero() ::zero()
} }
#[inline] #[inline]
fn normalize_cpy(_: &vec::Vec0<N>) -> vec::Vec0<N> { fn normalize_cpy(_: &vec::Vec0<N>) -> vec::Vec0<N> {
Zero::zero() ::zero()
} }
#[inline] #[inline]
fn normalize(&mut self) -> N { fn normalize(&mut self) -> N {
Zero::zero() ::zero()
} }
} }

View File

@ -3,7 +3,6 @@
#![allow(missing_docs)] // we allow missing to avoid having to document the dispatch traits. #![allow(missing_docs)] // we allow missing to avoid having to document the dispatch traits.
use std::mem; use std::mem;
use std::num::{Zero, One, Bounded, Num};
use std::slice::{Items, MutItems}; use std::slice::{Items, MutItems};
use std::iter::{Iterator, FromIterator}; use std::iter::{Iterator, FromIterator};
use traits::operations::{ApproxEq, POrd, POrdering, PartialLess, PartialEqual, use traits::operations::{ApproxEq, POrd, POrdering, PartialLess, PartialEqual,
@ -12,7 +11,7 @@ use traits::operations::{ApproxEq, POrd, POrdering, PartialLess, PartialEqual,
use traits::geometry::{Transform, Rotate, FromHomogeneous, ToHomogeneous, Dot, Norm, use traits::geometry::{Transform, Rotate, FromHomogeneous, ToHomogeneous, Dot, Norm,
Translation, Translate}; Translation, Translate};
use traits::structure::{Basis, Cast, Dim, Indexable, Iterable, IterableMut, VecAsPnt, Shape, use traits::structure::{Basis, Cast, Dim, Indexable, Iterable, IterableMut, VecAsPnt, Shape,
NumVec, FloatVec, BaseFloat}; NumVec, FloatVec, BaseFloat, BaseNum, Zero, One, Bounded};
use structs::pnt::{Pnt1, Pnt2, Pnt3, Pnt4, Pnt5, Pnt6}; use structs::pnt::{Pnt1, Pnt2, Pnt3, Pnt4, Pnt5, Pnt6};
@ -121,7 +120,7 @@ vec_sub_scalar_impl!(Vec1, int, Vec1SubRhs, x)
translation_impl!(Vec1) translation_impl!(Vec1)
norm_impl!(Vec1, x) norm_impl!(Vec1, x)
approx_eq_impl!(Vec1, x) approx_eq_impl!(Vec1, x)
one_impl!(Vec1, x) zero_one_impl!(Vec1, x)
from_iterator_impl!(Vec1, iterator) from_iterator_impl!(Vec1, iterator)
bounded_impl!(Vec1, x) bounded_impl!(Vec1, x)
axpy_impl!(Vec1, x) axpy_impl!(Vec1, x)
@ -226,7 +225,7 @@ vec_sub_scalar_impl!(Vec2, int, Vec2SubRhs, x, y)
translation_impl!(Vec2) translation_impl!(Vec2)
norm_impl!(Vec2, x, y) norm_impl!(Vec2, x, y)
approx_eq_impl!(Vec2, x, y) approx_eq_impl!(Vec2, x, y)
one_impl!(Vec2, x, y) zero_one_impl!(Vec2, x, y)
from_iterator_impl!(Vec2, iterator, iterator) from_iterator_impl!(Vec2, iterator, iterator)
bounded_impl!(Vec2, x, y) bounded_impl!(Vec2, x, y)
axpy_impl!(Vec2, x, y) axpy_impl!(Vec2, x, y)
@ -336,7 +335,7 @@ vec_sub_scalar_impl!(Vec3, int, Vec3SubRhs, x, y, z)
translation_impl!(Vec3) translation_impl!(Vec3)
norm_impl!(Vec3, x, y ,z) norm_impl!(Vec3, x, y ,z)
approx_eq_impl!(Vec3, x, y, z) approx_eq_impl!(Vec3, x, y, z)
one_impl!(Vec3, x, y, z) zero_one_impl!(Vec3, x, y, z)
from_iterator_impl!(Vec3, iterator, iterator, iterator) from_iterator_impl!(Vec3, iterator, iterator, iterator)
bounded_impl!(Vec3, x, y, z) bounded_impl!(Vec3, x, y, z)
axpy_impl!(Vec3, x, y, z) axpy_impl!(Vec3, x, y, z)
@ -446,7 +445,7 @@ vec_sub_scalar_impl!(Vec4, int, Vec4SubRhs, x, y, z, w)
translation_impl!(Vec4) translation_impl!(Vec4)
norm_impl!(Vec4, x, y, z, w) norm_impl!(Vec4, x, y, z, w)
approx_eq_impl!(Vec4, x, y, z, w) approx_eq_impl!(Vec4, x, y, z, w)
one_impl!(Vec4, x, y, z, w) zero_one_impl!(Vec4, x, y, z, w)
from_iterator_impl!(Vec4, iterator, iterator, iterator, iterator) from_iterator_impl!(Vec4, iterator, iterator, iterator, iterator)
bounded_impl!(Vec4, x, y, z, w) bounded_impl!(Vec4, x, y, z, w)
axpy_impl!(Vec4, x, y, z, w) axpy_impl!(Vec4, x, y, z, w)
@ -557,7 +556,7 @@ vec_sub_scalar_impl!(Vec5, int, Vec5SubRhs, x, y, z, w, a)
translation_impl!(Vec5) translation_impl!(Vec5)
norm_impl!(Vec5, x, y, z, w, a) norm_impl!(Vec5, x, y, z, w, a)
approx_eq_impl!(Vec5, x, y, z, w, a) approx_eq_impl!(Vec5, x, y, z, w, a)
one_impl!(Vec5, x, y, z, w, a) zero_one_impl!(Vec5, x, y, z, w, a)
from_iterator_impl!(Vec5, iterator, iterator, iterator, iterator, iterator) from_iterator_impl!(Vec5, iterator, iterator, iterator, iterator, iterator)
bounded_impl!(Vec5, x, y, z, w, a) bounded_impl!(Vec5, x, y, z, w, a)
axpy_impl!(Vec5, x, y, z, w, a) axpy_impl!(Vec5, x, y, z, w, a)
@ -670,7 +669,7 @@ vec_sub_scalar_impl!(Vec6, int, Vec6SubRhs, x, y, z, w, a, b)
translation_impl!(Vec6) translation_impl!(Vec6)
norm_impl!(Vec6, x, y, z, w, a, b) norm_impl!(Vec6, x, y, z, w, a, b)
approx_eq_impl!(Vec6, x, y, z, w, a, b) approx_eq_impl!(Vec6, x, y, z, w, a, b)
one_impl!(Vec6, x, y, z, w, a, b) zero_one_impl!(Vec6, x, y, z, w, a, b)
from_iterator_impl!(Vec6, iterator, iterator, iterator, iterator, iterator, iterator) from_iterator_impl!(Vec6, iterator, iterator, iterator, iterator, iterator, iterator)
bounded_impl!(Vec6, x, y, z, w, a, b) bounded_impl!(Vec6, x, y, z, w, a, b)
axpy_impl!(Vec6, x, y, z, w, a, b) axpy_impl!(Vec6, x, y, z, w, a, b)

View File

@ -135,9 +135,9 @@ macro_rules! vec_axis_impl(
/// Create a unit vector with its `$comp0` component equal to 1.0. /// Create a unit vector with its `$comp0` component equal to 1.0.
#[inline] #[inline]
pub fn $comp0() -> $t<N> { pub fn $comp0() -> $t<N> {
let mut res: $t<N> = Zero::zero(); let mut res: $t<N> = ::zero();
res.$comp0 = One::one(); res.$comp0 = ::one();
res res
} }
@ -146,9 +146,9 @@ macro_rules! vec_axis_impl(
/// Create a unit vector with its `$compN` component equal to 1.0. /// Create a unit vector with its `$compN` component equal to 1.0.
#[inline] #[inline]
pub fn $compN() -> $t<N> { pub fn $compN() -> $t<N> {
let mut res: $t<N> = Zero::zero(); let mut res: $t<N> = ::zero();
res.$compN = One::one(); res.$compN = ::one();
res res
} }
@ -308,10 +308,10 @@ macro_rules! basis_impl(
let mut basis: Vec<$t<N>> = Vec::new(); let mut basis: Vec<$t<N>> = Vec::new();
for i in range(0u, $dim) { for i in range(0u, $dim) {
let mut basis_element : $t<N> = Zero::zero(); let mut basis_element : $t<N> = ::zero();
unsafe { unsafe {
basis_element.set_fast(i, One::one()); basis_element.set_fast(i, ::one());
} }
if basis.len() == $dim - 1 { if basis.len() == $dim - 1 {
@ -326,7 +326,7 @@ macro_rules! basis_impl(
elt = elt - *v * Dot::dot(&elt, v) elt = elt - *v * Dot::dot(&elt, v)
}; };
if !ApproxEq::approx_eq(&Norm::sqnorm(&elt), &Zero::zero()) { if !ApproxEq::approx_eq(&Norm::sqnorm(&elt), &::zero()) {
let new_element = Norm::normalize_cpy(&elt); let new_element = Norm::normalize_cpy(&elt);
if !f(new_element.clone()) { return }; if !f(new_element.clone()) { return };
@ -339,10 +339,10 @@ macro_rules! basis_impl(
#[inline] #[inline]
fn canonical_basis_element(i: uint) -> Option<$t<N>> { fn canonical_basis_element(i: uint) -> Option<$t<N>> {
if i < $dim { if i < $dim {
let mut basis_element : $t<N> = Zero::zero(); let mut basis_element : $t<N> = ::zero();
unsafe { unsafe {
basis_element.set_fast(i, One::one()); basis_element.set_fast(i, ::one());
} }
Some(basis_element) Some(basis_element)
@ -424,7 +424,7 @@ macro_rules! neg_impl(
macro_rules! dot_impl( macro_rules! dot_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => ( ($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Num> 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(a: &$t<N>, b: &$t<N>) -> N {
a.$comp0 * b.$comp0 $(+ a.$compN * b.$compN )* a.$comp0 * b.$comp0 $(+ a.$compN * b.$compN )*
@ -608,17 +608,33 @@ macro_rules! approx_eq_impl(
) )
) )
macro_rules! one_impl( macro_rules! zero_one_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => ( ($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: One> One for $t<N> { impl<N: One> One for $t<N> {
#[inline] #[inline]
fn one() -> $t<N> { fn one() -> $t<N> {
$t { $t {
$comp0: One::one() $comp0: ::one()
$(, $compN: One::one() )* $(, $compN: ::one() )*
} }
} }
} }
impl<N: Zero> Zero for $t<N> {
#[inline]
fn zero() -> $t<N> {
$t {
$comp0: ::zero()
$(, $compN: ::zero() )*
}
}
#[inline]
fn is_zero(&self) -> bool {
self.$comp0.is_zero()
$(&& self.$compN.is_zero() )*
}
}
) )
) )
@ -659,7 +675,7 @@ macro_rules! vec_to_homogeneous_impl(
($t: ident, $t2: ident, $extra: ident, $comp0: ident $(,$compN: ident)*) => ( ($t: ident, $t2: ident, $extra: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Clone + One + Zero> ToHomogeneous<$t2<N>> for $t<N> { impl<N: Clone + One + Zero> ToHomogeneous<$t2<N>> for $t<N> {
fn to_homogeneous(v: &$t<N>) -> $t2<N> { fn to_homogeneous(v: &$t<N>) -> $t2<N> {
let mut res: $t2<N> = Zero::zero(); let mut res: $t2<N> = ::zero();
res.$comp0 = v.$comp0.clone(); res.$comp0 = v.$comp0.clone();
$( res.$compN = v.$compN.clone(); )* $( res.$compN = v.$compN.clone(); )*
@ -674,7 +690,7 @@ macro_rules! vec_from_homogeneous_impl(
($t: ident, $t2: ident, $extra: ident, $comp0: ident $(,$compN: ident)*) => ( ($t: ident, $t2: ident, $extra: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Clone + Div<N, N> + One + Zero> FromHomogeneous<$t2<N>> for $t<N> { impl<N: Clone + Div<N, N> + One + Zero> FromHomogeneous<$t2<N>> for $t<N> {
fn from(v: &$t2<N>) -> $t<N> { fn from(v: &$t2<N>) -> $t<N> {
let mut res: $t<N> = Zero::zero(); let mut res: $t<N> = ::zero();
res.$comp0 = v.$comp0.clone(); res.$comp0 = v.$comp0.clone();
$( res.$compN = v.$compN.clone(); )* $( res.$compN = v.$compN.clone(); )*
@ -765,11 +781,11 @@ macro_rules! vec_as_pnt_impl(
macro_rules! num_float_vec_impl( macro_rules! num_float_vec_impl(
($t: ident $(,$trhs: ident)*) => ( ($t: ident $(,$trhs: ident)*) => (
impl<N> NumVec<N> for $t<N> impl<N> NumVec<N> for $t<N>
where N: Num $(+ $trhs<N, $t<N>>)* { where N: BaseNum + Zero $(+ $trhs<N, $t<N>>)* {
} }
impl<N> FloatVec<N> for $t<N> impl<N> FloatVec<N> for $t<N>
where N: Num + Zero + One + ApproxEq<N> + BaseFloat $(+ $trhs<N, $t<N>>)* { where N: BaseNum + Zero + One + ApproxEq<N> + BaseFloat $(+ $trhs<N, $t<N>>)* {
} }
) )
) )

View File

@ -6,7 +6,8 @@ pub use traits::geometry::{AbsoluteRotate, Cross, CrossMatrix, Dot, FromHomogene
pub use traits::structure::{FloatVec, FloatPnt, Basis, Cast, Col, Dim, Indexable, Iterable, pub use traits::structure::{FloatVec, FloatPnt, Basis, Cast, Col, Dim, Indexable, Iterable,
IterableMut, Mat, SquareMat, Row, NumVec, NumPnt, PntAsVec, VecAsPnt, IterableMut, Mat, SquareMat, Row, NumVec, NumPnt, PntAsVec, VecAsPnt,
ColSlice, RowSlice, Diag, Eye, Shape, BaseFloat}; ColSlice, RowSlice, Diag, Eye, Shape, BaseFloat, BaseNum, Zero, One,
Bounded};
pub use traits::operations::{Absolute, ApproxEq, Axpy, Cov, Det, Inv, LMul, Mean, Outer, POrd, pub use traits::operations::{Absolute, ApproxEq, Axpy, Cov, Det, Inv, LMul, Mean, Outer, POrd,
RMul, ScalarAdd, ScalarSub, ScalarMul, ScalarDiv, Transpose, EigenQR}; RMul, ScalarAdd, ScalarSub, ScalarMul, ScalarDiv, Transpose, EigenQR};

View File

@ -257,27 +257,6 @@ pub trait EigenQR<N, V>: SquareMat<N, V> {
fn eigen_qr(m: &Self, eps: &N, niter: uint) -> (Self, V); fn eigen_qr(m: &Self, eps: &N, niter: uint) -> (Self, V);
} }
// /// Cholesky decomposition.
// pub trait Chol {
// /// Performs the cholesky decomposition on `self`. The resulting upper-triangular matrix is
// /// returned. Returns `None` if the matrix is not symetric positive-definite.
// fn chol(self) -> Option<Self>;
//
// /// Performs the cholesky decomposition on `self`. The resulting upper-triangular matrix is
// /// written to a given parameter. If the decomposition fails `false` is returned; the state of
// /// the output parameter is undefined.
// fn chol_to(&self, out: &mut Self) -> bool {
// match self.chol() {
// None => false,
// Some(decomp) => {
// *out = decomp;
// true
// }
// }
// }
// }
// 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
// and Sub. // and Sub.
// However, using the same trait multiple time as a trait bound (ex: impl<T: Add<N, V> + Add<V, V>) // However, using the same trait multiple time as a trait bound (ex: impl<T: Add<N, V> + Add<V, V>)
@ -347,7 +326,11 @@ pub trait Axpy<N> {
} }
/* /*
* Some implementation for scalar types. *
*
* Some implementations for scalar types.
*
*
*/ */
// FIXME: move this to another module ? // FIXME: move this to another module ?
macro_rules! impl_absolute( macro_rules! impl_absolute(
@ -373,6 +356,7 @@ macro_rules! impl_absolute_id(
impl_absolute!(f32) impl_absolute!(f32)
impl_absolute!(f64) impl_absolute!(f64)
impl_absolute!(i8)
impl_absolute!(i16) impl_absolute!(i16)
impl_absolute!(i32) impl_absolute!(i32)
impl_absolute!(i64) impl_absolute!(i64)

View File

@ -1,13 +1,21 @@
//! Traits giving structural informations on linear algebra objects or the space they live in. //! Traits giving structural informations on linear algebra objects or the space they live in.
use std::num::{Zero, One, FloatMath, Num}; use std::num::{Int, Float, FloatMath};
use std::slice::{Items, MutItems}; use std::slice::{Items, MutItems};
use traits::operations::{RMul, LMul, Axpy, Transpose, Inv, Absolute}; use traits::operations::{RMul, LMul, Axpy, Transpose, Inv, Absolute};
use traits::geometry::{Dot, Norm, Orig}; use traits::geometry::{Dot, Norm, Orig};
pub trait BaseFloat: FloatMath + Zero + One + Num + Absolute<Self> { /// Basic integral numeric trait.
pub trait BaseNum: Zero + One + Add<Self, Self> + Sub<Self, Self> + Mul<Self, Self> +
Div<Self, Self> + Rem<Self, Self> + Neg<Self> + PartialEq + Absolute<Self> {
} }
/// Basic floating-point number numeric trait.
pub trait BaseFloat: FloatMath + BaseNum {
}
impl BaseNum for f32 { }
impl BaseNum for f64 { }
impl BaseFloat for f32 { } impl BaseFloat for f32 { }
impl BaseFloat for f64 { } impl BaseFloat for f64 { }
@ -41,8 +49,29 @@ pub trait Eye {
fn new_identity(dim: uint) -> Self; fn new_identity(dim: uint) -> Self;
} }
// XXX: we keep ScalarAdd and ScalarSub here to avoid trait impl conflict (overriding) between the /// Additive identity.
// different Add/Sub traits. This is _so_ unfortunate… pub trait Zero {
/// Returns the additive identity.
fn zero() -> Self;
/// Tests if `self` is exactly zero.
fn is_zero(&self) -> bool;
}
/// Multiplicative identity.
pub trait One {
/// Returns the multiplicative identity.
fn one() -> Self;
}
/// Types that have maximum and minimum value.
pub trait Bounded {
/// The minimum value.
#[inline]
fn min_value() -> Self;
/// The maximum value.
#[inline]
fn max_value() -> Self;
}
// FIXME: return an iterator instead // FIXME: return an iterator instead
/// Traits of objects which can form a basis (typically vectors). /// Traits of objects which can form a basis (typically vectors).
@ -223,3 +252,74 @@ pub trait FloatPnt<N: BaseFloat, V: Norm<N>>: NumPnt<N, V> {
Norm::norm(&(*a - *b)) Norm::norm(&(*a - *b))
} }
} }
/*
*
*
* Some implementations for builtin types.
*
*
*/
macro_rules! impl_zero_one(
($n: ty, $zero: expr, $one: expr) => {
impl Zero for $n {
#[inline]
fn zero() -> $n {
$zero
}
#[inline]
fn is_zero(&self) -> bool {
*self == $zero
}
}
impl One for $n {
fn one() -> $n {
$one
}
}
}
)
impl_zero_one!(f32, 0.0, 1.0)
impl_zero_one!(f64, 0.0, 1.0)
impl_zero_one!(i8, 0, 1)
impl_zero_one!(i16, 0, 1)
impl_zero_one!(i32, 0, 1)
impl_zero_one!(i64, 0, 1)
impl_zero_one!(int, 0, 1)
impl_zero_one!(u8, 0, 1)
impl_zero_one!(u16, 0, 1)
impl_zero_one!(u32, 0, 1)
impl_zero_one!(u64, 0, 1)
impl_zero_one!(uint, 0, 1)
macro_rules! impl_bounded(
($n: ty, $min: expr, $max: expr) => {
impl Bounded for $n {
#[inline]
fn min_value() -> $n {
$min
}
#[inline]
fn max_value() -> $n {
$max
}
}
}
)
impl_bounded!(f32, Float::min_value(), Float::max_value())
impl_bounded!(f64, Float::min_value(), Float::max_value())
impl_bounded!(i8, Int::min_value(), Int::max_value())
impl_bounded!(i16, Int::min_value(), Int::max_value())
impl_bounded!(i32, Int::min_value(), Int::max_value())
impl_bounded!(i64, Int::min_value(), Int::max_value())
impl_bounded!(int, Int::min_value(), Int::max_value())
impl_bounded!(u8, Int::min_value(), Int::max_value())
impl_bounded!(u16, Int::min_value(), Int::max_value())
impl_bounded!(u32, Int::min_value(), Int::max_value())
impl_bounded!(u64, Int::min_value(), Int::max_value())
impl_bounded!(uint, Int::min_value(), Int::max_value())