Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
window.h
Go to the documentation of this file.
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  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id: window.h 5368 2012-03-28 04:27:59Z nizar $
37  *
38  */
39 
40 #ifndef PCL_VISUALIZER_WINDOW_H__
41 #define PCL_VISUALIZER_WINDOW_H__
42 
43 #include <pcl/pcl_macros.h>
44 #include <pcl/console/print.h>
45 #include <pcl/visualization/vtk.h>
47 
48 namespace pcl
49 {
50  namespace visualization
51  {
52  class MouseEvent;
53  class KeyboardEvent;
54 
56  {
57  public:
58  Window (const std::string& window_name = "");
59  Window (const Window &src);
60  Window& operator = (const Window &src);
61 
62  virtual ~Window ();
63 
65  void
66  spin ();
67 
73  void
74  spinOnce (int time = 1, bool force_redraw = false);
75 
77  bool
78  wasStopped () const { return (stopped_); }
79 
86  boost::signals2::connection
88  void* cookie = NULL)
89  {
90  return registerKeyboardCallback (boost::bind (callback, _1, cookie));
91  }
92 
100  template<typename T> boost::signals2::connection
101  registerKeyboardCallback (void (T::*callback) (const pcl::visualization::KeyboardEvent&, void*),
102  T& instance, void* cookie = NULL)
103  {
104  return registerKeyboardCallback (boost::bind (callback, boost::ref (instance), _1, cookie));
105  }
106 
113  boost::signals2::connection
114  registerMouseCallback (void (*callback) (const pcl::visualization::MouseEvent&, void*),
115  void* cookie = NULL)
116  {
117  return registerMouseCallback (boost::bind (callback, _1, cookie));
118  }
119 
127  template<typename T> boost::signals2::connection
128  registerMouseCallback (void (T::*callback) (const pcl::visualization::MouseEvent&, void*),
129  T& instance, void* cookie = NULL)
130  {
131  return registerMouseCallback (boost::bind (callback, boost::ref (instance), _1, cookie));
132  }
133 
134  protected: // methods
135 
137  void
138  resetStoppedFlag () { stopped_ = false; }
139 
145  boost::signals2::connection
146  registerMouseCallback (boost::function<void (const pcl::visualization::MouseEvent&)> );
147 
153  boost::signals2::connection
154  registerKeyboardCallback (boost::function<void (const pcl::visualization::KeyboardEvent&)> );
155 
156  void
157  emitMouseEvent (unsigned long event_id);
158 
159  void
160  emitKeyboardEvent (unsigned long event_id);
161 
162  // Callbacks used to register for vtk command
163  static void
164  MouseCallback (vtkObject*, unsigned long eid, void* clientdata, void *calldata);
165  static void
166  KeyboardCallback (vtkObject*, unsigned long eid, void* clientdata, void *calldata);
167 
168  protected: // types
169  struct ExitMainLoopTimerCallback : public vtkCommand
170  {
171  static ExitMainLoopTimerCallback* New()
172  {
173  return (new ExitMainLoopTimerCallback);
174  }
175 
176  ExitMainLoopTimerCallback () : right_timer_id (), window () {}
177  ExitMainLoopTimerCallback (const ExitMainLoopTimerCallback& src) : vtkCommand (), right_timer_id (src.right_timer_id), window (src.window) {}
178  ExitMainLoopTimerCallback& operator = (const ExitMainLoopTimerCallback& src) { right_timer_id = src.right_timer_id; window = src.window; return (*this); }
179 
180  virtual void
181  Execute (vtkObject*, unsigned long event_id, void* call_data)
182  {
183  if (event_id != vtkCommand::TimerEvent)
184  return;
185  int timer_id = *static_cast<int*> (call_data);
186  //PCL_WARN ("[pcl::visualization::Window::ExitMainLoopTimerCallback] Timer %d called.\n", timer_id);
187  if (timer_id != right_timer_id)
188  return;
189  window->interactor_->TerminateApp ();
190 // window->interactor_->stopLoop ();
191  }
192  int right_timer_id;
193  Window* window;
194  };
195 
196  struct ExitCallback : public vtkCommand
197  {
198  static ExitCallback* New ()
199  {
200  return (new ExitCallback);
201  }
202 
203  ExitCallback () : window () {}
204  ExitCallback (const ExitCallback &src) : vtkCommand (), window (src.window) {}
205  ExitCallback& operator = (const ExitCallback &src) { window = src.window; return (*this); }
206 
207  virtual void
208  Execute (vtkObject*, unsigned long event_id, void*)
209  {
210  if (event_id != vtkCommand::ExitEvent)
211  return;
212  window->interactor_->TerminateApp ();
213  window->stopped_ = true;
214  }
215  Window* window;
216  };
217 
218  bool stopped_;
219  int timer_id_;
220 
221  protected: // member fields
222  boost::signals2::signal<void (const pcl::visualization::MouseEvent&)> mouse_signal_;
223  boost::signals2::signal<void (const pcl::visualization::KeyboardEvent&)> keyboard_signal_;
224 
225  vtkSmartPointer<vtkRenderWindow> win_;
226  vtkSmartPointer<vtkRenderWindowInteractor> interactor_;
227  vtkCallbackCommand* mouse_command_;
228  vtkCallbackCommand* keyboard_command_;
230  vtkSmartPointer<PCLVisualizerInteractorStyle> style_;
232  vtkSmartPointer<vtkRendererCollection> rens_;
233  vtkSmartPointer<ExitMainLoopTimerCallback> exit_main_loop_timer_callback_;
234  vtkSmartPointer<ExitCallback> exit_callback_;
235  };
236  }
237 }
238 
239 #endif /* __WINDOW_H__ */
240