ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rview.c
Revision: 1.15
Committed: Tue May 21 17:41:32 1991 UTC (32 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.14: +5 -3 lines
Log Message:
added stratified random sampling with urand

File Contents

# Content
1 /* Copyright (c) 1987 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * rview.c - routines and variables for interactive view generation.
9 *
10 * 3/24/87
11 */
12
13 #include "ray.h"
14
15 #include "rpaint.h"
16
17 #include <signal.h>
18
19 #include <ctype.h>
20
21 VIEW ourview = STDVIEW; /* viewing parameters */
22 int hresolu, vresolu; /* image resolution */
23
24 int dimlist[MAXDIM]; /* sampling dimensions */
25 int ndims = 0; /* number of sampling dimensions */
26 int samplendx = 0; /* index for this sample */
27
28 int psample = 8; /* pixel sample size */
29 double maxdiff = .15; /* max. sample difference */
30
31 double exposure = 1.0; /* exposure for scene */
32
33 double dstrsrc = 0.0; /* square source distribution */
34 double shadthresh = .1; /* shadow threshold */
35 double shadcert = .25; /* shadow certainty */
36
37 int maxdepth = 4; /* maximum recursion depth */
38 double minweight = 1e-2; /* minimum ray weight */
39
40 COLOR ambval = BLKCOLOR; /* ambient value */
41 double ambacc = 0.2; /* ambient accuracy */
42 int ambres = 8; /* ambient resolution */
43 int ambdiv = 32; /* ambient divisions */
44 int ambssamp = 0; /* ambient super-samples */
45 int ambounce = 0; /* ambient bounces */
46 char *amblist[128]; /* ambient include/exclude list */
47 int ambincl = -1; /* include == 1, exclude == 0 */
48
49 int greyscale = 0; /* map colors to brightness? */
50 char *devname = dev_default; /* output device name */
51
52 struct driver *dev = NULL; /* driver functions */
53
54 VIEW oldview; /* previous view parameters */
55
56 PNODE ptrunk; /* the base of our image */
57 RECT pframe; /* current frame boundaries */
58 int pdepth; /* image depth in current frame */
59
60 static char *reserve_mem = NULL; /* pre-allocated reserve memory */
61
62 #define RESERVE_AMT 8192 /* amount of memory to reserve */
63
64 #define CTRL(c) ('c'-'@')
65
66
67 quit(code) /* quit program */
68 int code;
69 {
70 devclose();
71 exit(code);
72 }
73
74
75 devopen(dname) /* open device driver */
76 char *dname;
77 {
78 extern char *progname, *octname;
79 char *id;
80 register int i;
81
82 id = octname!=NULL ? octname : progname;
83 /* check device table */
84 for (i = 0; devtable[i].name; i++)
85 if (!strcmp(dname, devtable[i].name))
86 if ((dev = (*devtable[i].init)(dname, id)) == NULL) {
87 sprintf(errmsg, "cannot initialize %s", dname);
88 error(USER, errmsg);
89 } else
90 return;
91 /* not there, try exec */
92 if ((dev = comm_init(dname, id)) == NULL) {
93 sprintf(errmsg, "cannot start device \"%s\"", dname);
94 error(USER, errmsg);
95 }
96 }
97
98
99 devclose() /* close our device */
100 {
101 if (dev != NULL)
102 (*dev->close)();
103 dev = NULL;
104 }
105
106
107 printdevices() /* print list of output devices */
108 {
109 register int i;
110
111 for (i = 0; devtable[i].name; i++)
112 printf("%-16s # %s\n", devtable[i].name, devtable[i].descrip);
113 }
114
115
116 rview() /* do a view */
117 {
118 char buf[32];
119
120 devopen(devname); /* open device */
121 newimage(); /* start image (calls fillreserves) */
122
123 for ( ; ; ) { /* quit in command() */
124 while (hresolu <= 1<<pdepth && vresolu <= 1<<pdepth)
125 command("done: ");
126 while (reserve_mem == NULL)
127 command("out of memory: ");
128 errno = 0;
129 if (hresolu <= psample<<pdepth && vresolu <= psample<<pdepth) {
130 sprintf(buf, "%d sampling...\n", 1<<pdepth);
131 (*dev->comout)(buf);
132 rsample();
133 } else {
134 sprintf(buf, "%d refining...\n", 1<<pdepth);
135 (*dev->comout)(buf);
136 refine(&ptrunk, 0, 0, hresolu, vresolu, pdepth+1);
137 }
138 if (errno == ENOMEM) /* ran out of memory */
139 freereserves();
140 else if (dev->inpready) /* noticed some input */
141 command(": ");
142 else /* finished this depth */
143 pdepth++;
144 }
145 }
146
147
148 fillreserves() /* fill memory reserves */
149 {
150 if (reserve_mem != NULL)
151 return;
152 reserve_mem = malloc(RESERVE_AMT);
153 }
154
155
156 freereserves() /* free memory reserves */
157 {
158 if (reserve_mem == NULL)
159 return;
160 free(reserve_mem);
161 reserve_mem = NULL;
162 }
163
164
165 command(prompt) /* get/execute command */
166 char *prompt;
167 {
168 #define badcom(s) strncmp(s, inpbuf, args-inpbuf-1)
169 double atof();
170 char inpbuf[256];
171 char *args;
172 again:
173 (*dev->comin)(inpbuf, prompt); /* get command + arguments */
174 for (args = inpbuf; *args && *args != ' '; args++)
175 ;
176 if (*args) *args++ = '\0';
177 else *++args = '\0';
178
179 switch (inpbuf[0]) {
180 case 'f': /* new frame */
181 if (badcom("frame"))
182 goto commerr;
183 getframe(args);
184 break;
185 case 'v': /* view */
186 if (badcom("view"))
187 goto commerr;
188 getview(args);
189 break;
190 case 'l': /* last view */
191 if (badcom("last"))
192 goto commerr;
193 lastview(args);
194 break;
195 case 'e': /* exposure */
196 if (badcom("exposure"))
197 goto commerr;
198 getexposure(args);
199 break;
200 case 's': /* set a parameter */
201 if (badcom("set"))
202 goto commerr;
203 setparam(args);
204 break;
205 case 'n': /* new picture */
206 if (badcom("new"))
207 goto commerr;
208 newimage();
209 break;
210 case 't': /* trace a ray */
211 if (badcom("trace"))
212 goto commerr;
213 traceray(args);
214 break;
215 case 'a': /* aim camera */
216 if (badcom("aim"))
217 goto commerr;
218 getaim(args);
219 break;
220 case 'm': /* move camera */
221 if (badcom("move"))
222 goto commerr;
223 getmove(args);
224 break;
225 case 'r': /* rotate/repaint */
226 if (badcom("rotate")) {
227 if (badcom("repaint"))
228 goto commerr;
229 getrepaint(args);
230 break;
231 }
232 getrotate(args);
233 break;
234 case 'p': /* pivot view */
235 if (badcom("pivot"))
236 goto commerr;
237 getpivot(args);
238 break;
239 case CTRL(R): /* redraw */
240 redraw();
241 break;
242 case 'w': /* write */
243 if (badcom("write"))
244 goto commerr;
245 writepict(args);
246 break;
247 case 'q': /* quit */
248 if (badcom("quit"))
249 goto commerr;
250 quit(0);
251 case CTRL(C): /* interrupt */
252 goto again;
253 #ifdef SIGTSTP
254 case CTRL(Z): /* stop */
255 devclose();
256 kill(0, SIGTSTP);
257 /* pc stops here */
258 devopen(devname);
259 redraw();
260 break;
261 #endif
262 case '\0': /* continue */
263 break;
264 default:;
265 commerr:
266 if (iscntrl(inpbuf[0]))
267 sprintf(errmsg, "^%c: unknown control",
268 inpbuf[0]|0100);
269 else
270 sprintf(errmsg, "%s: unknown command", inpbuf);
271 error(COMMAND, errmsg);
272 break;
273 }
274 #undef badcom
275 }
276
277
278 rsample() /* sample the image */
279 {
280 int xsiz, ysiz, y;
281 RECT r;
282 PNODE *p;
283 register RECT *rl;
284 register PNODE **pl;
285 register int x;
286 /*
287 * We initialize the bottom row in the image at our current
288 * resolution. During sampling, we check super-pixels to the
289 * right and above by calling bigdiff(). If there is a significant
290 * difference, we subsample the super-pixels. The testing process
291 * includes initialization of the next row.
292 */
293 xsiz = (((pframe.r-pframe.l)<<pdepth)+hresolu-1) / hresolu;
294 ysiz = (((pframe.u-pframe.d)<<pdepth)+vresolu-1) / vresolu;
295 rl = (RECT *)malloc(xsiz*sizeof(RECT));
296 if (rl == NULL)
297 return;
298 pl = (PNODE **)malloc(xsiz*sizeof(PNODE *));
299 if (pl == NULL)
300 return;
301 /*
302 * Initialize the bottom row.
303 */
304 rl[0].l = rl[0].d = 0;
305 rl[0].r = hresolu; rl[0].u = vresolu;
306 pl[0] = findrect(pframe.l, pframe.d, &ptrunk, rl, pdepth);
307 for (x = 1; x < xsiz; x++) {
308 rl[x].l = rl[x].d = 0;
309 rl[x].r = hresolu; rl[x].u = vresolu;
310 pl[x] = findrect(pframe.l+((x*hresolu)>>pdepth),
311 pframe.d, &ptrunk, rl+x, pdepth);
312 }
313 /* sample the image */
314 for (y = 0; /* y < ysiz */ ; y++) {
315 for (x = 0; x < xsiz-1; x++) {
316 if (dev->inpready)
317 goto escape;
318 /*
319 * Test super-pixel to the right.
320 */
321 if (pl[x] != pl[x+1] && bigdiff(pl[x]->v,
322 pl[x+1]->v, maxdiff)) {
323 refine(pl[x], rl[x].l, rl[x].d,
324 rl[x].r, rl[x].u, 1);
325 refine(pl[x+1], rl[x+1].l, rl[x+1].d,
326 rl[x+1].r, rl[x+1].u, 1);
327 }
328 }
329 if (y >= ysiz-1)
330 break;
331 for (x = 0; x < xsiz; x++) {
332 if (dev->inpready)
333 goto escape;
334 /*
335 * Find super-pixel at this position in next row.
336 */
337 r.l = r.d = 0;
338 r.r = hresolu; r.u = vresolu;
339 p = findrect(pframe.l+((x*hresolu)>>pdepth),
340 pframe.d+(((y+1)*vresolu)>>pdepth),
341 &ptrunk, &r, pdepth);
342 /*
343 * Test super-pixel in next row.
344 */
345 if (pl[x] != p && bigdiff(pl[x]->v, p->v, maxdiff)) {
346 refine(pl[x], rl[x].l, rl[x].d,
347 rl[x].r, rl[x].u, 1);
348 refine(p, r.l, r.d, r.r, r.u, 1);
349 }
350 /*
351 * Copy into super-pixel array.
352 */
353 rl[x].l = r.l; rl[x].d = r.d;
354 rl[x].r = r.r; rl[x].u = r.u;
355 pl[x] = p;
356 }
357 }
358 escape:
359 free((char *)rl);
360 free((char *)pl);
361 }
362
363
364 int
365 refine(p, xmin, ymin, xmax, ymax, pd) /* refine a node */
366 register PNODE *p;
367 int xmin, ymin, xmax, ymax;
368 int pd;
369 {
370 int growth;
371 int mx, my;
372 int i;
373
374 if (dev->inpready) /* quit for input */
375 return(0);
376
377 if (pd <= 0) /* depth limit */
378 return(0);
379
380 mx = (xmin + xmax) >> 1;
381 my = (ymin + ymax) >> 1;
382 growth = 0;
383
384 if (p->kid == NULL) { /* subdivide */
385
386 if ((p->kid = newptree()) == NULL)
387 return(growth);
388 /*
389 * The following paint order can leave a black pixel
390 * when redraw() is called in (*dev->paintr)().
391 */
392 if (p->x >= mx && p->y >= my)
393 pcopy(p, p->kid+UR);
394 else
395 paint(p->kid+UR, mx, my, xmax, ymax);
396 if (p->x < mx && p->y >= my)
397 pcopy(p, p->kid+UL);
398 else
399 paint(p->kid+UL, xmin, my, mx, ymax);
400 if (p->x >= mx && p->y < my)
401 pcopy(p, p->kid+DR);
402 else
403 paint(p->kid+DR, mx, ymin, xmax, my);
404 if (p->x < mx && p->y < my)
405 pcopy(p, p->kid+DL);
406 else
407 paint(p->kid+DL, xmin, ymin, mx, my);
408
409 growth++;
410 }
411 /* do children */
412 if (mx > pframe.l) {
413 if (my > pframe.d)
414 growth += refine(p->kid+DL, xmin, ymin, mx, my, pd-1);
415 if (my < pframe.u)
416 growth += refine(p->kid+UL, xmin, my, mx, ymax, pd-1);
417 }
418 if (mx < pframe.r) {
419 if (my > pframe.d)
420 growth += refine(p->kid+DR, mx, ymin, xmax, my, pd-1);
421 if (my < pframe.u)
422 growth += refine(p->kid+UR, mx, my, xmax, ymax, pd-1);
423 }
424 /* recompute sum */
425 if (growth) {
426 setcolor(p->v, 0.0, 0.0, 0.0);
427 for (i = 0; i < 4; i++)
428 addcolor(p->v, p->kid[i].v);
429 scalecolor(p->v, 0.25);
430 }
431 return(growth);
432 }