1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (c) 1983-2001 Kevin Rosenberg
5 ** $Id: interpolator.h,v 1.2 2001/03/21 21:45:31 kevin Exp $
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License (version 2) as
9 ** published by the Free Software Foundation.
11 ** This program 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.
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ******************************************************************************/
22 class CubicSplineInterpolator {
24 double *m_pdY2; // second differential of y data
26 const double* const m_pdY;
30 CubicSplineInterpolator (const double* const y, int n);
32 ~CubicSplineInterpolator ();
34 double interpolate (double x);
37 class CubicPolyInterpolator {
39 const double* const m_pdY;
43 CubicPolyInterpolator (const double* const y, int n);
45 ~CubicPolyInterpolator ();
47 double interpolate (double x);
52 class BilinearInterpolator {
55 const unsigned int m_nx;
56 const unsigned int m_ny;
59 BilinearInterpolator (T** ppMatrix, unsigned int nx, unsigned int ny)
60 : m_ppMatrix(ppMatrix), m_nx(nx), m_ny(ny)
63 T interpolate (double dXPos, double dYPos)
65 int iFloorX = floor (dXPos);
66 int iFloorY = floor (dYPos);
67 double dXFrac = dXPos - iFloorX;
68 double dYFrac = dYPos - iFloorY;
72 if (iFloorX < 0 || iFloorY < 0 || iFloorX > m_nx-1 || iFloorY > m_ny-1)
74 else if (iFloorX == m_nx - 1 && iFloorY == m_ny - 1)
75 result = m_ppMatrix[m_nx-1][m_ny-1];
76 else if (iFloorX == m_nx - 1)
77 result = m_ppMatrix[iFloorX][iFloorY] + dYFrac * (m_ppMatrix[iFloorX][iFloorY+1] - m_ppMatrix[iFloorX][iFloorY]);
78 else if (iFloorY == m_ny - 1)
79 result = m_ppMatrix[iFloorX][iFloorY] + dXFrac * (m_ppMatrix[iFloorX+1][iFloorY] - m_ppMatrix[iFloorX][iFloorY]);
81 result = (1 - dXFrac) * (1 - dYFrac) * m_ppMatrix[iFloorX][iFloorY] +
82 dXFrac * (1 - dYFrac) * m_ppMatrix[iFloorX+1][iFloorY] +
83 dYFrac * (1 - dXFrac) * m_ppMatrix[iFloorX][iFloorY+1] +
84 dXFrac * dYFrac * m_ppMatrix[iFloorX+1][iFloorY+1];
92 class LinearInterpolator {
96 const unsigned int m_n;
99 LinearInterpolator (T* pY, unsigned int n)
100 : m_pY(pY), m_pX(0), m_n(n)
103 LinearInterpolator (T* pY, T* pX, unsigned int n)
104 : m_pY(pY), m_pX(pX), m_n(n)
107 double interpolate (double dX, int* piLastFloor = NULL)
116 else if (dX == m_n - 1)
117 result = m_pY[m_n-1];
118 else if (dX > m_n - 1)
121 int iFloor = floor(dX);
122 result = m_pY[iFloor] + (m_pY[iFloor+1] - m_pY[iFloor]) * (dX - iFloor);
127 if (piLastFloor && *piLastFloor >= 0 && m_pX[*piLastFloor] < dX)
128 iLower = *piLastFloor;
130 while (iUpper - iLower > 1) {
131 int iMiddle = (iUpper + iLower) >> 1;
132 if (dX >= m_pX[iMiddle])
139 else if (dX < m_pX[0])
141 else if (dX == m_pX[m_n-1])
142 result = m_pY[m_n-1];
143 else if (dX > m_pX[m_n-1])
146 if (iLower < 0 || iLower >= m_n) {
147 sys_error (ERR_SEVERE, "Coordinate out of range [linearInterpolate]");
152 *piLastFloor = iLower;
153 result = m_pY[iLower] + (m_pY[iUpper] - m_pY[iLower]) * ((dX - m_pX[iLower]) / (m_pX[iUpper] - m_pX[iLower]));