Point Cloud Library (PCL)  1.9.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/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  public:
111  using CloudActorMapPtr = boost::shared_ptr<CloudActorMap>;
112 
113  static PCLVisualizerInteractorStyle *New ();
114 
115  /** \brief Empty constructor. */
117  init_ (), win_height_ (), win_width_ (), win_pos_x_ (), win_pos_y_ (),
118  max_win_height_ (), max_win_width_ (), use_vbos_ (false), grid_enabled_ (), lut_enabled_ (),
119  stereo_anaglyph_mask_default_ (),
120  modifier_ (), camera_saved_ (), lut_actor_id_ ("")
121  {}
122 
123  /** \brief Empty destructor */
125 
126  // this macro defines Superclass, the isA functionality and the safe downcast method
127  vtkTypeMacro (PCLVisualizerInteractorStyle, vtkInteractorStyleRubberBandPick);
128 
129  /** \brief Initialization routine. Must be called before anything else. */
130  virtual void
131  Initialize ();
132 
133  /** \brief Pass a pointer to the cloud actor map
134  * \param[in] actors the actor map that will be used with this style
135  */
136  inline void
137  setCloudActorMap (const CloudActorMapPtr &actors) { cloud_actors_ = actors; }
138 
139  /** \brief Pass a pointer to the shape actor map
140  * \param[in] actors the actor map that will be used with this style
141  */
142  inline void
143  setShapeActorMap (const ShapeActorMapPtr &actors) { shape_actors_ = actors; }
144 
145  /** \brief Get the cloud actor map pointer. */
146  inline CloudActorMapPtr
147  getCloudActorMap () { return (cloud_actors_); }
148 
149  /** \brief Get the cloud actor map pointer. */
150  inline ShapeActorMapPtr
151  getShapeActorMap () { return (shape_actors_); }
152 
153  /** \brief Pass a set of renderers to the interactor style.
154  * \param[in] rens the vtkRendererCollection to use
155  */
156  void
158 
159  /** \brief Use Vertex Buffer Objects renderers.
160  * This is an optimization for the obsolete OpenGL backend. Modern OpenGL2 backend (VTK ≥ 6.3) uses vertex
161  * buffer objects by default, transparently for the user.
162  * \param[in] use_vbos set to true to use VBOs
163  */
164  inline void
165  setUseVbos (const bool use_vbos) { use_vbos_ = use_vbos; }
166 
167  /** \brief Register a callback function for mouse events
168  * \param[in] cb a std function that will be registered as a callback for a mouse event
169  * \return a connection object that allows to disconnect the callback function.
170  */
171  boost::signals2::connection
172  registerMouseCallback (std::function<void (const pcl::visualization::MouseEvent&)> cb);
173 
174  /** \brief Register a callback std::function for keyboard events
175  * \param[in] cb a std function that will be registered as a callback for a keyboard event
176  * \return a connection object that allows to disconnect the callback function.
177  */
178  boost::signals2::connection
179  registerKeyboardCallback (std::function<void (const pcl::visualization::KeyboardEvent&)> cb);
180 
181  /** \brief Register a callback function for point picking events
182  * \param[in] cb a std function that will be registered as a callback for a point picking event
183  * \return a connection object that allows to disconnect the callback function.
184  */
185  boost::signals2::connection
186  registerPointPickingCallback (std::function<void (const pcl::visualization::PointPickingEvent&)> cb);
187 
188  /** \brief Register a callback function for area picking events
189  * \param[in] cb a std function that will be registered as a callback for a area picking event
190  * \return a connection object that allows to disconnect the callback function.
191  */
192  boost::signals2::connection
193  registerAreaPickingCallback (std::function<void (const pcl::visualization::AreaPickingEvent&)> cb);
194 
195  /** \brief Save the current rendered image to disk, as a PNG screenshot.
196  * \param[in] file the name of the PNG file
197  */
198  void
199  saveScreenshot (const std::string &file);
200 
201  /** \brief Save the camera parameters to disk, as a .cam file.
202  * \param[in] file the name of the .cam file
203  */
204  bool
205  saveCameraParameters (const std::string &file);
206 
207  /** \brief Get camera parameters of a given viewport (0 means default viewport). */
208  void
209  getCameraParameters (Camera &camera, int viewport = 0) const;
210 
211  /** \brief Load camera parameters from a camera parameter file.
212  * \param[in] file the name of the camera parameter file
213  */
214  bool
215  loadCameraParameters (const std::string &file);
216 
217  /** \brief Set the camera parameters via an intrinsics and and extrinsics matrix
218  * \note This assumes that the pixels are square and that the center of the image is at the center of the sensor.
219  * \param[in] intrinsics the intrinsics that will be used to compute the VTK camera parameters
220  * \param[in] extrinsics the extrinsics that will be used to compute the VTK camera parameters
221  * \param[in] viewport the viewport to modify camera of (0 modifies all cameras)
222  */
223  void
224  setCameraParameters (const Eigen::Matrix3f &intrinsics, const Eigen::Matrix4f &extrinsics, int viewport = 0);
225 
226  /** \brief Set the camera parameters by given a full camera data structure.
227  * \param[in] camera camera structure containing all the camera parameters.
228  * \param[in] viewport the viewport to modify camera of (0 modifies all cameras)
229  */
230  void
231  setCameraParameters (const Camera &camera, int viewport = 0);
232 
233  /** \brief Set camera file for camera parameter saving/restoring.
234  * \param[in] file the name of the camera parameter file
235  */
236  void
237  setCameraFile (const std::string file)
238  {
239  camera_file_ = file;
240  }
241 
242  /** \brief Get camera file for camera parameter saving/restoring. */
243  std::string
244  getCameraFile () const
245  {
246  return (camera_file_);
247  }
248 
249  /** \brief Change the default keyboard modified from ALT to a different special key.
250  * Allowed values are:
251  * - INTERACTOR_KB_MOD_ALT
252  * - INTERACTOR_KB_MOD_CTRL
253  * - INTERACTOR_KB_MOD_SHIFT
254  * \param[in] modifier the new keyboard modifier
255  */
256  inline void
258  {
259  modifier_ = modifier;
260  }
261 
262  protected:
263  /** \brief Set to true after initialization is complete. */
264  bool init_;
265 
266  /** \brief Collection of vtkRenderers stored internally. */
268 
269  /** \brief Cloud actor map stored internally. */
271 
272  /** \brief Shape map stored internally. */
274 
275  /** \brief The current window width/height. */
276  int win_height_, win_width_;
277 
278  /** \brief The current window position x/y. */
279  int win_pos_x_, win_pos_y_;
280 
281  /** \brief The maximum resizeable window width/height. */
282  int max_win_height_, max_win_width_;
283 
284  /** \brief Boolean that holds whether or not to use the vtkVertexBufferObjectMapper*/
285  bool use_vbos_;
286 
287  /** \brief Set to true if the grid actor is enabled. */
289  /** \brief Actor for 2D grid on screen. */
291 
292  /** \brief Set to true if the LUT actor is enabled. */
294  /** \brief Actor for 2D lookup table on screen. */
296 
297  /** \brief A PNG writer for screenshot captures. */
299  /** \brief Internal window to image filter. Needed by \a snapshot_writer_. */
301  /** \brief Stores the point picker when switching to an area picker. */
303 
304  boost::signals2::signal<void (const pcl::visualization::MouseEvent&)> mouse_signal_;
305  boost::signals2::signal<void (const pcl::visualization::KeyboardEvent&)> keyboard_signal_;
306  boost::signals2::signal<void (const pcl::visualization::PointPickingEvent&)> point_picking_signal_;
307  boost::signals2::signal<void (const pcl::visualization::AreaPickingEvent&)> area_picking_signal_;
308 
309  /** \brief Interactor style internal method. Gets called whenever a key is pressed. */
310  void
311  OnChar () override;
312 
313  // Keyboard events
314  void
315  OnKeyDown () override;
316  void
317  OnKeyUp () override;
318 
319  // mouse button events
320  void
321  OnMouseMove () override;
322  void
323  OnLeftButtonDown () override;
324  void
325  OnLeftButtonUp () override;
326  void
327  OnMiddleButtonDown () override;
328  void
329  OnMiddleButtonUp () override;
330  void
331  OnRightButtonDown () override;
332  void
333  OnRightButtonUp () override;
334  void
335  OnMouseWheelForward () override;
336  void
337  OnMouseWheelBackward () override;
338 
339  // mouse move event
340  /** \brief Interactor style internal method. Gets called periodically if a timer is set. */
341  void
342  OnTimer () override;
343 
344  /** \brief Interactor style internal method. Zoom in. */
345  void
346  zoomIn ();
347 
348  /** \brief Interactor style internal method. Zoom out. */
349  void
350  zoomOut ();
351 
352  /** \brief Get camera parameters from a string vector.
353  * \param[in] camera A string vector:
354  * Clipping Range, Focal Point, Position, ViewUp, Distance, Field of View Y, Window Size, Window Pos.
355  * Values in each string are separated by a ','
356  */
357  bool
358  getCameraParameters (const std::vector<std::string> &camera);
359 
360  /** \brief Set render window. */
361  void
363  {
364  win_ = win;
365  }
366 
367  /** \brief True if we're using red-blue colors for anaglyphic stereo, false if magenta-green. */
369 
370  /** \brief A VTK Mouse Callback object, used for point picking. */
372 
373  /** \brief The keyboard modifier to use. Default: Alt. */
375 
376  /** \brief Camera file for camera parameter saving/restoring. */
377  std::string camera_file_;
378  /** \brief A \ref pcl::visualization::Camera for camera parameter saving/restoring. */
380  /** \brief A \ref pcl::visualization::Camera is saved or not. */
382  /** \brief The render window.
383  * Only used when interactor maybe not available
384  */
386 
387  friend class PointPickingCallback;
388  friend class PCLVisualizer;
389 
390  private:
391  /** \brief ID used to fetch/display the look up table on the visualizer
392  * It should be set by PCLVisualizer \ref setLookUpTableID
393  * @note If empty, a random actor added to the interactor will be used */
394  std::string lut_actor_id_;
395 
396  /** \brief Add/remove the look up table displayed when 'u' is pressed, can also be used to update the current LUT displayed
397  * \ref lut_actor_id_ is used (if not empty) to chose which cloud/shape actor LUT will be updated (depending on what is available)
398  * If \ref lut_actor_id_ is empty the first actor with LUT support found will be used. */
399  void
400  updateLookUpTableDisplay (bool add_lut = false);
401  };
402 
403  /** \brief PCL histogram visualizer interactory style class.
404  * \author Radu B. Rusu
405  */
406  class PCLHistogramVisualizerInteractorStyle : public vtkInteractorStyleTrackballCamera
407  {
408  public:
410 
411  /** \brief Empty constructor. */
413 
414  /** \brief Initialization routine. Must be called before anything else. */
415  void
416  Initialize ();
417 
418  /** \brief Pass a map of render/window/interactors to the interactor style.
419  * \param[in] wins the RenWinInteract map to use
420  */
421  void
422  setRenWinInteractMap (const RenWinInteractMap &wins) { wins_ = wins; }
423 
424  private:
425  /** \brief A map of all windows on screen (with their renderers and interactors). */
426  RenWinInteractMap wins_;
427 
428  /** \brief Set to true after initialization is complete. */
429  bool init_;
430 
431  /** \brief Interactor style internal method. Gets called whenever a key is pressed. */
432  void OnKeyDown () override;
433 
434  /** \brief Interactor style internal method. Gets called periodically if a timer is set. */
435  void OnTimer () override;
436  };
437  }
438 }
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.
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
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.
/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::shared_ptr< CloudActorMap > CloudActorMapPtr
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.
boost::shared_ptr< ShapeActorMap > ShapeActorMapPtr
Definition: actor_map.h:104
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.
#define PCL_EXPORTS
Definition: pcl_macros.h:241
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.