1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//!The vector module contains primitive objects and traits for performing vector geometry.

pub mod cartesian1;
pub mod cartesian2;
pub mod cartesian3;
pub mod space;
pub use self::cartesian1::*;
pub use self::cartesian2::*;
pub use self::cartesian3::*;
pub use self::space::*;

extern crate num;
use self::num::{Float, Zero};
use std::ops::{Add, Sub, Neg, Mul, Div};

///Trait that implements all the functions necessary for any n-dimensional particle.
pub trait Vector<D>: Sized + Clone + Copy + Zero + Add<Self, Output=Self> + Sub<Self, Output=Self> + Neg<Output=Self> +
    Mul<D, Output=Self> + Div<D, Output=Self>
    where D: Float
{
    ///Returns the space contained by an n-sphere with edge displacement d in the dimensional system of this vector
    fn space_ball(d: D) -> D;

    ///Returns the result of the cos of the angle between two vectors multiplied by their magnitudes
    fn dot(&lhs: &Self, rhs: &Self) -> D;

    ///Returns the space contained by the vector relative to the origin forming a box
    fn space_box(&self) -> D;

    ///Returns the length of a vector
    fn displacement(&self) -> D;

    ///Returns the squared length of a vector; this is more efficient than displacement() for cartesian vectors
    fn displacement_squared(&self) -> D {
        self.displacement().powi(2)
    }

    ///Returns a vector in the same direction as this one, but with length 1
    fn normalized(&self) -> Self {
        *self / self.displacement()
    }

    ///Make this vector normalized().
    fn normalize(&mut self) {
        *self = self.normalized();
    }
}

#[test]
fn dot_vector() {
    let a = Cartesian2::new(0.3, 0.5);
    let _b = Cartesian2::dot(&a, &Cartesian2::new(1.0, 0.5));
}

///CrossVector is a Vector that has dimensions such that the cross product can be computed.
pub trait CrossVector {
    fn cross(lhs: &Self, rhs: &Self) -> Self;
}

#[test]
fn cross_vector() {
    let a = Cartesian3::new(0.3, 0.5, 1.0);
    let _b = Cartesian3::cross(&a, &Cartesian3::new(1.0, 0.5, -2.0));
}