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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST