VR-Forces 4.10 Class 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  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 
98  template <class T> static T clamp(T val, T minval, T maxval);
100 
101  template <class T> static DtVector2<T> clamp(const DtVector2<T>& val, const DtVector2<T>& minval, const DtVector2<T>& maxval);
102  template <class T> static DtVector3<T> clamp(const DtVector3<T>& val, const DtVector3<T>& minval, const DtVector3<T>& maxval);
103  template <class T> static DtVector4<T> clamp(const DtVector4<T>& val, const DtVector4<T>& minval, const DtVector4<T>& maxval);
105 
106  template <class T> static DtVector3<T> toCartesian(T thetaDegrees, T phiDegrees, bool flipYZ);
107 
109  template <class T> static bool linePlaneIntersection(const DtVector3<T>& linePoint1, const DtVector3<T>& linePoint2,
111  const DtVector3<T>& planePoint, const DtVector3<T>& planeNormal,
112  DtVector3<T>& intersect, float & w);
114 
115  template <class T> static bool isFinite(T n);
116 
117  template <class T> static T Min(T lhs, T rhs);
118  template <class T> static T Max(T lhs, T rhs);
119 
121  static double fractional(double val);
122 
123  // This returns 0 for non-power-of-two numbers, or sizes out of reasonable light
124  // grid range (<4, or >256). Currently, a grid size of 32 (log2=5) is hardcoded
125  // elsewhere.
126  static unsigned int tilelog2(unsigned int x);
127 
129  template <class T>
130  static T rpmToRadiansPerSec(T rpm);
131 
133  template <class T>
134  static T radiansPerSecToRpm(T radiansPerSec);
135 
136 #undef max
137 #undef min
138  template <class T> static T positiveInfinity(void) { return std::numeric_limits<T>::max();}
139  template <class T> static T negativeInfinity(void) { return std::numeric_limits<T>::min();}
140 
141  static const float32 TWO_PI;
142  static const float32 PI;
143  static const float32 PI_BY_2;
144  static const float32 PI_BY_3;
145  static const float32 PI_BY_4;
146  static const float32 PI_BY_6;
147  static const float32 Gravity;
150  private:
151  };
152 
153  template <class T>
154  T DtMath::clamp(T val, T minval, T maxval)
155  {
156  Assert(minval <= maxval && "Invalid clamp range");
157  return std::max(std::min(val, maxval), minval);
158  }
159 
160  template <class T>
161  DtVector2<T> DtMath::clamp(const DtVector2<T>& val, const DtVector2<T>& minval, const DtVector2<T>& maxval)
162  {
163  DtVector2<T> vClampedVal;
164  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
165  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
166  return vClampedVal;
167  }
168 
169  template <class T>
170  DtVector3<T> DtMath::clamp(const DtVector3<T>& val, const DtVector3<T>& minval, const DtVector3<T>& maxval)
171  {
172  DtVector3<T> vClampedVal;
173  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
174  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
175  vClampedVal.z = DtMath::clamp(val.z, minval.z, maxval.z);
176  return vClampedVal;
177  }
178 
179  template <class T>
180  DtVector4<T> DtMath::clamp(const DtVector4<T>& val, const DtVector4<T>& minval, const DtVector4<T>& maxval)
181  {
182  DtVector4<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  vClampedVal.z = DtMath::clamp(val.z, minval.z, maxval.z);
186  vClampedVal.w = DtMath::clamp(val.w, minval.w, maxval.w);
187  return vClampedVal;
188  }
189 
190  template <class T>
191  DtVector3<T> DtMath::toCartesian(T thetaDegrees, T phiDegrees, bool flipYZ)
192  {
193  T theta = toRadians(thetaDegrees);
194  T phi = toRadians(phiDegrees);
195 
196  DtVector3<T> vPosition;
197  vPosition.x = DtMath::Cos(theta)*Cos(phi);
198  vPosition.y = DtMath::Sin(theta)*Cos(phi);
199  vPosition.z = DtMath::Sin(phi);
200 
201  if (flipYZ)
202  {
203  T temp = vPosition.y;
204  vPosition.y = vPosition.z;
205  vPosition.z = temp;
206  }
207 
208  return vPosition;
209  }
210 
211  template <class T>
212  bool DtMath::linePlaneIntersection(const DtVector3<T>& linePoint1, const DtVector3<T>& linePoint2,
213  const DtVector3<T>& planePoint, const DtVector3<T>& planeNormal,
214  DtVector3<T>& intersect, float& w)
215  {
216  DtVector3<T> lineDir = linePoint2 - linePoint1;
217  lineDir.normalize();
218 
219  DtVector3<T> pnorm = planeNormal;
220  pnorm.normalize();
221 
222  float denom = lineDir.dotProduct(pnorm);
223  if (denom == 0.0f)
224  {
225  return false;
226  }
227 
228  DtVector3<T> pTolDelta = (planePoint - linePoint1);
229  w = pTolDelta.dotProduct(pnorm) / denom;
230  intersect = (lineDir * w) + linePoint1;
231  return true;
232  }
233 
234  template <class T>
235  int DtMath::NumDigits(T val)
236  {
237  int digits = 0;
238  while (val) {
239  val /= 10;
240  digits++;
241  }
242  return digits;
243  }
244 
245  template <class T>
247  {
248  double normalizedValue = (double)(val)/(double)(std::numeric_limits<T>::max());
249  return normalizedValue;
250  }
251 
252  template <class T>
254  {
255  return ((n == n)
256  && (n != +std::numeric_limits<T>::infinity())
257  && (n != -std::numeric_limits<T>::infinity())
258  );
259  }
260 
261  template <class T>
262  T DtMath::Min(T lhs, T rhs)
263  {
264  if (lhs < rhs)
265  {
266  return lhs;
267  }
268  else
269  {
270  return rhs;
271  }
272  }
273 
274  template <class T>
275  T DtMath::Max(T lhs, T rhs)
276  {
277  if (lhs > rhs)
278  {
279  return lhs;
280  }
281  else
282  {
283  return rhs;
284  }
285  }
286 
287  template <class T>
289  {
290  return (rpm / 60.0f) * 2.0f * DtMath::PI;
291  }
292 
293  template <class T>
294  T DtMath::radiansPerSecToRpm(T radiansPerSec)
295  {
296  return (radiansPerSec / (2.0f * DtMath::PI)) * 60.0f;
297  }
298 
299 } //namespace makVrv
Dll export defines.
static T clamp(T val, T minval, T maxval)
Clamping routines.
Definition: DtMath.h:154
static const float32 PI
Definition: DtMath.h:142
static T Sin(T val)
Definition: DtMath.h:61
DtRadians(float32 fVal)
Definition: DtMath.h:41
static T Squared(T val)
Definition: DtMath.h:69
virtual ~DtRadians()
Definition: DtMath.h:42
T x
Definition: DtVector4.h:95
float32 myVal
Definition: DtMath.h:48
static const float32 PI_BY_3
Definition: DtMath.h:144
Definition: DtMath.h:38
static double GetNormalizedValueRespectingLength(T val)
Definition: DtMath.h:246
static T Cos(T val)
Definition: DtMath.h:62
static const float32 PI_BY_6
Definition: DtMath.h:146
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:139
static T radiansPerSecToRpm(T radiansPerSec)
radians per sec to rpm
Definition: DtMath.h:294
static const float32 PI_BY_4
Definition: DtMath.h:145
static bool isFinite(T n)
Definition: DtMath.h:253
T y
Definition: DtVector2.h:101
T z
Definition: DtVector3.h:132
static const float32 TWO_PI
Definition: DtMath.h:141
static int NumDigits(T val)
Definition: DtMath.h:235
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:212
static const float32 m_fConvertToDegrees
Definition: DtMath.h:149
4 dimensional vector
Definition: DtMath.h:22
static T Tan(T val)
Definition: DtMath.h:63
static DtVector3< T > toCartesian(T thetaDegrees, T phiDegrees, bool flipYZ)
Definition: DtMath.h:191
static int Abs(int val)
Definition: DtMath.h:60
float float32
Definition: DtCommonTypes.h:33
Definition: DtMath.h:25
T z
Definition: DtVector4.h:95
static T Max(T lhs, T rhs)
Definition: DtMath.h:275
static const float32 PI_BY_2
Definition: DtMath.h:143
T w
Definition: DtVector4.h:95
T y
Definition: DtVector3.h:132
static T positiveInfinity(void)
Definition: DtMath.h:138
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:288
#define DT_DLL_VRVGRAPHICSUTILS
Definition: vrvGraphicsUtils.h:18
float32 myVal
Definition: DtMath.h:35
T x
Definition: DtVector2.h:101
static T Min(T lhs, T rhs)
Definition: DtMath.h:262
assert macros
float32 val() const
Definition: DtMath.h:30
T y
Definition: DtVector4.h:95
static const float32 Gravity
Definition: DtMath.h:147
static const float32 m_fConvertToRadians
Definition: DtMath.h:148
static T Abs(T val)
Definition: DtMath.h:59
2 dimensional vector
Definition: DtMath.h:20
#define Assert(exp)
Definition: DtAssert.h:25

Document ID: Generated on Tue Sep 21 17:42:52 EDT 2021 from SVN revision 234861
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)