--- ray/src/common/readoct.c 1989/03/22 09:53:46 1.4 +++ ray/src/common/readoct.c 2003/02/22 02:07:22 2.13 @@ -1,13 +1,65 @@ -/* Copyright (c) 1986 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: readoct.c,v 2.13 2003/02/22 02:07:22 greg Exp $"; #endif - /* * readoct.c - routines to read octree information. + */ + +/* ==================================================================== + * The Radiance Software License, Version 1.0 * - * 7/30/85 + * Copyright (c) 1990 - 2002 The Regents of the University of California, + * through Lawrence Berkeley National Laboratory. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes Radiance software + * (http://radsite.lbl.gov/) + * developed by the Lawrence Berkeley National Laboratory + * (http://www.lbl.gov/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Radiance," "Lawrence Berkeley National Laboratory" + * and "The Regents of the University of California" must + * not be used to endorse or promote products derived from this + * software without prior written permission. For written + * permission, please contact radiance@radsite.lbl.gov. + * + * 5. Products derived from this software may not be called "Radiance", + * nor may "Radiance" appear in their name, without prior written + * permission of Lawrence Berkeley National Laboratory. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of Lawrence Berkeley National Laboratory. For more + * information on Lawrence Berkeley National Laboratory, please see + * . */ #include "standard.h" @@ -18,60 +70,72 @@ static char SCCSid[] = "$SunId$ LBL"; #include "otypes.h" -double atof(); -double getflt(); -long getint(); -char *getstr(); -OCTREE getfullnode(), gettree(); +static double ogetflt(); +static long ogetint(); +static char *ogetstr(); +static int nonsurfinset(); +static int getobj(), octerror(), skiptree(); +static OCTREE getfullnode(), gettree(); -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 OBJECT fnobjects; /* number of objects in this file */ static short otypmap[NUMOTYPE+8]; /* object type map */ 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[]; { - char sbuf[128]; + char sbuf[512]; int nf; register 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); } } +#ifdef MSDOS + setmode(fileno(infp), O_BINARY); +#endif /* get header */ - if (load & IO_INFO) - copyheader(infp, stdout); - else - getheader(infp, NULL); + if (checkheader(infp, OCTFMT, load&IO_INFO ? stdout : (FILE *)NULL) < 0) + octerror(USER, "not an octree"); /* check format */ - if (getint(2) != OCTMAGIC) - octerror(USER, "bad octree"); + if ((objsize = ogetint(2)-OCTMAGIC) <= 0 || + objsize > MAXOBJSIZ || objsize > sizeof(long)) + octerror(USER, "incompatible octree format"); /* get boundaries */ if (load & IO_BOUNDS) { for (i = 0; i < 3; i++) - scene->cuorg[i] = atof(getstr(sbuf)); - scene->cusize = atof(getstr(sbuf)); + scene->cuorg[i] = atof(ogetstr(sbuf)); + scene->cusize = atof(ogetstr(sbuf)); } else { for (i = 0; i < 4; i++) - getstr(sbuf); + ogetstr(sbuf); } objorig = nobjects; /* set object offset */ nf = 0; /* get object files */ - while (*getstr(sbuf)) { + while (*ogetstr(sbuf)) { if (load & IO_SCENE) readobj(sbuf); if (load & IO_FILES) @@ -80,93 +144,103 @@ char *ofn[]; } if (load & IO_FILES) ofn[nf] = NULL; + /* get number of objects */ + fnobjects = m = ogetint(objsize); + 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(); - /* get the scene */ - if (nf == 0 && load & IO_SCENE) { - for (i = 0; *getstr(sbuf); i++) - if ((otypmap[i] = otype(sbuf)) < 0) { - sprintf(errmsg, "unknown type \"%s\"", - sbuf); - octerror(WARNING, errmsg); - } - while (getobj() != OVOID) - ; - } - } - fclose(infp); + 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?"); + } + /* close the input */ + if (infn[0] == '!') + pclose(infp); + else + fclose(infp); return(nf); } static char * -getstr(s) /* get null-terminated string */ +ogetstr(s) /* get null-terminated string */ char *s; { - register char *cp; - register int c; + extern char *getstr(); - cp = s; - while ((c = getc(infp)) != EOF) - if ((*cp++ = c) == '\0') - return(s); - - octerror(USER, "truncated octree"); + if (getstr(s, infp) == NULL) + octerror(USER, "truncated octree"); + return(s); } static OCTREE getfullnode() /* get a set, return fullnode */ { - OBJECT set[MAXSET+1]; + OBJECT set[MAXSET+1]; register int i; + register long m; - set[0] = getint(sizeof(OBJECT)); - if (set[0] > MAXSET) + if ((set[0] = ogetint(objsize)) > MAXSET) octerror(USER, "bad set in getfullnode"); - for (i = 1; i <= set[0]; i++) - set[i] = getint(sizeof(OBJECT)) + objorig; + for (i = 1; i <= set[0]; i++) { + m = ogetint(objsize) + objorig; + if ((set[i] = m) != m) + octerror(USER, "too many objects"); + } return(fullnode(set)); } static long -getint(siz) /* get a siz-byte integer */ -register int siz; +ogetint(siz) /* get a siz-byte integer */ +int siz; { - register int c; + extern long getint(); register long r; - c = getc(infp); - r = c&0x80 ? -1L : 0L; /* sign extend */ - ungetc(c, infp); - while (siz--) { - if ((c = getc(infp)) == EOF) - octerror(USER, "truncated octree"); - r <<= 8; - r |= c; - } + r = getint(siz, infp); + if (feof(infp)) + octerror(USER, "truncated octree"); return(r); } static double -getflt() /* get a floating point number */ +ogetflt() /* get a floating point number */ { - extern double ldexp(); - double d; + extern double getflt(); + double r; - d = (double)getint(4)/0x7fffffff; - return(ldexp(d, getint(1))); + r = getflt(infp); + if (feof(infp)) + octerror(USER, "truncated octree"); + return(r); } static OCTREE gettree() /* get a pre-ordered octree */ { - register OCTREE ot; + register OCTREE ot; register int i; switch (getc(infp)) { @@ -188,15 +262,56 @@ 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 OBJREC *objp; + register long m; + register OBJREC *objp; - i = getint(1); + i = ogetint(1); if (i == -1) return(OVOID); /* terminator */ if ((obj = newobject()) == OVOID) @@ -204,41 +319,44 @@ getobj() /* get next object */ objp = objptr(obj); if ((objp->otype = otypmap[i]) < 0) octerror(USER, "reference to unknown type"); - if ((objp->omod = getint(sizeof(OBJECT))) != OVOID) - objp->omod += objorig; - objp->oname = savqstr(getstr(sbuf)); - if (objp->oargs.nsargs = getint(2)) { - objp->oargs.sarg = (char **)bmalloc + 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 **)malloc (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(getstr(sbuf)); + objp->oargs.sarg[i] = savestr(ogetstr(sbuf)); } else objp->oargs.sarg = NULL; -#ifdef IARGS - if (objp->oargs.niargs = getint(2)) { - objp->oargs.iarg = (long *)bmalloc +#ifdef IARGS + if (objp->oargs.niargs = ogetint(2)) { + objp->oargs.iarg = (long *)malloc (objp->oargs.niargs*sizeof(long)); if (objp->oargs.iarg == NULL) goto memerr; for (i = 0; i < objp->oargs.niargs; i++) - objp->oargs.iarg[i] = getint(4); + objp->oargs.iarg[i] = ogetint(4); } else objp->oargs.iarg = NULL; #endif - if (objp->oargs.nfargs = getint(2)) { - objp->oargs.farg = (double *)bmalloc - (objp->oargs.nfargs*sizeof(double)); + if (objp->oargs.nfargs = ogetint(2)) { + objp->oargs.farg = (FLOAT *)malloc + (objp->oargs.nfargs*sizeof(FLOAT)); if (objp->oargs.farg == NULL) goto memerr; for (i = 0; i < objp->oargs.nfargs; i++) - objp->oargs.farg[i] = getflt(); + objp->oargs.farg[i] = ogetflt(); } else objp->oargs.farg = NULL; /* initialize */ objp->os = NULL; - objp->lastrno = -1; /* insert */ insertobject(obj); return(obj);