MRPT  1.9.9
CDisplayWindowPlots.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef CDisplayWindowPlots_H
10 #define CDisplayWindowPlots_H
11 
15 #include <mrpt/img/CImage.h>
16 #include <mrpt/gui/gui_frwds.h>
17 
18 namespace mrpt::gui
19 {
20 /** Create a GUI window and display plots with MATLAB-like interfaces and
21  * commands.
22  *
23  * For a list of supported events with the observer/observable pattern, see the
24  * discussion in mrpt::gui::CBaseGUIWindow.
25  *
26  * See CDisplayWindowPlots::plot
27  * \ingroup mrpt_gui_grp
28  */
30 {
31  public:
33 
34  /** Type for the callback function used in setMenuCallback */
35  using TCallbackMenu =
36  void (*)(int menuID, float cursor_x, float cursor_y, void* userParam);
37 
38  protected:
39  friend class CWindowDialogPlots;
40 
41  /** Whether hold_on is enabled */
42  bool m_holdon;
44  /** Counter for hold_on */
48 
49  void internal_plot(
51  const std::string& lineFormat, const std::string& plotName);
52  template <typename VECTOR1, typename VECTOR2>
54  const VECTOR1& x, const VECTOR2& y, const std::string& lineFormat,
55  const std::string& plotName)
56  {
57  mrpt::math::CVectorFloat x1(x.size()), y1(y.size());
58  const size_t N1 = size_t(x.size());
59  for (size_t i = 0; i < N1; i++) x1[i] = x[i];
60  const size_t N2 = size_t(y.size());
61  for (size_t i = 0; i < N2; i++) y1[i] = y[i];
62  this->internal_plot(x1, y1, lineFormat, plotName);
63  }
64  template <typename VECTOR1>
66  const VECTOR1& y, const std::string& lineFormat,
67  const std::string& plotName)
68  {
69  const size_t N = size_t(y.size());
70  mrpt::math::CVectorFloat x1(N), y1(N);
71  for (size_t i = 0; i < N; i++)
72  {
73  x1[i] = i;
74  y1[i] = y[i];
75  }
76  this->internal_plot(x1, y1, lineFormat, plotName);
77  }
78 
79  public:
80  /** Constructor
81  */
83  const std::string& windowCaption = std::string(),
84  unsigned int initialWidth = 350, unsigned int initialHeight = 300);
85 
86  /** Class factory returning a smart pointer */
88  const std::string& windowCaption, unsigned int initialWindowWidth = 400,
89  unsigned int initialWindowHeight = 300);
90 
91  /** Destructor
92  */
93  virtual ~CDisplayWindowPlots();
94 
95  /** Gets the last x,y pixel coordinates of the mouse. \return False if the
96  * window is closed. */
97  virtual bool getLastMousePosition(int& x, int& y) const override;
98 
99  /** Set cursor style to default (cursorIsCross=false) or to a cross
100  * (cursorIsCross=true) */
101  virtual void setCursorCross(bool cursorIsCross) override;
102 
103  /** Resizes the window, stretching the image to fit into the display area.
104  */
105  void resize(unsigned int width, unsigned int height) override;
106 
107  /** Changes the position of the window on the screen.
108  */
109  void setPos(int x, int y) override;
110 
111  /** Changes the window title text.
112  */
113  void setWindowTitle(const std::string& str) override;
114 
115  /** Enable/disable the feature of pan/zoom with the mouse (default=enabled)
116  */
117  void enableMousePanZoom(bool enabled);
118 
119  /** Adds a new layer with a 2D plot based on two vectors of X and Y points,
120  *using a MATLAB-like syntax.
121  * Each call to this function creates a new plot, unless the plot name
122  *coincides with an already existing plot: in this case the X & Y points
123  *are used to update this existing layer (this also applies to using the
124  *default plot name).
125  * If "hold_on" is enabled, then every call will always create a new plot,
126  *even if no "plotName" is provided.
127  *
128  * The lineFormat string is a combination of the following characters:
129  * - Line styles:
130  * - '.': One point for each data point
131  * - '-': A continuous line
132  * - ':': A dashed line
133  * - Colors:
134  * - k: black
135  * - r: red
136  * - g: green
137  * - b: blue
138  * - m: magenta
139  * - c: cyan
140  * - Line width:
141  * - '1' to '9': The line width (default=1)
142  *
143  * Examples:
144  * - 'r.' -> red points.
145  * - 'k3' or 'k-3' -> A black line with a line width of 3 pixels.
146  * \note The vectors x & y can be of types: float or double.
147  * \sa axis, axis_equal, axis_fit, clear, hold_on, hold_off
148  * \tparam VECTOR Can be std::vector<float/double> or
149  *mrpt::dynamicsize_vector<float/double> or a column/row Eigen::Matrix<>
150  */
151  template <typename T1, typename T2>
152  inline void plot(
153  const std::vector<T1>& x, const std::vector<T2>& y,
154  const std::string& lineFormat = std::string("b-"),
155  const std::string& plotName = std::string("plotXY"))
156  {
157  this->internal_plot_interface(x, y, lineFormat, plotName);
158  }
159  //! \overload
160  template <typename T1, typename Derived2>
161  inline void plot(
162  const std::vector<T1>& x, const Eigen::MatrixBase<Derived2>& y,
163  const std::string& lineFormat = std::string("b-"),
164  const std::string& plotName = std::string("plotXY"))
165  {
166  this->internal_plot_interface(x, y, lineFormat, plotName);
167  }
168  //! \overload
169  template <typename Derived1, typename T2>
170  inline void plot(
171  const Eigen::MatrixBase<Derived1>& x, const std::vector<T2>& y,
172  const std::string& lineFormat = std::string("b-"),
173  const std::string& plotName = std::string("plotXY"))
174  {
175  this->internal_plot_interface(x, y, lineFormat, plotName);
176  }
177  //! \overload
178  template <typename Derived1, typename Derived2>
179  inline void plot(
182  const std::string& lineFormat = std::string("b-"),
183  const std::string& plotName = std::string("plotXY"))
184  {
185  this->internal_plot_interface(x, y, lineFormat, plotName);
186  }
187 
188  //! \overload
189  template <typename T>
190  void plot(
191  const std::vector<T>& y,
192  const std::string& lineFormat = std::string("b-"),
193  const std::string& plotName = std::string("plotXY"))
194  {
195  this->internal_plot_interface(y, lineFormat, plotName);
196  }
197  //! \overload
198  template <typename Derived>
199  void plot(
201  const std::string& lineFormat = std::string("b-"),
202  const std::string& plotName = std::string("plotXY"))
203  {
204  this->internal_plot_interface(y, lineFormat, plotName);
205  }
206 
207  /** Set the view area according to the passed coordinated. */
208  void axis(
209  float x_min, float x_max, float y_min, float y_max,
210  bool aspectRatioFix = false);
211 
212  /** Enable/disable the fixed X/Y aspect ratio fix feature
213  * (default=disabled). */
214  void axis_equal(bool enable = true);
215 
216  /** Fix automatically the view area according to existing graphs. */
217  void axis_fit(bool aspectRatioFix = false);
218 
219  /** Plots a 2D ellipse given its mean, covariance matrix, and
220  * Each call to this function creates a new plot, unless the plot name
221  * coincides with an already existing plot: in this case the new values are
222  * used to update this existing layer (this also applies to using the
223  * default plot name).
224  * If "hold_on" is enabled, then every call will always create a new plot,
225  * even if no "plotName" is provided.
226  *
227  * For a description of lineFormat see CDisplayWindowPlots::plot.
228  * The "quantiles" value determines the confidence interval for the
229  * ellipse:
230  * - 1 : 68.27% confidence interval
231  * - 2 : 95.45%
232  * - 3 : 99.73%
233  * - 4 : 99.994%
234  * \note This method can be called with 2x2 fixed-sized or dynamic-size
235  * matrices of types: float or double.
236  * \sa axis, axis_equal, axis_fit, hold_on, hold_off
237  */
238  template <typename T>
239  void plotEllipse(
240  const T mean_x, const T mean_y,
242  const float quantiles,
243  const std::string& lineFormat = std::string("b-"),
244  const std::string& plotName = std::string("plotEllipse"),
245  bool showName = false);
246 
247  //! \overload
248  template <typename T>
249  void plotEllipse(
250  const T mean_x, const T mean_y,
252  const float quantiles,
253  const std::string& lineFormat = std::string("b-"),
254  const std::string& plotName = std::string("plotEllipse"),
255  bool showName = false);
256 
257  /** Adds a bitmap image layer.
258  * Each call to this function creates a new layer, unless the plot name
259  * coincides with an already existing plot: in this case the new values are
260  * used to update this existing layer (this also applies to using the
261  * default plot name).
262  *
263  * \sa axis, axis_equal, axis_fit, hold_on, hold_off
264  */
265  void image(
266  const mrpt::img::CImage& img, const float& x_left,
267  const float& y_bottom, const float& x_width, const float& y_height,
268  const std::string& plotName = std::string("image"));
269 
270  /** Remove all plot objects in the display.
271  * \sa plot
272  */
273  void clear();
274 
275  /** Remove all plot objects in the display (clear and clf do exactly the
276  * same).
277  * \sa plot, hold_on, hold_off
278  */
279  inline void clf() { clear(); }
280  /** Enables keeping all the graphs, instead of overwritting them.
281  * \sa hold_off, plot
282  */
283  void hold_on();
284 
285  /** Disables keeping all the graphs (this is the default behavior).
286  * \sa hold_on, plot
287  */
288  void hold_off();
289 
290  /** Disables keeping all the graphs (this is the default behavior).
291  * \param label The text that appears in the new popup menu item.
292  * \param menuID Any positive number (0,1,..). Used to tell which menu was
293  * selected in the user callback.
294  * \sa setMenuCallback
295  */
296  void addPopupMenuEntry(const std::string& label, int menuID);
297 
298  /** Must be called to have a callback when the user selects one of the
299  * user-defined entries in the popup menu.
300  * \sa addPopupMenuEntry
301  */
302  void setMenuCallback(TCallbackMenu userFunction, void* userParam = nullptr);
303 
304 }; // End of class def.
305 }
306 #endif
307 
308 
Create a GUI window and display plots with MATLAB-like interfaces and commands.
void enableMousePanZoom(bool enabled)
Enable/disable the feature of pan/zoom with the mouse (default=enabled)
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:44
The wx dialog for gui::CDisplayWindowPlots.
Definition: WxSubsystem.h:438
void setPos(int x, int y) override
Changes the position of the window on the screen.
bool m_holdon
Whether hold_on is enabled.
void axis(float x_min, float x_max, float y_min, float y_max, bool aspectRatioFix=false)
Set the view area according to the passed coordinated.
void internal_plot(mrpt::math::CVectorFloat &x, mrpt::math::CVectorFloat &y, const std::string &lineFormat, const std::string &plotName)
void hold_off()
Disables keeping all the graphs (this is the default behavior).
void internal_plot_interface(const VECTOR1 &y, const std::string &lineFormat, const std::string &plotName)
GLenum GLsizei width
Definition: glext.h:3531
void axis_equal(bool enable=true)
Enable/disable the fixed X/Y aspect ratio fix feature (default=disabled).
CDisplayWindowPlots(const std::string &windowCaption=std::string(), unsigned int initialWidth=350, unsigned int initialHeight=300)
Constructor.
void plot(const std::vector< T1 > &x, const Eigen::MatrixBase< Derived2 > &y, const std::string &lineFormat=std::string("b-"), const std::string &plotName=std::string("plotXY"))
A numeric matrix of compile-time fixed size.
uint32_t m_holdon_cnt
Counter for hold_on.
GLint GLvoid * img
Definition: glext.h:3763
virtual bool getLastMousePosition(int &x, int &y) const override
Gets the last x,y pixel coordinates of the mouse.
void plot(const Eigen::MatrixBase< Derived1 > &x, const Eigen::MatrixBase< Derived2 > &y, const std::string &lineFormat=std::string("b-"), const std::string &plotName=std::string("plotXY"))
void(*)(int menuID, float cursor_x, float cursor_y, void *userParam) TCallbackMenu
Type for the callback function used in setMenuCallback.
void plot(const std::vector< T > &y, const std::string &lineFormat=std::string("b-"), const std::string &plotName=std::string("plotXY"))
void addPopupMenuEntry(const std::string &label, int menuID)
Disables keeping all the graphs (this is the default behavior).
void plot(const std::vector< T1 > &x, const std::vector< T2 > &y, const std::string &lineFormat=std::string("b-"), const std::string &plotName=std::string("plotXY"))
Adds a new layer with a 2D plot based on two vectors of X and Y points, using a MATLAB-like syntax...
GLsizei const GLchar ** string
Definition: glext.h:4101
void internal_plot_interface(const VECTOR1 &x, const VECTOR2 &y, const std::string &lineFormat, const std::string &plotName)
virtual ~CDisplayWindowPlots()
Destructor.
void clear()
Remove all plot objects in the display.
void axis_fit(bool aspectRatioFix=false)
Fix automatically the view area according to existing graphs.
void hold_on()
Enables keeping all the graphs, instead of overwritting them.
void clf()
Remove all plot objects in the display (clear and clf do exactly the same).
void image(const mrpt::img::CImage &img, const float &x_left, const float &y_bottom, const float &x_width, const float &y_height, const std::string &plotName=std::string("image"))
Adds a bitmap image layer.
void setWindowTitle(const std::string &str) override
Changes the window title text.
static CDisplayWindowPlots::Ptr Create(const std::string &windowCaption, unsigned int initialWindowWidth=400, unsigned int initialWindowHeight=300)
Class factory returning a smart pointer.
GLenum GLint GLint y
Definition: glext.h:3538
virtual void setCursorCross(bool cursorIsCross) override
Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true) ...
void plot(const Eigen::MatrixBase< Derived > &y, const std::string &lineFormat=std::string("b-"), const std::string &plotName=std::string("plotXY"))
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
GLenum GLint x
Definition: glext.h:3538
GLenum GLsizei GLsizei height
Definition: glext.h:3554
unsigned __int32 uint32_t
Definition: rptypes.h:47
void plot(const Eigen::MatrixBase< Derived1 > &x, const std::vector< T2 > &y, const std::string &lineFormat=std::string("b-"), const std::string &plotName=std::string("plotXY"))
The base class for GUI window classes.
void setMenuCallback(TCallbackMenu userFunction, void *userParam=nullptr)
Must be called to have a callback when the user selects one of the user-defined entries in the popup ...
void resize(unsigned int width, unsigned int height) override
Resizes the window, stretching the image to fit into the display area.
void plotEllipse(const T mean_x, const T mean_y, const mrpt::math::CMatrixTemplateNumeric< T > &cov22, const float quantiles, const std::string &lineFormat=std::string("b-"), const std::string &plotName=std::string("plotEllipse"), bool showName=false)
Plots a 2D ellipse given its mean, covariance matrix, and Each call to this function creates a new pl...
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020