ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/readoct.c
Revision: 1.13
Committed: Mon Aug 5 08:58:52 1991 UTC (32 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.12: +8 -10 lines
Log Message:
fixed small logic problem

File Contents

# Content
1 /* Copyright (c) 1991 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * readoct.c - routines to read octree information.
9 *
10 * 7/30/85
11 */
12
13 #include "standard.h"
14
15 #include "octree.h"
16
17 #include "object.h"
18
19 #include "otypes.h"
20
21 extern double atof();
22 static double getflt();
23 static long getint();
24 static char *getstr();
25 static OCTREE getfullnode(), gettree();
26
27 static char *infn; /* input file name */
28 static FILE *infp; /* input file stream */
29 static OBJECT objorig; /* zeroeth object */
30 static short otypmap[NUMOTYPE+8]; /* object type map */
31
32
33 int
34 readoct(fname, load, scene, ofn) /* read in octree from file */
35 char *fname;
36 int load;
37 CUBE *scene;
38 char *ofn[];
39 {
40 extern int fputs();
41 char sbuf[512];
42 int nf;
43 OBJECT fnobjects;
44 register int i;
45
46 if (fname == NULL) {
47 infn = "standard input";
48 infp = stdin;
49 } else {
50 infn = fname;
51 if ((infp = fopen(fname, "r")) == NULL) {
52 sprintf(errmsg, "cannot open octree file \"%s\"",
53 fname);
54 error(SYSTEM, errmsg);
55 }
56 }
57 /* get header */
58 if (checkheader(infp, OCTFMT, load&IO_INFO ? stdout : NULL) < 0)
59 octerror(USER, "not an octree");
60 /* check format */
61 if (getint(2) != OCTMAGIC)
62 octerror(USER, "incompatible octree format");
63 /* get boundaries */
64 if (load & IO_BOUNDS) {
65 for (i = 0; i < 3; i++)
66 scene->cuorg[i] = atof(getstr(sbuf));
67 scene->cusize = atof(getstr(sbuf));
68 } else {
69 for (i = 0; i < 4; i++)
70 getstr(sbuf);
71 }
72 objorig = nobjects; /* set object offset */
73 nf = 0; /* get object files */
74 while (*getstr(sbuf)) {
75 if (load & IO_SCENE)
76 readobj(sbuf);
77 if (load & IO_FILES)
78 ofn[nf] = savqstr(sbuf);
79 nf++;
80 }
81 if (load & IO_FILES)
82 ofn[nf] = NULL;
83 /* get number of objects */
84 fnobjects = getint(sizeof(OBJECT));
85
86 if (load & IO_TREE) {
87 /* get the octree */
88 scene->cutree = gettree();
89 /* get the scene */
90 if (nf == 0 && load & IO_SCENE) {
91 for (i = 0; *getstr(sbuf); i++)
92 if ((otypmap[i] = otype(sbuf)) < 0) {
93 sprintf(errmsg, "unknown type \"%s\"",
94 sbuf);
95 octerror(WARNING, errmsg);
96 }
97 while (getobj() != OVOID)
98 ;
99 } else if (load & IO_SCENE) { /* consistency checks */
100 /* check object count */
101 if (nobjects != objorig+fnobjects)
102 octerror(USER, "bad object count; octree stale?");
103 /* check for non-surfaces */
104 if (nonsurfinset(objorig, fnobjects))
105 octerror(USER, "non-surface in set; octree stale?");
106 }
107 }
108 fclose(infp);
109 return(nf);
110 }
111
112
113 static char *
114 getstr(s) /* get null-terminated string */
115 char *s;
116 {
117 register char *cp;
118 register int c;
119
120 cp = s;
121 while ((c = getc(infp)) != EOF)
122 if ((*cp++ = c) == '\0')
123 return(s);
124
125 octerror(USER, "truncated octree");
126 }
127
128
129 static OCTREE
130 getfullnode() /* get a set, return fullnode */
131 {
132 OBJECT set[MAXSET+1];
133 register int i;
134
135 set[0] = getint(sizeof(OBJECT));
136 if (set[0] > MAXSET)
137 octerror(USER, "bad set in getfullnode");
138 for (i = 1; i <= set[0]; i++)
139 set[i] = getint(sizeof(OBJECT)) + objorig;
140 return(fullnode(set));
141 }
142
143
144 static long
145 getint(siz) /* get a siz-byte integer */
146 register int siz;
147 {
148 register int c;
149 register long r;
150
151 if ((c = getc(infp)) == EOF)
152 goto end_file;
153 r = 0x80&c ? -1<<8|c : c; /* sign extend */
154 while (--siz > 0) {
155 if ((c = getc(infp)) == EOF)
156 goto end_file;
157 r <<= 8;
158 r |= c;
159 }
160 return(r);
161 end_file:
162 octerror(USER, "truncated octree");
163 }
164
165
166 static double
167 getflt() /* get a floating point number */
168 {
169 extern double ldexp();
170 double d;
171
172 d = (double)getint(4)/0x7fffffff;
173 return(ldexp(d, (int)getint(1)));
174 }
175
176
177 static OCTREE
178 gettree() /* get a pre-ordered octree */
179 {
180 register OCTREE ot;
181 register int i;
182
183 switch (getc(infp)) {
184 case OT_EMPTY:
185 return(EMPTY);
186 case OT_FULL:
187 return(getfullnode());
188 case OT_TREE:
189 if ((ot = octalloc()) == EMPTY)
190 octerror(SYSTEM, "out of tree space in gettree");
191 for (i = 0; i < 8; i++)
192 octkid(ot, i) = gettree();
193 return(ot);
194 case EOF:
195 octerror(USER, "truncated octree");
196 default:
197 octerror(USER, "damaged octree");
198 }
199 }
200
201
202 static
203 getobj() /* get next object */
204 {
205 char sbuf[MAXSTR];
206 int obj;
207 register int i;
208 register OBJREC *objp;
209
210 i = getint(1);
211 if (i == -1)
212 return(OVOID); /* terminator */
213 if ((obj = newobject()) == OVOID)
214 error(SYSTEM, "out of object space");
215 objp = objptr(obj);
216 if ((objp->otype = otypmap[i]) < 0)
217 octerror(USER, "reference to unknown type");
218 if ((objp->omod = getint(sizeof(OBJECT))) != OVOID)
219 objp->omod += objorig;
220 objp->oname = savqstr(getstr(sbuf));
221 if (objp->oargs.nsargs = getint(2)) {
222 objp->oargs.sarg = (char **)bmalloc
223 (objp->oargs.nsargs*sizeof(char *));
224 if (objp->oargs.sarg == NULL)
225 goto memerr;
226 for (i = 0; i < objp->oargs.nsargs; i++)
227 objp->oargs.sarg[i] = savestr(getstr(sbuf));
228 } else
229 objp->oargs.sarg = NULL;
230 #ifdef IARGS
231 if (objp->oargs.niargs = getint(2)) {
232 objp->oargs.iarg = (long *)bmalloc
233 (objp->oargs.niargs*sizeof(long));
234 if (objp->oargs.iarg == NULL)
235 goto memerr;
236 for (i = 0; i < objp->oargs.niargs; i++)
237 objp->oargs.iarg[i] = getint(4);
238 } else
239 objp->oargs.iarg = NULL;
240 #endif
241 if (objp->oargs.nfargs = getint(2)) {
242 objp->oargs.farg = (double *)bmalloc
243 (objp->oargs.nfargs*sizeof(double));
244 if (objp->oargs.farg == NULL)
245 goto memerr;
246 for (i = 0; i < objp->oargs.nfargs; i++)
247 objp->oargs.farg[i] = getflt();
248 } else
249 objp->oargs.farg = NULL;
250 /* initialize */
251 objp->os = NULL;
252 objp->lastrno = -1;
253 /* insert */
254 insertobject(obj);
255 return(obj);
256 memerr:
257 error(SYSTEM, "out of memory in getobj");
258 }
259
260
261 static
262 octerror(etyp, msg) /* octree error */
263 int etyp;
264 char *msg;
265 {
266 char msgbuf[128];
267
268 sprintf(msgbuf, "(%s): %s", infn, msg);
269 error(etyp, msgbuf);
270 }