#include #include #include #include "delabella.h" struct dba_point { double x, y; }; struct dba_edge { int a, b; }; int errlog(void* stream, const char* fmt, ...) { va_list arg; va_start(arg,fmt); int ret = vfprintf((FILE*)stream, fmt, arg); va_end(arg); //fflush((FILE*)stream); return ret; } int main( void ) { FILE *fp = NULL; fp = fopen( "dba_test.txt", "r" ); if ( !fp ) { printf( "File not found.\n" ); exit( 1 ); } // Read input file. int npt; fscanf( fp, "%d", &npt ); dba_point* cloud = new dba_point[npt]; for ( int j = 0; j < npt; j++ ) { int indx; fscanf( fp, "%d %lf %lf", &indx, &(cloud[ j ].x), &(cloud[ j ].y) ); } int nedg; fscanf( fp, "%d", &nedg ); dba_edge* bounds = new dba_edge[nedg]; for ( int i = 0 ; i < nedg ; i++ ) { int indx; fscanf( fp, "%d %d %d", &indx, &(bounds[i].a), &(bounds[i].b) ); } fclose( fp ); // Echo input file to verify read success. printf( "%d\n", npt ); for ( int j = 0; j < npt; j++ ) { printf( "%d %.18e %.18e\n", j, cloud[ j ].x, cloud[ j ].y ); } printf( "%d\n", nedg ); for ( int i = 0 ; i < ( int )nedg ; i++ ) { printf( "%d %d %d\n", i, bounds[i].a, bounds[i].b ); } printf( "\n" ); // Process points IDelaBella2 < double > * idb = IDelaBella2 < double > ::Create(); idb->SetErrLog( errlog, stdout ); printf( "Triangulate\n" ); int verts = idb->Triangulate( npt, &cloud->x, &cloud->y, sizeof( dba_point ) ); printf( "ConstrainEdges\n" ); idb->ConstrainEdges( nedg, &bounds->a, &bounds->b, sizeof( dba_edge ) ); printf( "Done\n" ); if ( verts > 0 ) { int tris = idb->GetNumPolygons(); const IDelaBella2::Simplex* dela = idb->GetFirstDelaunaySimplex(); for ( int i = 0; i < tris; i++ ) { for ( int j = 0; j < 3; j++ ) { if ( dela->v[ j ]->i >= npt || dela->v[ j ]->i < 0 ) { printf( "Invalid index.\n" ); } } dela = dela->next; } } else { printf( "DLB Error! %d\n", verts ); } delete[] cloud; delete[] bounds; idb->Destroy(); }