Point Cloud Library (PCL)  1.10.1-dev
openni_driver.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011 Willow Garage, Inc.
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the copyright holder(s) nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  */
36 
37 #pragma once
38 
39 #include <pcl/pcl_config.h>
40 #ifdef HAVE_OPENNI
41 
42 #include "openni.h"
43 #include "openni_exception.h"
44 #include "openni_device.h"
45 #include <pcl/io/boost.h>
46 #include <pcl/pcl_macros.h>
47 
48 #include <map>
49 #include <memory>
50 #include <string>
51 #include <vector>
52 
53 namespace openni_wrapper
54 {
55  //class OpenNIDevice;
56 
57  /**
58  * @brief Driver class implemented as Singleton. This class contains the xn::Context object used by all devices. It \
59  * provides methods for enumerating and accessing devices.
60  * @author Suat Gedikli
61  * @date 02.january 2011
62  * @ingroup io
63  */
65  {
66  public:
67  /**
68  * @author Suat Gedikli
69  * @brief virtual Destructor that never throws an exception
70  */
71  ~OpenNIDriver () noexcept;
72 
73  /**
74  * @author Suat Gedikli
75  * @brief static access method to the only instance.
76  * @return the only instance of this class.
77  */
78  inline static OpenNIDriver& getInstance ();
79 
80  /**
81  * @author Suat Gedikli
82  * @brief enumerates all devices and updates the list of available devices.
83  * @return the number of devices found.
84  */
85  unsigned updateDeviceList ();
86 
87  /**
88  * @author Suat Gedikli
89  * @return the number of available devices.
90  */
91  inline unsigned getNumberDevices () const throw ();
92 
93  /**
94  * @author Suat Gedikli
95  * @brief creates a virtual device from an ONI file.
96  * @param[in] path the path to the ONI file
97  * @param[in] repeat whether the ONI playback should be repeated in an infinite loop or not.
98  * @param[in] stream whether the device should be created as a streaming or trigger-based device.
99  * @return the shared_ptr to the newly created virtual device.
100  */
101  OpenNIDevice::Ptr createVirtualDevice (const std::string& path, bool repeat, bool stream) const;
102 
103  /**
104  * @author Suat Gedikli
105  * @brief returns the device with a given index, where the index is its position in the device list.
106  * @param[in] index index of the device to be retrieved.
107  * @return shared_ptr to the device, null if no matching device found.
108  */
109  OpenNIDevice::Ptr getDeviceByIndex (unsigned index) const;
110 
111  /**
112  * @author Suat Gedikli
113  * @brief returns the device with the given serial number.
114  * @param[in] serial_number the serial number of the device to be retrieved.
115  * @return shared_ptr to the device, null if no matching device found.
116  */
117  OpenNIDevice::Ptr getDeviceBySerialNumber (const std::string& serial_number) const;
118 
119 #ifndef _WIN32
120  /**
121  * @author Suat Gedikli
122  * @brief returns the device that is given by the USB bus/address combination.
123  * @param[in] bus the USB bus id
124  * @param[in] address the USB address
125  * @return shared_ptr to the device, null if no matching device found.
126  */
127  OpenNIDevice::Ptr getDeviceByAddress (unsigned char bus, unsigned char address) const;
128 #endif
129 
130  /**
131  * @author Suat Gedikli
132  * @brief method to retrieve the serial number of a device without creating it.
133  * @param[in] index the index of the device in the device list.
134  * @return the serial number of the device.
135  */
136  const char* getSerialNumber (unsigned index) const throw ();
137 
138  /**
139  * @author Suat Gedikli
140  * @brief method to retrieve the connection string of a device without creating it.
141  * @param[in] index the index of the device in the device list.
142  * @return the connection string of the device.
143  */
144  const char* getConnectionString (unsigned index) const throw ();
145 
146  /**
147  * @author Suat Gedikli
148  * @brief method to retrieve the vendor name of the USB device without creating it.
149  * @param[in] index the index of the device in the device list.
150  * @return the vendor name of the USB device.
151  */
152  const char* getVendorName (unsigned index) const throw ();
153 
154  /**
155  * @author Suat Gedikli
156  * @brief method to retrieve the product name of the USB device without creating it.
157  * @param[in] index the index of the device in the device list.
158  * @return the product name of the USB device.
159  */
160  const char* getProductName (unsigned index) const throw ();
161 
162  /**
163  * @author Suat Gedikli
164  * @brief method to retrieve the vendor id of the USB device without creating it.
165  * @param[in] index the index of the device in the device list.
166  * @return the vendor id of the USB device.
167  */
168  unsigned short getVendorID (unsigned index) const throw ();
169 
170  /**
171  * @author Suat Gedikli
172  * @brief method to retrieve the product id of the USB device without creating it.
173  * @param[in] index the index of the device in the device list.
174  * @return the product id of the USB device.
175  */
176  unsigned short getProductID (unsigned index) const throw ();
177 
178  /**
179  * @author Suat Gedikli
180  * @brief method to retrieve the bus id of the USB device without creating it.
181  * @param[in] index the index of the device in the device list.
182  * @return the bus id of the USB device.
183  */
184  unsigned char getBus (unsigned index) const throw ();
185 
186  /**
187  * @author Suat Gedikli
188  * @brief method to retrieve the vaddress of the USB device without creating it.
189  * @param[in] index the index of the device in the device list.
190  * @return the address of the USB device.
191  */
192  unsigned char getAddress (unsigned index) const throw ();
193 
194  /**
195  * @author Suat Gedikli
196  * @brief stops all streams from all devices.
197  */
198  void stopAll ();
199 
200  /**
201  * @author Suat Gedikli
202  * @brief decomposes the connection string into vendor id and product id.
203  * @param[in] connection_string the string containing the connection information
204  * @param[out] vendorId the vendor id
205  * @param[out] productId the product id
206  */
207  static void
208  getDeviceType (const std::string& connection_string, unsigned short& vendorId, unsigned short& productId);
209  protected:
210 
212  {
213  DeviceContext (const xn::NodeInfo& device_node, xn::NodeInfo* image_node, xn::NodeInfo* depth_node, xn::NodeInfo * ir_node);
214  DeviceContext (const xn::NodeInfo & device_node);
215  DeviceContext (const DeviceContext&);
216  xn::NodeInfo device_node;
217  std::shared_ptr<xn::NodeInfo> image_node;
218  std::shared_ptr<xn::NodeInfo> depth_node;
219  std::shared_ptr<xn::NodeInfo> ir_node;
220  boost::weak_ptr<OpenNIDevice> device;
221  } ;
222 
223  OpenNIDriver ();
224  OpenNIDevice::Ptr getDevice (unsigned index) const;
225 
226 #ifndef _WIN32
227  // workaround to get additional device nformation like serial number, vendor and product name, until Primesense fix this
228  void getDeviceInfos () noexcept;
229 #endif
230 
231  mutable std::vector<DeviceContext> device_context_;
232  mutable xn::Context context_;
233 
234  std::map< unsigned char, std::map<unsigned char, unsigned > > bus_map_;
235  std::map< std::string, unsigned > serial_map_;
236  std::map< std::string, unsigned > connection_string_map_;
237  } ;
238 
239  OpenNIDriver&
241  {
242  static OpenNIDriver driver;
243  return driver;
244  }
245 
246  unsigned
248  {
249  return static_cast<unsigned> (device_context_.size ());
250  }
251 } // namespace
252 #endif
std::shared_ptr< xn::NodeInfo > depth_node
boost::weak_ptr< OpenNIDevice > device
std::map< std::string, unsigned > connection_string_map_
pcl::shared_ptr< OpenNIDevice > Ptr
Definition: openni_device.h:82
std::map< std::string, unsigned > serial_map_
static OpenNIDriver & getInstance()
static access method to the only instance.
std::shared_ptr< xn::NodeInfo > image_node
std::shared_ptr< xn::NodeInfo > ir_node
unsigned getNumberDevices() const
Driver class implemented as Singleton.
Definition: openni_driver.h:64
std::vector< DeviceContext > device_context_
std::map< unsigned char, std::map< unsigned char, unsigned > > bus_map_
#define PCL_EXPORTS
Definition: pcl_macros.h:276
Defines all the PCL and non-PCL macros used.