Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <pcl/PCLPointCloud2.h>
00039 #include <pcl/point_types.h>
00040 #include <pcl/io/pcd_io.h>
00041 #include <pcl/console/print.h>
00042 #include <pcl/console/parse.h>
00043 #include <pcl/console/time.h>
00044 #include "boost.h"
00045
00046 using namespace pcl;
00047 using namespace pcl::io;
00048 using namespace pcl::console;
00049
00050 double default_standard_deviation = 0.01;
00051
00052 void
00053 printHelp (int, char **argv)
00054 {
00055 print_error ("Syntax is: %s input.pcd output.pcd <options>\n", argv[0]);
00056 print_info (" where options are:\n");
00057 print_info (" -sd X = the standard deviation for the normal distribution (default: ");
00058 print_value ("%f", default_standard_deviation); print_info (")\n");
00059 }
00060
00061 bool
00062 loadCloud (const std::string &filename, pcl::PCLPointCloud2 &cloud)
00063 {
00064 TicToc tt;
00065 print_highlight ("Loading "); print_value ("%s ", filename.c_str ());
00066
00067 tt.tic ();
00068 if (loadPCDFile (filename, cloud) < 0)
00069 return (false);
00070 print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms: "); print_value ("%d", cloud.width * cloud.height); print_info (" points]\n");
00071 print_info ("Available dimensions: "); print_value ("%s\n", pcl::getFieldsList (cloud).c_str ());
00072
00073 return (true);
00074 }
00075
00076 void
00077 compute (const pcl::PCLPointCloud2::ConstPtr &input, pcl::PCLPointCloud2 &output,
00078 double standard_deviation)
00079 {
00080 TicToc tt;
00081 tt.tic ();
00082
00083 print_highlight ("Adding Gaussian noise with mean 0.0 and standard deviation %f\n", standard_deviation);
00084
00085 PointCloud<PointXYZ>::Ptr xyz_cloud (new pcl::PointCloud<PointXYZ> ());
00086 fromPCLPointCloud2 (*input, *xyz_cloud);
00087
00088 PointCloud<PointXYZ>::Ptr xyz_cloud_filtered (new PointCloud<PointXYZ> ());
00089 xyz_cloud_filtered->points.resize (xyz_cloud->points.size ());
00090 xyz_cloud_filtered->header = xyz_cloud->header;
00091 xyz_cloud_filtered->width = xyz_cloud->width;
00092 xyz_cloud_filtered->height = xyz_cloud->height;
00093
00094
00095 boost::mt19937 rng; rng.seed (static_cast<unsigned int> (time (0)));
00096 boost::normal_distribution<> nd (0, standard_deviation);
00097 boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > var_nor (rng, nd);
00098
00099 for (size_t point_i = 0; point_i < xyz_cloud->points.size (); ++point_i)
00100 {
00101 xyz_cloud_filtered->points[point_i].x = xyz_cloud->points[point_i].x + static_cast<float> (var_nor ());
00102 xyz_cloud_filtered->points[point_i].y = xyz_cloud->points[point_i].y + static_cast<float> (var_nor ());
00103 xyz_cloud_filtered->points[point_i].z = xyz_cloud->points[point_i].z + static_cast<float> (var_nor ());
00104 }
00105
00106 pcl::PCLPointCloud2 input_xyz_filtered;
00107 toPCLPointCloud2 (*xyz_cloud_filtered, input_xyz_filtered);
00108 concatenateFields (*input, input_xyz_filtered, output);
00109
00110 print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms: "); print_value ("%d", output.width * output.height); print_info (" points]\n");
00111 }
00112
00113 void
00114 saveCloud (const std::string &filename, const pcl::PCLPointCloud2 &output)
00115 {
00116 TicToc tt;
00117 tt.tic ();
00118
00119 print_highlight ("Saving "); print_value ("%s ", filename.c_str ());
00120
00121 pcl::io::savePCDFile (filename, output);
00122
00123 print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms: "); print_value ("%d", output.width * output.height); print_info (" points]\n");
00124 }
00125
00126
00127 int
00128 main (int argc, char** argv)
00129 {
00130 print_info ("Add Gaussian noise to a point cloud. For more information, use: %s -h\n", argv[0]);
00131
00132 if (argc < 3)
00133 {
00134 printHelp (argc, argv);
00135 return (-1);
00136 }
00137
00138
00139 std::vector<int> p_file_indices;
00140 p_file_indices = parse_file_extension_argument (argc, argv, ".pcd");
00141 if (p_file_indices.size () != 2)
00142 {
00143 print_error ("Need one input PCD file and one output PCD file to continue.\n");
00144 return (-1);
00145 }
00146
00147
00148 double standard_deviation = default_standard_deviation;
00149 parse_argument (argc, argv, "-sd", standard_deviation);
00150
00151
00152 pcl::PCLPointCloud2::Ptr cloud (new pcl::PCLPointCloud2);
00153 if (!loadCloud (argv[p_file_indices[0]], *cloud))
00154 return (-1);
00155
00156
00157 pcl::PCLPointCloud2 output;
00158 compute (cloud, output, standard_deviation);
00159
00160
00161 saveCloud (argv[p_file_indices[1]], output);
00162 }
00163