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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020