Point Cloud Library (PCL)  1.14.1-dev
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 
113 
114  /** \brief Empty constructor. */
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
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  PCL_DEPRECATED(1, 18, "this function has no effect")
159  inline void
160  setUseVbos (const bool use_vbos) { use_vbos_ = use_vbos; }
161 
162  /** \brief Register a callback function for mouse events
163  * \param[in] cb a std function that will be registered as a callback for a mouse event
164  * \return a connection object that allows to disconnect the callback function.
165  */
166  boost::signals2::connection
167  registerMouseCallback (std::function<void (const pcl::visualization::MouseEvent&)> cb);
168 
169  /** \brief Register a callback std::function for keyboard events
170  * \param[in] cb a std function that will be registered as a callback for a keyboard event
171  * \return a connection object that allows to disconnect the callback function.
172  */
173  boost::signals2::connection
175 
176  /** \brief Register a callback function for point picking events
177  * \param[in] cb a std function that will be registered as a callback for a point picking event
178  * \return a connection object that allows to disconnect the callback function.
179  */
180  boost::signals2::connection
182 
183  /** \brief Register a callback function for area picking events
184  * \param[in] cb a std function that will be registered as a callback for a area picking event
185  * \return a connection object that allows to disconnect the callback function.
186  */
187  boost::signals2::connection
189 
190  /** \brief Save the current rendered image to disk, as a PNG screenshot.
191  * \param[in] file the name of the PNG file
192  */
193  void
194  saveScreenshot (const std::string &file);
195 
196  /** \brief Save the camera parameters to disk, as a .cam file.
197  * \param[in] file the name of the .cam file
198  */
199  bool
200  saveCameraParameters (const std::string &file);
201 
202  /** \brief Get camera parameters of a given viewport (0 means default viewport). */
203  void
204  getCameraParameters (Camera &camera, int viewport = 0) const;
205 
206  /** \brief Load camera parameters from a camera parameter file.
207  * \param[in] file the name of the camera parameter file
208  */
209  bool
210  loadCameraParameters (const std::string &file);
211 
212  /** \brief Set the camera parameters via an intrinsics and and extrinsics matrix
213  * \note This assumes that the pixels are square and that the center of the image is at the center of the sensor.
214  * \param[in] intrinsics the intrinsics that will be used to compute the VTK camera parameters
215  * \param[in] extrinsics the extrinsics that will be used to compute the VTK camera parameters
216  * \param[in] viewport the viewport to modify camera of (0 modifies all cameras)
217  */
218  void
219  setCameraParameters (const Eigen::Matrix3f &intrinsics, const Eigen::Matrix4f &extrinsics, int viewport = 0);
220 
221  /** \brief Set the camera parameters by given a full camera data structure.
222  * \param[in] camera camera structure containing all the camera parameters.
223  * \param[in] viewport the viewport to modify camera of (0 modifies all cameras)
224  */
225  void
226  setCameraParameters (const Camera &camera, int viewport = 0);
227 
228  /** \brief Set camera file for camera parameter saving/restoring.
229  * \param[in] file the name of the camera parameter file
230  */
231  void
232  setCameraFile (const std::string& file)
233  {
234  camera_file_ = file;
235  }
236 
237  /** \brief Get camera file for camera parameter saving/restoring. */
238  std::string
239  getCameraFile () const
240  {
241  return (camera_file_);
242  }
243 
244  /** \brief Change the default keyboard modified from ALT to a different special key.
245  * Allowed values are:
246  * - INTERACTOR_KB_MOD_ALT
247  * - INTERACTOR_KB_MOD_CTRL
248  * - INTERACTOR_KB_MOD_SHIFT
249  * \param[in] modifier the new keyboard modifier
250  */
251  inline void
253  {
254  modifier_ = modifier;
255  }
256 
257  protected:
258  /** \brief Set to true after initialization is complete. */
259  bool init_{false};
260 
261  /** \brief Collection of vtkRenderers stored internally. */
263 
264  /** \brief Cloud actor map stored internally. */
265  CloudActorMapPtr cloud_actors_{nullptr};
266 
267  /** \brief Shape map stored internally. */
268  ShapeActorMapPtr shape_actors_{nullptr};
269 
270  /** \brief The current window width/height. */
271  int win_height_{0}, win_width_{0};
272 
273  /** \brief The current window position x/y. */
274  int win_pos_x_{0}, win_pos_y_{0};
275 
276  /** \brief The maximum resizeable window width/height. */
277  int max_win_height_{0}, max_win_width_{0};
278 
279  /** \brief Boolean that holds whether or not to use the vtkVertexBufferObjectMapper*/
280  bool use_vbos_{false};
281 
282  /** \brief Set to true if the grid actor is enabled. */
283  bool grid_enabled_{false};
284  /** \brief Actor for 2D grid on screen. */
286 
287  /** \brief Set to true if the LUT actor is enabled. */
288  bool lut_enabled_{false};
289  /** \brief Actor for 2D lookup table on screen. */
291 
292  /** \brief A PNG writer for screenshot captures. */
294  /** \brief Internal window to image filter. Needed by \a snapshot_writer_. */
296  /** \brief Stores the point picker when switching to an area picker. */
298 
299  boost::signals2::signal<void (const pcl::visualization::MouseEvent&)> mouse_signal_;
300  boost::signals2::signal<void (const pcl::visualization::KeyboardEvent&)> keyboard_signal_;
301  boost::signals2::signal<void (const pcl::visualization::PointPickingEvent&)> point_picking_signal_;
302  boost::signals2::signal<void (const pcl::visualization::AreaPickingEvent&)> area_picking_signal_;
303 
304  /** \brief Interactor style internal method. Gets called whenever a key is pressed. */
305  void
306  OnChar () override;
307 
308  // Keyboard events
309  void
310  OnKeyDown () override;
311  void
312  OnKeyUp () override;
313 
314  // mouse button events
315  void
316  OnMouseMove () override;
317  void
318  OnLeftButtonDown () override;
319  void
320  OnLeftButtonUp () override;
321  void
322  OnMiddleButtonDown () override;
323  void
324  OnMiddleButtonUp () override;
325  void
326  OnRightButtonDown () override;
327  void
328  OnRightButtonUp () override;
329  void
330  OnMouseWheelForward () override;
331  void
332  OnMouseWheelBackward () override;
333 
334  // mouse move event
335  /** \brief Interactor style internal method. Gets called periodically if a timer is set. */
336  void
337  OnTimer () override;
338 
339  /** \brief Interactor style internal method. Zoom in. */
340  void
341  zoomIn ();
342 
343  /** \brief Interactor style internal method. Zoom out. */
344  void
346 
347  /** \brief Get camera parameters from a string vector.
348  * \param[in] camera A string vector:
349  * Clipping Range, Focal Point, Position, ViewUp, Distance, Field of View Y, Window Size, Window Pos.
350  * Values in each string are separated by a ','
351  */
352  bool
353  getCameraParameters (const std::vector<std::string> &camera);
354 
355  /** \brief Set render window. */
356  void
358  {
359  win_ = win;
360  }
361 
362  /** \brief True if we're using red-blue colors for anaglyphic stereo, false if magenta-green. */
363  bool stereo_anaglyph_mask_default_{false};
364 
365  /** \brief A VTK Mouse Callback object, used for point picking. */
367 
368  /** \brief The keyboard modifier to use. Default: Alt. */
370 
371  /** \brief Camera file for camera parameter saving/restoring. */
372  std::string camera_file_;
373  /** \brief A \ref pcl::visualization::Camera for camera parameter saving/restoring. */
375  /** \brief A \ref pcl::visualization::Camera is saved or not. */
376  bool camera_saved_{false};
377  /** \brief The render window.
378  * Only used when interactor maybe not available
379  */
381 
382  friend class PointPickingCallback;
383  friend class PCLVisualizer;
384 
385  private:
386  /** \brief ID used to fetch/display the look up table on the visualizer
387  * It should be set by PCLVisualizer \ref setLookUpTableID
388  * @note If empty, a random actor added to the interactor will be used */
389  std::string lut_actor_id_;
390 
391  /** \brief Add/remove the look up table displayed when 'u' is pressed, can also be used to update the current LUT displayed
392  * \ref lut_actor_id_ is used (if not empty) to chose which cloud/shape actor LUT will be updated (depending on what is available)
393  * If \ref lut_actor_id_ is empty the first actor with LUT support found will be used. */
394  void
395  updateLookUpTableDisplay (bool add_lut = false);
396  };
397 
398  /** \brief PCL histogram visualizer interactory style class.
399  * \author Radu B. Rusu
400  */
401  class PCLHistogramVisualizerInteractorStyle : public vtkInteractorStyleTrackballCamera
402  {
403  public:
405 
406  /** \brief Empty constructor. */
408 
409  /** \brief Initialization routine. Must be called before anything else. */
410  void
412 
413  /** \brief Pass a map of render/window/interactors to the interactor style.
414  * \param[in] wins the RenWinInteract map to use
415  */
416  void
417  setRenWinInteractMap (const RenWinInteractMap &wins) { wins_ = wins; }
418 
419  private:
420  /** \brief A map of all windows on screen (with their renderers and interactors). */
421  RenWinInteractMap wins_;
422 
423  /** \brief Set to true after initialization is complete. */
424  bool init_{false};
425 
426  /** \brief Interactor style internal method. Gets called whenever a key is pressed. */
427  void OnKeyDown () override;
428 
429  /** \brief Interactor style internal method. Gets called periodically if a timer is set. */
430  void OnTimer () override;
431  };
432  }
433 }
/brief Class representing 3D area picking events.
Camera class holds a set of camera parameters together with the window pos/size.
Definition: common.h:153
/brief Class representing key hit/release events
PCL histogram visualizer interactory style class.
void setRenWinInteractMap(const RenWinInteractMap &wins)
Pass a map of render/window/interactors to the interactor style.
PCLHistogramVisualizerInteractorStyle()=default
Empty constructor.
static PCLHistogramVisualizerInteractorStyle * New()
PCL Visualizer main class.
PCLVisualizerInteractorStyle defines an unique, custom VTK based interactory style for PCL Visualizer...
vtkSmartPointer< vtkWindowToImageFilter > wif_
Internal window to image filter.
void setCloudActorMap(const CloudActorMapPtr &actors)
Pass a pointer to the cloud actor map.
~PCLVisualizerInteractorStyle() override=default
Empty destructor.
void setCameraParameters(const Eigen::Matrix3f &intrinsics, const Eigen::Matrix4f &extrinsics, int viewport=0)
Set the camera parameters via an intrinsics and and extrinsics matrix.
void setRendererCollection(vtkSmartPointer< vtkRendererCollection > &rens)
Pass a set of renderers to the interactor style.
void getCameraParameters(Camera &camera, int viewport=0) const
Get camera parameters of a given viewport (0 means default viewport).
vtkTypeMacro(PCLVisualizerInteractorStyle, vtkInteractorStyleRubberBandPick)
vtkSmartPointer< vtkPointPicker > point_picker_
Stores the point picker when switching to an area picker.
CloudActorMapPtr getCloudActorMap()
Get the cloud actor map pointer.
virtual void Initialize()
Initialization routine.
ShapeActorMapPtr getShapeActorMap()
Get the cloud actor map pointer.
void saveScreenshot(const std::string &file)
Save the current rendered image to disk, as a PNG screenshot.
void setShapeActorMap(const ShapeActorMapPtr &actors)
Pass a pointer to the shape actor map.
pcl::visualization::CloudActorMapPtr CloudActorMapPtr
boost::signals2::connection registerMouseCallback(std::function< void(const pcl::visualization::MouseEvent &)> cb)
Register a callback function for mouse events.
bool loadCameraParameters(const std::string &file)
Load camera parameters from a camera parameter file.
void setRenderWindow(const vtkSmartPointer< vtkRenderWindow > &win)
Set render window.
vtkSmartPointer< vtkPNGWriter > snapshot_writer_
A PNG writer for screenshot captures.
Camera camera_
A pcl::visualization::Camera for camera parameter saving/restoring.
vtkSmartPointer< vtkRenderWindow > win_
The render window.
bool getCameraParameters(const std::vector< std::string > &camera)
Get camera parameters from a string vector.
boost::signals2::connection registerPointPickingCallback(std::function< void(const pcl::visualization::PointPickingEvent &)> cb)
Register a callback function for point picking events.
void zoomOut()
Interactor style internal method.
boost::signals2::signal< void(const pcl::visualization::AreaPickingEvent &)> area_picking_signal_
std::string getCameraFile() const
Get camera file for camera parameter saving/restoring.
void setKeyboardModifier(const InteractorKeyboardModifier &modifier)
Change the default keyboard modified from ALT to a different special key.
boost::signals2::signal< void(const pcl::visualization::MouseEvent &)> mouse_signal_
void setCameraParameters(const Camera &camera, int viewport=0)
Set the camera parameters by given a full camera data structure.
vtkSmartPointer< vtkLegendScaleActor > grid_actor_
Actor for 2D grid on screen.
vtkSmartPointer< vtkRendererCollection > rens_
Collection of vtkRenderers stored internally.
boost::signals2::connection registerKeyboardCallback(std::function< void(const pcl::visualization::KeyboardEvent &)> cb)
Register a callback std::function for keyboard events.
void OnTimer() override
Interactor style internal method.
boost::signals2::connection registerAreaPickingCallback(std::function< void(const pcl::visualization::AreaPickingEvent &)> cb)
Register a callback function for area picking events.
boost::signals2::signal< void(const pcl::visualization::KeyboardEvent &)> keyboard_signal_
void setCameraFile(const std::string &file)
Set camera file for camera parameter saving/restoring.
void OnChar() override
Interactor style internal method.
PCLVisualizerInteractorStyle()=default
Empty constructor.
vtkSmartPointer< PointPickingCallback > mouse_callback_
A VTK Mouse Callback object, used for point picking.
boost::signals2::signal< void(const pcl::visualization::PointPickingEvent &)> point_picking_signal_
bool saveCameraParameters(const std::string &file)
Save the camera parameters to disk, as a .cam file.
void zoomIn()
Interactor style internal method.
static PCLVisualizerInteractorStyle * New()
vtkSmartPointer< vtkScalarBarActor > lut_actor_
Actor for 2D lookup table on screen.
std::string camera_file_
Camera file for camera parameter saving/restoring.
/brief Class representing 3D point picking events.
InteractorKeyboardModifier
A list of potential keyboard modifiers for pcl::visualization::PCLVisualizerInteractorStyle::PCLVisua...
shared_ptr< CloudActorMap > CloudActorMapPtr
Definition: actor_map.h:97
std::map< std::string, RenWinInteract > RenWinInteractMap
shared_ptr< ShapeActorMap > ShapeActorMapPtr
Definition: actor_map.h:100
#define PCL_EXPORTS
Definition: pcl_macros.h:323
#define PCL_DEPRECATED(Major, Minor, Message)
macro for compatibility across compilers and help remove old deprecated items for the Major....
Definition: pcl_macros.h:156