| 1 | /******************************************************************************
|
|---|
| 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 |
|
|---|
| 10 | #ifndef QWT_SPLINE_POLYNOMIAL_H
|
|---|
| 11 | #define QWT_SPLINE_POLYNOMIAL_H
|
|---|
| 12 |
|
|---|
| 13 | #include "qwt_global.h"
|
|---|
| 14 |
|
|---|
| 15 | #include <qpoint.h>
|
|---|
| 16 | #include <qmetatype.h>
|
|---|
| 17 |
|
|---|
| 18 | /*!
|
|---|
| 19 | \brief A cubic polynomial without constant term
|
|---|
| 20 |
|
|---|
| 21 | QwtSplinePolynomial is a 3rd degree polynomial
|
|---|
| 22 | of the form: y = c3 * x³ + c2 * x² + c1 * x;
|
|---|
| 23 |
|
|---|
| 24 | QwtSplinePolynomial is usually used in combination with polygon
|
|---|
| 25 | interpolation, where it is not necessary to store a constant term ( c0 ),
|
|---|
| 26 | as the translation is known from the corresponding polygon points.
|
|---|
| 27 |
|
|---|
| 28 | \sa QwtSplineC1
|
|---|
| 29 | */
|
|---|
| 30 | class QWT_EXPORT QwtSplinePolynomial
|
|---|
| 31 | {
|
|---|
| 32 | public:
|
|---|
| 33 | QwtSplinePolynomial( double c3 = 0.0, double c2 = 0.0, double c1 = 0.0 );
|
|---|
| 34 |
|
|---|
| 35 | bool operator==( const QwtSplinePolynomial& ) const;
|
|---|
| 36 | bool operator!=( const QwtSplinePolynomial& ) const;
|
|---|
| 37 |
|
|---|
| 38 | double valueAt( double x ) const;
|
|---|
| 39 | double slopeAt( double x ) const;
|
|---|
| 40 | double curvatureAt( double x ) const;
|
|---|
| 41 |
|
|---|
| 42 | static QwtSplinePolynomial fromSlopes(
|
|---|
| 43 | const QPointF& p1, double m1,
|
|---|
| 44 | const QPointF& p2, double m2 );
|
|---|
| 45 |
|
|---|
| 46 | static QwtSplinePolynomial fromSlopes(
|
|---|
| 47 | double x, double y, double m1, double m2 );
|
|---|
| 48 |
|
|---|
| 49 | static QwtSplinePolynomial fromCurvatures(
|
|---|
| 50 | const QPointF& p1, double cv1,
|
|---|
| 51 | const QPointF& p2, double cv2 );
|
|---|
| 52 |
|
|---|
| 53 | static QwtSplinePolynomial fromCurvatures(
|
|---|
| 54 | double dx, double dy, double cv1, double cv2 );
|
|---|
| 55 |
|
|---|
| 56 | public:
|
|---|
| 57 | //! coefficient of the cubic summand
|
|---|
| 58 | double c3;
|
|---|
| 59 |
|
|---|
| 60 | //! coefficient of the quadratic summand
|
|---|
| 61 | double c2;
|
|---|
| 62 |
|
|---|
| 63 | //! coefficient of the linear summand
|
|---|
| 64 | double c1;
|
|---|
| 65 | };
|
|---|
| 66 |
|
|---|
| 67 | Q_DECLARE_TYPEINFO( QwtSplinePolynomial, Q_MOVABLE_TYPE );
|
|---|
| 68 | Q_DECLARE_METATYPE( QwtSplinePolynomial )
|
|---|
| 69 |
|
|---|
| 70 | /*!
|
|---|
| 71 | \brief Constructor
|
|---|
| 72 |
|
|---|
| 73 | \param a3 Coefficient of the cubic summand
|
|---|
| 74 | \param a2 Coefficient of the quadratic summand
|
|---|
| 75 | \param a1 Coefficient of the linear summand
|
|---|
| 76 | */
|
|---|
| 77 | inline QwtSplinePolynomial::QwtSplinePolynomial( double a3, double a2, double a1 )
|
|---|
| 78 | : c3( a3 )
|
|---|
| 79 | , c2( a2 )
|
|---|
| 80 | , c1( a1 )
|
|---|
| 81 | {
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /*!
|
|---|
| 85 | \param other Other polynomial
|
|---|
| 86 | \return true, when both polynomials have the same coefficients
|
|---|
| 87 | */
|
|---|
| 88 | inline bool QwtSplinePolynomial::operator==( const QwtSplinePolynomial& other ) const
|
|---|
| 89 | {
|
|---|
| 90 | return ( c3 == other.c3 ) && ( c2 == other.c2 ) && ( c1 == other.c1 );
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /*!
|
|---|
| 94 | \param other Other polynomial
|
|---|
| 95 | \return true, when the polynomials have different coefficients
|
|---|
| 96 | */
|
|---|
| 97 | inline bool QwtSplinePolynomial::operator!=( const QwtSplinePolynomial& other ) const
|
|---|
| 98 | {
|
|---|
| 99 | return ( !( *this == other ) );
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /*!
|
|---|
| 103 | Calculate the value of a polynomial for a given x
|
|---|
| 104 |
|
|---|
| 105 | \param x Parameter
|
|---|
| 106 | \return Value at x
|
|---|
| 107 | */
|
|---|
| 108 | inline double QwtSplinePolynomial::valueAt( double x ) const
|
|---|
| 109 | {
|
|---|
| 110 | return ( ( ( c3 * x ) + c2 ) * x + c1 ) * x;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /*!
|
|---|
| 114 | Calculate the value of the first derivate of a polynomial for a given x
|
|---|
| 115 |
|
|---|
| 116 | \param x Parameter
|
|---|
| 117 | \return Slope at x
|
|---|
| 118 | */
|
|---|
| 119 | inline double QwtSplinePolynomial::slopeAt( double x ) const
|
|---|
| 120 | {
|
|---|
| 121 | return ( 3.0 * c3 * x + 2.0 * c2 ) * x + c1;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /*!
|
|---|
| 125 | Calculate the value of the second derivate of a polynomial for a given x
|
|---|
| 126 |
|
|---|
| 127 | \param x Parameter
|
|---|
| 128 | \return Curvature at x
|
|---|
| 129 | */
|
|---|
| 130 | inline double QwtSplinePolynomial::curvatureAt( double x ) const
|
|---|
| 131 | {
|
|---|
| 132 | return 6.0 * c3 * x + 2.0 * c2;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /*!
|
|---|
| 136 | Find the coefficients for the polynomial including 2 points with
|
|---|
| 137 | specific values for the 1st derivates at these points.
|
|---|
| 138 |
|
|---|
| 139 | \param p1 First point
|
|---|
| 140 | \param m1 Value of the first derivate at p1
|
|---|
| 141 | \param p2 Second point
|
|---|
| 142 | \param m2 Value of the first derivate at p2
|
|---|
| 143 |
|
|---|
| 144 | \return Coefficients of the polynomials
|
|---|
| 145 | \note The missing constant term of the polynomial is p1.y()
|
|---|
| 146 | */
|
|---|
| 147 | inline QwtSplinePolynomial QwtSplinePolynomial::fromSlopes(
|
|---|
| 148 | const QPointF& p1, double m1, const QPointF& p2, double m2 )
|
|---|
| 149 | {
|
|---|
| 150 | return fromSlopes( p2.x() - p1.x(), p2.y() - p1.y(), m1, m2 );
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /*!
|
|---|
| 154 | Find the coefficients for the polynomial from the offset between 2 points
|
|---|
| 155 | and specific values for the 1st derivates at these points.
|
|---|
| 156 |
|
|---|
| 157 | \param dx X-offset
|
|---|
| 158 | \param dy Y-offset
|
|---|
| 159 | \param m1 Value of the first derivate at p1
|
|---|
| 160 | \param m2 Value of the first derivate at p2
|
|---|
| 161 |
|
|---|
| 162 | \return Coefficients of the polynomials
|
|---|
| 163 | */
|
|---|
| 164 | inline QwtSplinePolynomial QwtSplinePolynomial::fromSlopes(
|
|---|
| 165 | double dx, double dy, double m1, double m2 )
|
|---|
| 166 | {
|
|---|
| 167 | const double c2 = ( 3.0 * dy / dx - 2 * m1 - m2 ) / dx;
|
|---|
| 168 | const double c3 = ( ( m2 - m1 ) / dx - 2.0 * c2 ) / ( 3.0 * dx );
|
|---|
| 169 |
|
|---|
| 170 | return QwtSplinePolynomial( c3, c2, m1 );
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /*!
|
|---|
| 174 | Find the coefficients for the polynomial including 2 points with
|
|---|
| 175 | specific values for the 2nd derivates at these points.
|
|---|
| 176 |
|
|---|
| 177 | \param p1 First point
|
|---|
| 178 | \param cv1 Value of the second derivate at p1
|
|---|
| 179 | \param p2 Second point
|
|---|
| 180 | \param cv2 Value of the second derivate at p2
|
|---|
| 181 |
|
|---|
| 182 | \return Coefficients of the polynomials
|
|---|
| 183 | \note The missing constant term of the polynomial is p1.y()
|
|---|
| 184 | */
|
|---|
| 185 | inline QwtSplinePolynomial QwtSplinePolynomial::fromCurvatures(
|
|---|
| 186 | const QPointF& p1, double cv1, const QPointF& p2, double cv2 )
|
|---|
| 187 | {
|
|---|
| 188 | return fromCurvatures( p2.x() - p1.x(), p2.y() - p1.y(), cv1, cv2 );
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /*!
|
|---|
| 192 | Find the coefficients for the polynomial from the offset between 2 points
|
|---|
| 193 | and specific values for the 2nd derivates at these points.
|
|---|
| 194 |
|
|---|
| 195 | \param dx X-offset
|
|---|
| 196 | \param dy Y-offset
|
|---|
| 197 | \param cv1 Value of the second derivate at p1
|
|---|
| 198 | \param cv2 Value of the second derivate at p2
|
|---|
| 199 |
|
|---|
| 200 | \return Coefficients of the polynomials
|
|---|
| 201 | */
|
|---|
| 202 | inline QwtSplinePolynomial QwtSplinePolynomial::fromCurvatures(
|
|---|
| 203 | double dx, double dy, double cv1, double cv2 )
|
|---|
| 204 | {
|
|---|
| 205 | const double c3 = ( cv2 - cv1 ) / ( 6.0 * dx );
|
|---|
| 206 | const double c2 = 0.5 * cv1;
|
|---|
| 207 | const double c1 = dy / dx - ( c3 * dx + c2 ) * dx;
|
|---|
| 208 |
|
|---|
| 209 | return QwtSplinePolynomial( c3, c2, c1 );
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | #ifndef QT_NO_DEBUG_STREAM
|
|---|
| 213 |
|
|---|
| 214 | class QDebug;
|
|---|
| 215 | QWT_EXPORT QDebug operator<<( QDebug, const QwtSplinePolynomial& );
|
|---|
| 216 |
|
|---|
| 217 | #endif
|
|---|
| 218 |
|
|---|
| 219 | #endif
|
|---|