log2pdf.c
Go to the documentation of this file.
1 #include <time.h>
2 #include <string.h>
3 
4 
5 #ifdef LINUX
6 #include <linux/limits.h>
7 #endif
8 
9 #include <cairo.h>
10 #include <cairo-pdf.h>
11 
12 #include <options/options.h>
13 
14 #include "../csm/csm_all.h"
15 #include "../csm/laser_data_drawing.h"
16 #include "../csm/laser_data_cairo.h"
17 
18 
19 typedef struct {
20  const char*use;
21  double padding;
22  double dimension;
23 
26 
27  const char*output_filename;
28  const char*input_filename;
29 
31 
33 
34  /* Drawing style for scans */
36  /* Drawing style for robot path */
39 
40  double distance_xy, distance_th_deg;
42 
44 
45 double offset_theta = 0;
46 
47 const char* banner =
48  "This program draws laser scans.\n"
49  "\n"
50  "IMPORTANT: it is likely you have to set one or more parameters. \n"
51  " the default parameters are OK to draw very long laser logs\n\n";
52 
53 int main(int argc, const char* argv[]) {
54  sm_set_program_name(argv[0]);
55 
57 
58  lds_set_defaults(&(p.laser));
60 
61  p.laser.rays.draw = 0;
62  p.laser.points.draw = 0;
63  p.laser.normals.draw = 0;
64  p.laser.countour.width = 0.1;
65  p.pose_path.width = 0.1;
66  p.pose_path.color = "#f00";
67 
69  struct option * ops = options_allocate(100);
70  options_string(ops, "in", &p.input_filename, "stdin", "input file (Carmen or JSON)");
71  options_string(ops, "out", &p.output_filename, "", "output file (if empty, input file + '.pdf')");
72  options_double(ops, "padding", &p.padding, 0.2, "padding around bounding box (m)");
73  options_double(ops, "dimension", &p.dimension, 500.0, "dimension of the image (points)");
74  options_double(ops, "offset_theta_deg", &p.offset_theta_deg, 0.0, " rotate entire map by this angle (deg) ");
75 
76  options_string(ops, "use", &p.use, "estimate", "One in 'odometry','estimate','true_pose'");
77  options_double(ops, "distance_xy", &p.distance_xy, 5.0, " Minimum distance between scans (m) ");
78  options_double(ops, "distance_th_deg", &p.distance_th_deg, 45.0, " Minimum distance between scans (deg) ");
79  options_double(ops, "start_pose_width", &p.start_pose_width, 0.4, "First pose | Circle width");
80  lds_add_options(&(p.laser), ops, "laser_", "");
81  ls_add_options(&(p.pose_path), ops, "path_", "");
82 
83  if(!options_parse_args(ops, argc, argv)) {
84  sm_error("Could not parse arguments.\n");
85  options_print_help(ops, stderr);
86  return -1;
87  }
88 
89  /* If out not specified */
90  if(strlen(p.output_filename)==0) {
91  char buf[PATH_MAX];
92  sprintf(buf, "%s.pdf", p.input_filename);
93  p.output_filename = my_strdup(buf);
94 /* sm_info("Writing on file '%s'.\n", p.output_filename);*/
95  }
96 
98  if(Invalid == p.use_reference) {
99  sm_error("Invalid reference '%s'. "
100  "Use one in 'odometry','estimate','true_pose'.\n", p.use);
101  return -1;
102  }
103 /* sm_info("Using reference: %s.\n", ld_reference_to_string(p.use_reference));*/
104 
105  return !log2pdf(&p);
106 }
107 
108 
110 
112  FILE *input_file = open_file_for_reading(p->input_filename);
113  if(!input_file) return 0;
114 
115  LDP*scans; int nscans;
116 
117  if(!ld_read_some_scans_distance(input_file, &scans, &nscans,
119  sm_error("Could not read map from file '%s'.\n", p->input_filename);
120  return 0;
121  }
122 
123  if(nscans == 0) {
124  sm_error("I could not read any scan from file '%s'.\n", p->input_filename);
125  return 0;
126  }
127 
128  sm_debug("Read map: %d scans in total.\n", nscans);
129 
131  double bb_min[2], bb_max[2];
132  double offset[3] = {0,0,0};
133  lda_get_bounding_box(scans, nscans, bb_min, bb_max, offset, p->use_reference, p->laser.horizon);
134 
135  bb_min[0] -= p->padding;
136  bb_min[1] -= p->padding;
137  bb_max[0] += p->padding;
138  bb_max[1] += p->padding;
139 
140 
141  sm_debug("Bounding box: %f %f -- %f %f.\n", bb_min[0], bb_min[1],
142  bb_max[0], bb_max[1]);
143 
144 
145  /* Create PDF surface and setup paper size and transformations */
146  int max_width_points = p->dimension;
147  int max_height_points = p->dimension;
148  cairo_surface_t *surface;
149  cairo_t *cr;
150 
151  if(!create_pdf_surface(p->output_filename, max_width_points, max_height_points,
152  bb_min, bb_max, &surface, &cr)) return 0;
153 
154  /* Draw pose path */
155  if(p->pose_path.draw) {
156  cairo_save(cr);
157 
158  cr_set_style(cr, &(p->pose_path));
159  cr_lda_draw_pose_path(cr, scans, nscans, p->use_reference);
160 
161  if(nscans > 0 && p->laser.pose.draw) {
162  cairo_set_source_rgb(cr, 0.3, 0.0, 1.0);
163  double *pose0 = ld_get_reference_pose(scans[0], p->use_reference);
164  cairo_arc(cr, pose0[0], pose0[1], p->start_pose_width, 0.0, 2*M_PI);
165  cairo_fill(cr);
166  }
167 
168  cairo_restore(cr);
169  }
170 
171  /* Draw map */
172  int k; for(k=0;k<nscans;k++) {
173  LDP ld = scans[k];
174  double *pose = ld_get_reference_pose(ld, p->use_reference);
175  if(!pose) continue;
176 
177  double offset[3] = {0,0, deg2rad(p->offset_theta_deg) };
178  double world_pose[3];
179  oplus_d(offset, pose, world_pose);
180 
181  cairo_save(cr);
182  cr_set_reference(cr, world_pose);
183  cr_ld_draw(cr, ld, &(p->laser));
184  cairo_restore(cr);
185  }
186 
187  cairo_show_page (cr);
188 
189  cairo_destroy (cr);
190  cairo_surface_destroy (surface);
191  return 1;
192 }
193 
194 
195 
196 
const char * use
Definition: log2pdf.c:20
void options_banner(const char *message)
Definition: options.c:33
double horizon
line_style points
#define deg2rad(x)
Definition: gpc_test.c:22
void sm_set_program_name(const char *name)
Definition: logging.c:21
ld_reference use_reference
Definition: log2pdf.c:30
void ls_add_options(line_style *ls, struct option *ops, const char *prefix, const char *desc_prefix)
void cr_lda_draw_pose_path(cairo_t *cr, LDP *scans, int nscans, ld_reference use_reference)
void lds_add_options(ld_style *lds, struct option *ops, const char *prefix, const char *desc_prefix)
int main(int argc, const char *argv[])
Definition: log2pdf.c:53
line_style countour
const char * output_filename
Definition: log2pdf.c:27
double distance_xy
Definition: log2pdf.c:40
double start_pose_width
Definition: log2pdf.c:38
int create_pdf_surface(const char *file, int max_width_points, int max_height_points, double bb_min[2], double bb_max[2], cairo_surface_t **surface_p, cairo_t **cr)
int draw_confidence
Definition: log2pdf.c:24
line_style rays
void options_double(struct option *, const char *name, double *p, double def_value, const char *desc)
ld_reference
const char * color
struct option * ops
Definition: rb_sm.c:31
#define M_PI
Definition: math_utils.h:7
ld_style laser
Definition: log2pdf.c:35
void cr_set_reference(cairo_t *cr, double *pose)
double distance_th_deg
Definition: log2pdf.c:40
Definition: options.h:49
double * ld_get_reference_pose(LDP ld, ld_reference use_reference)
line_style normals
line_style pose_path
Definition: log2pdf.c:37
void * k
Definition: linkhash.h:52
double offset_theta
Definition: log2pdf.c:45
void cr_ld_draw(cairo_t *cr, LDP ld, ld_style *p)
struct option * options_allocate(int n)
void lda_get_bounding_box(LDP *lda, int nld, double bb_min[2], double bb_max[2], double offset[3], ld_reference use_reference, double horizon)
struct @0 p
const char * banner
Definition: log2pdf.c:47
double offset_theta_deg
Definition: log2pdf.c:32
FILE * open_file_for_reading(const char *filename)
Definition: utils.c:19
void oplus_d(const double x1[3], const double x2[3], double res[3])
Definition: math_utils.c:96
int log2pdf(log2pdf_params *p)
Definition: log2pdf.c:109
void options_print_help(struct option *options, FILE *f)
Definition: options.c:398
void ls_set_defaults(line_style *ls)
double dimension
Definition: log2pdf.c:22
void sm_debug(const char *msg,...)
Definition: logging.c:88
char * my_strdup(const char *s)
Definition: utils.c:61
void options_string(struct option *, const char *name, const char **p, const char *def_balue, const char *desc)
const char * input_filename
Definition: log2pdf.c:28
void cr_set_style(cairo_t *cr, line_style *ls)
int ld_read_some_scans_distance(FILE *file, LDP **array, int *num, ld_reference which, double d_xy, double d_th)
void sm_error(const char *msg,...)
Definition: logging.c:49
double padding
Definition: log2pdf.c:21
line_style pose
void lds_set_defaults(ld_style *lds)
char buf[100]
Definition: ld_recover.c:87
int options_parse_args(struct option *ops, int argc, const char *argv[])
Definition: options.c:66
double confidence_mult
Definition: log2pdf.c:25
ld_reference ld_string_to_reference(const char *s)


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