MathDefs.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include <cstdlib>
  25. #include <cmath>
  26. #ifndef M_PI
  27. static const float M_PI = 3.141592653589793238462643f;
  28. #endif
  29. static const int M_MIN_INT = 0x80000000;
  30. static const int M_MAX_INT = 0x7fffffff;
  31. static const unsigned M_MIN_UNSIGNED = 0x00000000;
  32. static const unsigned M_MAX_UNSIGNED = 0xffffffff;
  33. static const float M_EPSILON = 0.000001f;
  34. static const float M_LARGE_EPSILON = 0.00005f;
  35. static const float M_MIN_NEARCLIP = 0.01f;
  36. static const float M_MAX_FOV = 160.0f;
  37. static const float M_LARGE_VALUE = 100000000.0f;
  38. static const float M_INFINITY = (float)HUGE_VAL;
  39. static const float M_DEGTORAD = (float)M_PI / 180.0f;
  40. static const float M_RADTODEG = 1.0f / M_DEGTORAD;
  41. /// Intersection test result.
  42. enum Intersection
  43. {
  44. OUTSIDE,
  45. INTERSECTS,
  46. INSIDE
  47. };
  48. /// Linear interpolation between two float values.
  49. inline float Lerp(float lhs, float rhs, float t) { return lhs * (1.0f - t) + rhs * t; }
  50. /// Return a random float between 0.0 (inclusive) and 1.0 (exclusive.)
  51. inline float Random() { return (rand() & 32767) / 32768.0f; }
  52. /// Return a random float between 0.0 and range, inclusive.
  53. inline float Random(float range) { return (rand() & 32767) * range / 32767.0f; }
  54. /// Return a random integer between 0 and range, inclusive.
  55. inline int Random(int range) { return ((rand() & 32767) * range + 16384) / 32767; }
  56. /// Return the smaller of two floats.
  57. inline float Min(float lhs, float rhs) { return lhs < rhs ? lhs : rhs; }
  58. /// Return the larger of two floats.
  59. inline float Max(float lhs, float rhs) { return lhs > rhs ? lhs : rhs; }
  60. /// Clamp a float to a range.
  61. inline float Clamp(float value, float min, float max)
  62. {
  63. if (value < min)
  64. return min;
  65. else if (value > max)
  66. return max;
  67. else
  68. return value;
  69. }
  70. /// Check whether two floating point values are equal within accuracy.
  71. inline bool Equals(float lhs, float rhs) { return lhs + M_EPSILON >= rhs && lhs - M_EPSILON <= rhs; }
  72. /// Return the smaller of two integers.
  73. inline int Min(int lhs, int rhs) { return lhs < rhs ? lhs : rhs; }
  74. /// Return the larger of two integers.
  75. inline int Max(int lhs, int rhs) { return lhs > rhs ? lhs : rhs; }
  76. /// Clamp an integer to a range.
  77. inline int Clamp(int value, int min, int max)
  78. {
  79. if (value < min)
  80. return min;
  81. else if (value > max)
  82. return max;
  83. else
  84. return value;
  85. }
  86. /// Check whether an unsigned integer is a power of two.
  87. inline bool IsPowerOfTwo(unsigned value)
  88. {
  89. if (!value)
  90. return true;
  91. while (!(value & 1))
  92. value >>= 1;
  93. return value == 1;
  94. }
  95. /// Round up to next power of two.
  96. inline unsigned NextPowerOfTwo(unsigned value)
  97. {
  98. unsigned ret = 1;
  99. while (ret < value)
  100. ret <<= 1;
  101. return ret;
  102. }
  103. /// Fast square root.
  104. inline float FastSqrt(float x)
  105. {
  106. union
  107. {
  108. float f;
  109. int i;
  110. } u;
  111. u.f = x;
  112. u.i -= 1 << 23;
  113. u.i >>= 1;
  114. u.i += 1 << 29;
  115. return u.f;
  116. }
  117. /// Fast inverse square root.
  118. inline float FastInvSqrt(float x)
  119. {
  120. union
  121. {
  122. float f;
  123. int i;
  124. } u;
  125. float xHalf = 0.5f * x;
  126. u.f = x;
  127. u.i = 0x5f3759df - (u.i >> 1);
  128. x = u.f * (1.5f - xHalf * u.f * u.f);
  129. return x;
  130. }
  131. /// Update a hash with the given 8-bit value using the SDBM algorithm.
  132. inline unsigned SDBMHash(unsigned hash, unsigned char c) { return c + (hash << 6) + (hash << 16) - hash; }