openni_device_oni.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011 2011 Willow Garage, Inc.
5  * Suat Gedikli <gedikli@willowgarage.com>
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
39 
40 using namespace std;
41 using namespace boost;
42 
43 namespace openni_wrapper
44 {
45 
46 DeviceONI::DeviceONI(xn::Context& context, const std::string& file_name, bool repeat, bool streaming) throw (OpenNIException)
47  : OpenNIDevice(context)
48  , streaming_ (streaming)
49  , depth_stream_running_ (false)
50  , image_stream_running_ (false)
51  , ir_stream_running_ (false)
52 {
53  XnStatus status;
54  status = context_.OpenFileRecording(file_name.c_str());
55  if (status != XN_STATUS_OK)
56  THROW_OPENNI_EXCEPTION("Could not open ONI file. Reason: %s", xnGetStatusString(status));
57 
58  status = context.FindExistingNode(XN_NODE_TYPE_DEPTH, depth_generator_);
59  if (status != XN_STATUS_OK)
60  THROW_OPENNI_EXCEPTION("could not find depth stream in file %s. Reason: %s", file_name.c_str(), xnGetStatusString(status));
61  else
62  {
63  available_depth_modes_.push_back(getDepthOutputMode());
64  depth_generator_.RegisterToNewDataAvailable ((xn::StateChangedHandler)NewONIDepthDataAvailable, this, depth_callback_handle_);
65  }
66 
67  status = context.FindExistingNode(XN_NODE_TYPE_IMAGE, image_generator_);
68  if (status == XN_STATUS_OK)
69  {
70  available_image_modes_.push_back(getImageOutputMode());
71  image_generator_.RegisterToNewDataAvailable ((xn::StateChangedHandler)NewONIImageDataAvailable, this, image_callback_handle_);
72  }
73 
74  status = context.FindExistingNode(XN_NODE_TYPE_IR, ir_generator_);
75  if (status == XN_STATUS_OK)
76  ir_generator_.RegisterToNewDataAvailable ((xn::StateChangedHandler)NewONIIRDataAvailable, this, ir_callback_handle_);
77 
78  status = context.FindExistingNode(XN_NODE_TYPE_PLAYER, player_);
79  if (status != XN_STATUS_OK)
80  THROW_OPENNI_EXCEPTION("Failed to find player node: %s\n", xnGetStatusString(status));
81 
82  device_node_info_ = player_.GetInfo();
83 
84  Init ();
85 
86  player_.SetRepeat(repeat);
87  if (streaming_)
88  player_thread_ = boost::thread (&DeviceONI::PlayerThreadFunction, this);
89 }
90 
91 DeviceONI::~DeviceONI() throw ()
92 {
93  if (streaming_)
94  {
95  quit_ = true;
96  player_thread_.join();
97  }
98 }
99 
100 void DeviceONI::startImageStream () throw (OpenNIException)
101 {
102  if (hasImageStream() && !image_stream_running_)
103  image_stream_running_ = true;
104 }
105 
106 void DeviceONI::stopImageStream () throw (OpenNIException)
107 {
108  if (hasImageStream() && image_stream_running_)
109  image_stream_running_ = false;
110 }
111 
112 void DeviceONI::startDepthStream () throw (OpenNIException)
113 {
114  if (hasDepthStream() && !depth_stream_running_)
115  depth_stream_running_ = true;
116 }
117 
118 void DeviceONI::stopDepthStream () throw (OpenNIException)
119 {
120  if (hasDepthStream() && depth_stream_running_)
121  depth_stream_running_ = false;
122 }
123 
124 void DeviceONI::startIRStream () throw (OpenNIException)
125 {
126  if (hasIRStream() && !ir_stream_running_)
127  ir_stream_running_ = true;
128 }
129 
130 void DeviceONI::stopIRStream () throw (OpenNIException)
131 {
132  if (hasIRStream() && ir_stream_running_)
133  ir_stream_running_ = false;
134 }
135 
136 bool DeviceONI::isImageStreamRunning () const throw (OpenNIException)
137 {
138  return image_stream_running_;
139 }
140 
141 bool DeviceONI::isDepthStreamRunning () const throw (OpenNIException)
142 {
143  return depth_stream_running_;
144 }
145 
146 bool DeviceONI::isIRStreamRunning () const throw (OpenNIException)
147 {
148  return ir_stream_running_;
149 }
150 
151 bool DeviceONI::trigger () throw (OpenNIException)
152 {
153  if (player_.IsEOF())
154  return false;
155 
156  if (streaming_)
157  THROW_OPENNI_EXCEPTION ("Virtual device is in streaming mode. Trigger not available.");
158 
159  player_.ReadNext();
160  return true;
161 }
162 
163 bool DeviceONI::isStreaming () const throw (OpenNIException)
164 {
165  return streaming_;
166 }
167 
168 void DeviceONI::PlayerThreadFunction() throw (OpenNIException)
169 {
170  quit_ = false;
171  while (!quit_)
172  player_.ReadNext();
173 }
174 
175 void __stdcall DeviceONI::NewONIDepthDataAvailable (xn::ProductionNode& node, void* cookie) throw ()
176 {
177  DeviceONI* device = reinterpret_cast<DeviceONI*>(cookie);
178  if (device->depth_stream_running_)
179  device->depth_condition_.notify_all ();
180 }
181 
182 void __stdcall DeviceONI::NewONIImageDataAvailable (xn::ProductionNode& node, void* cookie) throw ()
183 {
184  DeviceONI* device = reinterpret_cast<DeviceONI*>(cookie);
185  if (device->image_stream_running_)
186  device->image_condition_.notify_all ();
187 }
188 
189 void __stdcall DeviceONI::NewONIIRDataAvailable (xn::ProductionNode& node, void* cookie) throw ()
190 {
191  DeviceONI* device = reinterpret_cast<DeviceONI*>(cookie);
192  if (device->ir_stream_running_)
193  device->ir_condition_.notify_all ();
194 }
195 
196 boost::shared_ptr<Image> DeviceONI::getCurrentImage(boost::shared_ptr<xn::ImageMetaData> image_meta_data) const throw ()
197 {
198  return boost::shared_ptr<Image > (new ImageRGB24(image_meta_data));
199 }
200 
201 bool DeviceONI::isImageResizeSupported(unsigned input_width, unsigned input_height, unsigned output_width, unsigned output_height) const throw ()
202 {
203  return ImageRGB24::resizingSupported (input_width, input_height, output_width, output_height);
204 }
205 
206 }
207 
208 
void notify_all() BOOST_NOEXCEPT
#define THROW_OPENNI_EXCEPTION(format,...)
boost::condition_variable depth_condition_
This class provides methods to fill a RGB or Grayscale image buffer from underlying RGB24 image...
General exception class.
boost::condition_variable ir_condition_
string status
Definition: test_launch.py:51
#define __stdcall
Definition: openni_device.h:53
boost::condition_variable image_condition_
Concrete implementation of the interface OpenNIDevice for a virtual device playing back an ONI file...
Class representing an astract device for Primesense or MS Kinect devices.
Definition: openni_device.h:66


openni_camera
Author(s): Patrick Mihelich, Suat Gedikli, Radu Bogdan Rusu
autogenerated on Mon Jun 10 2019 14:15:53