Point Cloud Library (PCL)  1.9.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
pcl_painter2D.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  *
37  */
38 
39 #ifndef PCL_VISUALUALIZATION_PCL_PAINTER2D_H_
40 #define PCL_VISUALUALIZATION_PCL_PAINTER2D_H_
41 
42 #include <iostream>
43 #include <map>
44 #include <vector>
45 #include <pcl/pcl_exports.h>
46 #include <vtkRenderer.h>
47 #include <vtkRenderWindow.h>
48 #include <vtkRenderWindowInteractor.h>
49 #include <vtkSmartPointer.h>
50 #include <vtkObjectFactory.h>
51 #include <vtkContext2D.h>
52 #include <vtkTransform2D.h>
53 #include <vtkContextItem.h>
54 #include <vtkContextView.h>
55 #include <vtkContextScene.h>
56 #include <vtkPen.h>
57 #include <vtkBrush.h>
58 #include <vtkTextProperty.h>
59 #include <vtkOpenGLContextDevice2D.h>
60 #include <vtkPoints2D.h>
61 #include "vtkCommand.h"
62 
63 namespace pcl
64 {
65  namespace visualization
66  {
67 
68  /** \brief Abstract class for storing figure information. All the derived class uses the same method draw() to invoke different drawing function of vtkContext2D
69  * \author Kripasindhu Sarkar
70  * \ingroup visualization
71  */
72  struct Figure2D
73  {
74  std::vector<float> info_; //information stored in a general form for every object
75  vtkPen *pen_; //the corresponding pen and brush for the figure
76  vtkBrush *brush_;
77  vtkTransform2D *transform_;
78 
79  Figure2D (std::vector<float> info, vtkPen *p, vtkBrush * b, vtkTransform2D *t)
80  {
81  this->pen_ = vtkPen::New ();
82  this->brush_ = vtkBrush::New ();
83  this->transform_ = vtkTransform2D::New();
84 
85  this->pen_->DeepCopy (p);
86  this->brush_->DeepCopy (b);
87  this->transform_->SetMatrix (t->GetMatrix());
88  this->info_ = info; //note: it copies :-)
89  }
90 
91  Figure2D (vtkPen *p, vtkBrush * b, vtkTransform2D *t)
92  {
93  this->pen_ = vtkPen::New ();
94  this->brush_ = vtkBrush::New ();
95  this->transform_ = vtkTransform2D::New();
96 
97  this->pen_->DeepCopy (p);
98  this->brush_->DeepCopy (b);
99  this->transform_->SetMatrix (t->GetMatrix());
100  }
101 
102  void applyInternals (vtkContext2D *painter)
103  {
104  painter->ApplyPen (pen_);
105  painter->ApplyBrush (brush_);
106  painter->GetDevice ()->SetMatrix (transform_->GetMatrix());
107  }
108 
109  virtual void draw (vtkContext2D *) {}
110  };
111 
112  /** \brief Class for PolyLine
113  */
114  struct FPolyLine2D : public Figure2D
115  {
116 
117  FPolyLine2D (std::vector<float> info, vtkPen *p, vtkBrush * b, vtkTransform2D *t) : Figure2D (info, p, b, t){}
118 
119  void draw (vtkContext2D * painter)
120  {
121  applyInternals(painter);
122  painter->DrawPoly (&info_[0], static_cast<unsigned int> (info_.size ()) / 2);
123  }
124  };
125 
126  /** \brief Class for storing Points
127  */
128  struct FPoints2D : public Figure2D
129  {
130 
131  FPoints2D (std::vector<float> info, vtkPen *p, vtkBrush * b, vtkTransform2D *t) : Figure2D (info, p, b, t) {}
132 
133  void draw (vtkContext2D * painter)
134  {
135  applyInternals(painter);
136  painter->DrawPoints (&info_[0], static_cast<unsigned int> (info_.size ()) / 2);
137  }
138  };
139 
140  /** \brief Class for storing Quads
141  */
142  struct FQuad2D : public Figure2D
143  {
144 
145  FQuad2D (std::vector<float> info, vtkPen *p, vtkBrush * b, vtkTransform2D *t) : Figure2D (info, p, b, t) {}
146 
147  void draw (vtkContext2D * painter)
148  {
149  applyInternals(painter);
150  painter->DrawQuad (&info_[0]);
151  }
152  };
153 
154  /** \brief Class for Polygon
155  */
156  struct FPolygon2D : public Figure2D
157  {
158 
159  FPolygon2D (std::vector<float> info, vtkPen *p, vtkBrush * b, vtkTransform2D *t) : Figure2D (info, p, b, t){}
160 
161  void draw (vtkContext2D * painter)
162  {
163  applyInternals(painter);
164  painter->DrawPolygon (&info_[0], static_cast<unsigned int> (info_.size ()) / 2);
165  }
166  };
167 
168  /** \brief Class for storing EllipticArc; every ellipse , circle are covered by this
169  */
170  struct FEllipticArc2D : public Figure2D
171  {
172 
173  FEllipticArc2D (std::vector<float> info, vtkPen *p, vtkBrush * b, vtkTransform2D *t) : Figure2D (info, p, b, t) {}
174 
175  FEllipticArc2D (float x, float y, float rx, float ry, float sa, float ea, vtkPen *p, vtkBrush * b, vtkTransform2D *t) : Figure2D (p, b, t)
176  {
177  info_.resize (6);
178  info_[0] = x;
179  info_[1] = y;
180  info_[2] = rx;
181  info_[3] = ry;
182  info_[4] = sa;
183  info_[5] = ea;
184  }
185 
186  void draw (vtkContext2D * painter)
187  {
188  applyInternals(painter);
189  painter->DrawEllipticArc (info_[0], info_[1], info_[2], info_[3], info_[4], info_[5]);
190  }
191  };
192 
193 
194  ////////////////////////////////////The Main Painter Class begins here//////////////////////////////////////
195  /** \brief PCL Painter2D main class. Class for drawing 2D figures
196  * \author Kripasindhu Sarkar
197  * \ingroup visualization
198  */
199  class PCL_EXPORTS PCLPainter2D: public vtkContextItem
200  {
201  public:
202 
203  //static PCLPainter2D *New();
204 
205  /** \brief Constructor of the class
206  */
207  PCLPainter2D (char const * name = "PCLPainter2D");
208  vtkTypeMacro (PCLPainter2D, vtkContextItem);
209 
210  /** \brief Paint event for the chart, called whenever the chart needs to be drawn
211  * \param[in] painter Name of the window
212  */
213  virtual bool
214  Paint (vtkContext2D *painter);
215 
216  /** \brief Draw a line between the specified points.
217  * \param[in] x1 X coordinate of the starting point of the line
218  * \param[in] y1 Y coordinate of the starting point of the line
219  * \param[in] x2 X coordinate of the ending point of the line
220  * \param[in] y2 Y coordinate of the ending point of the line
221  */
222  void
223  addLine (float x1, float y1, float x2, float y2);
224 
225  /** \brief Draw line(s) between the specified points
226  * \param[in] p a vector of size 2*n and the points are packed x1, y1, x2, y2 etc.
227  */
228  void
229  addLine (std::vector<float> p);
230 
231 
232  /** \brief Draw specified point(s).
233  * \param[in] x X coordinate of the point
234  * \param[in] y Y coordinate of the point
235  */
236  void
237  addPoint (float x, float y);
238  /** \brief Draw specified point(s).
239  * \param[in] points a vector of size 2*n and the points are packed x1, y1, x2, y2 etc.
240  */
241 
242  void
243  addPoints (std::vector<float> points);
244 
245 
246  /** \brief Draw a rectangle based on the given points
247  * \param[in] x X coordinate of the origin
248  * \param[in] y Y coordinate of the origin
249  * \param[in] width width of the rectangle
250  * \param[in] height height of the rectangle
251  */
252  void
253  addRect (float x, float y, float width, float height);
254 
255  /** \brief Draw a quadrilateral based on the given points
256  * \param[in] p a vector of size 8 and the points are packed x1, y1, x2, y2, x3, y3 and x4, y4.
257  */
258  void
259  addQuad (std::vector<float> p);
260 
261  /** \brief Draw a polygon between the specified points
262  * \param[in] p a vector of size 2*n and the points are packed x1, y1, x2, y2 etc.
263  */
264  void
265  addPolygon (std::vector<float> p);
266 
267 
268  /** \brief Draw an ellipse based on the inputs
269  * \param[in] x X coordinate of the origin
270  * \param[in] y Y coordinate of the origin
271  * \param[in] rx X radius of the ellipse
272  * \param[in] ry Y radius of the ellipse
273  */
274  void
275  addEllipse (float x, float y, float rx, float ry);
276 
277  /** \brief Draw a circle based on the inputs
278  * \param[in] x X coordinate of the origin
279  * \param[in] y Y coordinate of the origin
280  * \param[in] r radius of the circle
281  */
282  void
283  addCircle (float x, float y, float r);
284 
285  /** \brief Draw an elliptic arc based on the inputs
286  * \param[in] x X coordinate of the origin
287  * \param[in] y Y coordinate of the origin
288  * \param[in] rx X radius of the ellipse
289  * \param[in] ry Y radius of the ellipse
290  * \param[in] start_angle the starting angle of the arc expressed in degrees
291  * \param[in] end_angle the ending angle of the arc expressed in degrees
292  */
293  void
294  addEllipticArc (float x, float y, float rx, float ry, float start_angle, float end_angle);
295 
296  /** \brief Draw an arc based on the inputs
297  * \param[in] x X coordinate of the origin
298  * \param[in] y Y coordinate of the origin
299  * \param[in] r radius of the circle
300  * \param[in] start_angle the starting angle of the arc expressed in degrees
301  * \param[in] end_angle the ending angle of the arc expressed in degrees
302  */
303  void
304  addArc (float x, float y, float r, float start_angle, float end_angle);
305 
306 
307  /** \brief Create a translation matrix and concatenate it with the current transformation.
308  * \param[in] x translation along X axis
309  * \param[in] y translation along Y axis
310  */
311  void
312  translatePen (double x, double y);
313 
314  /** \brief Create a rotation matrix and concatenate it with the current transformation.
315  * \param[in] angle angle in degrees
316  */
317  void
318  rotatePen(double angle);
319 
320  /** \brief Create a scale matrix and concatenate it with the current transformation.
321  * \param[in] x translation along X axis
322  * \param[in] y translation along Y axis
323  */
324  void
325  scalePen(double x, double y);
326 
327  /** \brief Create a translation matrix and concatenate it with the current transformation.
328  * \param[in] matrix the transformation matrix
329  */
330  void
331  setTransform(vtkMatrix3x3 *matrix);
332 
333  /** \brief Returns the current transformation matrix.
334  */
335  vtkMatrix3x3 *
336  getTransform();
337 
338  /** \brief Clears all the transformation applied. Sets the transformation matrix to Identity
339  */
340  void
341  clearTransform();
342 
343  /** \brief remove all the figures from the window
344  */
345  void
346  clearFigures();
347 
348  /** \brief set/get methods for current working vtkPen
349  */
350  void setPenColor (unsigned char r, unsigned char g, unsigned char b, unsigned char a);
351  void setPenWidth (float w);
352  void setPenType (int type);
353 
354  /** \brief set/get methods for current working vtkPen
355  */
356  unsigned char* getPenColor ();
357  float getPenWidth ();
358  int getPenType ();
359  void setPen (vtkPen *pen);
360  vtkPen* getPen ();
361 
362  /** \brief set/get methods for current working vtkBrush
363  */
364  void setBrush (vtkBrush *brush);
365  vtkBrush* getBrush ();
366  void setBrushColor (unsigned char r, unsigned char g, unsigned char b, unsigned char a);
367  unsigned char* getBrushColor ();
368 
369  /** \brief set/get method for the viewport's background color.
370  * \param[in] r the red component of the RGB color
371  * \param[in] g the green component of the RGB color
372  * \param[in] b the blue component of the RGB color
373  */
374  void
375  setBackgroundColor (const double r, const double g, const double b);
376 
377  /** \brief set/get method for the viewport's background color.
378  * \param [in] color the array containing the 3 component of the RGB color
379  */
380  void
381  setBackgroundColor (const double color[3]);
382 
383  /** \brief set/get method for the viewport's background color.
384  * \return [out] color the array containing the 3 component of the RGB color
385  */
386  double *
387  getBackgroundColor ();
388 
389 
390  /** \brief set/get method for the window size.
391  * \param[in] w the width of the window
392  * \param[in] h the height of the window
393  */
394  void
395  setWindowSize (int w, int h);
396 
397  /** \brief set/get method for the window size.
398  * \return[in] array containing the width and height of the window
399  */
400  int *
401  getWindowSize ();
402 
403  /** \brief displays all the figures added in a window.
404  */
405  void display ();
406 
407  /** \brief spins (runs the event loop) the interactor for spin_time amount of time. The name is confusing and will be probably obsolete in the future release with a single overloaded spin()/display() function.
408  * \param[in] spin_time - How long (in ms) should the visualization loop be allowed to run.
409  */
410  void spinOnce ( const int spin_time = 0 );
411 
412  /** \brief spins (runs the event loop) the interactor indefinitely. Same as display() - added to retain the similarity between other existing visualization classes
413  */
414  void spin ();
415 
416  private:
417  //std::map< int, std::vector< std::vector<float> > > figures_; //FIG_TYPE -> vector<array>
418 
419  //All the figures drawn till now gets stored here
420  std::vector<Figure2D *> figures_;
421 
422  //state variables of the class
423  vtkPen *current_pen_;
424  vtkBrush *current_brush_;
425  vtkTransform2D *current_transform_;
426  int win_width_, win_height_;
427  double bkg_color_[3];
428 
429  vtkContextView *view_;
430 
431  //####event callback class####
432  struct ExitMainLoopTimerCallback : public vtkCommand
433  {
434  static ExitMainLoopTimerCallback* New ()
435  {
436  return (new ExitMainLoopTimerCallback);
437  }
438  virtual void
439  Execute (vtkObject* vtkNotUsed (caller), unsigned long event_id, void* call_data)
440  {
441  if (event_id != vtkCommand::TimerEvent)
442  return;
443  int timer_id = *(reinterpret_cast<int*> (call_data));
444 
445  if (timer_id != right_timer_id)
446  return;
447 
448  // Stop vtk loop and send notification to app to wake it up
449 #if ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION <= 4))
450  interactor->stopLoop ();
451 #else
452  interactor->TerminateApp ();
453 #endif
454  }
455  int right_timer_id;
456 #if ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION <= 4))
457  PCLVisualizerInteractor *interactor;
458 #else
459  vtkRenderWindowInteractor *interactor;
460 #endif
461  };
462 
463  /** \brief Callback object enabling us to leave the main loop, when a timer fires. */
465  };
466 
467  }
468 }
469 
470 #endif /* PCL_VISUALUALIZATION_PCL_PAINTER2D_H_ */
471 
void applyInternals(vtkContext2D *painter)
Figure2D(std::vector< float > info, vtkPen *p, vtkBrush *b, vtkTransform2D *t)
Definition: pcl_painter2D.h:79
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
vtkTransform2D * transform_
Definition: pcl_painter2D.h:77
void draw(vtkContext2D *painter)
void draw(vtkContext2D *painter)
The PCLVisualizer interactor.
Definition: interactor.h:74
FQuad2D(std::vector< float > info, vtkPen *p, vtkBrush *b, vtkTransform2D *t)
PCL Painter2D main class.
void draw(vtkContext2D *painter)
virtual void draw(vtkContext2D *)
FPolyLine2D(std::vector< float > info, vtkPen *p, vtkBrush *b, vtkTransform2D *t)
Class for storing Points.
FPoints2D(std::vector< float > info, vtkPen *p, vtkBrush *b, vtkTransform2D *t)
void draw(vtkContext2D *painter)
Class for storing Quads.
FEllipticArc2D(float x, float y, float rx, float ry, float sa, float ea, vtkPen *p, vtkBrush *b, vtkTransform2D *t)
void draw(vtkContext2D *painter)
std::vector< float > info_
Definition: pcl_painter2D.h:74
FEllipticArc2D(std::vector< float > info, vtkPen *p, vtkBrush *b, vtkTransform2D *t)
Figure2D(vtkPen *p, vtkBrush *b, vtkTransform2D *t)
Definition: pcl_painter2D.h:91
Class for storing EllipticArc; every ellipse , circle are covered by this.
Abstract class for storing figure information.
Definition: pcl_painter2D.h:72
FPolygon2D(std::vector< float > info, vtkPen *p, vtkBrush *b, vtkTransform2D *t)