ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
(Generate patch)

Comparing ray/src/rt/rpict.c (file contents):
Revision 1.12 by greg, Mon Jan 8 13:38:01 1990 UTC vs.
Revision 2.13 by greg, Tue Sep 8 20:56:20 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 17 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
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 */
# Line 32 | Line 44 | double  dstrpix = 0.67;                        /* square pixel distribution
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  
# Line 49 | Line 68 | 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         32              /* maximum sample size */
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 || ralrm > 0)          /* report status */
98 >        if (code)                       /* report status */
99                  report();
100 <
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   {
73 #ifdef BSD
112          struct rusage  rubuf;
113          double  t;
114  
# Line 83 | Line 121 | report()               /* report progress */
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 <        sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone);
129 < #endif
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 <        if (ralrm > 0)
139 <                alarm(ralrm);
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 <        FILE  *zfp;
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;
106                                        /* check sampling */
107        if (psample < 1)
108                psample = 1;
109        else if (psample > MAXDIV)
110                psample = MAXDIV;
310                                          /* allocate scanlines */
311          for (i = 0; i <= psample; i++) {
312 <                scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
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 ((zfp = fopen(zfile, "a+")) == 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(hresolu*sizeof(float));
333 >                        zbar[i] = (float *)malloc(hres*sizeof(float));
334                          if (zbar[i] == NULL)
335                                  goto memerr;
336                  }
337          } else {
338 <                zfp = NULL;
338 >                zfd = -1;
339                  for (i = 0; i <= psample; i++)
340                          zbar[i] = NULL;
341          }
342                                          /* write out boundaries */
343 <        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
343 >        fprtresolu(hres, vres, stdout);
344                                          /* recover file and compute first */
345          i = salvage(oldfile);
346 <        if (zfp != NULL && fseek(zfp, (long)i*hresolu*sizeof(float), 0) == EOF)
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 <        ypos = vresolu-1 - i;
350 <        fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
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 -= psample; ypos >= 0; ypos -= psample) {
360 <        
361 <                pctdone = 100.0*(vresolu-ypos-psample)/vresolu;
362 <
363 <                colptr = scanbar[psample];              /* move base to top */
364 <                scanbar[psample] = scanbar[0];
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[psample];
369 <                zbar[psample] = zbar[0];
368 >                zptr = zbar[ystep];
369 >                zbar[ystep] = zbar[0];
370                  zbar[0] = zptr;
371                                                          /* fill base line */
372 <                fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
372 >                fillscanline(scanbar[0], zbar[0], sampdens,
373 >                                hres, ypos, hstep);
374                                                          /* fill bar */
375 <                fillscanbar(scanbar, zbar, hresolu, ypos, psample);
375 >                fillscanbar(scanbar, zbar, hres, ypos, ystep);
376                                                          /* write it out */
377 <                for (i = psample; i > 0; i--) {
378 <                        if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu)
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],hresolu,stdout) < 0)
385 >                        if (fwritescan(scanbar[i], hres, stdout) < 0)
386                                  goto writerr;
387                  }
163                if (zfp != NULL && fflush(zfp) == EOF)
164                        goto writerr;
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          }
168                                                /* compute residual */
169        colptr = scanbar[psample];
170        scanbar[psample] = scanbar[0];
171        scanbar[0] = colptr;
172        zptr = zbar[psample];
173        zbar[psample] = zbar[0];
174        zbar[0] = zptr;
175        if (ypos > -psample) {
176                fillscanline(scanbar[-ypos], zbar[-ypos], hresolu, 0, psample);
177                fillscanbar(scanbar-ypos, zbar-ypos, hresolu, 0, psample+ypos);
178        }
179        for (i = psample; i+ypos >= 0; i--) {
180                if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu)
181                        goto writerr;
182                if (fwritescan(scanbar[i], hresolu, stdout) < 0)
183                        goto writerr;
184        }
399                                                  /* clean up */
400 <        if (zfp != NULL) {
401 <                if (fclose(zfp) == EOF)
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");
# Line 202 | Line 425 | memerr:
425   }
426  
427  
428 < fillscanline(scanline, zline, xres, y, xstep)   /* fill scan line at y */
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 <        int  b = xstep;
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 <
442 <        for (i = xstep; i < xres; i += xstep) {
443 <        
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 <                
450 <                b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
451 <                                i-xstep, y, xstep, 0, b/2);
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 (i-xstep < xres-1) {
226 <                z = pixvalue(scanline[xres-1], xres-1, y);
227 <                if (zline) zline[xres-1] = z;
228 <                fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
229 <                                i-xstep, y, xres-1-(i-xstep), 0, b/2);
230 <        }
459 >        if (sd && nc & 1) *sd = bl;
460   }
461  
462  
# Line 239 | Line 468 | int  xres, y, ysize;
468          COLOR  vline[MAXDIV+1];
469          float  zline[MAXDIV+1];
470          int  b = ysize;
242        double  z;
471          register int  i, j;
472          
473          for (i = 0; i < xres; i++) {
# Line 323 | Line 551 | pixvalue(col, x, y)            /* compute pixel value */
551   COLOR  col;                     /* returned color */
552   int  x, y;                      /* pixel position */
553   {
554 <        static RAY  thisray;    /* our ray for this pixel */
554 >        static RAY  thisray;
555  
556 <        viewray(thisray.rorg, thisray.rdir, &ourview,
557 <                        (x+pixjitter())/hresolu, (y+pixjitter())/vresolu);
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 <        
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.rot);                    /* return distance */
570 >        return(thisray.rt);                     /* return distance */
571   }
572  
573  
# Line 357 | Line 590 | char  *oldfile;
590                                  /* discard header */
591          getheader(fp, NULL);
592                                  /* get picture size */
593 <        if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) {
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 != hresolu || y != vresolu) {
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(hresolu*sizeof(COLR));
606 >        scanline = (COLR *)malloc(hres*sizeof(COLR));
607          if (scanline == NULL)
608                  error(SYSTEM, "out of memory in salvage");
609 <        for (y = 0; y < vresolu; y++) {
610 <                if (freadcolrs(scanline, hresolu, fp) < 0)
609 >        for (y = 0; y < vres; y++) {
610 >                if (freadcolrs(scanline, hres, fp) < 0)
611                          break;
612 <                if (fwritecolrs(scanline, hresolu, stdout) < 0)
612 >                if (fwritecolrs(scanline, hres, stdout) < 0)
613                          goto writerr;
614          }
615          if (fflush(stdout) == EOF)
# Line 386 | Line 619 | char  *oldfile;
619          unlink(oldfile);
620          return(y);
621   writerr:
622 <        error(SYSTEM, "write error in salvage");
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines