MathDefs.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // Copyright (c) 2008-2015 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. #include "../Math/Random.h"
  24. #include <cstdlib>
  25. #include <cmath>
  26. namespace Atomic
  27. {
  28. #undef M_PI
  29. static const float M_PI = 3.14159265358979323846264338327950288f;
  30. static const float M_HALF_PI = M_PI * 0.5f;
  31. static const int M_MIN_INT = 0x80000000;
  32. static const int M_MAX_INT = 0x7fffffff;
  33. static const unsigned M_MIN_UNSIGNED = 0x00000000;
  34. static const unsigned M_MAX_UNSIGNED = 0xffffffff;
  35. static const float M_EPSILON = 0.000001f;
  36. static const float M_LARGE_EPSILON = 0.00005f;
  37. static const float M_MIN_NEARCLIP = 0.01f;
  38. static const float M_MAX_FOV = 160.0f;
  39. static const float M_LARGE_VALUE = 100000000.0f;
  40. static const float M_INFINITY = (float)HUGE_VAL;
  41. static const float M_DEGTORAD = M_PI / 180.0f;
  42. static const float M_DEGTORAD_2 = M_PI / 360.0f; // M_DEGTORAD / 2.f
  43. static const float M_RADTODEG = 1.0f / M_DEGTORAD;
  44. /// Intersection test result.
  45. enum Intersection
  46. {
  47. OUTSIDE,
  48. INTERSECTS,
  49. INSIDE
  50. };
  51. /// Check whether two floating point values are equal within accuracy.
  52. inline bool Equals(float lhs, float rhs) { return lhs + M_EPSILON >= rhs && lhs - M_EPSILON <= rhs; }
  53. /// Linear interpolation between two float values.
  54. inline float Lerp(float lhs, float rhs, float t) { return lhs * (1.0f - t) + rhs * t; }
  55. /// Linear interpolation between two double values.
  56. inline double Lerp(double lhs, double rhs, float t) { return lhs * (1.0f - t) + rhs * t; }
  57. /// Return the smaller of two floats.
  58. inline float Min(float lhs, float rhs) { return lhs < rhs ? lhs : rhs; }
  59. /// Return the larger of two floats.
  60. inline float Max(float lhs, float rhs) { return lhs > rhs ? lhs : rhs; }
  61. /// Return absolute value of a float.
  62. inline float Abs(float value) { return value >= 0.0f ? value : -value; }
  63. /// Return the sign of a float (-1, 0 or 1.)
  64. inline float Sign(float value) { return value > 0.0f ? 1.0f : (value < 0.0f ? -1.0f : 0.0f); }
  65. /// Check whether a floating point value is NaN.
  66. /// Use a workaround for GCC, see https://github.com/urho3d/Urho3D/issues/655
  67. #ifndef __GNUC__
  68. inline bool IsNaN(float value) { return value != value; }
  69. #else
  70. inline bool IsNaN(float value)
  71. {
  72. unsigned u = *(unsigned*)(&value);
  73. return (u & 0x7fffffff) > 0x7f800000;
  74. }
  75. #endif
  76. /// Clamp a float to a range.
  77. inline float Clamp(float value, float min, float max)
  78. {
  79. if (value < min)
  80. return min;
  81. else if (value > max)
  82. return max;
  83. else
  84. return value;
  85. }
  86. /// Smoothly damp between values.
  87. inline float SmoothStep(float lhs, float rhs, float t)
  88. {
  89. t = Clamp((t - lhs) / (rhs - lhs), 0.0f, 1.0f); // Saturate t
  90. return t * t * (3.0f - 2.0f * t);
  91. }
  92. /// Return sine of an angle in degrees.
  93. inline float Sin(float angle) { return sinf(angle * M_DEGTORAD); }
  94. /// Return cosine of an angle in degrees.
  95. inline float Cos(float angle) { return cosf(angle * M_DEGTORAD); }
  96. /// Return tangent of an angle in degrees.
  97. inline float Tan(float angle) { return tanf(angle * M_DEGTORAD); }
  98. /// Return arc sine in degrees.
  99. inline float Asin(float x) { return M_RADTODEG * asinf(Clamp(x, -1.0f, 1.0f)); }
  100. /// Return arc cosine in degrees.
  101. inline float Acos(float x) { return M_RADTODEG * acosf(Clamp(x, -1.0f, 1.0f)); }
  102. /// Return arc tangent in degrees.
  103. inline float Atan(float x) { return M_RADTODEG * atanf(x); }
  104. /// Return arc tangent of y/x in degrees.
  105. inline float Atan2(float y, float x) { return M_RADTODEG * atan2f(y, x); }
  106. /// Return the smaller of two integers.
  107. inline int Min(int lhs, int rhs) { return lhs < rhs ? lhs : rhs; }
  108. /// Return the larger of two integers.
  109. inline int Max(int lhs, int rhs) { return lhs > rhs ? lhs : rhs; }
  110. /// Return absolute value of an integer
  111. inline int Abs(int value) { return value >= 0 ? value : -value; }
  112. /// Clamp an integer to a range.
  113. inline int Clamp(int value, int min, int max)
  114. {
  115. if (value < min)
  116. return min;
  117. else if (value > max)
  118. return max;
  119. else
  120. return value;
  121. }
  122. /// Check whether an unsigned integer is a power of two.
  123. inline bool IsPowerOfTwo(unsigned value)
  124. {
  125. if (!value)
  126. return true;
  127. while (!(value & 1))
  128. value >>= 1;
  129. return value == 1;
  130. }
  131. /// Round up to next power of two.
  132. inline unsigned NextPowerOfTwo(unsigned value)
  133. {
  134. unsigned ret = 1;
  135. while (ret < value && ret < 0x80000000)
  136. ret <<= 1;
  137. return ret;
  138. }
  139. /// Count the number of set bits in a mask.
  140. inline unsigned CountSetBits(unsigned value)
  141. {
  142. // Brian Kernighan's method
  143. unsigned count = 0;
  144. for (count = 0; value; count++)
  145. value &= value - 1;
  146. return count;
  147. }
  148. /// Update a hash with the given 8-bit value using the SDBM algorithm.
  149. inline unsigned SDBMHash(unsigned hash, unsigned char c) { return c + (hash << 6) + (hash << 16) - hash; }
  150. /// Return a random float between 0.0 (inclusive) and 1.0 (exclusive.)
  151. inline float Random() { return Rand() / 32768.0f; }
  152. /// Return a random float between 0.0 and range, inclusive from both ends.
  153. inline float Random(float range) { return Rand() * range / 32767.0f; }
  154. /// Return a random float between min and max, inclusive from both ends.
  155. inline float Random(float min, float max) { return Rand() * (max - min) / 32767.0f + min; }
  156. /// Return a random integer between 0 and range - 1.
  157. inline int Random(int range) { return (int)(Random() * range); }
  158. /// Return a random integer between min and max - 1.
  159. inline int Random(int min, int max) { float range = (float)(max - min); return (int)(Random() * range) + min; }
  160. /// Return a random normal distributed number with the given mean value and variance.
  161. inline float RandomNormal(float meanValue, float variance) { return RandStandardNormal() * sqrtf(variance) + meanValue; }
  162. /// Convert float to half float. From https://gist.github.com/martinkallman/5049614
  163. inline unsigned short FloatToHalf(float value)
  164. {
  165. unsigned inu = *((unsigned*)&value);
  166. unsigned t1 = inu & 0x7fffffff; // Non-sign bits
  167. unsigned t2 = inu & 0x80000000; // Sign bit
  168. unsigned t3 = inu & 0x7f800000; // Exponent
  169. t1 >>= 13; // Align mantissa on MSB
  170. t2 >>= 16; // Shift sign bit into position
  171. t1 -= 0x1c000; // Adjust bias
  172. t1 = (t3 < 0x38800000) ? 0 : t1; // Flush-to-zero
  173. t1 = (t3 > 0x47000000) ? 0x7bff : t1; // Clamp-to-max
  174. t1 = (t3 == 0 ? 0 : t1); // Denormals-as-zero
  175. t1 |= t2; // Re-insert sign bit
  176. return (unsigned short)t1;
  177. }
  178. /// Convert half float to float. From https://gist.github.com/martinkallman/5049614
  179. inline float HalfToFloat(unsigned short value)
  180. {
  181. unsigned t1 = value & 0x7fff; // Non-sign bits
  182. unsigned t2 = value & 0x8000; // Sign bit
  183. unsigned t3 = value & 0x7c00; // Exponent
  184. t1 <<= 13; // Align mantissa on MSB
  185. t2 <<= 16; // Shift sign bit into position
  186. t1 += 0x38000000; // Adjust bias
  187. t1 = (t3 == 0 ? 0 : t1); // Denormals-as-zero
  188. t1 |= t2; // Re-insert sign bit
  189. float out;
  190. *((unsigned*)&out) = t1;
  191. return out;
  192. }
  193. }