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 2.68 by greg, Sun Mar 28 16:31:14 2004 UTC vs.
Revision 2.92 by greg, Thu Mar 21 16:52:40 2019 UTC

# Line 1 | Line 1
1   #ifndef lint
2 < static const char RCSid[] = "$Id";
2 > static const char RCSid[] = "$Id$";
3   #endif
4   /*
5   *  rpict.c - routines and variables for picture generation.
# Line 7 | Line 7 | static const char RCSid[] = "$Id";
7  
8   #include "copyright.h"
9  
10 #include  "platform.h"
11 #include  "ray.h"
12 #include  "paths.h"
13
10   #include  <sys/types.h>
11  
12 < #ifndef NON_POSIX
13 < #ifdef BSD
14 < #include  <sys/time.h>
15 < #include  <sys/resource.h>
12 > #include  "platform.h"
13 > #ifdef NON_POSIX
14 > #ifdef MINGW
15 >  #include  <sys/time.h>
16 > #endif
17   #else
18 < #include  <sys/times.h>
19 < #include  <unistd.h>
18 > #ifdef BSD
19 >  #include  <sys/time.h>
20 >  #include  <sys/resource.h>
21 > #else
22 >  #include  <sys/times.h>
23 >  #include  <unistd.h>
24 > #endif
25   #endif
24 #endif
26  
27   #include  <time.h>
28   #include  <signal.h>
29  
30 + #include  "ray.h"
31 + #include  "paths.h"
32 + #include  "ambient.h"
33   #include  "view.h"
34   #include  "random.h"
35   #include  "paths.h"
36 < #include  "rtmisc.h" /* myhostname() */
36 > #include  "hilbert.h"
37 > #include  "pmapbias.h"
38 > #include  "pmapdiag.h"
39  
34
40   #define  RFTEMPLATE     "rfXXXXXX"
41  
42   #ifndef SIGCONT
# Line 47 | Line 52 | int  dimlist[MAXDIM];                  /* sampling dimensions */
52   int  ndims = 0;                         /* number of sampling dimensions */
53   int  samplendx;                         /* sample index number */
54  
50 extern void  ambnotify();
55   void  (*addobjnotify[])() = {ambnotify, NULL};
56  
57   VIEW  ourview = STDVIEW;                /* view parameters */
# Line 61 | Line 65 | double dstrpix = 0.67;                 /* square pixel distribution
65  
66   double  mblur = 0.;                     /* motion blur parameter */
67  
68 + double  dblur = 0.;                     /* depth-of-field blur parameter */
69 +
70   void  (*trace)() = NULL;                /* trace call */
71  
72   int  do_irrad = 0;                      /* compute irradiance? */
73  
74 + int  rand_samp = 0;                     /* pure Monte Carlo sampling? */
75 +
76   double  dstrsrc = 0.0;                  /* square source distribution */
77   double  shadthresh = .05;               /* shadow threshold */
78   double  shadcert = .5;                  /* shadow certainty */
# Line 84 | Line 92 | double specjitter = 1.;                /* specular sampling jitter *
92   int  backvis = 1;                       /* back face visibility */
93  
94   int  maxdepth = 7;                      /* maximum recursion depth */
95 < double  minweight = 4e-3;               /* minimum ray weight */
95 > double  minweight = 1e-3;               /* minimum ray weight */
96  
97   char  *ambfile = NULL;                  /* ambient file name */
98   COLOR  ambval = BLKCOLOR;               /* ambient value */
# Line 94 | Line 102 | int  ambres = 64;                      /* ambient resolution */
102   int  ambdiv = 512;                      /* ambient divisions */
103   int  ambssamp = 128;                    /* ambient super-samples */
104   int  ambounce = 0;                      /* ambient bounces */
105 < char  *amblist[128];                    /* ambient include/exclude list */
105 > char  *amblist[AMBLLEN];                /* ambient include/exclude list */
106   int  ambincl = -1;                      /* include == 1, exclude == 0 */
107  
108   int  ralrm = 0;                         /* seconds between reports */
# Line 111 | Line 119 | int  hres, vres;                       /* resolution for this frame */
119  
120   static VIEW     lastview;               /* the previous view input */
121  
122 < extern char  *mktemp();  /* XXX should be in stdlib.h or unistd.h */
122 > static void report(int);
123 > static int nextview(FILE *fp);
124 > static void render(char *zfile, char *oldfile);
125 > static void fillscanline(COLOR *scanline, float *zline, char *sd, int xres,
126 >                int y, int xstep);
127 > static void fillscanbar(COLOR *scanbar[], float *zbar[], int xres,
128 >                int y, int ysize);
129 > static int fillsample(COLOR *colline, float *zline, int x, int y,
130 >                int xlen, int ylen, int b);
131 > static double pixvalue(COLOR  col, int  x, int  y);
132 > static int salvage(char  *oldfile);
133 > static int pixnumber(int  x, int  y, int  xres, int  yres);
134  
116 void  report();
135  
118 double  pixvalue();
136  
120 #ifdef RHAS_STAT
121 #include  <sys/types.h>
122 #include  <sys/stat.h>
123 int
124 file_exists(fname)                              /* ordinary file exists? */
125 char  *fname;
126 {
127        struct stat  sbuf;
128        if (stat(fname, &sbuf) < 0) return(0);
129        return((sbuf.st_mode & S_IFREG) != 0);
130 }
131 #else
132 #define  file_exists(f) (access(f,F_OK)==0)
133 #endif
134
135
137   void
138   quit(code)                      /* quit program */
139   int  code;
140   {
141          if (code)                       /* report status */
142 <                report();
142 >                report(0);
143   #ifndef NON_POSIX
144          headclean();                    /* delete header file */
145          pfclean();                      /* clean up persist files */
# Line 148 | Line 149 | int  code;
149  
150  
151   #ifndef NON_POSIX
152 < void
153 < report()                /* report progress */
152 > static void
153 > report(int dummy)               /* report progress */
154   {
155 <        double  u, s;
155 >        char                    bcStat [128];
156 >        double          u, s;
157   #ifdef BSD
158 <        struct rusage  rubuf;
158 >        struct rusage   rubuf;
159   #else
160 <        struct tms  tbuf;
161 <        double  period;
160 >        double          period = 1.0 / 60.0;
161 >        struct tms      tbuf;
162   #endif
163  
164          tlastrept = time((time_t *)NULL);
165   #ifdef BSD
166          getrusage(RUSAGE_SELF, &rubuf);
167 <        u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
168 <        s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
167 >        u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6;
168 >        s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6;
169          getrusage(RUSAGE_CHILDREN, &rubuf);
170 <        u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
171 <        s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
170 >        u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6;
171 >        s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6;
172   #else
173          times(&tbuf);
174   #ifdef _SC_CLK_TCK
175          period = 1.0 / sysconf(_SC_CLK_TCK);
174 #else
175        period = 1.0 / 60.0;
176   #endif
177          u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period;
178          s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period;
179   #endif
180  
181 +        /* PMAP: Get photon map bias compensation statistics */
182 +        pmapBiasCompReport(bcStat);
183 +        
184          sprintf(errmsg,
185 <                "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n",
186 <                        nrays, pctdone, u/3600., s/3600.,
187 <                        (tlastrept-tstart)/3600., myhostname());
185 >                        "%lu rays, %s%4.2f%% after %.3fu %.3fs %.3fr hours on %s (PID %d)\n",
186 >                        nrays, bcStat, pctdone, u*(1./3600.), s*(1./3600.),
187 >                        (tlastrept-tstart)*(1./3600.), myhostname(), getpid());
188          eputs(errmsg);
189   #ifdef SIGCONT
190          signal(SIGCONT, report);
191   #endif
192   }
193   #else
194 < void
195 < report()                /* report progress */
194 > static void
195 > report(int dummy)               /* report progress */
196   {
197 +        char    bcStat [128];
198 +        
199          tlastrept = time((time_t *)NULL);
200 <        sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n",
201 <                        nrays, pctdone, (tlastrept-tstart)/3600.0);
200 >
201 >        /* PMAP: Get photon map bias compensation statistics */
202 >        pmapBiasCompReport(bcStat);
203 >        
204 >        sprintf(errmsg, "%lu rays, %s%4.2f%% after %5.4f hours\n",
205 >                        nrays, bcStat, pctdone, (tlastrept-tstart)/3600.0);
206          eputs(errmsg);
207   }
208   #endif
209  
210  
211   void
212 < rpict(seq, pout, zout, prvr)                    /* generate image(s) */
213 < int  seq;
214 < char  *pout, *zout, *prvr;
212 > rpict(                  /* generate image(s) */
213 >        int  seq,
214 >        char  *pout,
215 >        char  *zout,
216 >        char  *prvr
217 > )
218   /*
219   * If seq is greater than zero, then we will render a sequence of
220   * images based on view parameter strings read from the standard input.
# Line 217 | Line 229 | char  *pout, *zout, *prvr;
229   {
230          char  fbuf[128], fbuf2[128];
231          int  npicts;
232 <        register char  *cp;
232 >        char  *cp;
233          RESOLU  rs;
234          double  pa;
235                                          /* check sampling */
# Line 233 | Line 245 | char  *pout, *zout, *prvr;
245          if (seq <= 0)
246                  seq = 0;
247          else if (prvr != NULL && isint(prvr)) {
248 <                register int  rn;               /* skip to specified view */
248 >                int  rn;                /* skip to specified view */
249                  if ((rn = atoi(prvr)) < seq)
250                          error(USER, "recover frame less than start frame");
251                  if (pout == NULL)
# Line 276 | Line 288 | char  *pout, *zout, *prvr;
288                          break;
289                  pctdone = 0.0;
290                  if (pout != NULL) {
291 +                        close(1);                       /* reassign stdout */
292                          sprintf(fbuf, pout, seq);
293 <                        if (file_exists(fbuf)) {
294 <                                if (prvr != NULL || !strcmp(fbuf, pout)) {
293 >                        errno = 0;                      /* exclusive open */
294 >                        if (open(fbuf, O_WRONLY|O_CREAT|O_EXCL, 0666) != 1) {
295 >                                if ((errno != EEXIST) | (prvr != NULL) ||
296 >                                                !strcmp(fbuf, pout)) {
297                                          sprintf(errmsg,
298 <                                                "output file \"%s\" exists",
298 >                                                "cannot open output file \"%s\"",
299                                                  fbuf);
300 <                                        error(USER, errmsg);
300 >                                        error(SYSTEM, errmsg);
301                                  }
302                                  setview(&ourview);
303                                  continue;               /* don't clobber */
304                          }
305 <                        if (freopen(fbuf, "w", stdout) == NULL) {
291 <                                sprintf(errmsg,
292 <                                        "cannot open output file \"%s\"", fbuf);
293 <                                error(SYSTEM, errmsg);
294 <                        }
295 <                        SET_FILE_BINARY(stdout);
305 >                        SET_FD_BINARY(1);
306                          dupheader();
307                  }
308                  hres = hresolu; vres = vresolu; pa = pixaspect;
# Line 322 | Line 332 | char  *pout, *zout, *prvr;
332                  fputs(VIEWSTR, stdout);
333                  fprintview(&ourview, stdout);
334                  putchar('\n');
335 <                if (pa < .99 || pa > 1.01)
335 >                if ((pa < .99) | (pa > 1.01))
336                          fputaspect(pa, stdout);
337                  fputnow(stdout);
338                  fputformat(COLRFMT, stdout);
# Line 341 | Line 351 | char  *pout, *zout, *prvr;
351   }
352  
353  
354 < nextview(fp)                            /* get next view from fp */
355 < FILE  *fp;
354 > static int
355 > nextview(                               /* get next view from fp */
356 >        FILE  *fp
357 > )
358   {
359          char  linebuf[256];
360  
# Line 354 | Line 366 | FILE  *fp;
366   }      
367  
368  
369 < render(zfile, oldfile)                          /* render the scene */
370 < char  *zfile, *oldfile;
369 > static void
370 > render(                         /* render the scene */
371 >        char  *zfile,
372 >        char  *oldfile
373 > )
374   {
375          COLOR  *scanbar[MAXDIV+1];      /* scanline arrays of pixel values */
376          float  *zbar[MAXDIV+1];         /* z values */
# Line 366 | Line 381 | char  *zfile, *oldfile;
381          int  zfd;
382          COLOR  *colptr;
383          float  *zptr;
384 <        register int  i;
384 >        int  i;
385                                          /* check for empty image */
386 <        if (hres <= 0 || vres <= 0) {
386 >        if ((hres <= 0) | (vres <= 0)) {
387                  error(WARNING, "empty output picture");
388                  fprtresolu(0, 0, stdout);
389                  return;
# Line 412 | Line 427 | char  *zfile, *oldfile;
427          i = salvage(oldfile);
428          if (i >= vres)
429                  goto alldone;
430 <        if (zfd != -1 && i > 0 &&
430 >        if ((zfd != -1) & (i > 0) &&
431                          lseek(zfd, (off_t)i*hres*sizeof(float), SEEK_SET) < 0)
432                  error(SYSTEM, "z-file seek error in render");
433          pctdone = 100.0*i/vres;
434          if (ralrm > 0)                  /* report init stats */
435 <                report();
435 >                report(0);
436   #ifdef SIGCONT
437          else
438          signal(SIGCONT, report);
# Line 463 | Line 478 | char  *zfile, *oldfile;
478                                                          /* record progress */
479                  pctdone = 100.0*(vres-1-ypos)/vres;
480                  if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm)
481 <                        report();
481 >                        report(0);
482   #ifdef SIGCONT
483                  else
484                          signal(SIGCONT, report);
# Line 492 | Line 507 | alldone:
507                  free(sampdens);
508          pctdone = 100.0;
509          if (ralrm > 0)
510 <                report();
510 >                report(0);
511   #ifdef SIGCONT
512          signal(SIGCONT, SIG_DFL);
513   #endif
# Line 504 | Line 519 | memerr:
519   }
520  
521  
522 < fillscanline(scanline, zline, sd, xres, y, xstep)       /* fill scan at y */
523 < register COLOR  *scanline;
524 < register float  *zline;
525 < register char  *sd;
526 < int  xres, y, xstep;
522 > static void
523 > fillscanline(   /* fill scan at y */
524 >        COLOR   *scanline,
525 >        float   *zline,
526 >        char  *sd,
527 >        int  xres,
528 >        int  y,
529 >        int  xstep
530 > )
531   {
532          static int  nc = 0;             /* number of calls */
533          int  bl = xstep, b = xstep;
534          double  z;
535 <        register int  i;
535 >        int  i;
536  
537          z = pixvalue(scanline[0], 0, y);
538          if (zline) zline[0] = z;
# Line 539 | Line 558 | int  xres, y, xstep;
558   }
559  
560  
561 < fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
562 < register COLOR  *scanbar[];
563 < register float  *zbar[];
564 < int  xres, y, ysize;
561 > static void
562 > fillscanbar(    /* fill interior */
563 >        COLOR   *scanbar[],
564 >        float   *zbar[],
565 >        int  xres,
566 >        int  y,
567 >        int  ysize
568 > )
569   {
570          COLOR  vline[MAXDIV+1];
571          float  zline[MAXDIV+1];
572          int  b = ysize;
573 <        register int  i, j;
573 >        int  i, j;
574  
575          for (i = 0; i < xres; i++) {
576                  copycolor(vline[0], scanbar[0][i]);
# Line 568 | Line 591 | int  xres, y, ysize;
591   }
592  
593  
594 < int
595 < fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
596 < register COLOR  *colline;
597 < register float  *zline;
598 < int  x, y;
599 < int  xlen, ylen;
600 < int  b;
594 > static int
595 > fillsample( /* fill interior points */
596 >        COLOR   *colline,
597 >        float   *zline,
598 >        int  x,
599 >        int  y,
600 >        int  xlen,
601 >        int  ylen,
602 >        int  b
603 > )
604   {
605          double  ratio;
606          double  z;
607          COLOR  ctmp;
608          int  ncut;
609 <        register int  len;
609 >        int  len;
610  
611          if (xlen > 0)                   /* x or y length is zero */
612                  len = xlen;
# Line 621 | Line 647 | int  b;
647   }
648  
649  
650 < double
651 < pixvalue(col, x, y)             /* compute pixel value */
652 < COLOR  col;                     /* returned color */
653 < int  x, y;                      /* pixel position */
650 > static double
651 > pixvalue(               /* compute pixel value */
652 >        COLOR  col,                     /* returned color */
653 >        int  x,                 /* pixel position */
654 >        int  y
655 > )
656   {
657 +        extern void  SDsquare2disk(double ds[2], double seedx, double seedy);
658          RAY  thisray;
659          FVECT   lorg, ldir;
660 <        double  hpos, vpos, lmax, d;
660 >        double  hpos, vpos, vdist, lmax;
661 >        int     i;
662                                                  /* compute view ray */
663 +        setcolor(col, 0.0, 0.0, 0.0);
664          hpos = (x+pixjitter())/hres;
665          vpos = (y+pixjitter())/vres;
666          if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
667 <                                        &ourview, hpos, vpos)) < -FTINY) {
637 <                setcolor(col, 0.0, 0.0, 0.0);
667 >                                        &ourview, hpos, vpos)) < -FTINY)
668                  return(0.0);
639        }
669  
670 <        samplendx = pixnumber(x,y,hres,vres);   /* set pixel index */
671 <
670 >        vdist = ourview.vdist;
671 >                                                /* set pixel index */
672 >        samplendx = pixnumber(x,y,hres,vres);
673                                                  /* optional motion blur */
674          if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir,
675                                          &lastview, hpos, vpos)) >= -FTINY) {
676 <                register int    i;
647 <                register double  d = mblur*(.5-urand(281+samplendx));
676 >                double  d = mblur*(.5-urand(281+samplendx));
677  
678                  thisray.rmax = (1.-d)*thisray.rmax + d*lmax;
679                  for (i = 3; i--; ) {
# Line 653 | Line 682 | int  x, y;                     /* pixel position */
682                  }
683                  if (normalize(thisray.rdir) == 0.0)
684                          return(0.0);
685 +                vdist = (1.-d)*vdist + d*lastview.vdist;
686          }
687 +                                                /* optional depth-of-field */
688 +        if (dblur > FTINY) {
689 +                double  vc, df[2];
690 +                                                /* random point on disk */
691 +                SDsquare2disk(df, frandom(), frandom());
692 +                df[0] *= .5*dblur;
693 +                df[1] *= .5*dblur;
694 +                if ((ourview.type == VT_PER) | (ourview.type == VT_PAR)) {
695 +                        double  adj = 1.0;
696 +                        if (ourview.type == VT_PER)
697 +                                adj /= DOT(thisray.rdir, ourview.vdir);
698 +                        df[0] /= sqrt(ourview.hn2);
699 +                        df[1] /= sqrt(ourview.vn2);
700 +                        for (i = 3; i--; ) {
701 +                                vc = ourview.vp[i] + adj*vdist*thisray.rdir[i];
702 +                                thisray.rorg[i] += df[0]*ourview.hvec[i] +
703 +                                                        df[1]*ourview.vvec[i] ;
704 +                                thisray.rdir[i] = vc - thisray.rorg[i];
705 +                        }
706 +                } else {                        /* non-standard view case */
707 +                        double  dfd = PI/4.*dblur*(.5 - frandom());
708 +                        if ((ourview.type != VT_ANG) & (ourview.type != VT_PLS)) {
709 +                                if (ourview.type != VT_CYL)
710 +                                        df[0] /= sqrt(ourview.hn2);
711 +                                df[1] /= sqrt(ourview.vn2);
712 +                        }
713 +                        for (i = 3; i--; ) {
714 +                                vc = ourview.vp[i] + vdist*thisray.rdir[i];
715 +                                thisray.rorg[i] += df[0]*ourview.hvec[i] +
716 +                                                        df[1]*ourview.vvec[i] +
717 +                                                        dfd*ourview.vdir[i] ;
718 +                                thisray.rdir[i] = vc - thisray.rorg[i];
719 +                        }
720 +                }
721 +                if (normalize(thisray.rdir) == 0.0)
722 +                        return(0.0);
723 +        }
724  
725 <        rayorigin(&thisray, NULL, PRIMARY, 1.0);
725 >        rayorigin(&thisray, PRIMARY, NULL, NULL);
726  
727          rayvalue(&thisray);                     /* trace ray */
728  
729          copycolor(col, thisray.rcol);           /* return color */
730  
731 <        return(thisray.rt);                     /* return distance */
731 >        return(raydistance(&thisray));          /* return distance */
732   }
733  
734  
735 < int
736 < salvage(oldfile)                /* salvage scanlines from killed program */
737 < char  *oldfile;
735 > static int
736 > salvage(                /* salvage scanlines from killed program */
737 >        char  *oldfile
738 > )
739   {
740          COLR  *scanline;
741          FILE  *fp;
# Line 693 | Line 761 | char  *oldfile;
761                  goto gotzip;
762          }
763  
764 <        if (x != hres || y != vres) {
764 >        if ((x != hres) | (y != vres)) {
765                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
766                                  oldfile);
767                  error(USER, errmsg);
# Line 721 | Line 789 | gotzip:
789   writerr:
790          sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
791          error(SYSTEM, errmsg);
792 +        return -1; /* pro forma return */
793   }
794  
795 <
796 < int
797 < pixnumber(x, y, xres, yres)             /* compute pixel index (brushed) */
798 < register int  x, y;
799 < int  xres, yres;
795 > static int
796 > pixnumber(              /* compute pixel index (screen door) */
797 >        int  x,
798 >        int  y,
799 >        int  xres,
800 >        int  yres
801 > )
802   {
803 <        x -= y;
804 <        while (x < 0)
805 <                x += xres;
806 <        return((((x>>2)*yres + y) << 2) + (x & 3));
803 >        unsigned        nbits = 0;
804 >        bitmask_t       coord[2];
805 >
806 >        if (xres < yres) xres = yres;
807 >        while (xres > 0) {
808 >                xres >>= 1;
809 >                ++nbits;
810 >        }
811 >        coord[0] = x; coord[1] = y;
812 >        return ((int)hilbert_c2i(2, nbits, coord));
813   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines