| 11 |
|
#include <stdlib.h> |
| 12 |
|
#include "rtio.h" |
| 13 |
|
#include "rtmath.h" |
| 14 |
< |
#include "rtprocess.h" |
| 14 |
> |
#include "paths.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 |
| 24 |
– |
#endif |
| 25 |
– |
|
| 26 |
– |
#ifdef _WIN32 |
| 27 |
– |
#define SPECIALS " \t\"$*?" |
| 28 |
– |
#define QUOTCHAR '"' |
| 29 |
– |
#else |
| 30 |
– |
#define SPECIALS " \t\n'\"()${}*?[];|&" |
| 31 |
– |
#define QUOTCHAR '\'' |
| 32 |
– |
#define ALTQUOT '"' |
| 33 |
– |
#endif |
| 34 |
– |
|
| 21 |
|
#define MAXRCARG 512 |
| 22 |
|
|
| 23 |
|
char *progname; /* global argv[0] */ |
| 70 |
|
} POLYTRIS; /* triangulated polygon */ |
| 71 |
|
|
| 72 |
|
typedef struct param_s { |
| 73 |
< |
char hemis[32]; /* hemispherical sampling spec. */ |
| 73 |
> |
char sign; /* '-' for axis reversal */ |
| 74 |
> |
char hemis[31]; /* hemispherical sampling spec. */ |
| 75 |
|
int hsiz; /* hemisphere basis size */ |
| 76 |
|
int nsurfs; /* number of surfaces */ |
| 77 |
|
SURF *slist; /* list of surfaces */ |
| 78 |
|
FVECT vup; /* up vector (zero if unset) */ |
| 79 |
|
FVECT nrm; /* average normal direction */ |
| 80 |
< |
FVECT udir, vdir; /* v-up tangent axes */ |
| 80 |
> |
FVECT udir, vdir; /* tangent axes */ |
| 81 |
|
char *outfn; /* output file name (receiver) */ |
| 82 |
|
int (*sample_basis)(struct param_s *p, int, FILE *); |
| 83 |
|
} PARAMS; /* sender/receiver parameters */ |
| 142 |
|
strcpy(cp, *av++); |
| 143 |
|
while (*cp) cp++; |
| 144 |
|
*cp++ = ' '; |
| 145 |
< |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) { |
| 146 |
< |
fputs(progname, stderr); |
| 160 |
< |
fputs(": too many file arguments!\n", stderr); |
| 161 |
< |
exit(1); |
| 162 |
< |
} |
| 145 |
> |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) |
| 146 |
> |
goto overrun; |
| 147 |
|
} |
| 148 |
< |
strcpy(cp, recv); /* receiver goes last */ |
| 148 |
> |
/* receiver goes last */ |
| 149 |
> |
if (matchany(recv, SPECIALS)) { |
| 150 |
> |
*cp++ = QUOTCHAR; |
| 151 |
> |
while (*recv) { |
| 152 |
> |
if (cp >= oconvbuf+(sizeof(oconvbuf)-3)) |
| 153 |
> |
goto overrun; |
| 154 |
> |
*cp++ = *recv++; |
| 155 |
> |
} |
| 156 |
> |
*cp++ = QUOTCHAR; |
| 157 |
> |
*cp = '\0'; |
| 158 |
> |
} else |
| 159 |
> |
strcpy(cp, recv); |
| 160 |
|
return(oconvbuf); |
| 161 |
+ |
overrun: |
| 162 |
+ |
fputs(progname, stderr); |
| 163 |
+ |
fputs(": too many file arguments!\n", stderr); |
| 164 |
+ |
exit(1); |
| 165 |
|
} |
| 166 |
|
|
| 168 |
– |
/* Check if any of the characters in str2 are found in str1 */ |
| 169 |
– |
static int |
| 170 |
– |
matchany(const char *str1, const char *str2) |
| 171 |
– |
{ |
| 172 |
– |
while (*str1) { |
| 173 |
– |
const char *cp = str2; |
| 174 |
– |
while (*cp) |
| 175 |
– |
if (*cp++ == *str1) |
| 176 |
– |
return(*str1); |
| 177 |
– |
++str1; |
| 178 |
– |
} |
| 179 |
– |
return(0); |
| 180 |
– |
} |
| 181 |
– |
|
| 182 |
– |
|
| 183 |
– |
/* Convert a set of arguments into a command line for pipe() or system() */ |
| 184 |
– |
static char * |
| 185 |
– |
convert_commandline(char *cmd, const int len, char *av[]) |
| 186 |
– |
{ |
| 187 |
– |
int match; |
| 188 |
– |
char *cp; |
| 189 |
– |
|
| 190 |
– |
for (cp = cmd; *av != NULL; av++) { |
| 191 |
– |
const int n = strlen(*av); |
| 192 |
– |
if (cp+n >= cmd+(len-3)) { |
| 193 |
– |
fputs(progname, stderr); |
| 194 |
– |
return(NULL); |
| 195 |
– |
} |
| 196 |
– |
if ((match = matchany(*av, SPECIALS))) { |
| 197 |
– |
const int quote = |
| 198 |
– |
#ifdef ALTQUOT |
| 199 |
– |
(match == QUOTCHAR) ? ALTQUOT : |
| 200 |
– |
#endif |
| 201 |
– |
QUOTCHAR; |
| 202 |
– |
*cp++ = quote; |
| 203 |
– |
strcpy(cp, *av); |
| 204 |
– |
cp += n; |
| 205 |
– |
*cp++ = quote; |
| 206 |
– |
} else { |
| 207 |
– |
strcpy(cp, *av); |
| 208 |
– |
cp += n; |
| 209 |
– |
} |
| 210 |
– |
*cp++ = ' '; |
| 211 |
– |
} |
| 212 |
– |
if (cp <= cmd) |
| 213 |
– |
return(NULL); |
| 214 |
– |
*--cp = '\0'; |
| 215 |
– |
return(cmd); |
| 216 |
– |
} |
| 217 |
– |
|
| 167 |
|
/* Open a pipe to/from a command given as an argument list */ |
| 168 |
|
static FILE * |
| 169 |
|
popen_arglist(char *av[], char *mode) |
| 181 |
|
return(popen(cmd, mode)); |
| 182 |
|
} |
| 183 |
|
|
| 184 |
< |
#ifdef _WIN32 |
| 184 |
> |
#if defined(_WIN32) || defined(_WIN64) |
| 185 |
|
/* Execute system command (Windows version) */ |
| 186 |
|
static int |
| 187 |
|
my_exec(char *av[]) |
| 266 |
|
{ |
| 267 |
|
char *cp = pargs; |
| 268 |
|
int nparams = 0; |
| 269 |
+ |
int quot; |
| 270 |
|
int i; |
| 271 |
|
|
| 272 |
|
for ( ; ; ) { |
| 274 |
|
case 'h': |
| 275 |
|
if (*cp++ != '=') |
| 276 |
|
break; |
| 277 |
+ |
if ((*cp == '+') | (*cp == '-')) |
| 278 |
+ |
p->sign = *cp++; |
| 279 |
+ |
else |
| 280 |
+ |
p->sign = '+'; |
| 281 |
|
p->hsiz = 0; |
| 282 |
|
i = 0; |
| 283 |
|
while (*cp && !isspace(*cp)) { |
| 303 |
|
case 'o': |
| 304 |
|
if (*cp++ != '=') |
| 305 |
|
break; |
| 306 |
+ |
quot = 0; |
| 307 |
+ |
if ((*cp == '"') | (*cp == '\'')) |
| 308 |
+ |
quot = *cp++; |
| 309 |
|
i = 0; |
| 310 |
< |
while (*cp && !isspace(*cp++)) |
| 311 |
< |
i++; |
| 310 |
> |
while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) { |
| 311 |
> |
i++; cp++; |
| 312 |
> |
} |
| 313 |
|
if (!i) |
| 314 |
|
break; |
| 315 |
< |
*--cp = '\0'; |
| 315 |
> |
if (!*cp) { |
| 316 |
> |
if (quot) |
| 317 |
> |
break; |
| 318 |
> |
cp[1] = '\0'; |
| 319 |
> |
} |
| 320 |
> |
*cp = '\0'; |
| 321 |
|
p->outfn = savqstr(cp-i); |
| 322 |
< |
*cp++ = ' '; |
| 322 |
> |
*cp++ = quot ? quot : ' '; |
| 323 |
|
++nparams; |
| 324 |
|
continue; |
| 325 |
|
case ' ': |
| 394 |
|
exit(1); |
| 395 |
|
} |
| 396 |
|
calfn = shirchiufn; shirchiufn = NULL; |
| 397 |
< |
sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g", |
| 397 |
> |
sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
| 398 |
|
curparams.hsiz, |
| 399 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
| 400 |
< |
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
| 400 |
> |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
| 401 |
> |
curparams.sign); |
| 402 |
|
params = savqstr(sbuf); |
| 403 |
|
binv = "scbin"; |
| 404 |
|
nbins = "SCdim*SCdim"; |
| 405 |
|
} else if ((tolower(curparams.hemis[0]) == 'r') | |
| 406 |
|
(tolower(curparams.hemis[0]) == 't')) { |
| 407 |
|
calfn = reinhfn; reinhfn = NULL; |
| 408 |
< |
sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g", |
| 408 |
> |
sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
| 409 |
|
curparams.hsiz, |
| 410 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
| 411 |
< |
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
| 411 |
> |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
| 412 |
> |
curparams.sign); |
| 413 |
|
params = savqstr(sbuf); |
| 414 |
|
binv = "rbin"; |
| 415 |
|
nbins = "Nrbins"; |
| 437 |
|
progname, curparams.hemis); |
| 438 |
|
exit(1); |
| 439 |
|
} |
| 440 |
+ |
if (tolower(curparams.hemis[0]) == 'k') { |
| 441 |
+ |
sprintf(sbuf, "RHS=%c1", curparams.sign); |
| 442 |
+ |
params = savqstr(sbuf); |
| 443 |
+ |
} |
| 444 |
|
if (!uniform & (curparams.slist->styp == ST_SOURCE)) { |
| 445 |
|
SURF *sp; |
| 446 |
|
for (sp = curparams.slist; sp != NULL; sp = sp->next) |
| 488 |
|
{ |
| 489 |
|
int i; |
| 490 |
|
|
| 491 |
< |
if (!getperpendicular(uva[0], nrm)) { |
| 491 |
> |
if (!getperpendicular(uva[0], nrm, 1)) { |
| 492 |
|
fputs(progname, stderr); |
| 493 |
|
fputs(": bad surface normal in make_axes!\n", stderr); |
| 494 |
|
exit(1); |
| 632 |
|
/* special case for lone surface */ |
| 633 |
|
if (p->nsurfs == 1) { |
| 634 |
|
sp = p->slist; |
| 635 |
< |
if (DOT(sp->snrm, rdir) >= -FTINY) { |
| 635 |
> |
if (DOT(sp->snrm, rdir) >= FTINY) { |
| 636 |
|
fprintf(stderr, |
| 637 |
|
"%s: internal - sample behind sender '%s'\n", |
| 638 |
|
progname, sp->sname); |
| 643 |
|
if (p->nsurfs > nall) { /* (re)allocate surface area cache */ |
| 644 |
|
if (projsa) free(projsa); |
| 645 |
|
projsa = (double *)malloc(sizeof(double)*p->nsurfs); |
| 646 |
< |
if (!projsa) return(0); |
| 646 |
> |
if (projsa == NULL) { |
| 647 |
> |
fputs(progname, stderr); |
| 648 |
> |
fputs(": out of memory in sample_origin!\n", stderr); |
| 649 |
> |
exit(1); |
| 650 |
> |
} |
| 651 |
|
nall = p->nsurfs; |
| 652 |
|
} |
| 653 |
|
/* compute projected areas */ |
| 811 |
|
|
| 812 |
|
while (n--) { /* stratified sampling */ |
| 813 |
|
SDmultiSamp(samp2, 2, (n+frandom())/sampcnt); |
| 814 |
< |
if (!bi_getvec(duvw, b+samp2[1], kbasis[bi])) |
| 814 |
> |
if (!fo_getvec(duvw, b+samp2[1], kbasis[bi])) |
| 815 |
|
return(0); |
| 816 |
|
for (i = 3; i--; ) |
| 817 |
< |
orig_dir[1][i] = duvw[0]*p->udir[i] + |
| 818 |
< |
duvw[1]*p->vdir[i] + |
| 817 |
> |
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
| 818 |
> |
duvw[1]*p->vdir[i] - |
| 819 |
|
duvw[2]*p->nrm[i] ; |
| 820 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0])) |
| 821 |
|
return(0); |
| 855 |
|
else |
| 856 |
|
curparams.vup[1] = 1; |
| 857 |
|
} |
| 858 |
< |
VCROSS(curparams.udir, curparams.vup, curparams.nrm); |
| 858 |
> |
fcross(curparams.udir, curparams.vup, curparams.nrm); |
| 859 |
|
if (normalize(curparams.udir) == 0) { |
| 860 |
|
fputs(progname, stderr); |
| 861 |
|
fputs(": up vector coincides with sender normal\n", stderr); |
| 862 |
|
return(-1); |
| 863 |
|
} |
| 864 |
< |
VCROSS(curparams.vdir, curparams.nrm, curparams.udir); |
| 864 |
> |
fcross(curparams.vdir, curparams.nrm, curparams.udir); |
| 865 |
> |
if (curparams.sign == '-') { /* left-handed coordinate system? */ |
| 866 |
> |
curparams.udir[0] *= -1.; |
| 867 |
> |
curparams.udir[1] *= -1.; |
| 868 |
> |
curparams.udir[2] *= -1.; |
| 869 |
> |
} |
| 870 |
|
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') |
| 871 |
|
curparams.sample_basis = sample_uniform; |
| 872 |
|
else if (tolower(curparams.hemis[0]) == 's' && |
| 946 |
|
snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1)); |
| 947 |
|
if (snew == NULL) { |
| 948 |
|
fputs(progname, stderr); |
| 949 |
< |
fputs(": out of memory!\n", stderr); |
| 949 |
> |
fputs(": out of memory in add_surface!\n", stderr); |
| 950 |
|
exit(1); |
| 951 |
|
} |
| 952 |
|
strncpy(snew->sname, oname, sizeof(snew->sname)-1); |
| 1064 |
|
char thismod[128], otype[32], oname[128]; |
| 1065 |
|
int n; |
| 1066 |
|
|
| 1067 |
< |
if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 2) |
| 1067 |
> |
if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3) |
| 1068 |
|
return(0); /* must have hit EOF! */ |
| 1069 |
|
if (!strcmp(otype, "alias")) { |
| 1070 |
|
fscanf(fp, "%*s"); /* skip alias */ |
| 1254 |
|
if (argv[a][2] != 'v') na = 2; |
| 1255 |
|
break; |
| 1256 |
|
case 'a': /* special case */ |
| 1257 |
< |
na = (argv[a][2] == 'v') ? 4 : 2; |
| 1257 |
> |
if (argv[a][2] == 'p') { |
| 1258 |
> |
na = 2; /* photon map [+ bandwidth(s)] */ |
| 1259 |
> |
if (a < argc-3 && atoi(argv[a+1]) > 0) |
| 1260 |
> |
na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0); |
| 1261 |
> |
} else |
| 1262 |
> |
na = (argv[a][2] == 'v') ? 4 : 2; |
| 1263 |
|
break; |
| 1264 |
|
case 'm': /* special case */ |
| 1265 |
|
if (!argv[a][2]) goto userr; |