vtkPolyData与CGAL中Surface_mesh/Polyhedron的转换

1. vtkPolyData转CGAL::Surface_mesh

typedef CGAL::Surface_mesh<Kernel::Point_3> Mesh;

void ConvertToMeshCGAL(vtkSmartPointer<vtkPolyData> polydata, Mesh& mesh) {
  auto points = polydata->GetPoints();
  auto polys = polydata->GetPolys();

  for (int i = 0; i < points->GetNumberOfPoints(); i++)
  {
    auto p = points->GetPoint(i);
    mesh.add_vertex(Kernel::Point_3(p[0], p[1], p[2]));
  }

  for (int i = 0; i < polys->GetNumberOfCells(); i++)
  {
    const vtkIdType *indices(nullptr);
    vtkIdType numberOfPoints;
    polys->GetCellAtId(i, numberOfPoints, indices);
    Mesh::Vertex_index v0(indices[0]);
    Mesh::Vertex_index v1(indices[1]);
    Mesh::Vertex_index v2(indices[2]);
    mesh.add_face(v0, v1, v2);
  }
}

2. vtkPolyData转CGAL::Polyhedron_3

#include <CGAL/Polyhedron_incremental_builder_3.h>
#include <CGAL/Polyhedron_items_with_id_3.h>

typedef CGAL::Polyhedron_3<Kernel, CGAL::Polyhedron_items_with_id_3> Polyhedron;
typedef Polyhedron::HalfedgeDS HalfedgeDS;

class Polyhedron_builder : public CGAL::Modifier_base<HalfedgeDS>
{
public:
    vtkSmartPointer<vtkPoints> &vertices;
    vtkSmartPointer<vtkCellArray> &faces;
    int num_v = 0;
    int num_f = 0;

    Polyhedron_builder(vtkSmartPointer<vtkPoints> &_vertices, vtkSmartPointer<vtkCellArray> &_faces)
    : vertices(_vertices), faces(_faces)
    {
        num_v = _vertices->GetNumberOfPoints();
        num_f = _faces->GetNumberOfCells();
    }

    void operator()(HalfedgeDS &hds)
    {
        CGAL::Polyhedron_incremental_builder_3<HalfedgeDS> builder(hds, true);
        builder.begin_surface(num_v, num_f);

        for (int i = 0; i < num_v; i++)
        {
            auto point = vertices->GetPoint(i);
            builder.add_vertex(Kernel::Point_3(point[0], point[1], point[2]));
        }
        
        faces->InitTraversal();
        const vtkIdType *cell(nullptr);
        vtkIdType cellSize(0);
        while (faces->GetNextCell(cellSize, cell))
        {
            builder.begin_facet();
            for (vtkIdType i = 0; i < cellSize; i++)
            {
                builder.add_vertex_to_facet(cell[i]);
            }
            builder.end_facet();
        }
        builder.end_surface();
     }
};


// 使用方法
vtkSmartPointer<vtkPolyData> polydata;
auto points = polydata->GetPoints();
auto cells = polydata->GetPolys();
Polyhedron mesh;
Polyhedron_builder builder(points, cells);
mesh.delegate(builder);
  

3. CGAL::Surface_mesh转vtkPolyData

typedef CGAL::Surface_mesh<Kernel::Point_3> Mesh;

vtkSmartPointer<vtkPolyData> ConvertToVtkPolyData(Mesh &mesh)
{
    vtkNew<vtkPoints> vertices;
    vtkNew<vtkCellArray> polygons;
   
    for (Mesh::Vertex_index v : mesh.vertices())
    {
        Kernel::Point_3 p = mesh.point(v);
        vertices->InsertNextPoint(p[0], p[1], p[2]);
    }
    for (Mesh::Face_index f : mesh.faces())
    {
        vtkIdType ids[3] = {f.idx()};  
        CGAL::Vertex_around_face_circulator<Mesh> vcirc(mesh.halfedge(f), mesh), done(vcirc);
        int id = 0;
        do
        {
            ids[id++] = *vcirc;
        } while (++vcirc != done);
        polygons->InsertNextCell(3, ids);
    }
    vtkNew<vtkPolyData> polyData;
    polyData->SetPoints(vertices);
    polyData->SetPolys(polygons);
    return polyData;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值