algo/depth-to-rgb-calibration/utils.cpp
Go to the documentation of this file.
1 
4 #include "calibration.h"
5 #include "debug.h"
6 #include "utils.h"
7 #include <math.h>
8 
9 namespace librealsense {
10 namespace algo {
11 namespace depth_to_rgb_calibration {
12 
13  void write_to_file( void const * data, size_t cb, std::string const & dir, char const * filename )
14  {
15  std::string path = dir + filename;
16  std::fstream f( path, std::ios::out | std::ios::binary );
17  if( ! f )
18  throw std::runtime_error( "failed to open file:\n" + path );
19  f.write( (char const *)data, cb );
20  f.close();
21  }
22 
23  double get_norma( const std::vector< double3 > & vec )
24  {
25  double sum = 0;
26  std::for_each( vec.begin(), vec.end(), [&]( double3 const & v ) { sum += v.get_norm(); } );
27  return std::sqrt(sum);
28  }
29 
30  double rad_to_deg(double rad)
31  {
32  return rad * 180. / M_PI;
33  }
34 
35  double deg_to_rad(double deg)
36  {
37  return deg * M_PI / 180.;
38  }
39 
40  void direct_inv_6x6(const double A[36], const double B[6], double C[6])
41  {
42  double b_A[36];
43  int i0;
44  int j;
45  signed char ipiv[6];
46  int iy;
47  int b;
48  int jj;
49  int jy;
50  int jp1j;
51  int n;
52  int k;
53  double smax;
54  int ix;
55  double s;
56 
57  /* INV_MY Summary of this function goes here */
58  /* Detailed explanation goes here */
59  memcpy(&b_A[0], &A[0], 36U * sizeof(double));
60  for (i0 = 0; i0 < 6; i0++) {
61  ipiv[i0] = (signed char)(1 + i0);
62  }
63 
64  for (j = 0; j < 5; j++) {
65  b = j * 7;
66  jj = j * 7;
67  jp1j = b + 2;
68  n = 6 - j;
69  jy = 0;
70  ix = b;
71  smax = std::abs(b_A[b]);
72  for (k = 2; k <= n; k++) {
73  ix++;
74  s = std::abs(b_A[ix]);
75  if (s > smax) {
76  jy = k - 1;
77  smax = s;
78  }
79  }
80 
81  if (b_A[jj + jy] != 0.0) {
82  if (jy != 0) {
83  iy = j + jy;
84  ipiv[j] = (signed char)(iy + 1);
85  ix = j;
86  for (k = 0; k < 6; k++) {
87  smax = b_A[ix];
88  b_A[ix] = b_A[iy];
89  b_A[iy] = smax;
90  ix += 6;
91  iy += 6;
92  }
93  }
94 
95  i0 = (jj - j) + 6;
96  for (iy = jp1j; iy <= i0; iy++) {
97  b_A[iy - 1] /= b_A[jj];
98  }
99  }
100 
101  n = 4 - j;
102  jy = b + 6;
103  iy = jj;
104  for (b = 0; b <= n; b++) {
105  smax = b_A[jy];
106  if (b_A[jy] != 0.0) {
107  ix = jj + 1;
108  i0 = iy + 8;
109  jp1j = (iy - j) + 12;
110  for (k = i0; k <= jp1j; k++) {
111  b_A[k - 1] += b_A[ix] * -smax;
112  ix++;
113  }
114  }
115 
116  jy += 6;
117  iy += 6;
118  }
119  }
120 
121  for (iy = 0; iy < 6; iy++) {
122  C[iy] = B[iy];
123  }
124 
125  for (jy = 0; jy < 5; jy++) {
126  if (ipiv[jy] != jy + 1) {
127  smax = C[jy];
128  iy = ipiv[jy] - 1;
129  C[jy] = C[iy];
130  C[iy] = smax;
131  }
132  }
133 
134  for (k = 0; k < 6; k++) {
135  jy = 6 * k;
136  if (C[k] != 0.0) {
137  i0 = k + 2;
138  for (iy = i0; iy < 7; iy++) {
139  C[iy - 1] -= C[k] * b_A[(iy + jy) - 1];
140  }
141  }
142  }
143 
144  for (k = 5; k >= 0; k--) {
145  jy = 6 * k;
146  if (C[k] != 0.0) {
147  C[k] /= b_A[k + jy];
148  for (iy = 0; iy < k; iy++) {
149  C[iy] -= C[k] * b_A[iy + jy];
150  }
151  }
152  }
153  }
154 
155  void direct_inv_2x2(const double A[4], const double B[2], double C[2])
156  {
157  int r1;
158  int r2;
159  double a21;
160  double C_tmp;
161 
162  /* INV_MY Summary of this function goes here */
163  /* Detailed explanation goes here */
164  if (std::abs(A[1]) > std::abs(A[0])) {
165  r1 = 1;
166  r2 = 0;
167  }
168  else {
169  r1 = 0;
170  r2 = 1;
171  }
172 
173  a21 = A[r2] / A[r1];
174  C_tmp = A[2 + r1];
175  C[1] = (B[r2] - B[r1] * a21) / (A[2 + r2] - a21 * C_tmp);
176  C[0] = (B[r1] - C[1] * C_tmp) / A[r1];
177  }
178 
179  void ndgrid_my(const double vec1[5], const double vec2[5], double yScalingGrid
180  [25], double xScalingGrid[25])
181  {
182  int k;
183  int b_k;
184 
185  /* NDGRID_MY Summary of this function goes here */
186  /* Detailed explanation goes here */
187  for (k = 0; k < 5; k++) {
188  for (b_k = 0; b_k < 5; b_k++) {
189  yScalingGrid[b_k + 5 * k] = vec1[b_k];
190  }
191  }
192 
193  for (k = 0; k < 5; k++) {
194  for (b_k = 0; b_k < 5; b_k++) {
195  xScalingGrid[b_k + 5 * k] = vec2[k];
196  }
197  }
198 
199  /* search around last estimated scaling */
200  }
201 
202  void inv(const double x[9], double y[9])
203  {
204  double b_x[9];
205  int p1;
206  int p2;
207  int p3;
208  double absx11;
209  double absx21;
210  double absx31;
211  int itmp;
212  memcpy(&b_x[0], &x[0], 9U * sizeof(double));
213  p1 = 0;
214  p2 = 3;
215  p3 = 6;
216  absx11 = std::abs(x[0]);
217  absx21 = std::abs(x[1]);
218  absx31 = std::abs(x[2]);
219  if ((absx21 > absx11) && (absx21 > absx31)) {
220  p1 = 3;
221  p2 = 0;
222  b_x[0] = x[1];
223  b_x[1] = x[0];
224  b_x[3] = x[4];
225  b_x[4] = x[3];
226  b_x[6] = x[7];
227  b_x[7] = x[6];
228  }
229  else {
230  if (absx31 > absx11) {
231  p1 = 6;
232  p3 = 0;
233  b_x[0] = x[2];
234  b_x[2] = x[0];
235  b_x[3] = x[5];
236  b_x[5] = x[3];
237  b_x[6] = x[8];
238  b_x[8] = x[6];
239  }
240  }
241 
242  b_x[1] /= b_x[0];
243  b_x[2] /= b_x[0];
244  b_x[4] -= b_x[1] * b_x[3];
245  b_x[5] -= b_x[2] * b_x[3];
246  b_x[7] -= b_x[1] * b_x[6];
247  b_x[8] -= b_x[2] * b_x[6];
248  if (std::abs(b_x[5]) > std::abs(b_x[4])) {
249  itmp = p2;
250  p2 = p3;
251  p3 = itmp;
252  absx11 = b_x[1];
253  b_x[1] = b_x[2];
254  b_x[2] = absx11;
255  absx11 = b_x[4];
256  b_x[4] = b_x[5];
257  b_x[5] = absx11;
258  absx11 = b_x[7];
259  b_x[7] = b_x[8];
260  b_x[8] = absx11;
261  }
262 
263  b_x[5] /= b_x[4];
264  b_x[8] -= b_x[5] * b_x[7];
265  absx11 = (b_x[5] * b_x[1] - b_x[2]) / b_x[8];
266  absx21 = -(b_x[1] + b_x[7] * absx11) / b_x[4];
267  y[p1] = ((1.0 - b_x[3] * absx21) - b_x[6] * absx11) / b_x[0];
268  y[p1 + 1] = absx21;
269  y[p1 + 2] = absx11;
270  absx11 = -b_x[5] / b_x[8];
271  absx21 = (1.0 - b_x[7] * absx11) / b_x[4];
272  y[p2] = -(b_x[3] * absx21 + b_x[6] * absx11) / b_x[0];
273  y[p2 + 1] = absx21;
274  y[p2 + 2] = absx11;
275  absx11 = 1.0 / b_x[8];
276  absx21 = -b_x[7] * absx11 / b_x[4];
277  y[p3] = -(b_x[3] * absx21 + b_x[6] * absx11) / b_x[0];
278  y[p3 + 1] = absx21;
279  y[p3 + 2] = absx11;
280  }
281 
282  void transpose(const double x[9], double y[9])
283  {
284  for (auto i = 0; i < 3; i++)
285  for (auto j = 0; j < 3; j++)
286  y[i * 3 + j] = x[j * 3 + i];
287  }
288 
290  {
291  /* ROT Summary of this function goes here */
292  /* Detailed explanation goes here */
293  for (int j = 0; j < int(w); j++) {
294  for (int i = 0; i < int(h); i++) {
295  B[i + h * j] = A[(h * (w - 1 - j) - i) + h - 1];
296  }
297  }
298  }
299 
300  std::vector< double > interp1( const std::vector< double > & ind,
301  const std::vector< double > & vals,
302  const std::vector< double > & intrp )
303  {
304  std::vector<double> res(intrp.size(), 0);
305 
306  for (auto i = 0; i < intrp.size(); i++)
307  {
308  auto value = intrp[i];
309  auto it = std::lower_bound(ind.begin(), ind.end(), value);
310  if (it == ind.begin())
311  {
312  if (*it == ind.front())
313  res[i] = ind.front();
314  else
315  res[i] = std::numeric_limits<double>::max();
316  }
317  else if (it == ind.end())
318  {
319  if (*it == ind.back())
320  res[i] = ind.back();
321  else
322  res[i] = std::numeric_limits<double>::max();
323  }
324  else
325  {
326  auto val1 = *(--it);
327  auto ind1 = std::distance(ind.begin(), it);
328  auto val2= *(++it);
329  auto ind2 = std::distance(ind.begin(), it);
330 
331  auto target_val1 = vals[ind1];
332  auto target_val2 = vals[ind2];
333 
334  res[i] = ((val2 - value) / (val2 - val1))*target_val1 + ((value - val1) / (val2 - val1))*target_val2;
335  }
336  }
337  return res;
338  }
339 
340 
342  {
343  double3x3 res = { 0 };
344 
345  for( auto i = 0; i < 3; i++ )
346  {
347  for( auto j = 0; j <= i; j++ )
348  {
349  double sum = 0;
350 
351  if( i == j )
352  {
353  for( auto l = 0; l < i; l++ )
354  {
355  sum += res.mat[i][l] * res.mat[i][l];
356  }
357  res.mat[i][i] = sqrt( mat.mat[i][j] - sum );
358  }
359  else
360  {
361  for( auto l = 0; l < j; l++ )
362  {
363  sum += res.mat[i][l] * res.mat[j][l];
364  }
365  res.mat[i][j] = ( mat.mat[i][j] - sum ) / res.mat[j][j];
366  }
367  }
368  }
369  return res;
370  }
371 
372 }
373 }
374 }
GLboolean GLboolean GLboolean b
GLint y
void direct_inv_2x2(const double A[4], const double B[2], double C[2])
void write_to_file(void const *data, size_t cb, std::string const &dir, char const *filename)
GLdouble s
GLsizei const GLchar *const * path
Definition: glext.h:4276
GLfloat value
GLdouble GLdouble GLdouble w
GLsizei const GLchar *const * string
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
GLdouble n
Definition: glext.h:1966
unsigned char uint8_t
Definition: stdint.h:78
std::vector< double > interp1(const std::vector< double > &ind, const std::vector< double > &vals, const std::vector< double > &intrp)
GLsizei GLsizei GLfloat distance
Definition: glext.h:10563
GLdouble f
GLdouble x
unsigned int uint32_t
Definition: stdint.h:80
GLint j
void rotate_180(const uint8_t *A, uint8_t *B, uint32_t w, uint32_t h)
static auto it
const GLuint GLenum const void * binary
Definition: glext.h:1882
int i
GLuint res
Definition: glext.h:8856
void direct_inv_6x6(const double A[36], const double B[6], double C[6])
GLdouble v
void ndgrid_my(const double vec1[5], const double vec2[5], double yScalingGrid[25], double xScalingGrid[25])
Definition: parser.hpp:150


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