math.h - Engine C API Reference

math.h
  1. #pragma once
  2. #include "platform.h"
  3. #include <math.h>
  4. #include <limits>
  5. #include <float.h>
  6. namespace stingray_plugin_foundation {
  7. // Common math functions.
  8. namespace math {
  9. const float pi = 3.141592654f;
  10. __forceinline bool is_pow2(unsigned);
  11. __forceinline float square_root(float);
  12. __forceinline float degrees_to_radians(float);
  13. __forceinline float radians_to_degrees(float f);
  14. #undef max
  15. template <class T> __forceinline T max(T a, T b);
  16. template <class T> __forceinline T max3(T a, T b, T c);
  17. #undef min
  18. template <class T> __forceinline T min(T a, T b);
  19. template <class T> __forceinline T min3(T a, T b, T c);
  20. template <class T> __forceinline T clamp(T x, T min, T max);
  21. template <class T> __forceinline T abs(T x);
  22. template <class T> __forceinline T sign(T x);
  23. unsigned branchless_max(unsigned a, unsigned b);
  24. unsigned branchless_min(unsigned a, unsigned b);
  25. unsigned div_ceil(unsigned a, unsigned b);
  26. unsigned div_round(unsigned a, unsigned b);
  27. __forceinline unsigned round_down_to_align(unsigned x, unsigned align) {return x - x%align;}
  28. __forceinline unsigned round_up_to_align(unsigned x, unsigned align) {return round_down_to_align(x+align-1, align);}
  29. __forceinline bool valid(const float *fa, unsigned n)
  30. {
  31. for (unsigned i=0; i<n; ++i) {
  32. float f = fa[i];
  33. if (f != f || f >= FLT_MAX || f <= -FLT_MAX)
  34. return false;
  35. }
  36. return true;
  37. }
  38. __forceinline unsigned log2(unsigned a)
  39. {
  40. unsigned log2 = 0;
  41. while(a >>= 1) log2++;
  42. return log2;
  43. }
  44. }
  45. __forceinline float lerp(float a, float b, float t);
  46. // Finds the maximum value and its source from a number of tested
  47. // values.
  48. template <class SOURCE>
  49. class FindMaximum
  50. {
  51. public:
  52. // Initiates the search for the maximum value.
  53. // * `start` The start value of the search.
  54. FindMaximum(float start = -std::numeric_limits<float>::max()) : _value(start) {}
  55. // Tests if value is the biggest value encountered so far.
  56. // If it is, the value and the source are recorded and the
  57. // function returns true, otherwise it returns false.
  58. bool test(float value, const SOURCE &source) {
  59. if (value > _value) {
  60. _value = value;
  61. _source = source;
  62. return true;
  63. } else
  64. return false;
  65. }
  66. // Returns true if any value bigger than the start value was
  67. // found.
  68. bool found(float start = -std::numeric_limits<float>::max()) {return _value > start;}
  69. // Returns the maximum value found.
  70. const float &value() {return _value;}
  71. // Returns the source of the maximum value found
  72. const SOURCE &source() {return _source;}
  73. private:
  74. float _value;
  75. SOURCE _source;
  76. };
  77. // Finds the minimum value and its source from a number of tested
  78. // values.
  79. template <class SOURCE>
  80. class FindMinimum
  81. {
  82. public:
  83. // Initiates the search for the minimum avlue.
  84. // * `start` The start value of the search
  85. FindMinimum(float start = std::numeric_limits<float>::max()) : _value(start) {}
  86. // Tests if value is the smallest value encountered so far.
  87. // If it is, the value and the source are recorded and the
  88. // function returns true, otherwise it returns false.
  89. bool test(const float &value, const SOURCE &source) {
  90. if (value < _value) {
  91. _value = value;
  92. _source = source;
  93. return true;
  94. } else
  95. return false;
  96. }
  97. // Returns true if any value smaller than the start value was
  98. // found.
  99. bool found(float start = std::numeric_limits<float>::max()) {return _value < start;}
  100. // Returns the maximum value found.
  101. const float &value() {return _value;}
  102. // Returns the source of the maximum value found
  103. const SOURCE &source() {return _source;}
  104. private:
  105. float _value;
  106. SOURCE _source;
  107. };
  108. // Class that represents a cross fade.
  109. // A cross fade is a mix between an in fade and an out fade.
  110. class Crossfade
  111. {
  112. public:
  113. // Creates a new crossfade that is fully faded in.
  114. Crossfade() : _fade_in(1), _fade_in_t(FLT_MAX), _fade_out(0), _fade_out_t(FLT_MAX) {}
  115. // Creates a new crossfade that fades in over time `bt`.
  116. Crossfade(float bt) : _fade_in(0), _fade_in_t(bt), _fade_out(0), _fade_out_t(FLT_MAX) {}
  117. // Fades out the current crossfade over time bt.
  118. void fade_out(float bt) {
  119. if (bt < _fade_out_t)
  120. _fade_out_t = bt;
  121. if (_fade_in_t != FLT_MAX && bt < _fade_in_t)
  122. _fade_in_t = bt;
  123. }
  124. // Updates this crossfade
  125. void update(float dt);
  126. // Returns true if the crossfade is done
  127. bool is_done() {return _fade_out == 1;}
  128. // Returns the current blend value for the crossfade
  129. float blend() {return _fade_in - _fade_out;}
  130. private:
  131. float _fade_in, _fade_in_t;
  132. float _fade_out, _fade_out_t;
  133. };
  134. }
  135. #include "math.inl"