home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / forum23.lzh / f23b / SOFTWARE / PDRAW / pif.c < prev    next >
C/C++ Source or Header  |  1992-01-15  |  4KB  |  149 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "header.h"
  4. #define  LARGE 1.0e10
  5.  
  6. fileread_pif(fp)
  7. FILE *fp;
  8. {
  9.    nodeptr insert_node();
  10.    segmptr insert_segm();
  11.    void    print_segms();
  12.    void    delete_segm();
  13.    double  round_down();
  14.    double  round_up();
  15.  
  16.    extern  double   xmax,xmin,ymax,ymin,zmax,zmin;
  17.  
  18.    segmptr S,SA;
  19.    nodeptr Nd;
  20.    nodeptr points[1000];
  21.    double  x, y, z;
  22.    int     ndID, pyID;
  23.    int     npts, nr = 1, i;
  24.    char    ch;
  25.  
  26.    /* initialize */
  27.    SA    = insert_segm();
  28.    nr    = 1;
  29.    xmin  = LARGE;
  30.    xmax  = -LARGE;
  31.    ymin  = LARGE;
  32.    ymax  = -LARGE;
  33.    zmin  = LARGE;
  34.    zmax  = -LARGE;
  35.  
  36.    while (nr != EOF && nr != 0) {
  37.       /*
  38.        * Find the first character, which must be 'N' or 'P'
  39.        * If not found, go thru the characters until found
  40.        */
  41.       nr = fscanf(fp,"%c",&ch);
  42.       while ((ch != 'N') && (ch != 'P') && (nr != EOF) && (nr == 1)) 
  43.          nr = fscanf(fp,"%c",&ch);
  44.  
  45.       if (nr != EOF && nr != 0) {
  46.          switch (ch) {
  47.          case 'N' :
  48.             nr = fscanf(fp,"%d %lf %lf %lf",&ndID,&x,&y,&z);
  49.             if (nr != EOF && nr == 4) {
  50.                printf("Node #%d: %f %f %f\n",ndID,x,y,z);
  51.                Nd = insert_node(x,y,z,SA);
  52.                points[ndID] = Nd;
  53.                if (x < xmin) xmin = x;
  54.                if (x > xmax) xmax = x;
  55.                if (y < ymin) ymin = y;
  56.                if (y > ymax) ymax = y;
  57.                if (z < zmin) zmin = z;
  58.                if (z > zmax) zmax = z;
  59.             } else {
  60.                fprintf(stderr,"Error : Couldn't read Node\n");
  61.                exit(-1);
  62.             }
  63.             break;
  64.          case 'P' :
  65.             nr = fscanf(fp,"%d %d",&pyID,&npts);
  66.             if (nr != EOF && nr == 2) {
  67.                printf("Poly #%d (%d points):",pyID,npts);
  68.                S = insert_segm();
  69.                i = 0;
  70.                while (nr != EOF && i<npts) {
  71.                   nr = fscanf(fp,"%d",&ndID);
  72.                   i++;
  73.                   if (nr != EOF && nr == 1) {
  74.                      printf(" %d",ndID);
  75.                      if ((Nd = points[ndID]) != NULL) {
  76.                         x = Nd->x;
  77.                         y = Nd->y;
  78.                         z = Nd->z;
  79.                         Nd = insert_node(x,y,z,S);
  80.                      }
  81.                   } else {
  82.                      fprintf(stderr,"Error : Couldn't read Polygon\n");
  83.                      exit(-1);
  84.                   }
  85.                } 
  86.                printf("\n");
  87.             } else {
  88.                fprintf(stderr,"Error : Couldn't read Polygon\n");
  89.                exit(-1);
  90.             }
  91.             break;
  92.          default  :
  93.             break;
  94.          }
  95.       }
  96.    }
  97.  
  98.    /* round the min and max */
  99.    round_down(xmin);
  100.    round_up  (xmax);
  101.    round_down(ymin);
  102.    round_up  (ymax);
  103.    round_down(zmin);
  104.    round_up  (zmax);
  105.    printf("xmin = %10.5f   xmax = %10.5f\n",xmin,xmax);
  106.    printf("ymin = %10.5f   ymax = %10.5f\n",ymin,ymax);
  107.    printf("zmin = %10.5f   zmax = %10.5f\n",zmin,zmax);
  108.  
  109.    delete_segm(SA);
  110. }
  111.  
  112. /* round down a number */
  113. double round_down(x)
  114. double x;
  115. {
  116.    double pow();
  117.    double inc, xnew, dpower;
  118.    int    power;
  119.  
  120.    /* round down x */
  121.    dpower = log10(fabs(x+SMALL));
  122.    power  = (dpower > -SMALL) ? (int)dpower : (int)dpower - 1;
  123.    inc    = pow(10.0,(double)power);
  124.    if (x > -SMALL)
  125.       xnew = inc * ((int)(x/inc + SMALL));
  126.    else
  127.       xnew = inc * ((int)(x/inc + SMALL) - 1);
  128.    return(xnew);
  129. }
  130.  
  131. /* round up a number */
  132. double round_up(x)
  133. double x;
  134. {
  135.    double pow();
  136.    double inc, xnew, dpower;
  137.    int    power;
  138.  
  139.    /* round up x */
  140.    dpower = log10(fabs(x+SMALL));
  141.    power  = (dpower > -SMALL) ? (int)dpower : (int)dpower - 1;
  142.    inc    = pow(10.0,(double)power);
  143.    if (x > SMALL)
  144.       xnew = inc * ((int)(x/inc - SMALL) + 1);
  145.    else
  146.       xnew = inc * ((int)(x/inc - SMALL));
  147.    return(xnew);
  148. }
  149.