MathDefs.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "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. /// Clamp a float to a range.
  56. inline float Clamp(float value, float min, float max)
  57. {
  58. if (value < min)
  59. return min;
  60. else if (value > max)
  61. return max;
  62. else
  63. return value;
  64. }
  65. /// Check whether two floating point values are equal within accuracy.
  66. inline bool Equals(float lhs, float rhs) { return lhs + M_EPSILON >= rhs && lhs - M_EPSILON <= rhs; }
  67. /// Return the smaller of two integers.
  68. inline int Min(int lhs, int rhs) { return lhs < rhs ? lhs : rhs; }
  69. /// Return the larger of two integers.
  70. inline int Max(int lhs, int rhs) { return lhs > rhs ? lhs : rhs; }
  71. /// Clamp an integer to a range.
  72. inline int Clamp(int value, int min, int max)
  73. {
  74. if (value < min)
  75. return min;
  76. else if (value > max)
  77. return max;
  78. else
  79. return value;
  80. }
  81. /// Check whether an unsigned integer is a power of two.
  82. inline bool IsPowerOfTwo(unsigned value)
  83. {
  84. if (!value)
  85. return true;
  86. while (!(value & 1))
  87. value >>= 1;
  88. return value == 1;
  89. }
  90. /// Round up to next power of two.
  91. inline unsigned NextPowerOfTwo(unsigned value)
  92. {
  93. unsigned ret = 1;
  94. while (ret < value)
  95. ret <<= 1;
  96. return ret;
  97. }
  98. /// Update a hash with the given 8-bit value using the SDBM algorithm.
  99. inline unsigned SDBMHash(unsigned hash, unsigned char c) { return c + (hash << 6) + (hash << 16) - hash; }
  100. /// Return a random float between 0.0 (inclusive) and 1.0 (exclusive.)
  101. inline float Random() { return (Rand() & 32767) / 32768.0f; }
  102. /// Return a random float between 0.0 and range, inclusive.
  103. inline float Random(float range) { return (Rand() & 32767) * range / 32767.0f; }
  104. /// Return a random integer between 0 and range, inclusive.
  105. inline int Random(int range) { return ((Rand() & 32767) * range + 16384) / 32767; }