00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2009 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 00034 namespace mrpt 00035 { 00036 namespace math 00037 { 00038 class CMatrix; 00039 class CMatrixD; 00040 } 00041 namespace utils 00042 { 00043 class CImage; 00044 class CImageFloat; 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://babel.isa.uma.es/mrpt/index.php/Implemented_2D_Fonts">corresponding wiki page</a>. 00060 * 00061 * \sa CImage, CImageFloat 00062 */ 00063 class MRPTDLLIMPEXP 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 * This method must support (x,y) values OUT of the actual image size without neither 00096 * raising exceptions, nor leading to memory access errors. 00097 */ 00098 virtual void setPixel( int x, int y, size_t color) =0; 00099 00100 /** Returns the width of the image in pixels 00101 */ 00102 virtual size_t getWidth() const = 0; 00103 00104 /** Returns the height of the image in pixels 00105 */ 00106 virtual size_t getHeight() const = 0; 00107 00108 /** Draws a line. 00109 * \param x0 The starting point x coordinate 00110 * \param y0 The starting point y coordinate 00111 * \param x1 The end point x coordinate 00112 * \param y1 The end point y coordinate 00113 * \param color The color of the line 00114 * \param width The desired width of the line (this is IGNORED in this virtual class) 00115 * This method may be redefined in some classes implementing this interface in a more appropiate manner. 00116 */ 00117 virtual void line( 00118 int x0, 00119 int y0, 00120 int x1, 00121 int y1, 00122 unsigned int color, 00123 unsigned int width = 1, 00124 TPenStyle penStyle = psSolid); 00125 00126 /** Draws a rectangle (an empty rectangle, without filling) 00127 * \param x0 The top-left x coordinate 00128 * \param y0 The top-left y coordinate 00129 * \param x1 The right-bottom x coordinate 00130 * \param y1 The right-bottom y coordinate 00131 * \param color The color of the line 00132 * \param width The desired width of the line. 00133 * \sa filledRectangle 00134 */ 00135 void rectangle( 00136 int x0, 00137 int y0, 00138 int x1, 00139 int y1, 00140 unsigned int color, 00141 unsigned int width = 1 ); 00142 00143 /*****************************************************AJOGD***************************************************/ 00144 /** Draws a triangle 00145 * \param x0 The triangle center x coordinate 00146 * \param y0 The triangle center y coordinate 00147 * \param size The size of the triangle 00148 * \param color The color of the line 00149 * \param inferior The position of the triangle 00150 * \param width The desired width of the line. 00151 * \sa triangle 00152 */ 00153 void triangle( 00154 int x0, 00155 int y0, 00156 int size, 00157 unsigned int color, 00158 bool inferior = true, 00159 unsigned int width = 1 ); 00160 /************************************************************************************************************/ 00161 00162 /** Draws a filled rectangle. 00163 * \param x0 The top-left x coordinate 00164 * \param y0 The top-left y coordinate 00165 * \param x1 The right-bottom x coordinate 00166 * \param y1 The right-bottom y coordinate 00167 * \param color The color of the rectangle fill 00168 * This method may be redefined in some classes implementing this interface in a more appropiate manner. 00169 * \sa rectangle 00170 */ 00171 virtual void filledRectangle( 00172 int x0, 00173 int y0, 00174 int x1, 00175 int y1, 00176 unsigned int color 00177 ); 00178 00179 /** Renders 2D text using bitmap fonts. 00180 * \param x0 The x coordinates 00181 * \param y0 The y coordinates 00182 * \param str The string to put. If using UNICODE characters, use UTF-8 encoding. 00183 * \param color The text color 00184 * 00185 * \sa selectTextFont 00186 */ 00187 virtual void textOut( 00188 int x0, 00189 int y0, 00190 const std::string &str, 00191 unsigned int color 00192 ); 00193 00194 /** Select the current font used when drawing text. 00195 * \param fontName The name of the font 00196 * See <a href="http://babel.isa.uma.es/mrpt/index.php/Implemented_2D_Fonts">the wiki</a> for a list of valid font names. 00197 * \sa textOut 00198 */ 00199 virtual void selectTextFont( const std::string &fontName ); 00200 00201 /** Draws an image as a bitmap at a given position. 00202 * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn 00203 * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn 00204 * \param img The image to be drawn in this canvas 00205 * This method may be redefined in some classes implementing this interface in a more appropiate manner. 00206 */ 00207 virtual void drawImage( 00208 int x, 00209 int y, 00210 const utils::CImage &img ); 00211 00212 /** Draw a cross. 00213 * \param x0 The point x coordinate 00214 * \param y0 The point y coordinate 00215 * \param color The color of the cross 00216 * \param size The size of the cross 00217 * \param type The cross type. It could be: "x" or "+" 00218 * \param width The desired width of the cross (this is IGNORED yet) 00219 */ 00220 void cross (int x0,int y0, unsigned int color,char type, unsigned int size=5, unsigned int width = 1); 00221 00222 /** Draws an image as a bitmap at a given position, with some custom scale and rotation changes. 00223 * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn 00224 * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn 00225 * \param rotation The rotation in radians, positive values being anti-clockwise direction, 0 is the normal position. 00226 * \param scale The scale factor, e.g. 2 means twice the original size. 00227 * \param img The image to be drawn in this canvas 00228 * This method may be redefined in some classes implementing this interface in a more appropiate manner. 00229 */ 00230 virtual void drawImage( 00231 int x, 00232 int y, 00233 const utils::CImage &img, 00234 float rotation, 00235 float scale ); 00236 00237 /** Draws an image as a bitmap at a given position. 00238 * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn 00239 * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn 00240 * \param img The image to be drawn in this canvas 00241 * This method actually calls internally to "drawImage" with a "CImage" parameter. 00242 */ 00243 void drawImage( 00244 int x, 00245 int y, 00246 const utils::CImageFloat &img ); 00247 00248 /** Draws an image as a bitmap at a given position, with some custom scale and rotation changes. 00249 * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn 00250 * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn 00251 * \param rotation The rotation in radians, positive values being anti-clockwise direction, 0 is the normal position. 00252 * \param scale The scale factor, e.g. 2 means twice the original size. 00253 * \param img The image to be drawn in this canvas 00254 * This method actually calls internally to "drawImage" with a "CImage" parameter. 00255 */ 00256 void drawImage( 00257 int x, 00258 int y, 00259 const utils::CImageFloat &img, 00260 float rotation, 00261 float scale ); 00262 00263 /** Draws a circle of a given radius. 00264 * \param x The center - x coordinate in pixels. 00265 * \param y The center - y coordinate in pixels. 00266 * \param radius The radius - in pixels. 00267 * \param color The color of the circle. 00268 * \param width The desired width of the line (this is IGNORED in this virtual class) 00269 */ 00270 virtual void drawCircle( 00271 int x, 00272 int y, 00273 int radius, 00274 const mrpt::utils::TColor &color = mrpt::utils::TColor(255,255,255), 00275 unsigned int width = 1 ); 00276 00277 /** Draws an ellipse representing a given confidence interval of a 2D Gaussian distribution. 00278 * \param mean_x The x coordinate of the center point of the ellipse. 00279 * \param mean_y The y coordinate of the center point of the ellipse. 00280 * \param cov2D A 2x2 covariance matrix. 00281 * \param confIntervalStds How many "sigmas" for the confidence level (i.e. 2->95%, 3=99.97%,...) 00282 * \param color The color of the ellipse 00283 * \param width The desired width of the line (this is IGNORED in this virtual class) 00284 * \param nEllipsePoints The number of points to generate to approximate the ellipse shape. 00285 * \exception std::exception On an invalid matrix. 00286 */ 00287 template <class T> 00288 void ellipseGaussian( 00289 math::CMatrixTemplateNumeric<T> *cov2D, 00290 T mean_x, 00291 T mean_y, 00292 float confIntervalStds = 2, 00293 unsigned int color = 0xFFFFFF, 00294 unsigned int width = 1, 00295 int nEllipsePoints = 20 00296 ) 00297 { 00298 MRPT_TRY_START; 00299 int x1=0,y1=0,x2=0,y2=0; 00300 double ang; 00301 math::CMatrixTemplateNumeric<T> eigVal,eigVec; 00302 int i; 00303 00304 // Compute the eigen-vectors & values: 00305 cov2D->eigenVectors(eigVec,eigVal); 00306 00307 eigVal.Sqrt(); 00308 math::CMatrixTemplateNumeric<T> M( eigVal * (~eigVec) ); 00309 00310 // Compute the points of the 2D ellipse: 00311 for (i=0,ang=0;i<nEllipsePoints;i++,ang+= (M_2PI/(nEllipsePoints-1))) 00312 { 00313 double ccos = cos(ang); 00314 double ssin = sin(ang); 00315 00316 x2 = round( mean_x + confIntervalStds * (ccos * M(0,0) + ssin * M(1,0)) ); 00317 y2 = round( mean_y + confIntervalStds * (ccos * M(0,1) + ssin * M(1,1)) ); 00318 00319 if (i>0) 00320 line( x1, y1,x2, y2,color,width ); 00321 00322 x1 = x2; 00323 y1 = y2; 00324 } // end for points on ellipse 00325 00326 MRPT_TRY_END_WITH_CLEAN_UP( \ 00327 std::cout << "Covariance matrix leading to error is:" << std::endl << *cov2D << std::endl; \ 00328 ); 00329 } 00330 00331 }; // End of class 00332 00333 typedef CCanvas CMRPTCanvas; //!< Deprecated name. 00334 00335 } // end of namespace utils 00336 00337 } // end of namespace mrpt 00338 00339 #endif
Page generated by Doxygen 1.5.7.1 for MRPT 0.7.1 SVN: at Mon Aug 17 23:10:56 EDT 2009 |