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 |
|
|
26 |
– |
#ifdef _WIN32 |
27 |
– |
#define SPECIALS " \t\"$*?" |
28 |
– |
#define QUOTCHAR '"' |
29 |
– |
#else |
30 |
– |
#define SPECIALS " \t\n'\"()${}*?[];|&" |
31 |
– |
#define QUOTCHAR '\'' |
32 |
– |
#define ALTQUOT '"' |
33 |
– |
#endif |
34 |
– |
|
35 |
– |
#define MAXRCARG 512 |
36 |
– |
|
25 |
|
char *progname; /* global argv[0] */ |
26 |
|
|
27 |
|
int verbose = 0; /* verbose mode (< 0 no warnings) */ |
41 |
|
char *kfullfn = "klems_full.cal"; |
42 |
|
char *khalffn = "klems_half.cal"; |
43 |
|
char *kquarterfn = "klems_quarter.cal"; |
44 |
+ |
char *ciefn = "cieskyscan.cal"; |
45 |
|
|
46 |
|
/* string indicating parameters */ |
47 |
|
const char PARAMSTART[] = "@rfluxmtx"; |
67 |
|
FVECT uva[2]; /* tangent axes */ |
68 |
|
int ntris; /* number of triangles */ |
69 |
|
struct ptri { |
70 |
< |
float afrac; /* fraction of total area */ |
70 |
> |
double afrac; /* fraction of total area */ |
71 |
|
short vndx[3]; /* vertex indices */ |
72 |
|
} tri[1]; /* triangle array (extends struct) */ |
73 |
|
} POLYTRIS; /* triangulated polygon */ |
131 |
|
return(ST_NONE); |
132 |
|
} |
133 |
|
|
145 |
– |
/* Check if any of the characters in str2 are found in str1 */ |
146 |
– |
static int |
147 |
– |
matchany(const char *str1, const char *str2) |
148 |
– |
{ |
149 |
– |
while (*str1) { |
150 |
– |
const char *cp = str2; |
151 |
– |
while (*cp) |
152 |
– |
if (*cp++ == *str1) |
153 |
– |
return(*str1); |
154 |
– |
++str1; |
155 |
– |
} |
156 |
– |
return(0); |
157 |
– |
} |
158 |
– |
|
134 |
|
/* Add arguments to oconv command */ |
135 |
|
static char * |
136 |
|
oconv_command(int ac, char *av[]) |
141 |
|
|
142 |
|
if (ac-- <= 0) |
143 |
|
return(NULL); |
144 |
< |
while (ac-- > 0) { |
145 |
< |
strcpy(cp, *av++); |
146 |
< |
while (*cp) cp++; |
147 |
< |
*cp++ = ' '; |
148 |
< |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) |
144 |
> |
if (verbose < 0) { /* turn off warnings */ |
145 |
> |
strcpy(cp, "-w "); |
146 |
> |
cp += 3; |
147 |
> |
} |
148 |
> |
while (ac-- > 0) { /* copy each argument */ |
149 |
> |
int len = strlen(*av); |
150 |
> |
if (cp+len+4 >= oconvbuf+sizeof(oconvbuf)) |
151 |
|
goto overrun; |
152 |
+ |
if (matchany(*av, SPECIALS)) { |
153 |
+ |
*cp++ = QUOTCHAR; |
154 |
+ |
strcpy(cp, *av++); |
155 |
+ |
cp += len; |
156 |
+ |
*cp++ = QUOTCHAR; |
157 |
+ |
} else { |
158 |
+ |
strcpy(cp, *av++); |
159 |
+ |
cp += len; |
160 |
+ |
} |
161 |
+ |
*cp++ = ' '; |
162 |
|
} |
163 |
|
/* receiver goes last */ |
164 |
|
if (matchany(recv, SPECIALS)) { |
179 |
|
exit(1); |
180 |
|
} |
181 |
|
|
182 |
< |
/* Convert a set of arguments into a command line for pipe() or system() */ |
196 |
< |
static char * |
197 |
< |
convert_commandline(char *cmd, const int len, char *av[]) |
198 |
< |
{ |
199 |
< |
int match; |
200 |
< |
char *cp; |
182 |
> |
#if defined(_WIN32) || defined(_WIN64) |
183 |
|
|
202 |
– |
for (cp = cmd; *av != NULL; av++) { |
203 |
– |
const int n = strlen(*av); |
204 |
– |
if (cp+n >= cmd+(len-3)) { |
205 |
– |
fputs(progname, stderr); |
206 |
– |
return(NULL); |
207 |
– |
} |
208 |
– |
if (matchany(*av, SPECIALS)) { |
209 |
– |
const int quote = |
210 |
– |
#ifdef ALTQUOT |
211 |
– |
strchr(*av, QUOTCHAR) ? ALTQUOT : |
212 |
– |
#endif |
213 |
– |
QUOTCHAR; |
214 |
– |
*cp++ = quote; |
215 |
– |
strcpy(cp, *av); |
216 |
– |
cp += n; |
217 |
– |
*cp++ = quote; |
218 |
– |
} else { |
219 |
– |
strcpy(cp, *av); |
220 |
– |
cp += n; |
221 |
– |
} |
222 |
– |
*cp++ = ' '; |
223 |
– |
} |
224 |
– |
if (cp <= cmd) |
225 |
– |
return(NULL); |
226 |
– |
*--cp = '\0'; |
227 |
– |
return(cmd); |
228 |
– |
} |
229 |
– |
|
184 |
|
/* Open a pipe to/from a command given as an argument list */ |
185 |
|
static FILE * |
186 |
|
popen_arglist(char *av[], char *mode) |
198 |
|
return(popen(cmd, mode)); |
199 |
|
} |
200 |
|
|
201 |
< |
#ifdef _WIN32 |
201 |
> |
#define pclose_al pclose |
202 |
> |
|
203 |
|
/* Execute system command (Windows version) */ |
204 |
|
static int |
205 |
|
my_exec(char *av[]) |
215 |
|
fprintf(stderr, "%s: running: %s\n", progname, cmd); |
216 |
|
return(system(cmd)); |
217 |
|
} |
218 |
< |
#else |
218 |
> |
|
219 |
> |
#else /* UNIX */ |
220 |
> |
|
221 |
> |
static SUBPROC rt_proc = SP_INACTIVE; /* we only support one of these */ |
222 |
> |
|
223 |
> |
/* Open a pipe to a command using an argument list */ |
224 |
> |
static FILE * |
225 |
> |
popen_arglist(char *av[], char *mode) |
226 |
> |
{ |
227 |
> |
int fd; |
228 |
> |
|
229 |
> |
if (rt_proc.pid > 0) { |
230 |
> |
fprintf(stderr, "%s: only one i/o pipe at a time!\n", progname); |
231 |
> |
return(NULL); |
232 |
> |
} |
233 |
> |
if (verbose > 0) { |
234 |
> |
char cmd[4096]; |
235 |
> |
if (!convert_commandline(cmd, sizeof(cmd), av)) |
236 |
> |
strcpy(cmd, "COMMAND TOO LONG TO SHOW"); |
237 |
> |
fprintf(stderr, "%s: opening pipe %s: %s\n", |
238 |
> |
progname, (*mode=='w') ? "to" : "from", cmd); |
239 |
> |
} |
240 |
> |
if (*mode == 'w') { |
241 |
> |
fd = rt_proc.w = dup(fileno(stdout)); |
242 |
> |
rt_proc.flags |= PF_FILT_OUT; |
243 |
> |
} else if (*mode == 'r') { |
244 |
> |
fd = rt_proc.r = dup(fileno(stdin)); |
245 |
> |
rt_proc.flags |= PF_FILT_INP; |
246 |
> |
} |
247 |
> |
if (fd < 0 || open_process(&rt_proc, av) <= 0) { |
248 |
> |
perror(av[0]); |
249 |
> |
return(NULL); |
250 |
> |
} |
251 |
> |
return(fdopen(fd, mode)); |
252 |
> |
} |
253 |
> |
|
254 |
> |
/* Close command pipe (returns -1 on error to match pclose) */ |
255 |
> |
static int |
256 |
> |
pclose_al(FILE *fp) |
257 |
> |
{ |
258 |
> |
int prob = (fclose(fp) == EOF); |
259 |
> |
|
260 |
> |
if (rt_proc.pid <= 0) |
261 |
> |
return(-1); |
262 |
> |
|
263 |
> |
prob |= (close_process(&rt_proc) != 0); |
264 |
> |
|
265 |
> |
return(-prob); |
266 |
> |
} |
267 |
> |
|
268 |
|
/* Execute system command in our stead (Unix version) */ |
269 |
|
static int |
270 |
|
my_exec(char *av[]) |
285 |
|
perror(compath); |
286 |
|
return(1); |
287 |
|
} |
288 |
+ |
|
289 |
|
#endif |
290 |
|
|
291 |
|
/* Get normalized direction vector from string specification */ |
334 |
|
{ |
335 |
|
char *cp = pargs; |
336 |
|
int nparams = 0; |
337 |
+ |
int quot; |
338 |
|
int i; |
339 |
|
|
340 |
|
for ( ; ; ) { |
371 |
|
case 'o': |
372 |
|
if (*cp++ != '=') |
373 |
|
break; |
374 |
+ |
quot = 0; |
375 |
+ |
if ((*cp == '"') | (*cp == '\'')) |
376 |
+ |
quot = *cp++; |
377 |
|
i = 0; |
378 |
< |
while (*cp && !isspace(*cp++)) |
379 |
< |
i++; |
378 |
> |
while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) { |
379 |
> |
i++; cp++; |
380 |
> |
} |
381 |
|
if (!i) |
382 |
|
break; |
383 |
< |
*--cp = '\0'; |
383 |
> |
if (!*cp) { |
384 |
> |
if (quot) |
385 |
> |
break; |
386 |
> |
cp[1] = '\0'; |
387 |
> |
} |
388 |
> |
*cp = '\0'; |
389 |
|
p->outfn = savqstr(cp-i); |
390 |
< |
*cp++ = ' '; |
390 |
> |
*cp++ = quot ? quot : ' '; |
391 |
|
++nparams; |
392 |
|
continue; |
393 |
|
case ' ': |
447 |
|
curparams.vup[1] = 1; |
448 |
|
} |
449 |
|
/* determine sample type/bin */ |
450 |
< |
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') { |
450 |
> |
if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) { |
451 |
|
sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)", |
452 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]); |
453 |
|
binv = savqstr(sbuf); |
500 |
|
calfn = kquarterfn; kquarterfn = NULL; |
501 |
|
binf = "kqbin"; |
502 |
|
nbins = "Nkqbins"; |
503 |
+ |
} else if (!strcasecmp(curparams.hemis, "cie")) { |
504 |
+ |
calfn = ciefn; ciefn = NULL; |
505 |
+ |
sprintf(sbuf, "rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
506 |
+ |
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
507 |
+ |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
508 |
+ |
curparams.sign); |
509 |
+ |
binv = "cbin"; |
510 |
+ |
nbins = "Ncbins"; |
511 |
|
} else { |
512 |
|
fprintf(stderr, "%s: unrecognized hemisphere sampling: h=%s\n", |
513 |
|
progname, curparams.hemis); |
678 |
|
sp->priv = (void *)ptp; |
679 |
|
} |
680 |
|
/* pick triangle by partial area */ |
681 |
< |
for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++) |
681 |
> |
for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++) |
682 |
|
x -= ptp->tri[i].afrac; |
683 |
|
SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac); |
684 |
|
samp2[0] *= samp2[1] = sqrt(samp2[1]); |
719 |
|
if (p->nsurfs > nall) { /* (re)allocate surface area cache */ |
720 |
|
if (projsa) free(projsa); |
721 |
|
projsa = (double *)malloc(sizeof(double)*p->nsurfs); |
722 |
< |
if (!projsa) return(0); |
722 |
> |
if (projsa == NULL) { |
723 |
> |
fputs(progname, stderr); |
724 |
> |
fputs(": out of memory in sample_origin!\n", stderr); |
725 |
> |
exit(1); |
726 |
> |
} |
727 |
|
nall = p->nsurfs; |
728 |
|
} |
729 |
|
/* compute projected areas */ |
730 |
|
for (i = 0, sp = p->slist; sp != NULL; i++, sp = sp->next) { |
731 |
|
projsa[i] = -DOT(sp->snrm, rdir) * sp->area; |
732 |
< |
tarea += projsa[i] *= (double)(projsa[i] > FTINY); |
732 |
> |
tarea += projsa[i] *= (double)(projsa[i] > 0); |
733 |
|
} |
734 |
< |
if (tarea <= FTINY) { /* wrong side of sender? */ |
734 |
> |
if (tarea < FTINY*FTINY) { /* wrong side of sender? */ |
735 |
|
fputs(progname, stderr); |
736 |
|
fputs(": internal - sample behind all sender elements!\n", |
737 |
|
stderr); |
749 |
|
{ |
750 |
|
int n = sampcnt; |
751 |
|
double samp3[3]; |
752 |
< |
double duvw[3]; |
726 |
< |
FVECT orig_dir[2]; |
752 |
> |
FVECT duvw, orig_dir[2]; |
753 |
|
int i; |
754 |
|
|
755 |
|
if (fp == NULL) /* just requesting number of bins? */ |
757 |
|
|
758 |
|
while (n--) { /* stratified hemisphere sampling */ |
759 |
|
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
760 |
< |
SDsquare2disk(duvw, samp3[1], samp3[2]); |
760 |
> |
square2disk(duvw, samp3[1], samp3[2]); |
761 |
|
duvw[2] = -sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]); |
762 |
|
for (i = 3; i--; ) |
763 |
|
orig_dir[1][i] = duvw[0]*p->udir[i] + |
765 |
|
duvw[2]*p->nrm[i] ; |
766 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
767 |
|
return(0); |
768 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
768 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
769 |
|
return(0); |
770 |
|
} |
771 |
|
return(1); |
777 |
|
{ |
778 |
|
int n = sampcnt; |
779 |
|
double samp3[3]; |
780 |
< |
double duvw[3]; |
755 |
< |
FVECT orig_dir[2]; |
780 |
> |
FVECT duvw, orig_dir[2]; |
781 |
|
int i; |
782 |
|
|
783 |
|
if (fp == NULL) /* just requesting number of bins? */ |
785 |
|
|
786 |
|
while (n--) { /* stratified sampling */ |
787 |
|
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
788 |
< |
SDsquare2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz, |
788 |
> |
square2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz, |
789 |
|
(b%p->hsiz + samp3[2])/curparams.hsiz); |
790 |
|
duvw[2] = sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]); |
791 |
|
for (i = 3; i--; ) |
794 |
|
duvw[2]*p->nrm[i] ; |
795 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
796 |
|
return(0); |
797 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
797 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
798 |
|
return(0); |
799 |
|
} |
800 |
|
return(1); |
830 |
|
} |
831 |
|
while (n--) { /* stratified sampling */ |
832 |
|
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
833 |
+ |
if (row >= RowMax-1) /* avoid crowding at zenith */ |
834 |
+ |
samp3[1] *= samp3[1]; |
835 |
|
alt = (row+samp3[1])*RAH; |
836 |
|
azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row); |
837 |
|
duvw[2] = cos(alt); /* measured from horizon */ |
838 |
|
duvw[0] = tsin(azi)*duvw[2]; |
839 |
< |
duvw[1] = tcos(azi)*duvw[2]; |
839 |
> |
duvw[1] = -tcos(azi)*duvw[2]; |
840 |
|
duvw[2] = sqrt(1. - duvw[2]*duvw[2]); |
841 |
|
for (i = 3; i--; ) |
842 |
|
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
844 |
|
duvw[2]*p->nrm[i] ; |
845 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
846 |
|
return(0); |
847 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
847 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
848 |
|
return(0); |
849 |
|
} |
850 |
|
return(1); |
895 |
|
duvw[2]*p->nrm[i] ; |
896 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0])) |
897 |
|
return(0); |
898 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
898 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
899 |
|
return(0); |
900 |
|
} |
901 |
|
return(1); |
943 |
|
curparams.udir[1] *= -1.; |
944 |
|
curparams.udir[2] *= -1.; |
945 |
|
} |
946 |
< |
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') |
946 |
> |
if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) |
947 |
|
curparams.sample_basis = sample_uniform; |
948 |
|
else if (tolower(curparams.hemis[0]) == 's' && |
949 |
|
tolower(curparams.hemis[1]) == 'c') |
1004 |
|
VCOPY(e1, e2); |
1005 |
|
} |
1006 |
|
p->area = normalize(p->snrm)*0.5; |
1007 |
< |
return(p->area > FTINY); |
1007 |
> |
return(p->area > FTINY*FTINY); |
1008 |
|
} |
1009 |
|
|
1010 |
|
/* Add a surface to our current parameters */ |
1022 |
|
snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1)); |
1023 |
|
if (snew == NULL) { |
1024 |
|
fputs(progname, stderr); |
1025 |
< |
fputs(": out of memory!\n", stderr); |
1025 |
> |
fputs(": out of memory in add_surface!\n", stderr); |
1026 |
|
exit(1); |
1027 |
|
} |
1028 |
|
strncpy(snew->sname, oname, sizeof(snew->sname)-1); |
1067 |
|
snew->area *= PI*snew->area; |
1068 |
|
break; |
1069 |
|
} |
1070 |
< |
if ((snew->area <= FTINY) & (verbose >= 0)) { |
1070 |
> |
if ((snew->area <= FTINY*FTINY) & (verbose >= 0)) { |
1071 |
|
fprintf(stderr, "%s: warning - zero area for surface '%s'\n", |
1072 |
|
progname, oname); |
1073 |
|
free(snew); |
1104 |
|
} |
1105 |
|
/* is it a new receiver? */ |
1106 |
|
if ((st = surf_type(otype)) != ST_NONE) { |
1080 |
– |
if (curparams.slist != NULL && (st == ST_SOURCE) ^ |
1081 |
– |
(curparams.slist->styp == ST_SOURCE)) { |
1082 |
– |
fputs(progname, stderr); |
1083 |
– |
fputs(": cannot mix source/non-source receivers!\n", stderr); |
1084 |
– |
return(-1); |
1085 |
– |
} |
1107 |
|
if (strcmp(thismod, curmod)) { |
1108 |
|
if (curmod[0]) { /* output last receiver? */ |
1109 |
|
finish_receiver(); |
1286 |
|
yrs = argv[++a]; |
1287 |
|
na = 0; |
1288 |
|
continue; |
1289 |
< |
case 'c': /* number of samples */ |
1290 |
< |
sampcnt = atoi(argv[++a]); |
1291 |
< |
if (sampcnt <= 0) |
1292 |
< |
goto userr; |
1293 |
< |
na = 0; /* we re-add this later */ |
1294 |
< |
continue; |
1289 |
> |
case 'c': /* spectral sampling or count */ |
1290 |
> |
switch (argv[a][2]) { |
1291 |
> |
case 's': |
1292 |
> |
na = 2; |
1293 |
> |
break; |
1294 |
> |
case 'w': |
1295 |
> |
na = 3; |
1296 |
> |
break; |
1297 |
> |
case '\0': |
1298 |
> |
sampcnt = atoi(argv[++a]); |
1299 |
> |
if (sampcnt <= 0) |
1300 |
> |
goto userr; |
1301 |
> |
na = 0; /* we re-add this later */ |
1302 |
> |
continue; |
1303 |
> |
} |
1304 |
> |
break; |
1305 |
|
case 'I': /* only for pass-through mode */ |
1306 |
|
case 'i': |
1307 |
|
iropt = argv[a]; |
1308 |
|
na = 0; |
1309 |
|
continue; |
1310 |
|
case 'w': /* options without arguments */ |
1311 |
< |
if (argv[a][2] != '+') verbose = -1; |
1311 |
> |
if (!argv[a][2] || strchr("+1tTyY", argv[a][2]) == NULL) |
1312 |
> |
verbose = -1; |
1313 |
> |
break; |
1314 |
|
case 'V': |
1315 |
|
case 'u': |
1316 |
|
case 'h': |
1319 |
|
case 'n': /* options with 1 argument */ |
1320 |
|
case 's': |
1321 |
|
case 'o': |
1322 |
+ |
case 't': |
1323 |
|
na = 2; |
1324 |
|
break; |
1325 |
|
case 'b': /* special case */ |
1388 |
|
fputs(": -i, -I supported for pass-through only\n", stderr); |
1389 |
|
return(1); |
1390 |
|
} |
1391 |
< |
fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; |
1391 |
> |
#ifdef SMLFLT |
1392 |
> |
fmtopt[2] = 'f'; |
1393 |
> |
#else |
1394 |
> |
fmtopt[2] = 'd'; |
1395 |
> |
#endif |
1396 |
|
if (sampcnt <= 0) sampcnt = 10000; |
1397 |
|
} |
1398 |
|
sprintf(sampcntbuf, "%d", sampcnt); |
1439 |
|
for (i = 0; i < nsbins; i++) /* send rcontrib ray samples */ |
1440 |
|
if (!(*curparams.sample_basis)(&curparams, i, rcfp)) |
1441 |
|
return(1); |
1442 |
< |
return(pclose(rcfp) < 0); /* all finished! */ |
1442 |
> |
return(pclose_al(rcfp) < 0); /* all finished! */ |
1443 |
|
userr: |
1444 |
|
if (a < argc-2) |
1445 |
|
fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]); |