interpolation.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma once
  2. namespace nall {
  3. struct Interpolation {
  4. static inline auto Nearest(double mu, double a, double b, double c, double d) -> double {
  5. return (mu <= 0.5 ? b : c);
  6. }
  7. static inline auto Sublinear(double mu, double a, double b, double c, double d) -> double {
  8. mu = ((mu - 0.5) * 2.0) + 0.5;
  9. if(mu < 0) mu = 0;
  10. if(mu > 1) mu = 1;
  11. return b * (1.0 - mu) + c * mu;
  12. }
  13. static inline auto Linear(double mu, double a, double b, double c, double d) -> double {
  14. return b * (1.0 - mu) + c * mu;
  15. }
  16. static inline auto Cosine(double mu, double a, double b, double c, double d) -> double {
  17. mu = (1.0 - cos(mu * Math::Pi)) / 2.0;
  18. return b * (1.0 - mu) + c * mu;
  19. }
  20. static inline auto Cubic(double mu, double a, double b, double c, double d) -> double {
  21. double A = d - c - a + b;
  22. double B = a - b - A;
  23. double C = c - a;
  24. double D = b;
  25. return A * (mu * mu * mu) + B * (mu * mu) + C * mu + D;
  26. }
  27. static inline auto Hermite(double mu1, double a, double b, double c, double d) -> double {
  28. const double tension = 0.0; //-1 = low, 0 = normal, +1 = high
  29. const double bias = 0.0; //-1 = left, 0 = even, +1 = right
  30. double mu2, mu3, m0, m1, a0, a1, a2, a3;
  31. mu2 = mu1 * mu1;
  32. mu3 = mu2 * mu1;
  33. m0 = (b - a) * (1.0 + bias) * (1.0 - tension) / 2.0;
  34. m0 += (c - b) * (1.0 - bias) * (1.0 - tension) / 2.0;
  35. m1 = (c - b) * (1.0 + bias) * (1.0 - tension) / 2.0;
  36. m1 += (d - c) * (1.0 - bias) * (1.0 - tension) / 2.0;
  37. a0 = +2 * mu3 - 3 * mu2 + 1;
  38. a1 = mu3 - 2 * mu2 + mu1;
  39. a2 = mu3 - mu2;
  40. a3 = -2 * mu3 + 3 * mu2;
  41. return (a0 * b) + (a1 * m0) + (a2 * m1) + (a3 * c);
  42. }
  43. };
  44. }