Point Cloud Library (PCL)  1.8.1
interactor_style.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 #ifndef PCL_PCL_VISUALIZER_INTERACTOR_STYLE_H_
41 #define PCL_PCL_VISUALIZER_INTERACTOR_STYLE_H_
42 
43 #include <pcl/console/print.h>
44 #include <pcl/visualization/common/actor_map.h>
45 #include <pcl/visualization/common/ren_win_interact_map.h>
46 #include <pcl/visualization/keyboard_event.h>
47 #include <pcl/visualization/mouse_event.h>
48 #include <pcl/visualization/point_picking_event.h>
49 #include <pcl/visualization/area_picking_event.h>
50 #ifndef Q_MOC_RUN
51 #include <boost/signals2/signal.hpp>
52 #endif
53 #include <vtkInteractorStyleRubberBandPick.h>
54 
55 class vtkRendererCollection;
56 class vtkLegendScaleActor;
57 class vtkScalarBarActor;
58 class vtkPNGWriter;
59 class vtkWindowToImageFilter;
60 class vtkPointPicker;
61 
62 namespace pcl
63 {
64  namespace visualization
65  {
66 
67  /** \brief A list of potential keyboard modifiers for \ref pcl::visualization::PCLVisualizerInteractorStyle::PCLVisualizerInteractorStyle()
68  * Defaults to Alt.
69  */
71  {
75  };
76 
77  /** \brief PCLVisualizerInteractorStyle defines an unique, custom VTK
78  * based interactory style for PCL Visualizer applications. Besides
79  * defining the rendering style, we also create a list of custom actions
80  * that are triggered on different keys being pressed:
81  *
82  * - p, P : switch to a point-based representation
83  * - w, W : switch to a wireframe-based representation (where available)
84  * - s, S : switch to a surface-based representation (where available)
85  * - j, J : take a .PNG snapshot of the current window view
86  * - c, C : display current camera/window parameters
87  * - f, F : fly to point mode
88  * - e, E : exit the interactor\
89  * - q, Q : stop and call VTK's TerminateApp
90  * - + / - : increment/decrement overall point size
91  * - g, G : display scale grid (on/off)
92  * - u, U : display lookup table (on/off)
93  * - r, R [+ ALT] : reset camera [to viewpoint = {0, 0, 0} -> center_{x, y, z}]
94  * - CTRL + s, S : save camera parameters
95  * - CTRL + r, R : restore camera parameters
96  * - ALT + s, S : turn stereo mode on/off
97  * - ALT + f, F : switch between maximized window mode and original size
98  * - l, L : list all available geometric and color handlers for the current actor map
99  * - ALT + 0..9 [+ CTRL] : switch between different geometric handlers (where available)
100  * - 0..9 [+ CTRL] : switch between different color handlers (where available)
101  * -
102  * - SHIFT + left click : select a point
103  * - x, X : toggle rubber band selection mode for left mouse button
104  *
105  * \author Radu B. Rusu
106  * \ingroup visualization
107  */
108  class PCL_EXPORTS PCLVisualizerInteractorStyle : public vtkInteractorStyleRubberBandPick
109  {
110  typedef boost::shared_ptr<CloudActorMap> CloudActorMapPtr;
111 
112  public:
113  static PCLVisualizerInteractorStyle *New ();
114 
115  /** \brief Empty constructor. */
117  init_ (), rens_ (), cloud_actors_ (), shape_actors_ (), win_height_ (), win_width_ (), win_pos_x_ (), win_pos_y_ (),
118  max_win_height_ (), max_win_width_ (), use_vbos_ (false), grid_enabled_ (), grid_actor_ (), lut_enabled_ (),
119  lut_actor_ (), snapshot_writer_ (), wif_ (), mouse_signal_ (), keyboard_signal_ (),
120  point_picking_signal_ (), area_picking_signal_ (), stereo_anaglyph_mask_default_ (),
121  mouse_callback_ (), modifier_ (), camera_file_ (), camera_ (), camera_saved_ (), win_ (), lut_actor_id_ ("")
122  {}
123 
124  /** \brief Empty destructor */
126 
127  // this macro defines Superclass, the isA functionality and the safe downcast method
128  vtkTypeMacro (PCLVisualizerInteractorStyle, vtkInteractorStyleRubberBandPick);
129 
130  /** \brief Initialization routine. Must be called before anything else. */
131  virtual void
132  Initialize ();
133 
134  /** \brief Pass a pointer to the cloud actor map
135  * \param[in] actors the actor map that will be used with this style
136  */
137  inline void
138  setCloudActorMap (const CloudActorMapPtr &actors) { cloud_actors_ = actors; }
139 
140  /** \brief Pass a pointer to the shape actor map
141  * \param[in] actors the actor map that will be used with this style
142  */
143  inline void
144  setShapeActorMap (const ShapeActorMapPtr &actors) { shape_actors_ = actors; }
145 
146  /** \brief Get the cloud actor map pointer. */
147  inline CloudActorMapPtr
148  getCloudActorMap () { return (cloud_actors_); }
149 
150  /** \brief Get the cloud actor map pointer. */
151  inline ShapeActorMapPtr
152  getShapeActorMap () { return (shape_actors_); }
153 
154  /** \brief Pass a set of renderers to the interactor style.
155  * \param[in] rens the vtkRendererCollection to use
156  */
157  void
159 
160  /** \brief Use Vertex Buffer Objects renderers.
161  * This is an optimization for the obsolete OpenGL backend. Modern OpenGL2 backend (VTK ≥ 6.3) uses vertex
162  * buffer objects by default, transparently for the user.
163  * \param[in] use_vbos set to true to use VBOs
164  */
165  inline void
166  setUseVbos (const bool use_vbos) { use_vbos_ = use_vbos; }
167 
168  /** \brief Register a callback function for mouse events
169  * \param[in] cb a boost function that will be registered as a callback for a mouse event
170  * \return a connection object that allows to disconnect the callback function.
171  */
172  boost::signals2::connection
173  registerMouseCallback (boost::function<void (const pcl::visualization::MouseEvent&)> cb);
174 
175  /** \brief Register a callback boost::function for keyboard events
176  * \param[in] cb a boost function that will be registered as a callback for a keyboard event
177  * \return a connection object that allows to disconnect the callback function.
178  */
179  boost::signals2::connection
180  registerKeyboardCallback (boost::function<void (const pcl::visualization::KeyboardEvent&)> cb);
181 
182  /** \brief Register a callback function for point picking events
183  * \param[in] cb a boost function that will be registered as a callback for a point picking event
184  * \return a connection object that allows to disconnect the callback function.
185  */
186  boost::signals2::connection
187  registerPointPickingCallback (boost::function<void (const pcl::visualization::PointPickingEvent&)> cb);
188 
189  /** \brief Register a callback function for area picking events
190  * \param[in] cb a boost function that will be registered as a callback for a area picking event
191  * \return a connection object that allows to disconnect the callback function.
192  */
193  boost::signals2::connection
194  registerAreaPickingCallback (boost::function<void (const pcl::visualization::AreaPickingEvent&)> cb);
195 
196  /** \brief Save the current rendered image to disk, as a PNG screenshot.
197  * \param[in] file the name of the PNG file
198  */
199  void
200  saveScreenshot (const std::string &file);
201 
202  /** \brief Save the camera parameters to disk, as a .cam file.
203  * \param[in] file the name of the .cam file
204  */
205  bool
206  saveCameraParameters (const std::string &file);
207 
208  /** \brief Get camera parameters and save them to a \ref pcl::visualization::Camera.
209  * \param[out] camera the name of the \ref pcl::visualization::Camera
210  */
211  void
212  getCameraParameters (Camera &camera);
213 
214  /** \brief Load camera parameters from a camera parameter file.
215  * \param[in] file the name of the camera parameter file
216  */
217  bool
218  loadCameraParameters (const std::string &file);
219 
220  /** \brief Set the camera parameters via an intrinsics and and extrinsics matrix
221  * \note This assumes that the pixels are square and that the center of the image is at the center of the sensor.
222  * \param[in] intrinsics the intrinsics that will be used to compute the VTK camera parameters
223  * \param[in] extrinsics the extrinsics that will be used to compute the VTK camera parameters
224  * \param[in] viewport the viewport to modify camera of (0 modifies all cameras)
225  */
226  void
227  setCameraParameters (const Eigen::Matrix3f &intrinsics, const Eigen::Matrix4f &extrinsics, int viewport = 0);
228 
229  /** \brief Set the camera parameters by given a full camera data structure.
230  * \param[in] camera camera structure containing all the camera parameters.
231  * \param[in] viewport the viewport to modify camera of (0 modifies all cameras)
232  */
233  void
234  setCameraParameters (const Camera &camera, int viewport = 0);
235 
236  /** \brief Set camera file for camera parameter saving/restoring.
237  * \param[in] file the name of the camera parameter file
238  */
239  void
240  setCameraFile (const std::string file)
241  {
242  camera_file_ = file;
243  }
244 
245  /** \brief Get camera file for camera parameter saving/restoring. */
246  std::string
247  getCameraFile () const
248  {
249  return (camera_file_);
250  }
251 
252  /** \brief Change the default keyboard modified from ALT to a different special key.
253  * Allowed values are:
254  * - INTERACTOR_KB_MOD_ALT
255  * - INTERACTOR_KB_MOD_CTRL
256  * - INTERACTOR_KB_MOD_SHIFT
257  * \param[in] modifier the new keyboard modifier
258  */
259  inline void
261  {
262  modifier_ = modifier;
263  }
264 
265  protected:
266  /** \brief Set to true after initialization is complete. */
267  bool init_;
268 
269  /** \brief Collection of vtkRenderers stored internally. */
271 
272  /** \brief Cloud actor map stored internally. */
273  CloudActorMapPtr cloud_actors_;
274 
275  /** \brief Shape map stored internally. */
277 
278  /** \brief The current window width/height. */
279  int win_height_, win_width_;
280 
281  /** \brief The current window position x/y. */
282  int win_pos_x_, win_pos_y_;
283 
284  /** \brief The maximum resizeable window width/height. */
285  int max_win_height_, max_win_width_;
286 
287  /** \brief Boolean that holds whether or not to use the vtkVertexBufferObjectMapper*/
288  bool use_vbos_;
289 
290  /** \brief Set to true if the grid actor is enabled. */
292  /** \brief Actor for 2D grid on screen. */
294 
295  /** \brief Set to true if the LUT actor is enabled. */
297  /** \brief Actor for 2D lookup table on screen. */
299 
300  /** \brief A PNG writer for screenshot captures. */
302  /** \brief Internal window to image filter. Needed by \a snapshot_writer_. */
304  /** \brief Stores the point picker when switching to an area picker. */
306 
307  boost::signals2::signal<void (const pcl::visualization::MouseEvent&)> mouse_signal_;
308  boost::signals2::signal<void (const pcl::visualization::KeyboardEvent&)> keyboard_signal_;
309  boost::signals2::signal<void (const pcl::visualization::PointPickingEvent&)> point_picking_signal_;
310  boost::signals2::signal<void (const pcl::visualization::AreaPickingEvent&)> area_picking_signal_;
311 
312  /** \brief Interactor style internal method. Gets called whenever a key is pressed. */
313  virtual void
314  OnChar ();
315 
316  // Keyboard events
317  virtual void
318  OnKeyDown ();
319  virtual void
320  OnKeyUp ();
321 
322  // mouse button events
323  virtual void
324  OnMouseMove ();
325  virtual void
326  OnLeftButtonDown ();
327  virtual void
328  OnLeftButtonUp ();
329  virtual void
330  OnMiddleButtonDown ();
331  virtual void
332  OnMiddleButtonUp ();
333  virtual void
334  OnRightButtonDown ();
335  virtual void
336  OnRightButtonUp ();
337  virtual void
338  OnMouseWheelForward ();
339  virtual void
340  OnMouseWheelBackward ();
341 
342  // mouse move event
343  /** \brief Interactor style internal method. Gets called periodically if a timer is set. */
344  virtual void
345  OnTimer ();
346 
347  /** \brief Interactor style internal method. Zoom in. */
348  void
349  zoomIn ();
350 
351  /** \brief Interactor style internal method. Zoom out. */
352  void
353  zoomOut ();
354 
355  /** \brief Get camera parameters from a string vector.
356  * \param[in] camera A string vector:
357  * Clipping Range, Focal Point, Position, ViewUp, Distance, Field of View Y, Window Size, Window Pos.
358  * Values in each string are seperated by a ','
359  */
360  bool
361  getCameraParameters (const std::vector<std::string> &camera);
362 
363  /** \brief Set render window. */
364  void
366  {
367  win_ = win;
368  }
369 
370  /** \brief True if we're using red-blue colors for anaglyphic stereo, false if magenta-green. */
372 
373  /** \brief A VTK Mouse Callback object, used for point picking. */
375 
376  /** \brief The keyboard modifier to use. Default: Alt. */
378 
379  /** \brief Camera file for camera parameter saving/restoring. */
380  std::string camera_file_;
381  /** \brief A \ref pcl::visualization::Camera for camera parameter saving/restoring. */
383  /** \brief A \ref pcl::visualization::Camera is saved or not. */
385  /** \brief The render window.
386  * Only used when interactor maybe not available
387  */
389 
390  friend class PointPickingCallback;
391  friend class PCLVisualizer;
392 
393  private:
394  /** \brief ID used to fetch/display the look up table on the visualizer
395  * It should be set by PCLVisualizer \ref setLookUpTableID
396  * @note If empty, a random actor added to the interactor will be used */
397  std::string lut_actor_id_;
398 
399  /** \brief Add/remove the look up table displayed when 'u' is pressed, can also be used to update the current LUT displayed
400  * \ref lut_actor_id_ is used (if not empty) to chose which cloud/shape actor LUT will be updated (depending on what is available)
401  * If \ref lut_actor_id_ is empty the first actor with LUT support found will be used. */
402  void
403  updateLookUpTableDisplay (bool add_lut = false);
404  };
405 
406  /** \brief PCL histogram visualizer interactory style class.
407  * \author Radu B. Rusu
408  */
409  class PCLHistogramVisualizerInteractorStyle : public vtkInteractorStyleTrackballCamera
410  {
411  public:
413 
414  /** \brief Empty constructor. */
415  PCLHistogramVisualizerInteractorStyle () : wins_ (), init_ (false) {}
416 
417  /** \brief Initialization routine. Must be called before anything else. */
418  void
419  Initialize ();
420 
421  /** \brief Pass a map of render/window/interactors to the interactor style.
422  * \param[in] wins the RenWinInteract map to use
423  */
424  void
425  setRenWinInteractMap (const RenWinInteractMap &wins) { wins_ = wins; }
426 
427  private:
428  /** \brief A map of all windows on screen (with their renderers and interactors). */
429  RenWinInteractMap wins_;
430 
431  /** \brief Set to true after initialization is complete. */
432  bool init_;
433 
434  /** \brief Interactor style internal method. Gets called whenever a key is pressed. */
435  void OnKeyDown ();
436 
437  /** \brief Interactor style internal method. Gets called periodically if a timer is set. */
438  void OnTimer ();
439  };
440  }
441 }
442 
443 #endif
vtkSmartPointer< vtkWindowToImageFilter > wif_
Internal window to image filter.
virtual ~PCLVisualizerInteractorStyle()
Empty destructor.
vtkSmartPointer< vtkRendererCollection > rens_
Collection of vtkRenderers stored internally.
std::map< std::string, RenWinInteract > RenWinInteractMap
void setKeyboardModifier(const InteractorKeyboardModifier &modifier)
Change the default keyboard modified from ALT to a different special key.
CloudActorMapPtr cloud_actors_
Cloud actor map stored internally.
Camera class holds a set of camera parameters together with the window pos/size.
Definition: common.h:147
PCL histogram visualizer interactory style class.
InteractorKeyboardModifier modifier_
The keyboard modifier to use.
void setShapeActorMap(const ShapeActorMapPtr &actors)
Pass a pointer to the shape actor map.
vtkSmartPointer< vtkPNGWriter > snapshot_writer_
A PNG writer for screenshot captures.
std::string getCameraFile() const
Get camera file for camera parameter saving/restoring.
void setCloudActorMap(const CloudActorMapPtr &actors)
Pass a pointer to the cloud actor map.
/brief Class representing 3D point picking events.
InteractorKeyboardModifier
A list of potential keyboard modifiers for pcl::visualization::PCLVisualizerInteractorStyle::PCLVisua...
void setCameraFile(const std::string file)
Set camera file for camera parameter saving/restoring.
ShapeActorMapPtr getShapeActorMap()
Get the cloud actor map pointer.
boost::shared_ptr< CloudActorMap > CloudActorMapPtr
Definition: actor_map.h:100
bool stereo_anaglyph_mask_default_
True if we&#39;re using red-blue colors for anaglyphic stereo, false if magenta-green.
boost::signals2::signal< void(const pcl::visualization::KeyboardEvent &)> keyboard_signal_
PCL Visualizer main class.
void setUseVbos(const bool use_vbos)
Use Vertex Buffer Objects renderers.
boost::signals2::signal< void(const pcl::visualization::AreaPickingEvent &)> area_picking_signal_
vtkSmartPointer< vtkRenderWindow > win_
The render window.
std::string camera_file_
Camera file for camera parameter saving/restoring.
bool lut_enabled_
Set to true if the LUT actor is enabled.
void setRendererCollection(vtkSmartPointer< vtkRendererCollection > &rens)
Pass a set of renderers to the interactor style.
bool init_
Set to true after initialization is complete.
Camera camera_
A pcl::visualization::Camera for camera parameter saving/restoring.
PCLVisualizerInteractorStyle defines an unique, custom VTK based interactory style for PCL Visualizer...
vtkSmartPointer< PointPickingCallback > mouse_callback_
A VTK Mouse Callback object, used for point picking.
bool camera_saved_
A pcl::visualization::Camera is saved or not.
ShapeActorMapPtr shape_actors_
Shape map stored internally.
bool grid_enabled_
Set to true if the grid actor is enabled.
boost::signals2::signal< void(const pcl::visualization::PointPickingEvent &)> point_picking_signal_
void setRenderWindow(const vtkSmartPointer< vtkRenderWindow > &win)
Set render window.
/brief Class representing 3D area picking events.
/brief Class representing key hit/release events
bool use_vbos_
Boolean that holds whether or not to use the vtkVertexBufferObjectMapper.
vtkSmartPointer< vtkScalarBarActor > lut_actor_
Actor for 2D lookup table on screen.
boost::signals2::signal< void(const pcl::visualization::MouseEvent &)> mouse_signal_
void setRenWinInteractMap(const RenWinInteractMap &wins)
Pass a map of render/window/interactors to the interactor style.
boost::shared_ptr< ShapeActorMap > ShapeActorMapPtr
Definition: actor_map.h:103
vtkSmartPointer< vtkPointPicker > point_picker_
Stores the point picker when switching to an area picker.
vtkSmartPointer< vtkLegendScaleActor > grid_actor_
Actor for 2D grid on screen.
CloudActorMapPtr getCloudActorMap()
Get the cloud actor map pointer.