Point Cloud Library (PCL)  1.7.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 #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 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  * - ALT + s, S : turn stereo mode on/off
94  * - ALT + f, F : switch between maximized window mode and original size
95  * - l, L : list all available geometric and color handlers for the current actor map
96  * - ALT + 0..9 [+ CTRL] : switch between different geometric handlers (where available)
97  * - 0..9 [+ CTRL] : switch between different color handlers (where available)
98  * -
99  * - SHIFT + left click : select a point
100  * - x, X : toggle rubber band selection mode for left mouse button
101  *
102  * \author Radu B. Rusu
103  * \ingroup visualization
104  */
105  class PCL_EXPORTS PCLVisualizerInteractorStyle : public vtkInteractorStyleRubberBandPick
106  {
107  typedef boost::shared_ptr<CloudActorMap> CloudActorMapPtr;
108 
109  public:
110  static PCLVisualizerInteractorStyle *New ();
111 
112  /** \brief Empty constructor. */
114  init_ (), rens_ (), actors_ (), win_height_ (), win_width_ (), win_pos_x_ (), win_pos_y_ (),
115  max_win_height_ (), max_win_width_ (), grid_enabled_ (), grid_actor_ (), lut_enabled_ (),
116  lut_actor_ (), snapshot_writer_ (), wif_ (), mouse_signal_ (), keyboard_signal_ (),
117  point_picking_signal_ (), area_picking_signal_ (), stereo_anaglyph_mask_default_ (),
118  mouse_callback_ (), modifier_ ()
119  {}
120 
121  /** \brief Empty destructor */
123 
124  // this macro defines Superclass, the isA functionality and the safe downcast method
125  vtkTypeMacro (PCLVisualizerInteractorStyle, vtkInteractorStyleRubberBandPick);
126 
127  /** \brief Initialization routine. Must be called before anything else. */
128  virtual void
129  Initialize ();
130 
131  /** \brief Pass a pointer to the actor map
132  * \param[in] actors the actor map that will be used with this style
133  */
134  inline void
135  setCloudActorMap (const CloudActorMapPtr &actors) { actors_ = actors; }
136 
137  /** \brief Get the cloud actor map pointer. */
138  inline CloudActorMapPtr
139  getCloudActorMap () { return (actors_); }
140 
141  /** \brief Pass a set of renderers to the interactor style.
142  * \param[in] rens the vtkRendererCollection to use
143  */
144  void
146 
147  /** \brief Pass a pointer to the actor map
148  * \param[in] actors the actor map that will be used with this style
149  */
150  inline void
151  setUseVbos (const bool use_vbos) { use_vbos_ = use_vbos; }
152 
153  /** \brief Register a callback function for mouse events
154  * \param[in] cb a boost function that will be registered as a callback for a mouse event
155  * \return a connection object that allows to disconnect the callback function.
156  */
157  boost::signals2::connection
158  registerMouseCallback (boost::function<void (const pcl::visualization::MouseEvent&)> cb);
159 
160  /** \brief Register a callback boost::function for keyboard events
161  * \param[in] cb a boost function that will be registered as a callback for a keyboard event
162  * \return a connection object that allows to disconnect the callback function.
163  */
164  boost::signals2::connection
165  registerKeyboardCallback (boost::function<void (const pcl::visualization::KeyboardEvent&)> cb);
166 
167  /** \brief Register a callback function for point picking events
168  * \param[in] cb a boost function that will be registered as a callback for a point picking event
169  * \return a connection object that allows to disconnect the callback function.
170  */
171  boost::signals2::connection
172  registerPointPickingCallback (boost::function<void (const pcl::visualization::PointPickingEvent&)> cb);
173 
174  /** \brief Register a callback function for area picking events
175  * \param[in] cb a boost function that will be registered as a callback for a area picking event
176  * \return a connection object that allows to disconnect the callback function.
177  */
178  boost::signals2::connection
179  registerAreaPickingCallback (boost::function<void (const pcl::visualization::AreaPickingEvent&)> cb);
180 
181  /** \brief Save the current rendered image to disk, as a PNG screenshot.
182  * \param[in] file the name of the PNG file
183  */
184  void
185  saveScreenshot (const std::string &file);
186 
187  /** \brief Change the default keyboard modified from ALT to a different special key.
188  * Allowed values are:
189  * - INTERACTOR_KB_MOD_ALT
190  * - INTERACTOR_KB_MOD_CTRL
191  * - INTERACTOR_KB_MOD_SHIFT
192  * \param[in] modifier the new keyboard modifier
193  */
194  inline void
196  {
197  modifier_ = modifier;
198  }
199 
200  protected:
201  /** \brief Set to true after initialization is complete. */
202  bool init_;
203 
204  /** \brief Collection of vtkRenderers stored internally. */
206 
207  /** \brief Actor map stored internally. */
208  CloudActorMapPtr actors_;
209 
210  /** \brief The current window width/height. */
211  int win_height_, win_width_;
212 
213  /** \brief The current window position x/y. */
214  int win_pos_x_, win_pos_y_;
215 
216  /** \brief The maximum resizeable window width/height. */
217  int max_win_height_, max_win_width_;
218 
219  /** \brief The maximum resizeable window width/height. */
220  bool use_vbos_;
221 
222  /** \brief Set to true if the grid actor is enabled. */
224  /** \brief Actor for 2D grid on screen. */
226 
227  /** \brief Set to true if the LUT actor is enabled. */
229  /** \brief Actor for 2D lookup table on screen. */
231 
232  /** \brief A PNG writer for screenshot captures. */
234  /** \brief Internal window to image filter. Needed by \a snapshot_writer_. */
236  /** \brief Stores the point picker when switching to an area picker. */
238 
239  boost::signals2::signal<void (const pcl::visualization::MouseEvent&)> mouse_signal_;
240  boost::signals2::signal<void (const pcl::visualization::KeyboardEvent&)> keyboard_signal_;
241  boost::signals2::signal<void (const pcl::visualization::PointPickingEvent&)> point_picking_signal_;
242  boost::signals2::signal<void (const pcl::visualization::AreaPickingEvent&)> area_picking_signal_;
243 
244  /** \brief Interactor style internal method. Gets called whenever a key is pressed. */
245  virtual void
246  OnChar ();
247 
248  // Keyboard events
249  virtual void
250  OnKeyDown ();
251  virtual void
252  OnKeyUp ();
253 
254  // mouse button events
255  virtual void
256  OnMouseMove ();
257  virtual void
258  OnLeftButtonDown ();
259  virtual void
260  OnLeftButtonUp ();
261  virtual void
262  OnMiddleButtonDown ();
263  virtual void
264  OnMiddleButtonUp ();
265  virtual void
266  OnRightButtonDown ();
267  virtual void
268  OnRightButtonUp ();
269  virtual void
270  OnMouseWheelForward ();
271  virtual void
272  OnMouseWheelBackward ();
273 
274  // mouse move event
275  /** \brief Interactor style internal method. Gets called periodically if a timer is set. */
276  virtual void
277  OnTimer ();
278 
279  /** \brief Interactor style internal method. Zoom in. */
280  void
281  zoomIn ();
282 
283  /** \brief Interactor style internal method. Zoom out. */
284  void
285  zoomOut ();
286 
287  /** \brief True if we're using red-blue colors for anaglyphic stereo, false if magenta-green. */
289 
290  /** \brief A VTK Mouse Callback object, used for point picking. */
292 
293  /** \brief The keyboard modifier to use. Default: Alt. */
295 
296  friend class PointPickingCallback;
297  };
298 
299  /** \brief PCL histogram visualizer interactory style class.
300  * \author Radu B. Rusu
301  */
302  class PCLHistogramVisualizerInteractorStyle : public vtkInteractorStyleTrackballCamera
303  {
304  public:
306 
307  /** \brief Empty constructor. */
308  PCLHistogramVisualizerInteractorStyle () : wins_ (), init_ (false) {}
309 
310  /** \brief Initialization routine. Must be called before anything else. */
311  void
312  Initialize ();
313 
314  /** \brief Pass a map of render/window/interactors to the interactor style.
315  * \param[in] wins the RenWinInteract map to use
316  */
317  void
318  setRenWinInteractMap (const RenWinInteractMap &wins) { wins_ = wins; }
319 
320  private:
321  /** \brief A map of all windows on screen (with their renderers and interactors). */
322  RenWinInteractMap wins_;
323 
324  /** \brief Set to true after initialization is complete. */
325  bool init_;
326 
327  /** \brief Interactor style internal method. Gets called whenever a key is pressed. */
328  void OnKeyDown ();
329 
330  /** \brief Interactor style internal method. Gets called periodically if a timer is set. */
331  void OnTimer ();
332  };
333  }
334 }
335 
336 #endif