Math.h 1.6 KB

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