Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <openni_camera/openni_device_oni.h>
00038 #include <openni_camera/openni_image_rgb24.h>
00039
00040 using namespace std;
00041 using namespace boost;
00042
00043 namespace openni_wrapper
00044 {
00045
00046 DeviceONI::DeviceONI(xn::Context& context, const std::string& file_name, bool repeat, bool streaming) throw (OpenNIException)
00047 : OpenNIDevice(context)
00048 , streaming_ (streaming)
00049 , depth_stream_running_ (false)
00050 , image_stream_running_ (false)
00051 , ir_stream_running_ (false)
00052 {
00053 XnStatus status;
00054 status = context_.OpenFileRecording(file_name.c_str());
00055 if (status != XN_STATUS_OK)
00056 THROW_OPENNI_EXCEPTION("Could not open ONI file. Reason: %s", xnGetStatusString(status));
00057
00058 status = context.FindExistingNode(XN_NODE_TYPE_DEPTH, depth_generator_);
00059 if (status != XN_STATUS_OK)
00060 THROW_OPENNI_EXCEPTION("could not find depth stream in file %s. Reason: %s", file_name.c_str(), xnGetStatusString(status));
00061 else
00062 {
00063 available_depth_modes_.push_back(getDepthOutputMode());
00064 depth_generator_.RegisterToNewDataAvailable ((xn::StateChangedHandler)NewONIDepthDataAvailable, this, depth_callback_handle_);
00065 }
00066
00067 status = context.FindExistingNode(XN_NODE_TYPE_IMAGE, image_generator_);
00068 if (status == XN_STATUS_OK)
00069 {
00070 available_image_modes_.push_back(getImageOutputMode());
00071 image_generator_.RegisterToNewDataAvailable ((xn::StateChangedHandler)NewONIImageDataAvailable, this, image_callback_handle_);
00072 }
00073
00074 status = context.FindExistingNode(XN_NODE_TYPE_IR, ir_generator_);
00075 if (status == XN_STATUS_OK)
00076 ir_generator_.RegisterToNewDataAvailable ((xn::StateChangedHandler)NewONIIRDataAvailable, this, ir_callback_handle_);
00077
00078 status = context.FindExistingNode(XN_NODE_TYPE_PLAYER, player_);
00079 if (status != XN_STATUS_OK)
00080 THROW_OPENNI_EXCEPTION("Failed to find player node: %s\n", xnGetStatusString(status));
00081
00082 device_node_info_ = player_.GetInfo();
00083
00084 Init ();
00085
00086 player_.SetRepeat(repeat);
00087 if (streaming_)
00088 player_thread_ = boost::thread (&DeviceONI::PlayerThreadFunction, this);
00089 }
00090
00091 DeviceONI::~DeviceONI() throw ()
00092 {
00093 if (streaming_)
00094 {
00095 quit_ = true;
00096 player_thread_.join();
00097 }
00098 }
00099
00100 void DeviceONI::startImageStream () throw (OpenNIException)
00101 {
00102 if (hasImageStream() && !image_stream_running_)
00103 image_stream_running_ = true;
00104 }
00105
00106 void DeviceONI::stopImageStream () throw (OpenNIException)
00107 {
00108 if (hasImageStream() && image_stream_running_)
00109 image_stream_running_ = false;
00110 }
00111
00112 void DeviceONI::startDepthStream () throw (OpenNIException)
00113 {
00114 if (hasDepthStream() && !depth_stream_running_)
00115 depth_stream_running_ = true;
00116 }
00117
00118 void DeviceONI::stopDepthStream () throw (OpenNIException)
00119 {
00120 if (hasDepthStream() && depth_stream_running_)
00121 depth_stream_running_ = false;
00122 }
00123
00124 void DeviceONI::startIRStream () throw (OpenNIException)
00125 {
00126 if (hasIRStream() && !ir_stream_running_)
00127 ir_stream_running_ = true;
00128 }
00129
00130 void DeviceONI::stopIRStream () throw (OpenNIException)
00131 {
00132 if (hasIRStream() && ir_stream_running_)
00133 ir_stream_running_ = false;
00134 }
00135
00136 bool DeviceONI::isImageStreamRunning () const throw (OpenNIException)
00137 {
00138 return image_stream_running_;
00139 }
00140
00141 bool DeviceONI::isDepthStreamRunning () const throw (OpenNIException)
00142 {
00143 return depth_stream_running_;
00144 }
00145
00146 bool DeviceONI::isIRStreamRunning () const throw (OpenNIException)
00147 {
00148 return ir_stream_running_;
00149 }
00150
00151 bool DeviceONI::trigger () throw (OpenNIException)
00152 {
00153 if (player_.IsEOF())
00154 return false;
00155
00156 if (streaming_)
00157 THROW_OPENNI_EXCEPTION ("Virtual device is in streaming mode. Trigger not available.");
00158
00159 player_.ReadNext();
00160 return true;
00161 }
00162
00163 bool DeviceONI::isStreaming () const throw (OpenNIException)
00164 {
00165 return streaming_;
00166 }
00167
00168 void DeviceONI::PlayerThreadFunction() throw (OpenNIException)
00169 {
00170 quit_ = false;
00171 while (!quit_)
00172 player_.ReadNext();
00173 }
00174
00175 void __stdcall DeviceONI::NewONIDepthDataAvailable (xn::ProductionNode& node, void* cookie) throw ()
00176 {
00177 DeviceONI* device = reinterpret_cast<DeviceONI*>(cookie);
00178 if (device->depth_stream_running_)
00179 device->depth_condition_.notify_all ();
00180 }
00181
00182 void __stdcall DeviceONI::NewONIImageDataAvailable (xn::ProductionNode& node, void* cookie) throw ()
00183 {
00184 DeviceONI* device = reinterpret_cast<DeviceONI*>(cookie);
00185 if (device->image_stream_running_)
00186 device->image_condition_.notify_all ();
00187 }
00188
00189 void __stdcall DeviceONI::NewONIIRDataAvailable (xn::ProductionNode& node, void* cookie) throw ()
00190 {
00191 DeviceONI* device = reinterpret_cast<DeviceONI*>(cookie);
00192 if (device->ir_stream_running_)
00193 device->ir_condition_.notify_all ();
00194 }
00195
00196 boost::shared_ptr<Image> DeviceONI::getCurrentImage(boost::shared_ptr<xn::ImageMetaData> image_meta_data) const throw ()
00197 {
00198 return boost::shared_ptr<Image > (new ImageRGB24(image_meta_data));
00199 }
00200
00201 bool DeviceONI::isImageResizeSupported(unsigned input_width, unsigned input_height, unsigned output_width, unsigned output_height) const throw ()
00202 {
00203 return ImageRGB24::resizingSupported (input_width, input_height, output_width, output_height);
00204 }
00205
00206 }
00207
00208