-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathegl_tform3.h
2085 lines (1876 loc) · 75.3 KB
/
egl_tform3.h
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
//============================================================================
// EmberGL
//
// Copyright (c) 2022, Jarkko Lempiainen
// All rights reserved.
//============================================================================
#ifndef EGL_TFORM3_H
#define EGL_TFORM3_H
//----------------------------------------------------------------------------
//============================================================================
// interface
//============================================================================
// external
#include "egl_math.h"
EGL_NAMESPACE_BEGIN
// new
template<typename> struct tform3;
typedef tform3<float> tform3f;
typedef tform3<double> tform3d;
template<typename> struct tform_rt3;
typedef tform_rt3<float> tform_rt3f;
typedef tform_rt3<double> tform_rt3d;
template<typename> class camera;
typedef camera<float> cameraf;
typedef camera<double> camerad;
// tform3 operations
template<typename T> EGL_INLINE bool is_zero(const tform3<T>&); // test for zero-transform, i.e. all components equal zero (exact)
template<typename T> EGL_INLINE bool is_orthogonal(const tform3<T>&); // test for orthogonal transform
template<typename T> EGL_INLINE bool is_sat(const tform3<T>&); // test for saturated transform, i.e. all components are in range [0, 1]
template<typename T> EGL_INLINE bool is_ssat(const tform3<T>&); // test for signed saturated transform, i.e. all components are in range [-1, 1]
template<typename T> EGL_INLINE bool operator==(const tform3<T>&, const tform3<T>&); // test for equality of transforms, i.e. all components of transforms are equal (exact)
template<typename T> EGL_INLINE bool operator!=(const tform3<T>&, const tform3<T>&); // test for inequality of transforms, i.e. any of the components of the transforms are unequal (exact)
template<typename T> EGL_INLINE void operator+=(tform3<T>&, const tform3<T>&); // add transform to transform
template<typename T> EGL_INLINE void operator-=(tform3<T>&, const tform3<T>&); // subtract transform from transform
template<typename T> EGL_INLINE void operator*=(tform3<T>&, const tform3<T>&); // multiply transform by transform
template<typename T> EGL_INLINE void operator*=(tform3<T>&, const quat<T>&); // multiply transform by quaternion
template<typename T> EGL_INLINE void operator*=(tform3<T>&, const mat33<T>&); // multiply transform by matrix
template<typename T> EGL_INLINE void operator*=(tform3<T>&, typename math<T>::scalar_t); // multiply transform by scalar
template<typename T> EGL_INLINE void operator*=(vec3<T>&, const tform3<T>&); // multiply 3d vector by transform (assume w=1)
template<typename T> EGL_INLINE void operator*=(vec4<T>&, const tform3<T>&); // multiply 4d vector by transform
template<typename T> EGL_INLINE void operator*=(mat44<T>&, const tform3<T>&); // multiply 4x4 matrix by transform
template<typename T> EGL_INLINE void operator/=(tform3<T>&, typename math<T>::scalar_t); // divide transform by scalar
template<typename T> EGL_INLINE tform3<T> operator+(const tform3<T>&, const tform3<T>&); // add transform to transform
template<typename T> EGL_INLINE tform3<T> operator-(const tform3<T>&, const tform3<T>&); // subtract transform from transform
template<typename T> EGL_INLINE tform3<T> operator-(const tform3<T>&); // negate the transform
template<typename T> EGL_INLINE tform3<T> operator*(const tform3<T>&, const tform3<T>&); // multiply transform by transform
template<typename T> EGL_INLINE tform3<T> operator*(const tform3<T>&, const quat<T>&); // multiply transform by quaternion
template<typename T> EGL_INLINE tform3<T> operator*(const tform3<T>&, const mat33<T>&); // multiply transform by matrix
template<typename T> EGL_INLINE tform3<T> operator*(const tform3<T>&, typename math<T>::scalar_t); // multiply transform by scalar
template<typename T> EGL_INLINE tform3<T> operator*(typename math<T>::scalar_t, const tform3<T>&); // multiply transform by scalar
template<typename T> EGL_INLINE vec3<T> operator*(const vec3<T>&, const tform3<T>&); // multiply 3d vector by transform (assume w=1)
template<typename T> EGL_INLINE vec4<T> operator*(const vec4<T>&, const tform3<T>&); // multiply 4d vector by transform
template<typename T> EGL_INLINE vec3<T> operator*(const tform3<T>&, const vec3<T>&); // multiply transform by transposed 3d vector (assume w=1)
template<typename T> EGL_INLINE vec4<T> operator*(const tform3<T>&, const vec4<T>&); // multiply transform by transposed 4d vector
template<typename T> EGL_INLINE mat44<T> operator*(const mat44<T>&, const tform3<T>&); // multiply 4x4 matrix by transform
template<typename T> EGL_INLINE mat44<T> operator*(const tform3<T>&, const mat44<T>&); // multiply transform by 4x4 matrix
template<typename T> EGL_INLINE tform3<T> operator/(const tform3<T>&, typename math<T>::scalar_t); // divide transform by scalar
template<typename T> EGL_INLINE tform3<T> madd(const tform3<T> &t0_, // component-wise multiply-add: t0_*t1+t2_
const tform3<T> &t1_,
const tform3<T> &t2_);
template<typename T> EGL_INLINE typename math<T>::scalar_t det(const tform3<T>&); // determinant of the transform
template<typename T> EGL_INLINE tform3<T> inv(const tform3<T>&, typename math<T>::scalar_t *det_=0); // inverse of the transform
template<typename T> EGL_INLINE typename math<T>::scalar_t tr(const tform3<T>&); // trace of the transform
template<typename T> EGL_INLINE void identity(tform3<T>&); // set transform to identity
template<typename T> EGL_INLINE void transpose_matrix33(tform3<T>&); // transpose the 3x3 upper-left matrix of the transform
template<typename T> EGL_INLINE void transpose_matrix33(tform3<T>&, const tform3<T>&); // transpose the 3x3 upper-left matrix of the transform
template<typename T> EGL_INLINE void neg(tform3<T>&); // negate the transform
template<typename T> EGL_INLINE tform3<T> lerp(const tform3<T> &t0_, // linear interpolation of transforms: f(t=0)=t0, f(t=1)=t1
const tform3<T> &t1_,
typename math<T>::scalar_t t_);
template<typename T> EGL_INLINE void lerp(tform3<T> &tr_, // linear interpolation of transforms: f(t=0)=t0, f(t=1)=t1
const tform3<T> &t0_,
const tform3<T> &t1_,
typename math<T>::scalar_t t_);
template<typename T> EGL_INLINE void zrot_u(tform3<T>&, // setup transform from position and z-axis direction
const vec3<T> &pos_,
const vec3<T> &zdir_);
template<typename T> EGL_INLINE void zrot_u(tform3<T>&, // setup transform from position, z-axis direction and up-vector
const vec3<T> &pos_,
const vec3<T> &zdir_,
const vec3<T> &up_);
template<typename T> EGL_INLINE void axis_rot(tform3<T>&, // setup transform from position and rotation about given axis
const vec3<T> &pos_,
const vec3<T> &axis_,
typename math<T>::scalar_t angle_);
template<typename T> EGL_INLINE void axis_rot_u(tform3<T>&, // setup transform from position and rotation about given unit axis
const vec3<T> &pos_,
const vec3<T> &axis_,
typename math<T>::scalar_t angle_);
template<typename T, typename U> EGL_INLINE tform3<T> to_tform3(const tform3<U>&); // convert between tform3 types
template<typename T> EGL_INLINE void orthonormalize(tform3<T>&); // ortho-normalize the transform
// tform_rt3 operations
template<typename T> EGL_INLINE bool is_zero(const tform_rt3<T>&); // test for zero-transform, i.e. all components equal zero (exact)
template<typename T> EGL_INLINE bool is_sat(const tform3<T>&); // test for saturated transform, i.e. all components are in range [0, 1]
template<typename T> EGL_INLINE bool is_ssat(const tform3<T>&); // test for signed saturated transform, i.e. all components are in range [-1, 1]
template<typename T> EGL_INLINE bool operator==(const tform_rt3<T>&, const tform_rt3<T>&); // test for equality of transforms, i.e. all components of transforms are equal (exact)
template<typename T> EGL_INLINE bool operator!=(const tform_rt3<T>&, const tform_rt3<T>&); // test for inequality of transforms, i.e. any of the components of the transforms are unequal (exact)
template<typename T> EGL_INLINE void operator+=(tform_rt3<T>&, const tform_rt3<T>&); // add transform to transform
template<typename T> EGL_INLINE void operator-=(tform_rt3<T>&, const tform_rt3<T>&); // subtract transform from transform
template<typename T> EGL_INLINE void operator*=(tform_rt3<T>&, const tform_rt3<T>&); // multiply transform by transform
template<typename T> EGL_INLINE void operator*=(tform_rt3<T>&, const quat<T>&); // multiply transform by quaternion
template<typename T> EGL_INLINE void operator*=(tform_rt3<T>&, typename math<T>::scalar_t); // multiply transform by scalar
template<typename T> EGL_INLINE void operator*=(vec3<T>&, const tform_rt3<T>&); // multiply 3d vector by transform (assume w=1)
template<typename T> EGL_INLINE void operator*=(vec4<T>&, const tform_rt3<T>&); // multiply 4d vector by transform
template<typename T> EGL_INLINE void operator*=(mat44<T>&, const tform_rt3<T>&); // multiply 4x4 matrix by transform
template<typename T> EGL_INLINE void operator/=(tform_rt3<T>&, typename math<T>::scalar_t); // divide transform by scalar
template<typename T> EGL_INLINE tform_rt3<T> operator+(const tform_rt3<T>&, const tform_rt3<T>&); // add transform to transform
template<typename T> EGL_INLINE tform_rt3<T> operator-(const tform_rt3<T>&, const tform_rt3<T>&); // subtract transform from transform
template<typename T> EGL_INLINE tform_rt3<T> operator-(const tform_rt3<T>&); // negate the transform
template<typename T> EGL_INLINE tform_rt3<T> operator*(const tform_rt3<T>&, const tform_rt3<T>&); // multiply transform by transform
template<typename T> EGL_INLINE tform_rt3<T> operator*(const tform_rt3<T>&, const quat<T>&); // multiply transform by quaternion
template<typename T> EGL_INLINE tform_rt3<T> operator*(const tform_rt3<T>&, typename math<T>::scalar_t); // multiply transform by scalar
template<typename T> EGL_INLINE tform_rt3<T> operator*(typename math<T>::scalar_t, const tform_rt3<T>&); // multiply transform by scalar
template<typename T> EGL_INLINE vec3<T> operator*(const vec3<T>&, const tform_rt3<T>&); // multiply 3d vector by transform (assume w=1)
template<typename T> EGL_INLINE vec4<T> operator*(const vec4<T>&, const tform_rt3<T>&); // multiply 4d vector by transform
template<typename T> EGL_INLINE vec3<T> operator*(const tform_rt3<T>&, const vec3<T>&); // multiply transform by transposed 3d vector (assume w=1)
template<typename T> EGL_INLINE vec4<T> operator*(const tform_rt3<T>&, const vec4<T>&); // multiply transform by transposed 4d vector
template<typename T> EGL_INLINE mat44<T> operator*(const mat44<T>&, const tform_rt3<T>&); // multiply 4x4 matrix by transform
template<typename T> EGL_INLINE mat44<T> operator*(const tform_rt3<T>&, const mat44<T>&); // multiply transform by 4x4 matrix
template<typename T> EGL_INLINE tform_rt3<T> operator/(const tform_rt3<T>&, typename math<T>::scalar_t); // divide transform by scalar
template<typename T> EGL_INLINE tform_rt3<T> madd(const tform_rt3<T> &t0_, // component-wise multiply-add: t0_*t1+t2_
const tform_rt3<T> &t1_,
const tform_rt3<T> &t2_);
template<typename T> EGL_INLINE tform_rt3<T> inv(const tform_rt3<T>&); // inverse of the transform
template<typename T> EGL_INLINE void identity(tform_rt3<T>&); // set transform to identity
template<typename T> EGL_INLINE void neg(tform_rt3<T>&); // negate the transform
template<typename T> EGL_INLINE tform_rt3<T> lerp(const tform_rt3<T> &t0_, // linear interpolation of transforms: f(t=0)=t0, f(t=1)=t1
const tform_rt3<T> &t1_,
typename math<T>::scalar_t t_);
template<typename T> EGL_INLINE tform_rt3<T> nlerp(const tform_rt3<T> &t0_, // linear interpolation of transforms with normalized rotation
const tform_rt3<T> &t1_,
typename math<T>::scalar_t t_);
template<typename T> EGL_INLINE tform_rt3<T> nlerp_z(const tform_rt3<T> &t0_, // linear interpolation of transforms with normalized rotation, if |rotation|=0, rotation=[0, 0, 0, 1]
const tform_rt3<T> &t1_,
typename math<T>::scalar_t t_);
template<typename T> EGL_INLINE tform_rt3<T> slerp(const tform_rt3<T> &t0_, // linear interpolation of translation with spherical rotation interpolation
const tform_rt3<T> &t1_,
typename math<T>::scalar_t t_);
template<typename T> EGL_INLINE void zrot_u(tform_rt3<T>&, // setup transform from position and z-axis direction
const vec3<T> &pos_,
const vec3<T> &zdir_);
template<typename T> EGL_INLINE void axis_rot(tform_rt3<T>&, // setup transform from position and rotation about given axis
const vec3<T> &pos_,
const vec3<T> &axis_,
typename math<T>::scalar_t angle_);
template<typename T> EGL_INLINE void axis_rot_u(tform_rt3<T>&, // setup transform from position and rotation about given unit axis
const vec3<T> &pos_,
const vec3<T> &axis_,
typename math<T>::scalar_t angle_);
template<typename T, typename U> EGL_INLINE tform_rt3<T> to_tform_rt3(const tform_rt3<U>&); // convert between tform_rt3 types
// 3d unit vector <-> octahedron coordinate transforms
template<typename T> EGL_INLINE vec2<T> vec3_to_oct(const vec3<T>&); // convert 3d unit vector to 2d octahedron coordinates [-1, 1]
template<typename T> EGL_INLINE vec3<T> oct_to_vec3(const vec2<T>&); // convert 2d octahedron coordinates [-1, 1] to 3d unit vector
template<typename T> EGL_INLINE vec2<T> vec3_to_oct2x1(const vec3<T>&); // convert 3d unit vector to 2d octahedron coordinates [-2, 2]x[-1, 1]
template<typename T> EGL_INLINE vec3<T> oct2x1_to_vec3(const vec2<T>&); // convert 2d octahedron coordinates [-2, 2]x[-1, 1] to 3d unit vector
// 3d frame quantization
template<typename T> uint32_t quantize_mat33_32(const mat33<T>&); // quantize left/right handed 3d rotation matrix to 32 bits
template<typename T> mat33<T> dequantize_mat33_32(uint32_t); // dequantize left/right handed 3d rotation matrix from 32 bits
template<typename T> vec3<T> dequantize_mat33z_32(uint32_t); // dequantize z-axis from 32bit quantized 3d rotation matrix
// projection matrix setup
template<typename T> mat44<T> perspective_matrix(typename math<T>::scalar_t fov_width_, typename math<T>::scalar_t aspect_ratio_, typename math<T>::scalar_t near_, typename math<T>::scalar_t far_, bool reversed_=false);
template<typename T> mat44<T> orthogonal_matrix(typename math<T>::scalar_t width_, typename math<T>::scalar_t aspect_ratio_, typename math<T>::scalar_t near_, typename math<T>::scalar_t far_, bool reversed_=false);
template<typename T> EGL_INLINE typename math<T>::scalar_t fov_width(const mat44<T> &v2p_); // return fov width angle from view->projection matrix
template<typename T> EGL_INLINE typename math<T>::scalar_t fov_height(const mat44<T> &v2p_); // return fov height angle from view->projection matrix
// miscellaneous
template<typename T> vec4<T> sphere_screen_extents(const vec3<T> &pos_, typename math<T>::scalar_t rad_, const mat44<T> &v2p_);
//----------------------------------------------------------------------------
//============================================================================
// tform3
//============================================================================
template<typename T>
struct tform3
{
typedef typename math<T>::scalar_t scalar_t;
// construction
EGL_INLINE tform3();
EGL_INLINE tform3(scalar_t s_);
EGL_INLINE tform3(scalar_t sx_, scalar_t sy_, scalar_t sz_);
EGL_INLINE tform3(const vec3<T> &translation_);
EGL_INLINE tform3(const vec3<T> &x_,
const vec3<T> &y_,
const vec3<T> &z_);
EGL_INLINE tform3(const vec3<T> &x_,
const vec3<T> &y_,
const vec3<T> &z_,
const vec3<T> &translation_);
EGL_INLINE tform3(const vec4<T> &x_,
const vec4<T> &y_,
const vec4<T> &z_);
EGL_INLINE tform3(scalar_t xx_, scalar_t xy_, scalar_t xz_, scalar_t xtrans_,
scalar_t yx_, scalar_t yy_, scalar_t yz_, scalar_t ytrans_,
scalar_t zx_, scalar_t zy_, scalar_t zz_, scalar_t ztrans_);
EGL_INLINE tform3(const mat33<T>&);
EGL_INLINE tform3(const mat33<T>&, const vec3<T> &translation_);
EGL_INLINE tform3(const quat<T>&);
EGL_INLINE tform3(const quat<T>&, const vec3<T> &translation_);
EGL_INLINE tform3(const tform_rt3<T>&);
//--------------------------------------------------------------------------
// accessors
EGL_INLINE const vec4<T> &operator[](unsigned vidx_) const;
EGL_INLINE vec4<T> &operator[](unsigned vidx_);
EGL_INLINE mat33<T> matrix33() const;
EGL_INLINE mat44<T> matrix44() const;
EGL_INLINE const vec3<T> &x_axis() const;
EGL_INLINE const vec3<T> &y_axis() const;
EGL_INLINE const vec3<T> &z_axis() const;
EGL_INLINE vec3<T> translation() const;
//--------------------------------------------------------------------------
// mutators
EGL_INLINE void set(scalar_t s_);
EGL_INLINE void set(scalar_t sx_, scalar_t sy_, scalar_t sz_);
EGL_INLINE void set(const vec3<T> &translation_);
EGL_INLINE void set(const vec3<T> &x_,
const vec3<T> &y_,
const vec3<T> &z_);
EGL_INLINE void set(const vec3<T> &x_,
const vec3<T> &y_,
const vec3<T> &z_,
const vec3<T> &translation_);
EGL_INLINE void set(const vec4<T> &x_,
const vec4<T> &y_,
const vec4<T> &z_);
EGL_INLINE void set(scalar_t xx_, scalar_t xy_, scalar_t xz_, scalar_t xtrans_,
scalar_t yx_, scalar_t yy_, scalar_t yz_, scalar_t ytrans_,
scalar_t zx_, scalar_t zy_, scalar_t zz_, scalar_t ztrans_);
EGL_INLINE void set(const mat33<T>&);
EGL_INLINE void set(const mat33<T>&, const vec3<T> &translation_);
EGL_INLINE void set(const quat<T>&);
EGL_INLINE void set(const quat<T>&, const vec3<T> &translation_);
EGL_INLINE void set(const tform_rt3<T>&);
EGL_INLINE void set_rotation(const mat33<T>&);
EGL_INLINE void set_translation(const vec3<T>&);
//--------------------------------------------------------------------------
// constants
static EGL_INLINE tform3<T> zero();
static EGL_INLINE tform3<T> identity();
//--------------------------------------------------------------------------
vec4<T> x;
vec4<T> y;
vec4<T> z;
};
//----------------------------------------------------------------------------
//============================================================================
// tform_rt3
//============================================================================
template<typename T>
struct tform_rt3
{
typedef typename math<T>::scalar_t scalar_t;
// construction
EGL_INLINE tform_rt3();
EGL_INLINE tform_rt3(const quat<T> &rotation_);
EGL_INLINE tform_rt3(const vec3<T> &translation_);
EGL_INLINE tform_rt3(const quat<T> &rotation_, const vec3<T> &translation_);
EGL_INLINE tform_rt3(const tform3<T>&);
//--------------------------------------------------------------------------
// accessors
EGL_INLINE mat33<T> matrix33() const;
EGL_INLINE mat44<T> matrix44() const;
//--------------------------------------------------------------------------
// mutators
EGL_INLINE void set(const quat<T> &rotation_);
EGL_INLINE void set(const vec3<T> &translation_);
EGL_INLINE void set(const quat<T> &rotation_, const vec3<T> &translation_);
//--------------------------------------------------------------------------
// constants
static EGL_INLINE tform_rt3<T> zero();
static EGL_INLINE tform_rt3<T> identity();
//--------------------------------------------------------------------------
quat<T> rotation;
vec3<T> translation;
};
//----------------------------------------------------------------------------
//============================================================================
// camera
//============================================================================
template<typename T>
class camera
{
public:
typedef typename math<T>::scalar_t scalar_t;
// construction
camera();
camera(const mat44<T> &view_to_proj_, scalar_t near_, scalar_t far_);
camera(const tform3<T> &view_to_world_, const mat44<T> &view_to_proj_, scalar_t near_, scalar_t far_);
//--------------------------------------------------------------------------
// accessors
EGL_INLINE const mat44<T> &view_to_proj() const;
EGL_INLINE const mat44<T> &proj_to_view() const;
EGL_INLINE const tform3<T> &world_to_view() const;
EGL_INLINE const tform3<T> &view_to_world() const;
EGL_INLINE const mat44<T> &world_to_proj() const;
EGL_INLINE const mat44<T> &proj_to_world() const;
EGL_INLINE scalar_t near_plane() const;
EGL_INLINE scalar_t far_plane() const;
EGL_INLINE vec3<T> world_x() const;
EGL_INLINE vec3<T> world_y() const;
EGL_INLINE vec3<T> world_z() const;
EGL_INLINE vec3<T> world_pos() const;
//--------------------------------------------------------------------------
// mutators
EGL_INLINE void set_transform(const mat44<T> &view_to_proj_,
const tform3<T> &view_to_world_,
scalar_t near_,
scalar_t far_);
EGL_INLINE void set_view_to_proj(const mat44<T>&, scalar_t near_, scalar_t far_);
EGL_INLINE void set_view_to_world(const tform3<T>&);
//--------------------------------------------------------------------------
private:
void update_transforms();
//--------------------------------------------------------------------------
mat44<T> m_view_to_proj;
mat44<T> m_proj_to_view;
tform3<T> m_world_to_view;
tform3<T> m_view_to_world;
mat44<T> m_world_to_proj;
mat44<T> m_proj_to_world;
scalar_t m_near, m_far;
};
//----------------------------------------------------------------------------
//============================================================================
//============================================================================
// inline & template implementations
//============================================================================
//============================================================================
//============================================================================
// tform3
//============================================================================
template<typename T>
tform3<T> tform3<T>::zero()
{
return tform3<T>(0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0);
}
//----
template<typename T>
tform3<T> tform3<T>::identity()
{
return tform3<T>(scalar_t(1), 0, 0, 0,
0, scalar_t(1), 0, 0,
0, 0, scalar_t(1), 0);
}
//----------------------------------------------------------------------------
template<typename T>
tform3<T>::tform3()
:x(scalar_t(1), 0, 0, 0)
,y(0, scalar_t(1), 0, 0)
,z(0, 0, scalar_t(1), 0)
{
}
//----
template<typename T>
tform3<T>::tform3(scalar_t s_)
:x(s_, 0, 0, 0)
,y(0, s_, 0, 0)
,z(0, 0, s_, 0)
{
}
//----
template<typename T>
tform3<T>::tform3(scalar_t sx_, scalar_t sy_, scalar_t sz_)
:x(sx_, 0, 0, 0)
,y(0, sy_, 0, 0)
,z(0, 0, sz_, 0)
{
}
//----
template<typename T>
tform3<T>::tform3(const vec3<T> &translation_)
:x(scalar_t(1), 0, 0, translation_.x)
,y(0, scalar_t(1), 0, translation_.y)
,z(0, 0, scalar_t(1), translation_.z)
{
}
//----
template<typename T>
tform3<T>::tform3(const vec3<T> &x_, const vec3<T> &y_, const vec3<T> &z_)
:x(x_, 0)
,y(y_, 0)
,z(z_, 0)
{
}
//----
template<typename T>
tform3<T>::tform3(const vec3<T> &x_, const vec3<T> &y_, const vec3<T> &z_, const vec3<T> &translation_)
:x(x_, translation_.x)
,y(y_, translation_.y)
,z(z_, translation_.z)
{
}
//----
template<typename T>
tform3<T>::tform3(const vec4<T> &x_, const vec4<T> &y_, const vec4<T> &z_)
:x(x_)
,y(y_)
,z(z_)
{
}
//----
template<typename T>
tform3<T>::tform3(scalar_t xx_, scalar_t xy_, scalar_t xz_, scalar_t xtrans_,
scalar_t yx_, scalar_t yy_, scalar_t yz_, scalar_t ytrans_,
scalar_t zx_, scalar_t zy_, scalar_t zz_, scalar_t ztrans_)
:x(xx_, xy_, xz_, xtrans_)
,y(yx_, yy_, yz_, ytrans_)
,z(zx_, zy_, zz_, ztrans_)
{
}
//----
template<typename T>
tform3<T>::tform3(const mat33<T> &m_)
:x(m_.x, 0)
,y(m_.y, 0)
,z(m_.z, 0)
{
}
//----
template<typename T>
tform3<T>::tform3(const mat33<T> &m_, const vec3<T> &translation_)
:x(m_.x, translation_.x)
,y(m_.y, translation_.y)
,z(m_.z, translation_.z)
{
}
//----
template<typename T>
tform3<T>::tform3(const quat<T> &q_)
{
// convert quaternion to matrix and setup transform
mat33<T> m;
convert(m, q_);
x.set(m.x, 0);
y.set(m.y, 0);
z.set(m.z, 0);
}
//----
template<typename T>
tform3<T>::tform3(const quat<T> &q_, const vec3<T> &translation_)
{
// convert quaternion to matrix and setup transform
mat33<T> m;
convert(m, q_);
x.set(m.x, translation_.x);
y.set(m.y, translation_.y);
z.set(m.z, translation_.z);
}
//----
template<typename T>
tform3<T>::tform3(const tform_rt3<T> &t_)
{
// convert tform_rt3 rotation to matrix and setup transform
mat33<T> m;
convert(m, t_.rotation);
x.set(m.x, t_.translation.x);
y.set(m.y, t_.translation.y);
z.set(m.z, t_.translation.z);
}
//----------------------------------------------------------------------------
template<typename T>
const vec4<T> &tform3<T>::operator[](unsigned vidx_) const
{
// return reference to nth transformation vector (x=0, y=1, z=2)
EGL_STATIC_ASSERT_MSG(sizeof(tform3)==sizeof(vec4<T>)*3, transform3_size_is_incorrect);
EGL_ASSERT_PEDANTIC(vidx_<3);
return reinterpret_cast<const vec4<T>*>(this)[vidx_];
}
//----
template<typename T>
vec4<T> &tform3<T>::operator[](unsigned vidx_)
{
// return reference to nth transformation vector (x=0, y=1, z=2)
EGL_STATIC_ASSERT_MSG(sizeof(tform3)==sizeof(vec4<T>)*3, transform3_size_is_incorrect);
EGL_ASSERT_PEDANTIC(vidx_<3);
return reinterpret_cast<vec4<T>*>(this)[vidx_];
}
//----
template<typename T>
mat33<T> tform3<T>::matrix33() const
{
// return affine matrix of the transform
return mat33<T>(x.x, x.y, x.z,
y.x, y.y, y.z,
z.x, z.y, z.z);
}
//----
template<typename T>
mat44<T> tform3<T>::matrix44() const
{
// return 4x4 matrix of the transform
return mat44<T>(x.x, x.y, x.z, 0,
y.x, y.y, y.z, 0,
z.x, z.y, z.z, 0,
x.w, y.w, z.w, scalar_t(1));
}
//----
template<typename T>
const vec3<T> &tform3<T>::x_axis() const
{
// return x-axis vector of the transform
const void *xv=&x;
return *(const vec3<T>*)xv;
}
//----
template<typename T>
const vec3<T> &tform3<T>::y_axis() const
{
// return y-axis vector of the transform
const void *yv=&y;
return *(const vec3<T>*)yv;
}
//----
template<typename T>
const vec3<T> &tform3<T>::z_axis() const
{
// return z-axis vector of the transform
const void *zv=&z;
return *(const vec3<T>*)zv;
}
//----
template<typename T>
vec3<T> tform3<T>::translation() const
{
// return translation vector of the transform
return vec3<T>(x.w, y.w, z.w);
}
//----------------------------------------------------------------------------
template<typename T>
void tform3<T>::set(scalar_t s_)
{
// setup uniform scaling transform
x.set(s_, 0, 0, 0);
y.set(0, s_, 0, 0);
z.set(0, 0, s_, 0);
}
//----
template<typename T>
void tform3<T>::set(scalar_t sx_, scalar_t sy_, scalar_t sz_)
{
// setup non-uniform scaling transform
x.set(sx_, 0, 0, 0);
y.set(0, sy_, 0, 0);
z.set(0, 0, sz_, 0);
}
//----
template<typename T>
void tform3<T>::set(const vec3<T> &translation_)
{
// setup translation transform
x.set(scalar_t(1), 0, 0, translation_.x);
y.set(0, scalar_t(1), 0, translation_.y);
z.set(0, 0, scalar_t(1), translation_.z);
}
//----
template<typename T>
void tform3<T>::set(const vec3<T> &x_, const vec3<T> &y_, const vec3<T> &z_)
{
// setup transform from 3 axis and translation
x.set(x_, 0);
y.set(y_, 0);
z.set(z_, 0);
}
//----
template<typename T>
void tform3<T>::set(const vec3<T> &x_, const vec3<T> &y_, const vec3<T> &z_, const vec3<T> &translation_)
{
// setup transform from 3 axis and translation
x.set(x_, translation_.x);
y.set(y_, translation_.y);
z.set(z_, translation_.z);
}
//----
template<typename T>
void tform3<T>::set(const vec4<T> &x_, const vec4<T> &y_, const vec4<T> &z_)
{
// setup transform from 3 transform vectors
x=x_;
y=y_;
z=z_;
}
//----
template<typename T>
void tform3<T>::set(scalar_t xx_, scalar_t xy_, scalar_t xz_, scalar_t xtrans_,
scalar_t yx_, scalar_t yy_, scalar_t yz_, scalar_t ytrans_,
scalar_t zx_, scalar_t zy_, scalar_t zz_, scalar_t ztrans_)
{
// setup transform from scalars
x.set(xx_, xy_, xz_, xtrans_);
y.set(yx_, yy_, yz_, ytrans_);
z.set(zx_, zy_, zz_, ztrans_);
}
//----
template<typename T>
void tform3<T>::set(const mat33<T> &m_)
{
// setup transform from affine matrix
x.set(m_.x, 0);
y.set(m_.y, 0);
z.set(m_.z, 0);
}
//----
template<typename T>
void tform3<T>::set(const mat33<T> &m_, const vec3<T> &translation_)
{
// setup transform from affine matrix and translation
x.set(m_.x, translation_.x);
y.set(m_.y, translation_.y);
z.set(m_.z, translation_.z);
}
//----
template<typename T>
void tform3<T>::set(const quat<T> &q_)
{
// setup transform from quaternion rotation
mat33<T> m;
convert(m, q_);
x.set(m.x, 0);
y.set(m.y, 0);
z.set(m.z, 0);
}
//----
template<typename T>
void tform3<T>::set(const quat<T> &q_, const vec3<T> &translation_)
{
// setup transform from quaternion rotation and translation
mat33<T> m;
convert(m, q_);
x.set(m.x, translation_.x);
y.set(m.y, translation_.y);
z.set(m.z, translation_.z);
}
//----
template<typename T>
void tform3<T>::set(const tform_rt3<T> &t_)
{
// setup transform from tform_rt3
mat33<T> m;
convert(m, t_.rotation);
x.set(m.x, t_.translation.x);
y.set(m.y, t_.translation.y);
z.set(m.z, t_.translation.z);
}
//----
template<typename T>
void tform3<T>::set_rotation(const mat33<T> &r_)
{
// set rotation part of the transform
x.x=r_.x.x; x.y=r_.x.y; x.z=r_.x.z;
y.x=r_.y.x; y.y=r_.y.y; y.z=r_.y.z;
z.x=r_.z.x; z.y=r_.z.y; z.z=r_.z.z;
}
//----
template<typename T>
void tform3<T>::set_translation(const vec3<T> &t_)
{
// set translation part of the transform
x.w=t_.x;
y.w=t_.y;
z.w=t_.z;
}
//----------------------------------------------------------------------------
template<typename T>
EGL_INLINE bool is_zero(const tform3<T> &t_)
{
// test for zero-transform
return is_zero(t_.x) && is_zero(t_.y) && is_zero(t_.z);
}
//----
template<typename T>
EGL_INLINE bool is_orthogonal(const tform3<T> &t_)
{
// test for orthogonal transform
return t_.x.x*t_.y.x+t_.x.y*t_.y.y+t_.x.z*t_.y.z<0.0001f
&& t_.x.x*t_.z.x+t_.x.y*t_.z.y+t_.x.z*t_.z.z<0.0001f
&& t_.y.x*t_.z.x+t_.y.y*t_.z.y+t_.y.z*t_.z.z<0.0001f;
}
//----
template<typename T>
EGL_INLINE bool is_sat(const tform3<T> &t_)
{
// test for saturated transform
return is_sat(t_.x) && is_sat(t_.y) && is_sat(t_.z);
}
//----
template<typename T>
EGL_INLINE bool is_ssat(const tform3<T> &t_)
{
// test for signed saturated transform
return is_ssat(t_.x) && is_ssat(t_.y) && is_ssat(t_.z);
}
//----
template<typename T>
EGL_INLINE bool operator==(const tform3<T> &t0_, const tform3<T> &t1_)
{
// test for equality of transforms
return t0_.x==t1_.x
&& t0_.y==t1_.y
&& t0_.z==t1_.z;
}
//----
template<typename T>
EGL_INLINE bool operator!=(const tform3<T> &t0_, const tform3<T> &t1_)
{
// test for inequality of transforms
return t0_.x!=t1_.x
|| t0_.y!=t1_.y
|| t0_.z!=t1_.z;
}
//----
template<typename T>
EGL_INLINE void operator+=(tform3<T> &tr_, const tform3<T> &t_)
{
// add transform to transform
tr_.x+=t_.x;
tr_.y+=t_.y;
tr_.z+=t_.z;
}
//----
template<typename T>
EGL_INLINE void operator-=(tform3<T> &tr_, const tform3<T> &t_)
{
// subtract transform from transform
tr_.x-=t_.x;
tr_.y-=t_.y;
tr_.z-=t_.z;
}
//----
template<typename T>
EGL_INLINE void operator*=(tform3<T> &tr_, const tform3<T> &t_)
{
// multiply transform by transform
const vec3<T> p(tr_.x.w, tr_.y.w, tr_.z.w);
vec4<T> v=tr_.x;
tr_.x.x=v.x*t_.x.x+v.y*t_.y.x+v.z*t_.z.x; tr_.x.y=v.x*t_.x.y+v.y*t_.y.y+v.z*t_.z.y; tr_.x.z=v.x*t_.x.z+v.y*t_.y.z+v.z*t_.z.z; tr_.x.w=p.x*t_.x.x+p.y*t_.y.x+p.z*t_.z.x+t_.x.w;
v=tr_.y;
tr_.y.x=v.x*t_.x.x+v.y*t_.y.x+v.z*t_.z.x; tr_.y.y=v.x*t_.x.y+v.y*t_.y.y+v.z*t_.z.y; tr_.y.z=v.x*t_.x.z+v.y*t_.y.z+v.z*t_.z.z; tr_.y.w=p.x*t_.x.y+p.y*t_.y.y+p.z*t_.z.y+t_.y.w;
v=tr_.z;
tr_.z.x=v.x*t_.x.x+v.y*t_.y.x+v.z*t_.z.x; tr_.z.y=v.x*t_.x.y+v.y*t_.y.y+v.z*t_.z.y; tr_.z.z=v.x*t_.x.z+v.y*t_.y.z+v.z*t_.z.z; tr_.z.w=p.x*t_.x.z+p.y*t_.y.z+p.z*t_.z.z+t_.z.w;
}
//----
template<typename T>
EGL_INLINE void operator*=(tform3<T> &tr_, const quat<T> &q_)
{
// multiply transform by quaternion
vec3<T> p(tr_.x.w, tr_.y.w, tr_.z.w);
p*=q_;
reinterpret_cast<vec3<T>&>(tr_.x)*=q_; tr_.x.w=p.x;
reinterpret_cast<vec3<T>&>(tr_.y)*=q_; tr_.y.w=p.y;
reinterpret_cast<vec3<T>&>(tr_.z)*=q_; tr_.z.w=p.z;
}
//----
template<typename T>
EGL_INLINE void operator*=(tform3<T> &tr_, const mat33<T> &m_)
{
// multiply transform by matrix
vec3<T> p(tr_.x.w, tr_.y.w, tr_.z.w);
p*=m_;
reinterpret_cast<vec3<T>&>(tr_.x)*=m_; tr_.x.w=p.x;
reinterpret_cast<vec3<T>&>(tr_.y)*=m_; tr_.y.w=p.y;
reinterpret_cast<vec3<T>&>(tr_.z)*=m_; tr_.z.w=p.z;
}
//----
template<typename T>
EGL_INLINE void operator*=(tform3<T> &tr_, typename math<T>::scalar_t s_)
{
// multiply transform by scalar
tr_.x*=s_;
tr_.y*=s_;
tr_.z*=s_;
}
//----
template<typename T>
EGL_INLINE void operator*=(vec3<T> &vr_, const tform3<T> &t_)
{
// multiply vector by transform (assume w=1)
vec3<T> v(vr_);
vr_.x=v.x*t_.x.x+v.y*t_.y.x+v.z*t_.z.x+t_.x.w;
vr_.y=v.x*t_.x.y+v.y*t_.y.y+v.z*t_.z.y+t_.y.w;
vr_.z=v.x*t_.x.z+v.y*t_.y.z+v.z*t_.z.z+t_.z.w;
}
//----
template<typename T>
EGL_INLINE void operator*=(vec4<T> &vr_, const tform3<T> &t_)
{
// multiply vector by transform
vec4<T> v(vr_);
vr_.x=v.x*t_.x.x+v.y*t_.y.x+v.z*t_.z.x+v.w*t_.x.w;
vr_.y=v.x*t_.x.y+v.y*t_.y.y+v.z*t_.z.y+v.w*t_.y.w;
vr_.z=v.x*t_.x.z+v.y*t_.y.z+v.z*t_.z.z+v.w*t_.z.w;
}
//----
template<typename T>
EGL_INLINE void operator*=(mat44<T> &mr_, const tform3<T> &t_)
{
// multiply 4x4 matrix by transform
vec4<T> v=mr_.x;
mr_.x.x=v.x*t_.x.x+v.y*t_.y.x+v.z*t_.z.x+v.w*t_.x.w; mr_.x.y=v.x*t_.x.y+v.y*t_.y.y+v.z*t_.z.y+v.w*t_.y.w; mr_.x.z=v.x*t_.x.z+v.y*t_.y.z+v.z*t_.z.z+v.w*t_.z.w; mr_.x.w=v.w;
v=mr_.y;
mr_.y.x=v.x*t_.x.x+v.y*t_.y.x+v.z*t_.z.x+v.w*t_.x.w; mr_.y.y=v.x*t_.x.y+v.y*t_.y.y+v.z*t_.z.y+v.w*t_.y.w; mr_.y.z=v.x*t_.x.z+v.y*t_.y.z+v.z*t_.z.z+v.w*t_.z.w; mr_.y.w=v.w;
v=mr_.z;
mr_.z.x=v.x*t_.x.x+v.y*t_.y.x+v.z*t_.z.x+v.w*t_.x.w; mr_.z.y=v.x*t_.x.y+v.y*t_.y.y+v.z*t_.z.y+v.w*t_.y.w; mr_.z.z=v.x*t_.x.z+v.y*t_.y.z+v.z*t_.z.z+v.w*t_.z.w; mr_.z.w=v.w;
v=mr_.w;
mr_.w.x=v.x*t_.x.x+v.y*t_.y.x+v.z*t_.z.x+v.w*t_.x.w; mr_.w.y=v.x*t_.x.y+v.y*t_.y.y+v.z*t_.z.y+v.w*t_.y.w; mr_.w.z=v.x*t_.x.z+v.y*t_.y.z+v.z*t_.z.z+v.w*t_.z.w; mr_.w.w=v.w;
}
//----
template<typename T>
EGL_INLINE void operator/=(tform3<T> &tr_, typename math<T>::scalar_t s_)
{
// divide transform by scalar
typename math<T>::scalar_t rs=rcp(s_);
tr_.x*=rs;
tr_.y*=rs;
tr_.z*=rs;
}
//----
template<typename T>
EGL_INLINE tform3<T> operator+(const tform3<T> &t0_, const tform3<T> &t1_)
{
// add transform to transform
return tform3<T>(t0_.x+t1_.x,
t0_.y+t1_.y,
t0_.z+t1_.z);
}
//----
template<typename T>
EGL_INLINE tform3<T> operator-(const tform3<T> &t0_, const tform3<T> &t1_)
{
// subtract transform from transform
return tform3<T>(t0_.x-t1_.x,
t0_.y-t1_.y,
t0_.z-t1_.z);
}
//----
template<typename T>
EGL_INLINE tform3<T> operator-(const tform3<T> &t_)
{
// negate transform
return tform3<T>(-t_.x, -t_.y, -t_.z);
}
//----
template<typename T>
EGL_INLINE tform3<T> operator*(const tform3<T> &t0_, const tform3<T> &t1_)
{
// multiply transform by transform
return tform3<T>(t0_.x.x*t1_.x.x+t0_.x.y*t1_.y.x+t0_.x.z*t1_.z.x, t0_.x.x*t1_.x.y+t0_.x.y*t1_.y.y+t0_.x.z*t1_.z.y, t0_.x.x*t1_.x.z+t0_.x.y*t1_.y.z+t0_.x.z*t1_.z.z, t0_.x.w*t1_.x.x+t0_.y.w*t1_.y.x+t0_.z.w*t1_.z.x+t1_.x.w,
t0_.y.x*t1_.x.x+t0_.y.y*t1_.y.x+t0_.y.z*t1_.z.x, t0_.y.x*t1_.x.y+t0_.y.y*t1_.y.y+t0_.y.z*t1_.z.y, t0_.y.x*t1_.x.z+t0_.y.y*t1_.y.z+t0_.y.z*t1_.z.z, t0_.x.w*t1_.x.y+t0_.y.w*t1_.y.y+t0_.z.w*t1_.z.y+t1_.y.w,
t0_.z.x*t1_.x.x+t0_.z.y*t1_.y.x+t0_.z.z*t1_.z.x, t0_.z.x*t1_.x.y+t0_.z.y*t1_.y.y+t0_.z.z*t1_.z.y, t0_.z.x*t1_.x.z+t0_.z.y*t1_.y.z+t0_.z.z*t1_.z.z, t0_.x.w*t1_.x.z+t0_.y.w*t1_.y.z+t0_.z.w*t1_.z.z+t1_.z.w);
}
//----
template<typename T>
EGL_INLINE tform3<T> operator*(const tform3<T> &t_, const quat<T> &q_)
{
// multiply transform by quaternion
return tform3<T>(reinterpret_cast<const vec3<T>&>(t_.x)*q_,
reinterpret_cast<const vec3<T>&>(t_.y)*q_,
reinterpret_cast<const vec3<T>&>(t_.z)*q_,
vec3<T>(t_.x.w, t_.y.w, t_.z.w)*q_);
}
//----
template<typename T>
EGL_INLINE tform3<T> operator*(const tform3<T> &t_, const mat33<T> &m_)
{
// multiply transform by matrix
return tform3<T>(reinterpret_cast<const vec3<T>&>(t_.x)*m_,
reinterpret_cast<const vec3<T>&>(t_.y)*m_,
reinterpret_cast<const vec3<T>&>(t_.z)*m_,
vec3<T>(t_.x.w, t_.y.w, t_.z.w)*m_);
}
//----
template<typename T>
EGL_INLINE tform3<T> operator*(const tform3<T> &t_, typename math<T>::scalar_t s_)
{
// multiply transform by scalar
return tform3<T>(t_.x*s_, t_.y*s_, t_.z*s_);
}
//----
template<typename T>
EGL_INLINE tform3<T> operator*(typename math<T>::scalar_t s_, const tform3<T> &t_)
{
// multiply transform by scalar
return tform3<T>(t_.x*s_, t_.y*s_, t_.z*s_);
}
//----
template<typename T>
EGL_INLINE vec3<T> operator*(const vec3<T> &v_, const tform3<T> &t_)
{
// multiply 3d vector by transform (assume vector w=1)
return vec3<T>(v_.x*t_.x.x+v_.y*t_.y.x+v_.z*t_.z.x+t_.x.w,
v_.x*t_.x.y+v_.y*t_.y.y+v_.z*t_.z.y+t_.y.w,
v_.x*t_.x.z+v_.y*t_.y.z+v_.z*t_.z.z+t_.z.w);
}
//----
template<typename T>
EGL_INLINE vec4<T> operator*(const vec4<T> &v_, const tform3<T> &t_)
{
// multiply 4d vector by transform
return vec4<T>(v_.x*t_.x.x+v_.y*t_.y.x+v_.z*t_.z.x+v_.w*t_.x.w,
v_.x*t_.x.y+v_.y*t_.y.y+v_.z*t_.z.y+v_.w*t_.y.w,
v_.x*t_.x.z+v_.y*t_.y.z+v_.z*t_.z.z+v_.w*t_.z.w,
v_.w);
}
//----
template<typename T>
EGL_INLINE vec3<T> operator*(const tform3<T> &t_, const vec3<T> &v_)
{
// multiply transform by transposed 3d vector
return vec3<T>(v_.x*t_.x.x+v_.y*t_.x.y+v_.z*t_.x.z,
v_.x*t_.y.x+v_.y*t_.y.y+v_.z*t_.y.z,
v_.x*t_.z.x+v_.y*t_.z.y+v_.z*t_.z.z);