Rs2Stream.cpp
Go to the documentation of this file.
1 #include "Rs2Driver.h"
2 #include "Rs2Commands.h"
3 #include "XnDepthShiftTables.h"
4 #include <librealsense2/rsutil.h>
5 
6 namespace oni { namespace driver {
7 
8 Rs2Stream::Rs2Stream(OniSensorType sensorType)
9 :
10  m_rsType(convertStreamType(sensorType)),
11  m_oniType(sensorType),
12  m_device(nullptr),
13  m_sensor(nullptr),
14  m_sensorId(-1),
15  m_streamId(-1),
16  m_enabled(false),
17  m_depthScale(0),
18  m_fovX(0),
19  m_fovY(0)
20 {
21  rsLogDebug("+Rs2Stream type=%d", (int)m_rsType);
22 }
23 
25 {
26  rsLogDebug("~Rs2Stream type=%d", (int)m_rsType);
27 
28  shutdown();
29 }
30 
31 OniStatus Rs2Stream::initialize(class Rs2Device* device, rs2_sensor* sensor, int sensorId, int streamId, std::vector<Rs2StreamProfileInfo>* profiles)
32 {
33  rsTraceFunc("type=%d sensorId=%d streamId=%d", (int)m_rsType, sensorId, streamId);
34 
35  m_device = device;
36  m_sensor = sensor;
37  m_sensorId = sensorId;
38  m_streamId = streamId;
40 
41  memset(&m_videoMode, 0, sizeof(m_videoMode));
42  memset(&m_intrinsics, 0, sizeof(m_intrinsics));
44 
45  m_depthScale = 0;
46  m_fovX = 0;
47  m_fovY = 0;
48 
49  if (m_oniType == ONI_SENSOR_DEPTH) m_videoMode.pixelFormat = ONI_PIXEL_FORMAT_DEPTH_1_MM;
50  else if (m_oniType == ONI_SENSOR_COLOR) m_videoMode.pixelFormat = ONI_PIXEL_FORMAT_RGB888;
51  else m_videoMode.pixelFormat = ONI_PIXEL_FORMAT_GRAY8;
52 
53  // TODO: maybe use default sensor mode?
54  m_videoMode.resolutionX = 640;
55  m_videoMode.resolutionY = 480;
56  m_videoMode.fps = 30;
57 
58  if (m_oniType == ONI_SENSOR_DEPTH)
59  {
60  // TODO: these tables should be calculated depending on sensor parameters, using hardcoded values as workaround for NITE
61  setTable(S2D, sizeof(S2D), m_s2d); // XN_STREAM_PROPERTY_S2D_TABLE
62  setTable(D2S, sizeof(D2S), m_d2s); // XN_STREAM_PROPERTY_D2S_TABLE
63  }
64 
66 
67  return ONI_STATUS_OK;
68 }
69 
71 {
72  if (m_sensor) { rsTraceFunc("type=%d sensorId=%d streamId=%d", (int)m_rsType, m_sensorId, m_streamId); }
73 
74  stop();
75 
76  if (m_sensor)
77  {
79  m_sensor = nullptr;
80  }
81 }
82 
83 //=============================================================================
84 // StreamBase overrides
85 //=============================================================================
86 
87 OniStatus Rs2Stream::start()
88 {
89  if(!m_enabled)
90  {
91  rsTraceFunc("type=%d sensorId=%d streamId=%d", (int)m_rsType, m_sensorId, m_streamId);
92  m_enabled = true;
94  }
95 
96  return ONI_STATUS_OK;
97 }
98 
100 {
101  if (m_enabled)
102  {
103  rsTraceFunc("type=%d sensorId=%d streamId=%d", (int)m_rsType, m_sensorId, m_streamId);
104  m_enabled = false;
106  }
107 }
108 
109 OniStatus Rs2Stream::invoke(int commandId, void* data, int dataSize)
110 {
111  if (commandId == RS2_PROJECT_POINT_TO_PIXEL && data && dataSize == sizeof(Rs2PointPixel))
112  {
113  auto pp = (Rs2PointPixel*)data;
114  rs2_project_point_to_pixel(pp->pixel, &m_intrinsics, pp->point);
115  return ONI_STATUS_OK;
116  }
117 
118  #if defined(RS2_TRACE_NOT_SUPPORTED_CMDS)
119  rsTraceError("Not supported: commandId=%d", commandId);
120  #endif
121  return ONI_STATUS_NOT_SUPPORTED;
122 }
123 
124 OniBool Rs2Stream::isCommandSupported(int commandId)
125 {
126  switch (commandId)
127  {
128  case RS2_PROJECT_POINT_TO_PIXEL: return true;
129  }
130 
131  return false;
132 }
133 
134 OniStatus Rs2Stream::convertDepthToColorCoordinates(StreamBase* colorStream, int depthX, int depthY, OniDepthPixel depthZ, int* pColorX, int* pColorY)
135 {
137 
138  if (m_oniType != ONI_SENSOR_DEPTH)
139  {
140  rsTraceError("Invalid oniType=%d", (int)m_oniType);
141  return ONI_STATUS_ERROR;
142  }
143 
144  if (!colorStream || ((Rs2Stream*)colorStream)->getOniType() != ONI_SENSOR_COLOR)
145  {
146  rsTraceError("Invalid colorStream");
147  return ONI_STATUS_ERROR;
148  }
149 
150  if (getDevice()->getRegistrationMode() == ONI_IMAGE_REGISTRATION_DEPTH_TO_COLOR)
151  {
152  *pColorX = depthX;
153  *pColorY = depthY;
154  }
155  else
156  {
157  float pixel[2];
158  float point[3] = {0, 0, 0};
159  float transformed_point[3] = { 0 };
162  Rs2StreamProfileInfo* spiColor = ((Rs2Stream*)colorStream)->getCurrentProfile();
163  rs2_get_extrinsics(spiDepth->profile, spiColor->profile, &m_extrinsicsDepthToColor, nullptr);
165  }
166 
167  // depth pixel -> point
168  pixel[0] = (float)depthX;
169  pixel[1] = (float)depthY;
170  rs2_deproject_pixel_to_point(point, &m_intrinsics, pixel, m_depthScale * depthZ);
171 
172  // depth point -> color point
173  rs2_transform_point_to_point(transformed_point, &m_extrinsicsDepthToColor, point);
174 
175  // point -> color pixel
176  rs2_project_point_to_pixel(pixel, &((Rs2Stream*)colorStream)->m_intrinsics, transformed_point);
177  *pColorX = (int)pixel[0];
178  *pColorY = (int)pixel[1];
179  }
180 
181  //rsLogDebug("depth %d %d (%d) -> color %d %d", depthX, depthY, depthZ, *pColorX, *pColorY);
182 
183  return ONI_STATUS_OK;
184 }
185 
186 //=============================================================================
187 // Internals
188 //=============================================================================
189 
191 {
193 }
194 
196 {
197 }
198 
200 {
204 
205  if (m_oniType == ONI_SENSOR_DEPTH)
206  {
208  }
209  else
210  {
211  m_depthScale = 0;
212  }
214 }
215 
217 {
219 
220  rs2_format pixelFormat = convertPixelFormat(m_videoMode.pixelFormat);
221 
222  for (auto iter = m_profiles.begin(); iter != m_profiles.end(); ++iter)
223  {
224  Rs2StreamProfileInfo& sp = *iter;
225 
226  if (sp.streamType == m_rsType
227  && sp.format == pixelFormat
228  && sp.width == m_videoMode.resolutionX
229  && sp.height == m_videoMode.resolutionY
230  && sp.framerate == m_videoMode.fps)
231  {
232  return &sp;
233  }
234  }
235 
236  return nullptr;
237 }
238 
239 bool Rs2Stream::isVideoModeSupported(OniVideoMode* reqMode)
240 {
242 
243  rs2_format pixelFormat = convertPixelFormat(reqMode->pixelFormat);
244 
245  for (auto iter = m_profiles.begin(); iter != m_profiles.end(); ++iter)
246  {
247  const Rs2StreamProfileInfo& sp = *iter;
248 
249  if (sp.streamType == m_rsType
250  && sp.format == pixelFormat
251  && sp.width == reqMode->resolutionX
252  && sp.height == reqMode->resolutionY
253  && sp.framerate == reqMode->fps)
254  {
255  return true;
256  }
257  }
258 
259  return false;
260 }
261 
262 bool Rs2Stream::getTable(void* dst, int* size, const std::vector<uint16_t>& table)
263 {
264  const int tableSize = (int)(table.size() * sizeof(uint16_t));
265  if (dst && size && *size >= tableSize)
266  {
267  if (tableSize > 0)
268  {
269  memcpy(dst, &table[0], tableSize);
270  }
271  *size = tableSize;
272  return true;
273  }
274  return false;
275 }
276 
277 bool Rs2Stream::setTable(const void* src, int size, std::vector<uint16_t>& table)
278 {
279  if (src && size >= 0)
280  {
281  const int elemCount = size / sizeof(uint16_t);
282  if (elemCount > 0)
283  {
284  table.resize(elemCount);
285  memcpy(&table[0], src, elemCount * sizeof(uint16_t));
286  }
287  else
288  {
289  table.clear();
290  }
291  return true;
292  }
293  return false;
294 }
295 
296 }} // namespace
static void rs2_transform_point_to_point(float to_point[3], const struct rs2_extrinsics *extrin, const float from_point[3])
Definition: rsutil.h:168
#define RS2_PROJECT_POINT_TO_PIXEL
Definition: Rs2Commands.h:3
virtual OniStatus invoke(int commandId, void *data, int dataSize)
Definition: Rs2Stream.cpp:109
float rs2_get_option(const rs2_options *options, rs2_option option, rs2_error **error)
Definition: rs.cpp:628
bool setTable(const void *src, int size, std::vector< uint16_t > &table)
Definition: Rs2Stream.cpp:277
std::vector< uint16_t > m_d2s
Definition: Rs2Stream.h:89
bool getTable(void *dst, int *size, const std::vector< uint16_t > &table)
Definition: Rs2Stream.cpp:262
virtual OniStatus start()
Definition: Rs2Stream.cpp:87
static void rs2_fov(const struct rs2_intrinsics *intrin, float to_fov[2])
Definition: rsutil.h:176
OniSensorType convertStreamType(rs2_stream type)
Definition: Rs2Base.cpp:19
GLenum GLsizei dataSize
Definition: glext.h:5691
void rs2_get_extrinsics(const rs2_stream_profile *from, const rs2_stream_profile *to, rs2_extrinsics *extrin, rs2_error **error)
Definition: rs.cpp:1110
rs2_sensor * m_sensor
Definition: Rs2Stream.h:74
rs2_extrinsics m_extrinsicsDepthToColor
Definition: Rs2Stream.h:82
virtual OniStatus convertDepthToColorCoordinates(StreamBase *colorStream, int depthX, int depthY, OniDepthPixel depthZ, int *pColorX, int *pColorY)
Definition: Rs2Stream.cpp:134
GLenum GLenum dst
Definition: glext.h:1751
rs2_stream m_rsType
Definition: Rs2Stream.h:70
unsigned short uint16_t
Definition: stdint.h:79
Rs2StreamProfileInfo * getCurrentProfile()
Definition: Rs2Stream.cpp:216
GLenum src
Definition: glext.h:1751
class Rs2Device * getDevice()
Definition: Rs2Stream.h:42
point
Definition: rmse.py:166
#define rsTraceError(format,...)
Definition: Rs2Base.h:41
Definition: Rs2Base.cpp:3
GLsizeiptr size
static void rs2_deproject_pixel_to_point(float point[3], const struct rs2_intrinsics *intrin, const float pixel[2], float depth)
Definition: rsutil.h:83
virtual OniBool isCommandSupported(int commandId)
Definition: Rs2Stream.cpp:124
void rs2_delete_sensor(rs2_sensor *sensor)
Definition: rs.cpp:320
#define SCOPED_PROFILER
Definition: Rs2Base.h:32
std::vector< Rs2StreamProfileInfo > m_profiles
Definition: Rs2Stream.h:79
rs2_format
A stream&#39;s format identifies how binary data is encoded within a frame.
Definition: rs_sensor.h:59
#define rsLogDebug(format,...)
Definition: Rs2Base.h:43
Rs2Stream(OniSensorType sensorType)
Definition: Rs2Stream.cpp:8
void rs2_get_video_stream_intrinsics(const rs2_stream_profile *mode, rs2_intrinsics *intrinsics, rs2_error **error)
Definition: rs.cpp:374
const uint16_t S2D[]
bool m_needUpdateExtrinsicsDepthToColor
Definition: Rs2Stream.h:83
GLenum GLenum GLsizei void * table
#define rsTraceFunc(format,...)
Definition: Rs2Base.h:42
static void rs2_project_point_to_pixel(float pixel[2], const struct rs2_intrinsics *intrin, const float point[3])
Definition: rsutil.h:19
class Rs2Device * m_device
Definition: Rs2Stream.h:73
rs2_intrinsics m_intrinsics
Definition: Rs2Stream.h:81
OniSensorType getOniType() const
Definition: Rs2Stream.h:44
OniStatus initialize(class Rs2Device *device, rs2_sensor *sensor, int sensorId, int streamId, std::vector< Rs2StreamProfileInfo > *profiles)
Definition: Rs2Stream.cpp:31
const rs2_stream_profile * profile
Definition: Rs2Stream.h:9
std::vector< uint16_t > m_s2d
Definition: Rs2Stream.h:88
OniPixelFormat convertPixelFormat(rs2_format type)
Definition: Rs2Base.cpp:76
virtual void stop()
Definition: Rs2Stream.cpp:99
const unsigned short D2S[]
auto device
Definition: pyrs_net.cpp:17
OniVideoMode m_videoMode
Definition: Rs2Stream.h:80
bool isVideoModeSupported(OniVideoMode *reqMode)
Definition: Rs2Stream.cpp:239
Definition: parser.hpp:150
OniSensorType m_oniType
Definition: Rs2Stream.h:71


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:40