ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.13
Committed: Tue Sep 8 20:56:20 1992 UTC (31 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.12: +2 -0 lines
Log Message:
put back in final report() call

File Contents

# Content
1 /* Copyright (c) 1992 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 #endif
19
20 #include <signal.h>
21 #include <fcntl.h>
22
23 #include "view.h"
24
25 #include "resolu.h"
26
27 #include "random.h"
28
29 #include "paths.h"
30
31 int dimlist[MAXDIM]; /* sampling dimensions */
32 int ndims = 0; /* number of sampling dimensions */
33 int samplendx; /* sample index number */
34
35 VIEW ourview = STDVIEW; /* view parameters */
36 int hresolu = 512; /* horizontal resolution */
37 int vresolu = 512; /* vertical resolution */
38 double pixaspect = 1.0; /* pixel aspect ratio */
39
40 int psample = 4; /* pixel sample size */
41 double maxdiff = .05; /* max. difference for interpolation */
42 double dstrpix = 0.67; /* square pixel distribution */
43
44 double dstrsrc = 0.0; /* square source distribution */
45 double shadthresh = .05; /* shadow threshold */
46 double shadcert = .5; /* shadow certainty */
47 int directrelay = 1; /* number of source relays */
48 int vspretest = 512; /* virtual source pretest density */
49 int directinvis = 0; /* sources invisible? */
50 double srcsizerat = .25; /* maximum ratio source size/dist. */
51
52 double specthresh = .15; /* specular sampling threshold */
53 double specjitter = 1.; /* specular sampling jitter */
54
55 int maxdepth = 6; /* maximum recursion depth */
56 double minweight = 5e-3; /* minimum ray weight */
57
58 COLOR ambval = BLKCOLOR; /* ambient value */
59 double ambacc = 0.2; /* ambient accuracy */
60 int ambres = 32; /* ambient resolution */
61 int ambdiv = 128; /* ambient divisions */
62 int ambssamp = 0; /* ambient super-samples */
63 int ambounce = 0; /* ambient bounces */
64 char *amblist[128]; /* ambient include/exclude list */
65 int ambincl = -1; /* include == 1, exclude == 0 */
66
67 int ralrm = 0; /* seconds between reports */
68
69 double pctdone = 0.0; /* percentage done */
70
71 long tlastrept = 0L; /* time at last report */
72
73 extern long time();
74 extern long tstart; /* starting time */
75
76 extern long nrays; /* number of rays traced */
77
78 #define MAXDIV 16 /* maximum sample size */
79
80 #define pixjitter() (.5+dstrpix*(.5-frandom()))
81
82 #define RFTEMPLATE "rfXXXXXX"
83 #define HFTEMPLATE TEMPLATE
84
85 static char *hfname = NULL; /* header file name */
86 static FILE *hfp = NULL; /* header file pointer */
87
88 static int hres, vres; /* resolution for this frame */
89
90 extern char *mktemp();
91
92 double pixvalue();
93
94
95 quit(code) /* quit program */
96 int code;
97 {
98 if (code) /* report status */
99 report();
100 if (hfname != NULL) { /* delete header file */
101 if (hfp != NULL)
102 fclose(hfp);
103 unlink(hfname);
104 }
105 exit(code);
106 }
107
108
109 #ifdef BSD
110 report() /* report progress */
111 {
112 struct rusage rubuf;
113 double t;
114
115 getrusage(RUSAGE_SELF, &rubuf);
116 t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
117 t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
118 getrusage(RUSAGE_CHILDREN, &rubuf);
119 t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
120 t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
121
122 sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
123 nrays, pctdone, t/3600.0);
124 eputs(errmsg);
125 tlastrept = time((long *)0);
126 }
127 #else
128 report() /* report progress */
129 {
130 tlastrept = time((long *)0);
131 sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n",
132 nrays, pctdone, (tlastrept-tstart)/3600.0);
133 eputs(errmsg);
134 signal(SIGALRM, report);
135 }
136 #endif
137
138
139 openheader() /* save standard output to header file */
140 {
141 hfname = mktemp(HFTEMPLATE);
142 if (freopen(hfname, "w", stdout) == NULL) {
143 sprintf(errmsg, "cannot open header file \"%s\"", hfname);
144 error(SYSTEM, errmsg);
145 }
146 }
147
148
149 closeheader() /* done with header output */
150 {
151 if (hfname == NULL)
152 return;
153 if (fflush(stdout) == EOF || (hfp = fopen(hfname, "r")) == NULL)
154 error(SYSTEM, "error reopening header file");
155 }
156
157
158 dupheader() /* repeat header on standard output */
159 {
160 register int c;
161
162 if (fseek(hfp, 0L, 0) < 0)
163 error(SYSTEM, "seek error on header file");
164 while ((c = getc(hfp)) != EOF)
165 putchar(c);
166 }
167
168
169 rpict(seq, pout, zout, prvr) /* generate image(s) */
170 int seq;
171 char *pout, *zout, *prvr;
172 /*
173 * If seq is greater than zero, then we will render a sequence of
174 * images based on view parameter strings read from the standard input.
175 * If pout is NULL, then all images will be sent to the standard ouput.
176 * If seq is greater than zero and prvr is an integer, then it is the
177 * frame number at which rendering should begin. Preceeding view parameter
178 * strings will be skipped in the input.
179 * If pout and prvr are the same, prvr is renamed to avoid overwriting.
180 * Note that pout and zout should contain %d format specifications for
181 * sequenced file naming.
182 */
183 {
184 extern char *rindex(), *strncpy(), *strcat();
185 char fbuf[128], fbuf2[128];
186 register char *cp;
187 RESOLU rs;
188 double pa;
189 /* finished writing header */
190 closeheader();
191 /* check sampling */
192 if (psample < 1)
193 psample = 1;
194 else if (psample > MAXDIV) {
195 sprintf(errmsg, "pixel sampling reduced from %d to %d",
196 psample, MAXDIV);
197 error(WARNING, errmsg);
198 psample = MAXDIV;
199 }
200 /* get starting frame */
201 if (seq <= 0)
202 seq = 0;
203 else if (prvr != NULL && isint(prvr)) {
204 register int rn; /* skip to specified view */
205 if ((rn = atoi(prvr)) < seq)
206 error(USER, "recover frame less than start frame");
207 if (pout == NULL)
208 error(USER, "missing output file specification");
209 for ( ; seq < rn; seq++)
210 if (nextview(stdin) == EOF)
211 error(USER, "unexpected EOF on view input");
212 prvr = fbuf; /* mark for renaming */
213 }
214 if (pout != NULL) {
215 sprintf(fbuf, pout, seq);
216 if (prvr != NULL && !strcmp(prvr, fbuf)) { /* rename */
217 fbuf2[0] = '\0';
218 if ((cp = rindex(fbuf, '/')) != NULL)
219 strncpy(fbuf2, fbuf, cp-fbuf+1);
220 strcat(fbuf2, RFTEMPLATE);
221 prvr = mktemp(fbuf2);
222 if (rename(fbuf, prvr) < 0 && errno != ENOENT) {
223 sprintf(errmsg,
224 "cannot rename \"%s\" to \"%s\"",
225 fbuf, prvr);
226 error(SYSTEM, errmsg);
227 }
228 }
229 }
230 /* render sequence */
231 do {
232 if (seq && nextview(stdin) == EOF)
233 break;
234 if (pout != NULL) {
235 sprintf(fbuf, pout, seq);
236 if (freopen(fbuf, "w", stdout) == NULL) {
237 sprintf(errmsg,
238 "cannot open output file \"%s\"", fbuf);
239 error(SYSTEM, errmsg);
240 }
241 dupheader();
242 }
243 hres = hresolu; vres = vresolu; pa = pixaspect;
244 if (prvr != NULL)
245 if (viewfile(prvr, &ourview, &rs) <= 0
246 || rs.or != PIXSTANDARD) {
247 sprintf(errmsg,
248 "cannot recover view parameters from \"%s\"", prvr);
249 error(WARNING, errmsg);
250 } else {
251 pa = 0.0;
252 hres = scanlen(&rs);
253 vres = numscans(&rs);
254 }
255 if ((cp = setview(&ourview)) != NULL)
256 error(USER, cp);
257 normaspect(viewaspect(&ourview), &pa, &hres, &vres);
258 if (seq) {
259 if (ralrm > 0) {
260 fprintf(stderr, "FRAME %d:", seq);
261 fprintview(&ourview, stderr);
262 putc('\n', stderr);
263 fflush(stderr);
264 }
265 printf("FRAME=%d\n", seq);
266 }
267 fputs(VIEWSTR, stdout);
268 fprintview(&ourview, stdout);
269 putchar('\n');
270 if (pa < .99 || pa > 1.01)
271 fputaspect(pa, stdout);
272 fputformat(COLRFMT, stdout);
273 putchar('\n');
274 if (zout != NULL)
275 sprintf(cp=fbuf, zout, seq);
276 else
277 cp = NULL;
278 render(cp, prvr);
279 prvr = NULL;
280 } while (seq++);
281 }
282
283
284 nextview(fp) /* get next view from fp */
285 FILE *fp;
286 {
287 char linebuf[256];
288
289 while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
290 if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
291 return(0);
292 return(EOF);
293 }
294
295
296 render(zfile, oldfile) /* render the scene */
297 char *zfile, *oldfile;
298 {
299 extern long lseek();
300 COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
301 float *zbar[MAXDIV+1]; /* z values */
302 char *sampdens; /* previous sample density */
303 int ypos; /* current scanline */
304 int ystep; /* current y step size */
305 int hstep; /* h step size */
306 int zfd;
307 COLOR *colptr;
308 float *zptr;
309 register int i;
310 /* allocate scanlines */
311 for (i = 0; i <= psample; i++) {
312 scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
313 if (scanbar[i] == NULL)
314 goto memerr;
315 }
316 hstep = (psample*140+49)/99; /* quincunx sampling */
317 ystep = (psample*99+70)/140;
318 if (hstep > 2) {
319 i = hres/hstep + 2;
320 if ((sampdens = malloc(i)) == NULL)
321 goto memerr;
322 while (i--)
323 sampdens[i] = hstep;
324 } else
325 sampdens = NULL;
326 /* open z file */
327 if (zfile != NULL) {
328 if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
329 sprintf(errmsg, "cannot open z file \"%s\"", zfile);
330 error(SYSTEM, errmsg);
331 }
332 for (i = 0; i <= psample; i++) {
333 zbar[i] = (float *)malloc(hres*sizeof(float));
334 if (zbar[i] == NULL)
335 goto memerr;
336 }
337 } else {
338 zfd = -1;
339 for (i = 0; i <= psample; i++)
340 zbar[i] = NULL;
341 }
342 /* write out boundaries */
343 fprtresolu(hres, vres, stdout);
344 /* recover file and compute first */
345 i = salvage(oldfile);
346 if (zfd != -1 && i > 0 &&
347 lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
348 error(SYSTEM, "z file seek error in render");
349 pctdone = 100.0*i/vres;
350 if (ralrm > 0) /* report init stats */
351 report();
352 #ifndef BSD
353 else
354 #endif
355 signal(SIGALRM, report);
356 ypos = vres-1 - i;
357 fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
358 /* compute scanlines */
359 for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
360 /* bottom adjust? */
361 if (ypos < 0) {
362 ystep += ypos;
363 ypos = 0;
364 }
365 colptr = scanbar[ystep]; /* move base to top */
366 scanbar[ystep] = scanbar[0];
367 scanbar[0] = colptr;
368 zptr = zbar[ystep];
369 zbar[ystep] = zbar[0];
370 zbar[0] = zptr;
371 /* fill base line */
372 fillscanline(scanbar[0], zbar[0], sampdens,
373 hres, ypos, hstep);
374 /* fill bar */
375 fillscanbar(scanbar, zbar, hres, ypos, ystep);
376 /* write it out */
377 #ifndef BSD
378 signal(SIGALRM, SIG_IGN); /* don't interrupt writes */
379 #endif
380 for (i = ystep; i > 0; i--) {
381 if (zfd != -1 && write(zfd, (char *)zbar[i],
382 hres*sizeof(float))
383 < hres*sizeof(float))
384 goto writerr;
385 if (fwritescan(scanbar[i], hres, stdout) < 0)
386 goto writerr;
387 }
388 if (fflush(stdout) == EOF)
389 goto writerr;
390 /* record progress */
391 pctdone = 100.0*(vres-1-ypos)/vres;
392 if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
393 report();
394 #ifndef BSD
395 else
396 signal(SIGALRM, report);
397 #endif
398 }
399 /* clean up */
400 signal(SIGALRM, SIG_IGN);
401 if (zfd != -1) {
402 if (write(zfd, (char *)zbar[0], hres*sizeof(float))
403 < hres*sizeof(float))
404 goto writerr;
405 if (close(zfd) == -1)
406 goto writerr;
407 for (i = 0; i <= psample; i++)
408 free((char *)zbar[i]);
409 }
410 fwritescan(scanbar[0], hres, stdout);
411 if (fflush(stdout) == EOF)
412 goto writerr;
413 for (i = 0; i <= psample; i++)
414 free((char *)scanbar[i]);
415 if (sampdens != NULL)
416 free(sampdens);
417 pctdone = 100.0;
418 if (ralrm > 0)
419 report();
420 return;
421 writerr:
422 error(SYSTEM, "write error in render");
423 memerr:
424 error(SYSTEM, "out of memory in render");
425 }
426
427
428 fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */
429 register COLOR *scanline;
430 register float *zline;
431 register char *sd;
432 int xres, y, xstep;
433 {
434 static int nc = 0; /* number of calls */
435 int bl = xstep, b = xstep;
436 double z;
437 register int i;
438
439 z = pixvalue(scanline[0], 0, y);
440 if (zline) zline[0] = z;
441 /* zig-zag start for quincunx pattern */
442 for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
443 if (i >= xres) {
444 xstep += xres-1-i;
445 i = xres-1;
446 }
447 z = pixvalue(scanline[i], i, y);
448 if (zline) zline[i] = z;
449 if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
450 if (i <= xstep)
451 b = fillsample(scanline, zline, 0, y, i, 0, b/2);
452 else
453 b = fillsample(scanline+i-xstep,
454 zline ? zline+i-xstep : NULL,
455 i-xstep, y, xstep, 0, b/2);
456 if (sd) *sd++ = nc & 1 ? bl : b;
457 bl = b;
458 }
459 if (sd && nc & 1) *sd = bl;
460 }
461
462
463 fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */
464 register COLOR *scanbar[];
465 register float *zbar[];
466 int xres, y, ysize;
467 {
468 COLOR vline[MAXDIV+1];
469 float zline[MAXDIV+1];
470 int b = ysize;
471 register int i, j;
472
473 for (i = 0; i < xres; i++) {
474
475 copycolor(vline[0], scanbar[0][i]);
476 copycolor(vline[ysize], scanbar[ysize][i]);
477 if (zbar[0]) {
478 zline[0] = zbar[0][i];
479 zline[ysize] = zbar[ysize][i];
480 }
481
482 b = fillsample(vline, zbar[0] ? zline : NULL,
483 i, y, 0, ysize, b/2);
484
485 for (j = 1; j < ysize; j++)
486 copycolor(scanbar[j][i], vline[j]);
487 if (zbar[0])
488 for (j = 1; j < ysize; j++)
489 zbar[j][i] = zline[j];
490 }
491 }
492
493
494 int
495 fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
496 register COLOR *colline;
497 register float *zline;
498 int x, y;
499 int xlen, ylen;
500 int b;
501 {
502 extern double fabs();
503 double ratio;
504 double z;
505 COLOR ctmp;
506 int ncut;
507 register int len;
508
509 if (xlen > 0) /* x or y length is zero */
510 len = xlen;
511 else
512 len = ylen;
513
514 if (len <= 1) /* limit recursion */
515 return(0);
516
517 if (b > 0
518 || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
519 || bigdiff(colline[0], colline[len], maxdiff)) {
520
521 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
522 if (zline) zline[len>>1] = z;
523 ncut = 1;
524
525 } else { /* interpolate */
526
527 copycolor(colline[len>>1], colline[len]);
528 ratio = (double)(len>>1) / len;
529 scalecolor(colline[len>>1], ratio);
530 if (zline) zline[len>>1] = zline[len] * ratio;
531 ratio = 1.0 - ratio;
532 copycolor(ctmp, colline[0]);
533 scalecolor(ctmp, ratio);
534 addcolor(colline[len>>1], ctmp);
535 if (zline) zline[len>>1] += zline[0] * ratio;
536 ncut = 0;
537 }
538 /* recurse */
539 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
540
541 ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
542 x+(xlen>>1), y+(ylen>>1),
543 xlen-(xlen>>1), ylen-(ylen>>1), b/2);
544
545 return(ncut);
546 }
547
548
549 double
550 pixvalue(col, x, y) /* compute pixel value */
551 COLOR col; /* returned color */
552 int x, y; /* pixel position */
553 {
554 static RAY thisray;
555
556 if (viewray(thisray.rorg, thisray.rdir, &ourview,
557 (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) {
558 setcolor(col, 0.0, 0.0, 0.0);
559 return(0.0);
560 }
561
562 rayorigin(&thisray, NULL, PRIMARY, 1.0);
563
564 samplendx = pixnumber(x,y,hres,vres); /* set pixel index */
565
566 rayvalue(&thisray); /* trace ray */
567
568 copycolor(col, thisray.rcol); /* return color */
569
570 return(thisray.rt); /* return distance */
571 }
572
573
574 int
575 salvage(oldfile) /* salvage scanlines from killed program */
576 char *oldfile;
577 {
578 COLR *scanline;
579 FILE *fp;
580 int x, y;
581
582 if (oldfile == NULL)
583 return(0);
584
585 if ((fp = fopen(oldfile, "r")) == NULL) {
586 sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
587 error(WARNING, errmsg);
588 return(0);
589 }
590 /* discard header */
591 getheader(fp, NULL);
592 /* get picture size */
593 if (!fscnresolu(&x, &y, fp)) {
594 sprintf(errmsg, "bad recover file \"%s\"", oldfile);
595 error(WARNING, errmsg);
596 fclose(fp);
597 return(0);
598 }
599
600 if (x != hres || y != vres) {
601 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
602 oldfile);
603 error(USER, errmsg);
604 }
605
606 scanline = (COLR *)malloc(hres*sizeof(COLR));
607 if (scanline == NULL)
608 error(SYSTEM, "out of memory in salvage");
609 for (y = 0; y < vres; y++) {
610 if (freadcolrs(scanline, hres, fp) < 0)
611 break;
612 if (fwritecolrs(scanline, hres, stdout) < 0)
613 goto writerr;
614 }
615 if (fflush(stdout) == EOF)
616 goto writerr;
617 free((char *)scanline);
618 fclose(fp);
619 unlink(oldfile);
620 return(y);
621 writerr:
622 sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
623 error(SYSTEM, errmsg);
624 }
625
626
627 int
628 pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */
629 register int x, y;
630 int xres, yres;
631 {
632 x -= y;
633 while (x < 0)
634 x += xres;
635 return((((x>>2)*yres + y) << 2) + (x & 3));
636 }