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.45 by greg, Sat Nov 17 01:13:51 2007 UTC vs.
Revision 1.55 by greg, Wed Dec 9 21:43:35 2009 UTC

# Line 99 | Line 99 | struct rtproc {
99  
100                                          /* rtrace command and defaults */
101   char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
102 <                                "-dj", ".5", "-dr", "3",
102 >                                "-dj", ".9", "-dr", "3",
103                                  "-ab", "1", "-ad", "350", };
104  
105   int  rtargc = 9;
# Line 131 | Line 131 | int            inpfmt = 'a';           /* input format */
131   int             outfmt = 'a';           /* output format */
132  
133   int             header = 1;             /* output header? */
134 int             accumulate = 0;         /* accumulate ray values? */
134   int             force_open = 0;         /* truncate existing output? */
135 + int             recover = 0;            /* recover previous output? */
136 + int             accumulate = 1;         /* input rays per output record */
137   int             xres = 0;               /* horiz. output resolution */
138   int             yres = 0;               /* vert. output resolution */
139  
140 + int             account;                /* current accumulation count */
141   unsigned long   raysleft;               /* number of rays left to trace */
142   long            waitflush;              /* how long until next flush */
143  
# Line 165 | Line 167 | void process_queue(void);
167  
168   void put_contrib(const DCOLOR cnt, FILE *fout);
169   void add_contrib(const char *modn);
170 < void done_contrib(void);
170 > void done_contrib(int navg);
171  
172   /* return number of open rtrace processes */
173   static int
# Line 220 | Line 222 | main(int argc, char *argv[])
222   {
223          int     contrib = 0;
224          int     nprocs = 1;
223        int     recover = 0;
225          char    *curout = NULL;
226          char    *binval = NULL;
227          int     bincnt = 0;
# Line 245 | Line 246 | main(int argc, char *argv[])
246                  while ((j = expandarg(&argc, &argv, i)) > 0)
247                          ;
248                  if (j < 0) {
249 <                        fprintf(stderr, "%s: cannot expand '%s'",
249 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
250                                          argv[0], argv[i]);
251                          exit(1);
252                  }
# Line 274 | Line 275 | main(int argc, char *argv[])
275                                          continue;
276                                  }
277                                  break;
278 <                        case 'c':               /* accumulate ray values */
279 <                                switch (argv[i][2]) {
280 <                                case '\0':
281 <                                        accumulate = !accumulate;
281 <                                        continue;
282 <                                case '+': case '1':
283 <                                case 'T': case 't':
284 <                                case 'Y': case 'y':
285 <                                        accumulate = 1;
286 <                                        continue;
287 <                                case '-': case '0':
288 <                                case 'F': case 'f':
289 <                                case 'N': case 'n':
290 <                                        accumulate = 0;
291 <                                        continue;
292 <                                }
293 <                                break;
278 >                        case 'c':               /* input rays per output */
279 >                                if (argv[i][2] || i >= argc-2) break;
280 >                                accumulate = atoi(argv[++i]);
281 >                                continue;
282                          case 'r':               /* recover output */
283                                  if (argv[i][2]) break;
284 <                                recover++;
284 >                                recover = 1;
285                                  continue;
286                          case 'h':               /* output header? */
287                                  switch (argv[i][2]) {
# Line 328 | Line 316 | main(int argc, char *argv[])
316                                          continue;
317                                  }
318                                  if (argv[i][2] == 'o') {
319 <                                        force_open++;
319 >                                        force_open = 1;
320                                          continue;
321                                  }
322                                  setformat(argv[i]+2);
# Line 352 | Line 340 | main(int argc, char *argv[])
340                          case 'b':               /* bin expression/count */
341                                  if (i >= argc-2) break;
342                                  if (argv[i][2] == 'n') {
343 <                                        bincnt = atoi(argv[++i]);
343 >                                        bincnt = (int)(eval(argv[++i]) + .5);
344                                          continue;
345                                  }
346                                  if (argv[i][2]) break;
# Line 380 | Line 368 | main(int argc, char *argv[])
368                          }
369                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
370          }
371 <        if (accumulate)         /* no output flushing for single record */
371 >        if (accumulate <= 0)    /* no output flushing for single record */
372                  xres = yres = 0;
373                                  /* set global argument list */
374          gargc = argc; gargv = argv;
# Line 392 | Line 380 | main(int argc, char *argv[])
380          if (!strcmp(argv[i], "-defaults")) {
381                  char    sxres[16], syres[16];
382                  char    *rtpath;
383 <                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
383 >                printf("-n %-2d\t\t\t\t# number of processes\n", nprocs);
384 >                printf("-c %-5d\t\t\t# accumulated rays per record\n",
385 >                                accumulate);
386                  printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
387                                  contrib ? "contributions" : "coefficients");
398                printf("-c%c\t\t\t\t# %s\n", accumulate ? '+' : '-',
399                                accumulate ? "accumulate ray values" :
400                                        "one output record per ray");
388                  fflush(stdout);                 /* report OUR options */
389                  rtargv[rtargc++] = header ? "-h+" : "-h-";
390                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
# Line 437 | Line 424 | main(int argc, char *argv[])
424                  error(USER, "missing octree argument");
425          rtargv[rtargc++] = octree = argv[i];
426          rtargv[rtargc] = NULL;
427 <                                /* start rtrace */
427 >                                /* start rtrace & recover if requested */
428          init(nprocs);
429 <        if (recover)            /* perform recovery if requested */
430 <                if ((force_open = accumulate))
431 <                        reload_output();
445 <                else
446 <                        recover_output(stdin);
447 <        trace_contribs(stdin);  /* compute contributions */
429 >                                /* compute contributions */
430 >        trace_contribs(stdin);
431 >                                /* clean up */
432          quit(0);
433   }
434  
# Line 557 | Line 541 | init(int np)
541                          raysleft = yres;
542          } else
543                  raysleft = 0;
544 +        if ((account = accumulate) > 0)
545 +                raysleft *= accumulate;
546          waitflush = xres;
547 +        if (!recover)
548 +                return;
549 +                                        /* recover previous values */
550 +        if (accumulate <= 0)
551 +                reload_output();
552 +        else
553 +                recover_output(stdin);
554   }
555  
556   /* grow modifier to accommodate more bins */
# Line 587 | Line 580 | addmodifier(char *modn, char *outf, char *binv, int bi
580                  error(USER, errmsg);
581          }
582          if (nmods >= MAXMODLIST)
583 <                error(USER, "too many modifiers");
583 >                error(INTERNAL, "too many modifiers");
584          modname[nmods++] = modn;        /* XXX assumes static string */
585          lep->key = modn;                /* XXX assumes static string */
586          mp = (MODCONT *)malloc(sizeof(MODCONT));
# Line 595 | Line 588 | addmodifier(char *modn, char *outf, char *binv, int bi
588                  error(SYSTEM, "out of memory in addmodifier");
589          mp->outspec = outf;             /* XXX assumes static string */
590          mp->modname = modn;             /* XXX assumes static string */
591 <        if (binv != NULL)
592 <                mp->binv = eparse(binv);
593 <        else
594 <                mp->binv = eparse("0");
595 <        mp->nbins = 1;
591 >        if (binv == NULL)
592 >                binv = "0";             /* use single bin if unspecified */
593 >        mp->binv = eparse(binv);
594 >        if (mp->binv->type == NUM) {    /* check value if constant */
595 >                bincnt = (int)(evalue(mp->binv) + 1.5);
596 >                if (bincnt != 1) {
597 >                        sprintf(errmsg, "illegal non-zero constant for bin (%s)",
598 >                                        binv);
599 >                        error(USER, errmsg);
600 >                }
601 >        }
602 >        mp->nbins = 1;                  /* initialize results holder */
603          setcolor(mp->cbin[0], 0., 0., 0.);
604 <        if (mp->binv->type == NUM)      /* assume one bin if constant */
605 <                bincnt = 1;
606 <        else if (bincnt > 1)
604 >        if (bincnt > 1)
605                  mp = growmodifier(mp, bincnt);
606          lep->data = (char *)mp;
607                                          /* allocate output streams */
# Line 666 | Line 664 | ofname(char *oname, const char *ospec, const char *mna
664                                  mnp = cp;
665                                  break;
666                          case 'd':
667 +                        case 'i':
668 +                        case 'o':
669 +                        case 'x':
670 +                        case 'X':
671                                  if (bnp != NULL)
672                                          return -1;
673                                  bnp = cp;
# Line 779 | Line 781 | getostream(const char *ospec, const char *mname, int b
781                  sop->ofp = NULL;                /* open iff noopen==0 */
782                  sop->xr = xres; sop->yr = yres;
783                  lep->data = (char *)sop;
784 <                if (!force_open && access(oname, F_OK) == 0) {
784 >                if (!sop->outpipe & !force_open & !recover &&
785 >                                access(oname, F_OK) == 0) {
786                          errno = EEXIST;         /* file exists */
787                          goto openerr;
788                  }
# Line 808 | Line 811 | getostream(const char *ospec, const char *mname, int b
811                          *cp = '\0';
812                          printheader(sop->ofp, info);
813                  }
814 +                if (accumulate > 0) {           /* global resolution */
815 +                        sop->xr = xres; sop->yr = yres;
816 +                }
817                  printresolu(sop->ofp, sop->xr, sop->yr);
818                                                  /* play catch-up */
819 <                for (i = accumulate ? 0 : lastdone; i--; ) {
819 >                for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) {
820                          int     j = sop->reclen;
821                          if (j <= 0) j = 1;
822                          while (j--)
# Line 935 | Line 941 | put_contrib(const DCOLOR cnt, FILE *fout)
941                  fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
942                  break;
943          case 'f':
944 <                fv[0] = cnt[0];
939 <                fv[1] = cnt[1];
940 <                fv[2] = cnt[2];
944 >                copycolor(fv, cnt);
945                  fwrite(fv, sizeof(float), 3, fout);
946                  break;
947          case 'd':
# Line 954 | Line 958 | put_contrib(const DCOLOR cnt, FILE *fout)
958  
959   /* output ray tallies and clear for next accumulation */
960   void
961 < done_contrib(void)
961 > done_contrib(int navg)
962   {
963 +        double          sf = 1.;
964          int             i, j;
965          MODCONT         *mp;
966          STREAMOUT       *sop;
967 +                                                /* set average scaling */
968 +        if (navg > 1)
969 +                sf = 1. / (double)navg;
970                                                  /* output modifiers in order */
971          for (i = 0; i < nmods; i++) {
972                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
973 +                if (navg > 1)                   /* average scaling */
974 +                        for (j = mp->nbins; j--; )
975 +                                scalecolor(mp->cbin[j], sf);
976                  sop = getostream(mp->outspec, mp->modname, 0,0);
977                  put_contrib(mp->cbin[0], sop->ofp);
978                  if (mp->nbins > 3 &&            /* minor optimization */
# Line 1045 | Line 1056 | process_queue(void)
1056                          cp += sizeof(float)*9; n -= sizeof(float)*9;
1057                          add_contrib(modname);
1058                  }
1059 <                if (!accumulate)
1060 <                        done_contrib(); /* sum up contributions & output */
1059 >                                        /* time to produce record? */
1060 >                if (account > 0 && !--account)
1061 >                        done_contrib(account = accumulate);
1062                  lastdone = rtp->raynum;
1063                  if (rtp->buf != NULL)   /* free up buffer space */
1064                          free(rtp->buf);
# Line 1133 | Line 1145 | get_rproc(void)
1145   void
1146   trace_contribs(FILE *fin)
1147   {
1148 +        static int      ignore_warning_given = 0;
1149          char            inpbuf[128];
1150          int             iblen;
1151          struct rtproc   *rtp;
1152                                                  /* loop over input */
1153          while ((iblen = getinp(inpbuf, fin)) >= 0) {
1154 <                if (!iblen ||                   /* need reset? */
1154 >                if (!iblen && accumulate != 1) {
1155 >                        if (!ignore_warning_given++)
1156 >                                error(WARNING,
1157 >                                "dummy ray(s) ignored during accumulation\n");
1158 >                        continue;
1159 >                }
1160 >                if (!iblen ||                   /* need flush/reset? */
1161                                  queue_length() > 10*nrtprocs() ||
1162                                  lastray+1 < lastray) {
1163                          while (wait_rproc() != NULL)
# Line 1151 | Line 1170 | trace_contribs(FILE *fin)
1170                  if (iblen) {                    /* trace ray if valid */
1171                          writebuf(rtp->pd.w, inpbuf, iblen);
1172                  } else {                        /* else bypass dummy ray */
1173 <                        queue_raytree(rtp);     /* empty tree */
1174 <                        if ((yres <= 0) | (waitflush > 1))
1175 <                                waitflush = 1;  /* flush after this */
1173 >                        queue_raytree(rtp);     /* queue empty ray/record */
1174 >                        if ((yres <= 0) | (xres <= 0))
1175 >                                waitflush = 1;  /* flush right after */
1176                  }
1177                  process_queue();                /* catch up with results */
1178                  if (raysleft && !--raysleft)
# Line 1161 | Line 1180 | trace_contribs(FILE *fin)
1180          }
1181          while (wait_rproc() != NULL)            /* process outstanding rays */
1182                  process_queue();
1183 <        if (accumulate)
1184 <                done_contrib();                 /* output tallies */
1183 >        if (accumulate <= 0)
1184 >                done_contrib(0);                /* output tallies */
1185 >        else if (account < accumulate) {
1186 >                error(WARNING, "partial accumulation in final record");
1187 >                done_contrib(accumulate - account);
1188 >        }
1189          if (raysleft)
1190                  error(USER, "unexpected EOF on input");
1191          lu_done(&ofiletab);                     /* close output files */
1192   }
1193  
1194 < /* seek on the given output file */
1194 > /* get ray contribution from previous file */
1195   static int
1196 < myseeko(const LUENT *e, void *p)
1196 > get_contrib(DCOLOR cnt, FILE *finp)
1197   {
1198 +        COLOR   fv;
1199 +        COLR    cv;
1200 +
1201 +        switch (outfmt) {
1202 +        case 'a':
1203 +                return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
1204 +        case 'f':
1205 +                if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
1206 +                        return(0);
1207 +                copycolor(cnt, fv);
1208 +                return(1);
1209 +        case 'd':
1210 +                return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
1211 +        case 'c':
1212 +                if (fread(cv, sizeof(cv), 1, finp) != 1)
1213 +                        return(0);
1214 +                colr_color(fv, cv);
1215 +                copycolor(cnt, fv);
1216 +                return(1);
1217 +        default:
1218 +                error(INTERNAL, "botched output format");
1219 +        }
1220 +        return(0);      /* pro forma return */
1221 + }
1222 +
1223 + /* close output file opened for input */
1224 + static int
1225 + myclose(const LUENT *e, void *p)
1226 + {
1227          STREAMOUT       *sop = (STREAMOUT *)e->data;
1176        off_t           nbytes = *(off_t *)p;
1228          
1229 <        if (sop->reclen > 1)
1230 <                nbytes = nbytes * sop->reclen;
1231 <        if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1232 <                sprintf(errmsg, "seek error on file '%s'", e->key);
1233 <                error(SYSTEM, errmsg);
1183 <        }
1184 <        return 0;
1229 >        if (sop->ofp == NULL)
1230 >                return(0);
1231 >        fclose(sop->ofp);
1232 >        sop->ofp = NULL;
1233 >        return(0);
1234   }
1235  
1236   /* load previously accumulated values */
# Line 1195 | Line 1244 | reload_output(void)
1244          char            *fmode = "rb";
1245          char            *outvfmt;
1246          LUENT           *ment, *oent;
1247 +        int             xr, yr;
1248          STREAMOUT       sout;
1249 +        DCOLOR          rgbv;
1250  
1200        error(INTERNAL, "-r option not yet implemented with -c");
1201
1251          switch (outfmt) {
1252          case 'a':
1253                  outvfmt = "ascii";
# Line 1217 | Line 1266 | reload_output(void)
1266                  error(INTERNAL, "botched output format");
1267                  return;
1268          }
1269 <                                                /* check modifier outputs */
1269 >                                                /* reload modifier values */
1270          for (i = 0; i < nmods; i++) {
1271                  ment = lu_find(&modconttab,modname[i]);
1272                  mp = (MODCONT *)ment->data;
# Line 1225 | Line 1274 | reload_output(void)
1274                          error(USER, "cannot reload from stdout");
1275                  if (mp->outspec[0] == '!')
1276                          error(USER, "cannot reload from command");
1277 <                for (j = 0; ; j++) {            /* check each bin's file */
1277 >                for (j = 0; ; j++) {            /* load each modifier bin */
1278                          ofl = ofname(oname, mp->outspec, mp->modname, j);
1279                          if (ofl < 0)
1280                                  error(USER, "bad output file specification");
# Line 1234 | Line 1283 | reload_output(void)
1283                                  sout = *(STREAMOUT *)oent->data;
1284                          } else {
1285                                  sout.reclen = 0;
1286 +                                sout.outpipe = 0;
1287 +                                sout.xr = xres; sout.yr = yres;
1288                                  sout.ofp = NULL;
1289                          }
1290 <                        if (sout.ofp != NULL) { /* already open? */
1291 <                                if (ofl & OF_BIN)
1292 <                                        continue;
1293 <                                break;
1290 >                        if (sout.ofp == NULL) { /* open output as input */
1291 >                                sout.ofp = fopen(oname, fmode);
1292 >                                if (sout.ofp == NULL) {
1293 >                                        if (j)
1294 >                                                break;  /* assume end of modifier */
1295 >                                        sprintf(errmsg, "missing reload file '%s'",
1296 >                                                        oname);
1297 >                                        error(WARNING, errmsg);
1298 >                                        break;
1299 >                                }
1300 >                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1301 >                                        sprintf(errmsg, "format mismatch for '%s'",
1302 >                                                        oname);
1303 >                                        error(USER, errmsg);
1304 >                                }
1305 >                                if ((sout.xr > 0) & (sout.yr > 0) &&
1306 >                                                (!fscnresolu(&xr, &yr, sout.ofp) ||
1307 >                                                        (xr != sout.xr) |
1308 >                                                        (yr != sout.yr))) {
1309 >                                        sprintf(errmsg, "resolution mismatch for '%s'",
1310 >                                                        oname);
1311 >                                        error(USER, errmsg);
1312 >                                }
1313                          }
1314 <                                                /* open output */
1315 <                        sout.ofp = fopen(oname, fmode);
1316 <                        if (sout.ofp == NULL) {
1317 <                                if (j)
1318 <                                        break;  /* assume end of modifier */
1319 <                                sprintf(errmsg, "missing reload file '%s'",
1320 <                                                oname);
1321 <                                error(WARNING, errmsg);
1314 >                                                        /* read in RGB value */
1315 >                        if (!get_contrib(rgbv, sout.ofp)) {
1316 >                                if (!j) {
1317 >                                        fclose(sout.ofp);
1318 >                                        break;          /* ignore empty file */
1319 >                                }
1320 >                                if (j < mp->nbins) {
1321 >                                        sprintf(errmsg, "missing data in '%s'",
1322 >                                                        oname);
1323 >                                        error(USER, errmsg);
1324 >                                }
1325                                  break;
1326                          }
1327 <                        if (oent->key == NULL)  /* new entry */
1327 >                        if (j >= mp->nbins)             /* grow modifier size */
1328 >                                ment->data = (char *)(mp = growmodifier(mp, j+1));
1329 >                        copycolor(mp->cbin[j], rgbv);
1330 >                        if (oent->key == NULL)          /* new file entry */
1331                                  oent->key = strcpy((char *)
1332                                                  malloc(strlen(oname)+1), oname);
1333                          if (oent->data == NULL)
1334                                  oent->data = (char *)malloc(sizeof(STREAMOUT));
1335                          *(STREAMOUT *)oent->data = sout;
1260                        if (!(ofl & OF_BIN))
1261                                break;          /* no bin separation */
1336                  }
1263                if (j > mp->nbins)              /* reallocate modifier bins */
1264                        ment->data = (char *)(mp = growmodifier(mp, j));
1337          }
1338 <        lu_done(&ofiletab);                     /* close all files */
1338 >        lu_doall(&ofiletab, myclose, NULL);     /* close all files */
1339   }
1340  
1341 + /* seek on the given output file */
1342 + static int
1343 + myseeko(const LUENT *e, void *p)
1344 + {
1345 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1346 +        off_t           nbytes = *(off_t *)p;
1347 +        
1348 +        if (sop->reclen > 1)
1349 +                nbytes = nbytes * sop->reclen;
1350 +        if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1351 +                sprintf(errmsg, "seek error on file '%s'", e->key);
1352 +                error(SYSTEM, errmsg);
1353 +        }
1354 +        return 0;
1355 + }
1356 +
1357   /* recover output if possible */
1358   void
1359   recover_output(FILE *fin)
# Line 1319 | Line 1407 | recover_output(FILE *fin)
1407                                  sout = *(STREAMOUT *)oent->data;
1408                          } else {
1409                                  sout.reclen = 0;
1410 +                                sout.outpipe = 0;
1411                                  sout.ofp = NULL;
1412                          }
1413                          if (sout.ofp != NULL) { /* already open? */
# Line 1359 | Line 1448 | recover_output(FILE *fin)
1448                                                  oname);
1449                                  error(USER, errmsg);
1450                          }
1451 <                        if ((xres > 0) & (yres > 0) &&
1451 >                        sout.xr = xres; sout.yr = yres;
1452 >                        if ((sout.xr > 0) & (sout.yr > 0) &&
1453                                          (!fscnresolu(&xr, &yr, sout.ofp) ||
1454 <                                                xr != xres ||
1455 <                                                yr != yres)) {
1454 >                                                (xr != sout.xr) |
1455 >                                                (yr != sout.yr))) {
1456                                  sprintf(errmsg, "resolution mismatch for '%s'",
1457                                                  oname);
1458                                  error(USER, errmsg);
# Line 1391 | Line 1481 | recover_output(FILE *fin)
1481                  error(WARNING, "no output files to recover");
1482                  return;
1483          }
1484 <        if (raysleft && lastout >= raysleft) {
1484 >        if (raysleft && lastout >= raysleft/accumulate) {
1485                  error(WARNING, "output appears to be complete");
1486                  /* XXX should read & discard input? */
1487                  quit(0);
# Line 1403 | Line 1493 | recover_output(FILE *fin)
1493          for (nvals = 0; nvals < lastout; nvals++)
1494                  if (getinp(oname, fin) < 0)
1495                          error(USER, "unexpected EOF on input");
1496 <        lastray = lastdone = (unsigned long)lastout;
1496 >        lastray = lastdone = (unsigned long)lastout * accumulate;
1497          if (raysleft)
1498                  raysleft -= lastray;
1499   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines