-
Notifications
You must be signed in to change notification settings - Fork 3
/
vector.rs
1363 lines (1132 loc) · 45.6 KB
/
vector.rs
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014 Felix S. Klock II. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use super::typedefs::{vec1,vec2,vec3,vec4};
use super::typedefs::{ivec1,ivec2,ivec3,ivec4};
use src::scalar::{S,SAddRHS,SSubRHS,SMulRHS,SDivRHS};
use src::operators::{EpsilonEq};
use src::operators::{Increment,Decrement};
use src::operators::{AddAssign,SubAssign,MulAssign,DivAssign};
pub trait DotProduct<T> {
fn dot(&self, &Self) -> T;
}
pub fn dot<T,V:DotProduct<T>>(x: V, y: V) -> T { x.dot(&y) }
pub trait Summable<T> {
fn sum(&self) -> T;
}
macro_rules! swizzle_def {
( $Name:ident : $SuperTrait:ident
{ $(fn $core_name:ident)* }
{ $(($xy:ident $x2:ident $y2:ident))* }
{ $(($xyz:ident $x3:ident $y3:ident $z3:ident))* }
{ $(($xyzw:ident $x4:ident $y4:ident $z4:ident $w4:ident))* } )
=>
{
pub trait $Name<T> : $SuperTrait<T>
{
$(fn $core_name(&self) -> T;)*
$(fn $xy(&self) -> TVec2<T> {
TVec2 { x: self.$x2(), y: self.$y2() } })*
$(fn $xyz(&self) -> TVec3<T> {
TVec3 { x: self.$x3(), y: self.$y3(), z: self.$z3() } })*
$(fn $xyzw(&self) -> TVec4<T> {
TVec4 { x: self.$x4(), y: self.$y4(), z: self.$z4(), w: self.$w4() } })*
}
}
}
// (trivial base case to ease macro definition.)
trait Swizzle0<T> { }
impl<T,X> Swizzle0<T> for X { }
swizzle_def!{
Swizzle1 : Swizzle0 { fn x }
{ (xx x x) }
{ (xxx x x x) }
{ (xxxx x x x x) }
}
swizzle_def!{
Swizzle2 : Swizzle1 { fn y }
{ (xy x y)
(yx y x) (yy y y)
}
{ (xxy x x y)
(xyx x y x) (xyy x y y)
(yxx y x x) (yxy y x y)
(yyx y y x) (yyy y y y)
}
{ (xxxy x x x y)
(xxyx x x y x) (xxyy x x y y)
(xyxx x y x x) (xyxy x y x y)
(xyyx x y y x) (xyyy x y y y)
(yxxy y x x y)
(yxyx y x y x) (yxyy y x y y)
(yyxx y y x x) (yyxy y y x y)
(yyyx y y y x) (yyyy y y y y)
}
}
swizzle_def!{
Swizzle3 : Swizzle2 { fn z }
{ (xz x z)
(yz y z)
(zx z x) (zy z y) (zz z z)
}
{ (xxz x x z)
(xyz x y z)
(xzx x z x) (xzy x z y) (xzz x z z)
(yxz y x z)
(yyz y y z)
(yzx y z x) (yzy y z y) (yzz y z z)
(zxx z x x) (zxy z x y) (zxz z x z)
(zyx z y x) (zyy z y y) (zyz z y z)
(zzx z z x) (zzy z z y) (zzz z z z)
}
{
(xxxz x x x z)
(xxyz x x y z)
(xxzx x x z x) (xxzy x x z y) (xxzz x x z z)
(xyxz x y x z)
(xyyz x y y z)
(xyzx x y z x) (xyzy x y z y) (xyzz x y z z)
(xzxx x z x x) (xzxy x z x y) (xzxz x z x z)
(xzyx x z y x) (xzyy x z y y) (xzyz x z y z)
(xzzx x z z x) (xzzy x z z y) (xzzz x z z z)
(yxxz y x x z)
(yxyz y x y z)
(yxzx y x z x) (yxzy y x z y) (yxzz y x z z)
(yyxz y y x z)
(yyyz y y y z)
(yyzx y y z x) (yyzy y y z y) (yyzz y y z z)
(yzxx y z x x) (yzxy y z x y) (yzxz y z x z)
(yzyx y z y x) (yzyy y z y y) (yzyz y z y z)
(yzzx y z z x) (yzzy y z z y) (yzzz y z z z)
(zxxx z x x x) (zxxy z x x y) (zxxz z x x z)
(zxyx z x y x) (zxyy z x y y) (zxyz z x y z)
(zxzx z x z x) (zxzy z x z y) (zxzz z x z z)
(zyxx z y x x) (zyxy z y x y) (zyxz z y x z)
(zyyx z y y x) (zyyy z y y y) (zyyz z y y z)
(zyzx z y z x) (zyzy z y z y) (zyzz z y z z)
(zzxx z z x x) (zzxy z z x y) (zzxz z z x z)
(zzyx z z y x) (zzyy z z y y) (zzyz z z y z)
(zzzx z z z x) (zzzy z z z y) (zzzz z z z z)
}
}
swizzle_def!{
Swizzle4 : Swizzle3 { fn w }
{ (xw x w)
(yw y w)
(zw z w)
(wx w x) (wy w y) (wz w z) (ww w w)
}
{ (xxw x x w)
(xyw x y w)
(xzw x z w)
(xwx x w x) (xwy x w y) (xwz x w z) (xww x w w)
(yxw y x w)
(yyw y y w)
(yzw y z w)
(ywx y w x) (ywy y w y) (ywz y w z) (yww y w w)
(zxw z x w)
(zyw z y w)
(zzw z z w)
(zwx z w x) (zwy z w y) (zwz z w z) (zww z w w)
(wxx w x x) (wxy w x y) (wxz w x z) (wxw w x w)
(wyx w y x) (wyy w y y) (wyz w y z) (wyw w y w)
(wzx w z x) (wzy w z y) (wzz w z z) (wzw w z w)
(wwx w w x) (wwy w w y) (wwz w w z) (www w w w)
}
{ (xxxw x x x w)
(xxyw x x y w)
(xxzw x x z w)
(xxwx x x w x) (xxwy x x w y) (xxwz x x w z) (xxww x x w w)
(xyxw x y x w)
(xyyw x y y w)
(xyzw x y z w)
(xywx x y w x) (xywy x y w y) (xywz x y w z) (xyww x y w w)
(xzxw x z x w)
(xzyw x z y w)
(xzzw x z z w)
(xzwx x z w x) (xzwy x z w y) (xzwz x z w z) (xzww x z w w)
(xwxx x w x x) (xwxy x w x y) (xwxz x w x z) (xwxw x w x w)
(xwyx x w y x) (xwyy x w y y) (xwyz x w y z) (xwyw x w y w)
(xwzx x w z x) (xwzy x w z y) (xwzz x w z z) (xwzw x w z w)
(xwwx x w w x) (xwwy x w w y) (xwwz x w w z) (xwww x w w w)
(yxxw y x x w)
(yxyw y x y w)
(yxzw y x z w)
(yxwx y x w x) (yxwy y x w y) (yxwz y x w z) (yxww y x w w)
(yyxw y y x w)
(yyyw y y y w)
(yyzw y y z w)
(yywx y y w x) (yywy y y w y) (yywz y y w z) (yyww y y w w)
(yzxw y z x w)
(yzyw y z y w)
(yzzw y z z w)
(yzwx y z w x) (yzwy y z w y) (yzwz y z w z) (yzww y z w w)
(ywxx y w x x) (ywxy y w x y) (ywxz y w x z) (ywxw y w x w)
(ywyx y w y x) (ywyy y w y y) (ywyz y w y z) (ywyw y w y w)
(ywzx y w z x) (ywzy y w z y) (ywzz y w z z) (ywzw y w z w)
(ywwx y w w x) (ywwy y w w y) (ywwz y w w z) (ywww y w w w)
(zxxw z x x w)
(zxyw z x y w)
(zxzw z x z w)
(zxwx z x w x) (zxwy z x w y) (zxwz z x w z) (zxww z x w w)
(zyxw z y x w)
(zyyw z y y w)
(zyzw z y z w)
(zywx z y w x) (zywy z y w y) (zywz z y w z) (zyww z y w w)
(zzxw z z x w)
(zzyw z z y w)
(zzzw z z z w)
(zzwx z z w x) (zzwy z z w y) (zzwz z z w z) (zzww z z w w)
(zwxx z w x x) (zwxy z w x y) (zwxz z w x z) (zwxw z w x w)
(zwyx z w y x) (zwyy z w y y) (zwyz z w y z) (zwyw z w y w)
(zwzx z w z x) (zwzy z w z y) (zwzz z w z z) (zwzw z w z w)
(zwwx z w w x) (zwwy z w w y) (zwwz z w w z) (zwww z w w w)
(wxxx w x x x) (wxxy w x x y) (wxxz w x x z) (wxxw w x x w)
(wxyx w x y x) (wxyy w x y y) (wxyz w x y z) (wxyw w x y w)
(wxzx w x z x) (wxzy w x z y) (wxzz w x z z) (wxzw w x z w)
(wxwx w x w x) (wxwy w x w y) (wxwz w x w z) (wxww w x w w)
(wyxx w y x x) (wyxy w y x y) (wyxz w y x z) (wyxw w y x w)
(wyyx w y y x) (wyyy w y y y) (wyyz w y y z) (wyyw w y y w)
(wyzx w y z x) (wyzy w y z y) (wyzz w y z z) (wyzw w y z w)
(wywx w y w x) (wywy w y w y) (wywz w y w z) (wyww w y w w)
(wzxx w z x x) (wzxy w z x y) (wzxz w z x z) (wzxw w z x w)
(wzyx w z y x) (wzyy w z y y) (wzyz w z y z) (wzyw w z y w)
(wzzx w z z x) (wzzy w z z y) (wzzz w z z z) (wzzw w z z w)
(wzwx w z w x) (wzwy w z w y) (wzwz w z w z) (wzww w z w w)
(wwxx w w x x) (wwxy w w x y) (wwxz w w x z) (wwxw w w x w)
(wwyx w w y x) (wwyy w w y y) (wwyz w w y z) (wwyw w w y w)
(wwzx w w z x) (wwzy w w z y) (wwzz w w z z) (wwzw w w z w)
(wwwx w w w x) (wwwy w w w y) (wwwz w w w z) (wwww w w w w)
}
}
#[deriving(Eq, Show, Default)]
pub struct TVec1<T> { pub x: T, }
#[deriving(Eq, Show, Default)]
pub struct TVec2<T> { pub x: T, pub y: T, }
#[deriving(Eq, Show, Default)]
pub struct TVec3<T> { pub x: T, pub y: T, pub z: T, }
#[deriving(Eq, Show, Default)]
pub struct TVec4<T> { pub x: T, pub y: T, pub z: T, pub w: T, }
impl<T:Clone> Swizzle1<T> for TVec2<T> { fn x(&self) -> T { self.x.clone() } }
impl<T:Clone> Swizzle2<T> for TVec2<T> { fn y(&self) -> T { self.y.clone() } }
impl<T:Clone> Swizzle1<T> for TVec3<T> { fn x(&self) -> T { self.x.clone() } }
impl<T:Clone> Swizzle2<T> for TVec3<T> { fn y(&self) -> T { self.y.clone() } }
impl<T:Clone> Swizzle3<T> for TVec3<T> { fn z(&self) -> T { self.z.clone() } }
impl<T:Clone> Swizzle1<T> for TVec4<T> { fn x(&self) -> T { self.x.clone() } }
impl<T:Clone> Swizzle2<T> for TVec4<T> { fn y(&self) -> T { self.y.clone() } }
impl<T:Clone> Swizzle3<T> for TVec4<T> { fn z(&self) -> T { self.z.clone() } }
impl<T:Clone> Swizzle4<T> for TVec4<T> { fn w(&self) -> T { self.w.clone() } }
pub trait Vec1Args { fn make(self) -> vec1; }
pub fn vec1<Args:Vec1Args>(args: Args) -> vec1 { args.make() }
impl Vec1Args for f32 { fn make(self) -> vec1 { TVec1 { x: self } } }
pub trait IVec1Args { fn make(self) -> ivec1; }
pub fn ivec1<Args:IVec1Args>(args: Args) -> ivec1 { args.make() }
impl IVec1Args for i32 { fn make(self) -> ivec1 { TVec1 { x: self } } }
impl IVec1Args for TVec1<i32> { fn make(self) -> ivec1 { TVec1 { x: self.x } } }
pub trait IVec2Args { fn make(self) -> ivec2; }
pub fn ivec2<Args:IVec2Args>(args: Args) -> ivec2 { args.make() }
impl IVec2Args for i32 { fn make(self) -> ivec2 { TVec2 { x: self, y: self } } }
pub trait IVec3Args { fn make(self) -> ivec3; }
pub fn ivec3<Args:IVec3Args>(args: Args) -> ivec3 { args.make() }
impl IVec3Args for i32 { fn make(self) -> ivec3 { TVec3 { x: self, y: self, z: self } } }
impl IVec3Args for TVec3<i32> { fn make(self) -> ivec3 { TVec3 { x: self.x, y: self.y, z: self.z } } }
pub trait IVec4Args { fn make(self) -> ivec4; }
pub fn ivec4<Args:IVec4Args>(args: Args) -> ivec4 { args.make() }
impl IVec4Args for i32 { fn make(self) -> ivec4 { TVec4 { x: self, y: self, z: self, w: self } } }
impl IVec4Args for TVec4<i32> { fn make(self) -> ivec4 { TVec4 { x: self.x, y: self.y, z: self.z, w: self.w } } }
macro_rules! impl_IncrementDecrement_for {
($TVec:ident $($x:ident),*) => {
impl<T:Num + Clone> Increment for $TVec<T> {
fn postincrement(&mut self) -> $TVec<T> {
use std::num::One;
let ret = $TVec{ $( $x: self.$x.clone() ),* };
$( self.$x = self.$x + One::one(); )*
ret
}
fn preincrement<'a>(&'a mut self) -> &'a mut $TVec<T> {
use std::num::One;
$( self.$x = self.$x + One::one(); )*
self
}
}
impl<T:Num + Clone> Decrement for $TVec<T> {
fn postdecrement(&mut self) -> $TVec<T> {
use std::num::One;
let ret = $TVec{ $( $x: self.$x.clone() ),* };
$( self.$x = self.$x - One::one(); )*
ret
}
fn predecrement<'a>(&'a mut self) -> &'a mut $TVec<T> {
use std::num::One;
$( self.$x = self.$x - One::one(); )*
self
}
}
}
}
impl_IncrementDecrement_for!{ TVec1 x }
impl_IncrementDecrement_for!{ TVec2 x, y }
impl_IncrementDecrement_for!{ TVec3 x, y, z }
impl_IncrementDecrement_for!{ TVec4 x, y, z, w }
macro_rules! impl_Neg_for {
($TVec:ident $($x:ident),*) => {
impl<T:Neg<T>> Neg<$TVec<T>> for $TVec<T> {
fn neg(&self) -> $TVec<T> { $TVec{ $( $x: -self.$x ),* } }
}
}
}
impl_Neg_for!{ TVec1 x }
impl_Neg_for!{ TVec2 x, y }
impl_Neg_for!{ TVec3 x, y, z }
impl_Neg_for!{ TVec4 x, y, z, w }
impl<T:Num+Clone> Summable<T> for TVec1<T> {
fn sum(&self) -> T { self.x.clone() }
}
impl<T:Num+Clone> Summable<T> for TVec2<T> {
fn sum(&self) -> T { self.x + self.y }
}
impl<T:Num+Clone> Summable<T> for TVec3<T> {
fn sum(&self) -> T { self.x + self.y + self.z }
}
impl<T:Num+Clone> Summable<T> for TVec4<T> {
fn sum(&self) -> T { self.x + self.y + self.z + self.w }
}
macro_rules! impl_DotProduct_for {
($TVec:ident $($x:ident),*) => {
impl<T:Num> DotProduct<T> for $TVec<T> {
fn dot(&self, rhs: &$TVec<T>) -> T {
use std::num::Zero;
let mut sum : T = Zero::zero();
{ $( sum = sum + self.$x * rhs.$x; )* }
sum
}
}
}
}
impl_DotProduct_for!{ TVec1 x }
impl_DotProduct_for!{ TVec2 x, y }
impl_DotProduct_for!{ TVec3 x, y, z }
impl_DotProduct_for!{ TVec4 x, y, z, w }
pub trait Vec2Args { fn make(self) -> vec2; }
pub fn vec2<Args:Vec2Args>(args: Args) -> vec2 { args.make() }
macro_rules! impl_Vec2Args_for {
($a:ident,$b:ident) => {
impl Vec2Args for ($a,$b) { fn make(self) -> vec2 { let (x,y) = self; TVec2 { x: x as f32, y: y as f32 } } }
}
;
($a:ident 2 $S:ident) => {
impl Vec2Args for $a { fn make(self) -> vec2 { let $S{x:x,y:y,..} = self; TVec2 { x: x as f32, y: y as f32 } } }
}
;
($a:ident copy) => {
impl Vec2Args for $a { fn make(self) -> vec2 { TVec2 { x: self as f32, y: self as f32 } } }
}
}
impl_Vec2Args_for!(f32 copy)
impl_Vec2Args_for!(int,int)
impl_Vec2Args_for!(int,f32)
impl_Vec2Args_for!(f32,int)
impl_Vec2Args_for!(f32,f32)
impl_Vec2Args_for!(vec2 2 TVec2)
impl_Vec2Args_for!(vec3 2 TVec3)
impl_Vec2Args_for!(vec4 2 TVec4)
double_dispatch_T!{Add for TVec2 add via TVec2AddRHS rev_add}
double_dispatch_T!{AddAssign for mut TVec2 add_assign via TVec2AddAssignRHS add_into }
double_dispatch_T!{Sub for TVec2 sub via TVec2SubRHS rev_sub}
double_dispatch_T!{SubAssign for mut TVec2 sub_assign via TVec2SubAssignRHS rsb_into }
double_dispatch_T!{Mul for TVec2 mul via TVec2MulRHS rev_mul}
double_dispatch_T!{MulAssign for mut TVec2 mul_assign via TVec2MulAssignRHS mul_into }
double_dispatch_T!{Div for TVec2 div via TVec2DivRHS rev_div}
double_dispatch_T!{DivAssign for mut TVec2 div_assign via TVec2DivAssignRHS div_into }
double_dispatch_T!{Add for TVec3 add via TVec3AddRHS rev_add}
double_dispatch_T!{Sub for TVec3 sub via TVec3SubRHS rev_sub}
double_dispatch_T!{Mul for TVec3 mul via TVec3MulRHS rev_mul}
double_dispatch_T!{Div for TVec3 div via TVec3DivRHS rev_div}
double_dispatch_T!{AddAssign for mut TVec3 add_assign via TVec3AddAssignRHS add_into }
double_dispatch_T!{SubAssign for mut TVec3 sub_assign via TVec3SubAssignRHS rsb_into }
double_dispatch_T!{MulAssign for mut TVec3 mul_assign via TVec3MulAssignRHS mul_into }
double_dispatch_T!{DivAssign for mut TVec3 div_assign via TVec3DivAssignRHS div_into }
double_dispatch_T!{Add for TVec4 add via TVec4AddRHS rev_add}
double_dispatch_T!{Sub for TVec4 sub via TVec4SubRHS rev_sub}
double_dispatch_T!{Mul for TVec4 mul via TVec4MulRHS rev_mul}
double_dispatch_T!{Div for TVec4 div via TVec4DivRHS rev_div}
double_dispatch_T!{AddAssign for mut TVec4 add_assign via TVec4AddAssignRHS add_into }
double_dispatch_T!{SubAssign for mut TVec4 sub_assign via TVec4SubAssignRHS rsb_into }
double_dispatch_T!{MulAssign for mut TVec4 mul_assign via TVec4MulAssignRHS mul_into }
double_dispatch_T!{DivAssign for mut TVec4 div_assign via TVec4DivAssignRHS div_into }
macro_rules! double_dispatch_usual_impls_2 {
( $ft:ty $op:ident
$V_RHS_trait:ident { $v_rev_method:ident }
$S_RHS_trait:ident { $s_rev_method:ident }
$ASSIGN_RHS_trait:ident { $op_into_method:ident }
) => {
impl $V_RHS_trait<$ft,TVec2<$ft>> for $ft {
fn $v_rev_method(&self, lhs: &TVec2<$ft>) -> TVec2<$ft> {
TVec2 { x: lhs.x.$op(self), y: lhs.y.$op(self) }
}
}
impl<FT:Num> $S_RHS_trait<FT,TVec2<FT>> for TVec2<FT> {
fn $s_rev_method(&self, lhs: &S<FT>) -> TVec2<FT> {
let &S(ref lhs) = lhs;
TVec2 { x: lhs.$op(&self.x), y: lhs.$op(&self.y) }
}
}
impl<FT:Num> $V_RHS_trait<FT,TVec2<FT>> for TVec2<FT> {
fn $v_rev_method(&self, lhs: &TVec2<FT>) -> TVec2<FT> {
TVec2 { x: lhs.x.$op(&self.x), y: lhs.y.$op(&self.y) }
}
}
impl $ASSIGN_RHS_trait<$ft> for $ft {
fn $op_into_method(&self, recv: &mut TVec2<$ft>) {
recv.x = recv.x.$op(self);
recv.y = recv.y.$op(self);
}
}
impl<FT:Num> $ASSIGN_RHS_trait<FT> for TVec2<FT> {
fn $op_into_method(&self, recv: &mut TVec2<FT>) {
recv.x = recv.x.$op(&self.x);
recv.y = recv.y.$op(&self.y);
}
}
}
}
double_dispatch_usual_impls_2! { f32 add
TVec2AddRHS { rev_add } SAddRHS { rev_add }
TVec2AddAssignRHS { add_into } }
double_dispatch_usual_impls_2! { f32 sub
TVec2SubRHS { rev_sub } SSubRHS { rev_sub }
TVec2SubAssignRHS { rsb_into } }
double_dispatch_usual_impls_2! { f32 mul
TVec2MulRHS { rev_mul } SMulRHS { rev_mul }
TVec2MulAssignRHS { mul_into } }
double_dispatch_usual_impls_2! { f32 div
TVec2DivRHS { rev_div } SDivRHS { rev_div }
TVec2DivAssignRHS { div_into } }
macro_rules! double_dispatch_usual_impls_3 {
( $ft:ty $op:ident
$V_RHS_trait:ident { $v_rev_method:ident }
$S_RHS_trait:ident { $s_rev_method:ident }
$ASSIGN_RHS_trait:ident { $op_into_method:ident }
) => {
impl $V_RHS_trait<$ft,TVec3<$ft>> for $ft {
fn $v_rev_method(&self, lhs: &TVec3<$ft>) -> TVec3<$ft> {
TVec3 { x: lhs.x.$op(self), y: lhs.y.$op(self), z: lhs.z.$op(self) }
}
}
impl<FT:Num> $S_RHS_trait<FT,TVec3<FT>> for TVec3<FT> {
fn $s_rev_method(&self, lhs: &S<FT>) -> TVec3<FT> {
let &S(ref lhs) = lhs;
TVec3 { x: lhs.$op(&self.x), y: lhs.$op(&self.y), z: lhs.$op(&self.z) }
}
}
impl<FT:Num> $V_RHS_trait<FT,TVec3<FT>> for TVec3<FT> {
fn $v_rev_method(&self, lhs: &TVec3<FT>) -> TVec3<FT> {
TVec3 { x: lhs.x.$op(&self.x), y: lhs.y.$op(&self.y), z: lhs.z.$op(&self.z) }
}
}
impl $ASSIGN_RHS_trait<$ft> for $ft {
fn $op_into_method(&self, recv: &mut TVec3<$ft>) {
recv.x = recv.x.$op(self);
recv.y = recv.y.$op(self);
recv.z = recv.z.$op(self);
}
}
impl $ASSIGN_RHS_trait<$ft> for TVec3<$ft> {
fn $op_into_method(&self, recv: &mut TVec3<$ft>) {
recv.x = recv.x.$op(&self.x);
recv.y = recv.y.$op(&self.y);
recv.z = recv.z.$op(&self.z);
}
}
}
}
double_dispatch_usual_impls_3! { f32 add
TVec3AddRHS { rev_add } SAddRHS { rev_add }
TVec3AddAssignRHS { add_into } }
double_dispatch_usual_impls_3! { f32 sub
TVec3SubRHS { rev_sub } SSubRHS { rev_sub }
TVec3SubAssignRHS { rsb_into } }
double_dispatch_usual_impls_3! { f32 mul
TVec3MulRHS { rev_mul } SMulRHS { rev_mul }
TVec3MulAssignRHS { mul_into } }
double_dispatch_usual_impls_3! { f32 div
TVec3DivRHS { rev_div } SDivRHS { rev_div }
TVec3DivAssignRHS { div_into } }
macro_rules! double_dispatch_usual_impls_4 {
( $ft:ty $op:ident
$V_RHS_trait:ident { $v_rev_method:ident }
$S_RHS_trait:ident { $s_rev_method:ident }
$ASSIGN_RHS_trait:ident { $op_into_method:ident }
) => {
impl $V_RHS_trait<$ft,TVec4<$ft>> for $ft {
fn $v_rev_method(&self, lhs: &TVec4<$ft>) -> TVec4<$ft> {
TVec4 { x: lhs.x.$op(self), y: lhs.y.$op(self), z: lhs.z.$op(self), w: lhs.w.$op(self) }
}
}
impl<FT:Num> $S_RHS_trait<FT,TVec4<FT>> for TVec4<FT> {
fn $s_rev_method(&self, lhs: &S<FT>) -> TVec4<FT> {
let &S(ref lhs) = lhs;
TVec4 { x: lhs.$op(&self.x), y: lhs.$op(&self.y), z: lhs.$op(&self.z), w: lhs.$op(&self.w) }
}
}
impl<FT:Num> $V_RHS_trait<FT,TVec4<FT>> for TVec4<FT> {
fn $v_rev_method(&self, lhs: &TVec4<FT>) -> TVec4<FT> {
TVec4 { x: lhs.x.$op(&self.x), y: lhs.y.$op(&self.y), z: lhs.z.$op(&self.z), w: lhs.w.$op(&self.w) }
}
}
impl $ASSIGN_RHS_trait<$ft> for $ft {
fn $op_into_method(&self, recv: &mut TVec4<$ft>) {
recv.x = recv.x.$op(self);
recv.y = recv.y.$op(self);
recv.z = recv.z.$op(self);
recv.w = recv.w.$op(self);
}
}
impl<FT:Num> $ASSIGN_RHS_trait<FT> for TVec4<FT> {
fn $op_into_method(&self, recv: &mut TVec4<FT>) {
recv.x = recv.x.$op(&self.x);
recv.y = recv.y.$op(&self.y);
recv.z = recv.z.$op(&self.z);
recv.w = recv.w.$op(&self.w);
}
}
}
}
double_dispatch_usual_impls_4! { f32 add
TVec4AddRHS { rev_add } SAddRHS { rev_add }
TVec4AddAssignRHS { add_into } }
double_dispatch_usual_impls_4! { f32 sub
TVec4SubRHS { rev_sub } SSubRHS { rev_sub }
TVec4SubAssignRHS { rsb_into } }
double_dispatch_usual_impls_4! { f32 mul
TVec4MulRHS { rev_mul } SMulRHS { rev_mul }
TVec4MulAssignRHS { mul_into } }
double_dispatch_usual_impls_4! { f32 div
TVec4DivRHS { rev_div } SDivRHS { rev_div }
TVec4DivAssignRHS { div_into } }
pub trait Vec3Args { fn make(self) -> vec3; }
pub fn vec3<Args:Vec3Args>(args: Args) -> vec3 { args.make() }
macro_rules! impl_Vec3Args_for {
($a:ident,$b:ident,$c:ident) => {
impl Vec3Args for ($a,$b,$c) { fn make(self) -> vec3 { let (x,y,z) = self; TVec3 { x: x as f32, y: y as f32, z: z as f32 } } }
}
;
($a:ident 2,$b:ident) => {
impl Vec3Args for ($a,$b) { fn make(self) -> vec3 { let (TVec2{x:x,y:y},z) = self; TVec3 { x: x as f32, y: y as f32, z: z as f32 } } }
}
;
($a:ident,$b:ident 2) => {
impl Vec3Args for ($a,$b) { fn make(self) -> vec3 { let (x,TVec2{x:y,y:z}) = self; TVec3 { x: x as f32, y: y as f32, z: z as f32 } } }
}
;
($a:ident 3 $S:ident) => {
impl Vec3Args for $a { fn make(self) -> vec3 { let $S{x:x,y:y,z:z,..} = self; TVec3 { x: x as f32, y: y as f32, z: z as f32 } } }
}
;
($a:ident copy) => {
impl Vec3Args for $a { fn make(self) -> vec3 { TVec3 { x: self as f32, y: self as f32, z: self as f32 } } }
}
}
macro_rules! impl_Vec3Args_for_choice {
( $a:ident, $b:ident, $c:ident, $($ignore:ident),*) => {
impl_Vec3Args_for!($a, $b, $c)
}
}
impl_Vec3Args_for!(f32 copy)
impl_Vec3Args_for!(int copy)
all_choices!( impl_Vec3Args_for_choice :
todo: { (int | f32) (int | f32) (int | f32) }
done: { (ignored) })
impl_Vec3Args_for!(vec2 2,f32)
impl_Vec3Args_for!(vec2 2,int)
impl_Vec3Args_for!(f32,vec2 2)
impl_Vec3Args_for!(int,vec2 2)
impl_Vec3Args_for!(vec3 3 TVec3)
impl_Vec3Args_for!(vec4 3 TVec4)
impl Vec3Args for [int, ..3] { fn make(self) -> vec3 { let v = self; TVec3 { x: v[0] as f32, y: v[1] as f32, z: v[2] as f32 } } }
pub trait Vec4Args { fn make(self) -> vec4; }
pub fn vec4<Args:Vec4Args>(args: Args) -> vec4 { args.make() }
macro_rules! impl_Vec4Args_for {
($a:ident,$b:ident,$c:ident,$d:ident) => {
impl Vec4Args for ($a,$b,$c,$d) { fn make(self) -> vec4 { let (x,y,z,w) = self; TVec4 { x: x as f32, y: y as f32, z: z as f32, w: w as f32 } } }
}
;
($a:ident 2,$b:ident,$c:ident) => {
impl Vec4Args for ($a,$b,$c) { fn make(self) -> vec4 { let (TVec2{x:x,y:y},z,w) = self; TVec4 { x: x as f32, y: y as f32, z: z as f32, w: w as f32 } } }
}
;
($a:ident,$b:ident 2,$c:ident) => {
impl Vec4Args for ($a,$b,$c) { fn make(self) -> vec4 { let (x,TVec2{x:y,y:z},w) = self; TVec4 { x: x as f32, y: y as f32, z: z as f32, w: w as f32 } } }
}
;
($a:ident,$b:ident,$c:ident 2) => {
impl Vec4Args for ($a,$b,$c) { fn make(self) -> vec4 { let (x,y,TVec2{x:z,y:w}) = self; TVec4 { x: x as f32, y: y as f32, z: z as f32, w: w as f32 } } }
}
;
($a:ident 2,$b:ident 2) => {
impl Vec4Args for ($a,$b) { fn make(self) -> vec4 { let (TVec2{x:x,y:y},TVec2{x:z,y:w}) = self; TVec4 { x: x as f32, y: y as f32, z: z as f32, w: w as f32 } } }
}
;
($a:ident 3,$b:ident) => {
impl Vec4Args for ($a,$b) { fn make(self) -> vec4 { let (TVec3{x:x,y:y,z:z},w) = self; TVec4 { x: x as f32, y: y as f32, z: z as f32, w: w as f32 } } }
}
;
($a:ident,$b:ident 3) => {
impl Vec4Args for ($a,$b) { fn make(self) -> vec4 { let (x,TVec3{x:y,y:z,z:w}) = self; TVec4 { x: x as f32, y: y as f32, z: z as f32, w: w as f32 } } }
}
;
($a:ident 4 $S:ident) => {
impl Vec4Args for $a { fn make(self) -> vec4 { let $S{x:x,y:y,z:z,w:w,..} = self; TVec4 { x: x as f32, y: y as f32, z: z as f32, w: w as f32 } } }
}
;
($a:ident copy) => {
impl Vec4Args for $a { fn make(self) -> vec4 { TVec4 { x: self as f32, y: self as f32, z: self as f32, w: self as f32 } } }
}
}
impl Vec4Args for [int, ..4] { fn make(self) -> vec4 { let v = self; TVec4 { x: v[0] as f32, y: v[1] as f32, z: v[2] as f32, w: v[3] as f32 } } }
impl_Vec4Args_for!(f32 copy)
impl_Vec4Args_for!(int copy)
macro_rules! impl_Vec4Args_for_choice {
( $a:ident, $b:ident, $c:ident, $d:ident, $($ignore:ident),*) => {
impl_Vec4Args_for!($a, $b, $c, $d)
}
}
all_choices!( impl_Vec4Args_for_choice :
todo: { (int | f32) (int | f32) (int | f32) (int | f32) }
done: { (ignored) } )
impl_Vec4Args_for!(vec2 2,int,int)
impl_Vec4Args_for!(vec2 2,int,f32)
impl_Vec4Args_for!(vec2 2,f32,int)
impl_Vec4Args_for!(vec2 2,f32,f32)
impl_Vec4Args_for!(int, vec2 2,int)
impl_Vec4Args_for!(int, vec2 2,f32)
impl_Vec4Args_for!(f32, vec2 2,int)
impl_Vec4Args_for!(f32, vec2 2,f32)
impl_Vec4Args_for!(int, int, vec2 2)
impl_Vec4Args_for!(int, f32, vec2 2)
impl_Vec4Args_for!(f32, int, vec2 2)
impl_Vec4Args_for!(f32, f32, vec2 2)
impl_Vec4Args_for!(vec2 2,vec2 2)
impl_Vec4Args_for!(int, vec3 3)
impl_Vec4Args_for!(f32, vec3 3)
impl_Vec4Args_for!(vec3 3, int)
impl_Vec4Args_for!(vec3 3, f32)
impl_Vec4Args_for!(vec4 4 TVec4)
macro_rules! vec1 { ($($arg:expr),*) => { vec1(($($arg),*)) } }
macro_rules! vec2 { ($($arg:expr),*) => { vec2(($($arg),*)) } }
macro_rules! vec3 { ($($arg:expr),*) => { vec3(($($arg),*)) } }
macro_rules! vec4 { ($($arg:expr),*) => { vec4(($($arg),*)) } }
macro_rules! impl_EpsilonEq_for {
($TVec:ident $($x:ident),*) => {
impl<T:Num+Ord> EpsilonEq<$TVec<T>> for $TVec<T> {
fn epsilon_eq(&self, rhs: &$TVec<T>, epsilons: &$TVec<T>) -> bool {
let mut result = true;
{ $( result = result && ((self.$x - rhs.$x) < epsilons.$x); )* }
return result;
}
}
}
}
impl_EpsilonEq_for!(TVec1 x)
impl_EpsilonEq_for!(TVec2 x, y)
impl_EpsilonEq_for!(TVec3 x, y, z)
impl_EpsilonEq_for!(TVec4 x, y, z, w)
#[cfg(test)]
mod vec1_tests {
#![allow(uppercase_variables)]
use super::vec1;
use super::ivec1;
use src::operators::{Increment};
#[test]
fn test_operators() {
let A = vec1!(1.0f32);
let B = vec1!(2.0f32);
assert!(A != B);
}
#[test]
fn test_operator_increment() {
let v0 = ivec1(1i32);
let mut v1 = ivec1(v0);
let mut v2 = ivec1(v0);
let v3 = v1.preincrement();
let v4 = v2.postincrement();
let v1 = v1;
let v2 = v2;
assert_eq!(v0, v4);
assert_eq!(v1, v2);
assert_eq!(v1, *v3);
}
}
#[cfg(test)]
mod vec2_tests {
#![allow(uppercase_variables)]
use super::vec2;
use src::operators::{AddAssign,SubAssign,MulAssign,DivAssign};
use src::operators::{Increment, Decrement};
use src::scalar::S;
#[test]
fn test_operators() {
let A = vec2!(1.0f32);
let B = vec2!(1.0f32);
assert!(A == B);
let A = vec2!(1.0f32);
let C = A + 1.0f32;
let mut A = A;
A.add_assign(&1.0f32);
assert!(A.x == 2.0 && A.y == 2.0);
assert!(A == C);
let A = vec2!(1.0f32);
let B = vec2!(2.0f32, -1.0f32);
let C = A + B;
let mut A = A;
A.add_assign(&B);
assert!(A.x == 3.0 && A.y == 0.0);
assert!(A == C);
let A = vec2!(1.0f32);
let C = A - 1.0f32;
let mut A = A;
A.sub_assign(&1.0f32);
assert!(A.x == 0.0 && A.y == 0.0);
assert!(A == C);
let A = vec2!(1.0f32);
let C = A * 2.0f32;
let mut A = A;
A.mul_assign(&2.0f32);
assert!(A.x == 2.0 && A.y == 2.0);
assert!(A == C);
let A = vec2!(2.0f32);
let B = vec2!(2.0f32);
let C = A / B;
let mut A = A;
A.div_assign(&B);
assert!(A.x == 1.0 && A.y == 1.0);
assert!(A == C);
let A = vec2!(1.0f32, 2.0f32);
let B = vec2!(4.0f32, 5.0f32);
let C = A + B;
assert_eq!(C, vec2!( 5, 7 ));
let D = B - A;
assert_eq!(D, vec2!( 3, 3 ));
let E = A * B;
assert_eq!(E, vec2!( 4, 10 ));
let F = B / A;
assert_eq!(F, vec2!( 4, 2.5f32 ));
let G = A + 1.0f32;
assert_eq!(G, vec2!( 2, 3 ));
let H = B - 1.0f32;
assert_eq!(H, vec2!( 3, 4 ));
let I = A * 2.0f32;
assert_eq!(I, vec2!( 2, 4 ));
let J = B / 2.0f32;
assert_eq!(J, vec2!( 2, 2.5f32 ));
let K = S(1.0f32) + A;
assert_eq!(K, vec2!( 2, 3 ));
let L = S(1.0f32) - B;
assert_eq!(L, vec2!(-3, -4 ));
let M = S(2.0f32) * A;
assert_eq!(M, vec2!( 2, 4 ));
let N = S(2.0f32) / B;
assert_eq!(N, vec2!( 0.5f32, 2.0f32/5.0f32 ));
let A = vec2!( 1, 2 );
let B = vec2!( 4, 5 );
let mut A = A;
A.add_assign(&B);
assert_eq!(A, vec2!( 5, 7 ));
A.add_assign(&1.0f32);
assert_eq!(A, vec2!( 6, 8 ));
let A = vec2!( 1, 2 );
let B = vec2!( 4, 5 );
let mut B = B;
B.sub_assign(&A);
assert_eq!(B, vec2!( 3, 3 ));
B.sub_assign(&1.0f32);
assert_eq!(B, vec2!( 2, 2 ));
let A = vec2!( 1, 2 );
let B = vec2!( 4, 5 );
let mut A = A;
A.mul_assign(&B);
assert_eq!(A, vec2!( 4, 10 ));
A.mul_assign(&2.0f32);
assert_eq!(A, vec2!( 8, 20 ));
let A = vec2!( 1, 2 );
let B = vec2!( 4, 5 );
let mut B = B;
B.div_assign(&A);
assert_eq!(B, vec2!( 4, 2.5f32 ));
B.div_assign(&2.0f32);
assert_eq!(B, vec2!( 2, 1.25f32 ));
let B = vec2!(2.0f32);
let mut B = B;
let B_y = B.y;
B.div_assign(&B_y);
assert_eq!(B, vec2!( 1.0f32 ));
let A = vec2!( 1.0f32, 2.0f32 );
let B = -A;
assert_eq!(B, vec2!( -1.0f32, -2.0f32 ));
}
#[test]
fn test_incr_decr_operators() {
let A = vec2!( 1.0f32, 2.0f32 );
let mut A = A;
{
let B = A.predecrement(); // Rust does not have operator--(int).
assert_eq!(*B, vec2!( 0.0f32, 1.0f32 ));
}
assert_eq!(A, vec2!( 0.0f32, 1.0f32 ));
let A = vec2!( 1.0f32, 2.0f32 );
let mut A = A;
let B = A.postdecrement(); // Rust does not have operator--().
assert_eq!(B, vec2!( 1.0f32, 2.0f32 ));
assert_eq!(A, vec2!( 0.0f32, 1.0f32 ));
let A = vec2!( 1.0f32, 2.0f32 );
let mut A = A;
{
let B = A.preincrement(); // Rust does not have operator++(int).
assert_eq!(*B, vec2!( 2.0f32, 3.0f32 ));
}
assert_eq!(A, vec2!( 2.0f32, 3.0f32 ));
let A = vec2!( 1.0f32, 2.0f32 );
let mut A = A;
let B = A.postincrement(); // Rust does not have operator++().
assert_eq!(B, vec2!( 1.0f32, 2.0f32 ));
assert_eq!(A, vec2!( 2.0f32, 3.0f32 ));
}
}
#[cfg(test)]
mod vec3_tests {
#![allow(uppercase_variables)]
use super::{vec2, vec3, vec4};
use super::{ivec3};
use super::dot;
use super::{Swizzle2,Swizzle3,Swizzle4};
use src::operators::{Increment,Decrement};
use src::operators::{AddAssign,SubAssign,MulAssign,DivAssign};
use src::scalar::S;
#[test]
fn test_ctor() {
let A = vec3!(1);
let B = vec3!( 1, 1, 1 );
assert_eq!(A, B);
let mut Tests = vec![];
Tests.push(vec3!(vec2!(1,2), 3));
Tests.push(vec3!(1, vec2!(2,3)));
Tests.push(vec3!(1, 2, 3));
Tests.push(vec3!(vec4!(1, 2, 3, 4)));
for v in Tests.iter() {
assert_eq!(*v, vec3!(1, 2, 3));
}
}
#[test]
fn test_operators() {
let A = vec3!(1.0f32);
let B = vec3!(1.0f32);
assert!(A == B);
let A = vec3!(1.0f32, 2.0f32, 3.0f32);
let B = vec3!(4.0f32, 5.0f32, 6.0f32);
let C = A + B;
assert_eq!(C, vec3!(5, 7, 9));
let D = B - A;
assert_eq!(D, vec3!(3, 3, 3));