-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOVR_Math.h
3785 lines (3161 loc) · 126 KB
/
OVR_Math.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
/********************************************************************************//**
\file OVR_Math.h
\brief Implementation of 3D primitives such as vectors, matrices.
\copyright Copyright 2015 Oculus VR, LLC All Rights reserved.
*************************************************************************************/
#ifndef OVR_Math_h
#define OVR_Math_h
// This file is intended to be independent of the rest of LibOVR and LibOVRKernel and thus
// has no #include dependencies on either.
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <float.h>
#include "OVR_CAPI.h" // Currently required due to a dependence on the ovrFovPort_ declaration.
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4127) // conditional expression is constant
#endif
#if defined(_MSC_VER)
#define OVRMath_sprintf sprintf_s
#else
#define OVRMath_sprintf snprintf
#endif
//-------------------------------------------------------------------------------------
// ***** OVR_MATH_ASSERT
//
// Independent debug break implementation for OVR_Math.h.
#if !defined(OVR_MATH_DEBUG_BREAK)
#if defined(_DEBUG)
#if defined(_MSC_VER)
#define OVR_MATH_DEBUG_BREAK __debugbreak()
#else
#define OVR_MATH_DEBUG_BREAK __builtin_trap()
#endif
#else
#define OVR_MATH_DEBUG_BREAK ((void)0)
#endif
#endif
//-------------------------------------------------------------------------------------
// ***** OVR_MATH_ASSERT
//
// Independent OVR_MATH_ASSERT implementation for OVR_Math.h.
#if !defined(OVR_MATH_ASSERT)
#if defined(_DEBUG)
#define OVR_MATH_ASSERT(p) if (!(p)) { OVR_MATH_DEBUG_BREAK; }
#else
#define OVR_MATH_ASSERT(p) ((void)0)
#endif
#endif
//-------------------------------------------------------------------------------------
// ***** OVR_MATH_STATIC_ASSERT
//
// Independent OVR_MATH_ASSERT implementation for OVR_Math.h.
#if !defined(OVR_MATH_STATIC_ASSERT)
#if defined(__cplusplus) && ((defined(_MSC_VER) && (defined(_MSC_VER) >= 1600)) || defined(__GXX_EXPERIMENTAL_CXX0X__) || (__cplusplus >= 201103L))
#define OVR_MATH_STATIC_ASSERT static_assert
#else
#if !defined(OVR_SA_UNUSED)
#if defined(__GNUC__) || defined(__clang__)
#define OVR_SA_UNUSED __attribute__((unused))
#else
#define OVR_SA_UNUSED
#endif
#define OVR_SA_PASTE(a,b) a##b
#define OVR_SA_HELP(a,b) OVR_SA_PASTE(a,b)
#endif
#define OVR_MATH_STATIC_ASSERT(expression, msg) typedef char OVR_SA_HELP(compileTimeAssert, __LINE__) [((expression) != 0) ? 1 : -1] OVR_SA_UNUSED
#endif
#endif
namespace OVR {
template<class T>
const T OVRMath_Min(const T a, const T b)
{ return (a < b) ? a : b; }
template<class T>
const T OVRMath_Max(const T a, const T b)
{ return (b < a) ? a : b; }
template<class T>
void OVRMath_Swap(T& a, T& b)
{ T temp(a); a = b; b = temp; }
//-------------------------------------------------------------------------------------
// ***** Constants for 3D world/axis definitions.
// Definitions of axes for coordinate and rotation conversions.
enum Axis
{
Axis_X = 0, Axis_Y = 1, Axis_Z = 2
};
// RotateDirection describes the rotation direction around an axis, interpreted as follows:
// CW - Clockwise while looking "down" from positive axis towards the origin.
// CCW - Counter-clockwise while looking from the positive axis towards the origin,
// which is in the negative axis direction.
// CCW is the default for the RHS coordinate system. Oculus standard RHS coordinate
// system defines Y up, X right, and Z back (pointing out from the screen). In this
// system Rotate_CCW around Z will specifies counter-clockwise rotation in XY plane.
enum RotateDirection
{
Rotate_CCW = 1,
Rotate_CW = -1
};
// Constants for right handed and left handed coordinate systems
enum HandedSystem
{
Handed_R = 1, Handed_L = -1
};
// AxisDirection describes which way the coordinate axis points. Used by WorldAxes.
enum AxisDirection
{
Axis_Up = 2,
Axis_Down = -2,
Axis_Right = 1,
Axis_Left = -1,
Axis_In = 3,
Axis_Out = -3
};
struct WorldAxes
{
AxisDirection XAxis, YAxis, ZAxis;
WorldAxes(AxisDirection x, AxisDirection y, AxisDirection z)
: XAxis(x), YAxis(y), ZAxis(z)
{ OVR_MATH_ASSERT(abs(x) != abs(y) && abs(y) != abs(z) && abs(z) != abs(x));}
};
} // namespace OVR
//------------------------------------------------------------------------------------//
// ***** C Compatibility Types
// These declarations are used to support conversion between C types used in
// LibOVR C interfaces and their C++ versions. As an example, they allow passing
// Vector3f into a function that expects ovrVector3f.
typedef struct ovrQuatf_ ovrQuatf;
typedef struct ovrQuatd_ ovrQuatd;
typedef struct ovrSizei_ ovrSizei;
typedef struct ovrSizef_ ovrSizef;
typedef struct ovrSized_ ovrSized;
typedef struct ovrRecti_ ovrRecti;
typedef struct ovrVector2i_ ovrVector2i;
typedef struct ovrVector2f_ ovrVector2f;
typedef struct ovrVector2d_ ovrVector2d;
typedef struct ovrVector3f_ ovrVector3f;
typedef struct ovrVector3d_ ovrVector3d;
typedef struct ovrVector4f_ ovrVector4f;
typedef struct ovrVector4d_ ovrVector4d;
typedef struct ovrMatrix2f_ ovrMatrix2f;
typedef struct ovrMatrix2d_ ovrMatrix2d;
typedef struct ovrMatrix3f_ ovrMatrix3f;
typedef struct ovrMatrix3d_ ovrMatrix3d;
typedef struct ovrMatrix4f_ ovrMatrix4f;
typedef struct ovrMatrix4d_ ovrMatrix4d;
typedef struct ovrPosef_ ovrPosef;
typedef struct ovrPosed_ ovrPosed;
typedef struct ovrPoseStatef_ ovrPoseStatef;
typedef struct ovrPoseStated_ ovrPoseStated;
namespace OVR {
// Forward-declare our templates.
template<class T> class Quat;
template<class T> class Size;
template<class T> class Rect;
template<class T> class Vector2;
template<class T> class Vector3;
template<class T> class Vector4;
template<class T> class Matrix2;
template<class T> class Matrix3;
template<class T> class Matrix4;
template<class T> class Pose;
template<class T> class PoseState;
// CompatibleTypes::Type is used to lookup a compatible C-version of a C++ class.
template<class C>
struct CompatibleTypes
{
// Declaration here seems necessary for MSVC; specializations are
// used instead.
typedef struct {} Type;
};
// Specializations providing CompatibleTypes::Type value.
template<> struct CompatibleTypes<Quat<float> > { typedef ovrQuatf Type; };
template<> struct CompatibleTypes<Quat<double> > { typedef ovrQuatd Type; };
template<> struct CompatibleTypes<Matrix2<float> > { typedef ovrMatrix2f Type; };
template<> struct CompatibleTypes<Matrix2<double> > { typedef ovrMatrix2d Type; };
template<> struct CompatibleTypes<Matrix3<float> > { typedef ovrMatrix3f Type; };
template<> struct CompatibleTypes<Matrix3<double> > { typedef ovrMatrix3d Type; };
template<> struct CompatibleTypes<Matrix4<float> > { typedef ovrMatrix4f Type; };
template<> struct CompatibleTypes<Matrix4<double> > { typedef ovrMatrix4d Type; };
template<> struct CompatibleTypes<Size<int> > { typedef ovrSizei Type; };
template<> struct CompatibleTypes<Size<float> > { typedef ovrSizef Type; };
template<> struct CompatibleTypes<Size<double> > { typedef ovrSized Type; };
template<> struct CompatibleTypes<Rect<int> > { typedef ovrRecti Type; };
template<> struct CompatibleTypes<Vector2<int> > { typedef ovrVector2i Type; };
template<> struct CompatibleTypes<Vector2<float> > { typedef ovrVector2f Type; };
template<> struct CompatibleTypes<Vector2<double> > { typedef ovrVector2d Type; };
template<> struct CompatibleTypes<Vector3<float> > { typedef ovrVector3f Type; };
template<> struct CompatibleTypes<Vector3<double> > { typedef ovrVector3d Type; };
template<> struct CompatibleTypes<Vector4<float> > { typedef ovrVector4f Type; };
template<> struct CompatibleTypes<Vector4<double> > { typedef ovrVector4d Type; };
template<> struct CompatibleTypes<Pose<float> > { typedef ovrPosef Type; };
template<> struct CompatibleTypes<Pose<double> > { typedef ovrPosed Type; };
//------------------------------------------------------------------------------------//
// ***** Math
//
// Math class contains constants and functions. This class is a template specialized
// per type, with Math<float> and Math<double> being distinct.
template<class T>
class Math
{
public:
// By default, support explicit conversion to float. This allows Vector2<int> to
// compile, for example.
typedef float OtherFloatType;
static int Tolerance() { return 0; } // Default value so integer types compile
};
//------------------------------------------------------------------------------------//
// ***** double constants
#define MATH_DOUBLE_PI 3.14159265358979323846
#define MATH_DOUBLE_TWOPI (2*MATH_DOUBLE_PI)
#define MATH_DOUBLE_PIOVER2 (0.5*MATH_DOUBLE_PI)
#define MATH_DOUBLE_PIOVER4 (0.25*MATH_DOUBLE_PI)
#define MATH_FLOAT_MAXVALUE (FLT_MAX)
#define MATH_DOUBLE_RADTODEGREEFACTOR (360.0 / MATH_DOUBLE_TWOPI)
#define MATH_DOUBLE_DEGREETORADFACTOR (MATH_DOUBLE_TWOPI / 360.0)
#define MATH_DOUBLE_E 2.71828182845904523536
#define MATH_DOUBLE_LOG2E 1.44269504088896340736
#define MATH_DOUBLE_LOG10E 0.434294481903251827651
#define MATH_DOUBLE_LN2 0.693147180559945309417
#define MATH_DOUBLE_LN10 2.30258509299404568402
#define MATH_DOUBLE_SQRT2 1.41421356237309504880
#define MATH_DOUBLE_SQRT1_2 0.707106781186547524401
#define MATH_DOUBLE_TOLERANCE 1e-12 // a default number for value equality tolerance: about 4500*Epsilon;
#define MATH_DOUBLE_SINGULARITYRADIUS 1e-12 // about 1-cos(.0001 degree), for gimbal lock numerical problems
//------------------------------------------------------------------------------------//
// ***** float constants
#define MATH_FLOAT_PI float(MATH_DOUBLE_PI)
#define MATH_FLOAT_TWOPI float(MATH_DOUBLE_TWOPI)
#define MATH_FLOAT_PIOVER2 float(MATH_DOUBLE_PIOVER2)
#define MATH_FLOAT_PIOVER4 float(MATH_DOUBLE_PIOVER4)
#define MATH_FLOAT_RADTODEGREEFACTOR float(MATH_DOUBLE_RADTODEGREEFACTOR)
#define MATH_FLOAT_DEGREETORADFACTOR float(MATH_DOUBLE_DEGREETORADFACTOR)
#define MATH_FLOAT_E float(MATH_DOUBLE_E)
#define MATH_FLOAT_LOG2E float(MATH_DOUBLE_LOG2E)
#define MATH_FLOAT_LOG10E float(MATH_DOUBLE_LOG10E)
#define MATH_FLOAT_LN2 float(MATH_DOUBLE_LN2)
#define MATH_FLOAT_LN10 float(MATH_DOUBLE_LN10)
#define MATH_FLOAT_SQRT2 float(MATH_DOUBLE_SQRT2)
#define MATH_FLOAT_SQRT1_2 float(MATH_DOUBLE_SQRT1_2)
#define MATH_FLOAT_TOLERANCE 1e-5f // a default number for value equality tolerance: 1e-5, about 84*EPSILON;
#define MATH_FLOAT_SINGULARITYRADIUS 1e-7f // about 1-cos(.025 degree), for gimbal lock numerical problems
// Single-precision Math constants class.
template<>
class Math<float>
{
public:
typedef double OtherFloatType;
static inline float Tolerance() { return MATH_FLOAT_TOLERANCE; }; // a default number for value equality tolerance
static inline float SingularityRadius() { return MATH_FLOAT_SINGULARITYRADIUS; }; // for gimbal lock numerical problems
};
// Double-precision Math constants class
template<>
class Math<double>
{
public:
typedef float OtherFloatType;
static inline double Tolerance() { return MATH_DOUBLE_TOLERANCE; }; // a default number for value equality tolerance
static inline double SingularityRadius() { return MATH_DOUBLE_SINGULARITYRADIUS; }; // for gimbal lock numerical problems
};
typedef Math<float> Mathf;
typedef Math<double> Mathd;
// Conversion functions between degrees and radians
// (non-templated to ensure passing int arguments causes warning)
inline float RadToDegree(float rad) { return rad * MATH_FLOAT_RADTODEGREEFACTOR; }
inline double RadToDegree(double rad) { return rad * MATH_DOUBLE_RADTODEGREEFACTOR; }
inline float DegreeToRad(float deg) { return deg * MATH_FLOAT_DEGREETORADFACTOR; }
inline double DegreeToRad(double deg) { return deg * MATH_DOUBLE_DEGREETORADFACTOR; }
// Square function
template<class T>
inline T Sqr(T x) { return x*x; }
// Sign: returns 0 if x == 0, -1 if x < 0, and 1 if x > 0
template<class T>
inline T Sign(T x) { return (x != T(0)) ? (x < T(0) ? T(-1) : T(1)) : T(0); }
// Numerically stable acos function
inline float Acos(float x) { return (x > 1.0f) ? 0.0f : (x < -1.0f) ? MATH_FLOAT_PI : acosf(x); }
inline double Acos(double x) { return (x > 1.0) ? 0.0 : (x < -1.0) ? MATH_DOUBLE_PI : acos(x); }
// Numerically stable asin function
inline float Asin(float x) { return (x > 1.0f) ? MATH_FLOAT_PIOVER2 : (x < -1.0f) ? -MATH_FLOAT_PIOVER2 : asinf(x); }
inline double Asin(double x) { return (x > 1.0) ? MATH_DOUBLE_PIOVER2 : (x < -1.0) ? -MATH_DOUBLE_PIOVER2 : asin(x); }
#if defined(_MSC_VER)
inline int isnan(double x) { return ::_isnan(x); }
#elif !defined(isnan) // Some libraries #define isnan.
inline int isnan(double x) { return ::isnan(x); }
#endif
template<class T>
class Quat;
//-------------------------------------------------------------------------------------
// ***** Vector2<>
// Vector2f (Vector2d) represents a 2-dimensional vector or point in space,
// consisting of coordinates x and y
template<class T>
class Vector2
{
public:
typedef T ElementType;
static const size_t ElementCount = 2;
T x, y;
Vector2() : x(0), y(0) { }
Vector2(T x_, T y_) : x(x_), y(y_) { }
explicit Vector2(T s) : x(s), y(s) { }
explicit Vector2(const Vector2<typename Math<T>::OtherFloatType> &src)
: x((T)src.x), y((T)src.y) { }
static Vector2 Zero() { return Vector2(0, 0); }
// C-interop support.
typedef typename CompatibleTypes<Vector2<T> >::Type CompatibleType;
Vector2(const CompatibleType& s) : x(s.x), y(s.y) { }
operator const CompatibleType& () const
{
OVR_MATH_STATIC_ASSERT(sizeof(Vector2<T>) == sizeof(CompatibleType), "sizeof(Vector2<T>) failure");
return reinterpret_cast<const CompatibleType&>(*this);
}
bool operator== (const Vector2& b) const { return x == b.x && y == b.y; }
bool operator!= (const Vector2& b) const { return x != b.x || y != b.y; }
Vector2 operator+ (const Vector2& b) const { return Vector2(x + b.x, y + b.y); }
Vector2& operator+= (const Vector2& b) { x += b.x; y += b.y; return *this; }
Vector2 operator- (const Vector2& b) const { return Vector2(x - b.x, y - b.y); }
Vector2& operator-= (const Vector2& b) { x -= b.x; y -= b.y; return *this; }
Vector2 operator- () const { return Vector2(-x, -y); }
// Scalar multiplication/division scales vector.
Vector2 operator* (T s) const { return Vector2(x*s, y*s); }
Vector2& operator*= (T s) { x *= s; y *= s; return *this; }
Vector2 operator/ (T s) const { T rcp = T(1)/s;
return Vector2(x*rcp, y*rcp); }
Vector2& operator/= (T s) { T rcp = T(1)/s;
x *= rcp; y *= rcp;
return *this; }
static Vector2 Min(const Vector2& a, const Vector2& b) { return Vector2((a.x < b.x) ? a.x : b.x,
(a.y < b.y) ? a.y : b.y); }
static Vector2 Max(const Vector2& a, const Vector2& b) { return Vector2((a.x > b.x) ? a.x : b.x,
(a.y > b.y) ? a.y : b.y); }
Vector2 Clamped(T maxMag) const
{
T magSquared = LengthSq();
if (magSquared <= Sqr(maxMag))
return *this;
else
return *this * (maxMag / sqrt(magSquared));
}
// Compare two vectors for equality with tolerance. Returns true if vectors match withing tolerance.
bool IsEqual(const Vector2& b, T tolerance =Math<T>::Tolerance()) const
{
return (fabs(b.x-x) <= tolerance) &&
(fabs(b.y-y) <= tolerance);
}
bool Compare(const Vector2& b, T tolerance = Math<T>::Tolerance()) const
{
return IsEqual(b, tolerance);
}
// Access element by index
T& operator[] (int idx)
{
OVR_MATH_ASSERT(0 <= idx && idx < 2);
return *(&x + idx);
}
const T& operator[] (int idx) const
{
OVR_MATH_ASSERT(0 <= idx && idx < 2);
return *(&x + idx);
}
// Entry-wise product of two vectors
Vector2 EntrywiseMultiply(const Vector2& b) const { return Vector2(x * b.x, y * b.y);}
// Multiply and divide operators do entry-wise math. Used Dot() for dot product.
Vector2 operator* (const Vector2& b) const { return Vector2(x * b.x, y * b.y); }
Vector2 operator/ (const Vector2& b) const { return Vector2(x / b.x, y / b.y); }
// Dot product
// Used to calculate angle q between two vectors among other things,
// as (A dot B) = |a||b|cos(q).
T Dot(const Vector2& b) const { return x*b.x + y*b.y; }
// Returns the angle from this vector to b, in radians.
T Angle(const Vector2& b) const
{
T div = LengthSq()*b.LengthSq();
OVR_MATH_ASSERT(div != T(0));
T result = Acos((this->Dot(b))/sqrt(div));
return result;
}
// Return Length of the vector squared.
T LengthSq() const { return (x * x + y * y); }
// Return vector length.
T Length() const { return sqrt(LengthSq()); }
// Returns squared distance between two points represented by vectors.
T DistanceSq(const Vector2& b) const { return (*this - b).LengthSq(); }
// Returns distance between two points represented by vectors.
T Distance(const Vector2& b) const { return (*this - b).Length(); }
// Determine if this a unit vector.
bool IsNormalized() const { return fabs(LengthSq() - T(1)) < Math<T>::Tolerance(); }
// Normalize, convention vector length to 1.
void Normalize()
{
T s = Length();
if (s != T(0))
s = T(1) / s;
*this *= s;
}
// Returns normalized (unit) version of the vector without modifying itself.
Vector2 Normalized() const
{
T s = Length();
if (s != T(0))
s = T(1) / s;
return *this * s;
}
// Linearly interpolates from this vector to another.
// Factor should be between 0.0 and 1.0, with 0 giving full value to this.
Vector2 Lerp(const Vector2& b, T f) const { return *this*(T(1) - f) + b*f; }
// Projects this vector onto the argument; in other words,
// A.Project(B) returns projection of vector A onto B.
Vector2 ProjectTo(const Vector2& b) const
{
T l2 = b.LengthSq();
OVR_MATH_ASSERT(l2 != T(0));
return b * ( Dot(b) / l2 );
}
// returns true if vector b is clockwise from this vector
bool IsClockwise(const Vector2& b) const
{
return (x * b.y - y * b.x) < 0;
}
};
typedef Vector2<float> Vector2f;
typedef Vector2<double> Vector2d;
typedef Vector2<int> Vector2i;
typedef Vector2<float> Point2f;
typedef Vector2<double> Point2d;
typedef Vector2<int> Point2i;
//-------------------------------------------------------------------------------------
// ***** Vector3<> - 3D vector of {x, y, z}
//
// Vector3f (Vector3d) represents a 3-dimensional vector or point in space,
// consisting of coordinates x, y and z.
template<class T>
class Vector3
{
public:
typedef T ElementType;
static const size_t ElementCount = 3;
T x, y, z;
// FIXME: default initialization of a vector class can be very expensive in a full-blown
// application. A few hundred thousand vector constructions is not unlikely and can add
// up to milliseconds of time on processors like the PS3 PPU.
Vector3() : x(0), y(0), z(0) { }
Vector3(T x_, T y_, T z_ = 0) : x(x_), y(y_), z(z_) { }
explicit Vector3(T s) : x(s), y(s), z(s) { }
explicit Vector3(const Vector3<typename Math<T>::OtherFloatType> &src)
: x((T)src.x), y((T)src.y), z((T)src.z) { }
static Vector3 Zero() { return Vector3(0, 0, 0); }
// C-interop support.
typedef typename CompatibleTypes<Vector3<T> >::Type CompatibleType;
Vector3(const CompatibleType& s) : x(s.x), y(s.y), z(s.z) { }
operator const CompatibleType& () const
{
OVR_MATH_STATIC_ASSERT(sizeof(Vector3<T>) == sizeof(CompatibleType), "sizeof(Vector3<T>) failure");
return reinterpret_cast<const CompatibleType&>(*this);
}
bool operator== (const Vector3& b) const { return x == b.x && y == b.y && z == b.z; }
bool operator!= (const Vector3& b) const { return x != b.x || y != b.y || z != b.z; }
Vector3 operator+ (const Vector3& b) const { return Vector3(x + b.x, y + b.y, z + b.z); }
Vector3& operator+= (const Vector3& b) { x += b.x; y += b.y; z += b.z; return *this; }
Vector3 operator- (const Vector3& b) const { return Vector3(x - b.x, y - b.y, z - b.z); }
Vector3& operator-= (const Vector3& b) { x -= b.x; y -= b.y; z -= b.z; return *this; }
Vector3 operator- () const { return Vector3(-x, -y, -z); }
// Scalar multiplication/division scales vector.
Vector3 operator* (T s) const { return Vector3(x*s, y*s, z*s); }
Vector3& operator*= (T s) { x *= s; y *= s; z *= s; return *this; }
Vector3 operator/ (T s) const { T rcp = T(1)/s;
return Vector3(x*rcp, y*rcp, z*rcp); }
Vector3& operator/= (T s) { T rcp = T(1)/s;
x *= rcp; y *= rcp; z *= rcp;
return *this; }
static Vector3 Min(const Vector3& a, const Vector3& b)
{
return Vector3((a.x < b.x) ? a.x : b.x,
(a.y < b.y) ? a.y : b.y,
(a.z < b.z) ? a.z : b.z);
}
static Vector3 Max(const Vector3& a, const Vector3& b)
{
return Vector3((a.x > b.x) ? a.x : b.x,
(a.y > b.y) ? a.y : b.y,
(a.z > b.z) ? a.z : b.z);
}
Vector3 Clamped(T maxMag) const
{
T magSquared = LengthSq();
if (magSquared <= Sqr(maxMag))
return *this;
else
return *this * (maxMag / sqrt(magSquared));
}
// Compare two vectors for equality with tolerance. Returns true if vectors match withing tolerance.
bool IsEqual(const Vector3& b, T tolerance = Math<T>::Tolerance()) const
{
return (fabs(b.x-x) <= tolerance) &&
(fabs(b.y-y) <= tolerance) &&
(fabs(b.z-z) <= tolerance);
}
bool Compare(const Vector3& b, T tolerance = Math<T>::Tolerance()) const
{
return IsEqual(b, tolerance);
}
T& operator[] (int idx)
{
OVR_MATH_ASSERT(0 <= idx && idx < 3);
return *(&x + idx);
}
const T& operator[] (int idx) const
{
OVR_MATH_ASSERT(0 <= idx && idx < 3);
return *(&x + idx);
}
// Entrywise product of two vectors
Vector3 EntrywiseMultiply(const Vector3& b) const { return Vector3(x * b.x,
y * b.y,
z * b.z);}
// Multiply and divide operators do entry-wise math
Vector3 operator* (const Vector3& b) const { return Vector3(x * b.x,
y * b.y,
z * b.z); }
Vector3 operator/ (const Vector3& b) const { return Vector3(x / b.x,
y / b.y,
z / b.z); }
// Dot product
// Used to calculate angle q between two vectors among other things,
// as (A dot B) = |a||b|cos(q).
T Dot(const Vector3& b) const { return x*b.x + y*b.y + z*b.z; }
// Compute cross product, which generates a normal vector.
// Direction vector can be determined by right-hand rule: Pointing index finder in
// direction a and middle finger in direction b, thumb will point in a.Cross(b).
Vector3 Cross(const Vector3& b) const { return Vector3(y*b.z - z*b.y,
z*b.x - x*b.z,
x*b.y - y*b.x); }
// Returns the angle from this vector to b, in radians.
T Angle(const Vector3& b) const
{
T div = LengthSq()*b.LengthSq();
OVR_MATH_ASSERT(div != T(0));
T result = Acos((this->Dot(b))/sqrt(div));
return result;
}
// Return Length of the vector squared.
T LengthSq() const { return (x * x + y * y + z * z); }
// Return vector length.
T Length() const { return (T)sqrt(LengthSq()); }
// Returns squared distance between two points represented by vectors.
T DistanceSq(Vector3 const& b) const { return (*this - b).LengthSq(); }
// Returns distance between two points represented by vectors.
T Distance(Vector3 const& b) const { return (*this - b).Length(); }
bool IsNormalized() const { return fabs(LengthSq() - T(1)) < Math<T>::Tolerance(); }
// Normalize, convention vector length to 1.
void Normalize()
{
T s = Length();
if (s != T(0))
s = T(1) / s;
*this *= s;
}
// Returns normalized (unit) version of the vector without modifying itself.
Vector3 Normalized() const
{
T s = Length();
if (s != T(0))
s = T(1) / s;
return *this * s;
}
// Linearly interpolates from this vector to another.
// Factor should be between 0.0 and 1.0, with 0 giving full value to this.
Vector3 Lerp(const Vector3& b, T f) const { return *this*(T(1) - f) + b*f; }
// Projects this vector onto the argument; in other words,
// A.Project(B) returns projection of vector A onto B.
Vector3 ProjectTo(const Vector3& b) const
{
T l2 = b.LengthSq();
OVR_MATH_ASSERT(l2 != T(0));
return b * ( Dot(b) / l2 );
}
// Projects this vector onto a plane defined by a normal vector
Vector3 ProjectToPlane(const Vector3& normal) const { return *this - this->ProjectTo(normal); }
};
typedef Vector3<float> Vector3f;
typedef Vector3<double> Vector3d;
typedef Vector3<int32_t> Vector3i;
OVR_MATH_STATIC_ASSERT((sizeof(Vector3f) == 3*sizeof(float)), "sizeof(Vector3f) failure");
OVR_MATH_STATIC_ASSERT((sizeof(Vector3d) == 3*sizeof(double)), "sizeof(Vector3d) failure");
OVR_MATH_STATIC_ASSERT((sizeof(Vector3i) == 3*sizeof(int32_t)), "sizeof(Vector3i) failure");
typedef Vector3<float> Point3f;
typedef Vector3<double> Point3d;
typedef Vector3<int32_t> Point3i;
//-------------------------------------------------------------------------------------
// ***** Vector4<> - 4D vector of {x, y, z, w}
//
// Vector4f (Vector4d) represents a 3-dimensional vector or point in space,
// consisting of coordinates x, y, z and w.
template<class T>
class Vector4
{
public:
typedef T ElementType;
static const size_t ElementCount = 4;
T x, y, z, w;
// FIXME: default initialization of a vector class can be very expensive in a full-blown
// application. A few hundred thousand vector constructions is not unlikely and can add
// up to milliseconds of time on processors like the PS3 PPU.
Vector4() : x(0), y(0), z(0), w(0) { }
Vector4(T x_, T y_, T z_, T w_) : x(x_), y(y_), z(z_), w(w_) { }
explicit Vector4(T s) : x(s), y(s), z(s), w(s) { }
explicit Vector4(const Vector3<T>& v, const T w_=T(1)) : x(v.x), y(v.y), z(v.z), w(w_) { }
explicit Vector4(const Vector4<typename Math<T>::OtherFloatType> &src)
: x((T)src.x), y((T)src.y), z((T)src.z), w((T)src.w) { }
static Vector4 Zero() { return Vector4(0, 0, 0, 0); }
// C-interop support.
typedef typename CompatibleTypes< Vector4<T> >::Type CompatibleType;
Vector4(const CompatibleType& s) : x(s.x), y(s.y), z(s.z), w(s.w) { }
operator const CompatibleType& () const
{
OVR_MATH_STATIC_ASSERT(sizeof(Vector4<T>) == sizeof(CompatibleType), "sizeof(Vector4<T>) failure");
return reinterpret_cast<const CompatibleType&>(*this);
}
Vector4& operator= (const Vector3<T>& other) { x=other.x; y=other.y; z=other.z; w=1; return *this; }
bool operator== (const Vector4& b) const { return x == b.x && y == b.y && z == b.z && w == b.w; }
bool operator!= (const Vector4& b) const { return x != b.x || y != b.y || z != b.z || w != b.w; }
Vector4 operator+ (const Vector4& b) const { return Vector4(x + b.x, y + b.y, z + b.z, w + b.w); }
Vector4& operator+= (const Vector4& b) { x += b.x; y += b.y; z += b.z; w += b.w; return *this; }
Vector4 operator- (const Vector4& b) const { return Vector4(x - b.x, y - b.y, z - b.z, w - b.w); }
Vector4& operator-= (const Vector4& b) { x -= b.x; y -= b.y; z -= b.z; w -= b.w; return *this; }
Vector4 operator- () const { return Vector4(-x, -y, -z, -w); }
// Scalar multiplication/division scales vector.
Vector4 operator* (T s) const { return Vector4(x*s, y*s, z*s, w*s); }
Vector4& operator*= (T s) { x *= s; y *= s; z *= s; w *= s;return *this; }
Vector4 operator/ (T s) const { T rcp = T(1)/s;
return Vector4(x*rcp, y*rcp, z*rcp, w*rcp); }
Vector4& operator/= (T s) { T rcp = T(1)/s;
x *= rcp; y *= rcp; z *= rcp; w *= rcp;
return *this; }
static Vector4 Min(const Vector4& a, const Vector4& b)
{
return Vector4((a.x < b.x) ? a.x : b.x,
(a.y < b.y) ? a.y : b.y,
(a.z < b.z) ? a.z : b.z,
(a.w < b.w) ? a.w : b.w);
}
static Vector4 Max(const Vector4& a, const Vector4& b)
{
return Vector4((a.x > b.x) ? a.x : b.x,
(a.y > b.y) ? a.y : b.y,
(a.z > b.z) ? a.z : b.z,
(a.w > b.w) ? a.w : b.w);
}
Vector4 Clamped(T maxMag) const
{
T magSquared = LengthSq();
if (magSquared <= Sqr(maxMag))
return *this;
else
return *this * (maxMag / sqrt(magSquared));
}
// Compare two vectors for equality with tolerance. Returns true if vectors match withing tolerance.
bool IsEqual(const Vector4& b, T tolerance = Math<T>::Tolerance()) const
{
return (fabs(b.x-x) <= tolerance) &&
(fabs(b.y-y) <= tolerance) &&
(fabs(b.z-z) <= tolerance) &&
(fabs(b.w-w) <= tolerance);
}
bool Compare(const Vector4& b, T tolerance = Math<T>::Tolerance()) const
{
return IsEqual(b, tolerance);
}
T& operator[] (int idx)
{
OVR_MATH_ASSERT(0 <= idx && idx < 4);
return *(&x + idx);
}
const T& operator[] (int idx) const
{
OVR_MATH_ASSERT(0 <= idx && idx < 4);
return *(&x + idx);
}
// Entry wise product of two vectors
Vector4 EntrywiseMultiply(const Vector4& b) const { return Vector4(x * b.x,
y * b.y,
z * b.z,
w * b.w);}
// Multiply and divide operators do entry-wise math
Vector4 operator* (const Vector4& b) const { return Vector4(x * b.x,
y * b.y,
z * b.z,
w * b.w); }
Vector4 operator/ (const Vector4& b) const { return Vector4(x / b.x,
y / b.y,
z / b.z,
w / b.w); }
// Dot product
T Dot(const Vector4& b) const { return x*b.x + y*b.y + z*b.z + w*b.w; }
// Return Length of the vector squared.
T LengthSq() const { return (x * x + y * y + z * z + w * w); }
// Return vector length.
T Length() const { return sqrt(LengthSq()); }
bool IsNormalized() const { return fabs(LengthSq() - T(1)) < Math<T>::Tolerance(); }
// Normalize, convention vector length to 1.
void Normalize()
{
T s = Length();
if (s != T(0))
s = T(1) / s;
*this *= s;
}
// Returns normalized (unit) version of the vector without modifying itself.
Vector4 Normalized() const
{
T s = Length();
if (s != T(0))
s = T(1) / s;
return *this * s;
}
// Linearly interpolates from this vector to another.
// Factor should be between 0.0 and 1.0, with 0 giving full value to this.
Vector4 Lerp(const Vector4& b, T f) const { return *this*(T(1) - f) + b*f; }
};
typedef Vector4<float> Vector4f;
typedef Vector4<double> Vector4d;
typedef Vector4<int> Vector4i;
//-------------------------------------------------------------------------------------
// ***** Bounds3
// Bounds class used to describe a 3D axis aligned bounding box.
template<class T>
class Bounds3
{
public:
Vector3<T> b[2];
Bounds3()
{
}
Bounds3( const Vector3<T> & mins, const Vector3<T> & maxs )
{
b[0] = mins;
b[1] = maxs;
}
void Clear()
{
b[0].x = b[0].y = b[0].z = Math<T>::MaxValue;
b[1].x = b[1].y = b[1].z = -Math<T>::MaxValue;
}
void AddPoint( const Vector3<T> & v )
{
b[0].x = (b[0].x < v.x ? b[0].x : v.x);
b[0].y = (b[0].y < v.y ? b[0].y : v.y);
b[0].z = (b[0].z < v.z ? b[0].z : v.z);
b[1].x = (v.x < b[1].x ? b[1].x : v.x);
b[1].y = (v.y < b[1].y ? b[1].y : v.y);
b[1].z = (v.z < b[1].z ? b[1].z : v.z);
}
const Vector3<T> & GetMins() const { return b[0]; }
const Vector3<T> & GetMaxs() const { return b[1]; }
Vector3<T> & GetMins() { return b[0]; }
Vector3<T> & GetMaxs() { return b[1]; }
};
typedef Bounds3<float> Bounds3f;
typedef Bounds3<double> Bounds3d;
//-------------------------------------------------------------------------------------
// ***** Size
// Size class represents 2D size with Width, Height components.
// Used to describe distentions of render targets, etc.
template<class T>
class Size
{
public:
T w, h;
Size() : w(0), h(0) { }
Size(T w_, T h_) : w(w_), h(h_) { }
explicit Size(T s) : w(s), h(s) { }
explicit Size(const Size<typename Math<T>::OtherFloatType> &src)
: w((T)src.w), h((T)src.h) { }
// C-interop support.
typedef typename CompatibleTypes<Size<T> >::Type CompatibleType;
Size(const CompatibleType& s) : w(s.w), h(s.h) { }
operator const CompatibleType& () const
{
OVR_MATH_STATIC_ASSERT(sizeof(Size<T>) == sizeof(CompatibleType), "sizeof(Size<T>) failure");
return reinterpret_cast<const CompatibleType&>(*this);
}
bool operator== (const Size& b) const { return w == b.w && h == b.h; }
bool operator!= (const Size& b) const { return w != b.w || h != b.h; }
Size operator+ (const Size& b) const { return Size(w + b.w, h + b.h); }
Size& operator+= (const Size& b) { w += b.w; h += b.h; return *this; }
Size operator- (const Size& b) const { return Size(w - b.w, h - b.h); }
Size& operator-= (const Size& b) { w -= b.w; h -= b.h; return *this; }
Size operator- () const { return Size(-w, -h); }
Size operator* (const Size& b) const { return Size(w * b.w, h * b.h); }
Size& operator*= (const Size& b) { w *= b.w; h *= b.h; return *this; }
Size operator/ (const Size& b) const { return Size(w / b.w, h / b.h); }
Size& operator/= (const Size& b) { w /= b.w; h /= b.h; return *this; }
// Scalar multiplication/division scales both components.
Size operator* (T s) const { return Size(w*s, h*s); }
Size& operator*= (T s) { w *= s; h *= s; return *this; }
Size operator/ (T s) const { return Size(w/s, h/s); }
Size& operator/= (T s) { w /= s; h /= s; return *this; }
static Size Min(const Size& a, const Size& b) { return Size((a.w < b.w) ? a.w : b.w,
(a.h < b.h) ? a.h : b.h); }
static Size Max(const Size& a, const Size& b) { return Size((a.w > b.w) ? a.w : b.w,
(a.h > b.h) ? a.h : b.h); }