Math.cppm 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. export module Core.Math;
  2. import Core.Meta;
  3. export namespace Core {
  4. template<typename T>
  5. T interpolate(const T& a, const T& b, float f) noexcept {
  6. return a * (1.0f - f) + b * f;
  7. }
  8. constexpr bool isPowerOf2(int i) noexcept {
  9. return (i & (i - 1)) == 0;
  10. }
  11. template<typename T>
  12. constexpr T roundUpLog2(T i) noexcept {
  13. if(i <= 0) {
  14. return 0;
  15. }
  16. T 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) noexcept {
  24. return t;
  25. }
  26. template<typename T, typename... Args>
  27. constexpr const T& min(const T& t, Args&&... args) noexcept {
  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) noexcept {
  33. return t;
  34. }
  35. template<typename T, typename... Args>
  36. constexpr const T& max(const T& t, Args&&... args) noexcept {
  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(
  42. const T& t, const T& borderA, const T& borderB) noexcept {
  43. const T& low = min(borderA, borderB);
  44. const T& high = max(borderA, borderB);
  45. return max(low, min(high, t));
  46. }
  47. inline constexpr float PI = 3.14159265358979323846f;
  48. template<typename T>
  49. constexpr T radianToDegree(const T& radians) noexcept {
  50. return radians * static_cast<T>(180.0f / PI);
  51. }
  52. template<typename T>
  53. constexpr T degreeToRadian(const T& radians) noexcept {
  54. return radians * static_cast<T>(PI / 180.0f);
  55. }
  56. }