--- ray/src/util/rfluxmtx.c 2016/03/04 00:21:21 2.35 +++ ray/src/util/rfluxmtx.c 2025/10/22 20:00:17 2.65 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rfluxmtx.c,v 2.35 2016/03/04 00:21:21 greg Exp $"; +static const char RCSid[] = "$Id: rfluxmtx.c,v 2.65 2025/10/22 20:00:17 greg Exp $"; #endif /* * Calculate flux transfer matrix or matrices using rcontrib @@ -11,18 +11,21 @@ static const char RCSid[] = "$Id: rfluxmtx.c,v 2.35 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] */ +#define NOWARN 1 +#define VERBO 2 -int verbose = 0; /* verbose mode (< 0 no warnings) */ +int verbose = 0; /* verbose/warning mode */ char *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"}; int nrcargs = 2; @@ -39,9 +42,10 @@ char *shirchiufn = "disk2square.cal"; char *kfullfn = "klems_full.cal"; char *khalffn = "klems_half.cal"; char *kquarterfn = "klems_quarter.cal"; +char *ciefn = "cieskyscan.cal"; /* string indicating parameters */ -const char PARAMSTART[] = "@rfluxmtx"; +#define PARAMSTART "@rfluxmtx" /* surface type IDs */ #define ST_NONE 0 @@ -64,7 +68,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 */ @@ -106,6 +110,7 @@ clear_params(PARAMS *p, int reset_only) free(sdel); } if (reset_only) { + p->slist = NULL; p->nsurfs = 0; memset(p->nrm, 0, sizeof(FVECT)); memset(p->vup, 0, sizeof(FVECT)); @@ -138,12 +143,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 & NOWARN) { /* warnings off? */ + 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 +181,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) @@ -175,13 +194,14 @@ popen_arglist(char *av[], char *mode) fputs(": command line too long in popen_arglist()\n", stderr); return(NULL); } - if (verbose > 0) + if (verbose & VERBO) fprintf(stderr, "%s: opening pipe %s: %s\n", progname, (*mode=='w') ? "to" : "from", cmd); return(popen(cmd, mode)); } -#ifdef _WIN32 +#define pclose_al pclose + /* Execute system command (Windows version) */ static int my_exec(char *av[]) @@ -193,11 +213,60 @@ my_exec(char *av[]) fputs(": command line too long in my_exec()\n", stderr); return(1); } - if (verbose > 0) + if (verbose & VERBO) 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 & VERBO) { + 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[]) @@ -208,7 +277,7 @@ my_exec(char *av[]) fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]); return(1); } - if (verbose > 0) { + if (verbose & VERBO) { char cmd[4096]; if (!convert_commandline(cmd, sizeof(cmd), av)) strcpy(cmd, "COMMAND TOO LONG TO SHOW"); @@ -218,6 +287,7 @@ my_exec(char *av[]) perror(compath); return(1); } + #endif /* Get normalized direction vector from string specification */ @@ -379,10 +449,13 @@ finish_receiver(void) curparams.vup[1] = 1; } /* determine sample type/bin */ - 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); + if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) { + if (curparams.slist->styp != ST_SOURCE) { + sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)", + curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]); + binv = savqstr(sbuf); + } else + binv = "0"; nbins = "1"; /* uniform sampling -- one bin */ uniform = 1; } else if (tolower(curparams.hemis[0]) == 's' && @@ -432,6 +505,15 @@ finish_receiver(void) calfn = kquarterfn; kquarterfn = NULL; binf = "kqbin"; nbins = "Nkqbins"; + } else if (!strcasecmp(curparams.hemis, "cie")) { + calfn = ciefn; ciefn = NULL; + sprintf(sbuf, "rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", + curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], + curparams.vup[0], curparams.vup[1], curparams.vup[2], + curparams.sign); + params = savqstr(sbuf); + binv = "cbin"; + nbins = "Ncbins"; } else { fprintf(stderr, "%s: unrecognized hemisphere sampling: h=%s\n", progname, curparams.hemis); @@ -441,10 +523,10 @@ finish_receiver(void) sprintf(sbuf, "RHS=%c1", curparams.sign); params = savqstr(sbuf); } - if (!uniform & (curparams.slist->styp == ST_SOURCE)) { + if (!uniform) { SURF *sp; for (sp = curparams.slist; sp != NULL; sp = sp->next) - if (fabs(sp->area - PI) > 1e-3) { + if (sp->styp == ST_SOURCE && fabs(sp->area - PI) > 1e-3) { fprintf(stderr, "%s: source '%s' must be 180-degrees\n", progname, sp->sname); exit(1); @@ -602,7 +684,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]); @@ -643,15 +725,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); @@ -669,8 +755,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? */ @@ -678,7 +763,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] + @@ -686,7 +771,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); @@ -698,8 +783,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? */ @@ -707,7 +791,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--; ) @@ -716,7 +800,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); @@ -752,11 +836,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] - @@ -764,7 +850,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); @@ -815,7 +901,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); @@ -823,56 +909,56 @@ sample_klems(PARAMS *p, int b, FILE *fp) /* Prepare hemisphere basis sampler that will send rays to rcontrib */ static int -prepare_sampler(void) +prepare_sampler(PARAMS *p) { - if (curparams.slist == NULL) { /* missing sample surface! */ + if (p->slist == NULL) { /* missing sample surface! */ fputs(progname, stderr); fputs(": no sender surface!\n", stderr); return(-1); } /* misplaced output file spec. */ - if ((curparams.outfn != NULL) & (verbose >= 0)) + if ((p->outfn != NULL) & !(verbose & NOWARN)) fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n", - progname, curparams.outfn); + progname, p->outfn); /* check/set basis hemisphere */ - if (!curparams.hemis[0]) { + if (!p->hemis[0]) { fputs(progname, stderr); fputs(": missing sender sampling type!\n", stderr); return(-1); } - if (normalize(curparams.nrm) == 0) { + if (normalize(p->nrm) == 0) { fputs(progname, stderr); fputs(": undefined normal for sender sampling\n", stderr); return(-1); } - if (normalize(curparams.vup) == 0) { - if (fabs(curparams.nrm[2]) < .7) - curparams.vup[2] = 1; + if (normalize(p->vup) == 0) { + if (fabs(p->nrm[2]) < .7) + p->vup[2] = 1; else - curparams.vup[1] = 1; + p->vup[1] = 1; } - fcross(curparams.udir, curparams.vup, curparams.nrm); - if (normalize(curparams.udir) == 0) { + fcross(p->udir, p->vup, p->nrm); + if (normalize(p->udir) == 0) { fputs(progname, stderr); fputs(": up vector coincides with sender normal\n", stderr); return(-1); } - 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.; + fcross(p->vdir, p->nrm, p->udir); + if (p->sign == '-') { /* left-handed coordinate system? */ + p->udir[0] *= -1.; + p->udir[1] *= -1.; + p->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' && - tolower(curparams.hemis[1]) == 'c') - curparams.sample_basis = sample_shirchiu; - else if ((tolower(curparams.hemis[0]) == 'r') | - (tolower(curparams.hemis[0]) == 't')) - curparams.sample_basis = sample_reinhart; - else if (tolower(curparams.hemis[0]) == 'k') { - switch (curparams.hemis[1]) { + if ((tolower(p->hemis[0]) == 'u') | (p->hemis[0] == '1')) + p->sample_basis = sample_uniform; + else if (tolower(p->hemis[0]) == 's' && + tolower(p->hemis[1]) == 'c') + p->sample_basis = sample_shirchiu; + else if ((tolower(p->hemis[0]) == 'r') | + (tolower(p->hemis[0]) == 't')) + p->sample_basis = sample_reinhart; + else if (tolower(p->hemis[0]) == 'k') { + switch (p->hemis[1]) { case '1': case '2': case '4': @@ -880,28 +966,28 @@ prepare_sampler(void) case 'f': case 'F': case '\0': - curparams.hemis[1] = '1'; + p->hemis[1] = '1'; break; case 'h': case 'H': - curparams.hemis[1] = '2'; + p->hemis[1] = '2'; break; case 'q': case 'Q': - curparams.hemis[1] = '4'; + p->hemis[1] = '4'; break; default: goto unrecognized; } - curparams.hemis[2] = '\0'; - curparams.sample_basis = sample_klems; + p->hemis[2] = '\0'; + p->sample_basis = sample_klems; } else goto unrecognized; /* return number of bins */ - return((*curparams.sample_basis)(&curparams,0,NULL)); + return((*p->sample_basis)(p,0,NULL)); unrecognized: fprintf(stderr, "%s: unrecognized sender sampling: h=%s\n", - progname, curparams.hemis); + progname, p->hemis); return(-1); } @@ -924,7 +1010,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 */ @@ -942,7 +1028,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); @@ -987,7 +1073,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 & NOWARN)) { fprintf(stderr, "%s: warning - zero area for surface '%s'\n", progname, oname); free(snew); @@ -1024,12 +1110,6 @@ add_recv_object(FILE *fp) } /* is it a new receiver? */ if ((st = surf_type(otype)) != ST_NONE) { - if (curparams.slist != NULL && (st == ST_SOURCE) ^ - (curparams.slist->styp == ST_SOURCE)) { - fputs(progname, stderr); - fputs(": cannot mix source/non-source receivers!\n", stderr); - return(-1); - } if (strcmp(thismod, curmod)) { if (curmod[0]) { /* output last receiver? */ finish_receiver(); @@ -1165,8 +1245,9 @@ main(int argc, char *argv[]) FILE *rcfp; int nsbins; int a, i; + /* set global progname */ + fixargv0(argv[0]); /* screen rcontrib options */ - progname = argv[0]; for (a = 1; a < argc-2; a++) { int na; /* check for argument expansion */ @@ -1182,7 +1263,7 @@ main(int argc, char *argv[]) na = 1; switch (argv[a][1]) { /* !! Keep consistent !! */ case 'v': /* verbose mode */ - verbose = 1; + verbose ^= VERBO; na = 0; continue; case 'f': /* special case for -fo, -ff, etc. */ @@ -1212,19 +1293,35 @@ main(int argc, char *argv[]) yrs = argv[++a]; na = 0; continue; - case 'c': /* number of samples */ - sampcnt = atoi(argv[++a]); - if (sampcnt <= 0) - goto userr; - na = 0; /* we re-add this later */ - continue; + case 'c': /* spectral sampling or count */ + switch (argv[a][2]) { + case 's': + na = 2; + break; + case 'w': + na = 3; + break; + case '\0': + sampcnt = atoi(argv[++a]); + if (sampcnt <= 0) + goto userr; + na = 0; /* we re-add this later */ + continue; + } + break; case 'I': /* only for pass-through mode */ case 'i': iropt = argv[a]; na = 0; continue; case 'w': /* options without arguments */ - if (argv[a][2] != '+') verbose = -1; + if (!argv[a][2]) + verbose ^= NOWARN; + else if (strchr("+1tTyY", argv[a][2]) != NULL) + verbose &= ~NOWARN; + else + verbose |= NOWARN; + break; case 'V': case 'u': case 'h': @@ -1233,6 +1330,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 */ @@ -1301,7 +1399,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); @@ -1323,7 +1425,7 @@ main(int argc, char *argv[]) curmod[0] = '\0'; if (load_scene(sendfn, add_send_object) < 0) return(1); - if ((nsbins = prepare_sampler()) <= 0) + if ((nsbins = prepare_sampler(&curparams)) <= 0) return(1); CHECKARGC(3); /* add row count and octree */ rcarg[nrcargs++] = "-y"; @@ -1338,7 +1440,7 @@ main(int argc, char *argv[]) #ifdef getc_unlocked flockfile(rcfp); #endif - if (verbose > 0) { + if (verbose & VERBO) { fprintf(stderr, "%s: sampling %d directions", progname, nsbins); if (curparams.nsurfs > 1) fprintf(stderr, " (%d elements)\n", curparams.nsurfs); @@ -1348,10 +1450,10 @@ 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]); + fprintf(stderr, "%s: unsupported option '%s'\n", progname, argv[a]); fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [-i system.oct] [system.rad ..]\n", progname); return(1);