--- ray/src/util/rfluxmtx.c 2016/08/20 03:43:24 2.41 +++ ray/src/util/rfluxmtx.c 2020/03/02 22:00:05 2.51 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rfluxmtx.c,v 2.41 2016/08/20 03:43:24 greg Exp $"; +static const char RCSid[] = "$Id: rfluxmtx.c,v 2.51 2020/03/02 22:00:05 greg Exp $"; #endif /* * Calculate flux transfer matrix or matrices using rcontrib @@ -11,14 +11,16 @@ static const char RCSid[] = "$Id: rfluxmtx.c,v 2.41 20 #include #include "rtio.h" #include "rtmath.h" -#include "paths.h" +#include "rtprocess.h" #include "bsdf.h" #include "bsdf_m.h" #include "random.h" #include "triangulate.h" #include "platform.h" -#define MAXRCARG 512 +#ifndef MAXRCARG +#define MAXRCARG 10000 +#endif char *progname; /* global argv[0] */ @@ -138,12 +140,24 @@ oconv_command(int ac, char *av[]) if (ac-- <= 0) return(NULL); - while (ac-- > 0) { - strcpy(cp, *av++); - while (*cp) cp++; - *cp++ = ' '; - if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) + if (verbose < 0) { /* turn off warnings */ + strcpy(cp, "-w "); + cp += 3; + } + while (ac-- > 0) { /* copy each argument */ + int len = strlen(*av); + if (cp+len+4 >= oconvbuf+sizeof(oconvbuf)) goto overrun; + if (matchany(*av, SPECIALS)) { + *cp++ = QUOTCHAR; + strcpy(cp, *av++); + cp += len; + *cp++ = QUOTCHAR; + } else { + strcpy(cp, *av++); + cp += len; + } + *cp++ = ' '; } /* receiver goes last */ if (matchany(recv, SPECIALS)) { @@ -164,6 +178,8 @@ overrun: exit(1); } +#if defined(_WIN32) || defined(_WIN64) + /* Open a pipe to/from a command given as an argument list */ static FILE * popen_arglist(char *av[], char *mode) @@ -181,7 +197,8 @@ popen_arglist(char *av[], char *mode) return(popen(cmd, mode)); } -#if defined(_WIN32) || defined(_WIN64) +#define pclose_al pclose + /* Execute system command (Windows version) */ static int my_exec(char *av[]) @@ -197,7 +214,56 @@ my_exec(char *av[]) fprintf(stderr, "%s: running: %s\n", progname, cmd); return(system(cmd)); } -#else + +#else /* UNIX */ + +static SUBPROC rt_proc = SP_INACTIVE; /* we only support one of these */ + +/* Open a pipe to a command using an argument list */ +static FILE * +popen_arglist(char *av[], char *mode) +{ + int fd; + + if (rt_proc.pid > 0) { + fprintf(stderr, "%s: only one i/o pipe at a time!\n", progname); + return(NULL); + } + if (verbose > 0) { + char cmd[4096]; + if (!convert_commandline(cmd, sizeof(cmd), av)) + strcpy(cmd, "COMMAND TOO LONG TO SHOW"); + fprintf(stderr, "%s: opening pipe %s: %s\n", + progname, (*mode=='w') ? "to" : "from", cmd); + } + if (*mode == 'w') { + fd = rt_proc.w = dup(fileno(stdout)); + rt_proc.flags |= PF_FILT_OUT; + } else if (*mode == 'r') { + fd = rt_proc.r = dup(fileno(stdin)); + rt_proc.flags |= PF_FILT_INP; + } + if (fd < 0 || open_process(&rt_proc, av) <= 0) { + perror(av[0]); + return(NULL); + } + return(fdopen(fd, mode)); +} + +/* Close command pipe (returns -1 on error to match pclose) */ +static int +pclose_al(FILE *fp) +{ + int prob = (fclose(fp) == EOF); + + if (rt_proc.pid <= 0) + return(-1); + + prob |= (close_process(&rt_proc) != 0); + + return(-prob); +} + /* Execute system command in our stead (Unix version) */ static int my_exec(char *av[]) @@ -218,6 +284,7 @@ my_exec(char *av[]) perror(compath); return(1); } + #endif /* Get normalized direction vector from string specification */ @@ -379,7 +446,7 @@ finish_receiver(void) curparams.vup[1] = 1; } /* determine sample type/bin */ - if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') { + if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) { sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)", curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]); binv = savqstr(sbuf); @@ -655,7 +722,7 @@ sample_origin(PARAMS *p, FVECT orig, const FVECT rdir, projsa[i] = -DOT(sp->snrm, rdir) * sp->area; tarea += projsa[i] *= (double)(projsa[i] > FTINY); } - if (tarea <= FTINY) { /* wrong side of sender? */ + if (tarea < 0) { /* wrong side of sender? */ fputs(progname, stderr); fputs(": internal - sample behind all sender elements!\n", stderr); @@ -756,11 +823,13 @@ sample_reinhart(PARAMS *p, int b, FILE *fp) } while (n--) { /* stratified sampling */ SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); + if (row >= RowMax-1) /* avoid crowding at zenith */ + samp3[1] *= samp3[1]; alt = (row+samp3[1])*RAH; azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row); duvw[2] = cos(alt); /* measured from horizon */ duvw[0] = tsin(azi)*duvw[2]; - duvw[1] = tcos(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] - @@ -867,7 +936,7 @@ prepare_sampler(void) curparams.udir[1] *= -1.; curparams.udir[2] *= -1.; } - if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') + if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) curparams.sample_basis = sample_uniform; else if (tolower(curparams.hemis[0]) == 's' && tolower(curparams.hemis[1]) == 'c') @@ -1228,7 +1297,9 @@ main(int argc, char *argv[]) na = 0; continue; case 'w': /* options without arguments */ - if (argv[a][2] != '+') verbose = -1; + if (!argv[a][2] || strchr("+1tTyY", argv[a][2]) == NULL) + verbose = -1; + break; case 'V': case 'u': case 'h': @@ -1305,7 +1376,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); @@ -1352,7 +1427,7 @@ main(int argc, char *argv[]) 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_al(rcfp) < 0); /* all finished! */ userr: if (a < argc-2) fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]);