Math.hpp 1.7 KB

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