test-reproduction.cpp
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 //#cmake:add-file ../../../src/algo/depth-to-rgb-calibration/*.cpp
5 //#cmake:add-file ../../../src/algo/thermal-loop/*.cpp
6 
7 //#test:flag custom-args # disable passing in of catch2 arguments
8 
9 // We have our own main
10 #define NO_CATCH_CONFIG_MAIN
11 //#define CATCH_CONFIG_RUNNER
12 
13 #define DISABLE_LOG_TO_STDOUT
14 #include "d2rgb-common.h"
15 
16 
17 template< typename T >
18 void read_binary_file( char const * dir, char const * bin, T * data )
19 {
20  std::string filename = join( dir, bin );
21  AC_LOG( DEBUG, "... " << filename );
22  std::fstream f = std::fstream( filename, std::ios::in | std::ios::binary );
23  if( ! f )
24  throw std::runtime_error( "failed to read file:\n" + filename );
25  f.seekg( 0, f.end );
26  size_t cb = f.tellg();
27  f.seekg( 0, f.beg );
28  if( cb != sizeof( T ) )
29  throw std::runtime_error( to_string()
30  << "file size (" << cb << ") does not match data size (" << sizeof(T) << "): " << filename );
31  std::vector< T > vec( cb / sizeof( T ));
32  f.read( (char*) data, cb );
33  f.close();
34 }
35 
37 {
40  double __fx, __fy, __ppx, __ppy; // not in new calib!
42  int width, height;
44  double coeffs[5];
45 
46  operator algo::calib() const
47  {
48  algo::calib c;
49  c.rot = rot;
50  c.trans = trans;
51  c.k_mat = k_mat;
52  c.width = width;
53  c.height = height;
54  c.model = model;
55  for( auto x = 0; x < 5; ++x )
56  c.coeffs[x] = coeffs[x];
57  return c;
58  }
59 };
60 
61 
63 {
64  typedef ac_logger super;
65 
67 
68 public:
70 
71  std::string const & get_codes() const { return _codes; }
72  void reset() { _codes.clear(); }
73 
74 protected:
75  void on_log( char severity, char const * message ) override
76  {
77  super::on_log( severity, message );
78 
79  // parse it for keyword
80  auto cch = strlen( message );
81  if( cch < 4 || message[cch - 1] != ']' )
82  return;
83  char const * end = message + cch - 1;
84  char const * start = end - 1;
85  while( *start != '[' )
86  {
87  if( *start == ' ' )
88  return;
89  if( --start == message )
90  return;
91  }
92  if( start[-1] != ' ' )
93  return;
94  // parse it for parameters
95  char const * param = message;
97  while( param < start )
98  {
99  if( *param == '{' && ++param < start )
100  {
101  char const * param_end = param;
102  while( *param_end != '}' && param_end < start )
103  ++param_end;
104  if( param_end == start || param_end - param < 1 )
105  break;
106  char const * value = param_end + 1;
107  char const * value_end = value;
108  while( *value_end != ' ' )
109  ++value_end;
110  if( value_end == value )
111  break;
112  if( value_end - value > 1 && strchr( ".;", value_end[-1] ))
113  --value_end;
114  std::string param_name( param, param_end - param );
115  std::string param_value( value, value_end - value );
116  if( ! values.empty() )
117  values += ' ';
118  values += param_name;
119  values += '=';
120  values += param_value;
121  param = value_end;
122  }
123  ++param;
124  }
125  std::string code( start + 1, end - start - 1 );
126  if( ! values.empty() )
127  code += '[' + values + ']';
128  if( ! _codes.empty() )
129  _codes += ' ';
130  _codes += code;
131  }
132 };
133 
134 
135 int main( int argc, char * argv[] )
136 {
137  custom_ac_logger logger;
138 
139  bool ok = true;
140  bool debug_mode = false;
141  // Each of the arguments is the path to a directory to simulate
142  // We skip argv[0] which is the path to the executable
143  // We don't complain if no arguments -- that's how we'll run as part of unit-testing
144  for( int i = 1; i < argc; ++i )
145  {
146  try
147  {
148  char const * dir = argv[i];
149  if( !strcmp( dir, "--version" ) )
150  {
151  // The build number is only available within Jenkins and so we have to hard-
152  // code it ><
153  std::cout << RS2_API_VERSION_STR << ".2158" << std::endl;
154  continue;
155  }
156  if( ! strcmp( dir, "--debug" ) || ! strcmp( dir, "-d" ) )
157  {
158  debug_mode = true;
159  continue;
160  }
161  std::cout << "Processing: " << dir << " ..." << std::endl;
162 
163  algo::calib calibration;
164  try
165  {
166  read_binary_file( dir, "rgb.calib", &calibration );
167  }
168  catch( std::exception const & e )
169  {
170  std::cout << "!! failed: " << e.what() << std::endl;
171  old_algo_calib old_calibration;
172  read_binary_file( dir, "rgb.calib", &old_calibration );
173  calibration = old_calibration;
174  }
175 
177  camera.rgb = calibration.get_intrinsics();
178  camera.extrinsics = calibration.get_extrinsics();
179  algo::rs2_intrinsics_double d_intr; // intrinsics written in double!
180  read_binary_file( dir, "depth.intrinsics", &d_intr );
181  camera.z = d_intr;
182  read_binary_file( dir, "depth.units", &camera.z_units );
183  read_binary_file( dir, "cal.info", &camera.cal_info );
184  read_binary_file( dir, "cal.registers", &camera.cal_regs );
185  read_binary_file( dir, "dsm.params", &camera.dsm_params );
186 
187  if( camera.cal_regs.EXTLdsmXoffset < 0. || camera.cal_regs.EXTLdsmXoffset > 100000.
188  || camera.cal_regs.EXTLdsmXscale < 0. || camera.cal_regs.EXTLdsmXscale > 100000.
189  || camera.cal_regs.EXTLdsmYoffset < 0 || camera.cal_regs.EXTLdsmYoffset > 100000.
190  || camera.cal_regs.EXTLdsmYscale < 0 || camera.cal_regs.EXTLdsmYscale > 100000. )
191  {
192  throw std::invalid_argument( "cal.registers file is malformed! (hexdump -v -e '4/ \"%f \"')" );
193  }
194 
195  algo::optimizer::settings settings;
196  try
197  {
198  read_binary_file( dir, "settings", &settings );
199  }
200  catch( std::exception const & e )
201  {
202  std::cout << "!! failed: " << e.what() << " -> assuming [MANUAL LONG 9 @40degC]" << std::endl;
203  settings.digital_gain = RS2_DIGITAL_GAIN_HIGH;
204  settings.hum_temp = 40;
205  settings.is_manual_trigger = true;
206  settings.receiver_gain = 9;
207  }
208 
209  // If both the raw intrinsics (pre-thermal) and the thermal table exist, apply a thermal
210  // manipulation. Otherwise just take the final intrinsics from rgb.calib.
211  try
212  {
213  rs2_intrinsics raw_rgb_intr;
214  read_binary_file( dir, "raw_rgb.intrinsics", &raw_rgb_intr );
215 
216  auto vec = read_vector_from< byte >( join( dir, "rgb_thermal_table" ) );
217  thermal::l500::thermal_calibration_table thermal_table( vec );
218 
219  auto scale = thermal_table.get_thermal_scale( settings.hum_temp );
220  AC_LOG( DEBUG, "Thermal {scale}" << scale << " [TH]" );
221  raw_rgb_intr.fx = float( raw_rgb_intr.fx * scale );
222  raw_rgb_intr.fy = float( raw_rgb_intr.fy * scale );
223  camera.rgb = raw_rgb_intr;
224  }
225  catch( std::exception const & )
226  {
227  AC_LOG( ERROR, "Could not read raw_rgb.intrinsics or rgb_thermal_table; using rgb.calib [NO-THERMAL]" );
228  }
229 
230  algo::optimizer cal( settings, debug_mode );
232 
233  {
234  memory_profiler profiler;
235  init_algo( cal,
236  dir,
237  "rgb.raw",
238  "rgb_prev.raw",
239  "rgb_last_successful.raw",
240  "ir.raw",
241  "depth.raw",
242  camera,
243  &profiler );
244 
245  status = logger.get_codes();
246  logger.reset();
247 
248  profiler.section( "is_scene_valid" );
249  if( ! cal.is_scene_valid() )
250  {
251  TRACE( "-E- SCENE_INVALID" );
252  if( ! status.empty() )
253  status += ' ';
254  status += "SCENE_INVALID";
255  }
256  profiler.section_end();
257 
258  profiler.section( "optimize" );
259  size_t n_iteration = cal.optimize();
260  profiler.section_end();
261 
262  std::string results;
263  profiler.section( "is_valid_results" );
264  if( ! cal.is_valid_results() )
265  {
266  TRACE( "NOT VALID\n" );
267  results = "BAD_RESULT";
268  }
269  else
270  {
271  results = "SUCCESSFUL";
272  }
273  profiler.section_end();
274 
275  if( ! logger.get_codes().empty() )
276  {
277  if( ! status.empty() )
278  status += ' ';
279  status += logger.get_codes();
280  logger.reset();
281  }
282  if( ! status.empty() )
283  status += ' ';
284  status += results;
285  }
286 
287  TRACE( "\n___\nRESULTS: (" << RS2_API_VERSION_STR << " build 2158)" );
288 
289  auto intr = cal.get_calibration().get_intrinsics();
290  intr.model = RS2_DISTORTION_INVERSE_BROWN_CONRADY; // restore LRS model
291  auto extr = cal.get_calibration().get_extrinsics();
292  AC_LOG( DEBUG, AC_D_PREC << "intr" << (rs2_intrinsics)intr );
293  AC_LOG( DEBUG, AC_D_PREC << "extr" << (rs2_extrinsics)extr );
294  AC_LOG( DEBUG, AC_D_PREC << "dsm" << cal.get_dsm_params() );
295 
296  try
297  {
299  }
300  catch( librealsense::invalid_value_exception const & e )
301  {
302  AC_LOG( ERROR, "Exception: " << e.what() );
303  if( ! status.empty() )
304  status += ' ';
305  status += logger.get_codes();
306  logger.reset();
307  }
308 
309  TRACE( "\n___\nVS:" );
310  AC_LOG( DEBUG, AC_D_PREC << "intr" << (rs2_intrinsics)calibration.get_intrinsics() );
311  AC_LOG( DEBUG, AC_D_PREC << "extr" << (rs2_extrinsics)calibration.get_extrinsics() );
312  AC_LOG( DEBUG, AC_D_PREC << "dsm" << camera.dsm_params );
313 
314  TRACE( "\n___\nSTATUS: " + status );
315  }
316  catch( std::exception const & e )
317  {
318  std::cerr << "\n___\ncaught exception: " << e.what() << std::endl;
319  ok = false;
320  }
321  catch( ... )
322  {
323  std::cerr << "\n___\ncaught unknown exception!" << std::endl;
324  ok = false;
325  }
326  }
327 
328  return ! ok;
329 }
330 
GLenum GLuint GLenum GLsizei const GLchar * message
GLuint GLuint end
rs2_extrinsics_double get_extrinsics() const
Definition: calibration.cpp:63
void on_log(char severity, char const *message) override
GLenum GLuint GLenum severity
std::string join(const std::string &base, const std::string &path)
Definition: filesystem.h:113
void section_end()
Definition: profiler.h:56
librealsense::algo::depth_to_rgb_calibration::algo_calibration_info cal_info
Definition: scene-data.h:219
rs2_distortion
Distortion model: defines how pixel coordinates should be mapped to sensor coordinates.
Definition: rs_types.h:45
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:10806
GLfloat value
rs2_intrinsics_double get_intrinsics() const
Definition: calibration.cpp:55
GLsizei const GLchar *const * string
e
Definition: rmse.py:177
status
Defines return codes that SDK interfaces use. Negative values indicate errors, a zero value indicates...
librealsense::algo::depth_to_rgb_calibration::algo_calibration_registers cal_regs
Definition: scene-data.h:220
GLint GLenum GLint const GLfloat * coeffs
Definition: glext.h:10577
size_t optimize(std::function< void(data_collect const &data) > iteration_callback=nullptr)
Definition: optimizer.cpp:1977
std::string const & get_codes() const
librealsense::algo::depth_to_rgb_calibration::rs2_intrinsics_double z
Definition: scene-data.h:217
GLdouble f
std::ostream & cout()
const GLubyte * c
Definition: glext.h:12690
algo::k_matrix k_mat
GLdouble x
algo::matrix_3x3 rot
GLuint start
void read_binary_file(char const *dir, char const *bin, T *data)
void init_algo(algo::optimizer &cal, std::string const &dir, std::string const &yuy, std::string const &yuy_prev, std::string const &yuy_last_successful, std::string const &ir, std::string const &z, camera_params const &camera, memory_profiler *profiler=nullptr)
Definition: d2rgb-common.h:31
librealsense::algo::depth_to_rgb_calibration::rs2_extrinsics_double extrinsics
Definition: scene-data.h:221
#define AC_LOG(TYPE, MSG)
void validate_dsm_params(struct rs2_dsm_params const &dsm_params)
GLsizei const GLfloat * values
Cross-stream extrinsics: encodes the topology describing how the different devices are oriented...
Definition: rs_sensor.h:96
rs2_distortion model
rs2_dsm_params dsm_params
Definition: scene-data.h:218
bool is_scene_valid(input_validity_data *data=nullptr)
void section(char const *heading)
Definition: profiler.h:49
GLuint in
Definition: glext.h:8859
const char * what() const noexceptoverride
Definition: src/types.h:284
rs2_extrinsics extr
Definition: test-pose.cpp:258
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
static const textual_icon camera
Definition: model-views.h:219
#define RS2_API_VERSION_STR
Definition: rs.h:43
Video stream intrinsics.
Definition: rs_types.h:58
std::ostream & cerr()
int i
algo::translation trans
int main(int argc, char *argv[])
Definition: parser.hpp:150
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:50:11