8 |
|
*/ |
9 |
|
|
10 |
|
#define _USE_MATH_DEFINES |
11 |
– |
#include <stdio.h> |
11 |
|
#include <stdlib.h> |
12 |
|
#include <math.h> |
13 |
+ |
#include <ctype.h> |
14 |
+ |
#include "random.h" |
15 |
|
#include "platform.h" |
16 |
+ |
#include "paths.h" |
17 |
+ |
#include "rtio.h" |
18 |
|
#include "calcomp.h" |
19 |
|
#include "bsdfrep.h" |
20 |
|
/* global argv[0] */ |
21 |
|
char *progname; |
22 |
+ |
/* reciprocity averaging option */ |
23 |
+ |
static const char *recip = " -a"; |
24 |
|
/* percentage to cull (<0 to turn off) */ |
25 |
< |
double pctcull = 90.; |
25 |
> |
static double pctcull = 90.; |
26 |
|
/* sampling order */ |
27 |
< |
int samp_order = 6; |
27 |
> |
static int samp_order = 6; |
28 |
> |
/* super-sampling threshold */ |
29 |
> |
static double ssamp_thresh = 0.35; |
30 |
> |
/* number of super-samples */ |
31 |
> |
static int nssamp = 256; |
32 |
> |
/* limit on number of RBF lobes */ |
33 |
> |
static int lobe_lim = 15000; |
34 |
> |
/* progress bar length */ |
35 |
> |
static int do_prog = 79; |
36 |
|
|
37 |
< |
/* Output XML prologue to stdout */ |
37 |
> |
#define MAXCARG 512 /* wrapBSDF command */ |
38 |
> |
static char *wrapBSDF[MAXCARG] = {"wrapBSDF", "-U"}; |
39 |
> |
static int wbsdfac = 2; |
40 |
> |
|
41 |
> |
/* Add argument to wrapBSDF, allocating space if !isstatic */ |
42 |
|
static void |
43 |
< |
xml_prologue(int ac, char *av[]) |
43 |
> |
add_wbsdf(const char *arg, int isstatic) |
44 |
|
{ |
45 |
< |
puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
46 |
< |
puts("<WindowElement xmlns=\"http://windows.lbl.gov\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://windows.lbl.gov/BSDF-v1.4.xsd\">"); |
47 |
< |
fputs("<!-- File produced by:", stdout); |
48 |
< |
while (ac-- > 0) { |
49 |
< |
fputc(' ', stdout); |
50 |
< |
fputs(*av++, stdout); |
45 |
> |
if (arg == NULL) |
46 |
> |
return; |
47 |
> |
if (wbsdfac >= MAXCARG-1) { |
48 |
> |
fputs(progname, stderr); |
49 |
> |
fputs(": too many command arguments to wrapBSDF\n", stderr); |
50 |
> |
exit(1); |
51 |
|
} |
52 |
< |
puts(" -->"); |
53 |
< |
puts("<WindowElementType>System</WindowElementType>"); |
54 |
< |
puts("<FileType>BSDF</FileType>"); |
55 |
< |
puts("<Optical>"); |
56 |
< |
puts("<Layer>"); |
57 |
< |
puts("\t<Material>"); |
41 |
< |
puts("\t\t<Name>Name</Name>"); |
42 |
< |
puts("\t\t<Manufacturer>Manufacturer</Manufacturer>"); |
43 |
< |
puts("\t\t<DeviceType>Other</DeviceType>"); |
44 |
< |
puts("\t</Material>"); |
45 |
< |
puts("\t<DataDefinition>"); |
46 |
< |
printf("\t\t<IncidentDataStructure>TensorTree%c</IncidentDataStructure>\n", |
47 |
< |
single_plane_incident ? '3' : '4'); |
48 |
< |
puts("\t</DataDefinition>"); |
52 |
> |
if (!*arg) |
53 |
> |
arg = ""; |
54 |
> |
else if (!isstatic) |
55 |
> |
arg = savqstr((char *)arg); |
56 |
> |
|
57 |
> |
wrapBSDF[wbsdfac++] = (char *)arg; |
58 |
|
} |
59 |
|
|
60 |
< |
/* Output XML data prologue to stdout */ |
61 |
< |
static void |
62 |
< |
data_prologue() |
60 |
> |
/* Create Yuv component file and add appropriate arguments */ |
61 |
> |
static char * |
62 |
> |
create_component_file(int c) |
63 |
|
{ |
64 |
< |
static const char *bsdf_type[4] = { |
65 |
< |
"Reflection Front", |
66 |
< |
"Transmission Front", |
58 |
< |
"Transmission Back", |
59 |
< |
"Reflection Back" |
60 |
< |
}; |
64 |
> |
static const char sname[3][6] = {"CIE-Y", "CIE-u", "CIE-v"}; |
65 |
> |
static const char cname[4][4] = {"-rf", "-tf", "-tb", "-rb"}; |
66 |
> |
char *tfname = mktemp(savqstr(TEMPLATE)); |
67 |
|
|
68 |
< |
puts("\t<WavelengthData>"); |
69 |
< |
puts("\t\t<LayerNumber>System</LayerNumber>"); |
70 |
< |
puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>"); |
71 |
< |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
66 |
< |
puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>"); |
67 |
< |
puts("\t\t<WavelengthDataBlock>"); |
68 |
< |
printf("\t\t\t<WavelengthDataDirection>%s</WavelengthDataDirection>\n", |
69 |
< |
bsdf_type[(input_orient>0)<<1 | (output_orient>0)]); |
70 |
< |
puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>"); |
71 |
< |
puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>"); |
72 |
< |
puts("\t\t\t<ScatteringData>"); |
68 |
> |
add_wbsdf("-s", 1); add_wbsdf(sname[c], 1); |
69 |
> |
add_wbsdf(cname[(input_orient>0)<<1 | (output_orient>0)], 1); |
70 |
> |
add_wbsdf(tfname, 1); |
71 |
> |
return(tfname); |
72 |
|
} |
73 |
|
|
74 |
< |
/* Output XML data epilogue to stdout */ |
74 |
> |
/* Start new progress bar */ |
75 |
> |
#define prog_start(s) if (do_prog) fprintf(stderr, "%s: %s...\n", progname, s); else |
76 |
> |
|
77 |
> |
/* Draw progress bar of the appropriate length */ |
78 |
|
static void |
79 |
< |
data_epilogue(void) |
79 |
> |
prog_show(double frac) |
80 |
|
{ |
81 |
< |
puts("\t\t\t</ScatteringData>"); |
82 |
< |
puts("\t\t</WavelengthDataBlock>"); |
83 |
< |
puts("\t</WavelengthData>"); |
81 |
> |
static unsigned call_cnt = 0; |
82 |
> |
static char lastc[] = "-\\|/"; |
83 |
> |
char pbar[256]; |
84 |
> |
int nchars; |
85 |
> |
|
86 |
> |
if (do_prog <= 1) return; |
87 |
> |
if (do_prog > sizeof(pbar)-2) |
88 |
> |
do_prog = sizeof(pbar)-2; |
89 |
> |
if (frac < 0) frac = 0; |
90 |
> |
else if (frac >= 1) frac = .9999; |
91 |
> |
nchars = do_prog*frac; |
92 |
> |
pbar[0] = '\r'; |
93 |
> |
memset(pbar+1, '*', nchars); |
94 |
> |
pbar[nchars+1] = lastc[call_cnt++ & 3]; |
95 |
> |
memset(pbar+2+nchars, '-', do_prog-nchars-1); |
96 |
> |
pbar[do_prog+1] = '\0'; |
97 |
> |
fputs(pbar, stderr); |
98 |
|
} |
99 |
|
|
100 |
< |
/* Output XML epilogue to stdout */ |
100 |
> |
/* Finish progress bar */ |
101 |
|
static void |
102 |
< |
xml_epilogue(void) |
102 |
> |
prog_done(void) |
103 |
|
{ |
104 |
< |
puts("</Layer>"); |
105 |
< |
puts("</Optical>"); |
106 |
< |
puts("</WindowElement>"); |
104 |
> |
int n = do_prog; |
105 |
> |
|
106 |
> |
if (n <= 1) return; |
107 |
> |
fputc('\r', stderr); |
108 |
> |
while (n--) |
109 |
> |
fputc(' ', stderr); |
110 |
> |
fputc('\r', stderr); |
111 |
|
} |
112 |
|
|
113 |
+ |
/* Compute absolute relative difference */ |
114 |
+ |
static double |
115 |
+ |
abs_diff(double v1, double v0) |
116 |
+ |
{ |
117 |
+ |
if ((v0 < 0) | (v1 < 0)) |
118 |
+ |
return(.0); |
119 |
+ |
v1 = (v1-v0)*2./(v0+v1+.0001); |
120 |
+ |
if (v1 < 0) |
121 |
+ |
return(-v1); |
122 |
+ |
return(v1); |
123 |
+ |
} |
124 |
+ |
|
125 |
|
/* Interpolate and output isotropic BSDF data */ |
126 |
|
static void |
127 |
|
eval_isotropic(char *funame) |
128 |
|
{ |
129 |
|
const int sqres = 1<<samp_order; |
130 |
< |
FILE *ofp = NULL; |
130 |
> |
const double sqfact = 1./(double)sqres; |
131 |
> |
float *val_last = NULL; |
132 |
> |
float *val_next = NULL; |
133 |
> |
SDValue *sdv_next = NULL; |
134 |
> |
FILE *ofp, *uvfp[2]; |
135 |
> |
int assignD = 0; |
136 |
|
char cmd[128]; |
137 |
|
int ix, ox, oy; |
138 |
|
double iovec[6]; |
139 |
< |
float bsdf; |
140 |
< |
#if DEBUG |
104 |
< |
fprintf(stderr, "Writing isotropic order %d ", samp_order); |
105 |
< |
if (pctcull >= 0) fprintf(stderr, "data with %.1f%% culling\n", pctcull); |
106 |
< |
else fputs("raw data\n", stderr); |
107 |
< |
#endif |
108 |
< |
data_prologue(); /* begin output */ |
139 |
> |
float bsdf, uv[2]; |
140 |
> |
|
141 |
|
if (pctcull >= 0) { |
142 |
< |
sprintf(cmd, "rttree_reduce -h -a -ff -r 3 -t %f -g %d", |
143 |
< |
pctcull, samp_order); |
112 |
< |
fflush(stdout); |
142 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s", |
143 |
> |
recip, pctcull, samp_order, create_component_file(0)); |
144 |
|
ofp = popen(cmd, "w"); |
145 |
|
if (ofp == NULL) { |
146 |
|
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
148 |
|
exit(1); |
149 |
|
} |
150 |
|
SET_FILE_BINARY(ofp); |
151 |
< |
} else |
152 |
< |
fputs("{\n", stdout); |
151 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
152 |
> |
flockfile(ofp); |
153 |
> |
#endif |
154 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
155 |
> |
double uvcull = 100. - (100.-pctcull)*.25; |
156 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s", |
157 |
> |
recip, uvcull, samp_order, create_component_file(1)); |
158 |
> |
uvfp[0] = popen(cmd, "w"); |
159 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s", |
160 |
> |
recip, uvcull, samp_order, create_component_file(2)); |
161 |
> |
uvfp[1] = popen(cmd, "w"); |
162 |
> |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
163 |
> |
fprintf(stderr, "%s: cannot open pipes to uv output\n", |
164 |
> |
progname); |
165 |
> |
exit(1); |
166 |
> |
} |
167 |
> |
SET_FILE_BINARY(uvfp[0]); SET_FILE_BINARY(uvfp[1]); |
168 |
> |
#ifdef getc_unlocked |
169 |
> |
flockfile(uvfp[0]); flockfile(uvfp[1]); |
170 |
> |
#endif |
171 |
> |
} |
172 |
> |
} else { |
173 |
> |
ofp = fopen(create_component_file(0), "w"); |
174 |
> |
if (ofp == NULL) { |
175 |
> |
fprintf(stderr, "%s: cannot create Y output file\n", |
176 |
> |
progname); |
177 |
> |
exit(1); |
178 |
> |
} |
179 |
> |
fputs("{\n", ofp); |
180 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
181 |
> |
uvfp[0] = fopen(create_component_file(1), "w"); |
182 |
> |
uvfp[1] = fopen(create_component_file(2), "w"); |
183 |
> |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
184 |
> |
fprintf(stderr, "%s: cannot create uv output file(s)\n", |
185 |
> |
progname); |
186 |
> |
exit(1); |
187 |
> |
} |
188 |
> |
fputs("{\n", uvfp[0]); |
189 |
> |
fputs("{\n", uvfp[1]); |
190 |
> |
} |
191 |
> |
} |
192 |
> |
if (funame != NULL) /* need to assign Dx, Dy, Dz? */ |
193 |
> |
assignD = (fundefined(funame) < 6); |
194 |
> |
val_last = (float *)calloc(sqres, sizeof(float)); |
195 |
> |
if (funame == NULL) |
196 |
> |
sdv_next = (SDValue *)malloc(sizeof(SDValue)*sqres); |
197 |
> |
else |
198 |
> |
val_next = (float *)malloc(sizeof(float)*sqres); |
199 |
|
/* run through directions */ |
200 |
|
for (ix = 0; ix < sqres/2; ix++) { |
201 |
< |
RBFNODE *rbf = NULL; |
202 |
< |
iovec[0] = (ix+.5)/sqres - 1.; |
203 |
< |
iovec[1] = .0; |
204 |
< |
iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]); |
201 |
> |
const int zipsgn = (ix & 1)*2 - 1; |
202 |
> |
RBFNODE *rbf = NULL; |
203 |
> |
iovec[0] = 2.*sqfact*(ix+.5) - 1.; |
204 |
> |
iovec[1] = zipsgn*sqfact*.5; |
205 |
> |
iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0] |
206 |
> |
- iovec[1]*iovec[1]); |
207 |
|
if (funame == NULL) |
208 |
< |
rbf = advect_rbf(iovec); |
209 |
< |
for (ox = 0; ox < sqres; ox++) |
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); |
212 |
> |
iovec[5] = output_orient * |
213 |
> |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
214 |
> |
if (funame == NULL) { |
215 |
> |
eval_rbfcol(&sdv_next[oy], rbf, iovec+3); |
216 |
> |
} else { |
217 |
> |
if (assignD) { |
218 |
> |
varset("Dx", '=', -iovec[3]); |
219 |
> |
varset("Dy", '=', -iovec[4]); |
220 |
> |
varset("Dz", '=', -iovec[5]); |
221 |
> |
++eclock; |
222 |
> |
} |
223 |
> |
val_next[oy] = funvalue(funame, 6, iovec); |
224 |
> |
} |
225 |
> |
} |
226 |
> |
for (ox = 0; ox < sqres; ox++) { |
227 |
> |
/* |
228 |
> |
* Super-sample when we detect a difference from before |
229 |
> |
* or after in this row, above or below. |
230 |
> |
*/ |
231 |
|
for (oy = 0; oy < sqres; oy++) { |
232 |
< |
SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); |
233 |
< |
iovec[5] = output_orient * |
232 |
> |
if (ox < sqres-1) { /* keeping one row ahead... */ |
233 |
> |
SDsquare2disk(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 |
< |
if (funame == NULL) |
237 |
< |
bsdf = eval_rbfrep(rbf, iovec+3) * |
238 |
< |
output_orient/iovec[5]; |
239 |
< |
else |
240 |
< |
bsdf = funvalue(funame, 6, iovec); |
236 |
> |
} |
237 |
> |
if (funame == NULL) { |
238 |
> |
SDValue sdv = sdv_next[oy]; |
239 |
> |
bsdf = sdv.cieY; |
240 |
> |
if (ox < sqres-1) |
241 |
> |
eval_rbfcol(&sdv_next[oy], rbf, iovec+3); |
242 |
> |
if (abs_diff(bsdf, sdv_next[oy].cieY) > ssamp_thresh || |
243 |
> |
(ox && abs_diff(bsdf, val_last[oy]) > ssamp_thresh) || |
244 |
> |
(oy && abs_diff(bsdf, val_last[oy-1]) > ssamp_thresh) || |
245 |
> |
(oy < sqres-1 && |
246 |
> |
abs_diff(bsdf, sdv_next[oy+1].cieY) > ssamp_thresh)) { |
247 |
> |
int ssi; |
248 |
> |
double ssa[2], sum = 0, usum = 0, vsum = 0; |
249 |
> |
/* super-sample voxel */ |
250 |
> |
for (ssi = nssamp; ssi--; ) { |
251 |
> |
SDmultiSamp(ssa, 2, (ssi+frandom()) / |
252 |
> |
(double)nssamp); |
253 |
> |
SDsquare2disk(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]); |
257 |
> |
eval_rbfcol(&sdv, rbf, iovec+3); |
258 |
> |
sum += sdv.cieY; |
259 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
260 |
> |
sdv.cieY /= |
261 |
> |
-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.; |
262 |
> |
usum += 4.*sdv.spec.cx * sdv.cieY; |
263 |
> |
vsum += 9.*sdv.spec.cy * sdv.cieY; |
264 |
> |
} |
265 |
> |
} |
266 |
> |
bsdf = sum / (double)nssamp; |
267 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
268 |
> |
uv[0] = usum / (sum+FTINY); |
269 |
> |
uv[1] = vsum / (sum+FTINY); |
270 |
> |
} |
271 |
> |
} else |
272 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
273 |
> |
uv[0] = uv[1] = 1. / |
274 |
> |
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
275 |
> |
uv[0] *= 4.*sdv.spec.cx; |
276 |
> |
uv[1] *= 9.*sdv.spec.cy; |
277 |
> |
} |
278 |
> |
} else { |
279 |
> |
bsdf = val_next[oy]; |
280 |
> |
if (ox < sqres-1) { |
281 |
> |
if (assignD) { |
282 |
> |
varset("Dx", '=', -iovec[3]); |
283 |
> |
varset("Dy", '=', -iovec[4]); |
284 |
> |
varset("Dz", '=', -iovec[5]); |
285 |
> |
++eclock; |
286 |
> |
} |
287 |
> |
val_next[oy] = funvalue(funame, 6, iovec); |
288 |
> |
} |
289 |
> |
if (abs_diff(bsdf, val_next[oy]) > ssamp_thresh || |
290 |
> |
(ox && abs_diff(bsdf, val_last[oy]) > ssamp_thresh) || |
291 |
> |
(oy && abs_diff(bsdf, val_last[oy-1]) > ssamp_thresh) || |
292 |
> |
(oy < sqres-1 && |
293 |
> |
abs_diff(bsdf, val_next[oy+1]) > ssamp_thresh)) { |
294 |
> |
int ssi; |
295 |
> |
double ssa[4], ssvec[6], sum = 0; |
296 |
> |
/* super-sample voxel */ |
297 |
> |
for (ssi = nssamp; ssi--; ) { |
298 |
> |
SDmultiSamp(ssa, 4, (ssi+frandom()) / |
299 |
> |
(double)nssamp); |
300 |
> |
ssvec[0] = 2.*sqfact*(ix+ssa[0]) - 1.; |
301 |
> |
ssvec[1] = zipsgn*sqfact*ssa[1]; |
302 |
> |
ssvec[2] = 1. - ssvec[0]*ssvec[0] |
303 |
> |
- ssvec[1]*ssvec[1]; |
304 |
> |
if (ssvec[2] < .0) { |
305 |
> |
ssvec[1] = 0; |
306 |
> |
ssvec[2] = 1. - ssvec[0]*ssvec[0]; |
307 |
> |
} |
308 |
> |
ssvec[2] = input_orient * sqrt(ssvec[2]); |
309 |
> |
SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact, |
310 |
> |
(oy+ssa[3])*sqfact); |
311 |
> |
ssvec[5] = output_orient * |
312 |
> |
sqrt(1. - ssvec[3]*ssvec[3] - |
313 |
> |
ssvec[4]*ssvec[4]); |
314 |
> |
if (assignD) { |
315 |
> |
varset("Dx", '=', -ssvec[3]); |
316 |
> |
varset("Dy", '=', -ssvec[4]); |
317 |
> |
varset("Dz", '=', -ssvec[5]); |
318 |
> |
++eclock; |
319 |
> |
} |
320 |
> |
sum += funvalue(funame, 6, ssvec); |
321 |
> |
} |
322 |
> |
bsdf = sum / (double)nssamp; |
323 |
> |
} |
324 |
> |
} |
325 |
|
if (pctcull >= 0) |
326 |
< |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
326 |
> |
putbinary(&bsdf, sizeof(bsdf), 1, ofp); |
327 |
|
else |
328 |
< |
printf("\t%.3e\n", bsdf); |
328 |
> |
fprintf(ofp, "\t%.3e\n", bsdf); |
329 |
> |
|
330 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
331 |
> |
if (pctcull >= 0) { |
332 |
> |
putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]); |
333 |
> |
putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]); |
334 |
> |
} else { |
335 |
> |
fprintf(uvfp[0], "\t%.3e\n", uv[0]); |
336 |
> |
fprintf(uvfp[1], "\t%.3e\n", uv[1]); |
337 |
> |
} |
338 |
> |
} |
339 |
> |
if (val_last != NULL) |
340 |
> |
val_last[oy] = bsdf; |
341 |
|
} |
342 |
+ |
} |
343 |
|
if (rbf != NULL) |
344 |
|
free(rbf); |
345 |
+ |
prog_show((ix+1.)*(2.*sqfact)); |
346 |
|
} |
347 |
+ |
prog_done(); |
348 |
+ |
if (val_last != NULL) { |
349 |
+ |
free(val_last); |
350 |
+ |
if (val_next != NULL) free(val_next); |
351 |
+ |
if (sdv_next != NULL) free(sdv_next); |
352 |
+ |
} |
353 |
|
if (pctcull >= 0) { /* finish output */ |
354 |
|
if (pclose(ofp)) { |
355 |
< |
fprintf(stderr, "%s: error running '%s'\n", |
356 |
< |
progname, cmd); |
355 |
> |
fprintf(stderr, "%s: error running rttree_reduce on Y\n", |
356 |
> |
progname); |
357 |
|
exit(1); |
358 |
|
} |
359 |
+ |
if (rbf_colorimetry == RBCtristimulus && |
360 |
+ |
(pclose(uvfp[0]) || pclose(uvfp[1]))) { |
361 |
+ |
fprintf(stderr, "%s: error running rttree_reduce on uv\n", |
362 |
+ |
progname); |
363 |
+ |
exit(1); |
364 |
+ |
} |
365 |
|
} else { |
366 |
|
for (ix = sqres*sqres*sqres/2; ix--; ) |
367 |
< |
fputs("\t0\n", stdout); |
368 |
< |
fputs("}\n", stdout); |
367 |
> |
fputs("\t0\n", ofp); |
368 |
> |
fputs("}\n", ofp); |
369 |
> |
if (fclose(ofp)) { |
370 |
> |
fprintf(stderr, "%s: error writing Y file\n", |
371 |
> |
progname); |
372 |
> |
exit(1); |
373 |
> |
} |
374 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
375 |
> |
for (ix = sqres*sqres*sqres/2; ix--; ) { |
376 |
> |
fputs("\t0\n", uvfp[0]); |
377 |
> |
fputs("\t0\n", uvfp[1]); |
378 |
> |
} |
379 |
> |
fputs("}\n", uvfp[0]); |
380 |
> |
fputs("}\n", uvfp[1]); |
381 |
> |
if (fclose(uvfp[0]) || fclose(uvfp[1])) { |
382 |
> |
fprintf(stderr, "%s: error writing uv file(s)\n", |
383 |
> |
progname); |
384 |
> |
exit(1); |
385 |
> |
} |
386 |
> |
} |
387 |
|
} |
159 |
– |
data_epilogue(); |
388 |
|
} |
389 |
|
|
390 |
|
/* Interpolate and output anisotropic BSDF data */ |
392 |
|
eval_anisotropic(char *funame) |
393 |
|
{ |
394 |
|
const int sqres = 1<<samp_order; |
395 |
< |
FILE *ofp = NULL; |
395 |
> |
const double sqfact = 1./(double)sqres; |
396 |
> |
float *val_last = NULL; |
397 |
> |
float *val_next = NULL; |
398 |
> |
SDValue *sdv_next = NULL; |
399 |
> |
FILE *ofp, *uvfp[2]; |
400 |
> |
int assignD = 0; |
401 |
|
char cmd[128]; |
402 |
|
int ix, iy, ox, oy; |
403 |
|
double iovec[6]; |
404 |
< |
float bsdf; |
405 |
< |
#if DEBUG |
173 |
< |
fprintf(stderr, "Writing anisotropic order %d ", samp_order); |
174 |
< |
if (pctcull >= 0) fprintf(stderr, "data with %.1f%% culling\n", pctcull); |
175 |
< |
else fputs("raw data\n", stderr); |
176 |
< |
#endif |
177 |
< |
data_prologue(); /* begin output */ |
404 |
> |
float bsdf, uv[2]; |
405 |
> |
|
406 |
|
if (pctcull >= 0) { |
407 |
< |
sprintf(cmd, "rttree_reduce -h -a -ff -r 4 -t %f -g %d", |
408 |
< |
pctcull, samp_order); |
409 |
< |
fflush(stdout); |
407 |
> |
const char *avgopt = (input_orient>0 ^ output_orient>0) |
408 |
> |
? "" : recip; |
409 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
410 |
> |
avgopt, pctcull, samp_order, |
411 |
> |
create_component_file(0)); |
412 |
|
ofp = popen(cmd, "w"); |
413 |
|
if (ofp == NULL) { |
414 |
|
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
415 |
|
progname); |
416 |
|
exit(1); |
417 |
|
} |
418 |
< |
} else |
419 |
< |
fputs("{\n", stdout); |
418 |
> |
SET_FILE_BINARY(ofp); |
419 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
420 |
> |
flockfile(ofp); |
421 |
> |
#endif |
422 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
423 |
> |
double uvcull = 100. - (100.-pctcull)*.25; |
424 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
425 |
> |
avgopt, uvcull, samp_order, |
426 |
> |
create_component_file(1)); |
427 |
> |
uvfp[0] = popen(cmd, "w"); |
428 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
429 |
> |
avgopt, uvcull, samp_order, |
430 |
> |
create_component_file(2)); |
431 |
> |
uvfp[1] = popen(cmd, "w"); |
432 |
> |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
433 |
> |
fprintf(stderr, "%s: cannot open pipes to uv output\n", |
434 |
> |
progname); |
435 |
> |
exit(1); |
436 |
> |
} |
437 |
> |
SET_FILE_BINARY(uvfp[0]); SET_FILE_BINARY(uvfp[1]); |
438 |
> |
#ifdef getc_unlocked |
439 |
> |
flockfile(uvfp[0]); flockfile(uvfp[1]); |
440 |
> |
#endif |
441 |
> |
} |
442 |
> |
} else { |
443 |
> |
ofp = fopen(create_component_file(0), "w"); |
444 |
> |
if (ofp == NULL) { |
445 |
> |
fprintf(stderr, "%s: cannot create Y output file\n", |
446 |
> |
progname); |
447 |
> |
exit(1); |
448 |
> |
} |
449 |
> |
fputs("{\n", ofp); |
450 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
451 |
> |
uvfp[0] = fopen(create_component_file(1), "w"); |
452 |
> |
uvfp[1] = fopen(create_component_file(2), "w"); |
453 |
> |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
454 |
> |
fprintf(stderr, "%s: cannot create uv output file(s)\n", |
455 |
> |
progname); |
456 |
> |
exit(1); |
457 |
> |
} |
458 |
> |
fputs("{\n", uvfp[0]); |
459 |
> |
fputs("{\n", uvfp[1]); |
460 |
> |
} |
461 |
> |
} |
462 |
> |
if (funame != NULL) /* need to assign Dx, Dy, Dz? */ |
463 |
> |
assignD = (fundefined(funame) < 6); |
464 |
> |
val_last = (float *)calloc(sqres, sizeof(float)); |
465 |
> |
if (funame == NULL) |
466 |
> |
sdv_next = (SDValue *)malloc(sizeof(SDValue)*sqres); |
467 |
> |
else |
468 |
> |
val_next = (float *)malloc(sizeof(float)*sqres); |
469 |
|
/* run through directions */ |
470 |
|
for (ix = 0; ix < sqres; ix++) |
471 |
|
for (iy = 0; iy < sqres; iy++) { |
472 |
|
RBFNODE *rbf = NULL; /* Klems reversal */ |
473 |
< |
SDsquare2disk(iovec, (ix+.5)/sqres, (iy+.5)/sqres); |
195 |
< |
iovec[0] = -iovec[0]; iovec[1] = -iovec[1]; |
473 |
> |
SDsquare2disk(iovec, 1.-(ix+.5)*sqfact, 1.-(iy+.5)*sqfact); |
474 |
|
iovec[2] = input_orient * |
475 |
|
sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]); |
476 |
|
if (funame == NULL) |
477 |
< |
rbf = advect_rbf(iovec); |
478 |
< |
for (ox = 0; ox < sqres; ox++) |
477 |
> |
rbf = advect_rbf(iovec, lobe_lim); |
478 |
> |
/* presample first row */ |
479 |
> |
for (oy = 0; oy < sqres; oy++) { |
480 |
> |
SDsquare2disk(iovec+3, .5*sqfact, (oy+.5)*sqfact); |
481 |
> |
iovec[5] = output_orient * |
482 |
> |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
483 |
> |
if (funame == NULL) { |
484 |
> |
eval_rbfcol(&sdv_next[oy], rbf, iovec+3); |
485 |
> |
} else { |
486 |
> |
if (assignD) { |
487 |
> |
varset("Dx", '=', -iovec[3]); |
488 |
> |
varset("Dy", '=', -iovec[4]); |
489 |
> |
varset("Dz", '=', -iovec[5]); |
490 |
> |
++eclock; |
491 |
> |
} |
492 |
> |
val_next[oy] = funvalue(funame, 6, iovec); |
493 |
> |
} |
494 |
> |
} |
495 |
> |
for (ox = 0; ox < sqres; ox++) { |
496 |
> |
/* |
497 |
> |
* Super-sample when we detect a difference from before |
498 |
> |
* or after in this row, above or below. |
499 |
> |
*/ |
500 |
|
for (oy = 0; oy < sqres; oy++) { |
501 |
< |
SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); |
502 |
< |
iovec[5] = output_orient * |
501 |
> |
if (ox < sqres-1) { /* keeping one row ahead... */ |
502 |
> |
SDsquare2disk(iovec+3, (ox+1.5)*sqfact, (oy+.5)*sqfact); |
503 |
> |
iovec[5] = output_orient * |
504 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
505 |
< |
if (funame == NULL) |
506 |
< |
bsdf = eval_rbfrep(rbf, iovec+3) * |
507 |
< |
output_orient/iovec[5]; |
508 |
< |
else |
509 |
< |
bsdf = funvalue(funame, 6, iovec); |
505 |
> |
} |
506 |
> |
if (funame == NULL) { |
507 |
> |
SDValue sdv = sdv_next[oy]; |
508 |
> |
bsdf = sdv.cieY; |
509 |
> |
if (ox < sqres-1) |
510 |
> |
eval_rbfcol(&sdv_next[oy], rbf, iovec+3); |
511 |
> |
if (abs_diff(bsdf, sdv_next[oy].cieY) > ssamp_thresh || |
512 |
> |
(ox && abs_diff(bsdf, val_last[oy]) > ssamp_thresh) || |
513 |
> |
(oy && abs_diff(bsdf, val_last[oy-1]) > ssamp_thresh) || |
514 |
> |
(oy < sqres-1 && |
515 |
> |
abs_diff(bsdf, sdv_next[oy+1].cieY) > ssamp_thresh)) { |
516 |
> |
int ssi; |
517 |
> |
double ssa[2], sum = 0, usum = 0, vsum = 0; |
518 |
> |
/* super-sample voxel */ |
519 |
> |
for (ssi = nssamp; ssi--; ) { |
520 |
> |
SDmultiSamp(ssa, 2, (ssi+frandom()) / |
521 |
> |
(double)nssamp); |
522 |
> |
SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, |
523 |
> |
(oy+ssa[1])*sqfact); |
524 |
> |
iovec[5] = output_orient * |
525 |
> |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
526 |
> |
eval_rbfcol(&sdv, rbf, iovec+3); |
527 |
> |
sum += sdv.cieY; |
528 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
529 |
> |
sdv.cieY /= |
530 |
> |
-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.; |
531 |
> |
usum += 4.*sdv.spec.cx * sdv.cieY; |
532 |
> |
vsum += 9.*sdv.spec.cy * sdv.cieY; |
533 |
> |
} |
534 |
> |
} |
535 |
> |
bsdf = sum / (double)nssamp; |
536 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
537 |
> |
uv[0] = usum / (sum+FTINY); |
538 |
> |
uv[1] = vsum / (sum+FTINY); |
539 |
> |
} |
540 |
> |
} else |
541 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
542 |
> |
uv[0] = uv[1] = 1. / |
543 |
> |
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
544 |
> |
uv[0] *= 4.*sdv.spec.cx; |
545 |
> |
uv[1] *= 9.*sdv.spec.cy; |
546 |
> |
} |
547 |
> |
} else { |
548 |
> |
bsdf = val_next[oy]; |
549 |
> |
if (ox < sqres-1) { |
550 |
> |
if (assignD) { |
551 |
> |
varset("Dx", '=', -iovec[3]); |
552 |
> |
varset("Dy", '=', -iovec[4]); |
553 |
> |
varset("Dz", '=', -iovec[5]); |
554 |
> |
++eclock; |
555 |
> |
} |
556 |
> |
val_next[oy] = funvalue(funame, 6, iovec); |
557 |
> |
} |
558 |
> |
if (abs_diff(bsdf, val_next[oy]) > ssamp_thresh || |
559 |
> |
(ox && abs_diff(bsdf, val_last[oy]) > ssamp_thresh) || |
560 |
> |
(oy && abs_diff(bsdf, val_last[oy-1]) > ssamp_thresh) || |
561 |
> |
(oy < sqres-1 && |
562 |
> |
abs_diff(bsdf, val_next[oy+1]) > ssamp_thresh)) { |
563 |
> |
int ssi; |
564 |
> |
double ssa[4], ssvec[6], sum = 0; |
565 |
> |
/* super-sample voxel */ |
566 |
> |
for (ssi = nssamp; ssi--; ) { |
567 |
> |
SDmultiSamp(ssa, 4, (ssi+frandom()) / |
568 |
> |
(double)nssamp); |
569 |
> |
SDsquare2disk(ssvec, 1.-(ix+ssa[0])*sqfact, |
570 |
> |
1.-(iy+ssa[1])*sqfact); |
571 |
> |
ssvec[2] = input_orient * |
572 |
> |
sqrt(1. - ssvec[0]*ssvec[0] - |
573 |
> |
ssvec[1]*ssvec[1]); |
574 |
> |
SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact, |
575 |
> |
(oy+ssa[3])*sqfact); |
576 |
> |
ssvec[5] = output_orient * |
577 |
> |
sqrt(1. - ssvec[3]*ssvec[3] - |
578 |
> |
ssvec[4]*ssvec[4]); |
579 |
> |
if (assignD) { |
580 |
> |
varset("Dx", '=', -ssvec[3]); |
581 |
> |
varset("Dy", '=', -ssvec[4]); |
582 |
> |
varset("Dz", '=', -ssvec[5]); |
583 |
> |
++eclock; |
584 |
> |
} |
585 |
> |
sum += funvalue(funame, 6, ssvec); |
586 |
> |
} |
587 |
> |
bsdf = sum / (double)nssamp; |
588 |
> |
} |
589 |
> |
} |
590 |
|
if (pctcull >= 0) |
591 |
< |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
591 |
> |
putbinary(&bsdf, sizeof(bsdf), 1, ofp); |
592 |
|
else |
593 |
< |
printf("\t%.3e\n", bsdf); |
593 |
> |
fprintf(ofp, "\t%.3e\n", bsdf); |
594 |
> |
|
595 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
596 |
> |
if (pctcull >= 0) { |
597 |
> |
putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]); |
598 |
> |
putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]); |
599 |
> |
} else { |
600 |
> |
fprintf(uvfp[0], "\t%.3e\n", uv[0]); |
601 |
> |
fprintf(uvfp[1], "\t%.3e\n", uv[1]); |
602 |
> |
} |
603 |
> |
} |
604 |
> |
if (val_last != NULL) |
605 |
> |
val_last[oy] = bsdf; |
606 |
|
} |
607 |
+ |
} |
608 |
|
if (rbf != NULL) |
609 |
|
free(rbf); |
610 |
+ |
prog_show((ix*sqres+iy+1.)/(sqres*sqres)); |
611 |
|
} |
612 |
+ |
prog_done(); |
613 |
+ |
if (val_last != NULL) { |
614 |
+ |
free(val_last); |
615 |
+ |
if (val_next != NULL) free(val_next); |
616 |
+ |
if (sdv_next != NULL) free(sdv_next); |
617 |
+ |
} |
618 |
|
if (pctcull >= 0) { /* finish output */ |
619 |
|
if (pclose(ofp)) { |
620 |
< |
fprintf(stderr, "%s: error running '%s'\n", |
621 |
< |
progname, cmd); |
620 |
> |
fprintf(stderr, "%s: error running rttree_reduce on Y\n", |
621 |
> |
progname); |
622 |
|
exit(1); |
623 |
|
} |
624 |
< |
} else |
625 |
< |
fputs("}\n", stdout); |
626 |
< |
data_epilogue(); |
624 |
> |
if (rbf_colorimetry == RBCtristimulus && |
625 |
> |
(pclose(uvfp[0]) || pclose(uvfp[1]))) { |
626 |
> |
fprintf(stderr, "%s: error running rttree_reduce on uv\n", |
627 |
> |
progname); |
628 |
> |
exit(1); |
629 |
> |
} |
630 |
> |
} else { |
631 |
> |
fputs("}\n", ofp); |
632 |
> |
if (fclose(ofp)) { |
633 |
> |
fprintf(stderr, "%s: error writing Y file\n", |
634 |
> |
progname); |
635 |
> |
exit(1); |
636 |
> |
} |
637 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
638 |
> |
fputs("}\n", uvfp[0]); |
639 |
> |
fputs("}\n", uvfp[1]); |
640 |
> |
if (fclose(uvfp[0]) || fclose(uvfp[1])) { |
641 |
> |
fprintf(stderr, "%s: error writing uv file(s)\n", |
642 |
> |
progname); |
643 |
> |
exit(1); |
644 |
> |
} |
645 |
> |
} |
646 |
> |
} |
647 |
|
} |
648 |
|
|
649 |
+ |
#if defined(_WIN32) || defined(_WIN64) |
650 |
+ |
/* Execute wrapBSDF command (may never return) */ |
651 |
+ |
static int |
652 |
+ |
wrap_up(void) |
653 |
+ |
{ |
654 |
+ |
char cmd[32700]; |
655 |
+ |
|
656 |
+ |
if (bsdf_manuf[0]) { |
657 |
+ |
add_wbsdf("-f", 1); |
658 |
+ |
strcpy(cmd, "m="); |
659 |
+ |
strcpy(cmd+2, bsdf_manuf); |
660 |
+ |
add_wbsdf(cmd, 0); |
661 |
+ |
} |
662 |
+ |
if (bsdf_name[0]) { |
663 |
+ |
add_wbsdf("-f", 1); |
664 |
+ |
strcpy(cmd, "n="); |
665 |
+ |
strcpy(cmd+2, bsdf_name); |
666 |
+ |
add_wbsdf(cmd, 0); |
667 |
+ |
} |
668 |
+ |
if (!convert_commandline(cmd, sizeof(cmd), wrapBSDF)) { |
669 |
+ |
fputs(progname, stderr); |
670 |
+ |
fputs(": command line too long in wrap_up()\n", stderr); |
671 |
+ |
return(1); |
672 |
+ |
} |
673 |
+ |
return(system(cmd)); |
674 |
+ |
} |
675 |
+ |
#else |
676 |
+ |
/* Execute wrapBSDF command (may never return) */ |
677 |
+ |
static int |
678 |
+ |
wrap_up(void) |
679 |
+ |
{ |
680 |
+ |
char buf[256]; |
681 |
+ |
char *compath = getpath((char *)wrapBSDF[0], getenv("PATH"), X_OK); |
682 |
+ |
|
683 |
+ |
if (compath == NULL) { |
684 |
+ |
fprintf(stderr, "%s: cannot locate %s\n", progname, wrapBSDF[0]); |
685 |
+ |
return(1); |
686 |
+ |
} |
687 |
+ |
if (bsdf_manuf[0]) { |
688 |
+ |
add_wbsdf("-f", 1); |
689 |
+ |
strcpy(buf, "m="); |
690 |
+ |
strcpy(buf+2, bsdf_manuf); |
691 |
+ |
add_wbsdf(buf, 0); |
692 |
+ |
} |
693 |
+ |
if (bsdf_name[0]) { |
694 |
+ |
add_wbsdf("-f", 1); |
695 |
+ |
strcpy(buf, "n="); |
696 |
+ |
strcpy(buf+2, bsdf_name); |
697 |
+ |
add_wbsdf(buf, 0); |
698 |
+ |
} |
699 |
+ |
execv(compath, wrapBSDF); /* successful call never returns */ |
700 |
+ |
perror(compath); |
701 |
+ |
return(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 |
< |
int dofwd = 0, dobwd = 1; |
747 |
< |
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-1 && (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) |
767 |
+ |
single_plane_incident = 0; |
768 |
|
break; |
769 |
|
case 'f': |
770 |
< |
if (!argv[i][2]) |
771 |
< |
fcompile(argv[++i]); |
772 |
< |
else |
770 |
> |
if (!argv[i][2]) { |
771 |
> |
if (strchr(argv[++i], '=') != NULL) { |
772 |
> |
add_wbsdf("-f", 1); |
773 |
> |
add_wbsdf(argv[i], 1); |
774 |
> |
} else { |
775 |
> |
char *fpath = getpath(argv[i], |
776 |
> |
getrlibpath(), 0); |
777 |
> |
if (fpath == NULL) { |
778 |
> |
fprintf(stderr, |
779 |
> |
"%s: cannot find file '%s'\n", |
780 |
> |
argv[0], argv[i]); |
781 |
> |
return(1); |
782 |
> |
} |
783 |
> |
fcompile(fpath); |
784 |
> |
if (single_plane_incident < 0) |
785 |
> |
single_plane_incident = 0; |
786 |
> |
} |
787 |
> |
} else |
788 |
|
dofwd = (argv[i][0] == '+'); |
789 |
|
break; |
790 |
+ |
case 'a': |
791 |
+ |
recip = (argv[i][0] == '+') ? " -a" : ""; |
792 |
+ |
break; |
793 |
|
case 'b': |
794 |
|
dobwd = (argv[i][0] == '+'); |
795 |
|
break; |
796 |
+ |
case 'n': |
797 |
+ |
nssamp = atoi(argv[++i]); |
798 |
+ |
if (nssamp <= 0) |
799 |
+ |
goto userr; |
800 |
+ |
break; |
801 |
+ |
case 's': |
802 |
+ |
ssamp_thresh = atof(argv[++i]); |
803 |
+ |
if (ssamp_thresh <= FTINY) |
804 |
+ |
goto userr; |
805 |
+ |
break; |
806 |
|
case 't': |
807 |
|
switch (argv[i][2]) { |
808 |
|
case '3': |
809 |
< |
single_plane_incident = 0; |
809 |
> |
single_plane_incident = 1; |
810 |
|
break; |
811 |
|
case '4': |
812 |
< |
single_plane_incident = 1; |
812 |
> |
single_plane_incident = 0; |
813 |
|
break; |
814 |
|
case '\0': |
815 |
|
pctcull = atof(argv[++i]); |
821 |
|
case 'g': |
822 |
|
samp_order = atoi(argv[++i]); |
823 |
|
break; |
824 |
+ |
case 'l': |
825 |
+ |
lobe_lim = atoi(argv[++i]); |
826 |
+ |
break; |
827 |
+ |
case 'p': |
828 |
+ |
do_prog = atoi(argv[i]+2); |
829 |
+ |
break; |
830 |
+ |
case 'W': |
831 |
+ |
add_wbsdf(argv[i], 1); |
832 |
+ |
break; |
833 |
+ |
case 'u': |
834 |
+ |
case 'C': |
835 |
+ |
add_wbsdf(argv[i], 1); |
836 |
+ |
add_wbsdf(argv[++i], 1); |
837 |
+ |
break; |
838 |
|
default: |
839 |
|
goto userr; |
840 |
|
} |
841 |
< |
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]) != 6) { |
875 |
> |
eval_isotropic : eval_anisotropic; |
876 |
> |
if (i != argc-1 || fundefined(argv[i]) < 3) { |
877 |
|
fprintf(stderr, |
878 |
|
"%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n", |
879 |
|
progname); |
880 |
+ |
fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n"); |
881 |
|
goto userr; |
882 |
|
} |
883 |
< |
xml_prologue(argc, argv); /* start XML output */ |
883 |
> |
++eclock; |
884 |
> |
add_wbsdf("-a", 1); |
885 |
> |
add_wbsdf(tfmt[single_plane_incident], 1); |
886 |
|
if (dofwd) { |
887 |
|
input_orient = -1; |
888 |
|
output_orient = -1; |
889 |
< |
(*evf)(argv[i]); /* outside reflectance */ |
889 |
> |
prog_start("Evaluating outside reflectance"); |
890 |
> |
(*evf)(argv[i]); |
891 |
|
output_orient = 1; |
892 |
< |
(*evf)(argv[i]); /* outside -> inside */ |
892 |
> |
prog_start("Evaluating outside->inside transmission"); |
893 |
> |
(*evf)(argv[i]); |
894 |
|
} |
895 |
|
if (dobwd) { |
896 |
|
input_orient = 1; |
897 |
|
output_orient = 1; |
898 |
< |
(*evf)(argv[i]); /* inside reflectance */ |
898 |
> |
prog_start("Evaluating inside reflectance"); |
899 |
> |
(*evf)(argv[i]); |
900 |
|
output_orient = -1; |
901 |
< |
(*evf)(argv[i]); /* inside -> outside */ |
901 |
> |
prog_start("Evaluating inside->outside transmission"); |
902 |
> |
(*evf)(argv[i]); |
903 |
|
} |
904 |
< |
xml_epilogue(); /* finish XML output & exit */ |
905 |
< |
return(0); |
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 */ |
304 |
< |
int nbsdf = 0; |
305 |
< |
for ( ; i < argc; i++) { /* interpolate each component */ |
306 |
< |
FILE *fpin = fopen(argv[i], "rb"); |
307 |
< |
if (fpin == NULL) { |
308 |
< |
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
309 |
< |
progname, argv[i]); |
310 |
< |
return(1); |
311 |
< |
} |
312 |
< |
if (!load_bsdf_rep(fpin)) |
313 |
< |
return(1); |
314 |
< |
fclose(fpin); |
315 |
< |
if (!nbsdf++) /* start XML on first dist. */ |
316 |
< |
xml_prologue(argc, argv); |
317 |
< |
if (single_plane_incident) |
318 |
< |
eval_isotropic(NULL); |
319 |
< |
else |
320 |
< |
eval_anisotropic(NULL); |
321 |
< |
} |
322 |
< |
xml_epilogue(); /* finish XML output & exit */ |
323 |
< |
return(0); |
324 |
< |
} |
325 |
< |
SET_FILE_BINARY(stdin); /* load from stdin */ |
326 |
< |
if (!load_bsdf_rep(stdin)) |
327 |
< |
return(1); |
328 |
< |
xml_prologue(argc, argv); /* start XML output */ |
329 |
< |
if (single_plane_incident) /* resample dist. */ |
330 |
< |
eval_isotropic(NULL); |
331 |
< |
else |
332 |
< |
eval_anisotropic(NULL); |
333 |
< |
xml_epilogue(); /* finish XML output & exit */ |
334 |
< |
return(0); |
918 |
> |
return(wrap_up()); /* call wrapBSDF */ |
919 |
|
userr: |
920 |
|
fprintf(stderr, |
921 |
< |
"Usage: %s [-g Nlog2][-t pctcull] [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} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |
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", |
925 |
|
progname); |
926 |
|
return(1); |
927 |
|
} |