Main MRPT website > C++ reference
MRPT logo
CDisplayWindowPlots.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CDisplayWindowPlots_H
29 #define CDisplayWindowPlots_H
30 
34 #include <mrpt/utils/CImage.h>
35 
36 /*---------------------------------------------------------------
37  Class
38  ---------------------------------------------------------------*/
39 namespace mrpt
40 {
41  namespace gui
42  {
43  using namespace mrpt::utils;
44  using namespace mrpt::math;
45 
46  class CWindowDialogPlots;
47 
49 
50  /** Create a GUI window and display plots with MATLAB-like interfaces and commands.
51  *
52  * For a list of supported events with the observer/observable pattern, see the discussion in mrpt::gui::CBaseGUIWindow.
53  *
54  * See CDisplayWindowPlots::plot
55  * \ingroup mrpt_gui_grp
56  */
57  class GUI_IMPEXP CDisplayWindowPlots : public mrpt::gui::CBaseGUIWindow
58  {
59  // This must be added to any CObject derived class:
61 
62  public:
63  typedef void (* TCallbackMenu) (int menuID,float cursor_x, float cursor_y, void* userParam); //!< Type for the callback function used in setMenuCallback
64 
65  protected:
66  friend class CWindowDialogPlots;
67 
68  bool m_holdon; //!< Whether hold_on is enabled
69  bool m_holdon_just_disabled;
70  uint32_t m_holdon_cnt; //!< Counter for hold_on
71  TCallbackMenu m_callback;
72  void *m_callback_param;
73 
74  void internal_plot(vector_float &x,vector_float &y,const std::string &lineFormat,const std::string &plotName);
75  template <typename VECTOR1,typename VECTOR2>
76  void internal_plot_interface(const VECTOR1 &x,const VECTOR2 &y,const std::string &lineFormat,const std::string &plotName)
77  {
78  vector_float x1(x.size()), y1(y.size());
79  const size_t N1=size_t(x.size());
80  for (size_t i=0;i<N1;i++) x1[i]=x[i];
81  const size_t N2=size_t(y.size());
82  for (size_t i=0;i<N2;i++) y1[i]=y[i];
83  this->internal_plot(x1,y1,lineFormat,plotName);
84  }
85  template <typename VECTOR1>
86  void internal_plot_interface(const VECTOR1 &y,const std::string &lineFormat,const std::string &plotName)
87  {
88  const size_t N=size_t(y.size());
89  vector_float x1(N),y1(N);
90  for (size_t i=0;i<N;i++) { x1[i]=i; y1[i]=y[i]; }
91  this->internal_plot(x1,y1,lineFormat,plotName);
92  }
93 
94  public:
95 
96  /** Constructor
97  */
99  const std::string &windowCaption = std::string(),
100  unsigned int initialWidth = 350,
101  unsigned int initialHeight = 300 );
102 
103  /** Class factory returning a smart pointer */
104  static CDisplayWindowPlotsPtr Create(
105  const std::string &windowCaption = std::string(),
106  unsigned int initialWindowWidth = 400,
107  unsigned int initialWindowHeight = 300 )
108  {
109  return CDisplayWindowPlotsPtr(new CDisplayWindowPlots(windowCaption,initialWindowWidth,initialWindowHeight));
110  }
111 
112  /** Destructor
113  */
114  virtual ~CDisplayWindowPlots();
115 
116  /** Gets the last x,y pixel coordinates of the mouse. \return False if the window is closed. */
117  virtual bool getLastMousePosition(int &x, int &y) const;
118 
119  /** Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true) */
120  virtual void setCursorCross(bool cursorIsCross);
121 
122  /** Resizes the window, stretching the image to fit into the display area.
123  */
124  void resize( unsigned int width, unsigned int height );
125 
126  /** Changes the position of the window on the screen.
127  */
128  void setPos( int x, int y );
129 
130  /** Changes the window title text.
131  */
132  void setWindowTitle( const std::string &str );
133 
134  /** Enable/disable the feature of pan/zoom with the mouse (default=enabled)
135  */
136  void enableMousePanZoom( bool enabled );
137 
138  /** Adds a new layer with a 2D plot based on two vectors of X and Y points, using a MATLAB-like syntax.
139  * Each call to this function creates a new plot, unless the plot name coincides with an already existing plot: in this case the X & Y points are used to update this existing layer (this also applies to using the default plot name).
140  * If "hold_on" is enabled, then every call will always create a new plot, even if no "plotName" is provided.
141  *
142  * The lineFormat string is a combination of the following characters:
143  * - Line styles:
144  * - '.': One point for each data point
145  * - '-': A continuous line
146  * - ':': A dashed line
147  * - Colors:
148  * - k: black
149  * - r: red
150  * - g: green
151  * - b: blue
152  * - m: magenta
153  * - c: cyan
154  * - Line width:
155  * - '1' to '9': The line width (default=1)
156  *
157  * Examples:
158  * - 'r.' -> red points.
159  * - 'k3' or 'k-3' -> A black line with a line width of 3 pixels.
160  * \note The vectors x & y can be of types: float or double.
161  * \sa axis, axis_equal, axis_fit, clear, hold_on, hold_off
162  * \tparam VECTOR Can be std::vector<float/double> or mrpt::dynamicsize_vector<float/double> or a column/row Eigen::Matrix<>
163  */
164  template <typename T1,typename T2> inline 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") ) { this->internal_plot_interface(x,y,lineFormat,plotName); }
165  //! \overload
166  template <typename T1,typename Derived2> inline 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") ) { this->internal_plot_interface(x,y,lineFormat,plotName); }
167  //! \overload
168  template <typename Derived1,typename T2> inline 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") ) { this->internal_plot_interface(x,y,lineFormat,plotName); }
169  //! \overload
170  template <typename Derived1,typename Derived2> inline 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") ) { this->internal_plot_interface(x,y,lineFormat,plotName); }
171 
172  //! \overload
173  template <typename T> void plot(const std::vector<T> &y,const std::string &lineFormat = std::string("b-"),const std::string &plotName = std::string("plotXY") ) { this->internal_plot_interface(y,lineFormat,plotName); }
174  //! \overload
175  template <typename Derived> void plot(const Eigen::MatrixBase<Derived> &y,const std::string &lineFormat = std::string("b-"),const std::string &plotName = std::string("plotXY") ) { this->internal_plot_interface(y,lineFormat,plotName); }
176 
177  /** Set the view area according to the passed coordinated. */
178  void axis( float x_min, float x_max, float y_min, float y_max, bool aspectRatioFix = false );
179 
180  /** Enable/disable the fixed X/Y aspect ratio fix feature (default=disabled). */
181  void axis_equal(bool enable=true);
182 
183  /** Fix automatically the view area according to existing graphs. */
184  void axis_fit(bool aspectRatioFix=false);
185 
186  /** Plots a 2D ellipse given its mean, covariance matrix, and
187  * Each call to this function creates a new plot, unless the plot name coincides with an already existing plot: in this case the new values are used to update this existing layer (this also applies to using the default plot name).
188  * If "hold_on" is enabled, then every call will always create a new plot, even if no "plotName" is provided.
189  *
190  * For a description of lineFormat see CDisplayWindowPlots::plot.
191  * The "quantiles" value determines the confidence interval for the ellipse:
192  * - 1 : 68.27% confidence interval
193  * - 2 : 95.45%
194  * - 3 : 99.73%
195  * - 4 : 99.994%
196  * \note This method can be called with 2x2 fixed-sized or dynamic-size matrices of types: float or double.
197  * \sa axis, axis_equal, axis_fit, hold_on, hold_off
198  */
199  template <typename T>
200  void GUI_IMPEXP plotEllipse(
201  const T mean_x,
202  const T mean_y,
203  const CMatrixTemplateNumeric<T> &cov22,
204  const float quantiles,
205  const std::string &lineFormat = std::string("b-"),
206  const std::string &plotName = std::string("plotEllipse"),
207  bool showName = false);
208 
209  //! \overload
210  template <typename T>
211  void GUI_IMPEXP plotEllipse(
212  const T mean_x,
213  const T mean_y,
214  const CMatrixFixedNumeric<T,2,2> &cov22,
215  const float quantiles,
216  const std::string &lineFormat = std::string("b-"),
217  const std::string &plotName = std::string("plotEllipse"),
218  bool showName = false);
219 
220  /** Adds a bitmap image layer.
221  * Each call to this function creates a new layer, unless the plot name coincides with an already existing plot: in this case the new values are used to update this existing layer (this also applies to using the default plot name).
222  *
223  * \sa axis, axis_equal, axis_fit, hold_on, hold_off
224  */
225  void image(
226  const utils::CImage &img,
227  const float &x_left,
228  const float &y_bottom,
229  const float &x_width,
230  const float &y_height,
231  const std::string &plotName = std::string("image") );
232 
233 
234  /** Remove all plot objects in the display.
235  * \sa plot
236  */
237  void clear();
238 
239  /** Remove all plot objects in the display (clear and clf do exactly the same).
240  * \sa plot, hold_on, hold_off
241  */
242  inline void clf() {
243  clear();
244  }
245 
246  /** Enables keeping all the graphs, instead of overwritting them.
247  * \sa hold_off, plot
248  */
249  void hold_on();
250 
251  /** Disables keeping all the graphs (this is the default behavior).
252  * \sa hold_on, plot
253  */
254  void hold_off();
255 
256  /** Disables keeping all the graphs (this is the default behavior).
257  * \param label The text that appears in the new popup menu item.
258  * \param menuID Any positive number (0,1,..). Used to tell which menu was selected in the user callback.
259  * \sa setMenuCallback
260  */
261  void addPopupMenuEntry( const std::string &label, int menuID );
262 
263 
264  /** Must be called to have a callback when the user selects one of the user-defined entries in the popup menu.
265  * \sa addPopupMenuEntry
266  */
267  void setMenuCallback(TCallbackMenu userFunction, void* userParam = NULL );
268 
269 
270  }; // End of class def.
271  }
272 
273 } // End of namespace
274 
275 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013