1 /***************************************************************************
2
3 file : spline.cpp
4 created : Wed Mai 14 20:10:00 CET 2003
5 copyright : (C) 2003-2004 by Bernhard Wymann
6 email : berniw@bluewin.ch
7 version : $Id: spline.cpp,v 1.4 2006/03/06 22:43:50 berniw Exp $
8
9 ***************************************************************************/
10
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 ***************************************************************************/
19
20
21 #include "spline.h"
22
23
Spline(int dim,SplinePoint * s)24 Spline::Spline(int dim, SplinePoint *s)
25 {
26 this->s = s;
27 this->dim = dim;
28 }
29
30
evaluate(float z)31 float Spline::evaluate(float z)
32 {
33 int i, a, b;
34 float t, a0, a1, a2, a3, h;
35
36 // Binary search for interval.
37 a = 0; b = dim - 1;
38 do {
39 i = (a + b) / 2;
40 if (s[i].x <= z) {
41 a = i;
42 } else {
43 b = i;
44 }
45 } while ((a + 1) != b);
46
47 // Evaluate.
48 i = a;
49 h = s[i+1].x - s[i].x;
50 t = (z-s[i].x) / h;
51 a0 = s[i].y;
52 a1 = s[i+1].y - a0;
53 a2 = a1 - h*s[i].s;
54 a3 = h * s[i+1].s - a1;
55 a3 -= a2;
56 return a0 + (a1 + (a2 + a3*t) * (t-1))*t;
57 }
58
59