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