--- ray/src/util/rfluxmtx.c 2018/10/01 15:51:48 2.46 +++ ray/src/util/rfluxmtx.c 2020/09/07 04:06:17 2.52 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rfluxmtx.c,v 2.46 2018/10/01 15:51:48 greg Exp $"; +static const char RCSid[] = "$Id: rfluxmtx.c,v 2.52 2020/09/07 04:06:17 greg Exp $"; #endif /* * Calculate flux transfer matrix or matrices using rcontrib @@ -11,7 +11,7 @@ static const char RCSid[] = "$Id: rfluxmtx.c,v 2.46 20 #include #include "rtio.h" #include "rtmath.h" -#include "paths.h" +#include "rtprocess.h" #include "bsdf.h" #include "bsdf_m.h" #include "random.h" @@ -141,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)) { @@ -170,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) @@ -187,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[]) @@ -203,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[]) @@ -224,6 +284,7 @@ my_exec(char *av[]) perror(compath); return(1); } + #endif /* Get normalized direction vector from string specification */ @@ -659,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); @@ -936,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 */ @@ -999,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); @@ -1236,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': @@ -1364,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]);