11 |
|
#include <stdlib.h> |
12 |
|
#include "rtio.h" |
13 |
|
#include "rtmath.h" |
14 |
+ |
#include "rtprocess.h" |
15 |
|
#include "bsdf.h" |
16 |
|
#include "bsdf_m.h" |
17 |
|
#include "random.h" |
36 |
|
|
37 |
|
char *progname; /* global argv[0] */ |
38 |
|
|
39 |
< |
int verbose = 0; /* verbose mode? */ |
39 |
> |
int verbose = 0; /* verbose mode (< 0 no warnings) */ |
40 |
|
|
41 |
|
char *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"}; |
42 |
|
int nrcargs = 2; |
84 |
|
} POLYTRIS; /* triangulated polygon */ |
85 |
|
|
86 |
|
typedef struct param_s { |
87 |
< |
char hemis[32]; /* hemispherical sampling spec. */ |
87 |
> |
char sign; /* '-' for axis reversal */ |
88 |
> |
char hemis[31]; /* hemispherical sampling spec. */ |
89 |
|
int hsiz; /* hemisphere basis size */ |
90 |
|
int nsurfs; /* number of surfaces */ |
91 |
|
SURF *slist; /* list of surfaces */ |
92 |
|
FVECT vup; /* up vector (zero if unset) */ |
93 |
|
FVECT nrm; /* average normal direction */ |
94 |
< |
FVECT udir, vdir; /* v-up tangent axes */ |
94 |
> |
FVECT udir, vdir; /* tangent axes */ |
95 |
|
char *outfn; /* output file name (receiver) */ |
96 |
|
int (*sample_basis)(struct param_s *p, int, FILE *); |
97 |
|
} PARAMS; /* sender/receiver parameters */ |
142 |
|
return(ST_NONE); |
143 |
|
} |
144 |
|
|
143 |
– |
/* Add arguments to oconv command */ |
144 |
– |
static char * |
145 |
– |
oconv_command(int ac, char *av[]) |
146 |
– |
{ |
147 |
– |
static char oconvbuf[2048] = "!oconv -f"; |
148 |
– |
char *cp = oconvbuf + 9; |
149 |
– |
|
150 |
– |
while (ac-- > 0) { |
151 |
– |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) { |
152 |
– |
fputs(progname, stderr); |
153 |
– |
fputs(": too many file arguments!\n", stderr); |
154 |
– |
exit(1); |
155 |
– |
} |
156 |
– |
*cp++ = ' '; |
157 |
– |
strcpy(cp, *av++); |
158 |
– |
while (*cp) cp++; |
159 |
– |
} |
160 |
– |
*cp = '\0'; |
161 |
– |
return(oconvbuf); |
162 |
– |
} |
163 |
– |
|
145 |
|
/* Check if any of the characters in str2 are found in str1 */ |
146 |
|
static int |
147 |
|
matchany(const char *str1, const char *str2) |
156 |
|
return(0); |
157 |
|
} |
158 |
|
|
159 |
+ |
/* Add arguments to oconv command */ |
160 |
+ |
static char * |
161 |
+ |
oconv_command(int ac, char *av[]) |
162 |
+ |
{ |
163 |
+ |
static char oconvbuf[2048] = "!oconv -f "; |
164 |
+ |
char *cp = oconvbuf + 10; |
165 |
+ |
char *recv = *av++; |
166 |
+ |
|
167 |
+ |
if (ac-- <= 0) |
168 |
+ |
return(NULL); |
169 |
+ |
while (ac-- > 0) { |
170 |
+ |
strcpy(cp, *av++); |
171 |
+ |
while (*cp) cp++; |
172 |
+ |
*cp++ = ' '; |
173 |
+ |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) |
174 |
+ |
goto overrun; |
175 |
+ |
} |
176 |
+ |
/* receiver goes last */ |
177 |
+ |
if (matchany(recv, SPECIALS)) { |
178 |
+ |
*cp++ = QUOTCHAR; |
179 |
+ |
while (*recv) { |
180 |
+ |
if (cp >= oconvbuf+(sizeof(oconvbuf)-3)) |
181 |
+ |
goto overrun; |
182 |
+ |
*cp++ = *recv++; |
183 |
+ |
} |
184 |
+ |
*cp++ = QUOTCHAR; |
185 |
+ |
*cp = '\0'; |
186 |
+ |
} else |
187 |
+ |
strcpy(cp, recv); |
188 |
+ |
return(oconvbuf); |
189 |
+ |
overrun: |
190 |
+ |
fputs(progname, stderr); |
191 |
+ |
fputs(": too many file arguments!\n", stderr); |
192 |
+ |
exit(1); |
193 |
+ |
} |
194 |
|
|
195 |
|
/* Convert a set of arguments into a command line for pipe() or system() */ |
196 |
|
static char * |
205 |
|
fputs(progname, stderr); |
206 |
|
return(NULL); |
207 |
|
} |
208 |
< |
if ((match = matchany(*av, SPECIALS))) { |
208 |
> |
if (matchany(*av, SPECIALS)) { |
209 |
|
const int quote = |
210 |
|
#ifdef ALTQUOT |
211 |
< |
(match == QUOTCHAR) ? ALTQUOT : |
211 |
> |
strchr(*av, QUOTCHAR) ? ALTQUOT : |
212 |
|
#endif |
213 |
|
QUOTCHAR; |
214 |
|
*cp++ = quote; |
238 |
|
fputs(": command line too long in popen_arglist()\n", stderr); |
239 |
|
return(NULL); |
240 |
|
} |
241 |
< |
if (verbose) |
241 |
> |
if (verbose > 0) |
242 |
|
fprintf(stderr, "%s: opening pipe %s: %s\n", |
243 |
|
progname, (*mode=='w') ? "to" : "from", cmd); |
244 |
|
return(popen(cmd, mode)); |
256 |
|
fputs(": command line too long in my_exec()\n", stderr); |
257 |
|
return(1); |
258 |
|
} |
259 |
< |
if (verbose) |
259 |
> |
if (verbose > 0) |
260 |
|
fprintf(stderr, "%s: running: %s\n", progname, cmd); |
261 |
|
return(system(cmd)); |
262 |
|
} |
271 |
|
fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]); |
272 |
|
return(1); |
273 |
|
} |
274 |
< |
if (verbose) { |
274 |
> |
if (verbose > 0) { |
275 |
|
char cmd[4096]; |
276 |
|
if (!convert_commandline(cmd, sizeof(cmd), av)) |
277 |
|
strcpy(cmd, "COMMAND TOO LONG TO SHOW"); |
329 |
|
{ |
330 |
|
char *cp = pargs; |
331 |
|
int nparams = 0; |
332 |
+ |
int quot; |
333 |
|
int i; |
334 |
|
|
335 |
< |
for ( ; ; ) |
335 |
> |
for ( ; ; ) { |
336 |
|
switch (*cp++) { |
337 |
|
case 'h': |
338 |
|
if (*cp++ != '=') |
339 |
|
break; |
340 |
+ |
if ((*cp == '+') | (*cp == '-')) |
341 |
+ |
p->sign = *cp++; |
342 |
+ |
else |
343 |
+ |
p->sign = '+'; |
344 |
|
p->hsiz = 0; |
345 |
|
i = 0; |
346 |
|
while (*cp && !isspace(*cp)) { |
359 |
|
break; |
360 |
|
if (!get_direction(p->vup, cp)) |
361 |
|
break; |
362 |
+ |
while (*cp && !isspace(*cp++)) |
363 |
+ |
; |
364 |
|
++nparams; |
365 |
|
continue; |
366 |
|
case 'o': |
367 |
|
if (*cp++ != '=') |
368 |
|
break; |
369 |
+ |
quot = 0; |
370 |
+ |
if ((*cp == '"') | (*cp == '\'')) |
371 |
+ |
quot = *cp++; |
372 |
|
i = 0; |
373 |
< |
while (*cp && !isspace(*cp++)) |
374 |
< |
i++; |
373 |
> |
while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) { |
374 |
> |
i++; cp++; |
375 |
> |
} |
376 |
|
if (!i) |
377 |
|
break; |
378 |
< |
*--cp = '\0'; |
378 |
> |
if (!*cp) { |
379 |
> |
if (quot) |
380 |
> |
break; |
381 |
> |
cp[1] = '\0'; |
382 |
> |
} |
383 |
> |
*cp = '\0'; |
384 |
|
p->outfn = savqstr(cp-i); |
385 |
< |
*cp++ = ' '; |
385 |
> |
*cp++ = quot ? quot : ' '; |
386 |
|
++nparams; |
387 |
|
continue; |
388 |
|
case ' ': |
395 |
|
default: |
396 |
|
break; |
397 |
|
} |
398 |
< |
fprintf(stderr, "%s: bad parameter string '%s'\n", progname, pargs); |
398 |
> |
break; |
399 |
> |
} |
400 |
> |
fprintf(stderr, "%s: bad parameter string: %s", progname, pargs); |
401 |
|
exit(1); |
402 |
|
return(-1); /* pro forma return */ |
403 |
|
} |
443 |
|
} |
444 |
|
/* determine sample type/bin */ |
445 |
|
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') { |
446 |
< |
binv = "0"; /* uniform sampling -- one bin */ |
446 |
> |
sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)", |
447 |
> |
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]); |
448 |
> |
binv = savqstr(sbuf); |
449 |
> |
nbins = "1"; /* uniform sampling -- one bin */ |
450 |
|
uniform = 1; |
451 |
|
} else if (tolower(curparams.hemis[0]) == 's' && |
452 |
|
tolower(curparams.hemis[1]) == 'c') { |
457 |
|
exit(1); |
458 |
|
} |
459 |
|
calfn = shirchiufn; shirchiufn = NULL; |
460 |
< |
sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g", |
460 |
> |
sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
461 |
|
curparams.hsiz, |
462 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
463 |
< |
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
463 |
> |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
464 |
> |
curparams.sign); |
465 |
|
params = savqstr(sbuf); |
466 |
|
binv = "scbin"; |
467 |
|
nbins = "SCdim*SCdim"; |
468 |
|
} else if ((tolower(curparams.hemis[0]) == 'r') | |
469 |
|
(tolower(curparams.hemis[0]) == 't')) { |
470 |
|
calfn = reinhfn; reinhfn = NULL; |
471 |
< |
sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g", |
471 |
> |
sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
472 |
|
curparams.hsiz, |
473 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
474 |
< |
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
474 |
> |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
475 |
> |
curparams.sign); |
476 |
|
params = savqstr(sbuf); |
477 |
|
binv = "rbin"; |
478 |
|
nbins = "Nrbins"; |
500 |
|
progname, curparams.hemis); |
501 |
|
exit(1); |
502 |
|
} |
503 |
+ |
if (tolower(curparams.hemis[0]) == 'k') { |
504 |
+ |
sprintf(sbuf, "RHS=%c1", curparams.sign); |
505 |
+ |
params = savqstr(sbuf); |
506 |
+ |
} |
507 |
|
if (!uniform & (curparams.slist->styp == ST_SOURCE)) { |
508 |
|
SURF *sp; |
509 |
|
for (sp = curparams.slist; sp != NULL; sp = sp->next) |
551 |
|
{ |
552 |
|
int i; |
553 |
|
|
554 |
< |
uva[1][0] = 0.5 - frandom(); |
512 |
< |
uva[1][1] = 0.5 - frandom(); |
513 |
< |
uva[1][2] = 0.5 - frandom(); |
514 |
< |
for (i = 3; i--; ) |
515 |
< |
if ((-0.6 < nrm[i]) & (nrm[i] < 0.6)) |
516 |
< |
break; |
517 |
< |
if (i < 0) { |
554 |
> |
if (!getperpendicular(uva[0], nrm, 1)) { |
555 |
|
fputs(progname, stderr); |
556 |
|
fputs(": bad surface normal in make_axes!\n", stderr); |
557 |
|
exit(1); |
558 |
|
} |
559 |
< |
uva[1][i] = 1.0; |
523 |
< |
VCROSS(uva[0], uva[1], nrm); |
524 |
< |
normalize(uva[0]); |
525 |
< |
VCROSS(uva[1], nrm, uva[0]); |
559 |
> |
fcross(uva[1], nrm, uva[0]); |
560 |
|
} |
561 |
|
|
562 |
|
/* Illegal sender surfaces end up here */ |
695 |
|
/* special case for lone surface */ |
696 |
|
if (p->nsurfs == 1) { |
697 |
|
sp = p->slist; |
698 |
< |
if (DOT(sp->snrm, rdir) >= -FTINY) { |
698 |
> |
if (DOT(sp->snrm, rdir) >= FTINY) { |
699 |
|
fprintf(stderr, |
700 |
|
"%s: internal - sample behind sender '%s'\n", |
701 |
|
progname, sp->sname); |
818 |
|
alt = (row+samp3[1])*RAH; |
819 |
|
azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row); |
820 |
|
duvw[2] = cos(alt); /* measured from horizon */ |
821 |
< |
duvw[0] = tcos(azi)*duvw[2]; |
822 |
< |
duvw[1] = tsin(azi)*duvw[2]; |
821 |
> |
duvw[0] = tsin(azi)*duvw[2]; |
822 |
> |
duvw[1] = tcos(azi)*duvw[2]; |
823 |
|
duvw[2] = sqrt(1. - duvw[2]*duvw[2]); |
824 |
|
for (i = 3; i--; ) |
825 |
|
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
870 |
|
|
871 |
|
while (n--) { /* stratified sampling */ |
872 |
|
SDmultiSamp(samp2, 2, (n+frandom())/sampcnt); |
873 |
< |
if (!bo_getvec(duvw, b+samp2[1], kbasis[bi])) |
873 |
> |
if (!fo_getvec(duvw, b+samp2[1], kbasis[bi])) |
874 |
|
return(0); |
875 |
|
for (i = 3; i--; ) |
876 |
< |
orig_dir[1][i] = duvw[0]*p->udir[i] + |
877 |
< |
duvw[1]*p->vdir[i] + |
876 |
> |
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
877 |
> |
duvw[1]*p->vdir[i] - |
878 |
|
duvw[2]*p->nrm[i] ; |
879 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0])) |
880 |
|
return(0); |
893 |
|
fputs(": no sender surface!\n", stderr); |
894 |
|
return(-1); |
895 |
|
} |
896 |
< |
if (curparams.outfn != NULL) /* misplaced output file spec. */ |
896 |
> |
/* misplaced output file spec. */ |
897 |
> |
if ((curparams.outfn != NULL) & (verbose >= 0)) |
898 |
|
fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n", |
899 |
|
progname, curparams.outfn); |
900 |
|
/* check/set basis hemisphere */ |
914 |
|
else |
915 |
|
curparams.vup[1] = 1; |
916 |
|
} |
917 |
< |
VCROSS(curparams.udir, curparams.vup, curparams.nrm); |
917 |
> |
fcross(curparams.udir, curparams.vup, curparams.nrm); |
918 |
|
if (normalize(curparams.udir) == 0) { |
919 |
|
fputs(progname, stderr); |
920 |
|
fputs(": up vector coincides with sender normal\n", stderr); |
921 |
|
return(-1); |
922 |
|
} |
923 |
< |
VCROSS(curparams.vdir, curparams.nrm, curparams.udir); |
923 |
> |
fcross(curparams.vdir, curparams.nrm, curparams.udir); |
924 |
> |
if (curparams.sign == '-') { /* left-handed coordinate system? */ |
925 |
> |
curparams.udir[0] *= -1.; |
926 |
> |
curparams.udir[1] *= -1.; |
927 |
> |
curparams.udir[2] *= -1.; |
928 |
> |
} |
929 |
|
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') |
930 |
|
curparams.sample_basis = sample_uniform; |
931 |
|
else if (tolower(curparams.hemis[0]) == 's' && |
1050 |
|
snew->area *= PI*snew->area; |
1051 |
|
break; |
1052 |
|
} |
1053 |
< |
if (snew->area <= FTINY) { |
1053 |
> |
if ((snew->area <= FTINY) & (verbose >= 0)) { |
1054 |
|
fprintf(stderr, "%s: warning - zero area for surface '%s'\n", |
1055 |
|
progname, oname); |
1056 |
|
free(snew); |
1120 |
|
add_send_object(FILE *fp) |
1121 |
|
{ |
1122 |
|
int st; |
1123 |
< |
char otype[32], oname[128]; |
1123 |
> |
char thismod[128], otype[32], oname[128]; |
1124 |
|
int n; |
1125 |
|
|
1126 |
< |
if (fscanf(fp, "%*s %s %s", otype, oname) != 2) |
1126 |
> |
if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3) |
1127 |
|
return(0); /* must have hit EOF! */ |
1128 |
|
if (!strcmp(otype, "alias")) { |
1129 |
|
fscanf(fp, "%*s"); /* skip alias */ |
1136 |
|
fputs(": cannot use source as a sender!\n", stderr); |
1137 |
|
return(-1); |
1138 |
|
} |
1139 |
+ |
if (strcmp(thismod, curmod)) { |
1140 |
+ |
if (curmod[0]) { |
1141 |
+ |
fputs(progname, stderr); |
1142 |
+ |
fputs(": warning - multiple modifiers in sender\n", |
1143 |
+ |
stderr); |
1144 |
+ |
} |
1145 |
+ |
strcpy(curmod, thismod); |
1146 |
+ |
} |
1147 |
|
parse_params(&curparams, newparams); |
1148 |
|
newparams[0] = '\0'; |
1149 |
|
add_surface(st, oname, fp); /* read & store surface */ |
1230 |
|
int a, i; |
1231 |
|
/* screen rcontrib options */ |
1232 |
|
progname = argv[0]; |
1233 |
< |
for (a = 1; a < argc-2 && argv[a][0] == '-'; a++) { |
1234 |
< |
int na = 1; /* !! Keep consistent !! */ |
1235 |
< |
switch (argv[a][1]) { |
1233 |
> |
for (a = 1; a < argc-2; a++) { |
1234 |
> |
int na; |
1235 |
> |
/* check for argument expansion */ |
1236 |
> |
while ((na = expandarg(&argc, &argv, a)) > 0) |
1237 |
> |
; |
1238 |
> |
if (na < 0) { |
1239 |
> |
fprintf(stderr, "%s: cannot expand '%s'\n", |
1240 |
> |
progname, argv[a]); |
1241 |
> |
return(1); |
1242 |
> |
} |
1243 |
> |
if (argv[a][0] != '-' || !argv[a][1]) |
1244 |
> |
break; |
1245 |
> |
na = 1; |
1246 |
> |
switch (argv[a][1]) { /* !! Keep consistent !! */ |
1247 |
|
case 'v': /* verbose mode */ |
1248 |
< |
verbose = !verbose; |
1248 |
> |
verbose = 1; |
1249 |
|
na = 0; |
1250 |
|
continue; |
1251 |
|
case 'f': /* special case for -fo, -ff, etc. */ |
1286 |
|
iropt = argv[a]; |
1287 |
|
na = 0; |
1288 |
|
continue; |
1289 |
< |
case 'V': /* options without arguments */ |
1290 |
< |
case 'w': |
1289 |
> |
case 'w': /* options without arguments */ |
1290 |
> |
if (argv[a][2] != '+') verbose = -1; |
1291 |
> |
case 'V': |
1292 |
|
case 'u': |
1293 |
|
case 'h': |
1294 |
|
case 'r': |
1313 |
|
if (argv[a][2] != 'v') na = 2; |
1314 |
|
break; |
1315 |
|
case 'a': /* special case */ |
1316 |
< |
na = (argv[a][2] == 'v') ? 4 : 2; |
1316 |
> |
if (argv[a][2] == 'p') { |
1317 |
> |
na = 2; /* photon map [+ bandwidth(s)] */ |
1318 |
> |
if (a < argc-3 && atoi(argv[a+1]) > 0) |
1319 |
> |
na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0); |
1320 |
> |
} else |
1321 |
> |
na = (argv[a][2] == 'v') ? 4 : 2; |
1322 |
|
break; |
1323 |
|
case 'm': /* special case */ |
1324 |
|
if (!argv[a][2]) goto userr; |
1325 |
|
na = (argv[a][2] == 'e') | (argv[a][2] == 'a') ? 4 : 2; |
1326 |
|
break; |
1262 |
– |
case '\0': /* pass-through mode */ |
1263 |
– |
goto done_opts; |
1327 |
|
default: /* anything else is verbotten */ |
1328 |
|
goto userr; |
1329 |
|
} |
1333 |
|
while (--na) /* + arguments if any */ |
1334 |
|
rcarg[nrcargs++] = argv[++a]; |
1335 |
|
} |
1273 |
– |
done_opts: |
1336 |
|
if (a > argc-2) |
1337 |
|
goto userr; /* check at end of options */ |
1338 |
|
sendfn = argv[a++]; /* assign sender & receiver inputs */ |
1383 |
|
return(my_exec(rcarg)); /* rcontrib does everything */ |
1384 |
|
} |
1385 |
|
clear_params(&curparams, 0); /* else load sender surface & params */ |
1386 |
+ |
curmod[0] = '\0'; |
1387 |
|
if (load_scene(sendfn, add_send_object) < 0) |
1388 |
|
return(1); |
1389 |
|
if ((nsbins = prepare_sampler()) <= 0) |
1401 |
|
#ifdef getc_unlocked |
1402 |
|
flockfile(rcfp); |
1403 |
|
#endif |
1404 |
< |
if (verbose) { |
1404 |
> |
if (verbose > 0) { |
1405 |
|
fprintf(stderr, "%s: sampling %d directions", progname, nsbins); |
1406 |
|
if (curparams.nsurfs > 1) |
1407 |
|
fprintf(stderr, " (%d elements)\n", curparams.nsurfs); |
1411 |
|
for (i = 0; i < nsbins; i++) /* send rcontrib ray samples */ |
1412 |
|
if (!(*curparams.sample_basis)(&curparams, i, rcfp)) |
1413 |
|
return(1); |
1414 |
< |
return(pclose(rcfp) == 0); /* all finished! */ |
1414 |
> |
return(pclose(rcfp) < 0); /* all finished! */ |
1415 |
|
userr: |
1416 |
|
if (a < argc-2) |
1417 |
|
fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]); |
1418 |
< |
fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [system.rad ..]\n", |
1418 |
> |
fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [-i system.oct] [system.rad ..]\n", |
1419 |
|
progname); |
1420 |
|
return(1); |
1421 |
|
} |