10 |
|
#define _USE_MATH_DEFINES |
11 |
|
#include <stdlib.h> |
12 |
|
#include <math.h> |
13 |
+ |
#include <ctype.h> |
14 |
|
#include "random.h" |
15 |
|
#include "platform.h" |
16 |
|
#include "paths.h" |
135 |
|
int assignD = 0; |
136 |
|
char cmd[128]; |
137 |
|
int ix, ox, oy; |
138 |
< |
double iovec[6]; |
138 |
> |
RREAL iovec[6]; |
139 |
|
float bsdf, uv[2]; |
140 |
|
|
141 |
|
if (pctcull >= 0) { |
208 |
|
rbf = advect_rbf(iovec, lobe_lim); |
209 |
|
/* presample first row */ |
210 |
|
for (oy = 0; oy < sqres; oy++) { |
211 |
< |
SDsquare2disk(iovec+3, .5*sqfact, (oy+.5)*sqfact); |
211 |
> |
square2disk(iovec+3, .5*sqfact, (oy+.5)*sqfact); |
212 |
|
iovec[5] = output_orient * |
213 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
214 |
|
if (funame == NULL) { |
230 |
|
*/ |
231 |
|
for (oy = 0; oy < sqres; oy++) { |
232 |
|
if (ox < sqres-1) { /* keeping one row ahead... */ |
233 |
< |
SDsquare2disk(iovec+3, (ox+1.5)*sqfact, (oy+.5)*sqfact); |
233 |
> |
square2disk(iovec+3, (ox+1.5)*sqfact, (oy+.5)*sqfact); |
234 |
|
iovec[5] = output_orient * |
235 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
236 |
|
} |
250 |
|
for (ssi = nssamp; ssi--; ) { |
251 |
|
SDmultiSamp(ssa, 2, (ssi+frandom()) / |
252 |
|
(double)nssamp); |
253 |
< |
SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, |
253 |
> |
square2disk(iovec+3, (ox+ssa[0])*sqfact, |
254 |
|
(oy+ssa[1])*sqfact); |
255 |
|
iovec[5] = output_orient * |
256 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
268 |
|
uv[0] = usum / (sum+FTINY); |
269 |
|
uv[1] = vsum / (sum+FTINY); |
270 |
|
} |
271 |
< |
} else |
271 |
< |
if (rbf_colorimetry == RBCtristimulus) { |
271 |
> |
} else if (rbf_colorimetry == RBCtristimulus) { |
272 |
|
uv[0] = uv[1] = 1. / |
273 |
|
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
274 |
|
uv[0] *= 4.*sdv.spec.cx; |
305 |
|
ssvec[2] = 1. - ssvec[0]*ssvec[0]; |
306 |
|
} |
307 |
|
ssvec[2] = input_orient * sqrt(ssvec[2]); |
308 |
< |
SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact, |
308 |
> |
square2disk(ssvec+3, (ox+ssa[2])*sqfact, |
309 |
|
(oy+ssa[3])*sqfact); |
310 |
|
ssvec[5] = output_orient * |
311 |
|
sqrt(1. - ssvec[3]*ssvec[3] - |
399 |
|
int assignD = 0; |
400 |
|
char cmd[128]; |
401 |
|
int ix, iy, ox, oy; |
402 |
< |
double iovec[6]; |
402 |
> |
RREAL iovec[6]; |
403 |
|
float bsdf, uv[2]; |
404 |
|
|
405 |
|
if (pctcull >= 0) { |
469 |
|
for (ix = 0; ix < sqres; ix++) |
470 |
|
for (iy = 0; iy < sqres; iy++) { |
471 |
|
RBFNODE *rbf = NULL; /* Klems reversal */ |
472 |
< |
SDsquare2disk(iovec, 1.-(ix+.5)*sqfact, 1.-(iy+.5)*sqfact); |
472 |
> |
square2disk(iovec, 1.-(ix+.5)*sqfact, 1.-(iy+.5)*sqfact); |
473 |
|
iovec[2] = input_orient * |
474 |
|
sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]); |
475 |
|
if (funame == NULL) |
476 |
|
rbf = advect_rbf(iovec, lobe_lim); |
477 |
|
/* presample first row */ |
478 |
|
for (oy = 0; oy < sqres; oy++) { |
479 |
< |
SDsquare2disk(iovec+3, .5*sqfact, (oy+.5)*sqfact); |
479 |
> |
square2disk(iovec+3, .5*sqfact, (oy+.5)*sqfact); |
480 |
|
iovec[5] = output_orient * |
481 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
482 |
|
if (funame == NULL) { |
498 |
|
*/ |
499 |
|
for (oy = 0; oy < sqres; oy++) { |
500 |
|
if (ox < sqres-1) { /* keeping one row ahead... */ |
501 |
< |
SDsquare2disk(iovec+3, (ox+1.5)*sqfact, (oy+.5)*sqfact); |
501 |
> |
square2disk(iovec+3, (ox+1.5)*sqfact, (oy+.5)*sqfact); |
502 |
|
iovec[5] = output_orient * |
503 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
504 |
|
} |
518 |
|
for (ssi = nssamp; ssi--; ) { |
519 |
|
SDmultiSamp(ssa, 2, (ssi+frandom()) / |
520 |
|
(double)nssamp); |
521 |
< |
SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, |
521 |
> |
square2disk(iovec+3, (ox+ssa[0])*sqfact, |
522 |
|
(oy+ssa[1])*sqfact); |
523 |
|
iovec[5] = output_orient * |
524 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
536 |
|
uv[0] = usum / (sum+FTINY); |
537 |
|
uv[1] = vsum / (sum+FTINY); |
538 |
|
} |
539 |
< |
} else |
540 |
< |
if (rbf_colorimetry == RBCtristimulus) { |
539 |
> |
} else if (rbf_colorimetry == RBCtristimulus) { |
540 |
|
uv[0] = uv[1] = 1. / |
541 |
|
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
542 |
|
uv[0] *= 4.*sdv.spec.cx; |
564 |
|
for (ssi = nssamp; ssi--; ) { |
565 |
|
SDmultiSamp(ssa, 4, (ssi+frandom()) / |
566 |
|
(double)nssamp); |
567 |
< |
SDsquare2disk(ssvec, 1.-(ix+ssa[0])*sqfact, |
567 |
> |
square2disk(ssvec, 1.-(ix+ssa[0])*sqfact, |
568 |
|
1.-(iy+ssa[1])*sqfact); |
569 |
|
ssvec[2] = input_orient * |
570 |
|
sqrt(1. - ssvec[0]*ssvec[0] - |
571 |
|
ssvec[1]*ssvec[1]); |
572 |
< |
SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact, |
572 |
> |
square2disk(ssvec+3, (ox+ssa[2])*sqfact, |
573 |
|
(oy+ssa[3])*sqfact); |
574 |
|
ssvec[5] = output_orient * |
575 |
|
sqrt(1. - ssvec[3]*ssvec[3] - |
649 |
|
static int |
650 |
|
wrap_up(void) |
651 |
|
{ |
652 |
< |
char cmd[8192]; |
652 |
> |
char cmd[32700]; |
653 |
|
|
654 |
|
if (bsdf_manuf[0]) { |
655 |
|
add_wbsdf("-f", 1); |
700 |
|
} |
701 |
|
#endif |
702 |
|
|
703 |
+ |
#define HEAD_BUFLEN 10240 |
704 |
+ |
static char head_buf[HEAD_BUFLEN]; |
705 |
+ |
static int cur_headlen = 0; |
706 |
+ |
|
707 |
+ |
/* Record header line as comment associated with this SIR input */ |
708 |
+ |
static int |
709 |
+ |
record2header(char *s) |
710 |
+ |
{ |
711 |
+ |
int len = strlen(s); |
712 |
+ |
|
713 |
+ |
if (cur_headlen+len >= HEAD_BUFLEN-6) |
714 |
+ |
return(0); |
715 |
+ |
/* includes EOL */ |
716 |
+ |
strcpy(head_buf+cur_headlen, s); |
717 |
+ |
cur_headlen += len; |
718 |
+ |
|
719 |
+ |
#if defined(_WIN32) || defined(_WIN64) |
720 |
+ |
if (head_buf[cur_headlen-1] == '\n') |
721 |
+ |
head_buf[cur_headlen-1] = '\t'; |
722 |
+ |
#endif |
723 |
+ |
return(1); |
724 |
+ |
} |
725 |
+ |
|
726 |
+ |
/* Finish off header for this file */ |
727 |
+ |
static void |
728 |
+ |
done_header(void) |
729 |
+ |
{ |
730 |
+ |
while (cur_headlen > 0 && isspace(head_buf[cur_headlen-1])) |
731 |
+ |
--cur_headlen; |
732 |
+ |
head_buf[cur_headlen] = '\0'; |
733 |
+ |
if (!cur_headlen) |
734 |
+ |
return; |
735 |
+ |
add_wbsdf("-C", 1); |
736 |
+ |
add_wbsdf(head_buf, 0); |
737 |
+ |
head_buf[cur_headlen=0] = '\0'; |
738 |
+ |
} |
739 |
+ |
|
740 |
|
/* Read in BSDF and interpolate as tensor tree representation */ |
741 |
|
int |
742 |
|
main(int argc, char *argv[]) |
743 |
|
{ |
744 |
< |
static char tfmt[2][4] = {"t4", "t3"}; |
745 |
< |
int dofwd = 0, dobwd = 1; |
746 |
< |
char buf[2048]; |
747 |
< |
int i, na; |
744 |
> |
static const char tfmt[2][4] = {"t4", "t3"}; |
745 |
> |
int dofwd = 0, dobwd = 1; |
746 |
> |
int nsirs = 0; |
747 |
> |
char buf[1024]; |
748 |
> |
int i; |
749 |
|
|
750 |
|
progname = argv[0]; |
751 |
|
esupport |= E_VARIABLE|E_FUNCTION|E_RCONST; |
752 |
|
esupport &= ~(E_INCHAN|E_OUTCHAN); |
753 |
|
scompile("PI:3.14159265358979323846", NULL, 0); |
754 |
|
biggerlib(); |
755 |
< |
for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
756 |
< |
switch (argv[i][1]) { /* get options */ |
755 |
> |
strcpy(buf, "File produced by: "); |
756 |
> |
if (convert_commandline(buf+18, sizeof(buf)-18, argv) != NULL) { |
757 |
> |
add_wbsdf("-C", 1); add_wbsdf(buf, 0); |
758 |
> |
} |
759 |
> |
for (i = 1; i < argc; i++) |
760 |
> |
if ((argv[i][0] == '-') | (argv[i][0] == '+')) { |
761 |
> |
switch (argv[i][1]) { /* get option */ |
762 |
|
case 'e': |
763 |
|
scompile(argv[++i], NULL, 0); |
764 |
|
if (single_plane_incident < 0) |
765 |
|
single_plane_incident = 0; |
766 |
|
break; |
767 |
|
case 'f': |
768 |
< |
if (!argv[i][2]) { |
768 |
> |
if ((argv[i][0] == '-') & !argv[i][2]) { |
769 |
|
if (strchr(argv[++i], '=') != NULL) { |
770 |
|
add_wbsdf("-f", 1); |
771 |
|
add_wbsdf(argv[i], 1); |
836 |
|
default: |
837 |
|
goto userr; |
838 |
|
} |
839 |
< |
strcpy(buf, "File produced by: "); |
840 |
< |
if (convert_commandline(buf+18, sizeof(buf)-18, argv) != NULL) { |
841 |
< |
add_wbsdf("-C", 1); add_wbsdf(buf, 0); |
842 |
< |
} |
843 |
< |
if (single_plane_incident >= 0) { /* function-based BSDF? */ |
839 |
> |
} else { /* input SIR or function */ |
840 |
> |
FILE *fpin; |
841 |
> |
if (!nsirs & (single_plane_incident >= 0)) |
842 |
> |
break; /* must be a function */ |
843 |
> |
if (nsirs >= 4) { |
844 |
> |
fprintf(stderr, "At most 4 SIR inputs supported\n"); |
845 |
> |
goto userr; |
846 |
> |
} |
847 |
> |
fpin = fopen(argv[i], "rb"); /* open SIR file */ |
848 |
> |
if (fpin == NULL) { |
849 |
> |
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
850 |
> |
progname, argv[i]); |
851 |
> |
return(1); |
852 |
> |
} |
853 |
> |
sprintf(buf, "%s:\n", argv[i]); |
854 |
> |
record2header(buf); |
855 |
> |
sir_headshare = &record2header; |
856 |
> |
if (!load_bsdf_rep(fpin)) |
857 |
> |
return(1); |
858 |
> |
fclose(fpin); |
859 |
> |
done_header(); |
860 |
> |
sprintf(buf, "Interpolating component '%s'", argv[i]); |
861 |
> |
prog_start(buf); |
862 |
> |
if (!nsirs++) { |
863 |
> |
add_wbsdf("-a", 1); |
864 |
> |
add_wbsdf(tfmt[single_plane_incident], 1); |
865 |
> |
} |
866 |
> |
if (single_plane_incident) |
867 |
> |
eval_isotropic(NULL); |
868 |
> |
else |
869 |
> |
eval_anisotropic(NULL); |
870 |
> |
} |
871 |
> |
if (i < argc) { /* function-based BSDF? */ |
872 |
|
void (*evf)(char *s) = single_plane_incident ? |
873 |
|
eval_isotropic : eval_anisotropic; |
874 |
|
if (i != argc-1 || fundefined(argv[i]) < 3) { |
899 |
|
prog_start("Evaluating inside->outside transmission"); |
900 |
|
(*evf)(argv[i]); |
901 |
|
} |
902 |
< |
return(wrap_up()); |
902 |
> |
} else if (!nsirs) { /* else load SIR from stdin? */ |
903 |
> |
record2header("<stdin>:\n"); |
904 |
> |
sir_headshare = &record2header; |
905 |
> |
if (!load_bsdf_rep(stdin)) |
906 |
> |
return(1); |
907 |
> |
done_header(); |
908 |
> |
prog_start("Interpolating from standard input"); |
909 |
> |
add_wbsdf("-a", 1); |
910 |
> |
add_wbsdf(tfmt[single_plane_incident], 1); |
911 |
> |
if (single_plane_incident) /* resample dist. */ |
912 |
> |
eval_isotropic(NULL); |
913 |
> |
else |
914 |
> |
eval_anisotropic(NULL); |
915 |
|
} |
916 |
< |
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()); |
916 |
> |
return(wrap_up()); /* call wrapBSDF */ |
917 |
|
userr: |
918 |
|
fprintf(stderr, |
919 |
< |
"Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-n nss][-s thresh][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
919 |
> |
"Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-n nss][-s thresh][-l maxlobes][bsdf.sir ..] > bsdf.xml\n", |
920 |
|
progname); |
921 |
|
fprintf(stderr, |
922 |
|
" 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", |