Math.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef CORE_MATH_HPP
  2. #define CORE_MATH_HPP
  3. #include "utils/Meta.hpp"
  4. namespace Core::Math {
  5. template<typename T>
  6. T interpolate(const T& a, const T& b, float f) {
  7. return a * (1.0f - f) + b * f;
  8. }
  9. constexpr bool isPowerOf2(int i) {
  10. return (i & (i - 1)) == 0;
  11. }
  12. constexpr int roundUpLog2(int i) {
  13. if(i <= 0) {
  14. return 0;
  15. }
  16. int c = 1;
  17. while(((i - 1) >> c) > 0) {
  18. c++;
  19. }
  20. return c;
  21. }
  22. template<typename T>
  23. constexpr const T& min(const T& t) {
  24. return t;
  25. }
  26. template<typename T, typename... Args>
  27. constexpr const T& min(const T& t, Args&&... args) {
  28. const T& o = min(Core::forward<Args>(args)...);
  29. return t < o ? t : o;
  30. }
  31. template<typename T>
  32. constexpr const T& max(const T& t) {
  33. return t;
  34. }
  35. template<typename T, typename... Args>
  36. constexpr const T& max(const T& t, Args&&... args) {
  37. const T& o = max(Core::forward<Args>(args)...);
  38. return o < t ? t : o;
  39. }
  40. template<typename T>
  41. constexpr const T& clamp(const T& t, const T& borderA, const T& borderB) {
  42. const T& low = min(borderA, borderB);
  43. const T& high = max(borderA, borderB);
  44. return max(low, min(high, t));
  45. }
  46. constexpr float PI = 3.14159265358979323846f;
  47. float sin(float f);
  48. float cos(float f);
  49. float tan(float f);
  50. void sinCos(float a, float& s, float& c);
  51. constexpr float radianToDegree(float radians) {
  52. return radians * (180.0f / PI);
  53. }
  54. constexpr float degreeToRadian(float degrees) {
  55. return degrees * (PI / 180.0f);
  56. }
  57. float squareRoot(float f);
  58. }
  59. #endif