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



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