--- ray/src/common/readoct.c 1992/08/06 09:29:38 2.6 +++ ray/src/common/readoct.c 2003/07/17 09:21:29 2.20 @@ -1,63 +1,69 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: readoct.c,v 2.20 2003/07/17 09:21:29 schorsch Exp $"; #endif - /* * readoct.c - routines to read octree information. - * - * 7/30/85 */ -#include "standard.h" +#include "copyright.h" -#include "octree.h" +#include +#include +#include "standard.h" +#include "platform.h" +#include "octree.h" #include "object.h" - #include "otypes.h" +#include "resolu.h" -static double ogetflt(); -static long ogetint(); -static char *ogetstr(); -static int getobj(), octerror(); -static OCTREE getfullnode(), gettree(); +static double ogetflt(void); +static long ogetint(int); +static char *ogetstr(char *); +static int nonsurfinset(OBJECT *); +static void octerror(int etyp, char *msg); +static void skiptree(void); +static OCTREE getfullnode(void), gettree(void); -static char *infn; /* input file name */ +static char *infn; /* input file specification */ static FILE *infp; /* input file stream */ static int objsize; /* size of stored OBJECT's */ static OBJECT objorig; /* zeroeth object */ -static short otypmap[NUMOTYPE+8]; /* object type map */ +static OBJECT fnobjects; /* number of objects in this file */ int -readoct(fname, load, scene, ofn) /* read in octree from file */ -char *fname; +readoct(inpspec, load, scene, ofn) /* read in octree file or stream */ +char *inpspec; int load; CUBE *scene; char *ofn[]; { - extern int fputs(); char sbuf[512]; int nf; - OBJECT fnobjects; - register int i; + int i; long m; - if (fname == NULL) { + if (inpspec == NULL) { infn = "standard input"; infp = stdin; + } else if (inpspec[0] == '!') { + infn = inpspec; + if ((infp = popen(inpspec+1, "r")) == NULL) { + sprintf(errmsg, "cannot execute \"%s\"", inpspec); + error(SYSTEM, errmsg); + } } else { - infn = fname; - if ((infp = fopen(fname, "r")) == NULL) { + infn = inpspec; + if ((infp = fopen(inpspec, "r")) == NULL) { sprintf(errmsg, "cannot open octree file \"%s\"", - fname); + inpspec); error(SYSTEM, errmsg); } } + SET_FILE_BINARY(infp); /* 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,29 +94,29 @@ 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(); 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) - ; + /* load binary scene data */ + readscene(infp, objsize); + } 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)) + if (dosets(nonsurfinset)) octerror(USER, "modifier in tree; octree stale?"); } - fclose(infp); + /* close the input */ + if (infn[0] == '!') + pclose(infp); + else + fclose(infp); return(nf); } @@ -130,7 +136,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 +169,7 @@ static double ogetflt() /* get a floating point number */ { extern double getflt(); - double r; + double r; r = getflt(infp); if (feof(infp)) @@ -175,7 +181,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)) { @@ -194,13 +200,29 @@ gettree() /* get a pre-ordered octree */ default: octerror(USER, "damaged octree"); } + return NULL; /* pro forma return */ } -static -skiptree() /* skip octree on input */ +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 void +skiptree(void) /* skip octree on input */ +{ + register int i; switch (getc(infp)) { case OT_EMPTY: @@ -222,71 +244,7 @@ skiptree() /* skip octree on input */ } -static -getobj() /* get next object */ -{ - char sbuf[MAXSTR]; - int obj; - register int i; - register long m; - register OBJREC *objp; - - i = ogetint(1); - if (i == -1) - return(OVOID); /* terminator */ - if ((obj = newobject()) == OVOID) - error(SYSTEM, "out of object space"); - objp = objptr(obj); - if ((objp->otype = otypmap[i]) < 0) - octerror(USER, "reference to unknown type"); - if ((m = ogetint(objsize)) != OVOID) { - m += objorig; - if ((OBJECT)m != m) - octerror(USER, "too many objects"); - } - objp->omod = m; - objp->oname = savqstr(ogetstr(sbuf)); - if (objp->oargs.nsargs = ogetint(2)) { - objp->oargs.sarg = (char **)bmalloc - (objp->oargs.nsargs*sizeof(char *)); - if (objp->oargs.sarg == NULL) - goto memerr; - for (i = 0; i < objp->oargs.nsargs; i++) - objp->oargs.sarg[i] = savestr(ogetstr(sbuf)); - } else - objp->oargs.sarg = NULL; -#ifdef IARGS - if (objp->oargs.niargs = ogetint(2)) { - objp->oargs.iarg = (long *)bmalloc - (objp->oargs.niargs*sizeof(long)); - if (objp->oargs.iarg == NULL) - goto memerr; - for (i = 0; i < objp->oargs.niargs; i++) - objp->oargs.iarg[i] = ogetint(4); - } else - objp->oargs.iarg = NULL; -#endif - if (objp->oargs.nfargs = ogetint(2)) { - objp->oargs.farg = (FLOAT *)bmalloc - (objp->oargs.nfargs*sizeof(FLOAT)); - if (objp->oargs.farg == NULL) - goto memerr; - for (i = 0; i < objp->oargs.nfargs; i++) - objp->oargs.farg[i] = ogetflt(); - } else - objp->oargs.farg = NULL; - /* initialize */ - objp->os = NULL; - objp->lastrno = -1; - /* insert */ - insertobject(obj); - return(obj); -memerr: - error(SYSTEM, "out of memory in getobj"); -} - - -static +static void octerror(etyp, msg) /* octree error */ int etyp; char *msg;