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