Main MRPT website > C++ reference
MRPT logo
CCanvas.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CCanvas_H
29 #define CCanvas_H
30 
31 #include <mrpt/utils/utils_defs.h>
32 #include <mrpt/utils/TColor.h>
34 #include <mrpt/math/ops_matrices.h>
35 
36 namespace mrpt
37 {
38  namespace math
39  {
40  class CMatrix;
41  class CMatrixD;
42  }
43  namespace utils
44  {
45  class CImage;
46 
47  /** This virtual class defines the interface of any object accepting drawing primitives on it.
48  *
49  * A number of text fonts can be selected with CCanvas::selectTextFont(). These are the
50  * implemented font names:
51  *
52  * - "6x13"
53  * - "6x13B" (bold)
54  * - "6x13O" (italic)
55  * - "9x15"
56  * - "9x15B" (bold)
57  * - "10x20"
58  * - "18x18ja" (Japanese, UNICODE character values)
59  *
60  * For an example of each font check the <a href="http://www.mrpt.org/Implemented_2D_Fonts">corresponding wiki page</a>.
61  *
62  * \sa CImage
63  * \ingroup mrpt_base_grp
64  */
66  {
67  protected:
68  std::string m_selectedFont; //!< The selected font name.
69 
70  const uint32_t *m_selectedFontBitmaps; //!< Direct access to character bitmaps.
71 
72  public:
73 
74  CCanvas();
75 
76  /** Definition of pen styles
77  */
78  enum TPenStyle
79  {
80  psSolid = 0,
81  psDash, /* ------- */
82  psDot, /* ....... */
83  psDashDot, /* _._._._ */
84  psDashDotDot /* _.._.._ */
85  };
86 
87  /** Dummy virtual destructor:
88  */
89  virtual ~CCanvas()
90  {
91  }
92 
93  /** Changes the value of the pixel (x,y).
94  * Pixel coordinates starts at the left-top corner of the image, and start in (0,0).
95  * The meaning of the parameter "color" depends on the implementation: it will usually
96  * be a 24bit RGB value (0x00RRGGBB), but it can also be just a 8bit gray level.
97  *
98  * You can also use a TColor() type as input and it will be automatically converted to size_t.
99  *
100  * This method must support (x,y) values OUT of the actual image size without neither
101  * raising exceptions, nor leading to memory access errors.
102  *
103  */
104  virtual void setPixel( int x, int y, size_t color) =0;
105 
106  /** Returns the width of the image in pixels
107  */
108  virtual size_t getWidth() const = 0;
109 
110  /** Returns the height of the image in pixels
111  */
112  virtual size_t getHeight() const = 0;
113 
114  /** Draws a line.
115  * \param x0 The starting point x coordinate
116  * \param y0 The starting point y coordinate
117  * \param x1 The end point x coordinate
118  * \param y1 The end point y coordinate
119  * \param color The color of the line
120  * \param width The desired width of the line (this is IGNORED in this virtual class)
121  * This method may be redefined in some classes implementing this interface in a more appropiate manner.
122  */
123  virtual void line(
124  int x0,
125  int y0,
126  int x1,
127  int y1,
128  const mrpt::utils::TColor color,
129  unsigned int width = 1,
130  TPenStyle penStyle = psSolid);
131 
132  /** Draws a rectangle (an empty rectangle, without filling)
133  * \param x0 The top-left x coordinate
134  * \param y0 The top-left y coordinate
135  * \param x1 The right-bottom x coordinate
136  * \param y1 The right-bottom y coordinate
137  * \param color The color of the line
138  * \param width The desired width of the line.
139  * \sa filledRectangle
140  */
141  void rectangle(
142  int x0,
143  int y0,
144  int x1,
145  int y1,
146  const mrpt::utils::TColor color,
147  unsigned int width = 1 );
148 
149  /*****************************************************AJOGD***************************************************/
150  /** Draws a triangle
151  * \param x0 The triangle center x coordinate
152  * \param y0 The triangle center y coordinate
153  * \param size The size of the triangle
154  * \param color The color of the line
155  * \param inferior The position of the triangle
156  * \param width The desired width of the line.
157  * \sa triangle
158  */
159  void triangle(
160  int x0,
161  int y0,
162  int size,
163  const mrpt::utils::TColor color,
164  bool inferior = true,
165  unsigned int width = 1 );
166  /************************************************************************************************************/
167 
168  /** Draws a filled rectangle.
169  * \param x0 The top-left x coordinate
170  * \param y0 The top-left y coordinate
171  * \param x1 The right-bottom x coordinate
172  * \param y1 The right-bottom y coordinate
173  * \param color The color of the rectangle fill
174  * This method may be redefined in some classes implementing this interface in a more appropiate manner.
175  * \sa rectangle
176  */
177  virtual void filledRectangle(
178  int x0,
179  int y0,
180  int x1,
181  int y1,
182  const mrpt::utils::TColor color
183  );
184 
185  /** Renders 2D text using bitmap fonts.
186  * \param x0 The x coordinates
187  * \param y0 The y coordinates
188  * \param str The string to put. If using UNICODE characters, use UTF-8 encoding.
189  * \param color The text color
190  *
191  * \sa selectTextFont
192  */
193  virtual void textOut(
194  int x0,
195  int y0,
196  const std::string &str,
197  const mrpt::utils::TColor color
198  );
199 
200  /** Select the current font used when drawing text.
201  * \param fontName The name of the font
202  *
203  * Valid font names:
204  * - 5x7
205  * - 6x13
206  * - 6x13B
207  * - 6x13O
208  * - 9x15 (Default at start-up)
209  * - 9x15B
210  * - 10x20
211  * - 18x18ja (Asian characters for UTF-8 strings - Only available if MRPT is built with MRPT_HAS_ASIAN_FONTS = true)
212  *
213  * <img src="sample_textFonts.png" >
214  *
215  * \sa textOut, The example in <a href="http://www.mrpt.org/Implemented_2D_Fonts">this page</a>.
216  */
217  virtual void selectTextFont( const std::string &fontName );
218 
219  /** Draws an image as a bitmap at a given position.
220  * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn
221  * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn
222  * \param img The image to be drawn in this canvas
223  * This method may be redefined in some classes implementing this interface in a more appropiate manner.
224  */
225  virtual void drawImage(
226  int x,
227  int y,
228  const utils::CImage &img );
229 
230  /** Draw a cross.
231  * \param x0 The point x coordinate
232  * \param y0 The point y coordinate
233  * \param color The color of the cross
234  * \param size The size of the cross
235  * \param type The cross type. It could be: 'x', '+' or ':'(like '+' but clear at the center dot)
236  * \param width The desired width of the cross (this is IGNORED yet)
237  */
238  void cross (int x0,int y0, const mrpt::utils::TColor color,char type, unsigned int size=5, unsigned int width = 1);
239 
240  /** Draws an image as a bitmap at a given position, with some custom scale and rotation changes.
241  * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn
242  * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn
243  * \param rotation The rotation in radians, positive values being anti-clockwise direction, 0 is the normal position.
244  * \param scale The scale factor, e.g. 2 means twice the original size.
245  * \param img The image to be drawn in this canvas
246  * This method may be redefined in some classes implementing this interface in a more appropiate manner.
247  */
248  virtual void drawImage(
249  int x,
250  int y,
251  const utils::CImage &img,
252  float rotation,
253  float scale );
254 
255  /** Draws a circle of a given radius.
256  * \param x The center - x coordinate in pixels.
257  * \param y The center - y coordinate in pixels.
258  * \param radius The radius - in pixels.
259  * \param color The color of the circle.
260  * \param width The desired width of the line (this is IGNORED in this virtual class)
261  */
262  virtual void drawCircle(
263  int x,
264  int y,
265  int radius,
266  const mrpt::utils::TColor color = mrpt::utils::TColor(255,255,255),
267  unsigned int width = 1 );
268 
269  /** Draws an ellipse representing a given confidence interval of a 2D Gaussian distribution.
270  * \param mean_x The x coordinate of the center point of the ellipse.
271  * \param mean_y The y coordinate of the center point of the ellipse.
272  * \param cov2D A 2x2 covariance matrix.
273  * \param confIntervalStds How many "sigmas" for the confidence level (i.e. 2->95%, 3=99.97%,...)
274  * \param color The color of the ellipse
275  * \param width The desired width of the line (this is IGNORED in this virtual class)
276  * \param nEllipsePoints The number of points to generate to approximate the ellipse shape.
277  * \exception std::exception On an invalid matrix.
278  */
279  template <class MATRIX2X2>
280  void ellipseGaussian(
281  const MATRIX2X2 *cov2D,
282  const double mean_x,
283  const double mean_y,
284  double confIntervalStds = 2,
285  const mrpt::utils::TColor color = mrpt::utils::TColor(255,255,255),
286  unsigned int width = 1,
287  int nEllipsePoints = 20
288  )
289  {
290  MRPT_START
291  int x1=0,y1=0,x2=0,y2=0;
292  double ang;
293  MATRIX2X2 eigVal,eigVec;
294  int i;
295 
296  // Compute the eigen-vectors & values:
297  cov2D->eigenVectors(eigVec,eigVal);
298 
299  eigVal.Sqrt();
300  MATRIX2X2 M;
301  M.multiply_ABt(eigVal, eigVec);
302 
303  // Compute the points of the 2D ellipse:
304  for (i=0,ang=0;i<nEllipsePoints;i++,ang+= (M_2PI/(nEllipsePoints-1)))
305  {
306  double ccos = cos(ang);
307  double ssin = sin(ang);
308 
309  x2 = round( mean_x + confIntervalStds * (ccos * M(0,0) + ssin * M(1,0)) );
310  y2 = round( mean_y + confIntervalStds * (ccos * M(0,1) + ssin * M(1,1)) );
311 
312  if (i>0)
313  line( x1, y1,x2, y2,color,width );
314 
315  x1 = x2;
316  y1 = y2;
317  } // end for points on ellipse
318 
320  std::cout << "Covariance matrix leading to error is:" << std::endl << *cov2D << std::endl; \
321  );
322  }
323 
324  /** Draws a set of marks onto the image, given a generic container of entities having just "x" and "y" fields.
325  * The class of FEATURELIST can be, for example, std::vector<TPoint2D>, std::vector<TPixelCoordsf> or mrpt::vision::CFeatureList
326  * \sa drawFeatures
327  */
328  template <class FEATURELIST>
329  void drawFeaturesSimple( const FEATURELIST &list, const TColor &color = TColor::red, const int cross_size = 5 )
330  {
331  for(size_t i=0;i<list.size(); ++i )
332  {
333  const int x = round( list.getFeatureX(i) );
334  const int y = round( list.getFeatureY(i) );
335  this->cross( x,y, color, '+', cross_size );
336  }
337  }
338 
339  /** Draws a set of marks (or scaled circles for features with scale) onto the image, given a generic container of features.
340  * The class of FEATURELIST can be:
341  * - mrpt::vision::CFeatureList
342  * - mrpt::vision::TSimpleFeatureList
343  *
344  * \sa drawFeaturesSimple
345  */
346  template <class FEATURELIST>
347  void drawFeatures( const FEATURELIST &list, const TColor &color = TColor::red, const bool showIDs = false, const bool showResponse = false )
348  {
349  for(size_t i=0;i<list.size(); ++i )
350  {
351  const int x = round( list.getFeatureX(i) );
352  const int y = round( list.getFeatureY(i) );
353  this->cross(x,y, color, '+' );
354  if( showIDs ) this->textOut(x,y, format("%u", static_cast<unsigned int>(list.getFeatureID(i))), TColor::red );
355  if (showResponse) this->textOut( x,y+10, format("R:%u", static_cast<unsigned int>(list.getFeatureResponse(i))), TColor::red );
356  if( ! list.isPointFeature(i) ) this->drawCircle(x,y, list.getScale(i), TColor::red );
357  }
358  }
359 
360 
361 
362  }; // End of class
363 
364  typedef CCanvas CMRPTCanvas; //!< Deprecated name.
365 
366  } // end of namespace utils
367 
368 } // end of namespace mrpt
369 
370 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013