Main MRPT website > C++ reference
MRPT logo
CDisplayWindowPlots.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  CDisplayWindowPlots_H
00029 #define  CDisplayWindowPlots_H
00030 
00031 #include <mrpt/gui/CBaseGUIWindow.h>
00032 #include <mrpt/math/CMatrixTemplateNumeric.h>
00033 #include <mrpt/math/lightweight_geom_data.h>
00034 #include <mrpt/utils/CImage.h>
00035 
00036 /*---------------------------------------------------------------
00037         Class
00038   ---------------------------------------------------------------*/
00039 namespace mrpt
00040 {
00041         namespace gui
00042         {
00043                 using namespace mrpt::utils;
00044                 using namespace mrpt::math;
00045 
00046                 class CWindowDialogPlots;
00047 
00048                 DEFINE_MRPT_OBJECT_PRE_CUSTOM_BASE_LINKAGE(CDisplayWindowPlots,  mrpt::gui::CBaseGUIWindow, GUI_IMPEXP)
00049 
00050                 /** Create a GUI window and display plots with MATLAB-like interfaces and commands.
00051                  *
00052                  *  For a list of supported events with the observer/observable pattern, see the discussion in mrpt::gui::CBaseGUIWindow.
00053                  *
00054                  *   See CDisplayWindowPlots::plot
00055                  * \ingroup mrpt_gui_grp
00056                  */
00057                 class GUI_IMPEXP CDisplayWindowPlots : public mrpt::gui::CBaseGUIWindow
00058                 {
00059                         // This must be added to any CObject derived class:
00060                         DEFINE_MRPT_OBJECT( CDisplayWindowPlots )
00061 
00062                 public:
00063                         typedef void (* TCallbackMenu) (int menuID,float cursor_x, float cursor_y, void* userParam); //!< Type for the callback function used in setMenuCallback
00064 
00065                 protected:
00066                         friend class CWindowDialogPlots;
00067 
00068                         bool            m_holdon;       //!< Whether hold_on is enabled
00069                         bool            m_holdon_just_disabled;
00070                         uint32_t        m_holdon_cnt;   //!< Counter for hold_on
00071                         TCallbackMenu m_callback;
00072                         void            *m_callback_param;
00073 
00074                         void internal_plot(vector_float &x,vector_float &y,const std::string  &lineFormat,const std::string  &plotName);
00075                         template <typename VECTOR1,typename VECTOR2>
00076                         void internal_plot_interface(const VECTOR1 &x,const VECTOR2 &y,const std::string  &lineFormat,const std::string  &plotName)
00077                         {
00078                                 vector_float x1(x.size()), y1(y.size());
00079                                 const size_t N1=size_t(x.size());
00080                                 for (size_t i=0;i<N1;i++) x1[i]=x[i];
00081                                 const size_t N2=size_t(y.size());
00082                                 for (size_t i=0;i<N2;i++) y1[i]=y[i];
00083                                 this->internal_plot(x1,y1,lineFormat,plotName);
00084                         }
00085                         template <typename VECTOR1>
00086                         void internal_plot_interface(const VECTOR1 &y,const std::string  &lineFormat,const std::string  &plotName)
00087                         {
00088                                 const size_t N=size_t(y.size());
00089                                 vector_float x1(N),y1(N);
00090                                 for (size_t i=0;i<N;i++) { x1[i]=i; y1[i]=y[i]; }
00091                                 this->internal_plot(x1,y1,lineFormat,plotName);
00092                         }
00093 
00094                 public:
00095 
00096                         /** Constructor
00097                          */
00098                         CDisplayWindowPlots(
00099                                 const std::string &windowCaption = std::string(),
00100                                 unsigned int initialWidth = 350,
00101                                 unsigned int initialHeight = 300 );
00102 
00103                         /** Class factory returning a smart pointer */
00104                         static CDisplayWindowPlotsPtr Create(
00105                                 const std::string       &windowCaption = std::string(),
00106                                 unsigned int            initialWindowWidth = 400,
00107                                 unsigned int            initialWindowHeight = 300 )
00108                         {
00109                                 return CDisplayWindowPlotsPtr(new CDisplayWindowPlots(windowCaption,initialWindowWidth,initialWindowHeight));
00110                         }
00111 
00112                         /** Destructor
00113                          */
00114                         virtual ~CDisplayWindowPlots();
00115 
00116                         /** Gets the last x,y pixel coordinates of the mouse. \return False if the window is closed. */
00117                         virtual bool getLastMousePosition(int &x, int &y) const;
00118 
00119                         /** Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true) */
00120                         virtual void setCursorCross(bool cursorIsCross);
00121 
00122                         /** Resizes the window, stretching the image to fit into the display area.
00123                          */
00124                         void  resize( unsigned int width, unsigned int height );
00125 
00126                         /** Changes the position of the window on the screen.
00127                          */
00128                         void  setPos( int x, int y );
00129 
00130                         /** Changes the window title text.
00131                           */
00132                         void  setWindowTitle( const std::string &str );
00133 
00134                         /** Enable/disable the feature of pan/zoom with the mouse (default=enabled)
00135                         */
00136                         void  enableMousePanZoom( bool enabled );
00137 
00138                         /** Adds a new layer with a 2D plot based on two vectors of X and Y points, using a MATLAB-like syntax.
00139                           *  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).
00140                           *  If "hold_on" is enabled, then every call will always create a new plot, even if no "plotName" is provided.
00141                           *
00142                           *  The lineFormat string is a combination of the following characters:
00143                           * - Line styles:
00144                           *             - '.': One point for each data point
00145                           *             - '-': A continuous line
00146                           *             - ':': A dashed line
00147                           * - Colors:
00148                           *             - k: black
00149                           *             - r: red
00150                           *             - g: green
00151                           *             - b: blue
00152                           *             - m: magenta
00153                           *             - c: cyan
00154                           * - Line width:
00155                           *             - '1' to '9': The line width (default=1)
00156                           *
00157                           *  Examples:
00158                           *   - 'r.' -> red points.
00159                           *   - 'k3' or 'k-3' -> A black line with a line width of 3 pixels.
00160                           * \note The vectors x & y can be of types: float or double.
00161                           * \sa axis, axis_equal, axis_fit, clear, hold_on, hold_off
00162                           * \tparam VECTOR Can be std::vector<float/double> or mrpt::dynamicsize_vector<float/double> or a column/row Eigen::Matrix<>
00163                           */
00164                         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); }
00165                         //! \overload
00166                         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); }
00167                         //! \overload
00168                         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); }
00169                         //! \overload
00170                         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); }
00171 
00172                         //! \overload
00173                         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); }
00174                         //! \overload
00175                         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); }
00176 
00177                         /** Set the view area according to the passed coordinated. */
00178                         void axis( float x_min, float x_max, float y_min, float y_max, bool aspectRatioFix = false );
00179 
00180                         /** Enable/disable the fixed X/Y aspect ratio fix feature (default=disabled). */
00181                         void axis_equal(bool enable=true);
00182 
00183                         /** Fix automatically the view area according to existing graphs. */
00184                         void axis_fit(bool aspectRatioFix=false);
00185 
00186                         /** Plots a 2D ellipse given its mean, covariance matrix, and
00187                           *  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).
00188                           *  If "hold_on" is enabled, then every call will always create a new plot, even if no "plotName" is provided.
00189                           *
00190                           *  For a description of lineFormat see CDisplayWindowPlots::plot.
00191                           *  The "quantiles" value determines the confidence interval for the ellipse:
00192                           *     - 1 : 68.27% confidence interval
00193                           *     - 2 : 95.45%
00194                           *     - 3 : 99.73%
00195                           *     - 4 : 99.994%
00196                           * \note This method can be called with 2x2 fixed-sized or dynamic-size matrices of types: float or double.
00197                           * \sa axis, axis_equal, axis_fit, hold_on, hold_off
00198                           */
00199                         template <typename T>
00200                         void GUI_IMPEXP plotEllipse(
00201                                 const T mean_x,
00202                                 const T mean_y,
00203                                 const CMatrixTemplateNumeric<T> &cov22,
00204                                 const float quantiles,
00205                                 const std::string  &lineFormat = std::string("b-"),
00206                                 const std::string  &plotName = std::string("plotEllipse"),
00207                                 bool showName = false);
00208 
00209                         //! \overload
00210                         template <typename T>
00211                         void GUI_IMPEXP plotEllipse(
00212                                 const T mean_x,
00213                                 const T mean_y,
00214                                 const CMatrixFixedNumeric<T,2,2> &cov22,
00215                                 const float quantiles,
00216                                 const std::string  &lineFormat = std::string("b-"),
00217                                 const std::string  &plotName = std::string("plotEllipse"),
00218                                 bool showName = false);
00219 
00220                         /** Adds a bitmap image layer.
00221                           *  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).
00222                           *
00223                           * \sa axis, axis_equal, axis_fit, hold_on, hold_off
00224                           */
00225                         void image(
00226                                 const utils::CImage &img,
00227                                 const float &x_left,
00228                                 const float &y_bottom,
00229                                 const float &x_width,
00230                                 const float &y_height,
00231                                 const std::string  &plotName = std::string("image") );
00232 
00233 
00234                         /** Remove all plot objects in the display.
00235                           * \sa plot
00236                           */
00237                         void clear();
00238 
00239                         /** Remove all plot objects in the display (clear and clf do exactly the same).
00240                           * \sa plot, hold_on, hold_off
00241                           */
00242                         inline void clf() {
00243                                 clear();
00244                         }
00245 
00246                         /** Enables keeping all the graphs, instead of overwritting them.
00247                           * \sa hold_off, plot
00248                           */
00249                         void hold_on();
00250 
00251                         /** Disables keeping all the graphs (this is the default behavior).
00252                           * \sa hold_on, plot
00253                           */
00254                         void hold_off();
00255 
00256                         /** Disables keeping all the graphs (this is the default behavior).
00257                           * \param label The text that appears in the new popup menu item.
00258                           * \param menuID Any positive number (0,1,..). Used to tell which menu was selected in the user callback.
00259                           * \sa setMenuCallback
00260                           */
00261                         void addPopupMenuEntry( const std::string &label, int menuID );
00262 
00263 
00264                         /** Must be called to have a callback when the user selects one of the user-defined entries in the popup menu.
00265                           * \sa addPopupMenuEntry
00266                           */
00267                         void setMenuCallback(TCallbackMenu userFunction, void* userParam = NULL );
00268 
00269 
00270                 }; // End of class def.
00271         }
00272 
00273 } // End of namespace
00274 
00275 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011