ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/readoct.c
(Generate patch)

Comparing ray/src/common/readoct.c (file contents):
Revision 1.4 by greg, Wed Mar 22 09:53:46 1989 UTC vs.
Revision 2.6 by greg, Thu Aug 6 09:29:38 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 18 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include  "otypes.h"
20  
21 < double  atof();
22 < double  getflt();
23 < long  getint();
24 < char  *getstr();
25 < OCTREE  getfullnode(), gettree();
21 > static double  ogetflt();
22 > static long  ogetint();
23 > static char  *ogetstr();
24 > static int  getobj(), octerror();
25 > static OCTREE  getfullnode(), gettree();
26  
27   static char  *infn;                     /* input file name */
28   static FILE  *infp;                     /* input file stream */
29 + static int  objsize;                    /* size of stored OBJECT's */
30   static OBJECT  objorig;                 /* zeroeth object */
31   static short  otypmap[NUMOTYPE+8];      /* object type map */
32  
# Line 37 | Line 38 | int  load;
38   CUBE  *scene;
39   char  *ofn[];
40   {
41 <        char  sbuf[128];
41 >        extern int  fputs();
42 >        char  sbuf[512];
43          int  nf;
44 +        OBJECT  fnobjects;
45          register int  i;
46 +        long  m;
47          
48          if (fname == NULL) {
49                  infn = "standard input";
# Line 53 | Line 57 | char  *ofn[];
57                  }
58          }
59                                          /* get header */
60 <        if (load & IO_INFO)
61 <                copyheader(infp, stdout);
58 <        else
59 <                getheader(infp, NULL);
60 >        if (checkheader(infp, OCTFMT, load&IO_INFO ? stdout : NULL) < 0)
61 >                octerror(USER, "not an octree");
62                                          /* check format */
63 <        if (getint(2) != OCTMAGIC)
64 <                octerror(USER, "bad octree");
63 >        if ((objsize = ogetint(2)-OCTMAGIC) <= 0 ||
64 >                        objsize > MAXOBJSIZ || objsize > sizeof(long))
65 >                octerror(USER, "incompatible octree format");
66                                          /* get boundaries */
67          if (load & IO_BOUNDS) {
68                  for (i = 0; i < 3; i++)
69 <                        scene->cuorg[i] = atof(getstr(sbuf));
70 <                scene->cusize = atof(getstr(sbuf));
69 >                        scene->cuorg[i] = atof(ogetstr(sbuf));
70 >                scene->cusize = atof(ogetstr(sbuf));
71          } else {
72                  for (i = 0; i < 4; i++)
73 <                        getstr(sbuf);
73 >                        ogetstr(sbuf);
74          }
75          objorig = nobjects;             /* set object offset */
76          nf = 0;                         /* get object files */
77 <        while (*getstr(sbuf)) {
77 >        while (*ogetstr(sbuf)) {
78                  if (load & IO_SCENE)
79                          readobj(sbuf);
80                  if (load & IO_FILES)
# Line 80 | Line 83 | char  *ofn[];
83          }
84          if (load & IO_FILES)
85                  ofn[nf] = NULL;
86 +                                        /* get number of objects */
87 +        fnobjects = m = ogetint(objsize);
88 +        if (fnobjects != m)
89 +                octerror(USER, "too many objects");
90  
91 <        if (load & IO_TREE) {
85 <                                                /* get the octree */
91 >        if (load & IO_TREE)                     /* get the octree */
92                  scene->cutree = gettree();
93 <                                                /* get the scene */
94 <                if (nf == 0 && load & IO_SCENE) {
95 <                        for (i = 0; *getstr(sbuf); i++)
96 <                                if ((otypmap[i] = otype(sbuf)) < 0) {
97 <                                        sprintf(errmsg, "unknown type \"%s\"",
98 <                                                        sbuf);
99 <                                        octerror(WARNING, errmsg);
100 <                                }
101 <                        while (getobj() != OVOID)
102 <                                ;
103 <                }
104 <        }
93 >        else if (load & IO_SCENE && nf == 0)
94 >                skiptree();
95 >                
96 >        if (load & IO_SCENE)            /* get the scene */
97 >            if (nf == 0) {
98 >                for (i = 0; *ogetstr(sbuf); i++)
99 >                        if ((otypmap[i] = otype(sbuf)) < 0) {
100 >                                sprintf(errmsg, "unknown type \"%s\"", sbuf);
101 >                                octerror(WARNING, errmsg);
102 >                        }
103 >                while (getobj() != OVOID)
104 >                        ;
105 >            } else {                    /* consistency checks */
106 >                                /* check object count */
107 >                if (nobjects != objorig+fnobjects)
108 >                        octerror(USER, "bad object count; octree stale?");
109 >                                /* check for non-surfaces */
110 >                if (nonsurfinset(objorig, fnobjects))
111 >                        octerror(USER, "modifier in tree; octree stale?");
112 >            }
113          fclose(infp);
114          return(nf);
115   }
116  
117  
118   static char *
119 < getstr(s)                       /* get null-terminated string */
119 > ogetstr(s)                      /* get null-terminated string */
120   char  *s;
121   {
122 <        register char  *cp;
109 <        register int  c;
122 >        extern char  *getstr();
123  
124 <        cp = s;
125 <        while ((c = getc(infp)) != EOF)
126 <                if ((*cp++ = c) == '\0')
114 <                        return(s);
115 <                
116 <        octerror(USER, "truncated octree");
124 >        if (getstr(s, infp) == NULL)
125 >                octerror(USER, "truncated octree");
126 >        return(s);
127   }
128  
129  
# Line 122 | Line 132 | getfullnode()                  /* get a set, return fullnode */
132   {
133          OBJECT  set[MAXSET+1];
134          register int  i;
135 +        register long  m;
136  
137 <        set[0] = getint(sizeof(OBJECT));
127 <        if (set[0] > MAXSET)
137 >        if ((set[0] = ogetint(objsize)) > MAXSET)
138                  octerror(USER, "bad set in getfullnode");
139 <        for (i = 1; i <= set[0]; i++)
140 <                set[i] = getint(sizeof(OBJECT)) + objorig;
139 >        for (i = 1; i <= set[0]; i++) {
140 >                m = ogetint(objsize) + objorig;
141 >                if ((set[i] = m) != m)
142 >                        octerror(USER, "too many objects");
143 >        }
144          return(fullnode(set));
145   }      
146  
147  
148   static long
149 < getint(siz)                     /* get a siz-byte integer */
150 < register int  siz;
149 > ogetint(siz)                    /* get a siz-byte integer */
150 > int  siz;
151   {
152 <        register int  c;
152 >        extern long  getint();
153          register long  r;
154  
155 <        c = getc(infp);
156 <        r = c&0x80 ? -1L : 0L;          /* sign extend */
157 <        ungetc(c, infp);
145 <        while (siz--) {
146 <                if ((c = getc(infp)) == EOF)
147 <                        octerror(USER, "truncated octree");
148 <                r <<= 8;
149 <                r |= c;
150 <        }
155 >        r = getint(siz, infp);
156 >        if (feof(infp))
157 >                octerror(USER, "truncated octree");
158          return(r);
159   }
160  
161  
162   static double
163 < getflt()                        /* get a floating point number */
163 > ogetflt()                       /* get a floating point number */
164   {
165 <        extern double  ldexp();
166 <        double  d;
165 >        extern double  getflt();
166 >        double  r;
167  
168 <        d = (double)getint(4)/0x7fffffff;
169 <        return(ldexp(d, getint(1)));
168 >        r = getflt(infp);
169 >        if (feof(infp))
170 >                octerror(USER, "truncated octree");
171 >        return(r);
172   }
173          
174  
# Line 189 | Line 198 | gettree()                      /* get a pre-ordered octree */
198  
199  
200   static
201 + skiptree()                              /* skip octree on input */
202 + {
203 +        register int  i;
204 +        
205 +        switch (getc(infp)) {
206 +        case OT_EMPTY:
207 +                return;
208 +        case OT_FULL:
209 +                for (i = ogetint(objsize)*objsize; i-- > 0; )
210 +                        if (getc(infp) == EOF)
211 +                                octerror(USER, "truncated octree");
212 +                return;
213 +        case OT_TREE:
214 +                for (i = 0; i < 8; i++)
215 +                        skiptree();
216 +                return;
217 +        case EOF:
218 +                octerror(USER, "truncated octree");
219 +        default:
220 +                octerror(USER, "damaged octree");
221 +        }
222 + }
223 +
224 +
225 + static
226   getobj()                                /* get next object */
227   {
228          char  sbuf[MAXSTR];
229          int  obj;
230          register int  i;
231 +        register long  m;
232          register OBJREC  *objp;
233  
234 <        i = getint(1);
234 >        i = ogetint(1);
235          if (i == -1)
236                  return(OVOID);          /* terminator */
237          if ((obj = newobject()) == OVOID)
# Line 204 | Line 239 | getobj()                               /* get next object */
239          objp = objptr(obj);
240          if ((objp->otype = otypmap[i]) < 0)
241                  octerror(USER, "reference to unknown type");
242 <        if ((objp->omod = getint(sizeof(OBJECT))) != OVOID)
243 <                objp->omod += objorig;
244 <        objp->oname = savqstr(getstr(sbuf));
245 <        if (objp->oargs.nsargs = getint(2)) {
242 >        if ((m = ogetint(objsize)) != OVOID) {
243 >                m += objorig;
244 >                if ((OBJECT)m != m)
245 >                        octerror(USER, "too many objects");
246 >        }
247 >        objp->omod = m;
248 >        objp->oname = savqstr(ogetstr(sbuf));
249 >        if (objp->oargs.nsargs = ogetint(2)) {
250                  objp->oargs.sarg = (char **)bmalloc
251                                  (objp->oargs.nsargs*sizeof(char *));
252                  if (objp->oargs.sarg == NULL)
253                          goto memerr;
254                  for (i = 0; i < objp->oargs.nsargs; i++)
255 <                        objp->oargs.sarg[i] = savestr(getstr(sbuf));
255 >                        objp->oargs.sarg[i] = savestr(ogetstr(sbuf));
256          } else
257                  objp->oargs.sarg = NULL;
258   #ifdef  IARGS
259 <        if (objp->oargs.niargs = getint(2)) {
259 >        if (objp->oargs.niargs = ogetint(2)) {
260                  objp->oargs.iarg = (long *)bmalloc
261                                  (objp->oargs.niargs*sizeof(long));
262                  if (objp->oargs.iarg == NULL)
263                          goto memerr;
264                  for (i = 0; i < objp->oargs.niargs; i++)
265 <                        objp->oargs.iarg[i] = getint(4);
265 >                        objp->oargs.iarg[i] = ogetint(4);
266          } else
267                  objp->oargs.iarg = NULL;
268   #endif
269 <        if (objp->oargs.nfargs = getint(2)) {
270 <                objp->oargs.farg = (double *)bmalloc
271 <                                (objp->oargs.nfargs*sizeof(double));
269 >        if (objp->oargs.nfargs = ogetint(2)) {
270 >                objp->oargs.farg = (FLOAT *)bmalloc
271 >                                (objp->oargs.nfargs*sizeof(FLOAT));
272                  if (objp->oargs.farg == NULL)
273                          goto memerr;
274                  for (i = 0; i < objp->oargs.nfargs; i++)
275 <                        objp->oargs.farg[i] = getflt();
275 >                        objp->oargs.farg[i] = ogetflt();
276          } else
277                  objp->oargs.farg = NULL;
278                                                  /* initialize */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines