/* volume_dat2.c compute the volume of a single closed .dat graphics figure*/ /* compile with datread.c */ /* volume_dat bull.dat > volume_dat_bull.out */ #include #include #include #include "datread.h" #undef abs #define abs(x) ((x)<0.0?(-(x)):(x)) #undef max #define max(x,y) ((x)>(y)?(x):(y)) #undef min #define min(x,y) ((x)<(y)?(x):(y)) float znormal(int n, float x[], float y[], float z[], float *area); static int num_points; /* for datread */ static int num_polys; static dpts * data_points; static int * data_verts; static float size = 1.0; /* auto scale */ static char filename[100] = "cube1.dat"; static int status = -1; static int debug = 0; static float xmin, xmax, ymin, ymax, zmin, zmax; int main(int argc, char * argv[]) { float area, avgz, vol, dvol, minz, znorm, tarea; float offs; int i, j, k, npts, iv; float x[10], y[10], z[10]; /* max 10 points in polygon */ if(argc>1) { strcpy(filename,argv[1]); } printf("volume_dat2.c reading %s \n", filename); status = datread(filename, &data_points, &num_points, &data_verts, &num_polys, &size); printf("status=%d, zmax=%g, points=%d, polys=%d \n", status, size, num_points, num_polys); xmin = data_points[0].x; xmax = xmin; ymin = data_points[0].y; ymax = ymin; zmin = data_points[0].z; zmax = zmin; /* uncomment if data is clockwise looking from outside */ /* datccw(&data_points, &num_points, &data_verts, &num_polys); */ if(debug) datprint(data_points, num_points, data_verts, num_polys); vol = 0.0; /* total volume */ tarea = 0.0; /* total area */ offs = size; /* z's must be positive */ k = 0; /* npts, then npts node numbers, etc */ for(i=0; ioffs && znorm<0.0) printf("upper normal is negative \n"); if(debug) if(avgz0.0) printf("lower normal is positive \n"); } printf("\nxmin=%f, xmax=%f, ymin=%f, ymax=%f \n", xmin, xmax, ymin, ymax); printf("zmin=%f, zmax=%f \n", zmin, zmax); printf("enclosing area= %g, enclosing volume= %g \n", 2.0*((xmax-xmin)*(ymax-ymin)+(xmax-xmin)*(zmax-zmin)+(ymax-ymin)*(zmax-zmin)), (xmax-xmin)*(ymax-ymin)*(zmax-zmin)); printf("\nfinal total area = %g, total volume = %g \n\n", tarea, abs(vol)); return 0; } /* end main of volume_dat.c */ float znormal(int n, float x[], float y[], float z[], float *area) { float ax, ay, az, bx, by, bz, nx, ny, nz, ss; float cx, cy, cz, sss, sstot, ssstot; int i; sstot = 0.0; ssstot = 0.0; for(i=2; i0.1) printf("bad area ss=%g, sss=%g \n", sstot, ssstot); *area = sstot; return nz; } /* end znormal */