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 >
Wrap
C/C++ Source or Header
|
1992-01-15
|
4KB
|
149 lines
#include <stdio.h>
#include <math.h>
#include "header.h"
#define LARGE 1.0e10
fileread_pif(fp)
FILE *fp;
{
nodeptr insert_node();
segmptr insert_segm();
void print_segms();
void delete_segm();
double round_down();
double round_up();
extern double xmax,xmin,ymax,ymin,zmax,zmin;
segmptr S,SA;
nodeptr Nd;
nodeptr points[1000];
double x, y, z;
int ndID, pyID;
int npts, nr = 1, i;
char ch;
/* initialize */
SA = insert_segm();
nr = 1;
xmin = LARGE;
xmax = -LARGE;
ymin = LARGE;
ymax = -LARGE;
zmin = LARGE;
zmax = -LARGE;
while (nr != EOF && nr != 0) {
/*
* Find the first character, which must be 'N' or 'P'
* If not found, go thru the characters until found
*/
nr = fscanf(fp,"%c",&ch);
while ((ch != 'N') && (ch != 'P') && (nr != EOF) && (nr == 1))
nr = fscanf(fp,"%c",&ch);
if (nr != EOF && nr != 0) {
switch (ch) {
case 'N' :
nr = fscanf(fp,"%d %lf %lf %lf",&ndID,&x,&y,&z);
if (nr != EOF && nr == 4) {
printf("Node #%d: %f %f %f\n",ndID,x,y,z);
Nd = insert_node(x,y,z,SA);
points[ndID] = Nd;
if (x < xmin) xmin = x;
if (x > xmax) xmax = x;
if (y < ymin) ymin = y;
if (y > ymax) ymax = y;
if (z < zmin) zmin = z;
if (z > zmax) zmax = z;
} else {
fprintf(stderr,"Error : Couldn't read Node\n");
exit(-1);
}
break;
case 'P' :
nr = fscanf(fp,"%d %d",&pyID,&npts);
if (nr != EOF && nr == 2) {
printf("Poly #%d (%d points):",pyID,npts);
S = insert_segm();
i = 0;
while (nr != EOF && i<npts) {
nr = fscanf(fp,"%d",&ndID);
i++;
if (nr != EOF && nr == 1) {
printf(" %d",ndID);
if ((Nd = points[ndID]) != NULL) {
x = Nd->x;
y = Nd->y;
z = Nd->z;
Nd = insert_node(x,y,z,S);
}
} else {
fprintf(stderr,"Error : Couldn't read Polygon\n");
exit(-1);
}
}
printf("\n");
} else {
fprintf(stderr,"Error : Couldn't read Polygon\n");
exit(-1);
}
break;
default :
break;
}
}
}
/* round the min and max */
round_down(xmin);
round_up (xmax);
round_down(ymin);
round_up (ymax);
round_down(zmin);
round_up (zmax);
printf("xmin = %10.5f xmax = %10.5f\n",xmin,xmax);
printf("ymin = %10.5f ymax = %10.5f\n",ymin,ymax);
printf("zmin = %10.5f zmax = %10.5f\n",zmin,zmax);
delete_segm(SA);
}
/* round down a number */
double round_down(x)
double x;
{
double pow();
double inc, xnew, dpower;
int power;
/* round down x */
dpower = log10(fabs(x+SMALL));
power = (dpower > -SMALL) ? (int)dpower : (int)dpower - 1;
inc = pow(10.0,(double)power);
if (x > -SMALL)
xnew = inc * ((int)(x/inc + SMALL));
else
xnew = inc * ((int)(x/inc + SMALL) - 1);
return(xnew);
}
/* round up a number */
double round_up(x)
double x;
{
double pow();
double inc, xnew, dpower;
int power;
/* round up x */
dpower = log10(fabs(x+SMALL));
power = (dpower > -SMALL) ? (int)dpower : (int)dpower - 1;
inc = pow(10.0,(double)power);
if (x > SMALL)
xnew = inc * ((int)(x/inc - SMALL) + 1);
else
xnew = inc * ((int)(x/inc - SMALL));
return(xnew);
}