Point Cloud Library (PCL)  1.11.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 
41 #pragma once
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 #include <vtkRendererCollection.h>
55 #include <vtkRenderWindow.h>
56 
57 class vtkRendererCollection;
58 class vtkLegendScaleActor;
59 class vtkScalarBarActor;
60 class vtkPNGWriter;
61 class vtkWindowToImageFilter;
62 class vtkPointPicker;
63 
64 namespace pcl
65 {
66  namespace visualization
67  {
68 
69  /** \brief A list of potential keyboard modifiers for \ref pcl::visualization::PCLVisualizerInteractorStyle::PCLVisualizerInteractorStyle()
70  * Defaults to Alt.
71  */
73  {
77  };
78 
79  /** \brief PCLVisualizerInteractorStyle defines an unique, custom VTK
80  * based interactory style for PCL Visualizer applications. Besides
81  * defining the rendering style, we also create a list of custom actions
82  * that are triggered on different keys being pressed:
83  *
84  * - p, P : switch to a point-based representation
85  * - w, W : switch to a wireframe-based representation (where available)
86  * - s, S : switch to a surface-based representation (where available)
87  * - j, J : take a .PNG snapshot of the current window view
88  * - c, C : display current camera/window parameters
89  * - f, F : fly to point mode
90  * - e, E : exit the interactor\
91  * - q, Q : stop and call VTK's TerminateApp
92  * - + / - : increment/decrement overall point size
93  * - g, G : display scale grid (on/off)
94  * - u, U : display lookup table (on/off)
95  * - r, R [+ ALT] : reset camera [to viewpoint = {0, 0, 0} -> center_{x, y, z}]
96  * - CTRL + s, S : save camera parameters
97  * - CTRL + r, R : restore camera parameters
98  * - ALT + s, S : turn stereo mode on/off
99  * - ALT + f, F : switch between maximized window mode and original size
100  * - l, L : list all available geometric and color handlers for the current actor map
101  * - ALT + 0..9 [+ CTRL] : switch between different geometric handlers (where available)
102  * - 0..9 [+ CTRL] : switch between different color handlers (where available)
103  * -
104  * - SHIFT + left click : select a point
105  * - x, X : toggle rubber band selection mode for left mouse button
106  *
107  * \author Radu B. Rusu
108  * \ingroup visualization
109  */
110  class PCL_EXPORTS PCLVisualizerInteractorStyle : public vtkInteractorStyleRubberBandPick
111  {
112  public:
114 
115  static PCLVisualizerInteractorStyle *New ();
116 
117  /** \brief Empty constructor. */
119  init_ (), win_height_ (), win_width_ (), win_pos_x_ (), win_pos_y_ (),
120  max_win_height_ (), max_win_width_ (), use_vbos_ (false), grid_enabled_ (), lut_enabled_ (),
121  stereo_anaglyph_mask_default_ (),
122  modifier_ (), camera_saved_ (), lut_actor_id_ ("")
123  {}
124 
125  /** \brief Empty destructor */
127 
128  // this macro defines Superclass, the isA functionality and the safe downcast method
129  vtkTypeMacro (PCLVisualizerInteractorStyle, vtkInteractorStyleRubberBandPick);
130 
131  /** \brief Initialization routine. Must be called before anything else. */
132  virtual void
133  Initialize ();
134 
135  /** \brief Pass a pointer to the cloud actor map
136  * \param[in] actors the actor map that will be used with this style
137  */
138  inline void
139  setCloudActorMap (const CloudActorMapPtr &actors) { cloud_actors_ = actors; }
140 
141  /** \brief Pass a pointer to the shape actor map
142  * \param[in] actors the actor map that will be used with this style
143  */
144  inline void
145  setShapeActorMap (const ShapeActorMapPtr &actors) { shape_actors_ = actors; }
146 
147  /** \brief Get the cloud actor map pointer. */
148  inline CloudActorMapPtr
149  getCloudActorMap () { return (cloud_actors_); }
150 
151  /** \brief Get the cloud actor map pointer. */
152  inline ShapeActorMapPtr
153  getShapeActorMap () { return (shape_actors_); }
154 
155  /** \brief Pass a set of renderers to the interactor style.
156  * \param[in] rens the vtkRendererCollection to use
157  */
158  void
160 
161  /** \brief Use Vertex Buffer Objects renderers.
162  * This is an optimization for the obsolete OpenGL backend. Modern OpenGL2 backend (VTK ≥ 6.3) uses vertex
163  * buffer objects by default, transparently for the user.
164  * \param[in] use_vbos set to true to use VBOs
165  */
166  inline void
167  setUseVbos (const bool use_vbos) { use_vbos_ = use_vbos; }
168 
169  /** \brief Register a callback function for mouse events
170  * \param[in] cb a std function that will be registered as a callback for a mouse event
171  * \return a connection object that allows to disconnect the callback function.
172  */
173  boost::signals2::connection
174  registerMouseCallback (std::function<void (const pcl::visualization::MouseEvent&)> cb);
175 
176  /** \brief Register a callback std::function for keyboard events
177  * \param[in] cb a std function that will be registered as a callback for a keyboard event
178  * \return a connection object that allows to disconnect the callback function.
179  */
180  boost::signals2::connection
181  registerKeyboardCallback (std::function<void (const pcl::visualization::KeyboardEvent&)> cb);
182 
183  /** \brief Register a callback function for point picking events
184  * \param[in] cb a std function that will be registered as a callback for a point picking event
185  * \return a connection object that allows to disconnect the callback function.
186  */
187  boost::signals2::connection
188  registerPointPickingCallback (std::function<void (const pcl::visualization::PointPickingEvent&)> cb);
189 
190  /** \brief Register a callback function for area picking events
191  * \param[in] cb a std function that will be registered as a callback for a area picking event
192  * \return a connection object that allows to disconnect the callback function.
193  */
194  boost::signals2::connection
195  registerAreaPickingCallback (std::function<void (const pcl::visualization::AreaPickingEvent&)> cb);
196 
197  /** \brief Save the current rendered image to disk, as a PNG screenshot.
198  * \param[in] file the name of the PNG file
199  */
200  void
201  saveScreenshot (const std::string &file);
202 
203  /** \brief Save the camera parameters to disk, as a .cam file.
204  * \param[in] file the name of the .cam file
205  */
206  bool
207  saveCameraParameters (const std::string &file);
208 
209  /** \brief Get camera parameters of a given viewport (0 means default viewport). */
210  void
211  getCameraParameters (Camera &camera, int viewport = 0) const;
212 
213  /** \brief Load camera parameters from a camera parameter file.
214  * \param[in] file the name of the camera parameter file
215  */
216  bool
217  loadCameraParameters (const std::string &file);
218 
219  /** \brief Set the camera parameters via an intrinsics and and extrinsics matrix
220  * \note This assumes that the pixels are square and that the center of the image is at the center of the sensor.
221  * \param[in] intrinsics the intrinsics that will be used to compute the VTK camera parameters
222  * \param[in] extrinsics the extrinsics that will be used to compute the VTK camera parameters
223  * \param[in] viewport the viewport to modify camera of (0 modifies all cameras)
224  */
225  void
226  setCameraParameters (const Eigen::Matrix3f &intrinsics, const Eigen::Matrix4f &extrinsics, int viewport = 0);
227 
228  /** \brief Set the camera parameters by given a full camera data structure.
229  * \param[in] camera camera structure containing all the camera parameters.
230  * \param[in] viewport the viewport to modify camera of (0 modifies all cameras)
231  */
232  void
233  setCameraParameters (const Camera &camera, int viewport = 0);
234 
235  /** \brief Set camera file for camera parameter saving/restoring.
236  * \param[in] file the name of the camera parameter file
237  */
238  void
239  setCameraFile (const std::string file)
240  {
241  camera_file_ = file;
242  }
243 
244  /** \brief Get camera file for camera parameter saving/restoring. */
245  std::string
246  getCameraFile () const
247  {
248  return (camera_file_);
249  }
250 
251  /** \brief Change the default keyboard modified from ALT to a different special key.
252  * Allowed values are:
253  * - INTERACTOR_KB_MOD_ALT
254  * - INTERACTOR_KB_MOD_CTRL
255  * - INTERACTOR_KB_MOD_SHIFT
256  * \param[in] modifier the new keyboard modifier
257  */
258  inline void
260  {
261  modifier_ = modifier;
262  }
263 
264  protected:
265  /** \brief Set to true after initialization is complete. */
266  bool init_;
267 
268  /** \brief Collection of vtkRenderers stored internally. */
270 
271  /** \brief Cloud actor map stored internally. */
273 
274  /** \brief Shape map stored internally. */
276 
277  /** \brief The current window width/height. */
278  int win_height_, win_width_;
279 
280  /** \brief The current window position x/y. */
281  int win_pos_x_, win_pos_y_;
282 
283  /** \brief The maximum resizeable window width/height. */
284  int max_win_height_, max_win_width_;
285 
286  /** \brief Boolean that holds whether or not to use the vtkVertexBufferObjectMapper*/
287  bool use_vbos_;
288 
289  /** \brief Set to true if the grid actor is enabled. */
291  /** \brief Actor for 2D grid on screen. */
293 
294  /** \brief Set to true if the LUT actor is enabled. */
296  /** \brief Actor for 2D lookup table on screen. */
298 
299  /** \brief A PNG writer for screenshot captures. */
301  /** \brief Internal window to image filter. Needed by \a snapshot_writer_. */
303  /** \brief Stores the point picker when switching to an area picker. */
305 
306  boost::signals2::signal<void (const pcl::visualization::MouseEvent&)> mouse_signal_;
307  boost::signals2::signal<void (const pcl::visualization::KeyboardEvent&)> keyboard_signal_;
308  boost::signals2::signal<void (const pcl::visualization::PointPickingEvent&)> point_picking_signal_;
309  boost::signals2::signal<void (const pcl::visualization::AreaPickingEvent&)> area_picking_signal_;
310 
311  /** \brief Interactor style internal method. Gets called whenever a key is pressed. */
312  void
313  OnChar () override;
314 
315  // Keyboard events
316  void
317  OnKeyDown () override;
318  void
319  OnKeyUp () override;
320 
321  // mouse button events
322  void
323  OnMouseMove () override;
324  void
325  OnLeftButtonDown () override;
326  void
327  OnLeftButtonUp () override;
328  void
329  OnMiddleButtonDown () override;
330  void
331  OnMiddleButtonUp () override;
332  void
333  OnRightButtonDown () override;
334  void
335  OnRightButtonUp () override;
336  void
337  OnMouseWheelForward () override;
338  void
339  OnMouseWheelBackward () override;
340 
341  // mouse move event
342  /** \brief Interactor style internal method. Gets called periodically if a timer is set. */
343  void
344  OnTimer () override;
345 
346  /** \brief Interactor style internal method. Zoom in. */
347  void
348  zoomIn ();
349 
350  /** \brief Interactor style internal method. Zoom out. */
351  void
352  zoomOut ();
353 
354  /** \brief Get camera parameters from a string vector.
355  * \param[in] camera A string vector:
356  * Clipping Range, Focal Point, Position, ViewUp, Distance, Field of View Y, Window Size, Window Pos.
357  * Values in each string are separated by a ','
358  */
359  bool
360  getCameraParameters (const std::vector<std::string> &camera);
361 
362  /** \brief Set render window. */
363  void
365  {
366  win_ = win;
367  }
368 
369  /** \brief True if we're using red-blue colors for anaglyphic stereo, false if magenta-green. */
371 
372  /** \brief A VTK Mouse Callback object, used for point picking. */
374 
375  /** \brief The keyboard modifier to use. Default: Alt. */
377 
378  /** \brief Camera file for camera parameter saving/restoring. */
379  std::string camera_file_;
380  /** \brief A \ref pcl::visualization::Camera for camera parameter saving/restoring. */
382  /** \brief A \ref pcl::visualization::Camera is saved or not. */
384  /** \brief The render window.
385  * Only used when interactor maybe not available
386  */
388 
389  friend class PointPickingCallback;
390  friend class PCLVisualizer;
391 
392  private:
393  /** \brief ID used to fetch/display the look up table on the visualizer
394  * It should be set by PCLVisualizer \ref setLookUpTableID
395  * @note If empty, a random actor added to the interactor will be used */
396  std::string lut_actor_id_;
397 
398  /** \brief Add/remove the look up table displayed when 'u' is pressed, can also be used to update the current LUT displayed
399  * \ref lut_actor_id_ is used (if not empty) to chose which cloud/shape actor LUT will be updated (depending on what is available)
400  * If \ref lut_actor_id_ is empty the first actor with LUT support found will be used. */
401  void
402  updateLookUpTableDisplay (bool add_lut = false);
403  };
404 
405  /** \brief PCL histogram visualizer interactory style class.
406  * \author Radu B. Rusu
407  */
408  class PCLHistogramVisualizerInteractorStyle : public vtkInteractorStyleTrackballCamera
409  {
410  public:
412 
413  /** \brief Empty constructor. */
415 
416  /** \brief Initialization routine. Must be called before anything else. */
417  void
418  Initialize ();
419 
420  /** \brief Pass a map of render/window/interactors to the interactor style.
421  * \param[in] wins the RenWinInteract map to use
422  */
423  void
424  setRenWinInteractMap (const RenWinInteractMap &wins) { wins_ = wins; }
425 
426  private:
427  /** \brief A map of all windows on screen (with their renderers and interactors). */
428  RenWinInteractMap wins_;
429 
430  /** \brief Set to true after initialization is complete. */
431  bool init_;
432 
433  /** \brief Interactor style internal method. Gets called whenever a key is pressed. */
434  void OnKeyDown () override;
435 
436  /** \brief Interactor style internal method. Gets called periodically if a timer is set. */
437  void OnTimer () override;
438  };
439  }
440 }
vtkSmartPointer< vtkWindowToImageFilter > wif_
Internal window to image filter.
vtkSmartPointer< vtkRendererCollection > rens_
Collection of vtkRenderers stored internally.
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:151
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.
shared_ptr< CloudActorMap > CloudActorMapPtr
Definition: actor_map.h:102
/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.
std::map< std::string, RenWinInteract > RenWinInteractMap
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_
static PCLHistogramVisualizerInteractorStyle * New()
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.
pcl::visualization::CloudActorMapPtr CloudActorMapPtr
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.
#define PCL_EXPORTS
Definition: pcl_macros.h:328
shared_ptr< ShapeActorMap > ShapeActorMapPtr
Definition: actor_map.h:105
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.