ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
(Generate patch)

Comparing ray/src/util/rpiece.c (file contents):
Revision 2.29 by greg, Mon Feb 28 09:22:32 1994 UTC vs.
Revision 2.48 by greg, Tue Mar 11 02:21:47 2008 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1993 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Generate sections of a picture.
6   */
7  
8 +
9 + #include <stdio.h>
10 + #include <signal.h>
11 + #include <sys/types.h>
12 + #ifndef NON_POSIX /* XXX need abstraction for process management */
13 + #include <sys/wait.h>
14 + #endif
15 +
16 + #include "platform.h"
17   #include "standard.h"
18 + #include "color.h"
19 + #include "view.h"
20 + #include "rtprocess.h"
21  
22   #ifndef F_SETLKW
23  
24 < main(argc, argv)
25 < int argc;
26 < char *argv[];
24 > int
25 > main(
26 >        int argc,
27 >        char *argv[]
28 > )
29   {
30          fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]);
31          exit(1);
# Line 22 | Line 33 | char *argv[];
33  
34   #else
35  
25 #include <signal.h>
26 #include "color.h"
27 #include "view.h"
28 #include "resolu.h"
29
36   #ifndef NFS
37   #define  NFS                    1
38   #endif
# Line 44 | Line 50 | char *argv[];
50   #define unguard()       sigrelse(SIGALRM)
51   #endif
52   #ifndef guard_io
53 < #define guard_io()      0
54 < #define unguard()       0
53 > #define guard_io()      
54 > #define unguard()      
55   #endif
56 +
57 + extern char  *strerror();
58 +
59                                  /* rpict command */
60   char  *rpargv[128] = {"rpict", "-S", "1"};
61   int  rpargc = 3;
53 int  rpd[3];
62   FILE  *torp, *fromrp;
63   COLR  *pbuf;
64                                  /* our view parameters */
# Line 69 | Line 77 | int  nforked = 0;
77  
78   char  *progname;
79   int  verbose = 0;
80 + unsigned  timelim = 0;
81   int  rvrlim = -1;
82  
74 extern long  lseek(), ftell();
75
83   int  gotalrm = 0;
84 < int  onalrm() { gotalrm++; }
84 > void  onalrm(int i) { gotalrm++; }
85  
86 + static void dolock(int  fd, int  ltyp);
87 + static void init(int  ac, char  **av);
88 + static int nextpiece(int        *xp, int        *yp);
89 + static int rvrpiece(int *xp, int        *yp);
90 + static int cleanup(int  rstat);
91 + static void rpiece(void);
92 + static int putpiece(int xpos, int       ypos);
93 + static void filerr(char  *t);
94  
95 < main(argc, argv)
96 < int  argc;
97 < char  *argv[];
95 >
96 > int
97 > main(
98 >        int  argc,
99 >        char  *argv[]
100 > )
101   {
102          register int  i, rval;
103          
104          progname = argv[0];
105          for (i = 1; i < argc; i++) {
106                                                  /* expand arguments */
107 <                while (rval = expandarg(&argc, &argv, i))
108 <                        if (rval < 0) {
109 <                                fprintf(stderr, "%s: cannot expand '%s'",
110 <                                                argv[0], argv[i]);
111 <                                exit(1);
112 <                        }
107 >                while ((rval = expandarg(&argc, &argv, i)) > 0)
108 >                        ;
109 >                if (rval < 0) {
110 >                        fprintf(stderr, "%s: cannot expand '%s'",
111 >                                        argv[0], argv[i]);
112 >                        exit(1);
113 >                }
114                  if (argv[i][0] == '-')
115                          switch (argv[i][1]) {
116                          case 'v':
# Line 118 | Line 137 | char  *argv[];
137                          case 'p':               /* pixel aspect ratio? */
138                                  if (argv[i][2] != 'a' || argv[i][3])
139                                          break;
140 <                                pixaspect = atof(argv[i+1]);
140 >                                pixaspect = atof(argv[++i]);
141                                  continue;
142 +                        case 'T':               /* time limit (hours) */
143 +                                if (argv[i][2])
144 +                                        break;
145 +                                timelim = atof(argv[++i])*3600. + .5;
146 +                                break;
147                          case 'x':               /* overall x resolution */
148                                  if (argv[i][2])
149                                          break;
# Line 182 | Line 206 | char  *argv[];
206   }
207  
208  
209 < dolock(fd, ltyp)                /* lock or unlock a file */
210 < int  fd;
211 < int  ltyp;
209 > static void
210 > dolock(         /* lock or unlock a file */
211 >        int  fd,
212 >        int  ltyp
213 > )
214   {
215          static struct flock  fls;       /* static so initialized to zeroes */
190        extern char  *sys_errlist[];
216  
217          fls.l_type = ltyp;
218          if (fcntl(fd, F_SETLKW, &fls) < 0) {
219                  fprintf(stderr, "%s: cannot lock/unlock file: %s\n",
220 <                                progname, sys_errlist[errno]);
220 >                                progname, strerror(errno));
221                  exit(1);
222          }
223   }
224  
225  
226 < init(ac, av)                    /* set up output file and start rpict */
227 < int  ac;
228 < char  **av;
226 > static void
227 > init(                   /* set up output file and start rpict */
228 >        int  ac,
229 >        char  **av
230 > )
231   {
232          static char  hrbuf[16], vrbuf[16];
233          extern char  VersionID[];
234          char  *err;
235          FILE  *fp;
236          int  hr, vr;
237 +        SUBPROC  rpd; /* since we don't close_process(), this can be local */
238                                          /* set up view */
239          if ((err = setview(&ourview)) != NULL) {
240                  fprintf(stderr, "%s: %s\n", progname, err);
# Line 220 | Line 248 | char  **av;
248                                          /* compute piece size */
249          hres /= hmult;
250          vres /= vmult;
251 +        if (hres <= 0 || vres <= 0) {
252 +                fprintf(stderr, "%s: illegal resolution/subdivision\n", progname);
253 +                exit(1);
254 +        }
255          normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres);
256          sprintf(hrbuf, "%d", hres);
257          rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf;
# Line 265 | Line 297 | char  **av;
297                  goto filerr;
298          dolock(outfd, F_UNLCK);
299                                          /* start rpict process */
300 <        if (open_process(rpd, rpargv) <= 0) {
300 >        if (open_process(&rpd, rpargv) <= 0) {
301                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
302                  exit(1);
303          }
304 <        if ((fromrp = fdopen(rpd[0], "r")) == NULL ||
305 <                        (torp = fdopen(rpd[1], "w")) == NULL) {
304 >        if ((fromrp = fdopen(rpd.r, "r")) == NULL ||
305 >                        (torp = fdopen(rpd.w, "w")) == NULL) {
306                  fprintf(stderr, "%s: cannot open stream to %s\n",
307                                  progname, rpargv[0]);
308                  exit(1);
# Line 280 | Line 312 | char  **av;
312                  exit(1);
313          }
314          signal(SIGALRM, onalrm);
315 +        if (timelim)
316 +                alarm(timelim);
317          return;
318   filerr:
319          fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
# Line 287 | Line 321 | filerr:
321   }
322  
323  
324 < int
325 < nextpiece(xp, yp)               /* get next piece assignment */
326 < int  *xp, *yp;
324 > static int
325 > nextpiece(              /* get next piece assignment */
326 >        int     *xp,
327 >        int     *yp
328 > )
329   {
330          if (gotalrm)                    /* someone wants us to quit */
331                  return(0);
# Line 329 | Line 365 | int  *xp, *yp;
365   }
366  
367  
368 < int
369 < rvrpiece(xp, yp)                /* check for recoverable pieces */
370 < register int  *xp, *yp;
368 > static int
369 > rvrpiece(               /* check for recoverable pieces */
370 >        register int    *xp,
371 >        register int    *yp
372 > )
373   {
374          static char  *pdone = NULL;     /* which pieces are done */
375          static long  readpos = -1;      /* how far we've read */
# Line 345 | Line 383 | register int  *xp, *yp;
383                  return(0);              /* only check if asked */
384          if (pdone == NULL)              /* first call */
385                  pdone = calloc(hmult*vmult, sizeof(char));
386 +        if (pdone == NULL) {
387 +                fprintf(stderr, "%s: out of memory\n", progname);
388 +                exit(1);
389 +        }
390          if (readpos != -1)              /* mark what's been done */
391                  fseek(syncfp, readpos, 0);
392          while (fscanf(syncfp, "%d %d", xp, yp) == 2)
# Line 369 | Line 411 | register int  *xp, *yp;
411   }
412  
413  
414 < int
415 < cleanup(rstat)                  /* close rpict process and clean up */
416 < int  rstat;
414 > static int
415 > cleanup(                        /* close rpict process and clean up */
416 >        int  rstat
417 > )
418   {
419          int  status;
420  
# Line 385 | Line 428 | int  rstat;
428   }
429  
430  
431 < rpiece()                        /* render picture piece by piece */
431 > static void
432 > rpiece(void)                    /* render picture piece by piece */
433   {
434          VIEW  pview;
435          int  xorg, yorg;
436                                          /* compute view parameters */
437 <        copystruct(&pview, &ourview);
437 >        pview = ourview;
438          switch (ourview.type) {
439          case VT_PER:
440 <                pview.horiz = 2.*180./PI*atan(
441 <                                tan(PI/180./2.*ourview.horiz)/hmult );
442 <                pview.vert = 2.*180./PI*atan(
443 <                                tan(PI/180./2.*ourview.vert)/vmult );
440 >                pview.horiz = (2.*180./PI)*atan(
441 >                                tan((PI/180./2.)*ourview.horiz)/hmult );
442 >                pview.vert = (2.*180./PI)*atan(
443 >                                tan((PI/180./2.)*ourview.vert)/vmult );
444                  break;
445          case VT_PAR:
446          case VT_ANG:
447                  pview.horiz = ourview.horiz / hmult;
448                  pview.vert = ourview.vert / vmult;
449                  break;
450 +        case VT_CYL:
451 +                pview.horiz = ourview.horiz / hmult;
452 +                pview.vert = (2.*180./PI)*atan(
453 +                                tan((PI/180./2.)*ourview.vert)/vmult );
454 +                break;
455          case VT_HEM:
456 <                pview.horiz = 2.*180./PI*asin(
457 <                                sin(PI/180./2.*ourview.horiz)/hmult );
458 <                pview.vert = 2.*180./PI*asin(
459 <                                sin(PI/180./2.*ourview.vert)/vmult );
456 >                pview.horiz = (2.*180./PI)*asin(
457 >                                sin((PI/180./2.)*ourview.horiz)/hmult );
458 >                pview.vert = (2.*180./PI)*asin(
459 >                                sin((PI/180./2.)*ourview.vert)/vmult );
460                  break;
461 +        case VT_PLS:
462 +                pview.horiz = sin((PI/180./2.)*ourview.horiz) /
463 +                                (1.0 + cos((PI/180./2.)*ourview.horiz)) / hmult;
464 +                pview.horiz *= pview.horiz;
465 +                pview.horiz = (2.*180./PI)*acos((1. - pview.horiz) /
466 +                                                (1. + pview.horiz));
467 +                pview.vert = sin((PI/180./2.)*ourview.vert) /
468 +                                (1.0 + cos((PI/180./2.)*ourview.vert)) / vmult;
469 +                pview.vert *= pview.vert;
470 +                pview.vert = (2.*180./PI)*acos((1. - pview.vert) /
471 +                                                (1. + pview.vert));
472 +                break;
473          default:
474                  fprintf(stderr, "%s: unknown view type '-vt%c'\n",
475                                  progname, ourview.type);
# Line 416 | Line 477 | rpiece()                       /* render picture piece by piece */
477          }
478                                          /* render each piece */
479          while (nextpiece(&xorg, &yorg)) {
480 <                pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1);
481 <                pview.voff = ourview.voff + yorg - 0.5*(vmult-1);
480 >                pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1);
481 >                pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1);
482                  fputs(VIEWSTR, torp);
483                  fprintview(&pview, torp);
484                  putc('\n', torp);
# Line 427 | Line 488 | rpiece()                       /* render picture piece by piece */
488   }
489  
490  
491 < int
492 < putpiece(xpos, ypos)            /* get next piece from rpict */
493 < int  xpos, ypos;
491 > static int
492 > putpiece(               /* get next piece from rpict */
493 > int     xpos,
494 > int     ypos
495 > )
496   {
497          struct flock  fls;
498          int  pid, status;
499          int  hr, vr;
500          register int  y;
501                                  /* check bounds */
502 <        if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) {
502 >        if ((xpos < 0) | (ypos < 0) | (xpos >= hmult) | (ypos >= vmult)) {
503                  fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
504                                  progname, xpos, ypos);
505                  exit(cleanup(1));
# Line 444 | Line 507 | int  xpos, ypos;
507                                  /* check header from rpict */
508          guard_io();
509          getheader(fromrp, NULL, NULL);
510 <        if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) {
510 >        if (!fscnresolu(&hr, &vr, fromrp) || (hr != hres) | (vr != vres)) {
511                  fprintf(stderr, "%s: resolution mismatch from %s\n",
512                                  progname, rpargv[0]);
513                  exit(cleanup(1));
# Line 489 | Line 552 | int  xpos, ypos;
552                  filerr("lock");
553   #endif
554                                  /* write new piece to file */
555 <        if (lseek(outfd, fls.l_start, 0) == -1)
555 >        if (lseek(outfd, (off_t)fls.l_start, SEEK_SET) < 0)
556                  filerr("seek");
557          if (hmult == 1) {
558                  if (writebuf(outfd, (char *)pbuf,
# Line 501 | Line 564 | int  xpos, ypos;
564                                          hr*sizeof(COLR)) != hr*sizeof(COLR))
565                                  filerr("write");
566                          if (y < vr-1 && lseek(outfd,
567 <                                        (long)(hmult-1)*hr*sizeof(COLR),
568 <                                        1) == -1)
567 >                                        (off_t)(hmult-1)*hr*sizeof(COLR),
568 >                                        SEEK_CUR) < 0)
569                                  filerr("seek");
570                  }
571   #if NFS
# Line 510 | Line 573 | int  xpos, ypos;
573          if (fcntl(outfd, F_SETLKW, &fls) < 0)
574                  filerr("lock");
575   #endif
576 +        if (verbose) {                          /* notify caller */
577 +                printf("%d %d done\n", xpos, ypos);
578 +                fflush(stdout);
579 +        }
580          if (syncfp != NULL) {                   /* record what's been done */
581                  sflock(F_WRLCK);
582                  fseek(syncfp, 0L, 2);           /* append index */
# Line 518 | Line 585 | int  xpos, ypos;
585                                  /*** Unlock not necessary, since
586                  sflock(F_UNLCK);        _exit() or nextpiece() is next ***/
587          }
521        if (verbose) {                          /* notify caller */
522                printf("%d %d done\n", xpos, ypos);
523                fflush(stdout);
524        }
588          if (pid == -1)          /* didn't fork or fork failed */
589                  return(0);
590          _exit(0);               /* else exit child process (releasing locks) */
591   }
592  
593  
594 < filerr(t)                       /* report file error and exit */
595 < char  *t;
594 > static void
595 > filerr(                 /* report file error and exit */
596 >        char  *t
597 > )
598   {
534        extern char  *sys_errlist[];
535
599          fprintf(stderr, "%s: %s error on file \"%s\": %s\n",
600 <                        progname, t, outfile, sys_errlist[errno]);
600 >                        progname, t, outfile, strerror(errno));
601          _exit(1);
602   }
603  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines