Main MRPT website > C++ reference
MRPT logo
CCanvas.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 CCanvas_H
00029 #define CCanvas_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/utils/TColor.h>
00033 #include <mrpt/math/CMatrixTemplateNumeric.h>
00034 #include <mrpt/math/ops_matrices.h>
00035 
00036 namespace mrpt
00037 {
00038         namespace math
00039         {
00040                 class CMatrix;
00041                 class CMatrixD;
00042         }
00043         namespace utils
00044         {
00045                 class CImage;
00046 
00047                 /** This virtual class defines the interface of any object accepting drawing primitives on it.
00048                  *
00049                  *  A number of text fonts can be selected with CCanvas::selectTextFont(). These are the
00050                  *   implemented font names:
00051                  *
00052                  *  - "6x13"
00053                  *  - "6x13B" (bold)
00054                  *  - "6x13O" (italic)
00055                  *  - "9x15"
00056                  *  - "9x15B" (bold)
00057                  *  - "10x20"
00058                  *  - "18x18ja" (Japanese, UNICODE character values)
00059                  *
00060                  *  For an example of each font check the <a href="http://www.mrpt.org/Implemented_2D_Fonts">corresponding wiki page</a>.
00061                  *
00062                  * \sa CImage
00063                  * \ingroup mrpt_base_grp
00064                  */
00065                 class BASE_IMPEXP CCanvas
00066                 {
00067                 protected:
00068                         std::string     m_selectedFont; //!< The selected font name.
00069 
00070                         const uint32_t *m_selectedFontBitmaps; //!< Direct access to character bitmaps.
00071 
00072                 public:
00073 
00074                         CCanvas();
00075 
00076                         /** Definition of pen styles
00077                           */
00078                         enum TPenStyle
00079                         {
00080                                 psSolid = 0,
00081                                 psDash,                 /* -------  */
00082                                 psDot,                  /* .......  */
00083                                 psDashDot,              /* _._._._  */
00084                                 psDashDotDot    /* _.._.._  */
00085                         };
00086 
00087                         /** Dummy virtual destructor:
00088                           */
00089                         virtual ~CCanvas()
00090                         {
00091                         }
00092 
00093                         /** Changes the value of the pixel (x,y).
00094                           *  Pixel coordinates starts at the left-top corner of the image, and start in (0,0).
00095                           *  The meaning of the parameter "color" depends on the implementation: it will usually
00096                           *   be a 24bit RGB value (0x00RRGGBB), but it can also be just a 8bit gray level.
00097                           *
00098                           *  You can also use a TColor() type as input and it will be automatically converted to size_t.
00099                           *
00100                           *  This method must support (x,y) values OUT of the actual image size without neither
00101                           *   raising exceptions, nor leading to memory access errors.
00102                           *
00103                           */
00104                         virtual void  setPixel( int x, int y, size_t color) =0;
00105 
00106                         /** Returns the width of the image in pixels
00107                           */
00108                         virtual size_t  getWidth() const = 0;
00109 
00110                         /** Returns the height of the image in pixels
00111                           */
00112                         virtual size_t  getHeight() const = 0;
00113 
00114                         /** Draws a line.
00115                           * \param x0 The starting point x coordinate
00116                           * \param y0 The starting point y coordinate
00117                           * \param x1 The end point x coordinate
00118                           * \param y1 The end point y coordinate
00119                           * \param color The color of the line
00120                           * \param width The desired width of the line (this is IGNORED in this virtual class)
00121                           *  This method may be redefined in some classes implementing this interface in a more appropiate manner.
00122                           */
00123                         virtual void  line(
00124                                 int                             x0,
00125                                 int                             y0,
00126                                 int                             x1,
00127                                 int                             y1,
00128                                 const mrpt::utils::TColor color,
00129                                 unsigned int    width = 1,
00130                                 TPenStyle               penStyle = psSolid);
00131 
00132                         /** Draws a rectangle (an empty rectangle, without filling)
00133                           * \param x0 The top-left x coordinate
00134                           * \param y0 The top-left y coordinate
00135                           * \param x1 The right-bottom x coordinate
00136                           * \param y1 The right-bottom y coordinate
00137                           * \param color The color of the line
00138                           * \param width The desired width of the line.
00139                           * \sa filledRectangle
00140                           */
00141                         void  rectangle(
00142                                 int                             x0,
00143                                 int                             y0,
00144                                 int                             x1,
00145                                 int                             y1,
00146                                 const mrpt::utils::TColor       color,
00147                                 unsigned int    width = 1 );
00148 
00149         /*****************************************************AJOGD***************************************************/
00150                         /** Draws a triangle
00151                           * \param x0 The triangle center x coordinate
00152                           * \param y0 The triangle center y coordinate
00153                           * \param size The size of the triangle
00154                           * \param color The color of the line
00155                           *     \param inferior The position of the triangle
00156                           * \param width The desired width of the line.
00157                           * \sa triangle
00158                           */
00159                         void  triangle(
00160                                 int                             x0,
00161                                 int                             y0,
00162                                 int                             size,
00163                                 const mrpt::utils::TColor       color,
00164                                 bool                    inferior = true,
00165                                 unsigned int    width = 1 );
00166         /************************************************************************************************************/
00167 
00168                         /** Draws a filled rectangle.
00169                           * \param x0 The top-left x coordinate
00170                           * \param y0 The top-left y coordinate
00171                           * \param x1 The right-bottom x coordinate
00172                           * \param y1 The right-bottom y coordinate
00173                           * \param color The color of the rectangle fill
00174                           *  This method may be redefined in some classes implementing this interface in a more appropiate manner.
00175                           * \sa rectangle
00176                           */
00177                         virtual void  filledRectangle(
00178                                 int                             x0,
00179                                 int                             y0,
00180                                 int                             x1,
00181                                 int                             y1,
00182                                 const mrpt::utils::TColor       color
00183                                 );
00184 
00185                         /** Renders 2D text using bitmap fonts.
00186                           * \param x0 The x coordinates
00187                           * \param y0 The y coordinates
00188                           * \param str The string to put. If using UNICODE characters, use UTF-8 encoding.
00189                           * \param color The text color
00190                           *
00191                           * \sa selectTextFont
00192                           */
00193                         virtual void  textOut(
00194                                 int                                     x0,
00195                                 int                                     y0,
00196                                 const std::string       &str,
00197                                 const mrpt::utils::TColor       color
00198                                 );
00199 
00200                         /** Select the current font used when drawing text.
00201                           * \param fontName The name of the font
00202                           *
00203                           *  Valid font names:
00204                           *  - 5x7
00205                           *  - 6x13
00206                           *  - 6x13B
00207                           *  - 6x13O
00208                           *  - 9x15   (Default at start-up)
00209                           *  - 9x15B
00210                           *  - 10x20
00211                           *  - 18x18ja (Asian characters for UTF-8 strings - Only available if MRPT is built with MRPT_HAS_ASIAN_FONTS = true)
00212                           *
00213                           *   <img src="sample_textFonts.png" >
00214                           *
00215                           * \sa textOut, The example in <a href="http://www.mrpt.org/Implemented_2D_Fonts">this page</a>.
00216                           */
00217                         virtual void  selectTextFont( const std::string  &fontName );
00218 
00219                         /** Draws an image as a bitmap at a given position.
00220                           * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn
00221                           * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn
00222                           * \param img The image to be drawn in this canvas
00223                           *  This method may be redefined in some classes implementing this interface in a more appropiate manner.
00224                           */
00225                         virtual void  drawImage(
00226                                 int                                             x,
00227                                 int                                             y,
00228                                 const utils::CImage     &img );
00229 
00230                         /** Draw a cross.
00231                         * \param x0 The point x coordinate
00232                         * \param y0 The point y coordinate
00233                         * \param color The color of the cross
00234                         * \param size The size of the cross
00235                         * \param type The cross type. It could be: 'x', '+' or ':'(like '+' but clear at the center dot)
00236                         * \param width The desired width of the cross (this is IGNORED yet)
00237                         */
00238                         void  cross (int x0,int y0, const mrpt::utils::TColor   color,char type, unsigned int size=5, unsigned int width = 1);
00239 
00240                         /** Draws an image as a bitmap at a given position, with some custom scale and rotation changes.
00241                           * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn
00242                           * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn
00243                           * \param rotation The rotation in radians, positive values being anti-clockwise direction, 0 is the normal position.
00244                           * \param scale The scale factor, e.g. 2 means twice the original size.
00245                           * \param img The image to be drawn in this canvas
00246                           *  This method may be redefined in some classes implementing this interface in a more appropiate manner.
00247                           */
00248                         virtual void  drawImage(
00249                                 int                                             x,
00250                                 int                                             y,
00251                                 const utils::CImage     &img,
00252                                 float                                   rotation,
00253                                 float                                   scale );
00254 
00255                         /** Draws a circle of a given radius.
00256                           * \param x The center - x coordinate in pixels.
00257                           * \param y The center - y coordinate in pixels.
00258                           * \param radius The radius - in pixels.
00259                           * \param color The color of the circle.
00260                           * \param width The desired width of the line (this is IGNORED in this virtual class)
00261                           */
00262                         virtual void  drawCircle(
00263                                 int             x,
00264                                 int             y,
00265                                 int             radius,
00266                                 const mrpt::utils::TColor       color = mrpt::utils::TColor(255,255,255),
00267                                 unsigned int    width = 1 );
00268 
00269                         /** Draws an ellipse representing a given confidence interval of a 2D Gaussian distribution.
00270                           * \param mean_x The x coordinate of the center point of the ellipse.
00271                           * \param mean_y The y coordinate of the center point of the ellipse.
00272                           * \param cov2D A 2x2 covariance matrix.
00273                           * \param confIntervalStds How many "sigmas" for the confidence level (i.e. 2->95%, 3=99.97%,...)
00274                           * \param color The color of the ellipse
00275                           * \param width The desired width of the line (this is IGNORED in this virtual class)
00276                           * \param nEllipsePoints The number of points to generate to approximate the ellipse shape.
00277                           * \exception std::exception On an invalid matrix.
00278                           */
00279                         template <class MATRIX2X2>
00280                         void  ellipseGaussian(
00281                                 const MATRIX2X2                         *cov2D,
00282                                 const double                            mean_x,
00283                                 const double                            mean_y,
00284                                 double                                          confIntervalStds = 2,
00285                                 const mrpt::utils::TColor       color = mrpt::utils::TColor(255,255,255),
00286                                 unsigned int                            width = 1,
00287                                 int                                                     nEllipsePoints = 20
00288                                 )
00289                         {
00290                                 MRPT_START
00291                                 int                             x1=0,y1=0,x2=0,y2=0;
00292                                 double                  ang;
00293                                 MATRIX2X2               eigVal,eigVec;
00294                                 int                             i;
00295 
00296                                 // Compute the eigen-vectors & values:
00297                                 cov2D->eigenVectors(eigVec,eigVal);
00298 
00299                                 eigVal.Sqrt();
00300                                 MATRIX2X2       M;
00301                                 M.multiply_ABt(eigVal, eigVec);
00302 
00303                                 // Compute the points of the 2D ellipse:
00304                                 for (i=0,ang=0;i<nEllipsePoints;i++,ang+= (M_2PI/(nEllipsePoints-1)))
00305                                 {
00306                                         double ccos = cos(ang);
00307                                         double ssin = sin(ang);
00308 
00309                                         x2 = round( mean_x + confIntervalStds * (ccos * M(0,0) + ssin * M(1,0)) );
00310                                         y2 = round( mean_y + confIntervalStds * (ccos * M(0,1) + ssin * M(1,1)) );
00311 
00312                                         if (i>0)
00313                                                 line( x1, y1,x2, y2,color,width );
00314 
00315                                         x1 = x2;
00316                                         y1 = y2;
00317                                 } // end for points on ellipse
00318 
00319                                 MRPT_END_WITH_CLEAN_UP( \
00320                                         std::cout << "Covariance matrix leading to error is:" << std::endl << *cov2D << std::endl; \
00321                                         );
00322                         }
00323 
00324                         /** Draws a set of marks onto the image, given a generic container of entities having just "x" and "y" fields.
00325                          *  The class of FEATURELIST can be, for example, std::vector<TPoint2D>, std::vector<TPixelCoordsf> or mrpt::vision::CFeatureList
00326                          * \sa drawFeatures
00327                          */
00328                         template <class FEATURELIST>
00329                         void  drawFeaturesSimple( const FEATURELIST &list, const TColor &color = TColor::red )
00330                         {
00331                                 for(size_t i=0;i<list.size(); ++i )
00332                                 {
00333                                         const int x = round( list.getFeatureX(i) );
00334                                         const int y = round( list.getFeatureY(i) );
00335                                         this->cross( x,y, color, '+' );
00336                                 }
00337                         }
00338 
00339                         /** Draws a set of marks (or scaled circles for features with scale) onto the image, given a generic container of features.
00340                          *  The class of FEATURELIST can be:
00341                          *    - mrpt::vision::CFeatureList
00342                          *    - mrpt::vision::TSimpleFeatureList
00343                          *
00344                          * \sa drawFeaturesSimple
00345                          */
00346                         template <class FEATURELIST>
00347                         void  drawFeatures( const FEATURELIST &list, const TColor &color = TColor::red, const bool showIDs = false, const bool showResponse = false  )
00348                         {
00349                                 for(size_t i=0;i<list.size(); ++i )
00350                                 {
00351                                         const int x = round( list.getFeatureX(i) );
00352                                         const int y = round( list.getFeatureY(i) );
00353                                         this->cross(x,y, color, '+' );
00354                                         if( showIDs ) this->textOut(x,y, format("%u", static_cast<unsigned int>(list.getFeatureID(i))), TColor::red );
00355                                         if (showResponse) this->textOut( x,y+10, format("R:%u", static_cast<unsigned int>(list.getFeatureResponse(i))), TColor::red );
00356                                         if( ! list.isPointFeature(i) ) this->drawCircle(x,y, list.getScale(i), TColor::red );
00357                                 }
00358                         }
00359 
00360 
00361 
00362                 }; // End of class
00363 
00364                 typedef CCanvas CMRPTCanvas;    //!< Deprecated name.
00365 
00366         } // end of namespace utils
00367 
00368 } // end of namespace mrpt
00369 
00370 #endif



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