MathDefs.pkg 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. $#include "MathDefs.h"
  2. static const float M_PI;
  3. static const int M_MIN_INT;
  4. static const int M_MAX_INT;
  5. static const unsigned M_MIN_UNSIGNED;
  6. static const unsigned M_MAX_UNSIGNED;
  7. static const float M_EPSILON;
  8. static const float M_LARGE_EPSILON;
  9. static const float M_MIN_NEARCLIP;
  10. static const float M_MAX_FOV;
  11. static const float M_LARGE_VALUE;
  12. static const float M_INFINITY;
  13. static const float M_DEGTORAD;
  14. static const float M_DEGTORAD_2;
  15. static const float M_RADTODEG;
  16. /// Intersection test result.
  17. enum Intersection
  18. {
  19. OUTSIDE,
  20. INTERSECTS,
  21. INSIDE
  22. };
  23. /// Linear interpolation between two float values.
  24. inline float Lerp(float lhs, float rhs, float t) { return lhs * (1.0f - t) + rhs * t; }
  25. /// Return the smaller of two floats.
  26. inline float Min(float lhs, float rhs) { return lhs < rhs ? lhs : rhs; }
  27. /// Return the larger of two floats.
  28. inline float Max(float lhs, float rhs) { return lhs > rhs ? lhs : rhs; }
  29. /// Return absolute value of a float.
  30. inline float Abs(float value) { return value >= 0.0f ? value : -value; }
  31. /// Clamp a float to a range.
  32. inline float Clamp(float value, float min, float max)
  33. {
  34. if (value < min)
  35. return min;
  36. else if (value > max)
  37. return max;
  38. else
  39. return value;
  40. }
  41. /// Check whether two floating point values are equal within accuracy.
  42. inline bool Equals(float lhs, float rhs) { return lhs + M_EPSILON >= rhs && lhs - M_EPSILON <= rhs; }
  43. /// Return the smaller of two integers.
  44. inline int Min(int lhs, int rhs) { return lhs < rhs ? lhs : rhs; }
  45. /// Return the larger of two integers.
  46. inline int Max(int lhs, int rhs) { return lhs > rhs ? lhs : rhs; }
  47. /// Return absolute value of an integer
  48. inline int Abs(int value) { return value >= 0 ? value : -value; }
  49. /// Clamp an integer to a range.
  50. inline int Clamp(int value, int min, int max)
  51. {
  52. if (value < min)
  53. return min;
  54. else if (value > max)
  55. return max;
  56. else
  57. return value;
  58. }
  59. /// Check whether an unsigned integer is a power of two.
  60. inline bool IsPowerOfTwo(unsigned value)
  61. {
  62. if (!value)
  63. return true;
  64. while (!(value & 1))
  65. value >>= 1;
  66. return value == 1;
  67. }
  68. /// Round up to next power of two.
  69. inline unsigned NextPowerOfTwo(unsigned value)
  70. {
  71. unsigned ret = 1;
  72. while (ret < value && ret < 0x80000000)
  73. ret <<= 1;
  74. return ret;
  75. }
  76. /// Update a hash with the given 8-bit value using the SDBM algorithm.
  77. inline unsigned SDBMHash(unsigned hash, unsigned char c) { return c + (hash << 6) + (hash << 16) - hash; }
  78. /// Return a random float between 0.0 (inclusive) and 1.0 (exclusive.)
  79. inline float Random() { return Rand() / 32768.0f; }
  80. /// Return a random float between 0.0 and range, inclusive from both ends.
  81. inline float Random(float range) { return Rand() * range / 32767.0f; }
  82. /// Return a random integer between 0 and range - 1.
  83. inline int Random @ RandomInt(int range) { return (Rand() * (range - 1) + 16384) / 32767; }