1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.4 |
static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.3 2012/10/23 05:10:42 greg Exp $"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Load measured BSDF interpolant and write out as XML file with tensor tree. |
6 |
|
|
* |
7 |
|
|
* G. Ward |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#define _USE_MATH_DEFINES |
11 |
|
|
#include <stdio.h> |
12 |
|
|
#include <stdlib.h> |
13 |
|
|
#include <math.h> |
14 |
|
|
#include "platform.h" |
15 |
|
|
#include "bsdfrep.h" |
16 |
|
|
/* global argv[0] */ |
17 |
|
|
char *progname; |
18 |
|
|
/* percentage to cull (<0 to turn off) */ |
19 |
|
|
int pctcull = 90; |
20 |
|
|
/* sampling order */ |
21 |
|
|
int samp_order = 6; |
22 |
|
|
|
23 |
|
|
/* Interpolate and output isotropic BSDF data */ |
24 |
|
|
static void |
25 |
|
|
interp_isotropic() |
26 |
|
|
{ |
27 |
|
|
const int sqres = 1<<samp_order; |
28 |
|
|
FILE *ofp = NULL; |
29 |
|
|
char cmd[128]; |
30 |
|
|
int ix, ox, oy; |
31 |
|
|
FVECT ivec, ovec; |
32 |
greg |
2.4 |
float bsdf; |
33 |
greg |
2.1 |
#if DEBUG |
34 |
|
|
fprintf(stderr, "Writing isotropic order %d ", samp_order); |
35 |
|
|
if (pctcull >= 0) fprintf(stderr, "data with %d%% culling\n", pctcull); |
36 |
|
|
else fputs("raw data\n", stderr); |
37 |
|
|
#endif |
38 |
|
|
if (pctcull >= 0) { /* begin output */ |
39 |
greg |
2.4 |
sprintf(cmd, "rttree_reduce -h -a -ff -r 3 -t %d -g %d", |
40 |
greg |
2.1 |
pctcull, samp_order); |
41 |
|
|
fflush(stdout); |
42 |
|
|
ofp = popen(cmd, "w"); |
43 |
|
|
if (ofp == NULL) { |
44 |
|
|
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
45 |
|
|
progname); |
46 |
|
|
exit(1); |
47 |
|
|
} |
48 |
|
|
SET_FILE_BINARY(ofp); |
49 |
|
|
} else |
50 |
|
|
fputs("{\n", stdout); |
51 |
|
|
/* run through directions */ |
52 |
|
|
for (ix = 0; ix < sqres/2; ix++) { |
53 |
|
|
RBFNODE *rbf; |
54 |
|
|
SDsquare2disk(ivec, (ix+.5)/sqres, .5); |
55 |
|
|
ivec[2] = input_orient * |
56 |
|
|
sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]); |
57 |
|
|
rbf = advect_rbf(ivec); |
58 |
|
|
for (ox = 0; ox < sqres; ox++) |
59 |
|
|
for (oy = 0; oy < sqres; oy++) { |
60 |
|
|
SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres); |
61 |
|
|
ovec[2] = output_orient * |
62 |
|
|
sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]); |
63 |
|
|
bsdf = eval_rbfrep(rbf, ovec) / fabs(ovec[2]); |
64 |
|
|
if (pctcull >= 0) |
65 |
|
|
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
66 |
|
|
else |
67 |
|
|
printf("\t%.3e\n", bsdf); |
68 |
|
|
} |
69 |
greg |
2.3 |
if (rbf != NULL) |
70 |
|
|
free(rbf); |
71 |
greg |
2.1 |
} |
72 |
|
|
if (pctcull >= 0) { /* finish output */ |
73 |
|
|
if (pclose(ofp)) { |
74 |
|
|
fprintf(stderr, "%s: error running '%s'\n", |
75 |
|
|
progname, cmd); |
76 |
|
|
exit(1); |
77 |
|
|
} |
78 |
|
|
} else { |
79 |
|
|
for (ix = sqres*sqres*sqres/2; ix--; ) |
80 |
|
|
fputs("\t0\n", stdout); |
81 |
|
|
fputs("}\n", stdout); |
82 |
|
|
} |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
/* Interpolate and output anisotropic BSDF data */ |
86 |
|
|
static void |
87 |
|
|
interp_anisotropic() |
88 |
|
|
{ |
89 |
|
|
const int sqres = 1<<samp_order; |
90 |
|
|
FILE *ofp = NULL; |
91 |
|
|
char cmd[128]; |
92 |
|
|
int ix, iy, ox, oy; |
93 |
|
|
FVECT ivec, ovec; |
94 |
greg |
2.4 |
float bsdf; |
95 |
greg |
2.1 |
#if DEBUG |
96 |
|
|
fprintf(stderr, "Writing anisotropic order %d ", samp_order); |
97 |
|
|
if (pctcull >= 0) fprintf(stderr, "data with %d%% culling\n", pctcull); |
98 |
|
|
else fputs("raw data\n", stderr); |
99 |
|
|
#endif |
100 |
|
|
if (pctcull >= 0) { /* begin output */ |
101 |
greg |
2.4 |
sprintf(cmd, "rttree_reduce -h -a -ff -r 4 -t %d -g %d", |
102 |
greg |
2.1 |
pctcull, samp_order); |
103 |
|
|
fflush(stdout); |
104 |
|
|
ofp = popen(cmd, "w"); |
105 |
|
|
if (ofp == NULL) { |
106 |
|
|
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
107 |
|
|
progname); |
108 |
|
|
exit(1); |
109 |
|
|
} |
110 |
|
|
} else |
111 |
|
|
fputs("{\n", stdout); |
112 |
|
|
/* run through directions */ |
113 |
|
|
for (ix = 0; ix < sqres; ix++) |
114 |
|
|
for (iy = 0; iy < sqres; iy++) { |
115 |
|
|
RBFNODE *rbf; |
116 |
|
|
SDsquare2disk(ivec, (ix+.5)/sqres, (iy+.5)/sqres); |
117 |
|
|
ivec[2] = input_orient * |
118 |
|
|
sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]); |
119 |
|
|
rbf = advect_rbf(ivec); |
120 |
|
|
for (ox = 0; ox < sqres; ox++) |
121 |
|
|
for (oy = 0; oy < sqres; oy++) { |
122 |
|
|
SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres); |
123 |
|
|
ovec[2] = output_orient * |
124 |
|
|
sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]); |
125 |
|
|
bsdf = eval_rbfrep(rbf, ovec) / fabs(ovec[2]); |
126 |
|
|
if (pctcull >= 0) |
127 |
|
|
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
128 |
|
|
else |
129 |
|
|
printf("\t%.3e\n", bsdf); |
130 |
|
|
} |
131 |
greg |
2.3 |
if (rbf != NULL) |
132 |
|
|
free(rbf); |
133 |
greg |
2.1 |
} |
134 |
|
|
if (pctcull >= 0) { /* finish output */ |
135 |
|
|
if (pclose(ofp)) { |
136 |
|
|
fprintf(stderr, "%s: error running '%s'\n", |
137 |
|
|
progname, cmd); |
138 |
|
|
exit(1); |
139 |
|
|
} |
140 |
|
|
} else |
141 |
|
|
fputs("}\n", stdout); |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
/* Read in BSDF and interpolate as tensor tree representation */ |
145 |
|
|
int |
146 |
|
|
main(int argc, char *argv[]) |
147 |
|
|
{ |
148 |
|
|
FILE *fpin = stdin; |
149 |
|
|
int i; |
150 |
|
|
|
151 |
|
|
progname = argv[0]; /* get options */ |
152 |
|
|
while (argc > 2 && argv[1][0] == '-') { |
153 |
|
|
switch (argv[1][1]) { |
154 |
|
|
case 't': |
155 |
|
|
pctcull = atoi(argv[2]); |
156 |
|
|
break; |
157 |
|
|
case 'g': |
158 |
|
|
samp_order = atoi(argv[2]); |
159 |
|
|
break; |
160 |
|
|
default: |
161 |
|
|
goto userr; |
162 |
|
|
} |
163 |
|
|
argv += 2; argc -= 2; |
164 |
|
|
} |
165 |
greg |
2.2 |
if (argc == 2) { /* open input if given */ |
166 |
greg |
2.1 |
fpin = fopen(argv[1], "r"); |
167 |
greg |
2.2 |
if (fpin == NULL) { |
168 |
|
|
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
169 |
|
|
progname, argv[1]); |
170 |
|
|
return(1); |
171 |
|
|
} |
172 |
|
|
} else if (argc != 1) |
173 |
greg |
2.1 |
goto userr; |
174 |
|
|
SET_FILE_BINARY(fpin); /* load BSDF interpolant */ |
175 |
|
|
if (!load_bsdf_rep(fpin)) |
176 |
|
|
return(1); |
177 |
|
|
/* xml_prologue(); /* start XML output */ |
178 |
|
|
if (single_plane_incident) /* resample dist. */ |
179 |
|
|
interp_isotropic(); |
180 |
|
|
else |
181 |
|
|
interp_anisotropic(); |
182 |
|
|
/* xml_epilogue(); /* finish XML output */ |
183 |
|
|
return(0); |
184 |
|
|
userr: |
185 |
|
|
fprintf(stderr, |
186 |
|
|
"Usage: %s [-t pctcull][-g log2grid] [bsdf.sir] > bsdf.xml\n", |
187 |
|
|
progname); |
188 |
|
|
return(1); |
189 |
|
|
} |