ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/ot/oconv.c
Revision: 2.12
Committed: Sat Feb 22 02:07:26 2003 UTC (21 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.11: +7 -6 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /*
5 * oconv.c - main program for object to octree conversion.
6 *
7 * 7/29/85
8 */
9
10 #include "standard.h"
11
12 #include "octree.h"
13
14 #include "object.h"
15
16 #include "otypes.h"
17
18 #include "paths.h"
19
20 #define OMARGIN (10*FTINY) /* margin around global cube */
21
22 #define MAXOBJFIL 127 /* maximum number of scene files */
23
24 char *progname; /* argv[0] */
25
26 int nowarn = 0; /* supress warnings? */
27
28 int objlim = 5; /* # of objects before split */
29
30 int resolu = 8192; /* octree resolution limit */
31
32 CUBE thescene = {EMPTY, {0.0, 0.0, 0.0}, 0.0}; /* our scene */
33
34 char *ofname[MAXOBJFIL+1]; /* object file names */
35 int nfiles = 0; /* number of object files */
36
37 double mincusize; /* minimum cube size from resolu */
38
39 void (*addobjnotify[])() = {NULL}; /* new object notifier functions */
40
41
42 main(argc, argv) /* convert object files to an octree */
43 int argc;
44 char *argv[];
45 {
46 FVECT bbmin, bbmax;
47 char *infile = NULL;
48 int inpfrozen = 0;
49 int outflags = IO_ALL;
50 OBJECT startobj;
51 int i;
52
53 progname = argv[0] = fixargv0(argv[0]);
54
55 initotypes();
56
57 for (i = 1; i < argc && argv[i][0] == '-'; i++)
58 switch (argv[i][1]) {
59 case '\0': /* scene from stdin */
60 goto breakopt;
61 case 'i': /* input octree */
62 infile = argv[++i];
63 break;
64 case 'b': /* bounding cube */
65 thescene.cuorg[0] = atof(argv[++i]) - OMARGIN;
66 thescene.cuorg[1] = atof(argv[++i]) - OMARGIN;
67 thescene.cuorg[2] = atof(argv[++i]) - OMARGIN;
68 thescene.cusize = atof(argv[++i]) + 2*OMARGIN;
69 break;
70 case 'n': /* set limit */
71 objlim = atoi(argv[++i]);
72 break;
73 case 'r': /* resolution limit */
74 resolu = atoi(argv[++i]);
75 break;
76 case 'f': /* freeze octree */
77 outflags &= ~IO_FILES;
78 break;
79 case 'w': /* supress warnings */
80 nowarn = 1;
81 break;
82 default:
83 sprintf(errmsg, "unknown option: '%s'", argv[i]);
84 error(USER, errmsg);
85 break;
86 }
87 breakopt:
88 #ifdef MSDOS
89 setmode(fileno(stdout), O_BINARY);
90 #endif
91 if (infile != NULL) { /* get old octree & objects */
92 if (thescene.cusize > FTINY)
93 error(USER, "only one of '-b' or '-i'");
94 nfiles = readoct(infile, IO_ALL, &thescene, ofname);
95 if (nfiles == 0)
96 inpfrozen++;
97 } else
98 newheader("RADIANCE", stdout); /* new binary file header */
99 printargs(argc, argv, stdout);
100 fputformat(OCTFMT, stdout);
101 printf("\n");
102
103 startobj = nobjects; /* previous objects already converted */
104
105 for ( ; i < argc; i++) /* read new scene descriptions */
106 if (!strcmp(argv[i], "-")) { /* from stdin */
107 readobj(NULL);
108 outflags &= ~IO_FILES;
109 } else { /* from file */
110 if (nfiles >= MAXOBJFIL)
111 error(INTERNAL, "too many scene files");
112 readobj(ofname[nfiles++] = argv[i]);
113 }
114
115 ofname[nfiles] = NULL;
116
117 if (inpfrozen && outflags & IO_FILES) {
118 error(WARNING, "frozen octree");
119 outflags &= ~IO_FILES;
120 }
121 /* find bounding box */
122 bbmin[0] = bbmin[1] = bbmin[2] = FHUGE;
123 bbmax[0] = bbmax[1] = bbmax[2] = -FHUGE;
124 for (i = startobj; i < nobjects; i++)
125 add2bbox(objptr(i), bbmin, bbmax);
126 /* set/check cube */
127 if (thescene.cusize == 0.0) {
128 if (bbmin[0] <= bbmax[0]) {
129 for (i = 0; i < 3; i++) {
130 bbmin[i] -= OMARGIN;
131 bbmax[i] += OMARGIN;
132 }
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++)
142 if (bbmin[i] < thescene.cuorg[i] ||
143 bbmax[i] > thescene.cuorg[i] + thescene.cusize)
144 error(USER, "boundary does not encompass scene");
145 }
146
147 mincusize = thescene.cusize / resolu - FTINY;
148
149 for (i = startobj; i < nobjects; i++) /* add new objects */
150 addobject(&thescene, i);
151
152 thescene.cutree = combine(thescene.cutree); /* optimize */
153
154 writeoct(outflags, &thescene, ofname); /* write structures to stdout */
155
156 quit(0);
157 }
158
159
160 void
161 quit(code) /* exit program */
162 int code;
163 {
164 exit(code);
165 }
166
167
168 void
169 cputs() /* interactive error */
170 {
171 /* referenced, but not used */
172 }
173
174
175 void
176 wputs(s) /* warning message */
177 char *s;
178 {
179 if (!nowarn)
180 eputs(s);
181 }
182
183
184 void
185 eputs(s) /* put string to stderr */
186 register char *s;
187 {
188 static int inln = 0;
189
190 if (!inln++) {
191 fputs(progname, stderr);
192 fputs(": ", stderr);
193 }
194 fputs(s, stderr);
195 if (*s && s[strlen(s)-1] == '\n')
196 inln = 0;
197 }
198
199
200 #define bitop(f,i,op) (f[((i)>>3)] op (1<<((i)&7)))
201 #define tstbit(f,i) bitop(f,i,&)
202 #define setbit(f,i) bitop(f,i,|=)
203 #define clrbit(f,i) bitop(f,i,&=~)
204 #define tglbit(f,i) bitop(f,i,^=)
205
206
207 addobject(cu, obj) /* add an object to a cube */
208 register CUBE *cu;
209 OBJECT obj;
210 {
211 CUBE cukid;
212 OCTREE ot;
213 OBJECT oset[MAXSET+1];
214 unsigned char inflg[(MAXSET+7)/8], volflg[(MAXSET+7)/8];
215 int in;
216 register int i, j;
217
218 in = (*ofun[objptr(obj)->otype].funp)(objptr(obj), cu);
219
220 if (in == O_MISS)
221 return; /* no intersection */
222
223 if (istree(cu->cutree)) {
224 /* do children */
225 cukid.cusize = cu->cusize * 0.5;
226 for (i = 0; i < 8; i++) {
227 cukid.cutree = octkid(cu->cutree, i);
228 for (j = 0; j < 3; j++) {
229 cukid.cuorg[j] = cu->cuorg[j];
230 if ((1<<j) & i)
231 cukid.cuorg[j] += cukid.cusize;
232 }
233 addobject(&cukid, obj);
234 octkid(cu->cutree, i) = cukid.cutree;
235 }
236 return;
237 }
238 if (isempty(cu->cutree)) {
239 /* singular set */
240 oset[0] = 1; oset[1] = obj;
241 cu->cutree = fullnode(oset);
242 return;
243 }
244 /* add to full node */
245 objset(oset, cu->cutree);
246 cukid.cusize = cu->cusize * 0.5;
247
248 if (in==O_IN || oset[0] < objlim || cukid.cusize < mincusize) {
249 /* add to set */
250 if (oset[0] >= MAXSET) {
251 sprintf(errmsg, "set overflow in addobject (%s)",
252 objptr(obj)->oname);
253 error(INTERNAL, errmsg);
254 }
255 insertelem(oset, obj);
256 cu->cutree = fullnode(oset);
257 return;
258 }
259 /* subdivide cube */
260 if ((ot = octalloc()) == EMPTY)
261 error(SYSTEM, "out of octree space");
262 /* mark volumes */
263 j = (oset[0]+7)>>3;
264 while (j--)
265 volflg[j] = inflg[j] = 0;
266 for (j = 1; j <= oset[0]; j++)
267 if (isvolume(objptr(oset[j])->otype)) {
268 setbit(volflg,j-1);
269 if ((*ofun[objptr(oset[j])->otype].funp)
270 (objptr(oset[j]), cu) == O_IN)
271 setbit(inflg,j-1);
272 }
273 /* assign subcubes */
274 for (i = 0; i < 8; i++) {
275 cukid.cutree = EMPTY;
276 for (j = 0; j < 3; j++) {
277 cukid.cuorg[j] = cu->cuorg[j];
278 if ((1<<j) & i)
279 cukid.cuorg[j] += cukid.cusize;
280 }
281 /* surfaces first */
282 for (j = 1; j <= oset[0]; j++)
283 if (!tstbit(volflg,j-1))
284 addobject(&cukid, oset[j]);
285 /* then this object */
286 addobject(&cukid, obj);
287 /* then partial volumes */
288 for (j = 1; j <= oset[0]; j++)
289 if (tstbit(volflg,j-1) &&
290 !tstbit(inflg,j-1))
291 addobject(&cukid, oset[j]);
292 /* full volumes last */
293 for (j = 1; j <= oset[0]; j++)
294 if (tstbit(inflg,j-1))
295 addobject(&cukid, oset[j]);
296 /* returned node */
297 octkid(ot, i) = cukid.cutree;
298 }
299 cu->cutree = ot;
300 }