#include <CGAL/Exact_integer.h>
//#include <CGAL/Homogeneous.h>
#include <CGAL/Nef_polyhedron_S2.h>
//#include <CGAL/Exact_rational.h>
#include <CGAL/Cartesian.h>
#include <CGAL/Nef_polyhedron_S2.h>
#include <math.h>
//typedef CGAL::Exact_rational FT;
//typedef CGAL::Cartesian<FT> Kernel;
typedef CGAL::Cartesian<double> Kernel;
//typedef CGAL::Exact_integer RT;
//typedef CGAL::Homogeneous<RT> Kernel;
typedef CGAL::Nef_polyhedron_S2<Kernel> Nef_polyhedron;
typedef Nef_polyhedron::Sphere_circle Sphere_circle;
typedef Nef_polyhedron::Sphere_point Sphere_point;
typedef Nef_polyhedron::Sphere_segment Sphere_segment;
typedef Nef_polyhedron::SVertex_const_handle SVertex_const_handle;
typedef Nef_polyhedron::SVertex_const_iterator SVertex_const_iterator;
typedef Nef_polyhedron::SHalfedge_const_iterator SHalfedge_const_iterator;
int main()
{
Sphere_point p1(1,0,0), p2(0,1,0), p3(0,0,1), p4(0.5,0.5,sqrt(0.5));
Sphere_segment s1(p1,p2), s2(p2,p3), s3(p3,p1), s4(p2,p4), s5(p4,p1);
Sphere_segment triangle[3] = { s1, s2, s3 };
Sphere_segment triangle2[3] = { s1, s4, s5 };
Sphere_segment triangle3[3] = { s1.opposite(), s5.opposite(), s4.opposite() };
const Nef_polyhedron N4(triangle, triangle+3, Nef_polyhedron::EXCLUDED);
const Nef_polyhedron N5(triangle2, triangle2+3, Nef_polyhedron::EXCLUDED);
const Nef_polyhedron N6(triangle3, triangle3+3, Nef_polyhedron::EXCLUDED);
//---- 以下是Nef_polyhedron_S2遍历相关的代码 ----
//std::cout << N6;
const Nef_polyhedron::Sphere_map &m = N6.sphere_map();
Nef_polyhedron::Const_decorator D(&m);
std::cout << "vert count: " << D.number_of_svertices()<< "\n";
std::cout << "edge count: " << D.number_of_sedges() << "\n";
std::cout << "loop count: " << D.number_of_sloops() << "\n";
std::cout << "face count: " << D.number_of_sfaces() << "\n";
std::cout << "\n\n ------------------------\n";
// 遍历Nef_polyhedron_S2的顶点
int i = 0;
for (SVertex_const_iterator it = D.svertices_begin(); it != D.svertices_end(); i++, ++it)
{
std::cout << i << ":"
<< it->point() //Sphere_point SM_items::SVertex::point();
<< "|" << it->point().x()
<< "|" << it->point().y()
<< "|" << it->point().z()
<< std::endl;
}
// 遍历Nef_polyhedron_S2的边
std::cout << "\n\n *************************** \n";
i = 0;
for (SHalfedge_const_iterator it = D.shalfedges_begin(); it != D.shalfedges_end(); i++, ++it)
{
// CGAL-5.5.2\include\CGAL\Nef_S2\SM_items.h
// std::string SM_items::SHalfedge::debug();
// SVertex_handle SM_items::SHalfedge::source();
std::cout << i << ": "
<< " | " << it->source()->debug()
<< " | " << it->target()->debug()
// << " | " << it->debug()
<< std::endl;
}
return 0;
}
/* 以上程序的输出
vert count: 5
edge count: 6
loop count: 0
face count: 3
------------------------
0:0 -1 0|0|-1|0
1:-1 -0 -0|-1|-0|-0
2:0.5 0.5 0.707107|0.5|0.5|0.707107
3:1 0 0|1|0|0
4:0 1 0|0|1|0
***************************
0: | VPointC3(0, -1, 0) | VPointC3(-1, -0, -0)
1: | VPointC3(-1, -0, -0) | VPointC3(0, -1, 0)
2: | VPointC3(0, -1, 0) | VPointC3(1, 0, 0)
3: | VPointC3(1, 0, 0) | VPointC3(0, -1, 0)
4: | VPointC3(-1, -0, -0) | VPointC3(0, 1, 0)
5: | VPointC3(0, 1, 0) | VPointC3(-1, -0, -0)
6: | VPointC3(0.5, 0.5, 0.707107) | VPointC3(0, 1, 0)
7: | VPointC3(0, 1, 0) | VPointC3(0.5, 0.5, 0.707107)
8: | VPointC3(0.5, 0.5, 0.707107) | VPointC3(1, 0, 0)
9: | VPointC3(1, 0, 0) | VPointC3(0.5, 0.5, 0.707107)
10: | VPointC3(1, 0, 0) | VPointC3(0, 1, 0)
11: | VPointC3(0, 1, 0) | VPointC3(1, 0, 0)
*/
附:
哎,我只想对自己说,CGAL话说很强大的,但是这模板套模板的用法,实在是有点反人类,看了这些接口没感觉到优美(或许有人能感觉到它的优美之处),顿时感觉程序员苦逼了~~~ 有空还是看看opencascade怎么样,这个CGAL只能先这样吧,从还没入门到姑且放弃吧。。。