--- ray/src/util/rfluxmtx.c 2016/02/02 01:43:24 2.33 +++ ray/src/util/rfluxmtx.c 2021/12/15 01:38:50 2.54 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rfluxmtx.c,v 2.33 2016/02/02 01:43:24 greg Exp $"; +static const char RCSid[] = "$Id: rfluxmtx.c,v 2.54 2021/12/15 01:38:50 greg Exp $"; #endif /* * Calculate flux transfer matrix or matrices using rcontrib @@ -18,22 +18,10 @@ static const char RCSid[] = "$Id: rfluxmtx.c,v 2.33 20 #include "triangulate.h" #include "platform.h" -#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ -#undef getc -#define getc getc_unlocked +#ifndef MAXRCARG +#define MAXRCARG 10000 #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] */ int verbose = 0; /* verbose mode (< 0 no warnings) */ @@ -78,7 +66,7 @@ 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 */ @@ -142,20 +130,6 @@ surf_type(const char *otype) return(ST_NONE); } -/* 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); -} - /* Add arguments to oconv command */ static char * oconv_command(int ac, char *av[]) @@ -166,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)) { @@ -192,39 +178,8 @@ overrun: exit(1); } -/* 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; +#if defined(_WIN32) || defined(_WIN64) - for (cp = cmd; *av != NULL; av++) { - const int n = strlen(*av); - if (cp+n >= cmd+(len-3)) - return(NULL); - if (matchany(*av, SPECIALS)) { - const int quote = -#ifdef ALTQUOT - strchr(*av, 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) @@ -242,7 +197,8 @@ popen_arglist(char *av[], char *mode) return(popen(cmd, mode)); } -#ifdef _WIN32 +#define pclose_al pclose + /* Execute system command (Windows version) */ static int my_exec(char *av[]) @@ -258,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[]) @@ -279,6 +284,7 @@ my_exec(char *av[]) perror(compath); return(1); } + #endif /* Get normalized direction vector from string specification */ @@ -440,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); @@ -663,7 +669,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]); @@ -704,15 +710,19 @@ 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 */ 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); @@ -730,8 +740,7 @@ sample_uniform(PARAMS *p, int b, FILE *fp) { int n = sampcnt; double samp3[3]; - double duvw[3]; - FVECT orig_dir[2]; + FVECT duvw, orig_dir[2]; int i; if (fp == NULL) /* just requesting number of bins? */ @@ -739,7 +748,7 @@ sample_uniform(PARAMS *p, int b, FILE *fp) while (n--) { /* stratified hemisphere sampling */ SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); - SDsquare2disk(duvw, samp3[1], samp3[2]); + square2disk(duvw, samp3[1], samp3[2]); duvw[2] = -sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]); for (i = 3; i--; ) orig_dir[1][i] = duvw[0]*p->udir[i] + @@ -747,7 +756,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); @@ -759,8 +768,7 @@ sample_shirchiu(PARAMS *p, int b, FILE *fp) { int n = sampcnt; double samp3[3]; - double duvw[3]; - FVECT orig_dir[2]; + FVECT duvw, orig_dir[2]; int i; if (fp == NULL) /* just requesting number of bins? */ @@ -768,7 +776,7 @@ sample_shirchiu(PARAMS *p, int b, FILE *fp) while (n--) { /* stratified sampling */ SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); - SDsquare2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz, + square2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz, (b%p->hsiz + samp3[2])/curparams.hsiz); duvw[2] = sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]); for (i = 3; i--; ) @@ -777,7 +785,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); @@ -813,11 +821,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] - @@ -825,7 +835,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); @@ -876,7 +886,7 @@ sample_klems(PARAMS *p, int b, FILE *fp) 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); @@ -924,7 +934,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') @@ -985,7 +995,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 */ @@ -1003,7 +1013,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); @@ -1048,7 +1058,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); @@ -1285,7 +1295,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': @@ -1294,6 +1306,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 */ @@ -1362,7 +1375,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); @@ -1409,7 +1426,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]);