1 |
greg |
2.5 |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
greg |
1.1 |
|
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 |
greg |
2.4 |
static double ogetflt(); |
22 |
|
|
static long ogetint(); |
23 |
|
|
static char *ogetstr(); |
24 |
greg |
2.10 |
static int getobj(), octerror(), skiptree(); |
25 |
greg |
1.7 |
static OCTREE getfullnode(), gettree(); |
26 |
greg |
1.1 |
|
27 |
|
|
static char *infn; /* input file name */ |
28 |
|
|
static FILE *infp; /* input file stream */ |
29 |
greg |
1.14 |
static int objsize; /* size of stored OBJECT's */ |
30 |
greg |
1.1 |
static OBJECT objorig; /* zeroeth object */ |
31 |
|
|
static short otypmap[NUMOTYPE+8]; /* object type map */ |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
int |
35 |
|
|
readoct(fname, load, scene, ofn) /* read in octree from file */ |
36 |
|
|
char *fname; |
37 |
|
|
int load; |
38 |
|
|
CUBE *scene; |
39 |
|
|
char *ofn[]; |
40 |
|
|
{ |
41 |
greg |
1.11 |
char sbuf[512]; |
42 |
greg |
1.1 |
int nf; |
43 |
greg |
2.7 |
OBJECT fnobjects; |
44 |
greg |
1.1 |
register int i; |
45 |
greg |
1.15 |
long m; |
46 |
greg |
1.1 |
|
47 |
|
|
if (fname == NULL) { |
48 |
|
|
infn = "standard input"; |
49 |
|
|
infp = stdin; |
50 |
|
|
} else { |
51 |
|
|
infn = fname; |
52 |
|
|
if ((infp = fopen(fname, "r")) == NULL) { |
53 |
|
|
sprintf(errmsg, "cannot open octree file \"%s\"", |
54 |
|
|
fname); |
55 |
|
|
error(SYSTEM, errmsg); |
56 |
|
|
} |
57 |
|
|
} |
58 |
greg |
2.7 |
#ifdef MSDOS |
59 |
|
|
setmode(fileno(infp), O_BINARY); |
60 |
|
|
#endif |
61 |
greg |
1.1 |
/* get header */ |
62 |
greg |
1.12 |
if (checkheader(infp, OCTFMT, load&IO_INFO ? stdout : NULL) < 0) |
63 |
|
|
octerror(USER, "not an octree"); |
64 |
greg |
1.1 |
/* check format */ |
65 |
greg |
2.4 |
if ((objsize = ogetint(2)-OCTMAGIC) <= 0 || |
66 |
greg |
1.14 |
objsize > MAXOBJSIZ || objsize > sizeof(long)) |
67 |
greg |
1.12 |
octerror(USER, "incompatible octree format"); |
68 |
greg |
1.1 |
/* get boundaries */ |
69 |
|
|
if (load & IO_BOUNDS) { |
70 |
|
|
for (i = 0; i < 3; i++) |
71 |
greg |
2.4 |
scene->cuorg[i] = atof(ogetstr(sbuf)); |
72 |
|
|
scene->cusize = atof(ogetstr(sbuf)); |
73 |
greg |
1.1 |
} else { |
74 |
|
|
for (i = 0; i < 4; i++) |
75 |
greg |
2.4 |
ogetstr(sbuf); |
76 |
greg |
1.1 |
} |
77 |
|
|
objorig = nobjects; /* set object offset */ |
78 |
|
|
nf = 0; /* get object files */ |
79 |
greg |
2.4 |
while (*ogetstr(sbuf)) { |
80 |
greg |
1.1 |
if (load & IO_SCENE) |
81 |
|
|
readobj(sbuf); |
82 |
|
|
if (load & IO_FILES) |
83 |
|
|
ofn[nf] = savqstr(sbuf); |
84 |
|
|
nf++; |
85 |
|
|
} |
86 |
|
|
if (load & IO_FILES) |
87 |
|
|
ofn[nf] = NULL; |
88 |
greg |
1.8 |
/* get number of objects */ |
89 |
greg |
2.4 |
fnobjects = m = ogetint(objsize); |
90 |
greg |
1.15 |
if (fnobjects != m) |
91 |
|
|
octerror(USER, "too many objects"); |
92 |
greg |
1.1 |
|
93 |
greg |
2.5 |
if (load & IO_TREE) /* get the octree */ |
94 |
greg |
1.1 |
scene->cutree = gettree(); |
95 |
greg |
2.5 |
else if (load & IO_SCENE && nf == 0) |
96 |
|
|
skiptree(); |
97 |
|
|
|
98 |
|
|
if (load & IO_SCENE) /* get the scene */ |
99 |
|
|
if (nf == 0) { |
100 |
|
|
for (i = 0; *ogetstr(sbuf); i++) |
101 |
|
|
if ((otypmap[i] = otype(sbuf)) < 0) { |
102 |
|
|
sprintf(errmsg, "unknown type \"%s\"", sbuf); |
103 |
|
|
octerror(WARNING, errmsg); |
104 |
|
|
} |
105 |
|
|
while (getobj() != OVOID) |
106 |
|
|
; |
107 |
|
|
} else { /* consistency checks */ |
108 |
|
|
/* check object count */ |
109 |
|
|
if (nobjects != objorig+fnobjects) |
110 |
|
|
octerror(USER, "bad object count; octree stale?"); |
111 |
|
|
/* check for non-surfaces */ |
112 |
|
|
if (nonsurfinset(objorig, fnobjects)) |
113 |
|
|
octerror(USER, "modifier in tree; octree stale?"); |
114 |
|
|
} |
115 |
greg |
1.1 |
fclose(infp); |
116 |
|
|
return(nf); |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
|
120 |
|
|
static char * |
121 |
greg |
2.4 |
ogetstr(s) /* get null-terminated string */ |
122 |
greg |
1.1 |
char *s; |
123 |
|
|
{ |
124 |
greg |
2.4 |
extern char *getstr(); |
125 |
greg |
1.1 |
|
126 |
greg |
2.4 |
if (getstr(s, infp) == NULL) |
127 |
|
|
octerror(USER, "truncated octree"); |
128 |
|
|
return(s); |
129 |
greg |
1.1 |
} |
130 |
|
|
|
131 |
|
|
|
132 |
|
|
static OCTREE |
133 |
|
|
getfullnode() /* get a set, return fullnode */ |
134 |
|
|
{ |
135 |
greg |
2.7 |
OBJECT set[MAXSET+1]; |
136 |
greg |
1.1 |
register int i; |
137 |
greg |
1.14 |
register long m; |
138 |
greg |
1.1 |
|
139 |
greg |
2.4 |
if ((set[0] = ogetint(objsize)) > MAXSET) |
140 |
greg |
1.1 |
octerror(USER, "bad set in getfullnode"); |
141 |
greg |
1.14 |
for (i = 1; i <= set[0]; i++) { |
142 |
greg |
2.4 |
m = ogetint(objsize) + objorig; |
143 |
greg |
1.15 |
if ((set[i] = m) != m) |
144 |
greg |
1.14 |
octerror(USER, "too many objects"); |
145 |
|
|
} |
146 |
greg |
1.1 |
return(fullnode(set)); |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
|
150 |
|
|
static long |
151 |
greg |
2.4 |
ogetint(siz) /* get a siz-byte integer */ |
152 |
|
|
int siz; |
153 |
greg |
1.1 |
{ |
154 |
greg |
2.4 |
extern long getint(); |
155 |
greg |
1.3 |
register long r; |
156 |
greg |
1.1 |
|
157 |
greg |
2.4 |
r = getint(siz, infp); |
158 |
|
|
if (feof(infp)) |
159 |
|
|
octerror(USER, "truncated octree"); |
160 |
greg |
1.1 |
return(r); |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
|
164 |
|
|
static double |
165 |
greg |
2.4 |
ogetflt() /* get a floating point number */ |
166 |
greg |
1.1 |
{ |
167 |
greg |
2.4 |
extern double getflt(); |
168 |
greg |
2.7 |
double r; |
169 |
greg |
1.1 |
|
170 |
greg |
2.4 |
r = getflt(infp); |
171 |
|
|
if (feof(infp)) |
172 |
|
|
octerror(USER, "truncated octree"); |
173 |
|
|
return(r); |
174 |
greg |
1.1 |
} |
175 |
|
|
|
176 |
|
|
|
177 |
|
|
static OCTREE |
178 |
|
|
gettree() /* get a pre-ordered octree */ |
179 |
|
|
{ |
180 |
greg |
2.7 |
register OCTREE ot; |
181 |
greg |
1.1 |
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 |
greg |
2.5 |
case EOF: |
195 |
|
|
octerror(USER, "truncated octree"); |
196 |
|
|
default: |
197 |
|
|
octerror(USER, "damaged octree"); |
198 |
|
|
} |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
|
202 |
|
|
static |
203 |
|
|
skiptree() /* skip octree on input */ |
204 |
|
|
{ |
205 |
|
|
register int i; |
206 |
|
|
|
207 |
|
|
switch (getc(infp)) { |
208 |
|
|
case OT_EMPTY: |
209 |
|
|
return; |
210 |
|
|
case OT_FULL: |
211 |
greg |
2.6 |
for (i = ogetint(objsize)*objsize; i-- > 0; ) |
212 |
|
|
if (getc(infp) == EOF) |
213 |
|
|
octerror(USER, "truncated octree"); |
214 |
greg |
2.5 |
return; |
215 |
|
|
case OT_TREE: |
216 |
|
|
for (i = 0; i < 8; i++) |
217 |
|
|
skiptree(); |
218 |
|
|
return; |
219 |
greg |
1.1 |
case EOF: |
220 |
|
|
octerror(USER, "truncated octree"); |
221 |
|
|
default: |
222 |
|
|
octerror(USER, "damaged octree"); |
223 |
|
|
} |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
|
227 |
|
|
static |
228 |
|
|
getobj() /* get next object */ |
229 |
|
|
{ |
230 |
|
|
char sbuf[MAXSTR]; |
231 |
|
|
int obj; |
232 |
|
|
register int i; |
233 |
greg |
1.14 |
register long m; |
234 |
greg |
2.7 |
register OBJREC *objp; |
235 |
greg |
1.1 |
|
236 |
greg |
2.4 |
i = ogetint(1); |
237 |
greg |
1.3 |
if (i == -1) |
238 |
greg |
1.1 |
return(OVOID); /* terminator */ |
239 |
|
|
if ((obj = newobject()) == OVOID) |
240 |
|
|
error(SYSTEM, "out of object space"); |
241 |
|
|
objp = objptr(obj); |
242 |
|
|
if ((objp->otype = otypmap[i]) < 0) |
243 |
|
|
octerror(USER, "reference to unknown type"); |
244 |
greg |
2.4 |
if ((m = ogetint(objsize)) != OVOID) { |
245 |
greg |
1.14 |
m += objorig; |
246 |
|
|
if ((OBJECT)m != m) |
247 |
|
|
octerror(USER, "too many objects"); |
248 |
|
|
} |
249 |
|
|
objp->omod = m; |
250 |
greg |
2.4 |
objp->oname = savqstr(ogetstr(sbuf)); |
251 |
|
|
if (objp->oargs.nsargs = ogetint(2)) { |
252 |
greg |
1.1 |
objp->oargs.sarg = (char **)bmalloc |
253 |
|
|
(objp->oargs.nsargs*sizeof(char *)); |
254 |
|
|
if (objp->oargs.sarg == NULL) |
255 |
|
|
goto memerr; |
256 |
|
|
for (i = 0; i < objp->oargs.nsargs; i++) |
257 |
greg |
2.4 |
objp->oargs.sarg[i] = savestr(ogetstr(sbuf)); |
258 |
greg |
1.1 |
} else |
259 |
|
|
objp->oargs.sarg = NULL; |
260 |
greg |
2.7 |
#ifdef IARGS |
261 |
greg |
2.4 |
if (objp->oargs.niargs = ogetint(2)) { |
262 |
greg |
1.1 |
objp->oargs.iarg = (long *)bmalloc |
263 |
|
|
(objp->oargs.niargs*sizeof(long)); |
264 |
|
|
if (objp->oargs.iarg == NULL) |
265 |
|
|
goto memerr; |
266 |
|
|
for (i = 0; i < objp->oargs.niargs; i++) |
267 |
greg |
2.4 |
objp->oargs.iarg[i] = ogetint(4); |
268 |
greg |
1.1 |
} else |
269 |
|
|
objp->oargs.iarg = NULL; |
270 |
|
|
#endif |
271 |
greg |
2.4 |
if (objp->oargs.nfargs = ogetint(2)) { |
272 |
greg |
1.15 |
objp->oargs.farg = (FLOAT *)bmalloc |
273 |
|
|
(objp->oargs.nfargs*sizeof(FLOAT)); |
274 |
greg |
1.1 |
if (objp->oargs.farg == NULL) |
275 |
|
|
goto memerr; |
276 |
|
|
for (i = 0; i < objp->oargs.nfargs; i++) |
277 |
greg |
2.4 |
objp->oargs.farg[i] = ogetflt(); |
278 |
greg |
1.1 |
} else |
279 |
|
|
objp->oargs.farg = NULL; |
280 |
|
|
/* initialize */ |
281 |
|
|
objp->os = NULL; |
282 |
|
|
/* insert */ |
283 |
|
|
insertobject(obj); |
284 |
|
|
return(obj); |
285 |
|
|
memerr: |
286 |
|
|
error(SYSTEM, "out of memory in getobj"); |
287 |
|
|
} |
288 |
|
|
|
289 |
|
|
|
290 |
|
|
static |
291 |
|
|
octerror(etyp, msg) /* octree error */ |
292 |
|
|
int etyp; |
293 |
|
|
char *msg; |
294 |
|
|
{ |
295 |
|
|
char msgbuf[128]; |
296 |
|
|
|
297 |
|
|
sprintf(msgbuf, "(%s): %s", infn, msg); |
298 |
|
|
error(etyp, msgbuf); |
299 |
|
|
} |