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] == '-'; 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 files */ |
105 |
if (nfiles >= MAXOBJFIL) |
106 |
error(INTERNAL, "too many scene files"); |
107 |
readobj(ofname[nfiles++] = argv[i]); |
108 |
} |
109 |
ofname[nfiles] = NULL; |
110 |
/* find bounding box */ |
111 |
bbmin[0] = bbmin[1] = bbmin[2] = FHUGE; |
112 |
bbmax[0] = bbmax[1] = bbmax[2] = -FHUGE; |
113 |
for (i = startobj; i < nobjects; i++) |
114 |
add2bbox(objptr(i), bbmin, bbmax); |
115 |
/* set/check cube */ |
116 |
if (thescene.cusize == 0.0) { |
117 |
if (bbmin[0] <= bbmax[0]) { |
118 |
for (i = 0; i < 3; i++) { |
119 |
bbmin[i] -= OMARGIN; |
120 |
bbmax[i] += OMARGIN; |
121 |
} |
122 |
VCOPY(thescene.cuorg, bbmin); |
123 |
for (i = 0; i < 3; i++) |
124 |
if (bbmax[i] - bbmin[i] > thescene.cusize) |
125 |
thescene.cusize = bbmax[i] - bbmin[i]; |
126 |
} |
127 |
} else { |
128 |
for (i = 0; i < 3; i++) |
129 |
if (bbmin[i] < thescene.cuorg[i] || |
130 |
bbmax[i] > thescene.cuorg[i] + thescene.cusize) |
131 |
error(USER, "boundary does not encompass scene"); |
132 |
} |
133 |
|
134 |
mincusize = thescene.cusize / resolu - FTINY; |
135 |
|
136 |
for (i = startobj; i < nobjects; i++) /* add new objects */ |
137 |
addobject(&thescene, i); |
138 |
|
139 |
thescene.cutree = combine(thescene.cutree); /* optimize */ |
140 |
|
141 |
writeoct(outflags, &thescene, ofname); /* write structures to stdout */ |
142 |
|
143 |
quit(0); |
144 |
} |
145 |
|
146 |
|
147 |
quit(code) /* exit program */ |
148 |
int code; |
149 |
{ |
150 |
exit(code); |
151 |
} |
152 |
|
153 |
|
154 |
cputs() /* interactive error */ |
155 |
{ |
156 |
/* referenced, but not used */ |
157 |
} |
158 |
|
159 |
|
160 |
wputs(s) /* warning message */ |
161 |
char *s; |
162 |
{ |
163 |
if (!nowarn) |
164 |
eputs(s); |
165 |
} |
166 |
|
167 |
|
168 |
eputs(s) /* put string to stderr */ |
169 |
register char *s; |
170 |
{ |
171 |
static int inline = 0; |
172 |
|
173 |
if (!inline++) { |
174 |
fputs(progname, stderr); |
175 |
fputs(": ", stderr); |
176 |
} |
177 |
fputs(s, stderr); |
178 |
if (*s && s[strlen(s)-1] == '\n') |
179 |
inline = 0; |
180 |
} |
181 |
|
182 |
|
183 |
addobject(cu, obj) /* add an object to a cube */ |
184 |
register CUBE *cu; |
185 |
OBJECT obj; |
186 |
{ |
187 |
CUBE cukid; |
188 |
OCTREE ot; |
189 |
OBJECT oset[MAXSET+1]; |
190 |
int in; |
191 |
register int i, j; |
192 |
|
193 |
in = (*ofun[objptr(obj)->otype].funp)(objptr(obj), cu); |
194 |
|
195 |
if (!in) |
196 |
return; /* no intersection */ |
197 |
|
198 |
if (istree(cu->cutree)) { |
199 |
/* do children */ |
200 |
cukid.cusize = cu->cusize * 0.5; |
201 |
for (i = 0; i < 8; i++) { |
202 |
cukid.cutree = octkid(cu->cutree, i); |
203 |
for (j = 0; j < 3; j++) { |
204 |
cukid.cuorg[j] = cu->cuorg[j]; |
205 |
if ((1<<j) & i) |
206 |
cukid.cuorg[j] += cukid.cusize; |
207 |
} |
208 |
addobject(&cukid, obj); |
209 |
octkid(cu->cutree, i) = cukid.cutree; |
210 |
} |
211 |
|
212 |
} else if (isempty(cu->cutree)) { |
213 |
/* singular set */ |
214 |
oset[0] = 1; oset[1] = obj; |
215 |
cu->cutree = fullnode(oset); |
216 |
|
217 |
} else { |
218 |
/* add to full node */ |
219 |
objset(oset, cu->cutree); |
220 |
cukid.cusize = cu->cusize * 0.5; |
221 |
|
222 |
if (in == 2 || oset[0] < objlim || cukid.cusize < mincusize) { |
223 |
/* add to set */ |
224 |
if (oset[0] >= MAXSET) { |
225 |
sprintf(errmsg, |
226 |
"set overflow in addobject (%s)", |
227 |
objptr(obj)->oname); |
228 |
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; |
250 |
} |
251 |
} |
252 |
} |