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