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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019