--- ray/src/util/rfluxmtx.c 2014/07/25 16:58:20 2.9 +++ ray/src/util/rfluxmtx.c 2015/03/12 15:36:11 2.24 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rfluxmtx.c,v 2.9 2014/07/25 16:58:20 greg Exp $"; +static const char RCSid[] = "$Id: rfluxmtx.c,v 2.24 2015/03/12 15:36:11 greg Exp $"; #endif /* * Calculate flux transfer matrix or matrices using rcontrib @@ -11,6 +11,7 @@ static const char RCSid[] = "$Id: rfluxmtx.c,v 2.9 201 #include #include "rtio.h" #include "rtmath.h" +#include "rtprocess.h" #include "bsdf.h" #include "bsdf_m.h" #include "random.h" @@ -35,7 +36,7 @@ static const char RCSid[] = "$Id: rfluxmtx.c,v 2.9 201 char *progname; /* global argv[0] */ -int verbose = 0; /* verbose mode? */ +int verbose = 0; /* verbose mode (< 0 no warnings) */ char *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"}; int nrcargs = 2; @@ -144,20 +145,23 @@ surf_type(const char *otype) static char * oconv_command(int ac, char *av[]) { - static char oconvbuf[2048] = "!oconv -f"; - char *cp = oconvbuf + 9; - + static char oconvbuf[2048] = "!oconv -f "; + char *cp = oconvbuf + 10; + char *recv = *av++; + + if (ac-- <= 0) + return(NULL); while (ac-- > 0) { + strcpy(cp, *av++); + while (*cp) cp++; + *cp++ = ' '; if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) { fputs(progname, stderr); fputs(": too many file arguments!\n", stderr); exit(1); } - *cp++ = ' '; - strcpy(cp, *av++); - while (*cp) cp++; } - *cp = '\0'; + strcpy(cp, recv); /* receiver goes last */ return(oconvbuf); } @@ -222,7 +226,7 @@ popen_arglist(char *av[], char *mode) fputs(": command line too long in popen_arglist()\n", stderr); return(NULL); } - if (verbose) + if (verbose > 0) fprintf(stderr, "%s: opening pipe %s: %s\n", progname, (*mode=='w') ? "to" : "from", cmd); return(popen(cmd, mode)); @@ -240,7 +244,7 @@ my_exec(char *av[]) fputs(": command line too long in my_exec()\n", stderr); return(1); } - if (verbose) + if (verbose > 0) fprintf(stderr, "%s: running: %s\n", progname, cmd); return(system(cmd)); } @@ -255,7 +259,7 @@ my_exec(char *av[]) fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]); return(1); } - if (verbose) { + if (verbose > 0) { char cmd[4096]; if (!convert_commandline(cmd, sizeof(cmd), av)) strcpy(cmd, "COMMAND TOO LONG TO SHOW"); @@ -315,7 +319,7 @@ parse_params(PARAMS *p, char *pargs) int nparams = 0; int i; - for ( ; ; ) + for ( ; ; ) { switch (*cp++) { case 'h': if (*cp++ != '=') @@ -338,6 +342,8 @@ parse_params(PARAMS *p, char *pargs) break; if (!get_direction(p->vup, cp)) break; + while (*cp && !isspace(*cp++)) + ; ++nparams; continue; case 'o': @@ -363,7 +369,9 @@ parse_params(PARAMS *p, char *pargs) default: break; } - fprintf(stderr, "%s: bad parameter string '%s'\n", progname, pargs); + break; + } + fprintf(stderr, "%s: bad parameter string: %s", progname, pargs); exit(1); return(-1); /* pro forma return */ } @@ -401,14 +409,18 @@ finish_receiver(void) fputs(": undefined normal for hemisphere sampling\n", stderr); exit(1); } - if (normalize(curparams.vup) == 0) + if (normalize(curparams.vup) == 0) { if (fabs(curparams.nrm[2]) < .7) curparams.vup[2] = 1; else curparams.vup[1] = 1; + } /* determine sample type/bin */ if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') { - binv = "0"; /* uniform sampling -- one bin */ + sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)", + curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]); + binv = savqstr(sbuf); + nbins = "1"; /* uniform sampling -- one bin */ uniform = 1; } else if (tolower(curparams.hemis[0]) == 's' && tolower(curparams.hemis[1]) == 'c') { @@ -507,21 +519,12 @@ make_axes(FVECT uva[2], const FVECT nrm) { int i; - uva[1][0] = 0.5 - frandom(); - uva[1][1] = 0.5 - frandom(); - uva[1][2] = 0.5 - frandom(); - for (i = 3; i--; ) - if ((-0.6 < nrm[i]) & (nrm[i] < 0.6)) - break; - if (i < 0) { + if (!getperpendicular(uva[0], nrm)) { fputs(progname, stderr); fputs(": bad surface normal in make_axes!\n", stderr); exit(1); } - uva[1][i] = 1.0; - VCROSS(uva[0], uva[1], nrm); - normalize(uva[0]); - VCROSS(uva[1], nrm, uva[0]); + fcross(uva[1], nrm, uva[0]); } /* Illegal sender surfaces end up here */ @@ -783,8 +786,8 @@ sample_reinhart(PARAMS *p, int b, FILE *fp) alt = (row+samp3[1])*RAH; azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row); duvw[2] = cos(alt); /* measured from horizon */ - duvw[0] = tcos(azi)*duvw[2]; - duvw[1] = tsin(azi)*duvw[2]; + duvw[0] = tsin(azi)*duvw[2]; + duvw[1] = tcos(azi)*duvw[2]; duvw[2] = sqrt(1. - duvw[2]*duvw[2]); for (i = 3; i--; ) orig_dir[1][i] = -duvw[0]*p->udir[i] - @@ -835,7 +838,7 @@ sample_klems(PARAMS *p, int b, FILE *fp) while (n--) { /* stratified sampling */ SDmultiSamp(samp2, 2, (n+frandom())/sampcnt); - if (!bo_getvec(duvw, b+samp2[1], kbasis[bi])) + if (!bi_getvec(duvw, b+samp2[1], kbasis[bi])) return(0); for (i = 3; i--; ) orig_dir[1][i] = duvw[0]*p->udir[i] + @@ -858,7 +861,8 @@ prepare_sampler(void) fputs(": no sender surface!\n", stderr); return(-1); } - if (curparams.outfn != NULL) /* misplaced output file spec. */ + /* misplaced output file spec. */ + if ((curparams.outfn != NULL) & (verbose >= 0)) fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n", progname, curparams.outfn); /* check/set basis hemisphere */ @@ -872,11 +876,12 @@ prepare_sampler(void) fputs(": undefined normal for sender sampling\n", stderr); return(-1); } - if (normalize(curparams.vup) == 0) + if (normalize(curparams.vup) == 0) { if (fabs(curparams.nrm[2]) < .7) curparams.vup[2] = 1; else curparams.vup[1] = 1; + } VCROSS(curparams.udir, curparams.vup, curparams.nrm); if (normalize(curparams.udir) == 0) { fputs(progname, stderr); @@ -1008,7 +1013,7 @@ add_surface(int st, const char *oname, FILE *fp) snew->area *= PI*snew->area; break; } - if (snew->area <= FTINY) { + if ((snew->area <= FTINY) & (verbose >= 0)) { fprintf(stderr, "%s: warning - zero area for surface '%s'\n", progname, oname); free(snew); @@ -1066,9 +1071,9 @@ add_recv_object(FILE *fp) /* else skip arguments */ if (!fscanf(fp, "%d", &n)) return(0); while (n-- > 0) fscanf(fp, "%*s"); - if (!fscanf(fp, "%d", &n)) return; + if (!fscanf(fp, "%d", &n)) return(0); while (n-- > 0) fscanf(fp, "%*d"); - if (!fscanf(fp, "%d", &n)) return; + if (!fscanf(fp, "%d", &n)) return(0); while (n-- > 0) fscanf(fp, "%*f"); return(0); } @@ -1078,10 +1083,10 @@ static int add_send_object(FILE *fp) { int st; - char otype[32], oname[128]; + char thismod[128], otype[32], oname[128]; int n; - if (fscanf(fp, "%*s %s %s", otype, oname) != 2) + if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 2) return(0); /* must have hit EOF! */ if (!strcmp(otype, "alias")) { fscanf(fp, "%*s"); /* skip alias */ @@ -1094,6 +1099,14 @@ add_send_object(FILE *fp) fputs(": cannot use source as a sender!\n", stderr); return(-1); } + if (strcmp(thismod, curmod)) { + if (curmod[0]) { + fputs(progname, stderr); + fputs(": warning - multiple modifiers in sender\n", + stderr); + } + strcpy(curmod, thismod); + } parse_params(&curparams, newparams); newparams[0] = '\0'; add_surface(st, oname, fp); /* read & store surface */ @@ -1102,9 +1115,9 @@ add_send_object(FILE *fp) /* else skip arguments */ if (!fscanf(fp, "%d", &n)) return(0); while (n-- > 0) fscanf(fp, "%*s"); - if (!fscanf(fp, "%d", &n)) return; + if (!fscanf(fp, "%d", &n)) return(0); while (n-- > 0) fscanf(fp, "%*d"); - if (!fscanf(fp, "%d", &n)) return; + if (!fscanf(fp, "%d", &n)) return(0); while (n-- > 0) fscanf(fp, "%*f"); return(0); } @@ -1147,7 +1160,7 @@ load_scene(const char *inspec, int (*ocb)(FILE *)) strcat(newparams, inpbuf); continue; } - while ((c = getc(fp)) != EOF && c != '\n'); + while ((c = getc(fp)) != EOF && c != '\n') ; /* else skipping comment */ continue; } @@ -1172,7 +1185,7 @@ main(int argc, char *argv[]) { char fmtopt[6] = "-faa"; /* default output is ASCII */ char *xrs=NULL, *yrs=NULL, *ldopt=NULL; - int wantIrradiance = 0; + char *iropt = NULL; char *sendfn; char sampcntbuf[32], nsbinbuf[32]; FILE *rcfp; @@ -1180,11 +1193,22 @@ main(int argc, char *argv[]) int a, i; /* screen rcontrib options */ progname = argv[0]; - for (a = 1; a < argc-2 && argv[a][0] == '-'; a++) { - int na = 1; /* !! Keep consistent !! */ - switch (argv[a][1]) { + for (a = 1; a < argc-2; a++) { + int na; + /* check for argument expansion */ + while ((na = expandarg(&argc, &argv, a)) > 0) + ; + if (na < 0) { + fprintf(stderr, "%s: cannot expand '%s'\n", + progname, argv[a]); + return(1); + } + if (argv[a][0] != '-' || !argv[a][1]) + break; + na = 1; + switch (argv[a][1]) { /* !! Keep consistent !! */ case 'v': /* verbose mode */ - verbose = !verbose; + verbose = 1; na = 0; continue; case 'f': /* special case for -fo, -ff, etc. */ @@ -1221,13 +1245,14 @@ main(int argc, char *argv[]) na = 0; /* we re-add this later */ continue; case 'I': /* only for pass-through mode */ - wantIrradiance = 1; + case 'i': + iropt = argv[a]; na = 0; continue; - case 'V': /* options without arguments */ - case 'w': + case 'w': /* options without arguments */ + if (argv[a][2] != '+') verbose = -1; + case 'V': case 'u': - case 'i': case 'h': case 'r': break; @@ -1257,8 +1282,6 @@ main(int argc, char *argv[]) if (!argv[a][2]) goto userr; na = (argv[a][2] == 'e') | (argv[a][2] == 'a') ? 4 : 2; break; - case '\0': /* pass-through mode */ - goto done_opts; default: /* anything else is verbotten */ goto userr; } @@ -1268,16 +1291,15 @@ main(int argc, char *argv[]) while (--na) /* + arguments if any */ rcarg[nrcargs++] = argv[++a]; } -done_opts: if (a > argc-2) goto userr; /* check at end of options */ sendfn = argv[a++]; /* assign sender & receiver inputs */ if (sendfn[0] == '-') { /* user wants pass-through mode? */ if (sendfn[1]) goto userr; sendfn = NULL; - if (wantIrradiance) { + if (iropt) { CHECKARGC(1); - rcarg[nrcargs++] = "-I"; + rcarg[nrcargs++] = iropt; } if (xrs) { CHECKARGC(2); @@ -1295,9 +1317,9 @@ done_opts: } if (sampcnt <= 0) sampcnt = 1; } else { /* else in sampling mode */ - if (wantIrradiance) { + if (iropt) { fputs(progname, stderr); - fputs(": -I supported for pass-through only\n", stderr); + fputs(": -i, -I supported for pass-through only\n", stderr); return(1); } fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; @@ -1319,6 +1341,7 @@ done_opts: return(my_exec(rcarg)); /* rcontrib does everything */ } clear_params(&curparams, 0); /* else load sender surface & params */ + curmod[0] = '\0'; if (load_scene(sendfn, add_send_object) < 0) return(1); if ((nsbins = prepare_sampler()) <= 0) @@ -1336,7 +1359,7 @@ done_opts: #ifdef getc_unlocked flockfile(rcfp); #endif - if (verbose) { + if (verbose > 0) { fprintf(stderr, "%s: sampling %d directions", progname, nsbins); if (curparams.nsurfs > 1) fprintf(stderr, " (%d elements)\n", curparams.nsurfs); @@ -1346,11 +1369,11 @@ done_opts: for (i = 0; i < nsbins; i++) /* send rcontrib ray samples */ if (!(*curparams.sample_basis)(&curparams, i, rcfp)) return(1); - return(pclose(rcfp) == 0); /* all finished! */ + return(pclose(rcfp) < 0); /* all finished! */ userr: if (a < argc-2) fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]); - fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [system.rad ..]\n", + fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [-i system.oct] [system.rad ..]\n", progname); return(1); }