qwt_series_data.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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_SERIES_DATA_H
  10. #define QWT_SERIES_DATA_H 1
  11. #include "qwt_global.h"
  12. #include "qwt_interval.h"
  13. #include "qwt_point_3d.h"
  14. #include "qwt_point_polar.h"
  15. #include <qvector.h>
  16. #include <qrect.h>
  17. //! \brief A sample of the types (x1-x2, y) or (x, y1-y2)
  18. class QWT_EXPORT QwtIntervalSample
  19. {
  20. public:
  21. QwtIntervalSample();
  22. QwtIntervalSample( double, const QwtInterval & );
  23. QwtIntervalSample( double value, double min, double max );
  24. bool operator==( const QwtIntervalSample & ) const;
  25. bool operator!=( const QwtIntervalSample & ) const;
  26. //! Value
  27. double value;
  28. //! Interval
  29. QwtInterval interval;
  30. };
  31. /*!
  32. Constructor
  33. The value is set to 0.0, the interval is invalid
  34. */
  35. inline QwtIntervalSample::QwtIntervalSample():
  36. value( 0.0 )
  37. {
  38. }
  39. //! Constructor
  40. inline QwtIntervalSample::QwtIntervalSample(
  41. double v, const QwtInterval &intv ):
  42. value( v ),
  43. interval( intv )
  44. {
  45. }
  46. //! Constructor
  47. inline QwtIntervalSample::QwtIntervalSample(
  48. double v, double min, double max ):
  49. value( v ),
  50. interval( min, max )
  51. {
  52. }
  53. //! Compare operator
  54. inline bool QwtIntervalSample::operator==(
  55. const QwtIntervalSample &other ) const
  56. {
  57. return value == other.value && interval == other.interval;
  58. }
  59. //! Compare operator
  60. inline bool QwtIntervalSample::operator!=(
  61. const QwtIntervalSample &other ) const
  62. {
  63. return !( *this == other );
  64. }
  65. //! \brief A sample of the types (x1...xn, y) or (x, y1..yn)
  66. class QWT_EXPORT QwtSetSample
  67. {
  68. public:
  69. QwtSetSample();
  70. bool operator==( const QwtSetSample &other ) const;
  71. bool operator!=( const QwtSetSample &other ) const;
  72. //! value
  73. double value;
  74. //! Vector of values associated to value
  75. QVector<double> set;
  76. };
  77. /*!
  78. Constructor
  79. The value is set to 0.0
  80. */
  81. inline QwtSetSample::QwtSetSample():
  82. value( 0.0 )
  83. {
  84. }
  85. //! Compare operator
  86. inline bool QwtSetSample::operator==( const QwtSetSample &other ) const
  87. {
  88. return value == other.value && set == other.set;
  89. }
  90. //! Compare operator
  91. inline bool QwtSetSample::operator!=( const QwtSetSample &other ) const
  92. {
  93. return !( *this == other );
  94. }
  95. /*!
  96. \brief Abstract interface for iterating over samples
  97. Qwt offers several implementations of the QwtSeriesData API,
  98. but in situations, where data of an application specific format
  99. needs to be displayed, without having to copy it, it is recommended
  100. to implement an individual data access.
  101. */
  102. template <typename T>
  103. class QwtSeriesData
  104. {
  105. public:
  106. QwtSeriesData();
  107. virtual ~QwtSeriesData();
  108. //! \return Number of samples
  109. virtual size_t size() const = 0;
  110. /*!
  111. Return a sample
  112. \param i Index
  113. \return Sample at position i
  114. */
  115. virtual T sample( size_t i ) const = 0;
  116. /*!
  117. Calculate the bounding rect of all samples
  118. The bounding rect is necessary for autoscaling and can be used
  119. for a couple of painting optimizations.
  120. qwtBoundingRect(...) offers slow implementations iterating
  121. over the samples. For large sets it is recommended to implement
  122. something faster f.e. by caching the bounding rect.
  123. */
  124. virtual QRectF boundingRect() const = 0;
  125. virtual void setRectOfInterest( const QRectF & );
  126. protected:
  127. //! Can be used to cache a calculated bounding rectangle
  128. mutable QRectF d_boundingRect;
  129. private:
  130. QwtSeriesData<T> &operator=( const QwtSeriesData<T> & );
  131. };
  132. //! Constructor
  133. template <typename T>
  134. QwtSeriesData<T>::QwtSeriesData():
  135. d_boundingRect( 0.0, 0.0, -1.0, -1.0 )
  136. {
  137. }
  138. //! Destructor
  139. template <typename T>
  140. QwtSeriesData<T>::~QwtSeriesData()
  141. {
  142. }
  143. /*!
  144. Set a the "rect of interest"
  145. QwtPlotSeriesItem defines the current area of the plot canvas
  146. as "rect of interest" ( QwtPlotSeriesItem::updateScaleDiv() ).
  147. It can be used to implement different levels of details.
  148. The default implementation does nothing.
  149. */
  150. template <typename T>
  151. void QwtSeriesData<T>::setRectOfInterest( const QRectF & )
  152. {
  153. }
  154. /*!
  155. \brief Template class for data, that is organized as QVector
  156. QVector uses implicit data sharing and can be
  157. passed around as argument efficiently.
  158. */
  159. template <typename T>
  160. class QwtArraySeriesData: public QwtSeriesData<T>
  161. {
  162. public:
  163. QwtArraySeriesData();
  164. QwtArraySeriesData( const QVector<T> & );
  165. void setSamples( const QVector<T> & );
  166. const QVector<T> samples() const;
  167. virtual size_t size() const;
  168. virtual T sample( size_t ) const;
  169. protected:
  170. //! Vector of samples
  171. QVector<T> d_samples;
  172. };
  173. //! Constructor
  174. template <typename T>
  175. QwtArraySeriesData<T>::QwtArraySeriesData()
  176. {
  177. }
  178. /*!
  179. Constructor
  180. \param samples Array of samples
  181. */
  182. template <typename T>
  183. QwtArraySeriesData<T>::QwtArraySeriesData( const QVector<T> &samples ):
  184. d_samples( samples )
  185. {
  186. }
  187. /*!
  188. Assign an array of samples
  189. \param samples Array of samples
  190. */
  191. template <typename T>
  192. void QwtArraySeriesData<T>::setSamples( const QVector<T> &samples )
  193. {
  194. QwtSeriesData<T>::d_boundingRect = QRectF( 0.0, 0.0, -1.0, -1.0 );
  195. d_samples = samples;
  196. }
  197. //! \return Array of samples
  198. template <typename T>
  199. const QVector<T> QwtArraySeriesData<T>::samples() const
  200. {
  201. return d_samples;
  202. }
  203. //! \return Number of samples
  204. template <typename T>
  205. size_t QwtArraySeriesData<T>::size() const
  206. {
  207. return d_samples.size();
  208. }
  209. /*!
  210. Return a sample
  211. \param i Index
  212. \return Sample at position i
  213. */
  214. template <typename T>
  215. T QwtArraySeriesData<T>::sample( size_t i ) const
  216. {
  217. return d_samples[i];
  218. }
  219. //! Interface for iterating over an array of points
  220. class QWT_EXPORT QwtPointSeriesData: public QwtArraySeriesData<QPointF>
  221. {
  222. public:
  223. QwtPointSeriesData(
  224. const QVector<QPointF> & = QVector<QPointF>() );
  225. virtual QRectF boundingRect() const;
  226. };
  227. //! Interface for iterating over an array of 3D points
  228. class QWT_EXPORT QwtPoint3DSeriesData: public QwtArraySeriesData<QwtPoint3D>
  229. {
  230. public:
  231. QwtPoint3DSeriesData(
  232. const QVector<QwtPoint3D> & = QVector<QwtPoint3D>() );
  233. virtual QRectF boundingRect() const;
  234. };
  235. //! Interface for iterating over an array of intervals
  236. class QWT_EXPORT QwtIntervalSeriesData: public QwtArraySeriesData<QwtIntervalSample>
  237. {
  238. public:
  239. QwtIntervalSeriesData(
  240. const QVector<QwtIntervalSample> & = QVector<QwtIntervalSample>() );
  241. virtual QRectF boundingRect() const;
  242. };
  243. //! Interface for iterating over an array of samples
  244. class QWT_EXPORT QwtSetSeriesData: public QwtArraySeriesData<QwtSetSample>
  245. {
  246. public:
  247. QwtSetSeriesData(
  248. const QVector<QwtSetSample> & = QVector<QwtSetSample>() );
  249. virtual QRectF boundingRect() const;
  250. };
  251. /*!
  252. \brief Interface for iterating over two QVector<double> objects.
  253. */
  254. class QWT_EXPORT QwtPointArrayData: public QwtSeriesData<QPointF>
  255. {
  256. public:
  257. QwtPointArrayData( const QVector<double> &x, const QVector<double> &y );
  258. QwtPointArrayData( const double *x, const double *y, size_t size );
  259. virtual QRectF boundingRect() const;
  260. virtual size_t size() const;
  261. virtual QPointF sample( size_t i ) const;
  262. const QVector<double> &xData() const;
  263. const QVector<double> &yData() const;
  264. private:
  265. QVector<double> d_x;
  266. QVector<double> d_y;
  267. };
  268. /*!
  269. \brief Data class containing two pointers to memory blocks of doubles.
  270. */
  271. class QWT_EXPORT QwtCPointerData: public QwtSeriesData<QPointF>
  272. {
  273. public:
  274. QwtCPointerData( const double *x, const double *y, size_t size );
  275. virtual QRectF boundingRect() const;
  276. virtual size_t size() const;
  277. virtual QPointF sample( size_t i ) const;
  278. const double *xData() const;
  279. const double *yData() const;
  280. private:
  281. const double *d_x;
  282. const double *d_y;
  283. size_t d_size;
  284. };
  285. /*!
  286. \brief Synthetic point data
  287. QwtSyntheticPointData provides a fixed number of points for an interval.
  288. The points are calculated in equidistant steps in x-direction.
  289. If the interval is invalid, the points are calculated for
  290. the "rect of interest", what normally is the displayed area on the
  291. plot canvas. In this mode you get different levels of detail, when
  292. zooming in/out.
  293. \par Example
  294. The following example shows how to implement a sinus curve.
  295. \verbatim
  296. #include <cmath>
  297. #include <qwt_series_data.h>
  298. #include <qwt_plot_curve.h>
  299. #include <qwt_plot.h>
  300. #include <qapplication.h>
  301. class SinusData: public QwtSyntheticPointData
  302. {
  303. public:
  304. SinusData():
  305. QwtSyntheticPointData(100)
  306. {
  307. }
  308. virtual double y(double x) const
  309. {
  310. return qSin(x);
  311. }
  312. };
  313. int main(int argc, char **argv)
  314. {
  315. QApplication a(argc, argv);
  316. QwtPlot plot;
  317. plot.setAxisScale(QwtPlot::xBottom, 0.0, 10.0);
  318. plot.setAxisScale(QwtPlot::yLeft, -1.0, 1.0);
  319. QwtPlotCurve *curve = new QwtPlotCurve("y = sin(x)");
  320. curve->setData(SinusData());
  321. curve->attach(&plot);
  322. plot.show();
  323. return a.exec();
  324. }
  325. \endverbatim
  326. */
  327. class QWT_EXPORT QwtSyntheticPointData: public QwtSeriesData<QPointF>
  328. {
  329. public:
  330. QwtSyntheticPointData( size_t size,
  331. const QwtInterval & = QwtInterval() );
  332. void setSize( size_t size );
  333. size_t size() const;
  334. void setInterval( const QwtInterval& );
  335. QwtInterval interval() const;
  336. virtual QRectF boundingRect() const;
  337. virtual QPointF sample( size_t i ) const;
  338. /*!
  339. Calculate a y value for a x value
  340. \param x x value
  341. \return Corresponding y value
  342. */
  343. virtual double y( double x ) const = 0;
  344. virtual double x( uint index ) const;
  345. virtual void setRectOfInterest( const QRectF & );
  346. QRectF rectOfInterest() const;
  347. private:
  348. size_t d_size;
  349. QwtInterval d_interval;
  350. QRectF d_rectOfInterest;
  351. QwtInterval d_intervalOfInterest;
  352. };
  353. QWT_EXPORT QRectF qwtBoundingRect( const QwtSeriesData<QPointF> & );
  354. QWT_EXPORT QRectF qwtBoundingRect( const QwtSeriesData<QwtPoint3D> & );
  355. QWT_EXPORT QRectF qwtBoundingRect( const QwtSeriesData<QwtPointPolar> & );
  356. QWT_EXPORT QRectF qwtBoundingRect( const QwtSeriesData<QwtIntervalSample> & );
  357. QWT_EXPORT QRectF qwtBoundingRect( const QwtSeriesData<QwtSetSample> & );
  358. #endif