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.3 by greg, Wed Jun 7 08:35:21 1989 UTC vs.
Revision 2.1 by greg, Tue Nov 12 17:08:26 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 */
41  
42   double  dstrsrc = 0.0;                  /* square source distribution */
43 < double  shadthresh = .01;               /* shadow threshold */
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 */
52  
53   COLOR  ambval = BLKCOLOR;               /* ambient value */
54   double  ambacc = 0.2;                   /* ambient accuracy */
55 < int  ambres = 128;                      /* ambient resolution */
55 > int  ambres = 32;                       /* ambient resolution */
56   int  ambdiv = 128;                      /* ambient divisions */
57   int  ambssamp = 0;                      /* ambient super-samples */
58   int  ambounce = 0;                      /* ambient bounces */
# Line 46 | 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         15              /* maximum sample size */
74  
75   #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
76  
77 + double  pixvalue();
78  
79 +
80   quit(code)                      /* quit program */
81   int  code;
82   {
# Line 63 | Line 87 | int  code;
87   }
88  
89  
90 + #ifdef BSD
91   report()                /* report progress */
92   {
68 #ifdef BSD
93          struct rusage  rubuf;
94          double  t;
95  
# Line 78 | 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 >        signal(SIGALRM, report);
112 >        tlastrept = time((long *)0);
113 >        sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n",
114 >                        nrays, pctdone, (tlastrept-tstart)/3600.0);
115          eputs(errmsg);
85
86        if (ralrm > 0)
87                alarm(ralrm);
116   }
117 + #endif
118  
119  
120 < render(oldfile)                         /* render the scene */
121 < char  *oldfile;
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          int  ypos;                      /* current scanline */
127 +        int  ystep;                     /* current y step size */
128 +        int  hstep;                     /* h step size */
129 +        int  zfd;
130          COLOR  *colptr;
131 +        float  *zptr;
132          register int  i;
133 <
133 >                                        /* check sampling */
134          if (psample < 1)
135                  psample = 1;
136 <        else if (psample > MAXDIV)
136 >        else if (psample > MAXDIV) {
137 >                sprintf(errmsg, "pixel sampling reduced from %d to %d",
138 >                                psample, MAXDIV);
139 >                error(WARNING, errmsg);
140                  psample = MAXDIV;
141 <
142 <        ourview.hresolu -= ourview.hresolu % psample;
105 <        ourview.vresolu -= ourview.vresolu % psample;
106 <                
141 >        }
142 >                                        /* allocate scanlines */
143          for (i = 0; i <= psample; i++) {
144 <                scanbar[i] = (COLOR *)malloc((ourview.hresolu+1)*sizeof(COLOR));
144 >                scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
145                  if (scanbar[i] == NULL)
146 <                        error(SYSTEM, "out of memory in render");
146 >                        goto memerr;
147          }
148 <        
148 >                                        /* open z file */
149 >        if (zfile != NULL) {
150 >                if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
151 >                        sprintf(errmsg, "cannot open z file \"%s\"", zfile);
152 >                        error(SYSTEM, errmsg);
153 >                }
154 >                for (i = 0; i <= psample; i++) {
155 >                        zbar[i] = (float *)malloc(hresolu*sizeof(float));
156 >                        if (zbar[i] == NULL)
157 >                                goto memerr;
158 >                }
159 >        } else {
160 >                zfd = -1;
161 >                for (i = 0; i <= psample; i++)
162 >                        zbar[i] = NULL;
163 >        }
164                                          /* write out boundaries */
165 <        printf("-Y %d +X %d\n", ourview.vresolu, ourview.hresolu);
166 <
167 <        ypos = ourview.vresolu - salvage(oldfile);      /* find top line */
168 <        fillscanline(scanbar[0], ypos, psample);        /* top scan */
169 <
170 <        for (ypos -= psample; ypos > -psample; ypos -= psample) {
171 <        
172 <                colptr = scanbar[psample];              /* get last scanline */
173 <                scanbar[psample] = scanbar[0];
165 >        fprtresolu(hresolu, vresolu, stdout);
166 >                                        /* recover file and compute first */
167 >        i = salvage(oldfile);
168 >        if (zfd != -1 && i > 0 &&
169 >                        lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1)
170 >                error(SYSTEM, "z file seek error in render");
171 >        pctdone = 100.0*i/vresolu;
172 >        if (ralrm > 0)                  /* report init stats */
173 >                report();
174 > #ifndef  BSD
175 >        else
176 > #endif
177 >        signal(SIGALRM, report);
178 >        ypos = vresolu-1 - i;
179 >        hstep = (psample*140+49)/99;            /* quincunx sampling */
180 >        ystep = (psample*99+70)/140;
181 >        fillscanline(scanbar[0], zbar[0], hresolu, ypos, hstep);
182 >                                                /* compute scanlines */
183 >        for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
184 >                                                        /* bottom adjust? */
185 >                if (ypos < 0) {
186 >                        ystep += ypos;
187 >                        ypos = 0;
188 >                }
189 >                colptr = scanbar[ystep];                /* move base to top */
190 >                scanbar[ystep] = scanbar[0];
191                  scanbar[0] = colptr;
192 <
193 <                fillscanline(scanbar[0], ypos, psample);        /* base scan */
194 <        
195 <                fillscanbar(scanbar, ypos, psample);
196 <                
197 <                for (i = psample-1; i >= 0; i--)
198 <                        if (fwritescan(scanbar[i], ourview.hresolu, stdout) < 0)
192 >                zptr = zbar[ystep];
193 >                zbar[ystep] = zbar[0];
194 >                zbar[0] = zptr;
195 >                                                        /* fill base line */
196 >                fillscanline(scanbar[0], zbar[0], hresolu, ypos, hstep);
197 >                                                        /* fill bar */
198 >                fillscanbar(scanbar, zbar, hresolu, ypos, ystep);
199 >                                                        /* write it out */
200 > #ifndef  BSD
201 >                signal(SIGALRM, SIG_IGN);       /* don't interrupt writes */
202 > #endif
203 >                for (i = ystep; i > 0; i--) {
204 >                        if (zfd != -1 && write(zfd, (char *)zbar[i],
205 >                                        hresolu*sizeof(float))
206 >                                        < hresolu*sizeof(float))
207                                  goto writerr;
208 +                        if (fwritescan(scanbar[i], hresolu, stdout) < 0)
209 +                                goto writerr;
210 +                }
211                  if (fflush(stdout) == EOF)
212                          goto writerr;
213 <                pctdone = 100.0*(ourview.vresolu-ypos)/ourview.vresolu;
213 >                                                        /* record progress */
214 >                pctdone = 100.0*(vresolu-1-ypos)/vresolu;
215 >                if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
216 >                        report();
217 > #ifndef  BSD
218 >                else
219 >                        signal(SIGALRM, report);
220 > #endif
221          }
222 <                
222 >                                                /* clean up */
223 >        if (zfd != -1) {
224 >                if (write(zfd, (char *)zbar[0], hresolu*sizeof(float))
225 >                                < hresolu*sizeof(float))
226 >                        goto writerr;
227 >                if (close(zfd) == -1)
228 >                        goto writerr;
229 >                for (i = 0; i <= psample; i++)
230 >                        free((char *)zbar[i]);
231 >        }
232 >        fwritescan(scanbar[0], hresolu, stdout);
233 >        if (fflush(stdout) == EOF)
234 >                goto writerr;
235          for (i = 0; i <= psample; i++)
236                  free((char *)scanbar[i]);
237 +        pctdone = 100.0;
238          return;
239   writerr:
240          error(SYSTEM, "write error in render");
241 + memerr:
242 +        error(SYSTEM, "out of memory in render");
243   }
244  
245  
246 < fillscanline(scanline, y, xstep)                /* fill scan line at y */
246 > fillscanline(scanline, zline, xres, y, xstep)   /* fill scan line at y */
247   register COLOR  *scanline;
248 < int  y, xstep;
248 > register float  *zline;
249 > int  xres, y, xstep;
250   {
251 +        static int  nc = 0;             /* number of calls */
252          int  b = xstep;
253 +        double  z;
254          register int  i;
255          
256 <        pixvalue(scanline[0], 0, y);
257 <
258 <        for (i = xstep; i <= ourview.hresolu; i += xstep) {
259 <        
260 <                pixvalue(scanline[i], i, y);
256 >        z = pixvalue(scanline[0], 0, y);
257 >        if (zline) zline[0] = z;
258 >                                /* zig-zag start for quincunx pattern */
259 >        for (i = nc++ & 1 ? xstep/2 : xstep; i < xres-1+xstep; i += xstep) {
260 >                if (i >= xres) {
261 >                        xstep += xres-1-i;
262 >                        i = xres-1;
263 >                }
264 >                z = pixvalue(scanline[i], i, y);
265 >                if (zline) zline[i] = z;
266                  
267 <                b = fillsample(scanline+i-xstep, i-xstep, y, xstep, 0, b/2);
267 >                b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
268 >                                i-xstep, y, xstep, 0, b/2);
269          }
270   }
271  
272  
273 < fillscanbar(scanbar, y, ysize)          /* fill interior */
273 > fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
274   register COLOR  *scanbar[];
275 < int  y, ysize;
275 > register float  *zbar[];
276 > int  xres, y, ysize;
277   {
278          COLOR  vline[MAXDIV+1];
279 +        float  zline[MAXDIV+1];
280          int  b = ysize;
281          register int  i, j;
282          
283 <        for (i = 0; i < ourview.hresolu; i++) {
283 >        for (i = 0; i < xres; i++) {
284                  
285                  copycolor(vline[0], scanbar[0][i]);
286                  copycolor(vline[ysize], scanbar[ysize][i]);
287 +                if (zbar[0]) {
288 +                        zline[0] = zbar[0][i];
289 +                        zline[ysize] = zbar[ysize][i];
290 +                }
291                  
292 <                b = fillsample(vline, i, y, 0, ysize, b/2);
292 >                b = fillsample(vline, zbar[0] ? zline : NULL,
293 >                                i, y, 0, ysize, b/2);
294                  
295                  for (j = 1; j < ysize; j++)
296                          copycolor(scanbar[j][i], vline[j]);
297 +                if (zbar[0])
298 +                        for (j = 1; j < ysize; j++)
299 +                                zbar[j][i] = zline[j];
300          }
301   }
302  
303  
304   int
305 < fillsample(colline, x, y, xlen, ylen, b)        /* fill interior points */
305 > fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
306   register COLOR  *colline;
307 + register float  *zline;
308   int  x, y;
309   int  xlen, ylen;
310   int  b;
311   {
312 +        extern double  fabs();
313          double  ratio;
314 +        double  z;
315          COLOR  ctmp;
316          int  ncut;
317          register int  len;
# Line 201 | Line 324 | int  b;
324          if (len <= 1)                   /* limit recursion */
325                  return(0);
326          
327 <        if (b > 0 || bigdiff(colline[0], colline[len], maxdiff)) {
327 >        if (b > 0
328 >        || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
329 >                        || bigdiff(colline[0], colline[len], maxdiff)) {
330          
331 <                pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
331 >                z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
332 >                if (zline) zline[len>>1] = z;
333                  ncut = 1;
334                  
335          } else {                                        /* interpolate */
# Line 211 | Line 337 | int  b;
337                  copycolor(colline[len>>1], colline[len]);
338                  ratio = (double)(len>>1) / len;
339                  scalecolor(colline[len>>1], ratio);
340 <                copycolor(ctmp, colline[0]);
340 >                if (zline) zline[len>>1] = zline[len] * ratio;
341                  ratio = 1.0 - ratio;
342 +                copycolor(ctmp, colline[0]);
343                  scalecolor(ctmp, ratio);
344                  addcolor(colline[len>>1], ctmp);
345 +                if (zline) zline[len>>1] += zline[0] * ratio;
346                  ncut = 0;
347          }
348                                                          /* recurse */
349 <        ncut += fillsample(colline, x, y, xlen>>1, ylen>>1, (b-1)/2);
349 >        ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
350          
351 <        ncut += fillsample(colline + (len>>1), x + (xlen>>1), y + (ylen>>1),
352 <                                xlen - (xlen>>1), ylen - (ylen>>1), b/2);
351 >        ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
352 >                        x+(xlen>>1), y+(ylen>>1),
353 >                        xlen-(xlen>>1), ylen-(ylen>>1), b/2);
354  
355          return(ncut);
356   }
357  
358  
359 + double
360   pixvalue(col, x, y)             /* compute pixel value */
361   COLOR  col;                     /* returned color */
362   int  x, y;                      /* pixel position */
363   {
364 <        static RAY  thisray;    /* our ray for this pixel */
364 >        static RAY  thisray;
365  
366 <        rayview(thisray.rorg, thisray.rdir, &ourview,
367 <                        x + pixjitter(), y + pixjitter());
366 >        if (viewray(thisray.rorg, thisray.rdir, &ourview,
367 >                        (x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) {
368 >                setcolor(col, 0.0, 0.0, 0.0);
369 >                return(0.0);
370 >        }
371  
372          rayorigin(&thisray, NULL, PRIMARY, 1.0);
373 <        
373 >
374 >        samplendx = pixnumber(x,y,hresolu,vresolu);     /* set pixel index */
375 >
376          rayvalue(&thisray);                     /* trace ray */
377  
378          copycolor(col, thisray.rcol);           /* return color */
379 +        
380 +        return(thisray.rt);                     /* return distance */
381   }
382  
383  
# Line 254 | Line 391 | char  *oldfile;
391  
392          if (oldfile == NULL)
393                  return(0);
394 <        else if ((fp = fopen(oldfile, "r")) == NULL) {
394 >        
395 >        if ((fp = fopen(oldfile, "r")) == NULL) {
396                  sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
397                  error(WARNING, errmsg);
398                  return(0);
# Line 262 | Line 400 | char  *oldfile;
400                                  /* discard header */
401          getheader(fp, NULL);
402                                  /* get picture size */
403 <        if (fscanf(fp, "-Y %d +X %d\n", &y, &x) != 2) {
403 >        if (!fscnresolu(&x, &y, fp)) {
404                  sprintf(errmsg, "bad recover file \"%s\"", oldfile);
405                  error(WARNING, errmsg);
406                  fclose(fp);
407                  return(0);
408          }
409  
410 <        if (x != ourview.hresolu || y != ourview.vresolu) {
410 >        if (x != hresolu || y != vresolu) {
411                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
412                                  oldfile);
413                  error(USER, errmsg);
276                return(0);
414          }
415  
416 <        scanline = (COLR *)malloc(ourview.hresolu*sizeof(COLR));
416 >        scanline = (COLR *)malloc(hresolu*sizeof(COLR));
417          if (scanline == NULL)
418                  error(SYSTEM, "out of memory in salvage");
419 <        for (y = 0; y < ourview.vresolu; y++) {
420 <                if (freadcolrs(scanline, ourview.hresolu, fp) < 0)
419 >        for (y = 0; y < vresolu; y++) {
420 >                if (freadcolrs(scanline, hresolu, fp) < 0)
421                          break;
422 <                if (fwritecolrs(scanline, ourview.hresolu, stdout) < 0)
422 >                if (fwritecolrs(scanline, hresolu, stdout) < 0)
423                          goto writerr;
424          }
425          if (fflush(stdout) == EOF)
# Line 293 | Line 430 | char  *oldfile;
430          return(y);
431   writerr:
432          error(SYSTEM, "write error in salvage");
433 + }
434 +
435 +
436 + int
437 + pixnumber(x, y, xres, yres)             /* compute pixel index (brushed) */
438 + register int  x, y;
439 + int  xres, yres;
440 + {
441 +        x -= y;
442 +        while (x < 0)
443 +                x += xres;
444 +        return((((x>>2)*yres + y) << 2) + (x & 3));
445   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines