2014-06-01 21:21:45 +08:00
|
|
|
use std::rand::random;
|
2013-10-08 07:22:56 +08:00
|
|
|
use na::{Vec0, Vec1, Vec2, Vec3, Vec4, Vec5, Vec6};
|
2013-10-14 17:22:38 +08:00
|
|
|
use na::{Mat3, Iterable, IterableMut}; // FIXME: get rid of that
|
2013-10-08 07:22:56 +08:00
|
|
|
use na;
|
2013-05-19 05:56:03 +08:00
|
|
|
|
2013-06-29 05:03:40 +08:00
|
|
|
macro_rules! test_iterator_impl(
|
2013-08-05 16:13:44 +08:00
|
|
|
($t: ty, $n: ty) => (
|
2014-07-05 16:24:43 +08:00
|
|
|
for _ in range(0u, 10000) {
|
2013-08-05 16:13:44 +08:00
|
|
|
let v: $t = random();
|
|
|
|
let mut mv: $t = v.clone();
|
|
|
|
let n: $n = random();
|
2013-06-29 05:03:40 +08:00
|
|
|
|
2013-08-11 22:07:34 +08:00
|
|
|
let nv: $t = v.iter().map(|e| e * n).collect();
|
2013-06-29 05:03:40 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for e in mv.mut_iter() {
|
|
|
|
*e = *e * n
|
|
|
|
}
|
2013-06-29 05:03:40 +08:00
|
|
|
|
Rework of the traits for Vectors.
The goal is to make traits less fine-grained for vectors, and reduce the amount of `use`.
- Scalar{Mul, Div} are removed, replaced by Mul<N, V> and Div<N, V>,
- Ring and DivisionRing are removed. Use Num instead.
- VectorSpace, Dot, and Norm are removed, replaced by the new, higher-level traits.
Add four traits:
- Vec: common operations on vectors. Replaces VectorSpace and Dot.
- AlgebraicVec: Vec + the old Norm trait.
- VecExt: Vec + every other traits vectors implement.
- AlgebraicVecExt: AlgebraicVec + VecExt.
2013-08-19 00:33:25 +08:00
|
|
|
assert!(nv == mv && nv == v * n);
|
2014-01-30 18:28:15 +08:00
|
|
|
}
|
2013-08-05 16:13:44 +08:00
|
|
|
)
|
2013-06-29 05:03:40 +08:00
|
|
|
)
|
|
|
|
|
2013-05-22 07:15:03 +08:00
|
|
|
macro_rules! test_commut_dot_impl(
|
2013-08-05 16:13:44 +08:00
|
|
|
($t: ty) => (
|
2014-07-05 16:24:43 +08:00
|
|
|
for _ in range(0u, 10000) {
|
2013-08-05 16:13:44 +08:00
|
|
|
let v1 : $t = random();
|
|
|
|
let v2 : $t = random();
|
|
|
|
|
2014-01-10 03:48:30 +08:00
|
|
|
assert!(na::approx_eq(&na::dot(&v1, &v2), &na::dot(&v2, &v1)));
|
2014-01-30 18:28:15 +08:00
|
|
|
}
|
2013-08-05 16:13:44 +08:00
|
|
|
);
|
2013-05-22 07:15:03 +08:00
|
|
|
)
|
|
|
|
|
2013-06-19 18:26:59 +08:00
|
|
|
macro_rules! test_scalar_op_impl(
|
2013-08-05 16:13:44 +08:00
|
|
|
($t: ty, $n: ty) => (
|
2014-07-05 16:24:43 +08:00
|
|
|
for _ in range(0u, 10000) {
|
2013-08-05 16:13:44 +08:00
|
|
|
let v1 : $t = random();
|
|
|
|
let n : $n = random();
|
|
|
|
|
2014-01-10 03:48:30 +08:00
|
|
|
assert!(na::approx_eq(&((v1 * n) / n), &v1));
|
|
|
|
assert!(na::approx_eq(&((v1 / n) * n), &v1));
|
|
|
|
assert!(na::approx_eq(&((v1 - n) + n), &v1));
|
|
|
|
assert!(na::approx_eq(&((v1 + n) - n), &v1));
|
2013-08-05 16:13:44 +08:00
|
|
|
|
|
|
|
let mut v1 : $t = random();
|
|
|
|
let v0 : $t = v1.clone();
|
|
|
|
let n : $n = random();
|
|
|
|
|
Rework of the traits for Vectors.
The goal is to make traits less fine-grained for vectors, and reduce the amount of `use`.
- Scalar{Mul, Div} are removed, replaced by Mul<N, V> and Div<N, V>,
- Ring and DivisionRing are removed. Use Num instead.
- VectorSpace, Dot, and Norm are removed, replaced by the new, higher-level traits.
Add four traits:
- Vec: common operations on vectors. Replaces VectorSpace and Dot.
- AlgebraicVec: Vec + the old Norm trait.
- VecExt: Vec + every other traits vectors implement.
- AlgebraicVecExt: AlgebraicVec + VecExt.
2013-08-19 00:33:25 +08:00
|
|
|
v1 = v1 * n;
|
|
|
|
v1 = v1 / n;
|
2013-08-05 16:13:44 +08:00
|
|
|
|
2014-01-10 03:48:30 +08:00
|
|
|
assert!(na::approx_eq(&v1, &v0));
|
2014-01-30 18:28:15 +08:00
|
|
|
}
|
2013-08-05 16:13:44 +08:00
|
|
|
);
|
2013-06-19 18:26:59 +08:00
|
|
|
)
|
|
|
|
|
2013-05-22 07:15:03 +08:00
|
|
|
macro_rules! test_basis_impl(
|
2013-08-05 16:13:44 +08:00
|
|
|
($t: ty) => (
|
2014-07-05 16:24:43 +08:00
|
|
|
for _ in range(0u, 10000) {
|
2013-11-27 18:16:16 +08:00
|
|
|
na::canonical_basis(|e1: $t| {
|
|
|
|
na::canonical_basis(|e2: $t| {
|
2014-01-10 03:48:30 +08:00
|
|
|
assert!(e1 == e2 || na::approx_eq(&na::dot(&e1, &e2), &na::zero()));
|
2013-08-17 16:48:45 +08:00
|
|
|
|
|
|
|
true
|
2013-11-27 18:16:16 +08:00
|
|
|
});
|
2013-08-05 16:13:44 +08:00
|
|
|
|
2014-01-10 03:48:30 +08:00
|
|
|
assert!(na::approx_eq(&na::norm(&e1), &na::one()));
|
2013-08-17 16:48:45 +08:00
|
|
|
|
|
|
|
true
|
2013-11-27 18:16:16 +08:00
|
|
|
})
|
2014-01-30 18:28:15 +08:00
|
|
|
}
|
2013-08-05 16:13:44 +08:00
|
|
|
);
|
2013-05-22 07:15:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! test_subspace_basis_impl(
|
2013-08-05 16:13:44 +08:00
|
|
|
($t: ty) => (
|
2014-07-05 16:24:43 +08:00
|
|
|
for _ in range(0u, 10000) {
|
2013-08-05 16:13:44 +08:00
|
|
|
let v : $t = random();
|
2013-10-14 16:22:32 +08:00
|
|
|
let v1 = na::normalize(&v);
|
2013-08-05 16:13:44 +08:00
|
|
|
|
2013-11-27 18:16:16 +08:00
|
|
|
na::orthonormal_subspace_basis(&v1, |e1| {
|
2013-08-05 16:13:44 +08:00
|
|
|
// check vectors are orthogonal to v1
|
2014-01-10 03:48:30 +08:00
|
|
|
assert!(na::approx_eq(&na::dot(&v1, &e1), &na::zero()));
|
2013-08-05 16:13:44 +08:00
|
|
|
// check vectors form an orthonormal basis
|
2014-01-10 03:48:30 +08:00
|
|
|
assert!(na::approx_eq(&na::norm(&e1), &na::one()));
|
2013-08-05 16:13:44 +08:00
|
|
|
// check vectors form an ortogonal basis
|
2013-11-27 18:16:16 +08:00
|
|
|
na::orthonormal_subspace_basis(&v1, |e2| {
|
2014-01-10 03:48:30 +08:00
|
|
|
assert!(e1 == e2 || na::approx_eq(&na::dot(&e1, &e2), &na::zero()));
|
2013-08-17 16:48:45 +08:00
|
|
|
|
|
|
|
true
|
2013-11-27 18:16:16 +08:00
|
|
|
});
|
2013-08-17 16:48:45 +08:00
|
|
|
|
|
|
|
true
|
2013-11-27 18:16:16 +08:00
|
|
|
})
|
2014-01-30 18:28:15 +08:00
|
|
|
}
|
2013-08-05 16:13:44 +08:00
|
|
|
);
|
2013-05-22 07:15:03 +08:00
|
|
|
)
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_cross_vec3() {
|
2014-07-05 16:24:43 +08:00
|
|
|
for _ in range(0u, 10000) {
|
2013-08-05 16:13:44 +08:00
|
|
|
let v1 : Vec3<f64> = random();
|
|
|
|
let v2 : Vec3<f64> = random();
|
2013-10-08 07:22:56 +08:00
|
|
|
let v3 : Vec3<f64> = na::cross(&v1, &v2);
|
2013-05-19 01:04:03 +08:00
|
|
|
|
2014-01-10 03:48:30 +08:00
|
|
|
assert!(na::approx_eq(&na::dot(&v3, &v2), &na::zero()));
|
|
|
|
assert!(na::approx_eq(&na::dot(&v3, &v1), &na::zero()));
|
2014-01-30 18:28:15 +08:00
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_commut_dot_vec0() {
|
|
|
|
test_commut_dot_impl!(Vec0<f64>);
|
|
|
|
}
|
2013-07-20 23:02:54 +08:00
|
|
|
|
2013-05-19 01:04:03 +08:00
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_commut_dot_vec1() {
|
|
|
|
test_commut_dot_impl!(Vec1<f64>);
|
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_commut_dot_vec2() {
|
|
|
|
test_commut_dot_impl!(Vec2<f64>);
|
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_commut_dot_vec3() {
|
|
|
|
test_commut_dot_impl!(Vec3<f64>);
|
|
|
|
}
|
2013-07-20 23:02:54 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_commut_dot_vec4() {
|
|
|
|
test_commut_dot_impl!(Vec4<f64>);
|
|
|
|
}
|
2013-07-20 23:02:54 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_commut_dot_vec5() {
|
|
|
|
test_commut_dot_impl!(Vec5<f64>);
|
|
|
|
}
|
2013-07-20 23:02:54 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_commut_dot_vec6() {
|
|
|
|
test_commut_dot_impl!(Vec6<f64>);
|
|
|
|
}
|
2013-07-20 23:02:54 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_basis_vec0() {
|
|
|
|
test_basis_impl!(Vec0<f64>);
|
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_basis_vec1() {
|
|
|
|
test_basis_impl!(Vec1<f64>);
|
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_basis_vec2() {
|
|
|
|
test_basis_impl!(Vec2<f64>);
|
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_basis_vec3() {
|
|
|
|
test_basis_impl!(Vec3<f64>);
|
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_basis_vec4() {
|
|
|
|
test_basis_impl!(Vec4<f64>);
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_basis_vec5() {
|
|
|
|
test_basis_impl!(Vec5<f64>);
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_basis_vec6() {
|
|
|
|
test_basis_impl!(Vec6<f64>);
|
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
2013-07-20 23:02:54 +08:00
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_subspace_basis_vec0() {
|
|
|
|
test_subspace_basis_impl!(Vec0<f64>);
|
|
|
|
}
|
2013-07-20 23:02:54 +08:00
|
|
|
|
2013-05-19 01:04:03 +08:00
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_subspace_basis_vec1() {
|
|
|
|
test_subspace_basis_impl!(Vec1<f64>);
|
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_subspace_basis_vec2() {
|
|
|
|
test_subspace_basis_impl!(Vec2<f64>);
|
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_subspace_basis_vec3() {
|
|
|
|
test_subspace_basis_impl!(Vec3<f64>);
|
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
2013-05-22 07:15:03 +08:00
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_subspace_basis_vec4() {
|
|
|
|
test_subspace_basis_impl!(Vec4<f64>);
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_subspace_basis_vec5() {
|
|
|
|
test_subspace_basis_impl!(Vec5<f64>);
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_subspace_basis_vec6() {
|
|
|
|
test_subspace_basis_impl!(Vec6<f64>);
|
|
|
|
}
|
2013-06-10 08:09:36 +08:00
|
|
|
|
2013-07-20 23:02:54 +08:00
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_scalar_op_vec0() {
|
|
|
|
test_scalar_op_impl!(Vec0<f64>, f64);
|
|
|
|
}
|
2013-07-20 23:02:54 +08:00
|
|
|
|
2013-06-19 18:26:59 +08:00
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_scalar_op_vec1() {
|
|
|
|
test_scalar_op_impl!(Vec1<f64>, f64);
|
|
|
|
}
|
2013-06-19 18:26:59 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_scalar_op_vec2() {
|
|
|
|
test_scalar_op_impl!(Vec2<f64>, f64);
|
|
|
|
}
|
2013-06-19 18:26:59 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_scalar_op_vec3() {
|
|
|
|
test_scalar_op_impl!(Vec3<f64>, f64);
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_scalar_op_vec4() {
|
|
|
|
test_scalar_op_impl!(Vec4<f64>, f64);
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
2013-06-19 18:26:59 +08:00
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_scalar_op_vec5() {
|
|
|
|
test_scalar_op_impl!(Vec5<f64>, f64);
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_scalar_op_vec6() {
|
|
|
|
test_scalar_op_impl!(Vec6<f64>, f64);
|
|
|
|
}
|
2013-06-29 05:03:40 +08:00
|
|
|
|
2013-07-20 23:02:54 +08:00
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_iterator_vec0() {
|
|
|
|
test_iterator_impl!(Vec0<f64>, f64);
|
|
|
|
}
|
2013-07-20 23:02:54 +08:00
|
|
|
|
2013-06-29 05:03:40 +08:00
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_iterator_vec1() {
|
|
|
|
test_iterator_impl!(Vec1<f64>, f64);
|
|
|
|
}
|
2013-06-29 05:03:40 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_iterator_vec2() {
|
|
|
|
test_iterator_impl!(Vec2<f64>, f64);
|
|
|
|
}
|
2013-06-29 05:03:40 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_iterator_vec3() {
|
|
|
|
test_iterator_impl!(Vec3<f64>, f64);
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_iterator_vec4() {
|
|
|
|
test_iterator_impl!(Vec4<f64>, f64);
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_iterator_vec5() {
|
|
|
|
test_iterator_impl!(Vec5<f64>, f64);
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
2013-06-29 05:03:40 +08:00
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_iterator_vec6() {
|
|
|
|
test_iterator_impl!(Vec6<f64>, f64);
|
|
|
|
}
|
2013-08-04 16:36:35 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_ord_vec3() {
|
2013-08-04 16:36:35 +08:00
|
|
|
// equality
|
2014-07-05 16:24:43 +08:00
|
|
|
assert!(Vec3::new(0.5f64, 0.5, 0.5) == Vec3::new(0.5, 0.5, 0.5));
|
|
|
|
assert!(!(Vec3::new(1.5f64, 0.5, 0.5) == Vec3::new(0.5, 0.5, 0.5)));
|
|
|
|
assert!(Vec3::new(1.5f64, 0.5, 0.5) != Vec3::new(0.5, 0.5, 0.5));
|
2013-08-04 16:36:35 +08:00
|
|
|
|
|
|
|
// comparable
|
2014-07-05 16:24:43 +08:00
|
|
|
assert!(na::partial_cmp(&Vec3::new(0.5f64, 0.3, 0.3), &Vec3::new(1.0, 2.0, 1.0)).is_le());
|
|
|
|
assert!(na::partial_cmp(&Vec3::new(0.5f64, 0.3, 0.3), &Vec3::new(1.0, 2.0, 1.0)).is_lt());
|
|
|
|
assert!(na::partial_cmp(&Vec3::new(2.0f64, 4.0, 2.0), &Vec3::new(1.0, 2.0, 1.0)).is_ge());
|
|
|
|
assert!(na::partial_cmp(&Vec3::new(2.0f64, 4.0, 2.0), &Vec3::new(1.0, 2.0, 1.0)).is_gt());
|
2013-08-04 16:36:35 +08:00
|
|
|
|
|
|
|
// not comparable
|
2014-07-05 16:24:43 +08:00
|
|
|
assert!(na::partial_cmp(&Vec3::new(0.0f64, 3.0, 0.0), &Vec3::new(1.0, 2.0, 1.0)).is_not_comparable());
|
2013-08-04 16:36:35 +08:00
|
|
|
}
|
2013-08-04 17:06:23 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_min_max_vec3() {
|
2014-07-05 16:24:43 +08:00
|
|
|
assert_eq!(na::sup(&Vec3::new(1.0f64, 2.0, 3.0), &Vec3::new(3.0, 2.0, 1.0)), Vec3::new(3.0, 2.0, 3.0));
|
|
|
|
assert_eq!(na::inf(&Vec3::new(1.0f64, 2.0, 3.0), &Vec3::new(3.0, 2.0, 1.0)), Vec3::new(1.0, 2.0, 1.0));
|
2013-08-04 17:06:23 +08:00
|
|
|
}
|
2013-08-12 22:45:31 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_outer_vec3() {
|
|
|
|
assert_eq!(
|
2014-07-05 16:24:43 +08:00
|
|
|
na::outer(&Vec3::new(1.0f64, 2.0, 3.0), &Vec3::new(4.0, 5.0, 6.0)),
|
2013-10-14 17:22:38 +08:00
|
|
|
Mat3::new(
|
2014-03-15 19:23:54 +08:00
|
|
|
4.0, 5.0, 6.0,
|
|
|
|
8.0, 10.0, 12.0,
|
|
|
|
12.0, 15.0, 18.0));
|
2013-08-12 22:45:31 +08:00
|
|
|
}
|