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



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020