// This program takes in the location of a data file as input, reads the data, and prints // the area of the polygon represented in the data. // main.cpp : Defines the entry point for the console application. // //#include "stdafx.h" #include "genlib.h" #include "mathlib.h" #include "langlib.h" #include "ledslib.h" #include "stllib.h" #include "parsestl.h" /* General note: So-called "Hungarian" is used for variable names, where each variable name starts with some characters that describe its type. All variables which are pointers start with p. All class names start with C. */ int Polygonfileread(FILE* pf); FILE* OpenFilepath(char*); int main(int argc, char* argv[]) { // Here is some extraneous code that shows some mathlib calls. // Note: mathlib points are actually in 4D projective space, with the // 4th "w" coordinate generally = 1.0. To go from P^4 to R^3, you // just divide the first 3 (x,y, and z) coordinates by the 4th (w) // coordinate. Your point w's should always be = 1, so you can pretty much // just ignore it. If it isn't, the Normalize function will divide // through by w and then it will be. // For 3D vectors through the origin and point x,y,z, the w is 0. // See Foley et al. for more info on projective geometry. // (You should be able to use the library functions even if you don't // understand projective geometry.) FILE* pf=0; CHAR pcFileNameBuffer[256]; // CHAR *pcInput; CHAR **ppc; UINT_32 i; // LanguageType eInputLanguage; // FLOAT fEps; BOOL bAnalyze; // CHAR **ppcDirectories; // UINT_32 uiDirectories; // FILE *fpOutputSTLFile = NULL; // ppcDirectories = NULL; // uiDirectories = 0; // eInputLanguage = ltSize; // geOutputLanguage = ltSIF; // fEps = -1.0; //(Negative epsilon replaced by value based on min edge length) strcpy(pcFileNameBuffer, ""); // This should really use getarg()! ppc = argv; i = (UINT_32)argc; i--; ppc++; while (i) { if (ppc[0][0] == '-') { switch (ppc[0][1]) { case 'a': bAnalyze = TRUE; break; default: fprintf(stderr,"Unknown flag: %c\n", ppc[0][1]); // PrintUsage(argv[0]); // fprintf(stderr, gpcUsage, argv[0]); exit(1); } } else { strcpy(pcFileNameBuffer, ppc[0]); } i--; ppc++; } // get file path, open and read data char line[250],*ptr; int index=0; double a,b,x1,x2,y1,y2,area=0,temp_area; a=b=x1=x2=y1=y2=0; Vector v0,v1,v2,vCross,vTrans,vZaxis; vZaxis.Set(0,0,1); FLOAT fDot; if(pf=OpenFilepath(pcFileNameBuffer)){ //The first point if(fgets(line,100,pf)){ ptr=strpbrk(line," "); sscanf(line,"%s",line); a=atof(line); b=atof(ptr+1); v0.Set(a,b,0); v0.Display(); vTrans=v0; //v0 will be the "origin" v0=v0-vTrans; // translate v0 to the origin } //the second point if(fgets(line,100,pf)){ ptr=strpbrk(line," "); sscanf(line,"%s",line); x1=atof(line); y1=atof(ptr+1); v1.Set(x1,y1,0); v1.Display(); v1=v1-vTrans; // translate the point } // The remaining points while(fgets(line,100,pf)){ ptr=strpbrk(line," "); sscanf(line,"%s",line); x2=atof(line); y2=atof(ptr+1); v2.Set(x2,y2,0); v2.Display(); v2=v2-vTrans; // translate the point vCross=v1*v2; printf("vCross= "); vCross.Display(); temp_area=.5*vZaxis.Dot(vCross); index++; printf("\nThe area of triangle #%d= %f\n\n",index,temp_area); area+=temp_area; //area is the sum of all the signed cross products v1=v2; //make current 2nd vector the new 1st vector } fclose(pf); } printf("\n\nThe total area of the polygon= %f\n\n",area); return 0; } int Polygonfileread(FILE* pf) { /* char line[100], num[100], *ptr, *pF; int n=0, len; // process line with number of vertices and number of faces // get memory for vertices(x,y,z) and face(a,b,c) arrays fgets(line,100,pf); printf("%s",line); ptr=strpbrk(line,":"); pF=strpbrk(ptr+1,"F"); len=pF-(ptr+1); sprintf(num,"%s",ptr+1); num[len]='\0'; nvert=atoi(num); x=(double*)malloc(nvert*sizeof(double)); y=(double*)malloc(nvert*sizeof(double)); z=(double*)malloc(nvert*sizeof(double)); ptr=strpbrk(pF,":"); nface=atoi(ptr+1); a=(int*)malloc(nface*sizeof(int)); b=(int*)malloc(nface*sizeof(int)); c=(int*)malloc(nface*sizeof(int)); triarea=(double*)malloc(nface*sizeof(double)); fgets(line,100,pf); // read Vertex List line // read all vertex(x,y,z) data lines for(n=0;n< nvert;n++){ fgets(line,100,pf); readXYZline(line, &x[n], &y[n], &z[n]); } fgets(line,100,pf); // read Face List line // read all face(a,b,c) data lines for(n=0;n< nface;n++){ fgets(line,100,pf); readABCline(line, &a[n], &b[n], &c[n]); fgets(line,100,pf); //read SMOOTHING line } */ return 1; } FILE* OpenFilepath(char* path) { FILE* pf; int i=0; // retrieve file path with possible blanks // i.e. c:\My Documents\foo // open file pf=fopen(path,"r"); if (!pf) {printf("could not open file %s\n",path);return 0;} else printf("opened %s\n",path); return pf; }