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.56 by greg, Fri Jun 11 20:26:09 2010 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 390 | Line 378 | main(int argc, char *argv[])
378          rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
379                                  /* just asking for defaults? */
380          if (!strcmp(argv[i], "-defaults")) {
381 <                char    sxres[16], syres[16];
381 >                char    nps[8], sxres[16], syres[16];
382                  char    *rtpath;
383 <                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
383 >                printf("-c %-5d\t\t\t# accumulated rays per record\n",
384 >                                accumulate);
385                  printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
386                                  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");
387                  fflush(stdout);                 /* report OUR options */
388 +                rtargv[rtargc++] = "-n";
389 +                sprintf(nps, "%d", nprocs);
390 +                rtargv[rtargc++] = nps;
391                  rtargv[rtargc++] = header ? "-h+" : "-h-";
392                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
393                  rtargv[rtargc++] = fmt;
# Line 437 | Line 426 | main(int argc, char *argv[])
426                  error(USER, "missing octree argument");
427          rtargv[rtargc++] = octree = argv[i];
428          rtargv[rtargc] = NULL;
429 <                                /* start rtrace */
429 >                                /* start rtrace & recover if requested */
430          init(nprocs);
431 <        if (recover)            /* perform recovery if requested */
432 <                if ((force_open = accumulate))
433 <                        reload_output();
445 <                else
446 <                        recover_output(stdin);
447 <        trace_contribs(stdin);  /* compute contributions */
431 >                                /* compute contributions */
432 >        trace_contribs(stdin);
433 >                                /* clean up */
434          quit(0);
435   }
436  
# Line 557 | Line 543 | init(int np)
543                          raysleft = yres;
544          } else
545                  raysleft = 0;
546 +        if ((account = accumulate) > 0)
547 +                raysleft *= accumulate;
548          waitflush = xres;
549 +        if (!recover)
550 +                return;
551 +                                        /* recover previous values */
552 +        if (accumulate <= 0)
553 +                reload_output();
554 +        else
555 +                recover_output(stdin);
556   }
557  
558   /* grow modifier to accommodate more bins */
# Line 587 | Line 582 | addmodifier(char *modn, char *outf, char *binv, int bi
582                  error(USER, errmsg);
583          }
584          if (nmods >= MAXMODLIST)
585 <                error(USER, "too many modifiers");
585 >                error(INTERNAL, "too many modifiers");
586          modname[nmods++] = modn;        /* XXX assumes static string */
587          lep->key = modn;                /* XXX assumes static string */
588          mp = (MODCONT *)malloc(sizeof(MODCONT));
# Line 595 | Line 590 | addmodifier(char *modn, char *outf, char *binv, int bi
590                  error(SYSTEM, "out of memory in addmodifier");
591          mp->outspec = outf;             /* XXX assumes static string */
592          mp->modname = modn;             /* XXX assumes static string */
593 <        if (binv != NULL)
594 <                mp->binv = eparse(binv);
595 <        else
596 <                mp->binv = eparse("0");
597 <        mp->nbins = 1;
593 >        if (binv == NULL)
594 >                binv = "0";             /* use single bin if unspecified */
595 >        mp->binv = eparse(binv);
596 >        if (mp->binv->type == NUM) {    /* check value if constant */
597 >                bincnt = (int)(evalue(mp->binv) + 1.5);
598 >                if (bincnt != 1) {
599 >                        sprintf(errmsg, "illegal non-zero constant for bin (%s)",
600 >                                        binv);
601 >                        error(USER, errmsg);
602 >                }
603 >        }
604 >        mp->nbins = 1;                  /* initialize results holder */
605          setcolor(mp->cbin[0], 0., 0., 0.);
606 <        if (mp->binv->type == NUM)      /* assume one bin if constant */
605 <                bincnt = 1;
606 <        else if (bincnt > 1)
606 >        if (bincnt > 1)
607                  mp = growmodifier(mp, bincnt);
608          lep->data = (char *)mp;
609                                          /* allocate output streams */
# Line 666 | Line 666 | ofname(char *oname, const char *ospec, const char *mna
666                                  mnp = cp;
667                                  break;
668                          case 'd':
669 +                        case 'i':
670 +                        case 'o':
671 +                        case 'x':
672 +                        case 'X':
673                                  if (bnp != NULL)
674                                          return -1;
675                                  bnp = cp;
# Line 779 | Line 783 | getostream(const char *ospec, const char *mname, int b
783                  sop->ofp = NULL;                /* open iff noopen==0 */
784                  sop->xr = xres; sop->yr = yres;
785                  lep->data = (char *)sop;
786 <                if (!force_open && access(oname, F_OK) == 0) {
786 >                if (!sop->outpipe & !force_open & !recover &&
787 >                                access(oname, F_OK) == 0) {
788                          errno = EEXIST;         /* file exists */
789                          goto openerr;
790                  }
# Line 808 | Line 813 | getostream(const char *ospec, const char *mname, int b
813                          *cp = '\0';
814                          printheader(sop->ofp, info);
815                  }
816 +                if (accumulate > 0) {           /* global resolution */
817 +                        sop->xr = xres; sop->yr = yres;
818 +                }
819                  printresolu(sop->ofp, sop->xr, sop->yr);
820                                                  /* play catch-up */
821 <                for (i = accumulate ? 0 : lastdone; i--; ) {
821 >                for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) {
822                          int     j = sop->reclen;
823                          if (j <= 0) j = 1;
824                          while (j--)
# Line 935 | Line 943 | put_contrib(const DCOLOR cnt, FILE *fout)
943                  fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
944                  break;
945          case 'f':
946 <                fv[0] = cnt[0];
939 <                fv[1] = cnt[1];
940 <                fv[2] = cnt[2];
946 >                copycolor(fv, cnt);
947                  fwrite(fv, sizeof(float), 3, fout);
948                  break;
949          case 'd':
# Line 954 | Line 960 | put_contrib(const DCOLOR cnt, FILE *fout)
960  
961   /* output ray tallies and clear for next accumulation */
962   void
963 < done_contrib(void)
963 > done_contrib(int navg)
964   {
965 +        double          sf = 1.;
966          int             i, j;
967          MODCONT         *mp;
968          STREAMOUT       *sop;
969 +                                                /* set average scaling */
970 +        if (navg > 1)
971 +                sf = 1. / (double)navg;
972                                                  /* output modifiers in order */
973          for (i = 0; i < nmods; i++) {
974                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
975 +                if (navg > 1)                   /* average scaling */
976 +                        for (j = mp->nbins; j--; )
977 +                                scalecolor(mp->cbin[j], sf);
978                  sop = getostream(mp->outspec, mp->modname, 0,0);
979                  put_contrib(mp->cbin[0], sop->ofp);
980                  if (mp->nbins > 3 &&            /* minor optimization */
# Line 1045 | Line 1058 | process_queue(void)
1058                          cp += sizeof(float)*9; n -= sizeof(float)*9;
1059                          add_contrib(modname);
1060                  }
1061 <                if (!accumulate)
1062 <                        done_contrib(); /* sum up contributions & output */
1061 >                                        /* time to produce record? */
1062 >                if (account > 0 && !--account)
1063 >                        done_contrib(account = accumulate);
1064                  lastdone = rtp->raynum;
1065                  if (rtp->buf != NULL)   /* free up buffer space */
1066                          free(rtp->buf);
# Line 1133 | Line 1147 | get_rproc(void)
1147   void
1148   trace_contribs(FILE *fin)
1149   {
1150 +        static int      ignore_warning_given = 0;
1151          char            inpbuf[128];
1152          int             iblen;
1153          struct rtproc   *rtp;
1154                                                  /* loop over input */
1155          while ((iblen = getinp(inpbuf, fin)) >= 0) {
1156 <                if (!iblen ||                   /* need reset? */
1156 >                if (!iblen && accumulate != 1) {
1157 >                        if (!ignore_warning_given++)
1158 >                                error(WARNING,
1159 >                                "dummy ray(s) ignored during accumulation\n");
1160 >                        continue;
1161 >                }
1162 >                if (!iblen ||                   /* need flush/reset? */
1163                                  queue_length() > 10*nrtprocs() ||
1164                                  lastray+1 < lastray) {
1165                          while (wait_rproc() != NULL)
# Line 1151 | Line 1172 | trace_contribs(FILE *fin)
1172                  if (iblen) {                    /* trace ray if valid */
1173                          writebuf(rtp->pd.w, inpbuf, iblen);
1174                  } else {                        /* else bypass dummy ray */
1175 <                        queue_raytree(rtp);     /* empty tree */
1176 <                        if ((yres <= 0) | (waitflush > 1))
1177 <                                waitflush = 1;  /* flush after this */
1175 >                        queue_raytree(rtp);     /* queue empty ray/record */
1176 >                        if ((yres <= 0) | (xres <= 0))
1177 >                                waitflush = 1;  /* flush right after */
1178                  }
1179                  process_queue();                /* catch up with results */
1180                  if (raysleft && !--raysleft)
# Line 1161 | Line 1182 | trace_contribs(FILE *fin)
1182          }
1183          while (wait_rproc() != NULL)            /* process outstanding rays */
1184                  process_queue();
1185 <        if (accumulate)
1186 <                done_contrib();                 /* output tallies */
1185 >        if (accumulate <= 0)
1186 >                done_contrib(0);                /* output tallies */
1187 >        else if (account < accumulate) {
1188 >                error(WARNING, "partial accumulation in final record");
1189 >                done_contrib(accumulate - account);
1190 >        }
1191          if (raysleft)
1192                  error(USER, "unexpected EOF on input");
1193          lu_done(&ofiletab);                     /* close output files */
1194   }
1195  
1196 < /* seek on the given output file */
1196 > /* get ray contribution from previous file */
1197   static int
1198 < myseeko(const LUENT *e, void *p)
1198 > get_contrib(DCOLOR cnt, FILE *finp)
1199   {
1200 +        COLOR   fv;
1201 +        COLR    cv;
1202 +
1203 +        switch (outfmt) {
1204 +        case 'a':
1205 +                return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
1206 +        case 'f':
1207 +                if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
1208 +                        return(0);
1209 +                copycolor(cnt, fv);
1210 +                return(1);
1211 +        case 'd':
1212 +                return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
1213 +        case 'c':
1214 +                if (fread(cv, sizeof(cv), 1, finp) != 1)
1215 +                        return(0);
1216 +                colr_color(fv, cv);
1217 +                copycolor(cnt, fv);
1218 +                return(1);
1219 +        default:
1220 +                error(INTERNAL, "botched output format");
1221 +        }
1222 +        return(0);      /* pro forma return */
1223 + }
1224 +
1225 + /* close output file opened for input */
1226 + static int
1227 + myclose(const LUENT *e, void *p)
1228 + {
1229          STREAMOUT       *sop = (STREAMOUT *)e->data;
1176        off_t           nbytes = *(off_t *)p;
1230          
1231 <        if (sop->reclen > 1)
1232 <                nbytes = nbytes * sop->reclen;
1233 <        if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1234 <                sprintf(errmsg, "seek error on file '%s'", e->key);
1235 <                error(SYSTEM, errmsg);
1183 <        }
1184 <        return 0;
1231 >        if (sop->ofp == NULL)
1232 >                return(0);
1233 >        fclose(sop->ofp);
1234 >        sop->ofp = NULL;
1235 >        return(0);
1236   }
1237  
1238   /* load previously accumulated values */
# Line 1195 | Line 1246 | reload_output(void)
1246          char            *fmode = "rb";
1247          char            *outvfmt;
1248          LUENT           *ment, *oent;
1249 +        int             xr, yr;
1250          STREAMOUT       sout;
1251 +        DCOLOR          rgbv;
1252  
1200        error(INTERNAL, "-r option not yet implemented with -c");
1201
1253          switch (outfmt) {
1254          case 'a':
1255                  outvfmt = "ascii";
# Line 1217 | Line 1268 | reload_output(void)
1268                  error(INTERNAL, "botched output format");
1269                  return;
1270          }
1271 <                                                /* check modifier outputs */
1271 >                                                /* reload modifier values */
1272          for (i = 0; i < nmods; i++) {
1273                  ment = lu_find(&modconttab,modname[i]);
1274                  mp = (MODCONT *)ment->data;
# Line 1225 | Line 1276 | reload_output(void)
1276                          error(USER, "cannot reload from stdout");
1277                  if (mp->outspec[0] == '!')
1278                          error(USER, "cannot reload from command");
1279 <                for (j = 0; ; j++) {            /* check each bin's file */
1279 >                for (j = 0; ; j++) {            /* load each modifier bin */
1280                          ofl = ofname(oname, mp->outspec, mp->modname, j);
1281                          if (ofl < 0)
1282                                  error(USER, "bad output file specification");
# Line 1234 | Line 1285 | reload_output(void)
1285                                  sout = *(STREAMOUT *)oent->data;
1286                          } else {
1287                                  sout.reclen = 0;
1288 +                                sout.outpipe = 0;
1289 +                                sout.xr = xres; sout.yr = yres;
1290                                  sout.ofp = NULL;
1291                          }
1292 <                        if (sout.ofp != NULL) { /* already open? */
1293 <                                if (ofl & OF_BIN)
1294 <                                        continue;
1295 <                                break;
1292 >                        if (sout.ofp == NULL) { /* open output as input */
1293 >                                sout.ofp = fopen(oname, fmode);
1294 >                                if (sout.ofp == NULL) {
1295 >                                        if (j)
1296 >                                                break;  /* assume end of modifier */
1297 >                                        sprintf(errmsg, "missing reload file '%s'",
1298 >                                                        oname);
1299 >                                        error(WARNING, errmsg);
1300 >                                        break;
1301 >                                }
1302 >                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1303 >                                        sprintf(errmsg, "format mismatch for '%s'",
1304 >                                                        oname);
1305 >                                        error(USER, errmsg);
1306 >                                }
1307 >                                if ((sout.xr > 0) & (sout.yr > 0) &&
1308 >                                                (!fscnresolu(&xr, &yr, sout.ofp) ||
1309 >                                                        (xr != sout.xr) |
1310 >                                                        (yr != sout.yr))) {
1311 >                                        sprintf(errmsg, "resolution mismatch for '%s'",
1312 >                                                        oname);
1313 >                                        error(USER, errmsg);
1314 >                                }
1315                          }
1316 <                                                /* open output */
1317 <                        sout.ofp = fopen(oname, fmode);
1318 <                        if (sout.ofp == NULL) {
1319 <                                if (j)
1320 <                                        break;  /* assume end of modifier */
1321 <                                sprintf(errmsg, "missing reload file '%s'",
1322 <                                                oname);
1323 <                                error(WARNING, errmsg);
1316 >                                                        /* read in RGB value */
1317 >                        if (!get_contrib(rgbv, sout.ofp)) {
1318 >                                if (!j) {
1319 >                                        fclose(sout.ofp);
1320 >                                        break;          /* ignore empty file */
1321 >                                }
1322 >                                if (j < mp->nbins) {
1323 >                                        sprintf(errmsg, "missing data in '%s'",
1324 >                                                        oname);
1325 >                                        error(USER, errmsg);
1326 >                                }
1327                                  break;
1328                          }
1329 <                        if (oent->key == NULL)  /* new entry */
1329 >                        if (j >= mp->nbins)             /* grow modifier size */
1330 >                                ment->data = (char *)(mp = growmodifier(mp, j+1));
1331 >                        copycolor(mp->cbin[j], rgbv);
1332 >                        if (oent->key == NULL)          /* new file entry */
1333                                  oent->key = strcpy((char *)
1334                                                  malloc(strlen(oname)+1), oname);
1335                          if (oent->data == NULL)
1336                                  oent->data = (char *)malloc(sizeof(STREAMOUT));
1337                          *(STREAMOUT *)oent->data = sout;
1260                        if (!(ofl & OF_BIN))
1261                                break;          /* no bin separation */
1338                  }
1263                if (j > mp->nbins)              /* reallocate modifier bins */
1264                        ment->data = (char *)(mp = growmodifier(mp, j));
1339          }
1340 <        lu_done(&ofiletab);                     /* close all files */
1340 >        lu_doall(&ofiletab, myclose, NULL);     /* close all files */
1341   }
1342  
1343 + /* seek on the given output file */
1344 + static int
1345 + myseeko(const LUENT *e, void *p)
1346 + {
1347 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1348 +        off_t           nbytes = *(off_t *)p;
1349 +        
1350 +        if (sop->reclen > 1)
1351 +                nbytes = nbytes * sop->reclen;
1352 +        if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1353 +                sprintf(errmsg, "seek error on file '%s'", e->key);
1354 +                error(SYSTEM, errmsg);
1355 +        }
1356 +        return 0;
1357 + }
1358 +
1359   /* recover output if possible */
1360   void
1361   recover_output(FILE *fin)
# Line 1319 | Line 1409 | recover_output(FILE *fin)
1409                                  sout = *(STREAMOUT *)oent->data;
1410                          } else {
1411                                  sout.reclen = 0;
1412 +                                sout.outpipe = 0;
1413                                  sout.ofp = NULL;
1414                          }
1415                          if (sout.ofp != NULL) { /* already open? */
# Line 1359 | Line 1450 | recover_output(FILE *fin)
1450                                                  oname);
1451                                  error(USER, errmsg);
1452                          }
1453 <                        if ((xres > 0) & (yres > 0) &&
1453 >                        sout.xr = xres; sout.yr = yres;
1454 >                        if ((sout.xr > 0) & (sout.yr > 0) &&
1455                                          (!fscnresolu(&xr, &yr, sout.ofp) ||
1456 <                                                xr != xres ||
1457 <                                                yr != yres)) {
1456 >                                                (xr != sout.xr) |
1457 >                                                (yr != sout.yr))) {
1458                                  sprintf(errmsg, "resolution mismatch for '%s'",
1459                                                  oname);
1460                                  error(USER, errmsg);
# Line 1391 | Line 1483 | recover_output(FILE *fin)
1483                  error(WARNING, "no output files to recover");
1484                  return;
1485          }
1486 <        if (raysleft && lastout >= raysleft) {
1486 >        if (raysleft && lastout >= raysleft/accumulate) {
1487                  error(WARNING, "output appears to be complete");
1488                  /* XXX should read & discard input? */
1489                  quit(0);
# Line 1403 | Line 1495 | recover_output(FILE *fin)
1495          for (nvals = 0; nvals < lastout; nvals++)
1496                  if (getinp(oname, fin) < 0)
1497                          error(USER, "unexpected EOF on input");
1498 <        lastray = lastdone = (unsigned long)lastout;
1498 >        lastray = lastdone = (unsigned long)lastout * accumulate;
1499          if (raysleft)
1500                  raysleft -= lastray;
1501   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines