VR-Vantage 3.0.3 API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtMath.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2021 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  template <class T> class DtVector2;
21  template <class T> class DtVector3;
22  template <class T> class DtVector4;
23 
24  class DtRadians;
26  {
27  public:
28  explicit DtDegrees(float32 fVal): myVal(fVal){}
29  virtual ~DtDegrees(){}
30  float32 val() const {return myVal;}
31  bool isEqual(const DtDegrees& rhs) const;
32  DtRadians toRadians(void) const;
33  private:
36  };
37 
39  {
40  public:
41  explicit DtRadians(float32 fVal): myVal(fVal){}
42  virtual ~DtRadians(){}
43  float32 val() const {return myVal;}
44  bool isEqual(const DtRadians& rhs) const;
45  DtDegrees toDegrees(void) const;
46  private:
49  };
50 
54  {
55  public:
57 
58  template <class T> static T Sqrt(T val) {return sqrt(val);}
59  template <class T> static T Abs(T val) {return fabs(val);}
60  static int Abs(int val) {if (val>0) return val; else return -val;}
61  template <class T> static T Sin(T val) {return sin(val);}
62  template <class T> static T Cos(T val) {return cos(val);}
63  template <class T> static T Tan(T val) {return tan(val);}
64 
65  static float Sin(const DtDegrees& val);
66  static float Cos(const DtDegrees& val);
67  static float Tan(const DtDegrees& val);
68 
69  template <class T> static T Squared(T val) {return val*val;}
70 
71  template <class T> static int NumDigits(T val);
72  template <class T> static double GetNormalizedValueRespectingLength(T val);
73 
75 
76  static bool isEqual(int32 lhs, int32 rhs, int32 tolerance=std::numeric_limits<int32>::epsilon());
77  static bool isEqual(uint32 lhs, uint32 rhs, uint32 tolerance=std::numeric_limits<uint32>::epsilon());
78  static bool isEqual(float32 lhs, float32 rhs, float32 tolerance=std::numeric_limits<float32>::epsilon());
79  static bool isEqual(float64 lhs, float64 rhs, float64 tolerance=std::numeric_limits<float64>::epsilon());
81 
82 
84 
85  static float32 unitRandom32(void);
86  static float64 unitRandom64(void);
87 
88  static int rangeRandom(int iLow, int iHigh);
89  static float32 rangeRandom(float32 fLow, float32 fHigh);
90  static float64 rangeRandom(float64 fLow, float64 fHigh);
91  static bool randomBool(void);
93 
95  static uint32 getLowerPowerOfTwo(uint32 val);
96 
99  static bool isPowerOf2(uint32 i);
100 
103  static uint32 nextPowerOf2(uint32 i);
104 
106  static std::string formatBytes(uint64_t sizeInBytes);
107 
109 
110  template <class T> static T clamp(T val, T minval, T maxval);
111 
112  template <class T> static DtVector2<T> clamp(const DtVector2<T>& val, const DtVector2<T>& minval, const DtVector2<T>& maxval);
113  template <class T> static DtVector3<T> clamp(const DtVector3<T>& val, const DtVector3<T>& minval, const DtVector3<T>& maxval);
114  template <class T> static DtVector4<T> clamp(const DtVector4<T>& val, const DtVector4<T>& minval, const DtVector4<T>& maxval);
116 
117  template <class T> static DtVector3<T> toCartesian(T thetaDegrees, T phiDegrees, bool flipYZ);
118 
120 
121  template <class T> static bool linePlaneIntersection(const DtVector3<T>& linePoint1, const DtVector3<T>& linePoint2,
122  const DtVector3<T>& planePoint, const DtVector3<T>& planeNormal,
123  DtVector3<T>& intersect, float & w);
125 
126  template <class T> static bool isFinite(T n);
127 
128  template <class T> static T Min(T lhs, T rhs);
129  template <class T> static T Max(T lhs, T rhs);
130 
132  static double fractional(double val);
133 
134  // This returns 0 for non-power-of-two numbers, or sizes out of reasonable light
135  // grid range (<4, or >256). Currently, a grid size of 32 (log2=5) is hardcoded
136  // elsewhere.
137  static unsigned int tilelog2(unsigned int x);
138 
140  template <class T>
141  static T rpmToRadiansPerSec(T rpm);
142 
144  template <class T>
145  static T radiansPerSecToRpm(T radiansPerSec);
146 
147 #undef max
148 #undef min
149  template <class T> static T positiveInfinity(void) { return std::numeric_limits<T>::max();}
150  template <class T> static T negativeInfinity(void) { return std::numeric_limits<T>::min();}
151 
152  static const float32 TWO_PI;
153  static const float32 PI;
154  static const float32 PI_BY_2;
155  static const float32 PI_BY_3;
156  static const float32 PI_BY_4;
157  static const float32 PI_BY_6;
158  static const float32 Gravity;
161  private:
162  };
163 
164  template <class T>
165  T DtMath::clamp(T val, T minval, T maxval)
166  {
167  Assert(minval <= maxval && "Invalid clamp range");
168  return std::max(std::min(val, maxval), minval);
169  }
170 
171  template <class T>
172  DtVector2<T> DtMath::clamp(const DtVector2<T>& val, const DtVector2<T>& minval, const DtVector2<T>& maxval)
173  {
174  DtVector2<T> vClampedVal;
175  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
176  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
177  return vClampedVal;
178  }
179 
180  template <class T>
181  DtVector3<T> DtMath::clamp(const DtVector3<T>& val, const DtVector3<T>& minval, const DtVector3<T>& maxval)
182  {
183  DtVector3<T> vClampedVal;
184  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
185  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
186  vClampedVal.z = DtMath::clamp(val.z, minval.z, maxval.z);
187  return vClampedVal;
188  }
189 
190  template <class T>
191  DtVector4<T> DtMath::clamp(const DtVector4<T>& val, const DtVector4<T>& minval, const DtVector4<T>& maxval)
192  {
193  DtVector4<T> vClampedVal;
194  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
195  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
196  vClampedVal.z = DtMath::clamp(val.z, minval.z, maxval.z);
197  vClampedVal.w = DtMath::clamp(val.w, minval.w, maxval.w);
198  return vClampedVal;
199  }
200 
201  template <class T>
202  DtVector3<T> DtMath::toCartesian(T thetaDegrees, T phiDegrees, bool flipYZ)
203  {
204  T theta = toRadians(thetaDegrees);
205  T phi = toRadians(phiDegrees);
206 
207  DtVector3<T> vPosition;
208  vPosition.x = DtMath::Cos(theta)*Cos(phi);
209  vPosition.y = DtMath::Sin(theta)*Cos(phi);
210  vPosition.z = DtMath::Sin(phi);
211 
212  if (flipYZ)
213  {
214  T temp = vPosition.y;
215  vPosition.y = vPosition.z;
216  vPosition.z = temp;
217  }
218 
219  return vPosition;
220  }
221 
222  template <class T>
223  bool DtMath::linePlaneIntersection(const DtVector3<T>& linePoint1, const DtVector3<T>& linePoint2,
224  const DtVector3<T>& planePoint, const DtVector3<T>& planeNormal,
225  DtVector3<T>& intersect, float& w)
226  {
227  DtVector3<T> lineDir = linePoint2 - linePoint1;
228  lineDir.normalize();
229 
230  DtVector3<T> pnorm = planeNormal;
231  pnorm.normalize();
232 
233  float denom = lineDir.dotProduct(pnorm);
234  if (denom == 0.0f)
235  {
236  return false;
237  }
238 
239  DtVector3<T> pTolDelta = (planePoint - linePoint1);
240  w = pTolDelta.dotProduct(pnorm) / denom;
241  intersect = (lineDir * w) + linePoint1;
242  return true;
243  }
244 
245  template <class T>
247  {
248  int digits = 0;
249  while (val) {
250  val /= 10;
251  digits++;
252  }
253  return digits;
254  }
255 
256  template <class T>
258  {
259  double normalizedValue = (double)(val)/(double)(std::numeric_limits<T>::max());
260  return normalizedValue;
261  }
262 
263  template <class T>
265  {
266  return ((n == n)
267  && (n != +std::numeric_limits<T>::infinity())
268  && (n != -std::numeric_limits<T>::infinity())
269  );
270  }
271 
272  template <class T>
273  T DtMath::Min(T lhs, T rhs)
274  {
275  if (lhs < rhs)
276  {
277  return lhs;
278  }
279  else
280  {
281  return rhs;
282  }
283  }
284 
285  template <class T>
286  T DtMath::Max(T lhs, T rhs)
287  {
288  if (lhs > rhs)
289  {
290  return lhs;
291  }
292  else
293  {
294  return rhs;
295  }
296  }
297 
298  template <class T>
300  {
301  return (rpm / 60.0f) * 2.0f * DtMath::PI;
302  }
303 
304  template <class T>
305  T DtMath::radiansPerSecToRpm(T radiansPerSec)
306  {
307  return (radiansPerSec / (2.0f * DtMath::PI)) * 60.0f;
308  }
309 
310 } //namespace makVrv


Copyright © 2005-2023 MAK Technologies. All Rights Reserved (www.mak.com)