icp_covariance.c
Go to the documentation of this file.
1 #include <math.h>
2 #include <gsl/gsl_math.h>
3 #include <egsl/egsl_macros.h>
4 
5 #include "icp.h"
6 #include "../csm_all.h"
7 
8 
9 val compute_C_k(val p_j1, val p_j2);
10 val dC_drho(val p1, val p2);
11 
12 
14  LDP laser_ref, LDP laser_sens, const gsl_vector*x,
15  val *cov0_x, val *dx_dy1, val *dx_dy2)
16 {
17  egsl_push_named("compute_covariance_exact");
18 
19  val d2J_dxdy1 = zeros(3,(size_t)laser_ref ->nrays);
20  val d2J_dxdy2 = zeros(3,(size_t)laser_sens->nrays);
21 
22  /* the three pieces of d2J_dx2 */
23  val d2J_dt2 = zeros(2,2);
24  val d2J_dt_dtheta = zeros(2,1);
25  val d2J_dtheta2 = zeros(1,1);
26 
27  double theta = x->data[2];
28  val t = egsl_vFa(2,x->data);
29 
30  int i;
31  for(i=0;i<laser_sens->nrays;i++) {
32  if(!ld_valid_corr(laser_sens,i)) continue;
33  egsl_push_named("compute_covariance_exact iteration");
34 
35  int j1 = laser_sens->corr[i].j1;
36  int j2 = laser_sens->corr[i].j2;
37 
38  val p_i = egsl_vFa(2, laser_sens->points[i].p);
39  val p_j1 = egsl_vFa(2, laser_ref ->points[j1].p);
40  val p_j2 = egsl_vFa(2, laser_ref ->points[j2].p);
41 
42  /* v1 := rot(theta+M_PI/2)*p_i */
43  val v1 = m(rot(theta+M_PI/2), p_i);
44  /* v2 := (rot(theta)*p_i+t-p_j1) */
45  val v2 = sum3( m(rot(theta),p_i), t, minus(p_j1));
46  /* v3 := rot(theta)*v_i */
47  val v3 = vers(theta + laser_sens->theta[i]);
48  /* v4 := rot(theta+PI/2)*v_i; */
49  val v4 = vers(theta + laser_sens->theta[i] + M_PI/2);
50 
51  val C_k = compute_C_k(p_j1, p_j2);
52 
53  val d2J_dt2_k = sc(2.0, C_k);
54  val d2J_dt_dtheta_k = sc(2.0,m(C_k,v1));
55 
56  val v_new = m(rot(theta+M_PI), p_i);
57  val d2J_dtheta2_k = sc(2.0, sum( m3(tr(v2),C_k, v_new), m3(tr(v1),C_k,v1)));
58  add_to(d2J_dt2, d2J_dt2_k);
59  add_to(d2J_dt_dtheta, d2J_dt_dtheta_k );
60  add_to(d2J_dtheta2, d2J_dtheta2_k);
61 
62  /* for measurement rho_i in the second scan */
63  val d2Jk_dtdrho_i = sc(2.0, m(C_k,v3));
64  val d2Jk_dtheta_drho_i = sc(2.0, sum( m3(tr(v2),C_k,v4), m3(tr(v3),C_k,v1)));
65  add_to_col(d2J_dxdy2, (size_t)i, comp_col(d2Jk_dtdrho_i, d2Jk_dtheta_drho_i));
66 
67  /* for measurements rho_j1, rho_j2 in the first scan */
68 
69  val dC_drho_j1 = dC_drho(p_j1, p_j2);
70  val dC_drho_j2 = dC_drho(p_j2, p_j1);
71 
72 
73  val v_j1 = vers(laser_ref->theta[j1]);
74 
75  val d2Jk_dt_drho_j1 = sum(sc(-2.0,m(C_k,v_j1)), sc(2.0,m(dC_drho_j1,v2)));
76  val d2Jk_dtheta_drho_j1 = sum( sc(-2.0, m3(tr(v_j1),C_k,v1)), m3(tr(v2),dC_drho_j1,v1));
77  add_to_col(d2J_dxdy1, (size_t)j1, comp_col(d2Jk_dt_drho_j1, d2Jk_dtheta_drho_j1));
78 
79  /* for measurement rho_j2*/
80  val d2Jk_dt_drho_j2 = sc(2.0, m( dC_drho_j2,v2));
81  val d2Jk_dtheta_drho_j2 = sc(2.0, m3( tr(v2), dC_drho_j2, v1));
82  add_to_col(d2J_dxdy1, (size_t)j2, comp_col(d2Jk_dt_drho_j2, d2Jk_dtheta_drho_j2));
83 
84  egsl_pop_named("compute_covariance_exact iteration");
85  }
86 
87  /* composes matrix d2J_dx2 from the pieces*/
88  val d2J_dx2 = comp_col( comp_row( d2J_dt2 , d2J_dt_dtheta),
89  comp_row(tr(d2J_dt_dtheta), d2J_dtheta2));
90 
91  val edx_dy1 = sc(-1.0, m(inv(d2J_dx2), d2J_dxdy1));
92  val edx_dy2 = sc(-1.0, m(inv(d2J_dx2), d2J_dxdy2));
93 
94  val ecov0_x = sum(m(edx_dy1,tr(edx_dy1)),m(edx_dy2,tr(edx_dy2)) );
95 
96  /* With the egsl_promote we save the matrix in the previous
97  context */
98  *cov0_x = egsl_promote(ecov0_x);
99  *dx_dy1 = egsl_promote(edx_dy1);
100  *dx_dy2 = egsl_promote(edx_dy2);
101 
102  egsl_pop_named("compute_covariance_exact");
103  /* now edx_dy1 is not valid anymore, but *dx_dy1 is. */
104 }
105 
106 
107 val compute_C_k(val p_j1, val p_j2) {
108  val d = sub(p_j1, p_j2);
109  double alpha = M_PI/2 + atan2( atv(d,1), atv(d,0));
110  double c = cos(alpha); double s = sin(alpha);
111  double m[2*2] = {
112  c*c, c*s,
113  c*s, s*s
114  };
115  return egsl_vFda(2,2,m);
116 }
117 
118 
119 val dC_drho(val p1, val p2) {
120  double eps = 0.001;
121 
122  val C_k = compute_C_k(p1, p2);
123  val p1b = sum(p1, sc(eps/egsl_norm(p1),p1));
124  val C_k_eps = compute_C_k(p1b,p2);
125  return sc(1/eps, sub(C_k_eps,C_k));
126 }
127 
128 
#define m3(v1, v2, v3)
Definition: egsl_macros.h:14
point2d *restrict points
Definition: laser_data.h:44
double egsl_norm(val v1)
Definition: egsl.c:282
Definition: egsl.h:12
#define add_to(v1, v2)
Definition: egsl_macros.h:25
INLINE int ld_valid_corr(LDP ld, int i)
double *restrict theta
Definition: laser_data.h:21
val compute_C_k(val p_j1, val p_j2)
#define M_PI
Definition: math_utils.h:7
#define tr(v)
Definition: egsl_macros.h:12
#define comp_row(v1, v2)
Definition: egsl_macros.h:17
#define m(v1, v2)
Definition: egsl_macros.h:13
struct @0 p
#define sc(d, v)
Definition: egsl_macros.h:24
val egsl_promote(val v)
Definition: egsl.c:223
#define comp_col(v1, v2)
Definition: egsl_macros.h:16
LDP laser_ref
Definition: algos.h:14
#define rot(theta)
Definition: egsl_macros.h:22
LDP laser_sens
Definition: algos.h:16
val egsl_vFa(size_t rows, const double *)
#define vers(th)
Definition: egsl_macros.h:21
void egsl_push_named(const char *name)
Definition: egsl.c:91
#define inv(v)
Definition: egsl_macros.h:27
#define atv(v, i)
Definition: egsl_macros.h:6
struct correspondence *restrict corr
Definition: laser_data.h:36
#define sub(v1, v2)
Definition: egsl_macros.h:8
#define sum(v1, v2)
Definition: egsl_macros.h:10
#define sum3(v1, v2, v3)
Definition: egsl_macros.h:11
void compute_covariance_exact(LDP laser_ref, LDP laser_sens, const gsl_vector *x, val *cov0_x, val *dx_dy1, val *dx_dy2)
#define minus(v)
Definition: egsl_macros.h:9
val dC_drho(val p1, val p2)
#define add_to_col(v1, j, v2)
Definition: egsl_macros.h:26
double p[2]
Definition: laser_data.h:12
void egsl_pop_named(const char *name)
Definition: egsl.c:117
#define zeros(rows, cols)
Definition: egsl_macros.h:19
val egsl_vFda(size_t rows, size_t columns, const double *)


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23