11 |
|
#include <stdlib.h> |
12 |
|
#include "rtio.h" |
13 |
|
#include "rtmath.h" |
14 |
< |
#include "rtprocess.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 |
23 |
– |
#define getc getc_unlocked |
24 |
– |
#endif |
25 |
– |
|
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 |
– |
|
21 |
|
#define MAXRCARG 512 |
22 |
|
|
23 |
|
char *progname; /* global argv[0] */ |
64 |
|
FVECT uva[2]; /* tangent axes */ |
65 |
|
int ntris; /* number of triangles */ |
66 |
|
struct ptri { |
67 |
< |
float afrac; /* fraction of total area */ |
67 |
> |
double afrac; /* fraction of total area */ |
68 |
|
short vndx[3]; /* vertex indices */ |
69 |
|
} tri[1]; /* triangle array (extends struct) */ |
70 |
|
} POLYTRIS; /* triangulated polygon */ |
138 |
|
|
139 |
|
if (ac-- <= 0) |
140 |
|
return(NULL); |
141 |
+ |
if (verbose < 0) { /* turn off warnings */ |
142 |
+ |
strcpy(cp, "-w- "); |
143 |
+ |
cp += 4; |
144 |
+ |
} |
145 |
|
while (ac-- > 0) { |
146 |
|
strcpy(cp, *av++); |
147 |
|
while (*cp) cp++; |
148 |
|
*cp++ = ' '; |
149 |
< |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) { |
150 |
< |
fputs(progname, stderr); |
161 |
< |
fputs(": too many file arguments!\n", stderr); |
162 |
< |
exit(1); |
163 |
< |
} |
149 |
> |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) |
150 |
> |
goto overrun; |
151 |
|
} |
152 |
< |
strcpy(cp, recv); /* receiver goes last */ |
152 |
> |
/* receiver goes last */ |
153 |
> |
if (matchany(recv, SPECIALS)) { |
154 |
> |
*cp++ = QUOTCHAR; |
155 |
> |
while (*recv) { |
156 |
> |
if (cp >= oconvbuf+(sizeof(oconvbuf)-3)) |
157 |
> |
goto overrun; |
158 |
> |
*cp++ = *recv++; |
159 |
> |
} |
160 |
> |
*cp++ = QUOTCHAR; |
161 |
> |
*cp = '\0'; |
162 |
> |
} else |
163 |
> |
strcpy(cp, recv); |
164 |
|
return(oconvbuf); |
165 |
+ |
overrun: |
166 |
+ |
fputs(progname, stderr); |
167 |
+ |
fputs(": too many file arguments!\n", stderr); |
168 |
+ |
exit(1); |
169 |
|
} |
170 |
|
|
169 |
– |
/* Check if any of the characters in str2 are found in str1 */ |
170 |
– |
static int |
171 |
– |
matchany(const char *str1, const char *str2) |
172 |
– |
{ |
173 |
– |
while (*str1) { |
174 |
– |
const char *cp = str2; |
175 |
– |
while (*cp) |
176 |
– |
if (*cp++ == *str1) |
177 |
– |
return(*str1); |
178 |
– |
++str1; |
179 |
– |
} |
180 |
– |
return(0); |
181 |
– |
} |
182 |
– |
|
183 |
– |
|
184 |
– |
/* Convert a set of arguments into a command line for pipe() or system() */ |
185 |
– |
static char * |
186 |
– |
convert_commandline(char *cmd, const int len, char *av[]) |
187 |
– |
{ |
188 |
– |
int match; |
189 |
– |
char *cp; |
190 |
– |
|
191 |
– |
for (cp = cmd; *av != NULL; av++) { |
192 |
– |
const int n = strlen(*av); |
193 |
– |
if (cp+n >= cmd+(len-3)) { |
194 |
– |
fputs(progname, stderr); |
195 |
– |
return(NULL); |
196 |
– |
} |
197 |
– |
if ((match = matchany(*av, SPECIALS))) { |
198 |
– |
const int quote = |
199 |
– |
#ifdef ALTQUOT |
200 |
– |
(match == QUOTCHAR) ? ALTQUOT : |
201 |
– |
#endif |
202 |
– |
QUOTCHAR; |
203 |
– |
*cp++ = quote; |
204 |
– |
strcpy(cp, *av); |
205 |
– |
cp += n; |
206 |
– |
*cp++ = quote; |
207 |
– |
} else { |
208 |
– |
strcpy(cp, *av); |
209 |
– |
cp += n; |
210 |
– |
} |
211 |
– |
*cp++ = ' '; |
212 |
– |
} |
213 |
– |
if (cp <= cmd) |
214 |
– |
return(NULL); |
215 |
– |
*--cp = '\0'; |
216 |
– |
return(cmd); |
217 |
– |
} |
218 |
– |
|
171 |
|
/* Open a pipe to/from a command given as an argument list */ |
172 |
|
static FILE * |
173 |
|
popen_arglist(char *av[], char *mode) |
185 |
|
return(popen(cmd, mode)); |
186 |
|
} |
187 |
|
|
188 |
< |
#ifdef _WIN32 |
188 |
> |
#if defined(_WIN32) || defined(_WIN64) |
189 |
|
/* Execute system command (Windows version) */ |
190 |
|
static int |
191 |
|
my_exec(char *av[]) |
270 |
|
{ |
271 |
|
char *cp = pargs; |
272 |
|
int nparams = 0; |
273 |
+ |
int quot; |
274 |
|
int i; |
275 |
|
|
276 |
|
for ( ; ; ) { |
307 |
|
case 'o': |
308 |
|
if (*cp++ != '=') |
309 |
|
break; |
310 |
+ |
quot = 0; |
311 |
+ |
if ((*cp == '"') | (*cp == '\'')) |
312 |
+ |
quot = *cp++; |
313 |
|
i = 0; |
314 |
< |
while (*cp && !isspace(*cp++)) |
315 |
< |
i++; |
314 |
> |
while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) { |
315 |
> |
i++; cp++; |
316 |
> |
} |
317 |
|
if (!i) |
318 |
|
break; |
319 |
< |
*--cp = '\0'; |
319 |
> |
if (!*cp) { |
320 |
> |
if (quot) |
321 |
> |
break; |
322 |
> |
cp[1] = '\0'; |
323 |
> |
} |
324 |
> |
*cp = '\0'; |
325 |
|
p->outfn = savqstr(cp-i); |
326 |
< |
*cp++ = ' '; |
326 |
> |
*cp++ = quot ? quot : ' '; |
327 |
|
++nparams; |
328 |
|
continue; |
329 |
|
case ' ': |
383 |
|
curparams.vup[1] = 1; |
384 |
|
} |
385 |
|
/* determine sample type/bin */ |
386 |
< |
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') { |
386 |
> |
if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) { |
387 |
|
sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)", |
388 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]); |
389 |
|
binv = savqstr(sbuf); |
606 |
|
sp->priv = (void *)ptp; |
607 |
|
} |
608 |
|
/* pick triangle by partial area */ |
609 |
< |
for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++) |
609 |
> |
for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++) |
610 |
|
x -= ptp->tri[i].afrac; |
611 |
|
SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac); |
612 |
|
samp2[0] *= samp2[1] = sqrt(samp2[1]); |
636 |
|
/* special case for lone surface */ |
637 |
|
if (p->nsurfs == 1) { |
638 |
|
sp = p->slist; |
639 |
< |
if (DOT(sp->snrm, rdir) >= -FTINY) { |
639 |
> |
if (DOT(sp->snrm, rdir) >= FTINY) { |
640 |
|
fprintf(stderr, |
641 |
|
"%s: internal - sample behind sender '%s'\n", |
642 |
|
progname, sp->sname); |
647 |
|
if (p->nsurfs > nall) { /* (re)allocate surface area cache */ |
648 |
|
if (projsa) free(projsa); |
649 |
|
projsa = (double *)malloc(sizeof(double)*p->nsurfs); |
650 |
< |
if (!projsa) return(0); |
650 |
> |
if (projsa == NULL) { |
651 |
> |
fputs(progname, stderr); |
652 |
> |
fputs(": out of memory in sample_origin!\n", stderr); |
653 |
> |
exit(1); |
654 |
> |
} |
655 |
|
nall = p->nsurfs; |
656 |
|
} |
657 |
|
/* compute projected areas */ |
694 |
|
duvw[2]*p->nrm[i] ; |
695 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
696 |
|
return(0); |
697 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
697 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
698 |
|
return(0); |
699 |
|
} |
700 |
|
return(1); |
724 |
|
duvw[2]*p->nrm[i] ; |
725 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
726 |
|
return(0); |
727 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
727 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
728 |
|
return(0); |
729 |
|
} |
730 |
|
return(1); |
772 |
|
duvw[2]*p->nrm[i] ; |
773 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
774 |
|
return(0); |
775 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
775 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
776 |
|
return(0); |
777 |
|
} |
778 |
|
return(1); |
823 |
|
duvw[2]*p->nrm[i] ; |
824 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0])) |
825 |
|
return(0); |
826 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
826 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
827 |
|
return(0); |
828 |
|
} |
829 |
|
return(1); |
871 |
|
curparams.udir[1] *= -1.; |
872 |
|
curparams.udir[2] *= -1.; |
873 |
|
} |
874 |
< |
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') |
874 |
> |
if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) |
875 |
|
curparams.sample_basis = sample_uniform; |
876 |
|
else if (tolower(curparams.hemis[0]) == 's' && |
877 |
|
tolower(curparams.hemis[1]) == 'c') |
950 |
|
snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1)); |
951 |
|
if (snew == NULL) { |
952 |
|
fputs(progname, stderr); |
953 |
< |
fputs(": out of memory!\n", stderr); |
953 |
> |
fputs(": out of memory in add_surface!\n", stderr); |
954 |
|
exit(1); |
955 |
|
} |
956 |
|
strncpy(snew->sname, oname, sizeof(snew->sname)-1); |
1309 |
|
fputs(": -i, -I supported for pass-through only\n", stderr); |
1310 |
|
return(1); |
1311 |
|
} |
1312 |
< |
fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; |
1312 |
> |
#ifdef SMLFLT |
1313 |
> |
fmtopt[2] = 'f'; |
1314 |
> |
#else |
1315 |
> |
fmtopt[2] = 'd'; |
1316 |
> |
#endif |
1317 |
|
if (sampcnt <= 0) sampcnt = 10000; |
1318 |
|
} |
1319 |
|
sprintf(sampcntbuf, "%d", sampcnt); |