map.cpp
Go to the documentation of this file.
00001 
00002 #include "mrpt_bridge/map.h"
00003 #include "mrpt_bridge/pose.h"
00004 #include <nav_msgs/OccupancyGrid.h>
00005 #include <mrpt/slam/COccupancyGridMap2D.h>
00006 #include <ros/console.h>
00007 #include <mrpt/base.h>
00008 #include <mrpt/slam.h>
00009 
00010 #define INT8_MAX    0x7f
00011 #define INT8_MIN    (-INT8_MAX - 1)
00012 #define INT16_MAX   0x7fff
00013 #define INT16_MIN   (-INT16_MAX - 1)
00014 
00015 namespace mrpt_bridge
00016 {
00017 MapHdl* MapHdl::instance_ = NULL;
00018 
00019 MapHdl::MapHdl ()
00020 {
00022     mrpt::slam::CLogOddsGridMapLUT<mrpt::slam::COccupancyGridMap2D::cellType> table;
00023 #ifdef  OCCUPANCY_GRIDMAP_CELL_SIZE_8BITS
00024     lut_cellmrpt2rosPtr = lut_cellmrpt2ros + INT8_MAX + 1; // center the pointer
00025     lut_cellros2mrptPtr = lut_cellros2mrpt + INT8_MAX + 1; // center the pointer
00026     for ( int i = INT8_MIN; i < INT8_MAX; i++ ) {
00027 #else
00028     lut_cellmrpt2rosPtr = lut_cellmrpt2ros + INT16_MAX + 1; // center the pointer
00029     for ( int i = INT16_MIN; INT16_MIN < INT16_MAX; i++ ) {
00030 #endif
00031         float p = 1.0-table.l2p ( i );
00032         int idx = round(p * 100.);
00033         lut_cellmrpt2rosPtr[i] = idx;
00034         // printf("- cell -> ros = %4i -> %4i, p=%4.3f\n", i, idx, p);
00035     }
00036     for ( int i = INT8_MIN; i < INT8_MAX; i++ ) {
00037         float v = i;
00038         if( v > 100) v = 50;
00039         if( v < 0) v = 50;
00040         float p = 1.0 - (v /100.0);
00041         int idx = table.p2l(p);
00042         if(i < 0)  lut_cellros2mrptPtr[i] = table.p2l(0.5);
00043         else if(i > 100) lut_cellros2mrptPtr[i] = table.p2l(0.5);
00044         else lut_cellros2mrptPtr[i] = idx;
00045         // printf("- ros -> cell = %4i -> %4i, p=%4.3f\n", i, idx, p);
00046         fflush(stdout);
00047     }
00048 }
00049 MapHdl::~MapHdl () { }
00050 
00051 MapHdl* MapHdl::instance ()
00052 {
00053     if ( instance_ == NULL ) instance_ = new MapHdl ();
00054     return instance_;
00055 }
00056 
00057 bool convert ( const nav_msgs::OccupancyGrid  &src, mrpt::slam::COccupancyGridMap2D  &des )
00058 {
00059     if((src.info.origin.orientation.x != 0) ||
00060             (src.info.origin.orientation.y != 0) ||
00061             (src.info.origin.orientation.z != 0) ||
00062             (src.info.origin.orientation.w != 1)) {
00063         std::cerr << "Rotated maps are not supported by mrpt!" << std::endl;
00064         return false;
00065     }
00066     float xmin = src.info.origin.position.x;
00067     float ymin = src.info.origin.position.y;
00068     float xmax = xmin + src.info.width * src.info.resolution;
00069     float ymax = ymin + src.info.height * src.info.resolution;
00070 
00071     MRPT_START
00072     des.setSize(xmin, xmax, ymin, ymax, src.info.resolution);
00073     MRPT_END
00074     //printf("--------convert:  %i x %i, %4.3f, %4.3f, %4.3f, %4.3f, r:%4.3f\n",des.getSizeX(), des.getSizeY(), des.getXMin(), des.getXMax(), des.getYMin(), des.getYMax(), des.getResolution());
00075        
00077     for ( int h = 0; h < src.info.height; h++ ) {
00078         mrpt::slam::COccupancyGridMap2D::cellType *pDes = des.getRow (h);
00079         const int8_t *pSrc = &src.data[h * src.info.width];
00080         for ( int w = 0; w < src.info.width; w++ ) {
00081             *pDes++ = MapHdl::instance()->cellRos2Mrpt(*pSrc++);
00082         }
00083     }
00084     return true;
00085 }
00086 bool convert ( const mrpt::slam::COccupancyGridMap2D &src, nav_msgs::OccupancyGrid &des,    const std_msgs::Header &header
00087 )
00088 {
00089     des.header = header;
00090     return convert(src, des);
00091 }
00092 
00093 bool convert (    const mrpt::slam::COccupancyGridMap2D &src, nav_msgs::OccupancyGrid &des)
00094 {
00095     //printf("--------mrpt2ros:  %f, %f, %f, %f, r:%f\n",src.getXMin(), src.getXMax(), src.getYMin(), src.getYMax(), src.getResolution());
00096     des.info.width = src.getSizeX();
00097     des.info.height = src.getSizeY();
00098     des.info.resolution = src.getResolution ();
00099 
00100     des.info.origin.position.x = src.getXMin();
00101     des.info.origin.position.y = src.getYMin();
00102     des.info.origin.position.z = 0;
00103 
00104     des.info.origin.orientation.x = 0;
00105     des.info.origin.orientation.y = 0;
00106     des.info.origin.orientation.z = 0;
00107     des.info.origin.orientation.w = 1;
00108 
00110     des.data.resize ( des.info.width*des.info.height );
00111     for ( int h = 0; h < des.info.height; h++ ) {
00112         const mrpt::slam::COccupancyGridMap2D::cellType *pSrc = src.getRow (h);
00113         int8_t *pDes = &des.data[h * des.info.width];
00114         for ( int w = 0; w < des.info.width; w++ ) {
00115             *pDes++ = MapHdl::instance()->cellMrpt2Ros(*pSrc++);
00116         }
00117     }
00118     return true;
00119 }
00120 
00121 const bool MapHdl::loadMap(mrpt::slam::CMultiMetricMap &_metric_map, const mrpt::utils::CConfigFile &_config_file, const std::string &_map_file, const std::string &_section_name, bool _debug) {
00122 
00123     mrpt::slam::TSetOfMetricMapInitializers mapInitializers;
00124     mapInitializers.loadFromConfigFile( _config_file, _section_name);
00125     if(_debug) mapInitializers.dumpToConsole();
00126 
00127     mrpt::slam::CSimpleMap  simpleMap;
00128 
00129     // Load the set of metric maps to consider in the experiments:
00130     _metric_map.setListOfMaps( &mapInitializers );
00131     if(_debug) mapInitializers.dumpToConsole();
00132 
00133     mrpt::random::randomGenerator.randomize();
00134 
00135     if(_debug) printf("%s, _map_file.size() = %zu\n", _map_file.c_str(), _map_file.size());
00136     // Load the map (if any):
00137     if (_map_file.size() < 3) {
00138         if(_debug) printf("No mrpt map file!\n");
00139         return false;
00140     } else {
00141         ASSERT_( mrpt::utils::fileExists(_map_file) );
00142 
00143         // Detect file extension:
00144         std::string mapExt = mrpt::utils::lowerCase( mrpt::utils::extractFileExtension( _map_file, true ) ); // Ignore possible .gz extensions
00145 
00146         if ( !mapExt.compare( "simplemap" ) )
00147         {
00148             // It's a ".simplemap":
00149             if(_debug) ("Loading '.simplemap' file...");
00150             mrpt::utils::CFileGZInputStream(_map_file) >> simpleMap;
00151             printf("Ok\n");
00152 
00153             ASSERT_( simpleMap.size()>0 );
00154 
00155             // Build metric map:
00156             if(_debug) printf("Building metric map(s) from '.simplemap'...");
00157             _metric_map.loadFromProbabilisticPosesAndObservations(simpleMap);
00158             if(_debug) printf("Ok\n");
00159         }
00160         else if ( !mapExt.compare( "gridmap" ) )
00161         {
00162             // It's a ".gridmap":
00163             if(_debug) printf("Loading gridmap from '.gridmap'...");
00164             ASSERT_( _metric_map.m_gridMaps.size()==1 );
00165             mrpt::utils::CFileGZInputStream(_map_file) >> (*_metric_map.m_gridMaps[0]);
00166             if(_debug) printf("Ok\n");
00167         }
00168         else
00169         {
00170             THROW_EXCEPTION_CUSTOM_MSG1("Map file has unknown extension: '%s'",mapExt.c_str());
00171             return false;
00172         }
00173 
00174     }
00175     return true;
00176 }
00177 
00178 
00179 } // end namespace


mrpt_bridge
Author(s):
autogenerated on Tue Aug 5 2014 10:58:06