39 |
|
|
40 |
|
double mincusize; /* minimum cube size from resolu */ |
41 |
|
|
42 |
+ |
int (*addobjnotify[])() = {NULL}; /* new object notifier functions */ |
43 |
|
|
44 |
+ |
|
45 |
|
main(argc, argv) /* convert object files to an octree */ |
46 |
|
int argc; |
47 |
|
char **argv; |
59 |
|
if ((libpath = getenv("RAYPATH")) == NULL) |
60 |
|
libpath = ":/usr/local/lib/ray"; |
61 |
|
|
62 |
+ |
initotypes(); |
63 |
+ |
|
64 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
65 |
|
switch (argv[i][1]) { |
66 |
+ |
case '\0': /* scene from stdin */ |
67 |
+ |
goto breakopt; |
68 |
|
case 'i': /* input octree */ |
69 |
|
infile = argv[++i]; |
70 |
|
break; |
91 |
|
error(USER, errmsg); |
92 |
|
break; |
93 |
|
} |
94 |
< |
|
94 |
> |
breakopt: |
95 |
|
if (infile != NULL) { /* get old octree & objects */ |
96 |
|
if (thescene.cusize > FTINY) |
97 |
|
error(USER, "only one of '-b' or '-i'"); |
107 |
|
|
108 |
|
startobj = nobjects; /* previous objects already converted */ |
109 |
|
|
110 |
< |
for ( ; i < argc; i++) { /* read new files */ |
111 |
< |
if (nfiles >= MAXOBJFIL) |
112 |
< |
error(INTERNAL, "too many scene files"); |
113 |
< |
readobj(ofname[nfiles++] = argv[i]); |
114 |
< |
} |
110 |
> |
for ( ; i < argc; i++) /* read new scene descriptions */ |
111 |
> |
if (!strcmp(argv[i], "-")) { /* from stdin */ |
112 |
> |
readobj(NULL); |
113 |
> |
outflags &= ~IO_FILES; |
114 |
> |
} else { /* from file */ |
115 |
> |
if (nfiles >= MAXOBJFIL) |
116 |
> |
error(INTERNAL, "too many scene files"); |
117 |
> |
readobj(ofname[nfiles++] = argv[i]); |
118 |
> |
} |
119 |
> |
|
120 |
|
ofname[nfiles] = NULL; |
121 |
|
/* find bounding box */ |
122 |
|
bbmin[0] = bbmin[1] = bbmin[2] = FHUGE; |
130 |
|
bbmin[i] -= OMARGIN; |
131 |
|
bbmax[i] += OMARGIN; |
132 |
|
} |
122 |
– |
VCOPY(thescene.cuorg, bbmin); |
133 |
|
for (i = 0; i < 3; i++) |
134 |
|
if (bbmax[i] - bbmin[i] > thescene.cusize) |
135 |
|
thescene.cusize = bbmax[i] - bbmin[i]; |
136 |
+ |
for (i = 0; i < 3; i++) |
137 |
+ |
thescene.cuorg[i] = |
138 |
+ |
(bbmax[i]+bbmin[i]-thescene.cusize)*.5; |
139 |
|
} |
140 |
|
} else { |
141 |
|
for (i = 0; i < 3; i++) |
181 |
|
eputs(s) /* put string to stderr */ |
182 |
|
register char *s; |
183 |
|
{ |
184 |
< |
static int inline = 0; |
184 |
> |
static int inln = 0; |
185 |
|
|
186 |
< |
if (!inline++) { |
186 |
> |
if (!inln++) { |
187 |
|
fputs(progname, stderr); |
188 |
|
fputs(": ", stderr); |
189 |
|
} |
190 |
|
fputs(s, stderr); |
191 |
|
if (*s && s[strlen(s)-1] == '\n') |
192 |
< |
inline = 0; |
192 |
> |
inln = 0; |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
+ |
#define bitop(f,i,op) (f[((i)>>3)] op (1<<((i)&7))) |
197 |
+ |
#define tstbit(f,i) bitop(f,i,&) |
198 |
+ |
#define setbit(f,i) bitop(f,i,|=) |
199 |
+ |
#define clrbit(f,i) bitop(f,i,&=~) |
200 |
+ |
#define tglbit(f,i) bitop(f,i,^=) |
201 |
+ |
|
202 |
+ |
|
203 |
|
addobject(cu, obj) /* add an object to a cube */ |
204 |
|
register CUBE *cu; |
205 |
|
OBJECT obj; |
207 |
|
CUBE cukid; |
208 |
|
OCTREE ot; |
209 |
|
OBJECT oset[MAXSET+1]; |
210 |
+ |
unsigned char inflg[MAXSET/8], volflg[MAXSET/8]; |
211 |
|
int in; |
212 |
|
register int i, j; |
213 |
|
|
214 |
|
in = (*ofun[objptr(obj)->otype].funp)(objptr(obj), cu); |
215 |
|
|
216 |
< |
if (!in) |
216 |
> |
if (in == O_MISS) |
217 |
|
return; /* no intersection */ |
218 |
|
|
219 |
|
if (istree(cu->cutree)) { |
229 |
|
addobject(&cukid, obj); |
230 |
|
octkid(cu->cutree, i) = cukid.cutree; |
231 |
|
} |
232 |
< |
|
233 |
< |
} else if (isempty(cu->cutree)) { |
232 |
> |
return; |
233 |
> |
} |
234 |
> |
if (isempty(cu->cutree)) { |
235 |
|
/* 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; |
243 |
< |
|
244 |
< |
if (in == 2 || oset[0] < objlim || cukid.cusize < mincusize) { |
245 |
< |
/* add to set */ |
246 |
< |
if (oset[0] >= MAXSET) { |
247 |
< |
sprintf(errmsg, |
248 |
< |
"set overflow in addobject (%s)", |
249 |
< |
objptr(obj)->oname); |
250 |
< |
error(INTERNAL, errmsg); |
229 |
< |
} |
230 |
< |
insertelem(oset, obj); |
231 |
< |
cu->cutree = fullnode(oset); |
232 |
< |
|
233 |
< |
} else { |
234 |
< |
/* subdivide cube */ |
235 |
< |
if ((ot = octalloc()) == EMPTY) |
236 |
< |
error(SYSTEM, "out of octree space"); |
237 |
< |
for (i = 0; i < 8; i++) { |
238 |
< |
cukid.cutree = EMPTY; |
239 |
< |
for (j = 0; j < 3; j++) { |
240 |
< |
cukid.cuorg[j] = cu->cuorg[j]; |
241 |
< |
if ((1<<j) & i) |
242 |
< |
cukid.cuorg[j] += cukid.cusize; |
243 |
< |
} |
244 |
< |
for (j = 1; j <= oset[0]; j++) |
245 |
< |
addobject(&cukid, oset[j]); |
246 |
< |
addobject(&cukid, obj); |
247 |
< |
octkid(ot, i) = cukid.cutree; |
248 |
< |
} |
249 |
< |
cu->cutree = ot; |
238 |
> |
return; |
239 |
> |
} |
240 |
> |
/* add to full node */ |
241 |
> |
objset(oset, cu->cutree); |
242 |
> |
cukid.cusize = cu->cusize * 0.5; |
243 |
> |
|
244 |
> |
if (in==O_IN || oset[0] < objlim || cukid.cusize < mincusize) { |
245 |
> |
/* add to set */ |
246 |
> |
if (oset[0] >= MAXSET) { |
247 |
> |
sprintf(errmsg, |
248 |
> |
"set overflow in addobject (%s)", |
249 |
> |
objptr(obj)->oname); |
250 |
> |
error(INTERNAL, errmsg); |
251 |
|
} |
252 |
+ |
insertelem(oset, obj); |
253 |
+ |
cu->cutree = fullnode(oset); |
254 |
+ |
return; |
255 |
|
} |
256 |
+ |
/* subdivide cube */ |
257 |
+ |
if ((ot = octalloc()) == EMPTY) |
258 |
+ |
error(SYSTEM, "out of octree space"); |
259 |
+ |
/* mark volumes */ |
260 |
+ |
j = (oset[0]+7)>>3; |
261 |
+ |
while (j--) |
262 |
+ |
volflg[j] = inflg[j] = 0; |
263 |
+ |
for (j = 1; j <= oset[0]; j++) |
264 |
+ |
if (isvolume(objptr(oset[j])->otype)) { |
265 |
+ |
setbit(volflg,j-1); |
266 |
+ |
if ((*ofun[objptr(oset[j])->otype].funp) |
267 |
+ |
(objptr(oset[j]),cu) == O_IN) |
268 |
+ |
setbit(inflg,j-1); |
269 |
+ |
} |
270 |
+ |
/* assign subcubes */ |
271 |
+ |
for (i = 0; i < 8; i++) { |
272 |
+ |
cukid.cutree = EMPTY; |
273 |
+ |
for (j = 0; j < 3; j++) { |
274 |
+ |
cukid.cuorg[j] = cu->cuorg[j]; |
275 |
+ |
if ((1<<j) & i) |
276 |
+ |
cukid.cuorg[j] += cukid.cusize; |
277 |
+ |
} |
278 |
+ |
/* surfaces first */ |
279 |
+ |
for (j = 1; j <= oset[0]; j++) |
280 |
+ |
if (!tstbit(volflg,j-1)) |
281 |
+ |
addobject(&cukid, oset[j]); |
282 |
+ |
/* then this object */ |
283 |
+ |
addobject(&cukid, obj); |
284 |
+ |
/* partial volumes */ |
285 |
+ |
for (j = 1; j <= oset[0]; j++) |
286 |
+ |
if (tstbit(volflg,j-1) && |
287 |
+ |
!tstbit(inflg,j-1)) |
288 |
+ |
addobject(&cukid, oset[j]); |
289 |
+ |
/* full volumes */ |
290 |
+ |
for (j = 1; j <= oset[0]; j++) |
291 |
+ |
if (tstbit(inflg,j-1)) |
292 |
+ |
addobject(&cukid, oset[j]); |
293 |
+ |
/* returned node */ |
294 |
+ |
octkid(ot, i) = cukid.cutree; |
295 |
+ |
} |
296 |
+ |
cu->cutree = ot; |
297 |
|
} |