ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/readoct.c
Revision: 1.7
Committed: Mon Mar 12 10:39:17 1990 UTC (34 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.6: +5 -5 lines
Log Message:
fixed predeclarations for fussy compilers

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