IntpAkimaUniform3.h 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  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 <Mathematics/Array3.h>
  11. #include <algorithm>
  12. #include <array>
  13. #include <cstring>
  14. // The interpolator is for uniformly spaced(x,y z)-values. The input samples
  15. // must be stored in lexicographical order to represent f(x,y,z); that is,
  16. // F[c + xBound*(r + yBound*s)] corresponds to f(x,y,z), where c is the index
  17. // corresponding to x, r is the index corresponding to y, and s is the index
  18. // corresponding to z.
  19. namespace WwiseGTE
  20. {
  21. template <typename Real>
  22. class IntpAkimaUniform3
  23. {
  24. public:
  25. // Construction and destruction.
  26. IntpAkimaUniform3(int xBound, int yBound, int zBound, Real xMin,
  27. Real xSpacing, Real yMin, Real ySpacing, Real zMin, Real zSpacing,
  28. Real const* F)
  29. :
  30. mXBound(xBound),
  31. mYBound(yBound),
  32. mZBound(zBound),
  33. mQuantity(xBound* yBound* zBound),
  34. mXMin(xMin),
  35. mXSpacing(xSpacing),
  36. mYMin(yMin),
  37. mYSpacing(ySpacing),
  38. mZMin(zMin),
  39. mZSpacing(zSpacing),
  40. mF(F),
  41. mPoly(xBound - 1, yBound - 1, zBound - 1)
  42. {
  43. // At least a 3x3x3 block of data points is needed to construct
  44. // the estimates of the boundary derivatives.
  45. LogAssert(mXBound >= 3 && mYBound >= 3 && mZBound >= 3 && mF != nullptr, "Invalid input.");
  46. LogAssert(mXSpacing > (Real)0 && mYSpacing > (Real)0 && mZSpacing > (Real)0, "Invalid input.");
  47. mXMax = mXMin + mXSpacing * static_cast<Real>(mXBound - 1);
  48. mYMax = mYMin + mYSpacing * static_cast<Real>(mYBound - 1);
  49. mZMax = mZMin + mZSpacing * static_cast<Real>(mZBound - 1);
  50. // Create a 3D wrapper for the 1D samples.
  51. Array3<Real> Fmap(mXBound, mYBound, mZBound, const_cast<Real*>(mF));
  52. // Construct first-order derivatives.
  53. Array3<Real> FX(mXBound, mYBound, mZBound);
  54. Array3<Real> FY(mXBound, mYBound, mZBound);
  55. Array3<Real> FZ(mXBound, mYBound, mZBound);
  56. GetFX(Fmap, FX);
  57. GetFX(Fmap, FY);
  58. GetFX(Fmap, FZ);
  59. // Construct second-order derivatives.
  60. Array3<Real> FXY(mXBound, mYBound, mZBound);
  61. Array3<Real> FXZ(mXBound, mYBound, mZBound);
  62. Array3<Real> FYZ(mXBound, mYBound, mZBound);
  63. GetFX(Fmap, FXY);
  64. GetFX(Fmap, FXZ);
  65. GetFX(Fmap, FYZ);
  66. // Construct third-order derivatives.
  67. Array3<Real> FXYZ(mXBound, mYBound, mZBound);
  68. GetFXYZ(Fmap, FXYZ);
  69. // Construct polynomials.
  70. GetPolynomials(Fmap, FX, FY, FZ, FXY, FXZ, FYZ, FXYZ);
  71. }
  72. ~IntpAkimaUniform3() = default;
  73. // Member access.
  74. inline int GetXBound() const
  75. {
  76. return mXBound;
  77. }
  78. inline int GetYBound() const
  79. {
  80. return mYBound;
  81. }
  82. inline int GetZBound() const
  83. {
  84. return mZBound;
  85. }
  86. inline int GetQuantity() const
  87. {
  88. return mQuantity;
  89. }
  90. inline Real const* GetF() const
  91. {
  92. return mF;
  93. }
  94. inline Real GetXMin() const
  95. {
  96. return mXMin;
  97. }
  98. inline Real GetXMax() const
  99. {
  100. return mXMax;
  101. }
  102. inline Real GetXSpacing() const
  103. {
  104. return mXSpacing;
  105. }
  106. inline Real GetYMin() const
  107. {
  108. return mYMin;
  109. }
  110. inline Real GetYMax() const
  111. {
  112. return mYMax;
  113. }
  114. inline Real GetYSpacing() const
  115. {
  116. return mYSpacing;
  117. }
  118. inline Real GetZMin() const
  119. {
  120. return mZMin;
  121. }
  122. inline Real GetZMax() const
  123. {
  124. return mZMax;
  125. }
  126. inline Real GetZSpacing() const
  127. {
  128. return mZSpacing;
  129. }
  130. // Evaluate the function and its derivatives. The functions clamp the
  131. // inputs to xmin <= x <= xmax, ymin <= y <= ymax and
  132. // zmin <= z <= zmax. The first operator is for function evaluation.
  133. // The second operator is for function or derivative evaluations. The
  134. // xOrder argument is the order of the x-derivative, the yOrder
  135. // argument is the order of the y-derivative, and the zOrder argument
  136. // is the order of the z-derivative. All orders are zero to get the
  137. // function value itself.
  138. Real operator()(Real x, Real y, Real z) const
  139. {
  140. x = std::min(std::max(x, mXMin), mXMax);
  141. y = std::min(std::max(y, mYMin), mYMax);
  142. z = std::min(std::max(z, mZMin), mZMax);
  143. int ix, iy, iz;
  144. Real dx, dy, dz;
  145. XLookup(x, ix, dx);
  146. YLookup(y, iy, dy);
  147. ZLookup(z, iz, dz);
  148. return mPoly[iz][iy][ix](dx, dy, dz);
  149. }
  150. Real operator()(int xOrder, int yOrder, int zOrder, Real x, Real y, Real z) const
  151. {
  152. x = std::min(std::max(x, mXMin), mXMax);
  153. y = std::min(std::max(y, mYMin), mYMax);
  154. z = std::min(std::max(z, mZMin), mZMax);
  155. int ix, iy, iz;
  156. Real dx, dy, dz;
  157. XLookup(x, ix, dx);
  158. YLookup(y, iy, dy);
  159. ZLookup(z, iz, dz);
  160. return mPoly[iz][iy][ix](xOrder, yOrder, zOrder, dx, dy, dz);
  161. }
  162. private:
  163. class Polynomial
  164. {
  165. public:
  166. Polynomial()
  167. {
  168. for (size_t ix = 0; ix < 4; ++ix)
  169. {
  170. for (size_t iy = 0; iy < 4; ++iy)
  171. {
  172. mCoeff[ix][iy].fill((Real)0);
  173. }
  174. }
  175. }
  176. // P(x,y,z) = sum_{i=0}^3 sum_{j=0}^3 sum_{k=0}^3 a_{ijk} x^i y^j z^k.
  177. // The tensor term A[ix][iy][iz] corresponds to the polynomial term
  178. // x^{ix} y^{iy} z^{iz}.
  179. Real& A(int ix, int iy, int iz)
  180. {
  181. return mCoeff[ix][iy][iz];
  182. }
  183. Real operator()(Real x, Real y, Real z) const
  184. {
  185. std::array<Real, 4> xPow = { (Real)1, x, x * x, x * x * x };
  186. std::array<Real, 4> yPow = { (Real)1, y, y * y, y * y * y };
  187. std::array<Real, 4> zPow = { (Real)1, z, z * z, z * z * z };
  188. Real p = (Real)0;
  189. for (size_t iz = 0; iz <= 3; ++iz)
  190. {
  191. for (size_t iy = 0; iy <= 3; ++iy)
  192. {
  193. for (size_t ix = 0; ix <= 3; ++ix)
  194. {
  195. p += mCoeff[ix][iy][iz] * xPow[ix] * yPow[iy] * zPow[iz];
  196. }
  197. }
  198. }
  199. return p;
  200. }
  201. Real operator()(int xOrder, int yOrder, int zOrder, Real x, Real y, Real z) const
  202. {
  203. std::array<Real, 4> xPow;
  204. switch (xOrder)
  205. {
  206. case 0:
  207. xPow[0] = (Real)1;
  208. xPow[1] = x;
  209. xPow[2] = x * x;
  210. xPow[3] = x * x * x;
  211. break;
  212. case 1:
  213. xPow[0] = (Real)0;
  214. xPow[1] = (Real)1;
  215. xPow[2] = (Real)2 * x;
  216. xPow[3] = (Real)3 * x * x;
  217. break;
  218. case 2:
  219. xPow[0] = (Real)0;
  220. xPow[1] = (Real)0;
  221. xPow[2] = (Real)2;
  222. xPow[3] = (Real)6 * x;
  223. break;
  224. case 3:
  225. xPow[0] = (Real)0;
  226. xPow[1] = (Real)0;
  227. xPow[2] = (Real)0;
  228. xPow[3] = (Real)6;
  229. break;
  230. default:
  231. return (Real)0;
  232. }
  233. std::array<Real, 4> yPow;
  234. switch (yOrder)
  235. {
  236. case 0:
  237. yPow[0] = (Real)1;
  238. yPow[1] = y;
  239. yPow[2] = y * y;
  240. yPow[3] = y * y * y;
  241. break;
  242. case 1:
  243. yPow[0] = (Real)0;
  244. yPow[1] = (Real)1;
  245. yPow[2] = (Real)2 * y;
  246. yPow[3] = (Real)3 * y * y;
  247. break;
  248. case 2:
  249. yPow[0] = (Real)0;
  250. yPow[1] = (Real)0;
  251. yPow[2] = (Real)2;
  252. yPow[3] = (Real)6 * y;
  253. break;
  254. case 3:
  255. yPow[0] = (Real)0;
  256. yPow[1] = (Real)0;
  257. yPow[2] = (Real)0;
  258. yPow[3] = (Real)6;
  259. break;
  260. default:
  261. return (Real)0;
  262. }
  263. std::array<Real, 4> zPow;
  264. switch (zOrder)
  265. {
  266. case 0:
  267. zPow[0] = (Real)1;
  268. zPow[1] = z;
  269. zPow[2] = z * z;
  270. zPow[3] = z * z * z;
  271. break;
  272. case 1:
  273. zPow[0] = (Real)0;
  274. zPow[1] = (Real)1;
  275. zPow[2] = (Real)2 * z;
  276. zPow[3] = (Real)3 * z * z;
  277. break;
  278. case 2:
  279. zPow[0] = (Real)0;
  280. zPow[1] = (Real)0;
  281. zPow[2] = (Real)2;
  282. zPow[3] = (Real)6 * z;
  283. break;
  284. case 3:
  285. zPow[0] = (Real)0;
  286. zPow[1] = (Real)0;
  287. zPow[2] = (Real)0;
  288. zPow[3] = (Real)6;
  289. break;
  290. default:
  291. return (Real)0;
  292. }
  293. Real p = (Real)0;
  294. for (size_t iz = 0; iz <= 3; ++iz)
  295. {
  296. for (size_t iy = 0; iy <= 3; ++iy)
  297. {
  298. for (size_t ix = 0; ix <= 3; ++ix)
  299. {
  300. p += mCoeff[ix][iy][iz] * xPow[ix] * yPow[iy] * zPow[iz];
  301. }
  302. }
  303. }
  304. return p;
  305. }
  306. private:
  307. std::array<std::array<std::array<Real, 4>, 4>, 4> mCoeff;
  308. };
  309. // Support for construction.
  310. void GetFX(Array3<Real> const& F, Array3<Real>& FX)
  311. {
  312. Array3<Real> slope(mXBound + 3, mYBound, mZBound);
  313. Real invDX = (Real)1 / mXSpacing;
  314. int ix, iy, iz;
  315. for (iz = 0; iz < mZBound; ++iz)
  316. {
  317. for (iy = 0; iy < mYBound; ++iy)
  318. {
  319. for (ix = 0; ix < mXBound - 1; ++ix)
  320. {
  321. slope[iz][iy][ix + 2] = (F[iz][iy][ix + 1] - F[iz][iy][ix]) * invDX;
  322. }
  323. slope[iz][iy][1] = (Real)2 * slope[iz][iy][2] - slope[iz][iy][3];
  324. slope[iz][iy][0] = (Real)2 * slope[iz][iy][1] - slope[iz][iy][2];
  325. slope[iz][iy][mXBound + 1] = (Real)2 * slope[iz][iy][mXBound] - slope[iz][iy][mXBound - 1];
  326. slope[iz][iy][mXBound + 2] = (Real)2 * slope[iz][iy][mXBound + 1] - slope[iz][iy][mXBound];
  327. }
  328. }
  329. for (iz = 0; iz < mZBound; ++iz)
  330. {
  331. for (iy = 0; iy < mYBound; ++iy)
  332. {
  333. for (ix = 0; ix < mXBound; ++ix)
  334. {
  335. FX[iz][iy][ix] = ComputeDerivative(slope[iz][iy] + ix);
  336. }
  337. }
  338. }
  339. }
  340. void GetFY(Array3<Real> const& F, Array3<Real>& FY)
  341. {
  342. Array3<Real> slope(mYBound + 3, mXBound, mZBound);
  343. Real invDY = (Real)1 / mYSpacing;
  344. int ix, iy, iz;
  345. for (iz = 0; iz < mZBound; ++iz)
  346. {
  347. for (ix = 0; ix < mXBound; ++ix)
  348. {
  349. for (iy = 0; iy < mYBound - 1; ++iy)
  350. {
  351. slope[iz][ix][iy + 2] = (F[iz][iy + 1][ix] - F[iz][iy][ix]) * invDY;
  352. }
  353. slope[iz][ix][1] = (Real)2 * slope[iz][ix][2] - slope[iz][ix][3];
  354. slope[iz][ix][0] = (Real)2 * slope[iz][ix][1] - slope[iz][ix][2];
  355. slope[iz][ix][mYBound + 1] = (Real)2 * slope[iz][ix][mYBound] - slope[iz][ix][mYBound - 1];
  356. slope[iz][ix][mYBound + 2] = (Real)2 * slope[iz][ix][mYBound + 1] - slope[iz][ix][mYBound];
  357. }
  358. }
  359. for (iz = 0; iz < mZBound; ++iz)
  360. {
  361. for (ix = 0; ix < mXBound; ++ix)
  362. {
  363. for (iy = 0; iy < mYBound; ++iy)
  364. {
  365. FY[iz][iy][ix] = ComputeDerivative(slope[iz][ix] + iy);
  366. }
  367. }
  368. }
  369. }
  370. void GetFZ(Array3<Real> const& F, Array3<Real>& FZ)
  371. {
  372. Array3<Real> slope(mZBound + 3, mXBound, mYBound);
  373. Real invDZ = (Real)1 / mZSpacing;
  374. int ix, iy, iz;
  375. for (iy = 0; iy < mYBound; ++iy)
  376. {
  377. for (ix = 0; ix < mXBound; ++ix)
  378. {
  379. for (iz = 0; iz < mZBound - 1; ++iz)
  380. {
  381. slope[iy][ix][iz + 2] = (F[iz + 1][iy][ix] - F[iz][iy][ix]) * invDZ;
  382. }
  383. slope[iy][ix][1] = (Real)2 * slope[iy][ix][2] - slope[iy][ix][3];
  384. slope[iy][ix][0] = (Real)2 * slope[iy][ix][1] - slope[iy][ix][2];
  385. slope[iy][ix][mZBound + 1] = (Real)2 * slope[iy][ix][mZBound] - slope[iy][ix][mZBound - 1];
  386. slope[iy][ix][mZBound + 2] = (Real)2 * slope[iy][ix][mZBound + 1] - slope[iy][ix][mZBound];
  387. }
  388. }
  389. for (iy = 0; iy < mYBound; ++iy)
  390. {
  391. for (ix = 0; ix < mXBound; ++ix)
  392. {
  393. for (iz = 0; iz < mZBound; ++iz)
  394. {
  395. FZ[iz][iy][ix] = ComputeDerivative(slope[iy][ix] + iz);
  396. }
  397. }
  398. }
  399. }
  400. void GetFXY(Array3<Real> const& F, Array3<Real>& FXY)
  401. {
  402. int xBoundM1 = mXBound - 1;
  403. int yBoundM1 = mYBound - 1;
  404. int ix0 = xBoundM1, ix1 = ix0 - 1, ix2 = ix1 - 1;
  405. int iy0 = yBoundM1, iy1 = iy0 - 1, iy2 = iy1 - 1;
  406. int ix, iy, iz;
  407. Real invDXDY = (Real)1 / (mXSpacing * mYSpacing);
  408. for (iz = 0; iz < mZBound; ++iz)
  409. {
  410. // corners of z-slice
  411. FXY[iz][0][0] = (Real)0.25 * invDXDY * (
  412. (Real)9 * F[iz][0][0]
  413. - (Real)12 * F[iz][0][1]
  414. + (Real)3 * F[iz][0][2]
  415. - (Real)12 * F[iz][1][0]
  416. + (Real)16 * F[iz][1][1]
  417. - (Real)4 * F[iz][1][2]
  418. + (Real)3 * F[iz][2][0]
  419. - (Real)4 * F[iz][2][1]
  420. + F[iz][2][2]);
  421. FXY[iz][0][xBoundM1] = (Real)0.25 * invDXDY * (
  422. (Real)9 * F[iz][0][ix0]
  423. - (Real)12 * F[iz][0][ix1]
  424. + (Real)3 * F[iz][0][ix2]
  425. - (Real)12 * F[iz][1][ix0]
  426. + (Real)16 * F[iz][1][ix1]
  427. - (Real)4 * F[iz][1][ix2]
  428. + (Real)3 * F[iz][2][ix0]
  429. - (Real)4 * F[iz][2][ix1]
  430. + F[iz][2][ix2]);
  431. FXY[iz][yBoundM1][0] = (Real)0.25 * invDXDY * (
  432. (Real)9 * F[iz][iy0][0]
  433. - (Real)12 * F[iz][iy0][1]
  434. + (Real)3 * F[iz][iy0][2]
  435. - (Real)12 * F[iz][iy1][0]
  436. + (Real)16 * F[iz][iy1][1]
  437. - (Real)4 * F[iz][iy1][2]
  438. + (Real)3 * F[iz][iy2][0]
  439. - (Real)4 * F[iz][iy2][1]
  440. + F[iz][iy2][2]);
  441. FXY[iz][yBoundM1][xBoundM1] = (Real)0.25 * invDXDY * (
  442. (Real)9 * F[iz][iy0][ix0]
  443. - (Real)12 * F[iz][iy0][ix1]
  444. + (Real)3 * F[iz][iy0][ix2]
  445. - (Real)12 * F[iz][iy1][ix0]
  446. + (Real)16 * F[iz][iy1][ix1]
  447. - (Real)4 * F[iz][iy1][ix2]
  448. + (Real)3 * F[iz][iy2][ix0]
  449. - (Real)4 * F[iz][iy2][ix1]
  450. + F[iz][iy2][ix2]);
  451. // x-edges of z-slice
  452. for (ix = 1; ix < xBoundM1; ++ix)
  453. {
  454. FXY[iz][0][ix] = (Real)0.25 * invDXDY * (
  455. (Real)3 * (F[iz][0][ix - 1] - F[iz][0][ix + 1]) -
  456. (Real)4 * (F[iz][1][ix - 1] - F[iz][1][ix + 1]) +
  457. (F[iz][2][ix - 1] - F[iz][2][ix + 1]));
  458. FXY[iz][yBoundM1][ix] = (Real)0.25 * invDXDY * (
  459. (Real)3 * (F[iz][iy0][ix - 1] - F[iz][iy0][ix + 1])
  460. - (Real)4 * (F[iz][iy1][ix - 1] - F[iz][iy1][ix + 1]) +
  461. (F[iz][iy2][ix - 1] - F[iz][iy2][ix + 1]));
  462. }
  463. // y-edges of z-slice
  464. for (iy = 1; iy < yBoundM1; ++iy)
  465. {
  466. FXY[iz][iy][0] = (Real)0.25 * invDXDY * (
  467. (Real)3 * (F[iz][iy - 1][0] - F[iz][iy + 1][0]) -
  468. (Real)4 * (F[iz][iy - 1][1] - F[iz][iy + 1][1]) +
  469. (F[iz][iy - 1][2] - F[iz][iy + 1][2]));
  470. FXY[iz][iy][xBoundM1] = (Real)0.25 * invDXDY * (
  471. (Real)3 * (F[iz][iy - 1][ix0] - F[iz][iy + 1][ix0])
  472. - (Real)4 * (F[iz][iy - 1][ix1] - F[iz][iy + 1][ix1]) +
  473. (F[iz][iy - 1][ix2] - F[iz][iy + 1][ix2]));
  474. }
  475. // interior of z-slice
  476. for (iy = 1; iy < yBoundM1; ++iy)
  477. {
  478. for (ix = 1; ix < xBoundM1; ++ix)
  479. {
  480. FXY[iz][iy][ix] = (Real)0.25 * invDXDY * (
  481. F[iz][iy - 1][ix - 1] - F[iz][iy - 1][ix + 1] -
  482. F[iz][iy + 1][ix - 1] + F[iz][iy + 1][ix + 1]);
  483. }
  484. }
  485. }
  486. }
  487. void GetFXZ(Array3<Real> const& F, Array3<Real> & FXZ)
  488. {
  489. int xBoundM1 = mXBound - 1;
  490. int zBoundM1 = mZBound - 1;
  491. int ix0 = xBoundM1, ix1 = ix0 - 1, ix2 = ix1 - 1;
  492. int iz0 = zBoundM1, iz1 = iz0 - 1, iz2 = iz1 - 1;
  493. int ix, iy, iz;
  494. Real invDXDZ = (Real)1 / (mXSpacing * mZSpacing);
  495. for (iy = 0; iy < mYBound; ++iy)
  496. {
  497. // corners of z-slice
  498. FXZ[0][iy][0] = (Real)0.25 * invDXDZ * (
  499. (Real)9 * F[0][iy][0]
  500. - (Real)12 * F[0][iy][1]
  501. + (Real)3 * F[0][iy][2]
  502. - (Real)12 * F[1][iy][0]
  503. + (Real)16 * F[1][iy][1]
  504. - (Real)4 * F[1][iy][2]
  505. + (Real)3 * F[2][iy][0]
  506. - (Real)4 * F[2][iy][1]
  507. + F[2][iy][2]);
  508. FXZ[0][iy][xBoundM1] = (Real)0.25 * invDXDZ * (
  509. (Real)9 * F[0][iy][ix0]
  510. - (Real)12 * F[0][iy][ix1]
  511. + (Real)3 * F[0][iy][ix2]
  512. - (Real)12 * F[1][iy][ix0]
  513. + (Real)16 * F[1][iy][ix1]
  514. - (Real)4 * F[1][iy][ix2]
  515. + (Real)3 * F[2][iy][ix0]
  516. - (Real)4 * F[2][iy][ix1]
  517. + F[2][iy][ix2]);
  518. FXZ[zBoundM1][iy][0] = (Real)0.25 * invDXDZ * (
  519. (Real)9 * F[iz0][iy][0]
  520. - (Real)12 * F[iz0][iy][1]
  521. + (Real)3 * F[iz0][iy][2]
  522. - (Real)12 * F[iz1][iy][0]
  523. + (Real)16 * F[iz1][iy][1]
  524. - (Real)4 * F[iz1][iy][2]
  525. + (Real)3 * F[iz2][iy][0]
  526. - (Real)4 * F[iz2][iy][1]
  527. + F[iz2][iy][2]);
  528. FXZ[zBoundM1][iy][xBoundM1] = (Real)0.25 * invDXDZ * (
  529. (Real)9 * F[iz0][iy][ix0]
  530. - (Real)12 * F[iz0][iy][ix1]
  531. + (Real)3 * F[iz0][iy][ix2]
  532. - (Real)12 * F[iz1][iy][ix0]
  533. + (Real)16 * F[iz1][iy][ix1]
  534. - (Real)4 * F[iz1][iy][ix2]
  535. + (Real)3 * F[iz2][iy][ix0]
  536. - (Real)4 * F[iz2][iy][ix1]
  537. + F[iz2][iy][ix2]);
  538. // x-edges of y-slice
  539. for (ix = 1; ix < xBoundM1; ++ix)
  540. {
  541. FXZ[0][iy][ix] = (Real)0.25 * invDXDZ * (
  542. (Real)3 * (F[0][iy][ix - 1] - F[0][iy][ix + 1]) -
  543. (Real)4 * (F[1][iy][ix - 1] - F[1][iy][ix + 1]) +
  544. (F[2][iy][ix - 1] - F[2][iy][ix + 1]));
  545. FXZ[zBoundM1][iy][ix] = (Real)0.25 * invDXDZ * (
  546. (Real)3 * (F[iz0][iy][ix - 1] - F[iz0][iy][ix + 1])
  547. - (Real)4 * (F[iz1][iy][ix - 1] - F[iz1][iy][ix + 1]) +
  548. (F[iz2][iy][ix - 1] - F[iz2][iy][ix + 1]));
  549. }
  550. // z-edges of y-slice
  551. for (iz = 1; iz < zBoundM1; ++iz)
  552. {
  553. FXZ[iz][iy][0] = (Real)0.25 * invDXDZ * (
  554. (Real)3 * (F[iz - 1][iy][0] - F[iz + 1][iy][0]) -
  555. (Real)4 * (F[iz - 1][iy][1] - F[iz + 1][iy][1]) +
  556. (F[iz - 1][iy][2] - F[iz + 1][iy][2]));
  557. FXZ[iz][iy][xBoundM1] = (Real)0.25 * invDXDZ * (
  558. (Real)3 * (F[iz - 1][iy][ix0] - F[iz + 1][iy][ix0])
  559. - (Real)4 * (F[iz - 1][iy][ix1] - F[iz + 1][iy][ix1]) +
  560. (F[iz - 1][iy][ix2] - F[iz + 1][iy][ix2]));
  561. }
  562. // interior of y-slice
  563. for (iz = 1; iz < zBoundM1; ++iz)
  564. {
  565. for (ix = 1; ix < xBoundM1; ++ix)
  566. {
  567. FXZ[iz][iy][ix] = ((Real)0.25) * invDXDZ * (
  568. F[iz - 1][iy][ix - 1] - F[iz - 1][iy][ix + 1] -
  569. F[iz + 1][iy][ix - 1] + F[iz + 1][iy][ix + 1]);
  570. }
  571. }
  572. }
  573. }
  574. void GetFYZ(Array3<Real> const& F, Array3<Real> & FYZ)
  575. {
  576. int yBoundM1 = mYBound - 1;
  577. int zBoundM1 = mZBound - 1;
  578. int iy0 = yBoundM1, iy1 = iy0 - 1, iy2 = iy1 - 1;
  579. int iz0 = zBoundM1, iz1 = iz0 - 1, iz2 = iz1 - 1;
  580. int ix, iy, iz;
  581. Real invDYDZ = (Real)1 / (mYSpacing * mZSpacing);
  582. for (ix = 0; ix < mXBound; ++ix)
  583. {
  584. // corners of x-slice
  585. FYZ[0][0][ix] = (Real)0.25 * invDYDZ * (
  586. (Real)9 * F[0][0][ix]
  587. - (Real)12 * F[0][1][ix]
  588. + (Real)3 * F[0][2][ix]
  589. - (Real)12 * F[1][0][ix]
  590. + (Real)16 * F[1][1][ix]
  591. - (Real)4 * F[1][2][ix]
  592. + (Real)3 * F[2][0][ix]
  593. - (Real)4 * F[2][1][ix]
  594. + F[2][2][ix]);
  595. FYZ[0][yBoundM1][ix] = (Real)0.25 * invDYDZ * (
  596. (Real)9 * F[0][iy0][ix]
  597. - (Real)12 * F[0][iy1][ix]
  598. + (Real)3 * F[0][iy2][ix]
  599. - (Real)12 * F[1][iy0][ix]
  600. + (Real)16 * F[1][iy1][ix]
  601. - (Real)4 * F[1][iy2][ix]
  602. + (Real)3 * F[2][iy0][ix]
  603. - (Real)4 * F[2][iy1][ix]
  604. + F[2][iy2][ix]);
  605. FYZ[zBoundM1][0][ix] = (Real)0.25 * invDYDZ * (
  606. (Real)9 * F[iz0][0][ix]
  607. - (Real)12 * F[iz0][1][ix]
  608. + (Real)3 * F[iz0][2][ix]
  609. - (Real)12 * F[iz1][0][ix]
  610. + (Real)16 * F[iz1][1][ix]
  611. - (Real)4 * F[iz1][2][ix]
  612. + (Real)3 * F[iz2][0][ix]
  613. - (Real)4 * F[iz2][1][ix]
  614. + F[iz2][2][ix]);
  615. FYZ[zBoundM1][yBoundM1][ix] = (Real)0.25 * invDYDZ * (
  616. (Real)9 * F[iz0][iy0][ix]
  617. - (Real)12 * F[iz0][iy1][ix]
  618. + (Real)3 * F[iz0][iy2][ix]
  619. - (Real)12 * F[iz1][iy0][ix]
  620. + (Real)16 * F[iz1][iy1][ix]
  621. - (Real)4 * F[iz1][iy2][ix]
  622. + (Real)3 * F[iz2][iy0][ix]
  623. - (Real)4 * F[iz2][iy1][ix]
  624. + F[iz2][iy2][ix]);
  625. // y-edges of x-slice
  626. for (iy = 1; iy < yBoundM1; ++iy)
  627. {
  628. FYZ[0][iy][ix] = (Real)0.25 * invDYDZ * (
  629. (Real)3 * (F[0][iy - 1][ix] - F[0][iy + 1][ix]) -
  630. (Real)4 * (F[1][iy - 1][ix] - F[1][iy + 1][ix]) +
  631. (F[2][iy - 1][ix] - F[2][iy + 1][ix]));
  632. FYZ[zBoundM1][iy][ix] = (Real)0.25 * invDYDZ * (
  633. (Real)3 * (F[iz0][iy - 1][ix] - F[iz0][iy + 1][ix])
  634. - (Real)4 * (F[iz1][iy - 1][ix] - F[iz1][iy + 1][ix]) +
  635. (F[iz2][iy - 1][ix] - F[iz2][iy + 1][ix]));
  636. }
  637. // z-edges of x-slice
  638. for (iz = 1; iz < zBoundM1; ++iz)
  639. {
  640. FYZ[iz][0][ix] = (Real)0.25 * invDYDZ * (
  641. (Real)3 * (F[iz - 1][0][ix] - F[iz + 1][0][ix]) -
  642. (Real)4 * (F[iz - 1][1][ix] - F[iz + 1][1][ix]) +
  643. (F[iz - 1][2][ix] - F[iz + 1][2][ix]));
  644. FYZ[iz][yBoundM1][ix] = (Real)0.25 * invDYDZ * (
  645. (Real)3 * (F[iz - 1][iy0][ix] - F[iz + 1][iy0][ix])
  646. - (Real)4 * (F[iz - 1][iy1][ix] - F[iz + 1][iy1][ix]) +
  647. (F[iz - 1][iy2][ix] - F[iz + 1][iy2][ix]));
  648. }
  649. // interior of x-slice
  650. for (iz = 1; iz < zBoundM1; ++iz)
  651. {
  652. for (iy = 1; iy < yBoundM1; ++iy)
  653. {
  654. FYZ[iz][iy][ix] = (Real)0.25 * invDYDZ * (
  655. F[iz - 1][iy - 1][ix] - F[iz - 1][iy + 1][ix] -
  656. F[iz + 1][iy - 1][ix] + F[iz + 1][iy + 1][ix]);
  657. }
  658. }
  659. }
  660. }
  661. void GetFXYZ(Array3<Real> const& F, Array3<Real> & FXYZ)
  662. {
  663. int xBoundM1 = mXBound - 1;
  664. int yBoundM1 = mYBound - 1;
  665. int zBoundM1 = mZBound - 1;
  666. int ix, iy, iz, ix0, iy0, iz0;
  667. Real invDXDYDZ = ((Real)1) / (mXSpacing * mYSpacing * mZSpacing);
  668. // convolution masks
  669. // centered difference, O(h^2)
  670. Real CDer[3] = { -(Real)0.5, (Real)0, (Real)0.5 };
  671. // one-sided difference, O(h^2)
  672. Real ODer[3] = { -(Real)1.5, (Real)2, -(Real)0.5 };
  673. Real mask;
  674. // corners
  675. FXYZ[0][0][0] = (Real)0;
  676. FXYZ[0][0][xBoundM1] = (Real)0;
  677. FXYZ[0][yBoundM1][0] = (Real)0;
  678. FXYZ[0][yBoundM1][xBoundM1] = (Real)0;
  679. FXYZ[zBoundM1][0][0] = (Real)0;
  680. FXYZ[zBoundM1][0][xBoundM1] = (Real)0;
  681. FXYZ[zBoundM1][yBoundM1][0] = (Real)0;
  682. FXYZ[zBoundM1][yBoundM1][xBoundM1] = (Real)0;
  683. for (iz = 0; iz <= 2; ++iz)
  684. {
  685. for (iy = 0; iy <= 2; ++iy)
  686. {
  687. for (ix = 0; ix <= 2; ++ix)
  688. {
  689. mask = invDXDYDZ * ODer[ix] * ODer[iy] * ODer[iz];
  690. FXYZ[0][0][0] += mask * F[iz][iy][ix];
  691. FXYZ[0][0][xBoundM1] += mask * F[iz][iy][xBoundM1 - ix];
  692. FXYZ[0][yBoundM1][0] += mask * F[iz][yBoundM1 - iy][ix];
  693. FXYZ[0][yBoundM1][xBoundM1] += mask * F[iz][yBoundM1 - iy][xBoundM1 - ix];
  694. FXYZ[zBoundM1][0][0] += mask * F[zBoundM1 - iz][iy][ix];
  695. FXYZ[zBoundM1][0][xBoundM1] += mask * F[zBoundM1 - iz][iy][xBoundM1 - ix];
  696. FXYZ[zBoundM1][yBoundM1][0] += mask * F[zBoundM1 - iz][yBoundM1 - iy][ix];
  697. FXYZ[zBoundM1][yBoundM1][xBoundM1] += mask * F[zBoundM1 - iz][yBoundM1 - iy][xBoundM1 - ix];
  698. }
  699. }
  700. }
  701. // x-edges
  702. for (ix0 = 1; ix0 < xBoundM1; ++ix0)
  703. {
  704. FXYZ[0][0][ix0] = (Real)0;
  705. FXYZ[0][yBoundM1][ix0] = (Real)0;
  706. FXYZ[zBoundM1][0][ix0] = (Real)0;
  707. FXYZ[zBoundM1][yBoundM1][ix0] = (Real)0;
  708. for (iz = 0; iz <= 2; ++iz)
  709. {
  710. for (iy = 0; iy <= 2; ++iy)
  711. {
  712. for (ix = 0; ix <= 2; ++ix)
  713. {
  714. mask = invDXDYDZ * CDer[ix] * ODer[iy] * ODer[iz];
  715. FXYZ[0][0][ix0] += mask * F[iz][iy][ix0 + ix - 1];
  716. FXYZ[0][yBoundM1][ix0] += mask * F[iz][yBoundM1 - iy][ix0 + ix - 1];
  717. FXYZ[zBoundM1][0][ix0] += mask * F[zBoundM1 - iz][iy][ix0 + ix - 1];
  718. FXYZ[zBoundM1][yBoundM1][ix0] += mask * F[zBoundM1 - iz][yBoundM1 - iy][ix0 + ix - 1];
  719. }
  720. }
  721. }
  722. }
  723. // y-edges
  724. for (iy0 = 1; iy0 < yBoundM1; ++iy0)
  725. {
  726. FXYZ[0][iy0][0] = (Real)0;
  727. FXYZ[0][iy0][xBoundM1] = (Real)0;
  728. FXYZ[zBoundM1][iy0][0] = (Real)0;
  729. FXYZ[zBoundM1][iy0][xBoundM1] = (Real)0;
  730. for (iz = 0; iz <= 2; ++iz)
  731. {
  732. for (iy = 0; iy <= 2; ++iy)
  733. {
  734. for (ix = 0; ix <= 2; ++ix)
  735. {
  736. mask = invDXDYDZ * ODer[ix] * CDer[iy] * ODer[iz];
  737. FXYZ[0][iy0][0] += mask * F[iz][iy0 + iy - 1][ix];
  738. FXYZ[0][iy0][xBoundM1] += mask * F[iz][iy0 + iy - 1][xBoundM1 - ix];
  739. FXYZ[zBoundM1][iy0][0] += mask * F[zBoundM1 - iz][iy0 + iy - 1][ix];
  740. FXYZ[zBoundM1][iy0][xBoundM1] += mask * F[zBoundM1 - iz][iy0 + iy - 1][xBoundM1 - ix];
  741. }
  742. }
  743. }
  744. }
  745. // z-edges
  746. for (iz0 = 1; iz0 < zBoundM1; ++iz0)
  747. {
  748. FXYZ[iz0][0][0] = (Real)0;
  749. FXYZ[iz0][0][xBoundM1] = (Real)0;
  750. FXYZ[iz0][yBoundM1][0] = (Real)0;
  751. FXYZ[iz0][yBoundM1][xBoundM1] = (Real)0;
  752. for (iz = 0; iz <= 2; ++iz)
  753. {
  754. for (iy = 0; iy <= 2; ++iy)
  755. {
  756. for (ix = 0; ix <= 2; ++ix)
  757. {
  758. mask = invDXDYDZ * ODer[ix] * ODer[iy] * CDer[iz];
  759. FXYZ[iz0][0][0] += mask * F[iz0 + iz - 1][iy][ix];
  760. FXYZ[iz0][0][xBoundM1] += mask * F[iz0 + iz - 1][iy][xBoundM1 - ix];
  761. FXYZ[iz0][yBoundM1][0] += mask * F[iz0 + iz - 1][yBoundM1 - iy][ix];
  762. FXYZ[iz0][yBoundM1][xBoundM1] += mask * F[iz0 + iz - 1][yBoundM1 - iy][xBoundM1 - ix];
  763. }
  764. }
  765. }
  766. }
  767. // xy-faces
  768. for (iy0 = 1; iy0 < yBoundM1; ++iy0)
  769. {
  770. for (ix0 = 1; ix0 < xBoundM1; ++ix0)
  771. {
  772. FXYZ[0][iy0][ix0] = (Real)0;
  773. FXYZ[zBoundM1][iy0][ix0] = (Real)0;
  774. for (iz = 0; iz <= 2; ++iz)
  775. {
  776. for (iy = 0; iy <= 2; ++iy)
  777. {
  778. for (ix = 0; ix <= 2; ++ix)
  779. {
  780. mask = invDXDYDZ * CDer[ix] * CDer[iy] * ODer[iz];
  781. FXYZ[0][iy0][ix0] += mask * F[iz][iy0 + iy - 1][ix0 + ix - 1];
  782. FXYZ[zBoundM1][iy0][ix0] += mask * F[zBoundM1 - iz][iy0 + iy - 1][ix0 + ix - 1];
  783. }
  784. }
  785. }
  786. }
  787. }
  788. // xz-faces
  789. for (iz0 = 1; iz0 < zBoundM1; ++iz0)
  790. {
  791. for (ix0 = 1; ix0 < xBoundM1; ++ix0)
  792. {
  793. FXYZ[iz0][0][ix0] = (Real)0;
  794. FXYZ[iz0][yBoundM1][ix0] = (Real)0;
  795. for (iz = 0; iz <= 2; ++iz)
  796. {
  797. for (iy = 0; iy <= 2; ++iy)
  798. {
  799. for (ix = 0; ix <= 2; ++ix)
  800. {
  801. mask = invDXDYDZ * CDer[ix] * ODer[iy] * CDer[iz];
  802. FXYZ[iz0][0][ix0] += mask * F[iz0 + iz - 1][iy][ix0 + ix - 1];
  803. FXYZ[iz0][yBoundM1][ix0] += mask * F[iz0 + iz - 1][yBoundM1 - iy][ix0 + ix - 1];
  804. }
  805. }
  806. }
  807. }
  808. }
  809. // yz-faces
  810. for (iz0 = 1; iz0 < zBoundM1; ++iz0)
  811. {
  812. for (iy0 = 1; iy0 < yBoundM1; ++iy0)
  813. {
  814. FXYZ[iz0][iy0][0] = (Real)0;
  815. FXYZ[iz0][iy0][xBoundM1] = (Real)0;
  816. for (iz = 0; iz <= 2; ++iz)
  817. {
  818. for (iy = 0; iy <= 2; ++iy)
  819. {
  820. for (ix = 0; ix <= 2; ++ix)
  821. {
  822. mask = invDXDYDZ * ODer[ix] * CDer[iy] * CDer[iz];
  823. FXYZ[iz0][iy0][0] += mask * F[iz0 + iz - 1][iy0 + iy - 1][ix];
  824. FXYZ[iz0][iy0][xBoundM1] += mask * F[iz0 + iz - 1][iy0 + iy - 1][xBoundM1 - ix];
  825. }
  826. }
  827. }
  828. }
  829. }
  830. // interiors
  831. for (iz0 = 1; iz0 < zBoundM1; ++iz0)
  832. {
  833. for (iy0 = 1; iy0 < yBoundM1; ++iy0)
  834. {
  835. for (ix0 = 1; ix0 < xBoundM1; ++ix0)
  836. {
  837. FXYZ[iz0][iy0][ix0] = (Real)0;
  838. for (iz = 0; iz <= 2; ++iz)
  839. {
  840. for (iy = 0; iy <= 2; ++iy)
  841. {
  842. for (ix = 0; ix <= 2; ++ix)
  843. {
  844. mask = invDXDYDZ * CDer[ix] * CDer[iy] * CDer[iz];
  845. FXYZ[iz0][iy0][ix0] += mask * F[iz0 + iz - 1][iy0 + iy - 1][ix0 + ix - 1];
  846. }
  847. }
  848. }
  849. }
  850. }
  851. }
  852. }
  853. void GetPolynomials(Array3<Real> const& F, Array3<Real> const& FX,
  854. Array3<Real> const& FY, Array3<Real> const& FZ, Array3<Real> const& FXY,
  855. Array3<Real> const& FXZ, Array3<Real> const& FYZ, Array3<Real> const& FXYZ)
  856. {
  857. int xBoundM1 = mXBound - 1;
  858. int yBoundM1 = mYBound - 1;
  859. int zBoundM1 = mZBound - 1;
  860. for (int iz = 0; iz < zBoundM1; ++iz)
  861. {
  862. for (int iy = 0; iy < yBoundM1; ++iy)
  863. {
  864. for (int ix = 0; ix < xBoundM1; ++ix)
  865. {
  866. // Note the 'transposing' of the 2x2x2 blocks (to match
  867. // notation used in the polynomial definition).
  868. Real G[2][2][2] =
  869. {
  870. {
  871. {
  872. F[iz][iy][ix],
  873. F[iz + 1][iy][ix]
  874. },
  875. {
  876. F[iz][iy + 1][ix],
  877. F[iz + 1][iy + 1][ix]
  878. }
  879. },
  880. {
  881. {
  882. F[iz][iy][ix + 1],
  883. F[iz + 1][iy][ix + 1]
  884. },
  885. {
  886. F[iz][iy + 1][ix + 1],
  887. F[iz + 1][iy + 1][ix + 1]
  888. }
  889. }
  890. };
  891. Real GX[2][2][2] =
  892. {
  893. {
  894. {
  895. FX[iz][iy][ix],
  896. FX[iz + 1][iy][ix]
  897. },
  898. {
  899. FX[iz][iy + 1][ix],
  900. FX[iz + 1][iy + 1][ix]
  901. }
  902. },
  903. {
  904. {
  905. FX[iz][iy][ix + 1],
  906. FX[iz + 1][iy][ix + 1]
  907. },
  908. {
  909. FX[iz][iy + 1][ix + 1],
  910. FX[iz + 1][iy + 1][ix + 1]
  911. }
  912. }
  913. };
  914. Real GY[2][2][2] =
  915. {
  916. {
  917. {
  918. FY[iz][iy][ix],
  919. FY[iz + 1][iy][ix]
  920. },
  921. {
  922. FY[iz][iy + 1][ix],
  923. FY[iz + 1][iy + 1][ix]
  924. }
  925. },
  926. {
  927. {
  928. FY[iz][iy][ix + 1],
  929. FY[iz + 1][iy][ix + 1]
  930. },
  931. {
  932. FY[iz][iy + 1][ix + 1],
  933. FY[iz + 1][iy + 1][ix + 1]
  934. }
  935. }
  936. };
  937. Real GZ[2][2][2] =
  938. {
  939. {
  940. {
  941. FZ[iz][iy][ix],
  942. FZ[iz + 1][iy][ix]
  943. },
  944. {
  945. FZ[iz][iy + 1][ix],
  946. FZ[iz + 1][iy + 1][ix]
  947. }
  948. },
  949. {
  950. {
  951. FZ[iz][iy][ix + 1],
  952. FZ[iz + 1][iy][ix + 1]
  953. },
  954. {
  955. FZ[iz][iy + 1][ix + 1],
  956. FZ[iz + 1][iy + 1][ix + 1]
  957. }
  958. }
  959. };
  960. Real GXY[2][2][2] =
  961. {
  962. {
  963. {
  964. FXY[iz][iy][ix],
  965. FXY[iz + 1][iy][ix]
  966. },
  967. {
  968. FXY[iz][iy + 1][ix],
  969. FXY[iz + 1][iy + 1][ix]
  970. }
  971. },
  972. {
  973. {
  974. FXY[iz][iy][ix + 1],
  975. FXY[iz + 1][iy][ix + 1]
  976. },
  977. {
  978. FXY[iz][iy + 1][ix + 1],
  979. FXY[iz + 1][iy + 1][ix + 1]
  980. }
  981. }
  982. };
  983. Real GXZ[2][2][2] =
  984. {
  985. {
  986. {
  987. FXZ[iz][iy][ix],
  988. FXZ[iz + 1][iy][ix]
  989. },
  990. {
  991. FXZ[iz][iy + 1][ix],
  992. FXZ[iz + 1][iy + 1][ix]
  993. }
  994. },
  995. {
  996. {
  997. FXZ[iz][iy][ix + 1],
  998. FXZ[iz + 1][iy][ix + 1]
  999. },
  1000. {
  1001. FXZ[iz][iy + 1][ix + 1],
  1002. FXZ[iz + 1][iy + 1][ix + 1]
  1003. }
  1004. }
  1005. };
  1006. Real GYZ[2][2][2] =
  1007. {
  1008. {
  1009. {
  1010. FYZ[iz][iy][ix],
  1011. FYZ[iz + 1][iy][ix]
  1012. },
  1013. {
  1014. FYZ[iz][iy + 1][ix],
  1015. FYZ[iz + 1][iy + 1][ix]
  1016. }
  1017. },
  1018. {
  1019. {
  1020. FYZ[iz][iy][ix + 1],
  1021. FYZ[iz + 1][iy][ix + 1]
  1022. },
  1023. {
  1024. FYZ[iz][iy + 1][ix + 1],
  1025. FYZ[iz + 1][iy + 1][ix + 1]
  1026. }
  1027. }
  1028. };
  1029. Real GXYZ[2][2][2] =
  1030. {
  1031. {
  1032. {
  1033. FXYZ[iz][iy][ix],
  1034. FXYZ[iz + 1][iy][ix]
  1035. },
  1036. {
  1037. FXYZ[iz][iy + 1][ix],
  1038. FXYZ[iz + 1][iy + 1][ix]
  1039. }
  1040. },
  1041. {
  1042. {
  1043. FXYZ[iz][iy][ix + 1],
  1044. FXYZ[iz + 1][iy][ix + 1]
  1045. },
  1046. {
  1047. FXYZ[iz][iy + 1][ix + 1],
  1048. FXYZ[iz + 1][iy + 1][ix + 1]
  1049. }
  1050. }
  1051. };
  1052. Construct(mPoly[iz][iy][ix], G, GX, GY, GZ, GXY, GXZ, GYZ, GXYZ);
  1053. }
  1054. }
  1055. }
  1056. }
  1057. Real ComputeDerivative(Real const* slope) const
  1058. {
  1059. if (slope[1] != slope[2])
  1060. {
  1061. if (slope[0] != slope[1])
  1062. {
  1063. if (slope[2] != slope[3])
  1064. {
  1065. Real ad0 = std::fabs(slope[3] - slope[2]);
  1066. Real ad1 = std::fabs(slope[0] - slope[1]);
  1067. return (ad0 * slope[1] + ad1 * slope[2]) / (ad0 + ad1);
  1068. }
  1069. else
  1070. {
  1071. return slope[2];
  1072. }
  1073. }
  1074. else
  1075. {
  1076. if (slope[2] != slope[3])
  1077. {
  1078. return slope[1];
  1079. }
  1080. else
  1081. {
  1082. return (Real)0.5 * (slope[1] + slope[2]);
  1083. }
  1084. }
  1085. }
  1086. else
  1087. {
  1088. return slope[1];
  1089. }
  1090. }
  1091. void Construct(Polynomial& poly,
  1092. Real const F[2][2][2], Real const FX[2][2][2], Real const FY[2][2][2],
  1093. Real const FZ[2][2][2], Real const FXY[2][2][2], Real const FXZ[2][2][2],
  1094. Real const FYZ[2][2][2], Real const FXYZ[2][2][2])
  1095. {
  1096. Real dx = mXSpacing, dy = mYSpacing, dz = mZSpacing;
  1097. Real invDX = (Real)1 / dx, invDX2 = invDX * invDX;
  1098. Real invDY = (Real)1 / dy, invDY2 = invDY * invDY;
  1099. Real invDZ = (Real)1 / dz, invDZ2 = invDZ * invDZ;
  1100. Real b0, b1, b2, b3, b4, b5, b6, b7;
  1101. poly.A(0, 0, 0) = F[0][0][0];
  1102. poly.A(1, 0, 0) = FX[0][0][0];
  1103. poly.A(0, 1, 0) = FY[0][0][0];
  1104. poly.A(0, 0, 1) = FZ[0][0][0];
  1105. poly.A(1, 1, 0) = FXY[0][0][0];
  1106. poly.A(1, 0, 1) = FXZ[0][0][0];
  1107. poly.A(0, 1, 1) = FYZ[0][0][0];
  1108. poly.A(1, 1, 1) = FXYZ[0][0][0];
  1109. // solve for Aij0
  1110. b0 = (F[1][0][0] - poly(0, 0, 0, dx, (Real)0, (Real)0)) * invDX2;
  1111. b1 = (FX[1][0][0] - poly(1, 0, 0, dx, (Real)0, (Real)0)) * invDX;
  1112. poly.A(2, 0, 0) = (Real)3 * b0 - b1;
  1113. poly.A(3, 0, 0) = ((Real)-2 * b0 + b1) * invDX;
  1114. b0 = (F[0][1][0] - poly(0, 0, 0, (Real)0, dy, (Real)0)) * invDY2;
  1115. b1 = (FY[0][1][0] - poly(0, 1, 0, (Real)0, dy, (Real)0)) * invDY;
  1116. poly.A(0, 2, 0) = (Real)3 * b0 - b1;
  1117. poly.A(0, 3, 0) = ((Real)-2 * b0 + b1) * invDY;
  1118. b0 = (FY[1][0][0] - poly(0, 1, 0, dx, (Real)0, (Real)0)) * invDX2;
  1119. b1 = (FXY[1][0][0] - poly(1, 1, 0, dx, (Real)0, (Real)0)) * invDX;
  1120. poly.A(2, 1, 0) = (Real)3 * b0 - b1;
  1121. poly.A(3, 1, 0) = ((Real)-2 * b0 + b1) * invDX;
  1122. b0 = (FX[0][1][0] - poly(1, 0, 0, (Real)0, dy, (Real)0)) * invDY2;
  1123. b1 = (FXY[0][1][0] - poly(1, 1, 0, (Real)0, dy, (Real)0)) * invDY;
  1124. poly.A(1, 2, 0) = (Real)3 * b0 - b1;
  1125. poly.A(1, 3, 0) = ((Real)-2 * b0 + b1) * invDY;
  1126. b0 = (F[1][1][0] - poly(0, 0, 0, dx, dy, (Real)0)) * invDX2 * invDY2;
  1127. b1 = (FX[1][1][0] - poly(1, 0, 0, dx, dy, (Real)0)) * invDX * invDY2;
  1128. b2 = (FY[1][1][0] - poly(0, 1, 0, dx, dy, (Real)0)) * invDX2 * invDY;
  1129. b3 = (FXY[1][1][0] - poly(1, 1, 0, dx, dy, (Real)0)) * invDX * invDY;
  1130. poly.A(2, 2, 0) = (Real)9 * b0 - (Real)3 * b1 - (Real)3 * b2 + b3;
  1131. poly.A(3, 2, 0) = ((Real)-6 * b0 + (Real)3 * b1 + (Real)2 * b2 - b3) * invDX;
  1132. poly.A(2, 3, 0) = ((Real)-6 * b0 + (Real)2 * b1 + (Real)3 * b2 - b3) * invDY;
  1133. poly.A(3, 3, 0) = ((Real)4 * b0 - (Real)2 * b1 - (Real)2 * b2 + b3) * invDX * invDY;
  1134. // solve for Ai0k
  1135. b0 = (F[0][0][1] - poly(0, 0, 0, (Real)0, (Real)0, dz)) * invDZ2;
  1136. b1 = (FZ[0][0][1] - poly(0, 0, 1, (Real)0, (Real)0, dz)) * invDZ;
  1137. poly.A(0, 0, 2) = (Real)3 * b0 - b1;
  1138. poly.A(0, 0, 3) = ((Real)-2 * b0 + b1) * invDZ;
  1139. b0 = (FZ[1][0][0] - poly(0, 0, 1, dx, (Real)0, (Real)0)) * invDX2;
  1140. b1 = (FXZ[1][0][0] - poly(1, 0, 1, dx, (Real)0, (Real)0)) * invDX;
  1141. poly.A(2, 0, 1) = (Real)3 * b0 - b1;
  1142. poly.A(3, 0, 1) = ((Real)-2 * b0 + b1) * invDX;
  1143. b0 = (FX[0][0][1] - poly(1, 0, 0, (Real)0, (Real)0, dz)) * invDZ2;
  1144. b1 = (FXZ[0][0][1] - poly(1, 0, 1, (Real)0, (Real)0, dz)) * invDZ;
  1145. poly.A(1, 0, 2) = (Real)3 * b0 - b1;
  1146. poly.A(1, 0, 3) = ((Real)-2 * b0 + b1) * invDZ;
  1147. b0 = (F[1][0][1] - poly(0, 0, 0, dx, (Real)0, dz)) * invDX2 * invDZ2;
  1148. b1 = (FX[1][0][1] - poly(1, 0, 0, dx, (Real)0, dz)) * invDX * invDZ2;
  1149. b2 = (FZ[1][0][1] - poly(0, 0, 1, dx, (Real)0, dz)) * invDX2 * invDZ;
  1150. b3 = (FXZ[1][0][1] - poly(1, 0, 1, dx, (Real)0, dz)) * invDX * invDZ;
  1151. poly.A(2, 0, 2) = (Real)9 * b0 - (Real)3 * b1 - (Real)3 * b2 + b3;
  1152. poly.A(3, 0, 2) = ((Real)-6 * b0 + (Real)3 * b1 + (Real)2 * b2 - b3) * invDX;
  1153. poly.A(2, 0, 3) = ((Real)-6 * b0 + (Real)2 * b1 + (Real)3 * b2 - b3) * invDZ;
  1154. poly.A(3, 0, 3) = ((Real)4 * b0 - (Real)2 * b1 - (Real)2 * b2 + b3) * invDX * invDZ;
  1155. // solve for A0jk
  1156. b0 = (FZ[0][1][0] - poly(0, 0, 1, (Real)0, dy, (Real)0)) * invDY2;
  1157. b1 = (FYZ[0][1][0] - poly(0, 1, 1, (Real)0, dy, (Real)0)) * invDY;
  1158. poly.A(0, 2, 1) = (Real)3 * b0 - b1;
  1159. poly.A(0, 3, 1) = ((Real)-2 * b0 + b1) * invDY;
  1160. b0 = (FY[0][0][1] - poly(0, 1, 0, (Real)0, (Real)0, dz)) * invDZ2;
  1161. b1 = (FYZ[0][0][1] - poly(0, 1, 1, (Real)0, (Real)0, dz)) * invDZ;
  1162. poly.A(0, 1, 2) = (Real)3 * b0 - b1;
  1163. poly.A(0, 1, 3) = ((Real)-2 * b0 + b1) * invDZ;
  1164. b0 = (F[0][1][1] - poly(0, 0, 0, (Real)0, dy, dz)) * invDY2 * invDZ2;
  1165. b1 = (FY[0][1][1] - poly(0, 1, 0, (Real)0, dy, dz)) * invDY * invDZ2;
  1166. b2 = (FZ[0][1][1] - poly(0, 0, 1, (Real)0, dy, dz)) * invDY2 * invDZ;
  1167. b3 = (FYZ[0][1][1] - poly(0, 1, 1, (Real)0, dy, dz)) * invDY * invDZ;
  1168. poly.A(0, 2, 2) = (Real)9 * b0 - (Real)3 * b1 - (Real)3 * b2 + b3;
  1169. poly.A(0, 3, 2) = ((Real)-6 * b0 + (Real)3 * b1 + (Real)2 * b2 - b3) * invDY;
  1170. poly.A(0, 2, 3) = ((Real)-6 * b0 + (Real)2 * b1 + (Real)3 * b2 - b3) * invDZ;
  1171. poly.A(0, 3, 3) = ((Real)4 * b0 - (Real)2 * b1 - (Real)2 * b2 + b3) * invDY * invDZ;
  1172. // solve for Aij1
  1173. b0 = (FYZ[1][0][0] - poly(0, 1, 1, dx, (Real)0, (Real)0)) * invDX2;
  1174. b1 = (FXYZ[1][0][0] - poly(1, 1, 1, dx, (Real)0, (Real)0)) * invDX;
  1175. poly.A(2, 1, 1) = (Real)3 * b0 - b1;
  1176. poly.A(3, 1, 1) = ((Real)-2 * b0 + b1) * invDX;
  1177. b0 = (FXZ[0][1][0] - poly(1, 0, 1, (Real)0, dy, (Real)0)) * invDY2;
  1178. b1 = (FXYZ[0][1][0] - poly(1, 1, 1, (Real)0, dy, (Real)0)) * invDY;
  1179. poly.A(1, 2, 1) = (Real)3 * b0 - b1;
  1180. poly.A(1, 3, 1) = ((Real)-2 * b0 + b1) * invDY;
  1181. b0 = (FZ[1][1][0] - poly(0, 0, 1, dx, dy, (Real)0)) * invDX2 * invDY2;
  1182. b1 = (FXZ[1][1][0] - poly(1, 0, 1, dx, dy, (Real)0)) * invDX * invDY2;
  1183. b2 = (FYZ[1][1][0] - poly(0, 1, 1, dx, dy, (Real)0)) * invDX2 * invDY;
  1184. b3 = (FXYZ[1][1][0] - poly(1, 1, 1, dx, dy, (Real)0)) * invDX * invDY;
  1185. poly.A(2, 2, 1) = (Real)9 * b0 - (Real)3 * b1 - (Real)3 * b2 + b3;
  1186. poly.A(3, 2, 1) = ((Real)-6 * b0 + (Real)3 * b1 + (Real)2 * b2 - b3) * invDX;
  1187. poly.A(2, 3, 1) = ((Real)-6 * b0 + (Real)2 * b1 + (Real)3 * b2 - b3) * invDY;
  1188. poly.A(3, 3, 1) = ((Real)4 * b0 - (Real)2 * b1 - (Real)2 * b2 + b3) * invDX * invDY;
  1189. // solve for Ai1k
  1190. b0 = (FXY[0][0][1] - poly(1, 1, 0, (Real)0, (Real)0, dz)) * invDZ2;
  1191. b1 = (FXYZ[0][0][1] - poly(1, 1, 1, (Real)0, (Real)0, dz)) * invDZ;
  1192. poly.A(1, 1, 2) = (Real)3 * b0 - b1;
  1193. poly.A(1, 1, 3) = ((Real)-2 * b0 + b1) * invDZ;
  1194. b0 = (FY[1][0][1] - poly(0, 1, 0, dx, (Real)0, dz)) * invDX2 * invDZ2;
  1195. b1 = (FXY[1][0][1] - poly(1, 1, 0, dx, (Real)0, dz)) * invDX * invDZ2;
  1196. b2 = (FYZ[1][0][1] - poly(0, 1, 1, dx, (Real)0, dz)) * invDX2 * invDZ;
  1197. b3 = (FXYZ[1][0][1] - poly(1, 1, 1, dx, (Real)0, dz)) * invDX * invDZ;
  1198. poly.A(2, 1, 2) = (Real)9 * b0 - (Real)3 * b1 - (Real)3 * b2 + b3;
  1199. poly.A(3, 1, 2) = ((Real)-6 * b0 + (Real)3 * b1 + (Real)2 * b2 - b3) * invDX;
  1200. poly.A(2, 1, 3) = ((Real)-6 * b0 + (Real)2 * b1 + (Real)3 * b2 - b3) * invDZ;
  1201. poly.A(3, 1, 3) = ((Real)4 * b0 - (Real)2 * b1 - (Real)2 * b2 + b3) * invDX * invDZ;
  1202. // solve for A1jk
  1203. b0 = (FX[0][1][1] - poly(1, 0, 0, (Real)0, dy, dz)) * invDY2 * invDZ2;
  1204. b1 = (FXY[0][1][1] - poly(1, 1, 0, (Real)0, dy, dz)) * invDY * invDZ2;
  1205. b2 = (FXZ[0][1][1] - poly(1, 0, 1, (Real)0, dy, dz)) * invDY2 * invDZ;
  1206. b3 = (FXYZ[0][1][1] - poly(1, 1, 1, (Real)0, dy, dz)) * invDY * invDZ;
  1207. poly.A(1, 2, 2) = (Real)9 * b0 - (Real)3 * b1 - (Real)3 * b2 + b3;
  1208. poly.A(1, 3, 2) = ((Real)-6 * b0 + (Real)3 * b1 + (Real)2 * b2 - b3) * invDY;
  1209. poly.A(1, 2, 3) = ((Real)-6 * b0 + (Real)2 * b1 + (Real)3 * b2 - b3) * invDZ;
  1210. poly.A(1, 3, 3) = ((Real)4 * b0 - (Real)2 * b1 - (Real)2 * b2 + b3) * invDY * invDZ;
  1211. // solve for remaining Aijk with i >= 2, j >= 2, k >= 2
  1212. b0 = (F[1][1][1] - poly(0, 0, 0, dx, dy, dz)) * invDX2 * invDY2 * invDZ2;
  1213. b1 = (FX[1][1][1] - poly(1, 0, 0, dx, dy, dz)) * invDX * invDY2 * invDZ2;
  1214. b2 = (FY[1][1][1] - poly(0, 1, 0, dx, dy, dz)) * invDX2 * invDY * invDZ2;
  1215. b3 = (FZ[1][1][1] - poly(0, 0, 1, dx, dy, dz)) * invDX2 * invDY2 * invDZ;
  1216. b4 = (FXY[1][1][1] - poly(1, 1, 0, dx, dy, dz)) * invDX * invDY * invDZ2;
  1217. b5 = (FXZ[1][1][1] - poly(1, 0, 1, dx, dy, dz)) * invDX * invDY2 * invDZ;
  1218. b6 = (FYZ[1][1][1] - poly(0, 1, 1, dx, dy, dz)) * invDX2 * invDY * invDZ;
  1219. b7 = (FXYZ[1][1][1] - poly(1, 1, 1, dx, dy, dz)) * invDX * invDY * invDZ;
  1220. poly.A(2, 2, 2) = (Real)27 * b0 - (Real)9 * b1 - (Real)9 * b2 -
  1221. (Real)9 * b3 + (Real)3 * b4 + (Real)3 * b5 + (Real)3 * b6 - b7;
  1222. poly.A(3, 2, 2) = ((Real)-18 * b0 + (Real)9 * b1 + (Real)6 * b2 +
  1223. (Real)6 * b3 - (Real)3 * b4 - (Real)3 * b5 - (Real)2 * b6 + b7) * invDX;
  1224. poly.A(2, 3, 2) = ((Real)-18 * b0 + (Real)6 * b1 + (Real)9 * b2 +
  1225. (Real)6 * b3 - (Real)3 * b4 - (Real)2 * b5 - (Real)3 * b6 + b7) * invDY;
  1226. poly.A(2, 2, 3) = ((Real)-18 * b0 + (Real)6 * b1 + (Real)6 * b2 +
  1227. (Real)9 * b3 - (Real)2 * b4 - (Real)3 * b5 - (Real)3 * b6 + b7) * invDZ;
  1228. poly.A(3, 3, 2) = ((Real)12 * b0 - (Real)6 * b1 - (Real)6 * b2 -
  1229. (Real)4 * b3 + (Real)3 * b4 + (Real)2 * b5 + (Real)2 * b6 - b7) *
  1230. invDX * invDY;
  1231. poly.A(3, 2, 3) = ((Real)12 * b0 - (Real)6 * b1 - (Real)4 * b2 -
  1232. (Real)6 * b3 + (Real)2 * b4 + (Real)3 * b5 + (Real)2 * b6 - b7) *
  1233. invDX * invDZ;
  1234. poly.A(2, 3, 3) = ((Real)12 * b0 - (Real)4 * b1 - (Real)6 * b2 -
  1235. (Real)6 * b3 + (Real)2 * b4 + (Real)2 * b5 + (Real)3 * b6 - b7) *
  1236. invDY * invDZ;
  1237. poly.A(3, 3, 3) = ((Real)-8 * b0 + (Real)4 * b1 + (Real)4 * b2 +
  1238. (Real)4 * b3 - (Real)2 * b4 - (Real)2 * b5 - (Real)2 * b6 + b7) *
  1239. invDX * invDY * invDZ;
  1240. }
  1241. void XLookup(Real x, int& xIndex, Real& dx) const
  1242. {
  1243. for (xIndex = 0; xIndex + 1 < mXBound; ++xIndex)
  1244. {
  1245. if (x < mXMin + mXSpacing * (xIndex + 1))
  1246. {
  1247. dx = x - (mXMin + mXSpacing * xIndex);
  1248. return;
  1249. }
  1250. }
  1251. --xIndex;
  1252. dx = x - (mXMin + mXSpacing * xIndex);
  1253. }
  1254. void YLookup(Real y, int& yIndex, Real & dy) const
  1255. {
  1256. for (yIndex = 0; yIndex + 1 < mYBound; ++yIndex)
  1257. {
  1258. if (y < mYMin + mYSpacing * (yIndex + 1))
  1259. {
  1260. dy = y - (mYMin + mYSpacing * yIndex);
  1261. return;
  1262. }
  1263. }
  1264. --yIndex;
  1265. dy = y - (mYMin + mYSpacing * yIndex);
  1266. }
  1267. void ZLookup(Real z, int& zIndex, Real & dz) const
  1268. {
  1269. for (zIndex = 0; zIndex + 1 < mZBound; ++zIndex)
  1270. {
  1271. if (z < mZMin + mZSpacing * (zIndex + 1))
  1272. {
  1273. dz = z - (mZMin + mZSpacing * zIndex);
  1274. return;
  1275. }
  1276. }
  1277. --zIndex;
  1278. dz = z - (mZMin + mZSpacing * zIndex);
  1279. }
  1280. int mXBound, mYBound, mZBound, mQuantity;
  1281. Real mXMin, mXMax, mXSpacing;
  1282. Real mYMin, mYMax, mYSpacing;
  1283. Real mZMin, mZMax, mZSpacing;
  1284. Real const* mF;
  1285. Array3<Polynomial> mPoly;
  1286. };
  1287. }