代码语言:javascript复制
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Regular_triangulation_euclidean_traits_3.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <GLUT/GLUT.h>
#include <iostream>
#include <cmath>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Delaunay_triangulation_2<K> Delaunay;
typedef Delaunay::Vertex_handle Vertex_handle;
//初始化定义相关的结构体
typedef K::Point_2 Point;
std::vector<Point> vertices;
//定义顶点的结构
int global_w, global_h;
void points_add_point( int x, int y )
{
vertices.push_back( Point( x, global_h-y ) );
}
void points_draw()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
std::vector <Point>::iterator iter;
glColor3f( 0.0, 1.0, 1.0 );
glPointSize(5);
glBegin(GL_POINTS);
for( iter = vertices.begin(); iter != vertices.end(); iter )
glVertex2i( iter->hx(), iter->hy() );
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void points_triangulation()
{
points_draw(); //最后一个点还要再画一下!
Delaunay dt;
dt.insert(vertices.begin(), vertices.end());
//将vector数组传入狄洛尼三角形数据结构
//cgal提供狄洛尼三角形的核心数据结构就是Delaunay
glPushMatrix();
Delaunay::Finite_faces_iterator fit;
//遍历算法生成的所有三角面(其实是三角形)
glColor3f( 1.0, 1.0, 1.0 );
for( fit = dt.finite_faces_begin(); fit != dt.finite_faces_end(); fit )
{
//开始绘制三角形
glBegin(GL_LINE_LOOP);
glVertex2i( fit->vertex(0)->point().x(), fit->vertex(0)->point().y() );
glVertex2i( fit->vertex(1)->point().x(), fit->vertex(1)->point().y() );
glVertex2i( fit->vertex(2)->point().x(), fit->vertex(2)->point().y() );
glEnd();
}
Delaunay::Edge_iterator eit;
//遍历Delaunay的所有边,绘制Delaunay图的对偶图,即Voronoi图
glEnable( GL_LINE_STIPPLE );//使用点画模式,即使用虚线来绘制Voronoi图
glLineStipple( 1, 0x3333 );
glColor3f( 0.0, 1.0,1.0 );
for( eit = dt.edges_begin(); eit != dt.edges_end(); eit )
{
CGAL::Object o = dt.dual(eit);
//在其对偶图中所对应的边
if (CGAL::object_cast<K::Segment_2>(&o)) //如果这条边是线段,则绘制线段
{
glBegin(GL_LINES);
glVertex2i( CGAL::object_cast<K::Segment_2>(&o)->source().hx(), CGAL::object_cast<K::Segment_2>(&o)->source().hy() );
glVertex2i( CGAL::object_cast<K::Segment_2>(&o)->target().hx(), CGAL::object_cast<K::Segment_2>(&o)->target().hy() );
glEnd();
}
else if (CGAL::object_cast<K::Ray_2>(&o))//如果这条边是射线,则绘制射线
{
glBegin(GL_LINES);
glVertex2i( CGAL::object_cast<K::Ray_2>(&o)->source().hx(), CGAL::object_cast<K::Ray_2>(&o)->source().hy() );
glVertex2i( CGAL::object_cast<K::Ray_2>(&o)->point(1).hx(), CGAL::object_cast<K::Ray_2>(&o)->point(1).hy() );
glEnd();
}
}
glDisable( GL_LINE_STIPPLE );//关闭点画模式
glPopMatrix();
glutSwapBuffers();
}
void display(void)
{}
void init(void)
{
//初始化绘制环境
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void reshape(int w, int h)
{
global_w = w;
global_h = h;
glViewport (0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, 0, h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouse(int button, int state, int x, int y)
{ //定义鼠标事件
if ( button == GLUT_LEFT_BUTTON && state == GLUT_UP )
{
points_add_point(x,y);
points_draw();
}
if ( button == GLUT_RIGHT_BUTTON && state == GLUT_UP )
points_triangulation();
}
void read_file()//从文件中读入点集数据,调试时所使用
{
//文件读取
FILE* f;
f = freopen( "data.txt", "r", stdin );
int a,b;
while(std::cin>>a >> b)
{
vertices.push_back( Point( a, b ) );
}
fclose(f);
}
int main(int argc, char** argv)
{
//初始化配置环境
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (800, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Delaunay Voronoi");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}