laser_data_load.c
Go to the documentation of this file.
1 #include <ctype.h>
2 
3 #include "csm_all.h"
4 #include "laser_data_drawing.h"
5 
7 int ld_read_some(FILE*file, LDP **array, int*num, int (*accept)(LDP));
8 
9  /* Every n scans */
10  int interval_accept(LDP ld);
11  int interval_count = 0;
13 
14  /* Every one */
15  int always(LDP ld);
16 
17  /* Read according to distance */
18  int distance_accept(LDP ld);
19  static int distance_count;
20  static double distance_last_pose[3];
21  static double distance_interval_xy = 10;
22  static double distance_interval_th = 10;
24  void distance_accept_reset(ld_reference, double interval_xy, double interval_th);
25 
26 
27 
28 /* ---------------------------------------- */
29 
30 int ld_read_some(FILE*file, LDP **array, int*num, int (*accept)(LDP)) {
31  *array = 0; *num = 0;
32  int size = 10;
33  LDP * ar = (LDP*) malloc(sizeof(LDP)*size);
34 
35  while(1) {
36  LDP ld = ld_read_smart(file);
37 
38  if(!ld) break;
39 
40  if( ! (*accept)(ld) ) {
41  ld_free(ld);
42  continue;
43  }
44 
45 
46  ar[(*num)++] = ld;
47 
48  if(*num > size - 1) {
49  size *= 2;
50  if(! (ar = (LDP*) realloc(ar, sizeof(LDP)*size)) ) {
51  sm_error("Cannot allocate (size=%d)\n", size);
52  return 0;
53  }
54  }
55  }
56 
57  *array = ar;
58 
59  return feof(file);
60 }
61 
62 
63 /* Read every tot scans */
65  ld=ld;
66  int accept = interval_count % interval_interval == 0;
67  interval_count++;
68  return accept;
69 }
70 
71 int ld_read_some_scans(FILE*file, LDP **array, int*num, int interval) {
72  interval_count = 0;
73  interval_interval = interval;
74  return ld_read_some(file, array, num, interval_accept);
75 }
76 
77 /* Read all scans */
78 int always(LDP ld) { ld=ld; return 1; }
79 int ld_read_all(FILE*file, LDP **array, int*num) {
80  return ld_read_some(file, array, num, always);
81 }
82 
83 void distance_accept_reset(ld_reference which, double interval_xy, double interval_th) {
84  distance_count = 0;
85  distance_interval_xy = interval_xy;
86  distance_interval_th = interval_th;
87  distance_reference = which;
88 }
89 
91  double * this_pose = ld_get_reference_pose(ld, distance_reference);
92  if(!this_pose) return 0;
93 
94  distance_count++;
95  if(distance_count == 1) {
96  copy_d(this_pose, 3, distance_last_pose);
97  return 1;
98  } else {
99  double diff[3];
100  pose_diff_d(distance_last_pose, this_pose, diff);
101  double distance = norm_d(diff);
102 
103  if(distance >= distance_interval_xy ||
104  fabs(diff[2]) >= distance_interval_th )
105  {
106  copy_d(this_pose, 3, distance_last_pose);
107  /* sm_debug("Accepting #%d, %f\n", distance_count, distance);*/
108  return 1;
109  }
110  else
111  return 0;
112  }
113 }
114 
115 int ld_read_some_scans_distance(FILE*file, LDP **array, int*num,
116  ld_reference which, double d_xy, double d_th) {
117  distance_accept_reset(which, d_xy, d_th);
118  return ld_read_some(file, array, num, distance_accept);
119 }
120 
121 
122 
131  while(1) {
132  int c;
133  while(1) {
134  c = fgetc(f);
135  if(feof(f)) {
136  /* sm_debug("eof\n"); */
137  return 0;
138  }
139  if(!isspace(c)) break;
140  }
141  ungetc(c, f);
142 
143  switch(c) {
144  case '{': {
145 /* sm_debug("Reading JSON\n"); */
146  return ld_from_json_stream(f);
147  }
148  case 'F': {
149 /* sm_debug("Reading Carmen\n"); */
150  LDP ld;
151  if(!ld_read_next_laser_carmen(f, &ld)) {
152  sm_error("bad carmen\n");
153  return 0;
154  }
155  return ld;
156  }
157  default: {
158  /*sm_error("Could not read ld. First char is '%c'. ", c);*/
159  char max_line[10000];
160  char * res = fgets(max_line, 10000-2, f);
161  if(!res) {
162  sm_error("Could not skip line. \n");
163  return 0;
164  } else {
165  fprintf(stderr, "s");
166 /* sm_error("Skipped '%s'\n", res);*/
167  }
168  }
169  }
170  }
171 }
172 
173 LDP ld_read_smart_string(const char*line) {
174  switch(*line) {
175  case '{':
176  return ld_from_json_string(line);
177 
178  case 'F':
179  return ld_from_carmen_string(line);
180 
181  default:
182  sm_error("Invalid laserdata format: '%s'.", line);
183  return 0;
184  }
185 }
186 
187 
188 
189 
LDP ld_from_carmen_string(const char *line)
double norm_d(const double p[2])
Definition: math_utils.c:71
int interval_interval
int interval_accept(LDP ld)
void distance_accept_reset(ld_reference, double interval_xy, double interval_th)
void copy_d(const double *from, int n, double *to)
Definition: math_utils.c:83
int always(LDP ld)
ld_reference
LDP ld_read_smart_string(const char *line)
int distance_accept(LDP ld)
int ld_read_all(FILE *file, LDP **array, int *num)
double * ld_get_reference_pose(LDP ld, ld_reference use_reference)
void ld_free(LDP ld)
Definition: laser_data.c:87
int ld_read_some(FILE *file, LDP **array, int *num, int(*accept)(LDP))
static double distance_last_pose[3]
LDP ld_from_json_stream(FILE *file)
int ld_read_some_scans_distance(FILE *file, LDP **array, int *num, ld_reference which, double d_xy, double d_th)
static ld_reference distance_reference
int ld_read_next_laser_carmen(FILE *, LDP *ld)
static double distance_interval_xy
static int distance_count
LDP ld_read_smart(FILE *f)
static double distance_interval_th
void pose_diff_d(const double pose2[3], const double pose1[3], double res[3])
Definition: math_utils.c:115
int ld_read_some_scans(FILE *file, LDP **array, int *num, int interval)
int interval_count
LDP ld_from_json_string(const char *s)
void sm_error(const char *msg,...)
Definition: logging.c:49
double interval
Definition: ld_resample.c:12
double distance(const gsl_vector *a, const gsl_vector *b)


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