ld_remove_doubles.c
Go to the documentation of this file.
1 #include <time.h>
2 
3 #include <options/options.h>
4 
5 #include "../csm/csm_all.h"
6 
7 int ld_equal_readings(LDP ld1, LDP ld2, double epsilon);
8 
9 int main(int argc, const char*argv[]) {
10  sm_set_program_name(argv[0]);
11 
12  double epsilon;
13  int debug;
14 
15 
16  struct option* ops = options_allocate(3);
17  options_double(ops, "epsilon", &epsilon, 0.0001, "minimum difference between rays to be used");
18 
19 
20  if(!options_parse_args(ops, argc, argv)) {
21  options_print_help(ops, stderr);
22  return -1;
23  }
24 
25  sm_debug_write(debug);
26 
27 
28  /* Read first scan */
29  LDP laser_ref=0, laser_sens;
30 
31  int count = -1;
32  int num_discarded = 0;
33  int num_invalid = 0;
34  int ref_index = 0;
35 
36  while( (laser_sens = ld_read_smart(stdin)) ) {
37  count++;
38 
39  if(!ld_valid_fields(laser_sens)) {
40  sm_error("Invalid laser data (#%d in file)\n", count);
41  num_invalid++;
42  continue;
43  }
44 
45  if(!laser_ref) {
46  laser_ref = laser_sens;
47  ld_write_as_json(laser_sens, stdout);
48  count++;
49  continue;
50  }
51 
52  if(ld_equal_readings(laser_ref, laser_sens, epsilon)) {
53  sm_debug("Ignoring scan #%d, too similar to #%d.\n", count, ref_index);
54  num_discarded++;
55  } else {
56  ld_write_as_json(laser_sens, stdout);
57  ld_free(laser_ref);
58  laser_ref = laser_sens;
59  ref_index = count;
60  }
61 
62  }
63  if(laser_ref) ld_free(laser_ref);
64 
65  sm_info("# epsilon: %f m\n", epsilon);
66  sm_info("# scans: %d\n", count);
67  if(count>0) {
68  sm_info("# invalid: %d\n", num_invalid);
69  sm_info("# discarded: %d (%d%%)\n", num_discarded, num_discarded * 100 / count);
70  }
71 
72  return num_invalid;
73 }
74 
75 
76 
77 int ld_equal_readings(LDP ld1, LDP ld2, double epsilon) {
78  int i;
79  for(i=0;i<ld1->nrays;i++) {
80  if(!ld_valid_ray(ld1,i) || !ld_valid_ray(ld2,i)) continue;
81 
82  if(fabs(ld1->readings[i]-ld2->readings[i]) > epsilon)
83  return 0;
84  }
85  return 1;
86 }
void sm_set_program_name(const char *name)
Definition: logging.c:21
void ld_write_as_json(LDP ld, FILE *stream)
INLINE int ld_valid_ray(LDP ld, int i)
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
struct option * ops
Definition: rb_sm.c:31
LDP ld_read_smart(FILE *)
int ld_equal_readings(LDP ld1, LDP ld2, double epsilon)
int main(int argc, const char *argv[])
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)
void sm_debug_write(int flag)
Definition: logging.c:16
int debug
Definition: sm2.c:20
void options_print_help(struct option *options, FILE *f)
Definition: options.c:398
void sm_debug(const char *msg,...)
Definition: logging.c:88
void sm_info(const char *msg,...)
Definition: logging.c:71
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


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