VR-Vantage 2.7 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) 2019 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 
105 
106  template <class T> static bool linePlaneIntersection(const DtVector3<T>& linePoint1, const DtVector3<T>& linePoint2,
107  const DtVector3<T>& planePoint, const DtVector3<T>& planeNormal,
108  DtVector3<T>& intersect, float & w);
110 
111  template <class T> static bool isFinite(T n);
112 
113  template <class T> static T Min(T lhs, T rhs);
114  template <class T> static T Max(T lhs, T rhs);
115 
116 #undef max
117 #undef min
118  template <class T> static T positiveInfinity(void) { return std::numeric_limits<T>::max();}
119  template <class T> static T negativeInfinity(void) { return std::numeric_limits<T>::min();}
120 
121  static const float32 TWO_PI;
122  static const float32 PI;
123  static const float32 PI_BY_2;
124  static const float32 PI_BY_3;
125  static const float32 PI_BY_4;
126  static const float32 PI_BY_6;
127  static const float32 Gravity;
130  private:
131  };
132 
133  template <class T>
134  T DtMath::clamp(T val, T minval, T maxval)
135  {
136  Assert(minval <= maxval && "Invalid clamp range");
137  return std::max(std::min(val, maxval), minval);
138  }
139 
140  template <class T>
141  DtVector2<T> DtMath::clamp(const DtVector2<T>& val, const DtVector2<T>& minval, const DtVector2<T>& maxval)
142  {
143  DtVector2<T> vClampedVal;
144  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
145  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
146  return vClampedVal;
147  }
148 
149  template <class T>
150  DtVector3<T> DtMath::clamp(const DtVector3<T>& val, const DtVector3<T>& minval, const DtVector3<T>& maxval)
151  {
152  DtVector3<T> vClampedVal;
153  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
154  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
155  vClampedVal.z = DtMath::clamp(val.z, minval.z, maxval.z);
156  return vClampedVal;
157  }
158 
159  template <class T>
160  DtVector4<T> DtMath::clamp(const DtVector4<T>& val, const DtVector4<T>& minval, const DtVector4<T>& maxval)
161  {
162  DtVector4<T> vClampedVal;
163  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
164  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
165  vClampedVal.z = DtMath::clamp(val.z, minval.z, maxval.z);
166  vClampedVal.w = DtMath::clamp(val.w, minval.w, maxval.w);
167  return vClampedVal;
168  }
169 
170  template <class T>
171  DtVector3<T> DtMath::toCartesian(T thetaDegrees, T phiDegrees, bool flipYZ)
172  {
173  T theta = toRadians(thetaDegrees);
174  T phi = toRadians(phiDegrees);
175 
176  DtVector3<T> vPosition;
177  vPosition.x = DtMath::Cos(theta)*Cos(phi);
178  vPosition.y = DtMath::Sin(theta)*Cos(phi);
179  vPosition.z = DtMath::Sin(phi);
180 
181  if (flipYZ)
182  {
183  T temp = vPosition.y;
184  vPosition.y = vPosition.z;
185  vPosition.z = temp;
186  }
187 
188  return vPosition;
189  }
190 
191  template <class T>
192  bool DtMath::linePlaneIntersection(const DtVector3<T>& linePoint1, const DtVector3<T>& linePoint2,
193  const DtVector3<T>& planePoint, const DtVector3<T>& planeNormal,
194  DtVector3<T>& intersect, float& w)
195  {
196  DtVector3<T> lineDir = linePoint2 - linePoint1;
197  lineDir.normalize();
198 
199  DtVector3<T> pnorm = planeNormal;
200  pnorm.normalize();
201 
202  float denom = lineDir.dotProduct(pnorm);
203  if (denom == 0.0f)
204  {
205  return false;
206  }
207 
208  DtVector3<T> pTolDelta = (planePoint - linePoint1);
209  w = pTolDelta.dotProduct(pnorm) / denom;
210  intersect = (lineDir * w) + linePoint1;
211  return true;
212  }
213 
214  template <class T>
216  {
217  int digits = 0;
218  while (val) {
219  val /= 10;
220  digits++;
221  }
222  return digits;
223  }
224 
225  template <class T>
227  {
228  double normalizedValue = (double)(val)/(double)(std::numeric_limits<T>::max());
229  return normalizedValue;
230  }
231 
232  template <class T>
234  {
235  return ((n == n)
236  && (n != +std::numeric_limits<T>::infinity())
237  && (n != -std::numeric_limits<T>::infinity())
238  );
239  }
240 
241  template <class T>
242  T DtMath::Min(T lhs, T rhs)
243  {
244  if (lhs < rhs)
245  {
246  return lhs;
247  }
248  else
249  {
250  return rhs;
251  }
252  }
253 
254  template <class T>
255  T DtMath::Max(T lhs, T rhs)
256  {
257  if (lhs > rhs)
258  {
259  return lhs;
260  }
261  else
262  {
263  return rhs;
264  }
265  }
266 
267 } //namespace makVrv


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