MRPT  2.0.0
CEnhancedMetaFile.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 
12 #include <mrpt/img/CCanvas.h>
13 #include <mrpt/math/math_frwds.h>
14 
15 namespace mrpt::img
16 {
17 /** This class represents a Windows Enhanced Meta File (EMF) for generating and
18  * saving graphics.
19  * If used under Linux, a ".png", non-vectorial, file will be generated
20  * instead.
21  * \ingroup mrpt_img_grp
22  */
23 class CEnhancedMetaFile : public CCanvas
24 {
25  private:
27  int m_scale;
29  std::string m_targetFile;
30 
31  public:
32  /** In Linux, the size of the bitmap image that emulates the EMF
33  * (Default:800) */
34  static void LINUX_IMG_WIDTH(int value);
35  static int LINUX_IMG_WIDTH();
36  /** In Linux, the size of the bitmap image that emulates the EMF
37  * (Default:600) */
38  static void LINUX_IMG_HEIGHT(int value);
39  static int LINUX_IMG_HEIGHT();
40 
41  /** Constructor
42  * \param targetFileName This file will be created and the EMF saved
43  * there.
44  * \param scaleFactor All coordinates in draw commands will be internally
45  * multiplied by this scale, to provide a way of obtaining "subpixel"
46  * drawing.
47  */
48  CEnhancedMetaFile(const std::string& targetFileName, int scaleFactor = 1);
49 
50  /** Destructor */
51  ~CEnhancedMetaFile() override;
52 
53  /** Changes the value of the pixel (x,y).
54  * Pixel coordinates starts at the left-top corner of the image, and start
55  * in (0,0).
56  * The meaning of the parameter "color" depends on the implementation: it
57  * will usually
58  * be a 24bit RGB value (0x00RRGGBB), but it can also be just a 8bit gray
59  * level.
60  * This method must support (x,y) values OUT of the actual image size
61  * without neither
62  * raising exceptions, nor leading to memory access errors.
63  */
64  void setPixel(int x, int y, size_t color) override;
65 
66  /** Returns the width of the image in pixels (this currently has no
67  * applicability for a EMF file...) */
68  size_t getWidth() const override { return 640; }
69  /** Returns the height of the image in pixels (this currently has no
70  * applicability for a EMF file...) */
71  size_t getHeight() const override { return 480; }
72  /** Draws an image as a bitmap at a given position.
73  * \param x0 The top-left corner x coordinates on this canvas where the
74  * image is to be drawn
75  * \param y0 The top-left corner y coordinates on this canvas where the
76  * image is to be drawn
77  * \param img The image to be drawn in this canvas
78  * This method may be redefined in some classes implementing this
79  * interface in a more appropiate manner.
80  */
81  void drawImage(int x, int y, const mrpt::img::CImage& img) override;
82 
83  /** Draws a line.
84  * \param x0 The starting point x coordinate
85  * \param y0 The starting point y coordinate
86  * \param x1 The end point x coordinate
87  * \param y1 The end point y coordinate
88  * \param color The color of the line
89  * \param width The desired width of the line (this is IGNORED in this
90  * virtual class)
91  * This method may be redefined in some classes implementing this
92  * interface in a more appropiate manner.
93  */
94  void line(
95  int x0, int y0, int x1, int y1, const mrpt::img::TColor color,
96  unsigned int width = 1, TPenStyle penStyle = psSolid) override;
97 
98  /** Places a text label.
99  * \param x0 The x coordinates
100  * \param y0 The y coordinates
101  * \param str The string to put
102  * \param color The text color
103  * \param fontSize The font size, in "points"
104  * This method may be redefined in some classes implementing this
105  * interface in a more appropiate manner.
106  * \sa rectangle
107  */
108  void textOut(
109  int x0, int y0, const std::string& str,
110  const mrpt::img::TColor color) override;
111 
112  /** Select the current font used when drawing text.
113  * \param fontName The face name of a font (e.g. "Arial","System",...)
114  * \param fontSize The size of the font in pts.
115  * \param bold Whether the font is bold
116  * \param italic Whether the font is italic
117  * \sa textOut, CCanvas::selectTextFont
118  */
119  virtual void selectVectorTextFont(
120  const std::string& fontName, int fontSize, bool bold = false,
121  bool italic = false);
122 
123  /** Draws an image as a bitmap at a given position, with some custom scale
124  * and rotation changes.
125  * \param x0 The top-left corner x coordinates on this canvas where the
126  * image is to be drawn
127  * \param y0 The top-left corner y coordinates on this canvas where the
128  * image is to be drawn
129  * \param rotation The rotation in radians, positive values being
130  * anti-clockwise direction, 0 is the normal position.
131  * \param scale The scale factor, e.g. 2 means twice the original size.
132  * \param img The image to be drawn in this canvas
133  * This method may be redefined in some classes implementing this
134  * interface in a more appropiate manner.
135  */
136  void drawImage(
137  int x, int y, const mrpt::img::CImage& img, float rotation,
138  float scale) override
139  {
140  CCanvas::drawImage(x, y, img, rotation, scale);
141  }
142 
143  /** Draws a rectangle (an empty rectangle, without filling)
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 line
149  * \param width The desired width of the line.
150  * \sa filledRectangle
151  */
152  virtual void rectangle(
153  int x0, int y0, int x1, int y1, const mrpt::img::TColor color,
154  unsigned int width = 1);
155 
156  /** Draws an ellipse representing a given confidence interval of a 2D
157  * Gaussian distribution.
158  * \param mean_x The x coordinate of the center point of the ellipse.
159  * \param mean_y The y coordinate of the center point of the ellipse.
160  * \param cov2D A 2x2 covariance matrix.
161  * \param confIntervalStds How many "sigmas" for the confidence level (i.e.
162  * 2->95%, 3=99.97%,...)
163  * \param color The color of the ellipse
164  * \param width The desired width of the line (this is IGNORED in this
165  * virtual class)
166  * \param nEllipsePoints The number of points to generate to approximate
167  * the ellipse shape.
168  * \exception std::exception On an invalid matrix.
169  */
170  template <class T>
172  mrpt::math::CMatrixDynamic<T>* cov2D, T mean_x, T mean_y,
173  float confIntervalStds = 2,
174  const mrpt::img::TColor& color = mrpt::img::TColor(255, 255, 255),
175  unsigned int width = 1, int nEllipsePoints = 20)
176  {
177  MRPT_START
178  int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
179  double ang;
180  math::CMatrixDynamic<T> eigVal, eigVec;
181  int i;
182 
183  // Compute the eigen-vectors & values:
184  cov2D->eigenVectors(eigVec, eigVal);
185 
186  eigVal.Sqrt();
187  math::CMatrixDynamic<T> M(eigVal * (~eigVec));
188 
189  // Compute the points of the 2D ellipse:
190  for (i = 0, ang = 0; i < nEllipsePoints;
191  i++, ang += (M_2PI / (nEllipsePoints - 1)))
192  {
193  float ccos = cos(ang);
194  float ssin = sin(ang);
195 
196  x2 = round(
197  mean_x + confIntervalStds * (ccos * M(0, 0) + ssin * M(1, 0)));
198  y2 = round(
199  mean_y + confIntervalStds * (ccos * M(0, 1) + ssin * M(1, 1)));
200 
201  if (i > 0) line(x1, y1, x2, y2, color, width);
202 
203  x1 = x2;
204  y1 = y2;
205  } // end for points on ellipse
206 
208  std::cout << "Covariance matrix leading to error is:" << std::endl
209  << *cov2D << std::endl;);
210  }
211 }; // End of class def.
212 } // namespace mrpt::img
This class represents a Windows Enhanced Meta File (EMF) for generating and saving graphics...
void textOut(int x0, int y0, const std::string &str, const mrpt::img::TColor color) override
Places a text label.
#define MRPT_START
Definition: exceptions.h:241
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
void setPixel(int x, int y, size_t color) override
Changes the value of the pixel (x,y).
void drawImage(int x, int y, const mrpt::img::CImage &img, float rotation, float scale) override
Draws an image as a bitmap at a given position, with some custom scale and rotation changes...
#define M_2PI
Definition: common.h:58
void drawImage(int x, int y, const mrpt::img::CImage &img) override
Draws an image as a bitmap at a given position.
void ellipseGaussian(mrpt::math::CMatrixDynamic< T > *cov2D, T mean_x, T mean_y, float 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.
#define MRPT_END_WITH_CLEAN_UP(stuff)
Definition: exceptions.h:247
virtual void selectVectorTextFont(const std::string &fontName, int fontSize, bool bold=false, bool italic=false)
Select the current font used when drawing text.
CEnhancedMetaFile(const std::string &targetFileName, int scaleFactor=1)
Constructor.
void line(int x0, int y0, int x1, int y1, const mrpt::img::TColor color, unsigned int width=1, TPenStyle penStyle=psSolid) override
Draws a line.
~CEnhancedMetaFile() override
Destructor.
virtual 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)
size_t getHeight() const override
Returns the height of the image in pixels (this currently has no applicability for a EMF file...
size_t getWidth() const override
Returns the width of the image in pixels (this currently has no applicability for a EMF file...
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
This template class provides the basic functionality for a general 2D any-size, resizable container o...
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.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020