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 |
double atof(); |
22 |
double getflt(); |
23 |
long getint(); |
24 |
char *getstr(); |
25 |
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, "bad octree"); |
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 positive integer */ |
137 |
register int siz; |
138 |
{ |
139 |
register int c; |
140 |
register long r = 0L; |
141 |
|
142 |
while (siz--) { |
143 |
if ((c = getc(infp)) == EOF) |
144 |
octerror(USER, "truncated octree"); |
145 |
r <<= 8; |
146 |
r |= c; |
147 |
} |
148 |
return(r); |
149 |
} |
150 |
|
151 |
|
152 |
static double |
153 |
getflt() /* get a floating point number */ |
154 |
{ |
155 |
extern double ldexp(); |
156 |
double d; |
157 |
|
158 |
d = (double)getint(sizeof(long))/0x7fffffff; |
159 |
return(ldexp(d, (char)getint(1))); /* sign extend */ |
160 |
} |
161 |
|
162 |
|
163 |
static OCTREE |
164 |
gettree() /* get a pre-ordered octree */ |
165 |
{ |
166 |
register OCTREE ot; |
167 |
register int i; |
168 |
|
169 |
switch (getc(infp)) { |
170 |
case OT_EMPTY: |
171 |
return(EMPTY); |
172 |
case OT_FULL: |
173 |
return(getfullnode()); |
174 |
case OT_TREE: |
175 |
if ((ot = octalloc()) == EMPTY) |
176 |
octerror(SYSTEM, "out of tree space in gettree"); |
177 |
for (i = 0; i < 8; i++) |
178 |
octkid(ot, i) = gettree(); |
179 |
return(ot); |
180 |
case EOF: |
181 |
octerror(USER, "truncated octree"); |
182 |
default: |
183 |
octerror(USER, "damaged octree"); |
184 |
} |
185 |
} |
186 |
|
187 |
|
188 |
static |
189 |
getobj() /* get next object */ |
190 |
{ |
191 |
char sbuf[MAXSTR]; |
192 |
int obj; |
193 |
register int i; |
194 |
register OBJREC *objp; |
195 |
|
196 |
i = (char)getint(1); /* sign extend */ |
197 |
if (i < 0) |
198 |
return(OVOID); /* terminator */ |
199 |
if ((obj = newobject()) == OVOID) |
200 |
error(SYSTEM, "out of object space"); |
201 |
objp = objptr(obj); |
202 |
if ((objp->otype = otypmap[i]) < 0) |
203 |
octerror(USER, "reference to unknown type"); |
204 |
if ((objp->omod = getint(sizeof(OBJECT))) != OVOID) |
205 |
objp->omod += objorig; |
206 |
objp->oname = savqstr(getstr(sbuf)); |
207 |
if (objp->oargs.nsargs = getint(2)) { |
208 |
objp->oargs.sarg = (char **)bmalloc |
209 |
(objp->oargs.nsargs*sizeof(char *)); |
210 |
if (objp->oargs.sarg == NULL) |
211 |
goto memerr; |
212 |
for (i = 0; i < objp->oargs.nsargs; i++) |
213 |
objp->oargs.sarg[i] = savestr(getstr(sbuf)); |
214 |
} else |
215 |
objp->oargs.sarg = NULL; |
216 |
#ifdef IARGS |
217 |
if (objp->oargs.niargs = getint(2)) { |
218 |
objp->oargs.iarg = (long *)bmalloc |
219 |
(objp->oargs.niargs*sizeof(long)); |
220 |
if (objp->oargs.iarg == NULL) |
221 |
goto memerr; |
222 |
for (i = 0; i < objp->oargs.niargs; i++) |
223 |
objp->oargs.iarg[i] = getint(sizeof(long)); |
224 |
} else |
225 |
objp->oargs.iarg = NULL; |
226 |
#endif |
227 |
if (objp->oargs.nfargs = getint(2)) { |
228 |
objp->oargs.farg = (double *)bmalloc |
229 |
(objp->oargs.nfargs*sizeof(double)); |
230 |
if (objp->oargs.farg == NULL) |
231 |
goto memerr; |
232 |
for (i = 0; i < objp->oargs.nfargs; i++) |
233 |
objp->oargs.farg[i] = getflt(); |
234 |
} else |
235 |
objp->oargs.farg = NULL; |
236 |
/* initialize */ |
237 |
objp->os = NULL; |
238 |
objp->lastrno = -1; |
239 |
/* insert */ |
240 |
insertobject(obj); |
241 |
return(obj); |
242 |
memerr: |
243 |
error(SYSTEM, "out of memory in getobj"); |
244 |
} |
245 |
|
246 |
|
247 |
static |
248 |
octerror(etyp, msg) /* octree error */ |
249 |
int etyp; |
250 |
char *msg; |
251 |
{ |
252 |
char msgbuf[128]; |
253 |
|
254 |
sprintf(msgbuf, "(%s): %s", infn, msg); |
255 |
error(etyp, msgbuf); |
256 |
} |