ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.15
Committed: Fri Oct 2 16:19:10 1992 UTC (31 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.14: +0 -1 lines
Log Message:
Removed problematic math function declarations

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