qwt_spline.h 2.38 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
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#ifndef QWT_SPLINE_H
#define QWT_SPLINE_H

#include "qwt_global.h"
Bryant's avatar
Bryant committed
14
15
#include <qpolygon.h>
#include <qvector.h>
pixhawk's avatar
pixhawk committed
16
17
18
19
20
21

/*!
  \brief A class for spline interpolation

  The QwtSpline class is used for cubical spline interpolation.
  Two types of splines, natural and periodic, are supported.
22

pixhawk's avatar
pixhawk committed
23
24
  \par Usage:
  <ol>
25
  <li>First call setPoints() to determine the spline coefficients
pixhawk's avatar
pixhawk committed
26
27
      for a tabulated function y(x).
  <li>After the coefficients have been set up, the interpolated
28
      function value for an argument x can be determined by calling
pixhawk's avatar
pixhawk committed
29
30
31
32
33
34
35
36
37
38
      QwtSpline::value().
  </ol>

  \par Example:
  \code
#include <qwt_spline.h>

QPolygonF interpolate(const QPolygonF& points, int numValues)
{
    QwtSpline spline;
39
    if ( !spline.setPoints(points) )
pixhawk's avatar
pixhawk committed
40
41
42
43
        return points;

    QPolygonF interpolatedPoints(numValues);

44
    const double delta =
pixhawk's avatar
pixhawk committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
        (points[numPoints - 1].x() - points[0].x()) / (points.size() - 1);
    for(i = 0; i < points.size(); i++)  / interpolate
    {
        const double x = points[0].x() + i * delta;
        interpolatedPoints[i].setX(x);
        interpolatedPoints[i].setY(spline.value(x));
    }
    return interpolatedPoints;
}
  \endcode
*/

class QWT_EXPORT QwtSpline
{
public:
Bryant's avatar
Bryant committed
60
61
62
63
    //! Spline type
    enum SplineType
    {
        //! A natural spline
pixhawk's avatar
pixhawk committed
64
        Natural,
Bryant's avatar
Bryant committed
65
66

        //! A periodic spline
pixhawk's avatar
pixhawk committed
67
68
69
70
71
72
73
74
75
76
        Periodic
    };

    QwtSpline();
    QwtSpline( const QwtSpline & );

    ~QwtSpline();

    QwtSpline &operator=( const QwtSpline & );

Bryant's avatar
Bryant committed
77
    void setSplineType( SplineType );
pixhawk's avatar
pixhawk committed
78
79
    SplineType splineType() const;

Bryant's avatar
Bryant committed
80
    bool setPoints( const QPolygonF& points );
pixhawk's avatar
pixhawk committed
81
82
83
84
85
    QPolygonF points() const;

    void reset();

    bool isValid() const;
Bryant's avatar
Bryant committed
86
    double value( double x ) const;
pixhawk's avatar
pixhawk committed
87

Bryant's avatar
Bryant committed
88
89
90
    const QVector<double> &coefficientsA() const;
    const QVector<double> &coefficientsB() const;
    const QVector<double> &coefficientsC() const;
pixhawk's avatar
pixhawk committed
91

Bryant's avatar
Bryant committed
92
93
94
protected:
    bool buildNaturalSpline( const QPolygonF & );
    bool buildPeriodicSpline( const QPolygonF & );
pixhawk's avatar
pixhawk committed
95

Bryant's avatar
Bryant committed
96
private:
pixhawk's avatar
pixhawk committed
97
98
99
100
101
    class PrivateData;
    PrivateData *d_data;
};

#endif