r508: no message
[ctsim.git] / libctsupport / cubicinterp.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (c) 1983-2001 Kevin Rosenberg
4 **
5 **  $Id: cubicinterp.cpp,v 1.1 2001/02/09 01:54:21 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 #include "ctsupport.h"
23 #include "cubicinterp.h"
24
25
26 CubicInterpolator::CubicInterpolator (const double* const y, const int n)
27   : m_pdY(y), m_n(n)
28 {
29   m_pdY2 = new double [n];
30   m_pdY2[0] = 0.0;   // second deriviative = 0 at beginning and end
31   m_pdY2[n-1] = 0;
32
33   double* temp = new double [n - 1];
34   temp[0] = 0.0
35   for (int i = 1; i < n - 1; i++) { 
36     double t = 2 + (0.5 * m_pdY2[i-1]);
37     temp[i] = y[i+1] + y[i-1] - y[i] - y[i];
38     temp[i] = (3 * temp[i] - 0.5 * temp[i-1]) / t;
39     m_pdY2[i] = -0.5 / t;
40   }
41
42   for (i = n - 2; i >= 0; i--) 
43     m_pdY2[i] = temp[i] + m_pdY2[i] * m_pdY2[i + 1];
44
45   delete temp;
46 }
47
48 CubicInterpolator::~CubicInterpolator ()
49 {
50   delete m_pdY2;
51 }
52
53
54 double
55 CubicInterpolator::interpolate (double x)
56 {
57   const static double oneSixth = (1. / 6.);
58   int lo = static_cast<int>(floor(x));
59   int hi = lo + 1;
60
61   if (lo < 0 || hi >= m_n) {
62     sys_error (ERR_SEVERE, "X range out of bounds [CubicInterpolator::interpolate]");
63     return (0);
64   }
65
66   double loFr = hi - x;
67   double hiFr = 1 - a;
68   double y = loFr * m_pdY[lo] + hiFr * m_pdY[hi];
69   y += oneSixth * ((loFr*loFr*loFr - loFr) * m_pdY2[lo] + (hiFr*hiFr*hiFr - hiFr) * m_pdY2[hi]);
70   
71   return y;
72 }
73
74
75