💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.4 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_MATH_INTERPOLATION_HPP
  20. #define ANTKEEPER_MATH_INTERPOLATION_HPP
  21. #include "math/constants.hpp"
  22. #include "math/matrix-type.hpp"
  23. #include "math/quaternion-type.hpp"
  24. #include "math/transform-type.hpp"
  25. #include <cmath>
  26. #include <type_traits>
  27. namespace math {
  28. /**
  29. * Linearly interpolates between @p x and @p y.
  30. *
  31. * Requires the following operators to be defined:
  32. *
  33. * T operator+(const T&, const T&);
  34. * T operator-(const T&, const T&);
  35. * T operator*(const T&, S);
  36. *
  37. * @tparam T Value type.
  38. * @tparam S Scalar type.
  39. */
  40. template <typename T, typename S = float>
  41. T lerp(const T& x, const T& y, S a);
  42. /**
  43. * Linearly interpolates between two angles, @p x and @p y.
  44. *
  45. * @tparam T Value type.
  46. * @param x Start angle, in radians.
  47. * @param y End angle, in radians.
  48. * @param a Interpolation ratio.
  49. * @return Interpolated angle, in radians.
  50. */
  51. template <typename T>
  52. T lerp_angle(T x, T y, T a);
  53. /**
  54. * Logarithmically interpolates between @p x and @p y.
  55. *
  56. * @warning Undefined behavior when @p x is zero.
  57. *
  58. * @tparam T Value type.
  59. * @tparam S Scalar type.
  60. */
  61. template <typename T, typename S = float>
  62. T log_lerp(const T& x, const T& y, S a);
  63. template <typename T, typename S>
  64. inline T lerp(const T& x, const T& y, S a)
  65. {
  66. return x + (y - x) * a;
  67. }
  68. template <typename T>
  69. T lerp_angle(T x, T y, T a)
  70. {
  71. T shortest_angle = std::remainder((y - x), two_pi<T>);
  72. return std::remainder(x + shortest_angle * a, two_pi<T>);
  73. }
  74. template <typename T, typename S>
  75. inline T log_lerp(const T& x, const T& y, S a)
  76. {
  77. //return std::exp(linear(std::log(x), std::log(y), a));
  78. return x * std::pow(y / x, a);
  79. }
  80. } // namespace math
  81. #endif // ANTKEEPER_MATH_INTERPOLATION_HPP