read_pgm_costmap.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
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 // pgm.h is not very friendly with system headers... need to undef max() and min() afterwards
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   // set up cost map
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)                    // raw costmap from ROS
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 }


navfn
Author(s): Kurt Konolige, Eitan Marder-Eppstein, contradict@gmail.com
autogenerated on Thu Aug 27 2015 14:07:27