qwt_spline.cpp 8.26 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6 7 8 9 10 11 12 13 14 15 16
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include "qwt_spline.h"
#include "qwt_math.h"

class QwtSpline::PrivateData
{
public:
    PrivateData():
Bryant's avatar
Bryant committed
17 18
        splineType( QwtSpline::Natural )
    {
pixhawk's avatar
pixhawk committed
19 20 21 22 23
    }

    QwtSpline::SplineType splineType;

    // coefficient vectors
Bryant's avatar
Bryant committed
24 25 26
    QVector<double> a;
    QVector<double> b;
    QVector<double> c;
pixhawk's avatar
pixhawk committed
27 28 29 30 31

    // control points
    QPolygonF points;
};

Bryant's avatar
Bryant committed
32
static int lookup( double x, const QPolygonF &values )
pixhawk's avatar
pixhawk committed
33 34
{
#if 0
Bryant's avatar
Bryant committed
35
//qLowerBound/qHigherBound ???
pixhawk's avatar
pixhawk committed
36 37
#endif
    int i1;
Bryant's avatar
Bryant committed
38
    const int size = values.size();
39

Bryant's avatar
Bryant committed
40
    if ( x <= values[0].x() )
41
        i1 = 0;
Bryant's avatar
Bryant committed
42
    else if ( x >= values[size - 2].x() )
43
        i1 = size - 2;
Bryant's avatar
Bryant committed
44 45
    else
    {
pixhawk's avatar
pixhawk committed
46 47 48 49
        i1 = 0;
        int i2 = size - 2;
        int i3 = 0;

Bryant's avatar
Bryant committed
50 51 52
        while ( i2 - i1 > 1 )
        {
            i3 = i1 + ( ( i2 - i1 ) >> 1 );
pixhawk's avatar
pixhawk committed
53

Bryant's avatar
Bryant committed
54
            if ( values[i3].x() > x )
55
                i2 = i3;
pixhawk's avatar
pixhawk committed
56
            else
57
                i1 = i3;
pixhawk's avatar
pixhawk committed
58 59 60 61 62 63 64 65 66 67 68
        }
    }
    return i1;
}

//! Constructor
QwtSpline::QwtSpline()
{
    d_data = new PrivateData;
}

Bryant's avatar
Bryant committed
69 70 71 72 73
/*!
   Copy constructor
   \param other Spline used for initialization
*/
QwtSpline::QwtSpline( const QwtSpline& other )
pixhawk's avatar
pixhawk committed
74
{
Bryant's avatar
Bryant committed
75
    d_data = new PrivateData( *other.d_data );
pixhawk's avatar
pixhawk committed
76 77
}

Bryant's avatar
Bryant committed
78 79 80 81 82 83
/*!
   Assignment operator
   \param other Spline used for initialization
   \return *this
*/
QwtSpline &QwtSpline::operator=( const QwtSpline & other )
pixhawk's avatar
pixhawk committed
84 85 86 87 88 89 90 91 92 93 94
{
    *d_data = *other.d_data;
    return *this;
}

//! Destructor
QwtSpline::~QwtSpline()
{
    delete d_data;
}

Bryant's avatar
Bryant committed
95 96 97 98 99 100 101
/*!
   Select the algorithm used for calculating the spline

   \param splineType Spline type
   \sa splineType()
*/
void QwtSpline::setSplineType( SplineType splineType )
pixhawk's avatar
pixhawk committed
102 103 104 105
{
    d_data->splineType = splineType;
}

Bryant's avatar
Bryant committed
106 107 108 109
/*!
   \return the spline type
   \sa setSplineType()
*/
pixhawk's avatar
pixhawk committed
110 111 112 113 114 115 116 117 118 119
QwtSpline::SplineType QwtSpline::splineType() const
{
    return d_data->splineType;
}

/*!
  \brief Calculate the spline coefficients

  Depending on the value of \a periodic, this function
  will determine the coefficients for a natural or a periodic
120 121
  spline and store them internally.

Bryant's avatar
Bryant committed
122
  \param points Points
pixhawk's avatar
pixhawk committed
123 124
  \return true if successful
  \warning The sequence of x (but not y) values has to be strictly monotone
Bryant's avatar
Bryant committed
125
           increasing, which means <code>points[i].x() < points[i+1].x()</code>.
pixhawk's avatar
pixhawk committed
126 127
       If this is not the case, the function will return false
*/
Bryant's avatar
Bryant committed
128
bool QwtSpline::setPoints( const QPolygonF& points )
pixhawk's avatar
pixhawk committed
129 130
{
    const int size = points.size();
Bryant's avatar
Bryant committed
131 132
    if ( size <= 2 )
    {
pixhawk's avatar
pixhawk committed
133 134 135 136 137
        reset();
        return false;
    }

    d_data->points = points;
138

Bryant's avatar
Bryant committed
139 140 141
    d_data->a.resize( size - 1 );
    d_data->b.resize( size - 1 );
    d_data->c.resize( size - 1 );
pixhawk's avatar
pixhawk committed
142 143 144

    bool ok;
    if ( d_data->splineType == Periodic )
Bryant's avatar
Bryant committed
145
        ok = buildPeriodicSpline( points );
pixhawk's avatar
pixhawk committed
146
    else
Bryant's avatar
Bryant committed
147
        ok = buildNaturalSpline( points );
pixhawk's avatar
pixhawk committed
148

Bryant's avatar
Bryant committed
149
    if ( !ok )
pixhawk's avatar
pixhawk committed
150 151 152 153 154 155
        reset();

    return ok;
}

/*!
Bryant's avatar
Bryant committed
156
   \return Points, that have been by setPoints()
pixhawk's avatar
pixhawk committed
157 158 159 160 161 162
*/
QPolygonF QwtSpline::points() const
{
    return d_data->points;
}

Bryant's avatar
Bryant committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
//! \return A coefficients
const QVector<double> &QwtSpline::coefficientsA() const
{
    return d_data->a;
}

//! \return B coefficients
const QVector<double> &QwtSpline::coefficientsB() const
{
    return d_data->b;
}

//! \return C coefficients
const QVector<double> &QwtSpline::coefficientsC() const
{
    return d_data->c;
}

pixhawk's avatar
pixhawk committed
181 182 183 184

//! Free allocated memory and set size to 0
void QwtSpline::reset()
{
Bryant's avatar
Bryant committed
185 186 187 188
    d_data->a.resize( 0 );
    d_data->b.resize( 0 );
    d_data->c.resize( 0 );
    d_data->points.resize( 0 );
pixhawk's avatar
pixhawk committed
189 190 191 192 193 194 195 196 197
}

//! True if valid
bool QwtSpline::isValid() const
{
    return d_data->a.size() > 0;
}

/*!
198
  Calculate the interpolated function value corresponding
pixhawk's avatar
pixhawk committed
199
  to a given argument x.
Bryant's avatar
Bryant committed
200 201 202

  \param x Coordinate
  \return Interpolated coordinate
pixhawk's avatar
pixhawk committed
203
*/
Bryant's avatar
Bryant committed
204
double QwtSpline::value( double x ) const
pixhawk's avatar
pixhawk committed
205
{
Bryant's avatar
Bryant committed
206
    if ( d_data->a.size() == 0 )
pixhawk's avatar
pixhawk committed
207 208
        return 0.0;

Bryant's avatar
Bryant committed
209
    const int i = lookup( x, d_data->points );
pixhawk's avatar
pixhawk committed
210 211

    const double delta = x - d_data->points[i].x();
Bryant's avatar
Bryant committed
212 213
    return( ( ( ( d_data->a[i] * delta ) + d_data->b[i] )
        * delta + d_data->c[i] ) * delta + d_data->points[i].y() );
pixhawk's avatar
pixhawk committed
214 215 216 217 218 219
}

/*!
  \brief Determines the coefficients for a natural spline
  \return true if successful
*/
Bryant's avatar
Bryant committed
220
bool QwtSpline::buildNaturalSpline( const QPolygonF &points )
pixhawk's avatar
pixhawk committed
221 222
{
    int i;
223

pixhawk's avatar
pixhawk committed
224 225 226 227 228 229 230 231 232
    const QPointF *p = points.data();
    const int size = points.size();

    double *a = d_data->a.data();
    double *b = d_data->b.data();
    double *c = d_data->c.data();

    //  set up tridiagonal equation system; use coefficient
    //  vectors as temporary buffers
Bryant's avatar
Bryant committed
233 234 235
    QVector<double> h( size - 1 );
    for ( i = 0; i < size - 1; i++ )
    {
pixhawk's avatar
pixhawk committed
236
        h[i] = p[i+1].x() - p[i].x();
Bryant's avatar
Bryant committed
237
        if ( h[i] <= 0 )
pixhawk's avatar
pixhawk committed
238 239
            return false;
    }
240

Bryant's avatar
Bryant committed
241 242 243 244
    QVector<double> d( size - 1 );
    double dy1 = ( p[1].y() - p[0].y() ) / h[0];
    for ( i = 1; i < size - 1; i++ )
    {
pixhawk's avatar
pixhawk committed
245
        b[i] = c[i] = h[i];
Bryant's avatar
Bryant committed
246
        a[i] = 2.0 * ( h[i-1] + h[i] );
pixhawk's avatar
pixhawk committed
247

Bryant's avatar
Bryant committed
248 249
        const double dy2 = ( p[i+1].y() - p[i].y() ) / h[i];
        d[i] = 6.0 * ( dy1 - dy2 );
pixhawk's avatar
pixhawk committed
250 251 252 253 254 255
        dy1 = dy2;
    }

    //
    // solve it
    //
256

pixhawk's avatar
pixhawk committed
257
    // L-U Factorization
Bryant's avatar
Bryant committed
258 259
    for ( i = 1; i < size - 2; i++ )
    {
pixhawk's avatar
pixhawk committed
260
        c[i] /= a[i];
261
        a[i+1] -= b[i] * c[i];
pixhawk's avatar
pixhawk committed
262 263 264
    }

    // forward elimination
Bryant's avatar
Bryant committed
265
    QVector<double> s( size );
pixhawk's avatar
pixhawk committed
266
    s[1] = d[1];
Bryant's avatar
Bryant committed
267
    for ( i = 2; i < size - 1; i++ )
268 269
        s[i] = d[i] - c[i-1] * s[i-1];

pixhawk's avatar
pixhawk committed
270 271
    // backward elimination
    s[size - 2] = - s[size - 2] / a[size - 2];
Bryant's avatar
Bryant committed
272 273
    for ( i = size - 3; i > 0; i-- )
        s[i] = - ( s[i] + b[i] * s[i+1] ) / a[i];
pixhawk's avatar
pixhawk committed
274 275 276 277 278
    s[size - 1] = s[0] = 0.0;

    //
    // Finally, determine the spline coefficients
    //
Bryant's avatar
Bryant committed
279 280 281
    for ( i = 0; i < size - 1; i++ )
    {
        a[i] = ( s[i+1] - s[i] ) / ( 6.0 * h[i] );
pixhawk's avatar
pixhawk committed
282
        b[i] = 0.5 * s[i];
283
        c[i] = ( p[i+1].y() - p[i].y() ) / h[i]
Bryant's avatar
Bryant committed
284
            - ( s[i+1] + 2.0 * s[i] ) * h[i] / 6.0;
pixhawk's avatar
pixhawk committed
285 286 287 288 289 290 291 292 293
    }

    return true;
}

/*!
  \brief Determines the coefficients for a periodic spline
  \return true if successful
*/
Bryant's avatar
Bryant committed
294
bool QwtSpline::buildPeriodicSpline( const QPolygonF &points )
pixhawk's avatar
pixhawk committed
295 296
{
    int i;
297

pixhawk's avatar
pixhawk committed
298 299 300 301 302 303 304
    const QPointF *p = points.data();
    const int size = points.size();

    double *a = d_data->a.data();
    double *b = d_data->b.data();
    double *c = d_data->c.data();

Bryant's avatar
Bryant committed
305 306 307
    QVector<double> d( size - 1 );
    QVector<double> h( size - 1 );
    QVector<double> s( size );
308

pixhawk's avatar
pixhawk committed
309 310 311 312
    //
    //  setup equation system; use coefficient
    //  vectors as temporary buffers
    //
Bryant's avatar
Bryant committed
313 314
    for ( i = 0; i < size - 1; i++ )
    {
pixhawk's avatar
pixhawk committed
315
        h[i] = p[i+1].x() - p[i].x();
Bryant's avatar
Bryant committed
316
        if ( h[i] <= 0.0 )
pixhawk's avatar
pixhawk committed
317 318
            return false;
    }
319

pixhawk's avatar
pixhawk committed
320 321
    const int imax = size - 2;
    double htmp = h[imax];
Bryant's avatar
Bryant committed
322 323 324
    double dy1 = ( p[0].y() - p[imax].y() ) / htmp;
    for ( i = 0; i <= imax; i++ )
    {
pixhawk's avatar
pixhawk committed
325
        b[i] = c[i] = h[i];
Bryant's avatar
Bryant committed
326 327 328
        a[i] = 2.0 * ( htmp + h[i] );
        const double dy2 = ( p[i+1].y() - p[i].y() ) / h[i];
        d[i] = 6.0 * ( dy1 - dy2 );
pixhawk's avatar
pixhawk committed
329 330 331 332 333 334 335
        dy1 = dy2;
        htmp = h[i];
    }

    //
    // solve it
    //
336

pixhawk's avatar
pixhawk committed
337
    // L-U Factorization
Bryant's avatar
Bryant committed
338
    a[0] = qSqrt( a[0] );
pixhawk's avatar
pixhawk committed
339 340 341
    c[0] = h[imax] / a[0];
    double sum = 0;

Bryant's avatar
Bryant committed
342 343
    for ( i = 0; i < imax - 1; i++ )
    {
pixhawk's avatar
pixhawk committed
344
        b[i] /= a[i];
Bryant's avatar
Bryant committed
345
        if ( i > 0 )
346
            c[i] = - c[i-1] * b[i-1] / a[i];
Bryant's avatar
Bryant committed
347 348
        a[i+1] = qSqrt( a[i+1] - qwtSqr( b[i] ) );
        sum += qwtSqr( c[i] );
pixhawk's avatar
pixhawk committed
349
    }
Bryant's avatar
Bryant committed
350 351
    b[imax-1] = ( b[imax-1] - c[imax-2] * b[imax-2] ) / a[imax-1];
    a[imax] = qSqrt( a[imax] - qwtSqr( b[imax-1] ) - sum );
352

pixhawk's avatar
pixhawk committed
353 354 355 356

    // forward elimination
    s[0] = d[0] / a[0];
    sum = 0;
Bryant's avatar
Bryant committed
357 358 359
    for ( i = 1; i < imax; i++ )
    {
        s[i] = ( d[i] - b[i-1] * s[i-1] ) / a[i];
pixhawk's avatar
pixhawk committed
360 361
        sum += c[i-1] * s[i-1];
    }
Bryant's avatar
Bryant committed
362
    s[imax] = ( d[imax] - b[imax-1] * s[imax-1] - sum ) / a[imax];
363 364


pixhawk's avatar
pixhawk committed
365 366
    // backward elimination
    s[imax] = - s[imax] / a[imax];
Bryant's avatar
Bryant committed
367 368 369
    s[imax-1] = -( s[imax-1] + b[imax-1] * s[imax] ) / a[imax-1];
    for ( i = imax - 2; i >= 0; i-- )
        s[i] = - ( s[i] + b[i] * s[i+1] + c[i] * s[imax] ) / a[i];
pixhawk's avatar
pixhawk committed
370 371 372 373 374

    //
    // Finally, determine the spline coefficients
    //
    s[size-1] = s[0];
Bryant's avatar
Bryant committed
375 376 377
    for ( i = 0; i < size - 1; i++ )
    {
        a[i] = ( s[i+1] - s[i] ) / ( 6.0 * h[i] );
pixhawk's avatar
pixhawk committed
378
        b[i] = 0.5 * s[i];
379
        c[i] = ( p[i+1].y() - p[i].y() )
Bryant's avatar
Bryant committed
380
            / h[i] - ( s[i+1] + 2.0 * s[i] ) * h[i] / 6.0;
pixhawk's avatar
pixhawk committed
381 382 383 384
    }

    return true;
}