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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//! Functionality pertaining to `Vec4`

use core::ops::{Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign};
use core::fmt;

#[allow(unused_imports)]
use num::{Num, Integer, Unsigned, Signed, Float};

#[allow(unused_imports)]
use super::{Vector, VecItem, VecNum, VecInt, VecUnsigned, VecSigned, VecFloat};

#[cfg(feature = "serialize")]
#[derive(Copy, Clone, Default, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct Vec4<T: VecItem> {
    pub x: T,
    pub y: T,
    pub z: T,
    pub w: T,
}

#[cfg(not(feature = "serialize"))]
#[derive(Copy, Clone, Default, Hash, Eq, PartialEq)]
pub struct Vec4<T: VecItem> {
    pub x: T,
    pub y: T,
    pub z: T,
    pub w: T,
}

impl<T: VecItem> Vec4<T> {
    /// Creates a new Vec3 from multiple components
    pub fn new(x: T, y: T, z: T, w: T) -> Self { Self { x, y, z, w, ..Default::default() } }

    /// Returns the elements of the vector as an array
    pub fn elements(&self) -> [T; 4] { [self.x, self.y, self.z, self.w] }

    /// Apply an operation to all elements of this vector, returning the result
    pub fn map<U: VecItem, F: Fn(T) -> U>(&self, f: F) -> Vec4<U> {
        Vec4 {
            x: f(self.x),
            y: f(self.y),
            z: f(self.z),
            w: f(self.w),
        }
    }

    pub fn convert_to<U: VecItem + From<T>>(&self) -> Vec4<U> {
        Vec4 {
            x: U::from(self.x),
            y: U::from(self.y),
            z: U::from(self.z),
            w: U::from(self.w),
        }
    }
}

impl<T: VecItem> Vector for Vec4<T> {
    type Item = T;
}

// Debug and Display traits

impl<T: VecItem + fmt::Debug> fmt::Debug for Vec4<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "(x: {:?}, y: {:?}, z: {:?}, w: {:?})", self.x, self.y, self.z, self.w)
    }
}

impl<T: VecItem + fmt::Display> fmt::Display for Vec4<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "({}, {}, {}, {})", self.x, self.y, self.z, self.w)
    }
}

// From traits

impl<T: VecItem> From<[T; 4]> for Vec4<T> {
    fn from(arr: [T; 4]) -> Self { Self { x: arr[0], y: arr[1], z: arr[2], w: arr[3], ..Default::default() } }
}

impl<T: VecItem> From<(T, T, T, T)> for Vec4<T> {
    fn from(tup: (T, T, T, T)) -> Self { Self { x: tup.0, y: tup.1, z: tup.2, w: tup.3, ..Default::default() } }
}

// Op traits

impl<T> Add for Vec4<T> where T: VecItem + Add, T::Output: VecItem + Add {
    type Output = Vec4<T::Output>;
    fn add(self, other: Self) -> Vec4<T::Output> {
        Vec4 {
            x: self.x + other.x,
            y: self.y + other.y,
            z: self.z + other.z,
            w: self.w + other.w,
            ..Default::default()
        }
    }
}

impl<T> Sub for Vec4<T> where T: VecItem + Sub, T::Output: VecItem + Sub {
    type Output = Vec4<T::Output>;
    fn sub(self, other: Self) -> Vec4<T::Output> {
        Vec4 {
            x: self.x - other.x,
            y: self.y - other.y,
            z: self.z - other.z,
            w: self.w - other.w,
            ..Default::default()
        }
    }
}

impl<T> Mul for Vec4<T> where T: VecItem + Mul, T::Output: VecItem + Mul {
    type Output = Vec4<T::Output>;
    fn mul(self, other: Self) -> Vec4<T::Output> {
        Vec4 {
            x: self.x * other.x,
            y: self.y * other.y,
            z: self.z * other.z,
            w: self.w * other.w,
            ..Default::default()
        }
    }
}

impl<T> Div for Vec4<T> where T: VecItem + Div, T::Output: VecItem + Div {
    type Output = Vec4<T::Output>;
    fn div(self, other: Self) -> Vec4<T::Output> {
        Vec4 {
            x: self.x / other.x,
            y: self.y / other.y,
            z: self.z / other.z,
            w: self.w / other.w,
            ..Default::default()
        }
    }
}

// Op primitive traits

impl<T> Add<T> for Vec4<T> where T: VecItem + Add, T::Output: VecItem + Add {
    type Output = Vec4<T::Output>;
    fn add(self, other: T) -> Vec4<T::Output> {
        Vec4 {
            x: self.x + other,
            y: self.y + other,
            z: self.z + other,
            w: self.w + other,
            ..Default::default()
        }
    }
}

impl<T> Sub<T> for Vec4<T> where T: VecItem + Sub, T::Output: VecItem + Sub {
    type Output = Vec4<T::Output>;
    fn sub(self, other: T) -> Vec4<T::Output> {
        Vec4 {
            x: self.x - other,
            y: self.y - other,
            z: self.z - other,
            w: self.w - other,
            ..Default::default()
        }
    }
}

impl<T> Mul<T> for Vec4<T> where T: VecItem + Mul, T::Output: VecItem + Mul {
    type Output = Vec4<T::Output>;
    fn mul(self, other: T) -> Vec4<T::Output> {
        Vec4 {
            x: self.x * other,
            y: self.y * other,
            z: self.z * other,
            w: self.w * other,
            ..Default::default()
        }
    }
}

impl<T> Div<T> for Vec4<T> where T: VecItem + Div, T::Output: VecItem + Div {
    type Output = Vec4<T::Output>;
    fn div(self, other: T) -> Vec4<T::Output> {
        Vec4 {
            x: self.x / other,
            y: self.y / other,
            z: self.z / other,
            w: self.w / other,
            ..Default::default()
        }
    }
}

// Assign operators

impl<T> AddAssign for Vec4<T> where T: VecItem + Add<Output=T> {
    fn add_assign(&mut self, other: Self) {
        *self = Vec4 {
            x: self.x + other.x,
            y: self.y + other.y,
            z: self.z + other.z,
            w: self.w + other.w,
            ..Default::default()
        }
    }
}

impl<T> SubAssign for Vec4<T> where T: VecItem + Sub<Output=T> {
    fn sub_assign(&mut self, other: Self) {
        *self = Vec4 {
            x: self.x - other.x,
            y: self.y - other.y,
            z: self.z - other.z,
            w: self.w - other.w,
            ..Default::default()
        }
    }
}

impl<T> MulAssign for Vec4<T> where T: VecItem + Mul<Output=T> {
    fn mul_assign(&mut self, other: Self) {
        *self = Vec4 {
            x: self.x * other.x,
            y: self.y * other.y,
            z: self.z * other.z,
            w: self.w * other.w,
            ..Default::default()
        }
    }
}

impl<T> DivAssign for Vec4<T> where T: VecItem + Div<Output=T> {
    fn div_assign(&mut self, other: Self) {
        *self = Vec4 {
            x: self.x / other.x,
            y: self.y / other.y,
            z: self.z / other.z,
            w: self.w / other.w,
            ..Default::default()
        }
    }
}

// Assign primitive operators

impl<T> AddAssign<T> for Vec4<T> where T: VecItem + Add<Output=T> {
    fn add_assign(&mut self, other: T) {
        *self = Vec4 {
            x: self.x + other,
            y: self.y + other,
            z: self.z + other,
            w: self.w + other,
            ..Default::default()
        }
    }
}

impl<T> SubAssign<T> for Vec4<T> where T: VecItem + Sub<Output=T> {
    fn sub_assign(&mut self, other: T) {
        *self = Vec4 {
            x: self.x - other,
            y: self.y - other,
            z: self.z - other,
            w: self.w - other,
            ..Default::default()
        }
    }
}

impl<T> MulAssign<T> for Vec4<T> where T: VecItem + Mul<Output=T> {
    fn mul_assign(&mut self, other: T) {
        *self = Vec4 {
            x: self.x * other,
            y: self.y * other,
            z: self.z * other,
            w: self.w * other,
            ..Default::default()
        }
    }
}

impl<T> DivAssign<T> for Vec4<T> where T: VecItem + Div<Output=T> {
    fn div_assign(&mut self, other: T) {
        *self = Vec4 {
            x: self.x / other,
            y: self.y / other,
            z: self.z / other,
            w: self.w / other,
            ..Default::default()
        }
    }
}

// VecNum traits

impl<T> VecNum for Vec4<T> where T: VecItem + Num {
    fn sum(&self) -> Self::Item {
        self.x + self.y + self.z + self.w
    }

    fn product(&self) -> Self::Item {
        self.x * self.y * self.z * self.w
    }
}

// VecInt traits

impl<T> VecInt for Vec4<T> where T: VecItem + Integer {
    fn div_euc(&self, other: Self) -> Self {
        Self {
            x: self.x.div_floor(&other.x),
            y: self.y.div_floor(&other.y),
            z: self.z.div_floor(&other.z),
            w: self.w.div_floor(&other.w),
        }
    }
}

// VecSigned traits

impl<T> VecSigned for Vec4<T> where T: VecItem + Signed {
    fn snake_length(&self) -> Self::Item {
        self.x.abs() + self.y.abs() + self.z.abs() + self.w.abs()
    }
}

// VecFloat traits

impl<T> VecFloat for Vec4<T> where T: VecItem + Float {
    fn length(&self) -> Self::Item {
        (self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w).sqrt()
    }

    fn norm(&self) -> Self {
        let len = self.length();
        Vec4::new(
            self.x / len,
            self.y / len,
            self.z / len,
            self.w / len,
        )
    }

    fn floor(&self) -> Self {
        self.map(|e| e.floor())
    }

    fn ceil(&self) -> Self {
        self.map(|e| e.ceil())
    }

    fn round(&self) -> Self {
        self.map(|e| e.round())
    }
}