ds5-private.cpp
Go to the documentation of this file.
1 
4 #include "ds5-private.h"
5 
6 using namespace std;
7 
8 #define intrinsics_string(res) #res << "\t" << array2str((float_4&)table->rect_params[res]) << endl
9 
10 namespace librealsense
11 {
12  namespace ds
13  {
15  {
16  for (auto& elem : resolutions_list)
17  {
18  if (uint32_t(elem.second.x) == width && uint32_t(elem.second.y) == height)
19  return elem.first;
20  }
22  }
23 
25  {
26  auto table = check_calib<ds::coefficients_table>(raw_data);
27 
28  LOG_DEBUG(endl
29  << "baseline = " << table->baseline << " mm" << endl
30  << "Rect params: \t fX\t\t fY\t\t ppX\t\t ppY \n"
41 
43 
44  if (width == 848 && height == 100) // Special 848x100 resolution is available in some firmware versions
45  // for this resolution we use the same base-intrinsics as for 848x480 and adjust them later
46  {
48  }
49 
51  {
53  intrinsics.width = resolutions_list[resolution].x;
54  intrinsics.height = resolutions_list[resolution].y;
55 
56  auto rect_params = static_cast<const float4>(table->rect_params[resolution]);
57  // DS5U - assume ideal intrinsic params
58  if ((rect_params.x == rect_params.y) && (rect_params.z == rect_params.w))
59  {
60  rect_params.x = rect_params.y = intrinsics.width * 1.5f;
61  rect_params.z = intrinsics.width * 0.5f;
62  rect_params.w = intrinsics.height * 0.5f;
63  }
64  intrinsics.fx = rect_params[0];
65  intrinsics.fy = rect_params[1];
66  intrinsics.ppx = rect_params[2];
67  intrinsics.ppy = rect_params[3];
69  memset(intrinsics.coeffs, 0, sizeof(intrinsics.coeffs)); // All coefficients are zeroed since rectified depth is defined as CS origin
70 
71  // In case of the special 848x100 resolution adjust the intrinsics
72  if (width == 848 && height == 100)
73  {
74  intrinsics.height = 100;
75  intrinsics.ppy -= 190;
76  }
77 
78  return intrinsics;
79  }
80  else
81  {
82  // Intrinsics not found in the calibration table - use the generic calculation
85  intrinsics.width = width;
86  intrinsics.height = height;
87 
88  auto rect_params = static_cast<const float4>(table->rect_params[resolution]);
89  // DS5U - assume ideal intrinsic params
90  if ((rect_params.x == rect_params.y) && (rect_params.z == rect_params.w))
91  {
92  rect_params.x = rect_params.y = intrinsics.width * 1.5f;
93  rect_params.z = intrinsics.width * 0.5f;
94  rect_params.w = intrinsics.height * 0.5f;
95  }
96 
97  // Special resolution for auto-calibration requires special treatment...
98  if (width == 256 && height == 144)
99  {
100  intrinsics.fx = rect_params[0];
101  intrinsics.fy = rect_params[1];
102  intrinsics.ppx = rect_params[2] - 832;
103  intrinsics.ppy = rect_params[3] - 468;
104  }
105  else
106  {
107  intrinsics.fx = rect_params[0] * width / resolutions_list[resolution].x;
108  intrinsics.fy = rect_params[1] * height / resolutions_list[resolution].y;
109  intrinsics.ppx = rect_params[2] * width / resolutions_list[resolution].x;
110  intrinsics.ppy = rect_params[3] * height / resolutions_list[resolution].y;
111  }
112 
113  intrinsics.model = RS2_DISTORTION_BROWN_CONRADY;
114  memset(intrinsics.coeffs, 0, sizeof(intrinsics.coeffs)); // All coefficients are zeroed since rectified depth is defined as CS origin
115 
116  return intrinsics;
117  }
118  }
119 
120  rs2_intrinsics get_intrinsic_fisheye_table(const std::vector<uint8_t>& raw_data, uint32_t width, uint32_t height)
121  {
122  auto table = check_calib<ds::fisheye_calibration_table>(raw_data);
123 
125  auto intrin = table->intrinsic;
126  intrinsics.fx = intrin(0,0);
127  intrinsics.fy = intrin(1,1);
128  intrinsics.ppx = intrin(2,0);
129  intrinsics.ppy = intrin(2,1);
130  intrinsics.model = RS2_DISTORTION_FTHETA;
131 
132  intrinsics.height = height;
133  intrinsics.width = width;
134 
135  librealsense::copy(intrinsics.coeffs, table->distortion, sizeof(table->distortion));
136 
137  LOG_DEBUG(endl<< array2str((float_4&)(intrinsics.fx, intrinsics.fy, intrinsics.ppx, intrinsics.ppy)) << endl);
138 
139  return intrinsics;
140  }
141 
142  rs2_intrinsics get_color_stream_intrinsic(const std::vector<uint8_t>& raw_data, uint32_t width, uint32_t height)
143  {
144  auto table = check_calib<ds::rgb_calibration_table>(raw_data);
145 
146  // Compensate for aspect ratio as the normalized intrinsic is calculated with a single resolution
147  float3x3 intrin = table->intrinsic;
148  float calib_aspect_ratio = 9.f / 16.f; // shall be overwritten with the actual calib resolution
149 
150  if (table->calib_width && table->calib_height)
151  calib_aspect_ratio = float(table->calib_height) / float(table->calib_width);
152  else
153  {
154  LOG_WARNING("RGB Calibration resolution is not specified, using default 16/9 Aspect ratio");
155  }
156 
157  // Compensate for aspect ratio
158  float actual_aspect_ratio = height / (float)width;
159  if (actual_aspect_ratio < calib_aspect_ratio)
160  {
161  intrin(1, 1) *= calib_aspect_ratio / actual_aspect_ratio;
162  intrin(2, 1) *= calib_aspect_ratio / actual_aspect_ratio;
163  }
164  else
165  {
166  intrin(0, 0) *= actual_aspect_ratio / calib_aspect_ratio;
167  intrin(2, 0) *= actual_aspect_ratio / calib_aspect_ratio;
168  }
169 
170  // Calculate specific intrinsic parameters based on the normalized intrinsic and the sensor's resolution
171  rs2_intrinsics calc_intrinsic{
172  static_cast<int>(width),
173  static_cast<int>(height),
174  ((1 + intrin(2, 0))*width) / 2.f,
175  ((1 + intrin(2, 1))*height) / 2.f,
176  intrin(0, 0) * width / 2.f,
177  intrin(1, 1) * height / 2.f,
178  RS2_DISTORTION_INVERSE_BROWN_CONRADY // The coefficients shall be use for undistort
179  };
180  librealsense::copy(calc_intrinsic.coeffs, table->distortion, sizeof(table->distortion));
181  LOG_DEBUG(endl << array2str((float_4&)(calc_intrinsic.fx, calc_intrinsic.fy, calc_intrinsic.ppx, calc_intrinsic.ppy)) << endl);
182 
183  static rs2_intrinsics ref{};
184  if (memcmp(&calc_intrinsic, &ref, sizeof(rs2_intrinsics)))
185  {
186  LOG_DEBUG_THERMAL_LOOP("RGB Intrinsic: ScaleX, ScaleY = "
187  << std::setprecision(3) << intrin(0, 0) << ", " << intrin(1, 1)
188  << ". Fx,Fy = " << calc_intrinsic.fx << "," << calc_intrinsic.fy);
189  ref = calc_intrinsic;
190  }
191 
192  return calc_intrinsic;
193  }
194 
195  // Parse intrinsics from newly added RECPARAMSGET command
196  bool try_get_intrinsic_by_resolution_new(const vector<uint8_t>& raw_data,
198  {
199  using namespace ds;
200  auto count = raw_data.size() / sizeof(new_calibration_item);
201  auto items = (new_calibration_item*)raw_data.data();
202  for (int i = 0; i < count; i++)
203  {
204  auto&& item = items[i];
205  if (item.width == width && item.height == height)
206  {
207  result->width = width;
208  result->height = height;
209  result->ppx = item.ppx;
210  result->ppy = item.ppy;
211  result->fx = item.fx;
212  result->fy = item.fy;
214  memset(result->coeffs, 0, sizeof(result->coeffs)); // All coefficients are zeroed since rectified depth is defined as CS origin
215 
216  return true;
217  }
218  }
219  return false;
220  }
221 
223  {
224  switch (table_id)
225  {
227  {
228  return get_intrinsic_by_resolution_coefficients_table(raw_data, width, height);
229  }
231  {
232  return get_intrinsic_fisheye_table(raw_data, width, height);
233  }
234  case rgb_calibration_id:
235  {
236  return get_color_stream_intrinsic(raw_data, width, height);
237  }
238  default:
239  throw invalid_value_exception(to_string() << "Parsing Calibration table type " << table_id << " is not supported");
240  }
241  }
242 
243  pose get_fisheye_extrinsics_data(const vector<uint8_t> & raw_data)
244  {
245  auto table = check_calib<fisheye_extrinsics_table>(raw_data);
246 
247  auto rot = table->rotation;
248  auto trans = table->translation;
249 
250  pose ex = {{rot(0,0), rot(1,0),rot(2,0),rot(1,0), rot(1,1),rot(2,1),rot(0,2), rot(1,2),rot(2,2)},
251  {trans[0], trans[1], trans[2]}};
252  return ex;
253  }
254 
255  pose get_color_stream_extrinsic(const std::vector<uint8_t>& raw_data)
256  {
257  auto table = check_calib<rgb_calibration_table>(raw_data);
258  float3 trans_vector = table->translation_rect;
259  float3x3 rect_rot_mat = table->rotation_matrix_rect;
260  float trans_scale = -0.001f; // Convert units from mm to meter. Extrinsic of color is referenced to the Depth Sensor CS
261 
262  trans_vector.x *= trans_scale;
263  trans_vector.y *= trans_scale;
264  trans_vector.z *= trans_scale;
265 
266  return{ rect_rot_mat,trans_vector };
267  }
268 
269  bool try_fetch_usb_device(std::vector<platform::usb_device_info>& devices,
271  {
272  for (auto it = devices.begin(); it != devices.end(); ++it)
273  {
274  if (it->unique_id == info.unique_id)
275  {
276  bool found = false;
277  result = *it;
278  switch (info.pid)
279  {
280  case RS_USB2_PID:
281  case RS400_PID:
282  case RS405U_PID:
283  case RS410_PID:
284  case RS416_PID:
285  case RS460_PID:
286  case RS430_PID:
287  case RS420_PID:
288  case RS400_IMU_PID:
289  found = (result.mi == 3);
290  break;
291  case RS405_PID:
292  case RS430I_PID:
293  found = (result.mi == 4);
294  break;
295  case RS430_MM_PID:
296  case RS420_MM_PID:
297  case RS435I_PID:
298  case RS455_PID:
299  found = (result.mi == 6);
300  break;
301  case RS415_PID:
302  case RS416_RGB_PID:
303  case RS435_RGB_PID:
304  case RS465_PID:
305  found = (result.mi == 5);
306  break;
307  default:
308  throw not_implemented_exception(to_string() << "USB device "
309  << std::hex << info.pid << ":" << info.vid << std::dec << " is not supported.");
310  break;
311  }
312 
313  if (found)
314  {
315  devices.erase(it);
316  return true;
317  }
318  }
319  }
320  return false;
321  }
322 
323 
324  std::vector<platform::uvc_device_info> filter_device_by_capability(const std::vector<platform::uvc_device_info>& devices,
325  d400_caps caps)
326  {
327  std::vector<platform::uvc_device_info> results;
328 
329  switch (caps)
330  {
331  case d400_caps::CAP_FISHEYE_SENSOR:
332  std::copy_if(devices.begin(),devices.end(),std::back_inserter(results),
334  { return fisheye_pid.find(info.pid) != fisheye_pid.end();});
335  break;
336  default:
338  << "Capability filters are not implemented for val "
339  << std::hex << caps << std::dec);
340  }
341  return results;
342  }
343 
345  {
346  switch (flash_version)
347  {
348  // { number of payloads in section { ro table types }} see Flash.xml
349  case 100: return { 2, { 17, 10, 40, 29, 30, 54} };
350  case 101: return { 3, { 10, 16, 40, 29, 18, 19, 30, 20, 21, 54 } };
351  case 102: return { 3, { 9, 10, 16, 40, 29, 18, 19, 30, 20, 21, 54 } };
352  case 103: return { 4, { 9, 10, 16, 40, 29, 18, 19, 30, 20, 21, 54 } };
353  case 104: return { 4, { 9, 10, 40, 29, 18, 19, 30, 20, 21, 54 } };
354  case 105: return { 5, { 9, 10, 40, 29, 18, 19, 30, 20, 21, 54 } };
355  case 106: return { 5, { 15, 9, 10, 16, 40, 29, 18, 19, 30, 20, 21, 54 } };
356  case 107: return { 6, { 15, 9, 10, 16, 40, 29, 18, 19, 30, 20, 21, 54 } };
357  default:
358  throw std::runtime_error("Unsupported flash version: " + std::to_string(flash_version));
359  }
360  }
361 
363  {
364  switch (flash_version)
365  {
366  // { number of payloads in section { ro table types }} see Flash.xml
367  case 100: return { 2, { 134, 25 } };
368  default:
369  throw std::runtime_error("Unsupported flash version: " + std::to_string(flash_version));
370  }
371  }
372 
373  flash_info get_flash_info(const std::vector<uint8_t>& flash_buffer)
374  {
375  flash_info rv = {};
376 
377  uint32_t header_offset = FLASH_INFO_HEADER_OFFSET;
378  memcpy(&rv.header, flash_buffer.data() + header_offset, sizeof(rv.header));
379 
382 
383  auto ro_toc = parse_table_of_contents(flash_buffer, ro_toc_offset);
384  auto rw_toc = parse_table_of_contents(flash_buffer, rw_toc_offset);
385 
386  auto ro_structure = get_ro_flash_structure(ro_toc.header.version);
387  auto rw_structure = get_rw_flash_structure(rw_toc.header.version);
388 
389  rv.read_only_section = parse_flash_section(flash_buffer, ro_toc, ro_structure);
391  rv.read_write_section = parse_flash_section(flash_buffer, rw_toc, rw_structure);
393 
394  return rv;
395  }
396  } // librealsense::ds
397 } // namespace librealsense
rs2_intrinsics get_intrinsic_by_resolution_coefficients_table(const std::vector< uint8_t > &raw_data, uint32_t width, uint32_t height)
Definition: ds5-private.cpp:24
flash_section parse_flash_section(const std::vector< uint8_t > &flash_buffer, flash_table toc, flash_structure s)
bool try_get_intrinsic_by_resolution_new(const vector< uint8_t > &raw_data, uint32_t width, uint32_t height, rs2_intrinsics *result)
std::string array2str(T &data)
Definition: src/types.h:120
rs2_intrinsics get_intrinsic_fisheye_table(const std::vector< uint8_t > &raw_data, uint32_t width, uint32_t height)
#define LOG_WARNING(...)
Definition: src/types.h:241
const uint16_t RS415_PID
Definition: ds5-private.h:27
pose get_fisheye_extrinsics_data(const vector< uint8_t > &raw_data)
const uint16_t RS460_PID
Definition: ds5-private.h:39
#define LOG_DEBUG_THERMAL_LOOP(...)
Definition: ds5-private.h:18
rs2_intrinsics intrin
const uint16_t RS435_RGB_PID
Definition: ds5-private.h:40
static const std::set< std::uint16_t > fisheye_pid
Definition: ds5-private.h:120
float coeffs[5]
Definition: rs_types.h:67
static std::map< ds5_rect_resolutions, int2 > resolutions_list
Definition: ds5-private.h:686
ds5_rect_resolutions width_height_to_ds5_rect_resolutions(uint32_t width, uint32_t height)
Definition: ds5-private.cpp:14
const uint16_t RS400_IMU_PID
Definition: ds5-private.h:33
def info(name, value, persistent=False)
Definition: test.py:301
const uint16_t RS416_RGB_PID
Definition: ds5-private.h:46
const uint16_t RS405U_PID
Definition: ds5-private.h:41
const uint16_t RS455_PID
Definition: ds5-private.h:48
const uint16_t RS410_PID
Definition: ds5-private.h:26
bool try_fetch_usb_device(std::vector< platform::usb_device_info > &devices, const platform::uvc_device_info &info, platform::usb_device_info &result)
flash_structure get_ro_flash_structure(const uint32_t flash_version)
unsigned int uint32_t
Definition: stdint.h:80
devices
Definition: test-fg.py:9
GLint GLsizei GLsizei height
const uint32_t FLASH_RW_TABLE_OF_CONTENT_OFFSET
Definition: ds5-private.h:169
rs2_intrinsics get_intrinsic_by_resolution(const vector< uint8_t > &raw_data, calibration_table_id table_id, uint32_t width, uint32_t height)
const uint16_t RS420_PID
Definition: ds5-private.h:34
const uint16_t RS_USB2_PID
Definition: ds5-private.h:30
rs2_intrinsics get_color_stream_intrinsic(const std::vector< uint8_t > &raw_data, uint32_t width, uint32_t height)
flash_table parse_table_of_contents(const std::vector< uint8_t > &flash_buffer, uint32_t toc_offset)
flash_structure get_rw_flash_structure(const uint32_t flash_version)
dictionary intrinsics
Definition: t265_stereo.py:142
#define intrinsics_string(res)
Definition: ds5-private.cpp:8
const uint16_t RS416_PID
Definition: ds5-private.h:43
GLenum GLenum GLsizei void * table
const uint16_t RS430_PID
Definition: ds5-private.h:28
rs2_distortion model
Definition: rs_types.h:66
const uint16_t RS420_MM_PID
Definition: ds5-private.h:35
const uint16_t RS435I_PID
Definition: ds5-private.h:42
static auto it
GLint GLsizei count
GLint ref
const uint32_t FLASH_INFO_HEADER_OFFSET
Definition: ds5-private.h:171
Video stream intrinsics.
Definition: rs_types.h:58
const uint32_t FLASH_RO_TABLE_OF_CONTENT_OFFSET
Definition: ds5-private.h:170
int i
#define LOG_DEBUG(...)
Definition: src/types.h:239
float float_4[4]
Definition: src/types.h:507
std::vector< platform::uvc_device_info > filter_device_by_capability(const std::vector< platform::uvc_device_info > &devices, d400_caps caps)
pose get_color_stream_extrinsic(const std::vector< uint8_t > &raw_data)
const uint16_t RS465_PID
Definition: ds5-private.h:45
const uint16_t RS400_PID
Definition: ds5-private.h:25
GLuint64EXT * result
Definition: glext.h:10921
const uint16_t RS430I_PID
Definition: ds5-private.h:44
GLint GLsizei width
const uint16_t RS430_MM_PID
Definition: ds5-private.h:29
void copy(void *dst, void const *src, size_t size)
Definition: types.cpp:836
const uint16_t RS405_PID
Definition: ds5-private.h:47
flash_info get_flash_info(const std::vector< uint8_t > &flash_buffer)
std::string to_string(T value)


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