如何将CGAL点转换为PCL点云?

时间:2020-04-20 08:59:01

标签: point-cloud-library cgal 3d-reconstruction

我想利用两个库(CGAL和PCL)中的函数和类。因此,它需要将处理后的数据从一个转换为另一个。

那么,如何将CGAL中的点转换为pcl中的点云,反之亦然?

2 个答案:

答案 0 :(得分:0)

给出标题:

#include <pcl/point_types.h>
#include <pcl/conversions.h>

#include <CGAL/Surface_mesh.h>
#include <CGAL/Simple_cartesian.h>

这是将PCL转换为CGAL的功能

int convert_mesh_from_PCL_to_CGAL(pcl::PolygonMesh::Ptr PCL_mesh, CGAL_Mesh& CGAL_mesh)
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr mesh_cloud (new pcl::PointCloud<pcl::PointXYZ>);
    pcl::fromPCLPointCloud2( PCL_mesh->cloud, *mesh_cloud );

    // clear and reserve the
    CGAL_mesh.clear();
    int n = mesh_cloud->size();
    int f = PCL_mesh->polygons.size();
    int e = 0;
    CGAL_mesh.reserve(n, 2*f, e);

    //copy the vertices
    double x, y, z;
    for (int i=0; i<mesh_cloud->size(); i++)
    {
        Point p;
        x = mesh_cloud->points[i].x;
        y = mesh_cloud->points[i].y;
        z = mesh_cloud->points[i].z;
        p = Point(x, y, z);
        CGAL_mesh.add_vertex(p);
    }

    // copy the faces
    std::vector <int> vertices;
    for(int i=0; i<PCL_mesh->polygons.size(); i++)
    {
        vertices.resize(3);
        vertices[0] = PCL_mesh->polygons[i].vertices[0];
        vertices[1] = PCL_mesh->polygons[i].vertices[1];
        vertices[2] = PCL_mesh->polygons[i].vertices[2];
        CGAL_mesh.add_face(CGAL_Mesh::Vertex_index (vertices[0]), 
                           CGAL_Mesh::Vertex_index (vertices[1]),
                           CGAL_Mesh::Vertex_index (vertices[2]));
    }

    return 0;
}

对于从CGAL到PCL的代码,我有一些注释的代码,我将尝试对其进行测试并在以后对其进行更新,但这可能会给您一个有关如何做的想法。

int convert_mesh_from_CGAL_to_PCL(CGAL_Mesh CGAL_mesh, pcl::PolygonMesh::Ptr old_PCL_mesh, pcl::PolygonMesh::Ptr PCL_mesh)
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr mesh_cloud (new pcl::PointCloud<pcl::PointXYZ>);
    pcl::fromPCLPointCloud2( old_PCL_mesh->cloud, *mesh_cloud );

    int i=0;
    BOOST_FOREACH(CGAL_vertex v, vertices(CGAL_mesh))
    {
        mesh_cloud->points[i].x = CGAL_mesh[v].point.x();
        mesh_cloud->points[i].y = CGAL_mesh[v].point.y();
        mesh_cloud->points[i].z = CGAL_mesh[v].point.z();
        i++;
    }

    //BOOST_FOREACH(CGAL_vertex v, vertices(CGAL_mesh))
    //BOOST_FOREACH(CGAL_face f, faces(CGAL_mesh))

    pcl::toPCLPointCloud2( *mesh_cloud, PCL_mesh->cloud );

    return 0;
}

答案 1 :(得分:0)

没有那么复杂。这是一个简单的示例:


首先,您可能需要一些标题:

#include <CGAL/Simple_cartesian.h>

#include <pcl/point_types.h>
#include <pcl/point_cloud.h>

#include <utility>  // std::tuple
#include <vector>

一些using声明使生活变得轻松。
(CGAL示例倾向于使用typedef,例如this one。)

// CGAL using declarations
using Kernel = CGAL::Simple_cartesian<float>;

using Point = Kernel::Point_3; 
using Vector = Kernel::Vector_3;
using Color = std::array<unsigned char, 3>; 

using PNC = std::tuple<Point, Vector, Color>;

// PCL using declarations
using Cloud = pcl::PointCloud<pcl::PointXYZRGBNormal>;

要将 CGAL 点转换为 PCL 点云:

Cloud cgal2pcl(const std::vector<PNC> & points)
{
  Cloud cloud;

  for (const auto & pnc : points) {
    const Point & p = std::get<0>(pnc);
    const Vector & v = std::get<1>(pnc);
    const Color & c = std::get<2>(pnc);

    pcl::PointXYZRGBNormal pt; 
    pt.x = p.x();
    pt.y = p.y();
    pt.z = p.z();
    pt.normal_x = v.x();
    pt.normal_y = v.y();
    pt.normal_z = v.z();
    pt.r = c[0];
    pt.g = c[1];
    pt.b = c[2];
    cloud.points.push_back(pt);
  }

  return cloud;
}

PCL 点云到 CGAL 点:

std::vector<PNC> pcl2cgal(const Cloud & cloud)
{
  std::vector<PNC> points;
  points.reserve(cloud.points.size());

  for (const auto & pt : cloud.points) {
    Point p (pt.x, pt.y, pt.z );
    Vector n (pt.normal_x, pt.normal_y, pt.normal_z);
    Color c { {pt.r, pt.g, pt.b} };

    points.push_back(std::make_tuple(p, n, c));
  }

  return points;
}

对于您的具体示例,您可能需要将std::tuple<Point, Vector, Color>更改为std::pair<Point, Vector>来存储pcl::PointNormal

如果您需要pcl::PointXYZ,那么std::vector<Point>可能就可以满足要求。