2013-07-20 22:32:39 +08:00
|
|
|
use std::cast;
|
|
|
|
use std::num::{Zero, One, Algebraic, Bounded};
|
|
|
|
use std::vec::{VecIterator, VecMutIterator};
|
2013-09-09 17:19:54 +08:00
|
|
|
use std::iter::{Iterator, FromIterator};
|
2013-07-20 22:32:39 +08:00
|
|
|
use std::cmp::ApproxEq;
|
2013-09-22 16:58:21 +08:00
|
|
|
use traits::structure::{Iterable, IterableMut, Indexable, Basis, Dim};
|
|
|
|
use traits::geometry::{Translation, Dot, Norm};
|
2013-10-06 22:54:09 +08:00
|
|
|
use structs::vec;
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
/// Creates a new vector.
|
|
|
|
#[inline]
|
|
|
|
pub fn new() -> vec::Vec0<N> {
|
|
|
|
vec::Vec0
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-23 21:59:26 +08:00
|
|
|
impl<N> Indexable<uint, N> for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2013-08-14 15:43:02 +08:00
|
|
|
fn at(&self, _: uint) -> N {
|
2013-08-08 02:53:51 +08:00
|
|
|
fail!("Cannot index a Vec0.")
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2013-08-14 15:43:02 +08:00
|
|
|
fn set(&mut self, _: uint, _: N) {
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-08-14 15:43:02 +08:00
|
|
|
fn swap(&mut self, _: uint, _: uint) {
|
2013-08-08 02:53:51 +08:00
|
|
|
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-23 21:59:26 +08:00
|
|
|
impl<N> vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
/// Creates a new vector. The parameter is not taken in account.
|
|
|
|
#[inline]
|
|
|
|
pub fn new_repeat(_: N) -> vec::Vec0<N> {
|
|
|
|
vec::Vec0
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> Iterable<N> for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn iter<'l>(&'l self) -> VecIterator<'l, N> {
|
|
|
|
unsafe { cast::transmute::<&'l vec::Vec0<N>, &'l [N, ..0]>(self).iter() }
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> IterableMut<N> for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N> {
|
|
|
|
unsafe { cast::transmute::<&'l mut vec::Vec0<N>, &'l mut [N, ..0]>(self).mut_iter() }
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> Dim for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2013-08-28 20:22:12 +08:00
|
|
|
fn dim(_: Option<vec::Vec0<N>>) -> uint {
|
2013-08-08 02:53:51 +08:00
|
|
|
0
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-23 21:59:26 +08:00
|
|
|
impl<N> Basis for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline(always)]
|
2013-08-17 16:48:45 +08:00
|
|
|
fn canonical_basis(_: &fn(vec::Vec0<N>) -> bool) { }
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline(always)]
|
2013-08-17 16:48:45 +08:00
|
|
|
fn orthonormal_subspace_basis(&self, _: &fn(vec::Vec0<N>) -> bool) { }
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 03:11:43 +08:00
|
|
|
impl<N, T> Add<T, vec::Vec0<N>> for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2013-09-15 03:11:43 +08:00
|
|
|
fn add(&self, _: &T) -> vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
vec::Vec0
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 03:11:43 +08:00
|
|
|
impl<N, T> Sub<T, vec::Vec0<N>> for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2013-09-15 03:11:43 +08:00
|
|
|
fn sub(&self, _: &T) -> vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
vec::Vec0
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: Neg<N>> Neg<vec::Vec0<N>> for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn neg(&self) -> vec::Vec0<N> {
|
|
|
|
vec::Vec0
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-09-09 00:20:06 +08:00
|
|
|
impl<N: Num + Clone> Dot<N> for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn dot(&self, _: &vec::Vec0<N>) -> N {
|
|
|
|
Zero::zero()
|
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
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn sub_dot(&self, _: &vec::Vec0<N>, _: &vec::Vec0<N>) -> N {
|
|
|
|
Zero::zero()
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 03:11:43 +08:00
|
|
|
impl<N, T> Mul<T, vec::Vec0<N>> for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2013-09-15 03:11:43 +08:00
|
|
|
fn mul(&self, _: &T) -> vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
vec::Vec0
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 03:11:43 +08:00
|
|
|
impl<N, T> Div<T, vec::Vec0<N>> for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2013-09-15 03:11:43 +08:00
|
|
|
fn div(&self, _: &T) -> vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
vec::Vec0
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: Clone + Add<N, N> + Neg<N>> Translation<vec::Vec0<N>> for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn translation(&self) -> vec::Vec0<N> {
|
|
|
|
self.clone()
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn inv_translation(&self) -> vec::Vec0<N> {
|
|
|
|
-self
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn translate_by(&mut self, t: &vec::Vec0<N>) {
|
|
|
|
*self = *self + *t;
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn translated(&self, t: &vec::Vec0<N>) -> vec::Vec0<N> {
|
|
|
|
self + *t
|
|
|
|
}
|
2013-09-06 14:48:08 +08:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn set_translation(&mut self, _: vec::Vec0<N>) {
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-09-09 00:20:06 +08:00
|
|
|
impl<N: Clone + Num + Algebraic> Norm<N> for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn sqnorm(&self) -> N {
|
|
|
|
self.dot(self)
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn norm(&self) -> N {
|
|
|
|
self.sqnorm().sqrt()
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn normalized(&self) -> vec::Vec0<N> {
|
|
|
|
let mut res : vec::Vec0<N> = self.clone();
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
res.normalize();
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
res
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn normalize(&mut self) -> N {
|
|
|
|
let l = self.norm();
|
2013-07-20 22:32:39 +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
|
|
|
*self = *self / l;
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
l
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: ApproxEq<N>> ApproxEq<N> for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn approx_epsilon() -> N {
|
2013-08-28 20:22:12 +08:00
|
|
|
fail!("approx_epsilon is broken since rust revision 8693943676487c01fa09f5f3daf0df6a1f71e24d.")
|
|
|
|
// ApproxEq::<N>::approx_epsilon()
|
2013-08-08 02:53:51 +08:00
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn approx_eq(&self, _: &vec::Vec0<N>) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn approx_eq_eps(&self, _: &vec::Vec0<N>, _: &N) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: Clone + One> One for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn one() -> vec::Vec0<N> {
|
|
|
|
vec::Vec0
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-16 16:14:01 +08:00
|
|
|
impl<N> FromIterator<N> for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
2013-08-16 16:14:01 +08:00
|
|
|
fn from_iterator<I: Iterator<N>>(_: &mut I) -> vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
vec::Vec0
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: Bounded + Clone> Bounded for vec::Vec0<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
#[inline]
|
|
|
|
fn max_value() -> vec::Vec0<N> {
|
|
|
|
vec::Vec0
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn min_value() -> vec::Vec0<N> {
|
|
|
|
vec::Vec0
|
|
|
|
}
|
2013-07-20 22:32:39 +08:00
|
|
|
}
|