/* detread.c: reads in a det file object for vector display */ /* This now uses the accad/cgrg det format was load_obj.c */ #include #include #define DET_CODE 0x64657420 #include "datread.h" #undef abs #define abs(x) (((x)<0.0)?(-(x)):(x)) #define FLIP_ENDIAN 1 void flip_float(float *x) { union {float x; unsigned int i;} p; unsigned char b1, b2, b3, b4; p.x = *x; b4 = p.i&255; p.i = p.i/256; b3 = p.i&255; p.i = p.i/256; b2 = p.i&255; p.i = p.i/256; b1 = p.i; p.i = b1 +256*(b2 + 256*(b3 +256*b4)); *x = p.x; } void flip_short(short *x) { unsigned char b1, b2; b2 = *x&255; b1 = *x/256; b1 = b1&255; *x = b1+256*b2; } int detread(char filename[], dpts * *data_points, int *num_points, int * *data_polys, int *num_polys, float *size) { FILE * obj_file; /* file descriptor for input obj*/ short * tmp_pys; /* malloc'd approx buffer for poly count, indexs*/ short p, pts; int i, j, k; int code; unsigned char bt; /* numbers big endian, must turn around */ short num_pts, num_pys; /* 16 bit binary read */ *size = 0.0; if((obj_file = fopen(filename, "rb"))==NULL) { printf("could not open for reading %s \n", filename); return(1); } if((fread(&code, sizeof(long), 1, obj_file))==0) { printf("error reading DET code in file %s \n", filename); return(1); /* seems to be little endian, hmmm? */ } if (code != DET_CODE) { printf("looking for det file %X\n", code); /* return(1); */ } fread(&num_pts, sizeof(short), 1, obj_file); if(FLIP_ENDIAN) flip_short(&num_pts); *num_points = num_pts; /* send back to caller */ printf("num_points = %d \n", *num_points); fread(&num_pys, sizeof(short), 1, obj_file); if(FLIP_ENDIAN) flip_short(&num_pys); *num_polys = num_pys; /* send back to caller */ printf("num_polys = %d \n", *num_polys); *data_points = (dpts *)malloc(sizeof(dpts)*num_pts); if(*data_points == NULL) { printf("detread could not allocate enough space for points\n"); return(1); } tmp_pys = (short *)malloc(sizeof(short)*(num_pts*8+10)); if(tmp_pys == NULL) { printf("detread could not allocate enough space for indexs\n"); return(1); } for(i=0; i*size) *size = abs((*data_points)[i].x); if(abs((*data_points)[i].y)>*size) *size = abs((*data_points)[i].y); if(abs((*data_points)[i].z)>*size) *size = abs((*data_points)[i].z); } k = 0; /* locations count and pts in data_polys */ for (i=0; i