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://mrpt.sourceforge.net/                            |
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                  */
00056                 class GUI_IMPEXP CDisplayWindowPlots : public mrpt::gui::CBaseGUIWindow
00057                 {
00058                         // This must be added to any CObject derived class:
00059                         DEFINE_MRPT_OBJECT( CDisplayWindowPlots )
00060 
00061                 public:
00062                         typedef void (* TCallbackMenu) (int menuID,float cursor_x, float cursor_y, void* userParam); //!< Type for the callback function used in setMenuCallback
00063 
00064                 protected:
00065                         friend class CWindowDialogPlots;
00066 
00067                         bool            m_holdon;       //!< Whether hold_on is enabled
00068                         bool            m_holdon_just_disabled;
00069                         uint32_t        m_holdon_cnt;   //!< Counter for hold_on
00070                         TCallbackMenu m_callback;
00071                         void            *m_callback_param;
00072 
00073                         void internal_plot(vector_float &x,vector_float &y,const std::string  &lineFormat,const std::string  &plotName);
00074                         template <typename VECTOR1,typename VECTOR2>
00075                         void internal_plot_interface(const VECTOR1 &x,const VECTOR2 &y,const std::string  &lineFormat,const std::string  &plotName)
00076                         {
00077                                 vector_float x1(x.size()), y1(y.size());
00078                                 const size_t N1=size_t(x.size());
00079                                 for (size_t i=0;i<N1;i++) x1[i]=x[i];
00080                                 const size_t N2=size_t(y.size());
00081                                 for (size_t i=0;i<N2;i++) y1[i]=y[i];
00082                                 this->internal_plot(x1,y1,lineFormat,plotName);
00083                         }
00084                         template <typename VECTOR1>
00085                         void internal_plot_interface(const VECTOR1 &y,const std::string  &lineFormat,const std::string  &plotName)
00086                         {
00087                                 const size_t N=size_t(y.size());
00088                                 vector_float x1(N),y1(N);
00089                                 for (size_t i=0;i<N;i++) { x1[i]=i; y1[i]=y[i]; }
00090                                 this->internal_plot(x1,y1,lineFormat,plotName);
00091                         }
00092 
00093                 public:
00094 
00095                         /** Constructor
00096                          */
00097                         CDisplayWindowPlots(
00098                                 const std::string &windowCaption = std::string(),
00099                                 unsigned int initialWidth = 350,
00100                                 unsigned int initialHeight = 300 );
00101 
00102                         /** Class factory returning a smart pointer */
00103                         static CDisplayWindowPlotsPtr Create(
00104                                 const std::string       &windowCaption = std::string(),
00105                                 unsigned int            initialWindowWidth = 400,
00106                                 unsigned int            initialWindowHeight = 300 )
00107                         {
00108                                 return CDisplayWindowPlotsPtr(new CDisplayWindowPlots(windowCaption,initialWindowWidth,initialWindowHeight));
00109                         }
00110 
00111                         /** Destructor
00112                          */
00113                         virtual ~CDisplayWindowPlots();
00114 
00115                         /** Gets the last x,y pixel coordinates of the mouse. \return False if the window is closed. */
00116                         virtual bool getLastMousePosition(int &x, int &y) const;
00117 
00118                         /** Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true) */
00119                         virtual void setCursorCross(bool cursorIsCross);
00120 
00121                         /** Resizes the window, stretching the image to fit into the display area.
00122                          */
00123                         void  resize( unsigned int width, unsigned int height );
00124 
00125                         /** Changes the position of the window on the screen.
00126                          */
00127                         void  setPos( int x, int y );
00128 
00129                         /** Changes the window title text.
00130                           */
00131                         void  setWindowTitle( const std::string &str );
00132 
00133                         /** Enable/disable the feature of pan/zoom with the mouse (default=enabled)
00134                         */
00135                         void  enableMousePanZoom( bool enabled );
00136 
00137                         /** Adds a new layer with a 2D plot based on two vectors of X and Y points, using a MATLAB-like syntax.
00138                           *  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).
00139                           *  If "hold_on" is enabled, then every call will always create a new plot, even if no "plotName" is provided.
00140                           *
00141                           *  The lineFormat string is a combination of the following characters:
00142                           * - Line styles:
00143                           *             - '.': One point for each data point
00144                           *             - '-': A continuous line
00145                           *             - ':': A dashed line
00146                           * - Colors:
00147                           *             - k: black
00148                           *             - r: red
00149                           *             - g: green
00150                           *             - b: blue
00151                           *             - m: magenta
00152                           *             - c: cyan
00153                           * - Line width:
00154                           *             - '1' to '9': The line width (default=1)
00155                           *
00156                           *  Examples:
00157                           *   - 'r.' -> red points.
00158                           *   - 'k3' or 'k-3' -> A black line with a line width of 3 pixels.
00159                           * \note The vectors x & y can be of types: float or double.
00160                           * \sa axis, axis_equal, axis_fit, clear, hold_on, hold_off
00161                           * \tparam VECTOR Can be std::vector<float/double> or mrpt::dynamicsize_vector<float/double> or a column/row Eigen::Matrix<>
00162                           */
00163                         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); }
00164                         //! \overload
00165                         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); }
00166                         //! \overload
00167                         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); }
00168                         //! \overload
00169                         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); }
00170 
00171                         //! \overload
00172                         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); }
00173                         //! \overload
00174                         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); }
00175 
00176                         /** Set the view area according to the passed coordinated. */
00177                         void axis( float x_min, float x_max, float y_min, float y_max, bool aspectRatioFix = false );
00178 
00179                         /** Enable/disable the fixed X/Y aspect ratio fix feature (default=disabled). */
00180                         void axis_equal(bool enable=true);
00181 
00182                         /** Fix automatically the view area according to existing graphs. */
00183                         void axis_fit(bool aspectRatioFix=false);
00184 
00185                         /** Plots a 2D ellipse given its mean, covariance matrix, and
00186                           *  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).
00187                           *  If "hold_on" is enabled, then every call will always create a new plot, even if no "plotName" is provided.
00188                           *
00189                           *  For a description of lineFormat see CDisplayWindowPlots::plot.
00190                           *  The "quantiles" value determines the confidence interval for the ellipse:
00191                           *     - 1 : 68.27% confidence interval
00192                           *     - 2 : 95.45%
00193                           *     - 3 : 99.73%
00194                           *     - 4 : 99.994%
00195                           * \note This method can be called with 2x2 fixed-sized or dynamic-size matrices of types: float or double.
00196                           * \sa axis, axis_equal, axis_fit, hold_on, hold_off
00197                           */
00198                         template <typename T>
00199                         void GUI_IMPEXP plotEllipse(
00200                                 const T mean_x,
00201                                 const T mean_y,
00202                                 const CMatrixTemplateNumeric<T> &cov22,
00203                                 const float quantiles,
00204                                 const std::string  &lineFormat = std::string("b-"),
00205                                 const std::string  &plotName = std::string("plotEllipse"),
00206                                 bool showName = false);
00207 
00208                         //! \overload
00209                         template <typename T>
00210                         void GUI_IMPEXP plotEllipse(
00211                                 const T mean_x,
00212                                 const T mean_y,
00213                                 const CMatrixFixedNumeric<T,2,2> &cov22,
00214                                 const float quantiles,
00215                                 const std::string  &lineFormat = std::string("b-"),
00216                                 const std::string  &plotName = std::string("plotEllipse"),
00217                                 bool showName = false);
00218 
00219                         /** Adds a bitmap image layer.
00220                           *  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).
00221                           *
00222                           * \sa axis, axis_equal, axis_fit, hold_on, hold_off
00223                           */
00224                         void image(
00225                                 const utils::CImage &img,
00226                                 const float &x_left,
00227                                 const float &y_bottom,
00228                                 const float &x_width,
00229                                 const float &y_height,
00230                                 const std::string  &plotName = std::string("image") );
00231 
00232 
00233                         /** Remove all plot objects in the display.
00234                           * \sa plot
00235                           */
00236                         void clear();
00237 
00238                         /** Remove all plot objects in the display (clear and clf do exactly the same).
00239                           * \sa plot, hold_on, hold_off
00240                           */
00241                         inline void clf() {
00242                                 clear();
00243                         }
00244 
00245                         /** Enables keeping all the graphs, instead of overwritting them.
00246                           * \sa hold_off, plot
00247                           */
00248                         void hold_on();
00249 
00250                         /** Disables keeping all the graphs (this is the default behavior).
00251                           * \sa hold_on, plot
00252                           */
00253                         void hold_off();
00254 
00255                         /** Disables keeping all the graphs (this is the default behavior).
00256                           * \param label The text that appears in the new popup menu item.
00257                           * \param menuID Any positive number (0,1,..). Used to tell which menu was selected in the user callback.
00258                           * \sa setMenuCallback
00259                           */
00260                         void addPopupMenuEntry( const std::string &label, int menuID );
00261 
00262 
00263                         /** Must be called to have a callback when the user selects one of the user-defined entries in the popup menu.
00264                           * \sa addPopupMenuEntry
00265                           */
00266                         void setMenuCallback(TCallbackMenu userFunction, void* userParam = NULL );
00267 
00268 
00269                 }; // End of class def.
00270         }
00271 
00272 } // End of namespace
00273 
00274 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011