home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / isobuild / file_io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-11  |  13.7 KB  |  537 lines

  1.  
  2. /* file_io.c               -Brian Tierney,  LBL   12/90  */
  3.  
  4. /* This file contains code for read HIPS files to be used with isovis,
  5.    as well as the routine to output ASCII and binary 
  6.    formated vertex/triangle lists
  7.  */
  8.  
  9. /* $Id: file_io.c,v 1.4 1992/01/31 02:05:45 tierney Exp $ */
  10.  
  11. /* $Log: file_io.c,v $
  12.  * Revision 1.4  1992/01/31  02:05:45  tierney
  13.  * *** empty log message ***
  14.  *
  15.  * Revision 1.3  1992/01/30  20:05:03  davidr
  16.  * prior to Brian's changes
  17.  *
  18.  * Revision 1.2  1991/12/19  01:42:20  davidr
  19.  * added RCS identification markers
  20.  * */
  21.  
  22. static char rcsid[] = "$Id: file_io.c,v 1.4 1992/01/31 02:05:45 tierney Exp $" ;
  23.  
  24. #include "isobuild.h"
  25.  
  26. static struct header hd;
  27.  
  28. /**************************** get_data ****************************/
  29.  
  30. int
  31. get_size(fp)
  32.     FILE     *fp;
  33. /* This subroutine will read in the data file. */
  34. {
  35.     /* read HIPS header */
  36.     fread_header(fp, &hd, IN_FNAME);
  37.  
  38. #ifndef FLOAT_INPUT
  39.     if (hd.pixel_format != PFBYTE) {
  40.     Status("Error: pixel format must be byte.");
  41.     return (-1);
  42.     }
  43. #else
  44.     if (hd.pixel_format != PFFLOAT && hd.pixel_format != PFSHORT &&
  45.     hd.pixel_format != PFINT && hd.pixel_format != PFBYTE) {
  46.     Status("Error: pixel format must be float, int, short, or byte.");
  47.     return (-1);
  48.     }
  49. #endif
  50.  
  51.     zdim = hd.num_frame;
  52.     ydim = hd.orows;
  53.     xdim = hd.ocols;
  54.  
  55.     /* setup globals */
  56.     xmax = xdim - 1;
  57.     ymax = ydim - 1;
  58.     zmax = zdim - 1;
  59.  
  60.     if (VERBOSE)
  61.     fprintf(stderr, "%s: data set size xdim=%d ydim=%d zdim=%d\n",
  62.         MY_NAME, xdim, ydim, zdim);
  63.  
  64.     return (0);
  65. }
  66.  
  67. /***************************************************************/
  68. int
  69. read_hips_data(fp, in_data, size)
  70.     FILE     *fp;
  71.     Data_type ***in_data;
  72.     int       size;
  73. {
  74.  
  75. #ifndef FLOAT_INPUT
  76.     fprintf(stderr, "%s: Reading data file %s ... \n", MY_NAME, IN_FNAME);
  77.  
  78.     if (Fread(in_data[0][0], sizeof(Data_type), size, fp) != size) {
  79.     perror("\n error reading file\n");
  80.     return (-1);
  81.     }
  82. #else
  83.     register int i;
  84.     unsigned char cval;
  85.     short     sval;
  86.     int       ival;
  87.  
  88.     fprintf(stderr, "%s: Reading data file %s ... \n", MY_NAME, IN_FNAME);
  89.  
  90.     if (hd.pixel_format == PFFLOAT) {
  91.     if (Fread(in_data[0][0], sizeof(float), size, fp) != size) {
  92.         perror("\n error reading file\n");
  93.         return (-1);
  94.     }
  95.     } else {
  96.     Status("Converting input data set to floating point...");
  97.     if (hd.pixel_format == PFBYTE) {
  98.         for (i = 0; i < size; i++) {
  99.         if (Fread(&cval, sizeof(unsigned char), 1, fp) != 1) {
  100.             perror("\n error reading file\n");
  101.             exit(-1);
  102.         }
  103.         in_data[0][0][i] = (float) cval;
  104.         }
  105.     } else if (hd.pixel_format == PFSHORT) {
  106.         for (i = 0; i < size; i++) {
  107.         if (Fread(&sval, sizeof(short), 1, fp) != 1) {
  108.             perror("\n error reading file\n");
  109.             exit(-1);
  110.         }
  111.         in_data[0][0][i] = (float) sval;
  112.         }
  113.     } else if (hd.pixel_format == PFINT) {
  114.         for (i = 0; i < size; i++) {
  115.         if (Fread(&ival, sizeof(int), 1, fp) != 1) {
  116.             perror("\n error reading file\n");
  117.             exit(-1);
  118.         }
  119.         in_data[0][0][i] = (float) ival;
  120.         }
  121.     }
  122.     Status("Conversion done.");
  123.     }
  124. #endif
  125.  
  126.     return (0);
  127. }
  128.  
  129. /***************************************************************/
  130. int
  131. read_mask_file(size)        /* load an input segmentation mask */
  132.     int       size;
  133. {
  134.     Grid_type *grid_ptr;
  135.     register int i;
  136.  
  137.     if (fread_header(in_mask_fp, &hd, IN_MASK_FNAME) != 0) {
  138.     Status("Error reading hips header.");
  139.     return (-1);
  140.     }
  141.     if (hd.pixel_format != PFBYTE) {
  142.     Status("Error: mask pixel format must be byte.");
  143.     return (-1);
  144.     }
  145.     if (hd.num_frame != zdim || hd.orows != ydim || hd.ocols != xdim) {
  146.     Status("Error: mask and data must be the same size.");
  147.     return (-1);
  148.     }
  149.     fprintf(stderr, "%s: Reading mask file %s ... \n", MY_NAME, IN_MASK_FNAME);
  150.  
  151.     if (Fread(grid[0][0], sizeof(Grid_type), size, in_mask_fp) != size) {
  152.     perror("error reading file");
  153.     return (-1);
  154.     }
  155.  
  156.     /* iso_march expects mask to contain 0's and 1's only */
  157.     grid_ptr = grid[0][0];
  158.     for (i = 0; i < xdim * ydim * zdim; i++) {
  159.     if (grid_ptr[i] > 0)
  160.         grid_ptr[i] = 1;
  161.     }
  162.     return (1);
  163. }
  164.  
  165. /***************************************************************/
  166. int
  167. write_points(point_list, npoints, more)
  168.     POINT_PTR point_list;
  169.     int       npoints, more;
  170. {
  171.     int       h_buf[4];
  172.     static int tot_points = 0;
  173.  
  174. /*
  175.  * Header: more flag:  1 = MAY be more vertices to follow
  176.  * npoints: number of vertices
  177. */
  178.     if (tot_points == 0) {    /* write size header first time */
  179.     h_buf[0] = xdim;
  180.     h_buf[1] = ydim;
  181.     h_buf[2] = zdim;
  182.     h_buf[3] = 0;
  183.     if (fwrite((char *) h_buf, sizeof(int), 4, poly_fp) != 4)
  184.         perror("error writing header");
  185.     }
  186.     tot_points += npoints;
  187.     if (!more) {
  188.     fprintf(stderr, "Writing %d points.\n", tot_points);
  189.     tot_points = 0;
  190.     }
  191.     if (fwrite(&more, sizeof(int), 1, poly_fp) != 1)
  192.     perror("error writing more");
  193.     if (fwrite(&npoints, sizeof(int), 1, poly_fp) != 1)
  194.     perror("error writing npoints");
  195.  
  196.     if (npoints <= 0)
  197.     return 0;
  198.  
  199.     if (fwrite((char *) point_list, sizeof(POINT), npoints, poly_fp) != npoints)
  200.     perror("error writing output_buf");
  201.  
  202.     return 0;
  203. }
  204.  
  205. /***************************************************************/
  206. int
  207. write_polys(vert_list, norm_list, conn_list, nverts, nconn, more)
  208.     VERT_PTR  vert_list;
  209.     NORM_PTR  norm_list;
  210.     int      *conn_list;
  211.     int       nverts, nconn, more;
  212. {
  213.     int       norm_flag;
  214.     int       h_buf[4];
  215.     static int tot_verts = 0;
  216.  
  217. /*
  218.  * Header: more flag:  1 = MAY be more vertices to follow
  219.  * npoints: number of vertices
  220. */
  221.     if (tot_verts == 0) {    /* write size header first time */
  222.     h_buf[0] = xdim;
  223.     h_buf[1] = ydim;
  224.     h_buf[2] = zdim;
  225.     h_buf[3] = 0;
  226.     if (fwrite((char *) h_buf, sizeof(int), 4, poly_fp) != 4)
  227.         perror("error writing header");
  228.     }
  229.  
  230.     tot_verts += nverts;
  231.     if (!more) {
  232.     fprintf(stderr, "Writing %d vertices.\n", tot_verts);
  233.     tot_verts = 0;
  234.     }
  235.     if (fwrite(&more, sizeof(int), 1, poly_fp) != 1)
  236.     perror("error writing more flag");
  237.     if (fwrite(&nverts, sizeof(int), 1, poly_fp) != 1)
  238.     perror("error writing nverts");
  239.     if (fwrite(&nconn, sizeof(int), 1, poly_fp) != 1)
  240.     perror("error writing nconn");
  241.     if (MARCH_NORMALS)
  242.     norm_flag = 1;
  243.     else
  244.     norm_flag = 0;
  245.     if (fwrite(&norm_flag, sizeof(int), 1, poly_fp) != 1)
  246.     perror("error writing nconn");
  247.  
  248.     if (nverts <= 0)
  249.     return 0;
  250.  
  251.     /* write all vertices */
  252.     if (fwrite((char *) vert_list, sizeof(VERTEX), nverts, poly_fp) != nverts)
  253.     perror("error writing vertex list");
  254.  
  255.     if (MARCH_NORMALS) {
  256.     /* write all normals */
  257.     if (fwrite((char *) norm_list, sizeof(NORMAL), nverts, poly_fp) != nverts)
  258.         perror("error writing normal list");
  259.     }
  260.     if (nconn > 0) {
  261.     /* write connectivity list */
  262.     if (fwrite((char *) conn_list, sizeof(int), nconn, poly_fp) != nconn)
  263.         perror("error writing connectivity list");
  264.     }
  265.     return 0;
  266. }
  267.  
  268. /***************************************************************/
  269. int
  270. write_polys_movie_byu(vert_list, conn_list, nverts, nconn)
  271.     VERT_PTR  vert_list;
  272.     int      *conn_list;
  273.     int       nverts, nconn;
  274. {
  275.     int       val, i, npolys;
  276.  
  277. /* movie BYU header
  278.     1: number of parts or objects
  279.     2: number of vertices
  280.     3: number of polygons
  281.     4: number of edges
  282.  
  283.   move BYU data
  284.     1: parts array (starting and ending locations of objects)
  285.     2: vertices data (x,y,z order, floating point)
  286.     3: edge data, negative # to indicate end of polygon, type int
  287.  */
  288.     fprintf(stderr, "Writing movie BYU file (%d vertices) ... \n", nverts);
  289.  
  290.     val = 1;            /* only 1 object */
  291.     npolys = (nconn / 3);    /* this is the number of triangles */
  292.     fprintf(poly_fp, "%d %d %d %d \n", val, nverts, npolys, nconn);
  293.  
  294.     /*
  295.      * parts array just constists of 2 numbers, the upper and lower bounds of
  296.      * the vertex list (in this case: 1 and # polygons )
  297.      */
  298.     fprintf(poly_fp, "%d %d \n", val, npolys);
  299.  
  300.     if (nverts <= 0)
  301.     return 0;
  302.  
  303.     /* write all vertices */
  304.     for (i = 0; i < nverts; i++) {
  305.     fprintf(poly_fp, "%f %f %f \n", vert_list[i].x, vert_list[i].y,
  306.         vert_list[i].z);
  307.     }
  308.  
  309.     i = 0;
  310.     while (i < nconn) {
  311.     /*
  312.      * for byu format, indices start at 1, and last value for each
  313.      * polygon is negative
  314.      */
  315.     fprintf(poly_fp, "%d %d %d \n", conn_list[i++] + 1, conn_list[i++] + 1,
  316.         -(conn_list[i++] + 1));
  317.     }
  318.  
  319.     return 0;
  320. }
  321.  
  322. /***************************************************************/
  323. int
  324. write_polys_sunvision(vert_list, norm_list, conn_list, nverts, nconn)
  325.     VERT_PTR  vert_list;
  326.     NORM_PTR  norm_list;
  327.     int      *conn_list;
  328.     int       nverts, nconn;
  329. {
  330.     /* this routine written by Wing Nip, LBL */
  331.  
  332.     int       i;
  333.     float     min_in, max_in;
  334.     float     min_out = -1.0;
  335.     float     max_out = 1.0;
  336.     float     scale;
  337.     float     x, y, z;
  338.  
  339.     /* normalize vertices between -1.0 & 1.0 */
  340.     max_in = -1.0;
  341.     min_in = vert_list[0].x;    /* arbitrary assign a min value */
  342.  
  343.     for (i = 0; i < nverts; i++) {  /* find min and max values */
  344.     if (vert_list[i].x > max_in)
  345.         max_in = vert_list[i].x;
  346.     if (vert_list[i].y > max_in)
  347.         max_in = vert_list[i].y;
  348.     if (vert_list[i].z > max_in)
  349.         max_in = vert_list[i].z;
  350.     if (vert_list[i].x < min_in)
  351.         min_in = vert_list[i].x;
  352.     if (vert_list[i].y < min_in)
  353.         min_in = vert_list[i].y;
  354.     if (vert_list[i].z < min_in)
  355.         min_in = vert_list[i].z;
  356.     }
  357.     scale = (max_out - min_out) / (max_in - min_in);
  358.     fprintf(stderr, "Writing sunvision vff file(%d vertices) ... \n", nverts);
  359.     fprintf(poly_fp, "ncaa \n");
  360.     fprintf(poly_fp, "type=vertices; \n");
  361.     fprintf(poly_fp, "components=x y z normal_x normal_y normal_z; \n");
  362.     fprintf(poly_fp, "{ \n");
  363.  
  364.     /* write all vertices */
  365.     for (i = 0; i < nverts; i++) {
  366.     fprintf(poly_fp, "     {");
  367.     x = (vert_list[i].x - min_in) * scale + min_out;
  368.     y = (vert_list[i].y - min_in) * scale + min_out;
  369.     z = (vert_list[i].z - min_in) * scale + min_out;
  370.  
  371.     fprintf(poly_fp, "%f, %f, %f", x, y, z);
  372.  
  373.     /* normal */
  374.     if (MARCH_NORMALS) {
  375.         fprintf(poly_fp, " %f, %f, %f,", (float) norm_list[i].nx,
  376.             (float) norm_list[i].ny, (float) norm_list[i].nz);
  377.     } else {
  378.         fprintf(poly_fp, " 1.0, 1.0, 1.0");  /* flat shading */
  379.     }
  380.     fprintf(poly_fp, "},\n");
  381.     }
  382.  
  383.     fprintf(poly_fp, "}\n\n");
  384.     fprintf(poly_fp, "type=connectivity;\n");
  385.     fprintf(poly_fp, "components=variable.i;\n");
  386.     fprintf(poly_fp, "{\n");
  387.     i = 0;
  388.     while (i < nconn) {
  389.     fprintf(poly_fp, "     {");
  390.     fprintf(poly_fp, "%d, %d, %d,}, \n", conn_list[i++],
  391.         conn_list[i++], conn_list[i++]);
  392.     }
  393.     fprintf(poly_fp, "}\n");
  394.     return 0;
  395. }
  396.  
  397. /***************************************************************/
  398. int
  399. write_polys_wavefront(vert_list, norm_list, conn_list, nverts, nconn)
  400.     VERT_PTR  vert_list;
  401.     NORM_PTR  norm_list;
  402.     int      *conn_list;
  403.     int       nverts, nconn;
  404. {
  405.     int       i, f1, f2, f3;
  406.  
  407.     fprintf(stderr, "Writing wavefront formatted file (%d vertices) ... \n",
  408.         nverts);
  409.  
  410.     fprintf(poly_fp, "g %s \n", IN_FNAME);    /* group name */
  411.  
  412.     if (nverts <= 0)
  413.     return 0;
  414.  
  415.     /* write all vertices */
  416.     for (i = 0; i < nverts; i++) {
  417.     fprintf(poly_fp, "v %f %f %f \n", vert_list[i].x, vert_list[i].y,
  418.         vert_list[i].z);
  419.     }
  420.     if (MARCH_NORMALS) {
  421.     /* write all normals */
  422.     for (i = 0; i < nverts; i++) {
  423.         fprintf(poly_fp, "vn: %f %f %f \n",
  424.             (float) norm_list[i].nx, (float) norm_list[i].ny,
  425.             (float) norm_list[i].nz);
  426.     }
  427.     }
  428.     i = 0;
  429.     while (i < nconn) {
  430.     f1 = conn_list[i++] + 1;
  431.     f2 = conn_list[i++] + 1;
  432.     f3 = conn_list[i++] + 1;
  433.  
  434.     if (MARCH_NORMALS)
  435.         fprintf(poly_fp, "f %d//%d %d//%d %d//%d \n",
  436.             f1, f1, f2, f2, f3, f3);
  437.     else
  438.         fprintf(poly_fp, "f %d %d %d \n", f1, f2, f3);
  439.     }
  440.     fprintf(poly_fp, "\n");
  441.  
  442.     return 0;
  443. }
  444.  
  445. /****************************************************************/
  446. int
  447. write_points_ascii(point_list, npoints)
  448.     POINT_PTR point_list;
  449.     int       npoints;
  450. /* This subroutine writes out an ascii file of the polygons */
  451. {
  452.     register int i;
  453.  
  454.     fprintf(poly_fp, "num points: %d \n", npoints);
  455.  
  456.     /* write out all of the vertices */
  457.     for (i = 0; i < npoints; i++) {
  458.     if (fprintf(poly_fp, "vert: %.3f %.3f %.3f \n",
  459.             (float) point_list[i].x, (float) point_list[i].y,
  460.             (float) point_list[i].z) == -1) {
  461.         Error("writing to ascii point file.");
  462.     }
  463.     if (fprintf(poly_fp, "   norm: %.3f %.3f %.3f \n",
  464.             (float) point_list[i].nx, (float) point_list[i].ny,
  465.             (float) point_list[i].nz) == -1) {
  466.         Error("writing to ascii point file.");
  467.     }
  468.     }
  469.  
  470.     return 0;
  471. }
  472.  
  473. /****************************************************************/
  474. int
  475. write_polys_ascii(vert_list, norm_list, conn_list, nverts, nconn)
  476.     VERT_PTR  vert_list;
  477.     NORM_PTR  norm_list;
  478.     int      *conn_list;
  479.     int       nverts, nconn;
  480. /* This subroutine writes out an ascii file of the polygons */
  481. {
  482.     register int i;
  483.  
  484.     fprintf(poly_fp, "Num verts: %d,  num_edges: %d \n", nverts, nconn);
  485.  
  486.     /* write out all of the vertices */
  487.     for (i = 0; i < nverts; i++) {
  488.     if (fprintf(poly_fp, "vert: %.3f %.3f %.3f \n",
  489.             (float) vert_list[i].x, (float) vert_list[i].y,
  490.             (float) vert_list[i].z) == -1) {
  491.         Error("writing to ascii polygon file.");
  492.     }
  493.     if (MARCH_NORMALS) {
  494.         if (fprintf(poly_fp, "   norm: %.3f %.3f %.3f \n",
  495.             (float) norm_list[i].nx, (float) norm_list[i].ny,
  496.             (float) norm_list[i].nz) == -1) {
  497.         Error("writing to ascii polygon file.");
  498.         }
  499.     }
  500.     }
  501.  
  502.     i = 0;
  503.     while (i < nconn) {
  504.     if (fprintf(poly_fp, "edge: %d %d %d \n",
  505.             conn_list[i++], conn_list[i++],
  506.             conn_list[i++]) == -1) {
  507.         Error("writing to ascii polygon file.");
  508.     }
  509.     }
  510.  
  511.     return 0;
  512. }
  513.  
  514. /**************************************************************/
  515. void
  516. exit_handler()
  517. {
  518.     if (SERVER)
  519.     fprintf(stderr, "Caught signal from %s: Exiting... \n\n", MY_NAME);
  520.     else
  521.     fprintf(stderr, "%s: Exiting... \n\n", MY_NAME);
  522.  
  523.     if (grid != NULL)
  524.     free_3d_grid_array(grid);
  525.  
  526.     if (data != NULL)
  527.     free_3d_data_array(data);
  528.  
  529.     if (normals != NULL)
  530.     free_3d_normal_array(normals);
  531.  
  532.     if (block_info_array != NULL)
  533.     free_block_info_array(block_info_array);
  534.  
  535.     exit(-1);
  536. }
  537.