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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) 2016-2017 <daggerbot@gmail.com>
// This software is available under the terms of the zlib license.
// See COPYING.md for more information.

use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Not, Rem, Shl, Shr, Sub};

use numeric::{Lerp, One, RawFrom, Sqrt, TryAdd, TryDiv, TryMul, TrySub, Zero};

use complex::Complex;

/// Two dimensional vector structure.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
pub struct Vec2<T> (pub T, pub T);

impl<T> Vec2<T> {
    pub fn dot (self, rhs: Vec2<T>) -> T where T: Add<Output = T> + Mul<Output = T> {
        (self * rhs).sum()
    }

    pub fn magnitude (self) -> T where T: Copy + Add<Output = T> + Mul<Output = T> + Sqrt {
        self.square_magnitude().sqrt()
    }

    pub fn product (self) -> T where T: Mul<Output = T> {
        self.0 * self.1
    }

    pub fn square_magnitude (self) -> T where T: Copy + Add<Output = T> + Mul<Output = T> {
        self.0 * self.0 + self.1 * self.1
    }

    pub fn sum (self) -> T where T: Add<Output = T> {
        self.0 + self.1
    }

    #[inline(always)]
    pub fn x (self) -> T { self.0 }

    #[inline(always)]
    pub fn y (self) -> T { self.1 }
}

impl<T> From<Complex<T>> for Vec2<T> {
    fn from (other: Complex<T>) -> Vec2<T> { Vec2(other.0, other.1) }
}

impl<S: Lerp<T>, T: Copy> Lerp<T> for Vec2<S> {
    fn lerp (a: Vec2<S>, b: Vec2<S>, t: T) -> Vec2<S> {
        Vec2(S::lerp(a.0, b.0, t), S::lerp(a.1, b.1, t))
    }
}

impl<T: One> One for Vec2<T> {
    fn one () -> Vec2<T> { Vec2(T::one(), T::one()) }
}

impl<F, T: RawFrom<F>> RawFrom<Vec2<F>> for Vec2<T> {
    fn raw_from (other: Vec2<F>) -> Vec2<T> { Vec2(T::raw_from(other.0), T::raw_from(other.1)) }
}

impl<T: Zero> Zero for Vec2<T> {
    fn zero () -> Vec2<T> { Vec2(T::zero(), T::zero()) }
}

/// Three dimensional vector structure.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
pub struct Vec3<T> (pub T, pub T, pub T);

impl<T> Vec3<T> {
    pub fn dot (self, rhs: Vec3<T>) -> T where T: Add<Output = T> + Mul<Output = T> {
        (self * rhs).sum()
    }

    pub fn magnitude (self) -> T where T: Copy + Add<Output = T> + Mul<Output = T> + Sqrt {
        self.square_magnitude().sqrt()
    }

    pub fn product (self) -> T where T: Mul<Output = T> {
        self.0 * self.1 * self.2
    }

    pub fn square_magnitude (self) -> T where T: Copy + Add<Output = T> + Mul<Output = T> {
        self.0 * self.0 + self.1 * self.1 + self.2 * self.2
    }

    pub fn sum (self) -> T where T: Add<Output = T> {
        self.0 + self.1 + self.2
    }

    #[inline(always)]
    pub fn x (self) -> T { self.0 }

    #[inline(always)]
    pub fn y (self) -> T { self.1 }

    #[inline(always)]
    pub fn z (self) -> T { self.2 }
}

impl<S: Lerp<T>, T: Copy> Lerp<T> for Vec3<S> {
    fn lerp (a: Vec3<S>, b: Vec3<S>, t: T) -> Vec3<S> {
        Vec3(S::lerp(a.0, b.0, t), S::lerp(a.1, b.1, t), S::lerp(a.2, b.2, t))
    }
}

impl<T: One> One for Vec3<T> {
    fn one () -> Vec3<T> { Vec3(T::one(), T::one(), T::one()) }
}

impl<T: Zero> Zero for Vec3<T> {
    fn zero () -> Vec3<T> { Vec3(T::zero(), T::zero(), T::zero()) }
}

macro_rules! impl_vec_ops {
    { $(impl $op_trait:ident::$op_fn:ident for $vec:ident($($idx:tt),*);)* } => { $(
        impl<T: $op_trait> $op_trait for $vec<T> {
            type Output = $vec<T::Output>;

            fn $op_fn (self) -> $vec<T::Output> {
                $vec($(T::$op_fn(self.$idx)),*)
            }
        }
    )* };
}

macro_rules! impl_vec_scalar_ops {
    { $(impl $op_trait:ident::$op_fn:ident for $vec:ident($($idx:tt),*);)* } => { $(
        impl<T: $op_trait<Output = T> + Copy> $op_trait<T> for $vec<T> {
            type Output = $vec<T>;

            fn $op_fn (self, rhs: T) -> $vec<T> {
                $vec($(T::$op_fn(self.$idx, rhs)),*)
            }
        }
    )* };
}

macro_rules! impl_vec_vec_ops {
    { $(impl $op_trait:ident::$op_fn:ident for $vec:ident($($idx:tt),*);)* } => { $(
        impl<T: $op_trait<Output = T>> $op_trait for $vec<T> {
            type Output = $vec<T>;

            fn $op_fn (self, rhs: $vec<T>) -> $vec<T> {
                $vec($(T::$op_fn(self.$idx, rhs.$idx)),*)
            }
        }
    )* };
}

macro_rules! impl_try_vec_vec_ops {
    { $(impl $op_trait:ident::$op_fn:ident for $vec:ident($($idx:tt),*);)* } => { $(
        impl<T: $op_trait<Output = T>> $op_trait for $vec<T> {
            type Output = $vec<T>;
            type Err = T::Err;

            fn $op_fn (self, rhs: $vec<T>) -> Result<$vec<T>, T::Err> {
                Ok($vec($(T::$op_fn(self.$idx, rhs.$idx)?),*))
            }
        }
    )* };
}

macro_rules! impl_vec_shift_ops {
    { $(impl $op_trait:ident::$op_fn:ident for $vec:ident($($idx:tt),*);)* } => { $(
        impl<R: Copy, T: $op_trait<R, Output = T>> $op_trait<R> for $vec<T> {
            type Output = $vec<T>;

            fn $op_fn (self, rhs: R) -> $vec<T> {
                $vec($(T::$op_fn(self.$idx, rhs)),*)
            }
        }
    )* };
}

impl_vec_ops! {
    impl Neg::neg for Vec2(0, 1);
    impl Neg::neg for Vec3(0, 1, 2);
    impl Not::not for Vec2(0, 1);
    impl Not::not for Vec3(0, 1, 2);
}

impl_vec_scalar_ops! {
    impl Mul::mul for Vec2(0, 1);
    impl Mul::mul for Vec3(0, 1, 2);
    impl Div::div for Vec2(0, 1);
    impl Div::div for Vec3(0, 1, 2);
    impl Rem::rem for Vec2(0, 1);
    impl Rem::rem for Vec3(0, 1, 2);
    impl BitAnd::bitand for Vec2(0, 1);
    impl BitAnd::bitand for Vec3(0, 1, 2);
    impl BitOr::bitor for Vec2(0, 1);
    impl BitOr::bitor for Vec3(0, 1, 2);
    impl BitXor::bitxor for Vec2(0, 1);
    impl BitXor::bitxor for Vec3(0, 1, 2);
}

impl_vec_vec_ops! {
    impl Add::add for Vec2(0, 1);
    impl Add::add for Vec3(0, 1, 2);
    impl Sub::sub for Vec2(0, 1);
    impl Sub::sub for Vec3(0, 1, 2);
    impl Mul::mul for Vec2(0, 1);
    impl Mul::mul for Vec3(0, 1, 2);
    impl Div::div for Vec2(0, 1);
    impl Div::div for Vec3(0, 1, 2);
    impl Rem::rem for Vec2(0, 1);
    impl Rem::rem for Vec3(0, 1, 2);
    impl BitAnd::bitand for Vec2(0, 1);
    impl BitAnd::bitand for Vec3(0, 1, 2);
    impl BitOr::bitor for Vec2(0, 1);
    impl BitOr::bitor for Vec3(0, 1, 2);
    impl BitXor::bitxor for Vec2(0, 1);
    impl BitXor::bitxor for Vec3(0, 1, 2);
}

impl_try_vec_vec_ops! {
    impl TryAdd::try_add for Vec2(0, 1);
    impl TryAdd::try_add for Vec3(0, 1, 2);
    impl TrySub::try_sub for Vec2(0, 1);
    impl TrySub::try_sub for Vec3(0, 1, 2);
    impl TryMul::try_mul for Vec2(0, 1);
    impl TryMul::try_mul for Vec3(0, 1, 2);
    impl TryDiv::try_div for Vec2(0, 1);
    impl TryDiv::try_div for Vec3(0, 1, 2);
}

impl_vec_shift_ops! {
    impl Shl::shl for Vec2(0, 1);
    impl Shl::shl for Vec3(0, 1, 2);
    impl Shr::shr for Vec2(0, 1);
    impl Shr::shr for Vec3(0, 1, 2);
}