ld_resample.c
Go to the documentation of this file.
1 #include <gsl/gsl_rng.h>
2 #include <gsl/gsl_randist.h>
3 #include <gsl/gsl_math.h>
4 
5 #include <math.h>
6 #include <options/options.h>
7 
8 
9 #include "../csm/csm_all.h"
10 
11 struct {
12  double interval;
13  int seed;
14 }p ;
15 
16 LDP ld_resample(LDP ld);
17 
18 gsl_rng * rng;
19 
20 int main(int argc, const char ** argv) {
21  sm_set_program_name(argv[0]);
22 
23 
24  struct option* ops = options_allocate(3);
25  options_double(ops, "interval", &p.interval, sqrt(2.0), " 1 = no resampling");
26 
27  if(!options_parse_args(ops, argc, argv)) {
28  options_print_help(ops, stderr);
29  return -1;
30  }
31 
32  int count = -1;
33  LDP ld;
34  while( (ld = ld_read_smart(stdin))) {
35  count++;
36 
37  if(!ld_valid_fields(ld)) {
38  sm_error("Invalid laser data (#%d in file)\n", count);
39  continue;
40  }
41 
42 /* if(count & 1) {*/
43  LDP ld2 = ld_resample(ld);
44  ld_write_as_json(ld2, stdout);
45  ld_free(ld2);
46  ld_free(ld);
47 /* } else {
48  ld_write_as_json(ld, stdout);
49  ld_free(ld);
50  }*/
51 
52  count++;
53  }
54 
55  return 0;
56 }
57 
59  /* FIXME magic number */
60  int n = (int) (floor(ld->nrays / p.interval));
61 
62  LDP ld2 = ld_alloc_new(n);
63  int k;
64  for(k=0;k<n;k++) {
65  double index = k * p.interval;
66  int i = (int) floor(index);
67  int j = i + 1;
68  double a = 1 - (index - i);
69 
70 
71  if( (j>= ld->nrays) || !ld->valid[i] || !ld->valid[j]) {
72  ld2->valid[k] = 0;
73  ld2->readings[k] = NAN;
74  ld2->alpha_valid[k] = 0;
75  ld2->alpha[k] = NAN;
76  ld2->theta[k] = ld->theta[i];
77  } else {
78  ld2->theta[k] = a * ld->theta[i] + (1-a) * ld->theta[j];
79 
80 
81  if(is_nan(ld2->theta[k])) {
82  sm_debug("Hey, k=%d theta[%d]=%f theta[%d]=%f\n",
83  k,i,ld->theta[i],j,ld->theta[j]);
84  }
85 
86  ld2->readings[k] = a * ld->readings[i] + (1-a) * ld->readings[j];
87  ld2->valid[k] = 1;
88  }
89 
90 /* sm_debug("k=%d index=%f i=%d a=%f valid %d reading %f\n", k,index,i,a,ld2->valid[k],ld2->readings[k]);*/
91  }
92 
93  ld2->min_theta = ld2->theta[0];
94  ld2->max_theta = ld2->theta[n-1];
95  ld2->tv = ld->tv;
96 
97  copy_d(ld->odometry, 3, ld2->odometry);
98  copy_d(ld->estimate, 3, ld2->estimate);
99 
100 
101  return ld2;
102 }
103 
int *restrict valid
Definition: laser_data.h:23
void sm_set_program_name(const char *name)
Definition: logging.c:21
#define NAN
Definition: math_utils.h:11
double max_theta
Definition: laser_data.h:19
double min_theta
Definition: laser_data.h:18
void ld_write_as_json(LDP ld, FILE *stream)
double odometry[3]
Definition: laser_data.h:39
void copy_d(const double *from, int n, double *to)
Definition: math_utils.c:83
LDP ld_resample(LDP ld)
Definition: ld_resample.c:58
double *restrict theta
Definition: laser_data.h:21
void options_double(struct option *, const char *name, double *p, double def_value, const char *desc)
int ld_valid_fields(LDP ld)
Definition: laser_data.c:179
int seed
Definition: ld_resample.c:13
struct option * ops
Definition: rb_sm.c:31
LDP ld_read_smart(FILE *)
Definition: options.h:49
void ld_free(LDP ld)
Definition: laser_data.c:87
double *restrict readings
Definition: laser_data.h:24
struct option * options_allocate(int n)
int *restrict alpha_valid
Definition: laser_data.h:30
int main(int argc, const char **argv)
Definition: ld_resample.c:20
void options_print_help(struct option *options, FILE *f)
Definition: options.c:398
double estimate[3]
Definition: laser_data.h:40
int is_nan(double v)
Definition: math_utils.c:60
struct timeval tv
Definition: laser_data.h:50
void sm_debug(const char *msg,...)
Definition: logging.c:88
gsl_rng * rng
Definition: ld_resample.c:18
LDP ld_alloc_new(int nrays)
Definition: laser_data.c:12
double *restrict alpha
Definition: laser_data.h:28
struct @1 p
void sm_error(const char *msg,...)
Definition: logging.c:49
int options_parse_args(struct option *ops, int argc, const char *argv[])
Definition: options.c:66
double interval
Definition: ld_resample.c:12


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