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