算法的相关解析见点击打开链接,讲得不错
源码
#ifndef _TRIANGULATION_H
#define _TRIANGULATION_H
// TYPES //////////////////////////////////////////////////
typedef struct VERTEX2D_TYP
{
float x;
float y;
} VERTEX2D, *VERTEX2D_PTR;
typedef struct EDGE_TYP
{
VERTEX2D v1;
VERTEX2D v2;
} EDGE, *EDGE_PTR;
typedef struct TRIANGLE_TYP
{
int i1; // vertex index
int i2;
int i3;
TRIANGLE_TYP* pNext;
TRIANGLE_TYP* pPrev;
} TRIANGLE, *TRIANGLE_PTR;
typedef struct MESH_TYP
{
int vertex_num;
int triangle_num;
VERTEX2D_PTR pVerArr; // point to outer vertices arrary
TRIANGLE_PTR pTriArr; // point to outer triangles arrary
} MESH, *MESH_PTR;
// PROTOTYPES ///////////////////////////////////////////
// Delaunay triangulation functions
void InitMesh(MESH_PTR pMesh, int ver_num);
void UnInitMesh(MESH_PTR pMesh);
void AddBoundingBox(MESH_PTR pMesh);
void RemoveBoundingBox(MESH_PTR pMesh);;
void IncrementalDelaunay(MESH_PTR pMesh);
void Insert(MESH_PTR pMesh, int ver_index);
bool FlipTest(MESH_PTR pMesh, TRIANGLE_PTR pTestTri);
float InCircle(VERTEX2D_PTR pa, VERTEX2D_PTR pb, VERTEX2D_PTR pp, VERTEX2D_PTR pd);
float InTriangle(MESH_PTR pMesh, VERTEX2D_PTR pVer, TRIANGLE_PTR pTri);
void InsertInTriangle(MESH_PTR pMesh, TRIANGLE_PTR pTargetTri, int ver_index);
void InsertOnEdge(MESH_PTR pMesh, TRIANGLE_PTR pTargetTri, int ver_index);
// Helper functions
void RemoveTriangleNode(MESH_PTR pMesh, TRIANGLE_PTR pTri);
TRIANGLE_PTR AddTriangleNode(MESH_PTR pMesh, TRIANGLE_PTR pPrevTri, int i1, int i2, int i3);
// I/O functions
void Input(char* pFile, MESH_PTR pMesh);
void Output(char* pFile, MESH_PTR pMesh);
#endif
// Tiangulation.cpp : 定义控制台应用程序的入口点。
//
// File: Delaunay.cpp
// Description: Delaunay class to triangluate points set in 2D.
// TODO: The procedure uses Double List for holding data, it can be optimized by using another data structure such as DAG, Quad-edge, etc.
// Author: SoRoMan
// Date: 05/10/2007
// Version: 1.0
// History:
//
#include "stdafx.h"
// GLOBALS ////////////////////////////////////////////////
// FUNCTIONS /////////////////////////////////////////////
// The format of input file should be as follows:
// The First Line: amount of vertices (the amount of vertices/points needed to be triangulated)
// Other Lines: x y z (the vertices/points coordinates, z should be 0 for 2D)
// E.g.
// 4
// -1 -1 0
// -1 1 0
// 1 1 0
// 1 -1 0
void Input(char* pFile, MESH_PTR pMesh)
{
FILE* fp = fopen(pFile, "r");
if (!fp)
{
fprintf(stderr,"Error:%s open failed\n", pFile);
exit(1);
}
//int face;
int amount;
//fscanf( fp, "%d", &face);
fscanf( fp, "%d", &amount);
if (amount < 3)
{
fprintf(stderr,"Error:vertex amount should be greater than 2, but it is %d \n",amount);
exit(1);
}
InitMesh(pMesh, amount);
float x,y,z;
for ( int j=3; j<amount+3; ++j)
{
fscanf( fp, "%f %f %f", &x, &y, &z);
((VERTEX2D_PTR)(pMesh->pVerArr+j))->x = x;
((VERTEX2D_PTR)(pMesh->pVerArr+j))->y = y;
}
fclose(fp);
}
// Algorithm IncrementalDelaunay(V)
// Input: 由n个点组成的二维点集V
// Output: Delaunay三角剖分DT
// 1.add a appropriate triangle boudingbox to contain V ( such as: we can use triangle abc, a=(0, 3M), b=(-3M,-3M), c=(3M, 0), M=Max({|x1|,|x