ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PCL模型转点Mesh(obj,ply)转pcd

2021-11-18 09:04:17  阅读:322  来源: 互联网

标签:obj default ply indices Mesh argv file print size


代码如下:

#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/io/pcd_io.h>
#include <pcl/common/transforms.h>
#include <vtkPLYReader.h>
#include <vtkOBJReader.h>
#include <vtkPolyDataMapper.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>

using namespace pcl;
using namespace pcl::io;
using namespace pcl::console;

int default_tesselated_sphere_level = 2;
int default_resolution = 100;
float default_leaf_size = 0.01f;

void
printHelp (int, char **argv)
{
  print_error ("Syntax is: %s input.{ply,obj} output.pcd <options>\n", argv[0]);
  print_info ("  where options are:\n");
  print_info ("                     -level X      = tesselated sphere level (default: ");
  print_value ("%d", default_tesselated_sphere_level);
  print_info (")\n");
  print_info ("                     -resolution X = the sphere resolution in angle increments (default: ");
  print_value ("%d", default_resolution);
  print_info (" deg)\n");
  print_info (
              "                     -leaf_size X  = the XYZ leaf size for the VoxelGrid -- for data reduction (default: ");
  print_value ("%f", default_leaf_size);
  print_info (" m)\n");
  print_info (
              "                     -no_vis_result = flag to stop visualizing the generated pcd\n");
}

/* ---[ */
int
main (int argc, char **argv)
{
  print_info ("Convert a CAD model to a point cloud using ray tracing operations. For more information, use: %s -h\n",
              argv[0]);

  if (argc < 3)
  {
    printHelp (argc, argv);
    return (-1);
  }

  // Parse command line arguments
  int tesselated_sphere_level = default_tesselated_sphere_level;
  parse_argument (argc, argv, "-level", tesselated_sphere_level);
  int resolution = default_resolution;
  parse_argument (argc, argv, "-resolution", resolution);
  float leaf_size = default_leaf_size;
  parse_argument (argc, argv, "-leaf_size", leaf_size);
  bool vis_result = ! find_switch (argc, argv, "-no_vis_result");

  // Parse the command line arguments for .ply and PCD files
  std::vector<int> pcd_file_indices = parse_file_extension_argument (argc, argv, ".pcd");
  if (pcd_file_indices.size () != 1)
  {
    print_error ("Need a single output PCD file to continue.\n");
    return (-1);
  }
  std::vector<int> ply_file_indices = parse_file_extension_argument (argc, argv, ".ply");
  std::vector<int> obj_file_indices = parse_file_extension_argument (argc, argv, ".obj");
  if (ply_file_indices.size () != 1 && obj_file_indices.size () != 1)
  {
    print_error ("Need a single input PLY/OBJ file to continue.\n");
    return (-1);
  }

  vtkSmartPointer<vtkPolyData> polydata1;
  if (ply_file_indices.size () == 1)
  {
    vtkSmartPointer<vtkPLYReader> readerQuery = vtkSmartPointer<vtkPLYReader>::New ();
    readerQuery->SetFileName (argv[ply_file_indices[0]]);
    readerQuery->Update ();
    polydata1 = readerQuery->GetOutput ();
  }
  else if (obj_file_indices.size () == 1)
  {
    vtkSmartPointer<vtkOBJReader> readerQuery = vtkSmartPointer<vtkOBJReader>::New ();
    readerQuery->SetFileName (argv[obj_file_indices[0]]);
    readerQuery->Update ();
    polydata1 = readerQuery->GetOutput ();
  }

  bool INTER_VIS = false;

  visualization::PCLVisualizer vis;
  vis.addModelFromPolyData (polydata1, "mesh1", 0);
  vis.setRepresentationToSurfaceForAllActors ();

  PointCloud<PointXYZ>::CloudVectorType views_xyz;
  std::vector<Eigen::Matrix4f, Eigen::aligned_allocator<Eigen::Matrix4f> > poses;
  std::vector<float> enthropies;
  vis.renderViewTesselatedSphere (resolution, resolution, views_xyz, poses, enthropies, tesselated_sphere_level);

  //take views and fuse them together
  std::vector<PointCloud<PointXYZ>::Ptr> aligned_clouds;

  for (size_t i = 0; i < views_xyz.size (); i++)
  {
    PointCloud<PointXYZ>::Ptr cloud (new PointCloud<PointXYZ> ());
    Eigen::Matrix4f pose_inverse;
    pose_inverse = poses[i].inverse ();
    transformPointCloud (views_xyz[i], *cloud, pose_inverse);
    aligned_clouds.push_back (cloud);
  }

  if (INTER_VIS)
  {
    visualization::PCLVisualizer vis2 ("visualize");

    for (size_t i = 0; i < aligned_clouds.size (); i++)
    {
      std::stringstream name;
      name << "cloud_" << i;
      vis2.addPointCloud (aligned_clouds[i], name.str ());
      vis2.spin ();
    }
  }

  // Fuse clouds
  PointCloud<PointXYZ>::Ptr big_boy (new PointCloud<PointXYZ> ());
  for (size_t i = 0; i < aligned_clouds.size (); i++)
    *big_boy += *aligned_clouds[i];

  if (vis_result)
  {
    visualization::PCLVisualizer vis2 ("visualize");
    vis2.addPointCloud (big_boy);
    vis2.spin ();
  }

  // Voxelgrid
  VoxelGrid<PointXYZ> grid_;
  grid_.setInputCloud (big_boy);
  grid_.setLeafSize (leaf_size, leaf_size, leaf_size);
  grid_.filter (*big_boy);

  if (vis_result)
  {
    visualization::PCLVisualizer vis3 ("visualize");
    vis3.addPointCloud (big_boy);
    vis3.spin ();
  }

  savePCDFileASCII (argv[pcd_file_indices[0]], *big_boy);
}

来源:PCL官方示例

标签:obj,default,ply,indices,Mesh,argv,file,print,size
来源: https://blog.csdn.net/com1098247427/article/details/120704580

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有