MathDefs.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_MIN_NEARCLIP = 0.01f;
  35. static const float M_MAX_FOV = 160.0f;
  36. static const float M_LARGE_VALUE = 100000000.0f;
  37. static const float M_INFINITY = (float)HUGE_VAL;
  38. static const float M_DEGTORAD = (float)M_PI / 180.0f;
  39. static const float M_RADTODEG = 1.0f / M_DEGTORAD;
  40. /// Intersection test result
  41. enum Intersection
  42. {
  43. OUTSIDE,
  44. INTERSECTS,
  45. INSIDE
  46. };
  47. /// Linear interpolation between two float values
  48. inline float Lerp(float lhs, float rhs, float t)
  49. {
  50. return lhs * (1.0f - t) + rhs * t;
  51. }
  52. /// Return a random float between 0.0 (inclusive) and 1.0 (exclusive)
  53. inline float Random()
  54. {
  55. return (rand() & 32767) / 32768.0f;
  56. }
  57. /// Return a random float between 0.0 and range, inclusive
  58. inline float Random(float range)
  59. {
  60. return (rand() & 32767) * range / 32767.0f;
  61. }
  62. /// Return a random integer between 0 and range, inclusive
  63. inline int Random(int range)
  64. {
  65. return ((rand() & 32767) * range + 16384) / 32767;
  66. }
  67. /// Return the smaller of two floats
  68. inline float Min(float lhs, float rhs)
  69. {
  70. return lhs < rhs ? lhs : rhs;
  71. }
  72. /// Return the larger of two floats
  73. inline float Max(float lhs, float rhs)
  74. {
  75. return lhs > rhs ? lhs : rhs;
  76. }
  77. /// Clamp a float to a range
  78. inline float Clamp(float value, float min, float max)
  79. {
  80. if (value < min)
  81. return min;
  82. else if (value > max)
  83. return max;
  84. else
  85. return value;
  86. }
  87. /// Check whether two floating point values are equal within accuracy
  88. inline bool Equals(float lhs, float rhs)
  89. {
  90. return lhs + M_EPSILON >= rhs && lhs - M_EPSILON <= rhs;
  91. }
  92. /// Return the smaller of two integers
  93. inline int Min(int lhs, int rhs)
  94. {
  95. return lhs < rhs ? lhs : rhs;
  96. }
  97. /// Return the larger of two integers
  98. inline int Max(int lhs, int rhs)
  99. {
  100. return lhs > rhs ? lhs : rhs;
  101. }
  102. /// Clamp an integer to a range
  103. inline int Clamp(int value, int min, int max)
  104. {
  105. if (value < min)
  106. return min;
  107. else if (value > max)
  108. return max;
  109. else
  110. return value;
  111. }
  112. /// Check whether an unsigned integer is a power of two
  113. inline bool IsPowerOfTwo(unsigned value)
  114. {
  115. if (!value)
  116. return true;
  117. while (!(value & 1))
  118. value >>= 1;
  119. return value == 1;
  120. }
  121. /// Round up to next power of two
  122. inline unsigned NextPowerOfTwo(unsigned value)
  123. {
  124. unsigned ret = 1;
  125. while (ret < value)
  126. ret <<= 1;
  127. return ret;
  128. }
  129. /// Fast square root
  130. inline float FastSqrt(float x)
  131. {
  132. union
  133. {
  134. float f;
  135. int i;
  136. } u;
  137. u.f = x;
  138. u.i -= 1 << 23;
  139. u.i >>= 1;
  140. u.i += 1 << 29;
  141. return u.f;
  142. }
  143. /// Fast inverse square root
  144. inline float FastInvSqrt(float x)
  145. {
  146. union
  147. {
  148. float f;
  149. int i;
  150. } u;
  151. float xHalf = 0.5f * x;
  152. u.f = x;
  153. u.i = 0x5f3759df - (u.i >> 1);
  154. x = u.f * (1.5f - xHalf * u.f * u.f);
  155. return x;
  156. }
  157. /// Update a hash with the given 8-bit value using the SDBM algorithm
  158. inline unsigned SDBMHash(unsigned hash, unsigned char c)
  159. {
  160. return c + (hash << 6) + (hash << 16) - hash;
  161. }