ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 1.25
Committed: Tue May 21 17:41:35 1991 UTC (32 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.24: +9 -2 lines
Log Message:
added stratified random sampling with urand

File Contents

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