ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.4
Committed: Sat Dec 28 18:43:15 1991 UTC (32 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +1 -1 lines
Log Message:
minor change to signal handling under System V

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