--- ray/src/util/rfluxmtx.c 2017/02/07 19:53:59 2.43 +++ ray/src/util/rfluxmtx.c 2020/09/09 21:28:19 2.53 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rfluxmtx.c,v 2.43 2017/02/07 19:53:59 greg Exp $"; +static const char RCSid[] = "$Id: rfluxmtx.c,v 2.53 2020/09/09 21:28:19 greg Exp $"; #endif /* * Calculate flux transfer matrix or matrices using rcontrib @@ -11,14 +11,16 @@ static const char RCSid[] = "$Id: rfluxmtx.c,v 2.43 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] */ @@ -139,15 +141,23 @@ oconv_command(int ac, char *av[]) if (ac-- <= 0) return(NULL); if (verbose < 0) { /* turn off warnings */ - strcpy(cp, "-w- "); - cp += 4; + strcpy(cp, "-w "); + cp += 3; } - while (ac-- > 0) { - strcpy(cp, *av++); - while (*cp) cp++; - *cp++ = ' '; - if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) + 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)) { @@ -168,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) @@ -185,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[]) @@ -201,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[]) @@ -222,6 +284,7 @@ my_exec(char *av[]) perror(compath); return(1); } + #endif /* Get normalized direction vector from string specification */ @@ -383,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); @@ -657,9 +720,9 @@ sample_origin(PARAMS *p, FVECT orig, const FVECT rdir, /* compute projected areas */ for (i = 0, sp = p->slist; sp != NULL; i++, sp = sp->next) { projsa[i] = -DOT(sp->snrm, rdir) * sp->area; - tarea += projsa[i] *= (double)(projsa[i] > FTINY); + tarea += projsa[i] *= (double)(projsa[i] > 0); } - if (tarea <= FTINY) { /* wrong side of sender? */ + if (tarea < FTINY*FTINY) { /* wrong side of sender? */ fputs(progname, stderr); fputs(": internal - sample behind all sender elements!\n", stderr); @@ -760,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] - @@ -871,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') @@ -932,7 +997,7 @@ finish_polygon(SURF *p) VCOPY(e1, e2); } p->area = normalize(p->snrm)*0.5; - return(p->area > FTINY); + return(p->area > FTINY*FTINY); } /* Add a surface to our current parameters */ @@ -995,7 +1060,7 @@ add_surface(int st, const char *oname, FILE *fp) snew->area *= PI*snew->area; break; } - if ((snew->area <= FTINY) & (verbose >= 0)) { + if ((snew->area <= FTINY*FTINY) & (verbose >= 0)) { fprintf(stderr, "%s: warning - zero area for surface '%s'\n", progname, oname); free(snew); @@ -1232,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': @@ -1241,6 +1308,7 @@ main(int argc, char *argv[]) case 'n': /* options with 1 argument */ case 's': case 'o': + case 't': na = 2; break; case 'b': /* special case */ @@ -1360,7 +1428,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]);