PCL mesh和CGAL mesh的相互转换

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

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

// 将PCL mesh转换成CGAL mesh
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 mesh转换成PCL mesh
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;
}
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值