From 2ef3e92701726b5036320c03f90e2175cd7c9ccc Mon Sep 17 00:00:00 2001 From: Armin Hornung Date: Tue, 20 Dec 2011 16:06:19 +0000 Subject: [PATCH] write/readBinary now return bool, just like read/write --- octomap/include/octomap/AbstractOcTree.h | 4 +- octomap/include/octomap/OccupancyOcTreeBase.h | 58 ++++++++++++------- .../include/octomap/OccupancyOcTreeBase.hxx | 33 +++++------ octomap/src/convert_octree.cpp | 3 +- 4 files changed, 57 insertions(+), 41 deletions(-) diff --git a/octomap/include/octomap/AbstractOcTree.h b/octomap/include/octomap/AbstractOcTree.h index 43a1c7d0..a9dd025d 100644 --- a/octomap/include/octomap/AbstractOcTree.h +++ b/octomap/include/octomap/AbstractOcTree.h @@ -97,9 +97,9 @@ namespace octomap { static AbstractOcTree* read(std::istream &s); /// Read complete state of tree from stream virtual std::istream& readData(std::istream &s) = 0; - /// Write complete state of tree to stream, prune tree first (lossless compression) + /// Write complete state of tree to stream (without file header), prune tree first (lossless compression) virtual std::ostream& writeData(std::ostream &s) = 0; - /// Write complete state of tree to stream, no pruning (const version) + /// Write complete state of tree to stream (without file header), no pruning (const version) virtual std::ostream& writeDataConst(std::ostream &s) const = 0; private: /// create private store, Construct on first use diff --git a/octomap/include/octomap/OccupancyOcTreeBase.h b/octomap/include/octomap/OccupancyOcTreeBase.h index ddf72211..6d5a2325 100644 --- a/octomap/include/octomap/OccupancyOcTreeBase.h +++ b/octomap/include/octomap/OccupancyOcTreeBase.h @@ -310,30 +310,48 @@ namespace octomap { // -- I/O ----------------------------------------- - /// Reads an OcTree from an input stream. - /// Existing nodes of the tree are deleted before the tree is read. - std::istream& readBinary(std::istream &s); - - /// Writes OcTree to a binary stream. - /// The OcTree is first converted to the maximum likelihood estimate and pruned - /// for maximum compression. - std::ostream& writeBinary(std::ostream &s); - - /// Writes the maximum likelihood OcTree to a binary stream (const variant). - /// Files will be smaller when the tree is pruned first or by using - /// writeBinary() instead. - std::ostream& writeBinaryConst(std::ostream &s) const; - - /// Reads OcTree from a binary file. - /// Existing nodes of the tree are deleted before the tree is read. + /** + * Reads an OcTree from an input stream. + * Existing nodes of the tree are deleted before the tree is read. + * @return success of operation + */ + bool readBinary(std::istream &s); + + /** + * Writes compressed maximum likelihood OcTree to a binary stream. + * The OcTree is first converted to the maximum likelihood estimate and pruned + * for maximum compression. + * @return success of operation + */ + bool writeBinary(std::ostream &s); + + /** + * Writes the maximum likelihood OcTree to a binary stream (const variant). + * Files will be smaller when the tree is pruned first or by using + * writeBinary() instead. + * @return success of operation + */ + bool writeBinaryConst(std::ostream &s) const; + + /** + * Reads OcTree from a binary file. + * Existing nodes of the tree are deleted before the tree is read. + * @return success of operation + */ bool readBinary(const std::string& filename); - /// Writes OcTree to a binary file using writeBinary(). - /// The OcTree is first converted to the maximum likelihood estimate and pruned. + /** + * Writes OcTree to a binary file using writeBinary(). + * The OcTree is first converted to the maximum likelihood estimate and pruned. + * @return success of operation + */ bool writeBinary(const std::string& filename); - /// Writes OcTree to a binary file using writeBinaryConst(). - /// The OcTree is not changed, in particular not pruned first. + /** + * Writes OcTree to a binary file using writeBinaryConst(). + * The OcTree is not changed, in particular not pruned first. + * @return success of operation + */ bool writeBinaryConst(const std::string& filename) const; /** diff --git a/octomap/include/octomap/OccupancyOcTreeBase.hxx b/octomap/include/octomap/OccupancyOcTreeBase.hxx index 3c082fa3..ddae342c 100644 --- a/octomap/include/octomap/OccupancyOcTreeBase.hxx +++ b/octomap/include/octomap/OccupancyOcTreeBase.hxx @@ -698,9 +698,7 @@ namespace octomap { OCTOMAP_ERROR_STR("Filestream to "<< filename << " not open, nothing read."); return false; } - readBinary(binary_infile); - binary_infile.close(); - return true; + return readBinary(binary_infile); } template @@ -734,7 +732,7 @@ namespace octomap { template - std::istream& OccupancyOcTreeBase::readBinary(std::istream &s) { + bool OccupancyOcTreeBase::readBinary(std::istream &s) { if (!s.good()){ OCTOMAP_WARNING_STR("Input filestream not \"good\" in OcTree::readBinary"); @@ -749,7 +747,7 @@ namespace octomap { if (line.compare(0,AbstractOcTree::binaryFileHeader.length(), AbstractOcTree::binaryFileHeader) ==0){ std::string id; if (!AbstractOcTree::readHeader(s, id, size, res)) - return s; + return false; OCTOMAP_DEBUG_STR("Reading binary octree type "<< id); } else{ // try to read old binary format: @@ -763,25 +761,23 @@ namespace octomap { } else { OCTOMAP_ERROR_STR("First line of OcTree file header does not start with \""<< AbstractOcTree::binaryFileHeader<<"\""); - return s; + return false; } } // otherwise: values are valid, stream is now at binary data! this->clear(); this->setResolution(res); - - - this->readBinaryNode(s, this->itsRoot); this->sizeChanged = true; this->tree_size = OcTreeBase::calcNumNodes(); // compute number of nodes if (size != this->tree_size){ OCTOMAP_ERROR("Tree size mismatch: # read nodes (%zu) != # expected nodes (%d)\n",this->tree_size, size); + return false; } - return s; + return true; } template @@ -792,9 +788,7 @@ namespace octomap { OCTOMAP_ERROR_STR("Filestream to "<< filename << " not open, nothing written."); return false; } - writeBinary(binary_outfile); - binary_outfile.close(); - return true; + return writeBinary(binary_outfile); } template @@ -811,7 +805,7 @@ namespace octomap { } template - std::ostream& OccupancyOcTreeBase::writeBinary(std::ostream &s){ + bool OccupancyOcTreeBase::writeBinary(std::ostream &s){ // convert to max likelihood first, this makes efficient pruning on binary data possible this->toMaxLikelihood(); @@ -820,7 +814,7 @@ namespace octomap { } template - std::ostream& OccupancyOcTreeBase::writeBinaryConst(std::ostream &s) const{ + bool OccupancyOcTreeBase::writeBinaryConst(std::ostream &s) const{ // write new header first: s << AbstractOcTree::binaryFileHeader <<"\n# (feel free to add / change comments, but leave the first line as it is!)\n#\n"; s << "id " << this->getTreeType() << std::endl; @@ -830,8 +824,13 @@ namespace octomap { OCTOMAP_DEBUG_STR("Writing " << this->size() << " nodes to output stream..."); this->writeBinaryNode(s, this->itsRoot); - OCTOMAP_DEBUG_STR(" done."); - return s; + if (s.good()){ + OCTOMAP_DEBUG_STR(" done."); + return true; + } else { + OCTOMAP_WARNING_STR("Output stream not \"good\" after writing tree"); + return false; + } } template diff --git a/octomap/src/convert_octree.cpp b/octomap/src/convert_octree.cpp index 7d4dd5fb..2d74a9fd 100644 --- a/octomap/src/convert_octree.cpp +++ b/octomap/src/convert_octree.cpp @@ -88,9 +88,8 @@ int main(int argc, char** argv) { // reading binary: if (inputFilename.length() > 3 && (inputFilename.compare(inputFilename.length()-3, 3, ".bt") == 0)){ OcTree* binaryTree = new OcTree(0.1); - binaryTree->readBinary(file); - if (binaryTree->size() > 1) + if (binaryTree->readBinary(file) && binaryTree->size() > 1) tree = binaryTree; else { OCTOMAP_ERROR_STR("Could not detect binary OcTree format in file.");