Skip to content

Commit

Permalink
[TOOL] Uniform sampling: be able to deal with PCD, PLY and VTK file f…
Browse files Browse the repository at this point in the history
…ormats.
  • Loading branch information
frozar committed Jun 25, 2018
1 parent 39fe6b3 commit e17505f
Showing 1 changed file with 79 additions and 15 deletions.
94 changes: 79 additions & 15 deletions tools/uniform_sampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@
*/

#include <pcl/PCLPointCloud2.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/auto_io.h>
#include <pcl/filters/uniform_sampling.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
#include <pcl/console/time.h>
#include <boost/filesystem.hpp>
#include <algorithm>
#include <string>
#include <pcl/io/vtk_io.h>

using namespace std;
using namespace pcl;
Expand All @@ -49,13 +53,20 @@ using namespace pcl::console;

double default_radius = 0.01;

Eigen::Vector4f translation;
Eigen::Quaternionf orientation;
/** \brief File type for saving and loading files. */
typedef enum FileType
{
FT_PCD = 0,
FT_PLY = 1, /**< Polygon File Format. */
FT_VTK = 2 /**< VTK File Format. */
} FileType;

void
printHelp (int, char **argv)
{
print_error ("Syntax is: %s input.pcd output.pcd <options>\n", argv[0]);
print_error ("Syntax is: %s <input_point_cloud> <output_point_cloud> <options>\n", argv[0]);
print_info ("This tool rely on the file extensions to guess the good reader/writer.\n");
print_info ("The supported extension for the point cloud are .pcd .ply and .vtk\n");
print_info (" where options are:\n");
print_info (" -radius X = use a leaf size of X,X,X to uniformly select 1 point per leaf (default: ");
print_value ("%f", default_radius); print_info (")\n");
Expand All @@ -68,9 +79,10 @@ loadCloud (const string &filename, pcl::PCLPointCloud2 &cloud)
print_highlight ("Loading "); print_value ("%s ", filename.c_str ());

tt.tic ();
if (loadPCDFile (filename, cloud, translation, orientation) < 0)
if (pcl::io::load (filename, cloud))
return (false);
print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms : "); print_value ("%d", cloud.width * cloud.height); print_info (" points]\n");
print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms : ");
print_value ("%d", cloud.width * cloud.height); print_info (" points]\n");
print_info ("Available dimensions: "); print_value ("%s\n", getFieldsList (cloud).c_str ());

return (true);
Expand All @@ -96,22 +108,38 @@ compute (const pcl::PCLPointCloud2::ConstPtr &input, pcl::PCLPointCloud2 &output
PointCloud<PointXYZ> output_;
us.filter (output_);

print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms : "); print_value ("%d", output_.size()); print_info (" points]\n");
print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms : ");
print_value ("%d", output_.size()); print_info (" points]\n");

// Convert data back
toPCLPointCloud2 (output_, output);
}

void
saveCloud (const string &filename, const pcl::PCLPointCloud2 &output)
saveCloud (const string &filename, const pcl::PCLPointCloud2 &output,
const FileType &output_file_type)
{
TicToc tt;
tt.tic ();

print_highlight ("Saving "); print_value ("%s ", filename.c_str ());

PCDWriter w;
w.writeBinaryCompressed (filename, output, translation, orientation);
PCDWriter w_pcd;
PLYWriter w_ply;
switch (output_file_type)
{
case FT_PCD:
// w_pcd.writeBinaryCompressed (filename, output, translation, orientation);
w_pcd.writeBinaryCompressed (filename, output);
break;
case FT_PLY:
// w_ply.writeBinary (filename, output, translation, orientation);
w_ply.writeBinary (filename, output);
break;
case FT_VTK:
pcl::io::saveVTKFile (filename, output);
break;
}

print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms : "); print_value ("%d", output.width * output.height); print_info (" points]\n");
}
Expand All @@ -130,10 +158,47 @@ main (int argc, char** argv)

// Parse the command line arguments for .pcd files
vector<int> p_file_indices;
p_file_indices = parse_file_extension_argument (argc, argv, ".pcd");
vector<std::string> extension;
extension.push_back (".pcd");
extension.push_back (".ply");
extension.push_back (".vtk");
p_file_indices = parse_file_extension_argument (argc, argv, extension);

if (p_file_indices.size () != 2)
{
print_error ("Need one input PCD file and one output PCD file to continue.\n");
print_error ("Need one input file and one output file to continue.\n");
return (-1);
}

// Check if the input point cloud exists
std::string input_filename = argv[p_file_indices[0]];
ifstream ifile (input_filename.c_str ());
if (!ifile) {
print_error ("Cannot found input file name (%s).\n", input_filename);
return (-1);
}

// Guess the output format
std::string output_filename = argv[p_file_indices[1]];
std::string output_ext = boost::filesystem::extension (output_filename);
std::transform (output_ext.begin (), output_ext.end (), output_ext.begin (), ::tolower);

FileType output_file_type;
if (output_ext.compare(".pcd") == 0)
{
output_file_type = FT_PCD;
}
else if (output_ext.compare(".ply") == 0)
{
output_file_type = FT_PLY;
}
else if (output_ext.compare(".vtk") == 0)
{
output_file_type = FT_VTK;
}
else
{
print_error ("The output filename extension (%s) is not supported.\n", output_ext);
return (-1);
}

Expand All @@ -145,14 +210,13 @@ main (int argc, char** argv)

// Load the first file
pcl::PCLPointCloud2::Ptr cloud (new pcl::PCLPointCloud2);
if (!loadCloud (argv[p_file_indices[0]], *cloud))
if (!loadCloud (input_filename, *cloud))
return (-1);

// Perform the keypoint estimation
pcl::PCLPointCloud2 output;
compute (cloud, output, radius);

// Save into the second file
saveCloud (argv[p_file_indices[1]], output);
saveCloud (output_filename, output, output_file_type);
}

0 comments on commit e17505f

Please sign in to comment.