calibration.cpp
Go to the documentation of this file.
1 
4 #include "calibration.h"
5 #include "debug.h"
6 #include "utils.h"
7 
8 
10 
11 
13 {
14  auto const & r = extrin.rotation;
15  auto const & t = extrin.translation;
16  auto const & c = intrin.coeffs;
17 
18  height = intrin.height;
19  width = intrin.width;
20  rot = { r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8] };
21  trans = { t[0], t[1], t[2] };
22  k_mat = matrix_3x3{ intrin.fx, 0,intrin.ppx,
23  0,intrin.fy, intrin.ppy,
24  0,0,1 };
25 
26  coeffs[0] = c[0];
27  coeffs[1] = c[1];
28  coeffs[2] = c[2];
29  coeffs[3] = c[3];
30  coeffs[4] = c[4];
31  model = intrin.model;
32 }
33 
35 {
36  auto const & r = extrin.rotation;
37  auto const & t = extrin.translation;
38  auto const & c = intrin.coeffs;
39 
40  height = intrin.height;
41  width = intrin.width;
42  rot = { r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8] };
43  trans = { t[0], t[1], t[2] };
44  k_mat = matrix_3x3{ intrin.fx, 0,intrin.ppx,
45  0,intrin.fy, intrin.ppy,
46  0,0,1 };
47  coeffs[0] = c[0];
48  coeffs[1] = c[1];
49  coeffs[2] = c[2];
50  coeffs[3] = c[3];
51  coeffs[4] = c[4];
52  model = intrin.model;
53 }
54 
56 {
57  return {
58  width, height,
59  k_mat,
60  model, coeffs };
61 }
62 
64 {
65  auto & r = rot.rot;
66  auto & t = trans;
67  return {
68  { r[0] , r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8] },
69  { t.t1, t.t2, t.t3 }
70  };
71 }
72 
74 {
75  auto r = rot.rot;
76  auto t = trans;
77  auto k = k_mat.k_mat.rot;
78  /* auto fx = k_mat.fx;
79  auto fy = k_mat.fy;
80  auto ppx = k_mat.ppx;
81  auto ppy = k_mat.ppy;*/
82  p_matrix p_mat = {
83  k[0] * r[0] + k[1] * r[3] + k[2] * r[6],
84  k[0] * r[1] + k[1] * r[4] + k[2] * r[7],
85  k[0] * r[2] + k[1] * r[5] + k[2] * r[8],
86  k[0] * t.t1 + k[1] * t.t2 + k[2] * t.t3,
87 
88  k[3] * r[0] + k[4] * r[3] + k[5] * r[6],
89  k[3] * r[1] + k[4] * r[4] + k[5] * r[7],
90  k[3] * r[2] + k[4] * r[5] + k[5] * r[8],
91  k[3] * t.t1 + k[4] * t.t2 + k[5] * t.t3,
92 
93  r[6],
94  r[7],
95  r[8],
96  t.t3
97  };
98 
99  return p_mat;
100 }
101 
102 void calib::copy_coefs( calib & obj ) const
103 {
104  obj.width = this->width;
105  obj.height = this->height;
106 
107  obj.coeffs[0] = this->coeffs[0];
108  obj.coeffs[1] = this->coeffs[1];
109  obj.coeffs[2] = this->coeffs[2];
110  obj.coeffs[3] = this->coeffs[3];
111  obj.coeffs[4] = this->coeffs[4];
112 
113  obj.model = this->model;
114 }
115 
116 calib calib::operator*( double step_size ) const
117 {
118  calib res;
119  for (auto i = 0; i < 9; i++)
120  {
121  res.k_mat.k_mat.rot[i] = k_mat.k_mat.rot[i] * step_size;;
122  }
123 
124  for (auto i = 0; i < 9; i++)
125  res.rot.rot[i] = this->rot.rot[i] * step_size;
126 
127  res.trans.t1 = this->trans.t1 *step_size;
128  res.trans.t2 = this->trans.t2 * step_size;
129  res.trans.t3 = this->trans.t3 *step_size;
130 
131  copy_coefs( res );
132 
133  return res;
134 }
135 
136 calib calib::operator/( double factor ) const
137 {
138  return (*this)*(1.f / factor);
139 }
140 
141 calib calib::operator+( const calib & c ) const
142 {
143  calib res;
144 
145  for (auto i = 0; i < 9; i++)
146  {
147  res.k_mat.k_mat.rot[i] = k_mat.k_mat.rot[i] + c.k_mat.k_mat.rot[i];
148  }
149 
150  for (auto i = 0; i < 9; i++)
151  res.rot.rot[i] = this->rot.rot[i] + c.rot.rot[i];
152 
153  res.trans.t1 = this->trans.t1 + c.trans.t1;
154  res.trans.t2 = this->trans.t2 + c.trans.t2;
155  res.trans.t3 = this->trans.t3 + c.trans.t3;
156 
157  copy_coefs( res );
158 
159  return res;
160 }
161 
162 calib calib::operator-( const calib & c ) const
163 {
164  calib res;
165  for (auto i = 0; i < 9; i++)
166  {
167  res.k_mat.k_mat.rot[i] = k_mat.k_mat.rot[i] - c.k_mat.k_mat.rot[i];
168  }
169 
170  for (auto i = 0; i < 9; i++)
171  res.rot.rot[i] = this->rot.rot[i] - c.rot.rot[i];
172 
173  res.trans.t1 = this->trans.t1 - c.trans.t1;
174  res.trans.t2 = this->trans.t2 - c.trans.t2;
175  res.trans.t3 = this->trans.t3 - c.trans.t3;
176 
177  copy_coefs( res );
178 
179  return res;
180 }
181 
182 calib calib::operator/( const calib & c ) const
183 {
184  calib res;
185 
186  for (auto i = 0; i < 9; i++)
187  {
188  res.k_mat.k_mat.rot[i] = k_mat.k_mat.rot[i] / c.k_mat.k_mat.rot[i];
189  }
190 
191  for (auto i = 0; i < 9; i++)
192  res.rot.rot[i] = this->rot.rot[i] / c.rot.rot[i];
193 
194 
195  res.trans.t1 = this->trans.t1 / c.trans.t1;
196  res.trans.t2 = this->trans.t2 / c.trans.t2;
197  res.trans.t3 = this->trans.t3 / c.trans.t3;
198 
199  copy_coefs( res );
200 
201  return res;
202 }
203 
204 
205 p_matrix p_matrix::operator*(double step_size) const
206 {
207  p_matrix res;
208 
209  for (auto i = 0; i < 12; i++)
210  res.vals[i] = vals[i] * step_size;
211 
212  return res;
213 }
214 
215 p_matrix p_matrix::operator/(double factor) const
216 {
217  return (*this)*(1.f / factor);
218 }
219 
221 {
222  p_matrix res;
223  for (auto i = 0; i < 12; i++)
224  res.vals[i] = vals[i] + c.vals[i];
225 
226  return res;
227 }
228 
230 {
231  p_matrix res;
232  for (auto i = 0; i < 12; i++)
233  res.vals[i] = vals[i] - c.vals[i];
234 
235  return res;
236 }
237 
239 {
240  p_matrix res;
241  for (auto i = 0; i < 12; i++)
242  res.vals[i] = vals[i] * c.vals[i];
243 
244  return res;
245 }
246 
248 {
249  p_matrix res;
250  for (auto i = 0; i < 12; i++)
251  res.vals[i] = c.vals[i] == 0 ? 0 : vals[i] / c.vals[i];
252 
253  return res;
254 }
255 
256 double p_matrix::get_norma() const
257 {
258  double grads_norm = 0;
259 
260  for (auto i = 0; i < 12; i++)
261  grads_norm += vals[i] * vals[i];
262 
263  grads_norm = sqrt(grads_norm);
264 
265  return grads_norm;
266 }
267 
268 static inline bool rtIsNaN( double value ) { return (value != value); }
269 static inline bool rtIsInf( double value )
270 {
271  return ( value == std::numeric_limits< double >::infinity()
272  || value == -std::numeric_limits< double >::infinity() );
273 }
274 
275 double p_matrix::matrix_norm() const
276 {
277  // Code generated by Matlab Coder!
278  double absx;
279  double n = 0;
280  double m[12];
281  m[0] = vals[0];
282  m[1] = vals[4];
283  m[2] = vals[8];
284  m[3] = vals[1];
285  m[4] = vals[5];
286  m[5] = vals[9];
287  m[6] = vals[2];
288  m[7] = vals[6];
289  m[8] = vals[10];
290  m[9] = vals[3];
291  m[10] = vals[7];
292  m[11] = vals[11];
293  for( int j = 0; j < 4; j++ )
294  {
295  absx = std::abs( m[3 * j] );
296  if( rtIsNaN( absx ) || ( absx > n ) )
297  n = absx;
298 
299  absx = std::abs( m[1 + 3 * j] );
300  if( rtIsNaN( absx ) || ( absx > n ) )
301  n = absx;
302 
303  absx = std::abs( m[2 + 3 * j] );
304  if( rtIsNaN( absx ) || ( absx > n ) )
305  n = absx;
306  }
307  if( ( ! rtIsInf( n ) ) && ( ! rtIsNaN( n ) ) )
308  {
309  double dv0[3];
310  svd_3x4( m, dv0 );
311  n = dv0[0];
312  }
313  return n;
314 }
315 
316 double p_matrix::sum() const
317 {
318  double res = 0;
319  for( auto i = 0; i < 12; i++ )
320  res += vals[i];
321  return res;
322 }
323 
324 p_matrix p_matrix::normalize( double const norma ) const
325 {
326  p_matrix res;
327  for( auto i = 0; i < 12; i++ )
328  res.vals[i] = vals[i] / norma;
329  return res;
330 }
331 
333 {
334  //%firstThreeCols = P(:,1:3);% This is Krgb*R
335  double3x3 first_three_cols = { vals[0], vals[1], vals[2],
336  vals[4], vals[5], vals[6],
337  vals[8], vals[9], vals[10] };
338 
339  //%KSquare = firstThreeCols*firstThreeCols';% This is Krgb*R*R'*Krgb' = Krgb*Krgb'
340  auto k_square = first_three_cols * first_three_cols.transpose();
341  //%KSquareInv = inv(KSquare); % Returns a matrix that is equal to: inv(Krgb')*inv(Krgb)
342  std::vector<double> inv_k_square_vac( 9, 0 );
343  inv( k_square.to_vector().data(), inv_k_square_vac.data() );
344 
345  double3x3 inv_k_square = { inv_k_square_vac[0], inv_k_square_vac[3], inv_k_square_vac[6],
346  inv_k_square_vac[1], inv_k_square_vac[4], inv_k_square_vac[7],
347  inv_k_square_vac[2], inv_k_square_vac[5], inv_k_square_vac[8] };
348 
349  //%KInv = cholesky3x3(KSquareInv)';% Cholsky decomposition 3 by 3. returns a lower triangular matrix 3x3. Equal to inv(Krgb')
350  auto k_inv = cholesky3x3( inv_k_square ).transpose();
351  //%K = inv(KInv);
352  matrix_3x3 k_vac = { 0 };
353  inv( k_inv.to_vector().data(), k_vac.rot );
354  //%K = K/K(end);
355  for( auto i = 0; i < 9; i++ )
356  k_vac.rot[i] /= k_vac.rot[9 - 1];
357 
358  //%t = KInv*P(:,4);
359  auto t = k_inv * double3{ vals[3], vals[7], vals[11] };
360 
361  //%R = KInv*firstThreeCols;
362  auto r = (k_inv * first_three_cols).to_vector();
363 
364  krt calibration;
365  for( auto i = 0; i < r.size(); i++ )
366  calibration.rot.rot[i] = r[i];
367 
368  calibration.trans = { t.x, t.y, t.z };
369 
370  calibration.k_mat = matrix_3x3(k_vac);
371 
372  return calibration;
373 }
374 
376  calib const & in_calibration )
377 {
378  auto krt = in_mat.decompose();
379 
380  calib calibration;
381  calibration.rot = krt.rot;
382  calibration.trans = krt.trans;
383  calibration.k_mat = krt.k_mat;
384  in_calibration.copy_coefs( calibration );
385 
386  return calibration;
387 }
rs2_extrinsics_double get_extrinsics() const
Definition: calibration.cpp:63
const GLfloat * m
Definition: glext.h:6814
float translation[3]
Definition: rs_sensor.h:99
p_matrix operator+(const p_matrix &c) const
GLfloat value
rs2_intrinsics_double get_intrinsics() const
Definition: calibration.cpp:55
p_matrix operator-(const p_matrix &c) const
calib decompose(p_matrix const &mat, calib const &)
rs2_intrinsics intrin
static bool rtIsNaN(double value)
float coeffs[5]
Definition: rs_types.h:67
GLdouble n
Definition: glext.h:1966
GLhandleARB obj
Definition: glext.h:4157
float rotation[9]
Definition: rs_sensor.h:98
static bool rtIsInf(double value)
GLdouble t
Cross-stream extrinsics: encodes the topology describing how the different devices are oriented...
Definition: calibration.h:67
const GLubyte * c
Definition: glext.h:12690
GLdouble GLdouble r
GLint GLsizei GLsizei height
GLint j
Cross-stream extrinsics: encodes the topology describing how the different devices are oriented...
Definition: rs_sensor.h:96
rs2_distortion model
Definition: rs_types.h:66
Video stream intrinsics.
Definition: rs_types.h:58
void svd_3x4(const double in[12], double out[3])
Definition: svd_3x4.cpp:250
int i
GLuint res
Definition: glext.h:8856
GLint GLsizei width


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