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.29 by greg, Tue Jun 25 14:06:12 1991 UTC vs.
Revision 2.25 by greg, Fri May 28 18:59:50 1993 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines