Exemplo n.º 1
0
static PyObject *Polygon_read(Polygon *self, PyObject *args) {
    PyObject *O;
    int hflag = 1;
    if (! PyArg_ParseTuple(args, "O|i", &O, &hflag))
        return Polygon_Raise(ERR_ARG);
    if (PyFile_Check(O))
        gpc_read_polygon(PyFile_AsFile(O), hflag, self->gpc_p);
    else if (PyString_Check(O)) {
        FILE *f = fopen(PyString_AsString(O), "r");
        if (!f)
            return Polygon_Raise(PyExc_IOError, "Could not open file for reading!");
        gpc_read_polygon(f, hflag, self->gpc_p);
        fclose(f);
    } else
        return Polygon_Raise(ERR_ARG);
    Py_RETURN_NONE;
}
Exemplo n.º 2
0
int main()
{
  gpc_polygon subject, clip, result;
  FILE *sfp, *cfp, *ofp;

  printf("2\n");

  sfp= fopen("TEST_tree.gpf", "r");
  cfp= fopen("TEST_kachel.gpf", "r");

  //sfp= fopen("/home/heiner/flo/gpct101/polygons/britain.gpf", "r");
  //cfp= fopen("/home/heiner/flo/gpct101/polygons/arrows.gpf", "r");

  printf("3\n");

  gpc_read_polygon(sfp, 0, &subject);

  printf("1\n");

  gpc_read_polygon(cfp, 0, &clip);

  printf("4\n");

  gpc_polygon_clip(GPC_INT, &subject, &clip, &result);

  printf("5\n");

  ofp= fopen("ERGEBNIS", "w");
  gpc_write_polygon(ofp, 0, &result);

  printf("6\n");

  gpc_free_polygon(&subject);
  gpc_free_polygon(&clip);
  gpc_free_polygon(&result);

  printf("7\n");

  fclose(sfp);
  fclose(cfp);
  fclose(ofp);
  return 0;
}
Exemplo n.º 3
0
// load a level in memory
bool GameDataOpenLevel(const char *chFileName)
{
	FILE	*pFile;

	// close previous level
	GameDataEndLevel();

	if ((pFile = fopen(chFileName, "r")) != NULL)
	{
		gpc_read_polygon(pFile, 0, &oPolyLevel);
#ifdef EX0_CLIENT
		gpc_polygon_to_tristrip(&oPolyLevel, &oTristripLevel);
#endif // EX0_CLIENT
		fclose(pFile);
	}
	else
	{
		// level not found
		string sMessage = (string)"an eX0 level file \'" + chFileName + "\' could not be opened.";
#ifdef WIN32
		// TODO: Refactor, put error messages in the right place (outside?)
		//MessageBox(NULL, sMessage.c_str(), "eX0 error", MB_ICONERROR);
#else
		//printf("%s\n", sMessage.c_str());
#endif
		printf("%s (errno=%d)\n", sMessage.c_str(), errno);
		return false;
	}

#ifdef EX0_CLIENT
	// DEBUG: Open the same level with PolyBoolean
	LoadParea2(chFileName, &pPolyBooleanLevel);
	if (pPolyBooleanLevel == NULL)
		{ printf("Failed to load the PolyBoolean level.\n"); Terminate(2); }
	if (PAREA::Triangulate(pPolyBooleanLevel) != 0)
		{ printf("PolyBoolean level triangulation failed.\n"); Terminate(3); }

	// DEBUG: Print info about the level, namely the tristrip vertex count
	printf("opened level %s\n", chFileName);
	int nTotalTriangles = 0, nTotalVertices = 0;
	for (int nLoop1 = 0; nLoop1 < oTristripLevel.num_strips; ++nLoop1) {
		nTotalVertices += oTristripLevel.strip[nLoop1].num_vertices;
		nTotalTriangles += oTristripLevel.strip[nLoop1].num_vertices - 2;
	}
	printf("gpc tri: triangle count = %d; vertex count = %d; tristrips = %d\n", nTotalTriangles, nTotalVertices, oTristripLevel.num_strips);
	printf("PB tri: triangle count = %d; vertex count = %d\n", pPolyBooleanLevel->tnum, 3 * pPolyBooleanLevel->tnum);
#endif // EX0_CLIENT

	return true;
}
Exemplo n.º 4
0
int main (int argc, char **argv)
{

    struct st_01 {
        char file[256];
    };

    struct st_02 {
        FILE   *file;
    };

    struct header_01 {
        int version;
        int pasx;
        int pasy;
        int xmin;
        int ymin;
        int xmax;
        int ymax;
        int p1;
        int p2;
        int p3;
        int p4;
        int p5;
    };

    struct st_02 file_end[4];

    char bd_path[256];
    char gpc_path[256];
    char gpc_file[256];

    struct st_01 bd_end[4];

    int pas[] = {45, 15, 5, 1};
    int level = 3;
    int i;
    int pos_data;
    int tab_data;

    int x, y;
    int c;

    struct header_01 header_end[4];

    FILE *polygon_file;
    gpc_polygon polygon;

    int n_contour;
    int n_point;


    if (argc < 2 || argc > 3) {
        fprintf (stderr, "Sorry !!\n");
        fprintf (stderr, "Usage:  read_bd [f|h|i|l|c]\n\n");

        exit (EXIT_FAILURE);
    }

    pos_data = 0;
    sprintf(bd_path, "./bd/bd_%s", argv[1]);

    for (i=0; i<=level; i++)
    {
        printf("i: %d\n", i);
        sprintf(bd_end[i].file, "%s/poly-%s-%d.dat", bd_path, argv[1], pas[i]);
        if ((file_end[i].file = fopen (bd_end[i].file, "wb")) == NULL )
        {
            fprintf (stderr, "Impossible d'ouvrir le fichier %s\n", bd_end[i].file);
            exit (EXIT_FAILURE);
        }
        header_end[i].version=  234;
        header_end[i].pasx=     pas[i];
        header_end[i].pasy=     pas[i];
        header_end[i].xmin=     0;
        header_end[i].ymin=     -90;
        header_end[i].xmax=     360;
        header_end[i].ymax=     90;
        header_end[i].p1=       1;
        header_end[i].p2=       2;
        header_end[i].p3=       3;
        header_end[i].p4=       4;
        header_end[i].p5=       5;

        fwrite(&header_end[i], sizeof(header_end[i]), 1, file_end[i].file);

        for (x=0; x<360; x=x+pas[i])
        {
            for (y=-90; y<90; y=y+pas[i])
            {
                fwrite(&pos_data, sizeof(int), 1, file_end[i].file);
            }
        }
        for (x=0; x<360; x=x+pas[i])
        {
		printf("X=%d\n",x);
            for (y=-90; y<90; y=y+pas[i])
            {
                if (get_gpc_path(gpc_path, bd_path, x, y, pas, i) != 0)
                {
                    fseek(file_end[i].file, 0L, SEEK_END);
                    pos_data = ftell(file_end[i].file);
                    //printf("pos_data: %d\n", pos_data);

                    for (c=1; c<=5; c=c+1)
                    {
                        sprintf(gpc_file, "%s/%s%d.dat", gpc_path, argv[1], c);
                        //printf("X: %i, Y: %i, File: %s\n", x, y, gpc_file);

                        if ((polygon_file = fopen(gpc_file, "r")) == NULL )
                        {
                            fprintf (stderr, "Impossible d'ouvrir le fichier %s\n", gpc_file);
                            exit (EXIT_FAILURE);
                        }

                        gpc_read_polygon(polygon_file, 1, &polygon);      /* Lecture du fichier polygone*/

                        //printf("num_contour: %d\n", polygon.num_contours);

                        fwrite(&polygon.num_contours, sizeof(int), 1, file_end[i].file);
                        for (n_contour=0; n_contour<polygon.num_contours; n_contour++)
                        {
                            fwrite(&polygon.hole[n_contour], sizeof(int), 1, file_end[i].file);

                            fwrite(&polygon.contour[n_contour].num_vertices, sizeof(int), 1, file_end[i].file);
                            for (n_point=0; n_point<polygon.contour[n_contour].num_vertices; n_point++)
                            {
                                fwrite(&polygon.contour[n_contour].vertex[n_point].x, sizeof(double), 1, file_end[i].file);
                                fwrite(&polygon.contour[n_contour].vertex[n_point].y, sizeof(double), 1, file_end[i].file);
                            }
                        }


                        fclose(polygon_file);
                        gpc_free_polygon(&polygon);
                    }
                    tab_data = (x/pas[i])*(180/pas[i]) + (y+90)/pas[i];
                    //printf("tab_data: %d\n\n", tab_data);
                    fseek(file_end[i].file, sizeof(header_end[i]) + tab_data*sizeof(int), SEEK_SET); //erreur + tab_data*sizeof(long)
                    fwrite(&pos_data, sizeof(int), 1, file_end[i].file);

                }
            }
        }

    fclose(file_end[i].file);

    }

    return 0;

}
Exemplo n.º 5
0
int main ( void )
/************************************************************************
 * TESTGPC								*
 *									*
 * This program tests the GPC contributed library public functions.	*
 *									*
 **									*
 * Log:									*
 * D.W.Plummer/NCEP	 2/04						*
 * D.W.Plummer/NCEP	10/06	Added GPC_SET_EPSILON			*
 ***********************************************************************/
{
int		ii, cont, numsub, which, ier;
int		operation, readholeflag, writeholeflag, hole;
int		nverts=0;
double		e;
char		select[8];
float		xv[LLMXPT], yv[LLMXPT];
char		filnam[256], buffer[80];
int		pagflg;
char    	errgrp[8];
FILE		*fpread, *fpwrite;
gpc_polygon	subject_polygon, clip_polygon, result_polygon;
gpc_vertex_list	contour;
/*---------------------------------------------------------------------*/
    cont = G_FALSE;

    /*
     * Structure initializations.
     */
    subject_polygon.num_contours = 0;
    subject_polygon.hole         = (int*)NULL;
    subject_polygon.contour      = (gpc_vertex_list*)NULL;
    clip_polygon.num_contours    = 0;
    result_polygon.num_contours  = 0;
    contour.vertex 		 = (gpc_vertex*)NULL;
    contour.num_vertices 	 = 0;

    while ( cont == G_FALSE ) {
        printf ( "\n\n" );
        printf ( "*** ORIGINAL GPC PUBLIC FUNCTIONS ***\n");
        printf ( "   1 = GPC_READ_POLYGON    2 = GPC_WRITE_POLYGON \n");
        printf ( "   3 = GPC_ADD_CONTOUR     4 = GPC_POLYGON_CLIP \n");
        printf ( "   5 = GPC_FREE_POLYGON     \n");
        printf ( "*** GPC PUBLIC FUNCTION ADD-ONS ***\n");
        printf ( "  11 = GPC_CREATE_VERTEX_LIST   \n");
	printf ( "  12 = GPC_GET_VERTEX_LIST      \n");
	printf ( "  13 = GPC_GET_VERTEX_AREA      \n");
	printf ( "  14 = GPC_SET_EPSILON      \n");
        printf ( "*** HELP ***\n");
	printf ( "  99 = HELP on INPUT FILE FORMATS \n");
        printf ( "\n" );
        printf ( "Select a subroutine number or type EXIT: " );
        scanf ( " %s", select );
        switch ( select[0] ) {
    	    case 'e':
    	    case 'E':
    		cont = G_TRUE;
    	    default:
    		numsub = atoi ( select );
    		break;
        }

/*---------------------------------------------------------------------*/
        if ( numsub == 1 ) {

	    printf("Make sure your polygon file is in the proper format:\n");

	    printf("<num-contours>\n");
	    printf("<num-vertices-in-first-contour>\n");
	    printf("[<first-contour-hole-flag>]\n");
	    printf("<vertex-list>\n");
	    printf("<num-vertices-in-second-contour>\n");
	    printf("[<second-contour-hole-flag>]\n");
	    printf("<vertex-list> \n");
	    printf("etc...\n");

	    printf ( "Enter filename to read polygon from : \n");
	    scanf ( " %s", filnam );

	    printf ( "Enter whether file format contains hole flags (%d-FALSE,%d-TRUE) :\n",
		  G_FALSE, G_TRUE );
	    scanf ( " %d", &readholeflag );

	    printf ( "Enter which polygon (%d-SUBJECT,%d-CLIP) :\n",
		  SUBJECT, CLIP );
	    scanf ( " %d", &which );

	    fpread = (FILE *)cfl_ropn ( filnam, "", &ier );
	    if ( ier == G_NORMAL )  {
	      if ( which == SUBJECT )
	        gpc_read_polygon ( fpread, readholeflag, &subject_polygon );
	      else if ( which == CLIP )
	        gpc_read_polygon ( fpread, readholeflag, &clip_polygon );
	      else
		printf("Invalid polygon type\n");
	      cfl_clos ( fpread, &ier );
	    }
	    else  {
	      printf("Unable to open file %s\n", filnam );
	    }

        }
/*---------------------------------------------------------------------*/
        if ( numsub == 2 ) {

	    printf ( "Enter filename to write polygon to : \n");
	    scanf ( " %s", filnam );

	    printf ( "Enter the write hole flag (%d-FALSE,%d-TRUE):\n",
		  G_FALSE, G_TRUE );
	    scanf ( " %d", &writeholeflag );

	    printf ( "Enter which polygon (%d-SUBJECT,%d-CLIP,%d-RESULT):\n",
		  SUBJECT, CLIP, RESULT );
	    scanf ( " %d", &which );

	    fpwrite = (FILE *)cfl_wopn ( filnam, &ier );
	    if ( ier == G_NORMAL )  {
	      if ( which == SUBJECT )
	        gpc_write_polygon ( fpwrite, writeholeflag, &subject_polygon );
	      else if ( which == CLIP )
	        gpc_write_polygon ( fpwrite, writeholeflag, &clip_polygon );
	      else if ( which == RESULT )
	        gpc_write_polygon ( fpwrite, writeholeflag, &result_polygon );
	      else
		printf("Invalid polygon type\n");
	      cfl_clos ( fpwrite, &ier );
	    }
	    else  {
	      printf("Unable to open file %s\n", filnam );
	    }


        }
/*---------------------------------------------------------------------*/
	if ( numsub == 3 ) {

	    if ( nverts == 0 )  {
		printf("Must first create a vertex list (option 11)\n");
	    }
	    else  {

	        printf ( "Enter which polygon (%d-SUBJECT,%d-CLIP,%d-RESULT) to add vertex list to:\n",
		      SUBJECT, CLIP, RESULT );
	        scanf ( " %d", &which );

	        printf ( "Enter the hole flag (%d-HOLE,%d-NOT A HOLE):\n",
		      G_TRUE, G_FALSE );
	        scanf ( " %d", &hole );

	        if ( which == SUBJECT )  {
		    gpc_add_contour ( &subject_polygon, &contour, hole );
		}
		else if ( which == CLIP )  {
		    gpc_add_contour ( &clip_polygon, &contour, hole );
		}
		else {
		    printf("Invalid polygon\n");
		}
	    }

	}
/*---------------------------------------------------------------------*/
	if ( numsub == 4 ) {

	    printf ( "Enter operation (%d-GPC_DIFF,%d-GPC_INT,%d-GPC_XOR,%d-GPC_UNION):\n",
		  GPC_DIFF, GPC_INT, GPC_XOR, GPC_UNION );
	    scanf ( " %d", &operation );

	    gpc_polygon_clip ( operation, &subject_polygon,
		    &clip_polygon, &result_polygon );

	}
/*---------------------------------------------------------------------*/
        if ( numsub == 5 ) {

	    printf ( "Enter which polygon (%d-SUBJECT,%d-CLIP,%d-RESULT,%d-ALL) to free contours:\n ",
		  SUBJECT, CLIP, RESULT, ALL );
	    scanf ( " %d", &which );

	    if ( which == SUBJECT || which == ALL )  {
    	        if ( subject_polygon.num_contours != 0 )
		    gpc_free_polygon ( &subject_polygon );
	    }
	    else if ( which == CLIP || which == ALL )  {
    	        if ( clip_polygon.num_contours != 0 )
        	    gpc_free_polygon ( &clip_polygon );
	    }
	    else if ( which == RESULT || which == ALL )  {
    	        if ( result_polygon.num_contours != 0 )
        	    gpc_free_polygon ( &result_polygon );
            }


        }
/*---------------------------------------------------------------------*/
        if ( numsub == 11 ) {

	    printf ( "Enter either the number of points in polygon (to be followed by entering the points), or a filename to read points from: \n");
	    scanf ( " %s", filnam );

	    cst_numb ( filnam, &nverts, &ier );

	    if ( ier == 0 )  {
	        for ( ii = 0; ii < nverts; ii++ )
		    scanf ( "%f %f", &(xv[ii]), &(yv[ii]) );
    	        if ( contour.vertex != (gpc_vertex*)NULL )
		    free ( contour.vertex );
	        gpc_cvlist ( nverts, xv, yv, &contour, &ier );
	    }
	    else  {
	        printf ( "Note that the file format is simply a list of coordinate pairs separated by whitespace.\nThe number of points will be counted automatically. For instance, a file containing:\n0 0\n0 1\n1 1\nyields a vertex list of three points.\n\n");
		nverts = 0;
	        fpread = (FILE *)cfl_tbop ( filnam, "", &ier );
	        if ( ier == G_NORMAL )  {
		    cfl_trln ( fpread, sizeof(buffer), buffer, &ier );
		    while ( ier == 0 )  {
			sscanf ( buffer, "%f %f", 
			    &(xv[nverts]), &(yv[nverts]) );
			    nverts += 1;
			cfl_trln ( fpread, sizeof(buffer), buffer, &ier );
		    }
		    printf("EOF reached in file %s, number of vertices = %d\n", 
			    filnam, nverts );
		    cfl_clos( fpread, &ier );
    	            if ( contour.vertex != (gpc_vertex*)NULL )
		    	free ( contour.vertex );
	            gpc_cvlist ( nverts, xv, yv, &contour, &ier );
		}
	    }

        }
/*---------------------------------------------------------------------*/
        if ( numsub == 12 ) {

	    gpc_gvlist ( &contour, &nverts, xv, yv, &ier );

	    printf("gpc_gvlist, ier = %d\n", ier );

	    printf("Number of vertices = %d\n", nverts );
	    for ( ii = 0; ii < nverts; ii++ )
		printf ( "%d - %f %f\n", ii, xv[ii], yv[ii] );

        }
/*---------------------------------------------------------------------*/
        if ( numsub == 13 ) {

	    printf ( "Area of contour is %f\n", gpc_gvarea(&contour) );

        }
/*---------------------------------------------------------------------*/
        if ( numsub == 14 ) {

	    scanf ( " %lf", &e );

	    gpc_set_epsilon ( e );

        }
/*---------------------------------------------------------------------*/
        if ( numsub == 99 ) {

	    pagflg = G_FALSE;
	    strcpy ( errgrp, "TESTGPC" );
	    ip_help ( errgrp, &pagflg, &ier, strlen(errgrp) );

        }
/*---------------------------------------------------------------------*/
    }

    if ( subject_polygon.num_contours != 0 )
	gpc_free_polygon ( &subject_polygon );
    if ( clip_polygon.num_contours != 0 )
        gpc_free_polygon ( &clip_polygon );
    if ( result_polygon.num_contours != 0 )
        gpc_free_polygon ( &result_polygon );
    if ( contour.vertex != (gpc_vertex*)NULL )
	free ( contour.vertex );

    return(0);

}