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



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020