2013-07-22 16:26:20 +08:00
|
|
|
use std::cast;
|
|
|
|
use std::num::{Zero, One, Algebraic, Bounded};
|
|
|
|
use std::rand::Rng;
|
|
|
|
use std::vec::{VecIterator, VecMutIterator};
|
2013-08-02 16:50:04 +08:00
|
|
|
use std::iterator::{Iterator, FromIterator};
|
2013-07-22 16:26:20 +08:00
|
|
|
use std::cmp::ApproxEq;
|
|
|
|
use traits::basis::Basis;
|
|
|
|
use traits::dim::Dim;
|
2013-08-22 23:47:37 +08:00
|
|
|
use traits::translation::Translation;
|
2013-07-22 16:26:20 +08:00
|
|
|
use traits::homogeneous::{FromHomogeneous, ToHomogeneous};
|
|
|
|
use traits::indexable::Indexable;
|
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
|
|
|
use traits::scalar_op::{ScalarAdd, ScalarSub};
|
|
|
|
use traits::iterable::{Iterable, IterableMut};
|
|
|
|
use traits::vec_cast::VecCast;
|
|
|
|
use traits::vector::{Vec, AlgebraicVec};
|
2013-07-22 17:35:36 +08:00
|
|
|
|
2013-07-22 16:26:20 +08:00
|
|
|
mod vec_macros;
|
|
|
|
|
2013-07-24 22:50:40 +08:00
|
|
|
/// Vector of dimension 0.
|
2013-08-04 16:36:35 +08:00
|
|
|
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, Rand, Zero, ToStr)]
|
2013-07-22 16:26:20 +08:00
|
|
|
pub struct Vec0<N>;
|
|
|
|
|
2013-07-24 22:50:40 +08:00
|
|
|
/// Vector of dimension 1.
|
2013-08-04 16:36:35 +08:00
|
|
|
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
2013-08-05 16:13:44 +08:00
|
|
|
pub struct Vec1<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
/// First component of the vector.
|
|
|
|
x: N
|
2013-07-24 22:50:40 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
|
|
|
|
new_impl!(Vec1, x)
|
2013-08-04 16:36:35 +08:00
|
|
|
ord_impl!(Vec1, x)
|
2013-08-04 17:06:23 +08:00
|
|
|
orderable_impl!(Vec1, x)
|
2013-07-25 05:54:54 +08:00
|
|
|
vec_axis_impl!(Vec1, x)
|
2013-07-23 05:42:35 +08:00
|
|
|
vec_cast_impl!(Vec1, x)
|
2013-07-22 16:26:20 +08:00
|
|
|
indexable_impl!(Vec1, 1)
|
|
|
|
new_repeat_impl!(Vec1, val, x)
|
|
|
|
dim_impl!(Vec1, 1)
|
|
|
|
// (specialized) basis_impl!(Vec1, 1)
|
|
|
|
add_impl!(Vec1, x)
|
|
|
|
sub_impl!(Vec1, x)
|
|
|
|
neg_impl!(Vec1, x)
|
|
|
|
dot_impl!(Vec1, x)
|
|
|
|
scalar_mul_impl!(Vec1, x)
|
|
|
|
scalar_div_impl!(Vec1, x)
|
|
|
|
scalar_add_impl!(Vec1, x)
|
|
|
|
scalar_sub_impl!(Vec1, x)
|
|
|
|
translation_impl!(Vec1)
|
|
|
|
norm_impl!(Vec1)
|
|
|
|
approx_eq_impl!(Vec1, x)
|
2013-08-15 16:41:47 +08:00
|
|
|
round_impl!(Vec1, x)
|
2013-07-22 16:26:20 +08:00
|
|
|
one_impl!(Vec1)
|
|
|
|
from_iterator_impl!(Vec1, iterator)
|
|
|
|
bounded_impl!(Vec1)
|
|
|
|
iterable_impl!(Vec1, 1)
|
|
|
|
iterable_mut_impl!(Vec1, 1)
|
|
|
|
to_homogeneous_impl!(Vec1, Vec2, y, x)
|
|
|
|
from_homogeneous_impl!(Vec1, Vec2, y, x)
|
|
|
|
|
2013-07-24 22:50:40 +08:00
|
|
|
/// Vector of dimension 2.
|
2013-08-04 16:36:35 +08:00
|
|
|
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
2013-08-05 16:13:44 +08:00
|
|
|
pub struct Vec2<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
/// First component of the vector.
|
|
|
|
x: N,
|
|
|
|
/// Second component of the vector.
|
|
|
|
y: N
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
new_impl!(Vec2, x, y)
|
2013-08-04 16:36:35 +08:00
|
|
|
ord_impl!(Vec2, x, y)
|
2013-08-04 17:06:23 +08:00
|
|
|
orderable_impl!(Vec2, x, y)
|
2013-07-25 05:54:54 +08:00
|
|
|
vec_axis_impl!(Vec2, x, y)
|
2013-07-23 05:42:35 +08:00
|
|
|
vec_cast_impl!(Vec2, x, y)
|
2013-07-22 16:26:20 +08:00
|
|
|
indexable_impl!(Vec2, 2)
|
|
|
|
new_repeat_impl!(Vec2, val, x, y)
|
|
|
|
dim_impl!(Vec2, 2)
|
|
|
|
// (specialized) basis_impl!(Vec2, 1)
|
|
|
|
add_impl!(Vec2, x, y)
|
|
|
|
sub_impl!(Vec2, x, y)
|
|
|
|
neg_impl!(Vec2, x, y)
|
|
|
|
dot_impl!(Vec2, x, y)
|
|
|
|
scalar_mul_impl!(Vec2, x, y)
|
|
|
|
scalar_div_impl!(Vec2, x, y)
|
|
|
|
scalar_add_impl!(Vec2, x, y)
|
|
|
|
scalar_sub_impl!(Vec2, x, y)
|
|
|
|
translation_impl!(Vec2)
|
|
|
|
norm_impl!(Vec2)
|
|
|
|
approx_eq_impl!(Vec2, x, y)
|
2013-08-15 16:41:47 +08:00
|
|
|
round_impl!(Vec2, x, y)
|
2013-07-22 16:26:20 +08:00
|
|
|
one_impl!(Vec2)
|
|
|
|
from_iterator_impl!(Vec2, iterator, iterator)
|
|
|
|
bounded_impl!(Vec2)
|
|
|
|
iterable_impl!(Vec2, 2)
|
|
|
|
iterable_mut_impl!(Vec2, 2)
|
|
|
|
to_homogeneous_impl!(Vec2, Vec3, z, x, y)
|
|
|
|
from_homogeneous_impl!(Vec2, Vec3, z, x, y)
|
|
|
|
|
2013-07-24 22:50:40 +08:00
|
|
|
/// Vector of dimension 3.
|
2013-08-04 16:36:35 +08:00
|
|
|
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
2013-08-05 16:13:44 +08:00
|
|
|
pub struct Vec3<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
/// First component of the vector.
|
|
|
|
x: N,
|
|
|
|
/// Second component of the vector.
|
|
|
|
y: N,
|
|
|
|
/// Third component of the vector.
|
|
|
|
z: N
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
new_impl!(Vec3, x, y, z)
|
2013-08-04 16:36:35 +08:00
|
|
|
ord_impl!(Vec3, x, y, z)
|
2013-08-04 17:06:23 +08:00
|
|
|
orderable_impl!(Vec3, x, y, z)
|
2013-07-25 05:54:54 +08:00
|
|
|
vec_axis_impl!(Vec3, x, y, z)
|
2013-07-23 05:42:35 +08:00
|
|
|
vec_cast_impl!(Vec3, x, y, z)
|
2013-07-22 16:26:20 +08:00
|
|
|
indexable_impl!(Vec3, 3)
|
|
|
|
new_repeat_impl!(Vec3, val, x, y, z)
|
|
|
|
dim_impl!(Vec3, 3)
|
|
|
|
// (specialized) basis_impl!(Vec3, 1)
|
|
|
|
add_impl!(Vec3, x, y, z)
|
|
|
|
sub_impl!(Vec3, x, y, z)
|
|
|
|
neg_impl!(Vec3, x, y, z)
|
|
|
|
dot_impl!(Vec3, x, y, z)
|
|
|
|
scalar_mul_impl!(Vec3, x, y, z)
|
|
|
|
scalar_div_impl!(Vec3, x, y, z)
|
|
|
|
scalar_add_impl!(Vec3, x, y, z)
|
|
|
|
scalar_sub_impl!(Vec3, x, y, z)
|
|
|
|
translation_impl!(Vec3)
|
|
|
|
norm_impl!(Vec3)
|
|
|
|
approx_eq_impl!(Vec3, x, y, z)
|
2013-08-15 16:41:47 +08:00
|
|
|
round_impl!(Vec3, x, y, z)
|
2013-07-22 16:26:20 +08:00
|
|
|
one_impl!(Vec3)
|
|
|
|
from_iterator_impl!(Vec3, iterator, iterator, iterator)
|
|
|
|
bounded_impl!(Vec3)
|
|
|
|
iterable_impl!(Vec3, 3)
|
|
|
|
iterable_mut_impl!(Vec3, 3)
|
|
|
|
to_homogeneous_impl!(Vec3, Vec4, w, x, y, z)
|
|
|
|
from_homogeneous_impl!(Vec3, Vec4, w, x, y, z)
|
|
|
|
|
2013-07-24 22:50:40 +08:00
|
|
|
/// Vector of dimension 4.
|
2013-08-04 16:36:35 +08:00
|
|
|
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
2013-08-05 16:13:44 +08:00
|
|
|
pub struct Vec4<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
/// First component of the vector.
|
|
|
|
x: N,
|
|
|
|
/// Second component of the vector.
|
|
|
|
y: N,
|
|
|
|
/// Third component of the vector.
|
|
|
|
z: N,
|
|
|
|
/// Fourth component of the vector.
|
|
|
|
w: N
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
new_impl!(Vec4, x, y, z, w)
|
2013-08-04 16:36:35 +08:00
|
|
|
ord_impl!(Vec4, x, y, z, w)
|
2013-08-04 17:06:23 +08:00
|
|
|
orderable_impl!(Vec4, x, y, z, w)
|
2013-07-25 05:54:54 +08:00
|
|
|
vec_axis_impl!(Vec4, x, y, z, w)
|
2013-07-23 05:42:35 +08:00
|
|
|
vec_cast_impl!(Vec4, x, y, z, w)
|
2013-07-22 16:26:20 +08:00
|
|
|
indexable_impl!(Vec4, 4)
|
|
|
|
new_repeat_impl!(Vec4, val, x, y, z, w)
|
|
|
|
dim_impl!(Vec4, 4)
|
|
|
|
basis_impl!(Vec4, 4)
|
|
|
|
add_impl!(Vec4, x, y, z, w)
|
|
|
|
sub_impl!(Vec4, x, y, z, w)
|
|
|
|
neg_impl!(Vec4, x, y, z, w)
|
|
|
|
dot_impl!(Vec4, x, y, z, w)
|
|
|
|
scalar_mul_impl!(Vec4, x, y, z, w)
|
|
|
|
scalar_div_impl!(Vec4, x, y, z, w)
|
|
|
|
scalar_add_impl!(Vec4, x, y, z, w)
|
|
|
|
scalar_sub_impl!(Vec4, x, y, z, w)
|
|
|
|
translation_impl!(Vec4)
|
|
|
|
norm_impl!(Vec4)
|
|
|
|
approx_eq_impl!(Vec4, x, y, z, w)
|
2013-08-15 16:41:47 +08:00
|
|
|
round_impl!(Vec4, x, y, z, w)
|
2013-07-22 16:26:20 +08:00
|
|
|
one_impl!(Vec4)
|
|
|
|
from_iterator_impl!(Vec4, iterator, iterator, iterator, iterator)
|
|
|
|
bounded_impl!(Vec4)
|
|
|
|
iterable_impl!(Vec4, 4)
|
|
|
|
iterable_mut_impl!(Vec4, 4)
|
|
|
|
to_homogeneous_impl!(Vec4, Vec5, a, x, y, z, w)
|
|
|
|
from_homogeneous_impl!(Vec4, Vec5, a, x, y, z, w)
|
|
|
|
|
2013-07-24 22:50:40 +08:00
|
|
|
/// Vector of dimension 5.
|
2013-08-04 16:36:35 +08:00
|
|
|
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
2013-08-05 16:13:44 +08:00
|
|
|
pub struct Vec5<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
/// First component of the vector.
|
|
|
|
x: N,
|
|
|
|
/// Second component of the vector.
|
|
|
|
y: N,
|
|
|
|
/// Third component of the vector.
|
|
|
|
z: N,
|
|
|
|
/// Fourth component of the vector.
|
|
|
|
w: N,
|
|
|
|
/// Fifth of the vector.
|
|
|
|
a: N
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
new_impl!(Vec5, x, y, z, w, a)
|
2013-08-04 16:36:35 +08:00
|
|
|
ord_impl!(Vec5, x, y, z, w, a)
|
2013-08-04 17:06:23 +08:00
|
|
|
orderable_impl!(Vec5, x, y, z, w, a)
|
2013-07-25 05:54:54 +08:00
|
|
|
vec_axis_impl!(Vec5, x, y, z, w, a)
|
2013-07-23 05:42:35 +08:00
|
|
|
vec_cast_impl!(Vec5, x, y, z, w, a)
|
2013-07-22 16:26:20 +08:00
|
|
|
indexable_impl!(Vec5, 5)
|
|
|
|
new_repeat_impl!(Vec5, val, x, y, z, w, a)
|
|
|
|
dim_impl!(Vec5, 5)
|
|
|
|
basis_impl!(Vec5, 5)
|
|
|
|
add_impl!(Vec5, x, y, z, w, a)
|
|
|
|
sub_impl!(Vec5, x, y, z, w, a)
|
|
|
|
neg_impl!(Vec5, x, y, z, w, a)
|
|
|
|
dot_impl!(Vec5, x, y, z, w, a)
|
|
|
|
scalar_mul_impl!(Vec5, x, y, z, w, a)
|
|
|
|
scalar_div_impl!(Vec5, x, y, z, w, a)
|
|
|
|
scalar_add_impl!(Vec5, x, y, z, w, a)
|
|
|
|
scalar_sub_impl!(Vec5, x, y, z, w, a)
|
|
|
|
translation_impl!(Vec5)
|
|
|
|
norm_impl!(Vec5)
|
|
|
|
approx_eq_impl!(Vec5, x, y, z, w, a)
|
2013-08-15 16:41:47 +08:00
|
|
|
round_impl!(Vec5, x, y, z, w, a)
|
2013-07-22 16:26:20 +08:00
|
|
|
one_impl!(Vec5)
|
|
|
|
from_iterator_impl!(Vec5, iterator, iterator, iterator, iterator, iterator)
|
|
|
|
bounded_impl!(Vec5)
|
|
|
|
iterable_impl!(Vec5, 5)
|
|
|
|
iterable_mut_impl!(Vec5, 5)
|
|
|
|
to_homogeneous_impl!(Vec5, Vec6, b, x, y, z, w, a)
|
|
|
|
from_homogeneous_impl!(Vec5, Vec6, b, x, y, z, w, a)
|
|
|
|
|
2013-07-24 22:50:40 +08:00
|
|
|
/// Vector of dimension 6.
|
2013-08-04 16:36:35 +08:00
|
|
|
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
2013-08-05 16:13:44 +08:00
|
|
|
pub struct Vec6<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
/// First component of the vector.
|
|
|
|
x: N,
|
|
|
|
/// Second component of the vector.
|
|
|
|
y: N,
|
|
|
|
/// Third component of the vector.
|
|
|
|
z: N,
|
|
|
|
/// Fourth component of the vector.
|
|
|
|
w: N,
|
|
|
|
/// Fifth of the vector.
|
|
|
|
a: N,
|
|
|
|
/// Sixth component of the vector.
|
|
|
|
b: N
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
new_impl!(Vec6, x, y, z, w, a, b)
|
2013-08-04 16:36:35 +08:00
|
|
|
ord_impl!(Vec6, x, y, z, w, a, b)
|
2013-08-04 17:06:23 +08:00
|
|
|
orderable_impl!(Vec6, x, y, z, w, a, b)
|
2013-07-25 05:54:54 +08:00
|
|
|
vec_axis_impl!(Vec6, x, y, z, w, a, b)
|
2013-07-23 05:42:35 +08:00
|
|
|
vec_cast_impl!(Vec6, x, y, z, w, a, b)
|
2013-07-22 16:26:20 +08:00
|
|
|
indexable_impl!(Vec6, 6)
|
|
|
|
new_repeat_impl!(Vec6, val, x, y, z, w, a, b)
|
|
|
|
dim_impl!(Vec6, 6)
|
|
|
|
basis_impl!(Vec6, 6)
|
|
|
|
add_impl!(Vec6, x, y, z, w, a, b)
|
|
|
|
sub_impl!(Vec6, x, y, z, w, a, b)
|
|
|
|
neg_impl!(Vec6, x, y, z, w, a, b)
|
|
|
|
dot_impl!(Vec6, x, y, z, w, a, b)
|
|
|
|
scalar_mul_impl!(Vec6, x, y, z, w, a, b)
|
|
|
|
scalar_div_impl!(Vec6, x, y, z, w, a, b)
|
|
|
|
scalar_add_impl!(Vec6, x, y, z, w, a, b)
|
|
|
|
scalar_sub_impl!(Vec6, x, y, z, w, a, b)
|
|
|
|
translation_impl!(Vec6)
|
|
|
|
norm_impl!(Vec6)
|
|
|
|
approx_eq_impl!(Vec6, x, y, z, w, a, b)
|
2013-08-15 16:41:47 +08:00
|
|
|
round_impl!(Vec6, x, y, z, w, a, b)
|
2013-07-22 16:26:20 +08:00
|
|
|
one_impl!(Vec6)
|
|
|
|
from_iterator_impl!(Vec6, iterator, iterator, iterator, iterator, iterator, iterator)
|
|
|
|
bounded_impl!(Vec6)
|
|
|
|
iterable_impl!(Vec6, 6)
|
|
|
|
iterable_mut_impl!(Vec6, 6)
|