MathDefs.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Copyright (c) 2008-2013 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 "Random.h"
  24. #include <cstdlib>
  25. #include <cmath>
  26. namespace Urho3D
  27. {
  28. #undef M_PI
  29. static const float M_PI = 3.14159265358979323846264338327950288f;
  30. static const int M_MIN_INT = 0x80000000;
  31. static const int M_MAX_INT = 0x7fffffff;
  32. static const unsigned M_MIN_UNSIGNED = 0x00000000;
  33. static const unsigned M_MAX_UNSIGNED = 0xffffffff;
  34. static const float M_EPSILON = 0.000001f;
  35. static const float M_LARGE_EPSILON = 0.00005f;
  36. static const float M_MIN_NEARCLIP = 0.01f;
  37. static const float M_MAX_FOV = 160.0f;
  38. static const float M_LARGE_VALUE = 100000000.0f;
  39. static const float M_INFINITY = (float)HUGE_VAL;
  40. static const float M_DEGTORAD = (float)M_PI / 180.0f;
  41. static const float M_DEGTORAD_2 = (float)M_PI / 360.0f; // M_DEGTORAD / 2.f
  42. static const float M_RADTODEG = 1.0f / M_DEGTORAD;
  43. /// Intersection test result.
  44. enum Intersection
  45. {
  46. OUTSIDE,
  47. INTERSECTS,
  48. INSIDE
  49. };
  50. /// Check whether two floating point values are equal within accuracy.
  51. inline bool Equals(float lhs, float rhs) { return lhs + M_EPSILON >= rhs && lhs - M_EPSILON <= rhs; }
  52. /// Linear interpolation between two float values.
  53. inline float Lerp(float lhs, float rhs, float t) { return lhs * (1.0f - t) + rhs * t; }
  54. /// Return the smaller of two floats.
  55. inline float Min(float lhs, float rhs) { return lhs < rhs ? lhs : rhs; }
  56. /// Return the larger of two floats.
  57. inline float Max(float lhs, float rhs) { return lhs > rhs ? lhs : rhs; }
  58. /// Return absolute value of a float.
  59. inline float Abs(float value) { return value >= 0.0f ? value : -value; }
  60. /// Return the sign of a float (-1, 0 or 1.)
  61. inline float Sign(float value) { return value > 0.0f ? 1.0f : (value < 0.0f ? -1.0f : 0.0f); }
  62. /// Clamp a float to a range.
  63. inline float Clamp(float value, float min, float max)
  64. {
  65. if (value < min)
  66. return min;
  67. else if (value > max)
  68. return max;
  69. else
  70. return value;
  71. }
  72. /// Return sine of an angle in degrees.
  73. inline float Sin(float angle) { return sinf(angle * M_DEGTORAD); }
  74. /// Return cosine of an angle in degrees.
  75. inline float Cos(float angle) { return cosf(angle * M_DEGTORAD); }
  76. /// Return tangent of an angle in degrees.
  77. inline float Tan(float angle) { return tanf(angle * M_DEGTORAD); }
  78. /// Return arc sine in degrees.
  79. inline float Asin(float x) { return M_RADTODEG * asinf(Clamp(x, -1.0f, 1.0f)); }
  80. /// Return arc cosine in degrees.
  81. inline float Acos(float x) { return M_RADTODEG * acosf(Clamp(x, -1.0f, 1.0f)); }
  82. /// Return arc tangent in degrees.
  83. inline float Atan(float x) { return M_RADTODEG * atanf(x); }
  84. /// Return arc tangent of y/x in degrees.
  85. inline float Atan2(float y, float x) { return M_RADTODEG * atan2f(y, x); }
  86. /// Return the smaller of two integers.
  87. inline int Min(int lhs, int rhs) { return lhs < rhs ? lhs : rhs; }
  88. /// Return the larger of two integers.
  89. inline int Max(int lhs, int rhs) { return lhs > rhs ? lhs : rhs; }
  90. /// Return absolute value of an integer
  91. inline int Abs(int value) { return value >= 0 ? value : -value; }
  92. /// Clamp an integer to a range.
  93. inline int Clamp(int value, int min, int max)
  94. {
  95. if (value < min)
  96. return min;
  97. else if (value > max)
  98. return max;
  99. else
  100. return value;
  101. }
  102. /// Check whether an unsigned integer is a power of two.
  103. inline bool IsPowerOfTwo(unsigned value)
  104. {
  105. if (!value)
  106. return true;
  107. while (!(value & 1))
  108. value >>= 1;
  109. return value == 1;
  110. }
  111. /// Round up to next power of two.
  112. inline unsigned NextPowerOfTwo(unsigned value)
  113. {
  114. unsigned ret = 1;
  115. while (ret < value && ret < 0x80000000)
  116. ret <<= 1;
  117. return ret;
  118. }
  119. /// Update a hash with the given 8-bit value using the SDBM algorithm.
  120. inline unsigned SDBMHash(unsigned hash, unsigned char c) { return c + (hash << 6) + (hash << 16) - hash; }
  121. /// Return a random float between 0.0 (inclusive) and 1.0 (exclusive.)
  122. inline float Random() { return Rand() / 32768.0f; }
  123. /// Return a random float between 0.0 and range, inclusive from both ends.
  124. inline float Random(float range) { return Rand() * range / 32767.0f; }
  125. /// Return a random float between min and max, inclusive from both ends.
  126. inline float Random(float min, float max) { return Rand() * (max - min) / 32767.0f + min; }
  127. /// Return a random integer between 0 and range - 1.
  128. inline int Random(int range) { return (Rand() * (range - 1) + 16384) / 32767; }
  129. /// Return a random integer between min and max - 1.
  130. inline int Random(int min, int max) { return (Rand() * (max - min - 1) + 16384) / 32767 + min; }
  131. }