qwt_spline.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #include "qwt_spline.h"
  10. #include "qwt_math.h"
  11. class QwtSpline::PrivateData
  12. {
  13. public:
  14. PrivateData():
  15. splineType( QwtSpline::Natural )
  16. {
  17. }
  18. QwtSpline::SplineType splineType;
  19. // coefficient vectors
  20. QVector<double> a;
  21. QVector<double> b;
  22. QVector<double> c;
  23. // control points
  24. QPolygonF points;
  25. };
  26. static int lookup( double x, const QPolygonF &values )
  27. {
  28. #if 0
  29. //qLowerBound/qHigherBound ???
  30. #endif
  31. int i1;
  32. const int size = ( int )values.size();
  33. if ( x <= values[0].x() )
  34. i1 = 0;
  35. else if ( x >= values[size - 2].x() )
  36. i1 = size - 2;
  37. else
  38. {
  39. i1 = 0;
  40. int i2 = size - 2;
  41. int i3 = 0;
  42. while ( i2 - i1 > 1 )
  43. {
  44. i3 = i1 + ( ( i2 - i1 ) >> 1 );
  45. if ( values[i3].x() > x )
  46. i2 = i3;
  47. else
  48. i1 = i3;
  49. }
  50. }
  51. return i1;
  52. }
  53. //! Constructor
  54. QwtSpline::QwtSpline()
  55. {
  56. d_data = new PrivateData;
  57. }
  58. /*!
  59. Copy constructor
  60. \param other Spline used for initilization
  61. */
  62. QwtSpline::QwtSpline( const QwtSpline& other )
  63. {
  64. d_data = new PrivateData( *other.d_data );
  65. }
  66. /*!
  67. Assignment operator
  68. \param other Spline used for initilization
  69. */
  70. QwtSpline &QwtSpline::operator=( const QwtSpline & other )
  71. {
  72. *d_data = *other.d_data;
  73. return *this;
  74. }
  75. //! Destructor
  76. QwtSpline::~QwtSpline()
  77. {
  78. delete d_data;
  79. }
  80. /*!
  81. Select the algorithm used for calculating the spline
  82. \param splineType Spline type
  83. \sa splineType()
  84. */
  85. void QwtSpline::setSplineType( SplineType splineType )
  86. {
  87. d_data->splineType = splineType;
  88. }
  89. /*!
  90. \return the spline type
  91. \sa setSplineType()
  92. */
  93. QwtSpline::SplineType QwtSpline::splineType() const
  94. {
  95. return d_data->splineType;
  96. }
  97. /*!
  98. \brief Calculate the spline coefficients
  99. Depending on the value of \a periodic, this function
  100. will determine the coefficients for a natural or a periodic
  101. spline and store them internally.
  102. \param points Points
  103. \return true if successful
  104. \warning The sequence of x (but not y) values has to be strictly monotone
  105. increasing, which means <code>points[i].x() < points[i+1].x()</code>.
  106. If this is not the case, the function will return false
  107. */
  108. bool QwtSpline::setPoints( const QPolygonF& points )
  109. {
  110. const int size = points.size();
  111. if ( size <= 2 )
  112. {
  113. reset();
  114. return false;
  115. }
  116. d_data->points = points;
  117. d_data->a.resize( size - 1 );
  118. d_data->b.resize( size - 1 );
  119. d_data->c.resize( size - 1 );
  120. bool ok;
  121. if ( d_data->splineType == Periodic )
  122. ok = buildPeriodicSpline( points );
  123. else
  124. ok = buildNaturalSpline( points );
  125. if ( !ok )
  126. reset();
  127. return ok;
  128. }
  129. /*!
  130. Return points passed by setPoints
  131. */
  132. QPolygonF QwtSpline::points() const
  133. {
  134. return d_data->points;
  135. }
  136. //! \return A coefficients
  137. const QVector<double> &QwtSpline::coefficientsA() const
  138. {
  139. return d_data->a;
  140. }
  141. //! \return B coefficients
  142. const QVector<double> &QwtSpline::coefficientsB() const
  143. {
  144. return d_data->b;
  145. }
  146. //! \return C coefficients
  147. const QVector<double> &QwtSpline::coefficientsC() const
  148. {
  149. return d_data->c;
  150. }
  151. //! Free allocated memory and set size to 0
  152. void QwtSpline::reset()
  153. {
  154. d_data->a.resize( 0 );
  155. d_data->b.resize( 0 );
  156. d_data->c.resize( 0 );
  157. d_data->points.resize( 0 );
  158. }
  159. //! True if valid
  160. bool QwtSpline::isValid() const
  161. {
  162. return d_data->a.size() > 0;
  163. }
  164. /*!
  165. Calculate the interpolated function value corresponding
  166. to a given argument x.
  167. */
  168. double QwtSpline::value( double x ) const
  169. {
  170. if ( d_data->a.size() == 0 )
  171. return 0.0;
  172. const int i = lookup( x, d_data->points );
  173. const double delta = x - d_data->points[i].x();
  174. return( ( ( ( d_data->a[i] * delta ) + d_data->b[i] )
  175. * delta + d_data->c[i] ) * delta + d_data->points[i].y() );
  176. }
  177. /*!
  178. \brief Determines the coefficients for a natural spline
  179. \return true if successful
  180. */
  181. bool QwtSpline::buildNaturalSpline( const QPolygonF &points )
  182. {
  183. int i;
  184. const QPointF *p = points.data();
  185. const int size = points.size();
  186. double *a = d_data->a.data();
  187. double *b = d_data->b.data();
  188. double *c = d_data->c.data();
  189. // set up tridiagonal equation system; use coefficient
  190. // vectors as temporary buffers
  191. QVector<double> h( size - 1 );
  192. for ( i = 0; i < size - 1; i++ )
  193. {
  194. h[i] = p[i+1].x() - p[i].x();
  195. if ( h[i] <= 0 )
  196. return false;
  197. }
  198. QVector<double> d( size - 1 );
  199. double dy1 = ( p[1].y() - p[0].y() ) / h[0];
  200. for ( i = 1; i < size - 1; i++ )
  201. {
  202. b[i] = c[i] = h[i];
  203. a[i] = 2.0 * ( h[i-1] + h[i] );
  204. const double dy2 = ( p[i+1].y() - p[i].y() ) / h[i];
  205. d[i] = 6.0 * ( dy1 - dy2 );
  206. dy1 = dy2;
  207. }
  208. //
  209. // solve it
  210. //
  211. // L-U Factorization
  212. for ( i = 1; i < size - 2; i++ )
  213. {
  214. c[i] /= a[i];
  215. a[i+1] -= b[i] * c[i];
  216. }
  217. // forward elimination
  218. QVector<double> s( size );
  219. s[1] = d[1];
  220. for ( i = 2; i < size - 1; i++ )
  221. s[i] = d[i] - c[i-1] * s[i-1];
  222. // backward elimination
  223. s[size - 2] = - s[size - 2] / a[size - 2];
  224. for ( i = size - 3; i > 0; i-- )
  225. s[i] = - ( s[i] + b[i] * s[i+1] ) / a[i];
  226. s[size - 1] = s[0] = 0.0;
  227. //
  228. // Finally, determine the spline coefficients
  229. //
  230. for ( i = 0; i < size - 1; i++ )
  231. {
  232. a[i] = ( s[i+1] - s[i] ) / ( 6.0 * h[i] );
  233. b[i] = 0.5 * s[i];
  234. c[i] = ( p[i+1].y() - p[i].y() ) / h[i]
  235. - ( s[i+1] + 2.0 * s[i] ) * h[i] / 6.0;
  236. }
  237. return true;
  238. }
  239. /*!
  240. \brief Determines the coefficients for a periodic spline
  241. \return true if successful
  242. */
  243. bool QwtSpline::buildPeriodicSpline( const QPolygonF &points )
  244. {
  245. int i;
  246. const QPointF *p = points.data();
  247. const int size = points.size();
  248. double *a = d_data->a.data();
  249. double *b = d_data->b.data();
  250. double *c = d_data->c.data();
  251. QVector<double> d( size - 1 );
  252. QVector<double> h( size - 1 );
  253. QVector<double> s( size );
  254. //
  255. // setup equation system; use coefficient
  256. // vectors as temporary buffers
  257. //
  258. for ( i = 0; i < size - 1; i++ )
  259. {
  260. h[i] = p[i+1].x() - p[i].x();
  261. if ( h[i] <= 0.0 )
  262. return false;
  263. }
  264. const int imax = size - 2;
  265. double htmp = h[imax];
  266. double dy1 = ( p[0].y() - p[imax].y() ) / htmp;
  267. for ( i = 0; i <= imax; i++ )
  268. {
  269. b[i] = c[i] = h[i];
  270. a[i] = 2.0 * ( htmp + h[i] );
  271. const double dy2 = ( p[i+1].y() - p[i].y() ) / h[i];
  272. d[i] = 6.0 * ( dy1 - dy2 );
  273. dy1 = dy2;
  274. htmp = h[i];
  275. }
  276. //
  277. // solve it
  278. //
  279. // L-U Factorization
  280. a[0] = qSqrt( a[0] );
  281. c[0] = h[imax] / a[0];
  282. double sum = 0;
  283. for ( i = 0; i < imax - 1; i++ )
  284. {
  285. b[i] /= a[i];
  286. if ( i > 0 )
  287. c[i] = - c[i-1] * b[i-1] / a[i];
  288. a[i+1] = qSqrt( a[i+1] - qwtSqr( b[i] ) );
  289. sum += qwtSqr( c[i] );
  290. }
  291. b[imax-1] = ( b[imax-1] - c[imax-2] * b[imax-2] ) / a[imax-1];
  292. a[imax] = qSqrt( a[imax] - qwtSqr( b[imax-1] ) - sum );
  293. // forward elimination
  294. s[0] = d[0] / a[0];
  295. sum = 0;
  296. for ( i = 1; i < imax; i++ )
  297. {
  298. s[i] = ( d[i] - b[i-1] * s[i-1] ) / a[i];
  299. sum += c[i-1] * s[i-1];
  300. }
  301. s[imax] = ( d[imax] - b[imax-1] * s[imax-1] - sum ) / a[imax];
  302. // backward elimination
  303. s[imax] = - s[imax] / a[imax];
  304. s[imax-1] = -( s[imax-1] + b[imax-1] * s[imax] ) / a[imax-1];
  305. for ( i = imax - 2; i >= 0; i-- )
  306. s[i] = - ( s[i] + b[i] * s[i+1] + c[i] * s[imax] ) / a[i];
  307. //
  308. // Finally, determine the spline coefficients
  309. //
  310. s[size-1] = s[0];
  311. for ( i = 0; i < size - 1; i++ )
  312. {
  313. a[i] = ( s[i+1] - s[i] ) / ( 6.0 * h[i] );
  314. b[i] = 0.5 * s[i];
  315. c[i] = ( p[i+1].y() - p[i].y() )
  316. / h[i] - ( s[i+1] + 2.0 * s[i] ) * h[i] / 6.0;
  317. }
  318. return true;
  319. }