VR-Vantage API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DtMath.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2014 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 #pragma once
10 
11 #include "DtGraphicsUtilsPreReqs.h"
12 
13 #include <cmath>
14 #include <limits>
15 #include "DtCommonTypes.h"
16 #include "DtVectorForwardDeclare.h"
17 #include "DtAssert.h"
18 
19 namespace makVrv {
20 
21  class DtRadians;
23  {
24  public:
25  explicit DtDegrees(float32 fVal): myVal(fVal){}
26  virtual ~DtDegrees(){}
27  float32 val() const {return myVal;}
28  bool isEqual(const DtDegrees& rhs) const;
29  DtRadians toRadians(void) const;
30  private:
33  };
34 
36  {
37  public:
38  explicit DtRadians(float32 fVal): myVal(fVal){}
39  virtual ~DtRadians(){}
40  float32 val() const {return myVal;}
41  bool isEqual(const DtRadians& rhs) const;
42  DtDegrees toDegrees(void) const;
43  private:
46  };
47 
51  {
52  public:
54 
55  template <class T> static T Sqrt(T val) {return sqrt(val);}
56  template <class T> static T Abs(T val) {return fabs(val);}
57  static int Abs(int val) {if (val>0) return val; else return -val;}
58  template <class T> static T Sin(T val) {return sin(val);}
59  template <class T> static T Cos(T val) {return cos(val);}
60  template <class T> static T Tan(T val) {return tan(val);}
61 
62  static float Sin(const DtDegrees& val);
63  static float Cos(const DtDegrees& val);
64  static float Tan(const DtDegrees& val);
65 
66  template <class T> static T Squared(T val) {return val*val;}
67 
68  template <class T> static int NumDigits(T val);
69  template <class T> static double GetNormalizedValueRespectingLength(T val);
70 
72 
73  static bool isEqual(int32 lhs, int32 rhs, int32 tolerance=std::numeric_limits<int32>::epsilon());
74  static bool isEqual(uint32 lhs, uint32 rhs, uint32 tolerance=std::numeric_limits<uint32>::epsilon());
75  static bool isEqual(float32 lhs, float32 rhs, float32 tolerance=std::numeric_limits<float32>::epsilon());
76  static bool isEqual(float64 lhs, float64 rhs, float64 tolerance=std::numeric_limits<float64>::epsilon());
78 
79 
81 
82  static float32 unitRandom32(void);
83  static float64 unitRandom64(void);
84 
85  static int rangeRandom(int iLow, int iHigh);
86  static float32 rangeRandom(float32 fLow, float32 fHigh);
87  static float64 rangeRandom(float64 fLow, float64 fHigh);
88  static bool randomBool(void);
90 
91  static uint32 getUpperPowerOfTwo(uint32 val);
92  static uint32 getLowerPowerOfTwo(uint32 val);
93 
95 
96  template <class T> static T clamp(T val, T minval, T maxval);
97 
98  template <class T> static DtVector2<T> clamp(const DtVector2<T>& val, const DtVector2<T>& minval, const DtVector2<T>& maxval);
99  template <class T> static DtVector3<T> clamp(const DtVector3<T>& val, const DtVector3<T>& minval, const DtVector3<T>& maxval);
100  template <class T> static DtVector4<T> clamp(const DtVector4<T>& val, const DtVector4<T>& minval, const DtVector4<T>& maxval);
102 
103  template <class T> static DtVector3<T> toCartesian(T thetaDegrees, T phiDegrees, bool flipYZ);
104 
105 #undef max
106 #undef min
107  template <class T> static T positiveInfinity(void) { return std::numeric_limits<T>::max();}
108  template <class T> static T negativeInfinity(void) { return std::numeric_limits<T>::min();}
109 
110  static const float32 TWO_PI;
111  static const float32 PI;
112  static const float32 PI_BY_2;
113  static const float32 PI_BY_3;
114  static const float32 PI_BY_4;
115  static const float32 PI_BY_6;
116  static const float32 Gravity;
119  private:
120  };
121 
122  template <class T>
123  T DtMath::clamp(T val, T minval, T maxval)
124  {
125  Assert(minval <= maxval && "Invalid clamp range");
126  return std::max(std::min(val, maxval), minval);
127  }
128 
129  template <class T>
130  DtVector2<T> DtMath::clamp(const DtVector2<T>& val, const DtVector2<T>& minval, const DtVector2<T>& maxval)
131  {
132  DtVector2<T> vClampedVal;
133  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
134  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
135  return vClampedVal;
136  }
137 
138  template <class T>
139  DtVector3<T> DtMath::clamp(const DtVector3<T>& val, const DtVector3<T>& minval, const DtVector3<T>& maxval)
140  {
141  DtVector3<T> vClampedVal;
142  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
143  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
144  vClampedVal.z = DtMath::clamp(val.z, minval.z, maxval.z);
145  return vClampedVal;
146  }
147 
148  template <class T>
149  DtVector4<T> DtMath::clamp(const DtVector4<T>& val, const DtVector4<T>& minval, const DtVector4<T>& maxval)
150  {
151  DtVector4<T> vClampedVal;
152  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
153  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
154  vClampedVal.z = DtMath::clamp(val.z, minval.z, maxval.z);
155  vClampedVal.w = DtMath::clamp(val.w, minval.w, maxval.w);
156  return vClampedVal;
157  }
158 
159  template <class T>
160  DtVector3<T> DtMath::toCartesian(T thetaDegrees, T phiDegrees, bool flipYZ)
161  {
162  T theta = toRadians(thetaDegrees);
163  T phi = toRadians(phiDegrees);
164 
165  DtVector3<T> vPosition;
166  vPosition.x = DtMath::Cos(theta)*Cos(phi);
167  vPosition.y = DtMath::Sin(theta)*Cos(phi);
168  vPosition.z = DtMath::Sin(phi);
169 
170  if (flipYZ)
171  {
172  T temp = vPosition.y;
173  vPosition.y = vPosition.z;
174  vPosition.z = temp;
175  }
176 
177  return vPosition;
178  }
179 
180  template <class T>
182  {
183  int digits = 0;
184  while (val) {
185  val /= 10;
186  digits++;
187  }
188  return digits;
189  }
190 
191  template <class T>
193  {
194  double normalizedValue = (double)(val)/(double)(std::numeric_limits<T>::max());
195  return normalizedValue;
196  }
197 
198 } //namespace makVrv


Copyright © 2005-2013 VT MÄK. All Rights Reserved (www.mak.com)