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.11 by greg, Sun Dec 10 13:42:45 1989 UTC vs.
Revision 2.4 by greg, Sat Dec 28 18:43:15 1991 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1991 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 < VIEW  ourview = STDVIEW(512);           /* view parameters */
29 > int  dimlist[MAXDIM];                   /* sampling dimensions */
30 > int  ndims = 0;                         /* number of sampling dimensions */
31 > int  samplendx;                         /* sample index number */
32  
33 + VIEW  ourview = STDVIEW;                /* view parameters */
34 + int  hresolu = 512;                     /* horizontal resolution */
35 + int  vresolu = 512;                     /* vertical resolution */
36 + double  pixaspect = 1.0;                /* pixel aspect ratio */
37 +
38   int  psample = 4;                       /* pixel sample size */
39   double  maxdiff = .05;                  /* max. difference for interpolation */
40   double  dstrpix = 0.67;                 /* square pixel distribution */
# Line 30 | Line 42 | double  dstrpix = 0.67;                        /* square pixel distribution
42   double  dstrsrc = 0.0;                  /* square source distribution */
43   double  shadthresh = .05;               /* shadow threshold */
44   double  shadcert = .5;                  /* shadow certainty */
45 + int  directrelay = 1;                   /* number of source relays */
46 + int  vspretest = 512;                   /* virtual source pretest density */
47 + int  directinvis = 0;                   /* sources invisible? */
48 + double  srcsizerat = .25;               /* maximum ratio source size/dist. */
49  
50   int  maxdepth = 6;                      /* maximum recursion depth */
51   double  minweight = 5e-3;               /* minimum ray weight */
# Line 47 | Line 63 | int  ralrm = 0;                                /* seconds between reports */
63  
64   double  pctdone = 0.0;                  /* percentage done */
65  
66 + long  tlastrept = 0L;                   /* time at last report */
67 +
68 + extern long  time();
69 + extern long  tstart;                    /* starting time */
70 +
71   extern long  nrays;                     /* number of rays traced */
72  
73 < #define  MAXDIV         32              /* maximum sample size */
73 > #define  MAXDIV         16              /* maximum sample size */
74  
75   #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
76  
# Line 66 | Line 87 | int  code;
87   }
88  
89  
90 + #ifdef BSD
91   report()                /* report progress */
92   {
71 #ifdef BSD
93          struct rusage  rubuf;
94          double  t;
95  
# Line 81 | Line 102 | report()               /* report progress */
102  
103          sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
104                          nrays, pctdone, t/3600.0);
105 +        eputs(errmsg);
106 +        tlastrept = time((long *)0);
107 + }
108   #else
109 <        sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone);
110 < #endif
109 > report()                /* report progress */
110 > {
111 >        tlastrept = time((long *)0);
112 >        sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n",
113 >                        nrays, pctdone, (tlastrept-tstart)/3600.0);
114          eputs(errmsg);
115 <
89 <        if (ralrm > 0)
90 <                alarm(ralrm);
115 >        signal(SIGALRM, report);
116   }
117 + #endif
118  
119  
120   render(zfile, oldfile)                          /* render the scene */
121   char  *zfile, *oldfile;
122   {
123 +        extern long  lseek();
124          COLOR  *scanbar[MAXDIV+1];      /* scanline arrays of pixel values */
125          float  *zbar[MAXDIV+1];         /* z values */
126 +        char  *sampdens;                /* previous sample density */
127          int  ypos;                      /* current scanline */
128 <        FILE  *zfp;
128 >        int  ystep;                     /* current y step size */
129 >        int  hstep;                     /* h step size */
130 >        int  zfd;
131          COLOR  *colptr;
132          float  *zptr;
133          register int  i;
134                                          /* check sampling */
135          if (psample < 1)
136                  psample = 1;
137 <        else if (psample > MAXDIV)
137 >        else if (psample > MAXDIV) {
138 >                sprintf(errmsg, "pixel sampling reduced from %d to %d",
139 >                                psample, MAXDIV);
140 >                error(WARNING, errmsg);
141                  psample = MAXDIV;
142 +        }
143                                          /* allocate scanlines */
144          for (i = 0; i <= psample; i++) {
145 <                scanbar[i] = (COLOR *)malloc(ourview.hresolu*sizeof(COLOR));
145 >                scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
146                  if (scanbar[i] == NULL)
147                          goto memerr;
148          }
149 +        hstep = (psample*140+49)/99;            /* quincunx sampling */
150 +        ystep = (psample*99+70)/140;
151 +        if (hstep > 2) {
152 +                i = hresolu/hstep + 2;
153 +                if ((sampdens = malloc(i)) == NULL)
154 +                        goto memerr;
155 +                while (i--)
156 +                        sampdens[i] = hstep;
157 +        } else
158 +                sampdens = NULL;
159                                          /* open z file */
160          if (zfile != NULL) {
161 <                if ((zfp = fopen(zfile, "a+")) == NULL) {
161 >                if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
162                          sprintf(errmsg, "cannot open z file \"%s\"", zfile);
163                          error(SYSTEM, errmsg);
164                  }
165                  for (i = 0; i <= psample; i++) {
166 <                        zbar[i] = (float *)malloc(ourview.hresolu*sizeof(float));
166 >                        zbar[i] = (float *)malloc(hresolu*sizeof(float));
167                          if (zbar[i] == NULL)
168                                  goto memerr;
169                  }
170          } else {
171 <                zfp = NULL;
171 >                zfd = -1;
172                  for (i = 0; i <= psample; i++)
173                          zbar[i] = NULL;
174          }
175                                          /* write out boundaries */
176 <        fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout);
176 >        fprtresolu(hresolu, vresolu, stdout);
177                                          /* recover file and compute first */
178          i = salvage(oldfile);
179 <        if (zfp != NULL && fseek(zfp, (long)i*ourview.hresolu*sizeof(float), 0) == EOF)
179 >        if (zfd != -1 && i > 0 &&
180 >                        lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1)
181                  error(SYSTEM, "z file seek error in render");
182 <        ypos = ourview.vresolu-1 - i;
183 <        fillscanline(scanbar[0], zbar[0], ourview.hresolu, ypos, psample);
182 >        pctdone = 100.0*i/vresolu;
183 >        if (ralrm > 0)                  /* report init stats */
184 >                report();
185 > #ifndef  BSD
186 >        else
187 > #endif
188 >        signal(SIGALRM, report);
189 >        ypos = vresolu-1 - i;
190 >        fillscanline(scanbar[0], zbar[0], sampdens, hresolu, ypos, hstep);
191                                                  /* compute scanlines */
192 <        for (ypos -= psample; ypos >= 0; ypos -= psample) {
193 <        
194 <                pctdone = 100.0*(ourview.vresolu-ypos-psample)/ourview.vresolu;
195 <
196 <                colptr = scanbar[psample];              /* move base to top */
197 <                scanbar[psample] = scanbar[0];
192 >        for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
193 >                                                        /* bottom adjust? */
194 >                if (ypos < 0) {
195 >                        ystep += ypos;
196 >                        ypos = 0;
197 >                }
198 >                colptr = scanbar[ystep];                /* move base to top */
199 >                scanbar[ystep] = scanbar[0];
200                  scanbar[0] = colptr;
201 <                zptr = zbar[psample];
202 <                zbar[psample] = zbar[0];
201 >                zptr = zbar[ystep];
202 >                zbar[ystep] = zbar[0];
203                  zbar[0] = zptr;
204                                                          /* fill base line */
205 <                fillscanline(scanbar[0], zbar[0], ourview.hresolu, ypos, psample);
205 >                fillscanline(scanbar[0], zbar[0], sampdens,
206 >                                hresolu, ypos, hstep);
207                                                          /* fill bar */
208 <                fillscanbar(scanbar, zbar, ourview.hresolu, ypos, psample);
208 >                fillscanbar(scanbar, zbar, hresolu, ypos, ystep);
209                                                          /* write it out */
210 <                for (i = psample; i > 0; i--) {
211 <                        if (zfp != NULL && fwrite(zbar[i],sizeof(float),ourview.hresolu,zfp) != ourview.hresolu)
210 > #ifndef  BSD
211 >                signal(SIGALRM, SIG_IGN);       /* don't interrupt writes */
212 > #endif
213 >                for (i = ystep; i > 0; i--) {
214 >                        if (zfd != -1 && write(zfd, (char *)zbar[i],
215 >                                        hresolu*sizeof(float))
216 >                                        < hresolu*sizeof(float))
217                                  goto writerr;
218 <                        if (fwritescan(scanbar[i],ourview.hresolu,stdout) < 0)
218 >                        if (fwritescan(scanbar[i], hresolu, stdout) < 0)
219                                  goto writerr;
220                  }
161                if (zfp != NULL && fflush(zfp) == EOF)
162                        goto writerr;
221                  if (fflush(stdout) == EOF)
222                          goto writerr;
223 +                                                        /* record progress */
224 +                pctdone = 100.0*(vresolu-1-ypos)/vresolu;
225 +                if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
226 +                        report();
227 + #ifndef  BSD
228 +                else
229 +                        signal(SIGALRM, report);
230 + #endif
231          }
166                                                /* compute residual */
167        colptr = scanbar[psample];
168        scanbar[psample] = scanbar[0];
169        scanbar[0] = colptr;
170        zptr = zbar[psample];
171        zbar[psample] = zbar[0];
172        zbar[0] = zptr;
173        if (ypos > -psample) {
174                fillscanline(scanbar[-ypos], zbar[-ypos], ourview.hresolu,
175                                0, psample);
176                fillscanbar(scanbar-ypos, zbar-ypos, ourview.hresolu,
177                                0, psample+ypos);
178        }
179        for (i = psample; i+ypos >= 0; i--) {
180                if (zfp != NULL && fwrite(zbar[i],sizeof(float),ourview.hresolu,zfp) != ourview.hresolu)
181                        goto writerr;
182                if (fwritescan(scanbar[i], ourview.hresolu, stdout) < 0)
183                        goto writerr;
184        }
232                                                  /* clean up */
233 <        if (zfp != NULL) {
234 <                if (fclose(zfp) == EOF)
233 >        if (zfd != -1) {
234 >                if (write(zfd, (char *)zbar[0], hresolu*sizeof(float))
235 >                                < hresolu*sizeof(float))
236                          goto writerr;
237 +                if (close(zfd) == -1)
238 +                        goto writerr;
239                  for (i = 0; i <= psample; i++)
240                          free((char *)zbar[i]);
241          }
242 +        fwritescan(scanbar[0], hresolu, stdout);
243          if (fflush(stdout) == EOF)
244                  goto writerr;
245          for (i = 0; i <= psample; i++)
246                  free((char *)scanbar[i]);
247 +        if (sampdens != NULL)
248 +                free(sampdens);
249          pctdone = 100.0;
250          return;
251   writerr:
# Line 202 | Line 255 | memerr:
255   }
256  
257  
258 < fillscanline(scanline, zline, xres, y, xstep)   /* fill scan line at y */
258 > fillscanline(scanline, zline, sd, xres, y, xstep)       /* fill scan at y */
259   register COLOR  *scanline;
260   register float  *zline;
261 + register char  *sd;
262   int  xres, y, xstep;
263   {
264 <        int  b = xstep;
264 >        static int  nc = 0;             /* number of calls */
265 >        int  bl = xstep, b = xstep;
266          double  z;
267          register int  i;
268          
269          z = pixvalue(scanline[0], 0, y);
270          if (zline) zline[0] = z;
271 <
272 <        for (i = xstep; i < xres; i += xstep) {
273 <        
271 >                                /* zig-zag start for quincunx pattern */
272 >        for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
273 >                if (i >= xres) {
274 >                        xstep += xres-1-i;
275 >                        i = xres-1;
276 >                }
277                  z = pixvalue(scanline[i], i, y);
278                  if (zline) zline[i] = z;
279 <                
280 <                b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
281 <                                i-xstep, y, xstep, 0, b/2);
279 >                if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
280 >                if (i <= xstep)
281 >                        b = fillsample(scanline, zline, 0, y, i, 0, b/2);
282 >                else
283 >                        b = fillsample(scanline+i-xstep,
284 >                                        zline ? zline+i-xstep : NULL,
285 >                                        i-xstep, y, xstep, 0, b/2);
286 >                if (sd) *sd++ = nc & 1 ? bl : b;
287 >                bl = b;
288          }
289 <        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 <        }
289 >        if (sd && nc & 1) *sd = bl;
290   }
291  
292  
# Line 239 | Line 298 | int  xres, y, ysize;
298          COLOR  vline[MAXDIV+1];
299          float  zline[MAXDIV+1];
300          int  b = ysize;
242        double  z;
301          register int  i, j;
302          
303          for (i = 0; i < xres; i++) {
# Line 323 | Line 381 | pixvalue(col, x, y)            /* compute pixel value */
381   COLOR  col;                     /* returned color */
382   int  x, y;                      /* pixel position */
383   {
384 <        static RAY  thisray;    /* our ray for this pixel */
384 >        static RAY  thisray;
385  
386 <        rayview(thisray.rorg, thisray.rdir, &ourview,
387 <                        x + pixjitter(), y + pixjitter());
386 >        if (viewray(thisray.rorg, thisray.rdir, &ourview,
387 >                        (x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) {
388 >                setcolor(col, 0.0, 0.0, 0.0);
389 >                return(0.0);
390 >        }
391  
392          rayorigin(&thisray, NULL, PRIMARY, 1.0);
393 <        
393 >
394 >        samplendx = pixnumber(x,y,hresolu,vresolu);     /* set pixel index */
395 >
396          rayvalue(&thisray);                     /* trace ray */
397  
398          copycolor(col, thisray.rcol);           /* return color */
399          
400 <        return(thisray.rot);                    /* return distance */
400 >        return(thisray.rt);                     /* return distance */
401   }
402  
403  
# Line 357 | Line 420 | char  *oldfile;
420                                  /* discard header */
421          getheader(fp, NULL);
422                                  /* get picture size */
423 <        if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) {
423 >        if (!fscnresolu(&x, &y, fp)) {
424                  sprintf(errmsg, "bad recover file \"%s\"", oldfile);
425                  error(WARNING, errmsg);
426                  fclose(fp);
427                  return(0);
428          }
429  
430 <        if (x != ourview.hresolu || y != ourview.vresolu) {
430 >        if (x != hresolu || y != vresolu) {
431                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
432                                  oldfile);
433                  error(USER, errmsg);
434          }
435  
436 <        scanline = (COLR *)malloc(ourview.hresolu*sizeof(COLR));
436 >        scanline = (COLR *)malloc(hresolu*sizeof(COLR));
437          if (scanline == NULL)
438                  error(SYSTEM, "out of memory in salvage");
439 <        for (y = 0; y < ourview.vresolu; y++) {
440 <                if (freadcolrs(scanline, ourview.hresolu, fp) < 0)
439 >        for (y = 0; y < vresolu; y++) {
440 >                if (freadcolrs(scanline, hresolu, fp) < 0)
441                          break;
442 <                if (fwritecolrs(scanline, ourview.hresolu, stdout) < 0)
442 >                if (fwritecolrs(scanline, hresolu, stdout) < 0)
443                          goto writerr;
444          }
445          if (fflush(stdout) == EOF)
# Line 387 | Line 450 | char  *oldfile;
450          return(y);
451   writerr:
452          error(SYSTEM, "write error in salvage");
453 + }
454 +
455 +
456 + int
457 + pixnumber(x, y, xres, yres)             /* compute pixel index (brushed) */
458 + register int  x, y;
459 + int  xres, yres;
460 + {
461 +        x -= y;
462 +        while (x < 0)
463 +                x += xres;
464 +        return((((x>>2)*yres + y) << 2) + (x & 3));
465   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines