1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
18 |
|
|
19 |
|
#include "otypes.h" |
20 |
|
|
21 |
< |
#define MAXOBJFIL 63 /* maximum number of scene files */ |
21 |
> |
#include "paths.h" |
22 |
|
|
23 |
+ |
#define OMARGIN (10*FTINY) /* margin around global cube */ |
24 |
+ |
|
25 |
+ |
#define MAXOBJFIL 63 /* maximum number of scene files */ |
26 |
+ |
|
27 |
|
char *progname; /* argv[0] */ |
28 |
|
|
29 |
|
char *libpath; /* library search path */ |
39 |
|
char *ofname[MAXOBJFIL+1]; /* object file names */ |
40 |
|
int nfiles = 0; /* number of object files */ |
41 |
|
|
42 |
< |
double mincusize; /* minimum cube size from resolu */ |
42 |
> |
double mincusize; /* minimum cube size from resolu */ |
43 |
|
|
44 |
+ |
int (*addobjnotify[])() = {NULL}; /* new object notifier functions */ |
45 |
|
|
46 |
+ |
|
47 |
|
main(argc, argv) /* convert object files to an octree */ |
48 |
|
int argc; |
49 |
|
char **argv; |
50 |
|
{ |
51 |
< |
char *getenv(); |
46 |
< |
double atof(); |
51 |
> |
extern char *getenv(); |
52 |
|
FVECT bbmin, bbmax; |
53 |
|
char *infile = NULL; |
54 |
+ |
int inpfrozen = 0; |
55 |
|
int outflags = IO_ALL; |
56 |
< |
OBJECT startobj; |
56 |
> |
OBJECT startobj; |
57 |
|
int i; |
58 |
|
|
59 |
< |
progname = argv[0]; |
59 |
> |
progname = argv[0] = fixargv0(argv[0]); |
60 |
|
|
61 |
< |
if ((libpath = getenv("RAYPATH")) == NULL) |
62 |
< |
libpath = ":/usr/local/lib/ray"; |
61 |
> |
if ((libpath = getenv(ULIBVAR)) == NULL) |
62 |
> |
libpath = DEFPATH; |
63 |
|
|
64 |
+ |
initotypes(); |
65 |
+ |
|
66 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
67 |
|
switch (argv[i][1]) { |
68 |
+ |
case '\0': /* scene from stdin */ |
69 |
+ |
outflags &= ~IO_FILES; |
70 |
+ |
goto breakopt; |
71 |
|
case 'i': /* input octree */ |
72 |
|
infile = argv[++i]; |
73 |
|
break; |
74 |
|
case 'b': /* bounding cube */ |
75 |
< |
thescene.cuorg[0] = atof(argv[++i]) - FTINY; |
76 |
< |
thescene.cuorg[1] = atof(argv[++i]) - FTINY; |
77 |
< |
thescene.cuorg[2] = atof(argv[++i]) - FTINY; |
78 |
< |
thescene.cusize = atof(argv[++i]) + 2*FTINY; |
75 |
> |
thescene.cuorg[0] = atof(argv[++i]) - OMARGIN; |
76 |
> |
thescene.cuorg[1] = atof(argv[++i]) - OMARGIN; |
77 |
> |
thescene.cuorg[2] = atof(argv[++i]) - OMARGIN; |
78 |
> |
thescene.cusize = atof(argv[++i]) + 2*OMARGIN; |
79 |
|
break; |
80 |
|
case 'n': /* set limit */ |
81 |
|
objlim = atoi(argv[++i]); |
94 |
|
error(USER, errmsg); |
95 |
|
break; |
96 |
|
} |
97 |
< |
|
97 |
> |
breakopt: |
98 |
> |
#ifdef MSDOS |
99 |
> |
setmode(fileno(stdout), O_BINARY); |
100 |
> |
#endif |
101 |
|
if (infile != NULL) { /* get old octree & objects */ |
102 |
|
if (thescene.cusize > FTINY) |
103 |
|
error(USER, "only one of '-b' or '-i'"); |
104 |
|
nfiles = readoct(infile, IO_ALL, &thescene, ofname); |
105 |
< |
if (nfiles == 0 && outflags & IO_FILES) { |
106 |
< |
error(WARNING, "frozen octree"); |
93 |
< |
outflags &= ~IO_FILES; |
94 |
< |
} |
105 |
> |
if (nfiles == 0) |
106 |
> |
inpfrozen++; |
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 |
< |
} |
114 |
> |
|
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 |
+ |
|
127 |
+ |
if (inpfrozen && outflags & IO_FILES) { |
128 |
+ |
error(WARNING, "frozen octree"); |
129 |
+ |
outflags &= ~IO_FILES; |
130 |
+ |
} |
131 |
|
/* find bounding box */ |
132 |
|
bbmin[0] = bbmin[1] = bbmin[2] = FHUGE; |
133 |
|
bbmax[0] = bbmax[1] = bbmax[2] = -FHUGE; |
137 |
|
if (thescene.cusize == 0.0) { |
138 |
|
if (bbmin[0] <= bbmax[0]) { |
139 |
|
for (i = 0; i < 3; i++) { |
140 |
< |
bbmin[i] -= FTINY; |
141 |
< |
bbmax[i] += FTINY; |
140 |
> |
bbmin[i] -= OMARGIN; |
141 |
> |
bbmax[i] += OMARGIN; |
142 |
|
} |
120 |
– |
VCOPY(thescene.cuorg, bbmin); |
143 |
|
for (i = 0; i < 3; i++) |
144 |
|
if (bbmax[i] - bbmin[i] > thescene.cusize) |
145 |
|
thescene.cusize = bbmax[i] - bbmin[i]; |
146 |
+ |
for (i = 0; i < 3; i++) |
147 |
+ |
thescene.cuorg[i] = |
148 |
+ |
(bbmax[i]+bbmin[i]-thescene.cusize)*.5; |
149 |
|
} |
150 |
|
} else { |
151 |
|
for (i = 0; i < 3; i++) |
155 |
|
} |
156 |
|
|
157 |
|
mincusize = thescene.cusize / resolu - FTINY; |
158 |
< |
|
158 |
> |
|
159 |
|
for (i = startobj; i < nobjects; i++) /* add new objects */ |
160 |
|
addobject(&thescene, i); |
161 |
< |
|
161 |
> |
|
162 |
|
thescene.cutree = combine(thescene.cutree); /* optimize */ |
163 |
|
|
164 |
|
writeoct(outflags, &thescene, ofname); /* write structures to stdout */ |
191 |
|
eputs(s) /* put string to stderr */ |
192 |
|
register char *s; |
193 |
|
{ |
194 |
< |
static int inline = 0; |
194 |
> |
static int inln = 0; |
195 |
|
|
196 |
< |
if (!inline++) { |
196 |
> |
if (!inln++) { |
197 |
|
fputs(progname, stderr); |
198 |
|
fputs(": ", stderr); |
199 |
|
} |
200 |
|
fputs(s, stderr); |
201 |
|
if (*s && s[strlen(s)-1] == '\n') |
202 |
< |
inline = 0; |
202 |
> |
inln = 0; |
203 |
|
} |
204 |
|
|
205 |
|
|
206 |
+ |
#define bitop(f,i,op) (f[((i)>>3)] op (1<<((i)&7))) |
207 |
+ |
#define tstbit(f,i) bitop(f,i,&) |
208 |
+ |
#define setbit(f,i) bitop(f,i,|=) |
209 |
+ |
#define clrbit(f,i) bitop(f,i,&=~) |
210 |
+ |
#define tglbit(f,i) bitop(f,i,^=) |
211 |
+ |
|
212 |
+ |
|
213 |
|
addobject(cu, obj) /* add an object to a cube */ |
214 |
|
register CUBE *cu; |
215 |
< |
OBJECT obj; |
215 |
> |
OBJECT obj; |
216 |
|
{ |
217 |
|
CUBE cukid; |
218 |
< |
OCTREE ot; |
219 |
< |
OBJECT oset[MAXSET+1]; |
218 |
> |
OCTREE ot; |
219 |
> |
OBJECT oset[MAXSET+1]; |
220 |
> |
unsigned char inflg[(MAXSET+7)/8], volflg[(MAXSET+7)/8]; |
221 |
|
int in; |
222 |
|
register int i, j; |
223 |
|
|
224 |
|
in = (*ofun[objptr(obj)->otype].funp)(objptr(obj), cu); |
225 |
|
|
226 |
< |
if (!in) |
226 |
> |
if (in == O_MISS) |
227 |
|
return; /* no intersection */ |
228 |
< |
|
228 |
> |
|
229 |
|
if (istree(cu->cutree)) { |
230 |
|
/* do children */ |
231 |
|
cukid.cusize = cu->cusize * 0.5; |
239 |
|
addobject(&cukid, obj); |
240 |
|
octkid(cu->cutree, i) = cukid.cutree; |
241 |
|
} |
242 |
< |
|
243 |
< |
} else if (isempty(cu->cutree)) { |
242 |
> |
return; |
243 |
> |
} |
244 |
> |
if (isempty(cu->cutree)) { |
245 |
|
/* singular set */ |
246 |
|
oset[0] = 1; oset[1] = obj; |
247 |
|
cu->cutree = fullnode(oset); |
248 |
< |
|
249 |
< |
} else { |
250 |
< |
/* add to full node */ |
251 |
< |
objset(oset, cu->cutree); |
252 |
< |
cukid.cusize = cu->cusize * 0.5; |
219 |
< |
|
220 |
< |
if (in == 2 || oset[0] < objlim || cukid.cusize < mincusize) { |
221 |
< |
/* add to set */ |
222 |
< |
if (oset[0] >= MAXSET) { |
223 |
< |
sprintf(errmsg, |
224 |
< |
"set overflow in addobject (%s)", |
225 |
< |
objptr(obj)->oname); |
226 |
< |
error(INTERNAL, errmsg); |
227 |
< |
} |
228 |
< |
insertelem(oset, obj); |
229 |
< |
cu->cutree = fullnode(oset); |
248 |
> |
return; |
249 |
> |
} |
250 |
> |
/* add to full node */ |
251 |
> |
objset(oset, cu->cutree); |
252 |
> |
cukid.cusize = cu->cusize * 0.5; |
253 |
|
|
254 |
< |
} else { |
255 |
< |
/* subdivide cube */ |
256 |
< |
if ((ot = octalloc()) == EMPTY) |
257 |
< |
error(SYSTEM, "out of octree space"); |
258 |
< |
for (i = 0; i < 8; i++) { |
259 |
< |
cukid.cutree = EMPTY; |
237 |
< |
for (j = 0; j < 3; j++) { |
238 |
< |
cukid.cuorg[j] = cu->cuorg[j]; |
239 |
< |
if ((1<<j) & i) |
240 |
< |
cukid.cuorg[j] += cukid.cusize; |
241 |
< |
} |
242 |
< |
for (j = 1; j <= oset[0]; j++) |
243 |
< |
addobject(&cukid, oset[j]); |
244 |
< |
addobject(&cukid, obj); |
245 |
< |
octkid(ot, i) = cukid.cutree; |
246 |
< |
} |
247 |
< |
cu->cutree = ot; |
254 |
> |
if (in==O_IN || oset[0] < objlim || cukid.cusize < mincusize) { |
255 |
> |
/* add to set */ |
256 |
> |
if (oset[0] >= MAXSET) { |
257 |
> |
sprintf(errmsg, "set overflow in addobject (%s)", |
258 |
> |
objptr(obj)->oname); |
259 |
> |
error(INTERNAL, errmsg); |
260 |
|
} |
261 |
+ |
insertelem(oset, obj); |
262 |
+ |
cu->cutree = fullnode(oset); |
263 |
+ |
return; |
264 |
|
} |
265 |
+ |
/* subdivide cube */ |
266 |
+ |
if ((ot = octalloc()) == EMPTY) |
267 |
+ |
error(SYSTEM, "out of octree space"); |
268 |
+ |
/* mark volumes */ |
269 |
+ |
j = (oset[0]+7)>>3; |
270 |
+ |
while (j--) |
271 |
+ |
volflg[j] = inflg[j] = 0; |
272 |
+ |
for (j = 1; j <= oset[0]; j++) |
273 |
+ |
if (isvolume(objptr(oset[j])->otype)) { |
274 |
+ |
setbit(volflg,j-1); |
275 |
+ |
if ((*ofun[objptr(oset[j])->otype].funp) |
276 |
+ |
(objptr(oset[j]), cu) == O_IN) |
277 |
+ |
setbit(inflg,j-1); |
278 |
+ |
} |
279 |
+ |
/* assign subcubes */ |
280 |
+ |
for (i = 0; i < 8; i++) { |
281 |
+ |
cukid.cutree = EMPTY; |
282 |
+ |
for (j = 0; j < 3; j++) { |
283 |
+ |
cukid.cuorg[j] = cu->cuorg[j]; |
284 |
+ |
if ((1<<j) & i) |
285 |
+ |
cukid.cuorg[j] += cukid.cusize; |
286 |
+ |
} |
287 |
+ |
/* surfaces first */ |
288 |
+ |
for (j = 1; j <= oset[0]; j++) |
289 |
+ |
if (!tstbit(volflg,j-1)) |
290 |
+ |
addobject(&cukid, oset[j]); |
291 |
+ |
/* then this object */ |
292 |
+ |
addobject(&cukid, obj); |
293 |
+ |
/* then partial volumes */ |
294 |
+ |
for (j = 1; j <= oset[0]; j++) |
295 |
+ |
if (tstbit(volflg,j-1) && |
296 |
+ |
!tstbit(inflg,j-1)) |
297 |
+ |
addobject(&cukid, oset[j]); |
298 |
+ |
/* full volumes last */ |
299 |
+ |
for (j = 1; j <= oset[0]; j++) |
300 |
+ |
if (tstbit(inflg,j-1)) |
301 |
+ |
addobject(&cukid, oset[j]); |
302 |
+ |
/* returned node */ |
303 |
+ |
octkid(ot, i) = cukid.cutree; |
304 |
+ |
} |
305 |
+ |
cu->cutree = ot; |
306 |
|
} |