如何从 CGAL 中的坐标和拓扑列表创建 Polyhedron_3 数据结构
How to create a Polyhedron_3 data structure from list of coordinates and topology in CGAL
我有一个坐标列表(一个 n×3 矩阵,其中 n 是点数)和一个拓扑连通性(一个 m×3 矩阵,其中 m 是三角形数) .
我希望使用此信息在 CGAL 中创建 CGAL::Polyhedron_3
class。我该如何实现?
这是我当前的代码:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iterator>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Random.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Polyhedron_items_with_id_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/Surface_mesh_shortest_path.h>
#include <CGAL/boost/graph/graph_traits_Polyhedron_3.h>
#include <CGAL/boost/graph/iterator.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef CGAL::Polyhedron_3<Kernel, CGAL::Polyhedron_items_with_id_3> Polyhedron_3;
typedef Kernel::Point_3 Point_3;
typedef Polyhedron_3::HalfedgeDS HalfedgeDS;
typedef Polyhedron_3::Vertex_iterator Vertex_iterator;
typedef Polyhedron_3::Edge_iterator Edge_iterator;
typedef Polyhedron_3::Facet_iterator Facet_iterator;
typedef CGAL::Surface_mesh_shortest_path_traits<Kernel, Polyhedron_3> Traits;
typedef CGAL::Surface_mesh_shortest_path<Traits> Surface_mesh_shortest_path;
typedef boost::graph_traits<Polyhedron_3> Graph_traits;
typedef Graph_traits::vertex_iterator vertex_iterator;
typedef Graph_traits::face_iterator face_iterator;
typedef Polyhedron_3::Halfedge_around_facet_circulator Halfedge_facet_circulator;
template <class HDS>
class Build_triangle : public CGAL::Modifier_base<HDS>
{
public:
Build_triangle() {}
void operator()(HDS & hds)
{
// Postcondition: hds is a valid polyhedral surface.
CGAL::Polyhedron_incremental_builder_3<HDS> B(hds, true);
B.begin_surface(5, 4, 16);
typedef typename HDS::Vertex Vertex;
typedef typename Vertex::Point Point;
B.add_vertex(Point(0.0, 0.0, 0.0));
B.add_vertex(Point(1.0, 0.0, 0.0));
B.add_vertex(Point(1.0, 1.0, 0.0));
B.add_vertex(Point(0.0, 1.0, 0.0));
B.add_vertex(Point(0.5, 0.5, 0.0));
B.begin_facet();
B.add_vertex_to_facet(0);
B.add_vertex_to_facet(1);
B.add_vertex_to_facet(4);
B.end_facet();
B.begin_facet();
B.add_vertex_to_facet(1);
B.add_vertex_to_facet(2);
B.add_vertex_to_facet(4);
B.end_facet();
B.begin_facet();
B.add_vertex_to_facet(2);
B.add_vertex_to_facet(3);
B.add_vertex_to_facet(4);
B.end_facet();
B.begin_facet();
B.add_vertex_to_facet(3);
B.add_vertex_to_facet(0);
B.add_vertex_to_facet(4);
B.end_facet();
B.end_surface();
}
};
int main(int argc, char *argv[])
{
Polyhedron_3 P;
Build_triangle<HalfedgeDS> triangle;
P.delegate(triangle);
CGAL_assertion(P.is_triangle(P.halfedges_begin()));
return 0;
}
CGAL_assertion
断言语句失败,这意味着我未能正确生成三角形。我需要更改 HalfedgeDS 数据结构吗?我一直没能找到一个很好的例子来说明如何正确地做到这一点。
根据 documentation the function is_triangle(h)
returns true if the connected components of h
is a triangle (not if the face is a triangle). You can use the function is_pure_triangle()
.
我有一个坐标列表(一个 n×3 矩阵,其中 n 是点数)和一个拓扑连通性(一个 m×3 矩阵,其中 m 是三角形数) .
我希望使用此信息在 CGAL 中创建 CGAL::Polyhedron_3
class。我该如何实现?
这是我当前的代码:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iterator>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Random.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Polyhedron_items_with_id_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/Surface_mesh_shortest_path.h>
#include <CGAL/boost/graph/graph_traits_Polyhedron_3.h>
#include <CGAL/boost/graph/iterator.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef CGAL::Polyhedron_3<Kernel, CGAL::Polyhedron_items_with_id_3> Polyhedron_3;
typedef Kernel::Point_3 Point_3;
typedef Polyhedron_3::HalfedgeDS HalfedgeDS;
typedef Polyhedron_3::Vertex_iterator Vertex_iterator;
typedef Polyhedron_3::Edge_iterator Edge_iterator;
typedef Polyhedron_3::Facet_iterator Facet_iterator;
typedef CGAL::Surface_mesh_shortest_path_traits<Kernel, Polyhedron_3> Traits;
typedef CGAL::Surface_mesh_shortest_path<Traits> Surface_mesh_shortest_path;
typedef boost::graph_traits<Polyhedron_3> Graph_traits;
typedef Graph_traits::vertex_iterator vertex_iterator;
typedef Graph_traits::face_iterator face_iterator;
typedef Polyhedron_3::Halfedge_around_facet_circulator Halfedge_facet_circulator;
template <class HDS>
class Build_triangle : public CGAL::Modifier_base<HDS>
{
public:
Build_triangle() {}
void operator()(HDS & hds)
{
// Postcondition: hds is a valid polyhedral surface.
CGAL::Polyhedron_incremental_builder_3<HDS> B(hds, true);
B.begin_surface(5, 4, 16);
typedef typename HDS::Vertex Vertex;
typedef typename Vertex::Point Point;
B.add_vertex(Point(0.0, 0.0, 0.0));
B.add_vertex(Point(1.0, 0.0, 0.0));
B.add_vertex(Point(1.0, 1.0, 0.0));
B.add_vertex(Point(0.0, 1.0, 0.0));
B.add_vertex(Point(0.5, 0.5, 0.0));
B.begin_facet();
B.add_vertex_to_facet(0);
B.add_vertex_to_facet(1);
B.add_vertex_to_facet(4);
B.end_facet();
B.begin_facet();
B.add_vertex_to_facet(1);
B.add_vertex_to_facet(2);
B.add_vertex_to_facet(4);
B.end_facet();
B.begin_facet();
B.add_vertex_to_facet(2);
B.add_vertex_to_facet(3);
B.add_vertex_to_facet(4);
B.end_facet();
B.begin_facet();
B.add_vertex_to_facet(3);
B.add_vertex_to_facet(0);
B.add_vertex_to_facet(4);
B.end_facet();
B.end_surface();
}
};
int main(int argc, char *argv[])
{
Polyhedron_3 P;
Build_triangle<HalfedgeDS> triangle;
P.delegate(triangle);
CGAL_assertion(P.is_triangle(P.halfedges_begin()));
return 0;
}
CGAL_assertion
断言语句失败,这意味着我未能正确生成三角形。我需要更改 HalfedgeDS 数据结构吗?我一直没能找到一个很好的例子来说明如何正确地做到这一点。
根据 documentation the function is_triangle(h)
returns true if the connected components of h
is a triangle (not if the face is a triangle). You can use the function is_pure_triangle()
.