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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#![allow(dead_code)]
use std::ops::{Add, Sub, Mul, Div};
use std::cmp::{PartialEq, PartialOrd, Ordering};
pub use num::{Zero, One};
use num;
use super::float::Float;
use std::fmt;
use clamp::Clamp;
/// Vec4 is a generic four-component (3D) vector type.
#[derive(Copy, Clone, Debug)]
pub struct Vec4<T>{
x: T,
y: T,
z: T,
w: T
}
impl<T> Vec4<T>{
/// new returns a new vector with the given parameters.
///
/// # Examples
///
/// ```
/// let x = fiz_math::Vec4::new(4.0f32, 8.0f32, 2.0f32, 3.0f32);
/// ```
///
/// ```
/// let x = fiz_math::Vec4::new(1u8, 5u8, 2u8, 3u8);
/// ```
///
/// ```
/// use fiz_math::Vec4;
/// use fiz_math::unit::MM;
///
/// let x = Vec4::new(MM(1.0), MM(5.0), MM(2.0), MM(1.2));
/// let y = Vec4::new(MM(1.0), MM(5.1), MM(1.9), MM(1.1));
/// assert!(x.almost_equal(y, 0.1));
/// ```
pub fn new(x: T, y: T, z: T, w: T) -> Self {
Vec4{x: x, y: y, z: z, w: w}
}
}
impl<T: fmt::Display> fmt::Display for Vec4<T> {
/// fmt formats the vector.
///
/// # Examples
///
/// ```
/// let x = fiz_math::Vec4::new(1u8, 5u8, 2u8, 3u8);
/// assert_eq!(format!("{}", x), "Vec3(1, 5, 2, 3)");
/// ```
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Vec3({}, {}, {}, {})", self.x, self.y, self.z, self.w)
}
}
impl<T: One> One for Vec4<T>{
/// one returns the one value for a vector whose component type implements the
/// num::One trait.
///
/// # Examples
///
/// ```
/// use fiz_math::One;
///
/// let x = fiz_math::Vec4::<f32>::one();
/// ```
///
/// ```
/// use fiz_math::One;
///
/// let x = fiz_math::Vec4::<i64>::one();
/// ```
fn one() -> Self {
Vec4{x: T::one(), y: T::one(), z: T::one(), w: T::one()}
}
}
impl<T: Float> Vec4<T>{
/// almost_equal tells if this vector is equal to the other given an absolute
/// tolerence value (see the almost_equal function for more details).
///
/// # Examples
///
/// ```
/// use fiz_math::Vec4;
///
/// let a = Vec4::<f32>::new(1.0, 1.0, 1.0, 1.0);
/// let b = Vec4::<f32>::new(0.9, 0.9, 0.9, 0.9);
/// assert!(a.almost_equal(b, 0.1000001));
/// assert!(!a.almost_equal(b, 0.1));
/// ```
pub fn almost_equal<N: num::Float>(self, other: Self, abs_tol: N) -> bool {
self.x.almost_equal(other.x, abs_tol) &&
self.y.almost_equal(other.y, abs_tol) &&
self.z.almost_equal(other.z, abs_tol) &&
self.w.almost_equal(other.w, abs_tol)
}
/// is_nan tells if all of this vectors components are NaN.
///
/// # Examples
///
/// ```
/// # extern crate num;
/// # extern crate fiz_math;
/// use num::traits::Float;
/// use fiz_math::Vec4;
///
/// # fn main() {
/// let n:f32 = Float::nan();
/// assert!(Vec4::new(n, n, n, n).is_nan());
/// assert!(!Vec4::new(n, 0.0, 0.0, 0.0).is_nan());
/// # }
/// ```
pub fn is_nan(self) -> bool {
self.x.is_nan() &&
self.y.is_nan() &&
self.z.is_nan() &&
self.w.is_nan()
}
}
impl<T: Add<Output = T>> Add for Vec4<T>{
type Output = Self;
/// add performs component-wise addition of two vectors.
///
/// # Examples
///
/// ```
/// use fiz_math::Vec4;
///
/// let a = Vec4::new(1, 2, 3, 3);
/// let b = Vec4::new(4, 5, 6, 6);
/// assert_eq!(a + b, Vec4::new(5, 7, 9, 9));
/// ```
fn add(self, _rhs: Self) -> Self {
Vec4{
x: self.x + _rhs.x,
y: self.y + _rhs.y,
z: self.z + _rhs.z,
w: self.w + _rhs.w,
}
}
}
impl<T: Add<Output = T> + Copy> Vec4<T> {
/// add_scalar performs scalar addition on a vector.
///
/// # Examples
///
/// ```
/// use fiz_math::Vec4;
///
/// let a = Vec4::new(1, 2, 3, 4);
/// assert_eq!(a.add_scalar(1), Vec4::new(2, 3, 4, 5));
/// ```
pub fn add_scalar(self, _rhs: T) -> Self {
Vec4{
x: self.x + _rhs,
y: self.y + _rhs,
z: self.z + _rhs,
w: self.w + _rhs,
}
}
}
impl<T: Sub<Output = T>> Sub for Vec4<T>{
type Output = Self;
/// sub performs component-wise subtraction of two vectors.
///
/// # Examples
///
/// ```
/// use fiz_math::Vec4;
///
/// let a = Vec4::new(1, 2, 3, 3);
/// let b = Vec4::new(4, 5, 6, 6);
/// assert_eq!(a - b, Vec4::new(-3, -3, -3, -3));
/// ```
fn sub(self, _rhs: Self) -> Self {
Vec4{
x: self.x - _rhs.x,
y: self.y - _rhs.y,
z: self.z - _rhs.z,
w: self.w - _rhs.w,
}
}
}
impl<T: Sub<Output = T> + Copy> Vec4<T> {
/// sub_scalar performs scalar subtraction on a vector.
///
/// # Examples
///
/// ```
/// use fiz_math::Vec4;
///
/// let a = Vec4::new(2, 3, 4, 5);
/// assert_eq!(a.sub_scalar(1), Vec4::new(1, 2, 3, 4));
/// ```
pub fn sub_scalar(self, _rhs: T) -> Self {
Vec4{
x: self.x - _rhs,
y: self.y - _rhs,
z: self.z - _rhs,
w: self.w - _rhs,
}
}
}
impl<T: Mul<Output = T>> Mul for Vec4<T>{
type Output = Self;
/// mul performs component-wise multiplication of two vectors.
///
/// # Examples
///
/// ```
/// use fiz_math::Vec4;
///
/// let a = Vec4::new(1, 2, 3, 3);
/// let b = Vec4::new(4, 5, 6, 6);
/// assert_eq!(a * b, Vec4::new(4, 10, 18, 18));
/// ```
fn mul(self, _rhs: Self) -> Self {
Vec4{
x: self.x * _rhs.x,
y: self.y * _rhs.y,
z: self.z * _rhs.z,
w: self.w * _rhs.w,
}
}
}
impl<T: Mul<Output = T> + Copy> Vec4<T> {
/// mul_scalar performs scalar multiplication on a vector.
///
/// # Examples
///
/// ```
/// use fiz_math::Vec4;
///
/// let a = Vec4::new(1, 2, 3, 4);
/// assert_eq!(a.mul_scalar(2), Vec4::new(2, 4, 6, 8));
/// ```
pub fn mul_scalar(self, _rhs: T) -> Self {
Vec4{
x: self.x * _rhs,
y: self.y * _rhs,
z: self.z * _rhs,
w: self.w * _rhs,
}
}
}
impl<T: Div<Output = T>> Div for Vec4<T>{
type Output = Self;
/// div performs component-wise division of two vectors.
///
/// # Examples
///
/// ```
/// use fiz_math::Vec4;
///
/// let a = Vec4::new(4, 5, 9, 9);
/// let b = Vec4::new(1, 2, 3, 3);
/// assert_eq!(a / b, Vec4::new(4, 2, 3, 3));
/// ```
fn div(self, _rhs: Self) -> Self {
Vec4{
x: self.x / _rhs.x,
y: self.y / _rhs.y,
z: self.z / _rhs.z,
w: self.w / _rhs.w,
}
}
}
impl<T: Div<Output = T> + Copy> Vec4<T> {
/// div_scalar performs scalar division on a vector.
///
/// # Examples
///
/// ```
/// use fiz_math::Vec4;
///
/// let a = Vec4::new(2, 4, 6, 8);
/// assert_eq!(a.div_scalar(2), Vec4::new(1, 2, 3, 4));
/// ```
pub fn div_scalar(self, _rhs: T) -> Self {
Vec4{
x: self.x / _rhs,
y: self.y / _rhs,
z: self.z / _rhs,
w: self.w / _rhs,
}
}
}
impl<T: Clamp<Elem = T> + Copy> Clamp for Vec4<T>{
type Elem = T;
/// clamp returns the vector with each element clamped to the range of
/// [min, max].
///
/// # Examples
///
/// ```
/// use fiz_math::{Vec4, Clamp};
///
/// let a = Vec4::new(-2, 4, -6, 8);
/// assert_eq!(a.clamp(-1, 2), Vec4::new(-1, 2, -1, 2));
/// ```
fn clamp(self, min: T, max: T) -> Self {
Vec4{
x: self.x.clamp(min, max),
y: self.y.clamp(min, max),
z: self.z.clamp(min, max),
w: self.w.clamp(min, max),
}
}
}
impl<T> AsRef<Vec4<T>> for Vec4<T> {
fn as_ref(&self) -> &Self {
self
}
}
impl<T:PartialOrd> Vec4<T> {
/// any_less tells if any component of the other vector is less than any
/// component of this vector.
///
/// # Examples
///
/// ```
/// use fiz_math::Vec4;
///
/// let a = Vec4::new(0, 0, 0, 1);
/// assert!(a.any_less(Vec4::new(0, 0, 0, 2)));
/// ```
pub fn any_less<O:AsRef<Self>>(&self, other: O) -> bool {
let o = other.as_ref();
self.x < o.x || self.y < o.y || self.z < o.z || self.w < o.w
}
/// any_greater tells if any component of the other vector is greater than
/// any component of this vector.
///
/// # Examples
///
/// ```
/// use fiz_math::Vec4;
///
/// let a = Vec4::new(0, 0, 0, 2);
/// assert!(a.any_greater(Vec4::new(0, 0, 0, 1)));
/// ```
pub fn any_greater<O:AsRef<Self>>(&self, other: O) -> bool {
let o = other.as_ref();
self.x > o.x || self.y > o.y || self.z > o.z || self.w > o.w
}
}
impl<T: PartialEq> PartialEq for Vec4<T> {
/// eq tests for component-wise binary equality of two vectors.
///
/// # Examples
///
/// ```
/// use fiz_math::Vec4;
///
/// let a = Vec4::new(4.0, 5.0, 5.0, 9.0);
/// let b = Vec4::new(4.0, 5.0, 5.0, 9.00000000000000000000001);
/// assert_eq!(a, b);
/// ```
///
/// ```
/// use fiz_math::Vec4;
///
/// let a = Vec4::new(4, 5, 9, 9);
/// let b = Vec4::new(4, 5, 9, 9);
/// assert_eq!(a, b);
/// ```
fn eq(&self, _rhs: &Self) -> bool {
self.x == _rhs.x &&
self.y == _rhs.y &&
self.z == _rhs.z &&
self.w == _rhs.w
}
}
impl<T: PartialOrd> PartialOrd for Vec4<T>{
/// partial_cmp compares the two vectors component-wise.
///
/// # Examples
///
/// ```
/// use fiz_math::Vec4;
///
/// let a = Vec4::new(1.0, 2.0, 3.0, 4.0);
/// assert!(a < Vec4::new(1.1, 2.1, 3.1, 4.1));
/// ```
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.x < other.x && self.y < other.y && self.z < other.z && self.w < other.w {
Some(Ordering::Less)
} else if self.x > other.x && self.y > other.y && self.z > other.z && self.w > other.w {
Some(Ordering::Greater)
} else if self == other {
Some(Ordering::Equal)
} else {
None
}
}
}
impl<T: Zero> Zero for Vec4<T>{
/// zero returns the zero-value for the vector.
///
/// # Examples
///
/// ```
/// use fiz_math::{Zero, Vec4};
///
/// let x = Vec4::<u8>::zero();
/// let y = Vec4::<i64>::zero();
/// let z = Vec4::<f32>::zero();
/// let w = Vec4::<f64>::zero();
/// ```
fn zero() -> Self {
Vec4{x: Zero::zero(), y: Zero::zero(), z: Zero::zero(), w: Zero::zero()}
}
/// is_zero tests if the vector is equal to zero.
///
/// # Examples
///
/// ```
/// use fiz_math::{Zero, Vec4};
///
/// assert!(!Vec4::new(1i32, 0, 0, 0).is_zero());
/// assert!(Vec4::new(0u8, 0, 0, 0).is_zero());
/// assert!(!Vec4::new(1.0f32, 0.0, 0.0, 0.0).is_zero());
/// assert!(Vec4::new(0.0f64, 0.0, 0.0, 0.0).is_zero());
/// ```
fn is_zero(&self) -> bool {
self.x.is_zero() &&
self.y.is_zero() &&
self.z.is_zero() &&
self.w.is_zero()
}
}