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 * 00097 * You can also use a TColor() type as input and it will be automatically converted to size_t. 00098 * 00099 * This method must support (x,y) values OUT of the actual image size without neither 00100 * raising exceptions, nor leading to memory access errors. 00101 * 00102 */ 00103 virtual void setPixel( int x, int y, size_t color) =0; 00104 00105 /** Returns the width of the image in pixels 00106 */ 00107 virtual size_t getWidth() const = 0; 00108 00109 /** Returns the height of the image in pixels 00110 */ 00111 virtual size_t getHeight() const = 0; 00112 00113 /** Draws a line. 00114 * \param x0 The starting point x coordinate 00115 * \param y0 The starting point y coordinate 00116 * \param x1 The end point x coordinate 00117 * \param y1 The end point y coordinate 00118 * \param color The color of the line 00119 * \param width The desired width of the line (this is IGNORED in this virtual class) 00120 * This method may be redefined in some classes implementing this interface in a more appropiate manner. 00121 */ 00122 virtual void line( 00123 int x0, 00124 int y0, 00125 int x1, 00126 int y1, 00127 const mrpt::utils::TColor color, 00128 unsigned int width = 1, 00129 TPenStyle penStyle = psSolid); 00130 00131 /** Draws a rectangle (an empty rectangle, without filling) 00132 * \param x0 The top-left x coordinate 00133 * \param y0 The top-left y coordinate 00134 * \param x1 The right-bottom x coordinate 00135 * \param y1 The right-bottom y coordinate 00136 * \param color The color of the line 00137 * \param width The desired width of the line. 00138 * \sa filledRectangle 00139 */ 00140 void rectangle( 00141 int x0, 00142 int y0, 00143 int x1, 00144 int y1, 00145 const mrpt::utils::TColor color, 00146 unsigned int width = 1 ); 00147 00148 /*****************************************************AJOGD***************************************************/ 00149 /** Draws a triangle 00150 * \param x0 The triangle center x coordinate 00151 * \param y0 The triangle center y coordinate 00152 * \param size The size of the triangle 00153 * \param color The color of the line 00154 * \param inferior The position of the triangle 00155 * \param width The desired width of the line. 00156 * \sa triangle 00157 */ 00158 void triangle( 00159 int x0, 00160 int y0, 00161 int size, 00162 const mrpt::utils::TColor color, 00163 bool inferior = true, 00164 unsigned int width = 1 ); 00165 /************************************************************************************************************/ 00166 00167 /** Draws a filled rectangle. 00168 * \param x0 The top-left x coordinate 00169 * \param y0 The top-left y coordinate 00170 * \param x1 The right-bottom x coordinate 00171 * \param y1 The right-bottom y coordinate 00172 * \param color The color of the rectangle fill 00173 * This method may be redefined in some classes implementing this interface in a more appropiate manner. 00174 * \sa rectangle 00175 */ 00176 virtual void filledRectangle( 00177 int x0, 00178 int y0, 00179 int x1, 00180 int y1, 00181 const mrpt::utils::TColor color 00182 ); 00183 00184 /** Renders 2D text using bitmap fonts. 00185 * \param x0 The x coordinates 00186 * \param y0 The y coordinates 00187 * \param str The string to put. If using UNICODE characters, use UTF-8 encoding. 00188 * \param color The text color 00189 * 00190 * \sa selectTextFont 00191 */ 00192 virtual void textOut( 00193 int x0, 00194 int y0, 00195 const std::string &str, 00196 const mrpt::utils::TColor color 00197 ); 00198 00199 /** Select the current font used when drawing text. 00200 * \param fontName The name of the font 00201 * See <a href="http://www.mrpt.org/Implemented_2D_Fonts">the wiki</a> for a list of valid font names. 00202 * \sa textOut 00203 */ 00204 virtual void selectTextFont( const std::string &fontName ); 00205 00206 /** Draws an image as a bitmap at a given position. 00207 * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn 00208 * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn 00209 * \param img The image to be drawn in this canvas 00210 * This method may be redefined in some classes implementing this interface in a more appropiate manner. 00211 */ 00212 virtual void drawImage( 00213 int x, 00214 int y, 00215 const utils::CImage &img ); 00216 00217 /** Draw a cross. 00218 * \param x0 The point x coordinate 00219 * \param y0 The point y coordinate 00220 * \param color The color of the cross 00221 * \param size The size of the cross 00222 * \param type The cross type. It could be: "x" or "+" 00223 * \param width The desired width of the cross (this is IGNORED yet) 00224 */ 00225 void cross (int x0,int y0, const mrpt::utils::TColor color,char type, unsigned int size=5, unsigned int width = 1); 00226 00227 /** Draws an image as a bitmap at a given position, with some custom scale and rotation changes. 00228 * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn 00229 * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn 00230 * \param rotation The rotation in radians, positive values being anti-clockwise direction, 0 is the normal position. 00231 * \param scale The scale factor, e.g. 2 means twice the original size. 00232 * \param img The image to be drawn in this canvas 00233 * This method may be redefined in some classes implementing this interface in a more appropiate manner. 00234 */ 00235 virtual void drawImage( 00236 int x, 00237 int y, 00238 const utils::CImage &img, 00239 float rotation, 00240 float scale ); 00241 00242 /** Draws an image as a bitmap at a given position. 00243 * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn 00244 * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn 00245 * \param img The image to be drawn in this canvas 00246 * This method actually calls internally to "drawImage" with a "CImage" parameter. 00247 */ 00248 void drawImage( 00249 int x, 00250 int y, 00251 const utils::CImageFloat &img ); 00252 00253 /** Draws an image as a bitmap at a given position, with some custom scale and rotation changes. 00254 * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn 00255 * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn 00256 * \param rotation The rotation in radians, positive values being anti-clockwise direction, 0 is the normal position. 00257 * \param scale The scale factor, e.g. 2 means twice the original size. 00258 * \param img The image to be drawn in this canvas 00259 * This method actually calls internally to "drawImage" with a "CImage" parameter. 00260 */ 00261 void drawImage( 00262 int x, 00263 int y, 00264 const utils::CImageFloat &img, 00265 float rotation, 00266 float scale ); 00267 00268 /** Draws a circle of a given radius. 00269 * \param x The center - x coordinate in pixels. 00270 * \param y The center - y coordinate in pixels. 00271 * \param radius The radius - in pixels. 00272 * \param color The color of the circle. 00273 * \param width The desired width of the line (this is IGNORED in this virtual class) 00274 */ 00275 virtual void drawCircle( 00276 int x, 00277 int y, 00278 int radius, 00279 const mrpt::utils::TColor color = mrpt::utils::TColor(255,255,255), 00280 unsigned int width = 1 ); 00281 00282 /** Draws an ellipse representing a given confidence interval of a 2D Gaussian distribution. 00283 * \param mean_x The x coordinate of the center point of the ellipse. 00284 * \param mean_y The y coordinate of the center point of the ellipse. 00285 * \param cov2D A 2x2 covariance matrix. 00286 * \param confIntervalStds How many "sigmas" for the confidence level (i.e. 2->95%, 3=99.97%,...) 00287 * \param color The color of the ellipse 00288 * \param width The desired width of the line (this is IGNORED in this virtual class) 00289 * \param nEllipsePoints The number of points to generate to approximate the ellipse shape. 00290 * \exception std::exception On an invalid matrix. 00291 */ 00292 template <class MATRIX2X2> 00293 void ellipseGaussian( 00294 const MATRIX2X2 *cov2D, 00295 const double mean_x, 00296 const double mean_y, 00297 double confIntervalStds = 2, 00298 const mrpt::utils::TColor color = mrpt::utils::TColor(255,255,255), 00299 unsigned int width = 1, 00300 int nEllipsePoints = 20 00301 ) 00302 { 00303 MRPT_START; 00304 int x1=0,y1=0,x2=0,y2=0; 00305 double ang; 00306 MATRIX2X2 eigVal,eigVec; 00307 int i; 00308 00309 // Compute the eigen-vectors & values: 00310 cov2D->eigenVectors(eigVec,eigVal); 00311 00312 eigVal.Sqrt(); 00313 MATRIX2X2 M; 00314 M.multiply_ABt(eigVal, eigVec); 00315 00316 // Compute the points of the 2D ellipse: 00317 for (i=0,ang=0;i<nEllipsePoints;i++,ang+= (M_2PI/(nEllipsePoints-1))) 00318 { 00319 double ccos = cos(ang); 00320 double ssin = sin(ang); 00321 00322 x2 = round( mean_x + confIntervalStds * (ccos * M(0,0) + ssin * M(1,0)) ); 00323 y2 = round( mean_y + confIntervalStds * (ccos * M(0,1) + ssin * M(1,1)) ); 00324 00325 if (i>0) 00326 line( x1, y1,x2, y2,color,width ); 00327 00328 x1 = x2; 00329 y1 = y2; 00330 } // end for points on ellipse 00331 00332 MRPT_END_WITH_CLEAN_UP( \ 00333 std::cout << "Covariance matrix leading to error is:" << std::endl << *cov2D << std::endl; \ 00334 ); 00335 } 00336 00337 /** Draws a set of marks onto the image, given a generic container of entities having just "x" and "y" fields. 00338 * The class of FEATURELIST can be, for example, std::vector<TPoint2D>, std::vector<TPixelCoordsf> or mrpt::vision::CFeatureList 00339 * \sa drawFeatures 00340 */ 00341 template <class FEATURELIST> 00342 void drawFeaturesSimple( const FEATURELIST &list, const TColor &color = TColor::red ) 00343 { 00344 for(typename FEATURELIST::const_iterator i = list.begin(); i != list.end(); ++i ) { 00345 this->cross( round(i->x), round(i->y), color, '+' ); 00346 } 00347 } 00348 00349 /** Draws a set of marks (or scaled circles for features with scale) onto the image, given a generic container of features. 00350 * The class of FEATURELIST can be mrpt::vision::CFeatureList 00351 * \sa drawFeaturesSimple 00352 */ 00353 template <class FEATURELIST> 00354 void drawFeatures( const FEATURELIST &list, const TColor &color = TColor::red, const bool showIDs = false, const bool showResponse = false ) 00355 { 00356 for(typename FEATURELIST::const_iterator i = list.begin(); i != list.end(); ++i ) 00357 { 00358 const int x = round((*i)->x); 00359 const int y = round((*i)->y); 00360 this->cross(x,y, color, '+' ); 00361 if( showIDs ) this->textOut(x,y, format("%u", static_cast<unsigned int>((*i)->ID)), TColor::red ); 00362 if (showResponse) this->textOut( x,y+10, format("R:%u", static_cast<unsigned int>((*i)->response)), TColor::red ); 00363 if( ! (*i)->isPointFeature() ) this->drawCircle( round((*i)->x), round((*i)->y), (*i)->scale, TColor::red ); 00364 } 00365 } 00366 00367 00368 00369 }; // End of class 00370 00371 typedef CCanvas CMRPTCanvas; //!< Deprecated name. 00372 00373 } // end of namespace utils 00374 00375 } // end of namespace mrpt 00376 00377 #endif
| Page generated by Doxygen 1.7.1 for MRPT 0.9.1 SVN:exported at Fri Aug 6 10:31:35 UTC 2010 |
