ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/readoct.c
Revision: 1.9
Committed: Mon Sep 10 10:01:21 1990 UTC (34 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.8: +2 -2 lines
Log Message:
minor change in stale octree error reporting

File Contents

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