Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 #include <errno.h>
00029 #include <math.h>
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 
00034 #include "nav2d_localizer/map.h"
00035 
00036 
00038 
00039 int map_load_occ(map_t *map, const char *filename, double scale, int negate)
00040 {
00041   FILE *file;
00042   char magic[3];
00043   int i, j;
00044   int ch, occ;
00045   int width, height, depth;
00046   map_cell_t *cell;
00047 
00048   
00049   file = fopen(filename, "r");
00050   if (file == NULL)
00051   {
00052     fprintf(stderr, "%s: %s\n", strerror(errno), filename);
00053     return -1;
00054   }
00055 
00056   
00057   
00058   if ((fscanf(file, "%10s \n", magic) != 1) || (strcmp(magic, "P5") != 0))
00059   {
00060     fprintf(stderr, "incorrect image format; must be PGM/binary");
00061     return -1;
00062   }
00063 
00064   
00065   while ((ch = fgetc(file)) == '#')
00066     while (fgetc(file) != '\n');
00067   ungetc(ch, file);
00068 
00069   
00070   if(fscanf(file, " %d %d \n %d \n", &width, &height, &depth) != 3)
00071   {
00072     fprintf(stderr, "Failed ot read image dimensions");
00073     return -1;
00074   }
00075 
00076   
00077   if (map->cells == NULL)
00078   {
00079     map->scale = scale;
00080     map->size_x = width;
00081     map->size_y = height;
00082     map->cells = calloc(width * height, sizeof(map->cells[0]));
00083   }
00084   else
00085   {
00086     if (width != map->size_x || height != map->size_y)
00087     {
00088       
00089       return -1;
00090     }
00091   }
00092 
00093   
00094   for (j = height - 1; j >= 0; j--)
00095   {
00096     for (i = 0; i < width; i++)
00097     {
00098       ch = fgetc(file);
00099 
00100       
00101       if (!negate)
00102       {
00103         if (ch < depth / 4)
00104           occ = +1;
00105         else if (ch > 3 * depth / 4)
00106           occ = -1;
00107         else
00108           occ = 0;
00109       }
00110 
00111       
00112       else
00113       {
00114         if (ch < depth / 4)
00115           occ = -1;
00116         else if (ch > 3 * depth / 4)
00117           occ = +1;
00118         else
00119           occ = 0;
00120       }
00121 
00122       if (!MAP_VALID(map, i, j))
00123         continue;
00124       cell = map->cells + MAP_INDEX(map, i, j);
00125       cell->occ_state = occ;
00126     }
00127   }
00128   
00129   fclose(file);
00130   
00131   return 0;
00132 }
00133 
00134 
00136 
00137 
00138 
00139 
00140 
00141 
00142 
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154 
00155 
00156 
00157 
00158 
00159 
00160 
00161 
00162 
00163 
00164 
00165 
00166 
00167 
00168 
00169 
00170 
00171 
00172 
00173 
00174 
00175 
00176 
00177 
00178 
00179 
00180 
00181 
00182 
00183 
00184 
00185 
00186 
00187 
00188 
00189 
00190 
00191 
00192 
00193 
00194 
00195 
00196 
00197 
00198 
00199 
00200 
00201 
00202 
00203 
00204 
00205 
00206 
00207 
00208 
00209 
00210 
00211 
00212 
00213 
00214