raytracer.cpp
Go to the documentation of this file.
1 #include "raytracer.h"
2 
4  const char * file_map;
5  const char * file_poses;
6  const char * file_output;
7  double fov_deg;
8  int nrays;
9  double max_reading;
10 };
11 
12 
13 bool load_env_from_json(Environment& env, JO jo);
14 double cosine_between_angles(double a1, double a2);
15 
16 
17 int main(int argc, const char** argv)
18 {
19  sm_set_program_name(argv[0]);
20  struct raytracer_params p;
21 
22  struct option* ops = options_allocate(60);
23  options_string(ops, "map", &p.file_map, "map.json", "Environment description");
24  options_string(ops, "poses", &p.file_poses, "-", "List of poses");
25  options_string(ops, "out", &p.file_output, "stdout", "Output file ");
26  options_int(ops, "nrays", &p.nrays, 181, "Number of rays");
27  options_double(ops, "fov_deg", &p.fov_deg, 180.0, "Field of view (degrees)");
28  options_double(ops, "max_reading", &p.max_reading, 80.0, "Sensor horizon (meters)");
29 
30  if(!options_parse_args(ops, argc, argv)) {
31  sm_info(" simulats one scan from map. \n\nUsage:\n");
32  options_print_help(ops, stderr);
33  return -1;
34  }
35 
36 
38  if(!file_map) return -2;
39 
40  JO jo_map = json_read_stream(file_map);
41  if(!jo_map) {
42  sm_error("Could not read JSON from file '%s'\n", p.file_map);
43  return -3;
44  }
45  if(json_read_stream(file_map)) {
46  sm_error("I expect only 1 JSON object in file '%s'\n", p.file_map);
47  return -4;
48  }
49 
50  Environment env;
51 
52  load_env_from_json(env, jo_map);
53 
54 
56  if(!file_output) return -1;
57 
58 
60  if(!file_poses) return -2;
61 
62  JO jo_pose; int num = 0;
63  while( (jo_pose = json_read_stream(file_poses)) )
64  {
65  num++;
66 
67  LDP ld = ld_alloc_new(p.nrays);
68 
69  if(!jo_read_from_double_array (jo_pose, ld->true_pose, 3, GSL_NAN) || any_nan(ld->true_pose, 3)) {
70  sm_error("Bad pose: '%s'.\n", jo_to_string(jo_pose));
71  return -5;
72  }
73 
74 
75  double fov = deg2rad(p.fov_deg);
76 
77  ld->min_theta = -fov/2;
78  ld->max_theta = +fov/2;
79 
80  for(int i=0;i<ld->nrays;i++) {
81  ld->theta[i] = - fov/2 + fov * i / (ld->nrays-1);;
82 
83  double rho, alpha; int stuff_id;
84  if(env.ray_tracing(ld->true_pose, ld->theta[i] + ld->true_pose[2], rho, alpha, &stuff_id) && (rho<p.max_reading)) {
85 
86  ld->valid[i] = 1;
87  ld->readings[i] = rho;
88 
89  double relative_alpha = alpha-ld->true_pose[2];
90 
91  /* Make sure alpha points out of the wall */
92  if( cosine_between_angles(relative_alpha, ld->theta[i]) > 0) {
93  relative_alpha += M_PI;
94  }
95 
96  ld->true_alpha[i] = normalize_0_2PI(relative_alpha);
97 
98  } else {
99  ld->valid[i] = 0;
100  }
101 
102  }
103 
104  ld->tv.tv_sec = num;
105 
106 
107  JO jo = ld_to_json(ld);
108  fputs(jo_to_string(jo), file_output);
109  fputs("\n", file_output);
110  jo_free(jo);
111 
112  ld_free(ld);
113  }
114 
115  if(num == 0) {
116  sm_error("It looks like there wasn't any pose in the file '%s'\n", p.file_poses);
117  return -4;
118  }
119 
120  jo_free(jo_map);
121 }
122 
123 
124 
125 #define jo_expect_array(a) (a!=0 && json_object_is_type(a, json_type_array))
126 #define jo_expect_object(a) (a!=0 && json_object_is_type(a, json_type_object))
127 #define jo_expect_string(a) (a!=0 && json_object_is_type(a, json_type_string))
128 #define jo_expect_array_size(a,n) ( (a!=0) && (json_object_is_type(a, json_type_array)&& (jo_array_length(a)==n)))
129 #define jo_expect_array_size_min(a,n) ( (a!=0) && (json_object_is_type(a, json_type_array)&& (jo_array_length(a)>=n)))
130 
131 
132 #define expect(a) if(!a) { \
133  sm_error("Invalid format: \n\t %s \n", #a); \
134  return false; \
135  }
136 
137 #define expect_s(a, s) if(!a) { \
138  sm_error("Invalid format: %s \n\t %s \n", s, #a); \
139  return false; \
140  }
141 
142 double cosine_between_angles(double a1, double a2) {
143  return cos(a1)*cos(a2)+sin(a1)*sin(a2);
144 }
145 
146 
147 bool load_env_from_json(Environment& env, JO jo_map) {
148  jo_expect_object(jo_map);
149 
150  JO jo_objects = json_object_object_get(jo_map, "objects");
151  expect(jo_expect_array(jo_objects));
152 
153  for(int i=0; i < jo_array_length(jo_objects); i++) {
154  JO jo = jo_array_get(jo_objects, i);
156 
157  JO jo_type = jo_get(jo, "type");
158  expect(jo_expect_string(jo_type));
159 
160  if(!strcmp(jo_get_string(jo_type), "line")) {
161 
162  JO points = jo_get(jo, "points");
163  expect(jo_expect_array_size_min(points, 2));
164 
165 
166  for(int p=0;p<jo_array_length(points)-1;p++) {
167  Segment * s = new Segment();
168  expect(jo_read_from_double_array (jo_array_get(points, p ), s->p0, 2, NAN));
169  expect(jo_read_from_double_array (jo_array_get(points, p+1), s->p1, 2, NAN));
170  env.stuff.push_back(s);
171  }
172 
173  } else if(!strcmp(jo_get_string(jo_type), "square")) {
174 
175  JO corners = jo_get(jo, "corners");
176  expect_s(jo_expect_array_size(corners, 2), jo_to_string(corners));
177 
178  double pmin[2],pmax[2];
179 
180  expect(jo_read_from_double_array (jo_array_get(corners, 0 ), pmin, 2, NAN));
181  expect(jo_read_from_double_array (jo_array_get(corners, 1), pmax, 2, NAN));
182 
183  env.stuff.push_back(new Segment(pmin[0],pmin[1],pmax[0],pmin[1]));
184  env.stuff.push_back(new Segment(pmax[0],pmax[1],pmax[0],pmin[1]));
185  env.stuff.push_back(new Segment(pmax[0],pmax[1],pmin[0],pmax[1]));
186  env.stuff.push_back(new Segment(pmin[0],pmin[1],pmin[0],pmax[1]));
187 
188  } else {
189  sm_error("unknown object type: '%s'\n", jo_get_string(jo_type));
190  return false;
191  }
192  }
193 
194  return true;
195 }
196 
197 
bool ray_tracing(const double p[2], const double direction, double &out_distance, double &out_alpha, int *stuff_id) const
Definition: simplemap.cpp:28
#define jo_array_length
int *restrict valid
Definition: laser_data.h:23
std::vector< Stuff * > stuff
Definition: simplemap.h:53
#define deg2rad(x)
Definition: gpc_test.c:22
double true_pose[3]
Definition: laser_data.h:38
void sm_set_program_name(const char *name)
Definition: logging.c:21
#define NAN
Definition: math_utils.h:11
const char * file_map
Definition: raytracer.cpp:4
struct json_object * json_object_object_get(struct json_object *this, const char *key)
Definition: json_object.c:277
#define jo_to_string
double max_theta
Definition: laser_data.h:19
double min_theta
Definition: laser_data.h:18
double normalize_0_2PI(double t)
Definition: math_utils.c:198
double *restrict theta
Definition: laser_data.h:21
int main(int argc, const char **argv)
Definition: raytracer.cpp:17
void options_double(struct option *, const char *name, double *p, double def_value, const char *desc)
double cosine_between_angles(double a1, double a2)
Definition: raytracer.cpp:142
#define expect_s(a, s)
Definition: raytracer.cpp:137
struct option * ops
Definition: rb_sm.c:31
const char * file_output
Definition: raytracer.cpp:6
FILE * open_file_for_writing(const char *filename)
Definition: utils.c:25
#define M_PI
Definition: math_utils.h:7
double *restrict true_alpha
Definition: laser_data.h:34
#define expect(a)
Definition: raytracer.cpp:132
Definition: options.h:49
void ld_free(LDP ld)
Definition: laser_data.c:87
double *restrict readings
Definition: laser_data.h:24
bool load_env_from_json(Environment &env, JO jo)
Definition: raytracer.cpp:147
struct option * options_allocate(int n)
struct @0 p
void options_int(struct option *, const char *name, int *p, int def_value, const char *desc)
#define jo_get_string
JO ld_to_json(LDP ld)
double p1[2]
Definition: simplemap.h:30
#define jo_expect_array(a)
Definition: raytracer.cpp:125
FILE * open_file_for_reading(const char *filename)
Definition: utils.c:19
#define jo_expect_string(a)
Definition: raytracer.cpp:127
double max_reading
Definition: raytracer.cpp:9
#define jo_get
void options_print_help(struct option *options, FILE *f)
Definition: options.c:398
#define jo_expect_object(a)
Definition: raytracer.cpp:126
struct timeval tv
Definition: laser_data.h:50
double p0[2]
Definition: simplemap.h:30
const char * file_poses
Definition: raytracer.cpp:5
void sm_info(const char *msg,...)
Definition: logging.c:71
int any_nan(const double *d, int n)
Definition: math_utils.c:64
void options_string(struct option *, const char *name, const char **p, const char *def_balue, const char *desc)
JO json_read_stream(FILE *f)
#define jo_array_get
LDP ld_alloc_new(int nrays)
Definition: laser_data.c:12
int jo_read_from_double_array(JO jo, double *p, int n, double when_null)
void sm_error(const char *msg,...)
Definition: logging.c:49
#define jo_expect_array_size(a, n)
Definition: raytracer.cpp:128
int options_parse_args(struct option *ops, int argc, const char *argv[])
Definition: options.c:66
#define jo_expect_array_size_min(a, n)
Definition: raytracer.cpp:129
#define jo_free


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