laser_data_drawing.c
Go to the documentation of this file.
1 #include <string.h>
2 #include <strings.h>
3 #include "laser_data_drawing.h"
4 #include "logging.h"
5 #include "math_utils.h"
6 
7 const char*ld_reference_name[4] = { "invalid","odometry","estimate","true_pose"};
8 
10  return ld_reference_name[r];
11 }
12 
14  int i; for(i=1;i<=3;i++)
15  if(!strcasecmp(s, ld_reference_to_string( (ld_reference) i) ))
16  return (ld_reference) i;
17 
18  sm_error("Could not translate string '%s' to a reference name.\n", s);
19  return Invalid;
20 }
21 
22 int ld_get_bounding_box(LDP ld, double bb_min[2], double bb_max[2],
23  double pose[3], double horizon) {
24 
25  int rays_used = 0;
26  int i; for(i=0;i<ld->nrays;i++) {
27  if(!ld->valid[i]) continue;
28  if(ld->readings[i]>horizon) continue;
29 
30  double p0[2] = {
31  cos(ld->theta[i]) * ld->readings[i],
32  sin(ld->theta[i]) * ld->readings[i]
33  };
34 
35  double p[2];
36  transform_d(p0,pose,p);
37 
38  if(0 == rays_used) {
39  bb_min[0] = bb_max[0] = p[0];
40  bb_min[1] = bb_max[1] = p[1];
41  } else {
42  int j=0; for(j=0;j<2;j++) {
43  bb_min[j] = GSL_MIN(bb_min[j], p[j]);
44  bb_max[j] = GSL_MAX(bb_max[j], p[j]);
45  }
46  }
47 
48  rays_used++;
49  }
50 
51  return rays_used > 3;
52 }
53 
54 /*void lda_get_bb2(LDP *ld, int nld, BB2 bb2, ld_reference use_reference, double horizon) {
55  double bb_min[2], bb_max[2], offset[3] = {0,0,0};
56  lda_get_bounding_box(ld,nld, bb2->bb_)
57 }*/
58 
59 
60 void lda_get_bounding_box(LDP *lda, int nld, double bb_min[2], double bb_max[2],
61  double offset[3], ld_reference use_reference, double horizon) {
62 
63  int k;
64  for(k=0;k<nld;k++) {
65  LDP ld = lda[k];
66 
67  double *ref = ld_get_reference_pose(ld, use_reference);
68  if(!ref) {
69  sm_error("Pose %s not set in scan #%d.\n",
70  ld_reference_to_string(use_reference), k);
71  continue;
72  }
73 
74  double pose[3];
75  oplus_d(offset, ref, pose);
76 
77  if(k==0)
78  ld_get_bounding_box(ld, bb_min, bb_max, pose, horizon);
79  else {
80  double this_min[2], this_max[2];
81  ld_get_bounding_box(ld, this_min, this_max, pose, horizon);
82  int i; for(i=0;i<2;i++) {
83  bb_min[i] = GSL_MIN(bb_min[i], this_min[i]);
84  bb_max[i] = GSL_MAX(bb_max[i], this_max[i]);
85  }
86  }
87  }
88 }
89 
90 double * ld_get_reference_pose_silent(LDP ld, ld_reference use_reference) {
91  double * pose;
92  switch(use_reference) {
93  case Odometry: pose = ld->odometry; break;
94  case Estimate: pose = ld->estimate; break;
95  case True_pose: pose = ld->true_pose; break;
96  default:
97  sm_error("Could not find pose identified by %d.\n", (int) use_reference);
98  return 0;
99  }
100  return pose;
101 }
102 
103 double * ld_get_reference_pose(LDP ld, ld_reference use_reference) {
104  double * pose = ld_get_reference_pose_silent(ld, use_reference);
105  if(any_nan(pose, 3)) {
106  sm_error("Required field '%s' not set in laser scan.\n",
107  ld_reference_to_string(use_reference) );
108  return 0;
109  }
110  return pose;
111 }
112 
113 
114 void compute_stroke_sequence(LDP ld, struct stroke_sequence*draw_info,
115  double horizon, double connect_threshold) {
116  int last_valid = -1; int first = 1;
117  int i; for(i=0;i<ld->nrays;i++) {
118  if( (!ld_valid_ray(ld,i)) || (ld->readings[i] > horizon) ) {
119  draw_info[i].valid = 0;
120  continue;
121  }
122  draw_info[i].valid = 1;
123  draw_info[i].p[0] = ld->readings[i] * cos(ld->theta[i]);
124  draw_info[i].p[1] = ld->readings[i] * sin(ld->theta[i]);
125 
126  if(first) {
127  first = 0;
128  draw_info[i].begin_new_stroke = 1;
129  draw_info[i].end_stroke = 0;
130  } else {
131  int near = square(connect_threshold) >
132  distance_squared_d(draw_info[last_valid].p, draw_info[i].p);
133  draw_info[i].begin_new_stroke = near ? 0 : 1;
134  draw_info[i].end_stroke = 0;
135  draw_info[last_valid].end_stroke = draw_info[i].begin_new_stroke;
136  }
137  last_valid = i;
138  } /*for */
139  if(last_valid >= 0)
140  draw_info[last_valid].end_stroke = 1;
141 } /* find buff .. */
142 
143 
144 
int *restrict valid
Definition: laser_data.h:23
double true_pose[3]
Definition: laser_data.h:38
double distance_squared_d(const double a[2], const double b[2])
Definition: math_utils.c:48
void transform_d(const double point2d[2], const double pose[3], double result2d[2])
Definition: math_utils.c:108
double odometry[3]
Definition: laser_data.h:39
const char * ld_reference_name[4]
const char * ld_reference_to_string(ld_reference r)
void compute_stroke_sequence(LDP ld, struct stroke_sequence *draw_info, double horizon, double connect_threshold)
double *restrict theta
Definition: laser_data.h:21
INLINE int ld_valid_ray(LDP ld, int i)
ld_reference
double * ld_get_reference_pose(LDP ld, ld_reference use_reference)
double *restrict readings
Definition: laser_data.h:24
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
void oplus_d(const double x1[3], const double x2[3], double res[3])
Definition: math_utils.c:96
double * ld_get_reference_pose_silent(LDP ld, ld_reference use_reference)
double estimate[3]
Definition: laser_data.h:40
int any_nan(const double *d, int n)
Definition: math_utils.c:64
double square(double x)
Definition: math_utils.c:124
void sm_error(const char *msg,...)
Definition: logging.c:49
int ld_get_bounding_box(LDP ld, double bb_min[2], double bb_max[2], double pose[3], double horizon)
ld_reference ld_string_to_reference(const char *s)


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