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" |
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 |
– |
|
26 |
|
#define MAXRCARG 512 |
27 |
|
|
28 |
|
char *progname; /* global argv[0] */ |
75 |
|
} POLYTRIS; /* triangulated polygon */ |
76 |
|
|
77 |
|
typedef struct param_s { |
78 |
< |
char hemis[32]; /* hemispherical sampling spec. */ |
78 |
> |
char sign; /* '-' for axis reversal */ |
79 |
> |
char hemis[31]; /* hemispherical sampling spec. */ |
80 |
|
int hsiz; /* hemisphere basis size */ |
81 |
|
int nsurfs; /* number of surfaces */ |
82 |
|
SURF *slist; /* list of surfaces */ |
83 |
|
FVECT vup; /* up vector (zero if unset) */ |
84 |
|
FVECT nrm; /* average normal direction */ |
85 |
< |
FVECT udir, vdir; /* v-up tangent axes */ |
85 |
> |
FVECT udir, vdir; /* tangent axes */ |
86 |
|
char *outfn; /* output file name (receiver) */ |
87 |
|
int (*sample_basis)(struct param_s *p, int, FILE *); |
88 |
|
} PARAMS; /* sender/receiver parameters */ |
147 |
|
strcpy(cp, *av++); |
148 |
|
while (*cp) cp++; |
149 |
|
*cp++ = ' '; |
150 |
< |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) { |
151 |
< |
fputs(progname, stderr); |
160 |
< |
fputs(": too many file arguments!\n", stderr); |
161 |
< |
exit(1); |
162 |
< |
} |
150 |
> |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) |
151 |
> |
goto overrun; |
152 |
|
} |
153 |
< |
strcpy(cp, recv); /* receiver goes last */ |
153 |
> |
/* receiver goes last */ |
154 |
> |
if (matchany(recv, SPECIALS)) { |
155 |
> |
*cp++ = QUOTCHAR; |
156 |
> |
while (*recv) { |
157 |
> |
if (cp >= oconvbuf+(sizeof(oconvbuf)-3)) |
158 |
> |
goto overrun; |
159 |
> |
*cp++ = *recv++; |
160 |
> |
} |
161 |
> |
*cp++ = QUOTCHAR; |
162 |
> |
*cp = '\0'; |
163 |
> |
} else |
164 |
> |
strcpy(cp, recv); |
165 |
|
return(oconvbuf); |
166 |
+ |
overrun: |
167 |
+ |
fputs(progname, stderr); |
168 |
+ |
fputs(": too many file arguments!\n", stderr); |
169 |
+ |
exit(1); |
170 |
|
} |
171 |
|
|
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 |
– |
|
172 |
|
/* Open a pipe to/from a command given as an argument list */ |
173 |
|
static FILE * |
174 |
|
popen_arglist(char *av[], char *mode) |
271 |
|
{ |
272 |
|
char *cp = pargs; |
273 |
|
int nparams = 0; |
274 |
+ |
int quot; |
275 |
|
int i; |
276 |
|
|
277 |
|
for ( ; ; ) { |
279 |
|
case 'h': |
280 |
|
if (*cp++ != '=') |
281 |
|
break; |
282 |
+ |
if ((*cp == '+') | (*cp == '-')) |
283 |
+ |
p->sign = *cp++; |
284 |
+ |
else |
285 |
+ |
p->sign = '+'; |
286 |
|
p->hsiz = 0; |
287 |
|
i = 0; |
288 |
|
while (*cp && !isspace(*cp)) { |
308 |
|
case 'o': |
309 |
|
if (*cp++ != '=') |
310 |
|
break; |
311 |
+ |
quot = 0; |
312 |
+ |
if ((*cp == '"') | (*cp == '\'')) |
313 |
+ |
quot = *cp++; |
314 |
|
i = 0; |
315 |
< |
while (*cp && !isspace(*cp++)) |
316 |
< |
i++; |
315 |
> |
while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) { |
316 |
> |
i++; cp++; |
317 |
> |
} |
318 |
|
if (!i) |
319 |
|
break; |
320 |
< |
*--cp = '\0'; |
320 |
> |
if (!*cp) { |
321 |
> |
if (quot) |
322 |
> |
break; |
323 |
> |
cp[1] = '\0'; |
324 |
> |
} |
325 |
> |
*cp = '\0'; |
326 |
|
p->outfn = savqstr(cp-i); |
327 |
< |
*cp++ = ' '; |
327 |
> |
*cp++ = quot ? quot : ' '; |
328 |
|
++nparams; |
329 |
|
continue; |
330 |
|
case ' ': |
399 |
|
exit(1); |
400 |
|
} |
401 |
|
calfn = shirchiufn; shirchiufn = NULL; |
402 |
< |
sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g", |
402 |
> |
sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
403 |
|
curparams.hsiz, |
404 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
405 |
< |
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
405 |
> |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
406 |
> |
curparams.sign); |
407 |
|
params = savqstr(sbuf); |
408 |
|
binv = "scbin"; |
409 |
|
nbins = "SCdim*SCdim"; |
410 |
|
} else if ((tolower(curparams.hemis[0]) == 'r') | |
411 |
|
(tolower(curparams.hemis[0]) == 't')) { |
412 |
|
calfn = reinhfn; reinhfn = NULL; |
413 |
< |
sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g", |
413 |
> |
sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
414 |
|
curparams.hsiz, |
415 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
416 |
< |
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
416 |
> |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
417 |
> |
curparams.sign); |
418 |
|
params = savqstr(sbuf); |
419 |
|
binv = "rbin"; |
420 |
|
nbins = "Nrbins"; |
442 |
|
progname, curparams.hemis); |
443 |
|
exit(1); |
444 |
|
} |
445 |
+ |
if (tolower(curparams.hemis[0]) == 'k') { |
446 |
+ |
sprintf(sbuf, "RHS=%c1", curparams.sign); |
447 |
+ |
params = savqstr(sbuf); |
448 |
+ |
} |
449 |
|
if (!uniform & (curparams.slist->styp == ST_SOURCE)) { |
450 |
|
SURF *sp; |
451 |
|
for (sp = curparams.slist; sp != NULL; sp = sp->next) |
493 |
|
{ |
494 |
|
int i; |
495 |
|
|
496 |
< |
if (!getperpendicular(uva[0], nrm)) { |
496 |
> |
if (!getperpendicular(uva[0], nrm, 1)) { |
497 |
|
fputs(progname, stderr); |
498 |
|
fputs(": bad surface normal in make_axes!\n", stderr); |
499 |
|
exit(1); |
637 |
|
/* special case for lone surface */ |
638 |
|
if (p->nsurfs == 1) { |
639 |
|
sp = p->slist; |
640 |
< |
if (DOT(sp->snrm, rdir) >= -FTINY) { |
640 |
> |
if (DOT(sp->snrm, rdir) >= FTINY) { |
641 |
|
fprintf(stderr, |
642 |
|
"%s: internal - sample behind sender '%s'\n", |
643 |
|
progname, sp->sname); |
812 |
|
|
813 |
|
while (n--) { /* stratified sampling */ |
814 |
|
SDmultiSamp(samp2, 2, (n+frandom())/sampcnt); |
815 |
< |
if (!bi_getvec(duvw, b+samp2[1], kbasis[bi])) |
815 |
> |
if (!fo_getvec(duvw, b+samp2[1], kbasis[bi])) |
816 |
|
return(0); |
817 |
|
for (i = 3; i--; ) |
818 |
< |
orig_dir[1][i] = duvw[0]*p->udir[i] + |
819 |
< |
duvw[1]*p->vdir[i] + |
818 |
> |
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
819 |
> |
duvw[1]*p->vdir[i] - |
820 |
|
duvw[2]*p->nrm[i] ; |
821 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0])) |
822 |
|
return(0); |
856 |
|
else |
857 |
|
curparams.vup[1] = 1; |
858 |
|
} |
859 |
< |
VCROSS(curparams.udir, curparams.vup, curparams.nrm); |
859 |
> |
fcross(curparams.udir, curparams.vup, curparams.nrm); |
860 |
|
if (normalize(curparams.udir) == 0) { |
861 |
|
fputs(progname, stderr); |
862 |
|
fputs(": up vector coincides with sender normal\n", stderr); |
863 |
|
return(-1); |
864 |
|
} |
865 |
< |
VCROSS(curparams.vdir, curparams.nrm, curparams.udir); |
865 |
> |
fcross(curparams.vdir, curparams.nrm, curparams.udir); |
866 |
> |
if (curparams.sign == '-') { /* left-handed coordinate system? */ |
867 |
> |
curparams.udir[0] *= -1.; |
868 |
> |
curparams.udir[1] *= -1.; |
869 |
> |
curparams.udir[2] *= -1.; |
870 |
> |
} |
871 |
|
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') |
872 |
|
curparams.sample_basis = sample_uniform; |
873 |
|
else if (tolower(curparams.hemis[0]) == 's' && |
1062 |
|
add_send_object(FILE *fp) |
1063 |
|
{ |
1064 |
|
int st; |
1065 |
< |
char otype[32], oname[128]; |
1065 |
> |
char thismod[128], otype[32], oname[128]; |
1066 |
|
int n; |
1067 |
|
|
1068 |
< |
if (fscanf(fp, "%*s %s %s", otype, oname) != 2) |
1068 |
> |
if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3) |
1069 |
|
return(0); /* must have hit EOF! */ |
1070 |
|
if (!strcmp(otype, "alias")) { |
1071 |
|
fscanf(fp, "%*s"); /* skip alias */ |
1078 |
|
fputs(": cannot use source as a sender!\n", stderr); |
1079 |
|
return(-1); |
1080 |
|
} |
1081 |
+ |
if (strcmp(thismod, curmod)) { |
1082 |
+ |
if (curmod[0]) { |
1083 |
+ |
fputs(progname, stderr); |
1084 |
+ |
fputs(": warning - multiple modifiers in sender\n", |
1085 |
+ |
stderr); |
1086 |
+ |
} |
1087 |
+ |
strcpy(curmod, thismod); |
1088 |
+ |
} |
1089 |
|
parse_params(&curparams, newparams); |
1090 |
|
newparams[0] = '\0'; |
1091 |
|
add_surface(st, oname, fp); /* read & store surface */ |
1255 |
|
if (argv[a][2] != 'v') na = 2; |
1256 |
|
break; |
1257 |
|
case 'a': /* special case */ |
1258 |
< |
na = (argv[a][2] == 'v') ? 4 : 2; |
1258 |
> |
if (argv[a][2] == 'p') { |
1259 |
> |
na = 2; /* photon map [+ bandwidth(s)] */ |
1260 |
> |
if (a < argc-3 && atoi(argv[a+1]) > 0) |
1261 |
> |
na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0); |
1262 |
> |
} else |
1263 |
> |
na = (argv[a][2] == 'v') ? 4 : 2; |
1264 |
|
break; |
1265 |
|
case 'm': /* special case */ |
1266 |
|
if (!argv[a][2]) goto userr; |
1325 |
|
return(my_exec(rcarg)); /* rcontrib does everything */ |
1326 |
|
} |
1327 |
|
clear_params(&curparams, 0); /* else load sender surface & params */ |
1328 |
+ |
curmod[0] = '\0'; |
1329 |
|
if (load_scene(sendfn, add_send_object) < 0) |
1330 |
|
return(1); |
1331 |
|
if ((nsbins = prepare_sampler()) <= 0) |