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