template <class Refs>
struct My_face : public CGAL::HalfedgeDS_face_base<Refs> {
unsigned id;
};
template <class Refs, class Point>
struct My_vertex : public CGAL::HalfedgeDS_vertex_base<Refs, CGAL::Tag_true, Point> {
My_vertex() {}
My_vertex(const Point& pt) : CGAL::HalfedgeDS_vertex_base<Refs, CGAL::Tag_true, Point>(pt) {}
unsigned id;
};
// An items type using my face and vertex.
struct My_items : public CGAL::Polyhedron_items_3 {
template <class Refs, class Traits>
struct Face_wrapper {
typedef My_face<Refs> Face;
};
template <class Refs, class Traits>
struct Vertex_wrapper {
typedef typename Traits::Point_3 Point;
typedef My_vertex<Refs, Point> Vertex;
};
};
typedef CGAL::Cartesian<double> Kernel;
typedef CGAL::Polyhedron_3<Kernel, My_items> Polyhedron;
// A modifier converting a Maya mesh to CGAL Polyhedron_3
template <class HDS>
class Mesh_to_polyhedron : public CGAL::Modifier_base<HDS> {
public:
Mesh_to_polyhedron(MFnMesh &mesh) : m_mesh(mesh.object()) {}
void operator()(HDS& hds) {
// get mesh data
MFloatPointArray pts;
m_mesh.getPoints(pts);
MIntArray tcounts, tvers;
m_mesh.getTriangles(tcounts, tvers);
// Postcondition: `hds' is a valid polyhedral surface.
CGAL::Polyhedron_incremental_builder_3<HDS> B(hds, true);
B.begin_surface(pts.length(), tvers.length()/3);
// vertices
typedef typename HDS::Vertex::Point Vertex;
typedef typename Vertex Point;
for ( unsigned i = 0 ; i < pts.length() ; i++ ) {
HDS::Vertex_handle vh = B.add_vertex(Point(pts[i].x,pts[i].y,pts[i].z));
vh->id = i;
}
// triangles
for ( unsigned i = 0 ; i < tvers.length()/3 ; i++ ) {
HDS::Face_handle fh = B.begin_facet();
B.add_vertex_to_facet(tvers[3*i]);
B.add_vertex_to_facet(tvers[3*i+1]);
B.add_vertex_to_facet(tvers[3*i+2]);
B.end_facet();
fh->id = i;
}
B.end_surface();
}
private:
MFnMesh m_mesh;
};
void mesh2polyhedron(MFnMesh &mesh, Polyhedron &P)
{
Mesh_to_polyhedron<Polyhedron::HalfedgeDS> builder(mesh);
P.delegate(builder);
}
摘自: http://blog.csdn.net/csmqq/article/details/5312578
http://www.cgal.org/Manual/3.3/doc_html/cgal_manual/Polyhedron_ref/Class_Polyhedron_incremental_builder_3.html#Cross_link_anchor_697