Point Cloud Library (PCL)  1.7.0
/tmp/buildd/pcl-1.7-1.7.0/io/include/pcl/io/openni_camera/openni_driver.h
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2011 Willow Garage, Inc.
00005  *
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the copyright holder(s) nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  */
00036 
00037 #include <pcl/pcl_config.h>
00038 #ifdef HAVE_OPENNI
00039 
00040 #ifndef OPENNI_OPENNI_H_
00041 #define OPENNI_OPENNI_H_
00042 #include <string>
00043 #include <vector>
00044 #include <map>
00045 #include "openni.h"
00046 #include "openni_exception.h"
00047 #include "openni_device.h"
00048 #include <pcl/io/boost.h>
00049 #include <pcl/pcl_macros.h>
00050 
00051 namespace openni_wrapper
00052 {
00053   //class OpenNIDevice;
00054 
00055   /**
00056    * @brief Driver class implemented as Singleton. This class contains the xn::Context object used by all devices. It \
00057    * provides methods for enumerating and accessing devices.
00058    * @author Suat Gedikli
00059    * @date 02.january 2011
00060    * @ingroup io
00061    */
00062   class PCL_EXPORTS OpenNIDriver
00063   {
00064   public:
00065     /**
00066      * @author Suat Gedikli
00067      * @brief virtual Destructor that never throws an exception
00068      */
00069     ~OpenNIDriver () throw ();
00070 
00071     /**
00072      * @author Suat Gedikli
00073      * @brief static access method to the only instance.
00074      * @return the only instance of this class.
00075      */
00076     inline static OpenNIDriver& getInstance ();
00077 
00078     /**
00079      * @author Suat Gedikli
00080      * @brief enumerates all devices and updates the list of available devices.
00081      * @return the number of devices found.
00082      */
00083     unsigned updateDeviceList ();
00084 
00085     /**
00086      * @author Suat Gedikli
00087      * @return the number of available devices.
00088      */
00089     inline unsigned getNumberDevices () const throw ();
00090 
00091     /**
00092      * @author Suat Gedikli
00093      * @brief creates a virtual device from an ONI file.
00094      * @param[in] path the path to the ONI file
00095      * @param[in] repeat whether the ONI playback should be repeated in an infinite loop or not.
00096      * @param[in] stream whether the device should be created as a streaming or trigger-based device.
00097      * @return the shared_ptr to the newly created virtual device.
00098      */
00099     boost::shared_ptr<OpenNIDevice> createVirtualDevice (const std::string& path, bool repeat, bool stream) const;
00100 
00101     /**
00102      * @author Suat Gedikli
00103      * @brief returns the device with a given index, where the index is its position in the device list.
00104      * @param[in] index index of the device to be retrieved.
00105      * @return shared_ptr to the device, null if no matching device found.
00106      */
00107     boost::shared_ptr<OpenNIDevice> getDeviceByIndex (unsigned index) const;
00108 
00109     /**
00110      * @author Suat Gedikli
00111      * @brief returns the device with the given serial number.
00112      * @param[in] serial_number the serial number of the device to be retrieved.
00113      * @return shared_ptr to the device, null if no matching device found.
00114      */
00115     boost::shared_ptr<OpenNIDevice> getDeviceBySerialNumber (const std::string& serial_number) const;
00116     
00117 #ifndef _WIN32
00118     /**
00119      * @author Suat Gedikli
00120      * @brief returns the device that is given by the USB bus/address combination.
00121      * @param[in] bus the USB bus id
00122      * @param[in] address the USB address
00123      * @return shared_ptr to the device, null if no matching device found.
00124      */
00125     boost::shared_ptr<OpenNIDevice> getDeviceByAddress (unsigned char bus, unsigned char address) const;
00126 #endif
00127 
00128     /**
00129      * @author Suat Gedikli
00130      * @brief method to retrieve the serial number of a device without creating it.
00131      * @param[in] index the index of the device in the device list.
00132      * @return the serial number of the device.
00133      */
00134     const char* getSerialNumber (unsigned index) const throw ();
00135 
00136     /**
00137      * @author Suat Gedikli
00138      * @brief method to retrieve the connection string of a device without creating it.
00139      * @param[in] index the index of the device in the device list.
00140      * @return the connection string of the device.
00141      */
00142     const char* getConnectionString (unsigned index) const throw ();
00143 
00144     /**
00145      * @author Suat Gedikli
00146      * @brief method to retrieve the vendor name of the USB device without creating it.
00147      * @param[in] index the index of the device in the device list.
00148      * @return the vendor name of the USB device.
00149      */
00150     const char* getVendorName (unsigned index) const throw ();
00151 
00152     /**
00153      * @author Suat Gedikli
00154      * @brief method to retrieve the product name of the USB device without creating it.
00155      * @param[in] index the index of the device in the device list.
00156      * @return the product name of the USB device.
00157      */
00158     const char* getProductName (unsigned index) const throw ();
00159 
00160     /**
00161      * @author Suat Gedikli
00162      * @brief method to retrieve the vendor id of the USB device without creating it.
00163      * @param[in] index the index of the device in the device list.
00164      * @return the vendor id of the USB device.
00165      */
00166     unsigned short getVendorID (unsigned index) const throw ();
00167 
00168     /**
00169      * @author Suat Gedikli
00170      * @brief method to retrieve the product id of the USB device without creating it.
00171      * @param[in] index the index of the device in the device list.
00172      * @return the product id of the USB device.
00173      */
00174     unsigned short getProductID (unsigned index) const throw ();
00175 
00176     /**
00177      * @author Suat Gedikli
00178      * @brief method to retrieve the bus id of the USB device without creating it.
00179      * @param[in] index the index of the device in the device list.
00180      * @return the bus id of the USB device.
00181      */
00182     unsigned char  getBus (unsigned index) const throw ();
00183 
00184     /**
00185      * @author Suat Gedikli
00186      * @brief method to retrieve the vaddress of the USB device without creating it.
00187      * @param[in] index the index of the device in the device list.
00188      * @return the address of the USB device.
00189      */
00190     unsigned char  getAddress (unsigned index) const throw ();
00191 
00192     /**
00193      * @author Suat Gedikli
00194      * @brief stops all streams from all devices.
00195      */
00196     void stopAll ();
00197 
00198     /**
00199      * @author Suat Gedikli
00200      * @brief decomposes the connection string into vendor id and product id.
00201      * @param[in] connection_string the string containing teh connection information
00202      * @param[out] vendorId the vendor id
00203      * @param[out] productId the product id
00204      */
00205     static void
00206     getDeviceType (const std::string& connection_string, unsigned short& vendorId, unsigned short& productId);
00207   protected:
00208 
00209     struct PCL_EXPORTS DeviceContext
00210     {
00211       DeviceContext (const xn::NodeInfo& device_node, xn::NodeInfo* image_node, xn::NodeInfo* depth_node, xn::NodeInfo * ir_node);
00212       DeviceContext (const xn::NodeInfo & device_node);
00213       DeviceContext (const DeviceContext&);
00214       xn::NodeInfo device_node;
00215       boost::shared_ptr<xn::NodeInfo> image_node;
00216       boost::shared_ptr<xn::NodeInfo> depth_node;
00217       boost::shared_ptr<xn::NodeInfo> ir_node;
00218       boost::weak_ptr<OpenNIDevice> device;
00219     } ;
00220 
00221     OpenNIDriver ();
00222     boost::shared_ptr<OpenNIDevice> getDevice (unsigned index) const;
00223 
00224 #ifndef _WIN32
00225     // workaround to get additional device nformation like serial number, vendor and product name, until Primesense fix this
00226     void getDeviceInfos () throw ();
00227 #endif
00228 
00229     mutable std::vector<DeviceContext> device_context_;
00230     mutable xn::Context context_;
00231 
00232     std::map< unsigned char, std::map<unsigned char, unsigned > > bus_map_;
00233     std::map< std::string, unsigned > serial_map_;
00234     std::map< std::string, unsigned > connection_string_map_;
00235   } ;
00236 
00237   OpenNIDriver&
00238   OpenNIDriver::getInstance ()
00239   {
00240     static OpenNIDriver driver;
00241     return driver;
00242   }
00243 
00244   unsigned
00245   OpenNIDriver::getNumberDevices () const throw ()
00246   {
00247     return static_cast<unsigned> (device_context_.size ());
00248   }
00249 } // namespace
00250 #endif
00251 #endif