Go to the documentation of this file.00001 #include <options/options.h>
00002
00003 #include "../csm/csm_all.h"
00004
00005 struct {
00007 double scale_deg;
00008
00010 int neighbours;
00011 } p;
00012
00013 void ld_smooth(LDP ld, int neighbours, double scale_rad);
00014 void convolve(const int*valid,const double*original, int n, double*dest, double*filter, int filter_len);
00015
00016 int main(int argc, const char * argv[]) {
00017 sm_set_program_name(argv[0]);
00018
00019 struct option* ops = options_allocate(3);
00020 options_double(ops, "scale_deg", &p.scale_deg, 0.0, "Scale factor (degrees) ");
00021 options_int(ops, "neighbours", &p.neighbours, 1, "How many neighbours to consider (regardless of scale).");
00022
00023 if(!options_parse_args(ops, argc, argv)) {
00024 fprintf(stderr, "A simple program for smoothing a sensor scan.\n\nUsage:\n");
00025 options_print_help(ops, stderr);
00026 return -1;
00027 }
00028
00029 int errors = 0;
00030 int count = -1;
00031 LDP ld;
00032 while( (ld = ld_read_smart(stdin)) ) {
00033 count++;
00034 if(!ld_valid_fields(ld)) {
00035 sm_error("Invalid laser data (#%d in file)\n", count);
00036 errors++;
00037 continue;
00038 }
00039
00040 ld_smooth(ld, p.neighbours, deg2rad(p.scale_deg) );
00041
00042 ld_write_as_json(ld, stdout);
00043
00044 ld_free(ld);
00045 }
00046
00047 return errors;
00048 }
00049
00050 void convolve(const int*valid,const double*original, int n, double*dest, double*filter, int filter_len)
00051 {
00052 int i;
00053 int j;
00054
00055 for(i=0;i<n;i++) {
00056 if(!valid[i]) {
00057 dest[i] = GSL_NAN;
00058 continue;
00059 }
00060
00061 dest[i] = 0;
00062 for(j=-(filter_len-1);j<=(filter_len-1);j++) {
00063 int i2 = i + j;
00064 if(i2<0) i2=0; if(i2>=n) i2=n-1;
00065 if(!valid[i2]) i2 = i;
00066 dest[i] += original[i2] * filter[abs(j)];
00067 }
00068 }
00069 }
00070
00071 void ld_smooth(LDP ld, int neighbours, double scale_rad) {
00072 int len = neighbours + 1;
00073 double filter[len];
00074 int j;
00075 static int once = 1;
00076 for(j=0;j<len;j++) {
00077 double dist_rad = j * ((ld->max_theta-ld->min_theta)/ld->nrays);
00078 double mahal = square(dist_rad / scale_rad);
00079 filter[j] = exp(-mahal);
00080
00081 if(once)
00082 sm_info("filter[%d] = %f mahal = %f dist_rad = %f scale_rad = %f \n", j, filter[j], mahal, dist_rad, scale_rad);
00083
00084 }
00085
00086 once = 0;
00087
00088
00089 double filter_tot = filter[0];
00090 for(j=1;j<len;j++) filter_tot+=filter[j];
00091
00092 for(j=0;j<len;j++) filter[j]/=filter_tot;
00093
00094 double new_readings[ld->nrays];
00095 convolve(ld->valid, ld->readings, ld->nrays, new_readings, filter, len);
00096 copy_d(new_readings, ld->nrays, ld->readings);
00097
00098 for(j=0;j<len;j++) {
00099
00100 }
00101 }
00102
00103