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 */ |
146 |
|
static char * |
147 |
|
oconv_command(int ac, char *av[]) |
148 |
|
{ |
149 |
< |
static char oconvbuf[2048] = "!oconv -f"; |
150 |
< |
char *cp = oconvbuf + 9; |
151 |
< |
|
149 |
> |
static char oconvbuf[2048] = "!oconv -f "; |
150 |
> |
char *cp = oconvbuf + 10; |
151 |
> |
char *recv = *av++; |
152 |
> |
|
153 |
> |
if (ac-- <= 0) |
154 |
> |
return(NULL); |
155 |
|
while (ac-- > 0) { |
156 |
+ |
strcpy(cp, *av++); |
157 |
+ |
while (*cp) cp++; |
158 |
+ |
*cp++ = ' '; |
159 |
|
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) { |
160 |
|
fputs(progname, stderr); |
161 |
|
fputs(": too many file arguments!\n", stderr); |
162 |
|
exit(1); |
163 |
|
} |
156 |
– |
*cp++ = ' '; |
157 |
– |
strcpy(cp, *av++); |
158 |
– |
while (*cp) cp++; |
164 |
|
} |
165 |
< |
*cp = '\0'; |
165 |
> |
strcpy(cp, recv); /* receiver goes last */ |
166 |
|
return(oconvbuf); |
167 |
|
} |
168 |
|
|
227 |
|
fputs(": command line too long in popen_arglist()\n", stderr); |
228 |
|
return(NULL); |
229 |
|
} |
230 |
< |
if (verbose) |
230 |
> |
if (verbose > 0) |
231 |
|
fprintf(stderr, "%s: opening pipe %s: %s\n", |
232 |
|
progname, (*mode=='w') ? "to" : "from", cmd); |
233 |
|
return(popen(cmd, mode)); |
245 |
|
fputs(": command line too long in my_exec()\n", stderr); |
246 |
|
return(1); |
247 |
|
} |
248 |
< |
if (verbose) |
248 |
> |
if (verbose > 0) |
249 |
|
fprintf(stderr, "%s: running: %s\n", progname, cmd); |
250 |
|
return(system(cmd)); |
251 |
|
} |
260 |
|
fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]); |
261 |
|
return(1); |
262 |
|
} |
263 |
< |
if (verbose) { |
263 |
> |
if (verbose > 0) { |
264 |
|
char cmd[4096]; |
265 |
|
if (!convert_commandline(cmd, sizeof(cmd), av)) |
266 |
|
strcpy(cmd, "COMMAND TOO LONG TO SHOW"); |
320 |
|
int nparams = 0; |
321 |
|
int i; |
322 |
|
|
323 |
< |
for ( ; ; ) |
323 |
> |
for ( ; ; ) { |
324 |
|
switch (*cp++) { |
325 |
|
case 'h': |
326 |
|
if (*cp++ != '=') |
327 |
|
break; |
328 |
+ |
if ((*cp == '+') | (*cp == '-')) |
329 |
+ |
p->sign = *cp++; |
330 |
+ |
else |
331 |
+ |
p->sign = '+'; |
332 |
|
p->hsiz = 0; |
333 |
|
i = 0; |
334 |
|
while (*cp && !isspace(*cp)) { |
347 |
|
break; |
348 |
|
if (!get_direction(p->vup, cp)) |
349 |
|
break; |
350 |
+ |
while (*cp && !isspace(*cp++)) |
351 |
+ |
; |
352 |
|
++nparams; |
353 |
|
continue; |
354 |
|
case 'o': |
374 |
|
default: |
375 |
|
break; |
376 |
|
} |
377 |
< |
fprintf(stderr, "%s: bad parameter string '%s'\n", progname, pargs); |
377 |
> |
break; |
378 |
> |
} |
379 |
> |
fprintf(stderr, "%s: bad parameter string: %s", progname, pargs); |
380 |
|
exit(1); |
381 |
|
return(-1); /* pro forma return */ |
382 |
|
} |
414 |
|
fputs(": undefined normal for hemisphere sampling\n", stderr); |
415 |
|
exit(1); |
416 |
|
} |
417 |
< |
if (normalize(curparams.vup) == 0) |
417 |
> |
if (normalize(curparams.vup) == 0) { |
418 |
|
if (fabs(curparams.nrm[2]) < .7) |
419 |
|
curparams.vup[2] = 1; |
420 |
|
else |
421 |
|
curparams.vup[1] = 1; |
422 |
+ |
} |
423 |
|
/* determine sample type/bin */ |
424 |
|
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') { |
425 |
< |
binv = "0"; /* uniform sampling -- one bin */ |
425 |
> |
sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)", |
426 |
> |
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]); |
427 |
> |
binv = savqstr(sbuf); |
428 |
> |
nbins = "1"; /* uniform sampling -- one bin */ |
429 |
|
uniform = 1; |
430 |
|
} else if (tolower(curparams.hemis[0]) == 's' && |
431 |
|
tolower(curparams.hemis[1]) == 'c') { |
436 |
|
exit(1); |
437 |
|
} |
438 |
|
calfn = shirchiufn; shirchiufn = NULL; |
439 |
< |
sprintf(sbuf, "SCdim=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g", |
439 |
> |
sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
440 |
|
curparams.hsiz, |
441 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
442 |
< |
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
442 |
> |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
443 |
> |
curparams.sign); |
444 |
|
params = savqstr(sbuf); |
445 |
|
binv = "scbin"; |
446 |
|
nbins = "SCdim*SCdim"; |
447 |
|
} else if ((tolower(curparams.hemis[0]) == 'r') | |
448 |
|
(tolower(curparams.hemis[0]) == 't')) { |
449 |
|
calfn = reinhfn; reinhfn = NULL; |
450 |
< |
sprintf(sbuf, "MF=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g", |
450 |
> |
sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
451 |
|
curparams.hsiz, |
452 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
453 |
< |
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
453 |
> |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
454 |
> |
curparams.sign); |
455 |
|
params = savqstr(sbuf); |
456 |
|
binv = "rbin"; |
457 |
|
nbins = "Nrbins"; |
479 |
|
progname, curparams.hemis); |
480 |
|
exit(1); |
481 |
|
} |
482 |
+ |
if (tolower(curparams.hemis[0]) == 'k') { |
483 |
+ |
sprintf(sbuf, "RHS=%c1", curparams.sign); |
484 |
+ |
params = savqstr(sbuf); |
485 |
+ |
} |
486 |
|
if (!uniform & (curparams.slist->styp == ST_SOURCE)) { |
487 |
|
SURF *sp; |
488 |
|
for (sp = curparams.slist; sp != NULL; sp = sp->next) |
530 |
|
{ |
531 |
|
int i; |
532 |
|
|
533 |
< |
uva[1][0] = 0.5 - frandom(); |
511 |
< |
uva[1][1] = 0.5 - frandom(); |
512 |
< |
uva[1][2] = 0.5 - frandom(); |
513 |
< |
for (i = 3; i--; ) |
514 |
< |
if ((-0.6 < nrm[i]) & (nrm[i] < 0.6)) |
515 |
< |
break; |
516 |
< |
if (i < 0) { |
533 |
> |
if (!getperpendicular(uva[0], nrm)) { |
534 |
|
fputs(progname, stderr); |
535 |
|
fputs(": bad surface normal in make_axes!\n", stderr); |
536 |
|
exit(1); |
537 |
|
} |
538 |
< |
uva[1][i] = 1.0; |
522 |
< |
VCROSS(uva[0], uva[1], nrm); |
523 |
< |
normalize(uva[0]); |
524 |
< |
VCROSS(uva[1], nrm, uva[0]); |
538 |
> |
fcross(uva[1], nrm, uva[0]); |
539 |
|
} |
540 |
|
|
541 |
|
/* Illegal sender surfaces end up here */ |
797 |
|
alt = (row+samp3[1])*RAH; |
798 |
|
azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row); |
799 |
|
duvw[2] = cos(alt); /* measured from horizon */ |
800 |
< |
duvw[0] = tcos(azi)*duvw[2]; |
801 |
< |
duvw[1] = tsin(azi)*duvw[2]; |
800 |
> |
duvw[0] = tsin(azi)*duvw[2]; |
801 |
> |
duvw[1] = tcos(azi)*duvw[2]; |
802 |
|
duvw[2] = sqrt(1. - duvw[2]*duvw[2]); |
803 |
|
for (i = 3; i--; ) |
804 |
|
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
849 |
|
|
850 |
|
while (n--) { /* stratified sampling */ |
851 |
|
SDmultiSamp(samp2, 2, (n+frandom())/sampcnt); |
852 |
< |
if (!bo_getvec(duvw, b+samp2[1], kbasis[bi])) |
852 |
> |
if (!fo_getvec(duvw, b+samp2[1], kbasis[bi])) |
853 |
|
return(0); |
854 |
|
for (i = 3; i--; ) |
855 |
< |
orig_dir[1][i] = duvw[0]*p->udir[i] + |
856 |
< |
duvw[1]*p->vdir[i] + |
855 |
> |
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
856 |
> |
duvw[1]*p->vdir[i] - |
857 |
|
duvw[2]*p->nrm[i] ; |
858 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0])) |
859 |
|
return(0); |
872 |
|
fputs(": no sender surface!\n", stderr); |
873 |
|
return(-1); |
874 |
|
} |
875 |
< |
if (curparams.outfn != NULL) /* misplaced output file spec. */ |
875 |
> |
/* misplaced output file spec. */ |
876 |
> |
if ((curparams.outfn != NULL) & (verbose >= 0)) |
877 |
|
fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n", |
878 |
|
progname, curparams.outfn); |
879 |
|
/* check/set basis hemisphere */ |
887 |
|
fputs(": undefined normal for sender sampling\n", stderr); |
888 |
|
return(-1); |
889 |
|
} |
890 |
< |
if (normalize(curparams.vup) == 0) |
890 |
> |
if (normalize(curparams.vup) == 0) { |
891 |
|
if (fabs(curparams.nrm[2]) < .7) |
892 |
|
curparams.vup[2] = 1; |
893 |
|
else |
894 |
|
curparams.vup[1] = 1; |
895 |
< |
VCROSS(curparams.udir, curparams.vup, curparams.nrm); |
895 |
> |
} |
896 |
> |
fcross(curparams.udir, curparams.vup, curparams.nrm); |
897 |
|
if (normalize(curparams.udir) == 0) { |
898 |
|
fputs(progname, stderr); |
899 |
|
fputs(": up vector coincides with sender normal\n", stderr); |
900 |
|
return(-1); |
901 |
|
} |
902 |
< |
VCROSS(curparams.vdir, curparams.nrm, curparams.udir); |
902 |
> |
fcross(curparams.vdir, curparams.nrm, curparams.udir); |
903 |
> |
if (curparams.sign == '-') { /* left-handed coordinate system? */ |
904 |
> |
curparams.udir[0] *= -1.; |
905 |
> |
curparams.udir[1] *= -1.; |
906 |
> |
curparams.udir[2] *= -1.; |
907 |
> |
} |
908 |
|
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') |
909 |
|
curparams.sample_basis = sample_uniform; |
910 |
|
else if (tolower(curparams.hemis[0]) == 's' && |
1029 |
|
snew->area *= PI*snew->area; |
1030 |
|
break; |
1031 |
|
} |
1032 |
< |
if (snew->area <= FTINY) { |
1032 |
> |
if ((snew->area <= FTINY) & (verbose >= 0)) { |
1033 |
|
fprintf(stderr, "%s: warning - zero area for surface '%s'\n", |
1034 |
|
progname, oname); |
1035 |
|
free(snew); |
1087 |
|
/* else skip arguments */ |
1088 |
|
if (!fscanf(fp, "%d", &n)) return(0); |
1089 |
|
while (n-- > 0) fscanf(fp, "%*s"); |
1090 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1090 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1091 |
|
while (n-- > 0) fscanf(fp, "%*d"); |
1092 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1092 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1093 |
|
while (n-- > 0) fscanf(fp, "%*f"); |
1094 |
|
return(0); |
1095 |
|
} |
1099 |
|
add_send_object(FILE *fp) |
1100 |
|
{ |
1101 |
|
int st; |
1102 |
< |
char otype[32], oname[128]; |
1102 |
> |
char thismod[128], otype[32], oname[128]; |
1103 |
|
int n; |
1104 |
|
|
1105 |
< |
if (fscanf(fp, "%*s %s %s", otype, oname) != 2) |
1105 |
> |
if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3) |
1106 |
|
return(0); /* must have hit EOF! */ |
1107 |
|
if (!strcmp(otype, "alias")) { |
1108 |
|
fscanf(fp, "%*s"); /* skip alias */ |
1115 |
|
fputs(": cannot use source as a sender!\n", stderr); |
1116 |
|
return(-1); |
1117 |
|
} |
1118 |
+ |
if (strcmp(thismod, curmod)) { |
1119 |
+ |
if (curmod[0]) { |
1120 |
+ |
fputs(progname, stderr); |
1121 |
+ |
fputs(": warning - multiple modifiers in sender\n", |
1122 |
+ |
stderr); |
1123 |
+ |
} |
1124 |
+ |
strcpy(curmod, thismod); |
1125 |
+ |
} |
1126 |
|
parse_params(&curparams, newparams); |
1127 |
|
newparams[0] = '\0'; |
1128 |
|
add_surface(st, oname, fp); /* read & store surface */ |
1131 |
|
/* else skip arguments */ |
1132 |
|
if (!fscanf(fp, "%d", &n)) return(0); |
1133 |
|
while (n-- > 0) fscanf(fp, "%*s"); |
1134 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1134 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1135 |
|
while (n-- > 0) fscanf(fp, "%*d"); |
1136 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1136 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1137 |
|
while (n-- > 0) fscanf(fp, "%*f"); |
1138 |
|
return(0); |
1139 |
|
} |
1176 |
|
strcat(newparams, inpbuf); |
1177 |
|
continue; |
1178 |
|
} |
1179 |
< |
while ((c = getc(fp)) != EOF && c != '\n'); |
1179 |
> |
while ((c = getc(fp)) != EOF && c != '\n') |
1180 |
|
; /* else skipping comment */ |
1181 |
|
continue; |
1182 |
|
} |
1201 |
|
{ |
1202 |
|
char fmtopt[6] = "-faa"; /* default output is ASCII */ |
1203 |
|
char *xrs=NULL, *yrs=NULL, *ldopt=NULL; |
1204 |
< |
int wantIrradiance = 0; |
1204 |
> |
char *iropt = NULL; |
1205 |
|
char *sendfn; |
1206 |
|
char sampcntbuf[32], nsbinbuf[32]; |
1207 |
|
FILE *rcfp; |
1209 |
|
int a, i; |
1210 |
|
/* screen rcontrib options */ |
1211 |
|
progname = argv[0]; |
1212 |
< |
for (a = 1; a < argc-2 && argv[a][0] == '-'; a++) { |
1213 |
< |
int na = 1; /* !! Keep consistent !! */ |
1214 |
< |
switch (argv[a][1]) { |
1212 |
> |
for (a = 1; a < argc-2; a++) { |
1213 |
> |
int na; |
1214 |
> |
/* check for argument expansion */ |
1215 |
> |
while ((na = expandarg(&argc, &argv, a)) > 0) |
1216 |
> |
; |
1217 |
> |
if (na < 0) { |
1218 |
> |
fprintf(stderr, "%s: cannot expand '%s'\n", |
1219 |
> |
progname, argv[a]); |
1220 |
> |
return(1); |
1221 |
> |
} |
1222 |
> |
if (argv[a][0] != '-' || !argv[a][1]) |
1223 |
> |
break; |
1224 |
> |
na = 1; |
1225 |
> |
switch (argv[a][1]) { /* !! Keep consistent !! */ |
1226 |
|
case 'v': /* verbose mode */ |
1227 |
< |
verbose = !verbose; |
1227 |
> |
verbose = 1; |
1228 |
|
na = 0; |
1229 |
|
continue; |
1230 |
|
case 'f': /* special case for -fo, -ff, etc. */ |
1261 |
|
na = 0; /* we re-add this later */ |
1262 |
|
continue; |
1263 |
|
case 'I': /* only for pass-through mode */ |
1264 |
< |
wantIrradiance = 1; |
1264 |
> |
case 'i': |
1265 |
> |
iropt = argv[a]; |
1266 |
|
na = 0; |
1267 |
|
continue; |
1268 |
< |
case 'V': /* options without arguments */ |
1269 |
< |
case 'w': |
1268 |
> |
case 'w': /* options without arguments */ |
1269 |
> |
if (argv[a][2] != '+') verbose = -1; |
1270 |
> |
case 'V': |
1271 |
|
case 'u': |
1230 |
– |
case 'i': |
1272 |
|
case 'h': |
1273 |
|
case 'r': |
1274 |
|
break; |
1298 |
|
if (!argv[a][2]) goto userr; |
1299 |
|
na = (argv[a][2] == 'e') | (argv[a][2] == 'a') ? 4 : 2; |
1300 |
|
break; |
1260 |
– |
case '\0': /* pass-through mode */ |
1261 |
– |
goto done_opts; |
1301 |
|
default: /* anything else is verbotten */ |
1302 |
|
goto userr; |
1303 |
|
} |
1307 |
|
while (--na) /* + arguments if any */ |
1308 |
|
rcarg[nrcargs++] = argv[++a]; |
1309 |
|
} |
1271 |
– |
done_opts: |
1310 |
|
if (a > argc-2) |
1311 |
|
goto userr; /* check at end of options */ |
1312 |
|
sendfn = argv[a++]; /* assign sender & receiver inputs */ |
1313 |
|
if (sendfn[0] == '-') { /* user wants pass-through mode? */ |
1314 |
|
if (sendfn[1]) goto userr; |
1315 |
|
sendfn = NULL; |
1316 |
< |
if (wantIrradiance) { |
1316 |
> |
if (iropt) { |
1317 |
|
CHECKARGC(1); |
1318 |
< |
rcarg[nrcargs++] = "-I"; |
1318 |
> |
rcarg[nrcargs++] = iropt; |
1319 |
|
} |
1320 |
|
if (xrs) { |
1321 |
|
CHECKARGC(2); |
1333 |
|
} |
1334 |
|
if (sampcnt <= 0) sampcnt = 1; |
1335 |
|
} else { /* else in sampling mode */ |
1336 |
< |
if (wantIrradiance) { |
1336 |
> |
if (iropt) { |
1337 |
|
fputs(progname, stderr); |
1338 |
< |
fputs(": -I supported for pass-through only\n", stderr); |
1338 |
> |
fputs(": -i, -I supported for pass-through only\n", stderr); |
1339 |
|
return(1); |
1340 |
|
} |
1341 |
|
fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; |
1357 |
|
return(my_exec(rcarg)); /* rcontrib does everything */ |
1358 |
|
} |
1359 |
|
clear_params(&curparams, 0); /* else load sender surface & params */ |
1360 |
+ |
curmod[0] = '\0'; |
1361 |
|
if (load_scene(sendfn, add_send_object) < 0) |
1362 |
|
return(1); |
1363 |
|
if ((nsbins = prepare_sampler()) <= 0) |
1375 |
|
#ifdef getc_unlocked |
1376 |
|
flockfile(rcfp); |
1377 |
|
#endif |
1378 |
< |
if (verbose) { |
1378 |
> |
if (verbose > 0) { |
1379 |
|
fprintf(stderr, "%s: sampling %d directions", progname, nsbins); |
1380 |
|
if (curparams.nsurfs > 1) |
1381 |
|
fprintf(stderr, " (%d elements)\n", curparams.nsurfs); |
1385 |
|
for (i = 0; i < nsbins; i++) /* send rcontrib ray samples */ |
1386 |
|
if (!(*curparams.sample_basis)(&curparams, i, rcfp)) |
1387 |
|
return(1); |
1388 |
< |
return(pclose(rcfp) == 0); /* all finished! */ |
1388 |
> |
return(pclose(rcfp) < 0); /* all finished! */ |
1389 |
|
userr: |
1390 |
|
if (a < argc-2) |
1391 |
|
fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]); |
1392 |
< |
fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [system.rad ..]\n", |
1392 |
> |
fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [-i system.oct] [system.rad ..]\n", |
1393 |
|
progname); |
1394 |
|
return(1); |
1395 |
|
} |