Skip to content

Commit

Permalink
write/readBinary now return bool, just like read/write
Browse files Browse the repository at this point in the history
  • Loading branch information
Armin Hornung committed Dec 20, 2011
1 parent 95bc305 commit 2ef3e92
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 41 deletions.
4 changes: 2 additions & 2 deletions octomap/include/octomap/AbstractOcTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 38 additions & 20 deletions octomap/include/octomap/OccupancyOcTreeBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
33 changes: 16 additions & 17 deletions octomap/include/octomap/OccupancyOcTreeBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class NODE>
Expand Down Expand Up @@ -734,7 +732,7 @@ namespace octomap {


template <class NODE>
std::istream& OccupancyOcTreeBase<NODE>::readBinary(std::istream &s) {
bool OccupancyOcTreeBase<NODE>::readBinary(std::istream &s) {

if (!s.good()){
OCTOMAP_WARNING_STR("Input filestream not \"good\" in OcTree::readBinary");
Expand All @@ -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:
Expand All @@ -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<NODE>::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 <class NODE>
Expand All @@ -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 <class NODE>
Expand All @@ -811,7 +805,7 @@ namespace octomap {
}

template <class NODE>
std::ostream& OccupancyOcTreeBase<NODE>::writeBinary(std::ostream &s){
bool OccupancyOcTreeBase<NODE>::writeBinary(std::ostream &s){

// convert to max likelihood first, this makes efficient pruning on binary data possible
this->toMaxLikelihood();
Expand All @@ -820,7 +814,7 @@ namespace octomap {
}

template <class NODE>
std::ostream& OccupancyOcTreeBase<NODE>::writeBinaryConst(std::ostream &s) const{
bool OccupancyOcTreeBase<NODE>::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;
Expand All @@ -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 <class NODE>
Expand Down
3 changes: 1 addition & 2 deletions octomap/src/convert_octree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down

0 comments on commit 2ef3e92

Please sign in to comment.