ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rview.c
Revision: 1.14
Committed: Fri May 3 16:12:34 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.13: +19 -27 lines
Log Message:
final version of memory recovery during image refinement

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