ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rview.c
Revision: 2.2
Committed: Mon Nov 25 10:11:01 1991 UTC (32 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +9 -0 lines
Log Message:
added "memory" command to report memory statistics (#define MSTATS)

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