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.51 by greg, Sat Dec 6 01:08:53 2008 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 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 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 779 | Line 772 | getostream(const char *ospec, const char *mname, int b
772                  sop->ofp = NULL;                /* open iff noopen==0 */
773                  sop->xr = xres; sop->yr = yres;
774                  lep->data = (char *)sop;
775 <                if (!force_open && access(oname, F_OK) == 0) {
775 >                if (!sop->outpipe & !force_open & !recover &&
776 >                                access(oname, F_OK) == 0) {
777                          errno = EEXIST;         /* file exists */
778                          goto openerr;
779                  }
# Line 808 | Line 802 | getostream(const char *ospec, const char *mname, int b
802                          *cp = '\0';
803                          printheader(sop->ofp, info);
804                  }
805 +                if (accumulate > 0) {           /* global resolution */
806 +                        sop->xr = xres; sop->yr = yres;
807 +                }
808                  printresolu(sop->ofp, sop->xr, sop->yr);
809                                                  /* play catch-up */
810 <                for (i = accumulate ? 0 : lastdone; i--; ) {
810 >                for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) {
811                          int     j = sop->reclen;
812                          if (j <= 0) j = 1;
813                          while (j--)
# Line 935 | Line 932 | put_contrib(const DCOLOR cnt, FILE *fout)
932                  fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
933                  break;
934          case 'f':
935 <                fv[0] = cnt[0];
939 <                fv[1] = cnt[1];
940 <                fv[2] = cnt[2];
935 >                copycolor(fv, cnt);
936                  fwrite(fv, sizeof(float), 3, fout);
937                  break;
938          case 'd':
# Line 954 | Line 949 | put_contrib(const DCOLOR cnt, FILE *fout)
949  
950   /* output ray tallies and clear for next accumulation */
951   void
952 < done_contrib(void)
952 > done_contrib(int navg)
953   {
954 +        double          sf = 1.;
955          int             i, j;
956          MODCONT         *mp;
957          STREAMOUT       *sop;
958 +                                                /* set average scaling */
959 +        if (navg > 1)
960 +                sf = 1. / (double)navg;
961                                                  /* output modifiers in order */
962          for (i = 0; i < nmods; i++) {
963                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
964 +                if (navg > 1)                   /* average scaling */
965 +                        for (j = mp->nbins; j--; )
966 +                                scalecolor(mp->cbin[j], sf);
967                  sop = getostream(mp->outspec, mp->modname, 0,0);
968                  put_contrib(mp->cbin[0], sop->ofp);
969                  if (mp->nbins > 3 &&            /* minor optimization */
# Line 1045 | Line 1047 | process_queue(void)
1047                          cp += sizeof(float)*9; n -= sizeof(float)*9;
1048                          add_contrib(modname);
1049                  }
1050 <                if (!accumulate)
1051 <                        done_contrib(); /* sum up contributions & output */
1050 >                                        /* time to produce record? */
1051 >                if (account > 0 && !--account)
1052 >                        done_contrib(account = accumulate);
1053                  lastdone = rtp->raynum;
1054                  if (rtp->buf != NULL)   /* free up buffer space */
1055                          free(rtp->buf);
# Line 1133 | Line 1136 | get_rproc(void)
1136   void
1137   trace_contribs(FILE *fin)
1138   {
1139 +        static int      ignore_warning_given = 0;
1140          char            inpbuf[128];
1141          int             iblen;
1142          struct rtproc   *rtp;
1143                                                  /* loop over input */
1144          while ((iblen = getinp(inpbuf, fin)) >= 0) {
1145 +                if (!iblen && accumulate != 1) {
1146 +                        if (!ignore_warning_given++)
1147 +                                error(WARNING,
1148 +                                "dummy ray(s) ignored during accumulation\n");
1149 +                        continue;
1150 +                }
1151                  if (!iblen ||                   /* need reset? */
1152                                  queue_length() > 10*nrtprocs() ||
1153                                  lastray+1 < lastray) {
# Line 1161 | Line 1171 | trace_contribs(FILE *fin)
1171          }
1172          while (wait_rproc() != NULL)            /* process outstanding rays */
1173                  process_queue();
1174 <        if (accumulate)
1175 <                done_contrib();                 /* output tallies */
1174 >        if (accumulate <= 0)
1175 >                done_contrib(0);                /* output tallies */
1176 >        else if (account < accumulate) {
1177 >                error(WARNING, "partial accumulation in final record");
1178 >                done_contrib(accumulate - account);
1179 >        }
1180          if (raysleft)
1181                  error(USER, "unexpected EOF on input");
1182          lu_done(&ofiletab);                     /* close output files */
1183   }
1184  
1185 < /* seek on the given output file */
1185 > /* get ray contribution from previous file */
1186   static int
1187 < myseeko(const LUENT *e, void *p)
1187 > get_contrib(DCOLOR cnt, FILE *finp)
1188   {
1189 +        COLOR   fv;
1190 +        COLR    cv;
1191 +
1192 +        switch (outfmt) {
1193 +        case 'a':
1194 +                return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
1195 +        case 'f':
1196 +                if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
1197 +                        return(0);
1198 +                copycolor(cnt, fv);
1199 +                return(1);
1200 +        case 'd':
1201 +                return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
1202 +        case 'c':
1203 +                if (fread(cv, sizeof(cv), 1, finp) != 1)
1204 +                        return(0);
1205 +                colr_color(fv, cv);
1206 +                copycolor(cnt, fv);
1207 +                return(1);
1208 +        default:
1209 +                error(INTERNAL, "botched output format");
1210 +        }
1211 +        return(0);      /* pro forma return */
1212 + }
1213 +
1214 + /* close output file opened for input */
1215 + static int
1216 + myclose(const LUENT *e, void *p)
1217 + {
1218          STREAMOUT       *sop = (STREAMOUT *)e->data;
1176        off_t           nbytes = *(off_t *)p;
1219          
1220 <        if (sop->reclen > 1)
1221 <                nbytes = nbytes * sop->reclen;
1222 <        if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1223 <                sprintf(errmsg, "seek error on file '%s'", e->key);
1224 <                error(SYSTEM, errmsg);
1183 <        }
1184 <        return 0;
1220 >        if (sop->ofp == NULL)
1221 >                return(0);
1222 >        fclose(sop->ofp);
1223 >        sop->ofp = NULL;
1224 >        return(0);
1225   }
1226  
1227   /* load previously accumulated values */
# Line 1195 | Line 1235 | reload_output(void)
1235          char            *fmode = "rb";
1236          char            *outvfmt;
1237          LUENT           *ment, *oent;
1238 +        int             xr, yr;
1239          STREAMOUT       sout;
1240 +        DCOLOR          rgbv;
1241  
1200        error(INTERNAL, "-r option not yet implemented with -c");
1201
1242          switch (outfmt) {
1243          case 'a':
1244                  outvfmt = "ascii";
# Line 1217 | Line 1257 | reload_output(void)
1257                  error(INTERNAL, "botched output format");
1258                  return;
1259          }
1260 <                                                /* check modifier outputs */
1260 >                                                /* reload modifier values */
1261          for (i = 0; i < nmods; i++) {
1262                  ment = lu_find(&modconttab,modname[i]);
1263                  mp = (MODCONT *)ment->data;
# Line 1225 | Line 1265 | reload_output(void)
1265                          error(USER, "cannot reload from stdout");
1266                  if (mp->outspec[0] == '!')
1267                          error(USER, "cannot reload from command");
1268 <                for (j = 0; ; j++) {            /* check each bin's file */
1268 >                for (j = 0; ; j++) {            /* load each modifier bin */
1269                          ofl = ofname(oname, mp->outspec, mp->modname, j);
1270                          if (ofl < 0)
1271                                  error(USER, "bad output file specification");
# Line 1234 | Line 1274 | reload_output(void)
1274                                  sout = *(STREAMOUT *)oent->data;
1275                          } else {
1276                                  sout.reclen = 0;
1277 +                                sout.outpipe = 0;
1278 +                                sout.xr = xres; sout.yr = yres;
1279                                  sout.ofp = NULL;
1280                          }
1281 <                        if (sout.ofp != NULL) { /* already open? */
1282 <                                if (ofl & OF_BIN)
1283 <                                        continue;
1284 <                                break;
1281 >                        if (sout.ofp == NULL) { /* open output as input */
1282 >                                sout.ofp = fopen(oname, fmode);
1283 >                                if (sout.ofp == NULL) {
1284 >                                        if (j)
1285 >                                                break;  /* assume end of modifier */
1286 >                                        sprintf(errmsg, "missing reload file '%s'",
1287 >                                                        oname);
1288 >                                        error(WARNING, errmsg);
1289 >                                        break;
1290 >                                }
1291 >                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1292 >                                        sprintf(errmsg, "format mismatch for '%s'",
1293 >                                                        oname);
1294 >                                        error(USER, errmsg);
1295 >                                }
1296 >                                if ((sout.xr > 0) & (sout.yr > 0) &&
1297 >                                                (!fscnresolu(&xr, &yr, sout.ofp) ||
1298 >                                                        (xr != sout.xr) |
1299 >                                                        (yr != sout.yr))) {
1300 >                                        sprintf(errmsg, "resolution mismatch for '%s'",
1301 >                                                        oname);
1302 >                                        error(USER, errmsg);
1303 >                                }
1304                          }
1305 <                                                /* open output */
1306 <                        sout.ofp = fopen(oname, fmode);
1307 <                        if (sout.ofp == NULL) {
1308 <                                if (j)
1309 <                                        break;  /* assume end of modifier */
1310 <                                sprintf(errmsg, "missing reload file '%s'",
1311 <                                                oname);
1312 <                                error(WARNING, errmsg);
1305 >                                                        /* read in RGB value */
1306 >                        if (!get_contrib(rgbv, sout.ofp)) {
1307 >                                if (!j) {
1308 >                                        fclose(sout.ofp);
1309 >                                        break;          /* ignore empty file */
1310 >                                }
1311 >                                if (j < mp->nbins) {
1312 >                                        sprintf(errmsg, "missing data in '%s'",
1313 >                                                        oname);
1314 >                                        error(USER, errmsg);
1315 >                                }
1316                                  break;
1317                          }
1318 <                        if (oent->key == NULL)  /* new entry */
1318 >                        if (j >= mp->nbins)             /* grow modifier size */
1319 >                                ment->data = (char *)(mp = growmodifier(mp, j+1));
1320 >                        copycolor(mp->cbin[j], rgbv);
1321 >                        if (oent->key == NULL)          /* new file entry */
1322                                  oent->key = strcpy((char *)
1323                                                  malloc(strlen(oname)+1), oname);
1324                          if (oent->data == NULL)
1325                                  oent->data = (char *)malloc(sizeof(STREAMOUT));
1326                          *(STREAMOUT *)oent->data = sout;
1260                        if (!(ofl & OF_BIN))
1261                                break;          /* no bin separation */
1327                  }
1263                if (j > mp->nbins)              /* reallocate modifier bins */
1264                        ment->data = (char *)(mp = growmodifier(mp, j));
1328          }
1329 <        lu_done(&ofiletab);                     /* close all files */
1329 >        lu_doall(&ofiletab, myclose, NULL);     /* close all files */
1330   }
1331  
1332 + /* seek on the given output file */
1333 + static int
1334 + myseeko(const LUENT *e, void *p)
1335 + {
1336 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1337 +        off_t           nbytes = *(off_t *)p;
1338 +        
1339 +        if (sop->reclen > 1)
1340 +                nbytes = nbytes * sop->reclen;
1341 +        if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1342 +                sprintf(errmsg, "seek error on file '%s'", e->key);
1343 +                error(SYSTEM, errmsg);
1344 +        }
1345 +        return 0;
1346 + }
1347 +
1348   /* recover output if possible */
1349   void
1350   recover_output(FILE *fin)
# Line 1319 | Line 1398 | recover_output(FILE *fin)
1398                                  sout = *(STREAMOUT *)oent->data;
1399                          } else {
1400                                  sout.reclen = 0;
1401 +                                sout.outpipe = 0;
1402                                  sout.ofp = NULL;
1403                          }
1404                          if (sout.ofp != NULL) { /* already open? */
# Line 1359 | Line 1439 | recover_output(FILE *fin)
1439                                                  oname);
1440                                  error(USER, errmsg);
1441                          }
1442 <                        if ((xres > 0) & (yres > 0) &&
1442 >                        sout.xr = xres; sout.yr = yres;
1443 >                        if ((sout.xr > 0) & (sout.yr > 0) &&
1444                                          (!fscnresolu(&xr, &yr, sout.ofp) ||
1445 <                                                xr != xres ||
1446 <                                                yr != yres)) {
1445 >                                                (xr != sout.xr) |
1446 >                                                (yr != sout.yr))) {
1447                                  sprintf(errmsg, "resolution mismatch for '%s'",
1448                                                  oname);
1449                                  error(USER, errmsg);
# Line 1391 | Line 1472 | recover_output(FILE *fin)
1472                  error(WARNING, "no output files to recover");
1473                  return;
1474          }
1475 <        if (raysleft && lastout >= raysleft) {
1475 >        if (raysleft && lastout >= raysleft/accumulate) {
1476                  error(WARNING, "output appears to be complete");
1477                  /* XXX should read & discard input? */
1478                  quit(0);
# Line 1403 | Line 1484 | recover_output(FILE *fin)
1484          for (nvals = 0; nvals < lastout; nvals++)
1485                  if (getinp(oname, fin) < 0)
1486                          error(USER, "unexpected EOF on input");
1487 <        lastray = lastdone = (unsigned long)lastout;
1487 >        lastray = lastdone = (unsigned long)lastout * accumulate;
1488          if (raysleft)
1489                  raysleft -= lastray;
1490   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines