Point Cloud Library (PCL)  1.7.2
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 #include <boost/signals2/signal.hpp>
51 
52 #include <vtkInteractorStyleRubberBandPick.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  typedef boost::shared_ptr<CloudActorMap> CloudActorMapPtr;
110 
111  public:
112  static PCLVisualizerInteractorStyle *New ();
113 
114  /** \brief Empty constructor. */
116  init_ (), rens_ (), actors_ (), win_height_ (), win_width_ (), win_pos_x_ (), win_pos_y_ (),
117  max_win_height_ (), max_win_width_ (), grid_enabled_ (), grid_actor_ (), lut_enabled_ (),
118  lut_actor_ (), snapshot_writer_ (), wif_ (), mouse_signal_ (), keyboard_signal_ (),
119  point_picking_signal_ (), area_picking_signal_ (), stereo_anaglyph_mask_default_ (),
120  mouse_callback_ (), modifier_ (), camera_file_ (), camera_ (), camera_saved_ (), win_ ()
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 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) { actors_ = actors; }
138 
139  /** \brief Get the cloud actor map pointer. */
140  inline CloudActorMapPtr
141  getCloudActorMap () { return (actors_); }
142 
143  /** \brief Pass a set of renderers to the interactor style.
144  * \param[in] rens the vtkRendererCollection to use
145  */
146  void
148 
149  /** \brief Pass a pointer to the actor map
150  * \param[in] use_vbos
151  */
152  inline void
153  setUseVbos (const bool use_vbos) { use_vbos_ = use_vbos; }
154 
155  /** \brief Register a callback function for mouse events
156  * \param[in] cb a boost function that will be registered as a callback for a mouse event
157  * \return a connection object that allows to disconnect the callback function.
158  */
159  boost::signals2::connection
160  registerMouseCallback (boost::function<void (const pcl::visualization::MouseEvent&)> cb);
161 
162  /** \brief Register a callback boost::function for keyboard events
163  * \param[in] cb a boost function that will be registered as a callback for a keyboard event
164  * \return a connection object that allows to disconnect the callback function.
165  */
166  boost::signals2::connection
167  registerKeyboardCallback (boost::function<void (const pcl::visualization::KeyboardEvent&)> cb);
168 
169  /** \brief Register a callback function for point picking events
170  * \param[in] cb a boost function that will be registered as a callback for a point picking event
171  * \return a connection object that allows to disconnect the callback function.
172  */
173  boost::signals2::connection
174  registerPointPickingCallback (boost::function<void (const pcl::visualization::PointPickingEvent&)> cb);
175 
176  /** \brief Register a callback function for area picking events
177  * \param[in] cb a boost function that will be registered as a callback for a area picking event
178  * \return a connection object that allows to disconnect the callback function.
179  */
180  boost::signals2::connection
181  registerAreaPickingCallback (boost::function<void (const pcl::visualization::AreaPickingEvent&)> cb);
182 
183  /** \brief Save the current rendered image to disk, as a PNG screenshot.
184  * \param[in] file the name of the PNG file
185  */
186  void
187  saveScreenshot (const std::string &file);
188 
189  /** \brief Save the camera parameters to disk, as a .cam file.
190  * \param[in] file the name of the .cam file
191  */
192  bool
193  saveCameraParameters (const std::string &file);
194 
195  /** \brief Get camera parameters and save them to a \ref pcl::visualization::Camera.
196  * \param[out] camera the name of the \ref pcl::visualization::Camera
197  */
198  void
199  getCameraParameters (Camera &camera);
200 
201  /** \brief Load camera parameters from a camera parameter file.
202  * \param[in] file the name of the camera parameter file
203  */
204  bool
205  loadCameraParameters (const std::string &file);
206 
207  /** \brief Set the camera parameters via an intrinsics and and extrinsics matrix
208  * \note This assumes that the pixels are square and that the center of the image is at the center of the sensor.
209  * \param[in] intrinsics the intrinsics that will be used to compute the VTK camera parameters
210  * \param[in] extrinsics the extrinsics that will be used to compute the VTK camera parameters
211  * \param[in] viewport the viewport to modify camera of (0 modifies all cameras)
212  */
213  void
214  setCameraParameters (const Eigen::Matrix3f &intrinsics, const Eigen::Matrix4f &extrinsics, int viewport = 0);
215 
216  /** \brief Set the camera parameters by given a full camera data structure.
217  * \param[in] camera camera structure containing all the camera parameters.
218  * \param[in] viewport the viewport to modify camera of (0 modifies all cameras)
219  */
220  void
221  setCameraParameters (const Camera &camera, int viewport = 0);
222 
223  /** \brief Set camera file for camera parameter saving/restoring.
224  * \param[in] file the name of the camera parameter file
225  */
226  void
227  setCameraFile (const std::string file)
228  {
229  camera_file_ = file;
230  }
231 
232  /** \brief Get camera file for camera parameter saving/restoring. */
233  std::string
234  getCameraFile () const
235  {
236  return (camera_file_);
237  }
238 
239  /** \brief Change the default keyboard modified from ALT to a different special key.
240  * Allowed values are:
241  * - INTERACTOR_KB_MOD_ALT
242  * - INTERACTOR_KB_MOD_CTRL
243  * - INTERACTOR_KB_MOD_SHIFT
244  * \param[in] modifier the new keyboard modifier
245  */
246  inline void
248  {
249  modifier_ = modifier;
250  }
251 
252  protected:
253  /** \brief Set to true after initialization is complete. */
254  bool init_;
255 
256  /** \brief Collection of vtkRenderers stored internally. */
258 
259  /** \brief Actor map stored internally. */
260  CloudActorMapPtr actors_;
261 
262  /** \brief The current window width/height. */
263  int win_height_, win_width_;
264 
265  /** \brief The current window position x/y. */
266  int win_pos_x_, win_pos_y_;
267 
268  /** \brief The maximum resizeable window width/height. */
269  int max_win_height_, max_win_width_;
270 
271  /** \brief The maximum resizeable window width/height. */
272  bool use_vbos_;
273 
274  /** \brief Set to true if the grid actor is enabled. */
276  /** \brief Actor for 2D grid on screen. */
278 
279  /** \brief Set to true if the LUT actor is enabled. */
281  /** \brief Actor for 2D lookup table on screen. */
283 
284  /** \brief A PNG writer for screenshot captures. */
286  /** \brief Internal window to image filter. Needed by \a snapshot_writer_. */
288  /** \brief Stores the point picker when switching to an area picker. */
290 
291  boost::signals2::signal<void (const pcl::visualization::MouseEvent&)> mouse_signal_;
292  boost::signals2::signal<void (const pcl::visualization::KeyboardEvent&)> keyboard_signal_;
293  boost::signals2::signal<void (const pcl::visualization::PointPickingEvent&)> point_picking_signal_;
294  boost::signals2::signal<void (const pcl::visualization::AreaPickingEvent&)> area_picking_signal_;
295 
296  /** \brief Interactor style internal method. Gets called whenever a key is pressed. */
297  virtual void
298  OnChar ();
299 
300  // Keyboard events
301  virtual void
302  OnKeyDown ();
303  virtual void
304  OnKeyUp ();
305 
306  // mouse button events
307  virtual void
308  OnMouseMove ();
309  virtual void
310  OnLeftButtonDown ();
311  virtual void
312  OnLeftButtonUp ();
313  virtual void
314  OnMiddleButtonDown ();
315  virtual void
316  OnMiddleButtonUp ();
317  virtual void
318  OnRightButtonDown ();
319  virtual void
320  OnRightButtonUp ();
321  virtual void
322  OnMouseWheelForward ();
323  virtual void
324  OnMouseWheelBackward ();
325 
326  // mouse move event
327  /** \brief Interactor style internal method. Gets called periodically if a timer is set. */
328  virtual void
329  OnTimer ();
330 
331  /** \brief Interactor style internal method. Zoom in. */
332  void
333  zoomIn ();
334 
335  /** \brief Interactor style internal method. Zoom out. */
336  void
337  zoomOut ();
338 
339  /** \brief Get camera parameters from a string vector.
340  * \param[in] camera A string vector:
341  * Clipping Range, Focal Point, Position, ViewUp, Distance, Field of View Y, Window Size, Window Pos.
342  * Values in each string are seperated by a ','
343  */
344  bool
345  getCameraParameters (const std::vector<std::string> &camera);
346 
347  /** \brief Set render window. */
348  void
350  {
351  win_ = win;
352  }
353 
354  /** \brief True if we're using red-blue colors for anaglyphic stereo, false if magenta-green. */
356 
357  /** \brief A VTK Mouse Callback object, used for point picking. */
359 
360  /** \brief The keyboard modifier to use. Default: Alt. */
362 
363  /** \brief Camera file for camera parameter saving/restoring. */
364  std::string camera_file_;
365  /** \brief A \ref pcl::visualization::Camera for camera parameter saving/restoring. */
367  /** \brief A \ref pcl::visualization::Camera is saved or not. */
369  /** \brief The render window.
370  * Only used when interactor maybe not available
371  */
373 
374  friend class PointPickingCallback;
375  friend class PCLVisualizer;
376  };
377 
378  /** \brief PCL histogram visualizer interactory style class.
379  * \author Radu B. Rusu
380  */
381  class PCLHistogramVisualizerInteractorStyle : public vtkInteractorStyleTrackballCamera
382  {
383  public:
385 
386  /** \brief Empty constructor. */
387  PCLHistogramVisualizerInteractorStyle () : wins_ (), init_ (false) {}
388 
389  /** \brief Initialization routine. Must be called before anything else. */
390  void
391  Initialize ();
392 
393  /** \brief Pass a map of render/window/interactors to the interactor style.
394  * \param[in] wins the RenWinInteract map to use
395  */
396  void
397  setRenWinInteractMap (const RenWinInteractMap &wins) { wins_ = wins; }
398 
399  private:
400  /** \brief A map of all windows on screen (with their renderers and interactors). */
401  RenWinInteractMap wins_;
402 
403  /** \brief Set to true after initialization is complete. */
404  bool init_;
405 
406  /** \brief Interactor style internal method. Gets called whenever a key is pressed. */
407  void OnKeyDown ();
408 
409  /** \brief Interactor style internal method. Gets called periodically if a timer is set. */
410  void OnTimer ();
411  };
412  }
413 }
414 
415 #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.
Camera class holds a set of camera parameters together with the window pos/size.
Definition: common.h:122
PCL histogram visualizer interactory style class.
InteractorKeyboardModifier modifier_
The keyboard modifier to use.
vtkSmartPointer< vtkPNGWriter > snapshot_writer_
A PNG writer for screenshot captures.
void setCloudActorMap(const CloudActorMapPtr &actors)
Pass a pointer to the 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.
boost::shared_ptr< CloudActorMap > CloudActorMapPtr
Definition: actor_map.h:100
bool stereo_anaglyph_mask_default_
True if we're using red-blue colors for anaglyphic stereo, false if magenta-green.
boost::signals2::signal< void(const pcl::visualization::KeyboardEvent &)> keyboard_signal_
CloudActorMapPtr actors_
Actor map stored internally.
PCL Visualizer main class.
void setUseVbos(const bool use_vbos)
Pass a pointer to the actor map.
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.
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.
std::string getCameraFile() const
Get camera file for camera parameter saving/restoring.
/brief Class representing key hit/release events
bool use_vbos_
The maximum resizeable window width/height.
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.
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.