VR-Vantage 2.4 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) 2018 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 #pragma once
10 
14 
15 #include <cmath>
16 #include <limits>
17 
18 namespace makVrv {
19 
20  class DtRadians;
22  {
23  public:
24  explicit DtDegrees(float32 fVal): myVal(fVal){}
25  virtual ~DtDegrees(){}
26  float32 val() const {return myVal;}
27  bool isEqual(const DtDegrees& rhs) const;
28  DtRadians toRadians(void) const;
29  private:
32  };
33 
35  {
36  public:
37  explicit DtRadians(float32 fVal): myVal(fVal){}
38  virtual ~DtRadians(){}
39  float32 val() const {return myVal;}
40  bool isEqual(const DtRadians& rhs) const;
41  DtDegrees toDegrees(void) const;
42  private:
45  };
46 
50  {
51  public:
53 
54  template <class T> static T Sqrt(T val) {return sqrt(val);}
55  template <class T> static T Abs(T val) {return fabs(val);}
56  static int Abs(int val) {if (val>0) return val; else return -val;}
57  template <class T> static T Sin(T val) {return sin(val);}
58  template <class T> static T Cos(T val) {return cos(val);}
59  template <class T> static T Tan(T val) {return tan(val);}
60 
61  static float Sin(const DtDegrees& val);
62  static float Cos(const DtDegrees& val);
63  static float Tan(const DtDegrees& val);
64 
65  template <class T> static T Squared(T val) {return val*val;}
66 
67  template <class T> static int NumDigits(T val);
68  template <class T> static double GetNormalizedValueRespectingLength(T val);
69 
71 
72  static bool isEqual(int32 lhs, int32 rhs, int32 tolerance=std::numeric_limits<int32>::epsilon());
73  static bool isEqual(uint32 lhs, uint32 rhs, uint32 tolerance=std::numeric_limits<uint32>::epsilon());
74  static bool isEqual(float32 lhs, float32 rhs, float32 tolerance=std::numeric_limits<float32>::epsilon());
75  static bool isEqual(float64 lhs, float64 rhs, float64 tolerance=std::numeric_limits<float64>::epsilon());
77 
78 
80 
81  static float32 unitRandom32(void);
82  static float64 unitRandom64(void);
83 
84  static int rangeRandom(int iLow, int iHigh);
85  static float32 rangeRandom(float32 fLow, float32 fHigh);
86  static float64 rangeRandom(float64 fLow, float64 fHigh);
87  static bool randomBool(void);
89 
90  static uint32 getUpperPowerOfTwo(uint32 val);
91  static uint32 getLowerPowerOfTwo(uint32 val);
92 
94 
95  template <class T> static T clamp(T val, T minval, T maxval);
96 
97  template <class T> static DtVector2<T> clamp(const DtVector2<T>& val, const DtVector2<T>& minval, const DtVector2<T>& maxval);
98  template <class T> static DtVector3<T> clamp(const DtVector3<T>& val, const DtVector3<T>& minval, const DtVector3<T>& maxval);
99  template <class T> static DtVector4<T> clamp(const DtVector4<T>& val, const DtVector4<T>& minval, const DtVector4<T>& maxval);
101 
102  template <class T> static DtVector3<T> toCartesian(T thetaDegrees, T phiDegrees, bool flipYZ);
103 
104  template <class T> static bool isFinite(T n);
105 
106  template <class T> static T Min(T lhs, T rhs);
107  template <class T> static T Max(T lhs, T rhs);
108 
109 #undef max
110 #undef min
111  template <class T> static T positiveInfinity(void) { return std::numeric_limits<T>::max();}
112  template <class T> static T negativeInfinity(void) { return std::numeric_limits<T>::min();}
113 
114  static const float32 TWO_PI;
115  static const float32 PI;
116  static const float32 PI_BY_2;
117  static const float32 PI_BY_3;
118  static const float32 PI_BY_4;
119  static const float32 PI_BY_6;
120  static const float32 Gravity;
123  private:
124  };
125 
126  template <class T>
127  T DtMath::clamp(T val, T minval, T maxval)
128  {
129  Assert(minval <= maxval && "Invalid clamp range");
130  return std::max(std::min(val, maxval), minval);
131  }
132 
133  template <class T>
134  DtVector2<T> DtMath::clamp(const DtVector2<T>& val, const DtVector2<T>& minval, const DtVector2<T>& maxval)
135  {
136  DtVector2<T> vClampedVal;
137  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
138  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
139  return vClampedVal;
140  }
141 
142  template <class T>
143  DtVector3<T> DtMath::clamp(const DtVector3<T>& val, const DtVector3<T>& minval, const DtVector3<T>& maxval)
144  {
145  DtVector3<T> vClampedVal;
146  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
147  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
148  vClampedVal.z = DtMath::clamp(val.z, minval.z, maxval.z);
149  return vClampedVal;
150  }
151 
152  template <class T>
153  DtVector4<T> DtMath::clamp(const DtVector4<T>& val, const DtVector4<T>& minval, const DtVector4<T>& maxval)
154  {
155  DtVector4<T> vClampedVal;
156  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
157  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
158  vClampedVal.z = DtMath::clamp(val.z, minval.z, maxval.z);
159  vClampedVal.w = DtMath::clamp(val.w, minval.w, maxval.w);
160  return vClampedVal;
161  }
162 
163  template <class T>
164  DtVector3<T> DtMath::toCartesian(T thetaDegrees, T phiDegrees, bool flipYZ)
165  {
166  T theta = toRadians(thetaDegrees);
167  T phi = toRadians(phiDegrees);
168 
169  DtVector3<T> vPosition;
170  vPosition.x = DtMath::Cos(theta)*Cos(phi);
171  vPosition.y = DtMath::Sin(theta)*Cos(phi);
172  vPosition.z = DtMath::Sin(phi);
173 
174  if (flipYZ)
175  {
176  T temp = vPosition.y;
177  vPosition.y = vPosition.z;
178  vPosition.z = temp;
179  }
180 
181  return vPosition;
182  }
183 
184  template <class T>
186  {
187  int digits = 0;
188  while (val) {
189  val /= 10;
190  digits++;
191  }
192  return digits;
193  }
194 
195  template <class T>
197  {
198  double normalizedValue = (double)(val)/(double)(std::numeric_limits<T>::max());
199  return normalizedValue;
200  }
201 
202  template <class T>
204  {
205  return ((n == n)
206  && (n != +std::numeric_limits<T>::infinity())
207  && (n != -std::numeric_limits<T>::infinity())
208  );
209  }
210 
211  template <class T>
212  T DtMath::Min(T lhs, T rhs)
213  {
214  if (lhs < rhs)
215  {
216  return lhs;
217  }
218  else
219  {
220  return rhs;
221  }
222  }
223 
224  template <class T>
225  T DtMath::Max(T lhs, T rhs)
226  {
227  if (lhs > rhs)
228  {
229  return lhs;
230  }
231  else
232  {
233  return rhs;
234  }
235  }
236 
237 } //namespace makVrv


Copyright © 2005-2018 VT MAK. All Rights Reserved (www.mak.com)