Point Cloud Library (PCL)  1.7.0
window.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009-2012, 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  */
38 
39 #ifndef PCL_VISUALIZER_WINDOW_H__
40 #define PCL_VISUALIZER_WINDOW_H__
41 
42 #include <pcl/pcl_macros.h>
43 #include <boost/signals2/signal.hpp>
44 
45 #include <vtkCommand.h>
46 
47 template <typename T> class vtkSmartPointer;
48 class vtkObject;
49 class vtkRenderWindow;
50 class vtkRenderWindowInteractor;
51 class vtkCallbackCommand;
52 class vtkRendererCollection;
53 class PCLVisualizerInteractorStyle;
54 
55 namespace pcl
56 {
57  namespace visualization
58  {
59  class MouseEvent;
60  class KeyboardEvent;
61 
62  class PCL_EXPORTS Window
63  {
64  public:
65  Window (const std::string& window_name = "");
66  Window (const Window &src);
67  Window& operator = (const Window &src);
68 
69  virtual ~Window ();
70 
71  /** \brief Spin method. Calls the interactor and runs an internal loop. */
72  void
73  spin ();
74 
75  /** \brief Spin once method. Calls the interactor and updates the screen once.
76  * \param time - How long (in ms) should the visualization loop be allowed to run.
77  * \param force_redraw - if false it might return without doing anything if the
78  * interactor's framerate does not require a redraw yet.
79  */
80  void
81  spinOnce (int time = 1, bool force_redraw = false);
82 
83  /** \brief Returns true when the user tried to close the window */
84  bool
85  wasStopped () const { return (stopped_); }
86 
87  /**
88  * @brief registering a callback function for keyboard events
89  * @param callback the function that will be registered as a callback for a keyboard event
90  * @param cookie user data that is passed to the callback
91  * @return connection object that allows to disconnect the callback function.
92  */
93  boost::signals2::connection
95  void* cookie = NULL)
96  {
97  return registerKeyboardCallback (boost::bind (callback, _1, cookie));
98  }
99 
100  /**
101  * @brief registering a callback function for keyboard events
102  * @param callback the member function that will be registered as a callback for a keyboard event
103  * @param instance instance to the class that implements the callback function
104  * @param cookie user data that is passed to the callback
105  * @return connection object that allows to disconnect the callback function.
106  */
107  template<typename T> boost::signals2::connection
108  registerKeyboardCallback (void (T::*callback) (const pcl::visualization::KeyboardEvent&, void*),
109  T& instance, void* cookie = NULL)
110  {
111  return registerKeyboardCallback (boost::bind (callback, boost::ref (instance), _1, cookie));
112  }
113 
114  /**
115  * @brief
116  * @param callback the function that will be registered as a callback for a mouse event
117  * @param cookie user data that is passed to the callback
118  * @return connection object that allows to disconnect the callback function.
119  */
120  boost::signals2::connection
121  registerMouseCallback (void (*callback) (const pcl::visualization::MouseEvent&, void*),
122  void* cookie = NULL)
123  {
124  return registerMouseCallback (boost::bind (callback, _1, cookie));
125  }
126 
127  /**
128  * @brief registering a callback function for mouse events
129  * @param callback the member function that will be registered as a callback for a mouse event
130  * @param instance instance to the class that implements the callback function
131  * @param cookie user data that is passed to the callback
132  * @return connection object that allows to disconnect the callback function.
133  */
134  template<typename T> boost::signals2::connection
135  registerMouseCallback (void (T::*callback) (const pcl::visualization::MouseEvent&, void*),
136  T& instance, void* cookie = NULL)
137  {
138  return registerMouseCallback (boost::bind (callback, boost::ref (instance), _1, cookie));
139  }
140 
141  protected: // methods
142 
143  /** \brief Set the stopped flag back to false */
144  void
145  resetStoppedFlag () { stopped_ = false; }
146 
147  /**
148  * @brief registering a callback function for mouse events
149  * @param the boost function that will be registered as a callback for a mouse event
150  * @return connection object that allows to disconnect the callback function.
151  */
152  boost::signals2::connection
153  registerMouseCallback (boost::function<void (const pcl::visualization::MouseEvent&)> );
154 
155  /**
156  * @brief registering a callback boost::function for keyboard events
157  * @param the boost function that will be registered as a callback for a keyboard event
158  * @return connection object that allows to disconnect the callback function.
159  */
160  boost::signals2::connection
161  registerKeyboardCallback (boost::function<void (const pcl::visualization::KeyboardEvent&)> );
162 
163  void
164  emitMouseEvent (unsigned long event_id);
165 
166  void
167  emitKeyboardEvent (unsigned long event_id);
168 
169  // Callbacks used to register for vtk command
170  static void
171  MouseCallback (vtkObject*, unsigned long eid, void* clientdata, void *calldata);
172  static void
173  KeyboardCallback (vtkObject*, unsigned long eid, void* clientdata, void *calldata);
174 
175  protected: // types
176  struct ExitMainLoopTimerCallback : public vtkCommand
177  {
179  {
180  return (new ExitMainLoopTimerCallback);
181  }
182 
185  ExitMainLoopTimerCallback& operator = (const ExitMainLoopTimerCallback& src);
186 
187  virtual void
188  Execute (vtkObject*, unsigned long event_id, void* call_data);
189 
192  };
193 
194  struct ExitCallback : public vtkCommand
195  {
196  static ExitCallback* New ()
197  {
198  return (new ExitCallback);
199  }
200 
201  ExitCallback ();
202  ExitCallback (const ExitCallback &src);
203  ExitCallback& operator = (const ExitCallback &src);
204 
205  virtual void
206  Execute (vtkObject*, unsigned long event_id, void*);
207 
209  };
210 
211  bool stopped_;
213 
214  protected: // member fields
215  boost::signals2::signal<void (const pcl::visualization::MouseEvent&)> mouse_signal_;
216  boost::signals2::signal<void (const pcl::visualization::KeyboardEvent&)> keyboard_signal_;
217 
220  vtkCallbackCommand* mouse_command_;
221  vtkCallbackCommand* keyboard_command_;
222  /** \brief The render window interactor style. */
224  /** \brief The collection of renderers used. */
228  };
229  }
230 }
231 
232 #endif /* __WINDOW_H__ */
233 
vtkSmartPointer< vtkRenderWindowInteractor > interactor_
Definition: window.h:219
static ExitCallback * New()
Definition: window.h:196
vtkCallbackCommand * mouse_command_
Definition: window.h:220
boost::signals2::signal< void(const pcl::visualization::KeyboardEvent &)> keyboard_signal_
Definition: window.h:216
vtkSmartPointer< PCLVisualizerInteractorStyle > style_
The render window interactor style.
Definition: window.h:223
vtkCallbackCommand * keyboard_command_
Definition: window.h:221
bool wasStopped() const
Returns true when the user tried to close the window.
Definition: window.h:85
vtkSmartPointer< vtkRendererCollection > rens_
The collection of renderers used.
Definition: window.h:225
boost::signals2::connection registerKeyboardCallback(void(T::*callback)(const pcl::visualization::KeyboardEvent &, void *), T &instance, void *cookie=NULL)
registering a callback function for keyboard events
Definition: window.h:108
boost::signals2::signal< void(const pcl::visualization::MouseEvent &)> mouse_signal_
Definition: window.h:215
void resetStoppedFlag()
Set the stopped flag back to false.
Definition: window.h:145
vtkSmartPointer< ExitMainLoopTimerCallback > exit_main_loop_timer_callback_
Definition: window.h:226
boost::signals2::connection registerMouseCallback(void(T::*callback)(const pcl::visualization::MouseEvent &, void *), T &instance, void *cookie=NULL)
registering a callback function for mouse events
Definition: window.h:135
boost::signals2::connection registerKeyboardCallback(void(*callback)(const pcl::visualization::KeyboardEvent &, void *), void *cookie=NULL)
registering a callback function for keyboard events
Definition: window.h:94
boost::signals2::connection registerMouseCallback(void(*callback)(const pcl::visualization::MouseEvent &, void *), void *cookie=NULL)
Definition: window.h:121
static ExitMainLoopTimerCallback * New()
Definition: window.h:178
/brief Class representing key hit/release events
vtkSmartPointer< vtkRenderWindow > win_
Definition: window.h:218
vtkSmartPointer< ExitCallback > exit_callback_
Definition: window.h:227