compare-to-bin-file.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 #include <type_traits>
7 
8 
9 //
10 // In all the following:
11 // F = the type in the bin files, i.e. the "golden" value that we compare to
12 // D = the type in memory, i.e. the result of our own computations
13 // Since approx compares approximately to a "golden", the golden needs to go to
14 // approx!
15 //
16 
17 
18 template< typename F, typename D >
19 bool is_equal_approximetly( F fx, D dx, bool print = true)
20 {
21  return dx == approx( fx );
22 }
23 
24 template< typename D>
25 bool compare_and_trace(D val_matlab, D val_cpp, std::string const & compared)
26 {
27  if (val_cpp != approx(val_matlab))
28  {
29  AC_LOG(DEBUG, "... " << std::setprecision(16) << compared << ": {matlab} " << val_matlab << " !~ " << val_cpp << " {cpp} Diff = " << val_matlab - val_cpp);
30  return false;
31  }
32  return true;
33 }
34 
35 template<>
36 bool is_equal_approximetly<algo::k_matrix, algo::k_matrix>( algo::k_matrix fx, algo::k_matrix dx, bool print)
37 {
38  bool ok = true;
39 
40  ok = compare_and_trace(dx.get_fx(), fx.get_fx(), "fx") && ok;
41  ok = compare_and_trace(dx.get_fy(), fx.get_fy(), "fy") && ok;
42  ok = compare_and_trace(dx.get_ppx(), fx.get_ppx(), "ppx") && ok;
43  ok = compare_and_trace(dx.get_ppy(), fx.get_ppy(), "ppy") && ok;
44 
45  return ok;
46 }
47 
48 template<>
49 bool is_equal_approximetly<algo::rotation_in_angles, algo::rotation_in_angles>( algo::rotation_in_angles fx, algo::rotation_in_angles dx, bool print)
50 {
51  return dx.alpha == approx( fx.alpha ) &&
52  dx.beta == approx( fx.beta ) &&
53  dx.gamma == approx( fx.gamma );
54 }
55 
56 template<>
57 bool is_equal_approximetly<algo::p_matrix, algo::p_matrix>( algo::p_matrix fx, algo::p_matrix dx, bool print)
58 {
59  bool ok = true;
60  for( auto i = 0; i < 12; i++ )
61  {
62  if (print)
63  ok = compare_and_trace(dx.vals[i], fx.vals[i], "p_matrix") && ok;
64  else
65  ok = is_equal_approximetly(dx.vals[i], fx.vals[i], false) && ok;
66 
67  }
68  return ok;
69 }
70 
71 template<>
72 bool is_equal_approximetly<std::vector<double>, std::vector<double>>(std::vector<double> fx, std::vector<double> dx, bool print)
73 {
74  if(fx.size() != dx.size())
75  return false;
76 
77  for (auto i = 0; i < fx.size(); i++)
78  {
79  if (dx[i] != approx(fx[i]))
80  return false;
81  }
82  return true;
83 }
84 
85 template<>
86 bool is_equal_approximetly<algo::double2, algo::double2>(algo::double2 f, algo::double2 d, bool print)
87 {
88  if (print)
89  {
90  bool ok = true;
91  ok &= compare_and_trace(d.x, f.x, "x");
92  ok &= compare_and_trace(d.y, f.y, "y");
93 
94  return ok;
95  }
96 
97  return d.x == approx(f.x) && d.y == approx(f.y);
98 }
99 
100 template<>
101 bool is_equal_approximetly<algo::double3, algo::double3>(algo::double3 f, algo::double3 d, bool print)
102 {
103  if (print)
104  {
105  bool ok = true;
106  ok &= compare_and_trace(d.x, f.x, "x");
107  ok &= compare_and_trace(d.y, f.y, "y");
108  ok &= compare_and_trace(d.z, f.z, "z");
109 
110  return ok;
111  }
112 
113  return d.x == approx(f.x) && d.y == approx(f.y) && d.z == approx(f.z);
114 }
115 
116 template<>
117 bool is_equal_approximetly<algo::algo_calibration_registers, algo::algo_calibration_registers>(algo::algo_calibration_registers f, algo::algo_calibration_registers d, bool print)
118 {
119  bool ok = true;
120 
121  ok &= compare_and_trace(d.EXTLdsmXoffset, f.EXTLdsmXoffset, "Xoffset");
122  ok &= compare_and_trace(d.EXTLdsmXscale, f.EXTLdsmXscale, "Xscale");
123  ok &= compare_and_trace(d.EXTLdsmYoffset, f.EXTLdsmYoffset, "Yoffset");
124  ok &= compare_and_trace(d.EXTLdsmYscale, f.EXTLdsmYscale, "Yscale");
125 
126  return ok;
127 }
128 
129 template<>
130 bool is_equal_approximetly<algo::los_shift_scaling, algo::los_shift_scaling>(algo::los_shift_scaling f, algo::los_shift_scaling d, bool print)
131 {
132  bool ok = true;
133 
134  ok &= compare_and_trace(d.los_scaling_x, f.los_scaling_x, "los_scaling_x");
135  ok &= compare_and_trace(d.los_scaling_y, f.los_scaling_y, "los_scaling_y");
136  ok &= compare_and_trace(d.los_shift_x, f.los_shift_x, "los_shift_x");
137  ok &= compare_and_trace(d.los_shift_y, f.los_shift_y, "los_shift_y");
138 
139  return ok;
140 }
141 template< typename F, typename D >
142 void print( size_t x, F f, D d, bool is_approx = false )
143 {
144  // bytes will be written to stdout as characters, which we never want... hence '+fx'
145  AC_LOG( DEBUG, "... " << AC_D_PREC << x << ": {matlab}" << +f << (is_approx ? " !~ " : " != ") << +d << "{c++}" );
146 }
147 
148 template<>
149 void print<algo::k_matrix, algo::k_matrix>( size_t x, algo::k_matrix f, algo::k_matrix d, bool is_approx )
150 {
151  // bytes will be written to stdout as characters, which we never want... hence '+fx'
152  AC_LOG( DEBUG, "... " << AC_D_PREC << std::fixed << x << ": {matlab}" << f.get_fx() << " " << f.get_fy() << " " << f.get_ppx() << " " << f.get_ppy() << (is_approx ? " !~ " : " != ")
153  << d.get_fx() << " " << d.get_fy() << " " << d.get_ppx() << " " << d.get_ppy() << "{c++}" );
154 }
155 
156 template<>
157 void print<algo::double2, algo::double2>( size_t x, algo::double2 f, algo::double2 d, bool is_approx )
158 {
159  // bytes will be written to stdout as characters, which we never want... hence '+fx'
160  AC_LOG( DEBUG, "... " << AC_D_PREC << std::fixed << x << ": {matlab}" << f.x << " " << f.y << (is_approx ? " !~ " : " != ")
161  << d.x << " " << d.y << "{c++}" );
162 }
163 
164 template<>
165 void print<algo::double3, algo::double3>( size_t x, algo::double3 f, algo::double3 d, bool is_approx )
166 {
167  // bytes will be written to stdout as characters, which we never want... hence '+fx'
168  AC_LOG( DEBUG, "... " << std::setprecision( 15 ) << std::fixed << x << ": {matlab}" << f.x << " " << f.y << " " << f.z << (is_approx ? " !~ " : " != ")
169  << d.x << " " << d.y << " " << d.z << "{c++}" );
170 }
171 
172 template<>
173 void print<algo::rotation_in_angles, algo::rotation_in_angles>( size_t x, algo::rotation_in_angles f, algo::rotation_in_angles d, bool is_approx )
174 {
175  // bytes will be written to stdout as characters, which we never want... hence '+fx'
176  AC_LOG( DEBUG, "... " << std::setprecision( 15 ) << x << ": {matlab}" << f.alpha << " " << f.beta << " " << f.gamma << (is_approx ? " !~ " : " != ")
177  << d.alpha << " " << d.beta << " " << d.gamma << "{c++}" );
178 }
179 
180 template<>
181 void print<algo::p_matrix, algo::p_matrix>( size_t x, algo::p_matrix f, algo::p_matrix d, bool is_approx )
182 {
183  std::ostringstream s;
184 
185  for( auto i = 0; i < 12; i++ )
186  {
187  if( !is_equal_approximetly( f.vals[i], d.vals[i] ) )
188  {
189  s << i << ": " << std::setprecision( 15 ) << std::fixed << ": {matlab}" << f.vals[i] << (is_approx ? " !~ " : " != ");
190  s << std::setprecision( 15 ) << "{c++}" << d.vals[i] << "\n";
191  }
192 
193  }
194 
195  AC_LOG( DEBUG, "... " << std::setprecision( 15 ) << std::fixed << x << " " << s.str() );
196 }
197 
198 template<>
199 void print<std::vector<double>, std::vector<double>>(size_t x, std::vector<double> f, std::vector<double> d, bool is_approx)
200 {
201  std::ostringstream s;
202 
203  for (auto i = 0; i < f.size(); i++)
204  {
205  if (!is_equal_approximetly(f[i], d[i]))
206  {
207  s << i << ": " << std::setprecision(15) << std::fixed << ": {matlab}" << f[i] << (is_approx ? " !~ " : " != ");
208  s << std::setprecision(15) << "{c++}" << d[i] << "\n";
209  }
210 
211  }
212 
213  AC_LOG(DEBUG, "... " << std::setprecision(15) << std::fixed << x << " " << s.str());
214 }
215 
216 template<
217  typename F, typename D,
218  typename std::enable_if< !std::numeric_limits< D >::is_exact && !std::is_enum< D >::value, int >::type = 0
219 >
220 bool compare_t( F f, D d , bool print = true)
221 {
222  return is_equal_approximetly( f, d, print);
223 }
224 
225 template< typename F, typename D,
226  typename std::enable_if< std::numeric_limits< D >::is_exact || std::is_enum< D >::value, int >::type = 0
227 >
228 bool compare_t( F f, D d, bool print = false)
229 {
230  return f == d;
231 }
232 
233 
234 template< typename F, typename D >
235 bool compare_same_vectors( std::vector< F > const & matlab, std::vector< D > const & cpp )
236 {
237  assert( matlab.size() == cpp.size() );
238  size_t n_mismatches = 0;
239  size_t size = matlab.size();
240  for( size_t x = 0; x < size; ++x )
241  {
242  F fx = matlab[x];
243  D dx = cpp[x];
244  if( !compare_t( fx, dx, false ) )
245  {
246  if( ++n_mismatches <= 5 )
248  }
249  }
250  if( n_mismatches )
251  AC_LOG( DEBUG, "... " << n_mismatches << " mismatched values of " << size );
252  return (n_mismatches == 0);
253 }
254 
255 template< typename F, typename D > // F=in bin; D=in memory
257  std::vector< D > const & vec,
258  std::string const & scene_dir,
259  std::string const & filename,
260  size_t width, size_t height,
261  size_t size,
262  bool( *compare_vectors )(std::vector< F > const &, std::vector< D > const &) = nullptr
263 )
264 {
265  TRACE( "Comparing " << filename << " ..." );
266  bool ok = true;
267  auto bin = read_vector_from< F >( join( bin_dir( scene_dir ), filename ), width, height );
268  if( bin.size() != size)
269  TRACE( filename << ": {matlab size}" << bin.size() << " != {width}" << width << "x" << height << "{height}" ), ok = false;
270  if( vec.size() != bin.size() )
271  TRACE( filename << ": {c++ size}" << vec.size() << " != " << bin.size() << "{matlab size}" ), ok = false;
272  else
273  {
274  auto v = vec;
275  auto b = bin;
276 
277  if( compare_vectors && !(*compare_vectors)(b, v) )
278  ok = false;
279  if( !ok )
280  {
281  //dump_vec( vec, bin, filename, width, height );
282  //AC_LOG( DEBUG, "... dump of file written to: " << filename << ".dump" );
283  }
284  }
285  return ok;
286 }
287 
288 template< typename F, typename D > // F=in bin; D=in memory
290  std::vector< D > const & vec,
291  std::string const & scene_dir,
292  std::string const & filename,
293  size_t width, size_t height,
294  bool(*compare_vectors)(std::vector< F > const &, std::vector< D > const &) = nullptr
295 )
296 {
297  return compare_to_bin_file(vec, scene_dir, filename, width, height, width*height, compare_vectors);
298 }
299 
300 template< typename F, typename D > // F=in bin; D=in memory
302  std::vector< D > const & vec,
303  std::string const & scene_dir,
304  const char * prefix,
305  size_t width, size_t height,
306  const char * suffix,
307  bool( *compare_vectors )(std::vector< F > const &, std::vector< D > const &) = nullptr
308 
309 )
310 {
311  return compare_to_bin_file< F, D >( vec,
312  scene_dir, bin_file( prefix, width, height, suffix ) + ".bin",
313  height, width,
314  compare_vectors);
315 }
316 
317 
319  algo::calib& calib,
320  double& cost,
321  std::string const & scene_dir,
322  std::string const & filename
323 )
324 {
325  auto data_size = sizeof( algo::matrix_3x3 ) +
326  sizeof( algo::translation ) +
327  sizeof( algo::matrix_3x3 ) +
328  sizeof( double ); // cost
329 
330  auto bin = read_vector_from< double >( join( bin_dir( scene_dir ), filename ) );
331  if( bin.size() * sizeof( double ) != data_size )
332  {
333  AC_LOG( DEBUG, "... " << filename << ": {matlab size}" << bin.size() * sizeof(double) << " != " << data_size );
334  return false;
335  }
336 
337  auto data = bin.data();
338 
340  data += sizeof( algo::matrix_3x3) / sizeof( double );
341  auto r = *(algo::matrix_3x3*)(data);
342  data += sizeof( algo::matrix_3x3 ) / sizeof( double );
343  auto t = *(algo::translation*)(data);
344  data += sizeof( algo::translation ) / sizeof( double );
345  cost = *(double*)(data);
346 
347  calib.k_mat = algo::k_matrix( k );
348  calib.rot = r;
349  calib.trans = t;
350 
351  return true;
352 }
353 
354 
355 bool compare_calib( algo::calib const & calib,
356  double cost,
357  algo::calib calib_from_file,
358  double cost_matlab )
359 {
360  auto intr_matlab = calib_from_file.get_intrinsics();
361  auto extr_matlab = calib_from_file.get_extrinsics();
362 
363  auto intr_cpp = calib.get_intrinsics();
364  auto extr_cpp = calib.get_extrinsics();
365 
366  bool ok = true;
367 
368  ok &= compare_and_trace( cost_matlab, cost, "cost" );
369 
370  ok &= compare_and_trace( intr_matlab.fx, intr_cpp.fx, "fx" );
371  ok &= compare_and_trace( intr_matlab.fy, intr_cpp.fy, "fy" );
372  ok &= compare_and_trace( intr_matlab.ppx, intr_cpp.ppx, "ppx" );
373  ok &= compare_and_trace( intr_matlab.ppy, intr_cpp.ppy, "ppy" );
374 
375  for( auto i = 0; i < 9; i++ )
376  ok &= compare_and_trace( extr_matlab.rotation[i], extr_cpp.rotation[i], "rotation[" + std::to_string( i ) + "]" );
377 
378  for( auto i = 0; i < 3; i++ )
379  ok &= compare_and_trace( extr_matlab.translation[i], extr_cpp.translation[i], "translation[" + std::to_string( i ) + "]" );
380 
381  return ok;
382 }
383 
384 
386 {
387  bool ok = true;
388 
389  ok &= compare_and_trace(first.EXTLdsmXoffset, second.EXTLdsmXoffset, "dsm_x_offset");
390  ok &= compare_and_trace(first.EXTLdsmYoffset, second.EXTLdsmYoffset, "dsm_y_offset");
391  ok &= compare_and_trace(first.EXTLdsmXscale, second.EXTLdsmXscale, "dsm_x_scale");
392  ok &= compare_and_trace(first.EXTLdsmYscale, second.EXTLdsmYscale, "dsm_y_scale");
393 
394  return ok;
395 }
396 
398 {
399  bool ok = true;
400 
401  ok &= compare_and_trace(first.los_scaling_x, second.los_scaling_x, "los_scaling_x");
402  ok &= compare_and_trace(first.los_scaling_y, second.los_scaling_y, "los_scaling_y");
403  ok &= compare_and_trace(first.los_shift_x, second.los_shift_x, "los_shift_x");
404  ok &= compare_and_trace(first.los_shift_y, second.los_shift_y, "los_shift_x");
405 
406  return ok;
407 }
408 template< typename D> // F=in bin; D=in memory
410  D const & obj_cpp,
411  std::string const & scene_dir,
412  std::string const & filename
413 )
414 {
415  TRACE("Comparing " << filename << " ...");
416  bool ok = true;
417  auto obj_matlab = read_from< D >( join( bin_dir( scene_dir ), filename ) );
418 
419  return compare_t(obj_matlab, obj_cpp);
420 }
421 
423  double hum_temp,
424  double & scale )
425 {
426  try
427  {
428  auto vec = read_vector_from< byte >(
429  join( bin_dir( dir ), "rgb_thermal_table" ) );
430 
431  std::vector<byte> thermal_vec( 16, 0 ); // table header
432  thermal_vec.insert( thermal_vec.end(), vec.begin(), vec.end() );
433  thermal::l500::thermal_calibration_table thermal_table( thermal_vec );
434  scale = thermal_table.get_thermal_scale( hum_temp );
435  return true;
436  }
437  catch( std::exception const & )
438  {
439  return false;
440  }
441 }
rs2_extrinsics_double get_extrinsics() const
Definition: calibration.cpp:63
GLboolean GLboolean GLboolean b
Approx approx(F f)
Definition: approx.h:106
bool compare_to_bin_file(std::vector< D > const &vec, std::string const &scene_dir, std::string const &filename, size_t width, size_t height, size_t size, bool(*compare_vectors)(std::vector< F > const &, std::vector< D > const &)=nullptr)
bool get_calib_and_cost_from_raw_data(algo::calib &calib, double &cost, std::string const &scene_dir, std::string const &filename)
void print(size_t x, F f, D d, bool is_approx=false)
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
GLdouble s
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
d
Definition: rmse.py:171
bool read_thermal_data(std::string dir, double hum_temp, double &scale)
bool compare_calib(algo::calib const &calib, double cost, algo::calib calib_from_file, double cost_matlab)
GLdouble t
GLdouble f
bool compare_same_vectors(std::vector< F > const &matlab, std::vector< D > const &cpp)
GLsizeiptr size
GLdouble GLdouble r
bool compare_t(F f, D d, bool print=true)
GLdouble x
GLint GLsizei GLsizei height
bool is_equal_approximetly(F fx, D dx, bool print=true)
GLint first
#define AC_LOG(TYPE, MSG)
bool compare_and_trace(D val_matlab, D val_cpp, std::string const &compared)
GLenum type
std::string bin_dir(std::string const &scene_dir)
Definition: scene-data.h:13
int i
#define D(...)
Definition: usbhost.c:33
GLdouble v
bool operator==(const algo::algo_calibration_registers &first, const algo::algo_calibration_registers &second)
Definition: parser.hpp:150
GLint GLsizei width
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:12