2 #include <gsl/gsl_nan.h> 6 int minmax(
int from,
int to,
int x) {
7 return GSL_MAX(GSL_MIN(x,to),from);
11 const double *p_i_w,
LDP ld,
12 double max_angular_correction_deg,
double max_linear_correction,
int*from,
int*to,
int*start_cell)
17 double delta = fabs(
deg2rad(max_angular_correction_deg)) +
18 fabs(atan(max_linear_correction/
norm_d(p_i_w)));
21 int range = (int) ceil(delta/angle_res);
24 double start_theta = atan2(p_i_w[1], p_i_w[0]);
28 if(start_theta<ld->min_theta) start_theta += 2*
M_PI;
38 printf(
"from: %d to: %d delta: %f start_theta: %f min/max theta: [%f,%f] range: %d start_cell: %d\n",
55 double distance_d(
const double a[2],
const double b[2]) {
61 return v == v ? 0 : 1;
65 int i;
for(i=0;i<n;i++)
72 return sqrt(p[0]*p[0]+p[1]*p[1]);
76 return deg * (
M_PI / 180);
80 return rad * (180 /
M_PI);
83 void copy_d(
const double*from,
int n,
double*to) {
84 int i;
for(i=0;i<n;i++) to[i] = from[i];
87 void ominus_d(
const double x[3],
double res[3]) {
90 res[0] = -c*x[0]-s*x[1];
91 res[1] = s*x[0]-c*x[1];
96 void oplus_d(
const double x1[3],
const double x2[3],
double res[3]) {
97 double c = cos(x1[2]);
98 double s = sin(x1[2]);
99 double x = x1[0]+c*x2[0]-s*x2[1];
100 double y = x1[1]+s*x2[0]+c*x2[1];
101 double theta = x1[2]+x2[2];
109 double theta = pose[2];
110 double c = cos(theta);
double s = sin(theta);
111 result2d[0] = pose[0] + c * point2d[0] - s * point2d[1];
112 result2d[1] = pose[1] + s * point2d[0] + c * point2d[1];
115 void pose_diff_d(
const double pose2[3],
const double pose1[3],
double res[3]) {
120 while(res[2] > +
M_PI) res[2] -= 2*
M_PI;
121 while(res[2] < -
M_PI) res[2] += 2*
M_PI;
140 double t0 = a[0]-b[0];
141 double t1 = a[1]-b[1];
142 double one_on_r = 1 / sqrt(t0*t0+t1*t1);
144 double nx = t1 * one_on_r ;
145 double ny = -t0 * one_on_r ;
146 double c= nx, s = ny;
147 double rho = c*a[0]+s*a[1];
149 res[0] = c*rho + s*s*p[0] - c*s*p[1] ;
150 res[1] = s*rho - c*s*p[0] + c*c*p[1] ;
153 *distance = fabs(rho-(c*p[0]+s*p[1]));
163 if ((proj[0]-a[0])*(proj[0]-b[0]) +
164 (proj[1]-a[1])*(proj[1]-b[1]) < 0 ) {
175 double projection[2];
178 return distance_sq_d;
184 if ((proj[0]-a[0])*(proj[0]-b[0]) +
185 (proj[1]-a[1])*(proj[1]-b[1]) < 0 ) {
194 for(i=0;i<n;i++)
if(value == v[i]) num++;
200 sm_error(
"Passed NAN to normalize_0_2PI().\n");
203 while(t<0) t+=2*
M_PI;
208 double dot_d(
const double p[2],
const double q[2]);
211 double dot_d(
const double p[2],
const double q[2]) {
212 return p[0]*q[0] + p[1]*q[1];
218 int segment_ray_tracing(
const double p0[2],
const double p1[2],
const double eye[2],
double direction,
double*range) {
223 double arrow[2] = {p1[0]-p0[0],p1[1]-p0[1]};
225 double S[2] = { -arrow[1], arrow[0]};
227 double N[2] = { cos(direction), sin(direction)};
229 double S_dot_N =
dot_d(S,N);
230 if( S_dot_N == 0)
return 0;
232 double line_rho =
dot_d(p0,S);
234 double eye_rho =
dot_d(eye,S);
236 double dist = (line_rho - eye_rho) / S_dot_N;
237 if(dist<=0)
return 0;
243 double crossing[2] = {eye[0] + N[0]*dist, eye[1]+N[1]*dist};
245 double midpoint[2] = { 0.5*(p1[0]+p0[0]),0.5*(p1[1]+p0[1])};
248 double dist_to_midpoint =
distance_d(crossing, midpoint);
250 if(dist_to_midpoint > seg_size/2 )
258 double arrow[2] = {p1[0]-p0[0],p1[1]-p0[1]};
260 double S[2] = { -arrow[1], arrow[0]};
261 return atan2(S[1], S[0]);
267 sprintf(
tmp_buf,
"(%4.2f mm, %4.2f mm, %4.4f deg)",
268 1000*pose[0],1000*pose[1],
rad2deg(pose[2]));
double norm_d(const double p[2])
double deg2rad(double deg)
double distance_squared_d(const double a[2], const double b[2])
void transform_d(const double point2d[2], const double pose[3], double result2d[2])
void copy_d(const double *from, int n, double *to)
double angleDiff(double a, double b)
const char * friendly_pose(const double *pose)
double normalize_0_2PI(double t)
double dist_to_segment_squared_d(const double a[2], const double b[2], const double x[2])
int count_equal(const int *v, int n, int value)
double max_in_array(const double *v, int n)
int minmax(int from, int to, int x)
double distance_d(const double a[2], const double b[2])
double segment_alpha(const double p0[2], const double p1[2])
double dot_d(const double p[2], const double q[2])
void projection_on_line_d(const double a[2], const double b[2], const double p[2], double res[2], double *distance)
double dist_to_segment_d(const double a[2], const double b[2], const double x[2])
void oplus_d(const double x1[3], const double x2[3], double res[3])
int segment_ray_tracing(const double p0[2], const double p1[2], const double eye[2], double direction, double *range)
void ominus_d(const double x[3], double res[3])
int any_nan(const double *d, int n)
void possible_interval(const double *p_i_w, LDP ld, double max_angular_correction_deg, double max_linear_correction, int *from, int *to, int *start_cell)
void pose_diff_d(const double pose2[3], const double pose1[3], double res[3])
void projection_on_segment_d(const double a[2], const double b[2], const double x[2], double proj[2])
void sm_error(const char *msg,...)
static char tmp_buf[1024]
double rad2deg(double rad)
double distance(const gsl_vector *a, const gsl_vector *b)