ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 1.15
Committed: Fri Jan 12 09:27:11 1990 UTC (34 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.14: +23 -35 lines
Log Message:
simplified residual calculation

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1986 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * rpict.c - routines and variables for picture generation.
9     *
10     * 8/14/85
11     */
12    
13     #include "ray.h"
14    
15     #ifdef BSD
16     #include <sys/time.h>
17     #include <sys/resource.h>
18 greg 1.14 #else
19     #include <signal.h>
20 greg 1.1 #endif
21    
22     #include "view.h"
23    
24     #include "random.h"
25    
26 greg 1.12 VIEW ourview = STDVIEW; /* view parameters */
27     int hresolu = 512; /* horizontal resolution */
28     int vresolu = 512; /* vertical resolution */
29 greg 1.13 double pixaspect = 1.0; /* pixel aspect ratio */
30 greg 1.1
31     int psample = 4; /* pixel sample size */
32     double maxdiff = .05; /* max. difference for interpolation */
33 greg 1.3 double dstrpix = 0.67; /* square pixel distribution */
34 greg 1.1
35     double dstrsrc = 0.0; /* square source distribution */
36 greg 1.4 double shadthresh = .05; /* shadow threshold */
37 greg 1.5 double shadcert = .5; /* shadow certainty */
38 greg 1.1
39     int maxdepth = 6; /* maximum recursion depth */
40     double minweight = 5e-3; /* minimum ray weight */
41    
42     COLOR ambval = BLKCOLOR; /* ambient value */
43     double ambacc = 0.2; /* ambient accuracy */
44 greg 1.6 int ambres = 32; /* ambient resolution */
45 greg 1.1 int ambdiv = 128; /* ambient divisions */
46     int ambssamp = 0; /* ambient super-samples */
47     int ambounce = 0; /* ambient bounces */
48     char *amblist[128]; /* ambient include/exclude list */
49     int ambincl = -1; /* include == 1, exclude == 0 */
50    
51     int ralrm = 0; /* seconds between reports */
52    
53     double pctdone = 0.0; /* percentage done */
54    
55     extern long nrays; /* number of rays traced */
56    
57     #define MAXDIV 32 /* maximum sample size */
58    
59     #define pixjitter() (.5+dstrpix*(.5-frandom()))
60    
61 greg 1.11 double pixvalue();
62 greg 1.1
63 greg 1.11
64 greg 1.1 quit(code) /* quit program */
65     int code;
66     {
67     if (code || ralrm > 0) /* report status */
68     report();
69    
70     exit(code);
71     }
72    
73    
74     report() /* report progress */
75     {
76     #ifdef BSD
77     struct rusage rubuf;
78     double t;
79    
80     getrusage(RUSAGE_SELF, &rubuf);
81     t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
82     t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
83     getrusage(RUSAGE_CHILDREN, &rubuf);
84     t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
85     t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
86    
87     sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
88     nrays, pctdone, t/3600.0);
89     #else
90 greg 1.14 signal(SIGALRM, report);
91 greg 1.1 sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone);
92     #endif
93     eputs(errmsg);
94    
95     if (ralrm > 0)
96     alarm(ralrm);
97     }
98    
99    
100 greg 1.11 render(zfile, oldfile) /* render the scene */
101     char *zfile, *oldfile;
102 greg 1.1 {
103     COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
104 greg 1.11 float *zbar[MAXDIV+1]; /* z values */
105 greg 1.1 int ypos; /* current scanline */
106 greg 1.15 int ystep; /* current y step size */
107 greg 1.11 FILE *zfp;
108 greg 1.1 COLOR *colptr;
109 greg 1.11 float *zptr;
110 greg 1.1 register int i;
111 greg 1.10 /* check sampling */
112     if (psample < 1)
113 greg 1.1 psample = 1;
114 greg 1.10 else if (psample > MAXDIV)
115     psample = MAXDIV;
116 greg 1.9 /* allocate scanlines */
117 greg 1.1 for (i = 0; i <= psample; i++) {
118 greg 1.12 scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
119 greg 1.1 if (scanbar[i] == NULL)
120 greg 1.11 goto memerr;
121 greg 1.1 }
122 greg 1.11 /* open z file */
123     if (zfile != NULL) {
124     if ((zfp = fopen(zfile, "a+")) == NULL) {
125     sprintf(errmsg, "cannot open z file \"%s\"", zfile);
126     error(SYSTEM, errmsg);
127     }
128     for (i = 0; i <= psample; i++) {
129 greg 1.12 zbar[i] = (float *)malloc(hresolu*sizeof(float));
130 greg 1.11 if (zbar[i] == NULL)
131     goto memerr;
132     }
133     } else {
134     zfp = NULL;
135     for (i = 0; i <= psample; i++)
136     zbar[i] = NULL;
137     }
138 greg 1.1 /* write out boundaries */
139 greg 1.12 fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
140 greg 1.10 /* recover file and compute first */
141 greg 1.11 i = salvage(oldfile);
142 greg 1.12 if (zfp != NULL && fseek(zfp, (long)i*hresolu*sizeof(float), 0) == EOF)
143 greg 1.11 error(SYSTEM, "z file seek error in render");
144 greg 1.12 ypos = vresolu-1 - i;
145     fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
146 greg 1.15 ystep = psample;
147 greg 1.10 /* compute scanlines */
148 greg 1.15 for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
149     /* record progress */
150     pctdone = 100.0*(vresolu-ypos-ystep)/vresolu;
151     /* bottom adjust? */
152     if (ypos < 0) {
153     ystep += ypos;
154     ypos = 0;
155     }
156     colptr = scanbar[ystep]; /* move base to top */
157     scanbar[ystep] = scanbar[0];
158 greg 1.1 scanbar[0] = colptr;
159 greg 1.15 zptr = zbar[ystep];
160     zbar[ystep] = zbar[0];
161 greg 1.11 zbar[0] = zptr;
162 greg 1.10 /* fill base line */
163 greg 1.12 fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
164 greg 1.10 /* fill bar */
165 greg 1.15 fillscanbar(scanbar, zbar, hresolu, ypos, ystep);
166 greg 1.10 /* write it out */
167 greg 1.15 for (i = ystep; i > 0; i--) {
168 greg 1.12 if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu)
169 greg 1.1 goto writerr;
170 greg 1.12 if (fwritescan(scanbar[i],hresolu,stdout) < 0)
171 greg 1.11 goto writerr;
172     }
173     if (zfp != NULL && fflush(zfp) == EOF)
174     goto writerr;
175 greg 1.1 if (fflush(stdout) == EOF)
176     goto writerr;
177     }
178 greg 1.11 /* clean up */
179     if (zfp != NULL) {
180 greg 1.15 fwrite(zbar[0], sizeof(float), hresolu, zfp);
181 greg 1.11 if (fclose(zfp) == EOF)
182     goto writerr;
183     for (i = 0; i <= psample; i++)
184     free((char *)zbar[i]);
185     }
186 greg 1.15 fwritescan(scanbar[0], hresolu, stdout);
187 greg 1.10 if (fflush(stdout) == EOF)
188     goto writerr;
189 greg 1.1 for (i = 0; i <= psample; i++)
190     free((char *)scanbar[i]);
191 greg 1.11 pctdone = 100.0;
192 greg 1.1 return;
193     writerr:
194     error(SYSTEM, "write error in render");
195 greg 1.11 memerr:
196     error(SYSTEM, "out of memory in render");
197 greg 1.1 }
198    
199    
200 greg 1.11 fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */
201 greg 1.1 register COLOR *scanline;
202 greg 1.11 register float *zline;
203 greg 1.9 int xres, y, xstep;
204 greg 1.1 {
205     int b = xstep;
206 greg 1.11 double z;
207 greg 1.1 register int i;
208    
209 greg 1.11 z = pixvalue(scanline[0], 0, y);
210     if (zline) zline[0] = z;
211 greg 1.1
212 greg 1.15 for (i = xstep; i < xres-1+xstep; i += xstep) {
213     if (i >= xres) {
214     xstep += xres-1-i;
215     i = xres-1;
216     }
217 greg 1.11 z = pixvalue(scanline[i], i, y);
218     if (zline) zline[i] = z;
219 greg 1.1
220 greg 1.11 b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
221     i-xstep, y, xstep, 0, b/2);
222 greg 1.1 }
223     }
224    
225    
226 greg 1.11 fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */
227 greg 1.1 register COLOR *scanbar[];
228 greg 1.11 register float *zbar[];
229 greg 1.9 int xres, y, ysize;
230 greg 1.1 {
231     COLOR vline[MAXDIV+1];
232 greg 1.11 float zline[MAXDIV+1];
233 greg 1.1 int b = ysize;
234 greg 1.11 double z;
235 greg 1.1 register int i, j;
236    
237 greg 1.8 for (i = 0; i < xres; i++) {
238 greg 1.1
239     copycolor(vline[0], scanbar[0][i]);
240     copycolor(vline[ysize], scanbar[ysize][i]);
241 greg 1.11 if (zbar[0]) {
242     zline[0] = zbar[0][i];
243     zline[ysize] = zbar[ysize][i];
244     }
245 greg 1.1
246 greg 1.11 b = fillsample(vline, zbar[0] ? zline : NULL,
247     i, y, 0, ysize, b/2);
248 greg 1.1
249     for (j = 1; j < ysize; j++)
250     copycolor(scanbar[j][i], vline[j]);
251 greg 1.11 if (zbar[0])
252     for (j = 1; j < ysize; j++)
253     zbar[j][i] = zline[j];
254 greg 1.1 }
255     }
256    
257    
258     int
259 greg 1.11 fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
260 greg 1.1 register COLOR *colline;
261 greg 1.11 register float *zline;
262 greg 1.1 int x, y;
263     int xlen, ylen;
264     int b;
265     {
266 greg 1.11 extern double fabs();
267 greg 1.1 double ratio;
268 greg 1.11 double z;
269 greg 1.1 COLOR ctmp;
270     int ncut;
271     register int len;
272    
273     if (xlen > 0) /* x or y length is zero */
274     len = xlen;
275     else
276     len = ylen;
277    
278     if (len <= 1) /* limit recursion */
279     return(0);
280    
281 greg 1.11 if (b > 0
282     || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
283     || bigdiff(colline[0], colline[len], maxdiff)) {
284 greg 1.1
285 greg 1.11 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
286     if (zline) zline[len>>1] = z;
287 greg 1.1 ncut = 1;
288    
289     } else { /* interpolate */
290    
291     copycolor(colline[len>>1], colline[len]);
292     ratio = (double)(len>>1) / len;
293     scalecolor(colline[len>>1], ratio);
294 greg 1.11 if (zline) zline[len>>1] = zline[len] * ratio;
295     ratio = 1.0 - ratio;
296 greg 1.1 copycolor(ctmp, colline[0]);
297     scalecolor(ctmp, ratio);
298     addcolor(colline[len>>1], ctmp);
299 greg 1.11 if (zline) zline[len>>1] += zline[0] * ratio;
300 greg 1.1 ncut = 0;
301     }
302     /* recurse */
303 greg 1.11 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
304 greg 1.1
305 greg 1.11 ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
306     x+(xlen>>1), y+(ylen>>1),
307     xlen-(xlen>>1), ylen-(ylen>>1), b/2);
308 greg 1.1
309     return(ncut);
310     }
311    
312    
313 greg 1.11 double
314 greg 1.1 pixvalue(col, x, y) /* compute pixel value */
315     COLOR col; /* returned color */
316     int x, y; /* pixel position */
317     {
318     static RAY thisray; /* our ray for this pixel */
319    
320 greg 1.12 viewray(thisray.rorg, thisray.rdir, &ourview,
321     (x+pixjitter())/hresolu, (y+pixjitter())/vresolu);
322 greg 1.1
323     rayorigin(&thisray, NULL, PRIMARY, 1.0);
324    
325     rayvalue(&thisray); /* trace ray */
326    
327     copycolor(col, thisray.rcol); /* return color */
328 greg 1.11
329     return(thisray.rot); /* return distance */
330 greg 1.1 }
331    
332    
333     int
334     salvage(oldfile) /* salvage scanlines from killed program */
335     char *oldfile;
336     {
337     COLR *scanline;
338     FILE *fp;
339     int x, y;
340    
341     if (oldfile == NULL)
342     return(0);
343 greg 1.9
344     if ((fp = fopen(oldfile, "r")) == NULL) {
345 greg 1.1 sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
346     error(WARNING, errmsg);
347     return(0);
348     }
349     /* discard header */
350     getheader(fp, NULL);
351     /* get picture size */
352 greg 1.7 if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) {
353 greg 1.2 sprintf(errmsg, "bad recover file \"%s\"", oldfile);
354     error(WARNING, errmsg);
355 greg 1.1 fclose(fp);
356     return(0);
357     }
358    
359 greg 1.12 if (x != hresolu || y != vresolu) {
360 greg 1.1 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
361     oldfile);
362     error(USER, errmsg);
363     }
364    
365 greg 1.12 scanline = (COLR *)malloc(hresolu*sizeof(COLR));
366 greg 1.1 if (scanline == NULL)
367     error(SYSTEM, "out of memory in salvage");
368 greg 1.12 for (y = 0; y < vresolu; y++) {
369     if (freadcolrs(scanline, hresolu, fp) < 0)
370 greg 1.1 break;
371 greg 1.12 if (fwritecolrs(scanline, hresolu, stdout) < 0)
372 greg 1.1 goto writerr;
373     }
374     if (fflush(stdout) == EOF)
375     goto writerr;
376     free((char *)scanline);
377     fclose(fp);
378     unlink(oldfile);
379     return(y);
380     writerr:
381     error(SYSTEM, "write error in salvage");
382     }