octomap_saver.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010-2013, A. Hornung, University of Freiburg
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 University of Freiburg 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 <ros/ros.h>
00031 #include <octomap_msgs/conversions.h>
00032 #include <octomap/octomap.h>
00033 #include <fstream>
00034 
00035 #include <octomap_msgs/GetOctomap.h>
00036 using octomap_msgs::GetOctomap;
00037 
00038 #define USAGE "\nUSAGE: octomap_saver [-f] <mapfile.[bt|ot]>\n" \
00039                 "  -f: Query for the full occupancy octree, instead of just the compact binary one\n" \
00040                 "  mapfile.bt: filename of map to be saved (.bt: binary tree, .ot: general octree)\n"
00041 
00042 using namespace std;
00043 using namespace octomap;
00044 
00045 class MapSaver{
00046 public:
00047   MapSaver(const std::string& mapname, bool full){
00048     ros::NodeHandle n;
00049     std::string servname = "octomap_binary";
00050     if (full)
00051       servname = "octomap_full";
00052     ROS_INFO("Requesting the map from %s...", n.resolveName(servname).c_str());
00053     GetOctomap::Request req;
00054     GetOctomap::Response resp;
00055     while(n.ok() && !ros::service::call(servname, req, resp))
00056     {
00057       ROS_WARN("Request to %s failed; trying again...", n.resolveName(servname).c_str());
00058       usleep(1000000);
00059     }
00060 
00061     if (n.ok()){ // skip when CTRL-C
00062 
00063       AbstractOcTree* tree = octomap_msgs::msgToMap(resp.map);
00064       AbstractOccupancyOcTree* octree = NULL;
00065       if (tree){
00066         octree = dynamic_cast<AbstractOccupancyOcTree*>(tree);
00067       }
00068 
00069       if (octree){
00070         ROS_INFO("Map received (%zu nodes, %f m res), saving to %s", octree->size(), octree->getResolution(), mapname.c_str());
00071         
00072         std::string suffix = mapname.substr(mapname.length()-3, 3);
00073         if (suffix== ".bt"){ // write to binary file:
00074           if (!octree->writeBinary(mapname)){
00075             ROS_ERROR("Error writing to file %s", mapname.c_str());
00076           }
00077         } else if (suffix == ".ot"){ // write to full .ot file:
00078           if (!octree->write(mapname)){
00079             ROS_ERROR("Error writing to file %s", mapname.c_str());
00080           }
00081         } else{
00082           ROS_ERROR("Unknown file extension, must be either .bt or .ot");
00083         }
00084 
00085 
00086       } else{
00087         ROS_ERROR("Error reading OcTree from stream");
00088       }
00089 
00090       delete octree;
00091 
00092     }
00093   }
00094 };
00095 
00096 int main(int argc, char** argv){
00097   ros::init(argc, argv, "octomap_saver");
00098   std::string mapFilename("");
00099   bool fullmap = false;
00100   if (argc == 3 && strcmp(argv[1], "-f")==0){
00101     fullmap = true;
00102     mapFilename = std::string(argv[2]);
00103   } else if (argc == 2)
00104     mapFilename = std::string(argv[1]);
00105   else{
00106     ROS_ERROR("%s", USAGE);
00107     exit(1);
00108   }
00109 
00110   try{
00111     MapSaver ms(mapFilename, fullmap);
00112   }catch(std::runtime_error& e){
00113     ROS_ERROR("octomap_saver exception: %s", e.what());
00114     exit(2);
00115   }
00116 
00117   exit(0);
00118 }
00119 
00120 


octomap_server
Author(s): Armin Hornung
autogenerated on Thu Aug 27 2015 14:14:07