1 |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* oconv.c - main program for object to octree conversion. |
9 |
* |
10 |
* 7/29/85 |
11 |
*/ |
12 |
|
13 |
#include "standard.h" |
14 |
|
15 |
#include "octree.h" |
16 |
|
17 |
#include "object.h" |
18 |
|
19 |
#include "otypes.h" |
20 |
|
21 |
#define OMARGIN (10*FTINY) /* margin around global cube */ |
22 |
|
23 |
#define MAXOBJFIL 63 /* maximum number of scene files */ |
24 |
|
25 |
char *progname; /* argv[0] */ |
26 |
|
27 |
char *libpath; /* library search path */ |
28 |
|
29 |
int nowarn = 0; /* supress warnings? */ |
30 |
|
31 |
int objlim = 5; /* # of objects before split */ |
32 |
|
33 |
int resolu = 1024; /* octree resolution limit */ |
34 |
|
35 |
CUBE thescene = {EMPTY, {0.0, 0.0, 0.0}, 0.0}; /* our scene */ |
36 |
|
37 |
char *ofname[MAXOBJFIL+1]; /* object file names */ |
38 |
int nfiles = 0; /* number of object files */ |
39 |
|
40 |
double mincusize; /* minimum cube size from resolu */ |
41 |
|
42 |
|
43 |
main(argc, argv) /* convert object files to an octree */ |
44 |
int argc; |
45 |
char **argv; |
46 |
{ |
47 |
char *getenv(); |
48 |
double atof(); |
49 |
FVECT bbmin, bbmax; |
50 |
char *infile = NULL; |
51 |
int outflags = IO_ALL; |
52 |
OBJECT startobj; |
53 |
int i; |
54 |
|
55 |
progname = argv[0]; |
56 |
|
57 |
if ((libpath = getenv("RAYPATH")) == NULL) |
58 |
libpath = ":/usr/local/lib/ray"; |
59 |
|
60 |
for (i = 1; i < argc && argv[i][0] == '-' && argv[i][1]; i++) |
61 |
switch (argv[i][1]) { |
62 |
case 'i': /* input octree */ |
63 |
infile = argv[++i]; |
64 |
break; |
65 |
case 'b': /* bounding cube */ |
66 |
thescene.cuorg[0] = atof(argv[++i]) - OMARGIN; |
67 |
thescene.cuorg[1] = atof(argv[++i]) - OMARGIN; |
68 |
thescene.cuorg[2] = atof(argv[++i]) - OMARGIN; |
69 |
thescene.cusize = atof(argv[++i]) + 2*OMARGIN; |
70 |
break; |
71 |
case 'n': /* set limit */ |
72 |
objlim = atoi(argv[++i]); |
73 |
break; |
74 |
case 'r': /* resolution limit */ |
75 |
resolu = atoi(argv[++i]); |
76 |
break; |
77 |
case 'f': /* freeze octree */ |
78 |
outflags &= ~IO_FILES; |
79 |
break; |
80 |
case 'w': /* supress warnings */ |
81 |
nowarn = 1; |
82 |
break; |
83 |
default: |
84 |
sprintf(errmsg, "unknown option: '%s'", argv[i]); |
85 |
error(USER, errmsg); |
86 |
break; |
87 |
} |
88 |
|
89 |
if (infile != NULL) { /* get old octree & objects */ |
90 |
if (thescene.cusize > FTINY) |
91 |
error(USER, "only one of '-b' or '-i'"); |
92 |
nfiles = readoct(infile, IO_ALL, &thescene, ofname); |
93 |
if (nfiles == 0 && outflags & IO_FILES) { |
94 |
error(WARNING, "frozen octree"); |
95 |
outflags &= ~IO_FILES; |
96 |
} |
97 |
} |
98 |
|
99 |
printargs(argc, argv, stdout); /* info. header */ |
100 |
printf("\n"); |
101 |
|
102 |
startobj = nobjects; /* previous objects already converted */ |
103 |
|
104 |
for ( ; i < argc; i++) /* read new scene descriptions */ |
105 |
if (!strcmp(argv[i], "-")) { /* from stdin */ |
106 |
readobj(NULL); |
107 |
outflags &= ~IO_FILES; |
108 |
} else { /* from file */ |
109 |
if (nfiles >= MAXOBJFIL) |
110 |
error(INTERNAL, "too many scene files"); |
111 |
readobj(ofname[nfiles++] = argv[i]); |
112 |
} |
113 |
|
114 |
ofname[nfiles] = NULL; |
115 |
/* find bounding box */ |
116 |
bbmin[0] = bbmin[1] = bbmin[2] = FHUGE; |
117 |
bbmax[0] = bbmax[1] = bbmax[2] = -FHUGE; |
118 |
for (i = startobj; i < nobjects; i++) |
119 |
add2bbox(objptr(i), bbmin, bbmax); |
120 |
/* set/check cube */ |
121 |
if (thescene.cusize == 0.0) { |
122 |
if (bbmin[0] <= bbmax[0]) { |
123 |
for (i = 0; i < 3; i++) { |
124 |
bbmin[i] -= OMARGIN; |
125 |
bbmax[i] += OMARGIN; |
126 |
} |
127 |
for (i = 0; i < 3; i++) |
128 |
if (bbmax[i] - bbmin[i] > thescene.cusize) |
129 |
thescene.cusize = bbmax[i] - bbmin[i]; |
130 |
for (i = 0; i < 3; i++) |
131 |
thescene.cuorg[i] = |
132 |
(bbmax[i]+bbmin[i]-thescene.cusize)*.5; |
133 |
} |
134 |
} else { |
135 |
for (i = 0; i < 3; i++) |
136 |
if (bbmin[i] < thescene.cuorg[i] || |
137 |
bbmax[i] > thescene.cuorg[i] + thescene.cusize) |
138 |
error(USER, "boundary does not encompass scene"); |
139 |
} |
140 |
|
141 |
mincusize = thescene.cusize / resolu - FTINY; |
142 |
|
143 |
for (i = startobj; i < nobjects; i++) /* add new objects */ |
144 |
addobject(&thescene, i); |
145 |
|
146 |
thescene.cutree = combine(thescene.cutree); /* optimize */ |
147 |
|
148 |
writeoct(outflags, &thescene, ofname); /* write structures to stdout */ |
149 |
|
150 |
quit(0); |
151 |
} |
152 |
|
153 |
|
154 |
quit(code) /* exit program */ |
155 |
int code; |
156 |
{ |
157 |
exit(code); |
158 |
} |
159 |
|
160 |
|
161 |
cputs() /* interactive error */ |
162 |
{ |
163 |
/* referenced, but not used */ |
164 |
} |
165 |
|
166 |
|
167 |
wputs(s) /* warning message */ |
168 |
char *s; |
169 |
{ |
170 |
if (!nowarn) |
171 |
eputs(s); |
172 |
} |
173 |
|
174 |
|
175 |
eputs(s) /* put string to stderr */ |
176 |
register char *s; |
177 |
{ |
178 |
static int inline = 0; |
179 |
|
180 |
if (!inline++) { |
181 |
fputs(progname, stderr); |
182 |
fputs(": ", stderr); |
183 |
} |
184 |
fputs(s, stderr); |
185 |
if (*s && s[strlen(s)-1] == '\n') |
186 |
inline = 0; |
187 |
} |
188 |
|
189 |
|
190 |
addobject(cu, obj) /* add an object to a cube */ |
191 |
register CUBE *cu; |
192 |
OBJECT obj; |
193 |
{ |
194 |
CUBE cukid; |
195 |
OCTREE ot; |
196 |
OBJECT oset[MAXSET+1]; |
197 |
int in; |
198 |
register int i, j; |
199 |
|
200 |
in = (*ofun[objptr(obj)->otype].funp)(objptr(obj), cu); |
201 |
|
202 |
if (!in) |
203 |
return; /* no intersection */ |
204 |
|
205 |
if (istree(cu->cutree)) { |
206 |
/* do children */ |
207 |
cukid.cusize = cu->cusize * 0.5; |
208 |
for (i = 0; i < 8; i++) { |
209 |
cukid.cutree = octkid(cu->cutree, i); |
210 |
for (j = 0; j < 3; j++) { |
211 |
cukid.cuorg[j] = cu->cuorg[j]; |
212 |
if ((1<<j) & i) |
213 |
cukid.cuorg[j] += cukid.cusize; |
214 |
} |
215 |
addobject(&cukid, obj); |
216 |
octkid(cu->cutree, i) = cukid.cutree; |
217 |
} |
218 |
|
219 |
} else if (isempty(cu->cutree)) { |
220 |
/* singular set */ |
221 |
oset[0] = 1; oset[1] = obj; |
222 |
cu->cutree = fullnode(oset); |
223 |
|
224 |
} else { |
225 |
/* add to full node */ |
226 |
objset(oset, cu->cutree); |
227 |
cukid.cusize = cu->cusize * 0.5; |
228 |
|
229 |
if (in == 2 || oset[0] < objlim || cukid.cusize < mincusize) { |
230 |
/* add to set */ |
231 |
if (oset[0] >= MAXSET) { |
232 |
sprintf(errmsg, |
233 |
"set overflow in addobject (%s)", |
234 |
objptr(obj)->oname); |
235 |
error(INTERNAL, errmsg); |
236 |
} |
237 |
insertelem(oset, obj); |
238 |
cu->cutree = fullnode(oset); |
239 |
|
240 |
} else { |
241 |
/* subdivide cube */ |
242 |
if ((ot = octalloc()) == EMPTY) |
243 |
error(SYSTEM, "out of octree space"); |
244 |
for (i = 0; i < 8; i++) { |
245 |
cukid.cutree = EMPTY; |
246 |
for (j = 0; j < 3; j++) { |
247 |
cukid.cuorg[j] = cu->cuorg[j]; |
248 |
if ((1<<j) & i) |
249 |
cukid.cuorg[j] += cukid.cusize; |
250 |
} |
251 |
for (j = 1; j <= oset[0]; j++) |
252 |
addobject(&cukid, oset[j]); |
253 |
addobject(&cukid, obj); |
254 |
octkid(ot, i) = cukid.cutree; |
255 |
} |
256 |
cu->cutree = ot; |
257 |
} |
258 |
} |
259 |
} |