VR-Forces 4.6 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) 2017 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 
104  template <class T> static bool isFinite(T n);
105 
106 #undef max
107 #undef min
108  template <class T> static T positiveInfinity(void) { return std::numeric_limits<T>::max();}
109  template <class T> static T negativeInfinity(void) { return std::numeric_limits<T>::min();}
110 
111  static const float32 TWO_PI;
112  static const float32 PI;
113  static const float32 PI_BY_2;
114  static const float32 PI_BY_3;
115  static const float32 PI_BY_4;
116  static const float32 PI_BY_6;
117  static const float32 Gravity;
120  private:
121  };
122 
123  template <class T>
124  T DtMath::clamp(T val, T minval, T maxval)
125  {
126  Assert(minval <= maxval && "Invalid clamp range");
127  return std::max(std::min(val, maxval), minval);
128  }
129 
130  template <class T>
131  DtVector2<T> DtMath::clamp(const DtVector2<T>& val, const DtVector2<T>& minval, const DtVector2<T>& maxval)
132  {
133  DtVector2<T> vClampedVal;
134  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
135  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
136  return vClampedVal;
137  }
138 
139  template <class T>
140  DtVector3<T> DtMath::clamp(const DtVector3<T>& val, const DtVector3<T>& minval, const DtVector3<T>& maxval)
141  {
142  DtVector3<T> vClampedVal;
143  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
144  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
145  vClampedVal.z = DtMath::clamp(val.z, minval.z, maxval.z);
146  return vClampedVal;
147  }
148 
149  template <class T>
150  DtVector4<T> DtMath::clamp(const DtVector4<T>& val, const DtVector4<T>& minval, const DtVector4<T>& maxval)
151  {
152  DtVector4<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  vClampedVal.w = DtMath::clamp(val.w, minval.w, maxval.w);
157  return vClampedVal;
158  }
159 
160  template <class T>
161  DtVector3<T> DtMath::toCartesian(T thetaDegrees, T phiDegrees, bool flipYZ)
162  {
163  T theta = toRadians(thetaDegrees);
164  T phi = toRadians(phiDegrees);
165 
166  DtVector3<T> vPosition;
167  vPosition.x = DtMath::Cos(theta)*Cos(phi);
168  vPosition.y = DtMath::Sin(theta)*Cos(phi);
169  vPosition.z = DtMath::Sin(phi);
170 
171  if (flipYZ)
172  {
173  T temp = vPosition.y;
174  vPosition.y = vPosition.z;
175  vPosition.z = temp;
176  }
177 
178  return vPosition;
179  }
180 
181  template <class T>
182  int DtMath::NumDigits(T val)
183  {
184  int digits = 0;
185  while (val) {
186  val /= 10;
187  digits++;
188  }
189  return digits;
190  }
191 
192  template <class T>
194  {
195  double normalizedValue = (double)(val)/(double)(std::numeric_limits<T>::max());
196  return normalizedValue;
197  }
198 
199  template <class T>
201  {
202  return ((n == n)
203  && (n != +std::numeric_limits<T>::infinity())
204  && (n != -std::numeric_limits<T>::infinity())
205  );
206  }
207 
208 } //namespace makVrv

Document ID: Generated on Thu Apr 12 03:15:37 EDT 2018 from SVN revision 187986
Copyright © 2005-2018 VT MÄK. All Rights Reserved (www.mak.com)