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] */ |
64 |
|
FVECT uva[2]; /* tangent axes */ |
65 |
|
int ntris; /* number of triangles */ |
66 |
|
struct ptri { |
67 |
< |
float afrac; /* fraction of total area */ |
67 |
> |
double afrac; /* fraction of total area */ |
68 |
|
short vndx[3]; /* vertex indices */ |
69 |
|
} tri[1]; /* triangle array (extends struct) */ |
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 */ |
138 |
|
|
139 |
|
if (ac-- <= 0) |
140 |
|
return(NULL); |
141 |
+ |
if (verbose < 0) { /* turn off warnings */ |
142 |
+ |
strcpy(cp, "-w- "); |
143 |
+ |
cp += 4; |
144 |
+ |
} |
145 |
|
while (ac-- > 0) { |
146 |
|
strcpy(cp, *av++); |
147 |
|
while (*cp) cp++; |
148 |
|
*cp++ = ' '; |
149 |
< |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) { |
150 |
< |
fputs(progname, stderr); |
160 |
< |
fputs(": too many file arguments!\n", stderr); |
161 |
< |
exit(1); |
162 |
< |
} |
149 |
> |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) |
150 |
> |
goto overrun; |
151 |
|
} |
152 |
< |
strcpy(cp, recv); /* receiver goes last */ |
152 |
> |
/* receiver goes last */ |
153 |
> |
if (matchany(recv, SPECIALS)) { |
154 |
> |
*cp++ = QUOTCHAR; |
155 |
> |
while (*recv) { |
156 |
> |
if (cp >= oconvbuf+(sizeof(oconvbuf)-3)) |
157 |
> |
goto overrun; |
158 |
> |
*cp++ = *recv++; |
159 |
> |
} |
160 |
> |
*cp++ = QUOTCHAR; |
161 |
> |
*cp = '\0'; |
162 |
> |
} else |
163 |
> |
strcpy(cp, recv); |
164 |
|
return(oconvbuf); |
165 |
+ |
overrun: |
166 |
+ |
fputs(progname, stderr); |
167 |
+ |
fputs(": too many file arguments!\n", stderr); |
168 |
+ |
exit(1); |
169 |
|
} |
170 |
|
|
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 |
– |
|
171 |
|
/* Open a pipe to/from a command given as an argument list */ |
172 |
|
static FILE * |
173 |
|
popen_arglist(char *av[], char *mode) |
185 |
|
return(popen(cmd, mode)); |
186 |
|
} |
187 |
|
|
188 |
< |
#ifdef _WIN32 |
188 |
> |
#if defined(_WIN32) || defined(_WIN64) |
189 |
|
/* Execute system command (Windows version) */ |
190 |
|
static int |
191 |
|
my_exec(char *av[]) |
270 |
|
{ |
271 |
|
char *cp = pargs; |
272 |
|
int nparams = 0; |
273 |
+ |
int quot; |
274 |
|
int i; |
275 |
|
|
276 |
|
for ( ; ; ) { |
278 |
|
case 'h': |
279 |
|
if (*cp++ != '=') |
280 |
|
break; |
281 |
+ |
if ((*cp == '+') | (*cp == '-')) |
282 |
+ |
p->sign = *cp++; |
283 |
+ |
else |
284 |
+ |
p->sign = '+'; |
285 |
|
p->hsiz = 0; |
286 |
|
i = 0; |
287 |
|
while (*cp && !isspace(*cp)) { |
307 |
|
case 'o': |
308 |
|
if (*cp++ != '=') |
309 |
|
break; |
310 |
+ |
quot = 0; |
311 |
+ |
if ((*cp == '"') | (*cp == '\'')) |
312 |
+ |
quot = *cp++; |
313 |
|
i = 0; |
314 |
< |
while (*cp && !isspace(*cp++)) |
315 |
< |
i++; |
314 |
> |
while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) { |
315 |
> |
i++; cp++; |
316 |
> |
} |
317 |
|
if (!i) |
318 |
|
break; |
319 |
< |
*--cp = '\0'; |
319 |
> |
if (!*cp) { |
320 |
> |
if (quot) |
321 |
> |
break; |
322 |
> |
cp[1] = '\0'; |
323 |
> |
} |
324 |
> |
*cp = '\0'; |
325 |
|
p->outfn = savqstr(cp-i); |
326 |
< |
*cp++ = ' '; |
326 |
> |
*cp++ = quot ? quot : ' '; |
327 |
|
++nparams; |
328 |
|
continue; |
329 |
|
case ' ': |
330 |
|
case '\t': |
331 |
|
case '\r': |
365 |
– |
continue; |
332 |
|
case '\n': |
333 |
+ |
continue; |
334 |
|
case '\0': |
335 |
|
return(nparams); |
336 |
|
default: |
383 |
|
curparams.vup[1] = 1; |
384 |
|
} |
385 |
|
/* determine sample type/bin */ |
386 |
< |
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') { |
387 |
< |
binv = "0"; /* uniform sampling -- one bin */ |
386 |
> |
if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) { |
387 |
> |
sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)", |
388 |
> |
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]); |
389 |
> |
binv = savqstr(sbuf); |
390 |
> |
nbins = "1"; /* uniform sampling -- one bin */ |
391 |
|
uniform = 1; |
392 |
|
} else if (tolower(curparams.hemis[0]) == 's' && |
393 |
|
tolower(curparams.hemis[1]) == 'c') { |
398 |
|
exit(1); |
399 |
|
} |
400 |
|
calfn = shirchiufn; shirchiufn = NULL; |
401 |
< |
sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g", |
401 |
> |
sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
402 |
|
curparams.hsiz, |
403 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
404 |
< |
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
404 |
> |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
405 |
> |
curparams.sign); |
406 |
|
params = savqstr(sbuf); |
407 |
|
binv = "scbin"; |
408 |
|
nbins = "SCdim*SCdim"; |
409 |
|
} else if ((tolower(curparams.hemis[0]) == 'r') | |
410 |
|
(tolower(curparams.hemis[0]) == 't')) { |
411 |
|
calfn = reinhfn; reinhfn = NULL; |
412 |
< |
sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g", |
412 |
> |
sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
413 |
|
curparams.hsiz, |
414 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
415 |
< |
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
415 |
> |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
416 |
> |
curparams.sign); |
417 |
|
params = savqstr(sbuf); |
418 |
|
binv = "rbin"; |
419 |
|
nbins = "Nrbins"; |
441 |
|
progname, curparams.hemis); |
442 |
|
exit(1); |
443 |
|
} |
444 |
+ |
if (tolower(curparams.hemis[0]) == 'k') { |
445 |
+ |
sprintf(sbuf, "RHS=%c1", curparams.sign); |
446 |
+ |
params = savqstr(sbuf); |
447 |
+ |
} |
448 |
|
if (!uniform & (curparams.slist->styp == ST_SOURCE)) { |
449 |
|
SURF *sp; |
450 |
|
for (sp = curparams.slist; sp != NULL; sp = sp->next) |
492 |
|
{ |
493 |
|
int i; |
494 |
|
|
495 |
< |
if (!getperpendicular(uva[0], nrm)) { |
495 |
> |
if (!getperpendicular(uva[0], nrm, 1)) { |
496 |
|
fputs(progname, stderr); |
497 |
|
fputs(": bad surface normal in make_axes!\n", stderr); |
498 |
|
exit(1); |
606 |
|
sp->priv = (void *)ptp; |
607 |
|
} |
608 |
|
/* pick triangle by partial area */ |
609 |
< |
for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++) |
609 |
> |
for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++) |
610 |
|
x -= ptp->tri[i].afrac; |
611 |
|
SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac); |
612 |
|
samp2[0] *= samp2[1] = sqrt(samp2[1]); |
636 |
|
/* special case for lone surface */ |
637 |
|
if (p->nsurfs == 1) { |
638 |
|
sp = p->slist; |
639 |
< |
if (DOT(sp->snrm, rdir) >= -FTINY) { |
639 |
> |
if (DOT(sp->snrm, rdir) >= FTINY) { |
640 |
|
fprintf(stderr, |
641 |
|
"%s: internal - sample behind sender '%s'\n", |
642 |
|
progname, sp->sname); |
647 |
|
if (p->nsurfs > nall) { /* (re)allocate surface area cache */ |
648 |
|
if (projsa) free(projsa); |
649 |
|
projsa = (double *)malloc(sizeof(double)*p->nsurfs); |
650 |
< |
if (!projsa) return(0); |
650 |
> |
if (projsa == NULL) { |
651 |
> |
fputs(progname, stderr); |
652 |
> |
fputs(": out of memory in sample_origin!\n", stderr); |
653 |
> |
exit(1); |
654 |
> |
} |
655 |
|
nall = p->nsurfs; |
656 |
|
} |
657 |
|
/* compute projected areas */ |
694 |
|
duvw[2]*p->nrm[i] ; |
695 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
696 |
|
return(0); |
697 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
697 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
698 |
|
return(0); |
699 |
|
} |
700 |
|
return(1); |
724 |
|
duvw[2]*p->nrm[i] ; |
725 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
726 |
|
return(0); |
727 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
727 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
728 |
|
return(0); |
729 |
|
} |
730 |
|
return(1); |
760 |
|
} |
761 |
|
while (n--) { /* stratified sampling */ |
762 |
|
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
763 |
+ |
if (row >= RowMax-1) /* avoid crowding at zenith */ |
764 |
+ |
samp3[1] *= samp3[1]; |
765 |
|
alt = (row+samp3[1])*RAH; |
766 |
|
azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row); |
767 |
|
duvw[2] = cos(alt); /* measured from horizon */ |
768 |
|
duvw[0] = tsin(azi)*duvw[2]; |
769 |
< |
duvw[1] = tcos(azi)*duvw[2]; |
769 |
> |
duvw[1] = -tcos(azi)*duvw[2]; |
770 |
|
duvw[2] = sqrt(1. - duvw[2]*duvw[2]); |
771 |
|
for (i = 3; i--; ) |
772 |
|
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
774 |
|
duvw[2]*p->nrm[i] ; |
775 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
776 |
|
return(0); |
777 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
777 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
778 |
|
return(0); |
779 |
|
} |
780 |
|
return(1); |
817 |
|
|
818 |
|
while (n--) { /* stratified sampling */ |
819 |
|
SDmultiSamp(samp2, 2, (n+frandom())/sampcnt); |
820 |
< |
if (!bi_getvec(duvw, b+samp2[1], kbasis[bi])) |
820 |
> |
if (!fo_getvec(duvw, b+samp2[1], kbasis[bi])) |
821 |
|
return(0); |
822 |
|
for (i = 3; i--; ) |
823 |
< |
orig_dir[1][i] = duvw[0]*p->udir[i] + |
824 |
< |
duvw[1]*p->vdir[i] + |
823 |
> |
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
824 |
> |
duvw[1]*p->vdir[i] - |
825 |
|
duvw[2]*p->nrm[i] ; |
826 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0])) |
827 |
|
return(0); |
828 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
828 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
829 |
|
return(0); |
830 |
|
} |
831 |
|
return(1); |
861 |
|
else |
862 |
|
curparams.vup[1] = 1; |
863 |
|
} |
864 |
< |
VCROSS(curparams.udir, curparams.vup, curparams.nrm); |
864 |
> |
fcross(curparams.udir, curparams.vup, curparams.nrm); |
865 |
|
if (normalize(curparams.udir) == 0) { |
866 |
|
fputs(progname, stderr); |
867 |
|
fputs(": up vector coincides with sender normal\n", stderr); |
868 |
|
return(-1); |
869 |
|
} |
870 |
< |
VCROSS(curparams.vdir, curparams.nrm, curparams.udir); |
871 |
< |
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') |
870 |
> |
fcross(curparams.vdir, curparams.nrm, curparams.udir); |
871 |
> |
if (curparams.sign == '-') { /* left-handed coordinate system? */ |
872 |
> |
curparams.udir[0] *= -1.; |
873 |
> |
curparams.udir[1] *= -1.; |
874 |
> |
curparams.udir[2] *= -1.; |
875 |
> |
} |
876 |
> |
if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) |
877 |
|
curparams.sample_basis = sample_uniform; |
878 |
|
else if (tolower(curparams.hemis[0]) == 's' && |
879 |
|
tolower(curparams.hemis[1]) == 'c') |
952 |
|
snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1)); |
953 |
|
if (snew == NULL) { |
954 |
|
fputs(progname, stderr); |
955 |
< |
fputs(": out of memory!\n", stderr); |
955 |
> |
fputs(": out of memory in add_surface!\n", stderr); |
956 |
|
exit(1); |
957 |
|
} |
958 |
|
strncpy(snew->sname, oname, sizeof(snew->sname)-1); |
1067 |
|
add_send_object(FILE *fp) |
1068 |
|
{ |
1069 |
|
int st; |
1070 |
< |
char otype[32], oname[128]; |
1070 |
> |
char thismod[128], otype[32], oname[128]; |
1071 |
|
int n; |
1072 |
|
|
1073 |
< |
if (fscanf(fp, "%*s %s %s", otype, oname) != 2) |
1073 |
> |
if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3) |
1074 |
|
return(0); /* must have hit EOF! */ |
1075 |
|
if (!strcmp(otype, "alias")) { |
1076 |
|
fscanf(fp, "%*s"); /* skip alias */ |
1083 |
|
fputs(": cannot use source as a sender!\n", stderr); |
1084 |
|
return(-1); |
1085 |
|
} |
1086 |
+ |
if (strcmp(thismod, curmod)) { |
1087 |
+ |
if (curmod[0]) { |
1088 |
+ |
fputs(progname, stderr); |
1089 |
+ |
fputs(": warning - multiple modifiers in sender\n", |
1090 |
+ |
stderr); |
1091 |
+ |
} |
1092 |
+ |
strcpy(curmod, thismod); |
1093 |
+ |
} |
1094 |
|
parse_params(&curparams, newparams); |
1095 |
|
newparams[0] = '\0'; |
1096 |
|
add_surface(st, oname, fp); /* read & store surface */ |
1260 |
|
if (argv[a][2] != 'v') na = 2; |
1261 |
|
break; |
1262 |
|
case 'a': /* special case */ |
1263 |
< |
na = (argv[a][2] == 'v') ? 4 : 2; |
1263 |
> |
if (argv[a][2] == 'p') { |
1264 |
> |
na = 2; /* photon map [+ bandwidth(s)] */ |
1265 |
> |
if (a < argc-3 && atoi(argv[a+1]) > 0) |
1266 |
> |
na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0); |
1267 |
> |
} else |
1268 |
> |
na = (argv[a][2] == 'v') ? 4 : 2; |
1269 |
|
break; |
1270 |
|
case 'm': /* special case */ |
1271 |
|
if (!argv[a][2]) goto userr; |
1311 |
|
fputs(": -i, -I supported for pass-through only\n", stderr); |
1312 |
|
return(1); |
1313 |
|
} |
1314 |
< |
fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; |
1314 |
> |
#ifdef SMLFLT |
1315 |
> |
fmtopt[2] = 'f'; |
1316 |
> |
#else |
1317 |
> |
fmtopt[2] = 'd'; |
1318 |
> |
#endif |
1319 |
|
if (sampcnt <= 0) sampcnt = 10000; |
1320 |
|
} |
1321 |
|
sprintf(sampcntbuf, "%d", sampcnt); |
1334 |
|
return(my_exec(rcarg)); /* rcontrib does everything */ |
1335 |
|
} |
1336 |
|
clear_params(&curparams, 0); /* else load sender surface & params */ |
1337 |
+ |
curmod[0] = '\0'; |
1338 |
|
if (load_scene(sendfn, add_send_object) < 0) |
1339 |
|
return(1); |
1340 |
|
if ((nsbins = prepare_sampler()) <= 0) |
1352 |
|
#ifdef getc_unlocked |
1353 |
|
flockfile(rcfp); |
1354 |
|
#endif |
1355 |
< |
if (verbose) { |
1355 |
> |
if (verbose > 0) { |
1356 |
|
fprintf(stderr, "%s: sampling %d directions", progname, nsbins); |
1357 |
|
if (curparams.nsurfs > 1) |
1358 |
|
fprintf(stderr, " (%d elements)\n", curparams.nsurfs); |
1362 |
|
for (i = 0; i < nsbins; i++) /* send rcontrib ray samples */ |
1363 |
|
if (!(*curparams.sample_basis)(&curparams, i, rcfp)) |
1364 |
|
return(1); |
1365 |
< |
return(pclose(rcfp) == 0); /* all finished! */ |
1365 |
> |
return(pclose(rcfp) < 0); /* all finished! */ |
1366 |
|
userr: |
1367 |
|
if (a < argc-2) |
1368 |
|
fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]); |