MathDefs.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "Random.h"
  25. #include <cstdlib>
  26. #include <cmath>
  27. #ifndef M_PI
  28. static const float M_PI = 3.141592653589793238462643f;
  29. #endif
  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_RADTODEG = 1.0f / M_DEGTORAD;
  42. /// Intersection test result.
  43. enum Intersection
  44. {
  45. OUTSIDE,
  46. INTERSECTS,
  47. INSIDE
  48. };
  49. /// Linear interpolation between two float values.
  50. inline float Lerp(float lhs, float rhs, float t) { return lhs * (1.0f - t) + rhs * t; }
  51. /// Return the smaller of two floats.
  52. inline float Min(float lhs, float rhs) { return lhs < rhs ? lhs : rhs; }
  53. /// Return the larger of two floats.
  54. inline float Max(float lhs, float rhs) { return lhs > rhs ? lhs : rhs; }
  55. /// Return absolute value of a float.
  56. inline float Abs(float value) { return value >= 0.0f ? value : -value; }
  57. /// Clamp a float to a range.
  58. inline float Clamp(float value, float min, float max)
  59. {
  60. if (value < min)
  61. return min;
  62. else if (value > max)
  63. return max;
  64. else
  65. return value;
  66. }
  67. /// Check whether two floating point values are equal within accuracy.
  68. inline bool Equals(float lhs, float rhs) { return lhs + M_EPSILON >= rhs && lhs - M_EPSILON <= rhs; }
  69. /// Return the smaller of two integers.
  70. inline int Min(int lhs, int rhs) { return lhs < rhs ? lhs : rhs; }
  71. /// Return the larger of two integers.
  72. inline int Max(int lhs, int rhs) { return lhs > rhs ? lhs : rhs; }
  73. /// Return absolute value of an integer
  74. inline int Abs(int value) { return value >= 0 ? value : -value; }
  75. /// Clamp an integer to a range.
  76. inline int Clamp(int value, int min, int max)
  77. {
  78. if (value < min)
  79. return min;
  80. else if (value > max)
  81. return max;
  82. else
  83. return value;
  84. }
  85. /// Check whether an unsigned integer is a power of two.
  86. inline bool IsPowerOfTwo(unsigned value)
  87. {
  88. if (!value)
  89. return true;
  90. while (!(value & 1))
  91. value >>= 1;
  92. return value == 1;
  93. }
  94. /// Round up to next power of two.
  95. inline unsigned NextPowerOfTwo(unsigned value)
  96. {
  97. unsigned ret = 1;
  98. while (ret < value && ret < 0x80000000)
  99. ret <<= 1;
  100. return ret;
  101. }
  102. /// Update a hash with the given 8-bit value using the SDBM algorithm.
  103. inline unsigned SDBMHash(unsigned hash, unsigned char c) { return c + (hash << 6) + (hash << 16) - hash; }
  104. /// Return a random float between 0.0 (inclusive) and 1.0 (exclusive.)
  105. inline float Random() { return Rand() / 32768.0f; }
  106. /// Return a random float between 0.0 and range, inclusive from both ends.
  107. inline float Random(float range) { return Rand() * range / 32767.0f; }
  108. /// Return a random integer between 0 and range - 1.
  109. inline int Random(int range) { return (Rand() * (range - 1) + 16384) / 32767; }