Esempio n. 1
0
/*
 * build the 2D Delaunay triangulation given a set of points of at least 3 points
 *
 * @points: point set given as a sequence of tuple x0, y0, x1, y1, ....
 * @num_points: number of given point
 * @faces: the triangles given as a sequence: num verts, verts indices, num verts, verts indices
 *		first face is the external face
 * @return: the number of created faces
 */
int delaunay2d(real *points, int num_points, int **faces)
{
	delaunay_t	del;
	int			i, j, fbuff_size = 0;

	/* allocate the points */
	del.points	= (point2d_t**)malloc(num_points * sizeof(point2d_t*));
	assert( del.points != NULL );
	memset(del.points, 0, num_points * sizeof(point2d_t*));
	
	/* copy the points */
	for( i = 0; i < num_points; i++ )
	{
		del.points[i]		= point_alloc();
		del.points[i]->idx	= i;
		del.points[i]->x	= points[i * 2];
		del.points[i]->y	= points[i * 2 + 1];
	}

	qsort(del.points, num_points, sizeof(point2d_t*), cmp_points);

	if( num_points >= 3 )
	{
		del_divide_and_conquer( &del, 0, num_points - 1 );

		del_build_faces( &del );

		fbuff_size	= 0;
		for( i = 0; i < del.num_faces; i++ )
			fbuff_size	+= del.faces[i].num_verts + 1;

		(*faces) = (int*)malloc(sizeof(int) * fbuff_size);

		j = 0;
		for( i = 0; i < del.num_faces; i++ )
		{
			halfedge_t	*curr;

			(*faces)[j]	= del.faces[i].num_verts;
			j++;

			curr	= del.faces[i].he;
			do {
				(*faces)[j]	= curr->vertex->idx;
				j++;
				curr	= curr->alpha->amgis;
			} while( curr != del.faces[i].he );
		}

		del_free_halfedges( &del );

		free(del.faces);
		for( i = 0; i < num_points; i++ )
			point_free(del.points[i]);

		free(del.points);
	}

	return del.num_faces;
}
Esempio n. 2
0
int main() {
	struct table *shapes;
	int loops, planecount, pointcount, x, y, z;

	/* Get number of bulks to process. */
	scanf("%d", &loops);
	while(loops--) {

		/* allocate the shapes table and create the
		 * initial shape. */
		shapes = table_new(shape_comparator);
		struct shape *newshape = shape_alloc();
		table_add(shapes, newshape);

		/* Get the plane count. */
		scanf("%d", &planecount);
		while(planecount--) {

			/* Create a new plane and add it to the plane table inside
			 * the shape. */
			struct plane *newplane = plane_alloc();
			table_add(newshape->planes, newplane);

			/* Get the point count. */
			scanf("%d", &pointcount);
			while(pointcount--) {

				/* Add point to the shape. */
				scanf("%d %d %d", &x, &y, &z);
				struct point *newpoint = point_alloc(x, y, z);
				if (!table_add(newshape->points, newpoint)) {
					int loc;
					table_find(newshape->points, newpoint, &loc);
					free(newpoint);
					newpoint = table_get(newshape->points, loc);
				}

				/* Add mapping between plane and point. */
				table_add(newshape->ppmaps, ppmap_alloc(newplane, newpoint));
			}

			/* Add the plane to the planes table inside shape. */
			table_add(newshape->planes, newplane);
		}
		resolve_planes(shapes);
		resolve_shapes(shapes);
		coallece_planes(shapes);
	}
}