qwt_spline.cpp 8.37 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 17
 * 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"
#include "qwt_array.h"

class QwtSpline::PrivateData
{
public:
    PrivateData():
18
        splineType(QwtSpline::Natural) {
pixhawk's avatar
pixhawk committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    }

    QwtSpline::SplineType splineType;

    // coefficient vectors
    QwtArray<double> a;
    QwtArray<double> b;
    QwtArray<double> c;

    // control points
#if QT_VERSION < 0x040000
    QwtArray<QwtDoublePoint> points;
#else
    QPolygonF points;
#endif
};

#if QT_VERSION < 0x040000
static int lookup(double x, const QwtArray<QwtDoublePoint> &values)
#else
static int lookup(double x, const QPolygonF &values)
#endif
{
#if 0
//qLowerBiund/qHigherBound ???
#endif
    int i1;
    const int size = (int)values.size();
47

pixhawk's avatar
pixhawk committed
48
    if (x <= values[0].x())
49
        i1 = 0;
pixhawk's avatar
pixhawk committed
50
    else if (x >= values[size - 2].x())
51 52
        i1 = size - 2;
    else {
pixhawk's avatar
pixhawk committed
53 54 55 56
        i1 = 0;
        int i2 = size - 2;
        int i3 = 0;

57
        while ( i2 - i1 > 1 ) {
pixhawk's avatar
pixhawk committed
58 59 60
            i3 = i1 + ((i2 - i1) >> 1);

            if (values[i3].x() > x)
61
                i2 = i3;
pixhawk's avatar
pixhawk committed
62
            else
63
                i1 = i3;
pixhawk's avatar
pixhawk committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        }
    }
    return i1;
}

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

QwtSpline::QwtSpline(const QwtSpline& other)
{
    d_data = new PrivateData(*other.d_data);
}

QwtSpline &QwtSpline::operator=( const QwtSpline &other)
{
    *d_data = *other.d_data;
    return *this;
}

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

void QwtSpline::setSplineType(SplineType splineType)
{
    d_data->splineType = splineType;
}

QwtSpline::SplineType QwtSpline::splineType() const
{
    return d_data->splineType;
}

//! Determine the function table index corresponding to a value x

/*!
  \brief Calculate the spline coefficients

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

pixhawk's avatar
pixhawk committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  \param x
  \param y points
  \param size number of points
  \param periodic if true, calculate periodic spline
  \return true if successful
  \warning The sequence of x (but not y) values has to be strictly monotone
           increasing, which means <code>x[0] < x[1] < .... < x[n-1]</code>.
       If this is not the case, the function will return false
*/
#if QT_VERSION < 0x040000
bool QwtSpline::setPoints(const QwtArray<QwtDoublePoint>& points)
#else
bool QwtSpline::setPoints(const QPolygonF& points)
#endif
{
    const int size = points.size();
127
    if (size <= 2) {
pixhawk's avatar
pixhawk committed
128 129 130 131 132 133 134 135 136
        reset();
        return false;
    }

#if QT_VERSION < 0x040000
    d_data->points = points.copy(); // Qt3: deep copy
#else
    d_data->points = points;
#endif
137

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

    bool ok;
    if ( d_data->splineType == Periodic )
        ok = buildPeriodicSpline(points);
    else
        ok = buildNaturalSpline(points);

148
    if (!ok)
pixhawk's avatar
pixhawk committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        reset();

    return ok;
}

/*!
   Return points passed by setPoints
*/
#if QT_VERSION < 0x040000
QwtArray<QwtDoublePoint> QwtSpline::points() const
#else
QPolygonF QwtSpline::points() const
#endif
{
    return d_data->points;
}


//! Free allocated memory and set size to 0
void QwtSpline::reset()
{
    d_data->a.resize(0);
    d_data->b.resize(0);
    d_data->c.resize(0);
    d_data->points.resize(0);
}

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

/*!
183
  Calculate the interpolated function value corresponding
pixhawk's avatar
pixhawk committed
184 185 186 187 188 189 190 191 192 193
  to a given argument x.
*/
double QwtSpline::value(double x) const
{
    if (d_data->a.size() == 0)
        return 0.0;

    const int i = lookup(x, d_data->points);

    const double delta = x - d_data->points[i].x();
194 195
    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
196 197 198 199 200 201 202 203 204 205 206 207 208
}

/*!
  \brief Determines the coefficients for a natural spline
  \return true if successful
*/
#if QT_VERSION < 0x040000
bool QwtSpline::buildNaturalSpline(const QwtArray<QwtDoublePoint> &points)
#else
bool QwtSpline::buildNaturalSpline(const QPolygonF &points)
#endif
{
    int i;
209

pixhawk's avatar
pixhawk committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223
#if QT_VERSION < 0x040000
    const QwtDoublePoint *p = points.data();
#else
    const QPointF *p = points.data();
#endif
    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
    QwtArray<double> h(size-1);
224
    for (i = 0; i < size - 1; i++) {
pixhawk's avatar
pixhawk committed
225 226 227 228
        h[i] = p[i+1].x() - p[i].x();
        if (h[i] <= 0)
            return false;
    }
229

pixhawk's avatar
pixhawk committed
230 231
    QwtArray<double> d(size-1);
    double dy1 = (p[1].y() - p[0].y()) / h[0];
232
    for (i = 1; i < size - 1; i++) {
pixhawk's avatar
pixhawk committed
233 234 235 236 237 238 239 240 241 242 243
        b[i] = c[i] = h[i];
        a[i] = 2.0 * (h[i-1] + h[i]);

        const double dy2 = (p[i+1].y() - p[i].y()) / h[i];
        d[i] = 6.0 * ( dy1 - dy2);
        dy1 = dy2;
    }

    //
    // solve it
    //
244

pixhawk's avatar
pixhawk committed
245
    // L-U Factorization
246
    for(i = 1; i < size - 2; i++) {
pixhawk's avatar
pixhawk committed
247
        c[i] /= a[i];
248
        a[i+1] -= b[i] * c[i];
pixhawk's avatar
pixhawk committed
249 250 251 252 253 254
    }

    // forward elimination
    QwtArray<double> s(size);
    s[1] = d[1];
    for ( i = 2; i < size - 1; i++)
255 256
        s[i] = d[i] - c[i-1] * s[i-1];

pixhawk's avatar
pixhawk committed
257 258 259
    // backward elimination
    s[size - 2] = - s[size - 2] / a[size - 2];
    for (i = size -3; i > 0; i--)
260
        s[i] = - (s[i] + b[i] * s[i+1]) / a[i];
pixhawk's avatar
pixhawk committed
261 262 263 264 265
    s[size - 1] = s[0] = 0.0;

    //
    // Finally, determine the spline coefficients
    //
266
    for (i = 0; i < size - 1; i++) {
pixhawk's avatar
pixhawk committed
267 268
        a[i] = ( s[i+1] - s[i] ) / ( 6.0 * h[i]);
        b[i] = 0.5 * s[i];
269 270
        c[i] = ( p[i+1].y() - p[i].y() ) / h[i]
               - (s[i+1] + 2.0 * s[i] ) * h[i] / 6.0;
pixhawk's avatar
pixhawk committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    }

    return true;
}

/*!
  \brief Determines the coefficients for a periodic spline
  \return true if successful
*/
#if QT_VERSION < 0x040000
bool QwtSpline::buildPeriodicSpline(
    const QwtArray<QwtDoublePoint> &points)
#else
bool QwtSpline::buildPeriodicSpline(const QPolygonF &points)
#endif
{
    int i;
288

pixhawk's avatar
pixhawk committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302
#if QT_VERSION < 0x040000
    const QwtDoublePoint *p = points.data();
#else
    const QPointF *p = points.data();
#endif
    const int size = points.size();

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

    QwtArray<double> d(size-1);
    QwtArray<double> h(size-1);
    QwtArray<double> s(size);
303

pixhawk's avatar
pixhawk committed
304 305 306 307
    //
    //  setup equation system; use coefficient
    //  vectors as temporary buffers
    //
308
    for (i = 0; i < size - 1; i++) {
pixhawk's avatar
pixhawk committed
309 310 311 312
        h[i] = p[i+1].x() - p[i].x();
        if (h[i] <= 0.0)
            return false;
    }
313

pixhawk's avatar
pixhawk committed
314 315 316
    const int imax = size - 2;
    double htmp = h[imax];
    double dy1 = (p[0].y() - p[imax].y()) / htmp;
317
    for (i = 0; i <= imax; i++) {
pixhawk's avatar
pixhawk committed
318 319 320 321 322 323 324 325 326 327 328
        b[i] = c[i] = h[i];
        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);
        dy1 = dy2;
        htmp = h[i];
    }

    //
    // solve it
    //
329

pixhawk's avatar
pixhawk committed
330 331 332 333 334
    // L-U Factorization
    a[0] = sqrt(a[0]);
    c[0] = h[imax] / a[0];
    double sum = 0;

335
    for( i = 0; i < imax - 1; i++) {
pixhawk's avatar
pixhawk committed
336 337
        b[i] /= a[i];
        if (i > 0)
338
            c[i] = - c[i-1] * b[i-1] / a[i];
pixhawk's avatar
pixhawk committed
339 340 341 342 343
        a[i+1] = sqrt( a[i+1] - qwtSqr(b[i]));
        sum += qwtSqr(c[i]);
    }
    b[imax-1] = (b[imax-1] - c[imax-2] * b[imax-2]) / a[imax-1];
    a[imax] = sqrt(a[imax] - qwtSqr(b[imax-1]) - sum);
344

pixhawk's avatar
pixhawk committed
345 346 347 348

    // forward elimination
    s[0] = d[0] / a[0];
    sum = 0;
349
    for( i = 1; i < imax; i++) {
pixhawk's avatar
pixhawk committed
350 351 352 353
        s[i] = (d[i] - b[i-1] * s[i-1]) / a[i];
        sum += c[i-1] * s[i-1];
    }
    s[imax] = (d[imax] - b[imax-1] * s[imax-1] - sum) / a[imax];
354 355


pixhawk's avatar
pixhawk committed
356 357 358 359
    // backward elimination
    s[imax] = - s[imax] / a[imax];
    s[imax-1] = -(s[imax-1] + b[imax-1] * s[imax]) / a[imax-1];
    for (i= imax - 2; i >= 0; i--)
360
        s[i] = - (s[i] + b[i] * s[i+1] + c[i] * s[imax]) / a[i];
pixhawk's avatar
pixhawk committed
361 362 363 364 365

    //
    // Finally, determine the spline coefficients
    //
    s[size-1] = s[0];
366
    for ( i=0; i < size-1; i++) {
pixhawk's avatar
pixhawk committed
367 368
        a[i] = ( s[i+1] - s[i] ) / ( 6.0 * h[i]);
        b[i] = 0.5 * s[i];
369 370
        c[i] = ( p[i+1].y() - p[i].y() )
               / h[i] - (s[i+1] + 2.0 * s[i] ) * h[i] / 6.0;
pixhawk's avatar
pixhawk committed
371 372 373 374
    }

    return true;
}