研究了几天nef_2,终于明白了一点,写下来记录一下
本文主要讨论nef布尔运算后Explorer的情况
#include <CGAL/Gmpq.h>
#include <CGAL/Lazy_exact_nt.h>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Bounded_kernel.h>
#include <CGAL/Nef_polyhedron_2.h>
typedef CGAL::Lazy_exact_nt<CGAL::Gmpq> FT;
typedef CGAL::Simple_cartesian<FT> Kernel;
typedef CGAL::Bounded_kernel<Kernel> Extended_kernel;
typedef CGAL::Nef_polyhedron_2<Extended_kernel> Nef_polyhedron;
typedef Nef_polyhedron::Point Point;
typedef Nef_polyhedron::Explorer Explorer;
typedef Explorer::Face_const_iterator Face_const_iterator;
typedef Explorer::Hole_const_iterator Hole_const_iterator;
typedef Explorer::Halfedge_around_face_const_circulator Halfedge_around_face_const_circulator;
typedef Explorer::Vertex_const_handle Vertex_const_handle;
typedef Explorer::Point_const_iterator Point_const_iterator;
typedef Explorer::Isolated_vertex_const_iterator Isolated_vertex_const_iterator;
头文件和预定义,没什么好说的,下面开始三角形和线段的定义
Point r1[3] = { Point(20, 15), Point(25, 5), Point(30, 15) };
Point s0[2] = { Point(20, 10), Point(25, 16) };
std::list<std::pair<Point*, Point*> > polylines, polylines1;
polylines.push_back(std::make_pair(r1 + 0, r1 + 3));
polylines1.push_back(std::make_pair(s0 + 0, s0 + 2));
Nef_polyhedron RST0(polylines.begin(), polylines.end(), Nef_polyhedron::POLYGONS);//定义多边形
Nef_polyhedron RST1(polylines1.begin(), polylines1.end(), Nef_polyhedron::POLYLINES);//多段线
//相交运算
Nef_polyhedron RST = RST1.intersection(RST0);
//使用探测器
Explorer explorer = RST.explorer();
//遍历点
std::cout << "Point" << std::endl;
for (Point_const_iterator pntPos = explorer.points_begin(); pntPos != explorer.points_end(); ++pntPos)
{
std::cout << pntPos->x() << "," << pntPos->y() << std::endl;
}
//半边
std::cout << "halfedges" << std::endl;
for (Explorer::Halfedge_const_iterator posEdge = explorer.halfedges_begin(); posEdge != explorer.halfedges_end(); ++posEdge)
{
Vertex_const_handle vh = explorer.target(posEdge);
std::cout << explorer.point(vh) << " [" << explorer.point(vh).x() << " | " << explorer.point(vh).y() << "], ";
}
//开始面的遍历
//第一个面是无限大的面,没有外轮廓,只包含洞
Face_const_iterator posFace = explorer.faces_begin();
for (Hole_const_iterator hit = explorer.holes_begin(posFace); hit != explorer.holes_end(posFace); hit++)
{
std::cout << " A hole" << std::endl;
//半边循环
Halfedge_around_face_const_circulator hafc(hit), done(hafc);
do
{
Vertex_const_handle vh = explorer.target(hafc);
std::cout << explorer.point(vh) << " [" << explorer.point(vh).x() << " | " << explorer.point(vh).y() << "], ";
hafc++;
} while (hafc != done);
}
//其他面,有外轮廓,包含洞
for (++posFace; posFace != explorer.faces_end(); ++posFace)
{
Halfedge_around_face_const_circulator hafc = explorer.face_cycle(posFace), done(hafc);
do
{
Vertex_const_handle vh = explorer.target(hafc);
std::cout << explorer.point(vh) << " [" << explorer.point(vh).x()<< " | " << explorer.point(vh).y() << "], ";
hafc++;
} while (hafc != done);
}