scene-data.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2020 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 
6 // Utilities to read from/write to algo scene directories
7 
8 #include <fstream>
9 #include <string>
10 #include "../../../src/algo/depth-to-rgb-calibration/k-to-dsm.h"
11 #include "../../filesystem.h"
12 
13 inline std::string bin_dir( std::string const & scene_dir )
14 {
15  return join( join( scene_dir, "binFiles" ), "ac2" );
16 }
17 
18 
19 std::string bin_file( std::string const &prefix, size_t cycle, size_t iteration, size_t w, size_t h, std::string const &suffix )
20 {
21  return prefix + '_' + std::to_string( cycle ) + '_' + std::to_string( iteration ) + '_'
22  + std::to_string( h ) + "x" + std::to_string( w ) + "_" + suffix;
23 }
24 
25 std::string bin_file(std::string const &prefix, size_t cycle, size_t w, size_t h, std::string const &suffix)
26 {
27  return prefix + '_' + std::to_string(cycle) + '_'
28  + std::to_string(h) + "x" + std::to_string(w) + "_" + suffix;
29 }
30 std::string bin_file( std::string const &prefix, size_t w, size_t h, std::string const &suffix )
31 {
32  return prefix + "_" + std::to_string( h ) + "x" + std::to_string( w ) + "_" + suffix;
33 }
34 
35 
36 template< typename T >
38 {
39  std::fstream f = std::fstream( filename, std::ios::in | std::ios::binary );
40  if( !f )
41  throw std::runtime_error( "failed to read file:\n" + filename );
42  f.seekg( 0, f.end );
43  size_t cb = f.tellg();
44  f.seekg( 0, f.beg );
45  if( cb != sizeof( T ) )
46  throw std::runtime_error( librealsense::to_string()
47  << "file size (" << cb << ") does not match data size (" << sizeof( T ) << "): " << filename );
48  std::vector< T > vec( cb / sizeof( T ) );
49  f.read( (char *)data, cb );
50  f.close();
51 }
52 
53 template< typename T >
55 {
56  std::fstream f = std::fstream(filename, std::ios::in | std::ios::binary);
57  if (!f)
58  throw std::runtime_error("failed to read file:\n" + filename);
59  f.seekg(0, f.end);
60  size_t cb = f.tellg();
61  f.seekg(0, f.beg);
62  if (cb % sizeof(T))
63  throw std::runtime_error("file size is not a multiple of data size");
64  T obj;
65  f.read((char *)&obj, cb);
66  f.close();
67  return obj;
68 }
69 
70 template< typename T >
71 std::vector< T > read_vector_from( std::string const & filename, size_t size_x = 0, size_t size_y = 0)
72 {
73  std::fstream f = std::fstream( filename, std::ios::in | std::ios::binary );
74  if( !f )
75  throw std::runtime_error( "failed to read file:\n" + filename );
76  f.seekg( 0, f.end );
77  size_t cb = f.tellg();
78  f.seekg( 0, f.beg );
79  if( cb % sizeof( T ) )
80  throw std::runtime_error( "file size is not a multiple of data size" );
81  std::vector< T > vec( cb / sizeof( T ) );
82  f.read( (char *)vec.data(), cb );
83  f.close();
84  return vec;
85 }
86 
87 template<>
88 std::vector< std::vector<double> > read_vector_from(std::string const & filename, size_t size_x, size_t size_y)
89 {
90  std::vector< std::vector<double> > res;
91 
92  std::fstream f = std::fstream(filename, std::ios::in | std::ios::binary);
93  if (!f)
94  throw std::runtime_error("failed to read file:\n" + filename);
95  f.seekg(0, f.end);
96  size_t cb = f.tellg();
97  f.seekg(0, f.beg);
98  if (cb % sizeof(double))
99  throw std::runtime_error("file size is not a multiple of data size");
100 
101  res.resize(size_x);
102 
103  for (size_t i = 0; i < size_x; i++)
104  {
105  res[i].resize(size_y);
106  f.read((char *)res[i].data(), size_y * sizeof(double));
107  }
108  f.close();
109  //for(auto i=0;i<)
110  return res;
111 
112  /*std::fstream f = std::fstream(filename, std::ios::in | std::ios::binary);
113  if (!f)
114  throw std::runtime_error("failed to read file:\n" + filename);
115  f.seekg(0, f.end);
116  size_t cb = f.tellg();
117  f.seekg(0, f.beg);
118  if (cb % sizeof(T))
119  throw std::runtime_error("file size is not a multiple of data size");
120  std::vector< T > vec(cb / sizeof(T));
121  f.read((char *)vec.data(), cb);
122  f.close();
123  return vec;*/
124 }
125 template < class T >
126 std::vector< T > read_image_file( std::string const &file, size_t width, size_t height )
127 {
128  std::ifstream f;
129  f.open( file, std::ios::in | std::ios::binary );
130  if( !f.good() )
131  throw std::runtime_error( "invalid file: " + file );
132  f.seekg( 0, f.end );
133  size_t cb = f.tellg();
134  f.seekg( 0, f.beg );
135  if( cb != sizeof( T ) * width * height )
136  throw std::runtime_error( librealsense::to_string()
137  << "file size (" << cb << ") does not match expected size (" << sizeof( T ) * width * height << "): " << file );
138  std::vector< T > data( width * height );
139  f.read( (char *)data.data(), width * height * sizeof( T ) );
140  return data;
141 }
142 
143 template < typename T >
144 void dump_vec( std::vector< double > const & cpp, std::vector< T > const & matlab,
145  char const * basename,
146  size_t width, size_t height
147 )
148 {
149  std::string filename = basename;
150  filename += ".dump";
151 #if 0
152  std::fstream f = std::fstream( filename, std::ios::out );
153  if( !f )
154  throw std::runtime_error( "failed to write file:\n" + filename );
155  for( size_t x = )
156  std::vector< T > vec( cb / sizeof( T ) );
157  f.read( (char*)vec.data(), cb );
158  f.close();
159  return vec;
160 #endif
161 }
162 
163 // This metadata describes what Matlab did: the files it used (sometimes there are more) and the
164 // iteration data/results that we need for comparison
166 {
167  //uint64_t n_iterations; // how many steps through optimization, and how many iteration file sets
168  uint64_t n_cycles; // how many cycles of optimization
169  double correction_in_pixels; // XY movement
170  uint64_t n_edges; // strong edges, i.e. after suppression
177  std::string rgb_prev_file; // TODO: looks like these need to be turned around!!!
181 
182  scene_metadata( std::string const &scene_dir )
183  {
184  std::ifstream( join( bin_dir( scene_dir ), "yuy_prev_z_i.files" ) ) >> rgb_file
185  >> rgb_prev_file >> z_file >> ir_file >> rgb_prev_valid_file;
186  if( rgb_file.empty() )
187  throw std::runtime_error( "failed to read file:\n" + bin_dir( scene_dir ) + "yuy_prev_z_i.files" );
188  if( ir_file.empty() )
189  throw std::runtime_error( "not enough files in:\n" + bin_dir( scene_dir ) + "yuy_prev_z_i.files" );
190 
191  std::string metadata = join( bin_dir( scene_dir ), "metadata" );
192  std::fstream f = std::fstream( metadata, std::ios::in | std::ios::binary );
193  if( !f )
194  throw std::runtime_error( "failed to read file:\n" + metadata );
195  f.read( (char *)&correction_in_pixels, sizeof( correction_in_pixels ) );
196  f.read( (char *)&n_edges, sizeof( n_edges ) );
197  f.read( (char *)&n_valid_ir_edges, sizeof( n_valid_ir_edges ) );
198  f.read( (char *)&n_valid_pixels, sizeof( n_valid_pixels ) );
199  f.read((char *)&n_relevant_pixels, sizeof(n_relevant_pixels));
200  f.read( (char *)&n_cycles, sizeof( n_cycles ) );
201  char b;
202  f.read( &b, 1 );
203  is_scene_valid = b != 0;
204  f.read( &b, 1 );
205  is_output_valid = b != 0;
206  f.close();
207  }
208 };
209 
210 
211 // Encapsulate the calibration information for a specific camera
212 // All the sample images we use are usually from the same camera. I.e., their
213 // intrinsics & extrinsics are the same and can be reused via this structure
215 {
222  float z_units = 0.25;
223 };
224 
226 {
227  struct params_bin
228  {
229  // Some units are supposed to be int but we made matlab write out doubles....
230  double depth_width;
231  double depth_height;
232  double depth_units;
234  double rgb_width;
235  double rgb_height;
237  double coeffs[5];
238  double matrix_3x3[9];
239  double translation[3];
240  double p_mat[12];
241  };
242 
243  params_bin param;
244  read_data_from( join( bin_dir( scene_dir ), filename ), &param );
245 
246  double coeffs[5] = { 0 };
247  camera_params ci;
248  ci.rgb =
249  {
250  int( param.rgb_width ), int( param.rgb_height ),
251  param.k_rgb,
253  param.coeffs
254  };
255  ci.z =
256  {
257  int( param.depth_width ), int( param.depth_height ),
258  param.k_depth,
259  RS2_DISTORTION_NONE, coeffs
260  };
261  ci.extrinsics =
262  {
263  { param.matrix_3x3[0], param.matrix_3x3[1], param.matrix_3x3[2],
264  param.matrix_3x3[3], param.matrix_3x3[4], param.matrix_3x3[5],
265  param.matrix_3x3[6], param.matrix_3x3[7], param.matrix_3x3[8] },
266  { param.translation[0], param.translation[1], param.translation[2] }
267  };
268  return ci;
269 }
270 
272 {
276 };
277 
279 {
280  dsm_params res;
281 
282 #pragma pack(push, 1)
283  struct algo_calibration
284  {
285  uint8_t fovexExistenceFlag;
286  float fovexNominal[4];
287  float laserangleH;
288  float laserangleV;
289  float xfov[5];
290  float yfov[5];
291  float polyVars[3];
292  float undistAngHorz[4];
293  float pitchFixFactor;
294 
295  };
296 #pragma pack(pop)
297 
299  algo_calibration algo_calib;
300 
301  std::string dsmparams = join( bin_dir( scene_dir ), filename );
302  std::fstream f = std::fstream(dsmparams, std::ios::in | std::ios::binary );
303  if( !f )
304  throw std::runtime_error( "failed to read file:\n" + dsmparams);
305  f.read( (char *)&res.params, sizeof(rs2_dsm_params) );
306  f.read( (char *)&algo_calibration_registers,
308  f.read( (char *)&algo_calib, sizeof( algo_calibration ) );
309 
310  f.close();
311 
312  res.algo_calibration_registers = algo_calibration_registers;
313 
314 
315  res.regs.FRMWfovexExistenceFlag = algo_calib.fovexExistenceFlag;
316 
317  for (auto i = 0; i < 4; i++)
318  {
319  res.regs.FRMWfovexNominal[i] = algo_calib.fovexNominal[i];
320  res.regs.FRMWundistAngHorz[i] = algo_calib.undistAngHorz[i];
321  }
322 
323  res.regs.FRMWlaserangleH = algo_calib.laserangleH;
324  res.regs.FRMWlaserangleV = algo_calib.laserangleV;
325 
326  for (auto i = 0; i < 5; i++)
327  {
328  res.regs.FRMWxfov[i] = algo_calib.xfov[i];
329  res.regs.FRMWyfov[i] = algo_calib.yfov[i];
330  }
331 
332  for (auto i = 0; i < 3; i++)
333  {
334  res.regs.FRMWpolyVars[i] = algo_calib.polyVars[i];
335  }
336 
337  res.regs.FRMWpitchFixFactor = algo_calib.pitchFixFactor;
338 
339  return res;
340 }
GLboolean GLboolean GLboolean b
librealsense::algo::depth_to_rgb_calibration::algo_calibration_registers algo_calibration_registers
Definition: scene-data.h:274
bool is_output_valid
Definition: scene-data.h:175
std::string bin_file(std::string const &prefix, size_t cycle, size_t iteration, size_t w, size_t h, std::string const &suffix)
Definition: scene-data.h:19
std::string join(const std::string &base, const std::string &path)
Definition: filesystem.h:113
uint64_t n_valid_pixels
Definition: scene-data.h:171
librealsense::algo::depth_to_rgb_calibration::algo_calibration_info cal_info
Definition: scene-data.h:219
Video DSM (Digital Sync Module) parameters for calibration (same layout as in FW ac_depth_params) Thi...
Definition: rs_types.h:74
std::vector< T > read_image_file(std::string const &file, size_t width, size_t height)
Definition: scene-data.h:126
uint64_t n_relevant_pixels
Definition: scene-data.h:172
GLdouble GLdouble GLdouble w
GLsizei const GLchar *const * string
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
std::string rgb_prev_file
Definition: scene-data.h:177
unsigned char uint8_t
Definition: stdint.h:78
GLhandleARB obj
Definition: glext.h:4157
std::string z_file
Definition: scene-data.h:180
librealsense::algo::depth_to_rgb_calibration::algo_calibration_registers cal_regs
Definition: scene-data.h:220
std::vector< T > read_vector_from(std::string const &filename, size_t size_x=0, size_t size_y=0)
Definition: scene-data.h:71
GLint GLenum GLint const GLfloat * coeffs
Definition: glext.h:10577
Cross-stream extrinsics: encodes the topology describing how the different devices are oriented...
Definition: calibration.h:67
librealsense::algo::depth_to_rgb_calibration::rs2_intrinsics_double z
Definition: scene-data.h:217
GLdouble f
std::string rgb_file
Definition: scene-data.h:176
GLdouble x
std::string ir_file
Definition: scene-data.h:179
T read_from(std::string const &filename)
Definition: scene-data.h:54
GLint GLsizei GLsizei height
std::string rgb_prev_valid_file
Definition: scene-data.h:178
uint64_t n_edges
Definition: scene-data.h:170
unsigned __int64 uint64_t
Definition: stdint.h:90
uint64_t n_valid_ir_edges
Definition: scene-data.h:173
rs2_dsm_params params
Definition: scene-data.h:273
librealsense::algo::depth_to_rgb_calibration::rs2_extrinsics_double extrinsics
Definition: scene-data.h:221
camera_params read_camera_params(std::string const &scene_dir, std::string const &filename)
Definition: scene-data.h:225
rs2_dsm_params dsm_params
Definition: scene-data.h:218
::realsense_legacy_msgs::metadata_< std::allocator< void > > metadata
GLuint in
Definition: glext.h:8859
bool is_scene_valid
Definition: scene-data.h:174
GLenum GLfloat param
librealsense::algo::depth_to_rgb_calibration::rs2_intrinsics_double rgb
Definition: scene-data.h:216
const GLuint GLenum const void * binary
Definition: glext.h:1882
void dump_vec(std::vector< double > const &cpp, std::vector< T > const &matlab, char const *basename, size_t width, size_t height)
Definition: scene-data.h:144
std::string bin_dir(std::string const &scene_dir)
Definition: scene-data.h:13
int i
GLuint res
Definition: glext.h:8856
uint64_t n_cycles
Definition: scene-data.h:168
scene_metadata(std::string const &scene_dir)
Definition: scene-data.h:182
dsm_params read_dsm_params(std::string const &scene_dir, std::string const &filename)
Definition: scene-data.h:278
GLboolean * data
double correction_in_pixels
Definition: scene-data.h:169
Definition: parser.hpp:150
librealsense::algo::depth_to_rgb_calibration::algo_calibration_info regs
Definition: scene-data.h:275
GLint GLsizei width
std::string to_string(T value)
void read_data_from(std::string const &filename, T *data)
Definition: scene-data.h:37


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