IntpBilinear2.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 <array>
  10. // The interpolator is for uniformly spaced (x,y)-values. The input samples
  11. // F must be stored in row-major order to represent f(x,y); that is,
  12. // F[c + xBound*r] corresponds to f(x,y), where c is the index corresponding
  13. // to x and r is the index corresponding to y.
  14. namespace WwiseGTE
  15. {
  16. template <typename Real>
  17. class IntpBilinear2
  18. {
  19. public:
  20. // Construction.
  21. IntpBilinear2(int xBound, int yBound, Real xMin, Real xSpacing,
  22. Real yMin, Real ySpacing, Real const* F)
  23. :
  24. mXBound(xBound),
  25. mYBound(yBound),
  26. mQuantity(xBound* yBound),
  27. mXMin(xMin),
  28. mXSpacing(xSpacing),
  29. mYMin(yMin),
  30. mYSpacing(ySpacing),
  31. mF(F)
  32. {
  33. // At least a 3x3 block of data points are needed to construct the
  34. // estimates of the boundary derivatives.
  35. LogAssert(mXBound >= 2 && mYBound >= 2 && mF != nullptr, "Invalid input.");
  36. LogAssert(mXSpacing > (Real)0 && mYSpacing > (Real)0, "Invalid input.");
  37. mXMax = mXMin + mXSpacing * static_cast<Real>(mXBound - 1);
  38. mInvXSpacing = (Real)1 / mXSpacing;
  39. mYMax = mYMin + mYSpacing * static_cast<Real>(mYBound - 1);
  40. mInvYSpacing = (Real)1 / mYSpacing;
  41. mBlend[0][0] = (Real)1;
  42. mBlend[0][1] = (Real)-1;
  43. mBlend[1][0] = (Real)0;
  44. mBlend[1][1] = (Real)1;
  45. }
  46. // Member access.
  47. inline int GetXBound() const
  48. {
  49. return mXBound;
  50. }
  51. inline int GetYBound() const
  52. {
  53. return mYBound;
  54. }
  55. inline int GetQuantity() const
  56. {
  57. return mQuantity;
  58. }
  59. inline Real const* GetF() const
  60. {
  61. return mF;
  62. }
  63. inline Real GetXMin() const
  64. {
  65. return mXMin;
  66. }
  67. inline Real GetXMax() const
  68. {
  69. return mXMax;
  70. }
  71. inline Real GetXSpacing() const
  72. {
  73. return mXSpacing;
  74. }
  75. inline Real GetYMin() const
  76. {
  77. return mYMin;
  78. }
  79. inline Real GetYMax() const
  80. {
  81. return mYMax;
  82. }
  83. inline Real GetYSpacing() const
  84. {
  85. return mYSpacing;
  86. }
  87. // Evaluate the function and its derivatives. The functions clamp the
  88. // inputs to xmin <= x <= xmax and ymin <= y <= ymax. The first
  89. // operator is for function evaluation. The second operator is for
  90. // function or derivative evaluations. The xOrder argument is the
  91. // order of the x-derivative and the yOrder argument is the order of
  92. // the y-derivative. Both orders are zero to get the function value
  93. // itself.
  94. Real operator()(Real x, Real y) const
  95. {
  96. // Compute x-index and clamp to image.
  97. Real xIndex = (x - mXMin) * mInvXSpacing;
  98. int ix = static_cast<int>(xIndex);
  99. if (ix < 0)
  100. {
  101. ix = 0;
  102. }
  103. else if (ix >= mXBound)
  104. {
  105. ix = mXBound - 1;
  106. }
  107. // Compute y-index and clamp to image.
  108. Real yIndex = (y - mYMin) * mInvYSpacing;
  109. int iy = static_cast<int>(yIndex);
  110. if (iy < 0)
  111. {
  112. iy = 0;
  113. }
  114. else if (iy >= mYBound)
  115. {
  116. iy = mYBound - 1;
  117. }
  118. std::array<Real, 2> U;
  119. U[0] = (Real)1;
  120. U[1] = xIndex - ix;
  121. std::array<Real, 2> V;
  122. V[0] = (Real)1;
  123. V[1] = yIndex - iy;
  124. // Compute P = M*U and Q = M*V.
  125. std::array<Real, 2> P, Q;
  126. for (int row = 0; row < 2; ++row)
  127. {
  128. P[row] = (Real)0;
  129. Q[row] = (Real)0;
  130. for (int col = 0; col < 2; ++col)
  131. {
  132. P[row] += mBlend[row][col] * U[col];
  133. Q[row] += mBlend[row][col] * V[col];
  134. }
  135. }
  136. // Compute (M*U)^t D (M*V) where D is the 2x2 subimage
  137. // containing (x,y).
  138. Real result = (Real)0;
  139. for (int row = 0; row < 2; ++row)
  140. {
  141. int yClamp = iy + row;
  142. if (yClamp >= mYBound)
  143. {
  144. yClamp = mYBound - 1;
  145. }
  146. for (int col = 0; col < 2; ++col)
  147. {
  148. int xClamp = ix + col;
  149. if (xClamp >= mXBound)
  150. {
  151. xClamp = mXBound - 1;
  152. }
  153. result += P[col] * Q[row] * mF[xClamp + mXBound * yClamp];
  154. }
  155. }
  156. return result;
  157. }
  158. Real operator()(int xOrder, int yOrder, Real x, Real y) const
  159. {
  160. // Compute x-index and clamp to image.
  161. Real xIndex = (x - mXMin) * mInvXSpacing;
  162. int ix = static_cast<int>(xIndex);
  163. if (ix < 0)
  164. {
  165. ix = 0;
  166. }
  167. else if (ix >= mXBound)
  168. {
  169. ix = mXBound - 1;
  170. }
  171. // Compute y-index and clamp to image.
  172. Real yIndex = (y - mYMin) * mInvYSpacing;
  173. int iy = static_cast<int>(yIndex);
  174. if (iy < 0)
  175. {
  176. iy = 0;
  177. }
  178. else if (iy >= mYBound)
  179. {
  180. iy = mYBound - 1;
  181. }
  182. std::array<Real, 2> U;
  183. Real dx, xMult;
  184. switch (xOrder)
  185. {
  186. case 0:
  187. dx = xIndex - ix;
  188. U[0] = (Real)1;
  189. U[1] = dx;
  190. xMult = (Real)1;
  191. break;
  192. case 1:
  193. dx = xIndex - ix;
  194. U[0] = (Real)0;
  195. U[1] = (Real)1;
  196. xMult = mInvXSpacing;
  197. break;
  198. default:
  199. return (Real)0;
  200. }
  201. std::array<Real, 2> V;
  202. Real dy, yMult;
  203. switch (yOrder)
  204. {
  205. case 0:
  206. dy = yIndex - iy;
  207. V[0] = (Real)1;
  208. V[1] = dy;
  209. yMult = (Real)1;
  210. break;
  211. case 1:
  212. dy = yIndex - iy;
  213. V[0] = (Real)0;
  214. V[1] = (Real)1;
  215. yMult = mInvYSpacing;
  216. break;
  217. default:
  218. return (Real)0;
  219. }
  220. // Compute P = M*U and Q = M*V.
  221. std::array<Real, 2> P, Q;
  222. for (int row = 0; row < 2; ++row)
  223. {
  224. P[row] = (Real)0;
  225. Q[row] = (Real)0;
  226. for (int col = 0; col < 2; ++col)
  227. {
  228. P[row] += mBlend[row][col] * U[col];
  229. Q[row] += mBlend[row][col] * V[col];
  230. }
  231. }
  232. // Compute (M*U)^t D (M*V) where D is the 2x2 subimage containing (x,y).
  233. Real result = (Real)0;
  234. for (int row = 0; row < 2; ++row)
  235. {
  236. int yClamp = iy + row;
  237. if (yClamp >= mYBound)
  238. {
  239. yClamp = mYBound - 1;
  240. }
  241. for (int col = 0; col < 2; ++col)
  242. {
  243. int xClamp = ix + col;
  244. if (xClamp >= mXBound)
  245. {
  246. xClamp = mXBound - 1;
  247. }
  248. result += P[col] * Q[row] * mF[xClamp + mXBound * yClamp];
  249. }
  250. }
  251. result *= xMult * yMult;
  252. return result;
  253. }
  254. private:
  255. int mXBound, mYBound, mQuantity;
  256. Real mXMin, mXMax, mXSpacing, mInvXSpacing;
  257. Real mYMin, mYMax, mYSpacing, mInvYSpacing;
  258. Real const* mF;
  259. std::array<std::array<Real, 2>, 2> mBlend;
  260. };
  261. }