Main MRPT website > C++ reference
MRPT logo

CEnhancedMetaFile.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  CEnhancedMetaFile_H
00029 #define  CEnhancedMetaFile_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/utils/CCanvas.h>
00033 #include <mrpt/utils/safe_pointers.h>
00034 
00035 /*---------------------------------------------------------------
00036         Class
00037   ---------------------------------------------------------------*/
00038 namespace mrpt
00039 {
00040 namespace utils
00041 {
00042         /** This class represents a Windows Enhanced Meta File (EMF) for generating and saving graphics.
00043           *  If used under Linux, a ".png", non-vectorial, file will be generated instead.
00044           */
00045         class BASE_IMPEXP CEnhancedMetaFile : public CCanvas
00046         {
00047         private:
00048                 void_ptr_noncopy        m_hdc;
00049                 int                             m_scale;
00050                 void_ptr_noncopy        m_hFont;
00051                 std::string             m_targetFile;
00052 
00053         public:
00054                 static int LINUX_IMG_WIDTH;             //!< In Linux, the size of the bitmap image that emulates the EMF (Default:800)
00055                 static int LINUX_IMG_HEIGHT;    //!< In Linux, the size of the bitmap image that emulates the EMF (Default:600)
00056 
00057 
00058                 /** Constructor
00059                   *  \param targetFileName This file will be created and the EMF saved there.
00060                   *  \param scaleFactor All coordinates in draw commands will be internally multiplied by this scale, to provide a way of obtaining "subpixel" drawing.
00061                   */
00062                 CEnhancedMetaFile(
00063                         const std::string &targetFileName,
00064                         int             scaleFactor = 1);
00065 
00066                 /** Destructor
00067                   */
00068                 virtual ~CEnhancedMetaFile(  );
00069 
00070                 /** Changes the value of the pixel (x,y).
00071                   *  Pixel coordinates starts at the left-top corner of the image, and start in (0,0).
00072                   *  The meaning of the parameter "color" depends on the implementation: it will usually
00073                   *   be a 24bit RGB value (0x00RRGGBB), but it can also be just a 8bit gray level.
00074                   *  This method must support (x,y) values OUT of the actual image size without neither
00075                   *   raising exceptions, nor leading to memory access errors.
00076                   */
00077                 virtual void  setPixel( int x, int y, size_t color);
00078 
00079                 /** Returns the width of the image in pixels (this currently has no applicability for a EMF file...)
00080                   */
00081                 virtual size_t  getWidth() const { return 640; }
00082 
00083                 /** Returns the height of the image in pixels (this currently has no applicability for a EMF file...)
00084                   */
00085                 virtual size_t getHeight() const {return 480;}
00086 
00087                 /** Draws an image as a bitmap at a given position.
00088                   * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn
00089                   * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn
00090                   * \param img The image to be drawn in this canvas
00091                   *  This method may be redefined in some classes implementing this interface in a more appropiate manner.
00092                   */
00093                 virtual void  drawImage(
00094                         int                                             x,
00095                         int                                             y,
00096                         const utils::CImage     &img );
00097 
00098                 /** Draws a line.
00099                   * \param x0 The starting point x coordinate
00100                   * \param y0 The starting point y coordinate
00101                   * \param x1 The end point x coordinate
00102                   * \param y1 The end point y coordinate
00103                   * \param color The color of the line
00104                   * \param width The desired width of the line (this is IGNORED in this virtual class)
00105                   *  This method may be redefined in some classes implementing this interface in a more appropiate manner.
00106                   */
00107                 virtual void  line(
00108                         int                             x0,
00109                         int                             y0,
00110                         int                             x1,
00111                         int                             y1,
00112                         const mrpt::utils::TColor color,
00113                         unsigned int    width = 1,
00114                         TPenStyle               penStyle = psSolid);
00115 
00116                 /** Places a text label.
00117                   * \param x0 The x coordinates
00118                   * \param y0 The y coordinates
00119                   * \param str The string to put
00120                   * \param color The text color
00121                   * \param fontSize The font size, in "points"
00122                   *  This method may be redefined in some classes implementing this interface in a more appropiate manner.
00123                   * \sa rectangle
00124                   */
00125                 virtual void  textOut(
00126                         int                                     x0,
00127                         int                                     y0,
00128                         const std::string       &str,
00129                         const mrpt::utils::TColor color
00130                         );
00131 
00132                 /** Select the current font used when drawing text.
00133                   * \param fontName The face name of a font (e.g. "Arial","System",...)
00134                   * \param fontSize The size of the font in pts.
00135                   * \param bold Whether the font is bold
00136                   * \param italic Whether the font is italic
00137                   * \sa textOut
00138                   */
00139                 virtual void  selectTextFont(
00140                         const std::string  &fontName,
00141                         int                                     fontSize,
00142                         bool                            bold = false,
00143                         bool                            italic = false );
00144 
00145                 /** Draws an image as a bitmap at a given position, with some custom scale and rotation changes.
00146                   * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn
00147                   * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn
00148                   * \param rotation The rotation in radians, positive values being anti-clockwise direction, 0 is the normal position.
00149                   * \param scale The scale factor, e.g. 2 means twice the original size.
00150                   * \param img The image to be drawn in this canvas
00151                   *  This method may be redefined in some classes implementing this interface in a more appropiate manner.
00152                   */
00153                 virtual void  drawImage(
00154                         int                                             x,
00155                         int                                             y,
00156                         const utils::CImage     &img,
00157                         float                                   rotation,
00158                         float                                   scale )
00159                 {
00160                         CCanvas::drawImage(x,y,img,rotation,scale);
00161                 }
00162 
00163 
00164                 /** Draws a rectangle (an empty rectangle, without filling)
00165                   * \param x0 The top-left x coordinate
00166                   * \param y0 The top-left y coordinate
00167                   * \param x1 The right-bottom x coordinate
00168                   * \param y1 The right-bottom y coordinate
00169                   * \param color The color of the line
00170                   * \param width The desired width of the line.
00171                   * \sa filledRectangle
00172                   */
00173                 virtual void  rectangle(
00174                         int                             x0,
00175                         int                             y0,
00176                         int                             x1,
00177                         int                             y1,
00178                         const mrpt::utils::TColor color,
00179                         unsigned int    width = 1 );
00180 
00181                 /** Draws an ellipse representing a given confidence interval of a 2D Gaussian distribution.
00182                   * \param mean_x The x coordinate of the center point of the ellipse.
00183                   * \param mean_y The y coordinate of the center point of the ellipse.
00184                   * \param cov2D A 2x2 covariance matrix.
00185                   * \param confIntervalStds How many "sigmas" for the confidence level (i.e. 2->95%, 3=99.97%,...)
00186                   * \param color The color of the ellipse
00187                   * \param width The desired width of the line (this is IGNORED in this virtual class)
00188                   * \param nEllipsePoints The number of points to generate to approximate the ellipse shape.
00189                   * \exception std::exception On an invalid matrix.
00190                   */
00191                 template <class T>
00192                 void  ellipseGaussian(
00193                         math::CMatrixTemplateNumeric<T> *cov2D,
00194                         T                                                       mean_x,
00195                         T                                                       mean_y,
00196                         float                                           confIntervalStds = 2,
00197                         const mrpt::utils::TColor       color = mrpt::utils::TColor(255,255,255),
00198                         unsigned int                            width = 1,
00199                         int                                                     nEllipsePoints = 20
00200                         )
00201                 {
00202                         MRPT_START;
00203                         int                                                             x1=0,y1=0,x2=0,y2=0;
00204                         double                                                  ang;
00205                         math::CMatrixTemplateNumeric<T>         eigVal,eigVec;
00206                         int                                                             i;
00207 
00208                         // Compute the eigen-vectors & values:
00209                         cov2D->eigenVectors(eigVec,eigVal);
00210 
00211                         eigVal.Sqrt();
00212                         math::CMatrixTemplateNumeric<T>         M( eigVal * (~eigVec) );
00213 
00214                         // Compute the points of the 2D ellipse:
00215                         for (i=0,ang=0;i<nEllipsePoints;i++,ang+= (M_2PI/(nEllipsePoints-1)))
00216                         {
00217                                 float   ccos = cos(ang);
00218                                 float   ssin = sin(ang);
00219 
00220                                 x2 = round( mean_x + confIntervalStds * (ccos * M(0,0) + ssin * M(1,0)) );
00221                                 y2 = round( mean_y + confIntervalStds * (ccos * M(0,1) + ssin * M(1,1)) );
00222 
00223                                 if (i>0)
00224                                         line( x1, y1,x2, y2,color,width );
00225 
00226                                 x1 = x2;
00227                                 y1 = y2;
00228                         } // end for points on ellipse
00229 
00230                         MRPT_END_WITH_CLEAN_UP( \
00231                                 std::cout << "Covariance matrix leading to error is:" << std::endl << *cov2D << std::endl; \
00232                                 );
00233                 }
00234 
00235 
00236 
00237         }; // End of class def.
00238 
00239         } // End of namespace
00240 } // end of namespace
00241 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011