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 */ |
142 |
|
strcpy(cp, *av++); |
143 |
|
while (*cp) cp++; |
144 |
|
*cp++ = ' '; |
145 |
< |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) { |
146 |
< |
fputs(progname, stderr); |
161 |
< |
fputs(": too many file arguments!\n", stderr); |
162 |
< |
exit(1); |
163 |
< |
} |
145 |
> |
if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) |
146 |
> |
goto overrun; |
147 |
|
} |
148 |
< |
strcpy(cp, recv); /* receiver goes last */ |
148 |
> |
/* receiver goes last */ |
149 |
> |
if (matchany(recv, SPECIALS)) { |
150 |
> |
*cp++ = QUOTCHAR; |
151 |
> |
while (*recv) { |
152 |
> |
if (cp >= oconvbuf+(sizeof(oconvbuf)-3)) |
153 |
> |
goto overrun; |
154 |
> |
*cp++ = *recv++; |
155 |
> |
} |
156 |
> |
*cp++ = QUOTCHAR; |
157 |
> |
*cp = '\0'; |
158 |
> |
} else |
159 |
> |
strcpy(cp, recv); |
160 |
|
return(oconvbuf); |
161 |
+ |
overrun: |
162 |
+ |
fputs(progname, stderr); |
163 |
+ |
fputs(": too many file arguments!\n", stderr); |
164 |
+ |
exit(1); |
165 |
|
} |
166 |
|
|
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 |
– |
|
167 |
|
/* Open a pipe to/from a command given as an argument list */ |
168 |
|
static FILE * |
169 |
|
popen_arglist(char *av[], char *mode) |
181 |
|
return(popen(cmd, mode)); |
182 |
|
} |
183 |
|
|
184 |
< |
#ifdef _WIN32 |
184 |
> |
#if defined(_WIN32) || defined(_WIN64) |
185 |
|
/* Execute system command (Windows version) */ |
186 |
|
static int |
187 |
|
my_exec(char *av[]) |
266 |
|
{ |
267 |
|
char *cp = pargs; |
268 |
|
int nparams = 0; |
269 |
+ |
int quot; |
270 |
|
int i; |
271 |
|
|
272 |
|
for ( ; ; ) { |
303 |
|
case 'o': |
304 |
|
if (*cp++ != '=') |
305 |
|
break; |
306 |
+ |
quot = 0; |
307 |
+ |
if ((*cp == '"') | (*cp == '\'')) |
308 |
+ |
quot = *cp++; |
309 |
|
i = 0; |
310 |
< |
while (*cp && !isspace(*cp++)) |
311 |
< |
i++; |
310 |
> |
while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) { |
311 |
> |
i++; cp++; |
312 |
> |
} |
313 |
|
if (!i) |
314 |
|
break; |
315 |
< |
*--cp = '\0'; |
315 |
> |
if (!*cp) { |
316 |
> |
if (quot) |
317 |
> |
break; |
318 |
> |
cp[1] = '\0'; |
319 |
> |
} |
320 |
> |
*cp = '\0'; |
321 |
|
p->outfn = savqstr(cp-i); |
322 |
< |
*cp++ = ' '; |
322 |
> |
*cp++ = quot ? quot : ' '; |
323 |
|
++nparams; |
324 |
|
continue; |
325 |
|
case ' ': |
488 |
|
{ |
489 |
|
int i; |
490 |
|
|
491 |
< |
if (!getperpendicular(uva[0], nrm)) { |
491 |
> |
if (!getperpendicular(uva[0], nrm, 1)) { |
492 |
|
fputs(progname, stderr); |
493 |
|
fputs(": bad surface normal in make_axes!\n", stderr); |
494 |
|
exit(1); |
602 |
|
sp->priv = (void *)ptp; |
603 |
|
} |
604 |
|
/* pick triangle by partial area */ |
605 |
< |
for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++) |
605 |
> |
for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++) |
606 |
|
x -= ptp->tri[i].afrac; |
607 |
|
SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac); |
608 |
|
samp2[0] *= samp2[1] = sqrt(samp2[1]); |
632 |
|
/* special case for lone surface */ |
633 |
|
if (p->nsurfs == 1) { |
634 |
|
sp = p->slist; |
635 |
< |
if (DOT(sp->snrm, rdir) >= -FTINY) { |
635 |
> |
if (DOT(sp->snrm, rdir) >= FTINY) { |
636 |
|
fprintf(stderr, |
637 |
|
"%s: internal - sample behind sender '%s'\n", |
638 |
|
progname, sp->sname); |
643 |
|
if (p->nsurfs > nall) { /* (re)allocate surface area cache */ |
644 |
|
if (projsa) free(projsa); |
645 |
|
projsa = (double *)malloc(sizeof(double)*p->nsurfs); |
646 |
< |
if (!projsa) return(0); |
646 |
> |
if (projsa == NULL) { |
647 |
> |
fputs(progname, stderr); |
648 |
> |
fputs(": out of memory in sample_origin!\n", stderr); |
649 |
> |
exit(1); |
650 |
> |
} |
651 |
|
nall = p->nsurfs; |
652 |
|
} |
653 |
|
/* compute projected areas */ |
690 |
|
duvw[2]*p->nrm[i] ; |
691 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
692 |
|
return(0); |
693 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
693 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
694 |
|
return(0); |
695 |
|
} |
696 |
|
return(1); |
720 |
|
duvw[2]*p->nrm[i] ; |
721 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
722 |
|
return(0); |
723 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
723 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
724 |
|
return(0); |
725 |
|
} |
726 |
|
return(1); |
768 |
|
duvw[2]*p->nrm[i] ; |
769 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
770 |
|
return(0); |
771 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
771 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
772 |
|
return(0); |
773 |
|
} |
774 |
|
return(1); |
811 |
|
|
812 |
|
while (n--) { /* stratified sampling */ |
813 |
|
SDmultiSamp(samp2, 2, (n+frandom())/sampcnt); |
814 |
< |
if (!fi_getvec(duvw, b+samp2[1], kbasis[bi])) |
814 |
> |
if (!fo_getvec(duvw, b+samp2[1], kbasis[bi])) |
815 |
|
return(0); |
816 |
|
for (i = 3; i--; ) |
817 |
|
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
819 |
|
duvw[2]*p->nrm[i] ; |
820 |
|
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0])) |
821 |
|
return(0); |
822 |
< |
if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) |
822 |
> |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
823 |
|
return(0); |
824 |
|
} |
825 |
|
return(1); |
946 |
|
snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1)); |
947 |
|
if (snew == NULL) { |
948 |
|
fputs(progname, stderr); |
949 |
< |
fputs(": out of memory!\n", stderr); |
949 |
> |
fputs(": out of memory in add_surface!\n", stderr); |
950 |
|
exit(1); |
951 |
|
} |
952 |
|
strncpy(snew->sname, oname, sizeof(snew->sname)-1); |
1254 |
|
if (argv[a][2] != 'v') na = 2; |
1255 |
|
break; |
1256 |
|
case 'a': /* special case */ |
1257 |
< |
na = (argv[a][2] == 'v') ? 4 : 2; |
1257 |
> |
if (argv[a][2] == 'p') { |
1258 |
> |
na = 2; /* photon map [+ bandwidth(s)] */ |
1259 |
> |
if (a < argc-3 && atoi(argv[a+1]) > 0) |
1260 |
> |
na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0); |
1261 |
> |
} else |
1262 |
> |
na = (argv[a][2] == 'v') ? 4 : 2; |
1263 |
|
break; |
1264 |
|
case 'm': /* special case */ |
1265 |
|
if (!argv[a][2]) goto userr; |
1305 |
|
fputs(": -i, -I supported for pass-through only\n", stderr); |
1306 |
|
return(1); |
1307 |
|
} |
1308 |
< |
fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; |
1308 |
> |
#ifdef SMLFLT |
1309 |
> |
fmtopt[2] = 'f'; |
1310 |
> |
#else |
1311 |
> |
fmtopt[2] = 'd'; |
1312 |
> |
#endif |
1313 |
|
if (sampcnt <= 0) sampcnt = 10000; |
1314 |
|
} |
1315 |
|
sprintf(sampcntbuf, "%d", sampcnt); |