VR-Vantage 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) 2014 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 #pragma once
10 
11 #include "DtGraphicsUtilsPreReqs.h"
12 
13 #include <cmath>
14 #include <limits>
15 #include "DtVectorForwardDeclare.h"
16 #include "DtAssert.h"
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 #undef max
105 #undef min
106  template <class T> static T positiveInfinity(void) { return std::numeric_limits<T>::max();}
107  template <class T> static T negativeInfinity(void) { return std::numeric_limits<T>::min();}
108 
109  static const float32 TWO_PI;
110  static const float32 PI;
111  static const float32 PI_BY_2;
112  static const float32 PI_BY_3;
113  static const float32 PI_BY_4;
114  static const float32 PI_BY_6;
115  static const float32 Gravity;
118  private:
119  };
120 
121  template <class T>
122  T DtMath::clamp(T val, T minval, T maxval)
123  {
124  Assert(minval <= maxval && "Invalid clamp range");
125  return std::max(std::min(val, maxval), minval);
126  }
127 
128  template <class T>
129  DtVector2<T> DtMath::clamp(const DtVector2<T>& val, const DtVector2<T>& minval, const DtVector2<T>& maxval)
130  {
131  DtVector2<T> vClampedVal;
132  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
133  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
134  return vClampedVal;
135  }
136 
137  template <class T>
138  DtVector3<T> DtMath::clamp(const DtVector3<T>& val, const DtVector3<T>& minval, const DtVector3<T>& maxval)
139  {
140  DtVector3<T> vClampedVal;
141  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
142  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
143  vClampedVal.z = DtMath::clamp(val.z, minval.z, maxval.z);
144  return vClampedVal;
145  }
146 
147  template <class T>
148  DtVector4<T> DtMath::clamp(const DtVector4<T>& val, const DtVector4<T>& minval, const DtVector4<T>& maxval)
149  {
150  DtVector4<T> vClampedVal;
151  vClampedVal.x = DtMath::clamp(val.x, minval.x, maxval.x);
152  vClampedVal.y = DtMath::clamp(val.y, minval.y, maxval.y);
153  vClampedVal.z = DtMath::clamp(val.z, minval.z, maxval.z);
154  vClampedVal.w = DtMath::clamp(val.w, minval.w, maxval.w);
155  return vClampedVal;
156  }
157 
158  template <class T>
159  DtVector3<T> DtMath::toCartesian(T thetaDegrees, T phiDegrees, bool flipYZ)
160  {
161  T theta = toRadians(thetaDegrees);
162  T phi = toRadians(phiDegrees);
163 
164  DtVector3<T> vPosition;
165  vPosition.x = DtMath::Cos(theta)*Cos(phi);
166  vPosition.y = DtMath::Sin(theta)*Cos(phi);
167  vPosition.z = DtMath::Sin(phi);
168 
169  if (flipYZ)
170  {
171  T temp = vPosition.y;
172  vPosition.y = vPosition.z;
173  vPosition.z = temp;
174  }
175 
176  return vPosition;
177  }
178 
179  template <class T>
181  {
182  int digits = 0;
183  while (val) {
184  val /= 10;
185  digits++;
186  }
187  return digits;
188  }
189 
190  template <class T>
192  {
193  double normalizedValue = (double)(val)/(double)(std::numeric_limits<T>::max());
194  return normalizedValue;
195  }
196 
197 } //namespace makVrv


Copyright © 2005-2013 VT MÄK. All Rights Reserved (www.mak.com)