Point Cloud Library (PCL)  1.7.1
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 #include <pcl/pcl_config.h>
38 #ifdef HAVE_OPENNI
39 
40 #ifndef OPENNI_OPENNI_H_
41 #define OPENNI_OPENNI_H_
42 #include <string>
43 #include <vector>
44 #include <map>
45 #include "openni.h"
46 #include "openni_exception.h"
47 #include "openni_device.h"
48 #include <pcl/io/boost.h>
49 #include <pcl/pcl_macros.h>
50 
51 namespace openni_wrapper
52 {
53  //class OpenNIDevice;
54 
55  /**
56  * @brief Driver class implemented as Singleton. This class contains the xn::Context object used by all devices. It \
57  * provides methods for enumerating and accessing devices.
58  * @author Suat Gedikli
59  * @date 02.january 2011
60  * @ingroup io
61  */
62  class PCL_EXPORTS OpenNIDriver
63  {
64  public:
65  /**
66  * @author Suat Gedikli
67  * @brief virtual Destructor that never throws an exception
68  */
69  ~OpenNIDriver () throw ();
70 
71  /**
72  * @author Suat Gedikli
73  * @brief static access method to the only instance.
74  * @return the only instance of this class.
75  */
76  inline static OpenNIDriver& getInstance ();
77 
78  /**
79  * @author Suat Gedikli
80  * @brief enumerates all devices and updates the list of available devices.
81  * @return the number of devices found.
82  */
83  unsigned updateDeviceList ();
84 
85  /**
86  * @author Suat Gedikli
87  * @return the number of available devices.
88  */
89  inline unsigned getNumberDevices () const throw ();
90 
91  /**
92  * @author Suat Gedikli
93  * @brief creates a virtual device from an ONI file.
94  * @param[in] path the path to the ONI file
95  * @param[in] repeat whether the ONI playback should be repeated in an infinite loop or not.
96  * @param[in] stream whether the device should be created as a streaming or trigger-based device.
97  * @return the shared_ptr to the newly created virtual device.
98  */
99  boost::shared_ptr<OpenNIDevice> createVirtualDevice (const std::string& path, bool repeat, bool stream) const;
100 
101  /**
102  * @author Suat Gedikli
103  * @brief returns the device with a given index, where the index is its position in the device list.
104  * @param[in] index index of the device to be retrieved.
105  * @return shared_ptr to the device, null if no matching device found.
106  */
107  boost::shared_ptr<OpenNIDevice> getDeviceByIndex (unsigned index) const;
108 
109  /**
110  * @author Suat Gedikli
111  * @brief returns the device with the given serial number.
112  * @param[in] serial_number the serial number of the device to be retrieved.
113  * @return shared_ptr to the device, null if no matching device found.
114  */
115  boost::shared_ptr<OpenNIDevice> getDeviceBySerialNumber (const std::string& serial_number) const;
116 
117 #ifndef _WIN32
118  /**
119  * @author Suat Gedikli
120  * @brief returns the device that is given by the USB bus/address combination.
121  * @param[in] bus the USB bus id
122  * @param[in] address the USB address
123  * @return shared_ptr to the device, null if no matching device found.
124  */
125  boost::shared_ptr<OpenNIDevice> getDeviceByAddress (unsigned char bus, unsigned char address) const;
126 #endif
127 
128  /**
129  * @author Suat Gedikli
130  * @brief method to retrieve the serial number of a device without creating it.
131  * @param[in] index the index of the device in the device list.
132  * @return the serial number of the device.
133  */
134  const char* getSerialNumber (unsigned index) const throw ();
135 
136  /**
137  * @author Suat Gedikli
138  * @brief method to retrieve the connection string of a device without creating it.
139  * @param[in] index the index of the device in the device list.
140  * @return the connection string of the device.
141  */
142  const char* getConnectionString (unsigned index) const throw ();
143 
144  /**
145  * @author Suat Gedikli
146  * @brief method to retrieve the vendor name of the USB device without creating it.
147  * @param[in] index the index of the device in the device list.
148  * @return the vendor name of the USB device.
149  */
150  const char* getVendorName (unsigned index) const throw ();
151 
152  /**
153  * @author Suat Gedikli
154  * @brief method to retrieve the product name of the USB device without creating it.
155  * @param[in] index the index of the device in the device list.
156  * @return the product name of the USB device.
157  */
158  const char* getProductName (unsigned index) const throw ();
159 
160  /**
161  * @author Suat Gedikli
162  * @brief method to retrieve the vendor id of the USB device without creating it.
163  * @param[in] index the index of the device in the device list.
164  * @return the vendor id of the USB device.
165  */
166  unsigned short getVendorID (unsigned index) const throw ();
167 
168  /**
169  * @author Suat Gedikli
170  * @brief method to retrieve the product id of the USB device without creating it.
171  * @param[in] index the index of the device in the device list.
172  * @return the product id of the USB device.
173  */
174  unsigned short getProductID (unsigned index) const throw ();
175 
176  /**
177  * @author Suat Gedikli
178  * @brief method to retrieve the bus id of the USB device without creating it.
179  * @param[in] index the index of the device in the device list.
180  * @return the bus id of the USB device.
181  */
182  unsigned char getBus (unsigned index) const throw ();
183 
184  /**
185  * @author Suat Gedikli
186  * @brief method to retrieve the vaddress of the USB device without creating it.
187  * @param[in] index the index of the device in the device list.
188  * @return the address of the USB device.
189  */
190  unsigned char getAddress (unsigned index) const throw ();
191 
192  /**
193  * @author Suat Gedikli
194  * @brief stops all streams from all devices.
195  */
196  void stopAll ();
197 
198  /**
199  * @author Suat Gedikli
200  * @brief decomposes the connection string into vendor id and product id.
201  * @param[in] connection_string the string containing teh connection information
202  * @param[out] vendorId the vendor id
203  * @param[out] productId the product id
204  */
205  static void
206  getDeviceType (const std::string& connection_string, unsigned short& vendorId, unsigned short& productId);
207  protected:
208 
209  struct PCL_EXPORTS DeviceContext
210  {
211  DeviceContext (const xn::NodeInfo& device_node, xn::NodeInfo* image_node, xn::NodeInfo* depth_node, xn::NodeInfo * ir_node);
212  DeviceContext (const xn::NodeInfo & device_node);
213  DeviceContext (const DeviceContext&);
214  xn::NodeInfo device_node;
215  boost::shared_ptr<xn::NodeInfo> image_node;
216  boost::shared_ptr<xn::NodeInfo> depth_node;
217  boost::shared_ptr<xn::NodeInfo> ir_node;
218  boost::weak_ptr<OpenNIDevice> device;
219  } ;
220 
221  OpenNIDriver ();
222  boost::shared_ptr<OpenNIDevice> getDevice (unsigned index) const;
223 
224 #ifndef _WIN32
225  // workaround to get additional device nformation like serial number, vendor and product name, until Primesense fix this
226  void getDeviceInfos () throw ();
227 #endif
228 
229  mutable std::vector<DeviceContext> device_context_;
230  mutable xn::Context context_;
231 
232  std::map< unsigned char, std::map<unsigned char, unsigned > > bus_map_;
233  std::map< std::string, unsigned > serial_map_;
234  std::map< std::string, unsigned > connection_string_map_;
235  } ;
236 
237  OpenNIDriver&
239  {
240  static OpenNIDriver driver;
241  return driver;
242  }
243 
244  unsigned
246  {
247  return static_cast<unsigned> (device_context_.size ());
248  }
249 } // namespace
250 #endif
251 #endif