| 11 |
|
#include <stdlib.h> |
| 12 |
|
#include "rtio.h" |
| 13 |
|
#include "rtmath.h" |
| 14 |
< |
#include "paths.h" |
| 14 |
> |
#include "rtprocess.h" |
| 15 |
|
#include "bsdf.h" |
| 16 |
|
#include "bsdf_m.h" |
| 17 |
|
#include "random.h" |
| 18 |
|
#include "triangulate.h" |
| 19 |
|
#include "platform.h" |
| 20 |
|
|
| 21 |
< |
#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ |
| 22 |
< |
#undef getc |
| 23 |
< |
#define getc getc_unlocked |
| 21 |
> |
#ifndef MAXRCARG |
| 22 |
> |
#define MAXRCARG 10000 |
| 23 |
|
#endif |
| 24 |
|
|
| 26 |
– |
#define MAXRCARG 512 |
| 27 |
– |
|
| 28 |
– |
char *progname; /* global argv[0] */ |
| 29 |
– |
|
| 25 |
|
int verbose = 0; /* verbose mode (< 0 no warnings) */ |
| 26 |
|
|
| 27 |
|
char *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"}; |
| 39 |
|
char *kfullfn = "klems_full.cal"; |
| 40 |
|
char *khalffn = "klems_half.cal"; |
| 41 |
|
char *kquarterfn = "klems_quarter.cal"; |
| 42 |
+ |
char *ciefn = "cieskyscan.cal"; |
| 43 |
|
|
| 44 |
|
/* string indicating parameters */ |
| 45 |
|
const char PARAMSTART[] = "@rfluxmtx"; |
| 65 |
|
FVECT uva[2]; /* tangent axes */ |
| 66 |
|
int ntris; /* number of triangles */ |
| 67 |
|
struct ptri { |
| 68 |
< |
float afrac; /* fraction of total area */ |
| 68 |
> |
double afrac; /* fraction of total area */ |
| 69 |
|
short vndx[3]; /* vertex indices */ |
| 70 |
|
} tri[1]; /* triangle array (extends struct) */ |
| 71 |
|
} POLYTRIS; /* triangulated polygon */ |
| 139 |
|
|
| 140 |
|
if (ac-- <= 0) |
| 141 |
|
return(NULL); |
| 142 |
< |
while (ac-- > 0) { |
| 143 |
< |
strcpy(cp, *av++); |
| 144 |
< |
while (*cp) cp++; |
| 145 |
< |
*cp++ = ' '; |
| 146 |
< |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) |
| 142 |
> |
if (verbose < 0) { /* turn off warnings */ |
| 143 |
> |
strcpy(cp, "-w "); |
| 144 |
> |
cp += 3; |
| 145 |
> |
} |
| 146 |
> |
while (ac-- > 0) { /* copy each argument */ |
| 147 |
> |
int len = strlen(*av); |
| 148 |
> |
if (cp+len+4 >= oconvbuf+sizeof(oconvbuf)) |
| 149 |
|
goto overrun; |
| 150 |
+ |
if (matchany(*av, SPECIALS)) { |
| 151 |
+ |
*cp++ = QUOTCHAR; |
| 152 |
+ |
strcpy(cp, *av++); |
| 153 |
+ |
cp += len; |
| 154 |
+ |
*cp++ = QUOTCHAR; |
| 155 |
+ |
} else { |
| 156 |
+ |
strcpy(cp, *av++); |
| 157 |
+ |
cp += len; |
| 158 |
+ |
} |
| 159 |
+ |
*cp++ = ' '; |
| 160 |
|
} |
| 161 |
|
/* receiver goes last */ |
| 162 |
|
if (matchany(recv, SPECIALS)) { |
| 177 |
|
exit(1); |
| 178 |
|
} |
| 179 |
|
|
| 180 |
+ |
#if defined(_WIN32) || defined(_WIN64) |
| 181 |
+ |
|
| 182 |
|
/* Open a pipe to/from a command given as an argument list */ |
| 183 |
|
static FILE * |
| 184 |
|
popen_arglist(char *av[], char *mode) |
| 196 |
|
return(popen(cmd, mode)); |
| 197 |
|
} |
| 198 |
|
|
| 199 |
< |
#ifdef _WIN32 |
| 199 |
> |
#define pclose_al pclose |
| 200 |
> |
|
| 201 |
|
/* Execute system command (Windows version) */ |
| 202 |
|
static int |
| 203 |
|
my_exec(char *av[]) |
| 213 |
|
fprintf(stderr, "%s: running: %s\n", progname, cmd); |
| 214 |
|
return(system(cmd)); |
| 215 |
|
} |
| 216 |
< |
#else |
| 216 |
> |
|
| 217 |
> |
#else /* UNIX */ |
| 218 |
> |
|
| 219 |
> |
static SUBPROC rt_proc = SP_INACTIVE; /* we only support one of these */ |
| 220 |
> |
|
| 221 |
> |
/* Open a pipe to a command using an argument list */ |
| 222 |
> |
static FILE * |
| 223 |
> |
popen_arglist(char *av[], char *mode) |
| 224 |
> |
{ |
| 225 |
> |
int fd; |
| 226 |
> |
|
| 227 |
> |
if (rt_proc.pid > 0) { |
| 228 |
> |
fprintf(stderr, "%s: only one i/o pipe at a time!\n", progname); |
| 229 |
> |
return(NULL); |
| 230 |
> |
} |
| 231 |
> |
if (verbose > 0) { |
| 232 |
> |
char cmd[4096]; |
| 233 |
> |
if (!convert_commandline(cmd, sizeof(cmd), av)) |
| 234 |
> |
strcpy(cmd, "COMMAND TOO LONG TO SHOW"); |
| 235 |
> |
fprintf(stderr, "%s: opening pipe %s: %s\n", |
| 236 |
> |
progname, (*mode=='w') ? "to" : "from", cmd); |
| 237 |
> |
} |
| 238 |
> |
if (*mode == 'w') { |
| 239 |
> |
fd = rt_proc.w = dup(fileno(stdout)); |
| 240 |
> |
rt_proc.flags |= PF_FILT_OUT; |
| 241 |
> |
} else if (*mode == 'r') { |
| 242 |
> |
fd = rt_proc.r = dup(fileno(stdin)); |
| 243 |
> |
rt_proc.flags |= PF_FILT_INP; |
| 244 |
> |
} |
| 245 |
> |
if (fd < 0 || open_process(&rt_proc, av) <= 0) { |
| 246 |
> |
perror(av[0]); |
| 247 |
> |
return(NULL); |
| 248 |
> |
} |
| 249 |
> |
return(fdopen(fd, mode)); |
| 250 |
> |
} |
| 251 |
> |
|
| 252 |
> |
/* Close command pipe (returns -1 on error to match pclose) */ |
| 253 |
> |
static int |
| 254 |
> |
pclose_al(FILE *fp) |
| 255 |
> |
{ |
| 256 |
> |
int prob = (fclose(fp) == EOF); |
| 257 |
> |
|
| 258 |
> |
if (rt_proc.pid <= 0) |
| 259 |
> |
return(-1); |
| 260 |
> |
|
| 261 |
> |
prob |= (close_process(&rt_proc) != 0); |
| 262 |
> |
|
| 263 |
> |
return(-prob); |
| 264 |
> |
} |
| 265 |
> |
|
| 266 |
|
/* Execute system command in our stead (Unix version) */ |
| 267 |
|
static int |
| 268 |
|
my_exec(char *av[]) |
| 283 |
|
perror(compath); |
| 284 |
|
return(1); |
| 285 |
|
} |
| 286 |
+ |
|
| 287 |
|
#endif |
| 288 |
|
|
| 289 |
|
/* Get normalized direction vector from string specification */ |
| 445 |
|
curparams.vup[1] = 1; |
| 446 |
|
} |
| 447 |
|
/* determine sample type/bin */ |
| 448 |
< |
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') { |
| 448 |
> |
if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) { |
| 449 |
|
sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)", |
| 450 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]); |
| 451 |
|
binv = savqstr(sbuf); |
| 498 |
|
calfn = kquarterfn; kquarterfn = NULL; |
| 499 |
|
binf = "kqbin"; |
| 500 |
|
nbins = "Nkqbins"; |
| 501 |
+ |
} else if (!strcasecmp(curparams.hemis, "cie")) { |
| 502 |
+ |
calfn = ciefn; ciefn = NULL; |
| 503 |
+ |
sprintf(sbuf, "rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
| 504 |
+ |
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
| 505 |
+ |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
| 506 |
+ |
curparams.sign); |
| 507 |
+ |
binv = "cbin"; |
| 508 |
+ |
nbins = "Ncbins"; |
| 509 |
|
} else { |
| 510 |
|
fprintf(stderr, "%s: unrecognized hemisphere sampling: h=%s\n", |
| 511 |
|
progname, curparams.hemis); |
| 515 |
|
sprintf(sbuf, "RHS=%c1", curparams.sign); |
| 516 |
|
params = savqstr(sbuf); |
| 517 |
|
} |
| 518 |
< |
if (!uniform & (curparams.slist->styp == ST_SOURCE)) { |
| 518 |
> |
if (!uniform) { |
| 519 |
|
SURF *sp; |
| 520 |
|
for (sp = curparams.slist; sp != NULL; sp = sp->next) |
| 521 |
< |
if (fabs(sp->area - PI) > 1e-3) { |
| 521 |
> |
if (sp->styp == ST_SOURCE && fabs(sp->area - PI) > 1e-3) { |
| 522 |
|
fprintf(stderr, "%s: source '%s' must be 180-degrees\n", |
| 523 |
|
progname, sp->sname); |
| 524 |
|
exit(1); |
| 676 |
|
sp->priv = (void *)ptp; |
| 677 |
|
} |
| 678 |
|
/* pick triangle by partial area */ |
| 679 |
< |
for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++) |
| 679 |
> |
for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++) |
| 680 |
|
x -= ptp->tri[i].afrac; |
| 681 |
|
SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac); |
| 682 |
|
samp2[0] *= samp2[1] = sqrt(samp2[1]); |
| 717 |
|
if (p->nsurfs > nall) { /* (re)allocate surface area cache */ |
| 718 |
|
if (projsa) free(projsa); |
| 719 |
|
projsa = (double *)malloc(sizeof(double)*p->nsurfs); |
| 720 |
< |
if (!projsa) return(0); |
| 720 |
> |
if (projsa == NULL) { |
| 721 |
> |
fputs(progname, stderr); |
| 722 |
> |
fputs(": out of memory in sample_origin!\n", stderr); |
| 723 |
> |
exit(1); |
| 724 |
> |
} |
| 725 |
|
nall = p->nsurfs; |
| 726 |
|
} |
| 727 |
|
/* compute projected areas */ |
| 728 |
|
for (i = 0, sp = p->slist; sp != NULL; i++, sp = sp->next) { |
| 729 |
|
projsa[i] = -DOT(sp->snrm, rdir) * sp->area; |
| 730 |
< |
tarea += projsa[i] *= (double)(projsa[i] > FTINY); |
| 730 |
> |
tarea += projsa[i] *= (double)(projsa[i] > 0); |
| 731 |
|
} |
| 732 |
< |
if (tarea <= FTINY) { /* wrong side of sender? */ |
| 732 |
> |
if (tarea < FTINY*FTINY) { /* wrong side of sender? */ |
| 733 |
|
fputs(progname, stderr); |
| 734 |
|
fputs(": internal - sample behind all sender elements!\n", |
| 735 |
|
stderr); |
| 747 |
|
{ |
| 748 |
|
int n = sampcnt; |
| 749 |
|
double samp3[3]; |
| 750 |
< |
double duvw[3]; |
| 678 |
< |
FVECT orig_dir[2]; |
| 750 |
> |
FVECT duvw, orig_dir[2]; |
| 751 |
|
int i; |
| 752 |
|
|
| 753 |
|
if (fp == NULL) /* just requesting number of bins? */ |
| 755 |
|
|
| 756 |
|
while (n--) { /* stratified hemisphere sampling */ |
| 757 |
|
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
| 758 |
< |
SDsquare2disk(duvw, samp3[1], samp3[2]); |
| 758 |
> |
square2disk(duvw, samp3[1], samp3[2]); |
| 759 |
|
duvw[2] = -sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]); |
| 760 |
|
for (i = 3; i--; ) |
| 761 |
|
orig_dir[1][i] = duvw[0]*p->udir[i] + |
| 763 |
|
duvw[2]*p->nrm[i] ; |
| 764 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
| 765 |
|
return(0); |
| 766 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
| 766 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
| 767 |
|
return(0); |
| 768 |
|
} |
| 769 |
|
return(1); |
| 775 |
|
{ |
| 776 |
|
int n = sampcnt; |
| 777 |
|
double samp3[3]; |
| 778 |
< |
double duvw[3]; |
| 707 |
< |
FVECT orig_dir[2]; |
| 778 |
> |
FVECT duvw, orig_dir[2]; |
| 779 |
|
int i; |
| 780 |
|
|
| 781 |
|
if (fp == NULL) /* just requesting number of bins? */ |
| 783 |
|
|
| 784 |
|
while (n--) { /* stratified sampling */ |
| 785 |
|
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
| 786 |
< |
SDsquare2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz, |
| 786 |
> |
square2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz, |
| 787 |
|
(b%p->hsiz + samp3[2])/curparams.hsiz); |
| 788 |
|
duvw[2] = sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]); |
| 789 |
|
for (i = 3; i--; ) |
| 792 |
|
duvw[2]*p->nrm[i] ; |
| 793 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
| 794 |
|
return(0); |
| 795 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
| 795 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
| 796 |
|
return(0); |
| 797 |
|
} |
| 798 |
|
return(1); |
| 828 |
|
} |
| 829 |
|
while (n--) { /* stratified sampling */ |
| 830 |
|
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
| 831 |
+ |
if (row >= RowMax-1) /* avoid crowding at zenith */ |
| 832 |
+ |
samp3[1] *= samp3[1]; |
| 833 |
|
alt = (row+samp3[1])*RAH; |
| 834 |
|
azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row); |
| 835 |
|
duvw[2] = cos(alt); /* measured from horizon */ |
| 836 |
|
duvw[0] = tsin(azi)*duvw[2]; |
| 837 |
< |
duvw[1] = tcos(azi)*duvw[2]; |
| 837 |
> |
duvw[1] = -tcos(azi)*duvw[2]; |
| 838 |
|
duvw[2] = sqrt(1. - duvw[2]*duvw[2]); |
| 839 |
|
for (i = 3; i--; ) |
| 840 |
|
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
| 842 |
|
duvw[2]*p->nrm[i] ; |
| 843 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
| 844 |
|
return(0); |
| 845 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
| 845 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
| 846 |
|
return(0); |
| 847 |
|
} |
| 848 |
|
return(1); |
| 893 |
|
duvw[2]*p->nrm[i] ; |
| 894 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0])) |
| 895 |
|
return(0); |
| 896 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
| 896 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
| 897 |
|
return(0); |
| 898 |
|
} |
| 899 |
|
return(1); |
| 941 |
|
curparams.udir[1] *= -1.; |
| 942 |
|
curparams.udir[2] *= -1.; |
| 943 |
|
} |
| 944 |
< |
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') |
| 944 |
> |
if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) |
| 945 |
|
curparams.sample_basis = sample_uniform; |
| 946 |
|
else if (tolower(curparams.hemis[0]) == 's' && |
| 947 |
|
tolower(curparams.hemis[1]) == 'c') |
| 1002 |
|
VCOPY(e1, e2); |
| 1003 |
|
} |
| 1004 |
|
p->area = normalize(p->snrm)*0.5; |
| 1005 |
< |
return(p->area > FTINY); |
| 1005 |
> |
return(p->area > FTINY*FTINY); |
| 1006 |
|
} |
| 1007 |
|
|
| 1008 |
|
/* Add a surface to our current parameters */ |
| 1020 |
|
snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1)); |
| 1021 |
|
if (snew == NULL) { |
| 1022 |
|
fputs(progname, stderr); |
| 1023 |
< |
fputs(": out of memory!\n", stderr); |
| 1023 |
> |
fputs(": out of memory in add_surface!\n", stderr); |
| 1024 |
|
exit(1); |
| 1025 |
|
} |
| 1026 |
|
strncpy(snew->sname, oname, sizeof(snew->sname)-1); |
| 1065 |
|
snew->area *= PI*snew->area; |
| 1066 |
|
break; |
| 1067 |
|
} |
| 1068 |
< |
if ((snew->area <= FTINY) & (verbose >= 0)) { |
| 1068 |
> |
if ((snew->area <= FTINY*FTINY) & (verbose >= 0)) { |
| 1069 |
|
fprintf(stderr, "%s: warning - zero area for surface '%s'\n", |
| 1070 |
|
progname, oname); |
| 1071 |
|
free(snew); |
| 1102 |
|
} |
| 1103 |
|
/* is it a new receiver? */ |
| 1104 |
|
if ((st = surf_type(otype)) != ST_NONE) { |
| 1032 |
– |
if (curparams.slist != NULL && (st == ST_SOURCE) ^ |
| 1033 |
– |
(curparams.slist->styp == ST_SOURCE)) { |
| 1034 |
– |
fputs(progname, stderr); |
| 1035 |
– |
fputs(": cannot mix source/non-source receivers!\n", stderr); |
| 1036 |
– |
return(-1); |
| 1037 |
– |
} |
| 1105 |
|
if (strcmp(thismod, curmod)) { |
| 1106 |
|
if (curmod[0]) { /* output last receiver? */ |
| 1107 |
|
finish_receiver(); |
| 1237 |
|
FILE *rcfp; |
| 1238 |
|
int nsbins; |
| 1239 |
|
int a, i; |
| 1240 |
+ |
/* set global progname */ |
| 1241 |
+ |
fixargv0(argv[0]); |
| 1242 |
|
/* screen rcontrib options */ |
| 1174 |
– |
progname = argv[0]; |
| 1243 |
|
for (a = 1; a < argc-2; a++) { |
| 1244 |
|
int na; |
| 1245 |
|
/* check for argument expansion */ |
| 1285 |
|
yrs = argv[++a]; |
| 1286 |
|
na = 0; |
| 1287 |
|
continue; |
| 1288 |
< |
case 'c': /* number of samples */ |
| 1289 |
< |
sampcnt = atoi(argv[++a]); |
| 1290 |
< |
if (sampcnt <= 0) |
| 1291 |
< |
goto userr; |
| 1292 |
< |
na = 0; /* we re-add this later */ |
| 1293 |
< |
continue; |
| 1288 |
> |
case 'c': /* spectral sampling or count */ |
| 1289 |
> |
switch (argv[a][2]) { |
| 1290 |
> |
case 's': |
| 1291 |
> |
na = 2; |
| 1292 |
> |
break; |
| 1293 |
> |
case 'w': |
| 1294 |
> |
na = 3; |
| 1295 |
> |
break; |
| 1296 |
> |
case '\0': |
| 1297 |
> |
sampcnt = atoi(argv[++a]); |
| 1298 |
> |
if (sampcnt <= 0) |
| 1299 |
> |
goto userr; |
| 1300 |
> |
na = 0; /* we re-add this later */ |
| 1301 |
> |
continue; |
| 1302 |
> |
} |
| 1303 |
> |
break; |
| 1304 |
|
case 'I': /* only for pass-through mode */ |
| 1305 |
|
case 'i': |
| 1306 |
|
iropt = argv[a]; |
| 1307 |
|
na = 0; |
| 1308 |
|
continue; |
| 1309 |
|
case 'w': /* options without arguments */ |
| 1310 |
< |
if (argv[a][2] != '+') verbose = -1; |
| 1310 |
> |
if (!argv[a][2] || strchr("+1tTyY", argv[a][2]) == NULL) |
| 1311 |
> |
verbose = -1; |
| 1312 |
> |
break; |
| 1313 |
|
case 'V': |
| 1314 |
|
case 'u': |
| 1315 |
|
case 'h': |
| 1318 |
|
case 'n': /* options with 1 argument */ |
| 1319 |
|
case 's': |
| 1320 |
|
case 'o': |
| 1321 |
+ |
case 't': |
| 1322 |
|
na = 2; |
| 1323 |
|
break; |
| 1324 |
|
case 'b': /* special case */ |
| 1387 |
|
fputs(": -i, -I supported for pass-through only\n", stderr); |
| 1388 |
|
return(1); |
| 1389 |
|
} |
| 1390 |
< |
fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; |
| 1390 |
> |
#ifdef SMLFLT |
| 1391 |
> |
fmtopt[2] = 'f'; |
| 1392 |
> |
#else |
| 1393 |
> |
fmtopt[2] = 'd'; |
| 1394 |
> |
#endif |
| 1395 |
|
if (sampcnt <= 0) sampcnt = 10000; |
| 1396 |
|
} |
| 1397 |
|
sprintf(sampcntbuf, "%d", sampcnt); |
| 1438 |
|
for (i = 0; i < nsbins; i++) /* send rcontrib ray samples */ |
| 1439 |
|
if (!(*curparams.sample_basis)(&curparams, i, rcfp)) |
| 1440 |
|
return(1); |
| 1441 |
< |
return(pclose(rcfp) < 0); /* all finished! */ |
| 1441 |
> |
return(pclose_al(rcfp) < 0); /* all finished! */ |
| 1442 |
|
userr: |
| 1443 |
|
if (a < argc-2) |
| 1444 |
|
fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]); |