Advertisement
ana-GT

Change in PCL-1.7 mesh2pcd

Nov 23rd, 2013
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.44 KB | None | 0 0
  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.  *
  7.  *  All rights reserved.
  8.  *
  9.  *  Redistribution and use in source and binary forms, with or without
  10.  *  modification, are permitted provided that the following conditions
  11.  *  are met:
  12.  *
  13.  *   * Redistributions of source code must retain the above copyright
  14.  *     notice, this list of conditions and the following disclaimer.
  15.  *   * Redistributions in binary form must reproduce the above
  16.  *     copyright notice, this list of conditions and the following
  17.  *     disclaimer in the documentation and/or other materials provided
  18.  *     with the distribution.
  19.  *   * Neither the name of the copyright holder(s) nor the names of its
  20.  *     contributors may be used to endorse or promote products derived
  21.  *     from this software without specific prior written permission.
  22.  *
  23.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24.  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25.  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26.  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27.  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28.  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29.  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31.  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33.  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34.  *  POSSIBILITY OF SUCH DAMAGE.
  35.  *
  36.  */
  37.  
  38. #include <pcl/visualization/pcl_visualizer.h>
  39. #include <pcl/io/pcd_io.h>
  40. #include <pcl/common/transforms.h>
  41. #include <vtkPLYReader.h>
  42. #include <vtkOBJReader.h>
  43. #include <vtkPolyDataMapper.h>
  44. #include <pcl/filters/voxel_grid.h>
  45. #include <pcl/console/print.h>
  46. #include <pcl/console/parse.h>
  47.  
  48. #include <pcl/apps/render_views_tesselated_sphere.h>
  49.  
  50. using namespace pcl;
  51. using namespace pcl::io;
  52. using namespace pcl::console;
  53.  
  54. int default_tesselated_sphere_level = 2;
  55. int default_resolution = 100;
  56. float default_leaf_size = 0.01f;
  57.  
  58. void printHelp (int, char **argv)
  59. {
  60.   print_error ("Syntax is: %s input.{ply,obj} output.pcd <options>\n", argv[0]);
  61.   print_info ("  where options are:\n");
  62.   print_info ("                     -level X      = tesselated sphere level (default: ");
  63.   print_value ("%d", default_tesselated_sphere_level);
  64.   print_info (")\n");
  65.   print_info ("                     -resolution X = the sphere resolution in angle increments (default: ");
  66.   print_value ("%d", default_resolution);
  67.   print_info (" deg)\n");
  68.   print_info (
  69.               "                     -leaf_size X  = the XYZ leaf size for the VoxelGrid -- for data reduction (default: ");
  70.   print_value ("%f", default_leaf_size);
  71.   print_info (" m)\n");
  72. }
  73.  
  74. /**
  75.  * @function main
  76.  */
  77. int main (int argc, char **argv)
  78. {
  79.   print_info ("Convert a CAD model to a point cloud using ray tracing operations. For more information, use: %s -h\n",
  80.               argv[0]);
  81.  
  82.   if (argc < 3)
  83.   {
  84.     printHelp (argc, argv);
  85.     return (-1);
  86.   }
  87.  
  88.   // Parse command line arguments
  89.   int tesselated_sphere_level = default_tesselated_sphere_level;
  90.   parse_argument (argc, argv, "-level", tesselated_sphere_level);
  91.   int resolution = default_resolution;
  92.   parse_argument (argc, argv, "-resolution", resolution);
  93.   float leaf_size = default_leaf_size;
  94.   parse_argument (argc, argv, "-leaf_size", leaf_size);
  95.  
  96.   // Parse the command line arguments for .ply and PCD files
  97.   std::vector<int> pcd_file_indices = parse_file_extension_argument (argc, argv, ".pcd");
  98.   if (pcd_file_indices.size () != 1)
  99.   {
  100.     print_error ("Need a single output PCD file to continue.\n");
  101.     return (-1);
  102.   }
  103.   std::vector<int> ply_file_indices = parse_file_extension_argument (argc, argv, ".ply");
  104.   std::vector<int> obj_file_indices = parse_file_extension_argument (argc, argv, ".obj");
  105.   if (ply_file_indices.size () != 1 && obj_file_indices.size () != 1)
  106.   {
  107.     print_error ("Need a single input PLY/OBJ file to continue.\n");
  108.     return (-1);
  109.   }
  110.  
  111.   //  vtkSmartPointer < vtkPolyDataMapper > mapper = vtkSmartPointer<vtkPolyDataMapper>::New ();
  112.   //mapper->Update ();
  113.  
  114.   vtkSmartPointer<vtkPolyData> polydata1;
  115.   if (ply_file_indices.size () == 1)
  116.   {
  117.     vtkSmartPointer<vtkPLYReader> readerQuery = vtkSmartPointer<vtkPLYReader>::New ();
  118.     readerQuery->SetFileName (argv[ply_file_indices[0]]);
  119.     polydata1 = readerQuery->GetOutput ();
  120.     polydata1->Update ();
  121.  
  122.     //    mapper->SetInputConnection (reader->GetOutputPort ());
  123.   }
  124.   else if (obj_file_indices.size () == 1)
  125.   {
  126.     vtkSmartPointer<vtkOBJReader> readerQuery = vtkSmartPointer<vtkOBJReader>::New ();
  127.     readerQuery->SetFileName (argv[obj_file_indices[0]]);
  128.     polydata1 = readerQuery->GetOutput ();
  129.     polydata1->Update ();
  130.   }
  131.  
  132.   bool INTER_VIS = false;
  133.   bool VIS = true;
  134.  
  135.   visualization::PCLVisualizer vis;
  136.   vis.addModelFromPolyData (polydata1, "mesh1", 0);
  137.   vis.setRepresentationToSurfaceForAllActors ();
  138.  
  139.   //PointCloud<PointXYZ>::CloudVectorType views_xyz;
  140.   std::vector< PointCloud<PointXYZ>::Ptr > views_xyz;
  141.   std::vector<Eigen::Matrix4f, Eigen::aligned_allocator<Eigen::Matrix4f> > poses;
  142.   std::vector<float> enthropies;
  143.   //vis.renderViewTesselatedSphere (resolution, resolution, views_xyz, poses, enthropies, tesselated_sphere_level);
  144.  
  145.   pcl::apps::RenderViewsTesselatedSphere render_views;
  146.   render_views.addModelFromPolyData( polydata1 );
  147.   render_views.setResolution( resolution );
  148.   render_views.setComputeEntropies( true );
  149.   render_views.setTesselationLevel( tesselated_sphere_level );
  150.  
  151.   render_views.generateViews();
  152.  
  153.   render_views.getPoses( poses );
  154.   render_views.getViews( views_xyz );
  155.   render_views.getEntropies( enthropies );
  156.  
  157.   //take views and fuse them together
  158.   std::vector<PointCloud<PointXYZ>::Ptr> aligned_clouds;
  159.  
  160.   for (size_t i = 0; i < views_xyz.size (); i++)
  161.     {
  162.       PointCloud<PointXYZ>::Ptr cloud (new PointCloud<PointXYZ> ());
  163.       Eigen::Matrix4f pose_inverse;
  164.       pose_inverse = poses[i].inverse ();
  165.       transformPointCloud ( *views_xyz[i], *cloud, pose_inverse);
  166.       aligned_clouds.push_back (cloud);
  167.     }
  168.  
  169.   // ** INTER_VIS **
  170.   if (INTER_VIS)
  171.     {
  172.       visualization::PCLVisualizer vis2 ("visualize");
  173.      
  174.       for (size_t i = 0; i < aligned_clouds.size (); i++)
  175.     {
  176.       std::stringstream name;
  177.       name << "cloud_" << i;
  178.       vis2.addPointCloud (aligned_clouds[i], name.str ());
  179.  
  180.       vis2.spin ();
  181.     }
  182.     }
  183.  
  184.   // Fuse clouds
  185.   PointCloud<PointXYZ>::Ptr big_boy (new PointCloud<PointXYZ> ());
  186.   for (size_t i = 0; i < aligned_clouds.size (); i++)
  187.     *big_boy += *aligned_clouds[i];
  188.  
  189.   // ** VIS **
  190.   if (VIS)
  191.     {
  192.       visualization::PCLVisualizer vis2 ("visualize");
  193.       vis2.addPointCloud (big_boy);
  194.       vis2.spin ();
  195.     }
  196.  
  197.   // Voxelgrid
  198.   VoxelGrid<PointXYZ> grid_;
  199.   grid_.setInputCloud (big_boy);
  200.   grid_.setLeafSize (leaf_size, leaf_size, leaf_size);
  201.   grid_.filter (*big_boy);
  202.  
  203.   if (VIS)
  204.     {
  205.       visualization::PCLVisualizer vis3 ("visualize");
  206.       vis3.addPointCloud (big_boy);
  207.       vis3.spin ();
  208.     }
  209.  
  210.   printf("Saving to PCD \n");
  211.   savePCDFileASCII (argv[pcd_file_indices[0]], *big_boy);
  212.  
  213.   printf(" Closing visualization windows \n");
  214.  
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement