ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rview.c
Revision: 1.12
Committed: Fri May 3 15:22:24 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.11: +31 -3 lines
Log Message:
added minimal memory recovery

File Contents

# User Rev Content
1 greg 1.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 greg 1.9 VIEW ourview = STDVIEW; /* viewing parameters */
24     int hresolu, vresolu; /* image resolution */
25 greg 1.1
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 greg 1.3 double shadthresh = .1; /* shadow threshold */
33 greg 1.4 double shadcert = .25; /* shadow certainty */
34 greg 1.1
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 greg 1.5 int ambres = 8; /* ambient resolution */
41 greg 1.1 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 greg 1.11 char *devname = dev_default; /* output device name */
49 greg 1.1
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 greg 1.12 static char *reserve_mem = NULL; /* pre-allocated reserve memory */
59    
60     #define RESERVE_AMT 8192 /* amount of memory to reserve */
61    
62 greg 1.1 #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 greg 1.6 extern char *progname, *octname;
77 greg 1.7 char *id;
78 greg 1.1 register int i;
79 greg 1.6
80 greg 1.7 id = octname!=NULL ? octname : progname;
81 greg 1.1 /* check device table */
82     for (i = 0; devtable[i].name; i++)
83     if (!strcmp(dname, devtable[i].name))
84 greg 1.7 if ((dev = (*devtable[i].init)(dname, id)) == NULL) {
85 greg 1.1 sprintf(errmsg, "cannot initialize %s", dname);
86     error(USER, errmsg);
87     } else
88     return;
89     /* not there, try exec */
90 greg 1.7 if ((dev = comm_init(dname, id)) == NULL) {
91 greg 1.1 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(); /* set up image */
120    
121     for ( ; ; ) { /* quit in command() */
122 greg 1.9 while (hresolu <= 1<<pdepth &&
123     vresolu <= 1<<pdepth)
124 greg 1.1 command("done: ");
125    
126 greg 1.9 if (hresolu <= psample<<pdepth &&
127     vresolu <= psample<<pdepth) {
128 greg 1.1 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 greg 1.9 refine(&ptrunk, 0, 0, hresolu, vresolu, pdepth+1);
135 greg 1.1 }
136     if (dev->inpready)
137     command(": ");
138     else
139     pdepth++;
140     }
141     }
142    
143    
144 greg 1.12 memreserve() /* fill memory reserves */
145     {
146     if (reserve_mem != NULL)
147     return; /* got some already */
148     reserve_mem = malloc(RESERVE_AMT);
149     }
150    
151    
152     memerror(detail) /* try and rescue a memory error */
153     char *detail;
154     {
155     if (reserve_mem == NULL) {
156     sprintf(errmsg, "out of memory %s", detail);
157     error(SYSTEM, errmsg);
158     }
159     free(reserve_mem);
160     reserve_mem = NULL;
161     for ( ; ; )
162     command("out of memory: ");
163     }
164    
165    
166 greg 1.1 command(prompt) /* get/execute command */
167     char *prompt;
168     {
169     #define badcom(s) strncmp(s, inpbuf, args-inpbuf-1)
170     double atof();
171     char inpbuf[256];
172     char *args;
173     again:
174 greg 1.10 (*dev->comin)(inpbuf, prompt); /* get command + arguments */
175 greg 1.1 for (args = inpbuf; *args && *args != ' '; args++)
176     ;
177     if (*args) *args++ = '\0';
178     else *++args = '\0';
179    
180     switch (inpbuf[0]) {
181     case 'f': /* new frame */
182     if (badcom("frame"))
183     goto commerr;
184     getframe(args);
185     break;
186     case 'v': /* view */
187     if (badcom("view"))
188     goto commerr;
189     getview(args);
190     break;
191     case 'l': /* last view */
192     if (badcom("last"))
193     goto commerr;
194     lastview(args);
195     break;
196     case 'e': /* exposure */
197     if (badcom("exposure"))
198     goto commerr;
199     getexposure(args);
200     break;
201     case 's': /* set a parameter */
202     if (badcom("set"))
203     goto commerr;
204     setparam(args);
205     break;
206     case 'n': /* new picture */
207     if (badcom("new"))
208     goto commerr;
209     newimage();
210     break;
211     case 't': /* trace a ray */
212     if (badcom("trace"))
213     goto commerr;
214     traceray(args);
215     break;
216     case 'a': /* aim camera */
217     if (badcom("aim"))
218     goto commerr;
219     getaim(args);
220     break;
221     case 'm': /* move camera */
222     if (badcom("move"))
223     goto commerr;
224     getmove(args);
225     break;
226 greg 1.8 case 'r': /* rotate/repaint */
227     if (badcom("rotate")) {
228     if (badcom("repaint"))
229     goto commerr;
230     getrepaint(args);
231     break;
232     }
233 greg 1.1 getrotate(args);
234     break;
235     case 'p': /* pivot view */
236     if (badcom("pivot"))
237     goto commerr;
238     getpivot(args);
239     break;
240     case CTRL(R): /* redraw */
241     redraw();
242     break;
243     case 'w': /* write */
244     if (badcom("write"))
245     goto commerr;
246     writepict(args);
247     break;
248     case 'q': /* quit */
249     if (badcom("quit"))
250     goto commerr;
251     quit(0);
252     case CTRL(C): /* interrupt */
253     goto again;
254     #ifdef SIGTSTP
255     case CTRL(Z): /* stop */
256     devclose();
257     kill(0, SIGTSTP);
258     /* pc stops here */
259     devopen(devname);
260     redraw();
261     break;
262     #endif
263     case '\0': /* continue */
264     break;
265     default:;
266     commerr:
267     if (iscntrl(inpbuf[0]))
268     sprintf(errmsg, "^%c: unknown control",
269     inpbuf[0]|0100);
270     else
271     sprintf(errmsg, "%s: unknown command", inpbuf);
272     error(COMMAND, errmsg);
273     break;
274     }
275     #undef badcom
276     }
277    
278    
279     rsample() /* sample the image */
280     {
281     int xsiz, ysiz, y;
282     RECT r;
283     PNODE *p;
284     register RECT *rl;
285     register PNODE **pl;
286     register int x;
287     /*
288     * We initialize the bottom row in the image at our current
289     * resolution. During sampling, we check super-pixels to the
290     * right and above by calling bigdiff(). If there is a significant
291     * difference, we subsample the super-pixels. The testing process
292     * includes initialization of the next row.
293     */
294 greg 1.9 xsiz = (((pframe.r-pframe.l)<<pdepth)+hresolu-1) / hresolu;
295     ysiz = (((pframe.u-pframe.d)<<pdepth)+vresolu-1) / vresolu;
296 greg 1.1 rl = (RECT *)malloc(xsiz*sizeof(RECT));
297 greg 1.12 if (rl == NULL)
298     memerror("in rsample");
299 greg 1.1 pl = (PNODE **)malloc(xsiz*sizeof(PNODE *));
300 greg 1.12 if (pl == NULL)
301     memerror("in rsample");
302 greg 1.1 /*
303     * Initialize the bottom row.
304     */
305     rl[0].l = rl[0].d = 0;
306 greg 1.9 rl[0].r = hresolu; rl[0].u = vresolu;
307 greg 1.1 pl[0] = findrect(pframe.l, pframe.d, &ptrunk, rl, pdepth);
308     for (x = 1; x < xsiz; x++) {
309     rl[x].l = rl[x].d = 0;
310 greg 1.9 rl[x].r = hresolu; rl[x].u = vresolu;
311     pl[x] = findrect(pframe.l+((x*hresolu)>>pdepth),
312 greg 1.1 pframe.d, &ptrunk, rl+x, pdepth);
313     }
314     /* sample the image */
315     for (y = 0; /* y < ysiz */ ; y++) {
316     for (x = 0; x < xsiz-1; x++) {
317     if (dev->inpready)
318     goto escape;
319     /*
320     * Test super-pixel to the right.
321     */
322     if (pl[x] != pl[x+1] && bigdiff(pl[x]->v,
323     pl[x+1]->v, maxdiff)) {
324     refine(pl[x], rl[x].l, rl[x].d,
325     rl[x].r, rl[x].u, 1);
326     refine(pl[x+1], rl[x+1].l, rl[x+1].d,
327     rl[x+1].r, rl[x+1].u, 1);
328     }
329     }
330     if (y >= ysiz-1)
331     break;
332     for (x = 0; x < xsiz; x++) {
333     if (dev->inpready)
334     goto escape;
335     /*
336     * Find super-pixel at this position in next row.
337     */
338     r.l = r.d = 0;
339 greg 1.9 r.r = hresolu; r.u = vresolu;
340     p = findrect(pframe.l+((x*hresolu)>>pdepth),
341     pframe.d+(((y+1)*vresolu)>>pdepth),
342 greg 1.1 &ptrunk, &r, pdepth);
343     /*
344     * Test super-pixel in next row.
345     */
346     if (pl[x] != p && bigdiff(pl[x]->v, p->v, maxdiff)) {
347     refine(pl[x], rl[x].l, rl[x].d,
348     rl[x].r, rl[x].u, 1);
349     refine(p, r.l, r.d, r.r, r.u, 1);
350     }
351     /*
352     * Copy into super-pixel array.
353     */
354     rl[x].l = r.l; rl[x].d = r.d;
355     rl[x].r = r.r; rl[x].u = r.u;
356     pl[x] = p;
357     }
358     }
359     escape:
360     free((char *)rl);
361     free((char *)pl);
362     }
363    
364    
365     int
366     refine(p, xmin, ymin, xmax, ymax, pd) /* refine a node */
367     register PNODE *p;
368     int xmin, ymin, xmax, ymax;
369     int pd;
370     {
371     int growth;
372     int mx, my;
373     int i;
374    
375     if (dev->inpready) /* quit for input */
376     return(0);
377    
378     if (pd <= 0) /* depth limit */
379     return(0);
380    
381     mx = (xmin + xmax) >> 1;
382     my = (ymin + ymax) >> 1;
383     growth = 0;
384    
385     if (p->kid == NULL) { /* subdivide */
386    
387     if ((p->kid = newptree()) == NULL)
388 greg 1.12 memerror("in refine");
389 greg 1.1 /*
390     * The following paint order can leave a black pixel
391     * when redraw() is called in (*dev->paintr)().
392     */
393     if (p->x >= mx && p->y >= my)
394     pcopy(p, p->kid+UR);
395     else
396     paint(p->kid+UR, mx, my, xmax, ymax);
397     if (p->x < mx && p->y >= my)
398     pcopy(p, p->kid+UL);
399     else
400     paint(p->kid+UL, xmin, my, mx, ymax);
401     if (p->x >= mx && p->y < my)
402     pcopy(p, p->kid+DR);
403     else
404     paint(p->kid+DR, mx, ymin, xmax, my);
405     if (p->x < mx && p->y < my)
406     pcopy(p, p->kid+DL);
407     else
408     paint(p->kid+DL, xmin, ymin, mx, my);
409    
410     growth++;
411     }
412     /* do children */
413     if (mx > pframe.l) {
414     if (my > pframe.d)
415     growth += refine(p->kid+DL, xmin, ymin, mx, my, pd-1);
416     if (my < pframe.u)
417     growth += refine(p->kid+UL, xmin, my, mx, ymax, pd-1);
418     }
419     if (mx < pframe.r) {
420     if (my > pframe.d)
421     growth += refine(p->kid+DR, mx, ymin, xmax, my, pd-1);
422     if (my < pframe.u)
423     growth += refine(p->kid+UR, mx, my, xmax, ymax, pd-1);
424     }
425     /* recompute sum */
426     if (growth) {
427     setcolor(p->v, 0.0, 0.0, 0.0);
428     for (i = 0; i < 4; i++)
429     addcolor(p->v, p->kid[i].v);
430     scalecolor(p->v, 0.25);
431     }
432     return(growth);
433     }