| 10 |
|
#define _USE_MATH_DEFINES |
| 11 |
|
#include <stdlib.h> |
| 12 |
|
#include <math.h> |
| 13 |
+ |
#include <ctype.h> |
| 14 |
|
#include "random.h" |
| 14 |
– |
#include "platform.h" |
| 15 |
– |
#include "paths.h" |
| 15 |
|
#include "rtio.h" |
| 16 |
|
#include "calcomp.h" |
| 17 |
|
#include "bsdfrep.h" |
| 19 |
– |
/* global argv[0] */ |
| 20 |
– |
char *progname; |
| 18 |
|
/* reciprocity averaging option */ |
| 19 |
< |
static const char *recip = " -a"; |
| 19 |
> |
static const char *recip = ""; |
| 20 |
|
/* percentage to cull (<0 to turn off) */ |
| 21 |
|
static double pctcull = 90.; |
| 22 |
|
/* sampling order */ |
| 24 |
|
/* super-sampling threshold */ |
| 25 |
|
static double ssamp_thresh = 0.35; |
| 26 |
|
/* number of super-samples */ |
| 27 |
< |
static int nssamp = 256; |
| 27 |
> |
static int nssamp = 64; |
| 28 |
|
/* limit on number of RBF lobes */ |
| 29 |
|
static int lobe_lim = 15000; |
| 30 |
|
/* progress bar length */ |
| 131 |
|
int assignD = 0; |
| 132 |
|
char cmd[128]; |
| 133 |
|
int ix, ox, oy; |
| 134 |
< |
double iovec[6]; |
| 134 |
> |
RREAL iovec[6]; |
| 135 |
|
float bsdf, uv[2]; |
| 136 |
|
|
| 137 |
|
if (pctcull >= 0) { |
| 204 |
|
rbf = advect_rbf(iovec, lobe_lim); |
| 205 |
|
/* presample first row */ |
| 206 |
|
for (oy = 0; oy < sqres; oy++) { |
| 207 |
< |
SDsquare2disk(iovec+3, .5*sqfact, (oy+.5)*sqfact); |
| 207 |
> |
square2disk(iovec+3, .5*sqfact, (oy+.5)*sqfact); |
| 208 |
|
iovec[5] = output_orient * |
| 209 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
| 210 |
|
if (funame == NULL) { |
| 226 |
|
*/ |
| 227 |
|
for (oy = 0; oy < sqres; oy++) { |
| 228 |
|
if (ox < sqres-1) { /* keeping one row ahead... */ |
| 229 |
< |
SDsquare2disk(iovec+3, (ox+1.5)*sqfact, (oy+.5)*sqfact); |
| 229 |
> |
square2disk(iovec+3, (ox+1.5)*sqfact, (oy+.5)*sqfact); |
| 230 |
|
iovec[5] = output_orient * |
| 231 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
| 232 |
|
} |
| 246 |
|
for (ssi = nssamp; ssi--; ) { |
| 247 |
|
SDmultiSamp(ssa, 2, (ssi+frandom()) / |
| 248 |
|
(double)nssamp); |
| 249 |
< |
SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, |
| 249 |
> |
square2disk(iovec+3, (ox+ssa[0])*sqfact, |
| 250 |
|
(oy+ssa[1])*sqfact); |
| 251 |
|
iovec[5] = output_orient * |
| 252 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
| 264 |
|
uv[0] = usum / (sum+FTINY); |
| 265 |
|
uv[1] = vsum / (sum+FTINY); |
| 266 |
|
} |
| 267 |
< |
} else |
| 271 |
< |
if (rbf_colorimetry == RBCtristimulus) { |
| 267 |
> |
} else if (rbf_colorimetry == RBCtristimulus) { |
| 268 |
|
uv[0] = uv[1] = 1. / |
| 269 |
|
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
| 270 |
|
uv[0] *= 4.*sdv.spec.cx; |
| 301 |
|
ssvec[2] = 1. - ssvec[0]*ssvec[0]; |
| 302 |
|
} |
| 303 |
|
ssvec[2] = input_orient * sqrt(ssvec[2]); |
| 304 |
< |
SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact, |
| 304 |
> |
square2disk(ssvec+3, (ox+ssa[2])*sqfact, |
| 305 |
|
(oy+ssa[3])*sqfact); |
| 306 |
|
ssvec[5] = output_orient * |
| 307 |
|
sqrt(1. - ssvec[3]*ssvec[3] - |
| 395 |
|
int assignD = 0; |
| 396 |
|
char cmd[128]; |
| 397 |
|
int ix, iy, ox, oy; |
| 398 |
< |
double iovec[6]; |
| 398 |
> |
RREAL iovec[6]; |
| 399 |
|
float bsdf, uv[2]; |
| 400 |
|
|
| 401 |
|
if (pctcull >= 0) { |
| 465 |
|
for (ix = 0; ix < sqres; ix++) |
| 466 |
|
for (iy = 0; iy < sqres; iy++) { |
| 467 |
|
RBFNODE *rbf = NULL; /* Klems reversal */ |
| 468 |
< |
SDsquare2disk(iovec, 1.-(ix+.5)*sqfact, 1.-(iy+.5)*sqfact); |
| 468 |
> |
square2disk(iovec, 1.-(ix+.5)*sqfact, 1.-(iy+.5)*sqfact); |
| 469 |
|
iovec[2] = input_orient * |
| 470 |
|
sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]); |
| 471 |
|
if (funame == NULL) |
| 472 |
|
rbf = advect_rbf(iovec, lobe_lim); |
| 473 |
|
/* presample first row */ |
| 474 |
|
for (oy = 0; oy < sqres; oy++) { |
| 475 |
< |
SDsquare2disk(iovec+3, .5*sqfact, (oy+.5)*sqfact); |
| 475 |
> |
square2disk(iovec+3, .5*sqfact, (oy+.5)*sqfact); |
| 476 |
|
iovec[5] = output_orient * |
| 477 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
| 478 |
|
if (funame == NULL) { |
| 494 |
|
*/ |
| 495 |
|
for (oy = 0; oy < sqres; oy++) { |
| 496 |
|
if (ox < sqres-1) { /* keeping one row ahead... */ |
| 497 |
< |
SDsquare2disk(iovec+3, (ox+1.5)*sqfact, (oy+.5)*sqfact); |
| 497 |
> |
square2disk(iovec+3, (ox+1.5)*sqfact, (oy+.5)*sqfact); |
| 498 |
|
iovec[5] = output_orient * |
| 499 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
| 500 |
|
} |
| 514 |
|
for (ssi = nssamp; ssi--; ) { |
| 515 |
|
SDmultiSamp(ssa, 2, (ssi+frandom()) / |
| 516 |
|
(double)nssamp); |
| 517 |
< |
SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, |
| 517 |
> |
square2disk(iovec+3, (ox+ssa[0])*sqfact, |
| 518 |
|
(oy+ssa[1])*sqfact); |
| 519 |
|
iovec[5] = output_orient * |
| 520 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
| 532 |
|
uv[0] = usum / (sum+FTINY); |
| 533 |
|
uv[1] = vsum / (sum+FTINY); |
| 534 |
|
} |
| 535 |
< |
} else |
| 540 |
< |
if (rbf_colorimetry == RBCtristimulus) { |
| 535 |
> |
} else if (rbf_colorimetry == RBCtristimulus) { |
| 536 |
|
uv[0] = uv[1] = 1. / |
| 537 |
|
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
| 538 |
|
uv[0] *= 4.*sdv.spec.cx; |
| 560 |
|
for (ssi = nssamp; ssi--; ) { |
| 561 |
|
SDmultiSamp(ssa, 4, (ssi+frandom()) / |
| 562 |
|
(double)nssamp); |
| 563 |
< |
SDsquare2disk(ssvec, 1.-(ix+ssa[0])*sqfact, |
| 563 |
> |
square2disk(ssvec, 1.-(ix+ssa[0])*sqfact, |
| 564 |
|
1.-(iy+ssa[1])*sqfact); |
| 565 |
|
ssvec[2] = input_orient * |
| 566 |
|
sqrt(1. - ssvec[0]*ssvec[0] - |
| 567 |
|
ssvec[1]*ssvec[1]); |
| 568 |
< |
SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact, |
| 568 |
> |
square2disk(ssvec+3, (ox+ssa[2])*sqfact, |
| 569 |
|
(oy+ssa[3])*sqfact); |
| 570 |
|
ssvec[5] = output_orient * |
| 571 |
|
sqrt(1. - ssvec[3]*ssvec[3] - |
| 645 |
|
static int |
| 646 |
|
wrap_up(void) |
| 647 |
|
{ |
| 648 |
< |
char cmd[8192]; |
| 648 |
> |
char cmd[32700]; |
| 649 |
|
|
| 650 |
|
if (bsdf_manuf[0]) { |
| 651 |
|
add_wbsdf("-f", 1); |
| 696 |
|
} |
| 697 |
|
#endif |
| 698 |
|
|
| 699 |
+ |
#define HEAD_BUFLEN 10240 |
| 700 |
+ |
static char head_buf[HEAD_BUFLEN]; |
| 701 |
+ |
static int cur_headlen = 0; |
| 702 |
+ |
|
| 703 |
+ |
/* Record header line as comment associated with this SIR input */ |
| 704 |
+ |
static int |
| 705 |
+ |
record2header(char *s) |
| 706 |
+ |
{ |
| 707 |
+ |
int len = strlen(s); |
| 708 |
+ |
|
| 709 |
+ |
if (cur_headlen+len >= HEAD_BUFLEN-6) |
| 710 |
+ |
return(0); |
| 711 |
+ |
/* includes EOL */ |
| 712 |
+ |
strcpy(head_buf+cur_headlen, s); |
| 713 |
+ |
cur_headlen += len; |
| 714 |
+ |
|
| 715 |
+ |
#if defined(_WIN32) || defined(_WIN64) |
| 716 |
+ |
if (head_buf[cur_headlen-1] == '\n') |
| 717 |
+ |
head_buf[cur_headlen-1] = '\t'; |
| 718 |
+ |
#endif |
| 719 |
+ |
return(1); |
| 720 |
+ |
} |
| 721 |
+ |
|
| 722 |
+ |
/* Finish off header for this file */ |
| 723 |
+ |
static void |
| 724 |
+ |
done_header(void) |
| 725 |
+ |
{ |
| 726 |
+ |
while (cur_headlen > 0 && isspace(head_buf[cur_headlen-1])) |
| 727 |
+ |
--cur_headlen; |
| 728 |
+ |
head_buf[cur_headlen] = '\0'; |
| 729 |
+ |
if (!cur_headlen) |
| 730 |
+ |
return; |
| 731 |
+ |
add_wbsdf("-C", 1); |
| 732 |
+ |
add_wbsdf(head_buf, 0); |
| 733 |
+ |
head_buf[cur_headlen=0] = '\0'; |
| 734 |
+ |
} |
| 735 |
+ |
|
| 736 |
|
/* Read in BSDF and interpolate as tensor tree representation */ |
| 737 |
|
int |
| 738 |
|
main(int argc, char *argv[]) |
| 739 |
|
{ |
| 740 |
< |
static char tfmt[2][4] = {"t4", "t3"}; |
| 741 |
< |
int dofwd = 0, dobwd = 1; |
| 742 |
< |
char buf[2048]; |
| 743 |
< |
int i, na; |
| 744 |
< |
|
| 745 |
< |
progname = argv[0]; |
| 740 |
> |
static const char tfmt[2][4] = {"t4", "t3"}; |
| 741 |
> |
int dofwd = 0, dobwd = 1; |
| 742 |
> |
int nsirs = 0; |
| 743 |
> |
char buf[1024]; |
| 744 |
> |
int i; |
| 745 |
> |
/* set global progname */ |
| 746 |
> |
fixargv0(argv[0]); |
| 747 |
|
esupport |= E_VARIABLE|E_FUNCTION|E_RCONST; |
| 748 |
|
esupport &= ~(E_INCHAN|E_OUTCHAN); |
| 749 |
|
scompile("PI:3.14159265358979323846", NULL, 0); |
| 750 |
|
biggerlib(); |
| 751 |
< |
for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
| 752 |
< |
switch (argv[i][1]) { /* get options */ |
| 751 |
> |
strcpy(buf, "File produced by: "); |
| 752 |
> |
if (convert_commandline(buf+18, sizeof(buf)-18, argv) != NULL) { |
| 753 |
> |
add_wbsdf("-C", 1); add_wbsdf(buf, 0); |
| 754 |
> |
} |
| 755 |
> |
for (i = 1; i < argc; i++) |
| 756 |
> |
if ((argv[i][0] == '-') | (argv[i][0] == '+')) { |
| 757 |
> |
switch (argv[i][1]) { /* get option */ |
| 758 |
|
case 'e': |
| 759 |
|
scompile(argv[++i], NULL, 0); |
| 760 |
|
if (single_plane_incident < 0) |
| 761 |
|
single_plane_incident = 0; |
| 762 |
|
break; |
| 763 |
|
case 'f': |
| 764 |
< |
if (!argv[i][2]) { |
| 764 |
> |
if ((argv[i][0] == '-') & !argv[i][2]) { |
| 765 |
|
if (strchr(argv[++i], '=') != NULL) { |
| 766 |
|
add_wbsdf("-f", 1); |
| 767 |
|
add_wbsdf(argv[i], 1); |
| 832 |
|
default: |
| 833 |
|
goto userr; |
| 834 |
|
} |
| 835 |
< |
strcpy(buf, "File produced by: "); |
| 836 |
< |
if (convert_commandline(buf+18, sizeof(buf)-18, argv) != NULL) { |
| 837 |
< |
add_wbsdf("-C", 1); add_wbsdf(buf, 0); |
| 838 |
< |
} |
| 839 |
< |
if (single_plane_incident >= 0) { /* function-based BSDF? */ |
| 835 |
> |
} else { /* input SIR or function */ |
| 836 |
> |
FILE *fpin; |
| 837 |
> |
if (!nsirs & (single_plane_incident >= 0)) |
| 838 |
> |
break; /* must be a function */ |
| 839 |
> |
if (nsirs >= 4) { |
| 840 |
> |
fprintf(stderr, "At most 4 SIR inputs supported\n"); |
| 841 |
> |
goto userr; |
| 842 |
> |
} |
| 843 |
> |
fpin = fopen(argv[i], "rb"); /* open SIR file */ |
| 844 |
> |
if (fpin == NULL) { |
| 845 |
> |
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
| 846 |
> |
progname, argv[i]); |
| 847 |
> |
return(1); |
| 848 |
> |
} |
| 849 |
> |
sprintf(buf, "%s:\n", argv[i]); |
| 850 |
> |
record2header(buf); |
| 851 |
> |
sir_headshare = &record2header; |
| 852 |
> |
if (!load_bsdf_rep(fpin)) |
| 853 |
> |
return(1); |
| 854 |
> |
fclose(fpin); |
| 855 |
> |
done_header(); |
| 856 |
> |
sprintf(buf, "Interpolating component '%s'", argv[i]); |
| 857 |
> |
prog_start(buf); |
| 858 |
> |
if (!nsirs++) { |
| 859 |
> |
add_wbsdf("-a", 1); |
| 860 |
> |
add_wbsdf(tfmt[single_plane_incident], 1); |
| 861 |
> |
} |
| 862 |
> |
if (single_plane_incident) |
| 863 |
> |
eval_isotropic(NULL); |
| 864 |
> |
else |
| 865 |
> |
eval_anisotropic(NULL); |
| 866 |
> |
} |
| 867 |
> |
if (i < argc) { /* function-based BSDF? */ |
| 868 |
|
void (*evf)(char *s) = single_plane_incident ? |
| 869 |
|
eval_isotropic : eval_anisotropic; |
| 870 |
|
if (i != argc-1 || fundefined(argv[i]) < 3) { |
| 874 |
|
fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n"); |
| 875 |
|
goto userr; |
| 876 |
|
} |
| 877 |
+ |
doptimize(1); /* optimize definitions */ |
| 878 |
|
++eclock; |
| 879 |
|
add_wbsdf("-a", 1); |
| 880 |
|
add_wbsdf(tfmt[single_plane_incident], 1); |
| 896 |
|
prog_start("Evaluating inside->outside transmission"); |
| 897 |
|
(*evf)(argv[i]); |
| 898 |
|
} |
| 899 |
< |
return(wrap_up()); |
| 899 |
> |
} else if (!nsirs) { /* else load SIR from stdin? */ |
| 900 |
> |
record2header("<stdin>:\n"); |
| 901 |
> |
sir_headshare = &record2header; |
| 902 |
> |
if (!load_bsdf_rep(stdin)) |
| 903 |
> |
return(1); |
| 904 |
> |
done_header(); |
| 905 |
> |
prog_start("Interpolating from standard input"); |
| 906 |
> |
add_wbsdf("-a", 1); |
| 907 |
> |
add_wbsdf(tfmt[single_plane_incident], 1); |
| 908 |
> |
if (single_plane_incident) /* resample dist. */ |
| 909 |
> |
eval_isotropic(NULL); |
| 910 |
> |
else |
| 911 |
> |
eval_anisotropic(NULL); |
| 912 |
|
} |
| 913 |
< |
if (i < argc) { /* open input files if given */ |
| 835 |
< |
int nbsdf = 0; |
| 836 |
< |
for ( ; i < argc; i++) { /* interpolate each component */ |
| 837 |
< |
char pbuf[256]; |
| 838 |
< |
FILE *fpin = fopen(argv[i], "rb"); |
| 839 |
< |
if (fpin == NULL) { |
| 840 |
< |
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
| 841 |
< |
progname, argv[i]); |
| 842 |
< |
return(1); |
| 843 |
< |
} |
| 844 |
< |
if (!load_bsdf_rep(fpin)) |
| 845 |
< |
return(1); |
| 846 |
< |
fclose(fpin); |
| 847 |
< |
sprintf(pbuf, "Interpolating component '%s'", argv[i]); |
| 848 |
< |
prog_start(pbuf); |
| 849 |
< |
if (!nbsdf++) { |
| 850 |
< |
add_wbsdf("-a", 1); |
| 851 |
< |
add_wbsdf(tfmt[single_plane_incident], 1); |
| 852 |
< |
} |
| 853 |
< |
if (single_plane_incident) |
| 854 |
< |
eval_isotropic(NULL); |
| 855 |
< |
else |
| 856 |
< |
eval_anisotropic(NULL); |
| 857 |
< |
} |
| 858 |
< |
return(wrap_up()); |
| 859 |
< |
} |
| 860 |
< |
SET_FILE_BINARY(stdin); /* load from stdin */ |
| 861 |
< |
if (!load_bsdf_rep(stdin)) |
| 862 |
< |
return(1); |
| 863 |
< |
prog_start("Interpolating from standard input"); |
| 864 |
< |
add_wbsdf("-a", 1); |
| 865 |
< |
add_wbsdf(tfmt[single_plane_incident], 1); |
| 866 |
< |
if (single_plane_incident) /* resample dist. */ |
| 867 |
< |
eval_isotropic(NULL); |
| 868 |
< |
else |
| 869 |
< |
eval_anisotropic(NULL); |
| 870 |
< |
|
| 871 |
< |
return(wrap_up()); |
| 913 |
> |
return(wrap_up()); /* call wrapBSDF */ |
| 914 |
|
userr: |
| 915 |
|
fprintf(stderr, |
| 916 |
< |
"Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-n nss][-s thresh][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
| 916 |
> |
"Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-n nss][-s thresh][-l maxlobes][bsdf.sir ..] > bsdf.xml\n", |
| 917 |
|
progname); |
| 918 |
|
fprintf(stderr, |
| 919 |
|
" or: %s -t{3|4} [{+|-}a][-g Nlog2][-t pctcull][-n nss][-s thresh][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |