VR-Forces Developer's Guide
 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 
11 #include <vrvMath/vrvMath.h>
12 #include <vrvMath/DtCommonTypes.h>
13 #include <vrvMath/DtAssert.h>
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  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  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 
83  static float32 unitRandom32(void);
85  static float64 unitRandom64(void);
86 
87  static int rangeRandom(int iLow, int iHigh);
88  static float32 rangeRandom(float32 fLow, float32 fHigh);
89  static float64 rangeRandom(float64 fLow, float64 fHigh);
90  static bool randomBool(void);
92 
93  static uint32 getUpperPowerOfTwo(uint32 val);
94  static uint32 getLowerPowerOfTwo(uint32 val);
95 
98  static bool isPowerOf2(uint32 i);
99 
102  static uint32 nextPowerOf2(uint32 i);
103 
105  static std::string formatBytes(uint64_t sizeInBytes);
106 
108  template <class T> static T clamp(T val, T minval, T maxval);
110 
111  template <class T> static DtVector2<T> clamp(const DtVector2<T>& val, const DtVector2<T>& minval, const DtVector2<T>& maxval);
112  template <class T> static DtVector3<T> clamp(const DtVector3<T>& val, const DtVector3<T>& minval, const DtVector3<T>& maxval);
113  template <class T> static DtVector4<T> clamp(const DtVector4<T>& val, const DtVector4<T>& minval, const DtVector4<T>& maxval);
115 
117 
118  template <class T> static T lerp( T a, T b, T t );
119 
120  template <class T> static DtVector2<T> lerp( const DtVector2<T>& va, const DtVector2<T>& vb, const T t );
121  template <class T> static DtVector3<T> lerp( const DtVector3<T>& va, const DtVector3<T>& vb, const T t );
122  template <class T> static DtVector4<T> lerp( const DtVector4<T>& va, const DtVector4<T>& vb, const T t );
124 
125  template <class T> static DtVector3<T> toCartesian(T thetaDegrees, T phiDegrees, bool flipYZ);
126 
128  template <class T> static bool linePlaneIntersection(const DtVector3<T>& linePoint1, const DtVector3<T>& linePoint2,
130  const DtVector3<T>& planePoint, const DtVector3<T>& planeNormal,
131  DtVector3<T>& intersect, float & w);
133 
134  template <class T> static bool isFinite(T n);
135 
136  template <class T> static T Min(T lhs, T rhs);
137  template <class T> static T Max(T lhs, T rhs);
138 
140  static double fractional(double val);
141 
142  // This returns 0 for non-power-of-two numbers, or sizes out of reasonable light
143  // grid range (<4, or >256). Currently, a grid size of 32 (log2=5) is hardcoded
144  // elsewhere.
145  static unsigned int tilelog2(unsigned int x);
146 
148  template <class T>
149  static T rpmToRadiansPerSec(T rpm);
150 
152  template <class T>
153  static T radiansPerSecToRpm(T radiansPerSec);
154 
155 #undef max
156 #undef min
157  template <class T> static T positiveInfinity(void) { return std::numeric_limits<T>::max();}
158  template <class T> static T negativeInfinity(void) { return std::numeric_limits<T>::min();}
159 
160  static const float32 TWO_PI;
161  static const float32 PI;
162  static const float32 PI_BY_2;
163  static const float32 PI_BY_3;
164  static const float32 PI_BY_4;
165  static const float32 PI_BY_6;
166  static const float32 Gravity;
169  private:
170  };
171 
172  template <class T>
173  T DtMath::clamp(T val, T minval, T maxval)
174  {
175  Assert(minval <= maxval && "Invalid clamp range");
176  return std::max(std::min(val, maxval), minval);
177  }
178 
179  template <class T>
180  DtVector2<T> DtMath::clamp(const DtVector2<T>& val, const DtVector2<T>& minval, const DtVector2<T>& maxval)
181  {
182  DtVector2<T> vClampedVal;
183  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
184  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
185  return vClampedVal;
186  }
187 
188  template <class T>
189  DtVector3<T> DtMath::clamp(const DtVector3<T>& val, const DtVector3<T>& minval, const DtVector3<T>& maxval)
190  {
191  DtVector3<T> vClampedVal;
192  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
193  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
194  vClampedVal.z = DtMath::clamp(val.z, minval.z, maxval.z);
195  return vClampedVal;
196  }
197 
198  template <class T>
199  DtVector4<T> DtMath::clamp(const DtVector4<T>& val, const DtVector4<T>& minval, const DtVector4<T>& maxval)
200  {
201  DtVector4<T> vClampedVal;
202  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
203  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
204  vClampedVal.z = DtMath::clamp(val.z, minval.z, maxval.z);
205  vClampedVal.w = DtMath::clamp(val.w, minval.w, maxval.w);
206  return vClampedVal;
207  }
208 
209  template<class T>
210  inline T DtMath::lerp( T va, T vb, T t )
211  {
212  if( t <= 0.0 )
213  {
214  return va;
215  }
216 
217  if( t >= 1.0 )
218  {
219  return vb;
220  }
221 
222  T vm = va + (vb - va) * t;
223  return vm;
224  }
225 
226  template<class T>
227  inline DtVector2<T> DtMath::lerp( const DtVector2<T>& va, const DtVector2<T>& vb, const T t )
228  {
229  if( t <= 0.0 )
230  {
231  return va;
232  }
233 
234  if( t >= 1.0 )
235  {
236  return vb;
237  }
238 
239  DtVector2<T> vm;
240  vm[0] = va[0] + (vb[0] - va[0]) * t;
241  vm[1] = va[1] + (vb[1] - va[1]) * t;
242 
243  return vm;
244  }
245 
246  template<class T>
247  inline DtVector3<T> DtMath::lerp( const DtVector3<T>& va, const DtVector3<T>& vb, const T t )
248  {
249  if( t <= 0.0 )
250  {
251  return va;
252  }
253 
254  if( t >= 1.0 )
255  {
256  return vb;
257  }
258 
259  DtVector3<T> vm;
260  vm[0] = va[0] + (vb[0] - va[0]) * t;
261  vm[1] = va[1] + (vb[1] - va[1]) * t;
262  vm[2] = va[2] + (vb[2] - va[2]) * t;
263 
264  return vm;
265  }
266 
267  template<class T>
268  inline DtVector4<T> DtMath::lerp( const DtVector4<T>& va, const DtVector4<T>& vb, const T t )
269  {
270  if( t <= 0.0 )
271  {
272  return va;
273  }
274 
275  if( t >= 1.0 )
276  {
277  return vb;
278  }
279 
280  DtVector4<T> vm;
281  vm[0] = va[0] + (vb[0] - va[0]) * t;
282  vm[1] = va[1] + (vb[1] - va[1]) * t;
283  vm[2] = va[2] + (vb[2] - va[2]) * t;
284  vm[3] = va[3] + (vb[3] - va[3]) * t;
285 
286  return vm;
287  }
288 
289  template <class T>
290  DtVector3<T> DtMath::toCartesian(T thetaDegrees, T phiDegrees, bool flipYZ)
291  {
292  T theta = toRadians(thetaDegrees);
293  T phi = toRadians(phiDegrees);
294 
295  DtVector3<T> vPosition;
296  vPosition.x = DtMath::Cos(theta)*Cos(phi);
297  vPosition.y = DtMath::Sin(theta)*Cos(phi);
298  vPosition.z = DtMath::Sin(phi);
299 
300  if (flipYZ)
301  {
302  T temp = vPosition.y;
303  vPosition.y = vPosition.z;
304  vPosition.z = temp;
305  }
306 
307  return vPosition;
308  }
309 
310  template <class T>
311  bool DtMath::linePlaneIntersection(const DtVector3<T>& linePoint1, const DtVector3<T>& linePoint2,
312  const DtVector3<T>& planePoint, const DtVector3<T>& planeNormal,
313  DtVector3<T>& intersect, float& w)
314  {
315  DtVector3<T> lineDir = linePoint2 - linePoint1;
316  lineDir.normalize();
317 
318  DtVector3<T> pnorm = planeNormal;
319  pnorm.normalize();
320 
321  float denom = lineDir.dotProduct(pnorm);
322  if (denom == 0.0f)
323  {
324  return false;
325  }
326 
327  DtVector3<T> pTolDelta = (planePoint - linePoint1);
328  w = pTolDelta.dotProduct(pnorm) / denom;
329  intersect = (lineDir * w) + linePoint1;
330  return true;
331  }
332 
333  template <class T>
334  int DtMath::NumDigits(T val)
335  {
336  int digits = 0;
337  while (val) {
338  val /= 10;
339  digits++;
340  }
341  return digits;
342  }
343 
344  template <class T>
346  {
347  double normalizedValue = (double)(val)/(double)(std::numeric_limits<T>::max());
348  return normalizedValue;
349  }
350 
351  template <class T>
353  {
354  return ((n == n)
355  && (n != +std::numeric_limits<T>::infinity())
356  && (n != -std::numeric_limits<T>::infinity())
357  );
358  }
359 
360  template <class T>
361  T DtMath::Min(T lhs, T rhs)
362  {
363  if (lhs < rhs)
364  {
365  return lhs;
366  }
367  else
368  {
369  return rhs;
370  }
371  }
372 
373  template <class T>
374  T DtMath::Max(T lhs, T rhs)
375  {
376  if (lhs > rhs)
377  {
378  return lhs;
379  }
380  else
381  {
382  return rhs;
383  }
384  }
385 
386  template <class T>
388  {
389  return (rpm / 60.0f) * 2.0f * DtMath::PI;
390  }
391 
392  template <class T>
393  T DtMath::radiansPerSecToRpm(T radiansPerSec)
394  {
395  return (radiansPerSec / (2.0f * DtMath::PI)) * 60.0f;
396  }
397 
398 } //namespace makVrv
static T clamp(T val, T minval, T maxval)
Clamping routines.
Definition: DtMath.h:173
static const float32 PI
Definition: DtMath.h:161
static T Sin(T val)
Standard math routines.
Definition: DtMath.h:61
DtRadians(float32 fVal)
Definition: DtMath.h:41
static T Squared(T val)
Standard math routines.
Definition: DtMath.h:69
virtual ~DtRadians()
Definition: DtMath.h:42
T x
Definition: DtVector4.h:113
float32 myVal
Definition: DtMath.h:48
static const float32 PI_BY_3
Definition: DtMath.h:163
Definition: DtMath.h:38
static double GetNormalizedValueRespectingLength(T val)
Standard math routines.
Definition: DtMath.h:345
static T Cos(T val)
Standard math routines.
Definition: DtMath.h:62
static const float32 PI_BY_6
Definition: DtMath.h:165
DtDegrees(float32 fVal)
Definition: DtMath.h:28
T dotProduct(const DtVector3 &rhs) const
get the dot product of this vector and rhs
Definition: DtVector3.h:265
float32 val() const
Definition: DtMath.h:43
Dll export defines.
static T negativeInfinity(void)
Definition: DtMath.h:158
static T radiansPerSecToRpm(T radiansPerSec)
radians per sec to rpm
Definition: DtMath.h:393
static const float32 PI_BY_4
Definition: DtMath.h:164
static bool isFinite(T n)
Definition: DtMath.h:352
T y
Definition: DtVector2.h:105
T z
Definition: DtVector3.h:132
static const float32 TWO_PI
Definition: DtMath.h:160
static int NumDigits(T val)
Standard math routines.
Definition: DtMath.h:334
T x
Definition: DtVector3.h:132
DtDegrees()
Definition: DtMath.h:34
static T lerp(T a, T b, T t)
Lerping routines.
Definition: DtMath.h:210
static bool linePlaneIntersection(const DtVector3< T > &linePoint1, const DtVector3< T > &linePoint2, const DtVector3< T > &planePoint, const DtVector3< T > &planeNormal, DtVector3< T > &intersect, float &w)
Basic intersections.
Definition: DtMath.h:311
static const float32 m_fConvertToDegrees
Definition: DtMath.h:168
4 dimensional vector
Definition: DtMath.h:22
static T Tan(T val)
Standard math routines.
Definition: DtMath.h:63
static DtVector3< T > toCartesian(T thetaDegrees, T phiDegrees, bool flipYZ)
Definition: DtMath.h:290
static int Abs(int val)
Standard math routines.
Definition: DtMath.h:60
float float32
Definition: DtCommonTypes.h:33
Definition: DtMath.h:25
T z
Definition: DtVector4.h:113
static T Max(T lhs, T rhs)
Definition: DtMath.h:374
static const float32 PI_BY_2
Definition: DtMath.h:162
T w
Definition: DtVector4.h:113
T y
Definition: DtVector3.h:132
static T positiveInfinity(void)
Definition: DtMath.h:157
DtRadians()
Definition: DtMath.h:47
Math utilities.
Definition: DtMath.h:53
virtual ~DtDegrees()
Definition: DtMath.h:29
void normalize(void)
normalize this vector in place
Definition: DtVector3.h:164
2 dimensional vector
Definition: DtMath.h:21
unsigned int uint32
Definition: DtCommonTypes.h:28
static T rpmToRadiansPerSec(T rpm)
rpm to radians per sec
Definition: DtMath.h:387
float32 myVal
Definition: DtMath.h:35
T x
Definition: DtVector2.h:105
#define DT_DLL_VRVMATH
Definition: vrvMath.h:18
static T Min(T lhs, T rhs)
Definition: DtMath.h:361
assert macros
float32 val() const
Definition: DtMath.h:30
T y
Definition: DtVector4.h:113
static const float32 Gravity
Definition: DtMath.h:166
static const float32 m_fConvertToRadians
Definition: DtMath.h:167
static T Abs(T val)
Standard math routines.
Definition: DtMath.h:59
2 dimensional vector
Definition: DtMath.h:20
#define Assert(exp)
Definition: DtAssert.h:25

Document ID: Generated on Wed Mar 27 22:49:11 EDT 2024 from SVN revision 264633
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)