MathDefs.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #ifndef MATH_MATHDEFS_H
  24. #define MATH_MATHDEFS_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.0000001f;
  35. static const float M_MIN_NEARCLIP = 0.01f;
  36. static const float M_MAX_FOV = 160.0f;
  37. static const float M_LARGE_VALUE = 100000000.0f;
  38. static const float M_INFINITY = (float)HUGE_VAL;
  39. static const float M_DEGTORAD = (float)M_PI / 180.0f;
  40. static const float M_RADTODEG = 1.0f / M_DEGTORAD;
  41. //! Intersection test result
  42. enum Intersection
  43. {
  44. OUTSIDE,
  45. INTERSECTS,
  46. INSIDE
  47. };
  48. //! Linear interpolation between two float values
  49. inline float lerp(float lhs, float rhs, float t)
  50. {
  51. return lhs * (1.0f - t) + rhs * t;
  52. }
  53. //! Return a random float between 0.0 (inclusive) and 1.0 (exclusive)
  54. inline float random()
  55. {
  56. return (rand() & 32767) / 32768.0f;
  57. }
  58. //! Return a random float between 0.0 and range, inclusive
  59. inline float random(float range)
  60. {
  61. return (rand() & 32767) * range / 32767.0f;
  62. }
  63. //! Return a random integer between 0 and range, inclusive
  64. inline int random(int range)
  65. {
  66. return ((rand() & 32767) * range + 16384) / 32767;
  67. }
  68. //! Return the smaller of two floats
  69. inline float min(float lhs, float rhs)
  70. {
  71. return lhs < rhs ? lhs : rhs;
  72. }
  73. //! Return the larger of two floats
  74. inline float max(float lhs, float rhs)
  75. {
  76. return lhs > rhs ? lhs : rhs;
  77. }
  78. //! Clamp a float to a range
  79. inline float clamp(float value, float min, float max)
  80. {
  81. if (value < min)
  82. return min;
  83. if (value > max)
  84. return max;
  85. return value;
  86. }
  87. //! Return the smaller of two integers
  88. inline int min(int lhs, int rhs)
  89. {
  90. return lhs < rhs ? lhs : rhs;
  91. }
  92. //! Return the larger of two integers
  93. inline int max(int lhs, int rhs)
  94. {
  95. return lhs > rhs ? lhs : rhs;
  96. }
  97. //! Clamp an integer to a range
  98. inline int clamp(int value, int min, int max)
  99. {
  100. if (value < min)
  101. return min;
  102. if (value > max)
  103. return max;
  104. return value;
  105. }
  106. //! Check whether an unsigned integer is a power of two
  107. inline bool isPowerOfTwo(unsigned value)
  108. {
  109. if (!value)
  110. return true;
  111. while (!(value & 1))
  112. value >>= 1;
  113. return value == 1;
  114. }
  115. #endif // MATH_MATHDEFS_H