MathDefs.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #ifndef M_PI
  29. static const float M_PI = 3.14159265358979323846264338327950288f;
  30. #endif
  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 = (float)M_PI / 180.0f;
  42. static const float M_DEGTORAD_2 = (float)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. /// Linear interpolation between two float values.
  52. inline float Lerp(float lhs, float rhs, float t) { return lhs * (1.0f - t) + rhs * t; }
  53. /// Return the smaller of two floats.
  54. inline float Min(float lhs, float rhs) { return lhs < rhs ? lhs : rhs; }
  55. /// Return the larger of two floats.
  56. inline float Max(float lhs, float rhs) { return lhs > rhs ? lhs : rhs; }
  57. /// Return absolute value of a float.
  58. inline float Abs(float value) { return value >= 0.0f ? value : -value; }
  59. /// Clamp a float to a range.
  60. inline float Clamp(float value, float min, float max)
  61. {
  62. if (value < min)
  63. return min;
  64. else if (value > max)
  65. return max;
  66. else
  67. return value;
  68. }
  69. /// Check whether two floating point values are equal within accuracy.
  70. inline bool Equals(float lhs, float rhs) { return lhs + M_EPSILON >= rhs && lhs - M_EPSILON <= rhs; }
  71. /// Return the smaller of two integers.
  72. inline int Min(int lhs, int rhs) { return lhs < rhs ? lhs : rhs; }
  73. /// Return the larger of two integers.
  74. inline int Max(int lhs, int rhs) { return lhs > rhs ? lhs : rhs; }
  75. /// Return absolute value of an integer
  76. inline int Abs(int value) { return value >= 0 ? value : -value; }
  77. /// Clamp an integer to a range.
  78. inline int Clamp(int value, int min, int 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 an unsigned integer is a power of two.
  88. inline bool IsPowerOfTwo(unsigned value)
  89. {
  90. if (!value)
  91. return true;
  92. while (!(value & 1))
  93. value >>= 1;
  94. return value == 1;
  95. }
  96. /// Round up to next power of two.
  97. inline unsigned NextPowerOfTwo(unsigned value)
  98. {
  99. unsigned ret = 1;
  100. while (ret < value && ret < 0x80000000)
  101. ret <<= 1;
  102. return ret;
  103. }
  104. /// Update a hash with the given 8-bit value using the SDBM algorithm.
  105. inline unsigned SDBMHash(unsigned hash, unsigned char c) { return c + (hash << 6) + (hash << 16) - hash; }
  106. /// Return a random float between 0.0 (inclusive) and 1.0 (exclusive.)
  107. inline float Random() { return Rand() / 32768.0f; }
  108. /// Return a random float between 0.0 and range, inclusive from both ends.
  109. inline float Random(float range) { return Rand() * range / 32767.0f; }
  110. /// Return a random integer between 0 and range - 1.
  111. inline int Random(int range) { return (Rand() * (range - 1) + 16384) / 32767; }
  112. }