Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
OVR_Math.h
Go to the documentation of this file.
1 /************************************************************************************
2 
3 PublicHeader: OVR.h
4 Filename : OVR_Math.h
5 Content : Implementation of 3D primitives such as vectors, matrices.
6 Created : September 4, 2012
7 Authors : Andrew Reisse, Michael Antonov, Steve LaValle,
8  Anna Yershova, Max Katsev, Dov Katz
9 
10 Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
11 
12 Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
13 you may not use the Oculus VR Rift SDK except in compliance with the License,
14 which is provided at the time of installation or download, or which
15 otherwise accompanies this software in either electronic or hard copy form.
16 
17 You may obtain a copy of the License at
18 
19 http://www.oculusvr.com/licenses/LICENSE-3.1
20 
21 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
22 distributed under the License is distributed on an "AS IS" BASIS,
23 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 See the License for the specific language governing permissions and
25 limitations under the License.
26 
27 *************************************************************************************/
28 
29 #ifndef OVR_Math_h
30 #define OVR_Math_h
31 
32 #include <assert.h>
33 #include <stdlib.h>
34 #include <math.h>
35 
36 #include "OVR_Types.h"
37 #include "OVR_RefCount.h"
38 #include "OVR_Std.h"
39 #include "OVR_Alg.h"
40 
41 
42 namespace OVR {
43 
44 //-------------------------------------------------------------------------------------
45 // ***** Constants for 3D world/axis definitions.
46 
47 // Definitions of axes for coordinate and rotation conversions.
48 enum Axis
49 {
50  Axis_X = 0, Axis_Y = 1, Axis_Z = 2
51 };
52 
53 // RotateDirection describes the rotation direction around an axis, interpreted as follows:
54 // CW - Clockwise while looking "down" from positive axis towards the origin.
55 // CCW - Counter-clockwise while looking from the positive axis towards the origin,
56 // which is in the negative axis direction.
57 // CCW is the default for the RHS coordinate system. Oculus standard RHS coordinate
58 // system defines Y up, X right, and Z back (pointing out from the screen). In this
59 // system Rotate_CCW around Z will specifies counter-clockwise rotation in XY plane.
61 {
63  Rotate_CW = -1
64 };
65 
66 // Constants for right handed and left handed coordinate systems
68 {
69  Handed_R = 1, Handed_L = -1
70 };
71 
72 // AxisDirection describes which way the coordinate axis points. Used by WorldAxes.
74 {
75  Axis_Up = 2,
76  Axis_Down = -2,
78  Axis_Left = -1,
79  Axis_In = 3,
80  Axis_Out = -3
81 };
82 
83 struct WorldAxes
84 {
86 
88  : XAxis(x), YAxis(y), ZAxis(z)
89  { OVR_ASSERT(abs(x) != abs(y) && abs(y) != abs(z) && abs(z) != abs(x));}
90 };
91 
92 } // namespace OVR
93 
94 
95 //------------------------------------------------------------------------------------//
96 // ***** C Compatibility Types
97 
98 // These declarations are used to support conversion between C types used in
99 // LibOVR C interfaces and their C++ versions. As an example, they allow passing
100 // Vector3f into a function that expects ovrVector3f.
101 
102 typedef struct ovrQuatf_ ovrQuatf;
103 typedef struct ovrQuatd_ ovrQuatd;
104 typedef struct ovrSizei_ ovrSizei;
105 typedef struct ovrSizef_ ovrSizef;
106 typedef struct ovrRecti_ ovrRecti;
107 typedef struct ovrVector2i_ ovrVector2i;
108 typedef struct ovrVector2f_ ovrVector2f;
109 typedef struct ovrVector3f_ ovrVector3f;
110 typedef struct ovrVector3d_ ovrVector3d;
111 typedef struct ovrMatrix3d_ ovrMatrix3d;
112 typedef struct ovrMatrix4f_ ovrMatrix4f;
113 typedef struct ovrPosef_ ovrPosef;
114 typedef struct ovrPosed_ ovrPosed;
116 typedef struct ovrPoseStated_ ovrPoseStated;
117 
118 namespace OVR {
119 
120 // Forward-declare our templates.
121 template<class T> class Quat;
122 template<class T> class Size;
123 template<class T> class Rect;
124 template<class T> class Vector2;
125 template<class T> class Vector3;
126 template<class T> class Matrix3;
127 template<class T> class Matrix4;
128 template<class T> class Transform;
129 template<class T> class PoseState;
130 
131 // CompatibleTypes::Type is used to lookup a compatible C-version of a C++ class.
132 template<class C>
134 {
135  // Declaration here seems necessary for MSVC; specializations are
136  // used instead.
137  typedef float Type;
138 };
139 
140 // Specializations providing CompatibleTypes::Type value.
141 template<> struct CompatibleTypes<Quat<float> > { typedef ovrQuatf Type; };
142 template<> struct CompatibleTypes<Quat<double> > { typedef ovrQuatd Type; };
143 template<> struct CompatibleTypes<Matrix3<double> > { typedef ovrMatrix3d Type; };
144 template<> struct CompatibleTypes<Matrix4<float> > { typedef ovrMatrix4f Type; };
145 template<> struct CompatibleTypes<Size<int> > { typedef ovrSizei Type; };
146 template<> struct CompatibleTypes<Size<float> > { typedef ovrSizef Type; };
147 template<> struct CompatibleTypes<Rect<int> > { typedef ovrRecti Type; };
148 template<> struct CompatibleTypes<Vector2<int> > { typedef ovrVector2i Type; };
149 template<> struct CompatibleTypes<Vector2<float> > { typedef ovrVector2f Type; };
150 template<> struct CompatibleTypes<Vector3<float> > { typedef ovrVector3f Type; };
151 template<> struct CompatibleTypes<Vector3<double> > { typedef ovrVector3d Type; };
152 
153 template<> struct CompatibleTypes<Transform<float> > { typedef ovrPosef Type; };
154 template<> struct CompatibleTypes<PoseState<float> > { typedef ovrPoseStatef Type; };
155 
156 template<> struct CompatibleTypes<Transform<double> > { typedef ovrPosed Type; };
157 template<> struct CompatibleTypes<PoseState<double> > { typedef ovrPoseStated Type; };
158 
159 //------------------------------------------------------------------------------------//
160 // ***** Math
161 //
162 // Math class contains constants and functions. This class is a template specialized
163 // per type, with Math<float> and Math<double> being distinct.
164 template<class Type>
165 class Math
166 {
167 public:
168  // By default, support explicit conversion to float. This allows Vector2<int> to
169  // compile, for example.
170  typedef float OtherFloatType;
171 };
172 
173 // Single-precision Math constants class.
174 template<>
175 class Math<float>
176 {
177 public:
178  static const float Pi;
179  static const float TwoPi;
180  static const float PiOver2;
181  static const float PiOver4;
182  static const float E;
183 
184  static const float MaxValue; // Largest positive float Value
185  static const float MinPositiveValue; // Smallest possible positive value
186 
187  static const float RadToDegreeFactor;
188  static const float DegreeToRadFactor;
189 
190  static const float Tolerance; // 0.00001f;
191  static const float SingularityRadius; // 0.0000001f for Gimbal lock numerical problems
192 
193  // Used to support direct conversions in template classes.
194  typedef double OtherFloatType;
195 };
196 
197 // Double-precision Math constants class.
198 template<>
199 class Math<double>
200 {
201 public:
202  static const double Pi;
203  static const double TwoPi;
204  static const double PiOver2;
205  static const double PiOver4;
206  static const double E;
207 
208  static const double MaxValue; // Largest positive double Value
209  static const double MinPositiveValue; // Smallest possible positive value
210 
211  static const double RadToDegreeFactor;
212  static const double DegreeToRadFactor;
213 
214  static const double Tolerance; // 0.00001;
215  static const double SingularityRadius; // 0.000000000001 for Gimbal lock numerical problems
216 
217  typedef float OtherFloatType;
218 };
219 
220 
223 
224 // Conversion functions between degrees and radians
225 template<class T>
226 T RadToDegree(T rads) { return rads * Math<T>::RadToDegreeFactor; }
227 template<class T>
228 T DegreeToRad(T rads) { return rads * Math<T>::DegreeToRadFactor; }
229 
230 // Numerically stable acos function
231 template<class T>
232 T Acos(T val) {
233  if (val > T(1)) return T(0);
234  else if (val < T(-1)) return Math<T>::Pi;
235  else return acos(val);
236 };
237 
238 // Numerically stable asin function
239 template<class T>
240 T Asin(T val) {
241  if (val > T(1)) return Math<T>::PiOver2;
242  else if (val < T(-1)) return Math<T>::PiOver2 * T(3);
243  else return asin(val);
244 };
245 
246 #ifdef OVR_CC_MSVC
247 inline int isnan(double x) { return _isnan(x); };
248 #endif
249 
250 template<class T>
251 class Quat;
252 
253 
254 //-------------------------------------------------------------------------------------
255 // ***** Vector2<>
256 
257 // Vector2f (Vector2d) represents a 2-dimensional vector or point in space,
258 // consisting of coordinates x and y
259 
260 template<class T>
261 class Vector2
262 {
263 public:
264  T x, y;
265 
266  Vector2() : x(0), y(0) { }
267  Vector2(T x_, T y_) : x(x_), y(y_) { }
268  explicit Vector2(T s) : x(s), y(s) { }
269  explicit Vector2(const Vector2<typename Math<T>::OtherFloatType> &src)
270  : x((T)src.x), y((T)src.y) { }
271 
272 
273  // C-interop support.
274  typedef typename CompatibleTypes<Vector2<T> >::Type CompatibleType;
275 
276  Vector2(const CompatibleType& s) : x(s.x), y(s.y) { }
277 
278  operator const CompatibleType& () const
279  {
280  OVR_COMPILER_ASSERT(sizeof(Vector2<T>) == sizeof(CompatibleType));
281  return reinterpret_cast<const CompatibleType&>(*this);
282  }
283 
284 
285  bool operator== (const Vector2& b) const { return x == b.x && y == b.y; }
286  bool operator!= (const Vector2& b) const { return x != b.x || y != b.y; }
287 
288  Vector2 operator+ (const Vector2& b) const { return Vector2(x + b.x, y + b.y); }
289  Vector2& operator+= (const Vector2& b) { x += b.x; y += b.y; return *this; }
290  Vector2 operator- (const Vector2& b) const { return Vector2(x - b.x, y - b.y); }
291  Vector2& operator-= (const Vector2& b) { x -= b.x; y -= b.y; return *this; }
292  Vector2 operator- () const { return Vector2(-x, -y); }
293 
294  // Scalar multiplication/division scales vector.
295  Vector2 operator* (T s) const { return Vector2(x*s, y*s); }
296  Vector2& operator*= (T s) { x *= s; y *= s; return *this; }
297 
298  Vector2 operator/ (T s) const { T rcp = T(1)/s;
299  return Vector2(x*rcp, y*rcp); }
300  Vector2& operator/= (T s) { T rcp = T(1)/s;
301  x *= rcp; y *= rcp;
302  return *this; }
303 
304  static Vector2 Min(const Vector2& a, const Vector2& b) { return Vector2((a.x < b.x) ? a.x : b.x,
305  (a.y < b.y) ? a.y : b.y); }
306  static Vector2 Max(const Vector2& a, const Vector2& b) { return Vector2((a.x > b.x) ? a.x : b.x,
307  (a.y > b.y) ? a.y : b.y); }
308 
309  // Compare two vectors for equality with tolerance. Returns true if vectors match withing tolerance.
310  bool Compare(const Vector2&b, T tolerance = Mathf::Tolerance)
311  {
312  return (fabs(b.x-x) < tolerance) && (fabs(b.y-y) < tolerance);
313  }
314 
315  // Entrywise product of two vectors
316  Vector2 EntrywiseMultiply(const Vector2& b) const { return Vector2(x * b.x, y * b.y);}
317 
318 
319  // Multiply and divide operators do entry-wise math. Used Dot() for dot product.
320  Vector2 operator* (const Vector2& b) const { return Vector2(x * b.x, y * b.y); }
321  Vector2 operator/ (const Vector2& b) const { return Vector2(x / b.x, y / b.y); }
322 
323  // Dot product
324  // Used to calculate angle q between two vectors among other things,
325  // as (A dot B) = |a||b|cos(q).
326  T Dot(const Vector2& b) const { return x*b.x + y*b.y; }
327 
328  // Returns the angle from this vector to b, in radians.
329  T Angle(const Vector2& b) const
330  {
331  T div = LengthSq()*b.LengthSq();
332  OVR_ASSERT(div != T(0));
333  T result = Acos((this->Dot(b))/sqrt(div));
334  return result;
335  }
336 
337  // Return Length of the vector squared.
338  T LengthSq() const { return (x * x + y * y); }
339 
340  // Return vector length.
341  T Length() const { return sqrt(LengthSq()); }
342 
343  // Returns squared distance between two points represented by vectors.
344  T DistanceSq(Vector2& b) const { return (*this - b).LengthSq(); }
345 
346  // Returns distance between two points represented by vectors.
347  T Distance(Vector2& b) const { return (*this - b).Length(); }
348 
349  // Determine if this a unit vector.
350  bool IsNormalized() const { return fabs(LengthSq() - T(1)) < Math<T>::Tolerance; }
351 
352  // Normalize, convention vector length to 1.
353  void Normalize()
354  {
355  T l = Length();
356  OVR_ASSERT(l != T(0));
357  *this /= l;
358  }
359  // Returns normalized (unit) version of the vector without modifying itself.
361  {
362  T l = Length();
363  OVR_ASSERT(l != T(0));
364  return *this / l;
365  }
366 
367  // Linearly interpolates from this vector to another.
368  // Factor should be between 0.0 and 1.0, with 0 giving full value to this.
369  Vector2 Lerp(const Vector2& b, T f) const { return *this*(T(1) - f) + b*f; }
370 
371  // Projects this vector onto the argument; in other words,
372  // A.Project(B) returns projection of vector A onto B.
373  Vector2 ProjectTo(const Vector2& b) const
374  {
375  T l2 = b.LengthSq();
376  OVR_ASSERT(l2 != T(0));
377  return b * ( Dot(b) / l2 );
378  }
379 };
380 
381 
385 
386 //-------------------------------------------------------------------------------------
387 // ***** Vector3<> - 3D vector of {x, y, z}
388 
389 //
390 // Vector3f (Vector3d) represents a 3-dimensional vector or point in space,
391 // consisting of coordinates x, y and z.
392 
393 template<class T>
394 class Vector3
395 {
396 public:
397  T x, y, z;
398 
399  Vector3() : x(0), y(0), z(0) { }
400  Vector3(T x_, T y_, T z_ = 0) : x(x_), y(y_), z(z_) { }
401  explicit Vector3(T s) : x(s), y(s), z(s) { }
402  explicit Vector3(const Vector3<typename Math<T>::OtherFloatType> &src)
403  : x((T)src.x), y((T)src.y), z((T)src.z) { }
404 
405 
406  // C-interop support.
407  typedef typename CompatibleTypes<Vector3<T> >::Type CompatibleType;
408 
409  Vector3(const CompatibleType& s) : x(s.x), y(s.y), z(s.z) { }
410 
411  operator const CompatibleType& () const
412  {
413  OVR_COMPILER_ASSERT(sizeof(Vector3<T>) == sizeof(CompatibleType));
414  return reinterpret_cast<const CompatibleType&>(*this);
415  }
416 
417  bool operator== (const Vector3& b) const { return x == b.x && y == b.y && z == b.z; }
418  bool operator!= (const Vector3& b) const { return x != b.x || y != b.y || z != b.z; }
419 
420  Vector3 operator+ (const Vector3& b) const { return Vector3(x + b.x, y + b.y, z + b.z); }
421  Vector3& operator+= (const Vector3& b) { x += b.x; y += b.y; z += b.z; return *this; }
422  Vector3 operator- (const Vector3& b) const { return Vector3(x - b.x, y - b.y, z - b.z); }
423  Vector3& operator-= (const Vector3& b) { x -= b.x; y -= b.y; z -= b.z; return *this; }
424  Vector3 operator- () const { return Vector3(-x, -y, -z); }
425 
426  // Scalar multiplication/division scales vector.
427  Vector3 operator* (T s) const { return Vector3(x*s, y*s, z*s); }
428  Vector3& operator*= (T s) { x *= s; y *= s; z *= s; return *this; }
429 
430  Vector3 operator/ (T s) const { T rcp = T(1)/s;
431  return Vector3(x*rcp, y*rcp, z*rcp); }
432  Vector3& operator/= (T s) { T rcp = T(1)/s;
433  x *= rcp; y *= rcp; z *= rcp;
434  return *this; }
435 
436  static Vector3 Min(const Vector3& a, const Vector3& b)
437  {
438  return Vector3((a.x < b.x) ? a.x : b.x,
439  (a.y < b.y) ? a.y : b.y,
440  (a.z < b.z) ? a.z : b.z);
441  }
442  static Vector3 Max(const Vector3& a, const Vector3& b)
443  {
444  return Vector3((a.x > b.x) ? a.x : b.x,
445  (a.y > b.y) ? a.y : b.y,
446  (a.z > b.z) ? a.z : b.z);
447  }
448 
449  // Compare two vectors for equality with tolerance. Returns true if vectors match withing tolerance.
450  bool Compare(const Vector3&b, T tolerance = Mathf::Tolerance)
451  {
452  return (fabs(b.x-x) < tolerance) &&
453  (fabs(b.y-y) < tolerance) &&
454  (fabs(b.z-z) < tolerance);
455  }
456 
457  T& operator[] (int idx)
458  {
459  OVR_ASSERT(0 <= idx && idx < 3);
460  return *(&x + idx);
461  }
462 
463  const T& operator[] (int idx) const
464  {
465  OVR_ASSERT(0 <= idx && idx < 3);
466  return *(&x + idx);
467  }
468 
469  // Entrywise product of two vectors
470  Vector3 EntrywiseMultiply(const Vector3& b) const { return Vector3(x * b.x,
471  y * b.y,
472  z * b.z);}
473 
474  // Multiply and divide operators do entry-wise math
475  Vector3 operator* (const Vector3& b) const { return Vector3(x * b.x,
476  y * b.y,
477  z * b.z); }
478 
479  Vector3 operator/ (const Vector3& b) const { return Vector3(x / b.x,
480  y / b.y,
481  z / b.z); }
482 
483 
484  // Dot product
485  // Used to calculate angle q between two vectors among other things,
486  // as (A dot B) = |a||b|cos(q).
487  T Dot(const Vector3& b) const { return x*b.x + y*b.y + z*b.z; }
488 
489  // Compute cross product, which generates a normal vector.
490  // Direction vector can be determined by right-hand rule: Pointing index finder in
491  // direction a and middle finger in direction b, thumb will point in a.Cross(b).
492  Vector3 Cross(const Vector3& b) const { return Vector3(y*b.z - z*b.y,
493  z*b.x - x*b.z,
494  x*b.y - y*b.x); }
495 
496  // Returns the angle from this vector to b, in radians.
497  T Angle(const Vector3& b) const
498  {
499  T div = LengthSq()*b.LengthSq();
500  OVR_ASSERT(div != T(0));
501  T result = Acos((this->Dot(b))/sqrt(div));
502  return result;
503  }
504 
505  // Return Length of the vector squared.
506  T LengthSq() const { return (x * x + y * y + z * z); }
507 
508  // Return vector length.
509  T Length() const { return sqrt(LengthSq()); }
510 
511  // Returns squared distance between two points represented by vectors.
512  T DistanceSq(Vector3 const& b) const { return (*this - b).LengthSq(); }
513 
514  // Returns distance between two points represented by vectors.
515  T Distance(Vector3 const& b) const { return (*this - b).Length(); }
516 
517  // Determine if this a unit vector.
518  bool IsNormalized() const { return fabs(LengthSq() - T(1)) < Math<T>::Tolerance; }
519 
520  // Normalize, convention vector length to 1.
521  void Normalize()
522  {
523  T l = Length();
524  OVR_ASSERT(l != T(0));
525  *this /= l;
526  }
527 
528  // Returns normalized (unit) version of the vector without modifying itself.
530  {
531  T l = Length();
532  OVR_ASSERT(l != T(0));
533  return *this / l;
534  }
535 
536  // Linearly interpolates from this vector to another.
537  // Factor should be between 0.0 and 1.0, with 0 giving full value to this.
538  Vector3 Lerp(const Vector3& b, T f) const { return *this*(T(1) - f) + b*f; }
539 
540  // Projects this vector onto the argument; in other words,
541  // A.Project(B) returns projection of vector A onto B.
542  Vector3 ProjectTo(const Vector3& b) const
543  {
544  T l2 = b.LengthSq();
545  OVR_ASSERT(l2 != T(0));
546  return b * ( Dot(b) / l2 );
547  }
548 
549  // Projects this vector onto a plane defined by a normal vector
550  Vector3 ProjectToPlane(const Vector3& normal) const { return *this - this->ProjectTo(normal); }
551 };
552 
553 
557 
558 
559 // JDC: this was defined in Render_Device.h, I moved it here, but it
560 // needs to be fleshed out like the other Vector types.
561 //
562 // A vector with a dummy w component for alignment in uniform buffers (and for float colors).
563 // The w component is not used in any calculations.
564 
565 struct Vector4f : public Vector3f
566 {
567  float w;
568 
569  Vector4f() : w(1) {}
570  Vector4f(const Vector3f& v) : Vector3f(v), w(1) {}
571  Vector4f(float r, float g, float b, float a) : Vector3f(r,g,b), w(a) {}
572 };
573 
574 
575 
576 //-------------------------------------------------------------------------------------
577 // ***** Size
578 
579 // Size class represents 2D size with Width, Height components.
580 // Used to describe distentions of render targets, etc.
581 
582 template<class T>
583 class Size
584 {
585 public:
586  T w, h;
587 
588  Size() : w(0), h(0) { }
589  Size(T w_, T h_) : w(w_), h(h_) { }
590  explicit Size(T s) : w(s), h(s) { }
591  explicit Size(const Size<typename Math<T>::OtherFloatType> &src)
592  : w((T)src.w), h((T)src.h) { }
593 
594  // C-interop support.
595  typedef typename CompatibleTypes<Size<T> >::Type CompatibleType;
596 
597  Size(const CompatibleType& s) : w(s.w), h(s.h) { }
598 
599  operator const CompatibleType& () const
600  {
601  OVR_COMPILER_ASSERT(sizeof(Size<T>) == sizeof(CompatibleType));
602  return reinterpret_cast<const CompatibleType&>(*this);
603  }
604 
605  bool operator== (const Size& b) const { return w == b.w && h == b.h; }
606  bool operator!= (const Size& b) const { return w != b.w || h != b.h; }
607 
608  Size operator+ (const Size& b) const { return Size(w + b.w, h + b.h); }
609  Size& operator+= (const Size& b) { w += b.w; h += b.h; return *this; }
610  Size operator- (const Size& b) const { return Size(w - b.w, h - b.h); }
611  Size& operator-= (const Size& b) { w -= b.w; h -= b.h; return *this; }
612  Size operator- () const { return Size(-w, -h); }
613  Size operator* (const Size& b) const { return Size(w * b.w, h * b.h); }
614  Size& operator*= (const Size& b) { w *= b.w; h *= b.h; return *this; }
615  Size operator/ (const Size& b) const { return Size(w / b.w, h / b.h); }
616  Size& operator/= (const Size& b) { w /= b.w; h /= b.h; return *this; }
617 
618  // Scalar multiplication/division scales both components.
619  Size operator* (T s) const { return Size(w*s, h*s); }
620  Size& operator*= (T s) { w *= s; h *= s; return *this; }
621  Size operator/ (T s) const { return Size(w/s, h/s); }
622  Size& operator/= (T s) { w /= s; h /= s; return *this; }
623 
624  static Size Min(const Size& a, const Size& b) { return Size((a.w < b.w) ? a.w : b.w,
625  (a.h < b.h) ? a.h : b.h); }
626  static Size Max(const Size& a, const Size& b) { return Size((a.w > b.w) ? a.w : b.w,
627  (a.h > b.h) ? a.h : b.h); }
628 
629 
630  T Area() const { return w * h; }
631 
632  inline Vector2<T> ToVector() const { return Vector2<T>(w, h); }
633 };
634 
635 
636 typedef Size<int> Sizei;
640 
641 
642 
643 //-----------------------------------------------------------------------------------
644 // ***** Rect
645 
646 // Rect describes a rectangular area for rendering, that includes position and size.
647 template<class T>
648 class Rect
649 {
650 public:
651  T x, y;
652  T w, h;
653 
654  Rect() { }
655  Rect(T x1, T y1, T w1, T h1) : x(x1), y(y1), w(w1), h(h1) { }
656  Rect(const Vector2<T>& pos, const Size<T>& sz) : x(pos.x), y(pos.y), w(sz.w), h(sz.h) { }
657  Rect(const Size<T>& sz) : x(0), y(0), w(sz.w), h(sz.h) { }
658 
659  // C-interop support.
660  typedef typename CompatibleTypes<Rect<T> >::Type CompatibleType;
661 
662  Rect(const CompatibleType& s) : x(s.Pos.x), y(s.Pos.y), w(s.Size.w), h(s.Size.h) { }
663 
664  operator const CompatibleType& () const
665  {
666  OVR_COMPILER_ASSERT(sizeof(Rect<T>) == sizeof(CompatibleType));
667  return reinterpret_cast<const CompatibleType&>(*this);
668  }
669 
670  Vector2<T> GetPos() const { return Vector2<T>(x, y); }
671  Size<T> GetSize() const { return Size<T>(w, h); }
672  void SetPos(const Vector2<T>& pos) { x = pos.x; y = pos.y; }
673  void SetSize(const Size<T>& sz) { w = sz.w; h = sz.h; }
674 
675  bool operator == (const Rect& vp) const
676  { return (x == vp.x) && (y == vp.y) && (w == vp.w) && (h == vp.h); }
677  bool operator != (const Rect& vp) const
678  { return !operator == (vp); }
679 };
680 
681 typedef Rect<int> Recti;
682 
683 
684 //-------------------------------------------------------------------------------------//
685 // ***** Quat
686 //
687 // Quatf represents a quaternion class used for rotations.
688 //
689 // Quaternion multiplications are done in right-to-left order, to match the
690 // behavior of matrices.
691 
692 
693 template<class T>
694 class Quat
695 {
696 public:
697  // w + Xi + Yj + Zk
698  T x, y, z, w;
699 
700  Quat() : x(0), y(0), z(0), w(1) { }
701  Quat(T x_, T y_, T z_, T w_) : x(x_), y(y_), z(z_), w(w_) { }
702  explicit Quat(const Quat<typename Math<T>::OtherFloatType> &src)
703  : x((T)src.x), y((T)src.y), z((T)src.z), w((T)src.w) { }
704 
705  // C-interop support.
706  Quat(const typename CompatibleTypes<Quat<T> >::Type& s) : x(s.x), y(s.y), z(s.z), w(s.w) { }
707 
708  operator typename CompatibleTypes<Quat<T> >::Type () const
709  {
710  typename CompatibleTypes<Quat<T> >::Type result;
711  result.x = x;
712  result.y = y;
713  result.z = z;
714  result.w = w;
715  return result;
716  }
717 
718  // Constructs quaternion for rotation around the axis by an angle.
719  Quat(const Vector3<T>& axis, T angle)
720  {
721  // Make sure we don't divide by zero.
722  if (axis.LengthSq() == 0)
723  {
724  // Assert if the axis is zero, but the angle isn't
725  OVR_ASSERT(angle == 0);
726  x = 0; y = 0; z = 0; w = 1;
727  return;
728  }
729 
730  Vector3<T> unitAxis = axis.Normalized();
731  T sinHalfAngle = sin(angle * T(0.5));
732 
733  w = cos(angle * T(0.5));
734  x = unitAxis.x * sinHalfAngle;
735  y = unitAxis.y * sinHalfAngle;
736  z = unitAxis.z * sinHalfAngle;
737  }
738 
739  // Constructs quaternion for rotation around one of the coordinate axis by an angle.
741  {
742  T sinHalfAngle = s * d *sin(angle * T(0.5));
743  T v[3];
744  v[0] = v[1] = v[2] = T(0);
745  v[A] = sinHalfAngle;
746 
747  w = cos(angle * T(0.5));
748  x = v[0];
749  y = v[1];
750  z = v[2];
751  }
752 
753  // Compute axis and angle from quaternion
754  void GetAxisAngle(Vector3<T>* axis, T* angle) const
755  {
756  if ( x*x + y*y + z*z > Math<T>::Tolerance * Math<T>::Tolerance ) {
757  *axis = Vector3<T>(x, y, z).Normalized();
758  *angle = 2 * Acos(w);
759  if (*angle > Math<T>::Pi) // Reduce the magnitude of the angle, if necessary
760  {
761  *angle = Math<T>::TwoPi - *angle;
762  *axis = *axis * (-1);
763  }
764  }
765  else
766  {
767  *axis = Vector3<T>(1, 0, 0);
768  *angle= 0;
769  }
770  }
771 
772  // Constructs the quaternion from a rotation matrix
773  explicit Quat(const Matrix4<T>& m)
774  {
775  T trace = m.M[0][0] + m.M[1][1] + m.M[2][2];
776 
777  // In almost all cases, the first part is executed.
778  // However, if the trace is not positive, the other
779  // cases arise.
780  if (trace > T(0))
781  {
782  T s = sqrt(trace + T(1)) * T(2); // s=4*qw
783  w = T(0.25) * s;
784  x = (m.M[2][1] - m.M[1][2]) / s;
785  y = (m.M[0][2] - m.M[2][0]) / s;
786  z = (m.M[1][0] - m.M[0][1]) / s;
787  }
788  else if ((m.M[0][0] > m.M[1][1])&&(m.M[0][0] > m.M[2][2]))
789  {
790  T s = sqrt(T(1) + m.M[0][0] - m.M[1][1] - m.M[2][2]) * T(2);
791  w = (m.M[2][1] - m.M[1][2]) / s;
792  x = T(0.25) * s;
793  y = (m.M[0][1] + m.M[1][0]) / s;
794  z = (m.M[2][0] + m.M[0][2]) / s;
795  }
796  else if (m.M[1][1] > m.M[2][2])
797  {
798  T s = sqrt(T(1) + m.M[1][1] - m.M[0][0] - m.M[2][2]) * T(2); // S=4*qy
799  w = (m.M[0][2] - m.M[2][0]) / s;
800  x = (m.M[0][1] + m.M[1][0]) / s;
801  y = T(0.25) * s;
802  z = (m.M[1][2] + m.M[2][1]) / s;
803  }
804  else
805  {
806  T s = sqrt(T(1) + m.M[2][2] - m.M[0][0] - m.M[1][1]) * T(2); // S=4*qz
807  w = (m.M[1][0] - m.M[0][1]) / s;
808  x = (m.M[0][2] + m.M[2][0]) / s;
809  y = (m.M[1][2] + m.M[2][1]) / s;
810  z = T(0.25) * s;
811  }
812  }
813 
814  // Constructs the quaternion from a rotation matrix
815  explicit Quat(const Matrix3<T>& m)
816  {
817  T trace = m.M[0][0] + m.M[1][1] + m.M[2][2];
818 
819  // In almost all cases, the first part is executed.
820  // However, if the trace is not positive, the other
821  // cases arise.
822  if (trace > T(0))
823  {
824  T s = sqrt(trace + T(1)) * T(2); // s=4*qw
825  w = T(0.25) * s;
826  x = (m.M[2][1] - m.M[1][2]) / s;
827  y = (m.M[0][2] - m.M[2][0]) / s;
828  z = (m.M[1][0] - m.M[0][1]) / s;
829  }
830  else if ((m.M[0][0] > m.M[1][1])&&(m.M[0][0] > m.M[2][2]))
831  {
832  T s = sqrt(T(1) + m.M[0][0] - m.M[1][1] - m.M[2][2]) * T(2);
833  w = (m.M[2][1] - m.M[1][2]) / s;
834  x = T(0.25) * s;
835  y = (m.M[0][1] + m.M[1][0]) / s;
836  z = (m.M[2][0] + m.M[0][2]) / s;
837  }
838  else if (m.M[1][1] > m.M[2][2])
839  {
840  T s = sqrt(T(1) + m.M[1][1] - m.M[0][0] - m.M[2][2]) * T(2); // S=4*qy
841  w = (m.M[0][2] - m.M[2][0]) / s;
842  x = (m.M[0][1] + m.M[1][0]) / s;
843  y = T(0.25) * s;
844  z = (m.M[1][2] + m.M[2][1]) / s;
845  }
846  else
847  {
848  T s = sqrt(T(1) + m.M[2][2] - m.M[0][0] - m.M[1][1]) * T(2); // S=4*qz
849  w = (m.M[1][0] - m.M[0][1]) / s;
850  x = (m.M[0][2] + m.M[2][0]) / s;
851  y = (m.M[1][2] + m.M[2][1]) / s;
852  z = T(0.25) * s;
853  }
854  }
855 
856  bool operator== (const Quat& b) const { return x == b.x && y == b.y && z == b.z && w == b.w; }
857  bool operator!= (const Quat& b) const { return x != b.x || y != b.y || z != b.z || w != b.w; }
858 
859  Quat operator+ (const Quat& b) const { return Quat(x + b.x, y + b.y, z + b.z, w + b.w); }
860  Quat& operator+= (const Quat& b) { w += b.w; x += b.x; y += b.y; z += b.z; return *this; }
861  Quat operator- (const Quat& b) const { return Quat(x - b.x, y - b.y, z - b.z, w - b.w); }
862  Quat& operator-= (const Quat& b) { w -= b.w; x -= b.x; y -= b.y; z -= b.z; return *this; }
863 
864  Quat operator* (T s) const { return Quat(x * s, y * s, z * s, w * s); }
865  Quat& operator*= (T s) { w *= s; x *= s; y *= s; z *= s; return *this; }
866  Quat operator/ (T s) const { T rcp = T(1)/s; return Quat(x * rcp, y * rcp, z * rcp, w *rcp); }
867  Quat& operator/= (T s) { T rcp = T(1)/s; w *= rcp; x *= rcp; y *= rcp; z *= rcp; return *this; }
868 
869 
870  // Get Imaginary part vector
871  Vector3<T> Imag() const { return Vector3<T>(x,y,z); }
872 
873  // Get quaternion length.
874  T Length() const { return sqrt(LengthSq()); }
875 
876  // Get quaternion length squared.
877  T LengthSq() const { return (x * x + y * y + z * z + w * w); }
878 
879  // Simple Euclidean distance in R^4 (not SLERP distance, but at least respects Haar measure)
880  T Distance(const Quat& q) const
881  {
882  T d1 = (*this - q).Length();
883  T d2 = (*this + q).Length(); // Antipodal point check
884  return (d1 < d2) ? d1 : d2;
885  }
886 
887  T DistanceSq(const Quat& q) const
888  {
889  T d1 = (*this - q).LengthSq();
890  T d2 = (*this + q).LengthSq(); // Antipodal point check
891  return (d1 < d2) ? d1 : d2;
892  }
893 
894  T Dot(const Quat& q) const
895  {
896  return x * q.x + y * q.y + z * q.z + w * q.w;
897  }
898 
899  // Angle between two quaternions in radians
900  T Angle(const Quat& q) const
901  {
902  return 2 * Acos(Alg::Abs(Dot(q)));
903  }
904 
905  // Normalize
906  bool IsNormalized() const { return fabs(LengthSq() - T(1)) < Math<T>::Tolerance; }
907 
908  void Normalize()
909  {
910  T l = Length();
911  OVR_ASSERT(l != T(0));
912  *this /= l;
913  }
914 
915  Quat Normalized() const
916  {
917  T l = Length();
918  OVR_ASSERT(l != T(0));
919  return *this / l;
920  }
921 
922  // Returns conjugate of the quaternion. Produces inverse rotation if quaternion is normalized.
923  Quat Conj() const { return Quat(-x, -y, -z, w); }
924 
925  // Quaternion multiplication. Combines quaternion rotations, performing the one on the
926  // right hand side first.
927  Quat operator* (const Quat& b) const { return Quat(w * b.x + x * b.w + y * b.z - z * b.y,
928  w * b.y - x * b.z + y * b.w + z * b.x,
929  w * b.z + x * b.y - y * b.x + z * b.w,
930  w * b.w - x * b.x - y * b.y - z * b.z); }
931 
932  //
933  // this^p normalized; same as rotating by this p times.
934  Quat PowNormalized(T p) const
935  {
936  Vector3<T> v;
937  T a;
938  GetAxisAngle(&v, &a);
939  return Quat(v, a * p);
940  }
941 
942  // Normalized linear interpolation of quaternions
943  Quat Nlerp(const Quat& other, T a)
944  {
945  T sign = (Dot(other) >= 0) ? 1 : -1;
946  return (*this * sign * a + other * (1-a)).Normalized();
947  }
948 
949  // Rotate transforms vector in a manner that matches Matrix rotations (counter-clockwise,
950  // assuming negative direction of the axis). Standard formula: q(t) * V * q(t)^-1.
951  Vector3<T> Rotate(const Vector3<T>& v) const
952  {
953  return ((*this * Quat<T>(v.x, v.y, v.z, T(0))) * Inverted()).Imag();
954  }
955 
956  // Inversed quaternion rotates in the opposite direction.
957  Quat Inverted() const
958  {
959  return Quat(-x, -y, -z, w);
960  }
961 
962  // Sets this quaternion to the one rotates in the opposite direction.
963  void Invert()
964  {
965  *this = Quat(-x, -y, -z, w);
966  }
967 
968  // GetEulerAngles extracts Euler angles from the quaternion, in the specified order of
969  // axis rotations and the specified coordinate system. Right-handed coordinate system
970  // is the default, with CCW rotations while looking in the negative axis direction.
971  // Here a,b,c, are the Yaw/Pitch/Roll angles to be returned.
972  // rotation a around axis A1
973  // is followed by rotation b around axis A2
974  // is followed by rotation c around axis A3
975  // rotations are CCW or CW (D) in LH or RH coordinate system (S)
976  template <Axis A1, Axis A2, Axis A3, RotateDirection D, HandedSystem S>
977  void GetEulerAngles(T *a, T *b, T *c) const
978  {
979  OVR_COMPILER_ASSERT((A1 != A2) && (A2 != A3) && (A1 != A3));
980 
981  T Q[3] = { x, y, z }; //Quaternion components x,y,z
982 
983  T ww = w*w;
984  T Q11 = Q[A1]*Q[A1];
985  T Q22 = Q[A2]*Q[A2];
986  T Q33 = Q[A3]*Q[A3];
987 
988  T psign = T(-1);
989  // Determine whether even permutation
990  if (((A1 + 1) % 3 == A2) && ((A2 + 1) % 3 == A3))
991  psign = T(1);
992 
993  T s2 = psign * T(2) * (psign*w*Q[A2] + Q[A1]*Q[A3]);
994 
995  if (s2 < T(-1) + Math<T>::SingularityRadius)
996  { // South pole singularity
997  *a = T(0);
998  *b = -S*D*Math<T>::PiOver2;
999  *c = S*D*atan2(T(2)*(psign*Q[A1]*Q[A2] + w*Q[A3]),
1000  ww + Q22 - Q11 - Q33 );
1001  }
1002  else if (s2 > T(1) - Math<T>::SingularityRadius)
1003  { // North pole singularity
1004  *a = T(0);
1005  *b = S*D*Math<T>::PiOver2;
1006  *c = S*D*atan2(T(2)*(psign*Q[A1]*Q[A2] + w*Q[A3]),
1007  ww + Q22 - Q11 - Q33);
1008  }
1009  else
1010  {
1011  *a = -S*D*atan2(T(-2)*(w*Q[A1] - psign*Q[A2]*Q[A3]),
1012  ww + Q33 - Q11 - Q22);
1013  *b = S*D*asin(s2);
1014  *c = S*D*atan2(T(2)*(w*Q[A3] - psign*Q[A1]*Q[A2]),
1015  ww + Q11 - Q22 - Q33);
1016  }
1017  return;
1018  }
1019 
1020  template <Axis A1, Axis A2, Axis A3, RotateDirection D>
1021  void GetEulerAngles(T *a, T *b, T *c) const
1022  { GetEulerAngles<A1, A2, A3, D, Handed_R>(a, b, c); }
1023 
1024  template <Axis A1, Axis A2, Axis A3>
1025  void GetEulerAngles(T *a, T *b, T *c) const
1026  { GetEulerAngles<A1, A2, A3, Rotate_CCW, Handed_R>(a, b, c); }
1027 
1028 
1029  // GetEulerAnglesABA extracts Euler angles from the quaternion, in the specified order of
1030  // axis rotations and the specified coordinate system. Right-handed coordinate system
1031  // is the default, with CCW rotations while looking in the negative axis direction.
1032  // Here a,b,c, are the Yaw/Pitch/Roll angles to be returned.
1033  // rotation a around axis A1
1034  // is followed by rotation b around axis A2
1035  // is followed by rotation c around axis A1
1036  // Rotations are CCW or CW (D) in LH or RH coordinate system (S)
1037  template <Axis A1, Axis A2, RotateDirection D, HandedSystem S>
1038  void GetEulerAnglesABA(T *a, T *b, T *c) const
1039  {
1040  OVR_COMPILER_ASSERT(A1 != A2);
1041 
1042  T Q[3] = {x, y, z}; // Quaternion components
1043 
1044  // Determine the missing axis that was not supplied
1045  int m = 3 - A1 - A2;
1046 
1047  T ww = w*w;
1048  T Q11 = Q[A1]*Q[A1];
1049  T Q22 = Q[A2]*Q[A2];
1050  T Qmm = Q[m]*Q[m];
1051 
1052  T psign = T(-1);
1053  if ((A1 + 1) % 3 == A2) // Determine whether even permutation
1054  {
1055  psign = T(1);
1056  }
1057 
1058  T c2 = ww + Q11 - Q22 - Qmm;
1059  if (c2 < T(-1) + Math<T>::SingularityRadius)
1060  { // South pole singularity
1061  *a = T(0);
1062  *b = S*D*Math<T>::Pi;
1063  *c = S*D*atan2( T(2)*(w*Q[A1] - psign*Q[A2]*Q[m]),
1064  ww + Q22 - Q11 - Qmm);
1065  }
1066  else if (c2 > T(1) - Math<T>::SingularityRadius)
1067  { // North pole singularity
1068  *a = T(0);
1069  *b = T(0);
1070  *c = S*D*atan2( T(2)*(w*Q[A1] - psign*Q[A2]*Q[m]),
1071  ww + Q22 - Q11 - Qmm);
1072  }
1073  else
1074  {
1075  *a = S*D*atan2( psign*w*Q[m] + Q[A1]*Q[A2],
1076  w*Q[A2] -psign*Q[A1]*Q[m]);
1077  *b = S*D*acos(c2);
1078  *c = S*D*atan2( -psign*w*Q[m] + Q[A1]*Q[A2],
1079  w*Q[A2] + psign*Q[A1]*Q[m]);
1080  }
1081  return;
1082  }
1083 };
1084 
1087 
1088 //-------------------------------------------------------------------------------------
1089 // ***** Pose
1090 
1091 // Position and orientation combined.
1092 
1093 template<class T>
1094 class Transform
1095 {
1096 public:
1097 
1099 
1101  Transform(const Quat<T>& orientation, const Vector3<T>& pos)
1102  : Rotation(orientation), Translation(pos) { }
1106  : Rotation(s.Orientation), Translation(s.Position) { }
1107  explicit Transform(const Transform<typename Math<T>::OtherFloatType> &s)
1109 
1110  operator typename CompatibleTypes<Transform<T> >::Type () const
1111  {
1112  typename CompatibleTypes<Transform<T> >::Type result;
1113  result.Orientation = Rotation;
1114  result.Position = Translation;
1115  return result;
1116  }
1117 
1120 
1121  Vector3<T> Rotate(const Vector3<T>& v) const
1122  {
1123  return Rotation.Rotate(v);
1124  }
1125 
1127  {
1128  return v + Translation;
1129  }
1130 
1131  Vector3<T> Apply(const Vector3<T>& v) const
1132  {
1133  return Translate(Rotate(v));
1134  }
1135 
1136  Transform operator*(const Transform& other) const
1137  {
1138  return Transform(Rotation * other.Rotation, Apply(other.Translation));
1139  }
1140 
1141  PoseState<T> operator*(const PoseState<T>& poseState) const
1142  {
1143  PoseState<T> result;
1144  result.Pose = (*this) * poseState.Pose;
1145  result.LinearVelocity = this->Rotate(poseState.LinearVelocity);
1146  result.LinearAcceleration = this->Rotate(poseState.LinearAcceleration);
1147  result.AngularVelocity = this->Rotate(poseState.AngularVelocity);
1148  result.AngularAcceleration = this->Rotate(poseState.AngularAcceleration);
1149  return result;
1150  }
1151 
1153  {
1154  Quat<T> inv = Rotation.Inverted();
1155  return Transform(inv, inv.Rotate(-Translation));
1156  }
1157 };
1158 
1161 
1162 
1163 //-------------------------------------------------------------------------------------
1164 // ***** Matrix4
1165 //
1166 // Matrix4 is a 4x4 matrix used for 3d transformations and projections.
1167 // Translation stored in the last column.
1168 // The matrix is stored in row-major order in memory, meaning that values
1169 // of the first row are stored before the next one.
1170 //
1171 // The arrangement of the matrix is chosen to be in Right-Handed
1172 // coordinate system and counterclockwise rotations when looking down
1173 // the axis
1174 //
1175 // Transformation Order:
1176 // - Transformations are applied from right to left, so the expression
1177 // M1 * M2 * M3 * V means that the vector V is transformed by M3 first,
1178 // followed by M2 and M1.
1179 //
1180 // Coordinate system: Right Handed
1181 //
1182 // Rotations: Counterclockwise when looking down the axis. All angles are in radians.
1183 //
1184 // | sx 01 02 tx | // First column (sx, 10, 20): Axis X basis vector.
1185 // | 10 sy 12 ty | // Second column (01, sy, 21): Axis Y basis vector.
1186 // | 20 21 sz tz | // Third columnt (02, 12, sz): Axis Z basis vector.
1187 // | 30 31 32 33 |
1188 //
1189 // The basis vectors are first three columns.
1190 
1191 template<class T>
1192 class Matrix4
1193 {
1194  static const Matrix4 IdentityValue;
1195 
1196 public:
1197  T M[4][4];
1198 
1199  enum NoInitType { NoInit };
1200 
1201  // Construct with no memory initialization.
1203 
1204  // By default, we construct identity matrix.
1206  {
1207  SetIdentity();
1208  }
1209 
1210  Matrix4(T m11, T m12, T m13, T m14,
1211  T m21, T m22, T m23, T m24,
1212  T m31, T m32, T m33, T m34,
1213  T m41, T m42, T m43, T m44)
1214  {
1215  M[0][0] = m11; M[0][1] = m12; M[0][2] = m13; M[0][3] = m14;
1216  M[1][0] = m21; M[1][1] = m22; M[1][2] = m23; M[1][3] = m24;
1217  M[2][0] = m31; M[2][1] = m32; M[2][2] = m33; M[2][3] = m34;
1218  M[3][0] = m41; M[3][1] = m42; M[3][2] = m43; M[3][3] = m44;
1219  }
1220 
1221  Matrix4(T m11, T m12, T m13,
1222  T m21, T m22, T m23,
1223  T m31, T m32, T m33)
1224  {
1225  M[0][0] = m11; M[0][1] = m12; M[0][2] = m13; M[0][3] = 0;
1226  M[1][0] = m21; M[1][1] = m22; M[1][2] = m23; M[1][3] = 0;
1227  M[2][0] = m31; M[2][1] = m32; M[2][2] = m33; M[2][3] = 0;
1228  M[3][0] = 0; M[3][1] = 0; M[3][2] = 0; M[3][3] = 1;
1229  }
1230 
1231  explicit Matrix4(const Quat<T>& q)
1232  {
1233  T ww = q.w*q.w;
1234  T xx = q.x*q.x;
1235  T yy = q.y*q.y;
1236  T zz = q.z*q.z;
1237 
1238  M[0][0] = ww + xx - yy - zz; M[0][1] = 2 * (q.x*q.y - q.w*q.z); M[0][2] = 2 * (q.x*q.z + q.w*q.y); M[0][3] = 0;
1239  M[1][0] = 2 * (q.x*q.y + q.w*q.z); M[1][1] = ww - xx + yy - zz; M[1][2] = 2 * (q.y*q.z - q.w*q.x); M[1][3] = 0;
1240  M[2][0] = 2 * (q.x*q.z - q.w*q.y); M[2][1] = 2 * (q.y*q.z + q.w*q.x); M[2][2] = ww - xx - yy + zz; M[2][3] = 0;
1241  M[3][0] = 0; M[3][1] = 0; M[3][2] = 0; M[3][3] = 1;
1242  }
1243 
1244  explicit Matrix4(const Transform<T>& p)
1245  {
1246  Matrix4 result(p.Rotation);
1247  result.SetTranslation(p.Translation);
1248  *this = result;
1249  }
1250 
1251  // C-interop support
1252  explicit Matrix4(const Matrix4<typename Math<T>::OtherFloatType> &src)
1253  {
1254  for (int i = 0; i < 4; i++)
1255  for (int j = 0; j < 4; j++)
1256  M[i][j] = (T)src.M[i][j];
1257  }
1258 
1259  // C-interop support.
1260  Matrix4(const typename CompatibleTypes<Matrix4<T> >::Type& s)
1261  {
1262  OVR_COMPILER_ASSERT(sizeof(s) == sizeof(Matrix4));
1263  memcpy(M, s.M, sizeof(M));
1264  }
1265 
1266  operator typename CompatibleTypes<Matrix4<T> >::Type () const
1267  {
1268  typename CompatibleTypes<Matrix4<T> >::Type result;
1269  OVR_COMPILER_ASSERT(sizeof(result) == sizeof(Matrix4));
1270  memcpy(result.M, M, sizeof(M));
1271  return result;
1272  }
1273 
1274  void ToString(char* dest, UPInt destsize) const
1275  {
1276  UPInt pos = 0;
1277  for (int r=0; r<4; r++)
1278  for (int c=0; c<4; c++)
1279  pos += OVR_sprintf(dest+pos, destsize-pos, "%g ", M[r][c]);
1280  }
1281 
1282  static Matrix4 FromString(const char* src)
1283  {
1284  Matrix4 result;
1285  for (int r=0; r<4; r++)
1286  for (int c=0; c<4; c++)
1287  {
1288  result.M[r][c] = (T)atof(src);
1289  while (src && *src != ' ')
1290  src++;
1291  while (src && *src == ' ')
1292  src++;
1293  }
1294  return result;
1295  }
1296 
1297  static const Matrix4& Identity() { return IdentityValue; }
1298 
1300  {
1301  M[0][0] = M[1][1] = M[2][2] = M[3][3] = 1;
1302  M[0][1] = M[1][0] = M[2][3] = M[3][1] = 0;
1303  M[0][2] = M[1][2] = M[2][0] = M[3][2] = 0;
1304  M[0][3] = M[1][3] = M[2][1] = M[3][0] = 0;
1305  }
1306 
1307  bool operator== (const Matrix4& b) const
1308  {
1309  bool isEqual = true;
1310  for (int i = 0; i < 4; i++)
1311  for (int j = 0; j < 4; j++)
1312  isEqual &= (M[i][j] == b.M[i][j]);
1313 
1314  return isEqual;
1315  }
1316 
1317  Matrix4 operator+ (const Matrix4& b) const
1318  {
1319  Matrix4 result(*this);
1320  result += b;
1321  return result;
1322  }
1323 
1325  {
1326  for (int i = 0; i < 4; i++)
1327  for (int j = 0; j < 4; j++)
1328  M[i][j] += b.M[i][j];
1329  return *this;
1330  }
1331 
1332  Matrix4 operator- (const Matrix4& b) const
1333  {
1334  Matrix4 result(*this);
1335  result -= b;
1336  return result;
1337  }
1338 
1340  {
1341  for (int i = 0; i < 4; i++)
1342  for (int j = 0; j < 4; j++)
1343  M[i][j] -= b.M[i][j];
1344  return *this;
1345  }
1346 
1347  // Multiplies two matrices into destination with minimum copying.
1348  static Matrix4& Multiply(Matrix4* d, const Matrix4& a, const Matrix4& b)
1349  {
1350  OVR_ASSERT((d != &a) && (d != &b));
1351  int i = 0;
1352  do {
1353  d->M[i][0] = a.M[i][0] * b.M[0][0] + a.M[i][1] * b.M[1][0] + a.M[i][2] * b.M[2][0] + a.M[i][3] * b.M[3][0];
1354  d->M[i][1] = a.M[i][0] * b.M[0][1] + a.M[i][1] * b.M[1][1] + a.M[i][2] * b.M[2][1] + a.M[i][3] * b.M[3][1];
1355  d->M[i][2] = a.M[i][0] * b.M[0][2] + a.M[i][1] * b.M[1][2] + a.M[i][2] * b.M[2][2] + a.M[i][3] * b.M[3][2];
1356  d->M[i][3] = a.M[i][0] * b.M[0][3] + a.M[i][1] * b.M[1][3] + a.M[i][2] * b.M[2][3] + a.M[i][3] * b.M[3][3];
1357  } while((++i) < 4);
1358 
1359  return *d;
1360  }
1361 
1362  Matrix4 operator* (const Matrix4& b) const
1363  {
1364  Matrix4 result(Matrix4::NoInit);
1365  Multiply(&result, *this, b);
1366  return result;
1367  }
1368 
1370  {
1371  return Multiply(this, Matrix4(*this), b);
1372  }
1373 
1374  Matrix4 operator* (T s) const
1375  {
1376  Matrix4 result(*this);
1377  result *= s;
1378  return result;
1379  }
1380 
1382  {
1383  for (int i = 0; i < 4; i++)
1384  for (int j = 0; j < 4; j++)
1385  M[i][j] *= s;
1386  return *this;
1387  }
1388 
1389 
1390  Matrix4 operator/ (T s) const
1391  {
1392  Matrix4 result(*this);
1393  result /= s;
1394  return result;
1395  }
1396 
1398  {
1399  for (int i = 0; i < 4; i++)
1400  for (int j = 0; j < 4; j++)
1401  M[i][j] /= s;
1402  return *this;
1403  }
1404 
1406  {
1407  return Vector3<T>(M[0][0] * v.x + M[0][1] * v.y + M[0][2] * v.z + M[0][3],
1408  M[1][0] * v.x + M[1][1] * v.y + M[1][2] * v.z + M[1][3],
1409  M[2][0] * v.x + M[2][1] * v.y + M[2][2] * v.z + M[2][3]);
1410  }
1411 
1413  {
1414  return Matrix4(M[0][0], M[1][0], M[2][0], M[3][0],
1415  M[0][1], M[1][1], M[2][1], M[3][1],
1416  M[0][2], M[1][2], M[2][2], M[3][2],
1417  M[0][3], M[1][3], M[2][3], M[3][3]);
1418  }
1419 
1420  void Transpose()
1421  {
1422  *this = Transposed();
1423  }
1424 
1425 
1426  T SubDet (const UPInt* rows, const UPInt* cols) const
1427  {
1428  return M[rows[0]][cols[0]] * (M[rows[1]][cols[1]] * M[rows[2]][cols[2]] - M[rows[1]][cols[2]] * M[rows[2]][cols[1]])
1429  - M[rows[0]][cols[1]] * (M[rows[1]][cols[0]] * M[rows[2]][cols[2]] - M[rows[1]][cols[2]] * M[rows[2]][cols[0]])
1430  + M[rows[0]][cols[2]] * (M[rows[1]][cols[0]] * M[rows[2]][cols[1]] - M[rows[1]][cols[1]] * M[rows[2]][cols[0]]);
1431  }
1432 
1433  T Cofactor(UPInt I, UPInt J) const
1434  {
1435  const UPInt indices[4][3] = {{1,2,3},{0,2,3},{0,1,3},{0,1,2}};
1436  return ((I+J)&1) ? -SubDet(indices[I],indices[J]) : SubDet(indices[I],indices[J]);
1437  }
1438 
1439  T Determinant() const
1440  {
1441  return M[0][0] * Cofactor(0,0) + M[0][1] * Cofactor(0,1) + M[0][2] * Cofactor(0,2) + M[0][3] * Cofactor(0,3);
1442  }
1443 
1445  {
1446  return Matrix4(Cofactor(0,0), Cofactor(1,0), Cofactor(2,0), Cofactor(3,0),
1447  Cofactor(0,1), Cofactor(1,1), Cofactor(2,1), Cofactor(3,1),
1448  Cofactor(0,2), Cofactor(1,2), Cofactor(2,2), Cofactor(3,2),
1449  Cofactor(0,3), Cofactor(1,3), Cofactor(2,3), Cofactor(3,3));
1450  }
1451 
1453  {
1454  T det = Determinant();
1455  assert(det != 0);
1456  return Adjugated() * (1.0f/det);
1457  }
1458 
1459  void Invert()
1460  {
1461  *this = Inverted();
1462  }
1463 
1464  // This is more efficient than general inverse, but ONLY works
1465  // correctly if it is a homogeneous transform matrix (rot + trans)
1467  {
1468  // Make the inverse rotation matrix
1469  Matrix4 rinv = this->Transposed();
1470  rinv.M[3][0] = rinv.M[3][1] = rinv.M[3][2] = 0.0f;
1471  // Make the inverse translation matrix
1472  Vector3<T> tvinv(-M[0][3],-M[1][3],-M[2][3]);
1473  Matrix4 tinv = Matrix4::Translation(tvinv);
1474  return rinv * tinv; // "untranslate", then "unrotate"
1475  }
1476 
1477  // This is more efficient than general inverse, but ONLY works
1478  // correctly if it is a homogeneous transform matrix (rot + trans)
1480  {
1481  *this = InvertedHomogeneousTransform();
1482  }
1483 
1484  // Matrix to Euler Angles conversion
1485  // a,b,c, are the YawPitchRoll angles to be returned
1486  // rotation a around axis A1
1487  // is followed by rotation b around axis A2
1488  // is followed by rotation c around axis A3
1489  // rotations are CCW or CW (D) in LH or RH coordinate system (S)
1490  template <Axis A1, Axis A2, Axis A3, RotateDirection D, HandedSystem S>
1491  void ToEulerAngles(T *a, T *b, T *c)
1492  {
1493  OVR_COMPILER_ASSERT((A1 != A2) && (A2 != A3) && (A1 != A3));
1494 
1495  T psign = -1;
1496  if (((A1 + 1) % 3 == A2) && ((A2 + 1) % 3 == A3)) // Determine whether even permutation
1497  psign = 1;
1498 
1499  T pm = psign*M[A1][A3];
1500  if (pm < -1.0f + Math<T>::SingularityRadius)
1501  { // South pole singularity
1502  *a = 0;
1503  *b = -S*D*Math<T>::PiOver2;
1504  *c = S*D*atan2( psign*M[A2][A1], M[A2][A2] );
1505  }
1506  else if (pm > 1.0f - Math<T>::SingularityRadius)
1507  { // North pole singularity
1508  *a = 0;
1509  *b = S*D*Math<T>::PiOver2;
1510  *c = S*D*atan2( psign*M[A2][A1], M[A2][A2] );
1511  }
1512  else
1513  { // Normal case (nonsingular)
1514  *a = S*D*atan2( -psign*M[A2][A3], M[A3][A3] );
1515  *b = S*D*asin(pm);
1516  *c = S*D*atan2( -psign*M[A1][A2], M[A1][A1] );
1517  }
1518 
1519  return;
1520  }
1521 
1522  // Matrix to Euler Angles conversion
1523  // a,b,c, are the YawPitchRoll angles to be returned
1524  // rotation a around axis A1
1525  // is followed by rotation b around axis A2
1526  // is followed by rotation c around axis A1
1527  // rotations are CCW or CW (D) in LH or RH coordinate system (S)
1528  template <Axis A1, Axis A2, RotateDirection D, HandedSystem S>
1529  void ToEulerAnglesABA(T *a, T *b, T *c)
1530  {
1531  OVR_COMPILER_ASSERT(A1 != A2);
1532 
1533  // Determine the axis that was not supplied
1534  int m = 3 - A1 - A2;
1535 
1536  T psign = -1;
1537  if ((A1 + 1) % 3 == A2) // Determine whether even permutation
1538  psign = 1.0f;
1539 
1540  T c2 = M[A1][A1];
1541  if (c2 < -1 + Math<T>::SingularityRadius)
1542  { // South pole singularity
1543  *a = 0;
1544  *b = S*D*Math<T>::Pi;
1545  *c = S*D*atan2( -psign*M[A2][m],M[A2][A2]);
1546  }
1547  else if (c2 > 1.0f - Math<T>::SingularityRadius)
1548  { // North pole singularity
1549  *a = 0;
1550  *b = 0;
1551  *c = S*D*atan2( -psign*M[A2][m],M[A2][A2]);
1552  }
1553  else
1554  { // Normal case (nonsingular)
1555  *a = S*D*atan2( M[A2][A1],-psign*M[m][A1]);
1556  *b = S*D*acos(c2);
1557  *c = S*D*atan2( M[A1][A2],psign*M[A1][m]);
1558  }
1559  return;
1560  }
1561 
1562  // Creates a matrix that converts the vertices from one coordinate system
1563  // to another.
1564  static Matrix4 AxisConversion(const WorldAxes& to, const WorldAxes& from)
1565  {
1566  // Holds axis values from the 'to' structure
1567  int toArray[3] = { to.XAxis, to.YAxis, to.ZAxis };
1568 
1569  // The inverse of the toArray
1570  int inv[4];
1571  inv[0] = inv[abs(to.XAxis)] = 0;
1572  inv[abs(to.YAxis)] = 1;
1573  inv[abs(to.ZAxis)] = 2;
1574 
1575  Matrix4 m(0, 0, 0,
1576  0, 0, 0,
1577  0, 0, 0);
1578 
1579  // Only three values in the matrix need to be changed to 1 or -1.
1580  m.M[inv[abs(from.XAxis)]][0] = T(from.XAxis/toArray[inv[abs(from.XAxis)]]);
1581  m.M[inv[abs(from.YAxis)]][1] = T(from.YAxis/toArray[inv[abs(from.YAxis)]]);
1582  m.M[inv[abs(from.ZAxis)]][2] = T(from.ZAxis/toArray[inv[abs(from.ZAxis)]]);
1583  return m;
1584  }
1585 
1586 
1587  // Creates a matrix for translation by vector
1588  static Matrix4 Translation(const Vector3<T>& v)
1589  {
1590  Matrix4 t;
1591  t.M[0][3] = v.x;
1592  t.M[1][3] = v.y;
1593  t.M[2][3] = v.z;
1594  return t;
1595  }
1596 
1597  // Creates a matrix for translation by vector
1598  static Matrix4 Translation(T x, T y, T z = 0.0f)
1599  {
1600  Matrix4 t;
1601  t.M[0][3] = x;
1602  t.M[1][3] = y;
1603  t.M[2][3] = z;
1604  return t;
1605  }
1606 
1607  // Sets the translation part
1609  {
1610  M[0][3] = v.x;
1611  M[1][3] = v.y;
1612  M[2][3] = v.z;
1613  }
1614 
1616  {
1617  return Vector3<T>( M[0][3], M[1][3], M[2][3] );
1618  }
1619 
1620  // Creates a matrix for scaling by vector
1621  static Matrix4 Scaling(const Vector3<T>& v)
1622  {
1623  Matrix4 t;
1624  t.M[0][0] = v.x;
1625  t.M[1][1] = v.y;
1626  t.M[2][2] = v.z;
1627  return t;
1628  }
1629 
1630  // Creates a matrix for scaling by vector
1631  static Matrix4 Scaling(T x, T y, T z)
1632  {
1633  Matrix4 t;
1634  t.M[0][0] = x;
1635  t.M[1][1] = y;
1636  t.M[2][2] = z;
1637  return t;
1638  }
1639 
1640  // Creates a matrix for scaling by constant
1641  static Matrix4 Scaling(T s)
1642  {
1643  Matrix4 t;
1644  t.M[0][0] = s;
1645  t.M[1][1] = s;
1646  t.M[2][2] = s;
1647  return t;
1648  }
1649 
1650  // Simple L1 distance in R^12
1651  T Distance(const Matrix4& m2) const
1652  {
1653  T d = fabs(M[0][0] - m2.M[0][0]) + fabs(M[0][1] - m2.M[0][1]);
1654  d += fabs(M[0][2] - m2.M[0][2]) + fabs(M[0][3] - m2.M[0][3]);
1655  d += fabs(M[1][0] - m2.M[1][0]) + fabs(M[1][1] - m2.M[1][1]);
1656  d += fabs(M[1][2] - m2.M[1][2]) + fabs(M[1][3] - m2.M[1][3]);
1657  d += fabs(M[2][0] - m2.M[2][0]) + fabs(M[2][1] - m2.M[2][1]);
1658  d += fabs(M[2][2] - m2.M[2][2]) + fabs(M[2][3] - m2.M[2][3]);
1659  d += fabs(M[3][0] - m2.M[3][0]) + fabs(M[3][1] - m2.M[3][1]);
1660  d += fabs(M[3][2] - m2.M[3][2]) + fabs(M[3][3] - m2.M[3][3]);
1661  return d;
1662  }
1663 
1664  // Creates a rotation matrix rotating around the X axis by 'angle' radians.
1665  // Just for quick testing. Not for final API. Need to remove case.
1667  {
1668  T sina = s * d *sin(angle);
1669  T cosa = cos(angle);
1670 
1671  switch(A)
1672  {
1673  case Axis_X:
1674  return Matrix4(1, 0, 0,
1675  0, cosa, -sina,
1676  0, sina, cosa);
1677  case Axis_Y:
1678  return Matrix4(cosa, 0, sina,
1679  0, 1, 0,
1680  -sina, 0, cosa);
1681  case Axis_Z:
1682  return Matrix4(cosa, -sina, 0,
1683  sina, cosa, 0,
1684  0, 0, 1);
1685  }
1686  }
1687 
1688 
1689  // Creates a rotation matrix rotating around the X axis by 'angle' radians.
1690  // Rotation direction is depends on the coordinate system:
1691  // RHS (Oculus default): Positive angle values rotate Counter-clockwise (CCW),
1692  // while looking in the negative axis direction. This is the
1693  // same as looking down from positive axis values towards origin.
1694  // LHS: Positive angle values rotate clock-wise (CW), while looking in the
1695  // negative axis direction.
1696  static Matrix4 RotationX(T angle)
1697  {
1698  T sina = sin(angle);
1699  T cosa = cos(angle);
1700  return Matrix4(1, 0, 0,
1701  0, cosa, -sina,
1702  0, sina, cosa);
1703  }
1704 
1705  // Creates a rotation matrix rotating around the Y axis by 'angle' radians.
1706  // Rotation direction is depends on the coordinate system:
1707  // RHS (Oculus default): Positive angle values rotate Counter-clockwise (CCW),
1708  // while looking in the negative axis direction. This is the
1709  // same as looking down from positive axis values towards origin.
1710  // LHS: Positive angle values rotate clock-wise (CW), while looking in the
1711  // negative axis direction.
1712  static Matrix4 RotationY(T angle)
1713  {
1714  T sina = sin(angle);
1715  T cosa = cos(angle);
1716  return Matrix4(cosa, 0, sina,
1717  0, 1, 0,
1718  -sina, 0, cosa);
1719  }
1720 
1721  // Creates a rotation matrix rotating around the Z axis by 'angle' radians.
1722  // Rotation direction is depends on the coordinate system:
1723  // RHS (Oculus default): Positive angle values rotate Counter-clockwise (CCW),
1724  // while looking in the negative axis direction. This is the
1725  // same as looking down from positive axis values towards origin.
1726  // LHS: Positive angle values rotate clock-wise (CW), while looking in the
1727  // negative axis direction.
1728  static Matrix4 RotationZ(T angle)
1729  {
1730  T sina = sin(angle);
1731  T cosa = cos(angle);
1732  return Matrix4(cosa, -sina, 0,
1733  sina, cosa, 0,
1734  0, 0, 1);
1735  }
1736 
1737  // LookAtRH creates a View transformation matrix for right-handed coordinate system.
1738  // The resulting matrix points camera from 'eye' towards 'at' direction, with 'up'
1739  // specifying the up vector. The resulting matrix should be used with PerspectiveRH
1740  // projection.
1741  static Matrix4 LookAtRH(const Vector3<T>& eye, const Vector3<T>& at, const Vector3<T>& up)
1742  {
1743  Vector3<T> z = (eye - at).Normalized(); // Forward
1744  Vector3<T> x = up.Cross(z).Normalized(); // Right
1745  Vector3<T> y = z.Cross(x);
1746 
1747  Matrix4 m(x.x, x.y, x.z, -(x.Dot(eye)),
1748  y.x, y.y, y.z, -(y.Dot(eye)),
1749  z.x, z.y, z.z, -(z.Dot(eye)),
1750  0, 0, 0, 1 );
1751  return m;
1752  }
1753 
1754  // LookAtLH creates a View transformation matrix for left-handed coordinate system.
1755  // The resulting matrix points camera from 'eye' towards 'at' direction, with 'up'
1756  // specifying the up vector.
1757  static Matrix4 LookAtLH(const Vector3<T>& eye, const Vector3<T>& at, const Vector3<T>& up)
1758  {
1759  Vector3<T> z = (at - eye).Normalized(); // Forward
1760  Vector3<T> x = up.Cross(z).Normalized(); // Right
1761  Vector3<T> y = z.Cross(x);
1762 
1763  Matrix4 m(x.x, x.y, x.z, -(x.Dot(eye)),
1764  y.x, y.y, y.z, -(y.Dot(eye)),
1765  z.x, z.y, z.z, -(z.Dot(eye)),
1766  0, 0, 0, 1 );
1767  return m;
1768  }
1769 
1770  // PerspectiveRH creates a right-handed perspective projection matrix that can be
1771  // used with the Oculus sample renderer.
1772  // yfov - Specifies vertical field of view in radians.
1773  // aspect - Screen aspect ration, which is usually width/height for square pixels.
1774  // Note that xfov = yfov * aspect.
1775  // znear - Absolute value of near Z clipping clipping range.
1776  // zfar - Absolute value of far Z clipping clipping range (larger then near).
1777  // Even though RHS usually looks in the direction of negative Z, positive values
1778  // are expected for znear and zfar.
1779  static Matrix4 PerspectiveRH(T yfov, T aspect, T znear, T zfar)
1780  {
1781  Matrix4 m;
1782  T tanHalfFov = tan(yfov * 0.5f);
1783 
1784  m.M[0][0] = 1 / (aspect * tanHalfFov);
1785  m.M[1][1] = 1 / tanHalfFov;
1786  m.M[2][2] = zfar / (zfar - znear);
1787  m.M[3][2] = 1;
1788  m.M[2][3] = (zfar * znear) / (znear - zfar);
1789  m.M[3][3] = 0;
1790 
1791  // Note: Post-projection matrix result assumes Left-Handed coordinate system,
1792  // with Y up, X right and Z forward. This supports positive z-buffer values.
1793  return m;
1794  }
1795 
1796  // PerspectiveRH creates a left-handed perspective projection matrix that can be
1797  // used with the Oculus sample renderer.
1798  // yfov - Specifies vertical field of view in radians.
1799  // aspect - Screen aspect ration, which is usually width/height for square pixels.
1800  // Note that xfov = yfov * aspect.
1801  // znear - Absolute value of near Z clipping clipping range.
1802  // zfar - Absolute value of far Z clipping clipping range (larger then near).
1803  static Matrix4 PerspectiveLH(T yfov, T aspect, T znear, T zfar)
1804  {
1805  Matrix4 m;
1806  T tanHalfFov = tan(yfov * 0.5f);
1807 
1808  m.M[0][0] = 1.0 / (aspect * tanHalfFov);
1809  m.M[1][1] = 1.0 / tanHalfFov;
1810  m.M[2][2] = zfar / (znear - zfar);
1811  // m.M[2][2] = zfar / (zfar - znear);
1812  m.M[3][2] = -1.0;
1813  m.M[2][3] = (zfar * znear) / (znear - zfar);
1814  m.M[3][3] = 0.0;
1815 
1816  // Note: Post-projection matrix result assumes Left-Handed coordinate system,
1817  // with Y up, X right and Z forward. This supports positive z-buffer values.
1818  // This is the case even for RHS cooridnate input.
1819  return m;
1820  }
1821 
1822  static Matrix4 Ortho2D(T w, T h)
1823  {
1824  Matrix4 m;
1825  m.M[0][0] = 2.0/w;
1826  m.M[1][1] = -2.0/h;
1827  m.M[0][3] = -1.0;
1828  m.M[1][3] = 1.0;
1829  m.M[2][2] = 0;
1830  return m;
1831  }
1832 };
1833 
1836 
1837 //-------------------------------------------------------------------------------------
1838 // ***** Matrix3
1839 //
1840 // Matrix3 is a 3x3 matrix used for representing a rotation matrix.
1841 // The matrix is stored in row-major order in memory, meaning that values
1842 // of the first row are stored before the next one.
1843 //
1844 // The arrangement of the matrix is chosen to be in Right-Handed
1845 // coordinate system and counterclockwise rotations when looking down
1846 // the axis
1847 //
1848 // Transformation Order:
1849 // - Transformations are applied from right to left, so the expression
1850 // M1 * M2 * M3 * V means that the vector V is transformed by M3 first,
1851 // followed by M2 and M1.
1852 //
1853 // Coordinate system: Right Handed
1854 //
1855 // Rotations: Counterclockwise when looking down the axis. All angles are in radians.
1856 
1857 template<typename T>
1858 class SymMat3;
1859 
1860 template<class T>
1861 class Matrix3
1862 {
1863  static const Matrix3 IdentityValue;
1864 
1865 public:
1866  T M[3][3];
1867 
1868  enum NoInitType { NoInit };
1869 
1870  // Construct with no memory initialization.
1872 
1873  // By default, we construct identity matrix.
1875  {
1876  SetIdentity();
1877  }
1878 
1879  Matrix3(T m11, T m12, T m13,
1880  T m21, T m22, T m23,
1881  T m31, T m32, T m33)
1882  {
1883  M[0][0] = m11; M[0][1] = m12; M[0][2] = m13;
1884  M[1][0] = m21; M[1][1] = m22; M[1][2] = m23;
1885  M[2][0] = m31; M[2][1] = m32; M[2][2] = m33;
1886  }
1887 
1888  /*
1889  explicit Matrix3(const Quat<T>& q)
1890  {
1891  T ww = q.w*q.w;
1892  T xx = q.x*q.x;
1893  T yy = q.y*q.y;
1894  T zz = q.z*q.z;
1895 
1896  M[0][0] = ww + xx - yy - zz; M[0][1] = 2 * (q.x*q.y - q.w*q.z); M[0][2] = 2 * (q.x*q.z + q.w*q.y);
1897  M[1][0] = 2 * (q.x*q.y + q.w*q.z); M[1][1] = ww - xx + yy - zz; M[1][2] = 2 * (q.y*q.z - q.w*q.x);
1898  M[2][0] = 2 * (q.x*q.z - q.w*q.y); M[2][1] = 2 * (q.y*q.z + q.w*q.x); M[2][2] = ww - xx - yy + zz;
1899  }
1900  */
1901 
1902  explicit Matrix3(const Quat<T>& q)
1903  {
1904  const T tx = q.x+q.x, ty = q.y+q.y, tz = q.z+q.z;
1905  const T twx = q.w*tx, twy = q.w*ty, twz = q.w*tz;
1906  const T txx = q.x*tx, txy = q.x*ty, txz = q.x*tz;
1907  const T tyy = q.y*ty, tyz = q.y*tz, tzz = q.z*tz;
1908  M[0][0] = T(1) - (tyy + tzz); M[0][1] = txy - twz; M[0][2] = txz + twy;
1909  M[1][0] = txy + twz; M[1][1] = T(1) - (txx + tzz); M[1][2] = tyz - twx;
1910  M[2][0] = txz - twy; M[2][1] = tyz + twx; M[2][2] = T(1) - (txx + tyy);
1911  }
1912 
1913  inline explicit Matrix3(T s)
1914  {
1915  M[0][0] = M[1][1] = M[2][2] = s;
1916  M[0][1] = M[0][2] = M[1][0] = M[1][2] = M[2][0] = M[2][1] = 0;
1917  }
1918 
1919  explicit Matrix3(const Transform<T>& p)
1920  {
1921  Matrix3 result(p.Rotation);
1922  result.SetTranslation(p.Translation);
1923  *this = result;
1924  }
1925 
1926  // C-interop support
1927  explicit Matrix3(const Matrix4<typename Math<T>::OtherFloatType> &src)
1928  {
1929  for (int i = 0; i < 3; i++)
1930  for (int j = 0; j < 3; j++)
1931  M[i][j] = (T)src.M[i][j];
1932  }
1933 
1934  // C-interop support.
1935  Matrix3(const typename CompatibleTypes<Matrix3<T> >::Type& s)
1936  {
1937  OVR_COMPILER_ASSERT(sizeof(s) == sizeof(Matrix3));
1938  memcpy(M, s.M, sizeof(M));
1939  }
1940 
1941  operator typename CompatibleTypes<Matrix3<T> >::Type () const
1942  {
1943  typename CompatibleTypes<Matrix3<T> >::Type result;
1944  OVR_COMPILER_ASSERT(sizeof(result) == sizeof(Matrix3));
1945  memcpy(result.M, M, sizeof(M));
1946  return result;
1947  }
1948 
1949  void ToString(char* dest, UPInt destsize) const
1950  {
1951  UPInt pos = 0;
1952  for (int r=0; r<3; r++)
1953  for (int c=0; c<3; c++)
1954  pos += OVR_sprintf(dest+pos, destsize-pos, "%g ", M[r][c]);
1955  }
1956 
1957  static Matrix3 FromString(const char* src)
1958  {
1959  Matrix3 result;
1960  for (int r=0; r<3; r++)
1961  for (int c=0; c<3; c++)
1962  {
1963  result.M[r][c] = (T)atof(src);
1964  while (src && *src != ' ')
1965  src++;
1966  while (src && *src == ' ')
1967  src++;
1968  }
1969  return result;
1970  }
1971 
1972  static const Matrix3& Identity() { return IdentityValue; }
1973 
1975  {
1976  M[0][0] = M[1][1] = M[2][2] = 1;
1977  M[0][1] = M[1][0] = M[2][0] = 0;
1978  M[0][2] = M[1][2] = M[2][1] = 0;
1979  }
1980 
1981  bool operator== (const Matrix3& b) const
1982  {
1983  bool isEqual = true;
1984  for (int i = 0; i < 3; i++)
1985  for (int j = 0; j < 3; j++)
1986  isEqual &= (M[i][j] == b.M[i][j]);
1987 
1988  return isEqual;
1989  }
1990 
1991  Matrix3 operator+ (const Matrix3& b) const
1992  {
1993  Matrix4<T> result(*this);
1994  result += b;
1995  return result;
1996  }
1997 
1999  {
2000  for (int i = 0; i < 3; i++)
2001  for (int j = 0; j < 3; j++)
2002  M[i][j] += b.M[i][j];
2003  return *this;
2004  }
2005 
2006  void operator= (const Matrix3& b)
2007  {
2008  for (int i = 0; i < 3; i++)
2009  for (int j = 0; j < 3; j++)
2010  M[i][j] = b.M[i][j];
2011  return;
2012  }
2013 
2014  void operator= (const SymMat3<T>& b)
2015  {
2016  for (int i = 0; i < 3; i++)
2017  for (int j = 0; j < 3; j++)
2018  M[i][j] = 0;
2019 
2020  M[0][0] = b.v[0];
2021  M[0][1] = b.v[1];
2022  M[0][2] = b.v[2];
2023  M[1][1] = b.v[3];
2024  M[1][2] = b.v[4];
2025  M[2][2] = b.v[5];
2026 
2027  return;
2028  }
2029 
2030  Matrix3 operator- (const Matrix3& b) const
2031  {
2032  Matrix3 result(*this);
2033  result -= b;
2034  return result;
2035  }
2036 
2038  {
2039  for (int i = 0; i < 3; i++)
2040  for (int j = 0; j < 3; j++)
2041  M[i][j] -= b.M[i][j];
2042  return *this;
2043  }
2044 
2045  // Multiplies two matrices into destination with minimum copying.
2046  static Matrix3& Multiply(Matrix3* d, const Matrix3& a, const Matrix3& b)
2047  {
2048  OVR_ASSERT((d != &a) && (d != &b));
2049  int i = 0;
2050  do {
2051  d->M[i][0] = a.M[i][0] * b.M[0][0] + a.M[i][1] * b.M[1][0] + a.M[i][2] * b.M[2][0];
2052  d->M[i][1] = a.M[i][0] * b.M[0][1] + a.M[i][1] * b.M[1][1] + a.M[i][2] * b.M[2][1];
2053  d->M[i][2] = a.M[i][0] * b.M[0][2] + a.M[i][1] * b.M[1][2] + a.M[i][2] * b.M[2][2];
2054  } while((++i) < 3);
2055 
2056  return *d;
2057  }
2058 
2059  Matrix3 operator* (const Matrix3& b) const
2060  {
2061  Matrix3 result(Matrix3::NoInit);
2062  Multiply(&result, *this, b);
2063  return result;
2064  }
2065 
2067  {
2068  return Multiply(this, Matrix3(*this), b);
2069  }
2070 
2071  Matrix3 operator* (T s) const
2072  {
2073  Matrix3 result(*this);
2074  result *= s;
2075  return result;
2076  }
2077 
2079  {
2080  for (int i = 0; i < 3; i++)
2081  for (int j = 0; j < 3; j++)
2082  M[i][j] *= s;
2083  return *this;
2084  }
2085 
2087  {
2088  Vector3<T> result;
2089  result.x = M[0][0]*b.x + M[0][1]*b.y + M[0][2]*b.z;
2090  result.y = M[1][0]*b.x + M[1][1]*b.y + M[1][2]*b.z;
2091  result.z = M[2][0]*b.x + M[2][1]*b.y + M[2][2]*b.z;
2092 
2093  return result;
2094  }
2095 
2096  Matrix3 operator/ (T s) const
2097  {
2098  Matrix3 result(*this);
2099  result /= s;
2100  return result;
2101  }
2102 
2104  {
2105  for (int i = 0; i < 3; i++)
2106  for (int j = 0; j < 3; j++)
2107  M[i][j] /= s;
2108  return *this;
2109  }
2110 
2112  {
2113  return Vector3<T>(M[0][0] * v.x + M[0][1] * v.y + M[0][2] * v.z,
2114  M[1][0] * v.x + M[1][1] * v.y + M[1][2] * v.z,
2115  M[2][0] * v.x + M[2][1] * v.y + M[2][2] * v.z);
2116  }
2117 
2119  {
2120  return Matrix3(M[0][0], M[1][0], M[2][0],
2121  M[0][1], M[1][1], M[2][1],
2122  M[0][2], M[1][2], M[2][2]);
2123  }
2124 
2125  void Transpose()
2126  {
2127  *this = Transposed();
2128  }
2129 
2130 
2131  T SubDet (const UPInt* rows, const UPInt* cols) const
2132  {
2133  return M[rows[0]][cols[0]] * (M[rows[1]][cols[1]] * M[rows[2]][cols[2]] - M[rows[1]][cols[2]] * M[rows[2]][cols[1]])
2134  - M[rows[0]][cols[1]] * (M[rows[1]][cols[0]] * M[rows[2]][cols[2]] - M[rows[1]][cols[2]] * M[rows[2]][cols[0]])
2135  + M[rows[0]][cols[2]] * (M[rows[1]][cols[0]] * M[rows[2]][cols[1]] - M[rows[1]][cols[1]] * M[rows[2]][cols[0]]);
2136  }
2137 
2138  // M += a*b.t()
2139  inline void Rank1Add(const Vector3<T> &a, const Vector3<T> &b)
2140  {
2141  M[0][0] += a.x*b.x; M[0][1] += a.x*b.y; M[0][2] += a.x*b.z;
2142  M[1][0] += a.y*b.x; M[1][1] += a.y*b.y; M[1][2] += a.y*b.z;
2143  M[2][0] += a.z*b.x; M[2][1] += a.z*b.y; M[2][2] += a.z*b.z;
2144  }
2145 
2146  // M -= a*b.t()
2147  inline void Rank1Sub(const Vector3<T> &a, const Vector3<T> &b)
2148  {
2149  M[0][0] -= a.x*b.x; M[0][1] -= a.x*b.y; M[0][2] -= a.x*b.z;
2150  M[1][0] -= a.y*b.x; M[1][1] -= a.y*b.y; M[1][2] -= a.y*b.z;
2151  M[2][0] -= a.z*b.x; M[2][1] -= a.z*b.y; M[2][2] -= a.z*b.z;
2152  }
2153 
2154  inline Vector3<T> Col(int c) const
2155  {
2156  return Vector3<T>(M[0][c], M[1][c], M[2][c]);
2157  }
2158 
2159  inline Vector3<T> Row(int r) const
2160  {
2161  return Vector3<T>(M[r][0], M[r][1], M[r][2]);
2162  }
2163 
2164  inline T Determinant() const
2165  {
2166  const Matrix3<T>& m = *this;
2167  T d;
2168 
2169  d = m.M[0][0] * (m.M[1][1]*m.M[2][2] - m.M[1][2] * m.M[2][1]);
2170  d -= m.M[0][1] * (m.M[1][0]*m.M[2][2] - m.M[1][2] * m.M[2][0]);
2171  d += m.M[0][2] * (m.M[1][0]*m.M[2][1] - m.M[1][1] * m.M[2][0]);
2172 
2173  return d;
2174  }
2175 
2176  inline Matrix3<T> Inverse() const
2177  {
2178  Matrix3<T> a;
2179  const Matrix3<T>& m = *this;
2180  T d = Determinant();
2181 
2182  assert(d != 0);
2183  T s = T(1)/d;
2184 
2185  a.M[0][0] = s * (m.M[1][1] * m.M[2][2] - m.M[1][2] * m.M[2][1]);
2186  a.M[1][0] = s * (m.M[1][2] * m.M[2][0] - m.M[1][0] * m.M[2][2]);
2187  a.M[2][0] = s * (m.M[1][0] * m.M[2][1] - m.M[1][1] * m.M[2][0]);
2188 
2189  a.M[0][1] = s * (m.M[0][2] * m.M[2][1] - m.M[0][1] * m.M[2][2]);
2190  a.M[1][1] = s * (m.M[0][0] * m.M[2][2] - m.M[0][2] * m.M[2][0]);
2191  a.M[2][1] = s * (m.M[0][1] * m.M[2][0] - m.M[0][0] * m.M[2][1]);
2192 
2193  a.M[0][2] = s * (m.M[0][1] * m.M[1][2] - m.M[0][2] * m.M[1][1]);
2194  a.M[1][2] = s * (m.M[0][2] * m.M[1][0] - m.M[0][0] * m.M[1][2]);
2195  a.M[2][2] = s * (m.M[0][0] * m.M[1][1] - m.M[0][1] * m.M[1][0]);
2196 
2197  return a;
2198  }
2199 
2200 };
2201 
2204 
2205 //-------------------------------------------------------------------------------------
2206 
2207 template<typename T>
2208 class SymMat3
2209 {
2210 private:
2212 
2213 public:
2214  typedef T Value_t;
2215  // Upper symmetric
2216  T v[6]; // _00 _01 _02 _11 _12 _22
2217 
2218  inline SymMat3() {}
2219 
2220  inline explicit SymMat3(T s)
2221  {
2222  v[0] = v[3] = v[5] = s;
2223  v[1] = v[2] = v[4] = 0;
2224  }
2225 
2226  inline explicit SymMat3(T a00, T a01, T a02, T a11, T a12, T a22)
2227  {
2228  v[0] = a00; v[1] = a01; v[2] = a02;
2229  v[3] = a11; v[4] = a12;
2230  v[5] = a22;
2231  }
2232 
2233  static inline int Index(unsigned int i, unsigned int j)
2234  {
2235  return (i <= j) ? (3*i - i*(i+1)/2 + j) : (3*j - j*(j+1)/2 + i);
2236  }
2237 
2238  inline T operator()(int i, int j) const { return v[Index(i,j)]; }
2239 
2240  inline T &operator()(int i, int j) { return v[Index(i,j)]; }
2241 
2242  template<typename U>
2243  inline SymMat3<U> CastTo() const
2244  {
2245  return SymMat3<U>(static_cast<U>(v[0]), static_cast<U>(v[1]), static_cast<U>(v[2]),
2246  static_cast<U>(v[3]), static_cast<U>(v[4]), static_cast<U>(v[5]));
2247  }
2248 
2249  inline this_type& operator+=(const this_type& b)
2250  {
2251  v[0]+=b.v[0];
2252  v[1]+=b.v[1];
2253  v[2]+=b.v[2];
2254  v[3]+=b.v[3];
2255  v[4]+=b.v[4];
2256  v[5]+=b.v[5];
2257  return *this;
2258  }
2259 
2260  inline this_type& operator-=(const this_type& b)
2261  {
2262  v[0]-=b.v[0];
2263  v[1]-=b.v[1];
2264  v[2]-=b.v[2];
2265  v[3]-=b.v[3];
2266  v[4]-=b.v[4];
2267  v[5]-=b.v[5];
2268 
2269  return *this;
2270  }
2271 
2272  inline this_type& operator*=(T s)
2273  {
2274  v[0]*=s;
2275  v[1]*=s;
2276  v[2]*=s;
2277  v[3]*=s;
2278  v[4]*=s;
2279  v[5]*=s;
2280 
2281  return *this;
2282  }
2283 
2284  inline SymMat3 operator*(T s) const
2285  {
2286  SymMat3 d;
2287  d.v[0] = v[0]*s;
2288  d.v[1] = v[1]*s;
2289  d.v[2] = v[2]*s;
2290  d.v[3] = v[3]*s;
2291  d.v[4] = v[4]*s;
2292  d.v[5] = v[5]*s;
2293 
2294  return d;
2295  }
2296 
2297  // Multiplies two matrices into destination with minimum copying.
2298  static SymMat3& Multiply(SymMat3* d, const SymMat3& a, const SymMat3& b)
2299  {
2300  // _00 _01 _02 _11 _12 _22
2301 
2302  d->v[0] = a.v[0] * b.v[0];
2303  d->v[1] = a.v[0] * b.v[1] + a.v[1] * b.v[3];
2304  d->v[2] = a.v[0] * b.v[2] + a.v[1] * b.v[4];
2305 
2306  d->v[3] = a.v[3] * b.v[3];
2307  d->v[4] = a.v[3] * b.v[4] + a.v[4] * b.v[5];
2308 
2309  d->v[5] = a.v[5] * b.v[5];
2310 
2311  return *d;
2312  }
2313 
2314  inline T Determinant() const
2315  {
2316  const this_type& m = *this;
2317  T d;
2318 
2319  d = m(0,0) * (m(1,1)*m(2,2) - m(1,2) * m(2,1));
2320  d -= m(0,1) * (m(1,0)*m(2,2) - m(1,2) * m(2,0));
2321  d += m(0,2) * (m(1,0)*m(2,1) - m(1,1) * m(2,0));
2322 
2323  return d;
2324  }
2325 
2326  inline this_type Inverse() const
2327  {
2328  this_type a;
2329  const this_type& m = *this;
2330  T d = Determinant();
2331 
2332  assert(d != 0);
2333  T s = T(1)/d;
2334 
2335  a(0,0) = s * (m(1,1) * m(2,2) - m(1,2) * m(2,1));
2336 
2337  a(0,1) = s * (m(0,2) * m(2,1) - m(0,1) * m(2,2));
2338  a(1,1) = s * (m(0,0) * m(2,2) - m(0,2) * m(2,0));
2339 
2340  a(0,2) = s * (m(0,1) * m(1,2) - m(0,2) * m(1,1));
2341  a(1,2) = s * (m(0,2) * m(1,0) - m(0,0) * m(1,2));
2342  a(2,2) = s * (m(0,0) * m(1,1) - m(0,1) * m(1,0));
2343 
2344  return a;
2345  }
2346 
2347  inline T Trace() const { return v[0] + v[3] + v[5]; }
2348 
2349  // M = a*a.t()
2350  inline void Rank1(const Vector3<T> &a)
2351  {
2352  v[0] = a.x*a.x; v[1] = a.x*a.y; v[2] = a.x*a.z;
2353  v[3] = a.y*a.y; v[4] = a.y*a.z;
2354  v[5] = a.z*a.z;
2355  }
2356 
2357  // M += a*a.t()
2358  inline void Rank1Add(const Vector3<T> &a)
2359  {
2360  v[0] += a.x*a.x; v[1] += a.x*a.y; v[2] += a.x*a.z;
2361  v[3] += a.y*a.y; v[4] += a.y*a.z;
2362  v[5] += a.z*a.z;
2363  }
2364 
2365  // M -= a*a.t()
2366  inline void Rank1Sub(const Vector3<T> &a)
2367  {
2368  v[0] -= a.x*a.x; v[1] -= a.x*a.y; v[2] -= a.x*a.z;
2369  v[3] -= a.y*a.y; v[4] -= a.y*a.z;
2370  v[5] -= a.z*a.z;
2371  }
2372 };
2373 
2376 
2377 template<typename T>
2378 inline Matrix3<T> operator*(const SymMat3<T>& a, const SymMat3<T>& b)
2379 {
2380  #define AJB_ARBC(r,c) (a(r,0)*b(0,c)+a(r,1)*b(1,c)+a(r,2)*b(2,c))
2381  return Matrix3<T>(
2382  AJB_ARBC(0,0), AJB_ARBC(0,1), AJB_ARBC(0,2),
2383  AJB_ARBC(1,0), AJB_ARBC(1,1), AJB_ARBC(1,2),
2384  AJB_ARBC(2,0), AJB_ARBC(2,1), AJB_ARBC(2,2));
2385  #undef AJB_ARBC
2386 }
2387 
2388 template<typename T>
2389 inline Matrix3<T> operator*(const Matrix3<T>& a, const SymMat3<T>& b)
2390 {
2391  #define AJB_ARBC(r,c) (a(r,0)*b(0,c)+a(r,1)*b(1,c)+a(r,2)*b(2,c))
2392  return Matrix3<T>(
2393  AJB_ARBC(0,0), AJB_ARBC(0,1), AJB_ARBC(0,2),
2394  AJB_ARBC(1,0), AJB_ARBC(1,1), AJB_ARBC(1,2),
2395  AJB_ARBC(2,0), AJB_ARBC(2,1), AJB_ARBC(2,2));
2396  #undef AJB_ARBC
2397 }
2398 
2399 //-------------------------------------------------------------------------------------
2400 // ***** Angle
2401 
2402 // Cleanly representing the algebra of 2D rotations.
2403 // The operations maintain the angle between -Pi and Pi, the same range as atan2.
2404 
2405 template<class T>
2406 class Angle
2407 {
2408 public:
2410  {
2411  Radians = 0,
2413  };
2414 
2415  Angle() : a(0) {}
2416 
2417  // Fix the range to be between -Pi and Pi
2418  Angle(T a_, AngularUnits u = Radians) : a((u == Radians) ? a_ : a_*Math<T>::DegreeToRadFactor) { FixRange(); }
2419 
2420  T Get(AngularUnits u = Radians) const { return (u == Radians) ? a : a*Math<T>::RadToDegreeFactor; }
2421  void Set(const T& x, AngularUnits u = Radians) { a = (u == Radians) ? x : x*Math<T>::DegreeToRadFactor; FixRange(); }
2422  int Sign() const { if (a == 0) return 0; else return (a > 0) ? 1 : -1; }
2423  T Abs() const { return (a > 0) ? a : -a; }
2424 
2425  bool operator== (const Angle& b) const { return a == b.a; }
2426  bool operator!= (const Angle& b) const { return a != b.a; }
2427 // bool operator< (const Angle& b) const { return a < a.b; }
2428 // bool operator> (const Angle& b) const { return a > a.b; }
2429 // bool operator<= (const Angle& b) const { return a <= a.b; }
2430 // bool operator>= (const Angle& b) const { return a >= a.b; }
2431 // bool operator= (const T& x) { a = x; FixRange(); }
2432 
2433  // These operations assume a is already between -Pi and Pi.
2434  Angle& operator+= (const Angle& b) { a = a + b.a; FastFixRange(); return *this; }
2435  Angle& operator+= (const T& x) { a = a + x; FixRange(); return *this; }
2436  Angle operator+ (const Angle& b) const { Angle res = *this; res += b; return res; }
2437  Angle operator+ (const T& x) const { Angle res = *this; res += x; return res; }
2438  Angle& operator-= (const Angle& b) { a = a - b.a; FastFixRange(); return *this; }
2439  Angle& operator-= (const T& x) { a = a - x; FixRange(); return *this; }
2440  Angle operator- (const Angle& b) const { Angle res = *this; res -= b; return res; }
2441  Angle operator- (const T& x) const { Angle res = *this; res -= x; return res; }
2442 
2443  T Distance(const Angle& b) { T c = fabs(a - b.a); return (c <= Math<T>::Pi) ? c : Math<T>::TwoPi - c; }
2444 
2445 private:
2446 
2447  // The stored angle, which should be maintained between -Pi and Pi
2448  T a;
2449 
2450  // Fixes the angle range to [-Pi,Pi], but assumes no more than 2Pi away on either side
2451  inline void FastFixRange()
2452  {
2453  if (a < -Math<T>::Pi)
2454  a += Math<T>::TwoPi;
2455  else if (a > Math<T>::Pi)
2456  a -= Math<T>::TwoPi;
2457  }
2458 
2459  // Fixes the angle range to [-Pi,Pi] for any given range, but slower then the fast method
2460  inline void FixRange()
2461  {
2462  // do nothing if the value is already in the correct range, since fmod call is expensive
2463  if (a >= -Math<T>::Pi && a <= Math<T>::Pi)
2464  return;
2465  a = fmod(a,Math<T>::TwoPi);
2466  if (a < -Math<T>::Pi)
2467  a += Math<T>::TwoPi;
2468  else if (a > Math<T>::Pi)
2469  a -= Math<T>::TwoPi;
2470  }
2471 };
2472 
2473 
2476 
2477 
2478 //-------------------------------------------------------------------------------------
2479 // ***** Plane
2480 
2481 // Consists of a normal vector and distance from the origin where the plane is located.
2482 
2483 template<class T>
2484 class Plane : public RefCountBase<Plane<T> >
2485 {
2486 public:
2488  T D;
2489 
2490  Plane() : D(0) {}
2491 
2492  // Normals must already be normalized
2493  Plane(const Vector3<T>& n, T d) : N(n), D(d) {}
2494  Plane(T x, T y, T z, T d) : N(x,y,z), D(d) {}
2495 
2496  // construct from a point on the plane and the normal
2497  Plane(const Vector3<T>& p, const Vector3<T>& n) : N(n), D(-(p * n)) {}
2498 
2499  // Find the point to plane distance. The sign indicates what side of the plane the point is on (0 = point on plane).
2500  T TestSide(const Vector3<T>& p) const
2501  {
2502  return (N.Dot(p)) + D;
2503  }
2504 
2506  {
2507  return Plane(-N, -D);
2508  }
2509 
2510  void Flip()
2511  {
2512  N = -N;
2513  D = -D;
2514  }
2515 
2516  bool operator==(const Plane<T>& rhs) const
2517  {
2518  return (this->D == rhs.D && this->N == rhs.N);
2519  }
2520 };
2521 
2523 
2524 } // Namespace OVR
2525 
2526 #endif
T Distance(const Quat &q) const
Definition: OVR_Math.h:880
T M[3][3]
Definition: OVR_Math.h:1866
Angle & operator-=(const Angle &b)
Definition: OVR_Math.h:2438
void Rank1Sub(const Vector3< T > &a, const Vector3< T > &b)
Definition: OVR_Math.h:2147
Size & operator-=(const Size &b)
Definition: OVR_Math.h:611
Matrix4 operator/(T s) const
Definition: OVR_Math.h:1390
static const double PiOver2
Definition: OVR_Math.h:204
Matrix3 & operator*=(const Matrix3 &b)
Definition: OVR_Math.h:2066
CompatibleTypes< Size< T > >::Type CompatibleType
Definition: OVR_Math.h:595
Quat Inverted() const
Definition: OVR_Math.h:957
Vector3 operator-() const
Definition: OVR_Math.h:424
Vector2 & operator/=(T s)
Definition: OVR_Math.h:300
Matrix3(const typename CompatibleTypes< Matrix3< T > >::Type &s)
Definition: OVR_Math.h:1935
void FixRange()
Definition: OVR_Math.h:2460
static const double PiOver4
Definition: OVR_Math.h:205
AxisDirection XAxis
Definition: OVR_Math.h:85
Vector3 & operator+=(const Vector3 &b)
Definition: OVR_Math.h:421
Quat & operator*=(T s)
Definition: OVR_Math.h:865
Quat PowNormalized(T p) const
Definition: OVR_Math.h:934
Vector3< SInt32 > Vector3i
Definition: OVR_Math.h:556
struct ovrQuatd_ ovrQuatd
Definition: OVR_Math.h:103
bool operator==(const Matrix3 &b) const
Definition: OVR_Math.h:1981
Matrix3 operator*(const Matrix3 &b) const
Definition: OVR_Math.h:2059
Matrix3(const Quat< T > &q)
Definition: OVR_Math.h:1902
void Invert()
Definition: OVR_Math.h:1459
Size< unsigned > Sizeu
Definition: OVR_Math.h:637
T Length() const
Definition: OVR_Math.h:509
Vector3 Cross(const Vector3 &b) const
Definition: OVR_Math.h:492
Matrix4 & operator+=(const Matrix4 &b)
Definition: OVR_Math.h:1324
T Abs() const
Definition: OVR_Math.h:2423
CompatibleTypes< Transform< T > >::Type CompatibleType
Definition: OVR_Math.h:1098
Quat(Axis A, T angle, RotateDirection d=Rotate_CCW, HandedSystem s=Handed_R)
Definition: OVR_Math.h:740
void Set(const T &x, AngularUnits u=Radians)
Definition: OVR_Math.h:2421
T RadToDegree(T rads)
Definition: OVR_Math.h:226
Vector2(T s)
Definition: OVR_Math.h:268
void Normalize()
Definition: OVR_Math.h:521
bool operator!=(const Size &b) const
Definition: OVR_Math.h:606
T Dot(const Vector2 &b) const
Definition: OVR_Math.h:326
Transform operator*(const Transform &other) const
Definition: OVR_Math.h:1136
T Distance(Vector3 const &b) const
Definition: OVR_Math.h:515
Plane(const Vector3< T > &n, T d)
Definition: OVR_Math.h:2493
void InvertHomogeneousTransform()
Definition: OVR_Math.h:1479
Vector3(T s)
Definition: OVR_Math.h:401
CompatibleTypes< Vector3< T > >::Type CompatibleType
Definition: OVR_Math.h:407
Quat operator*(T s) const
Definition: OVR_Math.h:864
__BEGIN_NAMESPACE_STD void * memcpy(void *__restrict __dest, const void *__restrict __src, size_t __n) __THROW __nonnull((1
#define AJB_ARBC(r, c)
Rect(const CompatibleType &s)
Definition: OVR_Math.h:662
static const float SingularityRadius
Definition: OVR_Math.h:191
Angle operator-(const Angle &b) const
Definition: OVR_Math.h:2440
void SetPos(const Vector2< T > &pos)
Definition: OVR_Math.h:672
Size(const CompatibleType &s)
Definition: OVR_Math.h:597
Vector3< T > Imag() const
Definition: OVR_Math.h:871
Matrix3< float > Matrix3f
Definition: OVR_Math.h:2202
struct ovrSizef_ ovrSizef
Definition: OVR_Math.h:105
bool operator!=(const Angle &b) const
Definition: OVR_Math.h:2426
Size(const Size< typename Math< T >::OtherFloatType > &src)
Definition: OVR_Math.h:591
Vector2 Lerp(const Vector2 &b, T f) const
Definition: OVR_Math.h:369
Matrix3(const Transform< T > &p)
Definition: OVR_Math.h:1919
Quat operator/(T s) const
Definition: OVR_Math.h:866
T Angle(const Vector2 &b) const
Definition: OVR_Math.h:329
void Transpose()
Definition: OVR_Math.h:2125
this_type & operator+=(const this_type &b)
Definition: OVR_Math.h:2249
static Matrix3 & Multiply(Matrix3 *d, const Matrix3 &a, const Matrix3 &b)
Definition: OVR_Math.h:2046
static const float Pi
Definition: OVR_Math.h:178
static const double SingularityRadius
Definition: OVR_Math.h:215
T Cofactor(UPInt I, UPInt J) const
Definition: OVR_Math.h:1433
Matrix4 Inverted() const
Definition: OVR_Math.h:1452
Size< T > GetSize() const
Definition: OVR_Math.h:671
Matrix4 InvertedHomogeneousTransform() const
Definition: OVR_Math.h:1466
static Matrix4 Scaling(T s)
Definition: OVR_Math.h:1641
void operator=(const Matrix3 &b)
Definition: OVR_Math.h:2006
Size operator-() const
Definition: OVR_Math.h:612
void SetIdentity()
Definition: OVR_Math.h:1974
Quat & operator+=(const Quat &b)
Definition: OVR_Math.h:860
Vector3 ProjectToPlane(const Vector3 &normal) const
Definition: OVR_Math.h:550
Matrix4 Transposed() const
Definition: OVR_Math.h:1412
Matrix4(T m11, T m12, T m13, T m14, T m21, T m22, T m23, T m24, T m31, T m32, T m33, T m34, T m41, T m42, T m43, T m44)
Definition: OVR_Math.h:1210
Angle(T a_, AngularUnits u=Radians)
Definition: OVR_Math.h:2418
void GetEulerAngles(T *a, T *b, T *c) const
Definition: OVR_Math.h:1021
T DegreeToRad(T rads)
Definition: OVR_Math.h:228
Matrix4(T m11, T m12, T m13, T m21, T m22, T m23, T m31, T m32, T m33)
Definition: OVR_Math.h:1221
Matrix3 & operator/=(T s)
Definition: OVR_Math.h:2103
void Transpose()
Definition: OVR_Math.h:1420
void GetAxisAngle(Vector3< T > *axis, T *angle) const
Definition: OVR_Math.h:754
Quat(const Matrix3< T > &m)
Definition: OVR_Math.h:815
HandedSystem
Definition: OVR_Math.h:67
Vector2< double > Vector2d
Definition: OVR_Math.h:383
T Distance(const Matrix4 &m2) const
Definition: OVR_Math.h:1651
Rect(const Size< T > &sz)
Definition: OVR_Math.h:657
static Matrix4 & Multiply(Matrix4 *d, const Matrix4 &a, const Matrix4 &b)
Definition: OVR_Math.h:1348
void SetIdentity()
Definition: OVR_Math.h:1299
static const Matrix4 & Identity()
Definition: OVR_Math.h:1297
Vector2 operator-() const
Definition: OVR_Math.h:292
static const double DegreeToRadFactor
Definition: OVR_Math.h:212
void Flip()
Definition: OVR_Math.h:2510
static Matrix4 Scaling(const Vector3< T > &v)
Definition: OVR_Math.h:1621
Quat(T x_, T y_, T z_, T w_)
Definition: OVR_Math.h:701
size_t UPInt
Definition: OVR_Types.h:218
void Normalize()
Definition: OVR_Math.h:908
static Matrix4 RotationAxis(Axis A, T angle, RotateDirection d, HandedSystem s)
Definition: OVR_Math.h:1666
static Size Min(const Size &a, const Size &b)
Definition: OVR_Math.h:624
static Matrix4 FromString(const char *src)
Definition: OVR_Math.h:1282
Vector3< T > Rotate(const Vector3< T > &v) const
Definition: OVR_Math.h:951
Plane< float > Planef
Definition: OVR_Math.h:2522
Matrix3(T s)
Definition: OVR_Math.h:1913
Matrix3 & operator+=(const Matrix3 &b)
Definition: OVR_Math.h:1998
Vector2< int > Vector2i
Definition: OVR_Math.h:384
Vector4f(const Vector3f &v)
Definition: OVR_Math.h:570
void Invert()
Definition: OVR_Math.h:963
Vector2 Normalized() const
Definition: OVR_Math.h:360
Vector2< T > GetPos() const
Definition: OVR_Math.h:670
Transform< double > Transformd
Definition: OVR_Math.h:1160
Matrix3 operator+(const Matrix3 &b) const
Definition: OVR_Math.h:1991
Matrix4< double > Matrix4d
Definition: OVR_Math.h:1835
Vector2(const Vector2< typename Math< T >::OtherFloatType > &src)
Definition: OVR_Math.h:269
static Matrix3 FromString(const char *src)
Definition: OVR_Math.h:1957
Matrix4< float > Matrix4f
Definition: OVR_Math.h:1834
Transform(const Quat< T > &orientation, const Vector3< T > &pos)
Definition: OVR_Math.h:1101
Matrix4 operator-(const Matrix4 &b) const
Definition: OVR_Math.h:1332
bool operator==(const Angle &b) const
Definition: OVR_Math.h:2425
Vector3 operator+(const Vector3 &b) const
Definition: OVR_Math.h:420
bool operator==(const Vector2 &b) const
Definition: OVR_Math.h:285
static const float PiOver2
Definition: OVR_Math.h:180
static Matrix4 RotationX(T angle)
Definition: OVR_Math.h:1696
Vector3< T > AngularAcceleration
Axis
Definition: OVR_Math.h:48
void Rank1(const Vector3< T > &a)
Definition: OVR_Math.h:2350
Matrix4 Adjugated() const
Definition: OVR_Math.h:1444
Matrix3 operator/(T s) const
Definition: OVR_Math.h:2096
static const float TwoPi
Definition: OVR_Math.h:179
SymMat3< float > SymMat3f
Definition: OVR_Math.h:2374
Vector2< float > Vector2f
Definition: OVR_Math.h:382
T & operator()(int i, int j)
Definition: OVR_Math.h:2240
Angle< float > Anglef
Definition: OVR_Math.h:2474
Vector3< T > GetTranslation() const
Definition: OVR_Math.h:1615
Size(T w_, T h_)
Definition: OVR_Math.h:589
bool operator==(const Rect &vp) const
Definition: OVR_Math.h:675
bool operator!=(const Rect &vp) const
Definition: OVR_Math.h:677
T SubDet(const UPInt *rows, const UPInt *cols) const
Definition: OVR_Math.h:1426
static Matrix4 RotationY(T angle)
Definition: OVR_Math.h:1712
Quat(const typename CompatibleTypes< Quat< T > >::Type &s)
Definition: OVR_Math.h:706
Vector3 & operator*=(T s)
Definition: OVR_Math.h:428
SymMat3(T s)
Definition: OVR_Math.h:2220
Vector2(T x_, T y_)
Definition: OVR_Math.h:267
Angle< double > Angled
Definition: OVR_Math.h:2475
static const double Pi
Definition: OVR_Math.h:202
T Distance(Vector2 &b) const
Definition: OVR_Math.h:347
Transform(const Transform< typename Math< T >::OtherFloatType > &s)
Definition: OVR_Math.h:1107
T Area() const
Definition: OVR_Math.h:630
Transform< float > Transformf
Definition: OVR_Math.h:1159
Size & operator*=(const Size &b)
Definition: OVR_Math.h:614
static Vector2 Min(const Vector2 &a, const Vector2 &b)
Definition: OVR_Math.h:304
static Matrix4 AxisConversion(const WorldAxes &to, const WorldAxes &from)
Definition: OVR_Math.h:1564
Plane(T x, T y, T z, T d)
Definition: OVR_Math.h:2494
Matrix4 & operator*=(const Matrix4 &b)
Definition: OVR_Math.h:1369
T DistanceSq(Vector3 const &b) const
Definition: OVR_Math.h:512
static Matrix4 Scaling(T x, T y, T z)
Definition: OVR_Math.h:1631
T Get(AngularUnits u=Radians) const
Definition: OVR_Math.h:2420
Transform Inverted() const
Definition: OVR_Math.h:1152
AxisDirection
Definition: OVR_Math.h:73
Size(T s)
Definition: OVR_Math.h:590
T Determinant() const
Definition: OVR_Math.h:2164
bool IsNormalized() const
Definition: OVR_Math.h:518
Vector3 operator/(T s) const
Definition: OVR_Math.h:430
CompatibleTypes< Vector2< T > >::Type CompatibleType
Definition: OVR_Math.h:274
SymMat3< U > CastTo() const
Definition: OVR_Math.h:2243
SymMat3< double > SymMat3d
Definition: OVR_Math.h:2375
static const double RadToDegreeFactor
Definition: OVR_Math.h:211
Size operator/(const Size &b) const
Definition: OVR_Math.h:615
Quat< double > Quatd
Definition: OVR_Math.h:1086
static const float PiOver4
Definition: OVR_Math.h:181
Quat(const Quat< typename Math< T >::OtherFloatType > &src)
Definition: OVR_Math.h:702
Vector3< T > Apply(const Vector3< T > &v) const
Definition: OVR_Math.h:1131
Vector3< T > Translate(const Vector3< T > &v) const
Definition: OVR_Math.h:1126
bool IsNormalized() const
Definition: OVR_Math.h:350
static const double E
Definition: OVR_Math.h:206
Matrix3(const Matrix4< typename Math< T >::OtherFloatType > &src)
Definition: OVR_Math.h:1927
static const Matrix3 & Identity()
Definition: OVR_Math.h:1972
void Normalize()
Definition: OVR_Math.h:353
static const Matrix3 IdentityValue
Definition: OVR_Math.h:1863
const Matrix4< float > IdentityValue
Definition: OVR_Math.cpp:78
T DistanceSq(const Quat &q) const
Definition: OVR_Math.h:887
T Dot(const Quat &q) const
Definition: OVR_Math.h:894
T Length() const
Definition: OVR_Math.h:341
CompatibleTypes< Rect< T > >::Type CompatibleType
Definition: OVR_Math.h:660
Vector3 & operator-=(const Vector3 &b)
Definition: OVR_Math.h:423
Vector2 operator*(T s) const
Definition: OVR_Math.h:295
void ToString(char *dest, UPInt destsize) const
Definition: OVR_Math.h:1949
Vector3< T > AngularVelocity
bool operator==(const Plane< T > &rhs) const
Definition: OVR_Math.h:2516
bool Compare(const Vector3 &b, T tolerance=Mathf::Tolerance)
Definition: OVR_Math.h:450
Vector3 ProjectTo(const Vector3 &b) const
Definition: OVR_Math.h:542
static const float RadToDegreeFactor
Definition: OVR_Math.h:187
Vector3< double > Vector3d
Definition: OVR_Math.h:555
#define OVR_ASSERT(p)
static Vector2 Max(const Vector2 &a, const Vector2 &b)
Definition: OVR_Math.h:306
Vector2 EntrywiseMultiply(const Vector2 &b) const
Definition: OVR_Math.h:316
Math< float > Mathf
Definition: OVR_Math.h:221
Matrix3(T m11, T m12, T m13, T m21, T m22, T m23, T m31, T m32, T m33)
Definition: OVR_Math.h:1879
T TestSide(const Vector3< T > &p) const
Definition: OVR_Math.h:2500
static SymMat3 & Multiply(SymMat3 *d, const SymMat3 &a, const SymMat3 &b)
Definition: OVR_Math.h:2298
Vector3< T > Transform(const Vector3< T > &v) const
Definition: OVR_Math.h:2111
float OtherFloatType
Definition: OVR_Math.h:170
static Matrix4 LookAtLH(const Vector3< T > &eye, const Vector3< T > &at, const Vector3< T > &up)
Definition: OVR_Math.h:1757
Matrix3 operator-(const Matrix3 &b) const
Definition: OVR_Math.h:2030
WorldAxes(AxisDirection x, AxisDirection y, AxisDirection z)
Definition: OVR_Math.h:87
T Length() const
Definition: OVR_Math.h:874
Matrix3< double > Matrix3d
Definition: OVR_Math.h:2203
void GetEulerAngles(T *a, T *b, T *c) const
Definition: OVR_Math.h:1025
Transform(const Transform &s)
Definition: OVR_Math.h:1103
SymMat3< T > this_type
Definition: OVR_Math.h:2211
Quat operator+(const Quat &b) const
Definition: OVR_Math.h:859
static Size Max(const Size &a, const Size &b)
Definition: OVR_Math.h:626
PoseState< T > operator*(const PoseState< T > &poseState) const
Definition: OVR_Math.h:1141
void GetEulerAnglesABA(T *a, T *b, T *c) const
Definition: OVR_Math.h:1038
T Angle(const Quat &q) const
Definition: OVR_Math.h:900
static const float MaxValue
Definition: OVR_Math.h:184
double OtherFloatType
Definition: OVR_Math.h:194
static const Matrix4 IdentityValue
Definition: OVR_Math.h:1194
static const double MinPositiveValue
Definition: OVR_Math.h:209
T M[4][4]
Definition: OVR_Math.h:1197
Vector3 Normalized() const
Definition: OVR_Math.h:529
T DistanceSq(Vector2 &b) const
Definition: OVR_Math.h:344
void FastFixRange()
Definition: OVR_Math.h:2451
Vector3 EntrywiseMultiply(const Vector3 &b) const
Definition: OVR_Math.h:470
Size operator*(const Size &b) const
Definition: OVR_Math.h:613
void Rank1Add(const Vector3< T > &a)
Definition: OVR_Math.h:2358
Angle operator+(const Angle &b) const
Definition: OVR_Math.h:2436
static Matrix4 RotationZ(T angle)
Definition: OVR_Math.h:1728
Matrix4 & operator/=(T s)
Definition: OVR_Math.h:1397
Matrix3< T > Inverse() const
Definition: OVR_Math.h:2176
static Matrix4 PerspectiveRH(T yfov, T aspect, T znear, T zfar)
Definition: OVR_Math.h:1779
T Asin(T val)
Definition: OVR_Math.h:240
static const double Tolerance
Definition: OVR_Math.h:214
Vector2 ProjectTo(const Vector2 &b) const
Definition: OVR_Math.h:373
struct ovrVector3d_ ovrVector3d
Definition: OVR_Math.h:110
Vector3< T > Row(int r) const
Definition: OVR_Math.h:2159
static Matrix4 LookAtRH(const Vector3< T > &eye, const Vector3< T > &at, const Vector3< T > &up)
Definition: OVR_Math.h:1741
Matrix4 operator*(const Matrix4 &b) const
Definition: OVR_Math.h:1362
static Matrix4 Translation(T x, T y, T z=0.0f)
Definition: OVR_Math.h:1598
Matrix4(const Transform< T > &p)
Definition: OVR_Math.h:1244
Size< float > Sizef
Definition: OVR_Math.h:638
struct ovrMatrix3d_ ovrMatrix3d
Definition: OVR_Math.h:111
T operator()(int i, int j) const
Definition: OVR_Math.h:2238
static const double MaxValue
Definition: OVR_Math.h:208
bool operator==(const Size &b) const
Definition: OVR_Math.h:605
Vector3(T x_, T y_, T z_=0)
Definition: OVR_Math.h:400
Vector2 operator+(const Vector2 &b) const
Definition: OVR_Math.h:288
void Rank1Sub(const Vector3< T > &a)
Definition: OVR_Math.h:2366
bool IsNormalized() const
Definition: OVR_Math.h:906
Vector3< T > Translation
Definition: OVR_Math.h:1119
struct ovrPosed_ ovrPosed
Definition: OVR_Math.h:114
AxisDirection YAxis
Definition: OVR_Math.h:85
Vector3< T > LinearVelocity
Quat operator-(const Quat &b) const
Definition: OVR_Math.h:861
Vector3(const CompatibleType &s)
Definition: OVR_Math.h:409
void GetEulerAngles(T *a, T *b, T *c) const
Definition: OVR_Math.h:977
Quat< float > Quatf
Definition: OVR_Math.h:1085
struct ovrPoseStated_ ovrPoseStated
Definition: OVR_Math.h:116
Size & operator/=(const Size &b)
Definition: OVR_Math.h:616
Vector2 & operator*=(T s)
Definition: OVR_Math.h:296
T SubDet(const UPInt *rows, const UPInt *cols) const
Definition: OVR_Math.h:2131
Matrix4(NoInitType)
Definition: OVR_Math.h:1202
Matrix4 & operator-=(const Matrix4 &b)
Definition: OVR_Math.h:1339
bool operator!=(const Vector2 &b) const
Definition: OVR_Math.h:286
AxisDirection ZAxis
Definition: OVR_Math.h:85
T & operator[](int idx)
Definition: OVR_Math.h:457
static const double TwoPi
Definition: OVR_Math.h:203
this_type & operator*=(T s)
Definition: OVR_Math.h:2272
Matrix3 Transposed() const
Definition: OVR_Math.h:2118
SymMat3(T a00, T a01, T a02, T a11, T a12, T a22)
Definition: OVR_Math.h:2226
bool operator!=(const Vector3 &b) const
Definition: OVR_Math.h:418
Vector2(const CompatibleType &s)
Definition: OVR_Math.h:276
T Dot(const Vector3 &b) const
Definition: OVR_Math.h:487
Quat Nlerp(const Quat &other, T a)
Definition: OVR_Math.h:943
Quat & operator-=(const Quat &b)
Definition: OVR_Math.h:862
bool operator==(const Matrix4 &b) const
Definition: OVR_Math.h:1307
T LengthSq() const
Definition: OVR_Math.h:506
Matrix4(const Matrix4< typename Math< T >::OtherFloatType > &src)
Definition: OVR_Math.h:1252
Size operator+(const Size &b) const
Definition: OVR_Math.h:608
static Matrix4 PerspectiveLH(T yfov, T aspect, T znear, T zfar)
Definition: OVR_Math.h:1803
static Vector3 Min(const Vector3 &a, const Vector3 &b)
Definition: OVR_Math.h:436
Quat Normalized() const
Definition: OVR_Math.h:915
Vector3< T > Col(int c) const
Definition: OVR_Math.h:2154
Matrix4(const typename CompatibleTypes< Matrix4< T > >::Type &s)
Definition: OVR_Math.h:1260
Math< double > Mathd
Definition: OVR_Math.h:222
Plane(const Vector3< T > &p, const Vector3< T > &n)
Definition: OVR_Math.h:2497
Size< double > Sized
Definition: OVR_Math.h:639
T Trace() const
Definition: OVR_Math.h:2347
void Rank1Add(const Vector3< T > &a, const Vector3< T > &b)
Definition: OVR_Math.h:2139
Vector2 operator/(T s) const
Definition: OVR_Math.h:298
UPInt OVR_CDECL OVR_sprintf(char *dest, UPInt destsize, const char *format,...)
Definition: OVR_Std.h:280
T Determinant() const
Definition: OVR_Math.h:1439
this_type & operator-=(const this_type &b)
Definition: OVR_Math.h:2260
static Matrix4 Ortho2D(T w, T h)
Definition: OVR_Math.h:1822
Quat Conj() const
Definition: OVR_Math.h:923
Size & operator+=(const Size &b)
Definition: OVR_Math.h:609
Matrix3 & operator-=(const Matrix3 &b)
Definition: OVR_Math.h:2037
T Distance(const Angle &b)
Definition: OVR_Math.h:2443
Quat< T > Rotation
Definition: OVR_Math.h:1118
Vector2 & operator-=(const Vector2 &b)
Definition: OVR_Math.h:291
this_type Inverse() const
Definition: OVR_Math.h:2326
Vector3 Lerp(const Vector3 &b, T f) const
Definition: OVR_Math.h:538
static Vector3 Max(const Vector3 &a, const Vector3 &b)
Definition: OVR_Math.h:442
Vector4f(float r, float g, float b, float a)
Definition: OVR_Math.h:571
Vector3(const Vector3< typename Math< T >::OtherFloatType > &src)
Definition: OVR_Math.h:402
T Angle(const Vector3 &b) const
Definition: OVR_Math.h:497
Vector2< T > ToVector() const
Definition: OVR_Math.h:632
Matrix3< T > operator*(const SymMat3< T > &a, const SymMat3< T > &b)
Definition: OVR_Math.h:2378
static const float Tolerance
Definition: OVR_Math.h:190
bool Compare(const Vector2 &b, T tolerance=Mathf::Tolerance)
Definition: OVR_Math.h:310
bool operator!=(const Quat &b) const
Definition: OVR_Math.h:857
int Sign() const
Definition: OVR_Math.h:2422
Matrix4 operator+(const Matrix4 &b) const
Definition: OVR_Math.h:1317
Rect(T x1, T y1, T w1, T h1)
Definition: OVR_Math.h:655
static const float E
Definition: OVR_Math.h:182
void ToString(char *dest, UPInt destsize) const
Definition: OVR_Math.h:1274
static const float MinPositiveValue
Definition: OVR_Math.h:185
Angle & operator+=(const Angle &b)
Definition: OVR_Math.h:2434
void ToEulerAngles(T *a, T *b, T *c)
Definition: OVR_Math.h:1491
Rect(const Vector2< T > &pos, const Size< T > &sz)
Definition: OVR_Math.h:656
static Matrix4 Translation(const Vector3< T > &v)
Definition: OVR_Math.h:1588
Matrix3(NoInitType)
Definition: OVR_Math.h:1871
T Determinant() const
Definition: OVR_Math.h:2314
Rect< int > Recti
Definition: OVR_Math.h:681
Vector3< float > Vector3f
Definition: OVR_Math.h:554
Vector3 operator*(T s) const
Definition: OVR_Math.h:427
void SetTranslation(const Vector3< T > &v)
Definition: OVR_Math.h:1608
Transform(const CompatibleType &s)
Definition: OVR_Math.h:1105
Transform< T > Pose
Vector2 & operator+=(const Vector2 &b)
Definition: OVR_Math.h:289
T Acos(T val)
Definition: OVR_Math.h:232
Quat(const Matrix4< T > &m)
Definition: OVR_Math.h:773
Matrix4(const Quat< T > &q)
Definition: OVR_Math.h:1231
bool operator==(const Quat &b) const
Definition: OVR_Math.h:856
Size< int > Sizei
Definition: OVR_Math.h:636
void ToEulerAnglesABA(T *a, T *b, T *c)
Definition: OVR_Math.h:1529
OVR_FORCE_INLINE const T Abs(const T v)
Definition: OVR_Alg.h:79
#define OVR_COMPILER_ASSERT(x)
Quat(const Vector3< T > &axis, T angle)
Definition: OVR_Math.h:719
Vector3< T > Transform(const Vector3< T > &v) const
Definition: OVR_Math.h:1405
T LengthSq() const
Definition: OVR_Math.h:877
Vector3< T > Rotate(const Vector3< T > &v) const
Definition: OVR_Math.h:1121
Vector3< T > LinearAcceleration
Plane< T > Flipped() const
Definition: OVR_Math.h:2505
Vector3< T > N
Definition: OVR_Math.h:2487
void SetSize(const Size< T > &sz)
Definition: OVR_Math.h:673
Vector3 & operator/=(T s)
Definition: OVR_Math.h:432
RotateDirection
Definition: OVR_Math.h:60
static const float DegreeToRadFactor
Definition: OVR_Math.h:188
Quat & operator/=(T s)
Definition: OVR_Math.h:867
T LengthSq() const
Definition: OVR_Math.h:338
bool operator==(const Vector3 &b) const
Definition: OVR_Math.h:417
SymMat3 operator*(T s) const
Definition: OVR_Math.h:2284
static int Index(unsigned int i, unsigned int j)
Definition: OVR_Math.h:2233