Project

General

Profile

OPENXR Scol plugin
vrmaths.h
1
2#ifndef __VRMATHS_H__
3#define __VRMATHS_H__
4
5#include "xrmath.h"
6
7#define VR_PI 3.14159265358979323846f
8
9static inline float vrRadiansFromDegrees(float deg)
10{
11 return deg * VR_PI / 180.0f;
12}
13
14static inline float vrDegreesFromRadians(float rad)
15{
16 return rad * 180.f / VR_PI;
17}
18
19class Matrix3;
20class Quaternion;
21
23{
24public:
25 int x, y;
26
27public:
28 static const Vector2i ZERO;
29
30 inline Vector2i()
31 {
32 }
33
34 inline Vector2i(const XrVector2f vec)
35 {
36 x = (int)vec.x;
37 y = (int)vec.y;
38 }
39
40 inline Vector2i(const int fX, const int fY)
41 : x(fX), y(fY) {}
42
43 inline explicit Vector2i(int* const r)
44 : x(r[0]), y(r[1]) {}
45
46 inline explicit Vector2i(const int scaler)
47 : x(scaler)
48 , y(scaler) {}
49
50 inline int operator [] (const size_t i) const
51 {
52 return *(&x + i);
53 }
54
55 inline int& operator [] (const size_t i)
56 {
57 return *(&x + i);
58 }
60 inline int* ptr()
61 {
62 return &x;
63 }
65 inline const int* ptr() const
66 {
67 return &x;
68 }
69
70 inline Vector2i& operator = (const Vector2i& rkVector)
71 {
72 x = rkVector.x;
73 y = rkVector.y;
74
75 return *this;
76 }
77
78 inline Vector2i& operator = (const int fScaler)
79 {
80 x = fScaler;
81 y = fScaler;
82
83 return *this;
84 }
85
86 inline bool operator == (const Vector2i& rkVector) const
87 {
88 return (x == rkVector.x && y == rkVector.y);
89 }
90
91 inline bool operator != (const Vector2i& rkVector) const
92 {
93 return (x != rkVector.x || y != rkVector.y);
94 }
95
96 // arithmetic operations
97 inline Vector2i operator + (const Vector2i& rkVector) const
98 {
99 return Vector2i(
100 x + rkVector.x,
101 y + rkVector.y);
102 }
103
104 inline Vector2i operator - (const Vector2i& rkVector) const
105 {
106 return Vector2i(
107 x - rkVector.x,
108 y - rkVector.y);
109 }
110
111 inline Vector2i operator * (const int fScalar) const
112 {
113 return Vector2i(
114 x * fScalar,
115 y * fScalar);
116 }
117
118 inline Vector2i operator * (const Vector2i& rhs) const
119 {
120 return Vector2i(
121 x * rhs.x,
122 y * rhs.y);
123 }
124
125 inline Vector2i operator / (const int fScalar) const
126 {
127 float fInv = 1.0f / fScalar;
128
129 return Vector2i(
130 (int)((float)x * fInv),
131 (int)((float)y * fInv));
132 }
133
134 inline Vector2i operator / (const Vector2i& rhs) const
135 {
136 return Vector2i(
137 x / rhs.x,
138 y / rhs.y);
139 }
140
141 inline const Vector2i& operator + () const
142 {
143 return *this;
144 }
145
146 inline Vector2i operator - () const
147 {
148 return Vector2i(-x, -y);
149 }
150
151 // overloaded operators to help Vector2
152 inline friend Vector2i operator * (const int fScalar, const Vector2i& rkVector)
153 {
154 return Vector2i(
155 fScalar * rkVector.x,
156 fScalar * rkVector.y);
157 }
158
159 inline friend Vector2i operator / (const int fScalar, const Vector2i& rkVector)
160 {
161 return Vector2i(
162 fScalar / rkVector.x,
163 fScalar / rkVector.y);
164 }
165
166 inline friend Vector2i operator + (const Vector2i& lhs, const int rhs)
167 {
168 return Vector2i(
169 lhs.x + rhs,
170 lhs.y + rhs);
171 }
172
173 inline friend Vector2i operator + (const int lhs, const Vector2i& rhs)
174 {
175 return Vector2i(
176 lhs + rhs.x,
177 lhs + rhs.y);
178 }
179
180 inline friend Vector2i operator - (const Vector2i& lhs, const int rhs)
181 {
182 return Vector2i(
183 lhs.x - rhs,
184 lhs.y - rhs);
185 }
186
187 inline friend Vector2i operator - (const int lhs, const Vector2i& rhs)
188 {
189 return Vector2i(
190 lhs - rhs.x,
191 lhs - rhs.y);
192 }
193
194 // arithmetic updates
195 inline Vector2i& operator += (const Vector2i& rkVector)
196 {
197 x += rkVector.x;
198 y += rkVector.y;
199
200 return *this;
201 }
202
203 inline Vector2i& operator += (const int fScalar)
204 {
205 x += fScalar;
206 y += fScalar;
207 return *this;
208 }
209
210 inline Vector2i& operator -= (const Vector2i& rkVector)
211 {
212 x -= rkVector.x;
213 y -= rkVector.y;
214
215 return *this;
216 }
217
218 inline Vector2i& operator -= (const int fScalar)
219 {
220 x -= fScalar;
221 y -= fScalar;
222 return *this;
223 }
224
225 inline Vector2i& operator *= (const int fScalar)
226 {
227 x *= fScalar;
228 y *= fScalar;
229 return *this;
230 }
231
232 inline Vector2i& operator *= (const Vector2i& rkVector)
233 {
234 x *= rkVector.x;
235 y *= rkVector.y;
236
237 return *this;
238 }
239
240 inline Vector2i& operator /= (const Vector2i& rkVector)
241 {
242 x /= rkVector.x;
243 y /= rkVector.y;
244
245 return *this;
246 }
247};
248
250{
251public:
252 float x, y;
253
254public:
255 static const Vector2 ZERO;
256
257 inline Vector2()
258 {
259 }
260
261 inline Vector2(const XrVector2f vec)
262 {
263 x = (float)vec.x;
264 y = (float)vec.y;
265 }
266
267 inline Vector2(const float fX, const float fY)
268 : x(fX), y(fY) {}
269
270 inline explicit Vector2(float* const r)
271 : x(r[0]), y(r[1]) {}
272
273 inline explicit Vector2(const float scaler)
274 : x(scaler)
275 , y(scaler) {}
276
277 inline float operator [] (const size_t i) const
278 {
279 return *(&x + i);
280 }
281
282 inline float& operator [] (const size_t i)
283 {
284 return *(&x + i);
285 }
287 inline float* ptr()
288 {
289 return &x;
290 }
292 inline const float* ptr() const
293 {
294 return &x;
295 }
296
297 inline Vector2& operator = (const Vector2& rkVector)
298 {
299 x = rkVector.x;
300 y = rkVector.y;
301
302 return *this;
303 }
304
305 inline Vector2& operator = (const float fScaler)
306 {
307 x = fScaler;
308 y = fScaler;
309
310 return *this;
311 }
312
313 inline bool operator == (const Vector2& rkVector) const
314 {
315 return (x == rkVector.x && y == rkVector.y);
316 }
317
318 inline bool operator != (const Vector2& rkVector) const
319 {
320 return (x != rkVector.x || y != rkVector.y);
321 }
322
323 // arithmetic operations
324 inline Vector2 operator + (const Vector2& rkVector) const
325 {
326 return Vector2(
327 x + rkVector.x,
328 y + rkVector.y);
329 }
330
331 inline Vector2 operator - (const Vector2& rkVector) const
332 {
333 return Vector2(
334 x - rkVector.x,
335 y - rkVector.y);
336 }
337
338 inline Vector2 operator * (const float fScalar) const
339 {
340 return Vector2(
341 x * fScalar,
342 y * fScalar);
343 }
344
345 inline Vector2 operator * (const Vector2& rhs) const
346 {
347 return Vector2(
348 x * rhs.x,
349 y * rhs.y);
350 }
351
352 inline Vector2 operator / (const float fScalar) const
353 {
354 float fInv = 1.0f / fScalar;
355
356 return Vector2(
357 x * fInv,
358 y * fInv);
359 }
360
361 inline Vector2 operator / (const Vector2& rhs) const
362 {
363 return Vector2(
364 x / rhs.x,
365 y / rhs.y);
366 }
367
368 inline const Vector2& operator + () const
369 {
370 return *this;
371 }
372
373 inline Vector2 operator - () const
374 {
375 return Vector2(-x, -y);
376 }
377
378 // overloaded operators to help Vector2
379 inline friend Vector2 operator * (const float fScalar, const Vector2& rkVector)
380 {
381 return Vector2(
382 fScalar * rkVector.x,
383 fScalar * rkVector.y);
384 }
385
386 inline friend Vector2 operator / (const float fScalar, const Vector2& rkVector)
387 {
388 return Vector2(
389 fScalar / rkVector.x,
390 fScalar / rkVector.y);
391 }
392
393 inline friend Vector2 operator + (const Vector2& lhs, const float rhs)
394 {
395 return Vector2(
396 lhs.x + rhs,
397 lhs.y + rhs);
398 }
399
400 inline friend Vector2 operator + (const float lhs, const Vector2& rhs)
401 {
402 return Vector2(
403 lhs + rhs.x,
404 lhs + rhs.y);
405 }
406
407 inline friend Vector2 operator - (const Vector2& lhs, const float rhs)
408 {
409 return Vector2(
410 lhs.x - rhs,
411 lhs.y - rhs);
412 }
413
414 inline friend Vector2 operator - (const float lhs, const Vector2& rhs)
415 {
416 return Vector2(
417 lhs - rhs.x,
418 lhs - rhs.y);
419 }
420
421 // arithmetic updates
422 inline Vector2& operator += (const Vector2& rkVector)
423 {
424 x += rkVector.x;
425 y += rkVector.y;
426
427 return *this;
428 }
429
430 inline Vector2& operator += (const float fScalar)
431 {
432 x += fScalar;
433 y += fScalar;
434 return *this;
435 }
436
437 inline Vector2& operator -= (const Vector2& rkVector)
438 {
439 x -= rkVector.x;
440 y -= rkVector.y;
441
442 return *this;
443 }
444
445 inline Vector2& operator -= (const float fScalar)
446 {
447 x -= fScalar;
448 y -= fScalar;
449 return *this;
450 }
451
452 inline Vector2& operator *= (const float fScalar)
453 {
454 x *= fScalar;
455 y *= fScalar;
456 return *this;
457 }
458
459 inline Vector2& operator *= (const Vector2& rkVector)
460 {
461 x *= rkVector.x;
462 y *= rkVector.y;
463
464 return *this;
465 }
466
467 inline Vector2& operator /= (const float fScalar)
468 {
469 float fInv = 1.0f / fScalar;
470
471 x *= fInv;
472 y *= fInv;
473
474 return *this;
475 }
476
477 inline Vector2& operator /= (const Vector2& rkVector)
478 {
479 x /= rkVector.x;
480 y /= rkVector.y;
481
482 return *this;
483 }
484};
485
487{
488public:
489 float x, y, z;
490
491public:
492 static const Vector3 ZERO;
493
494 inline Vector3()
495 {
496 }
497
498 inline Vector3(const XrVector3f vec)
499 {
500 x = (float)vec.x;
501 y = (float)vec.y;
502 z = (float)vec.z;
503 }
504
505 inline Vector3(const float fX, const float fY, const float fZ)
506 : x(fX), y(fY), z(fZ) {}
507
508 inline explicit Vector3(const float afCoordinate[3])
509 : x(afCoordinate[0]),
510 y(afCoordinate[1]),
511 z(afCoordinate[2]) {}
512
513 inline explicit Vector3(const int afCoordinate[3])
514 {
515 x = (float)afCoordinate[0];
516 y = (float)afCoordinate[1];
517 z = (float)afCoordinate[2];
518 }
519
520 inline explicit Vector3(float* const r)
521 : x(r[0]), y(r[1]), z(r[2]) {}
522
523 inline explicit Vector3(const float scaler)
524 : x(scaler)
525 , y(scaler)
526 , z(scaler) {}
527
528 inline float operator [] (const size_t i) const
529 {
530 return *(&x + i);
531 }
532
533 inline float& operator [] (const size_t i)
534 {
535 return *(&x + i);
536 }
538 inline float* ptr()
539 {
540 return &x;
541 }
543 inline const float* ptr() const
544 {
545 return &x;
546 }
547
548 inline Vector3& operator = (const Vector3& rkVector)
549 {
550 x = rkVector.x;
551 y = rkVector.y;
552 z = rkVector.z;
553
554 return *this;
555 }
556
557 inline Vector3& operator = (const float fScaler)
558 {
559 x = fScaler;
560 y = fScaler;
561 z = fScaler;
562
563 return *this;
564 }
565
566 inline bool operator == (const Vector3& rkVector) const
567 {
568 return (x == rkVector.x && y == rkVector.y && z == rkVector.z);
569 }
570
571 inline bool operator != (const Vector3& rkVector) const
572 {
573 return (x != rkVector.x || y != rkVector.y || z != rkVector.z);
574 }
575
576 // arithmetic operations
577 inline Vector3 operator + (const Vector3& rkVector) const
578 {
579 return Vector3(
580 x + rkVector.x,
581 y + rkVector.y,
582 z + rkVector.z);
583 }
584
585 inline Vector3 operator - (const Vector3& rkVector) const
586 {
587 return Vector3(
588 x - rkVector.x,
589 y - rkVector.y,
590 z - rkVector.z);
591 }
592
593 inline Vector3 operator * (const float fScalar) const
594 {
595 return Vector3(
596 x * fScalar,
597 y * fScalar,
598 z * fScalar);
599 }
600
601 inline Vector3 operator * (const Vector3& rhs) const
602 {
603 return Vector3(
604 x * rhs.x,
605 y * rhs.y,
606 z * rhs.z);
607 }
608
609 inline Vector3 operator / (const float fScalar) const
610 {
611 float fInv = 1.0f / fScalar;
612
613 return Vector3(
614 x * fInv,
615 y * fInv,
616 z * fInv);
617 }
618
619 inline Vector3 operator / (const Vector3& rhs) const
620 {
621 return Vector3(
622 x / rhs.x,
623 y / rhs.y,
624 z / rhs.z);
625 }
626
627 inline const Vector3& operator + () const
628 {
629 return *this;
630 }
631
632 inline Vector3 operator - () const
633 {
634 return Vector3(-x, -y, -z);
635 }
636
637 // overloaded operators to help Vector3
638 inline friend Vector3 operator * (const float fScalar, const Vector3& rkVector)
639 {
640 return Vector3(
641 fScalar * rkVector.x,
642 fScalar * rkVector.y,
643 fScalar * rkVector.z);
644 }
645
646 inline friend Vector3 operator / (const float fScalar, const Vector3& rkVector)
647 {
648 return Vector3(
649 fScalar / rkVector.x,
650 fScalar / rkVector.y,
651 fScalar / rkVector.z);
652 }
653
654 inline friend Vector3 operator + (const Vector3& lhs, const float rhs)
655 {
656 return Vector3(
657 lhs.x + rhs,
658 lhs.y + rhs,
659 lhs.z + rhs);
660 }
661
662 inline friend Vector3 operator + (const float lhs, const Vector3& rhs)
663 {
664 return Vector3(
665 lhs + rhs.x,
666 lhs + rhs.y,
667 lhs + rhs.z);
668 }
669
670 inline friend Vector3 operator - (const Vector3& lhs, const float rhs)
671 {
672 return Vector3(
673 lhs.x - rhs,
674 lhs.y - rhs,
675 lhs.z - rhs);
676 }
677
678 inline friend Vector3 operator - (const float lhs, const Vector3& rhs)
679 {
680 return Vector3(
681 lhs - rhs.x,
682 lhs - rhs.y,
683 lhs - rhs.z);
684 }
685
686 // arithmetic updates
687 inline Vector3& operator += (const Vector3& rkVector)
688 {
689 x += rkVector.x;
690 y += rkVector.y;
691 z += rkVector.z;
692
693 return *this;
694 }
695
696 inline Vector3& operator += (const float fScalar)
697 {
698 x += fScalar;
699 y += fScalar;
700 z += fScalar;
701 return *this;
702 }
703
704 inline Vector3& operator -= (const Vector3& rkVector)
705 {
706 x -= rkVector.x;
707 y -= rkVector.y;
708 z -= rkVector.z;
709
710 return *this;
711 }
712
713 inline Vector3& operator -= (const float fScalar)
714 {
715 x -= fScalar;
716 y -= fScalar;
717 z -= fScalar;
718 return *this;
719 }
720
721 inline Vector3& operator *= (const float fScalar)
722 {
723 x *= fScalar;
724 y *= fScalar;
725 z *= fScalar;
726 return *this;
727 }
728
729 inline Vector3& operator *= (const Vector3& rkVector)
730 {
731 x *= rkVector.x;
732 y *= rkVector.y;
733 z *= rkVector.z;
734
735 return *this;
736 }
737
738 inline Vector3& operator /= (const float fScalar)
739 {
740 float fInv = 1.0f / fScalar;
741
742 x *= fInv;
743 y *= fInv;
744 z *= fInv;
745
746 return *this;
747 }
748
749 inline Vector3& operator /= (const Vector3& rkVector)
750 {
751 x /= rkVector.x;
752 y /= rkVector.y;
753 z /= rkVector.z;
754
755 return *this;
756 }
757
758 inline Vector3 crossProduct(const Vector3& rkVector) const
759 {
760 return Vector3(
761 y * rkVector.z - z * rkVector.y,
762 z * rkVector.x - x * rkVector.z,
763 x * rkVector.y - y * rkVector.x);
764 }
765
766 inline float dotProduct(const Vector3& vec) const
767 {
768 float ret = 0.0f;
769 for (int i = 0; i < 3; i++)
770 ret += ptr()[i] * vec.ptr()[i];
771 return ret;
772 }
773
774 inline float squaredLength() const { return dotProduct(*this); }
775
776 inline float length() const { return std::sqrt(squaredLength()); }
777
778 float distance(const Vector3& rhs) const
779 {
780 return (*this - rhs).length();
781 }
782};
783
785{
786public:
787 float x, y, z, w;
788
789public:
790 static const Vector4 ZERO;
791
792 inline Vector4()
793 {
794 }
795
796 inline Vector4(const float fX, const float fY, const float fZ, const float fW)
797 : x(fX), y(fY), z(fZ), w(fW) {}
798
799 inline explicit Vector4(const float afCoordinate[4])
800 : x(afCoordinate[0]),
801 y(afCoordinate[1]),
802 z(afCoordinate[2]),
803 w(afCoordinate[3]) {}
804
805 inline explicit Vector4(const int afCoordinate[4])
806 {
807 x = (float)afCoordinate[0];
808 y = (float)afCoordinate[1];
809 z = (float)afCoordinate[2];
810 w = (float)afCoordinate[3];
811 }
812
813 inline explicit Vector4(float* const r)
814 : x(r[0]), y(r[1]), z(r[2]), w(r[3]) {}
815
816 inline explicit Vector4(const float scaler)
817 : x(scaler)
818 , y(scaler)
819 , z(scaler)
820 , w(scaler) {}
821
822 inline float operator [] (const size_t i) const
823 {
824 return *(&x + i);
825 }
826
827 inline float& operator [] (const size_t i)
828 {
829 return *(&x + i);
830 }
832 inline float* ptr()
833 {
834 return &x;
835 }
837 inline const float* ptr() const
838 {
839 return &x;
840 }
841
842 inline Vector4& operator = (const Vector4& rkVector)
843 {
844 x = rkVector.x;
845 y = rkVector.y;
846 z = rkVector.z;
847 w = rkVector.w;
848
849 return *this;
850 }
851
852 inline Vector4& operator = (const float fScaler)
853 {
854 x = fScaler;
855 y = fScaler;
856 z = fScaler;
857 w = fScaler;
858
859 return *this;
860 }
861
862 inline bool operator == (const Vector4& rkVector) const
863 {
864 return (x == rkVector.x && y == rkVector.y && z == rkVector.z);
865 }
866
867 inline bool operator != (const Vector4& rkVector) const
868 {
869 return (x != rkVector.x || y != rkVector.y || z != rkVector.z || w != rkVector.w);
870 }
871
872 // arithmetic operations
873 inline Vector4 operator + (const Vector4& rkVector) const
874 {
875 return Vector4(
876 x + rkVector.x,
877 y + rkVector.y,
878 z + rkVector.z,
879 w + rkVector.w);
880 }
881
882 inline Vector4 operator - (const Vector4& rkVector) const
883 {
884 return Vector4(
885 x - rkVector.x,
886 y - rkVector.y,
887 z - rkVector.z,
888 w - rkVector.w);
889 }
890
891 inline Vector4 operator * (const float fScalar) const
892 {
893 return Vector4(
894 x * fScalar,
895 y * fScalar,
896 z * fScalar,
897 w * fScalar);
898 }
899
900 inline Vector4 operator * (const Vector4& rhs) const
901 {
902 return Vector4(
903 x * rhs.x,
904 y * rhs.y,
905 z * rhs.z,
906 w * rhs.w);
907 }
908
909 inline Vector4 operator / (const float fScalar) const
910 {
911 float fInv = 1.0f / fScalar;
912
913 return Vector4(
914 x * fInv,
915 y * fInv,
916 z * fInv,
917 w * fInv);
918 }
919
920 inline Vector4 operator / (const Vector4& rhs) const
921 {
922 return Vector4(
923 x / rhs.x,
924 y / rhs.y,
925 z / rhs.z,
926 z / rhs.w);
927 }
928
929 inline const Vector4& operator + () const
930 {
931 return *this;
932 }
933
934 inline Vector4 operator - () const
935 {
936 return Vector4(-x, -y, -z, -w);
937 }
938
939 // overloaded operators to help Vector3
940 inline friend Vector4 operator * (const float fScalar, const Vector4& rkVector)
941 {
942 return Vector4(
943 fScalar * rkVector.x,
944 fScalar * rkVector.y,
945 fScalar * rkVector.z,
946 fScalar * rkVector.w);
947 }
948
949 inline friend Vector4 operator / (const float fScalar, const Vector4& rkVector)
950 {
951 return Vector4(
952 fScalar / rkVector.x,
953 fScalar / rkVector.y,
954 fScalar / rkVector.z,
955 fScalar / rkVector.w);
956 }
957
958 inline friend Vector4 operator + (const Vector4& lhs, const float rhs)
959 {
960 return Vector4(
961 lhs.x + rhs,
962 lhs.y + rhs,
963 lhs.z + rhs,
964 lhs.w + rhs);
965 }
966
967 inline friend Vector4 operator + (const float lhs, const Vector4& rhs)
968 {
969 return Vector4(
970 lhs + rhs.x,
971 lhs + rhs.y,
972 lhs + rhs.z,
973 lhs + rhs.w);
974 }
975
976 inline friend Vector4 operator - (const Vector4& lhs, const float rhs)
977 {
978 return Vector4(
979 lhs.x - rhs,
980 lhs.y - rhs,
981 lhs.z - rhs,
982 lhs.w - rhs);
983 }
984
985 inline friend Vector4 operator - (const float lhs, const Vector4& rhs)
986 {
987 return Vector4(
988 lhs - rhs.x,
989 lhs - rhs.y,
990 lhs - rhs.z,
991 lhs - rhs.w);
992 }
993
994 // arithmetic updates
995 inline Vector4& operator += (const Vector4& rkVector)
996 {
997 x += rkVector.x;
998 y += rkVector.y;
999 z += rkVector.z;
1000 w += rkVector.w;
1001
1002 return *this;
1003 }
1004
1005 inline Vector4& operator += (const float fScalar)
1006 {
1007 x += fScalar;
1008 y += fScalar;
1009 z += fScalar;
1010 w += fScalar;
1011 return *this;
1012 }
1013
1014 inline Vector4& operator -= (const Vector4& rkVector)
1015 {
1016 x -= rkVector.x;
1017 y -= rkVector.y;
1018 z -= rkVector.z;
1019 w -= rkVector.w;
1020
1021 return *this;
1022 }
1023
1024 inline Vector4& operator -= (const float fScalar)
1025 {
1026 x -= fScalar;
1027 y -= fScalar;
1028 z -= fScalar;
1029 w -= fScalar;
1030 return *this;
1031 }
1032
1033 inline Vector4& operator *= (const float fScalar)
1034 {
1035 x *= fScalar;
1036 y *= fScalar;
1037 z *= fScalar;
1038 w *= fScalar;
1039 return *this;
1040 }
1041
1042 inline Vector4& operator *= (const Vector4& rkVector)
1043 {
1044 x *= rkVector.x;
1045 y *= rkVector.y;
1046 z *= rkVector.z;
1047 w *= rkVector.w;
1048
1049 return *this;
1050 }
1051
1052 inline Vector4& operator /= (const float fScalar)
1053 {
1054 float fInv = 1.0f / fScalar;
1055
1056 x *= fInv;
1057 y *= fInv;
1058 z *= fInv;
1059 w *= fInv;
1060
1061 return *this;
1062 }
1063
1064 inline Vector4& operator /= (const Vector4& rkVector)
1065 {
1066 x /= rkVector.x;
1067 y /= rkVector.y;
1068 z /= rkVector.z;
1069 w /= rkVector.w;
1070
1071 return *this;
1072 }
1073};
1074
1076{
1077public:
1078 inline Matrix3() {}
1079 inline explicit Matrix3(const float arr[3][3])
1080 {
1081 memcpy(m, arr, 9 * sizeof(float));
1082 }
1083 inline Matrix3(const Matrix3& rkMatrix)
1084 {
1085 memcpy(m, rkMatrix.m, 9 * sizeof(float));
1086 }
1087 Matrix3(float fEntry00, float fEntry01, float fEntry02,
1088 float fEntry10, float fEntry11, float fEntry12,
1089 float fEntry20, float fEntry21, float fEntry22)
1090 {
1091 m[0][0] = fEntry00;
1092 m[0][1] = fEntry01;
1093 m[0][2] = fEntry02;
1094 m[1][0] = fEntry10;
1095 m[1][1] = fEntry11;
1096 m[1][2] = fEntry12;
1097 m[2][0] = fEntry20;
1098 m[2][1] = fEntry21;
1099 m[2][2] = fEntry22;
1100 }
1101
1102 inline const float* operator[] (size_t iRow) const
1103 {
1104 return m[iRow];
1105 }
1106
1107 inline float* operator[] (size_t iRow)
1108 {
1109 return m[iRow];
1110 }
1111
1112 inline Matrix3& operator= (const Matrix3& rkMatrix)
1113 {
1114 memcpy(m, rkMatrix.m, 9 * sizeof(float));
1115 return *this;
1116 }
1117
1118 bool operator== (const Matrix3& rkMatrix) const;
1119
1120 inline bool operator!= (const Matrix3& rkMatrix) const
1121 {
1122 return !operator==(rkMatrix);
1123 }
1124
1125protected:
1126 float m[3][3];
1127
1128 // for faster access
1129 friend class Matrix4;
1130};
1131
1133{
1134public:
1135 float w, x, y, z;
1136
1137 inline Quaternion() : w(1), x(0), y(0), z(0) {}
1138
1139 inline Quaternion(float fW, float fX, float fY, float fZ) : w(fW), x(fX), y(fY), z(fZ) {}
1140
1141 inline Quaternion(const XrQuaternionf q)
1142 {
1143 w = q.w;
1144 x = q.x;
1145 y = q.y;
1146 z = q.z;
1147 }
1148
1149 inline Quaternion(const Matrix3& rot)
1150 {
1151 this->FromRotationMatrix(rot);
1152 }
1153
1154 inline Quaternion(const float& rfAngle, const Vector3& rkAxis)
1155 {
1156 this->FromAngleAxis(rfAngle, rkAxis);
1157 }
1158
1159 inline float operator [] (const size_t i) const
1160 {
1161 return *(&w + i);
1162 }
1163
1164 inline float& operator [] (const size_t i)
1165 {
1166 return *(&w + i);
1167 }
1168
1169 inline void FromAngleAxis(const float& rfAngle, const Vector3& rkAxis)
1170 {
1171 // assert: axis[] is unit length
1172 //
1173 // The quaternion representing the rotation is
1174 // q = cos(A/2)+sin(A/2)*(x*i+y*j+z*k)
1175
1176 float fHalfAngle = 0.5f * rfAngle;
1177 float fSin = std::sin(fHalfAngle);
1178 w = std::cos(fHalfAngle);
1179 x = fSin * rkAxis.x;
1180 y = fSin * rkAxis.y;
1181 z = fSin * rkAxis.z;
1182 }
1183
1184 void FromRotationMatrix(const Matrix3& kRot);
1185
1186 void ToRotationMatrix(Matrix3& kRot) const;
1187
1188 inline Quaternion& operator= (const Quaternion& rkQ)
1189 {
1190 w = rkQ.w;
1191 x = rkQ.x;
1192 y = rkQ.y;
1193 z = rkQ.z;
1194 return *this;
1195 }
1196 Quaternion operator+ (const Quaternion& rkQ) const
1197 {
1198 return Quaternion(w + rkQ.w, x + rkQ.x, y + rkQ.y, z + rkQ.z);
1199 }
1200 Quaternion operator- (const Quaternion& rkQ) const
1201 {
1202 return Quaternion(w - rkQ.w, x - rkQ.x, y - rkQ.y, z - rkQ.z);
1203 }
1204 Quaternion operator* (const Quaternion& rkQ) const
1205 {
1206 return Quaternion
1207 (
1208 w * rkQ.w - x * rkQ.x - y * rkQ.y - z * rkQ.z,
1209 w * rkQ.x + x * rkQ.w + y * rkQ.z - z * rkQ.y,
1210 w * rkQ.y + y * rkQ.w + z * rkQ.x - x * rkQ.z,
1211 w * rkQ.z + z * rkQ.w + x * rkQ.y - y * rkQ.x
1212 );
1213 }
1214 Quaternion operator* (float fScalar) const
1215 {
1216 return Quaternion(fScalar*w, fScalar*x, fScalar*y, fScalar*z);
1217 }
1218
1219 Vector3 operator* (const Vector3& v) const
1220 {
1221 // nVidia SDK implementation
1222 Vector3 uv, uuv;
1223 Vector3 qvec(x, y, z);
1224 uv = qvec.crossProduct(v);
1225 uuv = qvec.crossProduct(uv);
1226 uv *= (2.0f * w);
1227 uuv *= 2.0f;
1228
1229 return v + uv + uuv;
1230 }
1231
1232 Quaternion operator- () const
1233 {
1234 return Quaternion(-w, -x, -y, -z);
1235 }
1236 inline bool operator== (const Quaternion& rhs) const
1237 {
1238 return (rhs.x == x) && (rhs.y == y) && (rhs.z == z) && (rhs.w == w);
1239 }
1240 inline bool operator!= (const Quaternion& rhs) const
1241 {
1242 return !operator==(rhs);
1243 }
1244 inline float* ptr()
1245 {
1246 return &w;
1247 }
1248 inline const float* ptr() const
1249 {
1250 return &w;
1251 }
1252 float Norm() const;
1253 float normalise(void);
1254 Quaternion Inverse() const;
1255};
1256
1258{
1259protected:
1260 union {
1261 float m[4][4];
1262 float _m[16];
1263 };
1264public:
1265 static const Matrix4 IDENTITY;
1266
1267 inline Matrix4() {}
1268
1269 inline Matrix4(
1270 float m00, float m01, float m02, float m03,
1271 float m10, float m11, float m12, float m13,
1272 float m20, float m21, float m22, float m23,
1273 float m30, float m31, float m32, float m33)
1274 {
1275 m[0][0] = m00;
1276 m[0][1] = m01;
1277 m[0][2] = m02;
1278 m[0][3] = m03;
1279 m[1][0] = m10;
1280 m[1][1] = m11;
1281 m[1][2] = m12;
1282 m[1][3] = m13;
1283 m[2][0] = m20;
1284 m[2][1] = m21;
1285 m[2][2] = m22;
1286 m[2][3] = m23;
1287 m[3][0] = m30;
1288 m[3][1] = m31;
1289 m[3][2] = m32;
1290 m[3][3] = m33;
1291 }
1292
1293 inline Matrix4(XrMatrix4x4f mat)
1294 {
1295 m[0][0] = mat.m[0];
1296 m[0][1] = mat.m[4];
1297 m[0][2] = mat.m[8];
1298 m[0][3] = mat.m[12];
1299 m[1][0] = mat.m[1];
1300 m[1][1] = mat.m[5];
1301 m[1][2] = mat.m[9];
1302 m[1][3] = mat.m[13];
1303 m[2][0] = mat.m[2];
1304 m[2][1] = mat.m[6];
1305 m[2][2] = mat.m[10];
1306 m[2][3] = mat.m[14];
1307 m[3][0] = mat.m[3];
1308 m[3][1] = mat.m[7];
1309 m[3][2] = mat.m[11];
1310 m[3][3] = mat.m[15];
1311 }
1312
1313 inline Matrix4(const Matrix3& m3x3)
1314 {
1315 operator=(IDENTITY);
1316 operator=(m3x3);
1317 }
1318
1319 inline Matrix4(const Quaternion& rot)
1320 {
1321 Matrix3 m3x3;
1322 rot.ToRotationMatrix(m3x3);
1323 operator=(IDENTITY);
1324 operator=(m3x3);
1325 }
1326
1327 inline void extract3x3MatrixNegZ(Matrix3& m3x3) const
1328 {
1329 m3x3.m[0][0] = m[0][0];
1330 m3x3.m[0][1] = m[0][1];
1331 m3x3.m[0][2] = -m[0][2];
1332 m3x3.m[1][0] = m[1][0];
1333 m3x3.m[1][1] = m[1][1];
1334 m3x3.m[1][2] = -m[1][2];
1335 m3x3.m[2][0] = -m[2][0];
1336 m3x3.m[2][1] = -m[2][1];
1337 m3x3.m[2][2] = m[2][2];
1338 }
1339
1340 inline void extract3x3MatrixNegX(Matrix3& m3x3) const
1341 {
1342 m3x3.m[0][0] = -m[0][0];
1343 m3x3.m[0][1] = m[0][1];
1344 m3x3.m[0][2] = m[0][2];
1345 m3x3.m[1][0] = -m[1][0];
1346 m3x3.m[1][1] = m[1][1];
1347 m3x3.m[1][2] = m[1][2];
1348 m3x3.m[2][0] = m[2][0];
1349 m3x3.m[2][1] = -m[2][1];
1350 m3x3.m[2][2] = -m[2][2];
1351 }
1352
1353 inline void extract3x3Matrix(Matrix3& m3x3) const
1354 {
1355 m3x3.m[0][0] = m[0][0];
1356 m3x3.m[0][1] = m[0][1];
1357 m3x3.m[0][2] = m[0][2];
1358 m3x3.m[1][0] = m[1][0];
1359 m3x3.m[1][1] = m[1][1];
1360 m3x3.m[1][2] = m[1][2];
1361 m3x3.m[2][0] = m[2][0];
1362 m3x3.m[2][1] = m[2][1];
1363 m3x3.m[2][2] = m[2][2];
1364 }
1365
1366 inline Quaternion extractQuaternion() const
1367 {
1368 Matrix3 m3x3;
1369 extract3x3Matrix(m3x3);
1370 return Quaternion(m3x3);
1371 }
1372
1373 inline Quaternion extractQuaternionNegZ() const
1374 {
1375 Matrix3 m3x3;
1376 extract3x3MatrixNegZ(m3x3);
1377 return Quaternion(m3x3);
1378 }
1379
1380 inline Quaternion extractQuaternionNegX() const
1381 {
1382 Matrix3 m3x3;
1383 extract3x3MatrixNegX(m3x3);
1384 return Quaternion(m3x3);
1385 }
1386
1387 inline Vector3 getTrans() const
1388 {
1389 return Vector3(m[0][3], m[1][3], m[2][3]);
1390 }
1391 inline void operator = (const Matrix3& mat3)
1392 {
1393 m[0][0] = mat3.m[0][0]; m[0][1] = mat3.m[0][1]; m[0][2] = mat3.m[0][2];
1394 m[1][0] = mat3.m[1][0]; m[1][1] = mat3.m[1][1]; m[1][2] = mat3.m[1][2];
1395 m[2][0] = mat3.m[2][0]; m[2][1] = mat3.m[2][1]; m[2][2] = mat3.m[2][2];
1396 }
1397 inline float* operator [] (size_t iRow)
1398 {
1399 return m[iRow];
1400 }
1401
1402 inline const float *operator [] (size_t iRow) const
1403 {
1404 return m[iRow];
1405 }
1406
1407 inline Matrix4 operator * (const Matrix4 &m2) const
1408 {
1409 return concatenate(m2);
1410 }
1411
1412 inline Matrix4 concatenate(const Matrix4 &m2) const
1413 {
1414 Matrix4 r;
1415 r.m[0][0] = m[0][0] * m2.m[0][0] + m[0][1] * m2.m[1][0] + m[0][2] * m2.m[2][0] + m[0][3] * m2.m[3][0];
1416 r.m[0][1] = m[0][0] * m2.m[0][1] + m[0][1] * m2.m[1][1] + m[0][2] * m2.m[2][1] + m[0][3] * m2.m[3][1];
1417 r.m[0][2] = m[0][0] * m2.m[0][2] + m[0][1] * m2.m[1][2] + m[0][2] * m2.m[2][2] + m[0][3] * m2.m[3][2];
1418 r.m[0][3] = m[0][0] * m2.m[0][3] + m[0][1] * m2.m[1][3] + m[0][2] * m2.m[2][3] + m[0][3] * m2.m[3][3];
1419
1420 r.m[1][0] = m[1][0] * m2.m[0][0] + m[1][1] * m2.m[1][0] + m[1][2] * m2.m[2][0] + m[1][3] * m2.m[3][0];
1421 r.m[1][1] = m[1][0] * m2.m[0][1] + m[1][1] * m2.m[1][1] + m[1][2] * m2.m[2][1] + m[1][3] * m2.m[3][1];
1422 r.m[1][2] = m[1][0] * m2.m[0][2] + m[1][1] * m2.m[1][2] + m[1][2] * m2.m[2][2] + m[1][3] * m2.m[3][2];
1423 r.m[1][3] = m[1][0] * m2.m[0][3] + m[1][1] * m2.m[1][3] + m[1][2] * m2.m[2][3] + m[1][3] * m2.m[3][3];
1424
1425 r.m[2][0] = m[2][0] * m2.m[0][0] + m[2][1] * m2.m[1][0] + m[2][2] * m2.m[2][0] + m[2][3] * m2.m[3][0];
1426 r.m[2][1] = m[2][0] * m2.m[0][1] + m[2][1] * m2.m[1][1] + m[2][2] * m2.m[2][1] + m[2][3] * m2.m[3][1];
1427 r.m[2][2] = m[2][0] * m2.m[0][2] + m[2][1] * m2.m[1][2] + m[2][2] * m2.m[2][2] + m[2][3] * m2.m[3][2];
1428 r.m[2][3] = m[2][0] * m2.m[0][3] + m[2][1] * m2.m[1][3] + m[2][2] * m2.m[2][3] + m[2][3] * m2.m[3][3];
1429
1430 r.m[3][0] = m[3][0] * m2.m[0][0] + m[3][1] * m2.m[1][0] + m[3][2] * m2.m[2][0] + m[3][3] * m2.m[3][0];
1431 r.m[3][1] = m[3][0] * m2.m[0][1] + m[3][1] * m2.m[1][1] + m[3][2] * m2.m[2][1] + m[3][3] * m2.m[3][1];
1432 r.m[3][2] = m[3][0] * m2.m[0][2] + m[3][1] * m2.m[1][2] + m[3][2] * m2.m[2][2] + m[3][3] * m2.m[3][2];
1433 r.m[3][3] = m[3][0] * m2.m[0][3] + m[3][1] * m2.m[1][3] + m[3][2] * m2.m[2][3] + m[3][3] * m2.m[3][3];
1434
1435 return r;
1436 }
1437};
1438
1439#endif //__VRMATHS_H__
float * ptr()
Pointer accessor for direct copying.
Definition vrmaths.h:287
const float * ptr() const
Pointer accessor for direct copying.
Definition vrmaths.h:292
int * ptr()
Pointer accessor for direct copying.
Definition vrmaths.h:60
const int * ptr() const
Pointer accessor for direct copying.
Definition vrmaths.h:65
float * ptr()
Pointer accessor for direct copying.
Definition vrmaths.h:538
const float * ptr() const
Pointer accessor for direct copying.
Definition vrmaths.h:543
const float * ptr() const
Pointer accessor for direct copying.
Definition vrmaths.h:837
float * ptr()
Pointer accessor for direct copying.
Definition vrmaths.h:832