--- ray/src/common/readoct.c 1992/07/13 15:36:29 2.4 +++ ray/src/common/readoct.c 1997/03/07 15:45:26 2.12 @@ -1,4 +1,4 @@ -/* Copyright (c) 1991 Regents of the University of California */ +/* Copyright (c) 1992 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -21,13 +21,15 @@ static char SCCSid[] = "$SunId$ LBL"; static double ogetflt(); static long ogetint(); static char *ogetstr(); -static int getobj(), octerror(); +static int nonsurfinset(); +static int getobj(), octerror(), skiptree(); static OCTREE getfullnode(), gettree(); static char *infn; /* input file name */ static FILE *infp; /* input file stream */ static int objsize; /* size of stored OBJECT's */ static OBJECT objorig; /* zeroeth object */ +static OBJECT fnobjects; /* number of objects in this file */ static short otypmap[NUMOTYPE+8]; /* object type map */ @@ -38,10 +40,8 @@ int load; CUBE *scene; char *ofn[]; { - extern int fputs(); char sbuf[512]; int nf; - OBJECT fnobjects; register int i; long m; @@ -56,8 +56,11 @@ char *ofn[]; error(SYSTEM, errmsg); } } +#ifdef MSDOS + setmode(fileno(infp), O_BINARY); +#endif /* get header */ - if (checkheader(infp, OCTFMT, load&IO_INFO ? stdout : NULL) < 0) + if (checkheader(infp, OCTFMT, load&IO_INFO ? stdout : (FILE *)NULL) < 0) octerror(USER, "not an octree"); /* check format */ if ((objsize = ogetint(2)-OCTMAGIC) <= 0 || @@ -88,28 +91,28 @@ char *ofn[]; if (fnobjects != m) octerror(USER, "too many objects"); - if (load & IO_TREE) { - /* get the octree */ + if (load & IO_TREE) /* get the octree */ scene->cutree = gettree(); - if (load & IO_SCENE) /* get the scene */ - if (nf == 0) { - for (i = 0; *ogetstr(sbuf); i++) - if ((otypmap[i] = otype(sbuf)) < 0) { - sprintf(errmsg, "unknown type \"%s\"", - sbuf); - octerror(WARNING, errmsg); - } - while (getobj() != OVOID) - ; - } else { /* consistency checks */ - /* check object count */ - if (nobjects != objorig+fnobjects) - octerror(USER, "bad object count; octree stale?"); - /* check for non-surfaces */ - if (nonsurfinset(objorig, fnobjects)) - octerror(USER, "modifier in tree; octree stale?"); - } - } + else if (load & IO_SCENE && nf == 0) + skiptree(); + + if (load & IO_SCENE) /* get the scene */ + if (nf == 0) { + for (i = 0; *ogetstr(sbuf); i++) + if ((otypmap[i] = otype(sbuf)) < 0) { + sprintf(errmsg, "unknown type \"%s\"", sbuf); + octerror(WARNING, errmsg); + } + while (getobj() != OVOID) + ; + } else { /* consistency checks */ + /* check object count */ + if (nobjects != objorig+fnobjects) + octerror(USER, "bad object count; octree stale?"); + /* check for non-surfaces */ + if (dosets(nonsurfinset)) + octerror(USER, "modifier in tree; octree stale?"); + } fclose(infp); return(nf); } @@ -130,7 +133,7 @@ char *s; static OCTREE getfullnode() /* get a set, return fullnode */ { - OBJECT set[MAXSET+1]; + OBJECT set[MAXSET+1]; register int i; register long m; @@ -163,7 +166,7 @@ static double ogetflt() /* get a floating point number */ { extern double getflt(); - double r; + double r; r = getflt(infp); if (feof(infp)) @@ -175,7 +178,7 @@ ogetflt() /* get a floating point number */ static OCTREE gettree() /* get a pre-ordered octree */ { - register OCTREE ot; + register OCTREE ot; register int i; switch (getc(infp)) { @@ -197,14 +200,54 @@ gettree() /* get a pre-ordered octree */ } +static int +nonsurfinset(os) /* check set for modifier */ +register OBJECT *os; +{ + register OBJECT s; + register int i; + + for (i = *os; i-- > 0; ) + if ((s = *++os) >= objorig && s < objorig+fnobjects && + ismodifier(objptr(s)->otype)) + return(1); + return(0); +} + + static +skiptree() /* skip octree on input */ +{ + register int i; + + switch (getc(infp)) { + case OT_EMPTY: + return; + case OT_FULL: + for (i = ogetint(objsize)*objsize; i-- > 0; ) + if (getc(infp) == EOF) + octerror(USER, "truncated octree"); + return; + case OT_TREE: + for (i = 0; i < 8; i++) + skiptree(); + return; + case EOF: + octerror(USER, "truncated octree"); + default: + octerror(USER, "damaged octree"); + } +} + + +static getobj() /* get next object */ { char sbuf[MAXSTR]; int obj; register int i; register long m; - register OBJREC *objp; + register OBJREC *objp; i = ogetint(1); if (i == -1) @@ -230,7 +273,7 @@ getobj() /* get next object */ objp->oargs.sarg[i] = savestr(ogetstr(sbuf)); } else objp->oargs.sarg = NULL; -#ifdef IARGS +#ifdef IARGS if (objp->oargs.niargs = ogetint(2)) { objp->oargs.iarg = (long *)bmalloc (objp->oargs.niargs*sizeof(long)); @@ -252,7 +295,6 @@ getobj() /* get next object */ objp->oargs.farg = NULL; /* initialize */ objp->os = NULL; - objp->lastrno = -1; /* insert */ insertobject(obj); return(obj);