00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 * $Id: test_mls.cpp 36039 2011-02-17 20:56:28Z marton $ 00035 * 00036 */ 00037 00038 #include <boost/make_shared.hpp> 00039 #include <pcl/point_types.h> 00040 #include <pcl/io/pcd_io.h> 00041 #include <pcl/io/vtk_io.h> 00042 #include <pcl/kdtree/kdtree_flann.h> 00043 #include <pcl/surface/mls.h> 00044 #include <pcl/surface/gp3.h> 00045 #include <pcl/surface/grid_projection.h> 00046 00047 using namespace pcl; 00048 using namespace pcl::io; 00049 using namespace std; 00050 00051 typedef KdTree<PointXYZ>::Ptr KdTreePtr; 00052 00053 PointCloud<PointXYZ>::Ptr cloud (new PointCloud<PointXYZ> ()); 00054 vector<int> indices; 00055 KdTreePtr tree; 00056 00057 /* ---[ */ 00058 int 00059 main (int argc, char** argv) 00060 { 00061 //* 00062 // Input 00063 sensor_msgs::PointCloud2 cloud_blob; 00064 loadPCDFile ("./test/bun0.pcd", cloud_blob); 00065 fromROSMsg (cloud_blob, *cloud); 00066 00067 // Indices 00068 indices.resize (cloud->points.size ()); 00069 for (size_t i = 0; i < indices.size (); ++i) { indices[i] = i; } 00070 00071 // KD-Tree 00072 tree = boost::make_shared<KdTreeFLANN<PointXYZ> > (); 00073 tree->setInputCloud (cloud); 00074 00075 // Init objects 00076 PointCloud<PointXYZ> mls_points; 00077 PointCloud<Normal>::Ptr mls_normals (new PointCloud<Normal> ()); 00078 MovingLeastSquares<PointXYZ, Normal> mls; 00079 00080 // Set parameters 00081 mls.setInputCloud (cloud); 00082 mls.setIndices (boost::make_shared <vector<int> > (indices)); 00083 mls.setPolynomialFit (true); 00084 mls.setSearchMethod (tree); 00085 mls.setSearchRadius (0.03); 00086 00087 // Reconstruct 00088 //ros::Time::init(); 00089 //ros::Time t = ros::Time::now (); 00090 mls.setOutputNormals (mls_normals); 00091 mls.reconstruct (mls_points); 00092 //ROS_INFO ("MLS fit done in %g seconds.", (ros::Time::now () - t).toSec ()); 00093 //ROS_INFO ("MLS fit of %zu points and %zu normals.", mls_points.points.size (), mls_normals->points.size ()); 00094 00095 PointCloud<PointNormal> mls_cloud; 00096 pcl::concatenateFields (mls_points, *mls_normals, mls_cloud); 00097 00098 // Save output 00099 savePCDFile ("./test/bun0-mls.pcd", mls_cloud); 00100 00101 //*/ 00102 // sensor_msgs::PointCloud2 cloud_blob; 00103 // PointCloud<PointNormal> mls_points; 00104 // loadPCDFile ("./test/bun0-mls.pcd", cloud_blob); 00105 // fromROSMsg (cloud_blob, mls_points); 00106 // indices.resize (mls_points.points.size ()); 00107 // for (size_t i = 0; i < indices.size (); ++i) { indices[i] = i; } 00108 00109 // Test triangulation 00110 std::cerr << "TESTING TRIANGULATION" << std::endl; 00111 KdTree<PointNormal>::Ptr tree2 = boost::make_shared<KdTreeFLANN<PointNormal> > (); 00112 tree2->setInputCloud (mls_cloud.makeShared ()); 00113 PolygonMesh triangles; 00114 GreedyProjectionTriangulation<PointNormal> gp3; 00115 /* GridProjection<PointNormal> gp3; 00116 gp3.setInputCloud (mls_points.makeShared ()); 00117 gp3.setSearchMethod (tree2); 00118 gp3.setResolution (0.01); 00119 gp3.setPaddingSize (3);*/ 00120 gp3.setInputCloud (mls_cloud.makeShared ()); 00121 gp3.setSearchMethod (tree2); 00122 gp3.setIndices (boost::make_shared <vector<int> > (indices)); 00123 gp3.setSearchRadius (0.02); 00124 gp3.setMu (2.5); 00125 gp3.setMaximumNearestNeighbors (50); 00126 gp3.setMaximumSurfaceAgle(M_PI/4); // 45 degrees 00127 gp3.setMinimumAngle(M_PI/18); // 10 degrees 00128 gp3.setMaximumAngle(2*M_PI/3); // 120 degrees 00129 gp3.setNormalConsistency(false); 00130 gp3.reconstruct (triangles); 00131 saveVTKFile ("./test/bun0-mls.vtk", triangles); 00132 //std::cerr << "INPUT: ./test/bun0-mls.pcd" << std::endl; 00133 std::cerr << "OUTPUT: ./test/bun0-mls.vtk" << std::endl; 00134 00135 // Finish 00136 return (0); 00137 } 00138 /* ]--- */