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.18 by greg, Wed Feb 21 12:39:37 1990 UTC vs.
Revision 2.10 by greg, Sat Aug 8 18:45:02 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 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
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 + 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 */
# Line 36 | 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 + double  specthresh = .15;               /* specular sampling threshold */
51 + double  specjitter = 1.;                /* specular sampling jitter */
52 +
53   int  maxdepth = 6;                      /* maximum recursion depth */
54   double  minweight = 5e-3;               /* minimum ray weight */
55  
# Line 53 | Line 66 | int  ralrm = 0;                                /* seconds between reports */
66  
67   double  pctdone = 0.0;                  /* percentage done */
68  
69 + long  tlastrept = 0L;                   /* time at last report */
70 +
71 + extern long  time();
72 + extern long  tstart;                    /* starting time */
73 +
74   extern long  nrays;                     /* number of rays traced */
75  
76 < #define  MAXDIV         32              /* maximum sample size */
76 > #define  MAXDIV         16              /* maximum sample size */
77  
78   #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
79  
80 + #define  RFTEMPLATE     "rfXXXXXX"
81 + #define  HFTEMPLATE     "/tmp/hfXXXXXX"
82 +
83 + static char  *hfname = NULL;            /* header file name */
84 + static FILE  *hfp = NULL;               /* header file pointer */
85 +
86 + static int  hres, vres;                 /* resolution for this frame */
87 +
88 + extern char  *mktemp();
89 +
90   double  pixvalue();
91  
92  
93   quit(code)                      /* quit program */
94   int  code;
95   {
96 <        if (code || ralrm > 0)          /* report status */
96 >        if (code)                       /* report status */
97                  report();
98 <
98 >        if (hfname != NULL) {           /* delete header file */
99 >                if (hfp != NULL)
100 >                        fclose(hfp);
101 >                unlink(hfname);
102 >        }
103          exit(code);
104   }
105  
106  
107 + #ifdef BSD
108   report()                /* report progress */
109   {
77 #ifdef BSD
110          struct rusage  rubuf;
111          double  t;
112  
# Line 87 | Line 119 | report()               /* report progress */
119  
120          sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
121                          nrays, pctdone, t/3600.0);
122 +        eputs(errmsg);
123 +        tlastrept = time((long *)0);
124 + }
125   #else
126 + report()                /* report progress */
127 + {
128 +        tlastrept = time((long *)0);
129 +        sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n",
130 +                        nrays, pctdone, (tlastrept-tstart)/3600.0);
131 +        eputs(errmsg);
132          signal(SIGALRM, report);
133 <        sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone);
133 > }
134   #endif
94        eputs(errmsg);
135  
136 <        if (ralrm > 0)
137 <                alarm(ralrm);
136 >
137 > openheader()                    /* save standard output to header file */
138 > {
139 >        hfname = mktemp(HFTEMPLATE);
140 >        if (freopen(hfname, "w", stdout) == NULL) {
141 >                sprintf(errmsg, "cannot open header file \"%s\"", hfname);
142 >                error(SYSTEM, errmsg);
143 >        }
144   }
145  
146  
147 + closeheader()                   /* done with header output */
148 + {
149 +        if (hfname == NULL)
150 +                return;
151 +        if (fflush(stdout) == EOF || (hfp = fopen(hfname, "r")) == NULL)
152 +                error(SYSTEM, "error reopening header file");
153 + }
154 +
155 +
156 + dupheader()                     /* repeat header on standard output */
157 + {
158 +        register int  c;
159 +
160 +        if (fseek(hfp, 0L, 0) < 0)
161 +                error(SYSTEM, "seek error on header file");
162 +        while ((c = getc(hfp)) != EOF)
163 +                putchar(c);
164 + }
165 +
166 +
167 + rpict(seq, pout, zout, prvr)                    /* generate image(s) */
168 + int  seq;
169 + char  *pout, *zout, *prvr;
170 + /*
171 + * If seq is greater than zero, then we will render a sequence of
172 + * images based on view parameter strings read from the standard input.
173 + * If pout is NULL, then all images will be sent to the standard ouput.
174 + * If seq is greater than zero and prvr is an integer, then it is the
175 + * frame number at which rendering should begin.  Preceeding view parameter
176 + * strings will be skipped in the input.
177 + * If pout and prvr are the same, prvr is renamed to avoid overwriting.
178 + * Note that pout and zout should contain %d format specifications for
179 + * sequenced file naming.
180 + */
181 + {
182 +        extern char  *rindex(), *strncpy(), *strcat();
183 +        char  fbuf[128], fbuf2[128];
184 +        register char  *cp;
185 +        RESOLU  rs;
186 +        double  pa;
187 +                                        /* finished writing header */
188 +        closeheader();
189 +                                        /* check sampling */
190 +        if (psample < 1)
191 +                psample = 1;
192 +        else if (psample > MAXDIV) {
193 +                sprintf(errmsg, "pixel sampling reduced from %d to %d",
194 +                                psample, MAXDIV);
195 +                error(WARNING, errmsg);
196 +                psample = MAXDIV;
197 +        }
198 +                                        /* get starting frame */
199 +        if (seq <= 0)
200 +                seq = 0;
201 +        else if (prvr != NULL && isint(prvr)) {
202 +                register int  rn;               /* skip to specified view */
203 +                if ((rn = atoi(prvr)) < seq)
204 +                        error(USER, "recover frame less than start frame");
205 +                if (pout == NULL)
206 +                        error(USER, "missing output file specification");
207 +                for ( ; seq < rn; seq++)
208 +                        if (nextview(stdin) == EOF)
209 +                                error(USER, "unexpected EOF on view input");
210 +                prvr = fbuf;                    /* mark for renaming */
211 +        }
212 +        if (pout != NULL) {
213 +                sprintf(fbuf, pout, seq);
214 +                if (prvr != NULL && !strcmp(prvr, fbuf)) {      /* rename */
215 +                        fbuf2[0] = '\0';
216 +                        if ((cp = rindex(fbuf, '/')) != NULL)
217 +                                strncpy(fbuf2, fbuf, cp-fbuf+1);
218 +                        strcat(fbuf2, RFTEMPLATE);
219 +                        prvr = mktemp(fbuf2);
220 +                        if (rename(fbuf, prvr) < 0 && errno != ENOENT) {
221 +                                sprintf(errmsg,
222 +                                        "cannot rename \"%s\" to \"%s\"",
223 +                                                fbuf, prvr);
224 +                                error(SYSTEM, errmsg);
225 +                        }
226 +                }
227 +        }
228 +                                        /* render sequence */
229 +        do {
230 +                if (seq && nextview(stdin) == EOF)
231 +                        break;
232 +                if (pout != NULL) {
233 +                        sprintf(fbuf, pout, seq);
234 +                        if (freopen(fbuf, "w", stdout) == NULL) {
235 +                                sprintf(errmsg,
236 +                                        "cannot open output file \"%s\"", fbuf);
237 +                                error(SYSTEM, errmsg);
238 +                        }
239 +                        dupheader();
240 +                }
241 +                hres = hresolu; vres = vresolu; pa = pixaspect;
242 +                if (prvr != NULL)
243 +                        if (viewfile(prvr, &ourview, &rs) <= 0
244 +                                        || rs.or != PIXSTANDARD) {
245 +                                sprintf(errmsg,
246 +                        "cannot recover view parameters from \"%s\"", prvr);
247 +                                error(WARNING, errmsg);
248 +                        } else {
249 +                                pa = 0.0;
250 +                                hres = scanlen(&rs);
251 +                                vres = numscans(&rs);
252 +                        }
253 +                if ((cp = setview(&ourview)) != NULL)
254 +                        error(USER, cp);
255 +                normaspect(viewaspect(&ourview), &pa, &hres, &vres);
256 +                if (seq) {
257 +                        if (ralrm > 0) {
258 +                                sprintf(errmsg, "starting frame %d\n", seq);
259 +                                eputs(errmsg);
260 +                        }
261 +                        printf("FRAME=%d\n", seq);
262 +                }
263 +                fputs(VIEWSTR, stdout);
264 +                fprintview(&ourview, stdout);
265 +                putchar('\n');
266 +                if (pa < .99 || pa > 1.01)
267 +                        fputaspect(pa, stdout);
268 +                fputformat(COLRFMT, stdout);
269 +                putchar('\n');
270 +                if (zout != NULL)
271 +                        sprintf(cp=fbuf, zout, seq);
272 +                else
273 +                        cp = NULL;
274 +                render(cp, prvr);
275 +                prvr = NULL;
276 +        } while (seq++);
277 + }
278 +
279 +
280 + nextview(fp)                            /* get next view from fp */
281 + FILE  *fp;
282 + {
283 +        char  linebuf[256];
284 +
285 +        while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
286 +                if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
287 +                        return(0);
288 +        return(EOF);
289 + }      
290 +
291 +
292   render(zfile, oldfile)                          /* render the scene */
293   char  *zfile, *oldfile;
294   {
295          extern long  lseek();
296          COLOR  *scanbar[MAXDIV+1];      /* scanline arrays of pixel values */
297          float  *zbar[MAXDIV+1];         /* z values */
298 +        char  *sampdens;                /* previous sample density */
299          int  ypos;                      /* current scanline */
300          int  ystep;                     /* current y step size */
301 +        int  hstep;                     /* h step size */
302          int  zfd;
303          COLOR  *colptr;
304          float  *zptr;
305          register int  i;
113                                        /* check sampling */
114        if (psample < 1)
115                psample = 1;
116        else if (psample > MAXDIV)
117                psample = MAXDIV;
306                                          /* allocate scanlines */
307          for (i = 0; i <= psample; i++) {
308 <                scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
308 >                scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
309                  if (scanbar[i] == NULL)
310                          goto memerr;
311          }
312 +        hstep = (psample*140+49)/99;            /* quincunx sampling */
313 +        ystep = (psample*99+70)/140;
314 +        if (hstep > 2) {
315 +                i = hres/hstep + 2;
316 +                if ((sampdens = malloc(i)) == NULL)
317 +                        goto memerr;
318 +                while (i--)
319 +                        sampdens[i] = hstep;
320 +        } else
321 +                sampdens = NULL;
322                                          /* open z file */
323          if (zfile != NULL) {
324                  if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
# Line 128 | Line 326 | char  *zfile, *oldfile;
326                          error(SYSTEM, errmsg);
327                  }
328                  for (i = 0; i <= psample; i++) {
329 <                        zbar[i] = (float *)malloc(hresolu*sizeof(float));
329 >                        zbar[i] = (float *)malloc(hres*sizeof(float));
330                          if (zbar[i] == NULL)
331                                  goto memerr;
332                  }
# Line 138 | Line 336 | char  *zfile, *oldfile;
336                          zbar[i] = NULL;
337          }
338                                          /* write out boundaries */
339 <        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
339 >        fprtresolu(hres, vres, stdout);
340                                          /* recover file and compute first */
341          i = salvage(oldfile);
342 <        if (zfd != -1 && lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1)
342 >        if (zfd != -1 && i > 0 &&
343 >                        lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
344                  error(SYSTEM, "z file seek error in render");
345 <        ypos = vresolu-1 - i;
346 <        fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
347 <        ystep = psample;
345 >        pctdone = 100.0*i/vres;
346 >        if (ralrm > 0)                  /* report init stats */
347 >                report();
348 > #ifndef  BSD
349 >        else
350 > #endif
351 >        signal(SIGALRM, report);
352 >        ypos = vres-1 - i;
353 >        fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
354                                                  /* compute scanlines */
355          for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
151                                                        /* record progress */
152                pctdone = 100.0*(vresolu-ypos-ystep)/vresolu;
356                                                          /* bottom adjust? */
357                  if (ypos < 0) {
358                          ystep += ypos;
# Line 162 | Line 365 | char  *zfile, *oldfile;
365                  zbar[ystep] = zbar[0];
366                  zbar[0] = zptr;
367                                                          /* fill base line */
368 <                fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
368 >                fillscanline(scanbar[0], zbar[0], sampdens,
369 >                                hres, ypos, hstep);
370                                                          /* fill bar */
371 <                fillscanbar(scanbar, zbar, hresolu, ypos, ystep);
371 >                fillscanbar(scanbar, zbar, hres, ypos, ystep);
372                                                          /* write it out */
373 + #ifndef  BSD
374 +                signal(SIGALRM, SIG_IGN);       /* don't interrupt writes */
375 + #endif
376                  for (i = ystep; i > 0; i--) {
377                          if (zfd != -1 && write(zfd, (char *)zbar[i],
378 <                                        hresolu*sizeof(float))
379 <                                        < hresolu*sizeof(float))
378 >                                        hres*sizeof(float))
379 >                                        < hres*sizeof(float))
380                                  goto writerr;
381 <                        if (fwritescan(scanbar[i], hresolu, stdout) < 0)
381 >                        if (fwritescan(scanbar[i], hres, stdout) < 0)
382                                  goto writerr;
383                  }
384                  if (fflush(stdout) == EOF)
385                          goto writerr;
386 +                                                        /* record progress */
387 +                pctdone = 100.0*(vres-1-ypos)/vres;
388 +                if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
389 +                        report();
390 + #ifndef  BSD
391 +                else
392 +                        signal(SIGALRM, report);
393 + #endif
394          }
395                                                  /* clean up */
396 +        signal(SIGALRM, SIG_IGN);
397          if (zfd != -1) {
398 <                if (write(zfd, (char *)zbar[0], hresolu*sizeof(float))
399 <                                < hresolu*sizeof(float))
398 >                if (write(zfd, (char *)zbar[0], hres*sizeof(float))
399 >                                < hres*sizeof(float))
400                          goto writerr;
401 <                close(zfd);
401 >                if (close(zfd) == -1)
402 >                        goto writerr;
403                  for (i = 0; i <= psample; i++)
404                          free((char *)zbar[i]);
405          }
406 <        fwritescan(scanbar[0], hresolu, stdout);
406 >        fwritescan(scanbar[0], hres, stdout);
407          if (fflush(stdout) == EOF)
408                  goto writerr;
409          for (i = 0; i <= psample; i++)
410                  free((char *)scanbar[i]);
411 +        if (sampdens != NULL)
412 +                free(sampdens);
413          pctdone = 100.0;
414          return;
415   writerr:
# Line 200 | Line 419 | memerr:
419   }
420  
421  
422 < fillscanline(scanline, zline, xres, y, xstep)   /* fill scan line at y */
422 > fillscanline(scanline, zline, sd, xres, y, xstep)       /* fill scan at y */
423   register COLOR  *scanline;
424   register float  *zline;
425 + register char  *sd;
426   int  xres, y, xstep;
427   {
428 <        int  b = xstep;
428 >        static int  nc = 0;             /* number of calls */
429 >        int  bl = xstep, b = xstep;
430          double  z;
431          register int  i;
432          
433          z = pixvalue(scanline[0], 0, y);
434          if (zline) zline[0] = z;
435 <
436 <        for (i = xstep; i < xres-1+xstep; i += xstep) {
435 >                                /* zig-zag start for quincunx pattern */
436 >        for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
437                  if (i >= xres) {
438                          xstep += xres-1-i;
439                          i = xres-1;
440                  }
441                  z = pixvalue(scanline[i], i, y);
442                  if (zline) zline[i] = z;
443 <                
444 <                b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
445 <                                i-xstep, y, xstep, 0, b/2);
443 >                if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
444 >                if (i <= xstep)
445 >                        b = fillsample(scanline, zline, 0, y, i, 0, b/2);
446 >                else
447 >                        b = fillsample(scanline+i-xstep,
448 >                                        zline ? zline+i-xstep : NULL,
449 >                                        i-xstep, y, xstep, 0, b/2);
450 >                if (sd) *sd++ = nc & 1 ? bl : b;
451 >                bl = b;
452          }
453 +        if (sd && nc & 1) *sd = bl;
454   }
455  
456  
# Line 234 | Line 462 | int  xres, y, ysize;
462          COLOR  vline[MAXDIV+1];
463          float  zline[MAXDIV+1];
464          int  b = ysize;
237        double  z;
465          register int  i, j;
466          
467          for (i = 0; i < xres; i++) {
# Line 318 | Line 545 | pixvalue(col, x, y)            /* compute pixel value */
545   COLOR  col;                     /* returned color */
546   int  x, y;                      /* pixel position */
547   {
548 <        static RAY  thisray;    /* our ray for this pixel */
548 >        static RAY  thisray;
549  
550 <        viewray(thisray.rorg, thisray.rdir, &ourview,
551 <                        (x+pixjitter())/hresolu, (y+pixjitter())/vresolu);
550 >        if (viewray(thisray.rorg, thisray.rdir, &ourview,
551 >                        (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) {
552 >                setcolor(col, 0.0, 0.0, 0.0);
553 >                return(0.0);
554 >        }
555  
556          rayorigin(&thisray, NULL, PRIMARY, 1.0);
557 <        
557 >
558 >        samplendx = pixnumber(x,y,hres,vres);   /* set pixel index */
559 >
560          rayvalue(&thisray);                     /* trace ray */
561  
562          copycolor(col, thisray.rcol);           /* return color */
563          
564 <        return(thisray.rot);                    /* return distance */
564 >        return(thisray.rt);                     /* return distance */
565   }
566  
567  
# Line 352 | Line 584 | char  *oldfile;
584                                  /* discard header */
585          getheader(fp, NULL);
586                                  /* get picture size */
587 <        if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) {
587 >        if (!fscnresolu(&x, &y, fp)) {
588                  sprintf(errmsg, "bad recover file \"%s\"", oldfile);
589                  error(WARNING, errmsg);
590                  fclose(fp);
591                  return(0);
592          }
593  
594 <        if (x != hresolu || y != vresolu) {
594 >        if (x != hres || y != vres) {
595                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
596                                  oldfile);
597                  error(USER, errmsg);
598          }
599  
600 <        scanline = (COLR *)malloc(hresolu*sizeof(COLR));
600 >        scanline = (COLR *)malloc(hres*sizeof(COLR));
601          if (scanline == NULL)
602                  error(SYSTEM, "out of memory in salvage");
603 <        for (y = 0; y < vresolu; y++) {
604 <                if (freadcolrs(scanline, hresolu, fp) < 0)
603 >        for (y = 0; y < vres; y++) {
604 >                if (freadcolrs(scanline, hres, fp) < 0)
605                          break;
606 <                if (fwritecolrs(scanline, hresolu, stdout) < 0)
606 >                if (fwritecolrs(scanline, hres, stdout) < 0)
607                          goto writerr;
608          }
609          if (fflush(stdout) == EOF)
# Line 381 | Line 613 | char  *oldfile;
613          unlink(oldfile);
614          return(y);
615   writerr:
616 <        error(SYSTEM, "write error in salvage");
616 >        sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
617 >        error(SYSTEM, errmsg);
618 > }
619 >
620 >
621 > int
622 > pixnumber(x, y, xres, yres)             /* compute pixel index (brushed) */
623 > register int  x, y;
624 > int  xres, yres;
625 > {
626 >        x -= y;
627 >        while (x < 0)
628 >                x += xres;
629 >        return((((x>>2)*yres + y) << 2) + (x & 3));
630   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines