1 |
– |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* oconv.c - main program for object to octree conversion. |
6 |
|
* |
15 |
|
|
16 |
|
#include "otypes.h" |
17 |
|
|
18 |
< |
#define OMARGIN (10*FTINY) /* margin around global cube */ |
18 |
> |
#include "paths.h" |
19 |
|
|
20 |
< |
#define MAXOBJFIL 63 /* maximum number of scene files */ |
20 |
> |
#define OMARGIN (10*FTINY) /* margin around global cube */ |
21 |
|
|
22 |
+ |
#define MAXOBJFIL 127 /* maximum number of scene files */ |
23 |
+ |
|
24 |
|
char *progname; /* argv[0] */ |
25 |
|
|
27 |
– |
char *libpath; /* library search path */ |
28 |
– |
|
26 |
|
int nowarn = 0; /* supress warnings? */ |
27 |
|
|
28 |
< |
int objlim = 5; /* # of objects before split */ |
28 |
> |
int objlim = 6; /* # of objects before split */ |
29 |
|
|
30 |
< |
int resolu = 1024; /* octree resolution limit */ |
30 |
> |
int resolu = 16384; /* octree resolution limit */ |
31 |
|
|
32 |
|
CUBE thescene = {EMPTY, {0.0, 0.0, 0.0}, 0.0}; /* our scene */ |
33 |
|
|
34 |
|
char *ofname[MAXOBJFIL+1]; /* object file names */ |
35 |
|
int nfiles = 0; /* number of object files */ |
36 |
|
|
37 |
< |
double mincusize; /* minimum cube size from resolu */ |
37 |
> |
double mincusize; /* minimum cube size from resolu */ |
38 |
|
|
39 |
< |
int (*addobjnotify[])() = {NULL}; /* new object notifier functions */ |
39 |
> |
void (*addobjnotify[])() = {NULL}; /* new object notifier functions */ |
40 |
|
|
41 |
|
|
42 |
|
main(argc, argv) /* convert object files to an octree */ |
43 |
|
int argc; |
44 |
< |
char **argv; |
44 |
> |
char *argv[]; |
45 |
|
{ |
49 |
– |
char *getenv(); |
50 |
– |
double atof(); |
46 |
|
FVECT bbmin, bbmax; |
47 |
|
char *infile = NULL; |
48 |
+ |
int inpfrozen = 0; |
49 |
|
int outflags = IO_ALL; |
50 |
< |
OBJECT startobj; |
50 |
> |
OBJECT startobj; |
51 |
|
int i; |
52 |
|
|
53 |
< |
progname = argv[0]; |
53 |
> |
progname = argv[0] = fixargv0(argv[0]); |
54 |
|
|
55 |
< |
if ((libpath = getenv("RAYPATH")) == NULL) |
60 |
< |
libpath = ":/usr/local/lib/ray"; |
55 |
> |
initotypes(); |
56 |
|
|
57 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
58 |
|
switch (argv[i][1]) { |
85 |
|
break; |
86 |
|
} |
87 |
|
breakopt: |
88 |
+ |
#ifdef MSDOS |
89 |
+ |
setmode(fileno(stdout), O_BINARY); |
90 |
+ |
#endif |
91 |
|
if (infile != NULL) { /* get old octree & objects */ |
92 |
|
if (thescene.cusize > FTINY) |
93 |
|
error(USER, "only one of '-b' or '-i'"); |
94 |
|
nfiles = readoct(infile, IO_ALL, &thescene, ofname); |
95 |
< |
if (nfiles == 0 && outflags & IO_FILES) { |
96 |
< |
error(WARNING, "frozen octree"); |
97 |
< |
outflags &= ~IO_FILES; |
98 |
< |
} |
99 |
< |
} |
100 |
< |
|
103 |
< |
printargs(argc, argv, stdout); /* info. header */ |
95 |
> |
if (nfiles == 0) |
96 |
> |
inpfrozen++; |
97 |
> |
} else |
98 |
> |
newheader("RADIANCE", stdout); /* new binary file header */ |
99 |
> |
printargs(argc, argv, stdout); |
100 |
> |
fputformat(OCTFMT, stdout); |
101 |
|
printf("\n"); |
102 |
|
|
103 |
|
startobj = nobjects; /* previous objects already converted */ |
104 |
< |
|
104 |
> |
|
105 |
|
for ( ; i < argc; i++) /* read new scene descriptions */ |
106 |
|
if (!strcmp(argv[i], "-")) { /* from stdin */ |
107 |
|
readobj(NULL); |
113 |
|
} |
114 |
|
|
115 |
|
ofname[nfiles] = NULL; |
116 |
+ |
|
117 |
+ |
if (inpfrozen && outflags & IO_FILES) { |
118 |
+ |
error(WARNING, "frozen octree"); |
119 |
+ |
outflags &= ~IO_FILES; |
120 |
+ |
} |
121 |
|
/* find bounding box */ |
122 |
|
bbmin[0] = bbmin[1] = bbmin[2] = FHUGE; |
123 |
|
bbmax[0] = bbmax[1] = bbmax[2] = -FHUGE; |
145 |
|
} |
146 |
|
|
147 |
|
mincusize = thescene.cusize / resolu - FTINY; |
148 |
< |
|
148 |
> |
|
149 |
|
for (i = startobj; i < nobjects; i++) /* add new objects */ |
150 |
|
addobject(&thescene, i); |
151 |
< |
|
151 |
> |
|
152 |
|
thescene.cutree = combine(thescene.cutree); /* optimize */ |
153 |
|
|
154 |
|
writeoct(outflags, &thescene, ofname); /* write structures to stdout */ |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
+ |
void |
161 |
|
quit(code) /* exit program */ |
162 |
|
int code; |
163 |
|
{ |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
+ |
void |
169 |
|
cputs() /* interactive error */ |
170 |
|
{ |
171 |
|
/* referenced, but not used */ |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
+ |
void |
176 |
|
wputs(s) /* warning message */ |
177 |
|
char *s; |
178 |
|
{ |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
+ |
void |
185 |
|
eputs(s) /* put string to stderr */ |
186 |
|
register char *s; |
187 |
|
{ |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
+ |
#define bitop(f,i,op) (f[((i)>>3)] op (1<<((i)&7))) |
201 |
+ |
#define tstbit(f,i) bitop(f,i,&) |
202 |
+ |
#define setbit(f,i) bitop(f,i,|=) |
203 |
+ |
#define clrbit(f,i) bitop(f,i,&=~) |
204 |
+ |
#define tglbit(f,i) bitop(f,i,^=) |
205 |
+ |
|
206 |
+ |
|
207 |
|
addobject(cu, obj) /* add an object to a cube */ |
208 |
|
register CUBE *cu; |
209 |
< |
OBJECT obj; |
209 |
> |
OBJECT obj; |
210 |
|
{ |
211 |
< |
CUBE cukid; |
199 |
< |
OCTREE ot; |
200 |
< |
OBJECT oset[MAXSET+1]; |
201 |
< |
int in; |
202 |
< |
register int i, j; |
211 |
> |
int inc; |
212 |
|
|
213 |
< |
in = (*ofun[objptr(obj)->otype].funp)(objptr(obj), cu); |
213 |
> |
inc = (*ofun[objptr(obj)->otype].funp)(objptr(obj), cu); |
214 |
|
|
215 |
< |
if (in == O_MISS) |
215 |
> |
if (inc == O_MISS) |
216 |
|
return; /* no intersection */ |
217 |
< |
|
217 |
> |
|
218 |
|
if (istree(cu->cutree)) { |
219 |
< |
/* do children */ |
219 |
> |
CUBE cukid; /* do children */ |
220 |
> |
int i, j; |
221 |
|
cukid.cusize = cu->cusize * 0.5; |
222 |
|
for (i = 0; i < 8; i++) { |
223 |
|
cukid.cutree = octkid(cu->cutree, i); |
229 |
|
addobject(&cukid, obj); |
230 |
|
octkid(cu->cutree, i) = cukid.cutree; |
231 |
|
} |
232 |
< |
|
233 |
< |
} else if (isempty(cu->cutree)) { |
234 |
< |
/* singular set */ |
232 |
> |
return; |
233 |
> |
} |
234 |
> |
if (isempty(cu->cutree)) { |
235 |
> |
OBJECT oset[2]; /* singular set */ |
236 |
|
oset[0] = 1; oset[1] = obj; |
237 |
|
cu->cutree = fullnode(oset); |
238 |
< |
|
239 |
< |
} else { |
240 |
< |
/* add to full node */ |
241 |
< |
objset(oset, cu->cutree); |
242 |
< |
cukid.cusize = cu->cusize * 0.5; |
232 |
< |
|
233 |
< |
if (in==O_IN || oset[0] < objlim || cukid.cusize < mincusize) { |
234 |
< |
/* add to set */ |
235 |
< |
if (oset[0] >= MAXSET) { |
236 |
< |
sprintf(errmsg, |
237 |
< |
"set overflow in addobject (%s)", |
238 |
< |
objptr(obj)->oname); |
239 |
< |
error(INTERNAL, errmsg); |
240 |
< |
} |
241 |
< |
insertelem(oset, obj); |
242 |
< |
cu->cutree = fullnode(oset); |
238 |
> |
return; |
239 |
> |
} |
240 |
> |
/* add to full node */ |
241 |
> |
add2full(cu, obj, inc); |
242 |
> |
} |
243 |
|
|
244 |
< |
} else { |
245 |
< |
/* subdivide cube */ |
246 |
< |
if ((ot = octalloc()) == EMPTY) |
247 |
< |
error(SYSTEM, "out of octree space"); |
248 |
< |
for (i = 0; i < 8; i++) { |
249 |
< |
cukid.cutree = EMPTY; |
250 |
< |
for (j = 0; j < 3; j++) { |
251 |
< |
cukid.cuorg[j] = cu->cuorg[j]; |
252 |
< |
if ((1<<j) & i) |
253 |
< |
cukid.cuorg[j] += cukid.cusize; |
254 |
< |
} |
255 |
< |
/* surfaces first */ |
256 |
< |
for (j = 1; j <= oset[0]; j++) |
257 |
< |
if (!isvolume(objptr(oset[j])->otype)) |
258 |
< |
addobject(&cukid, oset[j]); |
259 |
< |
/* then this object */ |
260 |
< |
addobject(&cukid, obj); |
261 |
< |
/* volumes last */ |
262 |
< |
for (j = 1; j <= oset[0]; j++) |
263 |
< |
if (isvolume(objptr(oset[j])->otype)) |
264 |
< |
addobject(&cukid, oset[j]); |
265 |
< |
octkid(ot, i) = cukid.cutree; |
266 |
< |
} |
267 |
< |
cu->cutree = ot; |
244 |
> |
|
245 |
> |
add2full(cu, obj, inc) /* add object to full node */ |
246 |
> |
register CUBE *cu; |
247 |
> |
OBJECT obj; |
248 |
> |
int inc; |
249 |
> |
{ |
250 |
> |
OCTREE ot; |
251 |
> |
OBJECT oset[MAXSET+1]; |
252 |
> |
CUBE cukid; |
253 |
> |
unsigned char inflg[(MAXSET+7)/8], volflg[(MAXSET+7)/8]; |
254 |
> |
register int i, j; |
255 |
> |
|
256 |
> |
objset(oset, cu->cutree); |
257 |
> |
cukid.cusize = cu->cusize * 0.5; |
258 |
> |
|
259 |
> |
if (inc==O_IN || oset[0] < objlim || cukid.cusize < mincusize) { |
260 |
> |
/* add to set */ |
261 |
> |
if (oset[0] >= MAXSET) { |
262 |
> |
sprintf(errmsg, "set overflow in addobject (%s)", |
263 |
> |
objptr(obj)->oname); |
264 |
> |
error(INTERNAL, errmsg); |
265 |
|
} |
266 |
+ |
insertelem(oset, obj); |
267 |
+ |
cu->cutree = fullnode(oset); |
268 |
+ |
return; |
269 |
|
} |
270 |
+ |
/* subdivide cube */ |
271 |
+ |
if ((ot = octalloc()) == EMPTY) |
272 |
+ |
error(SYSTEM, "out of octree space"); |
273 |
+ |
/* mark volumes */ |
274 |
+ |
j = (oset[0]+7)>>3; |
275 |
+ |
while (j--) |
276 |
+ |
volflg[j] = inflg[j] = 0; |
277 |
+ |
for (j = 1; j <= oset[0]; j++) |
278 |
+ |
if (isvolume(objptr(oset[j])->otype)) { |
279 |
+ |
setbit(volflg,j-1); |
280 |
+ |
if ((*ofun[objptr(oset[j])->otype].funp) |
281 |
+ |
(objptr(oset[j]), cu) == O_IN) |
282 |
+ |
setbit(inflg,j-1); |
283 |
+ |
} |
284 |
+ |
/* assign subcubes */ |
285 |
+ |
for (i = 0; i < 8; i++) { |
286 |
+ |
cukid.cutree = EMPTY; |
287 |
+ |
for (j = 0; j < 3; j++) { |
288 |
+ |
cukid.cuorg[j] = cu->cuorg[j]; |
289 |
+ |
if ((1<<j) & i) |
290 |
+ |
cukid.cuorg[j] += cukid.cusize; |
291 |
+ |
} |
292 |
+ |
/* surfaces first */ |
293 |
+ |
for (j = 1; j <= oset[0]; j++) |
294 |
+ |
if (!tstbit(volflg,j-1)) |
295 |
+ |
addobject(&cukid, oset[j]); |
296 |
+ |
/* then this object */ |
297 |
+ |
addobject(&cukid, obj); |
298 |
+ |
/* then partial volumes */ |
299 |
+ |
for (j = 1; j <= oset[0]; j++) |
300 |
+ |
if (tstbit(volflg,j-1) && |
301 |
+ |
!tstbit(inflg,j-1)) |
302 |
+ |
addobject(&cukid, oset[j]); |
303 |
+ |
/* full volumes last */ |
304 |
+ |
for (j = 1; j <= oset[0]; j++) |
305 |
+ |
if (tstbit(inflg,j-1)) |
306 |
+ |
addobject(&cukid, oset[j]); |
307 |
+ |
/* returned node */ |
308 |
+ |
octkid(ot, i) = cukid.cutree; |
309 |
+ |
} |
310 |
+ |
cu->cutree = ot; |
311 |
|
} |