1 /*
2  * Software License Agreement (BSD License)
3  *
4  *  Point Cloud Library (PCL) - www.pointclouds.org
5  *  Copyright (c) 2010-2011, Willow Garage, Inc.
6  *  Copyright (c) 2012-, Open Perception, Inc.
7  *
8  *  All rights reserved.
9  *
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
13  *
14  *   * Redistributions of source code must retain the above copyright
15  *     notice, this list of conditions and the following disclaimer.
16  *   * Redistributions in binary form must reproduce the above
17  *     copyright notice, this list of conditions and the following
18  *     disclaimer in the documentation and/or other materials provided
19  *     with the distribution.
20  *   * Neither the name of the copyright holder(s) nor the names of its
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  *  POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #include <pcl/PCLPointCloud2.h>
42 #include <pcl/io/pcd_io.h>
43 #include <pcl/features/normal_3d.h>
44 #include <pcl/features/integral_image_normal.h>
45 #include <pcl/console/print.h>
46 #include <pcl/console/parse.h>
47 #include <pcl/console/time.h>
48 #include <boost/filesystem.hpp> // for path, exists, ...
49 #include <boost/algorithm/string/case_conv.hpp> // for to_upper_copy
50 
51 using namespace pcl;
52 using namespace pcl::io;
53 using namespace pcl::console;
54 
55 int    default_k = 0;
56 double default_radius = 0.0;
57 
58 void
printHelp(int,char ** argv)59 printHelp (int, char **argv)
60 {
61   print_error ("Syntax is: %s input.pcd output.pcd <options> [optional_arguments]\n", argv[0]);
62   print_info ("  where options are:\n");
63   print_info ("                     -radius X = use a radius of Xm around each point to determine the neighborhood (default: ");
64   print_value ("%f", default_radius); print_info (")\n");
65   print_info ("                     -k X      = use a fixed number of X-nearest neighbors around each point (default: ");
66   print_value ("%f", default_k); print_info (")\n");
67   print_info (" For organized datasets, an IntegralImageNormalEstimation approach will be used, with the RADIUS given value as SMOOTHING SIZE.\n");
68   print_info ("\nOptional arguments are:\n");
69   print_info ("                     -input_dir X  = batch process all PCD files found in input_dir\n");
70   print_info ("                     -output_dir X = save the processed files from input_dir in this directory\n");
71 }
72 
73 bool
loadCloud(const std::string & filename,pcl::PCLPointCloud2 & cloud,Eigen::Vector4f & translation,Eigen::Quaternionf & orientation)74 loadCloud (const std::string &filename, pcl::PCLPointCloud2 &cloud,
75            Eigen::Vector4f &translation, Eigen::Quaternionf &orientation)
76 {
77   if (loadPCDFile (filename, cloud, translation, orientation) < 0)
78     return (false);
79 
80   return (true);
81 }
82 
83 void
compute(const pcl::PCLPointCloud2::ConstPtr & input,pcl::PCLPointCloud2 & output,int k,double radius)84 compute (const pcl::PCLPointCloud2::ConstPtr &input, pcl::PCLPointCloud2 &output,
85          int k, double radius)
86 {
87   // Convert data to PointCloud<T>
88   PointCloud<PointXYZ>::Ptr xyz (new PointCloud<PointXYZ>);
89   fromPCLPointCloud2 (*input, *xyz);
90 
91   TicToc tt;
92   tt.tic ();
93 
94   PointCloud<Normal> normals;
95 
96   // Try our luck with organized integral image based normal estimation
97   if (xyz->isOrganized ())
98   {
99     IntegralImageNormalEstimation<PointXYZ, Normal> ne;
100     ne.setInputCloud (xyz);
101     ne.setNormalEstimationMethod (IntegralImageNormalEstimation<PointXYZ, Normal>::COVARIANCE_MATRIX);
102     ne.setNormalSmoothingSize (float (radius));
103     ne.setDepthDependentSmoothing (true);
104     ne.compute (normals);
105   }
106   else
107   {
108     NormalEstimation<PointXYZ, Normal> ne;
109     ne.setInputCloud (xyz);
110     ne.setSearchMethod (search::KdTree<PointXYZ>::Ptr (new search::KdTree<PointXYZ>));
111     ne.setKSearch (k);
112     ne.setRadiusSearch (radius);
113     ne.compute (normals);
114   }
115 
116   print_highlight ("Computed normals in "); print_value ("%g", tt.toc ()); print_info (" ms for "); print_value ("%d", normals.width * normals.height); print_info (" points.\n");
117 
118   // Convert data back
119   pcl::PCLPointCloud2 output_normals;
120   toPCLPointCloud2 (normals, output_normals);
121   concatenateFields (*input, output_normals, output);
122 }
123 
124 void
saveCloud(const std::string & filename,const pcl::PCLPointCloud2 & output,const Eigen::Vector4f & translation,const Eigen::Quaternionf & orientation)125 saveCloud (const std::string &filename, const pcl::PCLPointCloud2 &output,
126            const Eigen::Vector4f &translation, const Eigen::Quaternionf &orientation)
127 {
128   PCDWriter w;
129   w.writeBinaryCompressed (filename, output, translation, orientation);
130 }
131 
132 int
batchProcess(const std::vector<std::string> & pcd_files,std::string & output_dir,int k,double radius)133 batchProcess (const std::vector<std::string> &pcd_files, std::string &output_dir, int k, double radius)
134 {
135 #pragma omp parallel for \
136   default(none) \
137   shared(k, output_dir, pcd_files, radius)
138   for (int i = 0; i < int (pcd_files.size ()); ++i)
139   {
140     // Load the first file
141     Eigen::Vector4f translation;
142     Eigen::Quaternionf rotation;
143     pcl::PCLPointCloud2::Ptr cloud (new pcl::PCLPointCloud2);
144     if (!loadCloud (pcd_files[i], *cloud, translation, rotation))
145       continue;
146 
147     // Perform the feature estimation
148     pcl::PCLPointCloud2 output;
149     compute (cloud, output, k, radius);
150 
151     // Prepare output file name
152     std::string filename = boost::filesystem::path(pcd_files[i]).filename().string();
153 
154     // Save into the second file
155     const std::string filepath = output_dir + '/' + filename;
156     saveCloud (filepath, output, translation, rotation);
157   }
158   return (0);
159 }
160 
161 /* ---[ */
162 int
main(int argc,char ** argv)163 main (int argc, char** argv)
164 {
165   print_info ("Estimate surface normals using NormalEstimation. For more information, use: %s -h\n", argv[0]);
166 
167   if (argc < 3)
168   {
169     printHelp (argc, argv);
170     return (-1);
171   }
172 
173   bool batch_mode = false;
174 
175   // Command line parsing
176   int k = default_k;
177   double radius = default_radius;
178   parse_argument (argc, argv, "-k", k);
179   parse_argument (argc, argv, "-radius", radius);
180   std::string input_dir, output_dir;
181   if (parse_argument (argc, argv, "-input_dir", input_dir) != -1)
182   {
183     PCL_INFO ("Input directory given as %s. Batch process mode on.\n", input_dir.c_str ());
184     if (parse_argument (argc, argv, "-output_dir", output_dir) == -1)
185     {
186       PCL_ERROR ("Need an output directory! Please use -output_dir to continue.\n");
187       return (-1);
188     }
189 
190     // Both input dir and output dir given, switch into batch processing mode
191     batch_mode = true;
192   }
193 
194   if (!batch_mode)
195   {
196     // Parse the command line arguments for .pcd files
197     std::vector<int> p_file_indices;
198     p_file_indices = parse_file_extension_argument (argc, argv, ".pcd");
199     if (p_file_indices.size () != 2)
200     {
201       print_error ("Need one input PCD file and one output PCD file to continue.\n");
202       return (-1);
203     }
204 
205     print_info ("Estimating normals with a k/radius/smoothing size of: ");
206     print_value ("%d / %f / %f\n", k, radius, radius);
207 
208     // Load the first file
209     Eigen::Vector4f translation;
210     Eigen::Quaternionf rotation;
211     pcl::PCLPointCloud2::Ptr cloud (new pcl::PCLPointCloud2);
212     if (!loadCloud (argv[p_file_indices[0]], *cloud, translation, rotation))
213       return (-1);
214 
215     // Perform the feature estimation
216     pcl::PCLPointCloud2 output;
217     compute (cloud, output, k, radius);
218 
219     // Save into the second file
220     saveCloud (argv[p_file_indices[1]], output, translation, rotation);
221   }
222   else
223   {
224     if (!input_dir.empty() && boost::filesystem::exists (input_dir))
225     {
226       std::vector<std::string> pcd_files;
227       boost::filesystem::directory_iterator end_itr;
228       for (boost::filesystem::directory_iterator itr (input_dir); itr != end_itr; ++itr)
229       {
230         // Only add PCD files
231         if (!is_directory (itr->status ()) && boost::algorithm::to_upper_copy (boost::filesystem::extension (itr->path ())) == ".PCD" )
232         {
233           pcd_files.push_back (itr->path ().string ());
234           PCL_INFO ("[Batch processing mode] Added %s for processing.\n", itr->path ().string ().c_str ());
235         }
236       }
237       batchProcess (pcd_files, output_dir, k, radius);
238     }
239     else
240     {
241       PCL_ERROR ("Batch processing mode enabled, but invalid input directory (%s) given!\n", input_dir.c_str ());
242       return (-1);
243     }
244   }
245 }
246 
247