Skip to content
qwt_spline.h 2.38 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
pixhawk's avatar
pixhawk committed
 * 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
#include <qpolygon.h>
#include <qvector.h>
pixhawk's avatar
pixhawk committed

/*!
  \brief A class for spline interpolation

  The QwtSpline class is used for cubical spline interpolation.
  Two types of splines, natural and periodic, are supported.
pixhawk's avatar
pixhawk committed
  \par Usage:
  <ol>
  <li>First call setPoints() to determine the spline coefficients
pixhawk's avatar
pixhawk committed
      for a tabulated function y(x).
  <li>After the coefficients have been set up, the interpolated
      function value for an argument x can be determined by calling
pixhawk's avatar
pixhawk committed
      QwtSpline::value().
  </ol>

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

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

    QPolygonF interpolatedPoints(numValues);

    const double delta =
pixhawk's avatar
pixhawk committed
        (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
    //! Spline type
    enum SplineType
    {
        //! A natural spline
pixhawk's avatar
pixhawk committed
        Natural,
Bryant's avatar
Bryant committed

        //! A periodic spline
pixhawk's avatar
pixhawk committed
        Periodic
    };

    QwtSpline();
    QwtSpline( const QwtSpline & );

    ~QwtSpline();

    QwtSpline &operator=( const QwtSpline & );

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

Bryant's avatar
Bryant committed
    bool setPoints( const QPolygonF& points );
pixhawk's avatar
pixhawk committed
    QPolygonF points() const;

    void reset();

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

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

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

Bryant's avatar
Bryant committed
private:
pixhawk's avatar
pixhawk committed
    class PrivateData;
    PrivateData *d_data;
};

#endif