Main MRPT website > C++ reference
MRPT logo
CEnhancedMetaFile.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CEnhancedMetaFile_H
29 #define CEnhancedMetaFile_H
30 
31 #include <mrpt/utils/utils_defs.h>
32 #include <mrpt/utils/CCanvas.h>
34 
35 /*---------------------------------------------------------------
36  Class
37  ---------------------------------------------------------------*/
38 namespace mrpt
39 {
40 namespace utils
41 {
42  /** This class represents a Windows Enhanced Meta File (EMF) for generating and saving graphics.
43  * If used under Linux, a ".png", non-vectorial, file will be generated instead.
44  * \ingroup mrpt_base_grp
45  */
47  {
48  private:
50  int m_scale;
52  std::string m_targetFile;
53 
54  public:
55  static int LINUX_IMG_WIDTH; //!< In Linux, the size of the bitmap image that emulates the EMF (Default:800)
56  static int LINUX_IMG_HEIGHT; //!< In Linux, the size of the bitmap image that emulates the EMF (Default:600)
57 
58 
59  /** Constructor
60  * \param targetFileName This file will be created and the EMF saved there.
61  * \param scaleFactor All coordinates in draw commands will be internally multiplied by this scale, to provide a way of obtaining "subpixel" drawing.
62  */
64  const std::string &targetFileName,
65  int scaleFactor = 1);
66 
67  /** Destructor
68  */
69  virtual ~CEnhancedMetaFile( );
70 
71  /** Changes the value of the pixel (x,y).
72  * Pixel coordinates starts at the left-top corner of the image, and start in (0,0).
73  * The meaning of the parameter "color" depends on the implementation: it will usually
74  * be a 24bit RGB value (0x00RRGGBB), but it can also be just a 8bit gray level.
75  * This method must support (x,y) values OUT of the actual image size without neither
76  * raising exceptions, nor leading to memory access errors.
77  */
78  virtual void setPixel( int x, int y, size_t color);
79 
80  /** Returns the width of the image in pixels (this currently has no applicability for a EMF file...)
81  */
82  virtual size_t getWidth() const { return 640; }
83 
84  /** Returns the height of the image in pixels (this currently has no applicability for a EMF file...)
85  */
86  virtual size_t getHeight() const {return 480;}
87 
88  /** Draws an image as a bitmap at a given position.
89  * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn
90  * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn
91  * \param img The image to be drawn in this canvas
92  * This method may be redefined in some classes implementing this interface in a more appropiate manner.
93  */
94  virtual void drawImage(
95  int x,
96  int y,
97  const utils::CImage &img );
98 
99  /** Draws a line.
100  * \param x0 The starting point x coordinate
101  * \param y0 The starting point y coordinate
102  * \param x1 The end point x coordinate
103  * \param y1 The end point y coordinate
104  * \param color The color of the line
105  * \param width The desired width of the line (this is IGNORED in this virtual class)
106  * This method may be redefined in some classes implementing this interface in a more appropiate manner.
107  */
108  virtual void line(
109  int x0,
110  int y0,
111  int x1,
112  int y1,
113  const mrpt::utils::TColor color,
114  unsigned int width = 1,
115  TPenStyle penStyle = psSolid);
116 
117  /** Places a text label.
118  * \param x0 The x coordinates
119  * \param y0 The y coordinates
120  * \param str The string to put
121  * \param color The text color
122  * \param fontSize The font size, in "points"
123  * This method may be redefined in some classes implementing this interface in a more appropiate manner.
124  * \sa rectangle
125  */
126  virtual void textOut(
127  int x0,
128  int y0,
129  const std::string &str,
130  const mrpt::utils::TColor color
131  );
132 
133  /** Select the current font used when drawing text.
134  * \param fontName The face name of a font (e.g. "Arial","System",...)
135  * \param fontSize The size of the font in pts.
136  * \param bold Whether the font is bold
137  * \param italic Whether the font is italic
138  * \sa textOut
139  */
140  virtual void selectTextFont(
141  const std::string &fontName,
142  int fontSize,
143  bool bold = false,
144  bool italic = false );
145 
146  /** Draws an image as a bitmap at a given position, with some custom scale and rotation changes.
147  * \param x0 The top-left corner x coordinates on this canvas where the image is to be drawn
148  * \param y0 The top-left corner y coordinates on this canvas where the image is to be drawn
149  * \param rotation The rotation in radians, positive values being anti-clockwise direction, 0 is the normal position.
150  * \param scale The scale factor, e.g. 2 means twice the original size.
151  * \param img The image to be drawn in this canvas
152  * This method may be redefined in some classes implementing this interface in a more appropiate manner.
153  */
154  virtual void drawImage(
155  int x,
156  int y,
157  const utils::CImage &img,
158  float rotation,
159  float scale )
160  {
161  CCanvas::drawImage(x,y,img,rotation,scale);
162  }
163 
164 
165  /** Draws a rectangle (an empty rectangle, without filling)
166  * \param x0 The top-left x coordinate
167  * \param y0 The top-left y coordinate
168  * \param x1 The right-bottom x coordinate
169  * \param y1 The right-bottom y coordinate
170  * \param color The color of the line
171  * \param width The desired width of the line.
172  * \sa filledRectangle
173  */
174  virtual void rectangle(
175  int x0,
176  int y0,
177  int x1,
178  int y1,
179  const mrpt::utils::TColor color,
180  unsigned int width = 1 );
181 
182  /** Draws an ellipse representing a given confidence interval of a 2D Gaussian distribution.
183  * \param mean_x The x coordinate of the center point of the ellipse.
184  * \param mean_y The y coordinate of the center point of the ellipse.
185  * \param cov2D A 2x2 covariance matrix.
186  * \param confIntervalStds How many "sigmas" for the confidence level (i.e. 2->95%, 3=99.97%,...)
187  * \param color The color of the ellipse
188  * \param width The desired width of the line (this is IGNORED in this virtual class)
189  * \param nEllipsePoints The number of points to generate to approximate the ellipse shape.
190  * \exception std::exception On an invalid matrix.
191  */
192  template <class T>
193  void ellipseGaussian(
195  T mean_x,
196  T mean_y,
197  float confIntervalStds = 2,
198  const mrpt::utils::TColor color = mrpt::utils::TColor(255,255,255),
199  unsigned int width = 1,
200  int nEllipsePoints = 20
201  )
202  {
203  MRPT_START
204  int x1=0,y1=0,x2=0,y2=0;
205  double ang;
206  math::CMatrixTemplateNumeric<T> eigVal,eigVec;
207  int i;
208 
209  // Compute the eigen-vectors & values:
210  cov2D->eigenVectors(eigVec,eigVal);
211 
212  eigVal.Sqrt();
213  math::CMatrixTemplateNumeric<T> M( eigVal * (~eigVec) );
214 
215  // Compute the points of the 2D ellipse:
216  for (i=0,ang=0;i<nEllipsePoints;i++,ang+= (M_2PI/(nEllipsePoints-1)))
217  {
218  float ccos = cos(ang);
219  float ssin = sin(ang);
220 
221  x2 = round( mean_x + confIntervalStds * (ccos * M(0,0) + ssin * M(1,0)) );
222  y2 = round( mean_y + confIntervalStds * (ccos * M(0,1) + ssin * M(1,1)) );
223 
224  if (i>0)
225  line( x1, y1,x2, y2,color,width );
226 
227  x1 = x2;
228  y1 = y2;
229  } // end for points on ellipse
230 
232  std::cout << "Covariance matrix leading to error is:" << std::endl << *cov2D << std::endl; \
233  );
234  }
235 
236 
237 
238  }; // End of class def.
239 
240  } // End of namespace
241 } // end of namespace
242 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013