2013-06-02 02:50:00 +08:00
|
|
|
use std::uint::iterate;
|
|
|
|
use std::num::{Zero, One, Algebraic};
|
2013-06-29 05:03:40 +08:00
|
|
|
use std::vec::{VecIterator, VecMutIterator};
|
2013-07-02 18:00:55 +08:00
|
|
|
use std::vec::from_elem;
|
2013-06-02 02:50:00 +08:00
|
|
|
use std::cmp::ApproxEq;
|
2013-06-29 05:03:40 +08:00
|
|
|
use std::iterator::{FromIterator, IteratorUtil};
|
|
|
|
use traits::iterable::{Iterable, IterableMut};
|
2013-05-31 17:01:07 +08:00
|
|
|
use traits::ring::Ring;
|
|
|
|
use traits::division_ring::DivisionRing;
|
|
|
|
use traits::dot::Dot;
|
|
|
|
use traits::sub_dot::SubDot;
|
|
|
|
use traits::norm::Norm;
|
2013-06-28 00:16:07 +08:00
|
|
|
use traits::translation::{Translation, Translatable};
|
2013-06-16 04:16:44 +08:00
|
|
|
use traits::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-07-24 22:50:40 +08:00
|
|
|
/// Vector with a dimension unknown at compile-time.
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
#[deriving(Eq, Ord, ToStr, Clone)]
|
2013-06-10 07:36:47 +08:00
|
|
|
pub struct DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-07-24 22:50:40 +08:00
|
|
|
/// Components of the vector. Contains as much elements as the vector dimension.
|
2013-06-10 07:36:47 +08:00
|
|
|
at: ~[N]
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
|
2013-07-24 22:50:40 +08:00
|
|
|
/// Builds a vector filled with zeros.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
/// * `dim` - The dimension of the vector.
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
pub fn zero_vec_with_dim<N: Zero + Clone>(dim: uint) -> DVec<N>
|
2013-06-10 07:36:47 +08:00
|
|
|
{ DVec { at: from_elem(dim, Zero::zero::<N>()) } }
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-07-24 22:50:40 +08:00
|
|
|
/// Tests if all components of the vector are zeroes.
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
pub fn is_zero_vec<N: Zero>(vec: &DVec<N>) -> bool
|
2013-06-24 00:19:13 +08:00
|
|
|
{ vec.at.iter().all(|e| e.is_zero()) }
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-29 05:03:40 +08:00
|
|
|
impl<N> Iterable<N> for DVec<N>
|
|
|
|
{
|
|
|
|
fn iter<'l>(&'l self) -> VecIterator<'l, N>
|
|
|
|
{ self.at.iter() }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N> IterableMut<N> for DVec<N>
|
|
|
|
{
|
|
|
|
fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N>
|
|
|
|
{ self.at.mut_iter() }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N, Iter: Iterator<N>> FromIterator<N, Iter> for DVec<N>
|
|
|
|
{
|
|
|
|
fn from_iterator(param: &mut Iter) -> DVec<N>
|
|
|
|
{
|
|
|
|
let mut res = DVec { at: ~[] };
|
|
|
|
|
|
|
|
for param.advance |e|
|
|
|
|
{ res.at.push(e) }
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
impl<N: Clone + DivisionRing + Algebraic + ApproxEq<N>> DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-07-24 22:50:40 +08:00
|
|
|
/// Computes the canonical basis for the given dimension. A canonical basis is a set of
|
|
|
|
/// vectors, mutually orthogonal, with all its component equal to 0.0 exept one which is equal to
|
|
|
|
/// 1.0.
|
2013-06-10 07:36:47 +08:00
|
|
|
pub fn canonical_basis_with_dim(dim: uint) -> ~[DVec<N>]
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-10 07:36:47 +08:00
|
|
|
let mut res : ~[DVec<N>] = ~[];
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim) |i|
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-10 07:36:47 +08:00
|
|
|
let mut basis_element : DVec<N> = zero_vec_with_dim(dim);
|
2013-05-31 17:01:07 +08:00
|
|
|
|
|
|
|
basis_element.at[i] = One::one();
|
|
|
|
|
|
|
|
res.push(basis_element);
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2013-07-24 22:50:40 +08:00
|
|
|
/// Computes a basis of the space orthogonal to the vector. If the input vector is of dimension
|
|
|
|
/// `n`, this will return `n - 1` vectors.
|
2013-06-10 07:36:47 +08:00
|
|
|
pub fn orthogonal_subspace_basis(&self) -> ~[DVec<N>]
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
|
|
|
// compute the basis of the orthogonal subspace using Gram-Schmidt
|
|
|
|
// orthogonalization algorithm
|
2013-06-09 20:09:22 +08:00
|
|
|
let dim = self.at.len();
|
2013-06-10 07:36:47 +08:00
|
|
|
let mut res : ~[DVec<N>] = ~[];
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-02 02:50:00 +08:00
|
|
|
for iterate(0u, dim) |i|
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-10 07:36:47 +08:00
|
|
|
let mut basis_element : DVec<N> = zero_vec_with_dim(self.at.len());
|
2013-05-31 17:01:07 +08:00
|
|
|
|
|
|
|
basis_element.at[i] = One::one();
|
|
|
|
|
2013-06-24 00:08:50 +08:00
|
|
|
if res.len() == dim - 1
|
2013-05-31 17:01:07 +08:00
|
|
|
{ break; }
|
|
|
|
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
let mut elt = basis_element.clone();
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-19 18:26:59 +08:00
|
|
|
elt = elt - self.scalar_mul(&basis_element.dot(self));
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-08-01 15:18:21 +08:00
|
|
|
foreach v in res.iter()
|
2013-06-19 18:26:59 +08:00
|
|
|
{ elt = elt - v.scalar_mul(&elt.dot(v)) };
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-24 00:08:50 +08:00
|
|
|
if !elt.sqnorm().approx_eq(&Zero::zero())
|
2013-05-31 17:01:07 +08:00
|
|
|
{ res.push(elt.normalized()); }
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(res.len() == dim - 1);
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
impl<N: Add<N,N>> Add<DVec<N>, DVec<N>> for DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn add(&self, other: &DVec<N>) -> DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-09 20:09:22 +08:00
|
|
|
assert!(self.at.len() == other.at.len());
|
2013-07-02 18:00:55 +08:00
|
|
|
DVec {
|
|
|
|
at: self.at.iter().zip(other.at.iter()).transform(|(a, b)| *a + *b).collect()
|
|
|
|
}
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
impl<N: Sub<N,N>> Sub<DVec<N>, DVec<N>> for DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn sub(&self, other: &DVec<N>) -> DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-09 20:09:22 +08:00
|
|
|
assert!(self.at.len() == other.at.len());
|
2013-07-02 18:00:55 +08:00
|
|
|
DVec {
|
|
|
|
at: self.at.iter().zip(other.at.iter()).transform(|(a, b)| *a - *b).collect()
|
|
|
|
}
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 04:56:30 +08:00
|
|
|
impl<N: Neg<N>> Neg<DVec<N>> for DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn neg(&self) -> DVec<N>
|
2013-07-02 18:00:55 +08:00
|
|
|
{ DVec { at: self.at.iter().transform(|a| -a).collect() } }
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-27 04:56:30 +08:00
|
|
|
impl<N: Ring>
|
2013-06-10 07:36:47 +08:00
|
|
|
Dot<N> for DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn dot(&self, other: &DVec<N>) -> N
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-09 20:09:22 +08:00
|
|
|
assert!(self.at.len() == other.at.len());
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
let mut res = Zero::zero::<N>();
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-09 20:09:22 +08:00
|
|
|
for iterate(0u, self.at.len()) |i|
|
2013-06-19 18:26:59 +08:00
|
|
|
{ res = res + self.at[i] * other.at[i]; }
|
2013-05-31 17:01:07 +08:00
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 04:56:30 +08:00
|
|
|
impl<N: Ring> SubDot<N> for DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn sub_dot(&self, a: &DVec<N>, b: &DVec<N>) -> N
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-10 07:36:47 +08:00
|
|
|
let mut res = Zero::zero::<N>();
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-09 20:09:22 +08:00
|
|
|
for iterate(0u, self.at.len()) |i|
|
2013-06-19 18:26:59 +08:00
|
|
|
{ res = res + (self.at[i] - a.at[i]) * b.at[i]; }
|
2013-05-31 17:01:07 +08:00
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 04:56:30 +08:00
|
|
|
impl<N: Mul<N, N>>
|
2013-06-10 07:36:47 +08:00
|
|
|
ScalarMul<N> for DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn scalar_mul(&self, s: &N) -> DVec<N>
|
2013-07-02 18:00:55 +08:00
|
|
|
{ DVec { at: self.at.iter().transform(|a| a * *s).collect() } }
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn scalar_mul_inplace(&mut self, s: &N)
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-09 20:09:22 +08:00
|
|
|
for iterate(0u, self.at.len()) |i|
|
2013-06-27 04:56:30 +08:00
|
|
|
{ self.at[i] = self.at[i] * *s; }
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-27 04:56:30 +08:00
|
|
|
impl<N: Div<N, N>>
|
2013-06-10 07:36:47 +08:00
|
|
|
ScalarDiv<N> for DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn scalar_div(&self, s: &N) -> DVec<N>
|
2013-07-02 18:00:55 +08:00
|
|
|
{ DVec { at: self.at.iter().transform(|a| a / *s).collect() } }
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn scalar_div_inplace(&mut self, s: &N)
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-09 20:09:22 +08:00
|
|
|
for iterate(0u, self.at.len()) |i|
|
2013-06-27 04:56:30 +08:00
|
|
|
{ self.at[i] = self.at[i] / *s; }
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 04:56:30 +08:00
|
|
|
impl<N: Add<N, N>>
|
2013-06-10 07:36:47 +08:00
|
|
|
ScalarAdd<N> for DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn scalar_add(&self, s: &N) -> DVec<N>
|
2013-07-02 18:00:55 +08:00
|
|
|
{ DVec { at: self.at.iter().transform(|a| a + *s).collect() } }
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn scalar_add_inplace(&mut self, s: &N)
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-09 20:09:22 +08:00
|
|
|
for iterate(0u, self.at.len()) |i|
|
2013-06-27 04:56:30 +08:00
|
|
|
{ self.at[i] = self.at[i] + *s; }
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 04:56:30 +08:00
|
|
|
impl<N: Sub<N, N>>
|
2013-06-10 07:36:47 +08:00
|
|
|
ScalarSub<N> for DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn scalar_sub(&self, s: &N) -> DVec<N>
|
2013-07-02 18:00:55 +08:00
|
|
|
{ DVec { at: self.at.iter().transform(|a| a - *s).collect() } }
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn scalar_sub_inplace(&mut self, s: &N)
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-09 20:09:22 +08:00
|
|
|
for iterate(0u, self.at.len()) |i|
|
2013-06-27 04:56:30 +08:00
|
|
|
{ self.at[i] = self.at[i] - *s; }
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
impl<N: Add<N, N> + Neg<N> + Clone> Translation<DVec<N>> for DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn translation(&self) -> DVec<N>
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
{ self.clone() }
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-29 05:03:40 +08:00
|
|
|
fn inv_translation(&self) -> DVec<N>
|
|
|
|
{ -self }
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn translate_by(&mut self, t: &DVec<N>)
|
2013-05-31 17:01:07 +08:00
|
|
|
{ *self = *self + *t; }
|
|
|
|
}
|
|
|
|
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
impl<N: Add<N, N> + Neg<N> + Clone> Translatable<DVec<N>, DVec<N>> for DVec<N>
|
2013-06-28 00:16:07 +08:00
|
|
|
{
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-28 00:16:07 +08:00
|
|
|
fn translated(&self, t: &DVec<N>) -> DVec<N>
|
|
|
|
{ self + *t }
|
|
|
|
}
|
|
|
|
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
impl<N: DivisionRing + Algebraic + Clone>
|
2013-06-10 07:36:47 +08:00
|
|
|
Norm<N> for DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn sqnorm(&self) -> N
|
2013-05-31 17:01:07 +08:00
|
|
|
{ self.dot(self) }
|
|
|
|
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn norm(&self) -> N
|
2013-05-31 17:01:07 +08:00
|
|
|
{ self.sqnorm().sqrt() }
|
|
|
|
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn normalized(&self) -> DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
Removed occurences of copy/Copy + improved api.
Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].
Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].
Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
2013-07-20 21:07:49 +08:00
|
|
|
let mut res : DVec<N> = self.clone();
|
2013-05-31 17:01:07 +08:00
|
|
|
|
|
|
|
res.normalize();
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn normalize(&mut self) -> N
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
|
|
|
let l = self.norm();
|
|
|
|
|
2013-06-09 20:09:22 +08:00
|
|
|
for iterate(0u, self.at.len()) |i|
|
2013-06-27 04:56:30 +08:00
|
|
|
{ self.at[i] = self.at[i] / l; }
|
2013-05-31 17:01:07 +08:00
|
|
|
|
|
|
|
l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-10 07:36:47 +08:00
|
|
|
impl<N: ApproxEq<N>> ApproxEq<N> for DVec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
{
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn approx_epsilon() -> N
|
|
|
|
{ ApproxEq::approx_epsilon::<N, N>() }
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn approx_eq(&self, other: &DVec<N>) -> bool
|
2013-06-09 20:09:22 +08:00
|
|
|
{
|
|
|
|
let mut zip = self.at.iter().zip(other.at.iter());
|
|
|
|
|
|
|
|
do zip.all |(a, b)| { a.approx_eq(b) }
|
|
|
|
}
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-28 01:40:37 +08:00
|
|
|
#[inline]
|
2013-06-10 07:36:47 +08:00
|
|
|
fn approx_eq_eps(&self, other: &DVec<N>, epsilon: &N) -> bool
|
2013-06-09 20:09:22 +08:00
|
|
|
{
|
|
|
|
let mut zip = self.at.iter().zip(other.at.iter());
|
|
|
|
|
|
|
|
do zip.all |(a, b)| { a.approx_eq_eps(b, epsilon) }
|
|
|
|
}
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|