convert_octree.cpp
Go to the documentation of this file.
00001 /*
00002  * OctoMap - An Efficient Probabilistic 3D Mapping Framework Based on Octrees
00003  * http://octomap.github.com/
00004  *
00005  * Copyright (c) 2009-2013, K.M. Wurm and A. Hornung, University of Freiburg
00006  * All rights reserved.
00007  * License: New BSD
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions are met:
00011  *
00012  *     * Redistributions of source code must retain the above copyright
00013  *       notice, this list of conditions and the following disclaimer.
00014  *     * Redistributions in binary form must reproduce the above copyright
00015  *       notice, this list of conditions and the following disclaimer in the
00016  *       documentation and/or other materials provided with the distribution.
00017  *     * Neither the name of the University of Freiburg nor the names of its
00018  *       contributors may be used to endorse or promote products derived from
00019  *       this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00022  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00025  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00026  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00027  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00030  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031  * POSSIBILITY OF SUCH DAMAGE.
00032  */
00033 
00034 #include <octomap/AbstractOcTree.h>
00035 #include <octomap/OcTree.h>
00036 #include <octomap/ColorOcTree.h>
00037 #include <fstream>
00038 #include <iostream>
00039 #include <string.h>
00040 #include <stdlib.h>
00041 #include <list>
00042 
00043 using namespace std;
00044 using namespace octomap;
00045 
00046 void printUsage(char* self){
00047   std::cerr << "\nUSAGE: " << self << " input.(ot|bt|cot) [output.ot]\n\n";
00048 
00049   std::cerr << "This tool converts between OctoMap octree file formats, \n"
00050       "e.g. to convert old legacy files to the new .ot format or to convert \n"
00051       "between .bt and .ot files. The default output format is .ot.\n\n";
00052 
00053   exit(0);
00054 }
00055 
00056 int main(int argc, char** argv) {
00057   string inputFilename = "";
00058   string outputFilename = "";
00059 
00060   if (argc < 2 || argc > 3 || (argc > 1 && strcmp(argv[1], "-h") == 0)){
00061     printUsage(argv[0]);
00062   }
00063 
00064   inputFilename = std::string(argv[1]);
00065   if (argc == 3)
00066     outputFilename = std::string(argv[2]);
00067   else{
00068     outputFilename = inputFilename + ".ot";
00069   }
00070 
00071 
00072   cout << "\nReading OcTree file\n===========================\n";
00073   std::ifstream file(inputFilename.c_str(), std::ios_base::in |std::ios_base::binary);
00074 
00075   if (!file.is_open()){
00076     OCTOMAP_ERROR_STR("Filestream to "<< inputFilename << " not open, nothing read.");
00077     exit(-1);
00078   }
00079 
00080   std::istream::pos_type streampos = file.tellg();
00081   AbstractOcTree* tree;
00082 
00083   // reading binary:
00084   if (inputFilename.length() > 3 && (inputFilename.compare(inputFilename.length()-3, 3, ".bt") == 0)){
00085     OcTree* binaryTree = new OcTree(0.1);
00086 
00087     if (binaryTree->readBinary(file) && binaryTree->size() > 1)
00088       tree = binaryTree;
00089     else {
00090       OCTOMAP_ERROR_STR("Could not detect binary OcTree format in file.");
00091       exit(-1);
00092 
00093     }
00094   } else {
00095     tree = AbstractOcTree::read(file);
00096     if (!tree){
00097       OCTOMAP_WARNING_STR("Could not detect OcTree in file, trying legacy formats.");
00098       // TODO: check if .cot extension, try old format only then
00099       // reset and try old ColorOcTree format:
00100       file.clear(); // clear eofbit of istream
00101       file.seekg(streampos);
00102       ColorOcTree* colorTree = new ColorOcTree(0.1);
00103       colorTree->readData(file);
00104       if (colorTree->size() > 1 && file.good()){
00105         OCTOMAP_WARNING_STR("Detected Binary ColorOcTree to convert. \nPlease check and update the new file header (resolution will likely be wrong).");
00106         tree = colorTree;
00107       } else{
00108         delete colorTree;
00109         std::cerr << "Error reading from file " << inputFilename << std::endl;
00110         exit(-1);
00111       }
00112     }
00113 
00114 
00115   }
00116 
00117   // close filestream
00118   file.close();
00119 
00120 
00121   if (outputFilename.length() > 3 && (outputFilename.compare(outputFilename.length()-3, 3, ".bt") == 0)){
00122     std::cerr << "Writing binary (BonsaiTree) file" << std::endl;
00123     AbstractOccupancyOcTree* octree = dynamic_cast<AbstractOccupancyOcTree*>(tree);
00124     if (octree){
00125       if (!octree->writeBinary(outputFilename)){
00126         std::cerr << "Error writing to " << outputFilename << std::endl;
00127         exit(-2);
00128       }
00129     } else {
00130       std::cerr << "Error: Writing to .bt is not supported for this tree type: " << tree->getTreeType() << std::endl;
00131       exit(-2);
00132     }
00133   } else{
00134     std::cerr << "Writing general OcTree file" << std::endl;
00135     if (!tree->write(outputFilename)){
00136       std::cerr << "Error writing to " << outputFilename << std::endl;
00137       exit(-2);
00138     }
00139   }
00140 
00141 
00142 
00143 
00144 
00145   std::cout << "Finished writing to " << outputFilename << std::endl;
00146   exit(0);
00147 
00148 }


octomap
Author(s): Kai M. Wurm , Armin Hornung
autogenerated on Thu Aug 27 2015 14:13:14