ld_recover.c
Go to the documentation of this file.
1 #include <math.h>
2 #include <options/options.h>
3 
4 #include "../csm/csm_all.h"
5 #include "../csm/laser_data_drawing.h"
6 
8 int same_scan(LDP ld1, LDP ld2);
10 const char * short_desc(LDP ld);
11 
12 int main(int argc, const char * argv[]) {
13  sm_set_program_name(argv[0]);
14 
15  const char *in_filename;
16  const char *ref_filename;
17  const char *out_filename;
18  const char *ref_field_string; ld_reference ref_field;
19  const char *out_field_string; ld_reference out_field;
20 
21  struct option* ops = options_allocate(15);
22  options_string(ops, "in", &in_filename, "stdin", "scan matching log");
23  options_string(ops, "ref", &ref_filename, "ref.log", "slam log");
24  options_string(ops, "out", &out_filename, "stdout", "output file");
25 
26  options_string(ops, "ref_field", &ref_field_string, "estimate", "What field to find in ref.");
27  options_string(ops, "out_field", &out_field_string, "true_pose", "What field to copy to.");
28 
29  if(!options_parse_args(ops, argc, argv)) {
30  fprintf(stderr, " This program works on two logs: A and B. "
31  "For each scan in A, the program searches for the scan in B having the same timestamp. "
32  "Then, the true_pose field in B is copied to the scan form A, and it is written to the output.\n");
33  options_print_help(ops, stderr);
34  return -1;
35  }
36 
37  ref_field = ld_string_to_reference(ref_field_string);
38  out_field = ld_string_to_reference(out_field_string);
39 
40 
41  FILE * in_stream = open_file_for_reading(in_filename);
42  FILE * ref_stream = open_file_for_reading(ref_filename);
43  FILE * out_stream = open_file_for_writing(out_filename);
44 
45  if(!in_stream || !ref_stream || !out_stream) return -1;
46 
47  LDP ld_in;
48  while((ld_in = ld_read_smart(in_stream))) {
49  int matched = 0;
50  while(1) {
51  LDP ld_ref = ld_read_smart(ref_stream);
52  if(!ld_ref) break;
53  if(same_scan(ld_in, ld_ref)) {
54  matched = 1;
55  const double *ref_pose = ld_get_reference_pose(ld_ref, ref_field);
56  double *out_pose = ld_get_reference_pose_silent(ld_in, out_field);
57  copy_d(ref_pose, 3, out_pose);
58  ld_write_as_json(ld_in, out_stream);
59  fputs("\n", out_stream);
60  break;
61  }
62  ld_free(ld_ref);
63  }
64 
65  if(!matched) {
66  sm_error("Could not match %s. \n", short_desc(ld_in));
67  if(feof(ref_stream)) {
68  sm_error("..because ref stream has ended.\n");
69  break;
70  }
71  continue;
72  }
73 
74  ld_free(ld_in);
75  }
76 
77  return 0;
78 }
79 
80 
82 int same_scan(LDP ld1, LDP ld2) {
83  return (ld1->tv.tv_sec == ld2->tv.tv_sec) &&
84  (ld1->tv.tv_usec == ld2->tv.tv_usec);
85 }
86 
87 char buf[100];
88 const char * short_desc(LDP ld) {
89  sprintf(buf, "LD, tv=%d,%d", (int) ld->tv.tv_sec, (int) ld->tv.tv_usec);
90  return buf;
91 }
92 
void sm_set_program_name(const char *name)
Definition: logging.c:21
void ld_write_as_json(LDP ld, FILE *stream)
void copy_d(const double *from, int n, double *to)
Definition: math_utils.c:83
ld_reference
int same_scan(LDP ld1, LDP ld2)
Definition: ld_recover.c:82
struct option * ops
Definition: rb_sm.c:31
FILE * open_file_for_writing(const char *filename)
Definition: utils.c:25
LDP ld_read_smart(FILE *)
Definition: options.h:49
double * ld_get_reference_pose(LDP ld, ld_reference use_reference)
void ld_free(LDP ld)
Definition: laser_data.c:87
struct option * options_allocate(int n)
int main(int argc, const char *argv[])
Definition: ld_recover.c:12
FILE * open_file_for_reading(const char *filename)
Definition: utils.c:19
void options_print_help(struct option *options, FILE *f)
Definition: options.c:398
double * ld_get_reference_pose_silent(LDP ld, ld_reference use_reference)
struct timeval tv
Definition: laser_data.h:50
void options_string(struct option *, const char *name, const char **p, const char *def_balue, const char *desc)
const char * short_desc(LDP ld)
Definition: ld_recover.c:88
void sm_error(const char *msg,...)
Definition: logging.c:49
char buf[100]
Definition: ld_recover.c:87
int options_parse_args(struct option *ops, int argc, const char *argv[])
Definition: options.c:66
ld_reference ld_string_to_reference(const char *s)


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