Point Cloud Library (PCL)  1.14.1-dev
cloud_viewer.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  */
36 
37 #pragma once
38 
39 #include <pcl/visualization/pcl_visualizer.h> //pcl vis
40 
41 #include <string>
42 #include <memory>
43 
44 namespace pcl
45 {
46  namespace visualization
47  {
48  /** \brief Simple point cloud visualization class
49  * \author Ethan Rublee
50  * \ingroup visualization
51  */
52  class PCL_EXPORTS CloudViewer : boost::noncopyable
53  {
54  public:
55  using Ptr = shared_ptr<CloudViewer>;
56  using ConstPtr = shared_ptr<const CloudViewer>;
57 
62 
63  /** \brief Construct a cloud viewer, with a window name.
64  * \param window_name This is displayed at the top of the window
65  */
66  CloudViewer (const std::string& window_name);
67 
68  /** \brief Will quit the window,
69  * and release all resources held by the viewer.
70  * @return
71  */
73 
74  /** \brief Show a cloud, with an optional key for multiple clouds.
75  * \param[in] cloud RGB point cloud
76  * \param[in] cloudname a key for the point cloud, use the same name if you would like to overwrite the existing cloud.
77  */
78  void
79  showCloud (const ColorCloud::ConstPtr &cloud, const std::string& cloudname = "cloud");
80 
81  /** \brief Show a cloud, with an optional key for multiple clouds.
82  * \param[in] cloud RGB point cloud
83  * \param[in] cloudname a key for the point cloud, use the same name if you would like to overwrite the existing cloud.
84  */
85  void
86  showCloud (const ColorACloud::ConstPtr &cloud, const std::string& cloudname = "cloud");
87 
88  /** \brief Show a cloud, with an optional key for multiple clouds.
89  * \param[in] cloud XYZI point cloud
90  * \param[in] cloudname a key for the point cloud, use the same name if you would like to overwrite the existing cloud.
91  */
92  void
93  showCloud (const GrayCloud::ConstPtr &cloud, const std::string& cloudname = "cloud");
94 
95 
96  /** \brief Show a cloud, with an optional key for multiple clouds.
97  * \param[in] cloud XYZ point cloud
98  * \param[in] cloudname a key for the point cloud, use the same name if you would like to overwrite the existing cloud.
99  */
100  void
101  showCloud (const MonochromeCloud::ConstPtr &cloud, const std::string& cloudname = "cloud");
102 
103  /** \brief Check if the gui was quit, you should quit also
104  * \param millis_to_wait This will request to "spin" for the number of milliseconds, before exiting.
105  * \return true if the user signaled the gui to stop
106  */
107  bool
108  wasStopped (int millis_to_wait = 1);
109 
110  /** Visualization callable function, may be used for running things on the UI thread.
111  */
112  using VizCallable = std::function<void (pcl::visualization::PCLVisualizer&)>;
113 
114  /** \brief Run a callbable object on the UI thread. Will persist until removed
115  * @param x Use boost::ref(x) for a function object that you would like to not copy
116  * \param key The key for the callable -- use the same key to overwrite.
117  */
118  void
119  runOnVisualizationThread (VizCallable x, const std::string& key = "callable");
120 
121  /** \brief Run a callbable object on the UI thread. This will run once and be removed
122  * @param x Use boost::ref(x) for a function object that you would like to not copy
123  */
124  void
126 
127  /** \brief Remove a previously added callable object, NOP if it doesn't exist.
128  * @param key the key that was registered with the callable object.
129  */
130  void
131  removeVisualizationCallable (const std::string& key = "callable");
132 
133  /** \brief Register a callback function for keyboard events
134  * \param[in] callback the function that will be registered as a callback for a keyboard event
135  * \param[in] cookie user data that is passed to the callback
136  * \return connection object that allows to disconnect the callback function.
137  */
138  inline boost::signals2::connection
139  registerKeyboardCallback (void (*callback) (const pcl::visualization::KeyboardEvent&, void*), void* cookie = nullptr)
140  {
141  return (registerKeyboardCallback ([=] (const pcl::visualization::KeyboardEvent& e) { (*callback) (e, cookie); }));
142  }
143 
144  /** \brief Register a callback function for keyboard events
145  * \param[in] callback the member function that will be registered as a callback for a keyboard event
146  * \param[in] instance instance to the class that implements the callback function
147  * \param[in] cookie user data that is passed to the callback
148  * \return connection object that allows to disconnect the callback function.
149  */
150  template<typename T> inline boost::signals2::connection
151  registerKeyboardCallback (void (T::*callback) (const pcl::visualization::KeyboardEvent&, void*), T& instance, void* cookie = nullptr)
152  {
153  return (registerKeyboardCallback ([=, &instance] (const pcl::visualization::KeyboardEvent& e) { (instance.*callback) (e, cookie); }));
154  }
155 
156  /** \brief Register a callback function for mouse events
157  * \param[in] callback the function that will be registered as a callback for a mouse event
158  * \param[in] cookie user data that is passed to the callback
159  * \return connection object that allows to disconnect the callback function.
160  */
161  inline boost::signals2::connection
162  registerMouseCallback (void (*callback) (const pcl::visualization::MouseEvent&, void*), void* cookie = nullptr)
163  {
164  return (registerMouseCallback ([=] (const pcl::visualization::MouseEvent& e) { (*callback) (e, cookie); }));
165  }
166 
167  /** \brief Register a callback function for mouse events
168  * \param[in] callback the member function that will be registered as a callback for a mouse event
169  * \param[in] instance instance to the class that implements the callback function
170  * \param[in] cookie user data that is passed to the callback
171  * \return connection object that allows to disconnect the callback function.
172  */
173  template<typename T> inline boost::signals2::connection
174  registerMouseCallback (void (T::*callback) (const pcl::visualization::MouseEvent&, void*), T& instance, void* cookie = nullptr)
175  {
176  return (registerMouseCallback ([=, &instance] (const pcl::visualization::MouseEvent& e) { (instance.*callback) (e, cookie); }));
177  }
178 
179 
180  /** \brief Register a callback function for point picking events
181  * \param[in] callback the function that will be registered as a callback for a point picking event
182  * \param[in] cookie user data that is passed to the callback
183  * \return connection object that allows to disconnect the callback function.
184  */
185  inline boost::signals2::connection
186  registerPointPickingCallback (void (*callback) (const pcl::visualization::PointPickingEvent&, void*), void* cookie = nullptr)
187  {
188  return (registerPointPickingCallback ([=] (const pcl::visualization::PointPickingEvent& e) { (*callback) (e, cookie); }));
189  }
190 
191  /** \brief Register a callback function for point picking events
192  * \param[in] callback the member function that will be registered as a callback for a point picking event
193  * \param[in] instance instance to the class that implements the callback function
194  * \param[in] cookie user data that is passed to the callback
195  * \return connection object that allows to disconnect the callback function.
196  */
197  template<typename T> inline boost::signals2::connection
198  registerPointPickingCallback (void (T::*callback) (const pcl::visualization::PointPickingEvent&, void*), T& instance, void* cookie = nullptr)
199  {
200  return (registerPointPickingCallback ([=, &instance] (const pcl::visualization::PointPickingEvent& e) { (instance.*callback) (e, cookie); }));
201  }
202 
203  private:
204  /** \brief Private implementation. */
205  struct CloudViewer_impl;
206  std::unique_ptr<CloudViewer_impl> impl_;
207 
208  boost::signals2::connection
209  registerMouseCallback (std::function<void (const pcl::visualization::MouseEvent&)>);
210 
211  boost::signals2::connection
212  registerKeyboardCallback (std::function<void (const pcl::visualization::KeyboardEvent&)>);
213 
214  boost::signals2::connection
215  registerPointPickingCallback (std::function<void (const pcl::visualization::PointPickingEvent&)>);
216  };
217  }
218 }
shared_ptr< const PointCloud< pcl::PointXYZRGB > > ConstPtr
Definition: point_cloud.h:414
Simple point cloud visualization class.
Definition: cloud_viewer.h:53
shared_ptr< const CloudViewer > ConstPtr
Definition: cloud_viewer.h:56
~CloudViewer()
Will quit the window, and release all resources held by the viewer.
void showCloud(const ColorACloud::ConstPtr &cloud, const std::string &cloudname="cloud")
Show a cloud, with an optional key for multiple clouds.
void showCloud(const GrayCloud::ConstPtr &cloud, const std::string &cloudname="cloud")
Show a cloud, with an optional key for multiple clouds.
boost::signals2::connection registerKeyboardCallback(void(T::*callback)(const pcl::visualization::KeyboardEvent &, void *), T &instance, void *cookie=nullptr)
Register a callback function for keyboard events.
Definition: cloud_viewer.h:151
void runOnVisualizationThread(VizCallable x, const std::string &key="callable")
Run a callbable object on the UI thread.
void showCloud(const MonochromeCloud::ConstPtr &cloud, const std::string &cloudname="cloud")
Show a cloud, with an optional key for multiple clouds.
boost::signals2::connection registerPointPickingCallback(void(*callback)(const pcl::visualization::PointPickingEvent &, void *), void *cookie=nullptr)
Register a callback function for point picking events.
Definition: cloud_viewer.h:186
CloudViewer(const std::string &window_name)
Construct a cloud viewer, with a window name.
void removeVisualizationCallable(const std::string &key="callable")
Remove a previously added callable object, NOP if it doesn't exist.
bool wasStopped(int millis_to_wait=1)
Check if the gui was quit, you should quit also.
boost::signals2::connection registerPointPickingCallback(void(T::*callback)(const pcl::visualization::PointPickingEvent &, void *), T &instance, void *cookie=nullptr)
Register a callback function for point picking events.
Definition: cloud_viewer.h:198
void showCloud(const ColorCloud::ConstPtr &cloud, const std::string &cloudname="cloud")
Show a cloud, with an optional key for multiple clouds.
boost::signals2::connection registerKeyboardCallback(void(*callback)(const pcl::visualization::KeyboardEvent &, void *), void *cookie=nullptr)
Register a callback function for keyboard events.
Definition: cloud_viewer.h:139
boost::signals2::connection registerMouseCallback(void(*callback)(const pcl::visualization::MouseEvent &, void *), void *cookie=nullptr)
Register a callback function for mouse events.
Definition: cloud_viewer.h:162
shared_ptr< CloudViewer > Ptr
Definition: cloud_viewer.h:55
boost::signals2::connection registerMouseCallback(void(T::*callback)(const pcl::visualization::MouseEvent &, void *), T &instance, void *cookie=nullptr)
Register a callback function for mouse events.
Definition: cloud_viewer.h:174
void runOnVisualizationThreadOnce(VizCallable x)
Run a callbable object on the UI thread.
std::function< void(pcl::visualization::PCLVisualizer &)> VizCallable
Visualization callable function, may be used for running things on the UI thread.
Definition: cloud_viewer.h:112
/brief Class representing key hit/release events
PCL Visualizer main class.
/brief Class representing 3D point picking events.
#define PCL_EXPORTS
Definition: pcl_macros.h:323