qwt_curve_fitter.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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_curve_fitter.h"
  10. #include "qwt_math.h"
  11. #include "qwt_spline.h"
  12. #include <qstack.h>
  13. #include <qvector.h>
  14. #if QT_VERSION < 0x040601
  15. #define qFabs(x) ::fabs(x)
  16. #endif
  17. //! Constructor
  18. QwtCurveFitter::QwtCurveFitter()
  19. {
  20. }
  21. //! Destructor
  22. QwtCurveFitter::~QwtCurveFitter()
  23. {
  24. }
  25. class QwtSplineCurveFitter::PrivateData
  26. {
  27. public:
  28. PrivateData():
  29. fitMode( QwtSplineCurveFitter::Auto ),
  30. splineSize( 250 )
  31. {
  32. }
  33. QwtSpline spline;
  34. QwtSplineCurveFitter::FitMode fitMode;
  35. int splineSize;
  36. };
  37. //! Constructor
  38. QwtSplineCurveFitter::QwtSplineCurveFitter()
  39. {
  40. d_data = new PrivateData;
  41. }
  42. //! Destructor
  43. QwtSplineCurveFitter::~QwtSplineCurveFitter()
  44. {
  45. delete d_data;
  46. }
  47. /*!
  48. Select the algorithm used for building the spline
  49. \param mode Mode representing a spline algorithm
  50. \sa fitMode()
  51. */
  52. void QwtSplineCurveFitter::setFitMode( FitMode mode )
  53. {
  54. d_data->fitMode = mode;
  55. }
  56. /*!
  57. \return Mode representing a spline algorithm
  58. \sa setFitMode()
  59. */
  60. QwtSplineCurveFitter::FitMode QwtSplineCurveFitter::fitMode() const
  61. {
  62. return d_data->fitMode;
  63. }
  64. /*!
  65. Assign a spline
  66. \param spline Spline
  67. \sa spline()
  68. */
  69. void QwtSplineCurveFitter::setSpline( const QwtSpline &spline )
  70. {
  71. d_data->spline = spline;
  72. d_data->spline.reset();
  73. }
  74. /*!
  75. \return Spline
  76. \sa setSpline()
  77. */
  78. const QwtSpline &QwtSplineCurveFitter::spline() const
  79. {
  80. return d_data->spline;
  81. }
  82. /*!
  83. \return Spline
  84. \sa setSpline()
  85. */
  86. QwtSpline &QwtSplineCurveFitter::spline()
  87. {
  88. return d_data->spline;
  89. }
  90. /*!
  91. Assign a spline size ( has to be at least 10 points )
  92. \param splineSize Spline size
  93. \sa splineSize()
  94. */
  95. void QwtSplineCurveFitter::setSplineSize( int splineSize )
  96. {
  97. d_data->splineSize = qMax( splineSize, 10 );
  98. }
  99. /*!
  100. \return Spline size
  101. \sa setSplineSize()
  102. */
  103. int QwtSplineCurveFitter::splineSize() const
  104. {
  105. return d_data->splineSize;
  106. }
  107. /*!
  108. Find a curve which has the best fit to a series of data points
  109. \param points Series of data points
  110. \return Curve points
  111. */
  112. QPolygonF QwtSplineCurveFitter::fitCurve( const QPolygonF &points ) const
  113. {
  114. const int size = ( int )points.size();
  115. if ( size <= 2 )
  116. return points;
  117. FitMode fitMode = d_data->fitMode;
  118. if ( fitMode == Auto )
  119. {
  120. fitMode = Spline;
  121. const QPointF *p = points.data();
  122. for ( int i = 1; i < size; i++ )
  123. {
  124. if ( p[i].x() <= p[i-1].x() )
  125. {
  126. fitMode = ParametricSpline;
  127. break;
  128. }
  129. };
  130. }
  131. if ( fitMode == ParametricSpline )
  132. return fitParametric( points );
  133. else
  134. return fitSpline( points );
  135. }
  136. QPolygonF QwtSplineCurveFitter::fitSpline( const QPolygonF &points ) const
  137. {
  138. d_data->spline.setPoints( points );
  139. if ( !d_data->spline.isValid() )
  140. return points;
  141. QPolygonF fittedPoints( d_data->splineSize );
  142. const double x1 = points[0].x();
  143. const double x2 = points[int( points.size() - 1 )].x();
  144. const double dx = x2 - x1;
  145. const double delta = dx / ( d_data->splineSize - 1 );
  146. for ( int i = 0; i < d_data->splineSize; i++ )
  147. {
  148. QPointF &p = fittedPoints[i];
  149. const double v = x1 + i * delta;
  150. const double sv = d_data->spline.value( v );
  151. p.setX( qRound( v ) );
  152. p.setY( qRound( sv ) );
  153. }
  154. d_data->spline.reset();
  155. return fittedPoints;
  156. }
  157. QPolygonF QwtSplineCurveFitter::fitParametric( const QPolygonF &points ) const
  158. {
  159. int i;
  160. const int size = points.size();
  161. QPolygonF fittedPoints( d_data->splineSize );
  162. QPolygonF splinePointsX( size );
  163. QPolygonF splinePointsY( size );
  164. const QPointF *p = points.data();
  165. QPointF *spX = splinePointsX.data();
  166. QPointF *spY = splinePointsY.data();
  167. double param = 0.0;
  168. for ( i = 0; i < size; i++ )
  169. {
  170. const double x = p[i].x();
  171. const double y = p[i].y();
  172. if ( i > 0 )
  173. {
  174. const double delta = qSqrt( qwtSqr( x - spX[i-1].y() )
  175. + qwtSqr( y - spY[i-1].y() ) );
  176. param += qMax( delta, 1.0 );
  177. }
  178. spX[i].setX( param );
  179. spX[i].setY( x );
  180. spY[i].setX( param );
  181. spY[i].setY( y );
  182. }
  183. d_data->spline.setPoints( splinePointsX );
  184. if ( !d_data->spline.isValid() )
  185. return points;
  186. const double deltaX =
  187. splinePointsX[size - 1].x() / ( d_data->splineSize - 1 );
  188. for ( i = 0; i < d_data->splineSize; i++ )
  189. {
  190. const double dtmp = i * deltaX;
  191. fittedPoints[i].setX( qRound( d_data->spline.value( dtmp ) ) );
  192. }
  193. d_data->spline.setPoints( splinePointsY );
  194. if ( !d_data->spline.isValid() )
  195. return points;
  196. const double deltaY =
  197. splinePointsY[size - 1].x() / ( d_data->splineSize - 1 );
  198. for ( i = 0; i < d_data->splineSize; i++ )
  199. {
  200. const double dtmp = i * deltaY;
  201. fittedPoints[i].setY( qRound( d_data->spline.value( dtmp ) ) );
  202. }
  203. return fittedPoints;
  204. }
  205. class QwtWeedingCurveFitter::PrivateData
  206. {
  207. public:
  208. PrivateData():
  209. tolerance( 1.0 )
  210. {
  211. }
  212. double tolerance;
  213. };
  214. class QwtWeedingCurveFitter::Line
  215. {
  216. public:
  217. Line( int i1 = 0, int i2 = 0 ):
  218. from( i1 ),
  219. to( i2 )
  220. {
  221. }
  222. int from;
  223. int to;
  224. };
  225. /*!
  226. Constructor
  227. \param tolerance Tolerance
  228. \sa setTolerance(), tolerance()
  229. */
  230. QwtWeedingCurveFitter::QwtWeedingCurveFitter( double tolerance )
  231. {
  232. d_data = new PrivateData;
  233. setTolerance( tolerance );
  234. }
  235. //! Destructor
  236. QwtWeedingCurveFitter::~QwtWeedingCurveFitter()
  237. {
  238. delete d_data;
  239. }
  240. /*!
  241. Assign the tolerance
  242. The tolerance is the maximum distance, that is accaptable
  243. between the original curve and the smoothed curve.
  244. Increasing the tolerance will reduce the number of the
  245. resulting points.
  246. \param tolerance Tolerance
  247. \sa tolerance()
  248. */
  249. void QwtWeedingCurveFitter::setTolerance( double tolerance )
  250. {
  251. d_data->tolerance = qMax( tolerance, 0.0 );
  252. }
  253. /*!
  254. \return Tolerance
  255. \sa setTolerance()
  256. */
  257. double QwtWeedingCurveFitter::tolerance() const
  258. {
  259. return d_data->tolerance;
  260. }
  261. /*!
  262. \param points Series of data points
  263. \return Curve points
  264. */
  265. QPolygonF QwtWeedingCurveFitter::fitCurve( const QPolygonF &points ) const
  266. {
  267. QStack<Line> stack;
  268. stack.reserve( 500 );
  269. const QPointF *p = points.data();
  270. const int nPoints = points.size();
  271. QVector<bool> usePoint( nPoints, false );
  272. double distToSegment;
  273. stack.push( Line( 0, nPoints - 1 ) );
  274. while ( !stack.isEmpty() )
  275. {
  276. const Line r = stack.pop();
  277. // initialize line segment
  278. const double vecX = p[r.to].x() - p[r.from].x();
  279. const double vecY = p[r.to].y() - p[r.from].y();
  280. const double vecLength = qSqrt( vecX * vecX + vecY * vecY );
  281. const double unitVecX = ( vecLength != 0.0 ) ? vecX / vecLength : 0.0;
  282. const double unitVecY = ( vecLength != 0.0 ) ? vecY / vecLength : 0.0;
  283. double maxDist = 0.0;
  284. int nVertexIndexMaxDistance = r.from + 1;
  285. for ( int i = r.from + 1; i < r.to; i++ )
  286. {
  287. //compare to anchor
  288. const double fromVecX = p[i].x() - p[r.from].x();
  289. const double fromVecY = p[i].y() - p[r.from].y();
  290. const double fromVecLength =
  291. qSqrt( fromVecX * fromVecX + fromVecY * fromVecY );
  292. if ( fromVecX * unitVecX + fromVecY * unitVecY < 0.0 )
  293. {
  294. distToSegment = fromVecLength;
  295. }
  296. if ( fromVecX * unitVecX + fromVecY * unitVecY < 0.0 )
  297. {
  298. distToSegment = fromVecLength;
  299. }
  300. else
  301. {
  302. const double toVecX = p[i].x() - p[r.to].x();
  303. const double toVecY = p[i].y() - p[r.to].y();
  304. const double toVecLength = qSqrt( toVecX * toVecX + toVecY * toVecY );
  305. const double s = toVecX * ( -unitVecX ) + toVecY * ( -unitVecY );
  306. if ( s < 0.0 )
  307. distToSegment = toVecLength;
  308. else
  309. {
  310. distToSegment = qSqrt( qFabs( toVecLength * toVecLength - s * s ) );
  311. }
  312. }
  313. if ( maxDist < distToSegment )
  314. {
  315. maxDist = distToSegment;
  316. nVertexIndexMaxDistance = i;
  317. }
  318. }
  319. if ( maxDist <= d_data->tolerance )
  320. {
  321. usePoint[r.from] = true;
  322. usePoint[r.to] = true;
  323. }
  324. else
  325. {
  326. stack.push( Line( r.from, nVertexIndexMaxDistance ) );
  327. stack.push( Line( nVertexIndexMaxDistance, r.to ) );
  328. }
  329. }
  330. int cnt = 0;
  331. QPolygonF stripped( nPoints );
  332. for ( int i = 0; i < nPoints; i++ )
  333. {
  334. if ( usePoint[i] )
  335. stripped[cnt++] = p[i];
  336. }
  337. stripped.resize( cnt );
  338. return stripped;
  339. }