11 |
|
#include <stdlib.h> |
12 |
|
#include "rtio.h" |
13 |
|
#include "rtmath.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 |
22 |
< |
#define getc getc_unlocked |
21 |
> |
#ifndef MAXRCARG |
22 |
> |
#define MAXRCARG 10000 |
23 |
|
#endif |
24 |
|
|
25 |
– |
#ifdef _WIN32 |
26 |
– |
#define SPECIALS " \t\"$*?" |
27 |
– |
#define QUOTCHAR '"' |
28 |
– |
#else |
29 |
– |
#define SPECIALS " \t\n'\"()${}*?[];|&" |
30 |
– |
#define QUOTCHAR '\'' |
31 |
– |
#define ALTQUOT '"' |
32 |
– |
#endif |
33 |
– |
|
34 |
– |
#define MAXRCARG 512 |
35 |
– |
|
25 |
|
char *progname; /* global argv[0] */ |
26 |
|
|
27 |
< |
int verbose = 0; /* verbose mode? */ |
27 |
> |
int verbose = 0; /* verbose mode (< 0 no warnings) */ |
28 |
|
|
29 |
|
char *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"}; |
30 |
|
int nrcargs = 2; |
66 |
|
FVECT uva[2]; /* tangent axes */ |
67 |
|
int ntris; /* number of triangles */ |
68 |
|
struct ptri { |
69 |
< |
float afrac; /* fraction of total area */ |
69 |
> |
double afrac; /* fraction of total area */ |
70 |
|
short vndx[3]; /* vertex indices */ |
71 |
|
} tri[1]; /* triangle array (extends struct) */ |
72 |
|
} POLYTRIS; /* triangulated polygon */ |
73 |
|
|
74 |
|
typedef struct param_s { |
75 |
< |
char hemis[32]; /* hemispherical sampling spec. */ |
75 |
> |
char sign; /* '-' for axis reversal */ |
76 |
> |
char hemis[31]; /* hemispherical sampling spec. */ |
77 |
|
int hsiz; /* hemisphere basis size */ |
78 |
|
int nsurfs; /* number of surfaces */ |
79 |
|
SURF *slist; /* list of surfaces */ |
80 |
|
FVECT vup; /* up vector (zero if unset) */ |
81 |
|
FVECT nrm; /* average normal direction */ |
82 |
< |
FVECT udir, vdir; /* v-up tangent axes */ |
82 |
> |
FVECT udir, vdir; /* tangent axes */ |
83 |
|
char *outfn; /* output file name (receiver) */ |
84 |
|
int (*sample_basis)(struct param_s *p, int, FILE *); |
85 |
|
} PARAMS; /* sender/receiver parameters */ |
86 |
|
|
87 |
|
PARAMS curparams; |
88 |
|
char curmod[128]; |
89 |
+ |
char newparams[1024]; |
90 |
|
|
91 |
|
typedef int SURFSAMP(FVECT, SURF *, double); |
92 |
|
|
100 |
|
static void |
101 |
|
clear_params(PARAMS *p, int reset_only) |
102 |
|
{ |
112 |
– |
curmod[0] = '\0'; |
103 |
|
while (p->slist != NULL) { |
104 |
|
SURF *sdel = p->slist; |
105 |
|
p->slist = sdel->next; |
114 |
|
p->outfn = NULL; |
115 |
|
return; |
116 |
|
} |
117 |
< |
memset(p, 0, sizeof(curparams)); |
117 |
> |
memset(p, 0, sizeof(PARAMS)); |
118 |
|
} |
119 |
|
|
120 |
|
/* Get surface type from name */ |
134 |
|
static char * |
135 |
|
oconv_command(int ac, char *av[]) |
136 |
|
{ |
137 |
< |
static char oconvbuf[2048] = "!oconv -f"; |
138 |
< |
char *cp = oconvbuf + 9; |
139 |
< |
|
137 |
> |
static char oconvbuf[2048] = "!oconv -f "; |
138 |
> |
char *cp = oconvbuf + 10; |
139 |
> |
char *recv = *av++; |
140 |
> |
|
141 |
> |
if (ac-- <= 0) |
142 |
> |
return(NULL); |
143 |
> |
if (verbose < 0) { /* turn off warnings */ |
144 |
> |
strcpy(cp, "-w- "); |
145 |
> |
cp += 4; |
146 |
> |
} |
147 |
|
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++ = ' '; |
148 |
|
strcpy(cp, *av++); |
149 |
|
while (*cp) cp++; |
150 |
+ |
*cp++ = ' '; |
151 |
+ |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) |
152 |
+ |
goto overrun; |
153 |
|
} |
154 |
< |
*cp = '\0'; |
154 |
> |
/* receiver goes last */ |
155 |
> |
if (matchany(recv, SPECIALS)) { |
156 |
> |
*cp++ = QUOTCHAR; |
157 |
> |
while (*recv) { |
158 |
> |
if (cp >= oconvbuf+(sizeof(oconvbuf)-3)) |
159 |
> |
goto overrun; |
160 |
> |
*cp++ = *recv++; |
161 |
> |
} |
162 |
> |
*cp++ = QUOTCHAR; |
163 |
> |
*cp = '\0'; |
164 |
> |
} else |
165 |
> |
strcpy(cp, recv); |
166 |
|
return(oconvbuf); |
167 |
+ |
overrun: |
168 |
+ |
fputs(progname, stderr); |
169 |
+ |
fputs(": too many file arguments!\n", stderr); |
170 |
+ |
exit(1); |
171 |
|
} |
172 |
|
|
164 |
– |
/* Check if any of the characters in str2 are found in str1 */ |
165 |
– |
static int |
166 |
– |
matchany(const char *str1, const char *str2) |
167 |
– |
{ |
168 |
– |
while (*str1) { |
169 |
– |
const char *cp = str2; |
170 |
– |
while (*cp) |
171 |
– |
if (*cp++ == *str1) |
172 |
– |
return(*str1); |
173 |
– |
++str1; |
174 |
– |
} |
175 |
– |
return(0); |
176 |
– |
} |
177 |
– |
|
178 |
– |
|
179 |
– |
/* Convert a set of arguments into a command line for pipe() or system() */ |
180 |
– |
static char * |
181 |
– |
convert_commandline(char *cmd, const int len, char *av[]) |
182 |
– |
{ |
183 |
– |
int match; |
184 |
– |
char *cp; |
185 |
– |
|
186 |
– |
for (cp = cmd; *av != NULL; av++) { |
187 |
– |
const int n = strlen(*av); |
188 |
– |
if (cp+n >= cmd+(len-3)) { |
189 |
– |
fputs(progname, stderr); |
190 |
– |
return(NULL); |
191 |
– |
} |
192 |
– |
if ((match = matchany(*av, SPECIALS))) { |
193 |
– |
const int quote = |
194 |
– |
#ifdef ALTQUOT |
195 |
– |
(match == QUOTCHAR) ? ALTQUOT : |
196 |
– |
#endif |
197 |
– |
QUOTCHAR; |
198 |
– |
*cp++ = quote; |
199 |
– |
strcpy(cp, *av); |
200 |
– |
cp += n; |
201 |
– |
*cp++ = quote; |
202 |
– |
} else { |
203 |
– |
strcpy(cp, *av); |
204 |
– |
cp += n; |
205 |
– |
} |
206 |
– |
*cp++ = ' '; |
207 |
– |
} |
208 |
– |
if (cp <= cmd) |
209 |
– |
return(NULL); |
210 |
– |
*--cp = '\0'; |
211 |
– |
return(cmd); |
212 |
– |
} |
213 |
– |
|
173 |
|
/* Open a pipe to/from a command given as an argument list */ |
174 |
|
static FILE * |
175 |
|
popen_arglist(char *av[], char *mode) |
181 |
|
fputs(": command line too long in popen_arglist()\n", stderr); |
182 |
|
return(NULL); |
183 |
|
} |
184 |
< |
if (verbose) |
184 |
> |
if (verbose > 0) |
185 |
|
fprintf(stderr, "%s: opening pipe %s: %s\n", |
186 |
|
progname, (*mode=='w') ? "to" : "from", cmd); |
187 |
|
return(popen(cmd, mode)); |
188 |
|
} |
189 |
|
|
190 |
< |
#ifdef _WIN32 |
190 |
> |
#if defined(_WIN32) || defined(_WIN64) |
191 |
|
/* Execute system command (Windows version) */ |
192 |
|
static int |
193 |
|
my_exec(char *av[]) |
199 |
|
fputs(": command line too long in my_exec()\n", stderr); |
200 |
|
return(1); |
201 |
|
} |
202 |
< |
if (verbose) |
202 |
> |
if (verbose > 0) |
203 |
|
fprintf(stderr, "%s: running: %s\n", progname, cmd); |
204 |
|
return(system(cmd)); |
205 |
|
} |
214 |
|
fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]); |
215 |
|
return(1); |
216 |
|
} |
217 |
< |
if (verbose) { |
217 |
> |
if (verbose > 0) { |
218 |
|
char cmd[4096]; |
219 |
|
if (!convert_commandline(cmd, sizeof(cmd), av)) |
220 |
|
strcpy(cmd, "COMMAND TOO LONG TO SHOW"); |
268 |
|
|
269 |
|
/* Parse program parameters (directives) */ |
270 |
|
static int |
271 |
< |
parse_params(char *pargs) |
271 |
> |
parse_params(PARAMS *p, char *pargs) |
272 |
|
{ |
273 |
|
char *cp = pargs; |
274 |
|
int nparams = 0; |
275 |
+ |
int quot; |
276 |
|
int i; |
277 |
|
|
278 |
< |
for ( ; ; ) |
278 |
> |
for ( ; ; ) { |
279 |
|
switch (*cp++) { |
280 |
|
case 'h': |
281 |
|
if (*cp++ != '=') |
282 |
|
break; |
283 |
< |
curparams.hsiz = 0; |
283 |
> |
if ((*cp == '+') | (*cp == '-')) |
284 |
> |
p->sign = *cp++; |
285 |
> |
else |
286 |
> |
p->sign = '+'; |
287 |
> |
p->hsiz = 0; |
288 |
|
i = 0; |
289 |
|
while (*cp && !isspace(*cp)) { |
290 |
|
if (isdigit(*cp)) |
291 |
< |
curparams.hsiz = 10*curparams.hsiz + |
292 |
< |
*cp - '0'; |
329 |
< |
curparams.hemis[i++] = *cp++; |
291 |
> |
p->hsiz = 10*p->hsiz + *cp - '0'; |
292 |
> |
p->hemis[i++] = *cp++; |
293 |
|
} |
294 |
|
if (!i) |
295 |
|
break; |
296 |
< |
curparams.hemis[i] = '\0'; |
297 |
< |
curparams.hsiz += !curparams.hsiz; |
296 |
> |
p->hemis[i] = '\0'; |
297 |
> |
p->hsiz += !p->hsiz; |
298 |
|
++nparams; |
299 |
|
continue; |
300 |
|
case 'u': |
301 |
|
if (*cp++ != '=') |
302 |
|
break; |
303 |
< |
if (!get_direction(curparams.vup, cp)) |
303 |
> |
if (!get_direction(p->vup, cp)) |
304 |
|
break; |
305 |
+ |
while (*cp && !isspace(*cp++)) |
306 |
+ |
; |
307 |
|
++nparams; |
308 |
|
continue; |
309 |
|
case 'o': |
310 |
|
if (*cp++ != '=') |
311 |
|
break; |
312 |
+ |
quot = 0; |
313 |
+ |
if ((*cp == '"') | (*cp == '\'')) |
314 |
+ |
quot = *cp++; |
315 |
|
i = 0; |
316 |
< |
while (*cp && !isspace(*cp++)) |
317 |
< |
i++; |
316 |
> |
while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) { |
317 |
> |
i++; cp++; |
318 |
> |
} |
319 |
|
if (!i) |
320 |
|
break; |
321 |
< |
*--cp = '\0'; |
322 |
< |
curparams.outfn = savqstr(cp-i); |
323 |
< |
*cp++ = ' '; |
321 |
> |
if (!*cp) { |
322 |
> |
if (quot) |
323 |
> |
break; |
324 |
> |
cp[1] = '\0'; |
325 |
> |
} |
326 |
> |
*cp = '\0'; |
327 |
> |
p->outfn = savqstr(cp-i); |
328 |
> |
*cp++ = quot ? quot : ' '; |
329 |
|
++nparams; |
330 |
|
continue; |
331 |
|
case ' ': |
338 |
|
default: |
339 |
|
break; |
340 |
|
} |
341 |
< |
fprintf(stderr, "%s: bad parameter string '%s'\n", progname, pargs); |
341 |
> |
break; |
342 |
> |
} |
343 |
> |
fprintf(stderr, "%s: bad parameter string: %s", progname, pargs); |
344 |
|
exit(1); |
345 |
|
return(-1); /* pro forma return */ |
346 |
|
} |
378 |
|
fputs(": undefined normal for hemisphere sampling\n", stderr); |
379 |
|
exit(1); |
380 |
|
} |
381 |
< |
if (normalize(curparams.vup) == 0) |
381 |
> |
if (normalize(curparams.vup) == 0) { |
382 |
|
if (fabs(curparams.nrm[2]) < .7) |
383 |
|
curparams.vup[2] = 1; |
384 |
|
else |
385 |
|
curparams.vup[1] = 1; |
386 |
+ |
} |
387 |
|
/* determine sample type/bin */ |
388 |
< |
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') { |
389 |
< |
binv = "0"; /* uniform sampling -- one bin */ |
388 |
> |
if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) { |
389 |
> |
sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)", |
390 |
> |
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]); |
391 |
> |
binv = savqstr(sbuf); |
392 |
> |
nbins = "1"; /* uniform sampling -- one bin */ |
393 |
|
uniform = 1; |
394 |
|
} else if (tolower(curparams.hemis[0]) == 's' && |
395 |
|
tolower(curparams.hemis[1]) == 'c') { |
400 |
|
exit(1); |
401 |
|
} |
402 |
|
calfn = shirchiufn; shirchiufn = NULL; |
403 |
< |
sprintf(sbuf, "SCdim=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g", |
403 |
> |
sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
404 |
|
curparams.hsiz, |
405 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
406 |
< |
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
406 |
> |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
407 |
> |
curparams.sign); |
408 |
|
params = savqstr(sbuf); |
409 |
|
binv = "scbin"; |
410 |
|
nbins = "SCdim*SCdim"; |
411 |
|
} else if ((tolower(curparams.hemis[0]) == 'r') | |
412 |
|
(tolower(curparams.hemis[0]) == 't')) { |
413 |
|
calfn = reinhfn; reinhfn = NULL; |
414 |
< |
sprintf(sbuf, "MF=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g", |
414 |
> |
sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
415 |
|
curparams.hsiz, |
416 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
417 |
< |
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
417 |
> |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
418 |
> |
curparams.sign); |
419 |
|
params = savqstr(sbuf); |
420 |
|
binv = "rbin"; |
421 |
|
nbins = "Nrbins"; |
443 |
|
progname, curparams.hemis); |
444 |
|
exit(1); |
445 |
|
} |
446 |
+ |
if (tolower(curparams.hemis[0]) == 'k') { |
447 |
+ |
sprintf(sbuf, "RHS=%c1", curparams.sign); |
448 |
+ |
params = savqstr(sbuf); |
449 |
+ |
} |
450 |
|
if (!uniform & (curparams.slist->styp == ST_SOURCE)) { |
451 |
|
SURF *sp; |
452 |
|
for (sp = curparams.slist; sp != NULL; sp = sp->next) |
494 |
|
{ |
495 |
|
int i; |
496 |
|
|
497 |
< |
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) { |
497 |
> |
if (!getperpendicular(uva[0], nrm, 1)) { |
498 |
|
fputs(progname, stderr); |
499 |
|
fputs(": bad surface normal in make_axes!\n", stderr); |
500 |
|
exit(1); |
501 |
|
} |
502 |
< |
uva[1][i] = 1.0; |
523 |
< |
VCROSS(uva[0], uva[1], nrm); |
524 |
< |
normalize(uva[0]); |
525 |
< |
VCROSS(uva[1], nrm, uva[0]); |
502 |
> |
fcross(uva[1], nrm, uva[0]); |
503 |
|
} |
504 |
|
|
505 |
|
/* Illegal sender surfaces end up here */ |
531 |
|
sp->priv = (void *)uva; |
532 |
|
} |
533 |
|
SDmultiSamp(samp2, 2, x); |
534 |
< |
samp2[0] = sp->farg[6] + sqrt(samp2[0]*sp->area*(1./PI)); |
534 |
> |
samp2[0] = sqrt(samp2[0]*sp->area*(1./PI) + sp->farg[6]*sp->farg[6]); |
535 |
|
samp2[1] *= 2.*PI; |
536 |
|
uv[0] = samp2[0]*tcos(samp2[1]); |
537 |
|
uv[1] = samp2[0]*tsin(samp2[1]); |
584 |
|
} |
585 |
|
ptp->ntris = 0; |
586 |
|
v2l->p = (void *)ptp; |
587 |
< |
if (!polyTriangulate(v2l, add_triangle)) |
587 |
> |
if (!polyTriangulate(v2l, add_triangle)) { |
588 |
> |
fprintf(stderr, |
589 |
> |
"%s: cannot triangulate polygon '%s'\n", |
590 |
> |
progname, sp->sname); |
591 |
|
return(0); |
592 |
+ |
} |
593 |
|
for (i = ptp->ntris; i--; ) { |
594 |
|
int a = ptp->tri[i].vndx[0]; |
595 |
|
int b = ptp->tri[i].vndx[1]; |
608 |
|
sp->priv = (void *)ptp; |
609 |
|
} |
610 |
|
/* pick triangle by partial area */ |
611 |
< |
for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++) |
611 |
> |
for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++) |
612 |
|
x -= ptp->tri[i].afrac; |
613 |
|
SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac); |
614 |
|
samp2[0] *= samp2[1] = sqrt(samp2[1]); |
638 |
|
/* special case for lone surface */ |
639 |
|
if (p->nsurfs == 1) { |
640 |
|
sp = p->slist; |
641 |
< |
if (DOT(sp->snrm, rdir) >= -FTINY) |
642 |
< |
return(0); /* behind surface! */ |
641 |
> |
if (DOT(sp->snrm, rdir) >= FTINY) { |
642 |
> |
fprintf(stderr, |
643 |
> |
"%s: internal - sample behind sender '%s'\n", |
644 |
> |
progname, sp->sname); |
645 |
> |
return(0); |
646 |
> |
} |
647 |
|
return((*orig_in_surf[sp->styp])(orig, sp, x)); |
648 |
|
} |
649 |
|
if (p->nsurfs > nall) { /* (re)allocate surface area cache */ |
650 |
|
if (projsa) free(projsa); |
651 |
|
projsa = (double *)malloc(sizeof(double)*p->nsurfs); |
652 |
< |
if (!projsa) return(0); |
652 |
> |
if (projsa == NULL) { |
653 |
> |
fputs(progname, stderr); |
654 |
> |
fputs(": out of memory in sample_origin!\n", stderr); |
655 |
> |
exit(1); |
656 |
> |
} |
657 |
|
nall = p->nsurfs; |
658 |
|
} |
659 |
|
/* compute projected areas */ |
661 |
|
projsa[i] = -DOT(sp->snrm, rdir) * sp->area; |
662 |
|
tarea += projsa[i] *= (double)(projsa[i] > FTINY); |
663 |
|
} |
664 |
< |
if (tarea <= FTINY) /* wrong side of sender? */ |
664 |
> |
if (tarea <= FTINY) { /* wrong side of sender? */ |
665 |
> |
fputs(progname, stderr); |
666 |
> |
fputs(": internal - sample behind all sender elements!\n", |
667 |
> |
stderr); |
668 |
|
return(0); |
669 |
+ |
} |
670 |
|
tarea *= x; /* get surface from list */ |
671 |
|
for (i = 0, sp = p->slist; tarea > projsa[i]; sp = sp->next) |
672 |
|
tarea -= projsa[i++]; |
696 |
|
duvw[2]*p->nrm[i] ; |
697 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
698 |
|
return(0); |
699 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
699 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
700 |
|
return(0); |
701 |
|
} |
702 |
|
return(1); |
726 |
|
duvw[2]*p->nrm[i] ; |
727 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
728 |
|
return(0); |
729 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
729 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
730 |
|
return(0); |
731 |
|
} |
732 |
|
return(1); |
739 |
|
#define T_NALT 7 |
740 |
|
static const int tnaz[T_NALT] = {30, 30, 24, 24, 18, 12, 6}; |
741 |
|
const int RowMax = T_NALT*p->hsiz + 1; |
742 |
< |
const double RAH = (.25*PI)/(RowMax-.5); |
742 |
> |
const double RAH = (.5*PI)/(RowMax-.5); |
743 |
|
#define rnaz(r) (r >= RowMax-1 ? 1 : p->hsiz*tnaz[r/p->hsiz]) |
744 |
|
int n = sampcnt; |
745 |
|
int row, col; |
762 |
|
} |
763 |
|
while (n--) { /* stratified sampling */ |
764 |
|
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
765 |
+ |
if (row >= RowMax-1) /* avoid crowding at zenith */ |
766 |
+ |
samp3[1] *= samp3[1]; |
767 |
|
alt = (row+samp3[1])*RAH; |
768 |
|
azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row); |
769 |
< |
duvw[2] = tcos(alt); /* measured from horizon */ |
770 |
< |
duvw[0] = tcos(azi)*duvw[2]; |
771 |
< |
duvw[1] = tsin(azi)*duvw[2]; |
769 |
> |
duvw[2] = cos(alt); /* measured from horizon */ |
770 |
> |
duvw[0] = tsin(azi)*duvw[2]; |
771 |
> |
duvw[1] = -tcos(azi)*duvw[2]; |
772 |
|
duvw[2] = sqrt(1. - duvw[2]*duvw[2]); |
773 |
|
for (i = 3; i--; ) |
774 |
|
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
776 |
|
duvw[2]*p->nrm[i] ; |
777 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
778 |
|
return(0); |
779 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
779 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
780 |
|
return(0); |
781 |
|
} |
782 |
|
return(1); |
819 |
|
|
820 |
|
while (n--) { /* stratified sampling */ |
821 |
|
SDmultiSamp(samp2, 2, (n+frandom())/sampcnt); |
822 |
< |
if (!bo_getvec(duvw, b+samp2[1], kbasis[bi])) |
822 |
> |
if (!fo_getvec(duvw, b+samp2[1], kbasis[bi])) |
823 |
|
return(0); |
824 |
|
for (i = 3; i--; ) |
825 |
< |
orig_dir[1][i] = duvw[0]*p->udir[i] + |
826 |
< |
duvw[1]*p->vdir[i] + |
825 |
> |
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
826 |
> |
duvw[1]*p->vdir[i] - |
827 |
|
duvw[2]*p->nrm[i] ; |
828 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0])) |
829 |
|
return(0); |
830 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
830 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
831 |
|
return(0); |
832 |
|
} |
833 |
|
return(1); |
842 |
|
fputs(": no sender surface!\n", stderr); |
843 |
|
return(-1); |
844 |
|
} |
845 |
< |
if (curparams.outfn != NULL) /* misplaced output file spec. */ |
845 |
> |
/* misplaced output file spec. */ |
846 |
> |
if ((curparams.outfn != NULL) & (verbose >= 0)) |
847 |
|
fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n", |
848 |
|
progname, curparams.outfn); |
849 |
|
/* check/set basis hemisphere */ |
857 |
|
fputs(": undefined normal for sender sampling\n", stderr); |
858 |
|
return(-1); |
859 |
|
} |
860 |
< |
if (normalize(curparams.vup) == 0) |
860 |
> |
if (normalize(curparams.vup) == 0) { |
861 |
|
if (fabs(curparams.nrm[2]) < .7) |
862 |
|
curparams.vup[2] = 1; |
863 |
|
else |
864 |
|
curparams.vup[1] = 1; |
865 |
< |
VCROSS(curparams.udir, curparams.vup, curparams.nrm); |
865 |
> |
} |
866 |
> |
fcross(curparams.udir, curparams.vup, curparams.nrm); |
867 |
|
if (normalize(curparams.udir) == 0) { |
868 |
|
fputs(progname, stderr); |
869 |
|
fputs(": up vector coincides with sender normal\n", stderr); |
870 |
|
return(-1); |
871 |
|
} |
872 |
< |
VCROSS(curparams.vdir, curparams.nrm, curparams.udir); |
873 |
< |
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') |
872 |
> |
fcross(curparams.vdir, curparams.nrm, curparams.udir); |
873 |
> |
if (curparams.sign == '-') { /* left-handed coordinate system? */ |
874 |
> |
curparams.udir[0] *= -1.; |
875 |
> |
curparams.udir[1] *= -1.; |
876 |
> |
curparams.udir[2] *= -1.; |
877 |
> |
} |
878 |
> |
if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) |
879 |
|
curparams.sample_basis = sample_uniform; |
880 |
|
else if (tolower(curparams.hemis[0]) == 's' && |
881 |
|
tolower(curparams.hemis[1]) == 'c') |
954 |
|
snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1)); |
955 |
|
if (snew == NULL) { |
956 |
|
fputs(progname, stderr); |
957 |
< |
fputs(": out of memory!\n", stderr); |
957 |
> |
fputs(": out of memory in add_surface!\n", stderr); |
958 |
|
exit(1); |
959 |
|
} |
960 |
|
strncpy(snew->sname, oname, sizeof(snew->sname)-1); |
991 |
|
case ST_SOURCE: |
992 |
|
if (snew->nfargs != 4) |
993 |
|
goto badcount; |
994 |
< |
VCOPY(snew->snrm, snew->farg); |
994 |
> |
for (n = 3; n--; ) /* need to reverse "normal" */ |
995 |
> |
snew->snrm[n] = -snew->farg[n]; |
996 |
|
if (normalize(snew->snrm) == 0) |
997 |
|
goto badnorm; |
998 |
|
snew->area = sin((PI/180./2.)*snew->farg[3]); |
999 |
|
snew->area *= PI*snew->area; |
1000 |
|
break; |
1001 |
|
} |
1002 |
< |
if (snew->area <= FTINY) { |
1002 |
> |
if ((snew->area <= FTINY) & (verbose >= 0)) { |
1003 |
|
fprintf(stderr, "%s: warning - zero area for surface '%s'\n", |
1004 |
|
progname, oname); |
1005 |
|
free(snew); |
1011 |
|
curparams.nsurfs++; |
1012 |
|
return; |
1013 |
|
badcount: |
1014 |
< |
fprintf(stderr, "%s: bad argument count for surface '%s'\n", |
1014 |
> |
fprintf(stderr, "%s: bad argument count for surface element '%s'\n", |
1015 |
|
progname, oname); |
1016 |
|
exit(1); |
1017 |
|
badnorm: |
1018 |
< |
fprintf(stderr, "%s: bad orientation for surface '%s'\n", |
1018 |
> |
fprintf(stderr, "%s: bad orientation for surface element '%s'\n", |
1019 |
|
progname, oname); |
1020 |
|
exit(1); |
1021 |
|
} |
1047 |
|
finish_receiver(); |
1048 |
|
clear_params(&curparams, 1); |
1049 |
|
} |
1050 |
+ |
parse_params(&curparams, newparams); |
1051 |
+ |
newparams[0] = '\0'; |
1052 |
|
strcpy(curmod, thismod); |
1053 |
|
} |
1054 |
|
add_surface(st, oname, fp); /* read & store surface */ |
1055 |
|
return(1); |
1056 |
|
} |
1057 |
|
/* else skip arguments */ |
1058 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1058 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1059 |
|
while (n-- > 0) fscanf(fp, "%*s"); |
1060 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1060 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1061 |
|
while (n-- > 0) fscanf(fp, "%*d"); |
1062 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1062 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1063 |
|
while (n-- > 0) fscanf(fp, "%*f"); |
1064 |
|
return(0); |
1065 |
|
} |
1069 |
|
add_send_object(FILE *fp) |
1070 |
|
{ |
1071 |
|
int st; |
1072 |
< |
char otype[32], oname[128]; |
1072 |
> |
char thismod[128], otype[32], oname[128]; |
1073 |
|
int n; |
1074 |
|
|
1075 |
< |
if (fscanf(fp, "%*s %s %s", otype, oname) != 2) |
1075 |
> |
if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3) |
1076 |
|
return(0); /* must have hit EOF! */ |
1077 |
|
if (!strcmp(otype, "alias")) { |
1078 |
|
fscanf(fp, "%*s"); /* skip alias */ |
1085 |
|
fputs(": cannot use source as a sender!\n", stderr); |
1086 |
|
return(-1); |
1087 |
|
} |
1088 |
+ |
if (strcmp(thismod, curmod)) { |
1089 |
+ |
if (curmod[0]) { |
1090 |
+ |
fputs(progname, stderr); |
1091 |
+ |
fputs(": warning - multiple modifiers in sender\n", |
1092 |
+ |
stderr); |
1093 |
+ |
} |
1094 |
+ |
strcpy(curmod, thismod); |
1095 |
+ |
} |
1096 |
+ |
parse_params(&curparams, newparams); |
1097 |
+ |
newparams[0] = '\0'; |
1098 |
|
add_surface(st, oname, fp); /* read & store surface */ |
1099 |
|
return(0); |
1100 |
|
} |
1101 |
|
/* else skip arguments */ |
1102 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1102 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1103 |
|
while (n-- > 0) fscanf(fp, "%*s"); |
1104 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1104 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1105 |
|
while (n-- > 0) fscanf(fp, "%*d"); |
1106 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1106 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1107 |
|
while (n-- > 0) fscanf(fp, "%*f"); |
1108 |
|
return(0); |
1109 |
|
} |
1143 |
|
if (!isspace(c) && fscanf(fp, "%s", inpbuf) == 1 && |
1144 |
|
!strcmp(inpbuf, PARAMSTART)) { |
1145 |
|
if (fgets(inpbuf, sizeof(inpbuf), fp) != NULL) |
1146 |
< |
parse_params(inpbuf); |
1146 |
> |
strcat(newparams, inpbuf); |
1147 |
|
continue; |
1148 |
|
} |
1149 |
< |
while ((c = getc(fp)) != EOF && c != '\n'); |
1149 |
> |
while ((c = getc(fp)) != EOF && c != '\n') |
1150 |
|
; /* else skipping comment */ |
1151 |
|
continue; |
1152 |
|
} |
1171 |
|
{ |
1172 |
|
char fmtopt[6] = "-faa"; /* default output is ASCII */ |
1173 |
|
char *xrs=NULL, *yrs=NULL, *ldopt=NULL; |
1174 |
+ |
char *iropt = NULL; |
1175 |
|
char *sendfn; |
1176 |
|
char sampcntbuf[32], nsbinbuf[32]; |
1177 |
|
FILE *rcfp; |
1179 |
|
int a, i; |
1180 |
|
/* screen rcontrib options */ |
1181 |
|
progname = argv[0]; |
1182 |
< |
for (a = 1; a < argc-2 && argv[a][0] == '-'; a++) { |
1183 |
< |
int na = 1; /* !! Keep consistent !! */ |
1184 |
< |
switch (argv[a][1]) { |
1182 |
> |
for (a = 1; a < argc-2; a++) { |
1183 |
> |
int na; |
1184 |
> |
/* check for argument expansion */ |
1185 |
> |
while ((na = expandarg(&argc, &argv, a)) > 0) |
1186 |
> |
; |
1187 |
> |
if (na < 0) { |
1188 |
> |
fprintf(stderr, "%s: cannot expand '%s'\n", |
1189 |
> |
progname, argv[a]); |
1190 |
> |
return(1); |
1191 |
> |
} |
1192 |
> |
if (argv[a][0] != '-' || !argv[a][1]) |
1193 |
> |
break; |
1194 |
> |
na = 1; |
1195 |
> |
switch (argv[a][1]) { /* !! Keep consistent !! */ |
1196 |
|
case 'v': /* verbose mode */ |
1197 |
< |
verbose = !verbose; |
1197 |
> |
verbose = 1; |
1198 |
|
na = 0; |
1199 |
|
continue; |
1200 |
|
case 'f': /* special case for -fo, -ff, etc. */ |
1207 |
|
case 'f': |
1208 |
|
case 'd': |
1209 |
|
case 'c': |
1210 |
< |
if (!(fmtopt[4] = argv[a][3])) |
1211 |
< |
fmtopt[4] = argv[a][2]; |
1212 |
< |
fmtopt[3] = argv[a][2]; |
1210 |
> |
if (!(fmtopt[3] = argv[a][3])) |
1211 |
> |
fmtopt[3] = argv[a][2]; |
1212 |
> |
fmtopt[2] = argv[a][2]; |
1213 |
|
na = 0; |
1214 |
|
continue; /* will pass later */ |
1215 |
|
default: |
1225 |
|
na = 0; |
1226 |
|
continue; |
1227 |
|
case 'c': /* number of samples */ |
1228 |
< |
sampcnt = atoi(argv[a+1]); |
1228 |
> |
sampcnt = atoi(argv[++a]); |
1229 |
|
if (sampcnt <= 0) |
1230 |
|
goto userr; |
1231 |
|
na = 0; /* we re-add this later */ |
1232 |
|
continue; |
1233 |
< |
case 'V': /* options without arguments */ |
1207 |
< |
case 'w': |
1208 |
< |
case 'u': |
1233 |
> |
case 'I': /* only for pass-through mode */ |
1234 |
|
case 'i': |
1235 |
+ |
iropt = argv[a]; |
1236 |
+ |
na = 0; |
1237 |
+ |
continue; |
1238 |
+ |
case 'w': /* options without arguments */ |
1239 |
+ |
if (argv[a][2] != '+') verbose = -1; |
1240 |
+ |
case 'V': |
1241 |
+ |
case 'u': |
1242 |
|
case 'h': |
1243 |
|
case 'r': |
1244 |
|
break; |
1262 |
|
if (argv[a][2] != 'v') na = 2; |
1263 |
|
break; |
1264 |
|
case 'a': /* special case */ |
1265 |
< |
na = (argv[a][2] == 'v') ? 4 : 2; |
1265 |
> |
if (argv[a][2] == 'p') { |
1266 |
> |
na = 2; /* photon map [+ bandwidth(s)] */ |
1267 |
> |
if (a < argc-3 && atoi(argv[a+1]) > 0) |
1268 |
> |
na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0); |
1269 |
> |
} else |
1270 |
> |
na = (argv[a][2] == 'v') ? 4 : 2; |
1271 |
|
break; |
1272 |
|
case 'm': /* special case */ |
1273 |
|
if (!argv[a][2]) goto userr; |
1274 |
|
na = (argv[a][2] == 'e') | (argv[a][2] == 'a') ? 4 : 2; |
1275 |
|
break; |
1239 |
– |
case '\0': /* pass-through mode */ |
1240 |
– |
goto done_opts; |
1276 |
|
default: /* anything else is verbotten */ |
1277 |
|
goto userr; |
1278 |
|
} |
1282 |
|
while (--na) /* + arguments if any */ |
1283 |
|
rcarg[nrcargs++] = argv[++a]; |
1284 |
|
} |
1250 |
– |
done_opts: |
1285 |
|
if (a > argc-2) |
1286 |
|
goto userr; /* check at end of options */ |
1287 |
|
sendfn = argv[a++]; /* assign sender & receiver inputs */ |
1288 |
|
if (sendfn[0] == '-') { /* user wants pass-through mode? */ |
1289 |
|
if (sendfn[1]) goto userr; |
1290 |
|
sendfn = NULL; |
1291 |
+ |
if (iropt) { |
1292 |
+ |
CHECKARGC(1); |
1293 |
+ |
rcarg[nrcargs++] = iropt; |
1294 |
+ |
} |
1295 |
|
if (xrs) { |
1296 |
|
CHECKARGC(2); |
1297 |
|
rcarg[nrcargs++] = "-x"; |
1307 |
|
rcarg[nrcargs++] = ldopt; |
1308 |
|
} |
1309 |
|
if (sampcnt <= 0) sampcnt = 1; |
1310 |
< |
} else { /* else FVECT determines input format */ |
1311 |
< |
fmtopt[3] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; |
1310 |
> |
} else { /* else in sampling mode */ |
1311 |
> |
if (iropt) { |
1312 |
> |
fputs(progname, stderr); |
1313 |
> |
fputs(": -i, -I supported for pass-through only\n", stderr); |
1314 |
> |
return(1); |
1315 |
> |
} |
1316 |
> |
#ifdef SMLFLT |
1317 |
> |
fmtopt[2] = 'f'; |
1318 |
> |
#else |
1319 |
> |
fmtopt[2] = 'd'; |
1320 |
> |
#endif |
1321 |
|
if (sampcnt <= 0) sampcnt = 10000; |
1322 |
|
} |
1323 |
|
sprintf(sampcntbuf, "%d", sampcnt); |
1336 |
|
return(my_exec(rcarg)); /* rcontrib does everything */ |
1337 |
|
} |
1338 |
|
clear_params(&curparams, 0); /* else load sender surface & params */ |
1339 |
+ |
curmod[0] = '\0'; |
1340 |
|
if (load_scene(sendfn, add_send_object) < 0) |
1341 |
|
return(1); |
1342 |
|
if ((nsbins = prepare_sampler()) <= 0) |
1354 |
|
#ifdef getc_unlocked |
1355 |
|
flockfile(rcfp); |
1356 |
|
#endif |
1357 |
< |
if (verbose) { |
1357 |
> |
if (verbose > 0) { |
1358 |
|
fprintf(stderr, "%s: sampling %d directions", progname, nsbins); |
1359 |
|
if (curparams.nsurfs > 1) |
1360 |
< |
fprintf(stderr, " (%d surfaces)\n", curparams.nsurfs); |
1360 |
> |
fprintf(stderr, " (%d elements)\n", curparams.nsurfs); |
1361 |
|
else |
1362 |
|
fputc('\n', stderr); |
1363 |
|
} |
1364 |
|
for (i = 0; i < nsbins; i++) /* send rcontrib ray samples */ |
1365 |
|
if (!(*curparams.sample_basis)(&curparams, i, rcfp)) |
1366 |
|
return(1); |
1367 |
< |
return(pclose(rcfp) == 0); /* all finished! */ |
1367 |
> |
return(pclose(rcfp) < 0); /* all finished! */ |
1368 |
|
userr: |
1369 |
|
if (a < argc-2) |
1370 |
|
fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]); |
1371 |
< |
fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [system.rad ..]\n", |
1371 |
> |
fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [-i system.oct] [system.rad ..]\n", |
1372 |
|
progname); |
1373 |
|
return(1); |
1374 |
|
} |