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.44 by greg, Thu Nov 15 18:32:34 2007 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 98 | Line 101 | struct rtproc {
101                                          /* rtrace command and defaults */
102   char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
103                                  "-dj", ".5", "-dr", "3",
104 <                                "-ab", "1", "-ad", "128", };
104 >                                "-ab", "1", "-ad", "350", };
105 >
106   int  rtargc = 9;
107                                          /* overriding rtrace options */
108 < char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
108 > char            *myrtopts[] = { "-h-", "-x", "1", "-y", "0",
109                                  "-dt", "0", "-as", "0", "-aa", "0", NULL };
110  
111 + #define RTCOEFF         "-o~~TmWdp"     /* compute coefficients only */
112 + #define RTCONTRIB       "-o~~TmVdp"     /* compute ray contributions */
113 +
114   struct rtproc   rt0;                    /* head of rtrace process list */
115  
116   struct rtproc   *rt_unproc = NULL;      /* unprocessed ray trees */
117  
118 < char    persistfn[] = "pfXXXXXX";       /* persist file name */
118 > #define PERSIST_NONE    0               /* no persist file */
119 > #define PERSIST_SINGLE  1               /* user set -P persist */
120 > #define PERSIST_PARALL  2               /* user set -PP persist */
121 > #define PERSIST_OURS    3               /* -PP persist belongs to us */
122 > int     persist_state = PERSIST_NONE;   /* persist file state */
123 > char    persistfn[] = "pfXXXXXX";       /* our persist file name, if set */
124  
125   int             gargc;                  /* global argc */
126   char            **gargv;                /* global argv */
# Line 120 | Line 132 | int            inpfmt = 'a';           /* input format */
132   int             outfmt = 'a';           /* output format */
133  
134   int             header = 1;             /* output header? */
135 + int             force_open = 0;         /* truncate existing output? */
136   int             xres = 0;               /* horiz. output resolution */
137   int             yres = 0;               /* vert. output resolution */
138  
# Line 204 | Line 217 | fmterr:
217   int
218   main(int argc, char *argv[])
219   {
220 +        int     contrib = 0;
221          int     nprocs = 1;
222          int     recover = 0;
223          char    *curout = NULL;
# Line 214 | Line 228 | main(int argc, char *argv[])
228                                  /* need at least one argument */
229          if (argc < 2) {
230                  fprintf(stderr,
231 < "Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
231 > "Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
232                          argv[0]);
233                  exit(1);
234          }
# Line 236 | Line 250 | main(int argc, char *argv[])
250                  }
251                  if (argv[i][0] == '-')
252                          switch (argv[i][1]) {
239                        case 'r':               /* recover output */
240                                if (argv[i][2]) break;
241                                recover++;
242                                continue;
253                          case 'n':               /* number of processes */
254                                  if (argv[i][2] || i >= argc-2) break;
255                                  nprocs = atoi(argv[++i]);
256                                  if (nprocs <= 0)
257                                          error(USER, "illegal number of processes");
258                                  continue;
259 +                        case 'V':               /* output contributions */
260 +                                switch (argv[i][2]) {
261 +                                case '\0':
262 +                                        contrib = !contrib;
263 +                                        continue;
264 +                                case '+': case '1':
265 +                                case 'T': case 't':
266 +                                case 'Y': case 'y':
267 +                                        contrib = 1;
268 +                                        continue;
269 +                                case '-': case '0':
270 +                                case 'F': case 'f':
271 +                                case 'N': case 'n':
272 +                                        contrib = 0;
273 +                                        continue;
274 +                                }
275 +                                break;
276 +                        case 'r':               /* recover output */
277 +                                if (argv[i][2]) break;
278 +                                recover++;
279 +                                continue;
280                          case 'h':               /* output header? */
281                                  switch (argv[i][2]) {
282                                  case '\0':
# Line 263 | Line 294 | main(int argc, char *argv[])
294                                          continue;
295                                  }
296                                  break;
297 <                        case 'f':               /* file or i/o format */
297 >                        case 'f':               /* file or force or format */
298                                  if (!argv[i][2]) {
299                                          char    *fpath;
300                                          if (i >= argc-2) break;
# Line 278 | Line 309 | main(int argc, char *argv[])
309                                          fcompile(fpath);
310                                          continue;
311                                  }
312 +                                if (argv[i][2] == 'o') {
313 +                                        force_open++;
314 +                                        continue;
315 +                                }
316                                  setformat(argv[i]+2);
317                                  continue;
318                          case 'e':               /* expression */
# Line 317 | Line 352 | main(int argc, char *argv[])
352                                  rtargv[rtargc++] = argv[++i];
353                                  addmodfile(argv[i], curout, binval, bincnt);
354                                  continue;
355 +                        case 'P':               /* persist file */
356 +                                if (i >= argc-2) break;
357 +                                persist_state = (argv[i][2] == 'P') ?
358 +                                                PERSIST_PARALL : PERSIST_SINGLE;
359 +                                rtargv[rtargc++] = argv[i];
360 +                                rtargv[rtargc++] = argv[++i];
361 +                                continue;
362                          }
363                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
364          }
# Line 325 | Line 367 | main(int argc, char *argv[])
367                                  /* add "mandatory" rtrace settings */
368          for (j = 0; myrtopts[j] != NULL; j++)
369                  rtargv[rtargc++] = myrtopts[j];
370 +        rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
371                                  /* just asking for defaults? */
372          if (!strcmp(argv[i], "-defaults")) {
373                  char    sxres[16], syres[16];
374                  char    *rtpath;
375                  printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
376 +                printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
377 +                                contrib ? "contributions" : "coefficients");
378                  fflush(stdout);                 /* report OUR options */
379                  rtargv[rtargc++] = header ? "-h+" : "-h-";
380                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
# Line 340 | Line 385 | main(int argc, char *argv[])
385                  rtargv[rtargc++] = "-y";
386                  sprintf(syres, "%d", yres);
387                  rtargv[rtargc++] = syres;
343                rtargv[rtargc++] = "-oTW";
388                  rtargv[rtargc++] = "-defaults";
389                  rtargv[rtargc] = NULL;
390                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
# Line 354 | Line 398 | main(int argc, char *argv[])
398                  exit(1);
399          }
400          if (nprocs > 1) {       /* add persist file if parallel */
401 <                rtargv[rtargc++] = "-PP";
402 <                rtargv[rtargc++] = mktemp(persistfn);
401 >                if (persist_state == PERSIST_SINGLE)
402 >                        error(USER, "use -PP option for multiple processes");
403 >                if (persist_state == PERSIST_NONE) {
404 >                        rtargv[rtargc++] = "-PP";
405 >                        rtargv[rtargc++] = mktemp(persistfn);
406 >                        persist_state = PERSIST_OURS;
407 >                }
408          }
409                                  /* add format string */
410          sprintf(fmt, "-f%cf", inpfmt);
# Line 415 | Line 464 | quit(int status)
464   {
465          int     rtstat;
466  
467 <        if (rt0.next != NULL)           /* terminate persistent rtrace */
467 >        if (persist_state == PERSIST_OURS)  /* terminate persistent rtrace */
468                  killpersist();
469                                          /* clean up rtrace process(es) */
470          rtstat = done_rprocs(&rt0);
# Line 505 | Line 554 | addmodifier(char *modn, char *outf, char *binv, int bi
554   {
555          LUENT   *lep = lu_find(&modconttab, modn);
556          MODCONT *mp;
557 +        int     i;
558          
559          if (lep->data != NULL) {
560                  sprintf(errmsg, "duplicate modifier '%s'", modn);
# Line 517 | Line 567 | addmodifier(char *modn, char *outf, char *binv, int bi
567          mp = (MODCONT *)malloc(sizeof(MODCONT));
568          if (mp == NULL)
569                  error(SYSTEM, "out of memory in addmodifier");
520        lep->data = (char *)mp;
570          mp->outspec = outf;             /* XXX assumes static string */
571          mp->modname = modn;             /* XXX assumes static string */
572          if (binv != NULL)
# Line 530 | Line 579 | addmodifier(char *modn, char *outf, char *binv, int bi
579                  bincnt = 1;
580          else if (bincnt > 1)
581                  mp = growmodifier(mp, bincnt);
582 <        if (bincnt > 0)                 /* allocate output stream */
583 <                getostream(mp->outspec, mp->modname, 0, bincnt);
582 >        lep->data = (char *)mp;
583 >                                        /* allocate output streams */
584 >        for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i-- > 0; )
585 >                getostream(mp->outspec, mp->modname, i, 1);
586          return mp;
587   }
588  
# Line 660 | Line 711 | printresolu(FILE *fout)
711          }
712   }
713  
714 < /* Get output stream pointer (open and write header if new and bincnt==0) */
714 > /* Get output stream pointer (open and write header if new and noopen==0) */
715   STREAMOUT *
716 < getostream(const char *ospec, const char *mname, int bn, int bincnt)
716 > getostream(const char *ospec, const char *mname, int bn, int noopen)
717   {
718          static STREAMOUT        stdos;
719          int                     ofl;
# Line 671 | Line 722 | getostream(const char *ospec, const char *mname, int b
722          STREAMOUT               *sop;
723          
724          if (ospec == NULL) {                    /* use stdout? */
725 <                if (!bincnt && !using_stdout) {
725 >                if (!noopen && !using_stdout) {
726                          stdos.reclen = 0;
727                          if (outfmt != 'a')
728                                  SET_FILE_BINARY(stdout);
# Line 681 | Line 732 | getostream(const char *ospec, const char *mname, int b
732                          using_stdout = 1;
733                  }
734                  stdos.ofp = stdout;
735 <                stdos.reclen += bincnt;
735 >                stdos.reclen += noopen;
736                  return &stdos;
737          }
738          ofl = ofname(oname, ospec, mname, bn);  /* get output name */
# Line 694 | Line 745 | getostream(const char *ospec, const char *mname, int b
745                  lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
746          sop = (STREAMOUT *)lep->data;
747          if (sop == NULL) {                      /* allocate stream */
697                if (ofl & OF_BIN && bincnt > 1) /* don't overcount bins */
698                        bincnt = 1;
748                  sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
749                  if (sop == NULL)
750                          error(SYSTEM, "out of memory in getostream");
751 <                sop->reclen = oname[0] == '!' ? CNT_PIPE : 0;
752 <                sop->ofp = NULL;                /* open iff bincnt==0 */
751 >                sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN;
752 >                sop->ofp = NULL;                /* open iff noopen==0 */
753                  lep->data = (char *)sop;
754          }
755 <        if (!bincnt && sop->ofp == NULL) {      /* open output stream */
756 <                int             i;
755 >        if (!noopen && sop->ofp == NULL) {      /* open output stream */
756 >                long            i;
757                  if (oname[0] == '!')            /* output to command */
758                          sop->ofp = popen(oname+1, "w");
759 <                else
759 >                else if (!force_open && access(oname, F_OK) == 0)
760 >                        errno = EEXIST;         /* file exists */
761 >                else                            /* else open it */
762                          sop->ofp = fopen(oname, "w");
763                  if (sop->ofp == NULL) {
764                          sprintf(errmsg, "cannot open '%s' for writing", oname);
# Line 718 | Line 769 | getostream(const char *ospec, const char *mname, int b
769                  if (header) {
770                          char    info[512];
771                          char    *cp = info;
772 <                        if (ofl & OF_MODIFIER) {
772 >                        if (ofl & OF_MODIFIER || sop->reclen == 1) {
773                                  sprintf(cp, "MODIFIER=%s\n", mname);
774                                  while (*cp) ++cp;
775                          }
# Line 731 | Line 782 | getostream(const char *ospec, const char *mname, int b
782                  }
783                  printresolu(sop->ofp);
784                                                  /* play catch-up */
785 <                for (i = 0; i < lastdone; i++) {
785 >                for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone;
786 >                                                                i--; ) {
787                          static const DCOLOR     nocontrib = BLKCOLOR;
788                          put_contrib(nocontrib, sop->ofp);
789                          if (outfmt == 'a')
# Line 740 | Line 792 | getostream(const char *ospec, const char *mname, int b
792                  if (xres > 0)
793                          fflush(sop->ofp);
794          }
795 <        if (sop->reclen != CNT_PIPE)            /* add bincnt to count */
796 <                sop->reclen += bincnt;
795 >        if (sop->reclen != CNT_PIPE)            /* add to length if noopen */
796 >                sop->reclen += noopen;
797          return sop;                             /* return open stream */
798   }
799  
# Line 749 | Line 801 | getostream(const char *ospec, const char *mname, int b
801   int
802   getinp(char *buf, FILE *fp)
803   {
804 +        double  dv[3];
805 +        float   fv[3];
806          char    *cp;
807          int     i;
808  
# Line 757 | Line 811 | getinp(char *buf, FILE *fp)
811                  cp = buf;               /* make sure we get 6 floats */
812                  for (i = 0; i < 6; i++) {
813                          if (fgetword(cp, buf+127-cp, fp) == NULL)
814 <                                return 0;
814 >                                return -1;
815 >                        if (i >= 3)
816 >                                dv[i-3] = atof(cp);
817                          if ((cp = fskip(cp)) == NULL || *cp)
818 <                                return 0;
818 >                                return -1;
819                          *cp++ = ' ';
820                  }
821                  getc(fp);               /* get/put eol */
822                  *cp-- = '\0'; *cp = '\n';
823 +                if (DOT(dv,dv) <= FTINY*FTINY)
824 +                        return 0;       /* dummy ray */
825                  return strlen(buf);
826          case 'f':
827                  if (fread(buf, sizeof(float), 6, fp) < 6)
828 <                        return 0;
828 >                        return -1;
829 >                memcpy(fv, buf+3*sizeof(float), 3*sizeof(float));
830 >                if (DOT(fv,fv) <= FTINY*FTINY)
831 >                        return 0;       /* dummy ray */
832                  return sizeof(float)*6;
833          case 'd':
834                  if (fread(buf, sizeof(double), 6, fp) < 6)
835 <                        return 0;
835 >                        return -1;
836 >                memcpy(dv, buf+3*sizeof(double), 3*sizeof(double));
837 >                if (DOT(dv,dv) <= FTINY*FTINY)
838 >                        return 0;       /* dummy ray */
839                  return sizeof(double)*6;
840          }
841          error(INTERNAL, "botched input format");
842 <        return 0;       /* pro forma return */
842 >        return -1;      /* pro forma return */
843   }
844  
845   static float    rparams[9];             /* traced ray parameters */
# Line 935 | Line 999 | process_queue(void)
999                          if (!n || !(isalpha(*cp) | (*cp == '_')))
1000                                  error(USER, "bad modifier name from rtrace");
1001                                          /* get modifier name */
1002 <                        while (n > 0 && *cp != '\t') {
1002 >                        while (n > 1 && *cp != '\t') {
1003 >                                if (mnp - modname >= sizeof(modname)-2)
1004 >                                        error(INTERNAL, "modifier name too long");
1005                                  *mnp++ = *cp++; n--;
1006                          }
1007                          *mnp = '\0';
# Line 949 | Line 1015 | process_queue(void)
1015                  }
1016                  done_contrib();         /* sum up contributions & output */
1017                  lastdone = rtp->raynum;
1018 <                free(rtp->buf);         /* free up buffer space */
1018 >                if (rtp->buf != NULL)   /* free up buffer space */
1019 >                        free(rtp->buf);
1020                  rt_unproc = rtp->next;
1021                  free(rtp);              /* done with this ray tree */
1022          }
# Line 1037 | Line 1104 | trace_contribs(FILE *fin)
1104          int             iblen;
1105          struct rtproc   *rtp;
1106                                                  /* loop over input */
1107 <        while ((iblen = getinp(inpbuf, fin)) > 0) {
1108 <                if (lastray+1 < lastray ||      /* need reset? */
1109 <                                queue_length() > 5*nrtprocs()) {
1107 >        while ((iblen = getinp(inpbuf, fin)) >= 0) {
1108 >                if (!iblen ||                   /* need reset? */
1109 >                                queue_length() > 10*nrtprocs() ||
1110 >                                lastray+1 < lastray) {
1111                          while (wait_rproc() != NULL)
1112                                  process_queue();
1113 <                        lastdone = lastray = 0;
1113 >                        if (lastray+1 < lastray)
1114 >                                lastdone = lastray = 0;
1115                  }
1116                  rtp = get_rproc();              /* get avail. rtrace process */
1117 <                rtp->raynum = ++lastray;        /* assign ray to it */
1118 <                writebuf(rtp->pd.w, inpbuf, iblen);
1119 <                if (raysleft && !--raysleft)
1120 <                        break;
1117 >                rtp->raynum = ++lastray;        /* assign ray */
1118 >                if (iblen) {                    /* trace ray if valid */
1119 >                        writebuf(rtp->pd.w, inpbuf, iblen);
1120 >                } else {                        /* else bypass dummy ray */
1121 >                        queue_raytree(rtp);     /* empty tree */
1122 >                        if ((yres <= 0) | (waitflush > 1))
1123 >                                waitflush = 1;  /* flush after this */
1124 >                }
1125                  process_queue();                /* catch up with results */
1126 +                if (raysleft && !--raysleft)
1127 +                        break;                  /* preemptive EOI */
1128          }
1129          while (wait_rproc() != NULL)            /* process outstanding rays */
1130                  process_queue();
# Line 1071 | Line 1146 | myseeko(const LUENT *e, void *p)
1146                  sprintf(errmsg, "seek error on file '%s'", e->key);
1147                  error(SYSTEM, errmsg);
1148          }
1149 +        return 0;
1150   }
1151  
1152   /* recover output if possible */
# Line 1125 | Line 1201 | recover_output(FILE *fin)
1201                          if (oent->data != NULL) {
1202                                  sout = *(STREAMOUT *)oent->data;
1203                          } else {
1204 <                                sout.reclen = 0;
1204 >                                sout.reclen = CNT_UNKNOWN;
1205                                  sout.ofp = NULL;
1206                          }
1207                          if (sout.ofp != NULL) { /* already open? */
# Line 1148 | Line 1224 | recover_output(FILE *fin)
1224                                  fclose(sout.ofp);
1225                                  break;
1226                          }
1227 <                        if (!sout.reclen) {     /* unspecified record length */
1227 >                        if (sout.reclen == CNT_UNKNOWN) {
1228                                  if (!(ofl & OF_BIN)) {
1229                                          sprintf(errmsg,
1230 <                                                "guessing 1 value per record in '%s'",
1230 >                                                "need -bn to recover file '%s'",
1231                                                          oname);
1232 <                                        error(WARNING, errmsg);
1232 >                                        error(USER, errmsg);
1233                                  }
1234                                  recsiz = outvsiz;
1235                          } else
# Line 1207 | Line 1283 | recover_output(FILE *fin)
1283          lu_doall(&ofiletab, myseeko, &nvals);
1284                                                  /* skip repeated input */
1285          for (nvals = 0; nvals < lastout; nvals++)
1286 <                if (getinp(oname, fin) <= 0)
1286 >                if (getinp(oname, fin) < 0)
1287                          error(USER, "unexpected EOF on input");
1288          lastray = lastdone = (unsigned long)lastout;
1289          if (raysleft)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines