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" |
651 |
|
static int |
652 |
|
wrap_up(void) |
653 |
|
{ |
654 |
< |
char cmd[8192]; |
654 |
> |
char cmd[32700]; |
655 |
|
|
656 |
|
if (bsdf_manuf[0]) { |
657 |
|
add_wbsdf("-f", 1); |
702 |
|
} |
703 |
|
#endif |
704 |
|
|
705 |
+ |
#define HEAD_BUFLEN 10240 |
706 |
+ |
static char head_buf[HEAD_BUFLEN]; |
707 |
+ |
static int cur_headlen = 0; |
708 |
+ |
|
709 |
+ |
/* Record header line as comment associated with this SIR input */ |
710 |
+ |
static int |
711 |
+ |
record2header(char *s) |
712 |
+ |
{ |
713 |
+ |
int len = strlen(s); |
714 |
+ |
|
715 |
+ |
if (cur_headlen+len >= HEAD_BUFLEN-6) |
716 |
+ |
return(0); |
717 |
+ |
/* includes EOL */ |
718 |
+ |
strcpy(head_buf+cur_headlen, s); |
719 |
+ |
cur_headlen += len; |
720 |
+ |
|
721 |
+ |
#if defined(_WIN32) || defined(_WIN64) |
722 |
+ |
if (head_buf[cur_headlen-1] == '\n') |
723 |
+ |
head_buf[cur_headlen-1] = '\t'; |
724 |
+ |
#endif |
725 |
+ |
return(1); |
726 |
+ |
} |
727 |
+ |
|
728 |
+ |
/* Finish off header for this file */ |
729 |
+ |
static void |
730 |
+ |
done_header(void) |
731 |
+ |
{ |
732 |
+ |
while (cur_headlen > 0 && isspace(head_buf[cur_headlen-1])) |
733 |
+ |
--cur_headlen; |
734 |
+ |
head_buf[cur_headlen] = '\0'; |
735 |
+ |
if (!cur_headlen) |
736 |
+ |
return; |
737 |
+ |
add_wbsdf("-C", 1); |
738 |
+ |
add_wbsdf(head_buf, 0); |
739 |
+ |
head_buf[cur_headlen=0] = '\0'; |
740 |
+ |
} |
741 |
+ |
|
742 |
|
/* Read in BSDF and interpolate as tensor tree representation */ |
743 |
|
int |
744 |
|
main(int argc, char *argv[]) |
745 |
|
{ |
746 |
< |
static char tfmt[2][4] = {"t4", "t3"}; |
747 |
< |
int dofwd = 0, dobwd = 1; |
748 |
< |
char buf[2048]; |
749 |
< |
int i, na; |
746 |
> |
static const char tfmt[2][4] = {"t4", "t3"}; |
747 |
> |
int dofwd = 0, dobwd = 1; |
748 |
> |
int nsirs = 0; |
749 |
> |
char buf[1024]; |
750 |
> |
int i; |
751 |
|
|
752 |
|
progname = argv[0]; |
753 |
|
esupport |= E_VARIABLE|E_FUNCTION|E_RCONST; |
754 |
|
esupport &= ~(E_INCHAN|E_OUTCHAN); |
755 |
|
scompile("PI:3.14159265358979323846", NULL, 0); |
756 |
|
biggerlib(); |
757 |
< |
for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
758 |
< |
switch (argv[i][1]) { /* get options */ |
757 |
> |
strcpy(buf, "File produced by: "); |
758 |
> |
if (convert_commandline(buf+18, sizeof(buf)-18, argv) != NULL) { |
759 |
> |
add_wbsdf("-C", 1); add_wbsdf(buf, 0); |
760 |
> |
} |
761 |
> |
for (i = 1; i < argc; i++) |
762 |
> |
if ((argv[i][0] == '-') | (argv[i][0] == '+')) { |
763 |
> |
switch (argv[i][1]) { /* get option */ |
764 |
|
case 'e': |
765 |
|
scompile(argv[++i], NULL, 0); |
766 |
|
if (single_plane_incident < 0) |
838 |
|
default: |
839 |
|
goto userr; |
840 |
|
} |
841 |
< |
strcpy(buf, "File produced by: "); |
842 |
< |
if (convert_commandline(buf+18, sizeof(buf)-18, argv) != NULL) { |
843 |
< |
add_wbsdf("-C", 1); add_wbsdf(buf, 0); |
844 |
< |
} |
845 |
< |
if (single_plane_incident >= 0) { /* function-based BSDF? */ |
841 |
> |
} else { /* input SIR or function */ |
842 |
> |
FILE *fpin; |
843 |
> |
if (!nsirs & (single_plane_incident >= 0)) |
844 |
> |
break; /* must be a function */ |
845 |
> |
if (nsirs >= 4) { |
846 |
> |
fprintf(stderr, "At most 4 SIR inputs supported\n"); |
847 |
> |
goto userr; |
848 |
> |
} |
849 |
> |
fpin = fopen(argv[i], "rb"); /* open SIR file */ |
850 |
> |
if (fpin == NULL) { |
851 |
> |
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
852 |
> |
progname, argv[i]); |
853 |
> |
return(1); |
854 |
> |
} |
855 |
> |
sprintf(buf, "%s:\n", argv[i]); |
856 |
> |
record2header(buf); |
857 |
> |
sir_headshare = &record2header; |
858 |
> |
if (!load_bsdf_rep(fpin)) |
859 |
> |
return(1); |
860 |
> |
fclose(fpin); |
861 |
> |
done_header(); |
862 |
> |
sprintf(buf, "Interpolating component '%s'", argv[i]); |
863 |
> |
prog_start(buf); |
864 |
> |
if (!nsirs++) { |
865 |
> |
add_wbsdf("-a", 1); |
866 |
> |
add_wbsdf(tfmt[single_plane_incident], 1); |
867 |
> |
} |
868 |
> |
if (single_plane_incident) |
869 |
> |
eval_isotropic(NULL); |
870 |
> |
else |
871 |
> |
eval_anisotropic(NULL); |
872 |
> |
} |
873 |
> |
if (i < argc) { /* function-based BSDF? */ |
874 |
|
void (*evf)(char *s) = single_plane_incident ? |
875 |
|
eval_isotropic : eval_anisotropic; |
876 |
|
if (i != argc-1 || fundefined(argv[i]) < 3) { |
901 |
|
prog_start("Evaluating inside->outside transmission"); |
902 |
|
(*evf)(argv[i]); |
903 |
|
} |
904 |
< |
return(wrap_up()); |
904 |
> |
} else if (!nsirs) { /* else load SIR from stdin? */ |
905 |
> |
record2header("<stdin>:\n"); |
906 |
> |
sir_headshare = &record2header; |
907 |
> |
if (!load_bsdf_rep(stdin)) |
908 |
> |
return(1); |
909 |
> |
done_header(); |
910 |
> |
prog_start("Interpolating from standard input"); |
911 |
> |
add_wbsdf("-a", 1); |
912 |
> |
add_wbsdf(tfmt[single_plane_incident], 1); |
913 |
> |
if (single_plane_incident) /* resample dist. */ |
914 |
> |
eval_isotropic(NULL); |
915 |
> |
else |
916 |
> |
eval_anisotropic(NULL); |
917 |
|
} |
918 |
< |
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()); |
918 |
> |
return(wrap_up()); /* call wrapBSDF */ |
919 |
|
userr: |
920 |
|
fprintf(stderr, |
921 |
< |
"Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-n nss][-s thresh][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
921 |
> |
"Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-n nss][-s thresh][-l maxlobes][bsdf.sir ..] > bsdf.xml\n", |
922 |
|
progname); |
923 |
|
fprintf(stderr, |
924 |
|
" 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", |