1use std::ops::Index;
2use std::ops::IndexMut;
3use std::ops::Mul;
4use std::ops::MulAssign;
5use std::ops::Add;
6use std::ops::AddAssign;
7use std::ops::Sub;
8use std::ops::SubAssign;
9use std::ops::Div;
10use std::ops::DivAssign;
11use std::ops::Rem;
12use std::ops::RemAssign;
13use std::ops::Neg;
14use std::ops::Deref;
15use std::ops::DerefMut;
16
17use std::cmp::PartialEq;
18
19use std::fmt::Display;
20use std::fmt::Formatter;
21
22use crate::num::*;
23
24pub trait VecN<T: Number>:
30 Base<T> + Dot<T> +
31 Index<usize, Output=T> + IndexMut<usize> +
32 Add<T, Output=Self> + Sub<T, Output=Self> +
33 Mul<T, Output=Self> + Div<T, Output=Self> {
34 fn len() -> usize;
36 fn all(a: Self) -> bool;
38 fn any(a: Self) -> bool;
40 fn unit_x() -> Self;
42 fn unit_y() -> Self;
44 fn unit_z() -> Self;
46 fn unit_w() -> Self;
48 fn red() -> Self;
50 fn green() -> Self;
52 fn blue() -> Self;
54 fn cyan() -> Self;
56 fn magenta() -> Self;
58 fn yellow() -> Self;
60 fn black() -> Self;
62 fn white() -> Self;
64 fn as_slice(&self) -> &[T];
66 fn as_mut_slice(&mut self) -> &mut [T];
68 fn as_u8_slice(&self) -> &[u8];
70 fn max_scalar(a: Self) -> T;
72 fn min_scalar(a: Self) -> T;
74}
75
76pub trait SignedVecN<T: SignedNumber>: Neg<Output=Self> {
78 fn minus_one() -> Self;
80}
81
82pub trait Magnitude<T: Float> {
84 fn length(a: Self) -> T;
86 fn mag(a: Self) -> T;
88 fn mag2(a: Self) -> T;
90 fn normalize(a: Self) -> Self;
92}
93
94pub trait VecFloatOps<T: Float>: SignedVecN<T> + Magnitude<T> {
96 fn distance(a: Self, b: Self) -> T;
98 fn dist(a: Self, b: Self) -> T;
100 fn dist2(a: Self, b: Self) -> T;
102 fn reflect(i: Self, n: Self) -> Self;
104 fn refract(i: Self, n: Self, eta: T) -> Self;
106 fn vlerp(e0: Self, e1: Self, t: Self) -> Self;
108 fn vsmoothstep(e0: Self, e1: Self, t: Self) -> Self;
110 fn powfn(a: Self, exp: Self) -> Self;
112}
113
114pub trait Dot<T> {
116 fn dot(a: Self, b: Self) -> T;
118}
119
120impl<T> Dot<T> for Vec2<T> where T: Number {
121 fn dot(a: Self, b: Self) -> T {
122 a.x * b.x + a.y * b.y
123 }
124}
125
126impl<T> Dot<T> for Vec3<T> where T: Number {
127 fn dot(a: Self, b: Self) -> T {
128 a.x * b.x + a.y * b.y + a.z * b.z
129 }
130}
131
132impl<T> Dot<T> for Vec4<T> where T: Number {
133 fn dot(a: Self, b: Self) -> T {
134 a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w
135 }
136}
137
138pub trait Cross<T> {
140 fn cross(a: Self, b: Self) -> Self;
142}
143
144impl<T> Cross<T> for Vec3<T> where T: Number {
145 fn cross(a: Self, b: Self) -> Self {
146 Vec3 {
147 x: (a.y * b.z) - (a.z * b.y),
148 y: (a.z * b.x) - (a.x * b.z),
149 z: (a.x * b.y) - (a.y * b.x),
150 }
151 }
152}
153
154pub trait Triple<T> {
156 fn scalar_triple(a: Self, b: Self, c: Self) -> T;
158 fn vector_triple(a: Self, b: Self, c: Self) -> Self;
160}
161
162impl<T> Triple<T> for Vec3<T> where T: Number {
164 fn scalar_triple(a: Self, b: Self, c: Self) -> T {
165 a.x * (b.y * c.z - b.z * c.y) + a.y * (b.z * c.x - b.x * c.z) + a.z * (b.x * c.y - b.y * c.x)
166 }
167
168 fn vector_triple(a: Self, b: Self, c: Self) -> Self {
169 Self::cross(Self::cross(a, b), c)
170 }
171}
172
173impl<T> Triple<T> for Vec2<T> where T: Number + SignedNumber {
175 fn scalar_triple(_a: Self, b: Self, c: Self) -> T {
176 b.x * c.y - b.y * c.x
177 }
178
179 fn vector_triple(a: Self, b: Self, c: Self) -> Self {
180 let a3 = Vec3::<T>::new(a.x, a.y, T::zero());
181 let b3 = Vec3::<T>::new(b.x, b.y, T::zero());
182 let c3 = Vec3::<T>::new(c.x, c.y, T::zero());
183 let v3 = Vec3::<T>::cross(Vec3::<T>::cross(a3, b3), c3);
184 Self {
185 x: v3.x,
186 y: v3.y
187 }
188 }
189}
190
191pub trait Slerp<T: Float + FloatOps<T>> {
193 fn slerp(e0: Self, e1: Self, t: T) -> Self;
195}
196
197pub trait Nlerp<T: Float> {
199 fn nlerp(e0: Self, e1: Self, t: T) -> Self;
201}
202
203macro_rules! vec_impl {
209 ($VecN:ident { $($field:ident, $field_index:expr),* }, $len:expr, $module:ident) => {
210
211 #[cfg_attr(feature="serde", derive(serde::Serialize, serde::Deserialize))]
212 #[cfg_attr(feature="hash", derive(Hash))]
213 #[derive(Debug, Copy, Clone)]
214 #[repr(C)]
215 pub struct $VecN<T> {
216 $(pub $field: T,)+
217 }
218
219 impl<T> $VecN<T> where T: Number {
220 pub fn new($($field: T,)+) -> $VecN<T> {
222 $VecN {
223 $($field,)+
224 }
225 }
226 }
227
228 impl<T> VecN<T> for $VecN<T> where T: Number {
229 fn len() -> usize {
230 $len
231 }
232
233 fn all(a: Self) -> bool {
234 $(a.$field != T::zero() &&)+
235 true
236 }
237
238 fn any(a: Self) -> bool {
239 $(a.$field != T::zero() ||)+
240 false
241 }
242
243 fn max_scalar(a: Self) -> T {
244 let mut max = a[0];
245 for i in 1..Self::len() {
246 if a[i] > max {
247 max = a[i];
248 }
249 }
250 max
251 }
252
253 fn min_scalar(a: Self) -> T {
254 let mut min = a[0];
255 for i in 1..Self::len() {
256 if a[i] < min {
257 min = a[i];
258 }
259 }
260 min
261 }
262
263 fn unit_x() -> $VecN<T> {
264 let v = [T::one(), T::zero(), T::zero(), T::zero()];
265 Self {
266 $($field: v[$field_index],)+
267 }
268 }
269
270 fn unit_y() -> $VecN<T> {
271 let v = [T::zero(), T::one(), T::zero(), T::zero()];
272 Self {
273 $($field: v[$field_index],)+
274 }
275 }
276
277 fn unit_z() -> $VecN<T> {
278 let v = [T::zero(), T::zero(), T::one(), T::zero()];
279 Self {
280 $($field: v[$field_index],)+
281 }
282 }
283
284 fn unit_w() -> $VecN<T> {
285 let v = [T::zero(), T::zero(), T::zero(), T::one()];
286 Self {
287 $($field: v[$field_index],)+
288 }
289 }
290
291 fn red() -> $VecN<T> {
292 let v = [T::one(), T::zero(), T::zero(), T::one()];
293 Self {
294 $($field: v[$field_index],)+
295 }
296 }
297
298 fn green() -> $VecN<T> {
299 let v = [T::zero(), T::one(), T::zero(), T::one()];
300 Self {
301 $($field: v[$field_index],)+
302 }
303 }
304
305 fn blue() -> $VecN<T> {
306 let v = [T::zero(), T::zero(), T::one(), T::one()];
307 Self {
308 $($field: v[$field_index],)+
309 }
310 }
311
312 fn cyan() -> $VecN<T> {
313 let v = [T::zero(), T::one(), T::one(), T::one()];
314 Self {
315 $($field: v[$field_index],)+
316 }
317 }
318
319 fn magenta() -> $VecN<T> {
320 let v = [T::one(), T::zero(), T::one(), T::one()];
321 Self {
322 $($field: v[$field_index],)+
323 }
324 }
325
326 fn yellow() -> $VecN<T> {
327 let v = [T::one(), T::one(), T::zero(), T::one()];
328 Self {
329 $($field: v[$field_index],)+
330 }
331 }
332
333 fn black() -> $VecN<T> {
334 let v = [T::zero(), T::zero(), T::zero(), T::one()];
335 Self {
336 $($field: v[$field_index],)+
337 }
338 }
339
340 fn white() -> $VecN<T> {
341 Self::one()
342 }
343
344 fn as_slice(&self) -> &[T] {
345 unsafe {
349 std::slice::from_raw_parts(&self.x, $len)
350 }
351 }
352
353 fn as_mut_slice(&mut self) -> &mut [T] {
354 unsafe {
358 std::slice::from_raw_parts_mut(&mut self.x, $len)
359 }
360 }
361
362 fn as_u8_slice(&self) -> &[u8] {
363 unsafe {
367 std::slice::from_raw_parts((&self.x as *const T) as *const u8, std::mem::size_of::<$VecN<T>>())
368 }
369 }
370 }
371
372 impl<T> SignedVecN<T> for $VecN<T> where T: SignedNumber {
373 fn minus_one() -> Self {
374 $VecN {
375 $($field: T::minus_one(),)+
376 }
377 }
378 }
379
380 impl<T> IntegerOps<T> for $VecN<T> where T: Integer + IntegerOps<T> {
381 fn pow(a: Self, exp: u32) -> Self {
382 Self {
383 $($field: T::pow(a.$field, exp),)+
384 }
385 }
386 }
387
388 impl<T> NumberOps<T> for $VecN<T> where T: Number + NumberOps<T> {
389 fn min(a: Self, b: Self) -> Self {
390 Self {
391 $($field: T::min(a.$field, b.$field),)+
392 }
393 }
394
395 fn max(a: Self, b: Self) -> Self {
396 Self {
397 $($field: T::max(a.$field, b.$field),)+
398 }
399 }
400
401 fn clamp(x: Self, min: Self, max: Self) -> Self {
402 Self {
403 $($field: T::max(T::min(x.$field, max.$field), min.$field),)+
404 }
405 }
406
407 fn step(a: Self, b: Self) -> Self {
408 Self {
409 $($field: T::step(a.$field, b.$field),)+
410 }
411 }
412 }
413
414 impl<T> SignedNumberOps<T> for $VecN<T> where T: SignedNumber + SignedNumberOps<T> {
415 fn signum(a: Self) -> Self {
416 Self {
417 $($field: T::signum(a.$field),)+
418 }
419 }
420
421 fn abs(a: Self) -> Self {
422 Self {
423 $($field: T::abs(a.$field),)+
424 }
425 }
426 }
427
428 impl<T> Magnitude<T> for $VecN<T> where T: Float + FloatOps<T> {
429 fn length(a: Self) -> T {
430 T::sqrt(Self::dot(a, a))
431 }
432
433 fn mag(a: Self) -> T {
434 T::sqrt(Self::dot(a, a))
435 }
436
437 fn mag2(a: Self) -> T {
438 Self::dot(a, a)
439 }
440
441 fn normalize(a: Self) -> Self {
442 let m = Self::mag(a);
443 a / m
444 }
445 }
446
447 impl<T> VecFloatOps<T> for $VecN<T> where T: Float + FloatOps<T> {
448 fn distance(a: Self, b: Self) -> T {
449 let c = a-b;
450 T::sqrt(Self::dot(c, c))
451 }
452
453 fn dist(a: Self, b: Self) -> T {
454 Self::distance(a, b)
455 }
456
457 fn dist2(a: Self, b: Self) -> T {
458 let c = a-b;
459 Self::dot(c, c)
460 }
461
462 fn reflect(i: Self, n: Self) -> Self {
463 i - Self::from(T::two()) * n * Self::dot(i, n)
465 }
466
467 fn refract(i: Self, n: Self, eta: T) -> Self {
468 let n_dot_i = Self::dot(n, i);
470 let k = T::one() - eta * eta * (T::one() - n_dot_i * n_dot_i);
471 if k < T::zero() {
472 Self::zero()
473 }
474 else {
475 (i * eta) - n * ((n_dot_i + T::sqrt(k)) * eta)
476 }
477 }
478
479 fn vlerp(e0: Self, e1: Self, t: Self) -> Self {
480 Self {
481 $($field: T::lerp(e0.$field, e1.$field, t.$field),)+
482 }
483 }
484
485 fn vsmoothstep(e0: Self, e1: Self, t: Self) -> Self {
486 Self {
487 $($field: T::smoothstep(e0.$field, e1.$field, t.$field),)+
488 }
489 }
490
491 fn powfn(a: Self, exp: Self) -> Self {
492 Self {
493 $($field: T::powf(a.$field, exp.$field),)+
494 }
495 }
496 }
497
498 impl<T> Slerp<T> for $VecN<T> where T: Float + FloatOps<T> + NumberOps<T> {
499 fn slerp(e0: Self, e1: Self, t: T) -> Self {
500 let dot = Self::dot(e0, e1);
502 let dot = T::clamp(dot, T::minus_one(), T::one());
503 let theta = T::acos(dot) * t;
504 let v = Self::normalize(e1 - e0 * dot);
505 ((e0 * T::cos(theta)) + (v * T::sin(theta)))
506 }
507 }
508
509 impl<T> Nlerp<T> for $VecN<T> where T: Float + FloatOps<T> + NumberOps<T> {
510 fn nlerp(e0: Self, e1: Self, t: T) -> Self {
511 Self::normalize( Self {
512 $($field: T::lerp(e0.$field, e1.$field, t),)+
513 })
514 }
515 }
516
517 impl<T> Base<T> for $VecN<T> where T: Number {
518 fn zero() -> Self {
519 $VecN {
520 $($field: T::zero(),)+
521 }
522 }
523
524 fn one() -> Self {
525 $VecN {
526 $($field: T::one(),)+
527 }
528 }
529
530 fn two() -> Self {
531 $VecN {
532 $($field: T::two(),)+
533 }
534 }
535
536 fn three() -> Self {
537 $VecN {
538 $($field: T::three(),)+
539 }
540 }
541
542 fn four() -> Self {
543 $VecN {
544 $($field: T::four(),)+
545 }
546 }
547
548 fn min_value() -> Self {
549 $VecN {
550 $($field: T::min_value(),)+
551 }
552 }
553
554 fn max_value() -> Self {
555 $VecN {
556 $($field: T::max_value(),)+
557 }
558 }
559 }
560
561 impl<T> Lerp<T> for $VecN<T> where T: Float + Lerp<T> {
562 fn lerp(e0: Self, e1: Self, t: T) -> Self {
563 Self {
564 $($field: T::lerp(e0.$field, e1.$field, t),)+
565 }
566 }
567 }
568
569 impl<T> FloatOps<T> for $VecN<T> where T: Float + SignedNumberOps<T> + NumberOps<T> + FloatOps<T> {
570 fn point_five() -> Self {
571 $VecN {
572 $($field: T::point_five(),)+
573 }
574 }
575
576 fn pi() -> Self {
577 $VecN {
578 $($field: T::pi(),)+
579 }
580 }
581
582 fn two_pi() -> Self {
583 $VecN {
584 $($field: T::two_pi(),)+
585 }
586 }
587
588 fn inv_pi() -> Self {
589 $VecN {
590 $($field: T::inv_pi(),)+
591 }
592 }
593
594 fn phi() -> Self {
595 $VecN {
596 $($field: T::phi(),)+
597 }
598 }
599
600 fn inv_phi() -> Self {
601 $VecN {
602 $($field: T::inv_phi(),)+
603 }
604 }
605
606 fn tau() -> Self {
607 $VecN {
608 $($field: T::tau(),)+
609 }
610 }
611
612 fn sqrt(a: Self) -> Self {
613 Self {
614 $($field: T::sqrt(a.$field),)+
615 }
616 }
617
618 fn rsqrt(a: Self) -> Self {
619 Self {
620 $($field: T::recip(T::sqrt(a.$field)),)+
621 }
622 }
623
624 fn recip(a: Self) -> Self {
625 Self {
626 $($field: T::recip(a.$field),)+
627 }
628 }
629
630 fn powi(a: Self, exp: i32) -> Self {
631 Self {
632 $($field: T::powi(a.$field, exp),)+
633 }
634 }
635
636 fn powf(a: Self, exp: T) -> Self {
637 Self {
638 $($field: T::powf(a.$field, exp),)+
639 }
640 }
641
642 fn mad(m: Self, a: Self, b: Self) -> Self {
643 Self {
644 $($field: T::mad(m.$field, a.$field, b.$field),)+
645 }
646 }
647
648 fn approx(a: Self, b: Self, eps: T) -> bool {
649 $(T::abs(a.$field - b.$field) < eps &&)+
650 true
651 }
652
653 fn floor(a: Self) -> Self {
654 Self {
655 $($field: T::floor(a.$field),)+
656 }
657 }
658
659 fn ceil(a: Self) -> Self {
660 Self {
661 $($field: T::ceil(a.$field),)+
662 }
663 }
664
665 fn copysign(a: Self, sign: T) -> Self {
666 Self {
667 $($field: T::copysign(a.$field, sign),)+
668 }
669 }
670
671 fn smoothstep(e0: Self, e1: Self, t: T) -> Self {
672 Self {
673 $($field: T::smoothstep(e0.$field, e1.$field, t),)+
674 }
675 }
676
677 fn round(a: Self) -> Self {
678 Self {
679 $($field: T::round(a.$field),)+
680 }
681 }
682
683 fn is_nan(a: Self) -> Self {
684 Self {
685 $($field: T::is_nan(a.$field),)+
686 }
687 }
688
689 fn is_infinite(a: Self) -> Self {
690 Self {
691 $($field: T::is_infinite(a.$field),)+
692 }
693 }
694
695 fn is_finite(a: Self) -> Self {
696 Self {
697 $($field: T::is_finite(a.$field),)+
698 }
699 }
700
701 fn saturate(x: Self) -> Self {
702 Self::clamp(x, Self::zero(), Self::one())
703 }
704
705 fn deg_to_rad(a: Self) -> Self {
706 Self {
707 $($field: T::deg_to_rad(a.$field),)+
708 }
709 }
710
711 fn rad_to_deg(a: Self) -> Self {
712 Self {
713 $($field: T::rad_to_deg(a.$field),)+
714 }
715 }
716
717 fn fmod(x: Self, y: Self) -> Self {
718 x % y
719 }
720
721 fn frac(v: Self) -> Self {
722 Self {
723 $($field: T::frac(v.$field),)+
724 }
725 }
726
727 fn trunc(v: Self) -> Self {
728 Self {
729 $($field: T::trunc(v.$field),)+
730 }
731 }
732
733 fn modf(v: Self) -> (Self, Self) {
734 (
735 Self {
736 $($field: T::frac(v.$field),)+
737 },
738 Self {
739 $($field: T::trunc(v.$field),)+
740 }
741 )
742 }
743
744 fn cos(v: Self) -> Self {
745 Self {
746 $($field: T::cos(v.$field),)+
747 }
748 }
749
750 fn sin(v: Self) -> Self {
751 Self {
752 $($field: T::sin(v.$field),)+
753 }
754 }
755
756 fn tan(v: Self) -> Self {
757 Self {
758 $($field: T::tan(v.$field),)+
759 }
760 }
761
762 fn acos(v: Self) -> Self {
763 Self {
764 $($field: T::acos(v.$field),)+
765 }
766 }
767
768 fn asin(v: Self) -> Self {
769 Self {
770 $($field: T::asin(v.$field),)+
771 }
772 }
773
774 fn atan(v: Self) -> Self {
775 Self {
776 $($field: T::atan(v.$field),)+
777 }
778 }
779
780 fn cosh(v: Self) -> Self {
781 Self {
782 $($field: T::cosh(v.$field),)+
783 }
784 }
785
786 fn sinh(v: Self) -> Self {
787 Self {
788 $($field: T::sinh(v.$field),)+
789 }
790 }
791
792 fn tanh(v: Self) -> Self {
793 Self {
794 $($field: T::tanh(v.$field),)+
795 }
796 }
797
798 fn sin_cos(v: Self) -> (Self, Self) {
799 (
800 Self {
801 $($field: T::sin(v.$field),)+
802 },
803 Self {
804 $($field: T::cos(v.$field),)+
805 }
806 )
807 }
808
809 fn atan2(y: Self, x: Self) -> Self {
810 Self {
811 $($field: T::atan2(y.$field, x.$field),)+
812 }
813 }
814
815 fn exp(v: Self) -> Self {
816 Self {
817 $($field: T::exp(v.$field),)+
818 }
819 }
820
821 fn exp2(v: Self) -> Self {
822 Self {
823 $($field: T::exp2(v.$field),)+
824 }
825 }
826
827 fn log2(v: Self) -> Self {
828 Self {
829 $($field: T::log2(v.$field),)+
830 }
831 }
832
833 fn log10(v: Self) -> Self {
834 Self {
835 $($field: T::log10(v.$field),)+
836 }
837 }
838
839 fn log(v: Self, base: T) -> Self {
840 Self {
841 $($field: T::log(v.$field, base),)+
842 }
843 }
844 }
845
846 impl<T> Index<usize> for $VecN<T> {
847 type Output = T;
848 fn index(&self, i: usize) -> &Self::Output {
849 match i {
850 $($field_index => &self.$field, )+
851 _ => panic!()
852 }
853 }
854 }
855
856 impl<T> IndexMut<usize> for $VecN<T> {
857 fn index_mut(&mut self, i: usize) -> &mut T {
858 match i {
859 $($field_index => &mut self.$field, )+
860 _ => panic!()
861 }
862 }
863 }
864
865 impl<T> Display for $VecN<T> where T: Display {
867 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
868 let mut output = String::from("[");
869 $(
870 output += &self.$field.to_string();
871 if $field_index < $len-1 {
872 output += &String::from(", ");
873 }
874 )+
875 output += "]";
876 write!(f, "{}", output)
877 }
878 }
879
880 impl<T> Default for $VecN<T> where T: Number {
882 fn default() -> Self {
883 Self::zero()
884 }
885 }
886
887 impl<T> Deref for $VecN<T> where T: Number {
892 type Target = [T];
893 fn deref(&self) -> &Self::Target {
894 self.as_slice()
895 }
896 }
897
898 impl<T> DerefMut for $VecN<T> where T: Number {
899 fn deref_mut(&mut self) -> &mut [T] {
900 self.as_mut_slice()
901 }
902 }
903
904 impl<T> Add<Self> for $VecN<T> where T: Number {
905 type Output = Self;
906 fn add(self, other: Self) -> Self {
907 Self {
908 $($field: self.$field + other.$field,)+
909 }
910 }
911 }
912
913 impl<T> Add<T> for $VecN<T> where T: Number {
914 type Output = Self;
915 fn add(self, other: T) -> Self {
916 Self {
917 $($field: self.$field + other,)+
918 }
919 }
920 }
921
922 impl<T> Add<&Self> for $VecN<T> where T: Number {
924 type Output = Self;
925 fn add(self, other: &Self) -> Self {
926 self.add(*other)
927 }
928 }
929
930 impl<T> Add<$VecN<T>> for &$VecN<T> where T: Number {
931 type Output = $VecN<T>;
932 fn add(self, other: $VecN<T>) -> $VecN<T> {
933 *self + other
934 }
935 }
936
937 impl<T> Add<Self> for &$VecN<T> where T: Number {
938 type Output = $VecN<T>;
939 fn add(self, other: Self) -> $VecN<T> {
940 *self + other
941 }
942 }
943
944 impl<T> AddAssign<Self> for $VecN<T> where T: Number {
945 fn add_assign(&mut self, other: Self) {
946 $(self.$field += other.$field;)+
947 }
948 }
949
950 impl<T> AddAssign<T> for $VecN<T> where T: Number {
951 fn add_assign(&mut self, other: T) {
952 $(self.$field += other;)+
953 }
954 }
955
956 impl<T> AddAssign<&Self> for $VecN<T> where T: Number {
958 fn add_assign(&mut self, other: &Self) {
959 $(self.$field += other.$field;)+
960 }
961 }
962
963 impl<T> Sub<Self> for $VecN<T> where T: Number {
964 type Output = Self;
965 fn sub(self, other: Self) -> Self {
966 Self {
967 $($field: self.$field - other.$field,)+
968 }
969 }
970 }
971
972 impl<T> Sub<T> for $VecN<T> where T: Number {
973 type Output = Self;
974 fn sub(self, other: T) -> Self {
975 Self {
976 $($field: self.$field - other,)+
977 }
978 }
979 }
980
981 impl<T> Sub<&Self> for $VecN<T> where T: Number {
983 type Output = Self;
984 fn sub(self, other: &Self) -> Self {
985 self.sub(*other)
986 }
987 }
988
989 impl<T> Sub<$VecN<T>> for &$VecN<T> where T: Number {
990 type Output = $VecN<T>;
991 fn sub(self, other: $VecN<T>) -> $VecN<T> {
992 *self - other
993 }
994 }
995
996 impl<T> Sub<Self> for &$VecN<T> where T: Number {
997 type Output = $VecN<T>;
998 fn sub(self, other: Self) -> $VecN<T> {
999 *self - other
1000 }
1001 }
1002
1003 impl<T> SubAssign<Self> for $VecN<T> where T: Number {
1004 fn sub_assign(&mut self, other: Self) {
1005 $(self.$field -= other.$field;)+
1006 }
1007 }
1008
1009 impl<T> SubAssign<T> for $VecN<T> where T: Number {
1010 fn sub_assign(&mut self, other: T) {
1011 $(self.$field -= other;)+
1012 }
1013 }
1014
1015 impl<T> SubAssign<&Self> for $VecN<T> where T: Number {
1017 fn sub_assign(&mut self, other: &Self) {
1018 $(self.$field -= other.$field;)+
1019 }
1020 }
1021
1022 impl<T> Mul<Self> for $VecN<T> where T: Number {
1023 type Output = Self;
1024 fn mul(self, other: Self) -> Self {
1025 Self {
1026 $($field: self.$field * other.$field,)+
1027 }
1028 }
1029 }
1030
1031 impl<T> Mul<T> for $VecN<T> where T: Number {
1032 type Output = Self;
1033 fn mul(self, other: T) -> Self {
1034 Self {
1035 $($field: self.$field * other,)+
1036 }
1037 }
1038 }
1039
1040 impl<T> Mul<&Self> for $VecN<T> where T: Number {
1042 type Output = Self;
1043 fn mul(self, other: &Self) -> Self {
1044 self.mul(*other)
1045 }
1046 }
1047
1048 impl<T> Mul<$VecN<T>> for &$VecN<T> where T: Number {
1049 type Output = $VecN<T>;
1050 fn mul(self, other: $VecN<T>) -> $VecN<T> {
1051 *self * other
1052 }
1053 }
1054
1055 impl<T> Mul<Self> for &$VecN<T> where T: Number {
1056 type Output = $VecN<T>;
1057 fn mul(self, other: Self) -> $VecN<T> {
1058 *self * other
1059 }
1060 }
1061
1062 impl<T> MulAssign<Self> for $VecN<T> where T: Number {
1063 fn mul_assign(&mut self, other: Self) {
1064 $(self.$field *= other.$field;)+
1065 }
1066 }
1067
1068 impl<T> MulAssign<T> for $VecN<T> where T: Number {
1069 fn mul_assign(&mut self, other: T) {
1070 $(self.$field *= other;)+
1071 }
1072 }
1073
1074 impl<T> MulAssign<&Self> for $VecN<T> where T: Number {
1076 fn mul_assign(&mut self, other: &Self) {
1077 $(self.$field *= other.$field;)+
1078 }
1079 }
1080
1081 impl<T> Div<Self> for $VecN<T> where T: Number {
1082 type Output = Self;
1083 fn div(self, other: Self) -> Self {
1084 Self {
1085 $($field: self.$field / other.$field,)+
1086 }
1087 }
1088 }
1089
1090 impl<T> Div<T> for $VecN<T> where T: Number {
1091 type Output = Self;
1092 fn div(self, other: T) -> Self {
1093 Self {
1094 $($field: self.$field / other,)+
1095 }
1096 }
1097 }
1098
1099 impl<T> Div<&Self> for $VecN<T> where T: Number {
1101 type Output = Self;
1102 fn div(self, other: &Self) -> Self {
1103 self.div(*other)
1104 }
1105 }
1106
1107 impl<T> Div<$VecN<T>> for &$VecN<T> where T: Number {
1108 type Output = $VecN<T>;
1109 fn div(self, other: $VecN<T>) -> $VecN<T> {
1110 *self / other
1111 }
1112 }
1113
1114 impl<T> Div<Self> for &$VecN<T> where T: Number {
1115 type Output = $VecN<T>;
1116 fn div(self, other: Self) -> $VecN<T> {
1117 *self / other
1118 }
1119 }
1120
1121 impl<T> DivAssign<Self> for $VecN<T> where T: Number {
1122 fn div_assign(&mut self, other: Self) {
1123 $(self.$field /= other.$field;)+
1124 }
1125 }
1126
1127 impl<T> DivAssign<T> for $VecN<T> where T: Number {
1128 fn div_assign(&mut self, other: T) {
1129 $(self.$field /= other;)+
1130 }
1131 }
1132
1133 impl<T> DivAssign<&Self> for $VecN<T> where T: Number {
1135 fn div_assign(&mut self, other: &Self) {
1136 $(self.$field /= other.$field;)+
1137 }
1138 }
1139
1140 impl<T> Rem<Self> for $VecN<T> where T: Number {
1141 type Output = Self;
1142 fn rem(self, other: Self) -> Self {
1143 Self {
1144 $($field: self.$field % other.$field,)+
1145 }
1146 }
1147 }
1148
1149 impl<T> Rem<T> for $VecN<T> where T: Number {
1150 type Output = Self;
1151 fn rem(self, other: T) -> Self {
1152 Self {
1153 $($field: self.$field % other,)+
1154 }
1155 }
1156 }
1157
1158 impl<T> RemAssign<Self> for $VecN<T> where T: Number {
1159 fn rem_assign(&mut self, other: Self) {
1160 $(self.$field %= other.$field;)+
1161 }
1162 }
1163
1164 impl<T> RemAssign<T> for $VecN<T> where T: Number {
1165 fn rem_assign(&mut self, other: T) {
1166 $(self.$field %= other;)+
1167 }
1168 }
1169
1170 impl<T> Neg for $VecN<T> where T: SignedNumber {
1171 type Output = Self;
1172 fn neg(self) -> Self::Output {
1173 Self {
1174 $($field: -self.$field,)+
1175 }
1176 }
1177 }
1178
1179 impl<T> Eq for $VecN<T> where T: Eq {}
1180 impl<T> PartialEq for $VecN<T> where T: PartialEq {
1181 fn eq(&self, other: &Self) -> bool {
1182 $(self.$field == other.$field &&)+
1183 true
1184 }
1185 }
1186 }
1187}
1188
1189macro_rules! vec_ctor {
1191 ($VecN:ident { $($field:ident),+ }, $ctor:ident, $splat:ident, $t:ident) => {
1192 pub fn $ctor($($field: $t,)+) -> $VecN<$t> {
1193 $VecN {
1194 $($field,)+
1195 }
1196 }
1197 pub fn $splat(v: $t) -> $VecN<$t> {
1198 $VecN {
1199 $($field: v,)+
1200 }
1201 }
1202 }
1203}
1204
1205macro_rules! vec_scalar_lhs {
1207 ($VecN:ident { $($field:ident),+ }, $t:ident) => {
1208 impl Add<$VecN<$t>> for $t {
1209 type Output = $VecN<$t>;
1210 fn add(self, other: $VecN<$t>) -> $VecN<$t> {
1211 $VecN {
1212 $($field: self + other.$field,)+
1213 }
1214 }
1215 }
1216
1217 impl Sub<$VecN<$t>> for $t {
1218 type Output = $VecN<$t>;
1219 fn sub(self, other: $VecN<$t>) -> $VecN<$t> {
1220 $VecN {
1221 $($field: self - other.$field,)+
1222 }
1223 }
1224 }
1225
1226 impl Mul<$VecN<$t>> for $t {
1227 type Output = $VecN<$t>;
1228 fn mul(self, other: $VecN<$t>) -> $VecN<$t> {
1229 $VecN {
1230 $($field: self * other.$field,)+
1231 }
1232 }
1233 }
1234
1235 impl Div<$VecN<$t>> for $t {
1236 type Output = $VecN<$t>;
1237 fn div(self, other: $VecN<$t>) -> $VecN<$t> {
1238 $VecN {
1239 $($field: self / other.$field,)+
1240 }
1241 }
1242 }
1243
1244 impl Rem<$VecN<$t>> for $t {
1245 type Output = $VecN<$t>;
1246 fn rem(self, other: $VecN<$t>) -> $VecN<$t> {
1247 $VecN {
1248 $($field: self % other.$field,)+
1249 }
1250 }
1251 }
1252 }
1253}
1254
1255impl<T> From<T> for Vec2<T> where T: Number {
1261 fn from(other: T) -> Vec2<T> {
1262 Vec2 {
1263 x: other,
1264 y: other,
1265 }
1266 }
1267}
1268
1269impl<T> From<(T, T)> for Vec2<T> where T: Number {
1271 fn from(other: (T, T)) -> Vec2<T> {
1272 Vec2 {
1273 x: other.0,
1274 y: other.1,
1275 }
1276 }
1277}
1278
1279impl<T> From<Vec3<T>> for Vec2<T> where T: Number {
1281 fn from(other: Vec3<T>) -> Vec2<T> {
1282 Vec2 {
1283 x: other.x,
1284 y: other.y,
1285 }
1286 }
1287}
1288
1289impl<T> From<Vec4<T>> for Vec2<T> where T: Number {
1291 fn from(other: Vec4<T>) -> Vec2<T> {
1292 Vec2 {
1293 x: other.x,
1294 y: other.y,
1295 }
1296 }
1297}
1298
1299impl<T> From<T> for Vec3<T> where T: Number {
1301 fn from(other: T) -> Vec3<T> {
1302 Vec3 {
1303 x: other,
1304 y: other,
1305 z: other
1306 }
1307 }
1308}
1309
1310impl<T> From<Vec2<T>> for Vec3<T> where T: Number {
1312 fn from(other: Vec2<T>) -> Vec3<T> {
1313 Vec3 {
1314 x: other.x,
1315 y: other.y,
1316 z: T::zero()
1317 }
1318 }
1319}
1320
1321impl<T> From<(T, T, T)> for Vec3<T> where T: Number {
1323 fn from(other: (T, T, T)) -> Vec3<T> {
1324 Vec3 {
1325 x: other.0,
1326 y: other.1,
1327 z: other.2
1328 }
1329 }
1330}
1331
1332impl<T> From<(Vec2<T>, T)> for Vec3<T> where T: Number {
1334 fn from(other: (Vec2<T>, T)) -> Vec3<T> {
1335 Vec3 {
1336 x: other.0.x,
1337 y: other.0.y,
1338 z: other.1
1339 }
1340 }
1341}
1342
1343impl<T> From<Vec4<T>> for Vec3<T> where T: Number {
1345 fn from(other: Vec4<T>) -> Vec3<T> {
1346 Vec3 {
1347 x: other.x,
1348 y: other.y,
1349 z: other.z,
1350 }
1351 }
1352}
1353
1354impl<T> From<T> for Vec4<T> where T: Number {
1356 fn from(other: T) -> Vec4<T> {
1357 Vec4 {
1358 x: other,
1359 y: other,
1360 z: other,
1361 w: other
1362 }
1363 }
1364}
1365
1366impl<T> From<Vec2<T>> for Vec4<T> where T: Number {
1368 fn from(other: Vec2<T>) -> Vec4<T> {
1369 Vec4 {
1370 x: other.x,
1371 y: other.y,
1372 z: T::zero(),
1373 w: T::zero()
1374 }
1375 }
1376}
1377
1378impl<T> From<Vec3<T>> for Vec4<T> where T: Number {
1380 fn from(other: Vec3<T>) -> Vec4<T> {
1381 Vec4 {
1382 x: other.x,
1383 y: other.y,
1384 z: other.z,
1385 w: T::zero()
1386 }
1387 }
1388}
1389
1390impl<T> From<(Vec2<T>, T, T)> for Vec4<T> where T: Number {
1392 fn from(other: (Vec2<T>, T, T)) -> Vec4<T> {
1393 Vec4 {
1394 x: other.0.x,
1395 y: other.0.y,
1396 z: other.1,
1397 w: other.2
1398 }
1399 }
1400}
1401
1402impl<T> From<(Vec2<T>, Vec2<T>)> for Vec4<T> where T: Number {
1404 fn from(other: (Vec2<T>, Vec2<T>)) -> Vec4<T> {
1405 Vec4 {
1406 x: other.0.x,
1407 y: other.0.y,
1408 z: other.1.x,
1409 w: other.1.y
1410 }
1411 }
1412}
1413
1414impl<T> From<(Vec3<T>, T)> for Vec4<T> where T: Number {
1416 fn from(other: (Vec3<T>, T)) -> Vec4<T> {
1417 Vec4 {
1418 x: other.0.x,
1419 y: other.0.y,
1420 z: other.0.z,
1421 w: other.1
1422 }
1423 }
1424}
1425
1426impl<T> From<(T, T, T, T)> for Vec4<T> where T: Number {
1428 fn from(other: (T, T, T, T)) -> Vec4<T> {
1429 Vec4 {
1430 x: other.0,
1431 y: other.1,
1432 z: other.2,
1433 w: other.3
1434 }
1435 }
1436}
1437
1438macro_rules! vec_cast {
1439 ($VecN:ident { $($field:ident),+ }, $t:ident, $u:ident) => {
1440 impl From<$VecN<$u>> for $VecN<$t> {
1441 fn from(other: $VecN<$u>) -> $VecN<$t> {
1442 $VecN {
1443 $($field: other.$field as $t,)+
1444 }
1445 }
1446 }
1447
1448 impl From<$VecN<$t>> for $VecN<$u> {
1449 fn from(other: $VecN<$t>) -> $VecN<$u> {
1450 $VecN {
1451 $($field: other.$field as $u,)+
1452 }
1453 }
1454 }
1455 }
1456}
1457
1458vec_impl!(Vec2 { x, 0, y, 1 }, 2, v2);
1463vec_impl!(Vec3 { x, 0, y, 1, z, 2 }, 3, v3);
1464vec_impl!(Vec4 { x, 0, y, 1, z, 2, w, 3 }, 4, v4);
1465
1466#[cfg(feature = "lhs_scalar_vec_ops")]
1467vec_scalar_lhs!(Vec2 { x, y }, f32);
1468
1469#[cfg(feature = "lhs_scalar_vec_ops")]
1470vec_scalar_lhs!(Vec3 { x, y, z }, f32);
1471
1472#[cfg(feature = "lhs_scalar_vec_ops")]
1473vec_scalar_lhs!(Vec4 { x, y, z, w }, f32);
1474
1475#[cfg(feature = "lhs_scalar_vec_ops")]
1476vec_scalar_lhs!(Vec2 { x, y }, f64);
1477
1478#[cfg(feature = "lhs_scalar_vec_ops")]
1479vec_scalar_lhs!(Vec3 { x, y, z }, f64);
1480
1481#[cfg(feature = "lhs_scalar_vec_ops")]
1482vec_scalar_lhs!(Vec4 { x, y, z, w }, f64);
1483
1484#[cfg(feature = "lhs_scalar_vec_ops")]
1485vec_scalar_lhs!(Vec2 { x, y }, i32);
1486
1487#[cfg(feature = "lhs_scalar_vec_ops")]
1488vec_scalar_lhs!(Vec3 { x, y, z }, i32);
1489
1490#[cfg(feature = "lhs_scalar_vec_ops")]
1491vec_scalar_lhs!(Vec4 { x, y, z, w }, i32);
1492
1493#[cfg(feature = "lhs_scalar_vec_ops")]
1494vec_scalar_lhs!(Vec2 { x, y }, u32);
1495
1496#[cfg(feature = "lhs_scalar_vec_ops")]
1497vec_scalar_lhs!(Vec3 { x, y, z }, u32);
1498
1499#[cfg(feature = "lhs_scalar_vec_ops")]
1500vec_scalar_lhs!(Vec4 { x, y, z, w }, u32);
1501
1502#[cfg(feature = "short_hand_constructors")]
1503vec_ctor!(Vec2 { x, y }, vec2f, splat2f, f32);
1504
1505#[cfg(feature = "short_hand_constructors")]
1506vec_ctor!(Vec3 { x, y, z }, vec3f, splat3f, f32);
1507
1508#[cfg(feature = "short_hand_constructors")]
1509vec_ctor!(Vec4 { x, y, z, w }, vec4f, splat4f, f32);
1510
1511#[cfg(feature = "short_hand_constructors")]
1512vec_ctor!(Vec2 { x, y }, vec2d, splat2d, f64);
1513
1514#[cfg(feature = "short_hand_constructors")]
1515vec_ctor!(Vec3 { x, y, z }, vec3d, splat3d, f64);
1516
1517#[cfg(feature = "short_hand_constructors")]
1518vec_ctor!(Vec4 { x, y, z, w }, vec4d, splat4d, f64);
1519
1520#[cfg(feature = "short_hand_constructors")]
1521vec_ctor!(Vec2 { x, y }, vec2i, splat2i, i32);
1522
1523#[cfg(feature = "short_hand_constructors")]
1524vec_ctor!(Vec3 { x, y, z }, vec3i, splat3i, i32);
1525
1526#[cfg(feature = "short_hand_constructors")]
1527vec_ctor!(Vec4 { x, y, z, w }, vec4i, splat4i, i32);
1528
1529#[cfg(feature = "short_hand_constructors")]
1530vec_ctor!(Vec2 { x, y }, vec2u, splat2u, u32);
1531
1532#[cfg(feature = "short_hand_constructors")]
1533vec_ctor!(Vec3 { x, y, z }, vec3u, splat3u, u32);
1534
1535#[cfg(feature = "short_hand_constructors")]
1536vec_ctor!(Vec4 { x, y, z, w }, vec4u, splat4u, u32);
1537
1538#[cfg(feature = "casts")]
1539vec_cast!(Vec2 { x, y }, f64, i32);
1540
1541#[cfg(feature = "casts")]
1542vec_cast!(Vec2 { x, y }, f64, u32);
1543
1544#[cfg(feature = "casts")]
1545vec_cast!(Vec2 { x, y }, f32, f64);
1546
1547#[cfg(feature = "casts")]
1548vec_cast!(Vec2 { x, y }, f32, i32);
1549
1550#[cfg(feature = "casts")]
1551vec_cast!(Vec2 { x, y }, f32, u32);
1552
1553#[cfg(feature = "casts")]
1554vec_cast!(Vec2 { x, y }, i32, u32);
1555
1556#[cfg(feature = "casts")]
1557vec_cast!(Vec3 {x, y, z}, f64, i32);
1558
1559#[cfg(feature = "casts")]
1560vec_cast!(Vec3 {x, y, z}, f64, u32);
1561
1562#[cfg(feature = "casts")]
1563vec_cast!(Vec3 {x, y, z}, f32, f64);
1564
1565#[cfg(feature = "casts")]
1566vec_cast!(Vec3 {x, y, z}, f32, i32);
1567
1568#[cfg(feature = "casts")]
1569vec_cast!(Vec3 {x, y, z}, f32, u32);
1570
1571#[cfg(feature = "casts")]
1572vec_cast!(Vec3 {x, y, z}, i32, u32);
1573
1574#[cfg(feature = "casts")]
1575vec_cast!(Vec4 {x, y, z, w}, f64, i32);
1576
1577#[cfg(feature = "casts")]
1578vec_cast!(Vec4 {x, y, z, w}, f64, u32);
1579
1580#[cfg(feature = "casts")]
1581vec_cast!(Vec4 {x, y, z, w}, f32, f64);
1582
1583#[cfg(feature = "casts")]
1584vec_cast!(Vec4 {x, y, z, w}, f32, i32);
1585
1586#[cfg(feature = "casts")]
1587vec_cast!(Vec4 {x, y, z, w}, f32, u32);
1588
1589#[cfg(feature = "casts")]
1590vec_cast!(Vec4 {x, y, z, w}, i32, u32);