diff --git a/io/include/pcl/io/vtk_lib_io.h b/io/include/pcl/io/vtk_lib_io.h index f5cbd23c1c5..9dc7a0e3d48 100644 --- a/io/include/pcl/io/vtk_lib_io.h +++ b/io/include/pcl/io/vtk_lib_io.h @@ -119,11 +119,14 @@ namespace pcl /** \brief Save a \ref PolygonMesh object given an input file name, based on the file extension * \param[in] file_name the name of the file to save the data to * \param[in] mesh the object that contains the data + * \param[in] binary_format if true, exported file is in binary format + * \return True if successful, false otherwise * \ingroup io */ - PCL_EXPORTS int + PCL_EXPORTS bool savePolygonFile (const std::string &file_name, - const pcl::PolygonMesh& mesh); + const pcl::PolygonMesh& mesh, + const bool binary_format = true); /** \brief Load a VTK file into a \ref PolygonMesh object * \param[in] file_name the name of the file that contains the data @@ -177,29 +180,38 @@ namespace pcl /** \brief Save a \ref PolygonMesh object into a VTK file * \param[in] file_name the name of the file to save the data to * \param[in] mesh the object that contains the data + * \param[in] binary_format if true, exported file is in binary format + * \return True if successful, false otherwise * \ingroup io */ - PCL_EXPORTS int + PCL_EXPORTS bool savePolygonFileVTK (const std::string &file_name, - const pcl::PolygonMesh& mesh); + const pcl::PolygonMesh& mesh, + const bool binary_format = true); /** \brief Save a \ref PolygonMesh object into a PLY file * \param[in] file_name the name of the file to save the data to * \param[in] mesh the object that contains the data + * \param[in] binary_format if true, exported file is in binary format + * \return True if successful, false otherwise * \ingroup io */ - PCL_EXPORTS int + PCL_EXPORTS bool savePolygonFilePLY (const std::string &file_name, - const pcl::PolygonMesh& mesh); + const pcl::PolygonMesh& mesh, + const bool binary_format = true); /** \brief Save a \ref PolygonMesh object into an STL file * \param[in] file_name the name of the file to save the data to * \param[in] mesh the object that contains the data + * \param[in] binary_format if true, exported file is in binary format + * \return True if successful, false otherwise * \ingroup io */ - PCL_EXPORTS int + PCL_EXPORTS bool savePolygonFileSTL (const std::string &file_name, - const pcl::PolygonMesh& mesh); + const pcl::PolygonMesh& mesh, + const bool binary_format = true); /** \brief Write a \ref RangeImagePlanar object to a PNG file * \param[in] file_name the name of the file to save the data to diff --git a/io/src/vtk_lib_io.cpp b/io/src/vtk_lib_io.cpp index 983d9c277eb..e00bd79d97a 100644 --- a/io/src/vtk_lib_io.cpp +++ b/io/src/vtk_lib_io.cpp @@ -74,26 +74,21 @@ pcl::io::loadPolygonFile (const std::string &file_name, pcl::PolygonMesh& mesh) } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -int -pcl::io::savePolygonFile (const std::string &file_name, const pcl::PolygonMesh& mesh) +bool +pcl::io::savePolygonFile (const std::string &file_name, + const pcl::PolygonMesh& mesh, + const bool binary_format) { - // TODO: what about binary/ASCII modes?!?!?! // TODO: what about sensor position and orientation?!?!?!? - // TODO: how to adequately catch exceptions thrown by the vtk writers?! std::string extension = file_name.substr (file_name.find_last_of (".") + 1); - if (extension == "pcd") // no Polygon, but only a point cloud - { - int error_code = pcl::io::savePCDFile (file_name, mesh.cloud); - if (error_code != 0) - return (0); - return (static_cast (mesh.cloud.width * mesh.cloud.height)); - } + if (extension == "pcd") // no Polygon, but only a point cloud + return (pcl::io::savePCDFile (file_name, mesh.cloud, Eigen::Vector4f::Zero (), Eigen::Quaternionf::Identity (), binary_format) == 0); else if (extension == "vtk") - return (pcl::io::savePolygonFileVTK (file_name, mesh)); + return (pcl::io::savePolygonFileVTK (file_name, mesh, binary_format)); else if (extension == "ply") - return (pcl::io::savePolygonFilePLY (file_name, mesh)); - else if (extension == "stl" ) - return (pcl::io::savePolygonFileSTL (file_name, mesh)); + return (pcl::io::savePolygonFilePLY (file_name, mesh, binary_format)); + else if (extension == "stl") + return (pcl::io::savePolygonFileSTL (file_name, mesh, binary_format)); else { PCL_ERROR ("[pcl::io::savePolygonFile]: Unsupported file type (%s)\n", extension.c_str ()); @@ -171,8 +166,10 @@ pcl::io::loadPolygonFileSTL (const std::string &file_name, pcl::PolygonMesh& mes } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -int -pcl::io::savePolygonFileVTK (const std::string &file_name, const pcl::PolygonMesh& mesh) +bool +pcl::io::savePolygonFileVTK (const std::string &file_name, + const pcl::PolygonMesh& mesh, + const bool binary_format) { vtkSmartPointer poly_data = vtkSmartPointer::New (); @@ -184,15 +181,19 @@ pcl::io::savePolygonFileVTK (const std::string &file_name, const pcl::PolygonMes #else poly_writer->SetInputData (poly_data); #endif - poly_writer->SetFileName (file_name.c_str ()); - poly_writer->Write (); - return (static_cast (mesh.cloud.width * mesh.cloud.height)); + if (!binary_format) + poly_writer->SetFileTypeToASCII (); + + poly_writer->SetFileName (file_name.c_str ()); + return (poly_writer->Write ()); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -int -pcl::io::savePolygonFilePLY (const std::string &file_name, const pcl::PolygonMesh& mesh) +bool +pcl::io::savePolygonFilePLY (const std::string &file_name, + const pcl::PolygonMesh& mesh, + const bool binary_format) { vtkSmartPointer poly_data = vtkSmartPointer::New (); @@ -204,16 +205,20 @@ pcl::io::savePolygonFilePLY (const std::string &file_name, const pcl::PolygonMes #else poly_writer->SetInputData (poly_data); #endif + + if (!binary_format) + poly_writer->SetFileTypeToASCII (); + poly_writer->SetFileName (file_name.c_str ()); poly_writer->SetArrayName ("Colors"); - poly_writer->Write (); - - return (static_cast (mesh.cloud.width * mesh.cloud.height)); + return (poly_writer->Write ()); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -int -pcl::io::savePolygonFileSTL (const std::string &file_name, const pcl::PolygonMesh& mesh) +bool +pcl::io::savePolygonFileSTL (const std::string &file_name, + const pcl::PolygonMesh& mesh, + const bool binary_format) { vtkSmartPointer poly_data = vtkSmartPointer::New (); @@ -224,10 +229,12 @@ pcl::io::savePolygonFileSTL (const std::string &file_name, const pcl::PolygonMes #else poly_writer->SetInputData (poly_data); #endif - poly_writer->SetFileName (file_name.c_str ()); - poly_writer->Write (); - return (static_cast (mesh.cloud.width * mesh.cloud.height)); + if (!binary_format) + poly_writer->SetFileTypeToASCII (); + + poly_writer->SetFileName (file_name.c_str ()); + return (poly_writer->Write ()); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////