You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2018 lines
76 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: mathplot.cpp
  3. // Purpose: Framework for plotting in wxWindows
  4. // Original Author: David Schalig
  5. // Maintainer: Davide Rondini
  6. // Contributors: Jose Luis Blanco, Val Greene, Maciej Suminski, Tomasz Wlostowski
  7. // Created: 21/07/2003
  8. // Last edit: 05/08/2016
  9. // Copyright: (c) David Schalig, Davide Rondini
  10. // Licence: wxWindows licence
  11. /////////////////////////////////////////////////////////////////////////////
  12. #ifndef _MP_MATHPLOT_H_
  13. #define _MP_MATHPLOT_H_
  14. /** @file mathplot.h */
  15. /**
  16. * wxMathPlot is a framework for mathematical graph plotting in wxWindows.
  17. *
  18. * The framework is designed for convenience and ease of use.
  19. *
  20. * @section screenshots Screenshots
  21. * <a href="http://wxmathplot.sourceforge.net/screenshot.shtml">Go to the screenshots page.</a>
  22. *
  23. * @section overview Overview
  24. * The heart of wxMathPlot is mpWindow, which is a 2D canvas for plot layers.
  25. * mpWindow can be embedded as subwindow in a wxPane, a wxFrame, or any other wxWindow.
  26. * mpWindow provides a zoomable and moveable view of the layers. The current view can
  27. * be controlled with the mouse, the scrollbars, and a context menu.
  28. *
  29. * Plot layers are implementations of the abstract base class mpLayer. Those can
  30. * be function plots, scale rulers, or any other vector data visualisation.
  31. * wxMathPlot provides two mpLayer implementations for plotting horizontal and vertical rulers: mpScaleX and mpScaleY.
  32. * For convenient function plotting a series of classes derived from mpLayer are provided,
  33. * like mpFX, mpProfile, mpLegend and so on.
  34. * These base classes already come with plot code, user's own functions can be implemented by overriding just one member for retrieving a function value.
  35. *
  36. * mpWindow has built-in support for mouse-based pan and zoom through intuitive combinations of buttons and the mouse wheel. It also incorporates an optional double buffering mechanism to avoid flicker. Plots can be easily sent to printer evices or exported in bitmap formats like PNG, BMP or JPEG.
  37. *
  38. * @section coding Coding conventions
  39. * wxMathPlot sticks to wxWindow's coding conventions.
  40. * All entities defined by wxMathPlot have the prefix <i>mp</i>.
  41. *
  42. * @section author Author and license
  43. * wxMathPlot is published under the terms of the wxWindow license.<br>
  44. * The original author is David Schalig <mrhill@users.sourceforge.net>.<br>
  45. * From June 2007 the project is maintained by Davide Rondini <cdron77@users.sourceforge.net>.<br>
  46. * Authors can be contacted via the wxMathPlot's homepage at
  47. * https://sourceforge.net/projects/wxmathplot<br>
  48. * Contributors:<br>
  49. * Jose Luis Blanco, Val Greene.<br>
  50. */
  51. // this definition uses windows dll to export function.
  52. // WXDLLIMPEXP_MATHPLOT definition definition changed to WXDLLIMPEXP_MATHPLOT
  53. // mathplot_EXPORTS will be defined by cmake
  54. #ifdef mathplot_EXPORTS
  55. #define WXDLLIMPEXP_MATHPLOT WXEXPORT
  56. #define WXDLLIMPEXP_DATA_MATHPLOT( type ) WXEXPORT type
  57. #else // not making DLL
  58. #define WXDLLIMPEXP_MATHPLOT
  59. #define WXDLLIMPEXP_DATA_MATHPLOT( type ) type
  60. #endif
  61. #include <vector>
  62. // #include <wx/wx.h>
  63. #include <wx/defs.h>
  64. #include <wx/menu.h>
  65. #include <wx/scrolwin.h>
  66. #include <wx/event.h>
  67. #include <wx/dynarray.h>
  68. #include <wx/pen.h>
  69. #include <wx/dcmemory.h>
  70. #include <wx/string.h>
  71. #include <wx/print.h>
  72. #include <wx/image.h>
  73. #include <deque>
  74. #include <algorithm>
  75. // For memory leak debug
  76. #ifdef _WINDOWS
  77. #ifdef _DEBUG
  78. #include <crtdbg.h>
  79. #define DEBUG_NEW new (_NORMAL_BLOCK, __FILE__, __LINE__)
  80. #else
  81. #define DEBUG_NEW new
  82. #endif // _DEBUG
  83. #endif // _WINDOWS
  84. // Separation for axes when set close to border
  85. #define X_BORDER_SEPARATION 40
  86. #define Y_BORDER_SEPARATION 60
  87. // -----------------------------------------------------------------------------
  88. // classes
  89. // -----------------------------------------------------------------------------
  90. class WXDLLIMPEXP_MATHPLOT mpLayer;
  91. class WXDLLIMPEXP_MATHPLOT mpFX;
  92. class WXDLLIMPEXP_MATHPLOT mpFY;
  93. class WXDLLIMPEXP_MATHPLOT mpFXY;
  94. class WXDLLIMPEXP_MATHPLOT mpFXYVector;
  95. class WXDLLIMPEXP_MATHPLOT mpScaleX;
  96. class WXDLLIMPEXP_MATHPLOT mpScaleY;
  97. class WXDLLIMPEXP_MATHPLOT mpWindow;
  98. class WXDLLIMPEXP_MATHPLOT mpText;
  99. class WXDLLIMPEXP_MATHPLOT mpPrintout;
  100. /** Command IDs used by mpWindow */
  101. enum
  102. {
  103. mpID_FIT = 2000, // !< Fit view to match bounding box of all layers
  104. mpID_ZOOM_IN, // !< Zoom into view at clickposition / window center
  105. mpID_ZOOM_OUT, // !< Zoom out
  106. mpID_CENTER, // !< Center view on click position
  107. mpID_LOCKASPECT, // !< Lock x/y scaling aspect
  108. };
  109. // -----------------------------------------------------------------------------
  110. // mpLayer
  111. // -----------------------------------------------------------------------------
  112. typedef enum __mp_Layer_Type
  113. {
  114. mpLAYER_UNDEF, // !< Layer type undefined
  115. mpLAYER_AXIS, // !< Axis type layer
  116. mpLAYER_PLOT, // !< Plot type layer
  117. mpLAYER_INFO, // !< Info box type layer
  118. mpLAYER_BITMAP // !< Bitmap type layer
  119. } mpLayerType;
  120. /** Plot layer, abstract base class.
  121. * Any number of mpLayer implementations can be attached to mpWindow.
  122. * Examples for mpLayer implementations are function graphs, or scale rulers.
  123. *
  124. * For convenience mpLayer defines a name, a font (wxFont), a pen (wxPen),
  125. * and a continuity property (bool) as class members.
  126. * The default values at constructor are the default font, a black pen, and
  127. * continuity set to false (draw separate points).
  128. * These may or may not be used by implementations.
  129. */
  130. class mpScaleBase;
  131. class WXDLLIMPEXP_MATHPLOT mpLayer : public wxObject
  132. {
  133. public:
  134. mpLayer();
  135. virtual ~mpLayer() {};
  136. /** Check whether this layer has a bounding box.
  137. * The default implementation returns \a TRUE. Override and return
  138. * FALSE if your mpLayer implementation should be ignored by the calculation
  139. * of the global bounding box for all layers in a mpWindow.
  140. * @retval TRUE Has bounding box
  141. * @retval FALSE Has not bounding box
  142. */
  143. virtual bool HasBBox() { return TRUE; }
  144. /** Check whether the layer is an info box.
  145. * The default implementation returns \a FALSE. It is overrided to \a TRUE for mpInfoLayer
  146. * class and its derivative. It is necessary to define mouse actions behaviour over
  147. * info boxes.
  148. * @return whether the layer is an info boxes
  149. * @sa mpInfoLayer::IsInfo
  150. */
  151. virtual bool IsInfo() { return false; };
  152. /** Get inclusive left border of bounding box.
  153. * @return Value
  154. */
  155. virtual double GetMinX() { return -1.0; }
  156. /** Get inclusive right border of bounding box.
  157. * @return Value
  158. */
  159. virtual double GetMaxX() { return 1.0; }
  160. /** Get inclusive bottom border of bounding box.
  161. * @return Value
  162. */
  163. virtual double GetMinY() { return -1.0; }
  164. /** Get inclusive top border of bounding box.
  165. * @return Value
  166. */
  167. virtual double GetMaxY() { return 1.0; }
  168. /** Plot given view of layer to the given device context.
  169. * An implementation of this function has to transform layer coordinates to
  170. * wxDC coordinates based on the view parameters retrievable from the mpWindow
  171. * passed in \a w.
  172. * Note that the public methods of mpWindow: x2p,y2p and p2x,p2y are already provided
  173. * which transform layer coordinates to DC pixel coordinates, and <b>user code should rely
  174. * on them</b> for portability and future changes to be applied transparently, instead of
  175. * implementing the following formulas manually.
  176. *
  177. * The passed device context \a dc has its coordinate origin set to the top-left corner
  178. * of the visible area (the default). The coordinate orientation is as shown in the
  179. * following picture:
  180. * <pre>
  181. * (wxDC origin 0,0)
  182. * x-------------> ascending X ----------------+
  183. * | |
  184. * | |
  185. * | V ascending Y |
  186. * | |
  187. * | |
  188. * | |
  189. * |+------------------------------------------+ <-- right-bottom corner of the mpWindow visible area.
  190. * </pre>
  191. * Note that Y ascends in downward direction, whereas the usual vertical orientation
  192. * for mathematical plots is vice versa. Thus Y-orientation will be swapped usually,
  193. * when transforming between wxDC and mpLayer coordinates. This change of coordinates
  194. * is taken into account in the methods p2x,p2y,x2p,y2p.
  195. *
  196. * <b> Rules for transformation between mpLayer and wxDC coordinates </b>
  197. * @code
  198. * dc_X = (layer_X - mpWindow::GetPosX()) * mpWindow::GetScaleX()
  199. * dc_Y = (mpWindow::GetPosY() - layer_Y) * mpWindow::GetScaleY() // swapping Y-orientation
  200. *
  201. * layer_X = (dc_X / mpWindow::GetScaleX()) + mpWindow::GetPosX() // scale guaranteed to be not 0
  202. * layer_Y = mpWindow::GetPosY() - (dc_Y / mpWindow::GetScaleY()) // swapping Y-orientation
  203. * @endcode
  204. *
  205. * @param dc Device context to plot to.
  206. * @param w View to plot. The visible area can be retrieved from this object.
  207. * @sa mpWindow::p2x,mpWindow::p2y,mpWindow::x2p,mpWindow::y2p
  208. */
  209. virtual void Plot( wxDC& dc, mpWindow& w ) = 0;
  210. /** Get layer name.
  211. * @return Name
  212. */
  213. const wxString& GetName() const { return m_name; }
  214. /** Get font set for this layer.
  215. * @return Font
  216. */
  217. const wxFont& GetFont() const { return m_font; }
  218. /** Get pen set for this layer.
  219. * @return Pen
  220. */
  221. const wxPen& GetPen() const { return m_pen; }
  222. /** Set the 'continuity' property of the layer (true:draws a continuous line, false:draws separate points).
  223. * @sa GetContinuity
  224. */
  225. void SetContinuity( bool continuity ) { m_continuous = continuity; }
  226. /** Gets the 'continuity' property of the layer.
  227. * @sa SetContinuity
  228. */
  229. bool GetContinuity() const { return m_continuous; }
  230. /** Shows or hides the text label with the name of the layer (default is visible).
  231. */
  232. void ShowName( bool show ) { m_showName = show; };
  233. /** Set layer name
  234. * @param name Name, will be copied to internal class member
  235. */
  236. void SetName( wxString name ) { m_name = name; }
  237. /** Set layer font
  238. * @param font Font, will be copied to internal class member
  239. */
  240. void SetFont( wxFont& font ) { m_font = font; }
  241. /** Set layer pen
  242. * @param pen Pen, will be copied to internal class member
  243. */
  244. void SetPen( wxPen pen ) { m_pen = pen; }
  245. /** Set Draw mode: inside or outside margins. Default is outside, which allows the layer to draw up to the mpWindow border.
  246. * @param drawModeOutside The draw mode to be set */
  247. void SetDrawOutsideMargins( bool drawModeOutside ) { m_drawOutsideMargins = drawModeOutside; };
  248. /** Get Draw mode: inside or outside margins.
  249. * @return The draw mode */
  250. bool GetDrawOutsideMargins() { return m_drawOutsideMargins; };
  251. /** Get a small square bitmap filled with the colour of the pen used in the layer. Useful to create legends or similar reference to the layers.
  252. * @param side side length in pixels
  253. * @return a wxBitmap filled with layer's colour */
  254. wxBitmap GetColourSquare( int side = 16 );
  255. /** Get layer type: a Layer can be of different types: plot lines, axis, info boxes, etc, this method returns the right value.
  256. * @return An integer indicating layer type */
  257. mpLayerType GetLayerType() { return m_type; };
  258. /** Checks whether the layer is visible or not.
  259. * @return \a true if visible */
  260. bool IsVisible() { return m_visible; };
  261. /** Sets layer visibility.
  262. * @param show visibility bool. */
  263. void SetVisible( bool show ) { m_visible = show; };
  264. /** Get brush set for this layer.
  265. * @return brush. */
  266. const wxBrush& GetBrush() const { return m_brush; };
  267. /** Set layer brush
  268. * @param brush brush, will be copied to internal class member */
  269. void SetBrush( wxBrush brush ) { m_brush = brush; };
  270. protected:
  271. wxFont m_font; // !< Layer's font
  272. wxPen m_pen; // !< Layer's pen
  273. wxBrush m_brush; // !< Layer's brush
  274. wxString m_name; // !< Layer's name
  275. bool m_continuous; // !< Specify if the layer will be plotted as a continuous line or a set of points.
  276. bool m_showName; // !< States whether the name of the layer must be shown (default is true).
  277. bool m_drawOutsideMargins; // !< select if the layer should draw only inside margins or over all DC
  278. mpLayerType m_type; // !< Define layer type, which is assigned by constructor
  279. bool m_visible; // !< Toggles layer visibility
  280. DECLARE_DYNAMIC_CLASS( mpLayer )
  281. };
  282. // -----------------------------------------------------------------------------
  283. // mpInfoLayer
  284. // -----------------------------------------------------------------------------
  285. /** @class mpInfoLayer
  286. * @brief Base class to create small rectangular info boxes
  287. * mpInfoLayer is the base class to create a small rectangular info box in transparent overlay over plot layers. It is used to implement objects like legends.
  288. */
  289. class WXDLLIMPEXP_MATHPLOT mpInfoLayer : public mpLayer
  290. {
  291. public:
  292. /** Default constructor. */
  293. mpInfoLayer();
  294. /** Complete constructor.
  295. * @param rect Sets the initial size rectangle of the layer.
  296. * @param brush pointer to a fill brush. Default is transparent */
  297. mpInfoLayer( wxRect rect, const wxBrush* brush = wxTRANSPARENT_BRUSH );
  298. /** Destructor */
  299. virtual ~mpInfoLayer();
  300. /** Updates the content of the info box. Should be overidden by derived classes.
  301. * Update may behave in different ways according to the type of event which called it.
  302. * @param w parent mpWindow from which to obtain informations
  303. * @param event The event which called the update. */
  304. virtual void UpdateInfo( mpWindow& w, wxEvent& event );
  305. /** mpInfoLayer has not bounding box. @sa mpLayer::HasBBox
  306. * @return always \a FALSE */
  307. virtual bool HasBBox() override { return false; }
  308. /** Plot method. Can be overidden by derived classes.
  309. * @param dc the device content where to plot
  310. * @param w the window to plot
  311. * @sa mpLayer::Plot */
  312. virtual void Plot( wxDC& dc, mpWindow& w ) override;
  313. /** Specifies that this is an Info box layer.
  314. * @return always \a TRUE
  315. * @sa mpLayer::IsInfo */
  316. virtual bool IsInfo() override { return true; }
  317. /** Checks whether a point is inside the info box rectangle.
  318. * @param point The point to be checked
  319. * @return \a true if the point is inside the bounding box */
  320. virtual bool Inside( wxPoint& point );
  321. /** Moves the layer rectangle of given pixel deltas.
  322. * @param delta The wxPoint container for delta coordinates along x and y. Units are in pixels. */
  323. virtual void Move( wxPoint delta );
  324. /** Updates the rectangle reference point. Used by internal methods of mpWindow to correctly move mpInfoLayers. */
  325. virtual void UpdateReference();
  326. /** Returns the position of the upper left corner of the box (in pixels)
  327. * @return The rectangle position */
  328. wxPoint GetPosition();
  329. /** Returns the size of the box (in pixels)
  330. * @return The rectangle size */
  331. wxSize GetSize();
  332. /** Returns the current rectangle coordinates.
  333. * @return The info layer rectangle */
  334. const wxRect& GetRectangle() { return m_dim; };
  335. protected:
  336. wxRect m_dim; // !< The bounding rectangle of the box. It may be resized dynamically by the Plot method.
  337. wxPoint m_reference; // !< Holds the reference point for movements
  338. wxBrush m_brush; // !< The brush to be used for the background
  339. int m_winX, m_winY; // !< Holds the mpWindow size. Used to rescale position when window is resized.
  340. DECLARE_DYNAMIC_CLASS( mpInfoLayer )
  341. };
  342. /** @class mpInfoCoords
  343. * @brief Implements an overlay box which shows the mouse coordinates in plot units.
  344. * When an mpInfoCoords layer is activated, when mouse is moved over the mpWindow, its coordinates (in mpWindow units, not pixels) are continuously reported inside the layer box. */
  345. class WXDLLIMPEXP_MATHPLOT mpInfoCoords : public mpInfoLayer
  346. {
  347. public:
  348. /** Default constructor */
  349. mpInfoCoords();
  350. /** Complete constructor, setting initial rectangle and background brush.
  351. * @param rect The initial bounding rectangle.
  352. * @param brush The wxBrush to be used for box background: default is transparent */
  353. mpInfoCoords( wxRect rect, const wxBrush* brush = wxTRANSPARENT_BRUSH );
  354. /** Default destructor */
  355. ~mpInfoCoords();
  356. /** Updates the content of the info box. It is used to update coordinates.
  357. * @param w parent mpWindow from which to obtain information
  358. * @param event The event which called the update. */
  359. virtual void UpdateInfo( mpWindow& w, wxEvent& event ) override;
  360. /** Plot method.
  361. * @param dc the device content where to plot
  362. * @param w the window to plot
  363. * @sa mpLayer::Plot */
  364. virtual void Plot( wxDC& dc, mpWindow& w ) override;
  365. protected:
  366. wxString m_content; // !< string holding the coordinates to be drawn.
  367. };
  368. /** @class mpInfoLegend
  369. * @brief Implements the legend to be added to the plot
  370. * This layer allows you to add a legend to describe the plots in the window. The legend uses the layer name as a label, and displays only layers of type mpLAYER_PLOT. */
  371. class WXDLLIMPEXP_MATHPLOT mpInfoLegend : public mpInfoLayer
  372. {
  373. public:
  374. /** Default constructor */
  375. mpInfoLegend();
  376. /** Complete constructor, setting initial rectangle and background brush.
  377. * @param rect The initial bounding rectangle.
  378. * @param brush The wxBrush to be used for box background: default is transparent
  379. * @sa mpInfoLayer::mpInfoLayer */
  380. mpInfoLegend( wxRect rect, const wxBrush* brush = wxTRANSPARENT_BRUSH );
  381. /** Default destructor */
  382. ~mpInfoLegend();
  383. /** Updates the content of the info box. Unused in this class.
  384. * @param w parent mpWindow from which to obtain information
  385. * @param event The event which called the update. */
  386. virtual void UpdateInfo( mpWindow& w, wxEvent& event ) override;
  387. /** Plot method.
  388. * @param dc the device content where to plot
  389. * @param w the window to plot
  390. * @sa mpLayer::Plot */
  391. virtual void Plot( wxDC& dc, mpWindow& w ) override;
  392. protected:
  393. };
  394. // -----------------------------------------------------------------------------
  395. // mpLayer implementations - functions
  396. // -----------------------------------------------------------------------------
  397. /** @name Label alignment constants
  398. * @{*/
  399. /** @internal */
  400. #define mpALIGNMASK 0x03
  401. /** Aligns label to the right. For use with mpFX. */
  402. #define mpALIGN_RIGHT 0x00
  403. /** Aligns label to the center. For use with mpFX and mpFY. */
  404. #define mpALIGN_CENTER 0x01
  405. /** Aligns label to the left. For use with mpFX. */
  406. #define mpALIGN_LEFT 0x02
  407. /** Aligns label to the top. For use with mpFY. */
  408. #define mpALIGN_TOP mpALIGN_RIGHT
  409. /** Aligns label to the bottom. For use with mpFY. */
  410. #define mpALIGN_BOTTOM mpALIGN_LEFT
  411. /** Aligns X axis to bottom border. For mpScaleX */
  412. #define mpALIGN_BORDER_BOTTOM 0x04
  413. /** Aligns X axis to top border. For mpScaleX */
  414. #define mpALIGN_BORDER_TOP 0x05
  415. /** Set label for X axis in normal mode */
  416. #define mpX_NORMAL 0x00
  417. /** Set label for X axis in time mode: the value is represented as minutes:seconds.milliseconds if time is less than 2 minutes, hours:minutes:seconds otherwise. */
  418. #define mpX_TIME 0x01
  419. /** Set label for X axis in hours mode: the value is always represented as hours:minutes:seconds. */
  420. #define mpX_HOURS 0x02
  421. /** Set label for X axis in date mode: the value is always represented as yyyy-mm-dd. */
  422. #define mpX_DATE 0x03
  423. /** Set label for X axis in datetime mode: the value is always represented as yyyy-mm-ddThh:mm:ss. */
  424. #define mpX_DATETIME 0x04
  425. /** Aligns Y axis to left border. For mpScaleY */
  426. #define mpALIGN_BORDER_LEFT mpALIGN_BORDER_BOTTOM
  427. /** Aligns Y axis to right border. For mpScaleY */
  428. #define mpALIGN_BORDER_RIGHT mpALIGN_BORDER_TOP
  429. /** Aligns label to north-east. For use with mpFXY. */
  430. #define mpALIGN_NE 0x00
  431. /** Aligns label to north-west. For use with mpFXY. */
  432. #define mpALIGN_NW 0x01
  433. /** Aligns label to south-west. For use with mpFXY. */
  434. #define mpALIGN_SW 0x02
  435. /** Aligns label to south-east. For use with mpFXY. */
  436. #define mpALIGN_SE 0x03
  437. /*@}*/
  438. /** @name mpLayer implementations - functions
  439. * @{*/
  440. /** Abstract base class providing plot and labeling functionality for functions F:X->Y.
  441. * Override mpFX::GetY to implement a function.
  442. * Optionally implement a constructor and pass a name (label) and a label alignment
  443. * to the constructor mpFX::mpFX. If the layer name is empty, no label will be plotted.
  444. */
  445. class WXDLLIMPEXP_MATHPLOT mpFX : public mpLayer
  446. {
  447. public:
  448. /** @param name Label
  449. * @param flags Label alignment, pass one of #mpALIGN_RIGHT, #mpALIGN_CENTER, #mpALIGN_LEFT.
  450. */
  451. mpFX( const wxString& name = wxEmptyString, int flags = mpALIGN_RIGHT );
  452. /** Get function value for argument.
  453. * Override this function in your implementation.
  454. * @param x Argument
  455. * @return Function value
  456. */
  457. virtual double GetY( double x ) = 0;
  458. /** Layer plot handler.
  459. * This implementation will plot the function in the visible area and
  460. * put a label according to the alignment specified.
  461. */
  462. virtual void Plot( wxDC& dc, mpWindow& w ) override;
  463. protected:
  464. int m_flags; // !< Holds label alignment
  465. DECLARE_DYNAMIC_CLASS( mpFX )
  466. };
  467. /** Abstract base class providing plot and labeling functionality for functions F:Y->X.
  468. * Override mpFY::GetX to implement a function.
  469. * Optionally implement a constructor and pass a name (label) and a label alignment
  470. * to the constructor mpFY::mpFY. If the layer name is empty, no label will be plotted.
  471. */
  472. class WXDLLIMPEXP_MATHPLOT mpFY : public mpLayer
  473. {
  474. public:
  475. /** @param name Label
  476. * @param flags Label alignment, pass one of #mpALIGN_BOTTOM, #mpALIGN_CENTER, #mpALIGN_TOP.
  477. */
  478. mpFY( const wxString& name = wxEmptyString, int flags = mpALIGN_TOP );
  479. /** Get function value for argument.
  480. * Override this function in your implementation.
  481. * @param y Argument
  482. * @return Function value
  483. */
  484. virtual double GetX( double y ) = 0;
  485. /** Layer plot handler.
  486. * This implementation will plot the function in the visible area and
  487. * put a label according to the alignment specified.
  488. */
  489. virtual void Plot( wxDC& dc, mpWindow& w ) override;
  490. protected:
  491. int m_flags; // !< Holds label alignment
  492. DECLARE_DYNAMIC_CLASS( mpFY )
  493. };
  494. /** Abstract base class providing plot and labeling functionality for a locus plot F:N->X,Y.
  495. * Locus argument N is assumed to be in range 0 .. MAX_N, and implicitly derived by enumerating
  496. * all locus values. Override mpFXY::Rewind and mpFXY::GetNextXY to implement a locus.
  497. * Optionally implement a constructor and pass a name (label) and a label alignment
  498. * to the constructor mpFXY::mpFXY. If the layer name is empty, no label will be plotted.
  499. */
  500. class WXDLLIMPEXP_MATHPLOT mpFXY : public mpLayer
  501. {
  502. public:
  503. /** @param name Label
  504. * @param flags Label alignment, pass one of #mpALIGN_NE, #mpALIGN_NW, #mpALIGN_SW, #mpALIGN_SE.
  505. */
  506. mpFXY( const wxString& name = wxEmptyString, int flags = mpALIGN_NE );
  507. /** Rewind value enumeration with mpFXY::GetNextXY.
  508. * Override this function in your implementation.
  509. */
  510. virtual void Rewind() = 0;
  511. /** Get locus value for next N.
  512. * Override this function in your implementation.
  513. * @param x Returns X value
  514. * @param y Returns Y value
  515. */
  516. virtual bool GetNextXY( double& x, double& y ) = 0;
  517. virtual size_t GetCount() = 0;
  518. /** Layer plot handler.
  519. * This implementation will plot the locus in the visible area and
  520. * put a label according to the alignment specified.
  521. */
  522. virtual void Plot( wxDC& dc, mpWindow& w ) override;
  523. virtual void SetScale( mpScaleBase* scaleX, mpScaleBase* scaleY );
  524. void UpdateScales();
  525. double s2x( double plotCoordX ) const;
  526. double s2y( double plotCoordY ) const;
  527. double x2s( double x ) const;
  528. double y2s( double y ) const;
  529. protected:
  530. int m_flags; // !< Holds label alignment
  531. // Data to calculate label positioning
  532. wxCoord maxDrawX, minDrawX, maxDrawY, minDrawY;
  533. // int drawnPoints;
  534. mpScaleBase* m_scaleX, * m_scaleY;
  535. /** Update label positioning data
  536. * @param xnew New x coordinate
  537. * @param ynew New y coordinate
  538. */
  539. void UpdateViewBoundary( wxCoord xnew, wxCoord ynew );
  540. DECLARE_DYNAMIC_CLASS( mpFXY )
  541. };
  542. /** Abstract base class providing plot and labeling functionality for functions F:Y->X.
  543. * Override mpProfile::GetX to implement a function.
  544. * This class is similar to mpFY, but the Plot method is different. The plot is in fact represented by lines instead of points, which gives best rendering of rapidly-varying functions, and in general, data which are not so close one to another.
  545. * Optionally implement a constructor and pass a name (label) and a label alignment
  546. * to the constructor mpProfile::mpProfile. If the layer name is empty, no label will be plotted.
  547. */
  548. class WXDLLIMPEXP_MATHPLOT mpProfile : public mpLayer
  549. {
  550. public:
  551. /** @param name Label
  552. * @param flags Label alignment, pass one of #mpALIGN_BOTTOM, #mpALIGN_CENTER, #mpALIGN_TOP.
  553. */
  554. mpProfile( const wxString& name = wxEmptyString, int flags = mpALIGN_TOP );
  555. /** Get function value for argument.
  556. * Override this function in your implementation.
  557. * @param x Argument
  558. * @return Function value
  559. */
  560. virtual double GetY( double x ) = 0;
  561. /** Layer plot handler.
  562. * This implementation will plot the function in the visible area and
  563. * put a label according to the alignment specified.
  564. */
  565. virtual void Plot( wxDC& dc, mpWindow& w ) override;
  566. protected:
  567. int m_flags; // !< Holds label alignment
  568. DECLARE_DYNAMIC_CLASS( mpProfile )
  569. };
  570. /*@}*/
  571. // -----------------------------------------------------------------------------
  572. // mpLayer implementations - furniture (scales, ...)
  573. // -----------------------------------------------------------------------------
  574. /** @name mpLayer implementations - furniture (scales, ...)
  575. * @{*/
  576. /** Plot layer implementing a x-scale ruler.
  577. * The ruler is fixed at Y=0 in the coordinate system. A label is plotted at
  578. * the bottom-right hand of the ruler. The scale numbering automatically
  579. * adjusts to view and zoom factor.
  580. */
  581. class WXDLLIMPEXP_MATHPLOT mpScaleBase : public mpLayer
  582. {
  583. public:
  584. mpScaleBase();
  585. virtual ~mpScaleBase() {};
  586. virtual bool IsHorizontal() = 0;
  587. bool HasBBox() override { return FALSE; }
  588. /** Set X axis alignment.
  589. * @param align alignment (choose between mpALIGN_BORDER_BOTTOM, mpALIGN_BOTTOM, mpALIGN_CENTER,
  590. * mpALIGN_TOP, mpALIGN_BORDER_TOP
  591. */
  592. void SetAlign( int align ) { m_flags = align; };
  593. void SetNameAlign( int align ) { m_nameFlags = align; }
  594. /** Set X axis ticks or grid
  595. * @param enable = true to plot axis ticks, false to plot grid.
  596. */
  597. void SetTicks( bool enable ) { m_ticks = enable; };
  598. /** Get X axis ticks or grid
  599. * @return TRUE if plot is drawing axis ticks, FALSE if the grid is active.
  600. */
  601. bool GetTicks() { return m_ticks; };
  602. // virtual double X2p( mpWindow &w, double x ) = 0;
  603. // virtual double P2x( mpWindow &w, double x ) = 0;
  604. void SetDataRange( double minV, double maxV )
  605. {
  606. m_rangeSet = true;
  607. m_minV = minV;
  608. m_maxV = maxV;
  609. }
  610. void GetDataRange( double& minV, double& maxV )
  611. {
  612. minV = m_minV;
  613. maxV = m_maxV;
  614. }
  615. void ExtendDataRange( double minV, double maxV )
  616. {
  617. if( !m_rangeSet )
  618. {
  619. m_minV = minV;
  620. m_maxV = maxV;
  621. m_rangeSet = true;
  622. }
  623. else
  624. {
  625. m_minV = std::min( minV, m_minV );
  626. m_maxV = std::max( maxV, m_maxV );
  627. }
  628. if( m_minV == m_maxV )
  629. {
  630. m_minV = m_minV - 1.0;
  631. m_maxV = m_maxV + 1.0;
  632. }
  633. }
  634. void ResetDataRange()
  635. {
  636. m_rangeSet = 0;
  637. }
  638. double AbsMaxValue() const
  639. {
  640. return std::max( std::abs( m_maxV ), std::abs( m_minV ) );
  641. }
  642. double AbsVisibleMaxValue() const
  643. {
  644. return m_absVisibleMaxV;
  645. }
  646. virtual double TransformToPlot( double x ) { return 0.0; };
  647. virtual double TransformFromPlot( double xplot ) { return 0.0; };
  648. struct TickLabel
  649. {
  650. TickLabel( double pos_ = 0.0, const wxString& label_ = wxT("") ) :
  651. pos( pos_ ), label( label_ ), pixelPos( 0 ), visible( true )
  652. {}
  653. double pos;
  654. wxString label;
  655. int pixelPos;
  656. bool visible;
  657. };
  658. std::vector<TickLabel>& TickLabels() { return m_tickLabels; };
  659. protected:
  660. void updateTickLabels( wxDC& dc, mpWindow& w );
  661. void computeLabelExtents( wxDC& dc, mpWindow& w );
  662. // virtual int getLabelDecimalDigits(int maxDigits);
  663. virtual void getVisibleDataRange( mpWindow& w, double& minV, double& maxV ) {};
  664. virtual void recalculateTicks( wxDC& dc, mpWindow& w ) {};
  665. int tickCount() const
  666. {
  667. return m_tickValues.size();
  668. }
  669. virtual int labelCount() const
  670. {
  671. return m_tickLabels.size();
  672. }
  673. virtual const wxString formatLabel( double value, int nDigits ) { return wxT( "" ); }
  674. virtual void formatLabels() {};
  675. virtual double getTickPos( int n )
  676. {
  677. return m_tickValues[n];
  678. }
  679. virtual double getLabelPos( int n )
  680. {
  681. return m_tickLabels[n].pos;
  682. }
  683. virtual const wxString getLabel( int n )
  684. {
  685. return m_tickLabels[n].label;
  686. }
  687. std::vector<double> m_tickValues;
  688. std::vector<TickLabel> m_tickLabels;
  689. double m_offset, m_scale;
  690. double m_absVisibleMaxV;
  691. int m_flags; // !< Flag for axis alignment
  692. int m_nameFlags;
  693. bool m_ticks; // !< Flag to toggle between ticks or grid
  694. double m_minV, m_maxV;
  695. bool m_rangeSet;
  696. int m_maxLabelHeight;
  697. int m_maxLabelWidth;
  698. };
  699. class WXDLLIMPEXP_MATHPLOT mpScaleXBase : public mpScaleBase
  700. {
  701. public:
  702. /** Full constructor.
  703. * @param name Label to plot by the ruler
  704. * @param flags Set the position of the scale with respect to the window.
  705. * @param ticks Select ticks or grid. Give TRUE (default) for drawing axis ticks, FALSE for drawing the grid.
  706. * @param type mpX_NORMAL for normal labels, mpX_TIME for time axis in hours, minutes, seconds.
  707. */
  708. mpScaleXBase( const wxString& name = wxT("X"), int flags = mpALIGN_CENTER,
  709. bool ticks = true, unsigned int type = mpX_NORMAL );
  710. virtual ~mpScaleXBase() {};
  711. virtual bool IsHorizontal() override { return true; }
  712. /** Layer plot handler.
  713. * This implementation will plot the ruler adjusted to the visible area. */
  714. virtual void Plot( wxDC& dc, mpWindow& w ) override;
  715. virtual void getVisibleDataRange( mpWindow& w, double& minV, double& maxV ) override;
  716. // unsigned int m_labelType; //!< Select labels mode: mpX_NORMAL for normal labels, mpX_TIME for time axis in hours, minutes, seconds
  717. // wxString m_labelFormat; //!< Format string used to print labels
  718. DECLARE_DYNAMIC_CLASS( mpScaleXBase )
  719. };
  720. class WXDLLIMPEXP_MATHPLOT mpScaleX : public mpScaleXBase
  721. {
  722. public:
  723. /** Full constructor.
  724. * @param name Label to plot by the ruler
  725. * @param flags Set the position of the scale with respect to the window.
  726. * @param ticks Select ticks or grid. Give TRUE (default) for drawing axis ticks, FALSE for drawing the grid.
  727. * @param type mpX_NORMAL for normal labels, mpX_TIME for time axis in hours, minutes, seconds. */
  728. mpScaleX( const wxString& name = wxT("X"), int flags = mpALIGN_CENTER,
  729. bool ticks = true, unsigned int type = mpX_NORMAL );
  730. /** Layer plot handler.
  731. * This implementation will plot the ruler adjusted to the visible area. */
  732. // virtual void Plot(wxDC & dc, mpWindow & w);
  733. // virtual double X2p( mpWindow &w, double x );
  734. // virtual double P2x( mpWindow &w, double x );
  735. virtual double TransformToPlot( double x ) override;
  736. virtual double TransformFromPlot( double xplot ) override;
  737. protected:
  738. virtual void recalculateTicks( wxDC& dc, mpWindow& w ) override;
  739. DECLARE_DYNAMIC_CLASS( mpScaleX )
  740. };
  741. class WXDLLIMPEXP_MATHPLOT mpScaleXLog : public mpScaleXBase
  742. {
  743. public:
  744. /** Full constructor.
  745. * @param name Label to plot by the ruler
  746. * @param flags Set the position of the scale with respect to the window.
  747. * @param ticks Select ticks or grid. Give TRUE (default) for drawing axis ticks, FALSE for drawing the grid.
  748. * @param type mpX_NORMAL for normal labels, mpX_TIME for time axis in hours, minutes, seconds.
  749. */
  750. mpScaleXLog( const wxString& name = wxT("log(X)"), int flags = mpALIGN_CENTER,
  751. bool ticks = true, unsigned int type = mpX_NORMAL );
  752. virtual double TransformToPlot( double x ) override;
  753. virtual double TransformFromPlot( double xplot ) override;
  754. /** Layer plot handler.
  755. * This implementation will plot the ruler adjusted to the visible area.
  756. */
  757. // virtual double X2p( mpWindow &w, double x );
  758. // virtual double P2x( mpWindow &w, double x );
  759. protected:
  760. void recalculateTicks( wxDC& dc, mpWindow& w ) override;
  761. // int tickCount() const;
  762. // int labelCount() const;
  763. // const wxString getLabel( int n );
  764. // double getTickPos( int n );
  765. // double getLabelPos( int n );
  766. void computeLabelExtents( wxDC& dc, mpWindow& w );
  767. DECLARE_DYNAMIC_CLASS( mpScaleXLog )
  768. };
  769. /** Plot layer implementing a y-scale ruler.
  770. * If align is set to mpALIGN_CENTER, the ruler is fixed at X=0 in the coordinate system.
  771. * If the align is set to mpALIGN_TOP or mpALIGN_BOTTOM, the axis is always
  772. * drawn respectively at top or bottom of the window. A label is plotted at
  773. * the top-right hand of the ruler.
  774. * The scale numbering automatically adjusts to view and zoom factor.
  775. */
  776. class WXDLLIMPEXP_MATHPLOT mpScaleY : public mpScaleBase
  777. {
  778. public:
  779. /** @param name Label to plot by the ruler
  780. * @param flags Set position of the scale respect to the window.
  781. * @param ticks Select ticks or grid. Give TRUE (default) for drawing axis ticks, FALSE for drawing the grid
  782. */
  783. mpScaleY( const wxString& name = wxT("Y"), int flags = mpALIGN_CENTER, bool ticks = true );
  784. virtual bool IsHorizontal() override { return false; }
  785. /** Layer plot handler.
  786. * This implementation will plot the ruler adjusted to the visible area.
  787. */
  788. virtual void Plot( wxDC& dc, mpWindow& w ) override;
  789. /** Check whether this layer has a bounding box.
  790. * This implementation returns \a FALSE thus making the ruler invisible
  791. * to the plot layer bounding box calculation by mpWindow.
  792. */
  793. virtual bool HasBBox() override { return FALSE; }
  794. /** Set Y axis alignment.
  795. * @param align alignment (choose between mpALIGN_BORDER_LEFT, mpALIGN_LEFT, mpALIGN_CENTER, mpALIGN_RIGHT, mpALIGN_BORDER_RIGHT) */
  796. void SetAlign( int align ) { m_flags = align; };
  797. /** Set Y axis ticks or grid
  798. * @param ticks TRUE to plot axis ticks, FALSE to plot grid. */
  799. void SetTicks( bool ticks ) { m_ticks = ticks; };
  800. /** Get Y axis ticks or grid
  801. * @return TRUE if plot is drawing axis ticks, FALSE if the grid is active. */
  802. bool GetTicks() { return m_ticks; };
  803. virtual double TransformToPlot( double x ) override;
  804. virtual double TransformFromPlot( double xplot ) override;
  805. void SetMasterScale( mpScaleY* masterScale )
  806. {
  807. m_masterScale = masterScale;
  808. }
  809. protected:
  810. virtual void getVisibleDataRange( mpWindow& w, double& minV, double& maxV ) override;
  811. virtual void recalculateTicks( wxDC& dc, mpWindow& w ) override;
  812. // virtual int tickCount() const;
  813. // virtual int labelCount() const;
  814. // virtual const wxString getLabel( int n );
  815. // virtual double getTickPos( int n );
  816. // virtual double getLabelPos( int n );
  817. void computeLabelExtents( wxDC& dc, mpWindow& w );
  818. void computeSlaveTicks( mpWindow& w );
  819. mpScaleY* m_masterScale;
  820. // double m_minV, m_maxV;
  821. int m_flags; // !< Flag for axis alignment
  822. bool m_ticks; // !< Flag to toggle between ticks or grid
  823. // wxString m_labelFormat; //!< Format string used to print labels
  824. DECLARE_DYNAMIC_CLASS( mpScaleY )
  825. };
  826. // -----------------------------------------------------------------------------
  827. // mpWindow
  828. // -----------------------------------------------------------------------------
  829. /** @name Constants defining mouse modes for mpWindow
  830. * @{*/
  831. /** Mouse panning drags the view. Mouse mode for mpWindow. */
  832. #define mpMOUSEMODE_DRAG 0
  833. /** Mouse panning creates a zoom box. Mouse mode for mpWindow. */
  834. #define mpMOUSEMODE_ZOOMBOX 1
  835. /*@}*/
  836. /** Define the type for the list of layers inside mpWindow */
  837. // WX_DECLARE_HASH_MAP( int, mpLayer*, wxIntegerHash, wxIntegerEqual, wxLayerList );
  838. typedef std::deque<mpLayer*> wxLayerList;
  839. /** Canvas for plotting mpLayer implementations.
  840. *
  841. * This class defines a zoomable and moveable 2D plot canvas. Any number
  842. * of mpLayer implementations (scale rulers, function plots, ...) can be
  843. * attached using mpWindow::AddLayer.
  844. *
  845. * The canvas window provides a context menu with actions for navigating the view.
  846. * The context menu can be retrieved with mpWindow::GetPopupMenu, e.g. for extending it
  847. * externally.
  848. *
  849. * Since wxMathPlot version 0.03, the mpWindow incorporates the following features:
  850. * - DoubleBuffering (Default=disabled): Can be set with EnableDoubleBuffer
  851. * - Mouse based pan/zoom (Default=enabled): Can be set with EnableMousePanZoom.
  852. *
  853. * The mouse commands can be visualized by the user through the popup menu, and are:
  854. * - Mouse Move+CTRL: Pan (Move)
  855. * - Mouse Wheel: Vertical scroll
  856. * - Mouse Wheel+SHIFT: Horizontal scroll
  857. * - Mouse Wheel UP+CTRL: Zoom in
  858. * - Mouse Wheel DOWN+CTRL: Zoom out
  859. *
  860. */
  861. class WXDLLIMPEXP_MATHPLOT mpWindow : public wxWindow
  862. {
  863. public:
  864. mpWindow() {}
  865. mpWindow( wxWindow* parent, wxWindowID id,
  866. const wxPoint& pos = wxDefaultPosition,
  867. const wxSize& size = wxDefaultSize,
  868. long flags = 0 );
  869. ~mpWindow();
  870. /** Get reference to context menu of the plot canvas.
  871. * @return Pointer to menu. The menu can be modified.
  872. */
  873. wxMenu* GetPopupMenu() { return &m_popmenu; }
  874. /** Add a plot layer to the canvas.
  875. * @param layer Pointer to layer. The mpLayer object will get under control of mpWindow,
  876. * i.e. it will be delete'd on mpWindow destruction
  877. * @param refreshDisplay States whether to refresh the display (UpdateAll) after adding the layer.
  878. * @retval TRUE Success
  879. * @retval FALSE Failure due to out of memory.
  880. */
  881. bool AddLayer( mpLayer* layer, bool refreshDisplay = true );
  882. /** Remove a plot layer from the canvas.
  883. * @param layer Pointer to layer. The mpLayer object will be destructed using delete.
  884. * @param alsoDeleteObject If set to true, the mpLayer object will be also "deleted", not just removed from the internal list.
  885. * @param refreshDisplay States whether to refresh the display (UpdateAll) after removing the layer.
  886. * @return true if layer is deleted correctly
  887. *
  888. * N.B. Only the layer reference in the mpWindow is deleted, the layer object still exists!
  889. */
  890. bool DelLayer( mpLayer* layer, bool alsoDeleteObject = false, bool refreshDisplay = true );
  891. /** Remove all layers from the plot.
  892. * @param alsoDeleteObject If set to true, the mpLayer objects will be also "deleted", not just removed from the internal list.
  893. * @param refreshDisplay States whether to refresh the display (UpdateAll) after removing the layers.
  894. */
  895. void DelAllLayers( bool alsoDeleteObject, bool refreshDisplay = true );
  896. /*! Get the layer in list position indicated.
  897. * N.B. You <i>must</i> know the index of the layer inside the list!
  898. * @param position position of the layer in the layers list
  899. * @return pointer to mpLayer
  900. */
  901. mpLayer* GetLayer( int position );
  902. /*! Get the layer by its name (case sensitive).
  903. * @param name The name of the layer to retrieve
  904. * @return A pointer to the mpLayer object, or NULL if not found.
  905. */
  906. mpLayer* GetLayerByName( const wxString& name );
  907. /** Get current view's X scale.
  908. * See @ref mpLayer::Plot "rules for coordinate transformation"
  909. * @return Scale
  910. */
  911. double GetXscl() { return m_scaleX; }
  912. double GetScaleX( void ) const { return m_scaleX; }; // Schaling's method: maybe another method esists with the same name
  913. /** Get current view's Y scale.
  914. * See @ref mpLayer::Plot "rules for coordinate transformation"
  915. * @return Scale
  916. */
  917. double GetYscl() const { return m_scaleY; }
  918. double GetScaleY( void ) const { return m_scaleY; } // Schaling's method: maybe another method exists with the same name
  919. /** Get current view's X position.
  920. * See @ref mpLayer::Plot "rules for coordinate transformation"
  921. * @return X Position in layer coordinate system, that corresponds to the center point of the view.
  922. */
  923. double GetXpos() const { return m_posX; }
  924. double GetPosX( void ) const { return m_posX; }
  925. /** Get current view's Y position.
  926. * See @ref mpLayer::Plot "rules for coordinate transformation"
  927. * @return Y Position in layer coordinate system, that corresponds to the center point of the view.
  928. */
  929. double GetYpos() const { return m_posY; }
  930. double GetPosY( void ) const { return m_posY; }
  931. /** Get current view's X dimension in device context units.
  932. * Usually this is equal to wxDC::GetSize, but it might differ thus mpLayer
  933. * implementations should rely on the value returned by the function.
  934. * See @ref mpLayer::Plot "rules for coordinate transformation"
  935. * @return X dimension.
  936. */
  937. int GetScrX( void ) const { return m_scrX; }
  938. int GetXScreen( void ) const { return m_scrX; }
  939. /** Get current view's Y dimension in device context units.
  940. * Usually this is equal to wxDC::GetSize, but it might differ thus mpLayer
  941. * implementations should rely on the value returned by the function.
  942. * See @ref mpLayer::Plot "rules for coordinate transformation"
  943. * @return Y dimension.
  944. */
  945. int GetScrY( void ) const { return m_scrY; }
  946. int GetYScreen( void ) const { return m_scrY; }
  947. /** Set current view's X scale and refresh display.
  948. * @param scaleX New scale, must not be 0.
  949. */
  950. void SetScaleX( double scaleX );
  951. /** Set current view's Y scale and refresh display.
  952. * @param scaleY New scale, must not be 0.
  953. */
  954. void SetScaleY( double scaleY )
  955. {
  956. if( scaleY!=0 )
  957. m_scaleY = scaleY;
  958. UpdateAll();
  959. }
  960. /** Set current view's X position and refresh display.
  961. * @param posX New position that corresponds to the center point of the view.
  962. */
  963. void SetPosX( double posX ) { m_posX = posX; UpdateAll(); }
  964. /** Set current view's Y position and refresh display.
  965. * @param posY New position that corresponds to the center point of the view.
  966. */
  967. void SetPosY( double posY ) { m_posY = posY; UpdateAll(); }
  968. /** Set current view's X and Y position and refresh display.
  969. * @param posX New position that corresponds to the center point of the view.
  970. * @param posY New position that corresponds to the center point of the view.
  971. */
  972. void SetPos( double posX, double posY ) { m_posX = posX; m_posY = posY; UpdateAll(); }
  973. /** Set current view's dimensions in device context units.
  974. * Needed by plotting functions. It doesn't refresh display.
  975. * @param scrX New position that corresponds to the center point of the view.
  976. * @param scrY New position that corresponds to the center point of the view.
  977. */
  978. void SetScr( int scrX, int scrY ) { m_scrX = scrX; m_scrY = scrY; }
  979. /** Converts mpWindow (screen) pixel coordinates into graph (floating point) coordinates, using current mpWindow position and scale.
  980. * @sa p2y,x2p,y2p */
  981. // double p2x(wxCoord pixelCoordX, bool drawOutside = true ); // { return m_posX + pixelCoordX/m_scaleX; }
  982. inline double p2x( wxCoord pixelCoordX ) { return m_posX + pixelCoordX / m_scaleX; }
  983. /** Converts mpWindow (screen) pixel coordinates into graph (floating point) coordinates, using current mpWindow position and scale.
  984. * @sa p2x,x2p,y2p */
  985. // double p2y(wxCoord pixelCoordY, bool drawOutside = true ); //{ return m_posY - pixelCoordY/m_scaleY; }
  986. inline double p2y( wxCoord pixelCoordY ) { return m_posY - pixelCoordY / m_scaleY; }
  987. /** Converts graph (floating point) coordinates into mpWindow (screen) pixel coordinates, using current mpWindow position and scale.
  988. * @sa p2x,p2y,y2p */
  989. // wxCoord x2p(double x, bool drawOutside = true); // { return (wxCoord) ( (x-m_posX) * m_scaleX); }
  990. inline wxCoord x2p( double x ) { return (wxCoord) ( (x - m_posX) * m_scaleX ); }
  991. /** Converts graph (floating point) coordinates into mpWindow (screen) pixel coordinates, using current mpWindow position and scale.
  992. * @sa p2x,p2y,x2p */
  993. // wxCoord y2p(double y, bool drawOutside = true); // { return (wxCoord) ( (m_posY-y) * m_scaleY); }
  994. inline wxCoord y2p( double y ) { return (wxCoord) ( (m_posY - y) * m_scaleY ); }
  995. /** Enable/disable the double-buffering of the window, eliminating the flicker (default=disabled).
  996. */
  997. void EnableDoubleBuffer( bool enabled ) { m_enableDoubleBuffer = enabled; }
  998. /** Enable/disable the feature of pan/zoom with the mouse (default=enabled)
  999. */
  1000. void EnableMousePanZoom( bool enabled ) { m_enableMouseNavigation = enabled; }
  1001. /** Enable/disable trackpad friendly panning (2-axis scroll wheel)
  1002. */
  1003. void EnableMouseWheelPan( bool enabled ) { m_enableMouseWheelPan = enabled; }
  1004. /** Enable or disable X/Y scale aspect locking for the view.
  1005. * @note Explicit calls to mpWindow::SetScaleX and mpWindow::SetScaleY will set
  1006. * an unlocked aspect, but any other action changing the view scale will
  1007. * lock the aspect again.
  1008. */
  1009. void LockAspect( bool enable = TRUE );
  1010. /** Checks whether the X/Y scale aspect is locked.
  1011. * @retval TRUE Locked
  1012. * @retval FALSE Unlocked
  1013. */
  1014. inline bool IsAspectLocked() { return m_lockaspect; }
  1015. /** Set view to fit global bounding box of all plot layers and refresh display.
  1016. * Scale and position will be set to show all attached mpLayers.
  1017. * The X/Y scale aspect lock is taken into account.
  1018. */
  1019. void Fit() override;
  1020. /** Set view to fit a given bounding box and refresh display.
  1021. * The X/Y scale aspect lock is taken into account.
  1022. * If provided, the parameters printSizeX and printSizeY are taken as the DC size, and the
  1023. * pixel scales are computed accordingly. Also, in this case the passed borders are not saved
  1024. * as the "desired borders", since this use will be invoked only when printing.
  1025. */
  1026. void Fit( double xMin, double xMax, double yMin, double yMax,
  1027. wxCoord* printSizeX = NULL, wxCoord* printSizeY = NULL );
  1028. /** Zoom into current view and refresh display
  1029. * @param centerPoint The point (pixel coordinates) that will stay in the same
  1030. * position on the screen after the zoom (by default, the center of the mpWindow).
  1031. */
  1032. void ZoomIn( const wxPoint& centerPoint = wxDefaultPosition );
  1033. void ZoomIn( const wxPoint& centerPoint, double zoomFactor );
  1034. /** Zoom out current view and refresh display
  1035. * @param centerPoint The point (pixel coordinates) that will stay in the same
  1036. * position on the screen after the zoom (by default, the center of the mpWindow).
  1037. */
  1038. void ZoomOut( const wxPoint& centerPoint = wxDefaultPosition );
  1039. void ZoomOut( const wxPoint& centerPoint, double zoomFactor );
  1040. /** Zoom in current view along X and refresh display */
  1041. void ZoomInX();
  1042. /** Zoom out current view along X and refresh display */
  1043. void ZoomOutX();
  1044. /** Zoom in current view along Y and refresh display */
  1045. void ZoomInY();
  1046. /** Zoom out current view along Y and refresh display */
  1047. void ZoomOutY();
  1048. /** Zoom view fitting given coordinates to the window (p0 and p1 do not need to be in any specific order)
  1049. */
  1050. void ZoomRect( wxPoint p0, wxPoint p1 );
  1051. /** Refresh display */
  1052. void UpdateAll();
  1053. // Added methods by Davide Rondini
  1054. /** Counts the number of plot layers, excluding axes or text: this is to count only the layers
  1055. * which have a bounding box.
  1056. * \return The number of profiles plotted.
  1057. */
  1058. unsigned int CountLayers();
  1059. /** Counts the number of plot layers, whether or not they have a bounding box.
  1060. * \return The number of layers in the mpWindow. */
  1061. unsigned int CountAllLayers() { return m_layers.size(); };
  1062. #if 0
  1063. /** Draws the mpWindow on a page for printing
  1064. * \param print the mpPrintout where to print the graph
  1065. */
  1066. void PrintGraph(mpPrintout *print);
  1067. #endif
  1068. /** Returns the left-border layer coordinate that the user wants the mpWindow to show
  1069. * (it may be not exactly the actual shown coordinate in the case of locked aspect ratio).
  1070. * @sa Fit
  1071. */
  1072. double GetDesiredXmin() { return m_desiredXmin; }
  1073. /** Returns the right-border layer coordinate that the user wants the mpWindow to show
  1074. * (it may be not exactly the actual shown coordinate in the case of locked aspect ratio).
  1075. * @sa Fit
  1076. */
  1077. double GetDesiredXmax() { return m_desiredXmax; }
  1078. /** Returns the bottom-border layer coordinate that the user wants the mpWindow to show
  1079. * (it may be not exactly the actual shown coordinate in the case of locked aspect ratio).
  1080. * @sa Fit
  1081. */
  1082. double GetDesiredYmin() { return m_desiredYmin; }
  1083. /** Returns the top layer-border coordinate that the user wants the mpWindow to show
  1084. * (it may be not exactly the actual shown coordinate in the case of locked aspect ratio).
  1085. * @sa Fit
  1086. */
  1087. double GetDesiredYmax() { return m_desiredYmax; }
  1088. /** Returns the bounding box coordinates
  1089. * @param bbox Pointer to a 6-element double array where to store bounding box coordinates.
  1090. */
  1091. void GetBoundingBox( double* bbox );
  1092. /** Enable/disable scrollbars
  1093. * @param status Set to true to show scrollbars */
  1094. void SetMPScrollbars( bool status );
  1095. /** Get scrollbars status.
  1096. * @return true if scrollbars are visible */
  1097. bool GetMPScrollbars() { return m_enableScrollBars; };
  1098. /** Draw the window on a wxBitmap, then save it to a file.
  1099. * @param filename File name where to save the screenshot
  1100. * @param type image type to be saved: see wxImage output file types for flags
  1101. * @param imageSize Set a size for the output image. Default is the same as the screen size
  1102. * @param fit Decide whether to fit the plot into the size*/
  1103. bool SaveScreenshot( const wxString& filename, wxBitmapType type = wxBITMAP_TYPE_BMP,
  1104. wxSize imageSize = wxDefaultSize, bool fit = false );
  1105. /** This value sets the zoom steps whenever the user clicks "Zoom in/out" or performs zoom with the mouse wheel.
  1106. * It must be a number above unity. This number is used for zoom in, and its inverse for zoom out.
  1107. * Set to 1.5 by default.
  1108. */
  1109. static double zoomIncrementalFactor;
  1110. /** Set window margins, creating a blank area where some kinds of layers cannot draw.
  1111. * This is useful for example to draw axes outside the area where the plots are drawn.
  1112. * @param top Top border
  1113. * @param right Right border
  1114. * @param bottom Bottom border
  1115. * @param left Left border */
  1116. void SetMargins( int top, int right, int bottom, int left );
  1117. /** Set the top margin. @param top Top Margin */
  1118. void SetMarginTop( int top ) { m_marginTop = top; };
  1119. /** Set the right margin. @param right Right Margin */
  1120. void SetMarginRight( int right ) { m_marginRight = right; };
  1121. /** Set the bottom margin. @param bottom Bottom Margin */
  1122. void SetMarginBottom( int bottom ) { m_marginBottom = bottom; };
  1123. /** Set the left margin. @param left Left Margin */
  1124. void SetMarginLeft( int left ) { m_marginLeft = left; };
  1125. /** @return the top margin. */
  1126. int GetMarginTop() { return m_marginTop; };
  1127. /** @return the right margin. */
  1128. int GetMarginRight() { return m_marginRight; };
  1129. /** @return the bottom margin. */
  1130. int GetMarginBottom() { return m_marginBottom; };
  1131. /** @return the left margin. */
  1132. int GetMarginLeft() { return m_marginLeft; };
  1133. #if 0
  1134. /** Sets whether to show coordinate tooltip when mouse passes over the plot.
  1135. * \param value true for enable, false for disable
  1136. */
  1137. // void EnableCoordTooltip(bool value = true);
  1138. /** Gets coordinate tooltip status.
  1139. * \return true for enable, false for disable
  1140. */
  1141. // bool GetCoordTooltip() { return m_coordTooltip; };
  1142. #endif
  1143. /** Check if a given point is inside the area of a mpInfoLayer and eventually returns its pointer.
  1144. * @param point The position to be checked
  1145. * @return If an info layer is found, returns its pointer, NULL otherwise */
  1146. mpInfoLayer* IsInsideInfoLayer( wxPoint& point );
  1147. /** Sets the visibility of a layer by its name.
  1148. * @param name The layer name to set visibility
  1149. * @param viewable the view status to be set */
  1150. void SetLayerVisible( const wxString& name, bool viewable );
  1151. /** Check whether a layer with given name is visible
  1152. * @param name The layer name
  1153. * @return layer visibility status */
  1154. bool IsLayerVisible( const wxString& name );
  1155. /** Sets the visibility of a layer by its position in layer list.
  1156. * @param position The layer position in layer list
  1157. * @param viewable the view status to be set */
  1158. void SetLayerVisible( const unsigned int position, bool viewable );
  1159. /** Check whether the layer at given position is visible
  1160. * @param position The layer position in layer list
  1161. * @return layer visibility status */
  1162. bool IsLayerVisible( const unsigned int position );
  1163. /** Set Color theme. Provide colours to set a new colour theme.
  1164. * @param bgColour Background colour
  1165. * @param drawColour The colour used to draw all elements in foreground, axes excluded
  1166. * @param axesColour The colour used to draw axes (but not their labels) */
  1167. void SetColourTheme( const wxColour& bgColour,
  1168. const wxColour& drawColour,
  1169. const wxColour& axesColour );
  1170. /** Get axes draw colour
  1171. * @return reference to axis colour used in theme */
  1172. const wxColour& GetAxesColour() { return m_axColour; };
  1173. /** Limit zooming & panning to the area used by the plots */
  1174. void LimitView( bool aEnable )
  1175. {
  1176. m_enableLimitedView = aEnable;
  1177. }
  1178. protected:
  1179. void OnPaint( wxPaintEvent& event ); // !< Paint handler, will plot all attached layers
  1180. void OnSize( wxSizeEvent& event ); // !< Size handler, will update scroll bar sizes
  1181. // void OnScroll2 (wxScrollWinEvent &event); //!< Scroll handler, will move canvas
  1182. void OnShowPopupMenu( wxMouseEvent& event ); // !< Mouse handler, will show context menu
  1183. void OnMouseMiddleDown( wxMouseEvent& event ); // !< Mouse handler, for detecting when the user
  1184. // !< drags with the middle button or just "clicks" for the menu
  1185. void OnCenter( wxCommandEvent& event ); // !< Context menu handler
  1186. void OnFit( wxCommandEvent& event ); // !< Context menu handler
  1187. void OnZoomIn( wxCommandEvent& event ); // !< Context menu handler
  1188. void OnZoomOut( wxCommandEvent& event ); // !< Context menu handler
  1189. void OnLockAspect( wxCommandEvent& event ); // !< Context menu handler
  1190. void OnMouseWheel( wxMouseEvent& event ); // !< Mouse handler for the wheel
  1191. void OnMagnify( wxMouseEvent& event ); // !< Pinch zoom handler
  1192. void OnMouseMove( wxMouseEvent& event ); // !< Mouse handler for mouse motion (for pan)
  1193. void OnMouseLeftDown( wxMouseEvent& event ); // !< Mouse left click (for rect zoom)
  1194. void OnMouseLeftRelease( wxMouseEvent& event ); // !< Mouse left click (for rect zoom)
  1195. void OnScrollThumbTrack( wxScrollWinEvent& event ); // !< Scroll thumb on scroll bar moving
  1196. void OnScrollPageUp( wxScrollWinEvent& event ); // !< Scroll page up
  1197. void OnScrollPageDown( wxScrollWinEvent& event ); // !< Scroll page down
  1198. void OnScrollLineUp( wxScrollWinEvent& event ); // !< Scroll line up
  1199. void OnScrollLineDown( wxScrollWinEvent& event ); // !< Scroll line down
  1200. void OnScrollTop( wxScrollWinEvent& event ); // !< Scroll to top
  1201. void OnScrollBottom( wxScrollWinEvent& event ); // !< Scroll to bottom
  1202. void DoScrollCalc( const int position, const int orientation );
  1203. void DoZoomInXCalc( const int staticXpixel );
  1204. void DoZoomInYCalc( const int staticYpixel );
  1205. void DoZoomOutXCalc( const int staticXpixel );
  1206. void DoZoomOutYCalc( const int staticYpixel );
  1207. bool CheckXLimits( double& desiredMax, double& desiredMin ) const
  1208. {
  1209. return !( m_enableLimitedView
  1210. && (desiredMax > m_maxX - m_marginRight / m_scaleX
  1211. || desiredMin < m_minX - m_marginLeft / m_scaleX) );
  1212. }
  1213. bool CheckYLimits( double& desiredMax, double& desiredMin ) const
  1214. {
  1215. return !( m_enableLimitedView
  1216. && (desiredMax > m_maxY + m_marginBottom / m_scaleY
  1217. || desiredMin < m_minY + m_marginTop / m_scaleY) );
  1218. }
  1219. void AdjustLimitedView();
  1220. /** Recalculate global layer bounding box, and save it in m_minX,...
  1221. * \return true if there is any valid BBox information.
  1222. */
  1223. virtual bool UpdateBBox();
  1224. /** Applies new X view coordinates depending on the settings
  1225. * \return true if the changes were applied
  1226. */
  1227. virtual bool SetXView( double pos, double desiredMax, double desiredMin );
  1228. /** Applies new Y view coordinates depending on the settings
  1229. * \return true if the changes were applied
  1230. */
  1231. virtual bool SetYView( double pos, double desiredMax, double desiredMin );
  1232. // wxList m_layers; //!< List of attached plot layers
  1233. wxLayerList m_layers; // !< List of attached plot layers
  1234. wxMenu m_popmenu; // !< Canvas' context menu
  1235. bool m_lockaspect; // !< Scale aspect is locked or not
  1236. // bool m_coordTooltip; //!< Selects whether to show coordinate tooltip
  1237. wxColour m_bgColour; // !< Background Colour
  1238. wxColour m_fgColour; // !< Foreground Colour
  1239. wxColour m_axColour; // !< Axes Colour
  1240. double m_minX; // !< Global layer bounding box, left border incl.
  1241. double m_maxX; // !< Global layer bounding box, right border incl.
  1242. double m_minY; // !< Global layer bounding box, bottom border incl.
  1243. double m_maxY; // !< Global layer bounding box, top border incl.
  1244. double m_scaleX; // !< Current view's X scale
  1245. double m_scaleY; // !< Current view's Y scale
  1246. double m_posX; // !< Current view's X position
  1247. double m_posY; // !< Current view's Y position
  1248. int m_scrX; // !< Current view's X dimension
  1249. int m_scrY; // !< Current view's Y dimension
  1250. int m_clickedX; // !< Last mouse click X position, for centering and zooming the view
  1251. int m_clickedY; // !< Last mouse click Y position, for centering and zooming the view
  1252. /** These are updated in Fit() only, and may be different from the real borders
  1253. * (layer coordinates) only if lock aspect ratio is true.
  1254. */
  1255. double m_desiredXmin, m_desiredXmax, m_desiredYmin, m_desiredYmax;
  1256. int m_marginTop, m_marginRight, m_marginBottom, m_marginLeft;
  1257. int m_last_lx, m_last_ly; // !< For double buffering
  1258. wxMemoryDC m_buff_dc; // !< For double buffering
  1259. wxBitmap* m_buff_bmp; // !< For double buffering
  1260. bool m_enableDoubleBuffer; // !< For double buffering
  1261. bool m_enableMouseNavigation; // !< For pan/zoom with the mouse.
  1262. bool m_enableMouseWheelPan; // !< Trackpad pan/zoom
  1263. bool m_enableLimitedView;
  1264. wxPoint m_mouseMClick; // !< For the middle button "drag" feature
  1265. wxPoint m_mouseLClick; // !< Starting coords for rectangular zoom selection
  1266. bool m_enableScrollBars;
  1267. wxPoint m_scroll;
  1268. mpInfoLayer* m_movingInfoLayer; // !< For moving info layers over the window area
  1269. bool m_zooming;
  1270. wxRect m_zoomRect;
  1271. DECLARE_DYNAMIC_CLASS( mpWindow )
  1272. DECLARE_EVENT_TABLE()
  1273. };
  1274. // -----------------------------------------------------------------------------
  1275. // mpFXYVector - provided by Jose Luis Blanco
  1276. // -----------------------------------------------------------------------------
  1277. /** A class providing graphs functionality for a 2D plot (either continuous or a set of points), from vectors of data.
  1278. * This class can be used directly, the user does not need to derive any new class. Simply pass the data as two vectors
  1279. * with the same length containing the X and Y coordinates to the method SetData.
  1280. *
  1281. * To generate a graph with a set of points, call
  1282. * \code
  1283. * layerVar->SetContinuity(false)
  1284. * \endcode
  1285. *
  1286. * or
  1287. *
  1288. * \code
  1289. * layerVar->SetContinuity(true)
  1290. * \endcode
  1291. *
  1292. * to render the sequence of coordinates as a continuous line.
  1293. *
  1294. * (Added: Jose Luis Blanco, AGO-2007)
  1295. */
  1296. class WXDLLIMPEXP_MATHPLOT mpFXYVector : public mpFXY
  1297. {
  1298. public:
  1299. /** @param name Label
  1300. * @param flags Label alignment, pass one of #mpALIGN_NE, #mpALIGN_NW, #mpALIGN_SW, #mpALIGN_SE.
  1301. */
  1302. mpFXYVector( const wxString& name = wxEmptyString, int flags = mpALIGN_NE );
  1303. virtual ~mpFXYVector() {}
  1304. /** Changes the internal data: the set of points to draw.
  1305. * Both vectors MUST be of the same length. This method DOES NOT refresh the mpWindow; do it manually.
  1306. * @sa Clear
  1307. */
  1308. virtual void SetData( const std::vector<double>& xs, const std::vector<double>& ys );
  1309. /** Clears all the data, leaving the layer empty.
  1310. * @sa SetData
  1311. */
  1312. void Clear();
  1313. protected:
  1314. /** The internal copy of the set of data to draw.
  1315. */
  1316. std::vector<double> m_xs, m_ys;
  1317. /** The internal counter for the "GetNextXY" interface
  1318. */
  1319. size_t m_index;
  1320. /** Loaded at SetData
  1321. */
  1322. double m_minX, m_maxX, m_minY, m_maxY;
  1323. /** Rewind value enumeration with mpFXY::GetNextXY.
  1324. * Overridden in this implementation.
  1325. */
  1326. void Rewind() override;
  1327. /** Get locus value for next N.
  1328. * Overridden in this implementation.
  1329. * @param x Returns X value
  1330. * @param y Returns Y value
  1331. */
  1332. bool GetNextXY( double& x, double& y ) override;
  1333. size_t GetCount() override;
  1334. public:
  1335. /** Returns the actual minimum X data (loaded in SetData).
  1336. */
  1337. double GetMinX() override { return m_minX; }
  1338. /** Returns the actual minimum Y data (loaded in SetData).
  1339. */
  1340. double GetMinY() override { return m_minY; }
  1341. /** Returns the actual maximum X data (loaded in SetData).
  1342. */
  1343. double GetMaxX() override { return m_maxX; }
  1344. /** Returns the actual maximum Y data (loaded in SetData).
  1345. */
  1346. double GetMaxY() override { return m_maxY; }
  1347. protected:
  1348. DECLARE_DYNAMIC_CLASS( mpFXYVector )
  1349. };
  1350. #if 0
  1351. class WXDLLIMPEXP_MATHPLOT mpFSemiLogXVector : public mpFXYVector
  1352. {
  1353. public:
  1354. /** @param name Label
  1355. * @param flags Label alignment, pass one of #mpALIGN_NE, #mpALIGN_NW, #mpALIGN_SW, #mpALIGN_SE.
  1356. */
  1357. mpFSemiLogXVector( wxString name = wxEmptyString, int flags = mpALIGN_NE );
  1358. virtual ~mpFSemiLogXVector() {}
  1359. /** Changes the internal data: the set of points to draw.
  1360. * Both vectors MUST be of the same length. This method DOES NOT refresh the mpWindow; do it manually.
  1361. * @sa Clear
  1362. */
  1363. DECLARE_DYNAMIC_CLASS( mpFSemiLogXVector )
  1364. };
  1365. #endif
  1366. // -----------------------------------------------------------------------------
  1367. // mpText - provided by Val Greene
  1368. // -----------------------------------------------------------------------------
  1369. /** Plot layer implementing a text string.
  1370. * The text is plotted using a percentage system 0-100%, so the actual
  1371. * coordinates for the location are not required, and the text stays
  1372. * on the plot reguardless of the other layers location and scaling
  1373. * factors.
  1374. */
  1375. class WXDLLIMPEXP_MATHPLOT mpText : public mpLayer
  1376. {
  1377. public:
  1378. /** @param name text to be drawn in the plot
  1379. * @param offsetx holds offset for the X location in percentage (0-100)
  1380. * @param offsety holds offset for the Y location in percentage (0-100) */
  1381. mpText( const wxString& name = wxT("Title"), int offsetx = 5, int offsety = 50 );
  1382. /** Text Layer plot handler.
  1383. * This implementation will plot text adjusted to the visible area. */
  1384. virtual void Plot( wxDC& dc, mpWindow& w ) override;
  1385. /** mpText should not be used for scaling decisions. */
  1386. virtual bool HasBBox() override { return FALSE; }
  1387. protected:
  1388. int m_offsetx; // !< Holds offset for X in percentage
  1389. int m_offsety; // !< Holds offset for Y in percentage
  1390. DECLARE_DYNAMIC_CLASS( mpText )
  1391. };
  1392. // -----------------------------------------------------------------------------
  1393. // mpPrintout - provided by Davide Rondini
  1394. // -----------------------------------------------------------------------------
  1395. /** Printout class used by mpWindow to draw in the objects to be printed.
  1396. * The object itself can then used by the default wxWidgets printing system
  1397. * to print mppWindow objects.
  1398. */
  1399. class WXDLLIMPEXP_MATHPLOT mpPrintout : public wxPrintout
  1400. {
  1401. public:
  1402. mpPrintout( mpWindow* drawWindow, const wxChar* title = _T("wxMathPlot print output") );
  1403. virtual ~mpPrintout() {};
  1404. void SetDrawState( bool drawState ) { drawn = drawState; };
  1405. bool OnPrintPage( int page ) override;
  1406. bool HasPage( int page ) override;
  1407. private:
  1408. bool drawn;
  1409. mpWindow* plotWindow;
  1410. };
  1411. // -----------------------------------------------------------------------------
  1412. // mpMovableObject - provided by Jose Luis Blanco
  1413. // -----------------------------------------------------------------------------
  1414. /** This virtual class represents objects that can be moved to an arbitrary 2D location+rotation.
  1415. * The current transformation is set through SetCoordinateBase.
  1416. * To ease the implementation of descendent classes, mpMovableObject will
  1417. * be in charge of Bounding Box computation and layer rendering, assuming that
  1418. * the object updates its shape in m_shape_xs & m_shape_ys.
  1419. */
  1420. class WXDLLIMPEXP_MATHPLOT mpMovableObject : public mpLayer
  1421. {
  1422. public:
  1423. /** Default constructor (sets location and rotation to (0,0,0))
  1424. */
  1425. mpMovableObject() :
  1426. m_reference_x( 0 ),
  1427. m_reference_y( 0 ),
  1428. m_reference_phi( 0 ),
  1429. m_shape_xs( 0 ),
  1430. m_shape_ys( 0 )
  1431. {
  1432. m_type = mpLAYER_PLOT;
  1433. }
  1434. virtual ~mpMovableObject() {};
  1435. /** Get the current coordinate transformation.
  1436. */
  1437. void GetCoordinateBase( double& x, double& y, double& phi ) const
  1438. {
  1439. x = m_reference_x;
  1440. y = m_reference_y;
  1441. phi = m_reference_phi;
  1442. }
  1443. /** Set the coordinate transformation (phi in radians, 0 means no rotation).
  1444. */
  1445. void SetCoordinateBase( double x, double y, double phi = 0 )
  1446. {
  1447. m_reference_x = x;
  1448. m_reference_y = y;
  1449. m_reference_phi = phi;
  1450. m_flags = mpALIGN_NE;
  1451. ShapeUpdated();
  1452. }
  1453. virtual bool HasBBox() override { return m_trans_shape_xs.size()!=0; }
  1454. /** Get inclusive left border of bounding box.
  1455. */
  1456. virtual double GetMinX() override { return m_bbox_min_x; }
  1457. /** Get inclusive right border of bounding box.
  1458. */
  1459. virtual double GetMaxX() override { return m_bbox_max_x; }
  1460. /** Get inclusive bottom border of bounding box.
  1461. */
  1462. virtual double GetMinY() override { return m_bbox_min_y; }
  1463. /** Get inclusive top border of bounding box.
  1464. */
  1465. virtual double GetMaxY() override { return m_bbox_max_y; }
  1466. virtual void Plot( wxDC& dc, mpWindow& w ) override;
  1467. /** Set label axis alignment.
  1468. * @param align alignment (choose between mpALIGN_NE, mpALIGN_NW, mpALIGN_SW, mpALIGN_SE
  1469. */
  1470. void SetAlign( int align ) { m_flags = align; };
  1471. protected:
  1472. int m_flags; // !< Holds label alignment
  1473. /** The coordinates of the object (orientation "phi" is in radians).
  1474. */
  1475. double m_reference_x, m_reference_y, m_reference_phi;
  1476. /** A method for 2D translation and rotation, using the current transformation
  1477. * stored in m_reference_x,m_reference_y,m_reference_phi.
  1478. */
  1479. void TranslatePoint( double x, double y, double& out_x, double& out_y );
  1480. /** This contains the object points, in local coordinates
  1481. * (to be transformed by the current transformation).
  1482. */
  1483. std::vector<double> m_shape_xs, m_shape_ys;
  1484. /** The buffer for the translated & rotated points (to avoid recomputing them with each mpWindow refresh).
  1485. *
  1486. */
  1487. std::vector<double> m_trans_shape_xs, m_trans_shape_ys;
  1488. /** The precomputed bounding box:
  1489. * @sa ShapeUpdated
  1490. */
  1491. double m_bbox_min_x, m_bbox_max_x, m_bbox_min_y, m_bbox_max_y;
  1492. /** Must be called by the descendent class after updating the shape (m_shape_xs/ys),
  1493. * or when the transformation changes.
  1494. * This method updates the buffers m_trans_shape_xs/ys, and the precomputed bounding box.
  1495. */
  1496. void ShapeUpdated();
  1497. };
  1498. // -----------------------------------------------------------------------------
  1499. // mpCovarianceEllipse - provided by Jose Luis Blanco
  1500. // -----------------------------------------------------------------------------
  1501. /** A 2D ellipse, described by a 2x2 covariance matrix.
  1502. * The relation between the multivariate Gaussian confidence interval and
  1503. * the "quantiles" in this class is:
  1504. * - 1 : 68.27% confidence interval
  1505. * - 2 : 95.45%
  1506. * - 3 : 99.73%
  1507. * - 4 : 99.994%
  1508. * For example, see http://en.wikipedia.org/wiki/Normal_distribution#Standard_deviation_and_confidence_intervals
  1509. *
  1510. * The ellipse will be always centered at the origin. Use mpMovableObject::SetCoordinateBase to move it.
  1511. */
  1512. class WXDLLIMPEXP_MATHPLOT mpCovarianceEllipse : public mpMovableObject
  1513. {
  1514. public:
  1515. /** Default constructor.
  1516. * Initializes to a unity diagonal covariance matrix, a 95% confidence interval (2 sigmas),
  1517. * 32 segments, and a continuous plot (m_continuous=true).
  1518. */
  1519. mpCovarianceEllipse( double cov_00 = 1,
  1520. double cov_11 = 1,
  1521. double cov_01 = 0,
  1522. double quantiles = 2,
  1523. int segments = 32,
  1524. const wxString& layerName = wxT("") ) :
  1525. m_cov_00( cov_00 ),
  1526. m_cov_11( cov_11 ),
  1527. m_cov_01( cov_01 ),
  1528. m_quantiles( quantiles ),
  1529. m_segments( segments )
  1530. {
  1531. m_continuous = true;
  1532. m_name = layerName;
  1533. RecalculateShape();
  1534. m_type = mpLAYER_PLOT;
  1535. }
  1536. virtual ~mpCovarianceEllipse() {}
  1537. double GetQuantiles() const { return m_quantiles; }
  1538. /** Set how many "quantiles" to draw, that is, the confidence interval of the ellipse (see above).
  1539. */
  1540. void SetQuantiles( double q )
  1541. {
  1542. m_quantiles = q;
  1543. RecalculateShape();
  1544. }
  1545. void SetSegments( int segments ) { m_segments = segments; }
  1546. int GetSegments() const { return m_segments; }
  1547. /** Returns the elements of the current covariance matrix:
  1548. */
  1549. void GetCovarianceMatrix( double& cov_00, double& cov_01, double& cov_11 ) const
  1550. {
  1551. cov_00 = m_cov_00;
  1552. cov_01 = m_cov_01;
  1553. cov_11 = m_cov_11;
  1554. }
  1555. /** Changes the covariance matrix:
  1556. */
  1557. void SetCovarianceMatrix( double cov_00, double cov_01, double cov_11 )
  1558. {
  1559. m_cov_00 = cov_00;
  1560. m_cov_01 = cov_01;
  1561. m_cov_11 = cov_11;
  1562. RecalculateShape();
  1563. }
  1564. protected:
  1565. /** The elements of the matrix (only 3 since cov(0,1)=cov(1,0) in any positive definite matrix).
  1566. */
  1567. double m_cov_00, m_cov_11, m_cov_01;
  1568. double m_quantiles;
  1569. /** The number of line segments that build up the ellipse.
  1570. */
  1571. int m_segments;
  1572. /** Called to update the m_shape_xs, m_shape_ys vectors, whenever a parameter changes.
  1573. */
  1574. void RecalculateShape();
  1575. };
  1576. // -----------------------------------------------------------------------------
  1577. // mpPolygon - provided by Jose Luis Blanco
  1578. // -----------------------------------------------------------------------------
  1579. /** An arbitrary polygon, descendant of mpMovableObject.
  1580. * Use "setPoints" to set the list of N points. This class also can draw non-closed polygons by
  1581. * passing the appropriate parameters to "setPoints". To draw a point-cloud, call "SetContinuity(false)".
  1582. */
  1583. class WXDLLIMPEXP_MATHPLOT mpPolygon : public mpMovableObject
  1584. {
  1585. public:
  1586. /** Default constructor.
  1587. */
  1588. mpPolygon( const wxString& layerName = wxT("") )
  1589. {
  1590. m_continuous = true;
  1591. m_name = layerName;
  1592. }
  1593. virtual ~mpPolygon() {}
  1594. /** Set the points in the polygon.
  1595. * @param points_xs The X coordinates of the points.
  1596. * @param points_ys The Y coordinates of the points.
  1597. * @param closedShape If set to true, an additional segment will be added from the last to the first point.
  1598. */
  1599. void setPoints( const std::vector<double>& points_xs,
  1600. const std::vector<double>& points_ys,
  1601. bool closedShape = true );
  1602. };
  1603. // -----------------------------------------------------------------------------
  1604. // mpMovableObject - provided by Jose Luis Blanco
  1605. // -----------------------------------------------------------------------------
  1606. /** This virtual class represents objects that can be moved to an arbitrary 2D location+rotation.
  1607. * The current transformation is set through SetCoordinateBase.
  1608. * To ease the implementation of descendent classes, mpMovableObject will
  1609. * be in charge of Bounding Box computation and layer render, assuming that
  1610. * the object updates its shape in m_shape_xs & m_shape_ys.
  1611. */
  1612. class WXDLLIMPEXP_MATHPLOT mpBitmapLayer : public mpLayer
  1613. {
  1614. public:
  1615. /** Default constructor.
  1616. */
  1617. mpBitmapLayer()
  1618. {
  1619. m_min_x = m_max_x =
  1620. m_min_y = m_max_y = 0;
  1621. m_validImg = false;
  1622. m_type = mpLAYER_BITMAP;
  1623. }
  1624. virtual ~mpBitmapLayer() {};
  1625. /** Returns a copy of the current bitmap assigned to the layer.
  1626. */
  1627. void GetBitmapCopy( wxImage& outBmp ) const;
  1628. /** Change the bitmap associated with the layer (to update the screen, refresh the mpWindow).
  1629. * @param inBmp The bitmap to associate. A copy is made, thus it can be released after calling this.
  1630. * @param x The left corner X coordinate (in plot units).
  1631. * @param y The top corner Y coordinate (in plot units).
  1632. * @param lx The width in plot units.
  1633. * @param ly The height in plot units.
  1634. */
  1635. void SetBitmap( const wxImage& inBmp, double x, double y, double lx, double ly );
  1636. virtual bool HasBBox() override { return true; }
  1637. /** Get inclusive left border of bounding box.
  1638. */
  1639. virtual double GetMinX() override { return m_min_x; }
  1640. /** Get inclusive right border of bounding box.
  1641. */
  1642. virtual double GetMaxX() override { return m_max_x; }
  1643. /** Get inclusive bottom border of bounding box.
  1644. */
  1645. virtual double GetMinY() override { return m_min_y; }
  1646. /** Get inclusive top border of bounding box.
  1647. */
  1648. virtual double GetMaxY() override { return m_max_y; }
  1649. virtual void Plot( wxDC& dc, mpWindow& w ) override;
  1650. /** Set label axis alignment.
  1651. * @param align alignment (choose between mpALIGN_NE, mpALIGN_NW, mpALIGN_SW, mpALIGN_SE
  1652. */
  1653. void SetAlign( int align ) { m_flags = align; };
  1654. protected:
  1655. int m_flags; // !< Holds label alignment
  1656. /** The internal copy of the Bitmap:
  1657. */
  1658. wxImage m_bitmap;
  1659. wxBitmap m_scaledBitmap;
  1660. wxCoord m_scaledBitmap_offset_x, m_scaledBitmap_offset_y;
  1661. bool m_validImg;
  1662. /** The shape of the bitmap:
  1663. */
  1664. double m_min_x, m_max_x, m_min_y, m_max_y;
  1665. };
  1666. /*@}*/
  1667. #endif // _MP_MATHPLOT_H_