| 18 |
|
|
| 19 |
|
#include "otypes.h" |
| 20 |
|
|
| 21 |
+ |
#ifndef DEFPATH |
| 22 |
+ |
#define DEFPATH ":/usr/local/lib/ray" |
| 23 |
+ |
#endif |
| 24 |
+ |
|
| 25 |
|
#define OMARGIN (10*FTINY) /* margin around global cube */ |
| 26 |
|
|
| 27 |
|
#define MAXOBJFIL 63 /* maximum number of scene files */ |
| 43 |
|
|
| 44 |
|
double mincusize; /* minimum cube size from resolu */ |
| 45 |
|
|
| 46 |
+ |
int (*addobjnotify[])() = {NULL}; /* new object notifier functions */ |
| 47 |
|
|
| 48 |
+ |
|
| 49 |
|
main(argc, argv) /* convert object files to an octree */ |
| 50 |
|
int argc; |
| 51 |
|
char **argv; |
| 61 |
|
progname = argv[0]; |
| 62 |
|
|
| 63 |
|
if ((libpath = getenv("RAYPATH")) == NULL) |
| 64 |
< |
libpath = ":/usr/local/lib/ray"; |
| 64 |
> |
libpath = DEFPATH; |
| 65 |
|
|
| 66 |
+ |
initotypes(); |
| 67 |
+ |
|
| 68 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
| 69 |
|
switch (argv[i][1]) { |
| 70 |
+ |
case '\0': /* scene from stdin */ |
| 71 |
+ |
goto breakopt; |
| 72 |
|
case 'i': /* input octree */ |
| 73 |
|
infile = argv[++i]; |
| 74 |
|
break; |
| 95 |
|
error(USER, errmsg); |
| 96 |
|
break; |
| 97 |
|
} |
| 98 |
< |
|
| 98 |
> |
breakopt: |
| 99 |
|
if (infile != NULL) { /* get old octree & objects */ |
| 100 |
|
if (thescene.cusize > FTINY) |
| 101 |
|
error(USER, "only one of '-b' or '-i'"); |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
printargs(argc, argv, stdout); /* info. header */ |
| 110 |
+ |
fputformat(OCTFMT, stdout); |
| 111 |
|
printf("\n"); |
| 112 |
|
|
| 113 |
|
startobj = nobjects; /* previous objects already converted */ |
| 114 |
|
|
| 115 |
< |
for ( ; i < argc; i++) { /* read new files */ |
| 116 |
< |
if (nfiles >= MAXOBJFIL) |
| 117 |
< |
error(INTERNAL, "too many scene files"); |
| 118 |
< |
readobj(ofname[nfiles++] = argv[i]); |
| 119 |
< |
} |
| 115 |
> |
for ( ; i < argc; i++) /* read new scene descriptions */ |
| 116 |
> |
if (!strcmp(argv[i], "-")) { /* from stdin */ |
| 117 |
> |
readobj(NULL); |
| 118 |
> |
outflags &= ~IO_FILES; |
| 119 |
> |
} else { /* from file */ |
| 120 |
> |
if (nfiles >= MAXOBJFIL) |
| 121 |
> |
error(INTERNAL, "too many scene files"); |
| 122 |
> |
readobj(ofname[nfiles++] = argv[i]); |
| 123 |
> |
} |
| 124 |
> |
|
| 125 |
|
ofname[nfiles] = NULL; |
| 126 |
|
/* find bounding box */ |
| 127 |
|
bbmin[0] = bbmin[1] = bbmin[2] = FHUGE; |
| 135 |
|
bbmin[i] -= OMARGIN; |
| 136 |
|
bbmax[i] += OMARGIN; |
| 137 |
|
} |
| 122 |
– |
VCOPY(thescene.cuorg, bbmin); |
| 138 |
|
for (i = 0; i < 3; i++) |
| 139 |
|
if (bbmax[i] - bbmin[i] > thescene.cusize) |
| 140 |
|
thescene.cusize = bbmax[i] - bbmin[i]; |
| 141 |
+ |
for (i = 0; i < 3; i++) |
| 142 |
+ |
thescene.cuorg[i] = |
| 143 |
+ |
(bbmax[i]+bbmin[i]-thescene.cusize)*.5; |
| 144 |
|
} |
| 145 |
|
} else { |
| 146 |
|
for (i = 0; i < 3; i++) |
| 186 |
|
eputs(s) /* put string to stderr */ |
| 187 |
|
register char *s; |
| 188 |
|
{ |
| 189 |
< |
static int inline = 0; |
| 189 |
> |
static int inln = 0; |
| 190 |
|
|
| 191 |
< |
if (!inline++) { |
| 191 |
> |
if (!inln++) { |
| 192 |
|
fputs(progname, stderr); |
| 193 |
|
fputs(": ", stderr); |
| 194 |
|
} |
| 195 |
|
fputs(s, stderr); |
| 196 |
|
if (*s && s[strlen(s)-1] == '\n') |
| 197 |
< |
inline = 0; |
| 197 |
> |
inln = 0; |
| 198 |
|
} |
| 199 |
|
|
| 200 |
|
|
| 201 |
+ |
#define bitop(f,i,op) (f[((i)>>3)] op (1<<((i)&7))) |
| 202 |
+ |
#define tstbit(f,i) bitop(f,i,&) |
| 203 |
+ |
#define setbit(f,i) bitop(f,i,|=) |
| 204 |
+ |
#define clrbit(f,i) bitop(f,i,&=~) |
| 205 |
+ |
#define tglbit(f,i) bitop(f,i,^=) |
| 206 |
+ |
|
| 207 |
+ |
|
| 208 |
|
addobject(cu, obj) /* add an object to a cube */ |
| 209 |
|
register CUBE *cu; |
| 210 |
|
OBJECT obj; |
| 212 |
|
CUBE cukid; |
| 213 |
|
OCTREE ot; |
| 214 |
|
OBJECT oset[MAXSET+1]; |
| 215 |
+ |
unsigned char inflg[MAXSET/8], volflg[MAXSET/8]; |
| 216 |
|
int in; |
| 217 |
|
register int i, j; |
| 218 |
|
|
| 219 |
|
in = (*ofun[objptr(obj)->otype].funp)(objptr(obj), cu); |
| 220 |
|
|
| 221 |
< |
if (!in) |
| 221 |
> |
if (in == O_MISS) |
| 222 |
|
return; /* no intersection */ |
| 223 |
|
|
| 224 |
|
if (istree(cu->cutree)) { |
| 234 |
|
addobject(&cukid, obj); |
| 235 |
|
octkid(cu->cutree, i) = cukid.cutree; |
| 236 |
|
} |
| 237 |
< |
|
| 238 |
< |
} else if (isempty(cu->cutree)) { |
| 237 |
> |
return; |
| 238 |
> |
} |
| 239 |
> |
if (isempty(cu->cutree)) { |
| 240 |
|
/* singular set */ |
| 241 |
|
oset[0] = 1; oset[1] = obj; |
| 242 |
|
cu->cutree = fullnode(oset); |
| 243 |
< |
|
| 244 |
< |
} else { |
| 245 |
< |
/* add to full node */ |
| 246 |
< |
objset(oset, cu->cutree); |
| 247 |
< |
cukid.cusize = cu->cusize * 0.5; |
| 248 |
< |
|
| 249 |
< |
if (in == 2 || oset[0] < objlim || cukid.cusize < mincusize) { |
| 250 |
< |
/* add to set */ |
| 251 |
< |
if (oset[0] >= MAXSET) { |
| 252 |
< |
sprintf(errmsg, |
| 253 |
< |
"set overflow in addobject (%s)", |
| 254 |
< |
objptr(obj)->oname); |
| 255 |
< |
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; |
| 243 |
> |
return; |
| 244 |
> |
} |
| 245 |
> |
/* add to full node */ |
| 246 |
> |
objset(oset, cu->cutree); |
| 247 |
> |
cukid.cusize = cu->cusize * 0.5; |
| 248 |
> |
|
| 249 |
> |
if (in==O_IN || oset[0] < objlim || cukid.cusize < mincusize) { |
| 250 |
> |
/* add to set */ |
| 251 |
> |
if (oset[0] >= MAXSET) { |
| 252 |
> |
sprintf(errmsg, |
| 253 |
> |
"set overflow in addobject (%s)", |
| 254 |
> |
objptr(obj)->oname); |
| 255 |
> |
error(INTERNAL, errmsg); |
| 256 |
|
} |
| 257 |
+ |
insertelem(oset, obj); |
| 258 |
+ |
cu->cutree = fullnode(oset); |
| 259 |
+ |
return; |
| 260 |
|
} |
| 261 |
+ |
/* subdivide cube */ |
| 262 |
+ |
if ((ot = octalloc()) == EMPTY) |
| 263 |
+ |
error(SYSTEM, "out of octree space"); |
| 264 |
+ |
/* mark volumes */ |
| 265 |
+ |
j = (oset[0]+7)>>3; |
| 266 |
+ |
while (j--) |
| 267 |
+ |
volflg[j] = inflg[j] = 0; |
| 268 |
+ |
for (j = 1; j <= oset[0]; j++) |
| 269 |
+ |
if (isvolume(objptr(oset[j])->otype)) { |
| 270 |
+ |
setbit(volflg,j-1); |
| 271 |
+ |
if ((*ofun[objptr(oset[j])->otype].funp) |
| 272 |
+ |
(objptr(oset[j]),cu) == O_IN) |
| 273 |
+ |
setbit(inflg,j-1); |
| 274 |
+ |
} |
| 275 |
+ |
/* assign subcubes */ |
| 276 |
+ |
for (i = 0; i < 8; i++) { |
| 277 |
+ |
cukid.cutree = EMPTY; |
| 278 |
+ |
for (j = 0; j < 3; j++) { |
| 279 |
+ |
cukid.cuorg[j] = cu->cuorg[j]; |
| 280 |
+ |
if ((1<<j) & i) |
| 281 |
+ |
cukid.cuorg[j] += cukid.cusize; |
| 282 |
+ |
} |
| 283 |
+ |
/* surfaces first */ |
| 284 |
+ |
for (j = 1; j <= oset[0]; j++) |
| 285 |
+ |
if (!tstbit(volflg,j-1)) |
| 286 |
+ |
addobject(&cukid, oset[j]); |
| 287 |
+ |
/* then this object */ |
| 288 |
+ |
addobject(&cukid, obj); |
| 289 |
+ |
/* partial volumes */ |
| 290 |
+ |
for (j = 1; j <= oset[0]; j++) |
| 291 |
+ |
if (tstbit(volflg,j-1) && |
| 292 |
+ |
!tstbit(inflg,j-1)) |
| 293 |
+ |
addobject(&cukid, oset[j]); |
| 294 |
+ |
/* full volumes */ |
| 295 |
+ |
for (j = 1; j <= oset[0]; j++) |
| 296 |
+ |
if (tstbit(inflg,j-1)) |
| 297 |
+ |
addobject(&cukid, oset[j]); |
| 298 |
+ |
/* returned node */ |
| 299 |
+ |
octkid(ot, i) = cukid.cutree; |
| 300 |
+ |
} |
| 301 |
+ |
cu->cutree = ot; |
| 302 |
|
} |