convert_octree.cpp
Go to the documentation of this file.
1 /*
2  * OctoMap - An Efficient Probabilistic 3D Mapping Framework Based on Octrees
3  * http://octomap.github.com/
4  *
5  * Copyright (c) 2009-2013, K.M. Wurm and A. Hornung, University of Freiburg
6  * All rights reserved.
7  * License: New BSD
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * * Neither the name of the University of Freiburg nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <octomap/AbstractOcTree.h>
35 #include <octomap/OcTree.h>
36 #include <octomap/ColorOcTree.h>
37 #include <fstream>
38 #include <iostream>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <list>
42 
43 using namespace std;
44 using namespace octomap;
45 
46 void printUsage(char* self){
47  std::cerr << "\nUSAGE: " << self << " input.(ot|bt|cot) [output.ot]\n\n";
48 
49  std::cerr << "This tool converts between OctoMap octree file formats, \n"
50  "e.g. to convert old legacy files to the new .ot format or to convert \n"
51  "between .bt and .ot files. The default output format is .ot.\n\n";
52 
53  exit(0);
54 }
55 
56 int main(int argc, char** argv) {
57  string inputFilename = "";
58  string outputFilename = "";
59 
60  if (argc < 2 || argc > 3 || (argc > 1 && strcmp(argv[1], "-h") == 0)){
61  printUsage(argv[0]);
62  }
63 
64  inputFilename = std::string(argv[1]);
65  if (argc == 3)
66  outputFilename = std::string(argv[2]);
67  else{
68  outputFilename = inputFilename + ".ot";
69  }
70 
71 
72  cout << "\nReading OcTree file\n===========================\n";
73  std::ifstream file(inputFilename.c_str(), std::ios_base::in |std::ios_base::binary);
74 
75  if (!file.is_open()){
76  OCTOMAP_ERROR_STR("Filestream to "<< inputFilename << " not open, nothing read.");
77  exit(-1);
78  }
79 
80  std::istream::pos_type streampos = file.tellg();
81  AbstractOcTree* tree;
82 
83  // reading binary:
84  if (inputFilename.length() > 3 && (inputFilename.compare(inputFilename.length()-3, 3, ".bt") == 0)){
85  OcTree* binaryTree = new OcTree(0.1);
86 
87  if (binaryTree->readBinary(file) && binaryTree->size() > 1)
88  tree = binaryTree;
89  else {
90  OCTOMAP_ERROR_STR("Could not detect binary OcTree format in file.");
91  exit(-1);
92 
93  }
94  } else {
95  tree = AbstractOcTree::read(file);
96  if (!tree){
97  OCTOMAP_WARNING_STR("Could not detect OcTree in file, trying legacy formats.");
98  // TODO: check if .cot extension, try old format only then
99  // reset and try old ColorOcTree format:
100  file.clear(); // clear eofbit of istream
101  file.seekg(streampos);
102  ColorOcTree* colorTree = new ColorOcTree(0.1);
103  colorTree->readData(file);
104  if (colorTree->size() > 1 && file.good()){
105  OCTOMAP_WARNING_STR("Detected Binary ColorOcTree to convert. \nPlease check and update the new file header (resolution will likely be wrong).");
106  tree = colorTree;
107  } else{
108  delete colorTree;
109  std::cerr << "Error reading from file " << inputFilename << std::endl;
110  exit(-1);
111  }
112  }
113 
114 
115  }
116 
117  // close filestream
118  file.close();
119 
120 
121  if (outputFilename.length() > 3 && (outputFilename.compare(outputFilename.length()-3, 3, ".bt") == 0)){
122  std::cerr << "Writing binary (BonsaiTree) file" << std::endl;
123  AbstractOccupancyOcTree* octree = dynamic_cast<AbstractOccupancyOcTree*>(tree);
124  if (octree){
125  if (!octree->writeBinary(outputFilename)){
126  std::cerr << "Error writing to " << outputFilename << std::endl;
127  exit(-2);
128  }
129  } else {
130  std::cerr << "Error: Writing to .bt is not supported for this tree type: " << tree->getTreeType() << std::endl;
131  exit(-2);
132  }
133  } else{
134  std::cerr << "Writing general OcTree file" << std::endl;
135  if (!tree->write(outputFilename)){
136  std::cerr << "Error writing to " << outputFilename << std::endl;
137  exit(-2);
138  }
139  }
140 
141 
142 
143 
144 
145  std::cout << "Finished writing to " << outputFilename << std::endl;
146 
147  return 0;
148 }
virtual std::string getTreeType() const =0
returns actual class name as string for identification
void printUsage(char *self)
bool writeBinary(const std::string &filename)
std::istream & readData(std::istream &s)
#define OCTOMAP_WARNING_STR(args)
Definition: octomap_types.h:77
int main(int argc, char **argv)
#define OCTOMAP_ERROR_STR(args)
Definition: octomap_types.h:79
virtual size_t size() const
bool write(const std::string &filename) const
Write file header and complete tree to file (serialization)


octomap
Author(s): Kai M. Wurm , Armin Hornung
autogenerated on Mon Feb 28 2022 22:58:06