6 #include "../csm_all.h" 52 b->
results[i] = (
double*) calloc(3,
sizeof(
double));
56 double zero[3] = {0,0,0};
83 b->
disp[0] = base_pose[0];
84 b->
disp[1] = base_pose[1];
85 b->
disp[2] = base_pose[2];
96 double rho = x1 * b->
cost[i] + y1 * b->
sint[i];
103 b->
ht[i][rho_index] += (1-fabs(alpha)) * weight;
106 b->
ht[i][rho_index+1] += (fabs(alpha)) * weight;
108 if( (alpha < 0) && (rho_index > 0))
109 b->
ht[i][rho_index-1] += (fabs(alpha)) * weight;
113 double mdax(
double a,
double b) {
120 *rho_index = 0; *alpha =
NAN;
121 if ( (rho <= b->rho_min) || (rho >= b->
rho_max) )
129 *rho_index = (int) floor( x );
130 *alpha = (*rho_index+0.5)-x;
132 assert(fabs(*alpha) <= 0.5001);
133 assert(*rho_index >= 0);
134 assert(*rho_index < b->num_linear_cells);
152 b->
hs[t] += b->
ht[t][r] * b->
ht[t][r];
159 clock_t hsm_match_start = clock();
177 sm_error(
"Cross correlation of spectra has 0 peaks.\n");
184 for(
int np=0;np<npeaks;np++) {
188 sm_debug(
"Theta hyp#%d: lag %d, angle %fdeg\n", np, lag,
rad2deg(theta_hypothesis));
203 #define MAX_NPEAKS 1024 221 int min_lag = -max_lag;
222 sm_debug(
"Max lag: %d cells (max t: %f, cell size: %f)\n",
227 for(
int cd=0;cd<ndirections;cd++) {
231 printf(
" cd %d angle = %d deg\n", cd, (
int)
rad2deg(dirs[cd].angle));
234 int lags [2*max_lag + 1];
235 double xcorr [2*max_lag + 1];
239 double *f1 = b1->
ht[i1];
240 double *f2 = b2->
ht[i2];
245 xcorr, lags, min_lag, max_lag);
254 sm_debug(
"theta hyp #%d: Found %d (max %d) peaks for correlation.\n",
257 dirs[cd].nhypotheses = linear_npeaks;
259 for(
int lp=0;lp<linear_npeaks;lp++) {
260 int linear_xc_lag = lags[linear_peaks[lp]];
261 double value = xcorr[linear_peaks[lp]];
263 sm_debug(
"lag: %d delta: %f value: %f \n", linear_xc_lag, linear_xc_lag_m, value);
264 dirs[cd].hypotheses[lp].delta = linear_xc_lag_m;
265 dirs[cd].hypotheses[lp].value = value;
270 double true_delta = cos(dirs[cd].angle) * p->
debug_true_x[0] +
272 sm_debug(
"true_x delta = %f \n", true_delta );
278 sm_debug(
"Now doing all combinations. How many are there?\n");
279 int possible_choices[ndirections];
280 int num_combinations = 1;
281 for(
int cd=0;cd<ndirections;cd++) {
282 possible_choices[cd] = dirs[cd].nhypotheses;
283 num_combinations *= dirs[cd].nhypotheses;
285 sm_debug(
"Total: %d combinations\n", num_combinations);
287 for(
int comb=0;comb<num_combinations;comb++) {
288 int choices[ndirections];
292 double M[2][2]={{0,0},{0,0}};
double Z[2]={0,0};
294 double sum_values = 0;
295 for(
int cd=0;cd<ndirections;cd++) {
296 double angle = dirs[cd].angle;
297 double c = cos(angle), s = sin(angle);
298 double w = dirs[cd].hypotheses[choices[cd]].value;
299 double y = dirs[cd].hypotheses[choices[cd]].delta;
301 M[0][0] += c * c * w;
302 M[1][0] += c * s * w;
303 M[0][1] += c * s * w;
304 M[1][1] += s * s * w;
311 double det = M[0][0]*M[1][1]-M[0][1]*M[1][0];
313 Minv[0][0] = M[1][1] * (1/det);
314 Minv[1][1] = M[0][0] * (1/det);
315 Minv[0][1] = -M[0][1] * (1/det);
316 Minv[1][0] = -M[1][0] * (1/det);
319 Minv[0][0]*Z[0] + Minv[0][1]*Z[1],
320 Minv[1][0]*Z[0] + Minv[1][1]*Z[1]};
327 b1->
results[k][2] = theta_hypothesis;
356 results_tmp[i] = b1->
results[i];
361 b1->
results[i] = results_tmp[indexes[i]];
372 const char * ast = (i == 0) && (err_th > 2) ?
" ***** " :
"";
373 sprintf(near,
"th err %4d err_m %5f %s",(
int)err_th ,err_m,ast);
376 printf(
"after #%d %3.1fm %.1fm %3.0fdeg quality %5.0f \t%s\n",i,
383 clock_t hsm_match_stop = clock();
384 int ticks = hsm_match_stop-hsm_match_start;
385 double ctime = ((double)ticks) / CLOCKS_PER_SEC;
386 sm_debug(
"Time: %f sec (%d ticks)\n", ctime, ticks);
393 int i,
int i_choice[])
395 for(
int slot=0;slot<nslots;slot++) {
396 i_choice[slot] = i % possible_choices[slot];
397 i = (i - i % possible_choices[slot]) / possible_choices[slot];
402 int*peaks,
int* npeaks)
409 int maxima[n], nmaxima;
412 sm_debug(
"Found %d of %d are local maxima.\n", nmaxima, n);
421 for(
int m=0;
m<nmaxima;
m++) {
423 int candidate = maxima[
m];
424 double candidate_angle = candidate * (2*
M_PI/n);
427 for(
int a=0;a<*npeaks;a++) {
428 int other = peaks[a];
429 double other_angle = other * (2*
M_PI/n);
432 acceptable = 0;
break;
438 acceptable = 0;
break;
443 sm_debug(
"%saccepting candidate %d; lag = %d value = %f\n",
444 acceptable?
"":
"not ",
m, maxima[
m], f[maxima[m]]);
447 peaks[*npeaks] = candidate;
451 if(*npeaks>=max_peaks)
break;
455 sm_debug(
"found %d (max %d) maxima.\n", *npeaks, max_peaks);
461 int*peaks,
int* npeaks)
468 int maxima[n], nmaxima;
471 sm_debug(
"Found %d of %d are local maxima.\n", nmaxima, n);
479 for(
int m=0;
m<nmaxima;
m++) {
481 int candidate = maxima[
m];
484 for(
int a=0;a<*npeaks;a++) {
485 int other = peaks[a];
487 if(abs(other-candidate) < min_dist) {
488 acceptable = 0;
break;
492 sm_debug(
"%s accepting candidate %d; lag = %d value = %f\n",
493 acceptable?
"":
"not",
m, maxima[
m], f[maxima[m]]);
496 peaks[*npeaks] = candidate;
500 if(*npeaks >= max_peaks)
break;
503 sm_debug(
"Found %d (max %d) maxima.\n", *npeaks, max_peaks);
510 double dot = cos(angle1)*cos(angle2) + sin(angle1)*sin(angle2);
511 return (dot > cos(threshold_deg *
M_PI/180));
516 for(
int i=0;i<n;i++) {
518 double left = f[
pos_mod(i-1,n) ];
519 double right = f[
pos_mod(i+1,n) ];
520 if( (val>0) && (val>left) && (val>right))
521 maxima[(*nmaxima)++] = i;
528 for(
int i=1;i<n-1;i++) {
530 double left = f[i-1];
531 double right = f[i+1];
532 if( (val>0) && (val>left) && (val>right))
533 maxima[(*nmaxima)++] = i;
542 for(
int i=0;i<2*n;i++) aa[i] = a[i%n];
543 for(
int lag=0;lag<n;lag++) {
546 res[lag] += b[j] * aa[j+lag];
557 for(
int l=min_lag;l<=max_lag;l++) {
561 for(
int j=0; (j<nb) && (j+l<na);j++) {
576 int i1 = *( (
const int*) index_pt1);
577 int i2 = *( (
const int*) index_pt2);
579 return f[i1] < f[i2] ? +1 : f[i1] == f[i2] ? 0 : -1;
void sm_log_push(const char *cname)
int hsm_is_angle_between_smaller_than_deg(double angle1, double angle2, double threshold_deg)
void hsm_buffer_free(hsm_buffer b)
void hsm_generate_combinations(int nslots, const int possible_choices[], int i, int i_choice[])
void hsm_circular_cross_corr_stupid(int n, const double *a, const double *b, double *res)
int hsm_rho2index(hsm_buffer b, double rho, int *rho_index, double *alpha)
void hsm_compute_ht_base(hsm_buffer b, const double base_pose[3])
double angleDiff(double a, double b)
void hsm_find_peaks_circ(int n, const double *f, double min_angle_deg, int unidir, int max_peaks, int *peaks, int *npeaks)
int num_angular_hypotheses
void hsm_compute_ht_point(hsm_buffer b, double x0, double y0, double weight)
void hsm_compute_spectrum_norm(hsm_buffer b)
void qsort_descending(int *indexes, size_t nmemb, const double *values)
int compare_descending(const void *index_pt1, const void *index_pt2)
double mdax(double a, double b)
void hsm_compute_spectrum(hsm_buffer b)
void hsm_find_local_maxima_linear(int n, const double *f, int *maxima, int *nmaxima)
double angular_cell_size_deg
hsm_buffer hsm_buffer_alloc(struct hsm_params *p)
const double * qsort_descending_values
double linear_xc_peaks_min_distance
int pos_mod(int a, int b)
void hsm_match(struct hsm_params *p, hsm_buffer b1, hsm_buffer b2)
#define M(matrix, rows, col)
double xc_directions_min_distance_deg
void hsm_find_peaks_linear(int n, const double *f, double min_dist, int max_peaks, int *peaks, int *npeaks)
struct hsm_buffer_struct * hsm_buffer
void sm_debug(const char *msg,...)
void hsm_find_local_maxima_circ(int n, const double *f, int *maxima, int *nmaxima)
void sm_error(const char *msg,...)
double angular_hyp_min_distance_deg
void hsm_linear_cross_corr_stupid(int na, const double *a, int nb, const double *b, double *res, int *lags, int min_lag, int max_lag)
double rad2deg(double rad)