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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019