18 |
|
#include "triangulate.h" |
19 |
|
#include "platform.h" |
20 |
|
|
21 |
< |
#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ |
22 |
< |
#undef getc |
23 |
< |
#define getc getc_unlocked |
21 |
> |
#ifndef MAXRCARG |
22 |
> |
#define MAXRCARG 10000 |
23 |
|
#endif |
24 |
|
|
25 |
< |
#ifdef _WIN32 |
26 |
< |
#define SPECIALS " \t\"$*?" |
28 |
< |
#define QUOTCHAR '"' |
29 |
< |
#else |
30 |
< |
#define SPECIALS " \t\n'\"()${}*?[];|&" |
31 |
< |
#define QUOTCHAR '\'' |
32 |
< |
#define ALTQUOT '"' |
33 |
< |
#endif |
25 |
> |
#define NOWARN 1 |
26 |
> |
#define VERBO 2 |
27 |
|
|
28 |
< |
#define MAXRCARG 512 |
28 |
> |
int verbose = 0; /* verbose/warning mode */ |
29 |
|
|
37 |
– |
char *progname; /* global argv[0] */ |
38 |
– |
|
39 |
– |
int verbose = 0; /* verbose mode (< 0 no warnings) */ |
40 |
– |
|
30 |
|
char *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"}; |
31 |
|
int nrcargs = 2; |
32 |
|
|
42 |
|
char *kfullfn = "klems_full.cal"; |
43 |
|
char *khalffn = "klems_half.cal"; |
44 |
|
char *kquarterfn = "klems_quarter.cal"; |
45 |
+ |
char *ciefn = "cieskyscan.cal"; |
46 |
|
|
47 |
|
/* string indicating parameters */ |
48 |
< |
const char PARAMSTART[] = "@rfluxmtx"; |
48 |
> |
#define PARAMSTART "@rfluxmtx" |
49 |
|
|
50 |
|
/* surface type IDs */ |
51 |
|
#define ST_NONE 0 |
68 |
|
FVECT uva[2]; /* tangent axes */ |
69 |
|
int ntris; /* number of triangles */ |
70 |
|
struct ptri { |
71 |
< |
float afrac; /* fraction of total area */ |
71 |
> |
double afrac; /* fraction of total area */ |
72 |
|
short vndx[3]; /* vertex indices */ |
73 |
|
} tri[1]; /* triangle array (extends struct) */ |
74 |
|
} POLYTRIS; /* triangulated polygon */ |
75 |
|
|
76 |
|
typedef struct param_s { |
77 |
< |
char hemis[32]; /* hemispherical sampling spec. */ |
77 |
> |
char sign; /* '-' for axis reversal */ |
78 |
> |
char hemis[31]; /* hemispherical sampling spec. */ |
79 |
|
int hsiz; /* hemisphere basis size */ |
80 |
|
int nsurfs; /* number of surfaces */ |
81 |
|
SURF *slist; /* list of surfaces */ |
82 |
|
FVECT vup; /* up vector (zero if unset) */ |
83 |
|
FVECT nrm; /* average normal direction */ |
84 |
< |
FVECT udir, vdir; /* v-up tangent axes */ |
84 |
> |
FVECT udir, vdir; /* tangent axes */ |
85 |
|
char *outfn; /* output file name (receiver) */ |
86 |
|
int (*sample_basis)(struct param_s *p, int, FILE *); |
87 |
|
} PARAMS; /* sender/receiver parameters */ |
142 |
|
|
143 |
|
if (ac-- <= 0) |
144 |
|
return(NULL); |
145 |
< |
while (ac-- > 0) { |
146 |
< |
strcpy(cp, *av++); |
147 |
< |
while (*cp) cp++; |
157 |
< |
*cp++ = ' '; |
158 |
< |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) { |
159 |
< |
fputs(progname, stderr); |
160 |
< |
fputs(": too many file arguments!\n", stderr); |
161 |
< |
exit(1); |
162 |
< |
} |
145 |
> |
if (verbose & NOWARN) { /* warnings off? */ |
146 |
> |
strcpy(cp, "-w "); |
147 |
> |
cp += 3; |
148 |
|
} |
149 |
< |
strcpy(cp, recv); /* receiver goes last */ |
150 |
< |
return(oconvbuf); |
151 |
< |
} |
152 |
< |
|
153 |
< |
/* Check if any of the characters in str2 are found in str1 */ |
154 |
< |
static int |
155 |
< |
matchany(const char *str1, const char *str2) |
156 |
< |
{ |
157 |
< |
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; |
149 |
> |
while (ac-- > 0) { /* copy each argument */ |
150 |
> |
int len = strlen(*av); |
151 |
> |
if (cp+len+4 >= oconvbuf+sizeof(oconvbuf)) |
152 |
> |
goto overrun; |
153 |
> |
if (matchany(*av, SPECIALS)) { |
154 |
> |
*cp++ = QUOTCHAR; |
155 |
> |
strcpy(cp, *av++); |
156 |
> |
cp += len; |
157 |
> |
*cp++ = QUOTCHAR; |
158 |
|
} else { |
159 |
< |
strcpy(cp, *av); |
160 |
< |
cp += n; |
159 |
> |
strcpy(cp, *av++); |
160 |
> |
cp += len; |
161 |
|
} |
162 |
|
*cp++ = ' '; |
163 |
|
} |
164 |
< |
if (cp <= cmd) |
165 |
< |
return(NULL); |
166 |
< |
*--cp = '\0'; |
167 |
< |
return(cmd); |
164 |
> |
/* receiver goes last */ |
165 |
> |
if (matchany(recv, SPECIALS)) { |
166 |
> |
*cp++ = QUOTCHAR; |
167 |
> |
while (*recv) { |
168 |
> |
if (cp >= oconvbuf+(sizeof(oconvbuf)-3)) |
169 |
> |
goto overrun; |
170 |
> |
*cp++ = *recv++; |
171 |
> |
} |
172 |
> |
*cp++ = QUOTCHAR; |
173 |
> |
*cp = '\0'; |
174 |
> |
} else |
175 |
> |
strcpy(cp, recv); |
176 |
> |
return(oconvbuf); |
177 |
> |
overrun: |
178 |
> |
fputs(progname, stderr); |
179 |
> |
fputs(": too many file arguments!\n", stderr); |
180 |
> |
exit(1); |
181 |
|
} |
182 |
|
|
183 |
+ |
#if defined(_WIN32) || defined(_WIN64) |
184 |
+ |
|
185 |
|
/* Open a pipe to/from a command given as an argument list */ |
186 |
|
static FILE * |
187 |
|
popen_arglist(char *av[], char *mode) |
193 |
|
fputs(": command line too long in popen_arglist()\n", stderr); |
194 |
|
return(NULL); |
195 |
|
} |
196 |
< |
if (verbose > 0) |
196 |
> |
if (verbose & VERBO) |
197 |
|
fprintf(stderr, "%s: opening pipe %s: %s\n", |
198 |
|
progname, (*mode=='w') ? "to" : "from", cmd); |
199 |
|
return(popen(cmd, mode)); |
200 |
|
} |
201 |
|
|
202 |
< |
#ifdef _WIN32 |
202 |
> |
#define pclose_al pclose |
203 |
> |
|
204 |
|
/* Execute system command (Windows version) */ |
205 |
|
static int |
206 |
|
my_exec(char *av[]) |
212 |
|
fputs(": command line too long in my_exec()\n", stderr); |
213 |
|
return(1); |
214 |
|
} |
215 |
< |
if (verbose > 0) |
215 |
> |
if (verbose & VERBO) |
216 |
|
fprintf(stderr, "%s: running: %s\n", progname, cmd); |
217 |
|
return(system(cmd)); |
218 |
|
} |
219 |
< |
#else |
219 |
> |
|
220 |
> |
#else /* UNIX */ |
221 |
> |
|
222 |
> |
static SUBPROC rt_proc = SP_INACTIVE; /* we only support one of these */ |
223 |
> |
|
224 |
> |
/* Open a pipe to a command using an argument list */ |
225 |
> |
static FILE * |
226 |
> |
popen_arglist(char *av[], char *mode) |
227 |
> |
{ |
228 |
> |
int fd; |
229 |
> |
|
230 |
> |
if (rt_proc.pid > 0) { |
231 |
> |
fprintf(stderr, "%s: only one i/o pipe at a time!\n", progname); |
232 |
> |
return(NULL); |
233 |
> |
} |
234 |
> |
if (verbose & VERBO) { |
235 |
> |
char cmd[4096]; |
236 |
> |
if (!convert_commandline(cmd, sizeof(cmd), av)) |
237 |
> |
strcpy(cmd, "COMMAND TOO LONG TO SHOW"); |
238 |
> |
fprintf(stderr, "%s: opening pipe %s: %s\n", |
239 |
> |
progname, (*mode=='w') ? "to" : "from", cmd); |
240 |
> |
} |
241 |
> |
if (*mode == 'w') { |
242 |
> |
fd = rt_proc.w = dup(fileno(stdout)); |
243 |
> |
rt_proc.flags |= PF_FILT_OUT; |
244 |
> |
} else if (*mode == 'r') { |
245 |
> |
fd = rt_proc.r = dup(fileno(stdin)); |
246 |
> |
rt_proc.flags |= PF_FILT_INP; |
247 |
> |
} |
248 |
> |
if (fd < 0 || open_process(&rt_proc, av) <= 0) { |
249 |
> |
perror(av[0]); |
250 |
> |
return(NULL); |
251 |
> |
} |
252 |
> |
return(fdopen(fd, mode)); |
253 |
> |
} |
254 |
> |
|
255 |
> |
/* Close command pipe (returns -1 on error to match pclose) */ |
256 |
> |
static int |
257 |
> |
pclose_al(FILE *fp) |
258 |
> |
{ |
259 |
> |
int prob = (fclose(fp) == EOF); |
260 |
> |
|
261 |
> |
if (rt_proc.pid <= 0) |
262 |
> |
return(-1); |
263 |
> |
|
264 |
> |
prob |= (close_process(&rt_proc) != 0); |
265 |
> |
|
266 |
> |
return(-prob); |
267 |
> |
} |
268 |
> |
|
269 |
|
/* Execute system command in our stead (Unix version) */ |
270 |
|
static int |
271 |
|
my_exec(char *av[]) |
276 |
|
fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]); |
277 |
|
return(1); |
278 |
|
} |
279 |
< |
if (verbose > 0) { |
279 |
> |
if (verbose & VERBO) { |
280 |
|
char cmd[4096]; |
281 |
|
if (!convert_commandline(cmd, sizeof(cmd), av)) |
282 |
|
strcpy(cmd, "COMMAND TOO LONG TO SHOW"); |
286 |
|
perror(compath); |
287 |
|
return(1); |
288 |
|
} |
289 |
+ |
|
290 |
|
#endif |
291 |
|
|
292 |
|
/* Get normalized direction vector from string specification */ |
335 |
|
{ |
336 |
|
char *cp = pargs; |
337 |
|
int nparams = 0; |
338 |
+ |
int quot; |
339 |
|
int i; |
340 |
|
|
341 |
|
for ( ; ; ) { |
343 |
|
case 'h': |
344 |
|
if (*cp++ != '=') |
345 |
|
break; |
346 |
+ |
if ((*cp == '+') | (*cp == '-')) |
347 |
+ |
p->sign = *cp++; |
348 |
+ |
else |
349 |
+ |
p->sign = '+'; |
350 |
|
p->hsiz = 0; |
351 |
|
i = 0; |
352 |
|
while (*cp && !isspace(*cp)) { |
372 |
|
case 'o': |
373 |
|
if (*cp++ != '=') |
374 |
|
break; |
375 |
+ |
quot = 0; |
376 |
+ |
if ((*cp == '"') | (*cp == '\'')) |
377 |
+ |
quot = *cp++; |
378 |
|
i = 0; |
379 |
< |
while (*cp && !isspace(*cp++)) |
380 |
< |
i++; |
379 |
> |
while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) { |
380 |
> |
i++; cp++; |
381 |
> |
} |
382 |
|
if (!i) |
383 |
|
break; |
384 |
< |
*--cp = '\0'; |
384 |
> |
if (!*cp) { |
385 |
> |
if (quot) |
386 |
> |
break; |
387 |
> |
cp[1] = '\0'; |
388 |
> |
} |
389 |
> |
*cp = '\0'; |
390 |
|
p->outfn = savqstr(cp-i); |
391 |
< |
*cp++ = ' '; |
391 |
> |
*cp++ = quot ? quot : ' '; |
392 |
|
++nparams; |
393 |
|
continue; |
394 |
|
case ' ': |
395 |
|
case '\t': |
396 |
|
case '\r': |
365 |
– |
continue; |
397 |
|
case '\n': |
398 |
+ |
continue; |
399 |
|
case '\0': |
400 |
|
return(nparams); |
401 |
|
default: |
448 |
|
curparams.vup[1] = 1; |
449 |
|
} |
450 |
|
/* determine sample type/bin */ |
451 |
< |
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') { |
452 |
< |
binv = "0"; /* uniform sampling -- one bin */ |
451 |
> |
if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) { |
452 |
> |
sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)", |
453 |
> |
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]); |
454 |
> |
binv = savqstr(sbuf); |
455 |
> |
nbins = "1"; /* uniform sampling -- one bin */ |
456 |
|
uniform = 1; |
457 |
|
} else if (tolower(curparams.hemis[0]) == 's' && |
458 |
|
tolower(curparams.hemis[1]) == 'c') { |
463 |
|
exit(1); |
464 |
|
} |
465 |
|
calfn = shirchiufn; shirchiufn = NULL; |
466 |
< |
sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g", |
466 |
> |
sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
467 |
|
curparams.hsiz, |
468 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
469 |
< |
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
469 |
> |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
470 |
> |
curparams.sign); |
471 |
|
params = savqstr(sbuf); |
472 |
|
binv = "scbin"; |
473 |
|
nbins = "SCdim*SCdim"; |
474 |
|
} else if ((tolower(curparams.hemis[0]) == 'r') | |
475 |
|
(tolower(curparams.hemis[0]) == 't')) { |
476 |
|
calfn = reinhfn; reinhfn = NULL; |
477 |
< |
sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g", |
477 |
> |
sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
478 |
|
curparams.hsiz, |
479 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
480 |
< |
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
480 |
> |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
481 |
> |
curparams.sign); |
482 |
|
params = savqstr(sbuf); |
483 |
|
binv = "rbin"; |
484 |
|
nbins = "Nrbins"; |
501 |
|
calfn = kquarterfn; kquarterfn = NULL; |
502 |
|
binf = "kqbin"; |
503 |
|
nbins = "Nkqbins"; |
504 |
+ |
} else if (!strcasecmp(curparams.hemis, "cie")) { |
505 |
+ |
calfn = ciefn; ciefn = NULL; |
506 |
+ |
sprintf(sbuf, "rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
507 |
+ |
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
508 |
+ |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
509 |
+ |
curparams.sign); |
510 |
+ |
binv = "cbin"; |
511 |
+ |
nbins = "Ncbins"; |
512 |
|
} else { |
513 |
|
fprintf(stderr, "%s: unrecognized hemisphere sampling: h=%s\n", |
514 |
|
progname, curparams.hemis); |
515 |
|
exit(1); |
516 |
|
} |
517 |
< |
if (!uniform & (curparams.slist->styp == ST_SOURCE)) { |
517 |
> |
if (tolower(curparams.hemis[0]) == 'k') { |
518 |
> |
sprintf(sbuf, "RHS=%c1", curparams.sign); |
519 |
> |
params = savqstr(sbuf); |
520 |
> |
} |
521 |
> |
if (!uniform) { |
522 |
|
SURF *sp; |
523 |
|
for (sp = curparams.slist; sp != NULL; sp = sp->next) |
524 |
< |
if (fabs(sp->area - PI) > 1e-3) { |
524 |
> |
if (sp->styp == ST_SOURCE && fabs(sp->area - PI) > 1e-3) { |
525 |
|
fprintf(stderr, "%s: source '%s' must be 180-degrees\n", |
526 |
|
progname, sp->sname); |
527 |
|
exit(1); |
565 |
|
{ |
566 |
|
int i; |
567 |
|
|
568 |
< |
if (!getperpendicular(uva[0], nrm)) { |
568 |
> |
if (!getperpendicular(uva[0], nrm, 1)) { |
569 |
|
fputs(progname, stderr); |
570 |
|
fputs(": bad surface normal in make_axes!\n", stderr); |
571 |
|
exit(1); |
679 |
|
sp->priv = (void *)ptp; |
680 |
|
} |
681 |
|
/* pick triangle by partial area */ |
682 |
< |
for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++) |
682 |
> |
for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++) |
683 |
|
x -= ptp->tri[i].afrac; |
684 |
|
SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac); |
685 |
|
samp2[0] *= samp2[1] = sqrt(samp2[1]); |
709 |
|
/* special case for lone surface */ |
710 |
|
if (p->nsurfs == 1) { |
711 |
|
sp = p->slist; |
712 |
< |
if (DOT(sp->snrm, rdir) >= -FTINY) { |
712 |
> |
if (DOT(sp->snrm, rdir) >= FTINY) { |
713 |
|
fprintf(stderr, |
714 |
|
"%s: internal - sample behind sender '%s'\n", |
715 |
|
progname, sp->sname); |
720 |
|
if (p->nsurfs > nall) { /* (re)allocate surface area cache */ |
721 |
|
if (projsa) free(projsa); |
722 |
|
projsa = (double *)malloc(sizeof(double)*p->nsurfs); |
723 |
< |
if (!projsa) return(0); |
723 |
> |
if (projsa == NULL) { |
724 |
> |
fputs(progname, stderr); |
725 |
> |
fputs(": out of memory in sample_origin!\n", stderr); |
726 |
> |
exit(1); |
727 |
> |
} |
728 |
|
nall = p->nsurfs; |
729 |
|
} |
730 |
|
/* compute projected areas */ |
731 |
|
for (i = 0, sp = p->slist; sp != NULL; i++, sp = sp->next) { |
732 |
|
projsa[i] = -DOT(sp->snrm, rdir) * sp->area; |
733 |
< |
tarea += projsa[i] *= (double)(projsa[i] > FTINY); |
733 |
> |
tarea += projsa[i] *= (double)(projsa[i] > 0); |
734 |
|
} |
735 |
< |
if (tarea <= FTINY) { /* wrong side of sender? */ |
735 |
> |
if (tarea < FTINY*FTINY) { /* wrong side of sender? */ |
736 |
|
fputs(progname, stderr); |
737 |
|
fputs(": internal - sample behind all sender elements!\n", |
738 |
|
stderr); |
750 |
|
{ |
751 |
|
int n = sampcnt; |
752 |
|
double samp3[3]; |
753 |
< |
double duvw[3]; |
701 |
< |
FVECT orig_dir[2]; |
753 |
> |
FVECT duvw, orig_dir[2]; |
754 |
|
int i; |
755 |
|
|
756 |
|
if (fp == NULL) /* just requesting number of bins? */ |
758 |
|
|
759 |
|
while (n--) { /* stratified hemisphere sampling */ |
760 |
|
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
761 |
< |
SDsquare2disk(duvw, samp3[1], samp3[2]); |
761 |
> |
square2disk(duvw, samp3[1], samp3[2]); |
762 |
|
duvw[2] = -sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]); |
763 |
|
for (i = 3; i--; ) |
764 |
|
orig_dir[1][i] = duvw[0]*p->udir[i] + |
766 |
|
duvw[2]*p->nrm[i] ; |
767 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
768 |
|
return(0); |
769 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
769 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
770 |
|
return(0); |
771 |
|
} |
772 |
|
return(1); |
778 |
|
{ |
779 |
|
int n = sampcnt; |
780 |
|
double samp3[3]; |
781 |
< |
double duvw[3]; |
730 |
< |
FVECT orig_dir[2]; |
781 |
> |
FVECT duvw, orig_dir[2]; |
782 |
|
int i; |
783 |
|
|
784 |
|
if (fp == NULL) /* just requesting number of bins? */ |
786 |
|
|
787 |
|
while (n--) { /* stratified sampling */ |
788 |
|
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
789 |
< |
SDsquare2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz, |
789 |
> |
square2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz, |
790 |
|
(b%p->hsiz + samp3[2])/curparams.hsiz); |
791 |
|
duvw[2] = sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]); |
792 |
|
for (i = 3; i--; ) |
795 |
|
duvw[2]*p->nrm[i] ; |
796 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
797 |
|
return(0); |
798 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
798 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
799 |
|
return(0); |
800 |
|
} |
801 |
|
return(1); |
831 |
|
} |
832 |
|
while (n--) { /* stratified sampling */ |
833 |
|
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
834 |
+ |
if (row >= RowMax-1) /* avoid crowding at zenith */ |
835 |
+ |
samp3[1] *= samp3[1]; |
836 |
|
alt = (row+samp3[1])*RAH; |
837 |
|
azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row); |
838 |
|
duvw[2] = cos(alt); /* measured from horizon */ |
839 |
|
duvw[0] = tsin(azi)*duvw[2]; |
840 |
< |
duvw[1] = tcos(azi)*duvw[2]; |
840 |
> |
duvw[1] = -tcos(azi)*duvw[2]; |
841 |
|
duvw[2] = sqrt(1. - duvw[2]*duvw[2]); |
842 |
|
for (i = 3; i--; ) |
843 |
|
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
845 |
|
duvw[2]*p->nrm[i] ; |
846 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
847 |
|
return(0); |
848 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
848 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
849 |
|
return(0); |
850 |
|
} |
851 |
|
return(1); |
888 |
|
|
889 |
|
while (n--) { /* stratified sampling */ |
890 |
|
SDmultiSamp(samp2, 2, (n+frandom())/sampcnt); |
891 |
< |
if (!bi_getvec(duvw, b+samp2[1], kbasis[bi])) |
891 |
> |
if (!fo_getvec(duvw, b+samp2[1], kbasis[bi])) |
892 |
|
return(0); |
893 |
|
for (i = 3; i--; ) |
894 |
< |
orig_dir[1][i] = duvw[0]*p->udir[i] + |
895 |
< |
duvw[1]*p->vdir[i] + |
894 |
> |
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
895 |
> |
duvw[1]*p->vdir[i] - |
896 |
|
duvw[2]*p->nrm[i] ; |
897 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0])) |
898 |
|
return(0); |
899 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
899 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
900 |
|
return(0); |
901 |
|
} |
902 |
|
return(1); |
912 |
|
return(-1); |
913 |
|
} |
914 |
|
/* misplaced output file spec. */ |
915 |
< |
if ((curparams.outfn != NULL) & (verbose >= 0)) |
915 |
> |
if ((curparams.outfn != NULL) & !(verbose & NOWARN)) |
916 |
|
fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n", |
917 |
|
progname, curparams.outfn); |
918 |
|
/* check/set basis hemisphere */ |
932 |
|
else |
933 |
|
curparams.vup[1] = 1; |
934 |
|
} |
935 |
< |
VCROSS(curparams.udir, curparams.vup, curparams.nrm); |
935 |
> |
fcross(curparams.udir, curparams.vup, curparams.nrm); |
936 |
|
if (normalize(curparams.udir) == 0) { |
937 |
|
fputs(progname, stderr); |
938 |
|
fputs(": up vector coincides with sender normal\n", stderr); |
939 |
|
return(-1); |
940 |
|
} |
941 |
< |
VCROSS(curparams.vdir, curparams.nrm, curparams.udir); |
942 |
< |
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') |
941 |
> |
fcross(curparams.vdir, curparams.nrm, curparams.udir); |
942 |
> |
if (curparams.sign == '-') { /* left-handed coordinate system? */ |
943 |
> |
curparams.udir[0] *= -1.; |
944 |
> |
curparams.udir[1] *= -1.; |
945 |
> |
curparams.udir[2] *= -1.; |
946 |
> |
} |
947 |
> |
if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) |
948 |
|
curparams.sample_basis = sample_uniform; |
949 |
|
else if (tolower(curparams.hemis[0]) == 's' && |
950 |
|
tolower(curparams.hemis[1]) == 'c') |
1005 |
|
VCOPY(e1, e2); |
1006 |
|
} |
1007 |
|
p->area = normalize(p->snrm)*0.5; |
1008 |
< |
return(p->area > FTINY); |
1008 |
> |
return(p->area > FTINY*FTINY); |
1009 |
|
} |
1010 |
|
|
1011 |
|
/* Add a surface to our current parameters */ |
1023 |
|
snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1)); |
1024 |
|
if (snew == NULL) { |
1025 |
|
fputs(progname, stderr); |
1026 |
< |
fputs(": out of memory!\n", stderr); |
1026 |
> |
fputs(": out of memory in add_surface!\n", stderr); |
1027 |
|
exit(1); |
1028 |
|
} |
1029 |
|
strncpy(snew->sname, oname, sizeof(snew->sname)-1); |
1068 |
|
snew->area *= PI*snew->area; |
1069 |
|
break; |
1070 |
|
} |
1071 |
< |
if ((snew->area <= FTINY) & (verbose >= 0)) { |
1071 |
> |
if ((snew->area <= FTINY*FTINY) & !(verbose & NOWARN)) { |
1072 |
|
fprintf(stderr, "%s: warning - zero area for surface '%s'\n", |
1073 |
|
progname, oname); |
1074 |
|
free(snew); |
1105 |
|
} |
1106 |
|
/* is it a new receiver? */ |
1107 |
|
if ((st = surf_type(otype)) != ST_NONE) { |
1050 |
– |
if (curparams.slist != NULL && (st == ST_SOURCE) ^ |
1051 |
– |
(curparams.slist->styp == ST_SOURCE)) { |
1052 |
– |
fputs(progname, stderr); |
1053 |
– |
fputs(": cannot mix source/non-source receivers!\n", stderr); |
1054 |
– |
return(-1); |
1055 |
– |
} |
1108 |
|
if (strcmp(thismod, curmod)) { |
1109 |
|
if (curmod[0]) { /* output last receiver? */ |
1110 |
|
finish_receiver(); |
1132 |
|
add_send_object(FILE *fp) |
1133 |
|
{ |
1134 |
|
int st; |
1135 |
< |
char otype[32], oname[128]; |
1135 |
> |
char thismod[128], otype[32], oname[128]; |
1136 |
|
int n; |
1137 |
|
|
1138 |
< |
if (fscanf(fp, "%*s %s %s", otype, oname) != 2) |
1138 |
> |
if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3) |
1139 |
|
return(0); /* must have hit EOF! */ |
1140 |
|
if (!strcmp(otype, "alias")) { |
1141 |
|
fscanf(fp, "%*s"); /* skip alias */ |
1148 |
|
fputs(": cannot use source as a sender!\n", stderr); |
1149 |
|
return(-1); |
1150 |
|
} |
1151 |
+ |
if (strcmp(thismod, curmod)) { |
1152 |
+ |
if (curmod[0]) { |
1153 |
+ |
fputs(progname, stderr); |
1154 |
+ |
fputs(": warning - multiple modifiers in sender\n", |
1155 |
+ |
stderr); |
1156 |
+ |
} |
1157 |
+ |
strcpy(curmod, thismod); |
1158 |
+ |
} |
1159 |
|
parse_params(&curparams, newparams); |
1160 |
|
newparams[0] = '\0'; |
1161 |
|
add_surface(st, oname, fp); /* read & store surface */ |
1240 |
|
FILE *rcfp; |
1241 |
|
int nsbins; |
1242 |
|
int a, i; |
1243 |
+ |
/* set global progname */ |
1244 |
+ |
fixargv0(argv[0]); |
1245 |
|
/* screen rcontrib options */ |
1184 |
– |
progname = argv[0]; |
1246 |
|
for (a = 1; a < argc-2; a++) { |
1247 |
|
int na; |
1248 |
|
/* check for argument expansion */ |
1258 |
|
na = 1; |
1259 |
|
switch (argv[a][1]) { /* !! Keep consistent !! */ |
1260 |
|
case 'v': /* verbose mode */ |
1261 |
< |
verbose = 1; |
1261 |
> |
verbose ^= VERBO; |
1262 |
|
na = 0; |
1263 |
|
continue; |
1264 |
|
case 'f': /* special case for -fo, -ff, etc. */ |
1288 |
|
yrs = argv[++a]; |
1289 |
|
na = 0; |
1290 |
|
continue; |
1291 |
< |
case 'c': /* number of samples */ |
1292 |
< |
sampcnt = atoi(argv[++a]); |
1293 |
< |
if (sampcnt <= 0) |
1294 |
< |
goto userr; |
1295 |
< |
na = 0; /* we re-add this later */ |
1296 |
< |
continue; |
1291 |
> |
case 'c': /* spectral sampling or count */ |
1292 |
> |
switch (argv[a][2]) { |
1293 |
> |
case 's': |
1294 |
> |
na = 2; |
1295 |
> |
break; |
1296 |
> |
case 'w': |
1297 |
> |
na = 3; |
1298 |
> |
break; |
1299 |
> |
case '\0': |
1300 |
> |
sampcnt = atoi(argv[++a]); |
1301 |
> |
if (sampcnt <= 0) |
1302 |
> |
goto userr; |
1303 |
> |
na = 0; /* we re-add this later */ |
1304 |
> |
continue; |
1305 |
> |
} |
1306 |
> |
break; |
1307 |
|
case 'I': /* only for pass-through mode */ |
1308 |
|
case 'i': |
1309 |
|
iropt = argv[a]; |
1310 |
|
na = 0; |
1311 |
|
continue; |
1312 |
|
case 'w': /* options without arguments */ |
1313 |
< |
if (argv[a][2] != '+') verbose = -1; |
1313 |
> |
if (!argv[a][2]) |
1314 |
> |
verbose ^= NOWARN; |
1315 |
> |
else if (strchr("+1tTyY", argv[a][2]) != NULL) |
1316 |
> |
verbose &= ~NOWARN; |
1317 |
> |
else |
1318 |
> |
verbose |= NOWARN; |
1319 |
> |
break; |
1320 |
|
case 'V': |
1321 |
|
case 'u': |
1322 |
|
case 'h': |
1325 |
|
case 'n': /* options with 1 argument */ |
1326 |
|
case 's': |
1327 |
|
case 'o': |
1328 |
+ |
case 't': |
1329 |
|
na = 2; |
1330 |
|
break; |
1331 |
|
case 'b': /* special case */ |
1343 |
|
if (argv[a][2] != 'v') na = 2; |
1344 |
|
break; |
1345 |
|
case 'a': /* special case */ |
1346 |
< |
na = (argv[a][2] == 'v') ? 4 : 2; |
1346 |
> |
if (argv[a][2] == 'p') { |
1347 |
> |
na = 2; /* photon map [+ bandwidth(s)] */ |
1348 |
> |
if (a < argc-3 && atoi(argv[a+1]) > 0) |
1349 |
> |
na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0); |
1350 |
> |
} else |
1351 |
> |
na = (argv[a][2] == 'v') ? 4 : 2; |
1352 |
|
break; |
1353 |
|
case 'm': /* special case */ |
1354 |
|
if (!argv[a][2]) goto userr; |
1394 |
|
fputs(": -i, -I supported for pass-through only\n", stderr); |
1395 |
|
return(1); |
1396 |
|
} |
1397 |
< |
fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; |
1397 |
> |
#ifdef SMLFLT |
1398 |
> |
fmtopt[2] = 'f'; |
1399 |
> |
#else |
1400 |
> |
fmtopt[2] = 'd'; |
1401 |
> |
#endif |
1402 |
|
if (sampcnt <= 0) sampcnt = 10000; |
1403 |
|
} |
1404 |
|
sprintf(sampcntbuf, "%d", sampcnt); |
1417 |
|
return(my_exec(rcarg)); /* rcontrib does everything */ |
1418 |
|
} |
1419 |
|
clear_params(&curparams, 0); /* else load sender surface & params */ |
1420 |
+ |
curmod[0] = '\0'; |
1421 |
|
if (load_scene(sendfn, add_send_object) < 0) |
1422 |
|
return(1); |
1423 |
|
if ((nsbins = prepare_sampler()) <= 0) |
1435 |
|
#ifdef getc_unlocked |
1436 |
|
flockfile(rcfp); |
1437 |
|
#endif |
1438 |
< |
if (verbose) { |
1438 |
> |
if (verbose & VERBO) { |
1439 |
|
fprintf(stderr, "%s: sampling %d directions", progname, nsbins); |
1440 |
|
if (curparams.nsurfs > 1) |
1441 |
|
fprintf(stderr, " (%d elements)\n", curparams.nsurfs); |
1445 |
|
for (i = 0; i < nsbins; i++) /* send rcontrib ray samples */ |
1446 |
|
if (!(*curparams.sample_basis)(&curparams, i, rcfp)) |
1447 |
|
return(1); |
1448 |
< |
return(pclose(rcfp) == 0); /* all finished! */ |
1448 |
> |
return(pclose_al(rcfp) < 0); /* all finished! */ |
1449 |
|
userr: |
1450 |
|
if (a < argc-2) |
1451 |
|
fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]); |