8 |
|
*/ |
9 |
|
|
10 |
|
#define _USE_MATH_DEFINES |
11 |
– |
#include <stdio.h> |
11 |
|
#include <stdlib.h> |
12 |
|
#include <math.h> |
13 |
|
#include "random.h" |
18 |
|
#include "bsdfrep.h" |
19 |
|
/* global argv[0] */ |
20 |
|
char *progname; |
21 |
+ |
/* reciprocity averaging option */ |
22 |
+ |
static const char *recip = " -a"; |
23 |
|
/* percentage to cull (<0 to turn off) */ |
24 |
|
static double pctcull = 90.; |
25 |
|
/* sampling order */ |
28 |
|
const double ssamp_thresh = 0.35; |
29 |
|
/* number of super-samples */ |
30 |
|
#ifndef NSSAMP |
31 |
< |
#define NSSAMP 100 |
31 |
> |
#define NSSAMP 64 |
32 |
|
#endif |
33 |
|
/* limit on number of RBF lobes */ |
34 |
|
static int lobe_lim = 15000; |
39 |
|
static char *wrapBSDF[MAXCARG] = {"wrapBSDF", "-U"}; |
40 |
|
static int wbsdfac = 2; |
41 |
|
|
42 |
< |
/* Add argument to wrapBSDF, allocating space if isstatic */ |
42 |
> |
/* Add argument to wrapBSDF, allocating space if !isstatic */ |
43 |
|
static void |
44 |
|
add_wbsdf(const char *arg, int isstatic) |
45 |
|
{ |
128 |
|
eval_isotropic(char *funame) |
129 |
|
{ |
130 |
|
const int sqres = 1<<samp_order; |
131 |
+ |
const double sqfact = 1./(double)sqres; |
132 |
|
FILE *ofp, *uvfp[2]; |
133 |
|
int assignD = 0; |
134 |
|
char cmd[128]; |
137 |
|
float bsdf, uv[2]; |
138 |
|
|
139 |
|
if (pctcull >= 0) { |
140 |
< |
sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d > %s", |
141 |
< |
pctcull, samp_order, create_component_file(0)); |
140 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s", |
141 |
> |
recip, pctcull, samp_order, create_component_file(0)); |
142 |
|
ofp = popen(cmd, "w"); |
143 |
|
if (ofp == NULL) { |
144 |
|
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
151 |
|
#endif |
152 |
|
if (rbf_colorimetry == RBCtristimulus) { |
153 |
|
double uvcull = 100. - (100.-pctcull)*.25; |
154 |
< |
sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d > %s", |
155 |
< |
uvcull, samp_order, create_component_file(1)); |
154 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s", |
155 |
> |
recip, uvcull, samp_order, create_component_file(1)); |
156 |
|
uvfp[0] = popen(cmd, "w"); |
157 |
< |
sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d > %s", |
158 |
< |
uvcull, samp_order, create_component_file(2)); |
157 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s", |
158 |
> |
recip, uvcull, samp_order, create_component_file(2)); |
159 |
|
uvfp[1] = popen(cmd, "w"); |
160 |
|
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
161 |
|
fprintf(stderr, "%s: cannot open pipes to uv output\n", |
187 |
|
fputs("{\n", uvfp[1]); |
188 |
|
} |
189 |
|
} |
190 |
< |
/* need to assign Dx, Dy, Dz? */ |
189 |
< |
if (funame != NULL) |
190 |
> |
if (funame != NULL) /* need to assign Dx, Dy, Dz? */ |
191 |
|
assignD = (fundefined(funame) < 6); |
192 |
|
/* run through directions */ |
193 |
|
for (ix = 0; ix < sqres/2; ix++) { |
194 |
|
RBFNODE *rbf = NULL; |
195 |
< |
iovec[0] = 2.*(ix+.5)/sqres - 1.; |
195 |
> |
iovec[0] = 2.*sqfact*(ix+.5) - 1.; |
196 |
|
iovec[1] = .0; |
197 |
|
iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]); |
198 |
|
if (funame == NULL) |
200 |
|
for (ox = 0; ox < sqres; ox++) { |
201 |
|
float last_bsdf = -1; |
202 |
|
for (oy = 0; oy < sqres; oy++) { |
203 |
< |
SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); |
203 |
> |
SDsquare2disk(iovec+3, (ox+.5)*sqfact, (oy+.5)*sqfact); |
204 |
|
iovec[5] = output_orient * |
205 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
206 |
|
if (funame == NULL) { |
207 |
|
SDValue sdv; |
208 |
|
eval_rbfcol(&sdv, rbf, iovec+3); |
209 |
|
bsdf = sdv.cieY; |
210 |
+ |
#if (NSSAMP > 0) |
211 |
+ |
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
212 |
+ |
int ssi; |
213 |
+ |
double ssa[2], sum = 0, usum = 0, vsum = 0; |
214 |
+ |
/* super-sample voxel */ |
215 |
+ |
for (ssi = NSSAMP; ssi--; ) { |
216 |
+ |
SDmultiSamp(ssa, 2, (ssi+frandom()) * |
217 |
+ |
(1./NSSAMP)); |
218 |
+ |
SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, |
219 |
+ |
(oy+ssa[1])*sqfact); |
220 |
+ |
iovec[5] = output_orient * |
221 |
+ |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
222 |
+ |
eval_rbfcol(&sdv, rbf, iovec+3); |
223 |
+ |
sum += sdv.cieY; |
224 |
+ |
if (rbf_colorimetry == RBCtristimulus) { |
225 |
+ |
sdv.cieY /= |
226 |
+ |
-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.; |
227 |
+ |
usum += 4.*sdv.spec.cx * sdv.cieY; |
228 |
+ |
vsum += 9.*sdv.spec.cy * sdv.cieY; |
229 |
+ |
} |
230 |
+ |
} |
231 |
+ |
bsdf = sum * (1./NSSAMP); |
232 |
+ |
if (rbf_colorimetry == RBCtristimulus) { |
233 |
+ |
uv[0] = usum / (sum+FTINY); |
234 |
+ |
uv[1] = vsum / (sum+FTINY); |
235 |
+ |
} |
236 |
+ |
} else |
237 |
+ |
#endif |
238 |
|
if (rbf_colorimetry == RBCtristimulus) { |
210 |
– |
c_ccvt(&sdv.spec, C_CSXY); |
239 |
|
uv[0] = uv[1] = 1. / |
240 |
|
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
241 |
|
uv[0] *= 4.*sdv.spec.cx; |
257 |
|
for (ssi = NSSAMP; ssi--; ) { |
258 |
|
SDmultiSamp(ssa, 3, (ssi+frandom()) * |
259 |
|
(1./NSSAMP)); |
260 |
< |
ssvec[0] = 2.*(ix+ssa[0])/sqres - 1.; |
260 |
> |
ssvec[0] = 2.*sqfact*(ix+ssa[0]) - 1.; |
261 |
|
ssvec[1] = .0; |
262 |
|
ssvec[2] = input_orient * |
263 |
|
sqrt(1. - ssvec[0]*ssvec[0]); |
264 |
< |
SDsquare2disk(ssvec+3, (ox+ssa[1])/sqres, |
265 |
< |
(oy+ssa[2])/sqres); |
264 |
> |
SDsquare2disk(ssvec+3, (ox+ssa[1])*sqfact, |
265 |
> |
(oy+ssa[2])*sqfact); |
266 |
|
ssvec[5] = output_orient * |
267 |
|
sqrt(1. - ssvec[3]*ssvec[3] - |
268 |
|
ssvec[4]*ssvec[4]); |
274 |
|
} |
275 |
|
sum += funvalue(funame, 6, ssvec); |
276 |
|
} |
277 |
< |
bsdf = sum/NSSAMP; |
277 |
> |
bsdf = sum * (1./NSSAMP); |
278 |
|
} |
279 |
|
#endif |
280 |
|
} |
281 |
|
if (pctcull >= 0) |
282 |
< |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
282 |
> |
putbinary(&bsdf, sizeof(bsdf), 1, ofp); |
283 |
|
else |
284 |
|
fprintf(ofp, "\t%.3e\n", bsdf); |
285 |
|
|
286 |
|
if (rbf_colorimetry == RBCtristimulus) { |
287 |
|
if (pctcull >= 0) { |
288 |
< |
fwrite(&uv[0], sizeof(*uv), 1, uvfp[0]); |
289 |
< |
fwrite(&uv[1], sizeof(*uv), 1, uvfp[1]); |
288 |
> |
putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]); |
289 |
> |
putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]); |
290 |
|
} else { |
291 |
|
fprintf(uvfp[0], "\t%.3e\n", uv[0]); |
292 |
|
fprintf(uvfp[1], "\t%.3e\n", uv[1]); |
297 |
|
} |
298 |
|
if (rbf != NULL) |
299 |
|
free(rbf); |
300 |
< |
prog_show((ix+1.)*(2./sqres)); |
300 |
> |
prog_show((ix+1.)*(2.*sqfact)); |
301 |
|
} |
302 |
+ |
prog_done(); |
303 |
|
if (pctcull >= 0) { /* finish output */ |
304 |
|
if (pclose(ofp)) { |
305 |
|
fprintf(stderr, "%s: error running rttree_reduce on Y\n", |
335 |
|
} |
336 |
|
} |
337 |
|
} |
309 |
– |
prog_done(); |
338 |
|
} |
339 |
|
|
340 |
|
/* Interpolate and output anisotropic BSDF data */ |
342 |
|
eval_anisotropic(char *funame) |
343 |
|
{ |
344 |
|
const int sqres = 1<<samp_order; |
345 |
+ |
const double sqfact = 1./(double)sqres; |
346 |
|
FILE *ofp, *uvfp[2]; |
347 |
|
int assignD = 0; |
348 |
|
char cmd[128]; |
351 |
|
float bsdf, uv[2]; |
352 |
|
|
353 |
|
if (pctcull >= 0) { |
354 |
+ |
const char *avgopt = (input_orient>0 ^ output_orient>0) |
355 |
+ |
? "" : recip; |
356 |
|
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
357 |
< |
(input_orient>0 ^ output_orient>0) ? "" : " -a", |
358 |
< |
pctcull, samp_order, create_component_file(0)); |
357 |
> |
avgopt, pctcull, samp_order, |
358 |
> |
create_component_file(0)); |
359 |
|
ofp = popen(cmd, "w"); |
360 |
|
if (ofp == NULL) { |
361 |
|
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
369 |
|
if (rbf_colorimetry == RBCtristimulus) { |
370 |
|
double uvcull = 100. - (100.-pctcull)*.25; |
371 |
|
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
372 |
< |
(input_orient>0 ^ output_orient>0) ? "" : " -a", |
373 |
< |
uvcull, samp_order, create_component_file(1)); |
372 |
> |
avgopt, uvcull, samp_order, |
373 |
> |
create_component_file(1)); |
374 |
|
uvfp[0] = popen(cmd, "w"); |
375 |
|
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
376 |
< |
(input_orient>0 ^ output_orient>0) ? "" : " -a", |
377 |
< |
uvcull, samp_order, create_component_file(2)); |
376 |
> |
avgopt, uvcull, samp_order, |
377 |
> |
create_component_file(2)); |
378 |
|
uvfp[1] = popen(cmd, "w"); |
379 |
|
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
380 |
|
fprintf(stderr, "%s: cannot open pipes to uv output\n", |
406 |
|
fputs("{\n", uvfp[1]); |
407 |
|
} |
408 |
|
} |
409 |
< |
/* need to assign Dx, Dy, Dz? */ |
379 |
< |
if (funame != NULL) |
409 |
> |
if (funame != NULL) /* need to assign Dx, Dy, Dz? */ |
410 |
|
assignD = (fundefined(funame) < 6); |
411 |
|
/* run through directions */ |
412 |
|
for (ix = 0; ix < sqres; ix++) |
413 |
|
for (iy = 0; iy < sqres; iy++) { |
414 |
|
RBFNODE *rbf = NULL; /* Klems reversal */ |
415 |
< |
SDsquare2disk(iovec, 1.-(ix+.5)/sqres, 1.-(iy+.5)/sqres); |
415 |
> |
SDsquare2disk(iovec, 1.-(ix+.5)*sqfact, 1.-(iy+.5)*sqfact); |
416 |
|
iovec[2] = input_orient * |
417 |
|
sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]); |
418 |
|
if (funame == NULL) |
420 |
|
for (ox = 0; ox < sqres; ox++) { |
421 |
|
float last_bsdf = -1; |
422 |
|
for (oy = 0; oy < sqres; oy++) { |
423 |
< |
SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); |
423 |
> |
SDsquare2disk(iovec+3, (ox+.5)*sqfact, (oy+.5)*sqfact); |
424 |
|
iovec[5] = output_orient * |
425 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
426 |
|
if (funame == NULL) { |
427 |
|
SDValue sdv; |
428 |
|
eval_rbfcol(&sdv, rbf, iovec+3); |
429 |
|
bsdf = sdv.cieY; |
430 |
+ |
#if (NSSAMP > 0) |
431 |
+ |
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
432 |
+ |
int ssi; |
433 |
+ |
double ssa[2], sum = 0, usum = 0, vsum = 0; |
434 |
+ |
/* super-sample voxel */ |
435 |
+ |
for (ssi = NSSAMP; ssi--; ) { |
436 |
+ |
SDmultiSamp(ssa, 2, (ssi+frandom()) * |
437 |
+ |
(1./NSSAMP)); |
438 |
+ |
SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, |
439 |
+ |
(oy+ssa[1])*sqfact); |
440 |
+ |
iovec[5] = output_orient * |
441 |
+ |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
442 |
+ |
eval_rbfcol(&sdv, rbf, iovec+3); |
443 |
+ |
sum += sdv.cieY; |
444 |
+ |
if (rbf_colorimetry == RBCtristimulus) { |
445 |
+ |
sdv.cieY /= |
446 |
+ |
-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.; |
447 |
+ |
usum += 4.*sdv.spec.cx * sdv.cieY; |
448 |
+ |
vsum += 9.*sdv.spec.cy * sdv.cieY; |
449 |
+ |
} |
450 |
+ |
} |
451 |
+ |
bsdf = sum * (1./NSSAMP); |
452 |
+ |
if (rbf_colorimetry == RBCtristimulus) { |
453 |
+ |
uv[0] = usum / (sum+FTINY); |
454 |
+ |
uv[1] = vsum / (sum+FTINY); |
455 |
+ |
} |
456 |
+ |
} else |
457 |
+ |
#endif |
458 |
|
if (rbf_colorimetry == RBCtristimulus) { |
401 |
– |
c_ccvt(&sdv.spec, C_CSXY); |
459 |
|
uv[0] = uv[1] = 1. / |
460 |
|
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
461 |
|
uv[0] *= 4.*sdv.spec.cx; |
477 |
|
for (ssi = NSSAMP; ssi--; ) { |
478 |
|
SDmultiSamp(ssa, 4, (ssi+frandom()) * |
479 |
|
(1./NSSAMP)); |
480 |
< |
SDsquare2disk(ssvec, 1.-(ix+ssa[0])/sqres, |
481 |
< |
1.-(iy+ssa[1])/sqres); |
480 |
> |
SDsquare2disk(ssvec, 1.-(ix+ssa[0])*sqfact, |
481 |
> |
1.-(iy+ssa[1])*sqfact); |
482 |
|
ssvec[2] = input_orient * |
483 |
|
sqrt(1. - ssvec[0]*ssvec[0] - |
484 |
|
ssvec[1]*ssvec[1]); |
485 |
< |
SDsquare2disk(ssvec+3, (ox+ssa[2])/sqres, |
486 |
< |
(oy+ssa[3])/sqres); |
485 |
> |
SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact, |
486 |
> |
(oy+ssa[3])*sqfact); |
487 |
|
ssvec[5] = output_orient * |
488 |
|
sqrt(1. - ssvec[3]*ssvec[3] - |
489 |
|
ssvec[4]*ssvec[4]); |
495 |
|
} |
496 |
|
sum += funvalue(funame, 6, ssvec); |
497 |
|
} |
498 |
< |
bsdf = sum/NSSAMP; |
498 |
> |
bsdf = sum * (1./NSSAMP); |
499 |
|
} |
500 |
|
#endif |
501 |
|
} |
502 |
|
if (pctcull >= 0) |
503 |
< |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
503 |
> |
putbinary(&bsdf, sizeof(bsdf), 1, ofp); |
504 |
|
else |
505 |
|
fprintf(ofp, "\t%.3e\n", bsdf); |
506 |
|
|
507 |
|
if (rbf_colorimetry == RBCtristimulus) { |
508 |
|
if (pctcull >= 0) { |
509 |
< |
fwrite(&uv[0], sizeof(*uv), 1, uvfp[0]); |
510 |
< |
fwrite(&uv[1], sizeof(*uv), 1, uvfp[1]); |
509 |
> |
putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]); |
510 |
> |
putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]); |
511 |
|
} else { |
512 |
|
fprintf(uvfp[0], "\t%.3e\n", uv[0]); |
513 |
|
fprintf(uvfp[1], "\t%.3e\n", uv[1]); |
520 |
|
free(rbf); |
521 |
|
prog_show((ix*sqres+iy+1.)/(sqres*sqres)); |
522 |
|
} |
523 |
+ |
prog_done(); |
524 |
|
if (pctcull >= 0) { /* finish output */ |
525 |
|
if (pclose(ofp)) { |
526 |
|
fprintf(stderr, "%s: error running rttree_reduce on Y\n", |
550 |
|
} |
551 |
|
} |
552 |
|
} |
495 |
– |
prog_done(); |
553 |
|
} |
554 |
|
|
555 |
< |
#ifdef _WIN32 |
555 |
> |
#if defined(_WIN32) || defined(_WIN64) |
556 |
|
/* Execute wrapBSDF command (may never return) */ |
557 |
|
static int |
558 |
|
wrap_up(void) |
622 |
|
esupport &= ~(E_INCHAN|E_OUTCHAN); |
623 |
|
scompile("PI:3.14159265358979323846", NULL, 0); |
624 |
|
biggerlib(); |
625 |
< |
for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
625 |
> |
for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
626 |
|
switch (argv[i][1]) { /* get options */ |
627 |
|
case 'e': |
628 |
|
scompile(argv[++i], NULL, 0); |
632 |
|
if (strchr(argv[++i], '=') != NULL) { |
633 |
|
add_wbsdf("-f", 1); |
634 |
|
add_wbsdf(argv[i], 1); |
635 |
< |
} else |
636 |
< |
fcompile(argv[i]); |
635 |
> |
} else { |
636 |
> |
char *fpath = getpath(argv[i], |
637 |
> |
getrlibpath(), 0); |
638 |
> |
if (fpath == NULL) { |
639 |
> |
fprintf(stderr, |
640 |
> |
"%s: cannot find file '%s'\n", |
641 |
> |
argv[0], argv[i]); |
642 |
> |
return(1); |
643 |
> |
} |
644 |
> |
fcompile(fpath); |
645 |
> |
single_plane_incident = 0; |
646 |
> |
} |
647 |
|
} else |
648 |
|
dofwd = (argv[i][0] == '+'); |
649 |
|
break; |
650 |
+ |
case 'a': |
651 |
+ |
recip = (argv[i][0] == '+') ? " -a" : ""; |
652 |
+ |
break; |
653 |
|
case 'b': |
654 |
|
dobwd = (argv[i][0] == '+'); |
655 |
|
break; |
765 |
|
return(wrap_up()); |
766 |
|
userr: |
767 |
|
fprintf(stderr, |
768 |
< |
"Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
768 |
> |
"Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
769 |
|
progname); |
770 |
|
fprintf(stderr, |
771 |
< |
" or: %s -t{3|4} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |
771 |
> |
" or: %s -t{3|4} [{+|-}a][-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |
772 |
|
progname); |
773 |
|
return(1); |
774 |
|
} |