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



Page generated by Doxygen 1.8.14 for MRPT 2.0.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020