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

Comparing ray/src/util/rtcontrib.c (file contents):
Revision 1.31 by greg, Thu Oct 6 05:49:44 2005 UTC vs.
Revision 1.39 by greg, Fri Oct 21 01:12:59 2005 UTC

# Line 63 | Line 63 | static void
63   closestream(void *p)
64   {
65          STREAMOUT       *sop = (STREAMOUT *)p;
66 +        int             status;
67          if (sop->reclen == CNT_PIPE)
68 <                pclose(sop->ofp);
68 >                status = pclose(sop->ofp);
69          else
70 <                fclose(sop->ofp);
70 >                status = fclose(sop->ofp);
71 >        if (status)
72 >                error(SYSTEM, "error closing output stream");
73          free(p);
74   }
75  
# Line 75 | Line 78 | LUTAB  ofiletab = LU_SINIT(free,closestream);  /* output
78   #define OF_MODIFIER     01
79   #define OF_BIN          02
80  
81 < STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int bincnt);
81 > STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int noopen);
82   int ofname(char *oname, const char *ospec, const char *mname, int bn);
83   void printheader(FILE *fout, const char *info);
84   void printresolu(FILE *fout);
# Line 120 | Line 123 | int            inpfmt = 'a';           /* input format */
123   int             outfmt = 'a';           /* output format */
124  
125   int             header = 1;             /* output header? */
126 + int             force_open = 0;         /* truncate existing output? */
127   int             xres = 0;               /* horiz. output resolution */
128   int             yres = 0;               /* vert. output resolution */
129  
# Line 263 | Line 267 | main(int argc, char *argv[])
267                                          continue;
268                                  }
269                                  break;
270 <                        case 'f':               /* file or i/o format */
270 >                        case 'f':               /* file or force or format */
271                                  if (!argv[i][2]) {
272                                          char    *fpath;
273                                          if (i >= argc-2) break;
# Line 278 | Line 282 | main(int argc, char *argv[])
282                                          fcompile(fpath);
283                                          continue;
284                                  }
285 +                                if (argv[i][2] == 'o') {
286 +                                        force_open++;
287 +                                        continue;
288 +                                }
289                                  setformat(argv[i]+2);
290                                  continue;
291                          case 'e':               /* expression */
# Line 317 | Line 325 | main(int argc, char *argv[])
325                                  rtargv[rtargc++] = argv[++i];
326                                  addmodfile(argv[i], curout, binval, bincnt);
327                                  continue;
328 +                        case 'P':               /* persist file */
329 +                                error(USER, "persist file is automatic");
330 +                                break;
331                          }
332                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
333          }
# Line 505 | Line 516 | addmodifier(char *modn, char *outf, char *binv, int bi
516   {
517          LUENT   *lep = lu_find(&modconttab, modn);
518          MODCONT *mp;
519 +        int     i;
520          
521          if (lep->data != NULL) {
522                  sprintf(errmsg, "duplicate modifier '%s'", modn);
# Line 517 | Line 529 | addmodifier(char *modn, char *outf, char *binv, int bi
529          mp = (MODCONT *)malloc(sizeof(MODCONT));
530          if (mp == NULL)
531                  error(SYSTEM, "out of memory in addmodifier");
520        lep->data = (char *)mp;
532          mp->outspec = outf;             /* XXX assumes static string */
533          mp->modname = modn;             /* XXX assumes static string */
534          if (binv != NULL)
# Line 530 | Line 541 | addmodifier(char *modn, char *outf, char *binv, int bi
541                  bincnt = 1;
542          else if (bincnt > 1)
543                  mp = growmodifier(mp, bincnt);
544 <        if (bincnt > 0)                 /* allocate output stream */
545 <                getostream(mp->outspec, mp->modname, 0, bincnt);
544 >        lep->data = (char *)mp;
545 >                                        /* allocate output streams */
546 >        for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i--; )
547 >                getostream(mp->outspec, mp->modname, i, 1);
548          return mp;
549   }
550  
# Line 660 | Line 673 | printresolu(FILE *fout)
673          }
674   }
675  
676 < /* Get output stream pointer (open and write header if new and bincnt==0) */
676 > /* Get output stream pointer (open and write header if new and noopen==0) */
677   STREAMOUT *
678 < getostream(const char *ospec, const char *mname, int bn, int bincnt)
678 > getostream(const char *ospec, const char *mname, int bn, int noopen)
679   {
680          static STREAMOUT        stdos;
681          int                     ofl;
# Line 671 | Line 684 | getostream(const char *ospec, const char *mname, int b
684          STREAMOUT               *sop;
685          
686          if (ospec == NULL) {                    /* use stdout? */
687 <                if (!bincnt && !using_stdout) {
687 >                if (!noopen && !using_stdout) {
688                          stdos.reclen = 0;
689                          if (outfmt != 'a')
690                                  SET_FILE_BINARY(stdout);
# Line 681 | Line 694 | getostream(const char *ospec, const char *mname, int b
694                          using_stdout = 1;
695                  }
696                  stdos.ofp = stdout;
697 <                stdos.reclen += bincnt;
697 >                stdos.reclen += noopen;
698                  return &stdos;
699          }
700          ofl = ofname(oname, ospec, mname, bn);  /* get output name */
# Line 694 | Line 707 | getostream(const char *ospec, const char *mname, int b
707                  lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
708          sop = (STREAMOUT *)lep->data;
709          if (sop == NULL) {                      /* allocate stream */
697                if (ofl & OF_BIN && bincnt > 1) /* don't overcount bins */
698                        bincnt = 1;
710                  sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
711                  if (sop == NULL)
712                          error(SYSTEM, "out of memory in getostream");
713 <                sop->reclen = oname[0] == '!' ? CNT_PIPE : 0;
714 <                sop->ofp = NULL;                /* open iff bincnt==0 */
713 >                sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN;
714 >                sop->ofp = NULL;                /* open iff noopen==0 */
715                  lep->data = (char *)sop;
716          }
717 <        if (!bincnt && sop->ofp == NULL) {      /* open output stream */
718 <                int             i;
717 >        if (!noopen && sop->ofp == NULL) {      /* open output stream */
718 >                long            i;
719                  if (oname[0] == '!')            /* output to command */
720                          sop->ofp = popen(oname+1, "w");
721 <                else
721 >                else if (!force_open && access(oname, F_OK) == 0)
722 >                        errno = EEXIST;         /* file exists */
723 >                else                            /* else open it */
724                          sop->ofp = fopen(oname, "w");
725                  if (sop->ofp == NULL) {
726                          sprintf(errmsg, "cannot open '%s' for writing", oname);
# Line 718 | Line 731 | getostream(const char *ospec, const char *mname, int b
731                  if (header) {
732                          char    info[512];
733                          char    *cp = info;
734 <                        if (ofl & OF_MODIFIER) {
734 >                        if (ofl & OF_MODIFIER || sop->reclen == 1) {
735                                  sprintf(cp, "MODIFIER=%s\n", mname);
736                                  while (*cp) ++cp;
737                          }
# Line 731 | Line 744 | getostream(const char *ospec, const char *mname, int b
744                  }
745                  printresolu(sop->ofp);
746                                                  /* play catch-up */
747 <                for (i = 0; i < lastdone; i++) {
747 >                for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone;
748 >                                                                i--; ) {
749                          static const DCOLOR     nocontrib = BLKCOLOR;
750                          put_contrib(nocontrib, sop->ofp);
751                          if (outfmt == 'a')
# Line 740 | Line 754 | getostream(const char *ospec, const char *mname, int b
754                  if (xres > 0)
755                          fflush(sop->ofp);
756          }
757 <        if (sop->reclen != CNT_PIPE)            /* add bincnt to count */
758 <                sop->reclen += bincnt;
757 >        if (sop->reclen != CNT_PIPE)            /* add to length if noopen */
758 >                sop->reclen += noopen;
759          return sop;                             /* return open stream */
760   }
761  
# Line 749 | Line 763 | getostream(const char *ospec, const char *mname, int b
763   int
764   getinp(char *buf, FILE *fp)
765   {
766 +        double  dv[3];
767 +        float   fv[3];
768          char    *cp;
769          int     i;
770  
# Line 757 | Line 773 | getinp(char *buf, FILE *fp)
773                  cp = buf;               /* make sure we get 6 floats */
774                  for (i = 0; i < 6; i++) {
775                          if (fgetword(cp, buf+127-cp, fp) == NULL)
776 <                                return 0;
776 >                                return -1;
777 >                        if (i >= 3)
778 >                                dv[i-3] = atof(cp);
779                          if ((cp = fskip(cp)) == NULL || *cp)
780 <                                return 0;
780 >                                return -1;
781                          *cp++ = ' ';
782                  }
783                  getc(fp);               /* get/put eol */
784                  *cp-- = '\0'; *cp = '\n';
785 +                if (DOT(dv,dv) <= FTINY*FTINY)
786 +                        return 0;       /* dummy ray */
787                  return strlen(buf);
788          case 'f':
789                  if (fread(buf, sizeof(float), 6, fp) < 6)
790 <                        return 0;
790 >                        return -1;
791 >                memcpy(fv, buf+3*sizeof(float), 3*sizeof(float));
792 >                if (DOT(fv,fv) <= FTINY*FTINY)
793 >                        return 0;       /* dummy ray */
794                  return sizeof(float)*6;
795          case 'd':
796                  if (fread(buf, sizeof(double), 6, fp) < 6)
797 <                        return 0;
797 >                        return -1;
798 >                memcpy(dv, buf+3*sizeof(double), 3*sizeof(double));
799 >                if (DOT(dv,dv) <= FTINY*FTINY)
800 >                        return 0;       /* dummy ray */
801                  return sizeof(double)*6;
802          }
803          error(INTERNAL, "botched input format");
804 <        return 0;       /* pro forma return */
804 >        return -1;      /* pro forma return */
805   }
806  
807   static float    rparams[9];             /* traced ray parameters */
# Line 949 | Line 975 | process_queue(void)
975                  }
976                  done_contrib();         /* sum up contributions & output */
977                  lastdone = rtp->raynum;
978 <                free(rtp->buf);         /* free up buffer space */
978 >                if (rtp->buf != NULL)   /* free up buffer space */
979 >                        free(rtp->buf);
980                  rt_unproc = rtp->next;
981                  free(rtp);              /* done with this ray tree */
982          }
# Line 1037 | Line 1064 | trace_contribs(FILE *fin)
1064          int             iblen;
1065          struct rtproc   *rtp;
1066                                                  /* loop over input */
1067 <        while ((iblen = getinp(inpbuf, fin)) > 0) {
1068 <                if (lastray+1 < lastray ||      /* need reset? */
1069 <                                queue_length() > 5*nrtprocs()) {
1067 >        while ((iblen = getinp(inpbuf, fin)) >= 0) {
1068 >                if (!iblen ||                   /* need reset? */
1069 >                                queue_length() > 10*nrtprocs() ||
1070 >                                lastray+1 < lastray) {
1071                          while (wait_rproc() != NULL)
1072                                  process_queue();
1073 <                        lastdone = lastray = 0;
1073 >                        if (lastray+1 < lastray)
1074 >                                lastdone = lastray = 0;
1075                  }
1076                  rtp = get_rproc();              /* get avail. rtrace process */
1077 <                rtp->raynum = ++lastray;        /* assign ray to it */
1078 <                writebuf(rtp->pd.w, inpbuf, iblen);
1079 <                if (raysleft && !--raysleft)
1080 <                        break;
1077 >                rtp->raynum = ++lastray;        /* assign ray */
1078 >                if (iblen) {                    /* trace ray if valid */
1079 >                        writebuf(rtp->pd.w, inpbuf, iblen);
1080 >                } else {                        /* else bypass dummy ray */
1081 >                        queue_raytree(rtp);     /* empty tree */
1082 >                        if ((yres <= 0) | (waitflush > 1))
1083 >                                waitflush = 1;  /* flush after this */
1084 >                }
1085                  process_queue();                /* catch up with results */
1086 +                if (raysleft && !--raysleft)
1087 +                        break;                  /* preemptive EOI */
1088          }
1089          while (wait_rproc() != NULL)            /* process outstanding rays */
1090                  process_queue();
# Line 1125 | Line 1160 | recover_output(FILE *fin)
1160                          if (oent->data != NULL) {
1161                                  sout = *(STREAMOUT *)oent->data;
1162                          } else {
1163 <                                sout.reclen = 0;
1163 >                                sout.reclen = CNT_UNKNOWN;
1164                                  sout.ofp = NULL;
1165                          }
1166                          if (sout.ofp != NULL) { /* already open? */
# Line 1148 | Line 1183 | recover_output(FILE *fin)
1183                                  fclose(sout.ofp);
1184                                  break;
1185                          }
1186 <                        if (!sout.reclen) {     /* unspecified record length */
1186 >                        if (sout.reclen == CNT_UNKNOWN) {
1187                                  if (!(ofl & OF_BIN)) {
1188                                          sprintf(errmsg,
1189 <                                                "guessing 1 value per record in '%s'",
1189 >                                                "need -bn to recover file '%s'",
1190                                                          oname);
1191 <                                        error(WARNING, errmsg);
1191 >                                        error(USER, errmsg);
1192                                  }
1193                                  recsiz = outvsiz;
1194                          } else
# Line 1207 | Line 1242 | recover_output(FILE *fin)
1242          lu_doall(&ofiletab, myseeko, &nvals);
1243                                                  /* skip repeated input */
1244          for (nvals = 0; nvals < lastout; nvals++)
1245 <                if (getinp(oname, fin) <= 0)
1245 >                if (getinp(oname, fin) < 0)
1246                          error(USER, "unexpected EOF on input");
1247          lastray = lastdone = (unsigned long)lastout;
1248          if (raysleft)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines