r642: no message
[ctsim.git] / include / interpolator.h
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (c) 1983-2001 Kevin Rosenberg
4 **
5 **  $Id: interpolator.h,v 1.3 2001/03/22 02:30:00 kevin Exp $
6 **
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.
10 **
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.
15 **
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 ******************************************************************************/
20
21
22 class CubicSplineInterpolator {
23 private:
24   double *m_pdY2;  // second differential of y data
25
26   const double* const m_pdY;
27   const int m_n;
28
29 public:
30   CubicSplineInterpolator (const double* const y, int n);
31
32   ~CubicSplineInterpolator ();
33
34   double interpolate (double x);
35 };
36
37 class CubicPolyInterpolator {
38 private:
39   const double* const m_pdY;
40   const int m_n;
41
42 public:
43   CubicPolyInterpolator (const double* const y, int n);
44
45   ~CubicPolyInterpolator ();
46
47   double interpolate (double x);
48 };
49
50
51 template<class T>
52 class BilinearInterpolator {
53 private:
54   T** const m_ppMatrix;
55   const unsigned int m_nx;
56   const unsigned int m_ny;
57
58 public:
59   BilinearInterpolator (T** ppMatrix, unsigned int nx, unsigned int ny)
60   : m_ppMatrix(ppMatrix), m_nx(nx), m_ny(ny)
61   {}
62   
63   T interpolate (double dXPos, double dYPos)
64   {
65     int iFloorX = floor (dXPos);
66     int iFloorY = floor (dYPos);
67     double dXFrac = dXPos - iFloorX;
68     double dYFrac = dYPos - iFloorY;
69
70     T result = 0;
71
72     if (iFloorX < 0 || iFloorY < 0 || iFloorX > m_nx-1 || iFloorY > m_ny-1)
73       result = 0;
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]);
80     else
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];
85
86     return result;
87   }
88 };
89
90
91 template<class T>
92 class LinearInterpolator {
93 private:
94   T* const m_pX;
95   T* const m_pY;
96   const unsigned int m_n;
97   const bool m_bZeroOutsideRange;
98
99 public:
100   LinearInterpolator (T* pY, unsigned int n, bool bZeroOutside = true)
101   : m_pX(0), m_pY(pY), m_n(n), m_bZeroOutsideRange(bZeroOutside)
102   {}
103   
104   LinearInterpolator (T* pX, T* pY, unsigned int n, bool bZeroOutside = true)
105   : m_pX(pX), m_pY(pY), m_n(n), m_bZeroOutsideRange(bZeroOutside)
106   {}
107   
108   double interpolate (double dX, int* piLastFloor = NULL)
109   {
110     double result = 0;
111
112     if (! m_pX) {
113       if (dX == 0)
114         result = m_pY[0];
115       else if (dX < 0) {
116         if (m_bZeroOutsideRange)
117           result = 0;
118         else
119           result = m_pY[0];
120       } else if (dX == m_n - 1)
121         result = m_pY[m_n-1];
122       else if (dX > m_n - 1) {
123         if (m_bZeroOutsideRange)
124           result = 0;
125         else
126           result = m_pY[m_n - 1];
127       } else {
128        int iFloor = floor(dX);
129        result = m_pY[iFloor] + (m_pY[iFloor+1] - m_pY[iFloor]) * (dX - iFloor);
130       }
131     } else {
132       int iLower = -1;
133       int iUpper = m_n;
134       if (piLastFloor && *piLastFloor >= 0 && m_pX[*piLastFloor] < dX)
135         iLower = *piLastFloor;
136
137       while (iUpper - iLower > 1) {
138         int iMiddle = (iUpper + iLower) >> 1;
139         if (dX >= m_pX[iMiddle])
140           iLower = iMiddle;
141         else
142           iUpper = iMiddle;
143       }
144       if (dX == m_pX[0])
145         result = m_pY[0];
146       else if (dX < m_pX[0]) {
147         if (m_bZeroOutsideRange)
148           result = 0;
149         else
150           result = m_pY[0];
151       } else if (dX == m_pX[m_n-1])
152         result = m_pY[m_n-1];
153       else if (dX > m_pX[m_n - 1]) {
154         if (m_bZeroOutsideRange)
155           result = 0;
156         else
157           result = m_pY[m_n - 1];
158       } else {
159         if (iLower < 0 || iLower >= m_n) {
160           sys_error (ERR_SEVERE, "Coordinate out of range [linearInterpolate]");
161           return 0;
162         }
163
164        if (piLastFloor)
165          *piLastFloor = iLower;
166        result = m_pY[iLower] + (m_pY[iUpper] - m_pY[iLower]) * ((dX - m_pX[iLower]) / (m_pX[iUpper] - m_pX[iLower]));
167       }
168     }
169
170     return result;
171   }
172 };
173