PCL从CAD模型采样得到点云

1、整体点云采样

#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/vtk/pcl_vtk_compatibility.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/vtk_lib_io.h>
#include <vtkVersion.h>
#include <vtkOBJReader.h>
#include <vtkTriangle.h>
#include <vtkTriangleFilter.h>
#include <vtkPolyDataMapper.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>

inline double
uniform_deviate (int seed)
{
    
    
  double ran = seed * (1.0 / (RAND_MAX + 1.0));
  return ran;
}

inline void
randomPointTriangle (float a1, float a2, float a3, float b1, float b2, float b3, float c1, float c2, float c3,
                     float r1, float r2, Eigen::Vector3f& p)
{
    
    
  float r1sqr = std::sqrt (r1);
  float OneMinR1Sqr = (1 - r1sqr);
  float OneMinR2 = (1 - r2);
  a1 *= OneMinR1Sqr;
  a2 *= OneMinR1Sqr;
  a3 *= OneMinR1Sqr;
  b1 *= OneMinR2;
  b2 *= OneMinR2;
  b3 *= OneMinR2;
  c1 = r1sqr * (r2 * c1 + b1) + a1;
  c2 = r1sqr * (r2 * c2 + b2) + a2;
  c3 = r1sqr * (r2 * c3 + b3) + a3;
  p[0] = c1;
  p[1] = c2;
  p[2] = c3;
}

inline void
randPSurface (vtkPolyData * polydata, std::vector<double> * cumulativeAreas, double totalArea, Eigen::Vector3f& p, bool calcNormal, Eigen::Vector3f& n, bool calcColor, Eigen::Vector3f& c)
{
    
    
  float r = static_cast<float> (uniform_deviate (rand ()) * totalArea);

  auto low = std::lower_bound (cumulativeAreas->begin (), cumulativeAreas->end (), r);
  vtkIdType el = vtkIdType (low - cumulativeAreas->begin ());

  double A[3], B[3], C[3];
  vtkIdType npts = 0;
  vtkCellPtsPtr ptIds = nullptr;

  polydata->GetCellPoints (el, npts, ptIds);
  polydata->GetPoint (ptIds[0], A);
  polydata->GetPoint (ptIds[1], B);
  polydata->GetPoint (ptIds[2], C);
  if (calcNormal)
  {
    
    
    // OBJ: Vertices are stored in a counter-clockwise order by default
    Eigen::Vector3f v1 = Eigen::Vector3f (A[0], A[1], A[2]) - Eigen::Vector3f (C[0], C[1], C[2]);
    Eigen::Vector3f v2 = Eigen::Vector3f (B[0], B[1], B[2]) - Eigen::Vector3f (C[0], C[1], C[2]);
    n = v1.cross (v2);
    n.normalize ();
  }
  float r1 = static_cast<float> (uniform_deviate (rand ()));
  float r2 = static_cast<float> (uniform_deviate (rand ()));
  randomPointTriangle (float (A[0]), float (A[1]), float (A[2]),
                       float (B[0]), float (B[1]), float (B[2]),
                       float (C[0]), float (C[1]), float (C[2]), r1, r2, p);

  if (calcColor)
  {
    
    
    vtkUnsignedCharArray *const colors = vtkUnsignedCharArray::SafeDownCast (polydata->GetPointData ()->GetScalars ());
    if (colors && colors->GetNumberOfComponents () == 3)
    {
    
    
      double cA[3], cB[3], cC[3];
      colors->GetTuple (ptIds[0], cA);
      colors->GetTuple (ptIds[1], cB);
      colors->GetTuple (ptIds[2], cC);

      randomPointTriangle (float (cA[0]), float (cA[1]), float (cA[2]),
                           float (cB[0]), float (cB[1]), float (cB[2]),
                           float (cC[0]), float (cC[1]), float (cC[2]), r1, r2, c);
    }
    else
    {
    
    
      static bool printed_once = false;
      if (!printed_once)
        PCL_WARN ("Mesh has no vertex colors, or vertex colors are not RGB!\n");
      printed_once = true;
    }
  }
}

void
uniform_sampling (vtkSmartPointer<vtkPolyData> polydata, std::size_t n_samples, bool calc_normal, bool calc_color, pcl::PointCloud<pcl::PointXYZRGBNormal> & cloud_out)
{
    
    
  polydata->BuildCells ();
  vtkSmartPointer<vtkCellArray> cells = polydata->GetPolys ();

  double p1[3], p2[3], p3[3], totalArea = 0;
  std::vector<double> cumulativeAreas (cells->GetNumberOfCells (), 0);
  vtkIdType npts = 0;
  vtkCellPtsPtr ptIds = nullptr;
  std::size_t cellId = 0;
  for (cells->InitTraversal (); cells->GetNextCell (npts, ptIds); cellId++)
  {
    
    
    polydata->GetPoint (ptIds[0], p1);
    polydata->GetPoint (ptIds[1], p2);
    polydata->GetPoint (ptIds[2], p3);
    totalArea += vtkTriangle::TriangleArea (p1, p2, p3);
    cumulativeAreas[cellId] = totalArea;
  }

  cloud_out.resize (n_samples);
  cloud_out.width = static_cast<std::uint32_t> (n_samples);
  cloud_out.height = 1;

  for (std::size_t i = 0; i < n_samples; i++)
  {
    
    
    Eigen::Vector3f p;
    Eigen::Vector3f n (0, 0, 0);
    Eigen::Vector3f c (0, 0, 0);
    randPSurface (polydata, &cumulativeAreas, totalArea, p, calc_normal, n, calc_color, c);
    cloud_out[i].x = p[0];
    cloud_out[i].y = p[1];
    cloud_out[i].z = p[2];
    if (calc_normal)
    {
    
    
      cloud_out[i].normal_x = n[0];
      cloud_out[i].normal_y = n[1];
      cloud_out[i].normal_z = n[2];
    }
    if (calc_color)
    {
    
    
      cloud_out[i].r = static_cast<std::uint8_t>(c[0]);
      cloud_out[i].g = static_cast<std::uint8_t>(c[1]);
      cloud_out[i].b = static_cast<std::uint8_t>(c[2]);
    }
  }
}

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

const int default_number_samples = 100000;
const 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 ("                     -n_samples X      = number of samples (default: ");
  print_value ("%d", default_number_samples);
  print_info (")\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 ("                     -write_normals = flag to write normals to the output pcd\n");
  print_info ("                     -write_colors  = flag to write colors to the output pcd\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 uniform sampling. For more information, use: %s -h\n",
              argv[0]);

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

  // Parse command line arguments
  int SAMPLE_POINTS_ = default_number_samples;
  parse_argument (argc, argv, "-n_samples", SAMPLE_POINTS_);
  float leaf_size = default_leaf_size;
  parse_argument (argc, argv, "-leaf_size", leaf_size);
  bool vis_result = ! find_switch (argc, argv, "-no_vis_result");
  const bool write_normals = find_switch (argc, argv, "-write_normals");
  const bool write_colors = find_switch (argc, argv, "-write_colors");

  // 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 = vtkSmartPointer<vtkPolyData>::New ();
  if (ply_file_indices.size () == 1)
  {
    
    
    pcl::PolygonMesh mesh;
    pcl::io::loadPolygonFilePLY (argv[ply_file_indices[0]], mesh);
    pcl::io::mesh2vtk (mesh, polydata1);
  }
  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 ();
  }

  //make sure that the polygons are triangles!
  vtkSmartPointer<vtkTriangleFilter> triangleFilter = vtkSmartPointer<vtkTriangleFilter>::New ();
  triangleFilter->SetInputData (polydata1);
  triangleFilter->Update ();

  vtkSmartPointer<vtkPolyDataMapper> triangleMapper = vtkSmartPointer<vtkPolyDataMapper>::New ();
  triangleMapper->SetInputConnection (triangleFilter->GetOutputPort ());
  triangleMapper->Update ();
  polydata1 = triangleMapper->GetInput ();

  pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr cloud_1 (new pcl::PointCloud<pcl::PointXYZRGBNormal>);
  uniform_sampling (polydata1, SAMPLE_POINTS_, write_normals, write_colors, *cloud_1);

  // Voxelgrid
  VoxelGrid<PointXYZRGBNormal> grid_;
  grid_.setInputCloud (cloud_1);
  grid_.setLeafSize (leaf_size, leaf_size, leaf_size);

  pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr voxel_cloud (new pcl::PointCloud<pcl::PointXYZRGBNormal>);
  grid_.filter (*voxel_cloud);

  if (vis_result)
  {
    
    
    visualization::PCLVisualizer vis3 ("VOXELIZED SAMPLES CLOUD");
    vis3.addPointCloud<pcl::PointXYZRGBNormal> (voxel_cloud);
    if (write_normals)
      vis3.addPointCloudNormals<pcl::PointXYZRGBNormal> (voxel_cloud, 1, 0.02f, "cloud_normals");
    vis3.spin ();
  }

  if (write_normals && write_colors)
  {
    
    
    savePCDFileASCII (argv[pcd_file_indices[0]], *voxel_cloud);
  }
  else if (write_normals)
  {
    
    
    pcl::PointCloud<pcl::PointNormal>::Ptr cloud_xyzn (new pcl::PointCloud<pcl::PointNormal>);
    // Strip uninitialized colors from cloud:
    pcl::copyPointCloud (*voxel_cloud, *cloud_xyzn);
    savePCDFileASCII (argv[pcd_file_indices[0]], *cloud_xyzn);
  }
  else if (write_colors)
  {
    
    
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_xyzrgb (new pcl::PointCloud<pcl::PointXYZRGB>);
    // Strip uninitialized normals from cloud:
    pcl::copyPointCloud (*voxel_cloud, *cloud_xyzrgb);
    savePCDFileASCII (argv[pcd_file_indices[0]], *cloud_xyzrgb);
  }
  else // !write_normals && !write_colors
  {
    
    
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_xyz (new pcl::PointCloud<pcl::PointXYZ>);
    // Strip uninitialized normals and colors from cloud:
    pcl::copyPointCloud (*voxel_cloud, *cloud_xyz);
    savePCDFileASCII (argv[pcd_file_indices[0]], *cloud_xyz);
  }
}

2、多视角采样
包括原来CAD模型某个视角下的一面,这种在做配准(registration)的时候更方便应用,因为我们使用的深度相机一般就是从一个视角拍摄,这时比较好用的是PCL自带函数:renderViewTesselatedSphere

#include <iostream>
#include <string>

#include <opencv2/opencv.hpp>  
#include <pcl/common/common_headers.h>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/console/parse.h>
//#include <glut.h>
#include <vtkAutoInit.h>

#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/obj_io.h>
#include <pcl/PolygonMesh.h>
#include <pcl/point_cloud.h>
#include <pcl/io/vtk_lib_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/io/ply_io.h>
#include <pcl/console/parse.h>
#include <pcl/common/transforms.h>
#include <vtkTriangle.h>

// PCL 分割
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/features/normal_3d.h>
#include <pcl/kdtree/kdtree.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/segmentation/extract_clusters.h>
#include<pcl/io/io.h>
#include<pcl/io/ply_io.h>
#include<pcl/filters/radius_outlier_removal.h>

using namespace cv;
using namespace std;

const double Pi = 3.141592654;

int main()
{
    
    
	cout << "Test PCL !" << endl;
/*+++++++++++++++++++++++++单视角点云获取+++++++++++++++++++++++++++++++*/
	vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
	vtkSmartPointer<vtkSTLReader> readerQuery = vtkSmartPointer<vtkSTLReader>::New();
	//读取CAD模型
	readerQuery->SetFileName("prismTarget.stl");
	readerQuery->Update();
	polydata = readerQuery->GetOutput();
	polydata->GetNumberOfPoints();

	//单视角点云获取
	float resx = 512;
	float resy = resx;
	std::vector<pcl::PointCloud<pcl::PointXYZ>, Eigen::aligned_allocator<pcl::PointCloud<pcl::PointXYZ> > > views_xyz;
	std::vector<Eigen::Matrix4f, Eigen::aligned_allocator<Eigen::Matrix4f> > poses;
	std::vector<float> entropies;
	pcl::visualization::PCLVisualizer vis;
	vis.addModelFromPolyData(polydata, "mesh", 0);
	vis.setRepresentationToSurfaceForAllActors();
	vis.renderViewTesselatedSphere(resx, resy, views_xyz, poses, entropies, 0, 90, 1, FALSE);
	for (int i = 0; i < views_xyz.size(); i++)
	{
    
    
		pcl::PointCloud<pcl::PointXYZ> views_cloud;
		pcl::transformPointCloud<pcl::PointXYZ>(views_xyz[i], views_cloud, poses[i]);

		std::stringstream ss;
		ss << "cloud_view_" << i << ".ply";
		pcl::io::savePLYFile(ss.str(), views_cloud);
	    //pcl::io::savePCDFileASCII(ss.str(),views_cloud);
	}

	while (!vis.wasStopped())
	{
    
    
	}


	cout << "Test PCL Finish!" << endl;
	cout << "hello" << endl;
	system("pause");
	return 0;
}

函数解释:

void pcl::visualization::PCLVisualizer::renderViewTesselatedSphere  ( int  xres,  
  int  yres,  
  pcl::PointCloud< pcl::PointXYZ >::CloudVectorType &  cloud,  
  std::vector< Eigen::Matrix4f, Eigen::aligned_allocator< Eigen::Matrix4f > > &  poses, 
  std::vector< float > &  enthropies,  
  int  tesselation_level,  
  float  view_angle = 45,  
  float  radius_sphere = 1,  
  bool  use_vertices = true  
 )   

这个函数是从不同视角得到CAD模型的部分视图。这里设定的视角是一个包在CAD模型外面的,由正三角形组成的二十面体,虚拟的相机从二十面体的每个顶点(或者每个面)拍摄CAD模型,然后得到对应视角下的点云。这个函数如果不改内部的代码,是不能指定视角的,每次运行,虚拟的相机会在每个顶点(或者面)都拍一遍,得到12个(或者20个,对应面的数量)视角下的点云,之后可以挑选自己需要的点云来进行之后的操作。

参数如下:

[in] xres       窗口x方向大小(即分辨率),分辨率越大,采样点云包含点的数目越多
[in] yres        窗口y方向大小(即分辨率),分辨率越大,采样点云包含点的数目越多  
[in] cloud       有XYZ信息的点云向量代表各视角下的模型
[out] poses      从物体坐标系到各视角相机坐标系的位姿转换
[out] enthropies      在0-1之间,各视角看到模型的比率
[in] tesselation_level     对于原始二十面体三角形面的分割数,如果设为0,则是原始二十面体,设为1,每个三角形面会被分为4个三角形
[in] view_angle       相机的视场角FOV,默认为45  
[in] radius_sphere   半径,默认为1
[in] use_vertices    设为TRUE,则使用顶点,得到12个视角(tesselation_level =0)或42个视角(tesselation_level =1),设为FALSE,则使用面,得到得到20个视角(tesselation_level =0)或80个视角(tesselation_level =1

[in] xres 窗口x方向大小(即分辨率),分辨率越大,采样点云包含点的数目越多
[in] yres 窗口y方向大小(即分辨率),分辨率越大,采样点云包含点的数目越多
[in] cloud 有XYZ信息的点云向量代表各视角下的模型
[out] poses 从物体坐标系到各视角相机坐标系的位姿转换
[out] enthropies 在0-1之间,各视角看到模型的比率
[in] tesselation_level 对于原始二十面体三角形面的分割数,如果设为0,则是原始二十面体,设为1,每个三角形面会被分为4个三角形
[in] view_angle 相机的视场角FOV,默认为45
[in] radius_sphere 半径,默认为1
[in] use_vertices 设为TRUE,则使用顶点,得到12个视角(tesselation_level =0)或42个视角(tesselation_level =1),设为FALSE,则使用面,得到得到20个视角(tesselation_level =0)或80个视角(tesselation_level =1)

1、错误

error C4996: 'pcl::SAC_SAMPLE_SIZE': This map is deprecated and is kept only to prevent breaking existing user code. Starting from PCL 1.8.0 model sample size is a protected member of the SampleConsensusModel class

处理方法:
打开项目属性页>C/C++>常规>SDL检查(设置为否)。
在这里插入图片描述
2、PCL1.8.1和PCL1.6.1不能共存
在安装PCL1.8.1之前,将PCL1.6.1的环境变量都删掉,不然容易报错。

猜你喜欢

转载自blog.csdn.net/qq_27353621/article/details/127217296