11 |
|
#include <stdlib.h> |
12 |
|
#include "rtio.h" |
13 |
|
#include "rtmath.h" |
14 |
< |
#include "paths.h" |
14 |
> |
#include "rtprocess.h" |
15 |
|
#include "bsdf.h" |
16 |
|
#include "bsdf_m.h" |
17 |
|
#include "random.h" |
22 |
|
#define MAXRCARG 10000 |
23 |
|
#endif |
24 |
|
|
25 |
– |
char *progname; /* global argv[0] */ |
26 |
– |
|
25 |
|
int verbose = 0; /* verbose mode (< 0 no warnings) */ |
26 |
|
|
27 |
|
char *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"}; |
39 |
|
char *kfullfn = "klems_full.cal"; |
40 |
|
char *khalffn = "klems_half.cal"; |
41 |
|
char *kquarterfn = "klems_quarter.cal"; |
42 |
+ |
char *ciefn = "cieskyscan.cal"; |
43 |
|
|
44 |
|
/* string indicating parameters */ |
45 |
|
const char PARAMSTART[] = "@rfluxmtx"; |
140 |
|
if (ac-- <= 0) |
141 |
|
return(NULL); |
142 |
|
if (verbose < 0) { /* turn off warnings */ |
143 |
< |
strcpy(cp, "-w- "); |
144 |
< |
cp += 4; |
143 |
> |
strcpy(cp, "-w "); |
144 |
> |
cp += 3; |
145 |
|
} |
146 |
< |
while (ac-- > 0) { |
147 |
< |
strcpy(cp, *av++); |
148 |
< |
while (*cp) cp++; |
150 |
< |
*cp++ = ' '; |
151 |
< |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) |
146 |
> |
while (ac-- > 0) { /* copy each argument */ |
147 |
> |
int len = strlen(*av); |
148 |
> |
if (cp+len+4 >= oconvbuf+sizeof(oconvbuf)) |
149 |
|
goto overrun; |
150 |
+ |
if (matchany(*av, SPECIALS)) { |
151 |
+ |
*cp++ = QUOTCHAR; |
152 |
+ |
strcpy(cp, *av++); |
153 |
+ |
cp += len; |
154 |
+ |
*cp++ = QUOTCHAR; |
155 |
+ |
} else { |
156 |
+ |
strcpy(cp, *av++); |
157 |
+ |
cp += len; |
158 |
+ |
} |
159 |
+ |
*cp++ = ' '; |
160 |
|
} |
161 |
|
/* receiver goes last */ |
162 |
|
if (matchany(recv, SPECIALS)) { |
177 |
|
exit(1); |
178 |
|
} |
179 |
|
|
180 |
+ |
#if defined(_WIN32) || defined(_WIN64) |
181 |
+ |
|
182 |
|
/* Open a pipe to/from a command given as an argument list */ |
183 |
|
static FILE * |
184 |
|
popen_arglist(char *av[], char *mode) |
196 |
|
return(popen(cmd, mode)); |
197 |
|
} |
198 |
|
|
199 |
< |
#if defined(_WIN32) || defined(_WIN64) |
199 |
> |
#define pclose_al pclose |
200 |
> |
|
201 |
|
/* Execute system command (Windows version) */ |
202 |
|
static int |
203 |
|
my_exec(char *av[]) |
213 |
|
fprintf(stderr, "%s: running: %s\n", progname, cmd); |
214 |
|
return(system(cmd)); |
215 |
|
} |
216 |
< |
#else |
216 |
> |
|
217 |
> |
#else /* UNIX */ |
218 |
> |
|
219 |
> |
static SUBPROC rt_proc = SP_INACTIVE; /* we only support one of these */ |
220 |
> |
|
221 |
> |
/* Open a pipe to a command using an argument list */ |
222 |
> |
static FILE * |
223 |
> |
popen_arglist(char *av[], char *mode) |
224 |
> |
{ |
225 |
> |
int fd; |
226 |
> |
|
227 |
> |
if (rt_proc.pid > 0) { |
228 |
> |
fprintf(stderr, "%s: only one i/o pipe at a time!\n", progname); |
229 |
> |
return(NULL); |
230 |
> |
} |
231 |
> |
if (verbose > 0) { |
232 |
> |
char cmd[4096]; |
233 |
> |
if (!convert_commandline(cmd, sizeof(cmd), av)) |
234 |
> |
strcpy(cmd, "COMMAND TOO LONG TO SHOW"); |
235 |
> |
fprintf(stderr, "%s: opening pipe %s: %s\n", |
236 |
> |
progname, (*mode=='w') ? "to" : "from", cmd); |
237 |
> |
} |
238 |
> |
if (*mode == 'w') { |
239 |
> |
fd = rt_proc.w = dup(fileno(stdout)); |
240 |
> |
rt_proc.flags |= PF_FILT_OUT; |
241 |
> |
} else if (*mode == 'r') { |
242 |
> |
fd = rt_proc.r = dup(fileno(stdin)); |
243 |
> |
rt_proc.flags |= PF_FILT_INP; |
244 |
> |
} |
245 |
> |
if (fd < 0 || open_process(&rt_proc, av) <= 0) { |
246 |
> |
perror(av[0]); |
247 |
> |
return(NULL); |
248 |
> |
} |
249 |
> |
return(fdopen(fd, mode)); |
250 |
> |
} |
251 |
> |
|
252 |
> |
/* Close command pipe (returns -1 on error to match pclose) */ |
253 |
> |
static int |
254 |
> |
pclose_al(FILE *fp) |
255 |
> |
{ |
256 |
> |
int prob = (fclose(fp) == EOF); |
257 |
> |
|
258 |
> |
if (rt_proc.pid <= 0) |
259 |
> |
return(-1); |
260 |
> |
|
261 |
> |
prob |= (close_process(&rt_proc) != 0); |
262 |
> |
|
263 |
> |
return(-prob); |
264 |
> |
} |
265 |
> |
|
266 |
|
/* Execute system command in our stead (Unix version) */ |
267 |
|
static int |
268 |
|
my_exec(char *av[]) |
283 |
|
perror(compath); |
284 |
|
return(1); |
285 |
|
} |
286 |
+ |
|
287 |
|
#endif |
288 |
|
|
289 |
|
/* Get normalized direction vector from string specification */ |
498 |
|
calfn = kquarterfn; kquarterfn = NULL; |
499 |
|
binf = "kqbin"; |
500 |
|
nbins = "Nkqbins"; |
501 |
+ |
} else if (!strcasecmp(curparams.hemis, "cie")) { |
502 |
+ |
calfn = ciefn; ciefn = NULL; |
503 |
+ |
sprintf(sbuf, "rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
504 |
+ |
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
505 |
+ |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
506 |
+ |
curparams.sign); |
507 |
+ |
binv = "cbin"; |
508 |
+ |
nbins = "Ncbins"; |
509 |
|
} else { |
510 |
|
fprintf(stderr, "%s: unrecognized hemisphere sampling: h=%s\n", |
511 |
|
progname, curparams.hemis); |
515 |
|
sprintf(sbuf, "RHS=%c1", curparams.sign); |
516 |
|
params = savqstr(sbuf); |
517 |
|
} |
518 |
< |
if (!uniform & (curparams.slist->styp == ST_SOURCE)) { |
518 |
> |
if (!uniform) { |
519 |
|
SURF *sp; |
520 |
|
for (sp = curparams.slist; sp != NULL; sp = sp->next) |
521 |
< |
if (fabs(sp->area - PI) > 1e-3) { |
521 |
> |
if (sp->styp == ST_SOURCE && fabs(sp->area - PI) > 1e-3) { |
522 |
|
fprintf(stderr, "%s: source '%s' must be 180-degrees\n", |
523 |
|
progname, sp->sname); |
524 |
|
exit(1); |
727 |
|
/* compute projected areas */ |
728 |
|
for (i = 0, sp = p->slist; sp != NULL; i++, sp = sp->next) { |
729 |
|
projsa[i] = -DOT(sp->snrm, rdir) * sp->area; |
730 |
< |
tarea += projsa[i] *= (double)(projsa[i] > FTINY); |
730 |
> |
tarea += projsa[i] *= (double)(projsa[i] > 0); |
731 |
|
} |
732 |
< |
if (tarea < 0) { /* wrong side of sender? */ |
732 |
> |
if (tarea < FTINY*FTINY) { /* wrong side of sender? */ |
733 |
|
fputs(progname, stderr); |
734 |
|
fputs(": internal - sample behind all sender elements!\n", |
735 |
|
stderr); |
747 |
|
{ |
748 |
|
int n = sampcnt; |
749 |
|
double samp3[3]; |
750 |
< |
double duvw[3]; |
683 |
< |
FVECT orig_dir[2]; |
750 |
> |
FVECT duvw, orig_dir[2]; |
751 |
|
int i; |
752 |
|
|
753 |
|
if (fp == NULL) /* just requesting number of bins? */ |
755 |
|
|
756 |
|
while (n--) { /* stratified hemisphere sampling */ |
757 |
|
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
758 |
< |
SDsquare2disk(duvw, samp3[1], samp3[2]); |
758 |
> |
square2disk(duvw, samp3[1], samp3[2]); |
759 |
|
duvw[2] = -sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]); |
760 |
|
for (i = 3; i--; ) |
761 |
|
orig_dir[1][i] = duvw[0]*p->udir[i] + |
775 |
|
{ |
776 |
|
int n = sampcnt; |
777 |
|
double samp3[3]; |
778 |
< |
double duvw[3]; |
712 |
< |
FVECT orig_dir[2]; |
778 |
> |
FVECT duvw, orig_dir[2]; |
779 |
|
int i; |
780 |
|
|
781 |
|
if (fp == NULL) /* just requesting number of bins? */ |
783 |
|
|
784 |
|
while (n--) { /* stratified sampling */ |
785 |
|
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
786 |
< |
SDsquare2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz, |
786 |
> |
square2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz, |
787 |
|
(b%p->hsiz + samp3[2])/curparams.hsiz); |
788 |
|
duvw[2] = sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]); |
789 |
|
for (i = 3; i--; ) |
1002 |
|
VCOPY(e1, e2); |
1003 |
|
} |
1004 |
|
p->area = normalize(p->snrm)*0.5; |
1005 |
< |
return(p->area > FTINY); |
1005 |
> |
return(p->area > FTINY*FTINY); |
1006 |
|
} |
1007 |
|
|
1008 |
|
/* Add a surface to our current parameters */ |
1065 |
|
snew->area *= PI*snew->area; |
1066 |
|
break; |
1067 |
|
} |
1068 |
< |
if ((snew->area <= FTINY) & (verbose >= 0)) { |
1068 |
> |
if ((snew->area <= FTINY*FTINY) & (verbose >= 0)) { |
1069 |
|
fprintf(stderr, "%s: warning - zero area for surface '%s'\n", |
1070 |
|
progname, oname); |
1071 |
|
free(snew); |
1102 |
|
} |
1103 |
|
/* is it a new receiver? */ |
1104 |
|
if ((st = surf_type(otype)) != ST_NONE) { |
1039 |
– |
if (curparams.slist != NULL && (st == ST_SOURCE) ^ |
1040 |
– |
(curparams.slist->styp == ST_SOURCE)) { |
1041 |
– |
fputs(progname, stderr); |
1042 |
– |
fputs(": cannot mix source/non-source receivers!\n", stderr); |
1043 |
– |
return(-1); |
1044 |
– |
} |
1105 |
|
if (strcmp(thismod, curmod)) { |
1106 |
|
if (curmod[0]) { /* output last receiver? */ |
1107 |
|
finish_receiver(); |
1237 |
|
FILE *rcfp; |
1238 |
|
int nsbins; |
1239 |
|
int a, i; |
1240 |
+ |
/* set global progname */ |
1241 |
+ |
fixargv0(argv[0]); |
1242 |
|
/* screen rcontrib options */ |
1181 |
– |
progname = argv[0]; |
1243 |
|
for (a = 1; a < argc-2; a++) { |
1244 |
|
int na; |
1245 |
|
/* check for argument expansion */ |
1285 |
|
yrs = argv[++a]; |
1286 |
|
na = 0; |
1287 |
|
continue; |
1288 |
< |
case 'c': /* number of samples */ |
1289 |
< |
sampcnt = atoi(argv[++a]); |
1290 |
< |
if (sampcnt <= 0) |
1291 |
< |
goto userr; |
1292 |
< |
na = 0; /* we re-add this later */ |
1293 |
< |
continue; |
1288 |
> |
case 'c': /* spectral sampling or count */ |
1289 |
> |
switch (argv[a][2]) { |
1290 |
> |
case 's': |
1291 |
> |
na = 2; |
1292 |
> |
break; |
1293 |
> |
case 'w': |
1294 |
> |
na = 3; |
1295 |
> |
break; |
1296 |
> |
case '\0': |
1297 |
> |
sampcnt = atoi(argv[++a]); |
1298 |
> |
if (sampcnt <= 0) |
1299 |
> |
goto userr; |
1300 |
> |
na = 0; /* we re-add this later */ |
1301 |
> |
continue; |
1302 |
> |
} |
1303 |
> |
break; |
1304 |
|
case 'I': /* only for pass-through mode */ |
1305 |
|
case 'i': |
1306 |
|
iropt = argv[a]; |
1307 |
|
na = 0; |
1308 |
|
continue; |
1309 |
|
case 'w': /* options without arguments */ |
1310 |
< |
if (argv[a][2] != '+') verbose = -1; |
1310 |
> |
if (!argv[a][2] || strchr("+1tTyY", argv[a][2]) == NULL) |
1311 |
> |
verbose = -1; |
1312 |
> |
break; |
1313 |
|
case 'V': |
1314 |
|
case 'u': |
1315 |
|
case 'h': |
1318 |
|
case 'n': /* options with 1 argument */ |
1319 |
|
case 's': |
1320 |
|
case 'o': |
1321 |
+ |
case 't': |
1322 |
|
na = 2; |
1323 |
|
break; |
1324 |
|
case 'b': /* special case */ |
1438 |
|
for (i = 0; i < nsbins; i++) /* send rcontrib ray samples */ |
1439 |
|
if (!(*curparams.sample_basis)(&curparams, i, rcfp)) |
1440 |
|
return(1); |
1441 |
< |
return(pclose(rcfp) < 0); /* all finished! */ |
1441 |
> |
return(pclose_al(rcfp) < 0); /* all finished! */ |
1442 |
|
userr: |
1443 |
|
if (a < argc-2) |
1444 |
|
fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]); |