qwt_spline.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
  2. * Qwt Widget Library
  3. * Copyright (C) 1997 Josef Wilgen
  4. * Copyright (C) 2002 Uwe Rathmann
  5. *
  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. *****************************************************************************/
  9. #ifndef QWT_SPLINE_H
  10. #define QWT_SPLINE_H
  11. #include "qwt_global.h"
  12. #include <qpolygon.h>
  13. #include <qvector.h>
  14. /*!
  15. \brief A class for spline interpolation
  16. The QwtSpline class is used for cubical spline interpolation.
  17. Two types of splines, natural and periodic, are supported.
  18. \par Usage:
  19. <ol>
  20. <li>First call setPoints() to determine the spline coefficients
  21. for a tabulated function y(x).
  22. <li>After the coefficients have been set up, the interpolated
  23. function value for an argument x can be determined by calling
  24. QwtSpline::value().
  25. </ol>
  26. \par Example:
  27. \code
  28. #include <qwt_spline.h>
  29. QPolygonF interpolate(const QPolygonF& points, int numValues)
  30. {
  31. QwtSpline spline;
  32. if ( !spline.setPoints(points) )
  33. return points;
  34. QPolygonF interpolatedPoints(numValues);
  35. const double delta =
  36. (points[numPoints - 1].x() - points[0].x()) / (points.size() - 1);
  37. for(i = 0; i < points.size(); i++) / interpolate
  38. {
  39. const double x = points[0].x() + i * delta;
  40. interpolatedPoints[i].setX(x);
  41. interpolatedPoints[i].setY(spline.value(x));
  42. }
  43. return interpolatedPoints;
  44. }
  45. \endcode
  46. */
  47. class QWT_EXPORT QwtSpline
  48. {
  49. public:
  50. //! Spline type
  51. enum SplineType
  52. {
  53. //! A natural spline
  54. Natural,
  55. //! A periodic spline
  56. Periodic
  57. };
  58. QwtSpline();
  59. QwtSpline( const QwtSpline & );
  60. ~QwtSpline();
  61. QwtSpline &operator=( const QwtSpline & );
  62. void setSplineType( SplineType );
  63. SplineType splineType() const;
  64. bool setPoints( const QPolygonF& points );
  65. QPolygonF points() const;
  66. void reset();
  67. bool isValid() const;
  68. double value( double x ) const;
  69. const QVector<double> &coefficientsA() const;
  70. const QVector<double> &coefficientsB() const;
  71. const QVector<double> &coefficientsC() const;
  72. protected:
  73. bool buildNaturalSpline( const QPolygonF & );
  74. bool buildPeriodicSpline( const QPolygonF & );
  75. private:
  76. class PrivateData;
  77. PrivateData *d_data;
  78. };
  79. #endif