1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3
* Copyright (C) 1997 Josef Wilgen
4
* Copyright (C) 2002 Uwe Rathmann
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the Qwt License, Version 1.0
8
*****************************************************************************/
10
#include "qwt_spline.h"
13
class QwtSpline::PrivateData
17
splineType( QwtSpline::Natural )
21
QwtSpline::SplineType splineType;
23
// coefficient vectors
32
static int lookup( double x, const QPolygonF &values )
35
//qLowerBound/qHigherBound ???
38
const int size = ( int )values.size();
40
if ( x <= values[0].x() )
42
else if ( x >= values[size - 2].x() )
52
i3 = i1 + ( ( i2 - i1 ) >> 1 );
54
if ( values[i3].x() > x )
64
QwtSpline::QwtSpline()
66
d_data = new PrivateData;
71
\param other Spline used for initilization
73
QwtSpline::QwtSpline( const QwtSpline& other )
75
d_data = new PrivateData( *other.d_data );
80
\param other Spline used for initilization
82
QwtSpline &QwtSpline::operator=( const QwtSpline & other )
84
*d_data = *other.d_data;
89
QwtSpline::~QwtSpline()
95
Select the algorithm used for calculating the spline
97
\param splineType Spline type
100
void QwtSpline::setSplineType( SplineType splineType )
102
d_data->splineType = splineType;
106
\return the spline type
109
QwtSpline::SplineType QwtSpline::splineType() const
111
return d_data->splineType;
115
\brief Calculate the spline coefficients
117
Depending on the value of \a periodic, this function
118
will determine the coefficients for a natural or a periodic
119
spline and store them internally.
122
\return true if successful
123
\warning The sequence of x (but not y) values has to be strictly monotone
124
increasing, which means <code>points[i].x() < points[i+1].x()</code>.
125
If this is not the case, the function will return false
127
bool QwtSpline::setPoints( const QPolygonF& points )
129
const int size = points.size();
136
d_data->points = points;
138
d_data->a.resize( size - 1 );
139
d_data->b.resize( size - 1 );
140
d_data->c.resize( size - 1 );
143
if ( d_data->splineType == Periodic )
144
ok = buildPeriodicSpline( points );
146
ok = buildNaturalSpline( points );
155
Return points passed by setPoints
157
QPolygonF QwtSpline::points() const
159
return d_data->points;
162
//! \return A coefficients
163
const QVector<double> &QwtSpline::coefficientsA() const
168
//! \return B coefficients
169
const QVector<double> &QwtSpline::coefficientsB() const
174
//! \return C coefficients
175
const QVector<double> &QwtSpline::coefficientsC() const
181
//! Free allocated memory and set size to 0
182
void QwtSpline::reset()
184
d_data->a.resize( 0 );
185
d_data->b.resize( 0 );
186
d_data->c.resize( 0 );
187
d_data->points.resize( 0 );
191
bool QwtSpline::isValid() const
193
return d_data->a.size() > 0;
197
Calculate the interpolated function value corresponding
198
to a given argument x.
200
double QwtSpline::value( double x ) const
202
if ( d_data->a.size() == 0 )
205
const int i = lookup( x, d_data->points );
207
const double delta = x - d_data->points[i].x();
208
return( ( ( ( d_data->a[i] * delta ) + d_data->b[i] )
209
* delta + d_data->c[i] ) * delta + d_data->points[i].y() );
213
\brief Determines the coefficients for a natural spline
214
\return true if successful
216
bool QwtSpline::buildNaturalSpline( const QPolygonF &points )
220
const QPointF *p = points.data();
221
const int size = points.size();
223
double *a = d_data->a.data();
224
double *b = d_data->b.data();
225
double *c = d_data->c.data();
227
// set up tridiagonal equation system; use coefficient
228
// vectors as temporary buffers
229
QVector<double> h( size - 1 );
230
for ( i = 0; i < size - 1; i++ )
232
h[i] = p[i+1].x() - p[i].x();
237
QVector<double> d( size - 1 );
238
double dy1 = ( p[1].y() - p[0].y() ) / h[0];
239
for ( i = 1; i < size - 1; i++ )
242
a[i] = 2.0 * ( h[i-1] + h[i] );
244
const double dy2 = ( p[i+1].y() - p[i].y() ) / h[i];
245
d[i] = 6.0 * ( dy1 - dy2 );
254
for ( i = 1; i < size - 2; i++ )
257
a[i+1] -= b[i] * c[i];
260
// forward elimination
261
QVector<double> s( size );
263
for ( i = 2; i < size - 1; i++ )
264
s[i] = d[i] - c[i-1] * s[i-1];
266
// backward elimination
267
s[size - 2] = - s[size - 2] / a[size - 2];
268
for ( i = size - 3; i > 0; i-- )
269
s[i] = - ( s[i] + b[i] * s[i+1] ) / a[i];
270
s[size - 1] = s[0] = 0.0;
273
// Finally, determine the spline coefficients
275
for ( i = 0; i < size - 1; i++ )
277
a[i] = ( s[i+1] - s[i] ) / ( 6.0 * h[i] );
279
c[i] = ( p[i+1].y() - p[i].y() ) / h[i]
280
- ( s[i+1] + 2.0 * s[i] ) * h[i] / 6.0;
287
\brief Determines the coefficients for a periodic spline
288
\return true if successful
290
bool QwtSpline::buildPeriodicSpline( const QPolygonF &points )
294
const QPointF *p = points.data();
295
const int size = points.size();
297
double *a = d_data->a.data();
298
double *b = d_data->b.data();
299
double *c = d_data->c.data();
301
QVector<double> d( size - 1 );
302
QVector<double> h( size - 1 );
303
QVector<double> s( size );
306
// setup equation system; use coefficient
307
// vectors as temporary buffers
309
for ( i = 0; i < size - 1; i++ )
311
h[i] = p[i+1].x() - p[i].x();
316
const int imax = size - 2;
317
double htmp = h[imax];
318
double dy1 = ( p[0].y() - p[imax].y() ) / htmp;
319
for ( i = 0; i <= imax; i++ )
322
a[i] = 2.0 * ( htmp + h[i] );
323
const double dy2 = ( p[i+1].y() - p[i].y() ) / h[i];
324
d[i] = 6.0 * ( dy1 - dy2 );
334
a[0] = qSqrt( a[0] );
335
c[0] = h[imax] / a[0];
338
for ( i = 0; i < imax - 1; i++ )
342
c[i] = - c[i-1] * b[i-1] / a[i];
343
a[i+1] = qSqrt( a[i+1] - qwtSqr( b[i] ) );
344
sum += qwtSqr( c[i] );
346
b[imax-1] = ( b[imax-1] - c[imax-2] * b[imax-2] ) / a[imax-1];
347
a[imax] = qSqrt( a[imax] - qwtSqr( b[imax-1] ) - sum );
350
// forward elimination
353
for ( i = 1; i < imax; i++ )
355
s[i] = ( d[i] - b[i-1] * s[i-1] ) / a[i];
356
sum += c[i-1] * s[i-1];
358
s[imax] = ( d[imax] - b[imax-1] * s[imax-1] - sum ) / a[imax];
361
// backward elimination
362
s[imax] = - s[imax] / a[imax];
363
s[imax-1] = -( s[imax-1] + b[imax-1] * s[imax] ) / a[imax-1];
364
for ( i = imax - 2; i >= 0; i-- )
365
s[i] = - ( s[i] + b[i] * s[i+1] + c[i] * s[imax] ) / a[i];
368
// Finally, determine the spline coefficients
371
for ( i = 0; i < size - 1; i++ )
373
a[i] = ( s[i+1] - s[i] ) / ( 6.0 * h[i] );
375
c[i] = ( p[i+1].y() - p[i].y() )
376
/ h[i] - ( s[i+1] + 2.0 * s[i] ) * h[i] / 6.0;