VR-Forces 5.0.1 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 
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 
82 
84  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 
94  static uint32 getUpperPowerOfTwo(uint32 val);
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  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  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>
246  int DtMath::NumDigits(T val)
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
Dll export defines.
static T clamp(T val, T minval, T maxval)
Clamping routines.
Definition: DtMath.h:165
static const float32 PI
Definition: DtMath.h:153
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:101
float32 myVal
Definition: DtMath.h:48
static const float32 PI_BY_3
Definition: DtMath.h:155
Definition: DtMath.h:38
static double GetNormalizedValueRespectingLength(T val)
Standard math routines.
Definition: DtMath.h:257
static T Cos(T val)
Standard math routines.
Definition: DtMath.h:62
static const float32 PI_BY_6
Definition: DtMath.h:157
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
static T negativeInfinity(void)
Definition: DtMath.h:150
static T radiansPerSecToRpm(T radiansPerSec)
radians per sec to rpm
Definition: DtMath.h:305
static const float32 PI_BY_4
Definition: DtMath.h:156
static bool isFinite(T n)
Definition: DtMath.h:264
T y
Definition: DtVector2.h:105
T z
Definition: DtVector3.h:132
static const float32 TWO_PI
Definition: DtMath.h:152
static int NumDigits(T val)
Standard math routines.
Definition: DtMath.h:246
T x
Definition: DtVector3.h:132
DtDegrees()
Definition: DtMath.h:34
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:223
static const float32 m_fConvertToDegrees
Definition: DtMath.h:160
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:202
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:101
static T Max(T lhs, T rhs)
Definition: DtMath.h:286
static const float32 PI_BY_2
Definition: DtMath.h:154
T w
Definition: DtVector4.h:101
T y
Definition: DtVector3.h:132
static T positiveInfinity(void)
Definition: DtMath.h:149
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:299
#define DT_DLL_VRVGRAPHICSUTILS
Definition: vrvGraphicsUtils.h:18
float32 myVal
Definition: DtMath.h:35
T x
Definition: DtVector2.h:105
static T Min(T lhs, T rhs)
Definition: DtMath.h:273
assert macros
float32 val() const
Definition: DtMath.h:30
T y
Definition: DtVector4.h:101
static const float32 Gravity
Definition: DtMath.h:158
static const float32 m_fConvertToRadians
Definition: DtMath.h:159
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 Mon Jun 20 00:38:30 EDT 2022 from SVN revision 244029
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)