Main MRPT website > C++ reference
MRPT logo
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-2014, 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/utils/CImage.h>
16 
17 /*---------------------------------------------------------------
18  Class
19  ---------------------------------------------------------------*/
20 namespace mrpt
21 {
22  namespace gui
23  {
24  using namespace mrpt::utils;
25  using namespace mrpt::math;
26 
27  class CWindowDialogPlots;
28 
30 
31  /** Create a GUI window and display plots with MATLAB-like interfaces and commands.
32  *
33  * For a list of supported events with the observer/observable pattern, see the discussion in mrpt::gui::CBaseGUIWindow.
34  *
35  * See CDisplayWindowPlots::plot
36  * \ingroup mrpt_gui_grp
37  */
39  {
40  // This must be added to any CObject derived class:
42 
43  public:
44  typedef void (* TCallbackMenu) (int menuID,float cursor_x, float cursor_y, void* userParam); //!< Type for the callback function used in setMenuCallback
45 
46  protected:
47  friend class CWindowDialogPlots;
48 
49  bool m_holdon; //!< Whether hold_on is enabled
50  bool m_holdon_just_disabled;
51  uint32_t m_holdon_cnt; //!< Counter for hold_on
52  TCallbackMenu m_callback;
53  void *m_callback_param;
54 
55  void internal_plot(CVectorFloat &x,CVectorFloat &y,const std::string &lineFormat,const std::string &plotName);
56  template <typename VECTOR1,typename VECTOR2>
57  void internal_plot_interface(const VECTOR1 &x,const VECTOR2 &y,const std::string &lineFormat,const std::string &plotName)
58  {
59  CVectorFloat x1(x.size()), y1(y.size());
60  const size_t N1=size_t(x.size());
61  for (size_t i=0;i<N1;i++) x1[i]=x[i];
62  const size_t N2=size_t(y.size());
63  for (size_t i=0;i<N2;i++) y1[i]=y[i];
64  this->internal_plot(x1,y1,lineFormat,plotName);
65  }
66  template <typename VECTOR1>
67  void internal_plot_interface(const VECTOR1 &y,const std::string &lineFormat,const std::string &plotName)
68  {
69  const size_t N=size_t(y.size());
70  CVectorFloat x1(N),y1(N);
71  for (size_t i=0;i<N;i++) { x1[i]=i; y1[i]=y[i]; }
72  this->internal_plot(x1,y1,lineFormat,plotName);
73  }
74 
75  public:
76 
77  /** Constructor
78  */
80  const std::string &windowCaption = std::string(),
81  unsigned int initialWidth = 350,
82  unsigned int initialHeight = 300 );
83 
84  /** Class factory returning a smart pointer */
85  static CDisplayWindowPlotsPtr Create(
86  const std::string &windowCaption,
87  unsigned int initialWindowWidth = 400,
88  unsigned int initialWindowHeight = 300 );
89 
90  /** Destructor
91  */
92  virtual ~CDisplayWindowPlots();
93 
94  /** Gets the last x,y pixel coordinates of the mouse. \return False if the window is closed. */
95  virtual bool getLastMousePosition(int &x, int &y) const;
96 
97  /** Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true) */
98  virtual void setCursorCross(bool cursorIsCross);
99 
100  /** Resizes the window, stretching the image to fit into the display area.
101  */
102  void resize( unsigned int width, unsigned int height );
103 
104  /** Changes the position of the window on the screen.
105  */
106  void setPos( int x, int y );
107 
108  /** Changes the window title text.
109  */
110  void setWindowTitle( const std::string &str );
111 
112  /** Enable/disable the feature of pan/zoom with the mouse (default=enabled)
113  */
114  void enableMousePanZoom( bool enabled );
115 
116  /** Adds a new layer with a 2D plot based on two vectors of X and Y points, using a MATLAB-like syntax.
117  * 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).
118  * If "hold_on" is enabled, then every call will always create a new plot, even if no "plotName" is provided.
119  *
120  * The lineFormat string is a combination of the following characters:
121  * - Line styles:
122  * - '.': One point for each data point
123  * - '-': A continuous line
124  * - ':': A dashed line
125  * - Colors:
126  * - k: black
127  * - r: red
128  * - g: green
129  * - b: blue
130  * - m: magenta
131  * - c: cyan
132  * - Line width:
133  * - '1' to '9': The line width (default=1)
134  *
135  * Examples:
136  * - 'r.' -> red points.
137  * - 'k3' or 'k-3' -> A black line with a line width of 3 pixels.
138  * \note The vectors x & y can be of types: float or double.
139  * \sa axis, axis_equal, axis_fit, clear, hold_on, hold_off
140  * \tparam VECTOR Can be std::vector<float/double> or mrpt::dynamicsize_vector<float/double> or a column/row Eigen::Matrix<>
141  */
142  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); }
143  //! \overload
144  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); }
145  //! \overload
146  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); }
147  //! \overload
148  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); }
149 
150  //! \overload
151  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); }
152  //! \overload
153  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); }
154 
155  /** Set the view area according to the passed coordinated. */
156  void axis( float x_min, float x_max, float y_min, float y_max, bool aspectRatioFix = false );
157 
158  /** Enable/disable the fixed X/Y aspect ratio fix feature (default=disabled). */
159  void axis_equal(bool enable=true);
160 
161  /** Fix automatically the view area according to existing graphs. */
162  void axis_fit(bool aspectRatioFix=false);
163 
164  /** Plots a 2D ellipse given its mean, covariance matrix, and
165  * 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).
166  * If "hold_on" is enabled, then every call will always create a new plot, even if no "plotName" is provided.
167  *
168  * For a description of lineFormat see CDisplayWindowPlots::plot.
169  * The "quantiles" value determines the confidence interval for the ellipse:
170  * - 1 : 68.27% confidence interval
171  * - 2 : 95.45%
172  * - 3 : 99.73%
173  * - 4 : 99.994%
174  * \note This method can be called with 2x2 fixed-sized or dynamic-size matrices of types: float or double.
175  * \sa axis, axis_equal, axis_fit, hold_on, hold_off
176  */
177  template <typename T>
178  void GUI_IMPEXP plotEllipse(
179  const T mean_x,
180  const T mean_y,
181  const CMatrixTemplateNumeric<T> &cov22,
182  const float quantiles,
183  const std::string &lineFormat = std::string("b-"),
184  const std::string &plotName = std::string("plotEllipse"),
185  bool showName = false);
186 
187  //! \overload
188  template <typename T>
189  void GUI_IMPEXP plotEllipse(
190  const T mean_x,
191  const T mean_y,
192  const CMatrixFixedNumeric<T,2,2> &cov22,
193  const float quantiles,
194  const std::string &lineFormat = std::string("b-"),
195  const std::string &plotName = std::string("plotEllipse"),
196  bool showName = false);
197 
198  /** Adds a bitmap image layer.
199  * 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).
200  *
201  * \sa axis, axis_equal, axis_fit, hold_on, hold_off
202  */
203  void image(
204  const utils::CImage &img,
205  const float &x_left,
206  const float &y_bottom,
207  const float &x_width,
208  const float &y_height,
209  const std::string &plotName = std::string("image") );
210 
211 
212  /** Remove all plot objects in the display.
213  * \sa plot
214  */
215  void clear();
216 
217  /** Remove all plot objects in the display (clear and clf do exactly the same).
218  * \sa plot, hold_on, hold_off
219  */
220  inline void clf() {
221  clear();
222  }
223 
224  /** Enables keeping all the graphs, instead of overwritting them.
225  * \sa hold_off, plot
226  */
227  void hold_on();
228 
229  /** Disables keeping all the graphs (this is the default behavior).
230  * \sa hold_on, plot
231  */
232  void hold_off();
233 
234  /** Disables keeping all the graphs (this is the default behavior).
235  * \param label The text that appears in the new popup menu item.
236  * \param menuID Any positive number (0,1,..). Used to tell which menu was selected in the user callback.
237  * \sa setMenuCallback
238  */
239  void addPopupMenuEntry( const std::string &label, int menuID );
240 
241 
242  /** Must be called to have a callback when the user selects one of the user-defined entries in the popup menu.
243  * \sa addPopupMenuEntry
244  */
245  void setMenuCallback(TCallbackMenu userFunction, void* userParam = NULL );
246 
247 
248  }; // End of class def.
250  }
251 
252 } // End of namespace
253 
254 #endif
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
Create a GUI window and display plots with MATLAB-like interfaces and commands.
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:97
STL namespace.
void internal_plot_interface(const VECTOR1 &y, const std::string &lineFormat, const std::string &plotName)
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.
This base provides a set of functions for maths stuff.
Definition: CArray.h:18
#define DEFINE_MRPT_OBJECT(class_name)
This declaration must be inserted in all CObject classes definition, within the class declaration...
Definition: CObject.h:167
#define DEFINE_MRPT_OBJECT_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
Definition: CObject.h:172
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 plot(const std::vector< T > &y, const std::string &lineFormat=std::string("b-"), const std::string &plotName=std::string("plotXY"))
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...
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:51
#define DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
Definition: CObject.h:171
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void clf()
Remove all plot objects in the display (clear and clf do exactly the same).
void plot(const Eigen::MatrixBase< Derived > &y, const std::string &lineFormat=std::string("b-"), const std::string &plotName=std::string("plotXY"))
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.



Page generated by Doxygen 1.8.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014