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.6 by greg, Mon Jul 10 15:21:30 1989 UTC vs.
Revision 1.31 by greg, Tue Aug 13 12:16:34 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 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>
20   #endif
21 + #include  <fcntl.h>
22  
23   #include  "view.h"
24  
25   #include  "random.h"
26  
27 < VIEW  ourview = STDVIEW(512);           /* view parameters */
27 > int  dimlist[MAXDIM];                   /* sampling dimensions */
28 > int  ndims = 0;                         /* number of sampling dimensions */
29 > int  samplendx;                         /* sample index number */
30  
31 + VIEW  ourview = STDVIEW;                /* view parameters */
32 + int  hresolu = 512;                     /* horizontal resolution */
33 + int  vresolu = 512;                     /* vertical resolution */
34 + double  pixaspect = 1.0;                /* pixel aspect ratio */
35 +
36   int  psample = 4;                       /* pixel sample size */
37   double  maxdiff = .05;                  /* max. difference for interpolation */
38   double  dstrpix = 0.67;                 /* square pixel distribution */
# Line 30 | Line 40 | double  dstrpix = 0.67;                        /* square pixel distribution
40   double  dstrsrc = 0.0;                  /* square source distribution */
41   double  shadthresh = .05;               /* shadow threshold */
42   double  shadcert = .5;                  /* shadow certainty */
43 + int  directrelay = 0;                   /* number of source relays */
44 + int  vspretest = 512;                   /* virtual source pretest density */
45 + int  directinvis = 0;                   /* sources invisible? */
46  
47   int  maxdepth = 6;                      /* maximum recursion depth */
48   double  minweight = 5e-3;               /* minimum ray weight */
# Line 47 | Line 60 | int  ralrm = 0;                                /* seconds between reports */
60  
61   double  pctdone = 0.0;                  /* percentage done */
62  
63 + long  tlastrept = 0L;                   /* time at last report */
64 +
65 + extern long  time();
66 + extern long  tstart;                    /* starting time */
67 +
68   extern long  nrays;                     /* number of rays traced */
69  
70 < #define  MAXDIV         32              /* maximum sample size */
70 > #define  MAXDIV         15              /* maximum sample size */
71  
72   #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
73  
74 + double  pixvalue();
75  
76 +
77   quit(code)                      /* quit program */
78   int  code;
79   {
# Line 64 | Line 84 | int  code;
84   }
85  
86  
87 + #ifdef BSD
88   report()                /* report progress */
89   {
69 #ifdef BSD
90          struct rusage  rubuf;
91          double  t;
92  
# Line 79 | Line 99 | report()               /* report progress */
99  
100          sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
101                          nrays, pctdone, t/3600.0);
102 +        eputs(errmsg);
103 +        tlastrept = time((long *)0);
104 + }
105   #else
106 <        sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone);
107 < #endif
106 > report()                /* report progress */
107 > {
108 >        signal(SIGALRM, report);
109 >        tlastrept = time((long *)0);
110 >        sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n",
111 >                        nrays, pctdone, (tlastrept-tstart)/3600.0);
112          eputs(errmsg);
86
87        if (ralrm > 0)
88                alarm(ralrm);
113   }
114 + #endif
115  
116  
117 < render(oldfile)                         /* render the scene */
118 < char  *oldfile;
117 > render(zfile, oldfile)                          /* render the scene */
118 > char  *zfile, *oldfile;
119   {
120 +        extern long  lseek();
121          COLOR  *scanbar[MAXDIV+1];      /* scanline arrays of pixel values */
122 +        float  *zbar[MAXDIV+1];         /* z values */
123          int  ypos;                      /* current scanline */
124 +        int  ystep;                     /* current y step size */
125 +        int  zfd;
126          COLOR  *colptr;
127 +        float  *zptr;
128          register int  i;
129 <
129 >                                        /* check sampling */
130          if (psample < 1)
131                  psample = 1;
132 <        else if (psample > MAXDIV)
132 >        else if (psample > MAXDIV) {
133 >                sprintf(errmsg, "pixel sampling reduced from %d to %d",
134 >                                psample, MAXDIV);
135 >                error(WARNING, errmsg);
136                  psample = MAXDIV;
137 <
138 <        ourview.hresolu -= ourview.hresolu % psample;
106 <        ourview.vresolu -= ourview.vresolu % psample;
107 <                
137 >        }
138 >                                        /* allocate scanlines */
139          for (i = 0; i <= psample; i++) {
140 <                scanbar[i] = (COLOR *)malloc((ourview.hresolu+1)*sizeof(COLOR));
140 >                scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
141                  if (scanbar[i] == NULL)
142 <                        error(SYSTEM, "out of memory in render");
142 >                        goto memerr;
143          }
144 <        
144 >                                        /* open z file */
145 >        if (zfile != NULL) {
146 >                if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
147 >                        sprintf(errmsg, "cannot open z file \"%s\"", zfile);
148 >                        error(SYSTEM, errmsg);
149 >                }
150 >                for (i = 0; i <= psample; i++) {
151 >                        zbar[i] = (float *)malloc(hresolu*sizeof(float));
152 >                        if (zbar[i] == NULL)
153 >                                goto memerr;
154 >                }
155 >        } else {
156 >                zfd = -1;
157 >                for (i = 0; i <= psample; i++)
158 >                        zbar[i] = NULL;
159 >        }
160                                          /* write out boundaries */
161 <        printf("-Y %d +X %d\n", ourview.vresolu, ourview.hresolu);
162 <
163 <        ypos = ourview.vresolu - salvage(oldfile);      /* find top line */
164 <        fillscanline(scanbar[0], ypos, psample);        /* top scan */
165 <
166 <        for (ypos -= psample; ypos > -psample; ypos -= psample) {
167 <        
168 <                colptr = scanbar[psample];              /* get last scanline */
169 <                scanbar[psample] = scanbar[0];
161 >        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
162 >                                        /* recover file and compute first */
163 >        i = salvage(oldfile);
164 >        if (zfd != -1 && i > 0 &&
165 >                        lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1)
166 >                error(SYSTEM, "z file seek error in render");
167 >        pctdone = 100.0*i/vresolu;
168 >        if (ralrm > 0)                  /* report init stats */
169 >                report();
170 >        ypos = vresolu-1 - i;
171 >        fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
172 >        ystep = psample;
173 >                                                /* compute scanlines */
174 >        for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
175 >                                                        /* bottom adjust? */
176 >                if (ypos < 0) {
177 >                        ystep += ypos;
178 >                        ypos = 0;
179 >                }
180 >                colptr = scanbar[ystep];                /* move base to top */
181 >                scanbar[ystep] = scanbar[0];
182                  scanbar[0] = colptr;
183 <
184 <                fillscanline(scanbar[0], ypos, psample);        /* base scan */
185 <        
186 <                fillscanbar(scanbar, ypos, psample);
187 <                
188 <                for (i = psample-1; i >= 0; i--)
189 <                        if (fwritescan(scanbar[i], ourview.hresolu, stdout) < 0)
183 >                zptr = zbar[ystep];
184 >                zbar[ystep] = zbar[0];
185 >                zbar[0] = zptr;
186 >                                                        /* fill base line */
187 >                fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
188 >                                                        /* fill bar */
189 >                fillscanbar(scanbar, zbar, hresolu, ypos, ystep);
190 >                                                        /* write it out */
191 >                for (i = ystep; i > 0; i--) {
192 >                        if (zfd != -1 && write(zfd, (char *)zbar[i],
193 >                                        hresolu*sizeof(float))
194 >                                        < hresolu*sizeof(float))
195                                  goto writerr;
196 +                        if (fwritescan(scanbar[i], hresolu, stdout) < 0)
197 +                                goto writerr;
198 +                }
199                  if (fflush(stdout) == EOF)
200                          goto writerr;
201 <                pctdone = 100.0*(ourview.vresolu-ypos)/ourview.vresolu;
201 >                                                        /* record progress */
202 >                pctdone = 100.0*(vresolu-1-ypos)/vresolu;
203 >                if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
204 >                        report();
205          }
206 <                
206 >                                                /* clean up */
207 >        if (zfd != -1) {
208 >                if (write(zfd, (char *)zbar[0], hresolu*sizeof(float))
209 >                                < hresolu*sizeof(float))
210 >                        goto writerr;
211 >                if (close(zfd) == -1)
212 >                        goto writerr;
213 >                for (i = 0; i <= psample; i++)
214 >                        free((char *)zbar[i]);
215 >        }
216 >        fwritescan(scanbar[0], hresolu, stdout);
217 >        if (fflush(stdout) == EOF)
218 >                goto writerr;
219          for (i = 0; i <= psample; i++)
220                  free((char *)scanbar[i]);
221 +        pctdone = 100.0;
222          return;
223   writerr:
224          error(SYSTEM, "write error in render");
225 + memerr:
226 +        error(SYSTEM, "out of memory in render");
227   }
228  
229  
230 < fillscanline(scanline, y, xstep)                /* fill scan line at y */
230 > fillscanline(scanline, zline, xres, y, xstep)   /* fill scan line at y */
231   register COLOR  *scanline;
232 < int  y, xstep;
232 > register float  *zline;
233 > int  xres, y, xstep;
234   {
235          int  b = xstep;
236 +        double  z;
237          register int  i;
238          
239 <        pixvalue(scanline[0], 0, y);
239 >        z = pixvalue(scanline[0], 0, y);
240 >        if (zline) zline[0] = z;
241  
242 <        for (i = xstep; i <= ourview.hresolu; i += xstep) {
243 <        
244 <                pixvalue(scanline[i], i, y);
242 >        for (i = xstep; i < xres-1+xstep; i += xstep) {
243 >                if (i >= xres) {
244 >                        xstep += xres-1-i;
245 >                        i = xres-1;
246 >                }
247 >                z = pixvalue(scanline[i], i, y);
248 >                if (zline) zline[i] = z;
249                  
250 <                b = fillsample(scanline+i-xstep, i-xstep, y, xstep, 0, b/2);
250 >                b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
251 >                                i-xstep, y, xstep, 0, b/2);
252          }
253   }
254  
255  
256 < fillscanbar(scanbar, y, ysize)          /* fill interior */
256 > fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
257   register COLOR  *scanbar[];
258 < int  y, ysize;
258 > register float  *zbar[];
259 > int  xres, y, ysize;
260   {
261          COLOR  vline[MAXDIV+1];
262 +        float  zline[MAXDIV+1];
263          int  b = ysize;
264          register int  i, j;
265          
266 <        for (i = 0; i < ourview.hresolu; i++) {
266 >        for (i = 0; i < xres; i++) {
267                  
268                  copycolor(vline[0], scanbar[0][i]);
269                  copycolor(vline[ysize], scanbar[ysize][i]);
270 +                if (zbar[0]) {
271 +                        zline[0] = zbar[0][i];
272 +                        zline[ysize] = zbar[ysize][i];
273 +                }
274                  
275 <                b = fillsample(vline, i, y, 0, ysize, b/2);
275 >                b = fillsample(vline, zbar[0] ? zline : NULL,
276 >                                i, y, 0, ysize, b/2);
277                  
278                  for (j = 1; j < ysize; j++)
279                          copycolor(scanbar[j][i], vline[j]);
280 +                if (zbar[0])
281 +                        for (j = 1; j < ysize; j++)
282 +                                zbar[j][i] = zline[j];
283          }
284   }
285  
286  
287   int
288 < fillsample(colline, x, y, xlen, ylen, b)        /* fill interior points */
288 > fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
289   register COLOR  *colline;
290 + register float  *zline;
291   int  x, y;
292   int  xlen, ylen;
293   int  b;
294   {
295 +        extern double  fabs();
296          double  ratio;
297 +        double  z;
298          COLOR  ctmp;
299          int  ncut;
300          register int  len;
# Line 202 | Line 307 | int  b;
307          if (len <= 1)                   /* limit recursion */
308                  return(0);
309          
310 <        if (b > 0 || bigdiff(colline[0], colline[len], maxdiff)) {
310 >        if (b > 0
311 >        || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
312 >                        || bigdiff(colline[0], colline[len], maxdiff)) {
313          
314 <                pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
314 >                z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
315 >                if (zline) zline[len>>1] = z;
316                  ncut = 1;
317                  
318          } else {                                        /* interpolate */
# Line 212 | Line 320 | int  b;
320                  copycolor(colline[len>>1], colline[len]);
321                  ratio = (double)(len>>1) / len;
322                  scalecolor(colline[len>>1], ratio);
323 <                copycolor(ctmp, colline[0]);
323 >                if (zline) zline[len>>1] = zline[len] * ratio;
324                  ratio = 1.0 - ratio;
325 +                copycolor(ctmp, colline[0]);
326                  scalecolor(ctmp, ratio);
327                  addcolor(colline[len>>1], ctmp);
328 +                if (zline) zline[len>>1] += zline[0] * ratio;
329                  ncut = 0;
330          }
331                                                          /* recurse */
332 <        ncut += fillsample(colline, x, y, xlen>>1, ylen>>1, (b-1)/2);
332 >        ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
333          
334 <        ncut += fillsample(colline + (len>>1), x + (xlen>>1), y + (ylen>>1),
335 <                                xlen - (xlen>>1), ylen - (ylen>>1), b/2);
334 >        ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
335 >                        x+(xlen>>1), y+(ylen>>1),
336 >                        xlen-(xlen>>1), ylen-(ylen>>1), b/2);
337  
338          return(ncut);
339   }
340  
341  
342 + double
343   pixvalue(col, x, y)             /* compute pixel value */
344   COLOR  col;                     /* returned color */
345   int  x, y;                      /* pixel position */
346   {
347 <        static RAY  thisray;    /* our ray for this pixel */
347 >        static RAY  thisray;
348  
349 <        rayview(thisray.rorg, thisray.rdir, &ourview,
350 <                        x + pixjitter(), y + pixjitter());
349 >        if (viewray(thisray.rorg, thisray.rdir, &ourview,
350 >                        (x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) {
351 >                setcolor(col, 0.0, 0.0, 0.0);
352 >                return(0.0);
353 >        }
354  
355          rayorigin(&thisray, NULL, PRIMARY, 1.0);
356 <        
356 >
357 >        samplendx = pixnumber(x,y,hresolu,vresolu);     /* set pixel index */
358 >
359          rayvalue(&thisray);                     /* trace ray */
360  
361          copycolor(col, thisray.rcol);           /* return color */
362 +        
363 +        return(thisray.rt);                     /* return distance */
364   }
365  
366  
# Line 255 | Line 374 | char  *oldfile;
374  
375          if (oldfile == NULL)
376                  return(0);
377 <        else if ((fp = fopen(oldfile, "r")) == NULL) {
377 >        
378 >        if ((fp = fopen(oldfile, "r")) == NULL) {
379                  sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
380                  error(WARNING, errmsg);
381                  return(0);
# Line 263 | Line 383 | char  *oldfile;
383                                  /* discard header */
384          getheader(fp, NULL);
385                                  /* get picture size */
386 <        if (fscanf(fp, "-Y %d +X %d\n", &y, &x) != 2) {
386 >        if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) {
387                  sprintf(errmsg, "bad recover file \"%s\"", oldfile);
388                  error(WARNING, errmsg);
389                  fclose(fp);
390                  return(0);
391          }
392  
393 <        if (x != ourview.hresolu || y != ourview.vresolu) {
393 >        if (x != hresolu || y != vresolu) {
394                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
395                                  oldfile);
396                  error(USER, errmsg);
277                return(0);
397          }
398  
399 <        scanline = (COLR *)malloc(ourview.hresolu*sizeof(COLR));
399 >        scanline = (COLR *)malloc(hresolu*sizeof(COLR));
400          if (scanline == NULL)
401                  error(SYSTEM, "out of memory in salvage");
402 <        for (y = 0; y < ourview.vresolu; y++) {
403 <                if (freadcolrs(scanline, ourview.hresolu, fp) < 0)
402 >        for (y = 0; y < vresolu; y++) {
403 >                if (freadcolrs(scanline, hresolu, fp) < 0)
404                          break;
405 <                if (fwritecolrs(scanline, ourview.hresolu, stdout) < 0)
405 >                if (fwritecolrs(scanline, hresolu, stdout) < 0)
406                          goto writerr;
407          }
408          if (fflush(stdout) == EOF)
# Line 294 | Line 413 | char  *oldfile;
413          return(y);
414   writerr:
415          error(SYSTEM, "write error in salvage");
416 + }
417 +
418 +
419 + int
420 + pixnumber(x, y, xres, yres)             /* compute pixel index (brushed) */
421 + register int  x, y;
422 + int  xres, yres;
423 + {
424 +        x -= y;
425 +        while (x < 0)
426 +                x += xres;
427 +        return((((x>>2)*yres + y) << 2) + (x & 3));
428   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines