orientation.c
Go to the documentation of this file.
1 #include <math.h>
2 #include <gsl/gsl_nan.h>
3 
4 #include "csm_all.h"
5 
6 #include <egsl/egsl_macros.h>
7 
8 
9 void find_neighbours(LDP ld, int i, int max_num, int*indexes, size_t*num_found);
10 void filter_orientation(double theta0, double rho0, size_t n,
11  const double*thetas, const double*rhos, double *alpha, double*cov0_alpha );
12 
14 void ld_compute_orientation(LDP ld, int size_neighbourhood, double sigma) {
15  int i;
16  for(i=0;i<ld->nrays;i++){
17  if(!ld_valid_ray(ld,i) || (ld->cluster[i] == -1)) {
18  ld->alpha[i] = GSL_NAN;
19  ld->cov_alpha[i] = GSL_NAN;
20  ld->alpha_valid[i] = 0;
21  continue;
22  }
23 
24  int neighbours[size_neighbourhood*2];
25  size_t num_neighbours;
26  find_neighbours(ld, i, size_neighbourhood, neighbours, &num_neighbours);
27 
28  if(0==num_neighbours) {
29  ld->alpha[i] = GSL_NAN;
30  ld->cov_alpha[i] = GSL_NAN;
31  ld->alpha_valid[i] = 0;
32  continue;
33  }
34 
35 /* printf("orientation for i=%d:\n",i); */
36  double thetas[num_neighbours];
37  double readings[num_neighbours];
38  size_t a=0;
39  for(a=0;a<num_neighbours;a++) {
40  thetas[a] = ld->theta[neighbours[a]];
41  readings[a] = ld->readings[neighbours[a]];
42  /* printf(" j = %d theta = %f rho = %f\n", neighbours[a], thetas[a],readings[a]); */
43  }
44 
45  double alpha=42, cov0_alpha=32;
46  filter_orientation(ld->theta[i],ld->readings[i],num_neighbours,
47  thetas,readings,&alpha,&cov0_alpha);
48 
49  if(gsl_isnan(alpha)) {
50  ld->alpha[i] = GSL_NAN;
51  ld->cov_alpha[i] = GSL_NAN;
52  ld->alpha_valid[i] = 0;
53  } else {
54  ld->alpha[i] = alpha;
55  ld->cov_alpha[i] = cov0_alpha * square(sigma);
56  ld->alpha_valid[i] = 1;
57  }
58  /* printf("---------- i = %d alpha = %f sigma=%f cov_alpha = %f\n", i, alpha, ld->cov_alpha[i]);*/
59  }
60 }
61 
64 void filter_orientation(double theta0, double rho0, size_t n,
65  const double*thetas, const double*rhos, double *alpha, double*cov0_alpha ) {
66 
67  egsl_push();
68  /* Y = L x + R epsilon */
69  val Y = zeros(n,1);
70  val L = ones(n,1);
71  val R = zeros(n,n+1);
72 
73  size_t i; for(i=0;i<n;i++) {
74  *egsl_atmp(Y, i, 0) = (rhos[i]-rho0)/(thetas[i]-theta0);
75  *egsl_atmp(R, i, 0) = -1/(thetas[i]-theta0);
76  *egsl_atmp(R, i, i+1) = +1/(thetas[i]-theta0);
77  }
78 
79  val eRinv = inv(m(R, tr(R)));
80  val vcov_f1 = inv(m3(tr(L),eRinv, L));
81  val vf1 = m4(vcov_f1, tr(L), eRinv, Y);
82 
83  double cov_f1 = *egsl_atmp(vcov_f1,0,0);
84  double f1 = *egsl_atmp(vf1,0,0);
85 
86  *alpha = theta0 - atan(f1/rho0);
87 
88  if(cos(*alpha)*cos(theta0)+sin(*alpha)*sin(theta0)>0)
89  *alpha = *alpha + M_PI;
90 
91  double dalpha_df1 = rho0 / (square(rho0) + square(f1));
92  double dalpha_drho = -f1 / (square(rho0) + square(f1));
93 
94  *cov0_alpha = square(dalpha_df1) * cov_f1 + square(dalpha_drho);
95 
96 
97  if(gsl_isnan(*alpha)) {
98  egsl_print("Y",Y);
99  egsl_print("L",L);
100  egsl_print("R",R);
101  egsl_print("eRinv",eRinv);
102  egsl_print("vcov_f1",vcov_f1);
103 
104  printf(" f1 = %f cov =%f \n", f1,cov_f1);
105  printf(" f1/rho = %f \n", f1/rho0);
106  printf(" atan = %f \n", atan(f1/rho0));
107  printf(" theta0= %f \n", theta0);
108  }
109 
110  egsl_pop();
111 /*
112 // printf("dalpha_df1 = %f dalpha_drho = %f\n",dalpha_df1,dalpha_drho);
113 // printf("f1 = %f covf1 = %f alpha = %f cov_alpha = %f\n ",f1,cov_f1,*alpha,*cov0_alpha);
114 // printf("sotto = %f\n ",(square(rho0) + square(f1)));
115 
116 // printf(" alpha = %f sigma= %f°\n", *alpha, rad2deg(0.01*sqrt(*cov0_alpha)));
117 
118  printf("l= ");
119  gsl_matrix_fprintf(stdout, l, "%f");
120  printf("\ny= ");
121  gsl_matrix_fprintf(stdout, y, "%f");
122  printf("\nr= ");
123  gsl_matrix_fprintf(stdout, r, "%f");
124  printf("\ninv(r*r)= ");
125  gsl_matrix_fprintf(stdout, Rinv, "%f");
126  printf("\nf1 = %lf ",f1);
127  printf("\ncov_f1 = %lf ",cov_f1);
128 */
129 }
130 
131 /* indexes: an array of size "max_num*2" */
132 void find_neighbours(LDP ld, int i, int max_num, int*indexes, size_t*num_found) {
133  *num_found = 0;
134 
135  int up = i;
136  while ((up+1 <= i+max_num) && (up+1<ld->nrays) && ld_valid_ray(ld,up+1)
137  && (ld->cluster[up+1] == ld->cluster[i])) {
138  up+=1;
139  indexes[(*num_found)++] = up;
140  }
141  int down = i;
142  while ((down >= i-max_num) && (down-1>=0) && ld_valid_ray(ld,down-1) &&
143  (ld->cluster[down-1] == ld->cluster[i])) {
144  down-=1;
145  indexes[(*num_found)++] = down;
146  }
147 }
148 
149 
double *restrict cov_alpha
Definition: laser_data.h:29
#define m3(v1, v2, v3)
Definition: egsl_macros.h:14
#define ones(rows, cols)
Definition: egsl_macros.h:20
void egsl_pop()
Definition: egsl.c:89
Definition: egsl.h:12
int *restrict cluster
Definition: laser_data.h:26
double *restrict theta
Definition: laser_data.h:21
INLINE int ld_valid_ray(LDP ld, int i)
void egsl_push()
Definition: egsl.c:88
#define M_PI
Definition: math_utils.h:7
#define m4(v1, v2, v3, v4)
Definition: egsl_macros.h:15
#define tr(v)
Definition: egsl_macros.h:12
#define m(v1, v2)
Definition: egsl_macros.h:13
double *restrict readings
Definition: laser_data.h:24
int *restrict alpha_valid
Definition: laser_data.h:30
int neighbours
#define inv(v)
Definition: egsl_macros.h:27
void find_neighbours(LDP ld, int i, int max_num, int *indexes, size_t *num_found)
Definition: orientation.c:132
double * egsl_atmp(val v, size_t i, size_t j)
Definition: egsl.c:276
double square(double x)
Definition: math_utils.c:124
void ld_compute_orientation(LDP ld, int size_neighbourhood, double sigma)
Definition: orientation.c:14
double *restrict alpha
Definition: laser_data.h:28
void egsl_print(const char *str, val v)
Definition: egsl.c:251
void filter_orientation(double theta0, double rho0, size_t n, const double *thetas, const double *rhos, double *alpha, double *cov0_alpha)
Definition: orientation.c:64
#define zeros(rows, cols)
Definition: egsl_macros.h:19


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