IntpAkima1.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/Logger.h>
  9. #include <Mathematics/Math.h>
  10. #include <algorithm>
  11. #include <array>
  12. #include <vector>
  13. namespace WwiseGTE
  14. {
  15. template <typename Real>
  16. class IntpAkima1
  17. {
  18. protected:
  19. // Construction (abstract base class).
  20. IntpAkima1(int quantity, Real const* F)
  21. :
  22. mQuantity(quantity),
  23. mF(F)
  24. {
  25. // At least three data points are needed to construct the
  26. // estimates of the boundary derivatives.
  27. LogAssert(mQuantity >= 3, "Invalid input to IntpAkima1 constructor.");
  28. mPoly.resize(mQuantity - 1);
  29. }
  30. public:
  31. // Abstract base class.
  32. virtual ~IntpAkima1() = default;
  33. // Member access.
  34. inline int GetQuantity() const
  35. {
  36. return mQuantity;
  37. }
  38. inline Real const* GetF() const
  39. {
  40. return mF;
  41. }
  42. virtual Real GetXMin() const = 0;
  43. virtual Real GetXMax() const = 0;
  44. // Evaluate the function and its derivatives. The functions clamp the
  45. // inputs to xmin <= x <= xmax. The first operator is for function
  46. // evaluation. The second operator is for function or derivative
  47. // evaluations. The 'order' argument is the order of the derivative
  48. // or zero for the function itself.
  49. Real operator()(Real x) const
  50. {
  51. x = std::min(std::max(x, GetXMin()), GetXMax());
  52. int index;
  53. Real dx;
  54. Lookup(x, index, dx);
  55. return mPoly[index](dx);
  56. }
  57. Real operator()(int order, Real x) const
  58. {
  59. x = std::min(std::max(x, GetXMin()), GetXMax());
  60. int index;
  61. Real dx;
  62. Lookup(x, index, dx);
  63. return mPoly[index](order, dx);
  64. }
  65. protected:
  66. class Polynomial
  67. {
  68. public:
  69. // P(x) = c[0] + c[1]*x + c[2]*x^2 + c[3]*x^3
  70. inline Real& operator[](int i)
  71. {
  72. return mCoeff[i];
  73. }
  74. Real operator()(Real x) const
  75. {
  76. return mCoeff[0] + x * (mCoeff[1] + x * (mCoeff[2] + x * mCoeff[3]));
  77. }
  78. Real operator()(int order, Real x) const
  79. {
  80. switch (order)
  81. {
  82. case 0:
  83. return mCoeff[0] + x * (mCoeff[1] + x * (mCoeff[2] + x * mCoeff[3]));
  84. case 1:
  85. return mCoeff[1] + x * ((Real)2 * mCoeff[2] + x * (Real)3 * mCoeff[3]);
  86. case 2:
  87. return (Real)2 * mCoeff[2] + x * (Real)6 * mCoeff[3];
  88. case 3:
  89. return (Real)6 * mCoeff[3];
  90. }
  91. return (Real)0;
  92. }
  93. private:
  94. std::array<Real, 4> mCoeff;
  95. };
  96. Real ComputeDerivative(Real* slope) const
  97. {
  98. if (slope[1] != slope[2])
  99. {
  100. if (slope[0] != slope[1])
  101. {
  102. if (slope[2] != slope[3])
  103. {
  104. Real ad0 = std::fabs(slope[3] - slope[2]);
  105. Real ad1 = std::fabs(slope[0] - slope[1]);
  106. return (ad0 * slope[1] + ad1 * slope[2]) / (ad0 + ad1);
  107. }
  108. else
  109. {
  110. return slope[2];
  111. }
  112. }
  113. else
  114. {
  115. if (slope[2] != slope[3])
  116. {
  117. return slope[1];
  118. }
  119. else
  120. {
  121. return ((Real)0.5)* (slope[1] + slope[2]);
  122. }
  123. }
  124. }
  125. else
  126. {
  127. return slope[1];
  128. }
  129. }
  130. virtual void Lookup(Real x, int& index, Real& dx) const = 0;
  131. int mQuantity;
  132. Real const* mF;
  133. std::vector<Polynomial> mPoly;
  134. };
  135. }