MathDefs.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #ifdef _MSC_VER
  24. #pragma warning(push)
  25. #pragma warning(disable:4018) // Signed/unsigned mismatch
  26. #pragma warning(disable:4244) // Conversion from 'double' to 'float'
  27. #pragma warning(disable:4702) // unreachable code
  28. #endif
  29. #include "../Math/Random.h"
  30. #include <cstdlib>
  31. #include <cmath>
  32. #include <limits>
  33. namespace Urho3D
  34. {
  35. #undef M_PI
  36. static const float M_PI = 3.14159265358979323846264338327950288f;
  37. static const float M_HALF_PI = M_PI * 0.5f;
  38. static const int M_MIN_INT = 0x80000000;
  39. static const int M_MAX_INT = 0x7fffffff;
  40. static const unsigned M_MIN_UNSIGNED = 0x00000000;
  41. static const unsigned M_MAX_UNSIGNED = 0xffffffff;
  42. static const float M_EPSILON = 0.000001f;
  43. static const float M_LARGE_EPSILON = 0.00005f;
  44. static const float M_MIN_NEARCLIP = 0.01f;
  45. static const float M_MAX_FOV = 160.0f;
  46. static const float M_LARGE_VALUE = 100000000.0f;
  47. static const float M_INFINITY = (float)HUGE_VAL;
  48. static const float M_DEGTORAD = M_PI / 180.0f;
  49. static const float M_DEGTORAD_2 = M_PI / 360.0f; // M_DEGTORAD / 2.f
  50. static const float M_RADTODEG = 1.0f / M_DEGTORAD;
  51. /// Intersection test result.
  52. enum Intersection
  53. {
  54. OUTSIDE,
  55. INTERSECTS,
  56. INSIDE
  57. };
  58. /// Check whether two floating point values are equal within accuracy.
  59. template <class T>
  60. inline bool Equals(T lhs, T rhs) { return lhs + std::numeric_limits<T>::epsilon() >= rhs && lhs - std::numeric_limits<T>::epsilon() <= rhs; }
  61. /// Linear interpolation between two values.
  62. template <class T, class U>
  63. inline T Lerp(T lhs, T rhs, U t) { return lhs * (1.0 - t) + rhs * t; }
  64. /// Return the smaller of two values.
  65. template <class T, class U>
  66. inline T Min(T lhs, U rhs) { return lhs < rhs ? lhs : rhs; }
  67. /// Return the larger of two values.
  68. template <class T, class U>
  69. inline T Max(T lhs, U rhs) { return lhs > rhs ? lhs : rhs; }
  70. /// Return absolute value of a value
  71. template <class T>
  72. inline T Abs(T value) { return value >= 0.0 ? value : -value; }
  73. /// Return the sign of a float (-1, 0 or 1.)
  74. template <class T>
  75. inline T Sign(T value) { return value > 0.0 ? 1.0 : (value < 0.0 ? -1.0 : 0.0); }
  76. /// Check whether a floating point value is NaN.
  77. /// Use a workaround for GCC, see https://github.com/urho3d/Urho3D/issues/655
  78. #ifndef __GNUC__
  79. inline bool IsNaN(float value) { return value != value; }
  80. #else
  81. inline bool IsNaN(float value)
  82. {
  83. unsigned u = *(unsigned*)(&value);
  84. return (u & 0x7fffffff) > 0x7f800000;
  85. }
  86. #endif
  87. /// Clamp a number to a range.
  88. template <class T>
  89. inline T Clamp(T value, T min, T max)
  90. {
  91. if (value < min)
  92. return min;
  93. else if (value > max)
  94. return max;
  95. else
  96. return value;
  97. }
  98. /// Smoothly damp between values.
  99. template <class T>
  100. inline T SmoothStep(T lhs, T rhs, T t)
  101. {
  102. t = Clamp((t - lhs) / (rhs - lhs), T(0.0), T(1.0)); // Saturate t
  103. return t * t * (3.0 - 2.0 * t);
  104. }
  105. /// Return sine of an angle in degrees. Uses sinf() for floats, sin() for
  106. /// everything else
  107. template <class T>
  108. inline T Sin(T angle) { return sin(angle * M_DEGTORAD); }
  109. template <> inline float Sin<float>(float angle) { return sinf(angle * M_DEGTORAD); }
  110. /// Return cosine of an angle in degrees. Uses cosf() for floats, cos() for
  111. /// everything else
  112. template <class T>
  113. inline T Cos(T angle) { return cos(angle * M_DEGTORAD); }
  114. template <> inline float Cos<float>(float angle) { return cosf(angle * M_DEGTORAD); }
  115. /// Return tangent of an angle in degrees. Uses tanf() for floats, tan() for
  116. /// everything else
  117. template <class T>
  118. inline T Tan(T angle) { return tan(angle * M_DEGTORAD); }
  119. template <> inline float Tan<float>(float angle) { return tanf(angle * M_DEGTORAD); }
  120. /// Return arc sine in degrees. Uses asinf() for floats, asin() for everything
  121. /// else
  122. template <class T>
  123. inline T Asin(T x) { return M_RADTODEG * asin(Clamp(x, -1.0, 1.0)); }
  124. template <> inline float Asin<float>(float x) { return M_RADTODEG * asinf(Clamp(x, -1.0f, 1.0f)); }
  125. /// Return arc cosine in degrees. Uses acosf() for floats, acos() for
  126. /// everything else
  127. template <class T>
  128. inline T Acos(T x) { return M_RADTODEG * acos(Clamp(x, -1.0, 1.0)); }
  129. template <> inline float Acos<float>(float x) { return M_RADTODEG * acosf(Clamp(x, -1.0f, 1.0f)); }
  130. /// Return arc tangent in degrees. Uses atanf() for floats, atan() for
  131. /// everything else
  132. template <class T>
  133. inline T Atan(T x) { return M_RADTODEG * atan(x); }
  134. template <> inline float Atan<float>(float x) { return M_RADTODEG * atanf(x); }
  135. /// Return arc tangent of y/x in degrees. Uses atan2f() for floats, atan2()
  136. /// for everything else
  137. template <class T>
  138. inline T Atan2(T y, T x) { return M_RADTODEG * atan2(y, x); }
  139. template <> inline float Atan2<float>(float y, float x) { return M_RADTODEG * atan2f(y, x); }
  140. /// Check whether an unsigned integer is a power of two.
  141. inline bool IsPowerOfTwo(unsigned value)
  142. {
  143. if (!value)
  144. return true;
  145. while (!(value & 1))
  146. value >>= 1;
  147. return value == 1;
  148. }
  149. /// Round up to next power of two.
  150. inline unsigned NextPowerOfTwo(unsigned value)
  151. {
  152. unsigned ret = 1;
  153. while (ret < value && ret < 0x80000000)
  154. ret <<= 1;
  155. return ret;
  156. }
  157. /// Count the number of set bits in a mask.
  158. inline unsigned CountSetBits(unsigned value)
  159. {
  160. // Brian Kernighan's method
  161. unsigned count = 0;
  162. for (count = 0; value; count++)
  163. value &= value - 1;
  164. return count;
  165. }
  166. /// Update a hash with the given 8-bit value using the SDBM algorithm.
  167. inline unsigned SDBMHash(unsigned hash, unsigned char c) { return c + (hash << 6) + (hash << 16) - hash; }
  168. /// Return a random float between 0.0 (inclusive) and 1.0 (exclusive.)
  169. inline float Random() { return Rand() / 32768.0f; }
  170. /// Return a random float between 0.0 and range, inclusive from both ends.
  171. inline float Random(float range) { return Rand() * range / 32767.0f; }
  172. /// Return a random float between min and max, inclusive from both ends.
  173. inline float Random(float min, float max) { return Rand() * (max - min) / 32767.0f + min; }
  174. /// Return a random integer between 0 and range - 1.
  175. inline int Random(int range) { return (int)(Random() * range); }
  176. /// Return a random integer between min and max - 1.
  177. inline int Random(int min, int max) { float range = (float)(max - min); return (int)(Random() * range) + min; }
  178. /// Return a random normal distributed number with the given mean value and variance.
  179. inline float RandomNormal(float meanValue, float variance) { return RandStandardNormal() * sqrtf(variance) + meanValue; }
  180. /// Convert float to half float. From https://gist.github.com/martinkallman/5049614
  181. inline unsigned short FloatToHalf(float value)
  182. {
  183. unsigned inu = *((unsigned*)&value);
  184. unsigned t1 = inu & 0x7fffffff; // Non-sign bits
  185. unsigned t2 = inu & 0x80000000; // Sign bit
  186. unsigned t3 = inu & 0x7f800000; // Exponent
  187. t1 >>= 13; // Align mantissa on MSB
  188. t2 >>= 16; // Shift sign bit into position
  189. t1 -= 0x1c000; // Adjust bias
  190. t1 = (t3 < 0x38800000) ? 0 : t1; // Flush-to-zero
  191. t1 = (t3 > 0x47000000) ? 0x7bff : t1; // Clamp-to-max
  192. t1 = (t3 == 0 ? 0 : t1); // Denormals-as-zero
  193. t1 |= t2; // Re-insert sign bit
  194. return (unsigned short)t1;
  195. }
  196. /// Convert half float to float. From https://gist.github.com/martinkallman/5049614
  197. inline float HalfToFloat(unsigned short value)
  198. {
  199. unsigned t1 = value & 0x7fff; // Non-sign bits
  200. unsigned t2 = value & 0x8000; // Sign bit
  201. unsigned t3 = value & 0x7c00; // Exponent
  202. t1 <<= 13; // Align mantissa on MSB
  203. t2 <<= 16; // Shift sign bit into position
  204. t1 += 0x38000000; // Adjust bias
  205. t1 = (t3 == 0 ? 0 : t1); // Denormals-as-zero
  206. t1 |= t2; // Re-insert sign bit
  207. float out;
  208. *((unsigned*)&out) = t1;
  209. return out;
  210. }
  211. }
  212. #ifdef _MSC_VER
  213. #pragma warning(pop)
  214. #endif