Point Cloud Library (PCL)  1.14.0-dev
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 #pragma once
40 
41 #include <vector>
42 #include <pcl/pcl_exports.h>
43 #include <vtkRenderer.h>
44 #include <vtkRenderWindow.h>
45 #include <vtkRenderWindowInteractor.h>
46 #include <vtkSmartPointer.h>
47 #include <vtkObjectFactory.h>
48 #include <vtkContext2D.h>
49 #include <vtkTransform2D.h>
50 #include <vtkContextItem.h>
51 #include <vtkContextView.h>
52 #include <vtkContextScene.h>
53 #include <vtkPen.h>
54 #include <vtkBrush.h>
55 #include <vtkTextProperty.h>
56 #include <vtkOpenGLContextDevice2D.h>
57 #include <vtkPoints2D.h>
58 #include "vtkCommand.h"
59 
60 namespace pcl
61 {
62  namespace visualization
63  {
64 
65  /** \brief Abstract class for storing figure information. All the derived class uses the same method draw() to invoke different drawing function of vtkContext2D
66  * \author Kripasindhu Sarkar
67  * \ingroup visualization
68  */
69  struct Figure2D
70  {
71  std::vector<float> info_; //information stored in a general form for every object
72  vtkPen *pen_; //the corresponding pen and brush for the figure
73  vtkBrush *brush_;
74  vtkTransform2D *transform_;
75 
76  Figure2D (std::vector<float> info, vtkPen *p, vtkBrush * b, vtkTransform2D *t)
77  {
78  this->pen_ = vtkPen::New ();
79  this->brush_ = vtkBrush::New ();
80  this->transform_ = vtkTransform2D::New();
81 
82  this->pen_->DeepCopy (p);
83  this->brush_->DeepCopy (b);
84  this->transform_->SetMatrix (t->GetMatrix());
85  this->info_ = info; //note: it copies :-)
86  }
87 
88  Figure2D (vtkPen *p, vtkBrush * b, vtkTransform2D *t)
89  {
90  this->pen_ = vtkPen::New ();
91  this->brush_ = vtkBrush::New ();
92  this->transform_ = vtkTransform2D::New();
93 
94  this->pen_->DeepCopy (p);
95  this->brush_->DeepCopy (b);
96  this->transform_->SetMatrix (t->GetMatrix());
97  }
98 
99  virtual ~Figure2D()
100  {
101  pen_->Delete();
102  brush_->Delete();
103  transform_->Delete();
104  }
105 
106  void applyInternals (vtkContext2D *painter) const
107  {
108  painter->ApplyPen (pen_);
109  painter->ApplyBrush (brush_);
110  painter->GetDevice ()->SetMatrix (transform_->GetMatrix());
111  }
112 
113  virtual void draw (vtkContext2D *) {}
114  };
115 
116  /** \brief Class for PolyLine
117  */
118  struct FPolyLine2D : public Figure2D
119  {
120 
121  FPolyLine2D (std::vector<float> info, vtkPen *p, vtkBrush * b, vtkTransform2D *t) : Figure2D (info, p, b, t){}
122 
123  void draw (vtkContext2D * painter) override
124  {
125  applyInternals(painter);
126  painter->DrawPoly (info_.data(), static_cast<unsigned int> (info_.size ()) / 2);
127  }
128  };
129 
130  /** \brief Class for storing Points
131  */
132  struct FPoints2D : public Figure2D
133  {
134 
135  FPoints2D (std::vector<float> info, vtkPen *p, vtkBrush * b, vtkTransform2D *t) : Figure2D (info, p, b, t) {}
136 
137  void draw (vtkContext2D * painter) override
138  {
139  applyInternals(painter);
140  painter->DrawPoints (info_.data(), static_cast<unsigned int> (info_.size ()) / 2);
141  }
142  };
143 
144  /** \brief Class for storing Quads
145  */
146  struct FQuad2D : public Figure2D
147  {
148 
149  FQuad2D (std::vector<float> info, vtkPen *p, vtkBrush * b, vtkTransform2D *t) : Figure2D (info, p, b, t) {}
150 
151  void draw (vtkContext2D * painter) override
152  {
153  applyInternals(painter);
154  painter->DrawQuad (info_.data());
155  }
156  };
157 
158  /** \brief Class for Polygon
159  */
160  struct FPolygon2D : public Figure2D
161  {
162 
163  FPolygon2D (std::vector<float> info, vtkPen *p, vtkBrush * b, vtkTransform2D *t) : Figure2D (info, p, b, t){}
164 
165  void draw (vtkContext2D * painter) override
166  {
167  applyInternals(painter);
168  painter->DrawPolygon (info_.data(), static_cast<unsigned int> (info_.size ()) / 2);
169  }
170  };
171 
172  /** \brief Class for storing EllipticArc; every ellipse , circle are covered by this
173  */
174  struct FEllipticArc2D : public Figure2D
175  {
176 
177  FEllipticArc2D (std::vector<float> info, vtkPen *p, vtkBrush * b, vtkTransform2D *t) : Figure2D (info, p, b, t) {}
178 
179  FEllipticArc2D (float x, float y, float rx, float ry, float sa, float ea, vtkPen *p, vtkBrush * b, vtkTransform2D *t) : Figure2D (p, b, t)
180  {
181  info_.resize (6);
182  info_[0] = x;
183  info_[1] = y;
184  info_[2] = rx;
185  info_[3] = ry;
186  info_[4] = sa;
187  info_[5] = ea;
188  }
189 
190  void draw (vtkContext2D * painter) override
191  {
192  applyInternals(painter);
193  painter->DrawEllipticArc (info_[0], info_[1], info_[2], info_[3], info_[4], info_[5]);
194  }
195  };
196 
197 
198  ////////////////////////////////////The Main Painter Class begins here//////////////////////////////////////
199  /** \brief PCL Painter2D main class. Class for drawing 2D figures
200  * \author Kripasindhu Sarkar
201  * \ingroup visualization
202  */
203  class PCL_EXPORTS PCLPainter2D: public vtkContextItem
204  {
205  public:
206 
207  //static PCLPainter2D *New();
208 
209  /** \brief Constructor of the class
210  */
211  PCLPainter2D (char const * name = "PCLPainter2D");
212  vtkTypeMacro (PCLPainter2D, vtkContextItem);
213 
214  /** \brief Paint event for the chart, called whenever the chart needs to be drawn
215  * \param[in] painter Name of the window
216  */
217  bool
218  Paint (vtkContext2D *painter) override;
219 
220  /** \brief Draw a line between the specified points.
221  * \param[in] x1 X coordinate of the starting point of the line
222  * \param[in] y1 Y coordinate of the starting point of the line
223  * \param[in] x2 X coordinate of the ending point of the line
224  * \param[in] y2 Y coordinate of the ending point of the line
225  */
226  void
227  addLine (float x1, float y1, float x2, float y2);
228 
229  /** \brief Draw line(s) between the specified points
230  * \param[in] p a vector of size 2*n and the points are packed x1, y1, x2, y2 etc.
231  */
232  void
233  addLine (std::vector<float> p);
234 
235 
236  /** \brief Draw specified point(s).
237  * \param[in] x X coordinate of the point
238  * \param[in] y Y coordinate of the point
239  */
240  void
241  addPoint (float x, float y);
242  /** \brief Draw specified point(s).
243  * \param[in] points a vector of size 2*n and the points are packed x1, y1, x2, y2 etc.
244  */
245 
246  void
247  addPoints (std::vector<float> points);
248 
249 
250  /** \brief Draw a rectangle based on the given points
251  * \param[in] x X coordinate of the origin
252  * \param[in] y Y coordinate of the origin
253  * \param[in] width width of the rectangle
254  * \param[in] height height of the rectangle
255  */
256  void
257  addRect (float x, float y, float width, float height);
258 
259  /** \brief Draw a quadrilateral based on the given points
260  * \param[in] p a vector of size 8 and the points are packed x1, y1, x2, y2, x3, y3 and x4, y4.
261  */
262  void
263  addQuad (std::vector<float> p);
264 
265  /** \brief Draw a polygon between the specified points
266  * \param[in] p a vector of size 2*n and the points are packed x1, y1, x2, y2 etc.
267  */
268  void
269  addPolygon (std::vector<float> p);
270 
271 
272  /** \brief Draw an ellipse based on the inputs
273  * \param[in] x X coordinate of the origin
274  * \param[in] y Y coordinate of the origin
275  * \param[in] rx X radius of the ellipse
276  * \param[in] ry Y radius of the ellipse
277  */
278  void
279  addEllipse (float x, float y, float rx, float ry);
280 
281  /** \brief Draw a circle based on the inputs
282  * \param[in] x X coordinate of the origin
283  * \param[in] y Y coordinate of the origin
284  * \param[in] r radius of the circle
285  */
286  void
287  addCircle (float x, float y, float r);
288 
289  /** \brief Draw an elliptic arc based on the inputs
290  * \param[in] x X coordinate of the origin
291  * \param[in] y Y coordinate of the origin
292  * \param[in] rx X radius of the ellipse
293  * \param[in] ry Y radius of the ellipse
294  * \param[in] start_angle the starting angle of the arc expressed in degrees
295  * \param[in] end_angle the ending angle of the arc expressed in degrees
296  */
297  void
298  addEllipticArc (float x, float y, float rx, float ry, float start_angle, float end_angle);
299 
300  /** \brief Draw an arc based on the inputs
301  * \param[in] x X coordinate of the origin
302  * \param[in] y Y coordinate of the origin
303  * \param[in] r radius of the circle
304  * \param[in] start_angle the starting angle of the arc expressed in degrees
305  * \param[in] end_angle the ending angle of the arc expressed in degrees
306  */
307  void
308  addArc (float x, float y, float r, float start_angle, float end_angle);
309 
310 
311  /** \brief Create a translation matrix and concatenate it with the current transformation.
312  * \param[in] x translation along X axis
313  * \param[in] y translation along Y axis
314  */
315  void
316  translatePen (double x, double y);
317 
318  /** \brief Create a rotation matrix and concatenate it with the current transformation.
319  * \param[in] angle angle in degrees
320  */
321  void
322  rotatePen(double angle);
323 
324  /** \brief Create a scale matrix and concatenate it with the current transformation.
325  * \param[in] x translation along X axis
326  * \param[in] y translation along Y axis
327  */
328  void
329  scalePen(double x, double y);
330 
331  /** \brief Create a translation matrix and concatenate it with the current transformation.
332  * \param[in] matrix the transformation matrix
333  */
334  void
335  setTransform(vtkMatrix3x3 *matrix);
336 
337  /** \brief Returns the current transformation matrix.
338  */
339  vtkMatrix3x3 *
341 
342  /** \brief Clears all the transformation applied. Sets the transformation matrix to Identity
343  */
344  void
346 
347  /** \brief remove all the figures from the window
348  */
349  void
351 
352  /** \brief set/get methods for current working vtkPen
353  */
354  void setPenColor (unsigned char r, unsigned char g, unsigned char b, unsigned char a);
355  void setPenWidth (float w);
356  void setPenType (int type);
357 
358  /** \brief set/get methods for current working vtkPen
359  */
360  unsigned char* getPenColor ();
361  float getPenWidth ();
362  int getPenType ();
363  void setPen (vtkPen *pen);
364  vtkPen* getPen ();
365 
366  /** \brief set/get methods for current working vtkBrush
367  */
368  void setBrush (vtkBrush *brush);
369  vtkBrush* getBrush ();
370  void setBrushColor (unsigned char r, unsigned char g, unsigned char b, unsigned char a);
371  unsigned char* getBrushColor ();
372 
373  /** \brief set/get method for the viewport's background color.
374  * \param[in] r the red component of the RGB color
375  * \param[in] g the green component of the RGB color
376  * \param[in] b the blue component of the RGB color
377  */
378  void
379  setBackgroundColor (const double r, const double g, const double b);
380 
381  /** \brief set/get method for the viewport's background color.
382  * \param [in] color the array containing the 3 component of the RGB color
383  */
384  void
385  setBackgroundColor (const double color[3]);
386 
387  /** \brief set/get method for the viewport's background color.
388  * \return [out] color the array containing the 3 component of the RGB color
389  */
390  double *
392 
393 
394  /** \brief set/get method for the window size.
395  * \param[in] w the width of the window
396  * \param[in] h the height of the window
397  */
398  void
399  setWindowSize (int w, int h);
400 
401  /** \brief set/get method for the window size.
402  * \return[in] array containing the width and height of the window
403  */
404  int *
405  getWindowSize () const;
406 
407  /** \brief displays all the figures added in a window.
408  */
409  void display ();
410 
411  /** \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.
412  * \param[in] spin_time - How long (in ms) should the visualization loop be allowed to run.
413  */
414  void spinOnce ( const int spin_time = 0 );
415 
416  /** \brief spins (runs the event loop) the interactor indefinitely. Same as display() - added to retain the similarity between other existing visualization classes
417  */
418  void spin ();
419 
420  private:
421  //std::map< int, std::vector< std::vector<float> > > figures_; //FIG_TYPE -> std::vector<array>
422 
423  //All the figures drawn till now gets stored here
424  std::vector<Figure2D *> figures_;
425 
426  //state variables of the class
427  vtkPen *current_pen_;
428  vtkBrush *current_brush_;
429  vtkTransform2D *current_transform_;
430  int win_width_, win_height_;
431  double bkg_color_[3];
432 
433  vtkContextView *view_;
434 
435  //####event callback class####
436  struct ExitMainLoopTimerCallback : public vtkCommand
437  {
438  static ExitMainLoopTimerCallback* New ()
439  {
440  return (new ExitMainLoopTimerCallback);
441  }
442  void
443  Execute (vtkObject* vtkNotUsed (caller), unsigned long event_id, void* call_data) override
444  {
445  if (event_id != vtkCommand::TimerEvent)
446  return;
447  int timer_id = *(reinterpret_cast<int*> (call_data));
448 
449  if (timer_id != right_timer_id)
450  return;
451 
452  // Stop vtk loop and send notification to app to wake it up
453  interactor->TerminateApp ();
454  }
455  int right_timer_id;
456  vtkRenderWindowInteractor *interactor;
457  };
458 
459  /** \brief Callback object enabling us to leave the main loop, when a timer fires. */
461  };
462 
463  }
464 }
PCL Painter2D main class.
void clearTransform()
Clears all the transformation applied.
void addCircle(float x, float y, float r)
Draw a circle based on the inputs.
void spin()
spins (runs the event loop) the interactor indefinitely.
void addRect(float x, float y, float width, float height)
Draw a rectangle based on the given points.
PCLPainter2D(char const *name="PCLPainter2D")
Constructor of the class.
void setBrushColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
void rotatePen(double angle)
Create a rotation matrix and concatenate it with the current transformation.
void addEllipticArc(float x, float y, float rx, float ry, float start_angle, float end_angle)
Draw an elliptic arc based on the inputs.
void scalePen(double x, double y)
Create a scale matrix and concatenate it with the current transformation.
void addPoints(std::vector< float > points)
Draw specified point(s).
vtkMatrix3x3 * getTransform()
Returns the current transformation matrix.
void setBackgroundColor(const double r, const double g, const double b)
set/get method for the viewport's background color.
void display()
displays all the figures added in a window.
void addEllipse(float x, float y, float rx, float ry)
Draw an ellipse based on the inputs.
bool Paint(vtkContext2D *painter) override
Paint event for the chart, called whenever the chart needs to be drawn.
void setBackgroundColor(const double color[3])
set/get method for the viewport's background color.
void translatePen(double x, double y)
Create a translation matrix and concatenate it with the current transformation.
vtkTypeMacro(PCLPainter2D, vtkContextItem)
void addLine(std::vector< float > p)
Draw line(s) between the specified points.
void clearFigures()
remove all the figures from the window
void addPolygon(std::vector< float > p)
Draw a polygon between the specified points.
void addQuad(std::vector< float > p)
Draw a quadrilateral based on the given points.
void setTransform(vtkMatrix3x3 *matrix)
Create a translation matrix and concatenate it with the current transformation.
unsigned char * getPenColor()
set/get methods for current working vtkPen
void addPoint(float x, float y)
Draw specified point(s).
void setBrush(vtkBrush *brush)
set/get methods for current working vtkBrush
void addArc(float x, float y, float r, float start_angle, float end_angle)
Draw an arc based on the inputs.
void spinOnce(const int spin_time=0)
spins (runs the event loop) the interactor for spin_time amount of time.
double * getBackgroundColor()
set/get method for the viewport's background color.
void addLine(float x1, float y1, float x2, float y2)
Draw a line between the specified points.
void setPenColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
set/get methods for current working vtkPen
int * getWindowSize() const
set/get method for the window size.
void setWindowSize(int w, int h)
set/get method for the window size.
#define PCL_EXPORTS
Definition: pcl_macros.h:323
Class for storing EllipticArc; every ellipse , circle are covered by this.
void draw(vtkContext2D *painter) override
FEllipticArc2D(float x, float y, float rx, float ry, float sa, float ea, vtkPen *p, vtkBrush *b, vtkTransform2D *t)
FEllipticArc2D(std::vector< float > info, vtkPen *p, vtkBrush *b, vtkTransform2D *t)
Class for storing Points.
void draw(vtkContext2D *painter) override
FPoints2D(std::vector< float > info, vtkPen *p, vtkBrush *b, vtkTransform2D *t)
FPolyLine2D(std::vector< float > info, vtkPen *p, vtkBrush *b, vtkTransform2D *t)
void draw(vtkContext2D *painter) override
void draw(vtkContext2D *painter) override
FPolygon2D(std::vector< float > info, vtkPen *p, vtkBrush *b, vtkTransform2D *t)
Class for storing Quads.
FQuad2D(std::vector< float > info, vtkPen *p, vtkBrush *b, vtkTransform2D *t)
void draw(vtkContext2D *painter) override
Abstract class for storing figure information.
Definition: pcl_painter2D.h:70
void applyInternals(vtkContext2D *painter) const
std::vector< float > info_
Definition: pcl_painter2D.h:71
Figure2D(vtkPen *p, vtkBrush *b, vtkTransform2D *t)
Definition: pcl_painter2D.h:88
virtual void draw(vtkContext2D *)
Figure2D(std::vector< float > info, vtkPen *p, vtkBrush *b, vtkTransform2D *t)
Definition: pcl_painter2D.h:76
vtkTransform2D * transform_
Definition: pcl_painter2D.h:74