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;
}