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
00029
00030 #include <navfn/navfn.h>
00031 #include <stdlib.h>
00032 #include <stdint.h>
00033
00034 #ifdef __APPLE__
00035 # include <netpbm/pgm.h>
00036 #else
00037 extern "C" {
00038 #include <stdio.h>
00039
00040 #include <pgm.h>
00041 #undef max
00042 #undef min
00043 }
00044 #endif
00045
00046 void
00047 setcostobs(COSTTYPE *cmap, int n, int w)
00048 {
00049 int CS = 11;
00050 for (int i=-CS/2; i<CS/2; i++)
00051 {
00052 COSTTYPE *cm = i*w + &cmap[n];
00053 for (int j=-CS/2; j<CS/2; j++)
00054 cm[j] = COST_NEUTRAL + 50;
00055 }
00056 CS = 7;
00057 for (int i=-CS/2; i<CS/2; i++)
00058 {
00059 COSTTYPE *cm = i*w + &cmap[n];
00060 for (int j=-CS/2; j<CS/2; j++)
00061 cm[j] = COST_OBS;
00062 }
00063 }
00064
00065 void setcostunk(COSTTYPE *cmap, int n, int w)
00066 {
00067 cmap[n] = COST_OBS;
00068 }
00069
00070 #define unknown_gray 0xCC // seems to be the value of "unknown" in maps
00071
00072 COSTTYPE *
00073 readPGM(const char *fname, int *width, int *height, bool raw)
00074 {
00075 pm_init("navfn_tests",0);
00076
00077 FILE *pgmfile;
00078 pgmfile = fopen(fname,"r");
00079 if (!pgmfile)
00080 {
00081 printf("readPGM() Can't find file %s\n", fname);
00082 return NULL;
00083 }
00084
00085 printf("readPGM() Reading costmap file %s\n", fname);
00086 int ncols, nrows;
00087 gray maxval;
00088 int format;
00089 pgm_readpgminit(pgmfile, &ncols, &nrows, &maxval, &format);
00090 printf("readPGM() Size: %d x %d\n", ncols, nrows);
00091
00092
00093 COSTTYPE *cmap = (COSTTYPE *)malloc(ncols*nrows*sizeof(COSTTYPE));
00094 if (!raw)
00095 for (int i=0; i<ncols*nrows; i++)
00096 cmap[i] = COST_NEUTRAL;
00097
00098 gray * row(pgm_allocrow(ncols));
00099 int otot = 0;
00100 int utot = 0;
00101 int ftot = 0;
00102 for (int ii = 0; ii < nrows; ii++) {
00103 pgm_readpgmrow(pgmfile, row, ncols, maxval, format);
00104 if (raw)
00105 {
00106 for (int jj(ncols - 1); jj >= 0; --jj)
00107 {
00108 int v = row[jj];
00109 cmap[ii*ncols+jj] = v;
00110 if (v >= COST_OBS_ROS)
00111 otot++;
00112 if (v == 0)
00113 ftot++;
00114 }
00115 }
00116 else
00117 {
00118 ftot = ncols*nrows;
00119 for (int jj(ncols - 1); jj >= 0; --jj)
00120 {
00121 if (row[jj] < unknown_gray && ii < nrows-7 && ii > 7)
00122 {
00123 setcostobs(cmap,ii*ncols+jj,ncols);
00124 otot++;
00125 ftot--;
00126 }
00127 else if (row[jj] <= unknown_gray)
00128 {
00129 setcostunk(cmap,ii*ncols+jj,ncols);
00130 utot++;
00131 ftot--;
00132 }
00133 }
00134 }
00135 }
00136 printf("readPGM() Found %d obstacle cells, %d free cells, %d unknown cells\n", otot, ftot, utot);
00137 pgm_freerow(row);
00138 *width = ncols;
00139 *height = nrows;
00140 return cmap;
00141 }