laser_data_json.c
Go to the documentation of this file.
1 #include <errno.h>
2 #include <string.h>
3 #include <math.h>
4 #include <ctype.h>
5 
6 #include "csm_all.h"
7 
8 void jo_add_timestamp(JO jo, const char*name, struct timeval*tv);
9 int is_all_nan(const double *v, int n );
10 void jo_add_double_array_if_not_nan(JO jo, const char*name, const double *v, int n);
11 int all_is(int *a, int n, int v);
12 
13 /* -------------------------------------------- */
14 
15 
16 JO matrix_to_json(gsl_matrix*m) {
17  JO jo = jo_new_array();
18  if(m->size1>1) {
19  size_t i,j;
20  for(i=0;i<m->size1;i++) {
21  JO row = json_object_new_array();
22  for(j=0;j<m->size2;j++) {
23  double v = gsl_matrix_get(m,i,j);
25  }
26  json_object_array_add(jo, row);
27  }
28  } else {
29  size_t i = 1, j;
30  JO row = jo;
31  for(j=0;j<m->size2;j++) {
32  double v = gsl_matrix_get(m,i,j);
34  }
35  }
36  return jo;
37 }
38 
39 int json_to_corr(JO array, struct correspondence*corr, int n) {
41  int i;
42  for(i=0;i<n;i++) {
43  JO element = json_object_array_get_idx(array, i);
44  if(element == NULL) {
45  corr[i].valid = 0;
46  corr[i].j1 = -1;
47  corr[i].j2 = -1;
48  } else {
49  corr[i].valid = 1;
50  jo_read_int(element, "j1", &(corr[i].j1));
51  jo_read_int(element, "j2", &(corr[i].j2));
52  int type;
53  jo_read_int(element, "type", &(type));
54  corr[i].type = type;
55  }
56  }
57  return 1;
58 }
59 
60 JO corr_to_json(struct correspondence*corr, int n) {
61  JO jo = jo_new_array();
62  int i;
63  for(i=0;i<n;i++) {
64  if(corr[i].valid) {
65  JO c = jo_new();
66  jo_add_int(c, "j1", corr[i].j1);
67  jo_add_int(c, "j2", corr[i].j2);
68  jo_add_int(c, "type", (int) corr[i].type);
69  jo_array_add(jo, c);
70  } else {
72  }
73  }
74  return jo;
75 }
76 
77 
78 JO vector_to_json(gsl_vector*vec) {
80  size_t j;
81  for(j=0;j<vec->size;j++) {
82  double v = gsl_vector_get(vec,j);
84  }
85  return jo;
86 }
87 
88 
89 void jo_add_timestamp(JO jo, const char*name, struct timeval*tv) {
90  int array[2] = {tv->tv_sec, tv->tv_usec};
91  jo_add_int_array(jo, name, array, 2);
92 }
93 
94 JO result_to_json(struct sm_params*p, struct sm_result *r) {
96  jo_add_int(jo, "valid", r->valid);
97 
98  if(r->valid) {
99  jo_add_double_array(jo, "x", r->x, 3);
100 
101  if(p->do_compute_covariance) {
102  jo_add(jo, "cov_x", matrix_to_json(r->cov_x_m ) );
103  jo_add(jo, "dx_dy1", matrix_to_json(r->dx_dy1_m) );
104  jo_add(jo, "dx_dy2", matrix_to_json(r->dx_dy2_m) );
105  }
106  }
107  jo_add_int(jo, "iterations", r->iterations);
108  jo_add_int(jo, "nvalid", r->nvalid);
109  jo_add_double(jo, "error", r->error);
110 
111  jo_add_timestamp(jo, "laser_ref_timestamp", &(p->laser_ref->tv));
112  jo_add_timestamp(jo, "laser_sens_timestamp", &(p->laser_sens->tv));
113 
114  return jo;
115 }
116 
117 int is_all_nan(const double *v, int n ) {
118  int i; for(i=0;i<n;i++) if(v[i]==v[i]) return 0;
119  return 1;
120 }
121 
123 void jo_add_double_array_if_not_nan(JO jo, const char*name, const double *v, int n) {
124  if(is_all_nan(v,n)) return;
125  jo_add_double_array(jo, name, v, n);
126 }
127 
129 int all_is(int *a, int n, int v) {
130  int i; for(i=0;i<n;i++) if(a[i]!=v) return 0;
131  return 1;
132 }
133 
135  JO jo = json_object_new_object();
136  int n = ld->nrays;
137 
138  jo_add_int(jo, "nrays", ld->nrays);
139  jo_add_double(jo, "min_theta", ld->min_theta);
140  jo_add_double(jo, "max_theta", ld->max_theta);
141 
142  jo_add_double_array(jo, "odometry", ld->odometry, 3);
143  jo_add_double_array(jo, "estimate", ld->estimate, 3);
144  jo_add_double_array(jo, "true_pose", ld->true_pose, 3);
145 
146  jo_add_double_array(jo, "theta", ld->theta, n);
147  jo_add_double_array(jo, "readings", ld->readings, n);
148  jo_add_double_array_if_not_nan(jo, "readings_sigma", ld->readings_sigma, n);
149 
150  jo_add_int_array(jo, "valid", ld->valid, n);
151 
152  if(!all_is(ld->cluster, n, -1))
153  jo_add_int_array(jo, "cluster", ld->cluster, n);
154 
155  jo_add_double_array_if_not_nan(jo, "alpha", ld->alpha, n);
156  jo_add_double_array_if_not_nan(jo, "cov_alpha", ld->cov_alpha, n);
157 
158  if(!all_is(ld->alpha_valid, n, 0))
159  jo_add_int_array(jo, "alpha_valid", ld->alpha_valid, n);
160 
161  jo_add_double_array_if_not_nan(jo, "true_alpha", ld->true_alpha, n);
162 
163  int timeval[2] = {ld->tv.tv_sec, ld->tv.tv_usec};
164  jo_add_int_array(jo, "timestamp", timeval, 2);
165 
166  return jo;
167 }
168 
169 /* TODO: add some checks */
171  int n;
172  if(!jo_read_int(jo, "nrays", &n)) {
173  sm_error("Could not read nrays.\n");
174  return 0;
175  }
176 
177  LDP ld = ld_alloc_new(n);
178 
179  jo_read_double(jo, "min_theta", &ld->min_theta);
180  jo_read_double(jo, "max_theta", &ld->max_theta);
181  /* XXX add more checks */
182  jo_read_double_array(jo, "theta", ld->theta, n, NAN);
183  jo_read_double_array(jo, "readings", ld->readings, n, NAN);
184 
185  if(jo_has_field(jo,"readings_sigma") && !jo_read_double_array(jo, "readings_sigma", ld->readings_sigma, n, NAN))
186  { sm_error("Error while reading field 'readings_sigma'.\n"); return 0; }
187 
188  jo_read_int_array(jo, "valid", ld->valid, n, 0);
189  jo_read_int_array(jo, "cluster", ld->cluster, n, -1);
190 
191  if(jo_has_field(jo,"alpha") && !jo_read_double_array(jo, "alpha",ld->alpha, n, NAN))
192  { sm_error("Error while reading field alpha.\n"); return 0; }
193 
194  if(jo_has_field(jo,"cov_alpha") && !jo_read_double_array(jo, "cov_alpha", ld->cov_alpha, n, NAN))
195  { sm_error("Error while reading field cov_alpha.\n"); return 0; }
196 
197  if(jo_has_field(jo,"alpha_valid") && !jo_read_int_array(jo, "alpha_valid", ld->alpha_valid, n, 0))
198  { sm_error("Error while reading field alpha_valid.\n"); return 0; }
199 
200  if(jo_has_field(jo,"true_alpha") && !jo_read_double_array(jo, "true_alpha", ld->true_alpha, n, NAN))
201  { sm_error("Error while reading field true_alpha.\n"); return 0; }
202 
203 
204  jo_read_double_array(jo, "odometry", ld->odometry, 3, NAN);
205  jo_read_double_array(jo, "estimate", ld->estimate, 3, NAN);
206  jo_read_double_array(jo, "true_pose", ld->true_pose, 3, NAN);
207 
208  int array[2] = {-1,-1};
209  jo_read_int_array(jo, "timestamp", array, 2,-1);
210  ld->tv.tv_sec = array[0];
211  ld->tv.tv_usec = array[1];
212 
213  return ld;
214 }
215 
216 LDP ld_from_json_string(const char*s) {
217  JO jo = json_parse(s);
218  if(!jo) {
219  sm_error("Invalid JSON found.\n");
220  return 0;
221  }
222  LDP ld = json_to_ld(jo);
223  if(!ld) {
224  sm_error("Could not read laser_data:\n\n%s\n", json_object_to_json_string(jo));
225  return 0;
226  }
227  jo_free(jo);
228  return ld;
229 }
230 
232  JO jo; /* the monkey */
233  LDP ld;
234 
235  jo = json_read_stream(file);
236  if(!jo) {
237  if(!feof(file)) {
238  fprintf(stderr, " (!)\n");
239  sm_error("Invalid JSON found.\n");
240  }
241  fprintf(stderr, " EOF\n");
242 
243  return 0;
244  }
245 
246  ld = json_to_ld(jo);
247  if(!ld) {
248  sm_error("Could not read laser_data:\n\n%s\n", json_object_to_json_string(jo));
249  return 0;
250  }
251  jo_free(jo);
252 
253  fprintf(stderr, "l");
254 
255  return ld;
256 }
257 
258 void ld_write_as_json(LDP ld, FILE * stream) {
259  if(!ld_valid_fields(ld)) {
260  sm_error("Writing bad data to the stream.\n");
261  }
262 
263  JO jo = ld_to_json(ld);
264  fputs(json_object_to_json_string(jo), stream);
265  fputs("\n", stream);
266  jo_free(jo);
267 }
268 
double *restrict cov_alpha
Definition: laser_data.h:29
int *restrict valid
Definition: laser_data.h:23
double true_pose[3]
Definition: laser_data.h:38
#define NAN
Definition: math_utils.h:11
LDP json_to_ld(JO jo)
int nvalid
Definition: algos.h:148
JO vector_to_json(gsl_vector *vec)
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
double *restrict readings_sigma
Definition: laser_data.h:32
int is_all_nan(const double *v, int n)
void jo_add_timestamp(JO jo, const char *name, struct timeval *tv)
int *restrict cluster
Definition: laser_data.h:26
int iterations
Definition: algos.h:146
void jo_add_int_array(JO root, const char *name, const int *v, int n)
int jo_read_double(JO jo, const char *name, double *p)
double *restrict theta
Definition: laser_data.h:21
#define jo_new
int ld_valid_fields(LDP ld)
Definition: laser_data.c:179
enum correspondence::@5 type
double error
Definition: algos.h:150
int valid
Definition: algos.h:140
#define jo_add
int json_object_array_add(struct json_object *this, struct json_object *val)
Definition: json_object.c:522
double *restrict true_alpha
Definition: laser_data.h:34
gsl_matrix * dx_dy2_m
Definition: algos.h:156
#define jo_new_null()
#define m(v1, v2)
Definition: egsl_macros.h:13
double *restrict readings
Definition: laser_data.h:24
JO result_to_json(struct sm_params *p, struct sm_result *r)
LDP ld_from_json_stream(FILE *file)
struct @0 p
const char * json_object_to_json_string(struct json_object *this)
Definition: json_object.c:199
int *restrict alpha_valid
Definition: laser_data.h:30
struct json_object * json_object_new_array()
Definition: json_object.c:495
JO ld_to_json(LDP ld)
LDP laser_ref
Definition: algos.h:14
JO matrix_to_json(gsl_matrix *m)
int jo_read_double_array(JO s, const char *name, double *p, int n, double when_null)
LDP laser_sens
Definition: algos.h:16
struct json_object * json_object_new_object()
Definition: json_object.c:248
void jo_add_double_array(JO root, const char *name, const double *v, int n)
void jo_add_double_array_if_not_nan(JO jo, const char *name, const double *v, int n)
JO json_parse(const char *str)
double estimate[3]
Definition: laser_data.h:40
JO corr_to_json(struct correspondence *corr, int n)
struct timeval tv
Definition: laser_data.h:50
int all_is(int *a, int n, int v)
#define jo_new_array
#define jo_array_add
double x[3]
Definition: algos.h:143
void jo_add_double(JO root, const char *name, double v)
int do_compute_covariance
Definition: algos.h:114
int json_to_corr(JO array, struct correspondence *corr, int n)
JO json_read_stream(FILE *f)
gsl_matrix * cov_x_m
Definition: algos.h:154
LDP ld_alloc_new(int nrays)
Definition: laser_data.c:12
double *restrict alpha
Definition: laser_data.h:28
struct json_object * json_object_array_get_idx(struct json_object *this, int idx)
Definition: json_object.c:535
LDP ld_from_json_string(const char *s)
void sm_error(const char *msg,...)
Definition: logging.c:49
int jo_read_int_array(JO s, const char *name, int *p, int n, int when_null)
int jo_read_int(JO jo, const char *name, int *p)
gsl_matrix * dx_dy1_m
Definition: algos.h:155
void jo_add_int(JO root, const char *name, int v)
JO jo_double_or_null(double v)
int jo_has_field(JO s, const char *name)
#define jo_free


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