--- ray/src/util/rfluxmtx.c 2015/02/18 06:18:38 2.23 +++ ray/src/util/rfluxmtx.c 2017/02/07 19:53:59 2.43 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rfluxmtx.c,v 2.23 2015/02/18 06:18:38 greg Exp $"; +static const char RCSid[] = "$Id: rfluxmtx.c,v 2.43 2017/02/07 19:53:59 greg Exp $"; #endif /* * Calculate flux transfer matrix or matrices using rcontrib @@ -11,27 +11,13 @@ static const char RCSid[] = "$Id: rfluxmtx.c,v 2.23 20 #include #include "rtio.h" #include "rtmath.h" -#include "rtprocess.h" +#include "paths.h" #include "bsdf.h" #include "bsdf_m.h" #include "random.h" #include "triangulate.h" #include "platform.h" -#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ -#undef getc -#define getc getc_unlocked -#endif - -#ifdef _WIN32 -#define SPECIALS " \t\"$*?" -#define QUOTCHAR '"' -#else -#define SPECIALS " \t\n'\"()${}*?[];|&" -#define QUOTCHAR '\'' -#define ALTQUOT '"' -#endif - #define MAXRCARG 512 char *progname; /* global argv[0] */ @@ -78,19 +64,20 @@ typedef struct { FVECT uva[2]; /* tangent axes */ int ntris; /* number of triangles */ struct ptri { - float afrac; /* fraction of total area */ + double afrac; /* fraction of total area */ short vndx[3]; /* vertex indices */ } tri[1]; /* triangle array (extends struct) */ } POLYTRIS; /* triangulated polygon */ typedef struct param_s { - char hemis[32]; /* hemispherical sampling spec. */ + char sign; /* '-' for axis reversal */ + char hemis[31]; /* hemispherical sampling spec. */ int hsiz; /* hemisphere basis size */ int nsurfs; /* number of surfaces */ SURF *slist; /* list of surfaces */ FVECT vup; /* up vector (zero if unset) */ FVECT nrm; /* average normal direction */ - FVECT udir, vdir; /* v-up tangent axes */ + FVECT udir, vdir; /* tangent axes */ char *outfn; /* output file name (receiver) */ int (*sample_basis)(struct param_s *p, int, FILE *); } PARAMS; /* sender/receiver parameters */ @@ -151,70 +138,36 @@ oconv_command(int ac, char *av[]) if (ac-- <= 0) return(NULL); + if (verbose < 0) { /* turn off warnings */ + strcpy(cp, "-w- "); + cp += 4; + } 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); - } + if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) + goto overrun; } - strcpy(cp, recv); /* receiver goes last */ + /* receiver goes last */ + if (matchany(recv, SPECIALS)) { + *cp++ = QUOTCHAR; + while (*recv) { + if (cp >= oconvbuf+(sizeof(oconvbuf)-3)) + goto overrun; + *cp++ = *recv++; + } + *cp++ = QUOTCHAR; + *cp = '\0'; + } else + strcpy(cp, recv); return(oconvbuf); +overrun: + fputs(progname, stderr); + fputs(": too many file arguments!\n", stderr); + exit(1); } -/* Check if any of the characters in str2 are found in str1 */ -static int -matchany(const char *str1, const char *str2) -{ - while (*str1) { - const char *cp = str2; - while (*cp) - if (*cp++ == *str1) - return(*str1); - ++str1; - } - return(0); -} - - -/* Convert a set of arguments into a command line for pipe() or system() */ -static char * -convert_commandline(char *cmd, const int len, char *av[]) -{ - int match; - char *cp; - - for (cp = cmd; *av != NULL; av++) { - const int n = strlen(*av); - if (cp+n >= cmd+(len-3)) { - fputs(progname, stderr); - return(NULL); - } - if ((match = matchany(*av, SPECIALS))) { - const int quote = -#ifdef ALTQUOT - (match == QUOTCHAR) ? ALTQUOT : -#endif - QUOTCHAR; - *cp++ = quote; - strcpy(cp, *av); - cp += n; - *cp++ = quote; - } else { - strcpy(cp, *av); - cp += n; - } - *cp++ = ' '; - } - if (cp <= cmd) - return(NULL); - *--cp = '\0'; - return(cmd); -} - /* Open a pipe to/from a command given as an argument list */ static FILE * popen_arglist(char *av[], char *mode) @@ -232,7 +185,7 @@ popen_arglist(char *av[], char *mode) return(popen(cmd, mode)); } -#ifdef _WIN32 +#if defined(_WIN32) || defined(_WIN64) /* Execute system command (Windows version) */ static int my_exec(char *av[]) @@ -317,6 +270,7 @@ parse_params(PARAMS *p, char *pargs) { char *cp = pargs; int nparams = 0; + int quot; int i; for ( ; ; ) { @@ -324,6 +278,10 @@ parse_params(PARAMS *p, char *pargs) case 'h': if (*cp++ != '=') break; + if ((*cp == '+') | (*cp == '-')) + p->sign = *cp++; + else + p->sign = '+'; p->hsiz = 0; i = 0; while (*cp && !isspace(*cp)) { @@ -349,14 +307,23 @@ parse_params(PARAMS *p, char *pargs) case 'o': if (*cp++ != '=') break; + quot = 0; + if ((*cp == '"') | (*cp == '\'')) + quot = *cp++; i = 0; - while (*cp && !isspace(*cp++)) - i++; + while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) { + i++; cp++; + } if (!i) break; - *--cp = '\0'; + if (!*cp) { + if (quot) + break; + cp[1] = '\0'; + } + *cp = '\0'; p->outfn = savqstr(cp-i); - *cp++ = ' '; + *cp++ = quot ? quot : ' '; ++nparams; continue; case ' ': @@ -431,20 +398,22 @@ finish_receiver(void) exit(1); } calfn = shirchiufn; shirchiufn = NULL; - sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g", + sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", curparams.hsiz, curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], - curparams.vup[0], curparams.vup[1], curparams.vup[2]); + curparams.vup[0], curparams.vup[1], curparams.vup[2], + curparams.sign); params = savqstr(sbuf); binv = "scbin"; nbins = "SCdim*SCdim"; } else if ((tolower(curparams.hemis[0]) == 'r') | (tolower(curparams.hemis[0]) == 't')) { calfn = reinhfn; reinhfn = NULL; - sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g", + sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", curparams.hsiz, curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], - curparams.vup[0], curparams.vup[1], curparams.vup[2]); + curparams.vup[0], curparams.vup[1], curparams.vup[2], + curparams.sign); params = savqstr(sbuf); binv = "rbin"; nbins = "Nrbins"; @@ -472,6 +441,10 @@ finish_receiver(void) progname, curparams.hemis); exit(1); } + if (tolower(curparams.hemis[0]) == 'k') { + sprintf(sbuf, "RHS=%c1", curparams.sign); + params = savqstr(sbuf); + } if (!uniform & (curparams.slist->styp == ST_SOURCE)) { SURF *sp; for (sp = curparams.slist; sp != NULL; sp = sp->next) @@ -519,7 +492,7 @@ make_axes(FVECT uva[2], const FVECT nrm) { int i; - if (!getperpendicular(uva[0], nrm)) { + if (!getperpendicular(uva[0], nrm, 1)) { fputs(progname, stderr); fputs(": bad surface normal in make_axes!\n", stderr); exit(1); @@ -633,7 +606,7 @@ ssamp_poly(FVECT orig, SURF *sp, double x) sp->priv = (void *)ptp; } /* pick triangle by partial area */ - for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++) + for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++) x -= ptp->tri[i].afrac; SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac); samp2[0] *= samp2[1] = sqrt(samp2[1]); @@ -663,7 +636,7 @@ sample_origin(PARAMS *p, FVECT orig, const FVECT rdir, /* special case for lone surface */ if (p->nsurfs == 1) { sp = p->slist; - if (DOT(sp->snrm, rdir) >= -FTINY) { + if (DOT(sp->snrm, rdir) >= FTINY) { fprintf(stderr, "%s: internal - sample behind sender '%s'\n", progname, sp->sname); @@ -674,7 +647,11 @@ sample_origin(PARAMS *p, FVECT orig, const FVECT rdir, if (p->nsurfs > nall) { /* (re)allocate surface area cache */ if (projsa) free(projsa); projsa = (double *)malloc(sizeof(double)*p->nsurfs); - if (!projsa) return(0); + if (projsa == NULL) { + fputs(progname, stderr); + fputs(": out of memory in sample_origin!\n", stderr); + exit(1); + } nall = p->nsurfs; } /* compute projected areas */ @@ -717,7 +694,7 @@ sample_uniform(PARAMS *p, int b, FILE *fp) duvw[2]*p->nrm[i] ; if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) return(0); - if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) + if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) return(0); } return(1); @@ -747,7 +724,7 @@ sample_shirchiu(PARAMS *p, int b, FILE *fp) duvw[2]*p->nrm[i] ; if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) return(0); - if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) + if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) return(0); } return(1); @@ -795,7 +772,7 @@ sample_reinhart(PARAMS *p, int b, FILE *fp) duvw[2]*p->nrm[i] ; if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) return(0); - if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) + if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) return(0); } return(1); @@ -838,15 +815,15 @@ sample_klems(PARAMS *p, int b, FILE *fp) while (n--) { /* stratified sampling */ SDmultiSamp(samp2, 2, (n+frandom())/sampcnt); - if (!bi_getvec(duvw, b+samp2[1], kbasis[bi])) + if (!fo_getvec(duvw, b+samp2[1], kbasis[bi])) return(0); for (i = 3; i--; ) - orig_dir[1][i] = duvw[0]*p->udir[i] + - duvw[1]*p->vdir[i] + + orig_dir[1][i] = -duvw[0]*p->udir[i] - + duvw[1]*p->vdir[i] - duvw[2]*p->nrm[i] ; if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0])) return(0); - if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) + if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) return(0); } return(1); @@ -882,13 +859,18 @@ prepare_sampler(void) else curparams.vup[1] = 1; } - VCROSS(curparams.udir, curparams.vup, curparams.nrm); + fcross(curparams.udir, curparams.vup, curparams.nrm); if (normalize(curparams.udir) == 0) { fputs(progname, stderr); fputs(": up vector coincides with sender normal\n", stderr); return(-1); } - VCROSS(curparams.vdir, curparams.nrm, curparams.udir); + fcross(curparams.vdir, curparams.nrm, curparams.udir); + if (curparams.sign == '-') { /* left-handed coordinate system? */ + curparams.udir[0] *= -1.; + curparams.udir[1] *= -1.; + curparams.udir[2] *= -1.; + } if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') curparams.sample_basis = sample_uniform; else if (tolower(curparams.hemis[0]) == 's' && @@ -968,7 +950,7 @@ add_surface(int st, const char *oname, FILE *fp) snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1)); if (snew == NULL) { fputs(progname, stderr); - fputs(": out of memory!\n", stderr); + fputs(": out of memory in add_surface!\n", stderr); exit(1); } strncpy(snew->sname, oname, sizeof(snew->sname)-1); @@ -1083,10 +1065,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) != 3) return(0); /* must have hit EOF! */ if (!strcmp(otype, "alias")) { fscanf(fp, "%*s"); /* skip alias */ @@ -1099,6 +1081,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 */ @@ -1268,7 +1258,12 @@ main(int argc, char *argv[]) if (argv[a][2] != 'v') na = 2; break; case 'a': /* special case */ - na = (argv[a][2] == 'v') ? 4 : 2; + if (argv[a][2] == 'p') { + na = 2; /* photon map [+ bandwidth(s)] */ + if (a < argc-3 && atoi(argv[a+1]) > 0) + na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0); + } else + na = (argv[a][2] == 'v') ? 4 : 2; break; case 'm': /* special case */ if (!argv[a][2]) goto userr; @@ -1314,7 +1309,11 @@ main(int argc, char *argv[]) fputs(": -i, -I supported for pass-through only\n", stderr); return(1); } - fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; +#ifdef SMLFLT + fmtopt[2] = 'f'; +#else + fmtopt[2] = 'd'; +#endif if (sampcnt <= 0) sampcnt = 10000; } sprintf(sampcntbuf, "%d", sampcnt); @@ -1333,6 +1332,7 @@ main(int argc, char *argv[]) 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)