1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.45 2020/05/06 01:30:00 greg Exp $"; |
3 |
#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 <stdlib.h> |
12 |
#include <math.h> |
13 |
#include "random.h" |
14 |
#include "platform.h" |
15 |
#include "paths.h" |
16 |
#include "rtio.h" |
17 |
#include "calcomp.h" |
18 |
#include "bsdfrep.h" |
19 |
/* global argv[0] */ |
20 |
char *progname; |
21 |
/* reciprocity averaging option */ |
22 |
static const char *recip = " -a"; |
23 |
/* percentage to cull (<0 to turn off) */ |
24 |
static double pctcull = 90.; |
25 |
/* sampling order */ |
26 |
static int samp_order = 6; |
27 |
/* super-sampling threshold */ |
28 |
const double ssamp_thresh = 0.25; |
29 |
/* number of super-samples */ |
30 |
#ifndef NSSAMP |
31 |
#define NSSAMP 256 |
32 |
#endif |
33 |
/* limit on number of RBF lobes */ |
34 |
static int lobe_lim = 15000; |
35 |
/* progress bar length */ |
36 |
static int do_prog = 79; |
37 |
|
38 |
#define MAXCARG 512 /* wrapBSDF command */ |
39 |
static char *wrapBSDF[MAXCARG] = {"wrapBSDF", "-U"}; |
40 |
static int wbsdfac = 2; |
41 |
|
42 |
/* Add argument to wrapBSDF, allocating space if !isstatic */ |
43 |
static void |
44 |
add_wbsdf(const char *arg, int isstatic) |
45 |
{ |
46 |
if (arg == NULL) |
47 |
return; |
48 |
if (wbsdfac >= MAXCARG-1) { |
49 |
fputs(progname, stderr); |
50 |
fputs(": too many command arguments to wrapBSDF\n", stderr); |
51 |
exit(1); |
52 |
} |
53 |
if (!*arg) |
54 |
arg = ""; |
55 |
else if (!isstatic) |
56 |
arg = savqstr((char *)arg); |
57 |
|
58 |
wrapBSDF[wbsdfac++] = (char *)arg; |
59 |
} |
60 |
|
61 |
/* Create Yuv component file and add appropriate arguments */ |
62 |
static char * |
63 |
create_component_file(int c) |
64 |
{ |
65 |
static const char sname[3][6] = {"CIE-Y", "CIE-u", "CIE-v"}; |
66 |
static const char cname[4][4] = {"-rf", "-tf", "-tb", "-rb"}; |
67 |
char *tfname = mktemp(savqstr(TEMPLATE)); |
68 |
|
69 |
add_wbsdf("-s", 1); add_wbsdf(sname[c], 1); |
70 |
add_wbsdf(cname[(input_orient>0)<<1 | (output_orient>0)], 1); |
71 |
add_wbsdf(tfname, 1); |
72 |
return(tfname); |
73 |
} |
74 |
|
75 |
/* Start new progress bar */ |
76 |
#define prog_start(s) if (do_prog) fprintf(stderr, "%s: %s...\n", progname, s); else |
77 |
|
78 |
/* Draw progress bar of the appropriate length */ |
79 |
static void |
80 |
prog_show(double frac) |
81 |
{ |
82 |
static unsigned call_cnt = 0; |
83 |
static char lastc[] = "-\\|/"; |
84 |
char pbar[256]; |
85 |
int nchars; |
86 |
|
87 |
if (do_prog <= 1) return; |
88 |
if (do_prog > sizeof(pbar)-2) |
89 |
do_prog = sizeof(pbar)-2; |
90 |
if (frac < 0) frac = 0; |
91 |
else if (frac >= 1) frac = .9999; |
92 |
nchars = do_prog*frac; |
93 |
pbar[0] = '\r'; |
94 |
memset(pbar+1, '*', nchars); |
95 |
pbar[nchars+1] = lastc[call_cnt++ & 3]; |
96 |
memset(pbar+2+nchars, '-', do_prog-nchars-1); |
97 |
pbar[do_prog+1] = '\0'; |
98 |
fputs(pbar, stderr); |
99 |
} |
100 |
|
101 |
/* Finish progress bar */ |
102 |
static void |
103 |
prog_done(void) |
104 |
{ |
105 |
int n = do_prog; |
106 |
|
107 |
if (n <= 1) return; |
108 |
fputc('\r', stderr); |
109 |
while (n--) |
110 |
fputc(' ', stderr); |
111 |
fputc('\r', stderr); |
112 |
} |
113 |
|
114 |
/* Compute absolute relative difference */ |
115 |
static double |
116 |
abs_diff(double v1, double v0) |
117 |
{ |
118 |
if ((v0 < 0) | (v1 < 0)) |
119 |
return(.0); |
120 |
v1 = (v1-v0)*2./(v0+v1+.0001); |
121 |
if (v1 < 0) |
122 |
return(-v1); |
123 |
return(v1); |
124 |
} |
125 |
|
126 |
/* Interpolate and output isotropic BSDF data */ |
127 |
static void |
128 |
eval_isotropic(char *funame) |
129 |
{ |
130 |
const int sqres = 1<<samp_order; |
131 |
const double sqfact = 1./(double)sqres; |
132 |
FILE *ofp, *uvfp[2]; |
133 |
int assignD = 0; |
134 |
char cmd[128]; |
135 |
int ix, ox, oy; |
136 |
double iovec[6]; |
137 |
float bsdf, uv[2]; |
138 |
|
139 |
if (pctcull >= 0) { |
140 |
sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s", |
141 |
recip, pctcull, samp_order, create_component_file(0)); |
142 |
ofp = popen(cmd, "w"); |
143 |
if (ofp == NULL) { |
144 |
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
145 |
progname); |
146 |
exit(1); |
147 |
} |
148 |
SET_FILE_BINARY(ofp); |
149 |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
150 |
flockfile(ofp); |
151 |
#endif |
152 |
if (rbf_colorimetry == RBCtristimulus) { |
153 |
double uvcull = 100. - (100.-pctcull)*.25; |
154 |
sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s", |
155 |
recip, uvcull, samp_order, create_component_file(1)); |
156 |
uvfp[0] = popen(cmd, "w"); |
157 |
sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s", |
158 |
recip, uvcull, samp_order, create_component_file(2)); |
159 |
uvfp[1] = popen(cmd, "w"); |
160 |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
161 |
fprintf(stderr, "%s: cannot open pipes to uv output\n", |
162 |
progname); |
163 |
exit(1); |
164 |
} |
165 |
SET_FILE_BINARY(uvfp[0]); SET_FILE_BINARY(uvfp[1]); |
166 |
#ifdef getc_unlocked |
167 |
flockfile(uvfp[0]); flockfile(uvfp[1]); |
168 |
#endif |
169 |
} |
170 |
} else { |
171 |
ofp = fopen(create_component_file(0), "w"); |
172 |
if (ofp == NULL) { |
173 |
fprintf(stderr, "%s: cannot create Y output file\n", |
174 |
progname); |
175 |
exit(1); |
176 |
} |
177 |
fputs("{\n", ofp); |
178 |
if (rbf_colorimetry == RBCtristimulus) { |
179 |
uvfp[0] = fopen(create_component_file(1), "w"); |
180 |
uvfp[1] = fopen(create_component_file(2), "w"); |
181 |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
182 |
fprintf(stderr, "%s: cannot create uv output file(s)\n", |
183 |
progname); |
184 |
exit(1); |
185 |
} |
186 |
fputs("{\n", uvfp[0]); |
187 |
fputs("{\n", uvfp[1]); |
188 |
} |
189 |
} |
190 |
if (funame != NULL) /* need to assign Dx, Dy, Dz? */ |
191 |
assignD = (fundefined(funame) < 6); |
192 |
/* run through directions */ |
193 |
for (ix = 0; ix < sqres/2; ix++) { |
194 |
const int zipsgn = (ix & 1)*2 - 1; |
195 |
RBFNODE *rbf = NULL; |
196 |
iovec[0] = 2.*sqfact*(ix+.5) - 1.; |
197 |
iovec[1] = zipsgn*sqfact*.5; |
198 |
iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0] |
199 |
- iovec[1]*iovec[1]); |
200 |
if (funame == NULL) |
201 |
rbf = advect_rbf(iovec, lobe_lim); |
202 |
for (ox = 0; ox < sqres; ox++) { |
203 |
float last_bsdf = -1; |
204 |
for (oy = 0; oy < sqres; oy++) { |
205 |
SDsquare2disk(iovec+3, (ox+.5)*sqfact, (oy+.5)*sqfact); |
206 |
iovec[5] = output_orient * |
207 |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
208 |
if (funame == NULL) { |
209 |
SDValue sdv; |
210 |
eval_rbfcol(&sdv, rbf, iovec+3); |
211 |
bsdf = sdv.cieY; |
212 |
#if (NSSAMP > 0) |
213 |
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
214 |
int ssi; |
215 |
double ssa[2], sum = 0, usum = 0, vsum = 0; |
216 |
/* super-sample voxel */ |
217 |
for (ssi = NSSAMP; ssi--; ) { |
218 |
SDmultiSamp(ssa, 2, (ssi+frandom()) * |
219 |
(1./NSSAMP)); |
220 |
SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, |
221 |
(oy+ssa[1])*sqfact); |
222 |
iovec[5] = output_orient * |
223 |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
224 |
eval_rbfcol(&sdv, rbf, iovec+3); |
225 |
sum += sdv.cieY; |
226 |
if (rbf_colorimetry == RBCtristimulus) { |
227 |
sdv.cieY /= |
228 |
-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.; |
229 |
usum += 4.*sdv.spec.cx * sdv.cieY; |
230 |
vsum += 9.*sdv.spec.cy * sdv.cieY; |
231 |
} |
232 |
} |
233 |
bsdf = sum * (1./NSSAMP); |
234 |
if (rbf_colorimetry == RBCtristimulus) { |
235 |
uv[0] = usum / (sum+FTINY); |
236 |
uv[1] = vsum / (sum+FTINY); |
237 |
} |
238 |
} else |
239 |
#endif |
240 |
if (rbf_colorimetry == RBCtristimulus) { |
241 |
uv[0] = uv[1] = 1. / |
242 |
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
243 |
uv[0] *= 4.*sdv.spec.cx; |
244 |
uv[1] *= 9.*sdv.spec.cy; |
245 |
} |
246 |
} else { |
247 |
if (assignD) { |
248 |
varset("Dx", '=', -iovec[3]); |
249 |
varset("Dy", '=', -iovec[4]); |
250 |
varset("Dz", '=', -iovec[5]); |
251 |
++eclock; |
252 |
} |
253 |
bsdf = funvalue(funame, 6, iovec); |
254 |
#if (NSSAMP > 0) |
255 |
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
256 |
int ssi; |
257 |
double ssa[4], ssvec[6], sum = 0; |
258 |
/* super-sample voxel */ |
259 |
for (ssi = NSSAMP; ssi--; ) { |
260 |
SDmultiSamp(ssa, 4, (ssi+frandom()) * |
261 |
(1./NSSAMP)); |
262 |
ssvec[0] = 2.*sqfact*(ix+ssa[0]) - 1.; |
263 |
ssvec[1] = zipsgn*sqfact*ssa[1]; |
264 |
ssvec[2] = 1. - ssvec[0]*ssvec[0] |
265 |
- ssvec[1]*ssvec[1]; |
266 |
if (ssvec[2] < .0) { |
267 |
ssvec[1] = 0; |
268 |
ssvec[2] = 1. - ssvec[0]*ssvec[0]; |
269 |
} |
270 |
ssvec[2] = input_orient * sqrt(ssvec[2]); |
271 |
SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact, |
272 |
(oy+ssa[3])*sqfact); |
273 |
ssvec[5] = output_orient * |
274 |
sqrt(1. - ssvec[3]*ssvec[3] - |
275 |
ssvec[4]*ssvec[4]); |
276 |
if (assignD) { |
277 |
varset("Dx", '=', -ssvec[3]); |
278 |
varset("Dy", '=', -ssvec[4]); |
279 |
varset("Dz", '=', -ssvec[5]); |
280 |
++eclock; |
281 |
} |
282 |
sum += funvalue(funame, 6, ssvec); |
283 |
} |
284 |
bsdf = sum * (1./NSSAMP); |
285 |
} |
286 |
#endif |
287 |
} |
288 |
if (pctcull >= 0) |
289 |
putbinary(&bsdf, sizeof(bsdf), 1, ofp); |
290 |
else |
291 |
fprintf(ofp, "\t%.3e\n", bsdf); |
292 |
|
293 |
if (rbf_colorimetry == RBCtristimulus) { |
294 |
if (pctcull >= 0) { |
295 |
putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]); |
296 |
putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]); |
297 |
} else { |
298 |
fprintf(uvfp[0], "\t%.3e\n", uv[0]); |
299 |
fprintf(uvfp[1], "\t%.3e\n", uv[1]); |
300 |
} |
301 |
} |
302 |
last_bsdf = bsdf; |
303 |
} |
304 |
} |
305 |
if (rbf != NULL) |
306 |
free(rbf); |
307 |
prog_show((ix+1.)*(2.*sqfact)); |
308 |
} |
309 |
prog_done(); |
310 |
if (pctcull >= 0) { /* finish output */ |
311 |
if (pclose(ofp)) { |
312 |
fprintf(stderr, "%s: error running rttree_reduce on Y\n", |
313 |
progname); |
314 |
exit(1); |
315 |
} |
316 |
if (rbf_colorimetry == RBCtristimulus && |
317 |
(pclose(uvfp[0]) || pclose(uvfp[1]))) { |
318 |
fprintf(stderr, "%s: error running rttree_reduce on uv\n", |
319 |
progname); |
320 |
exit(1); |
321 |
} |
322 |
} else { |
323 |
for (ix = sqres*sqres*sqres/2; ix--; ) |
324 |
fputs("\t0\n", ofp); |
325 |
fputs("}\n", ofp); |
326 |
if (fclose(ofp)) { |
327 |
fprintf(stderr, "%s: error writing Y file\n", |
328 |
progname); |
329 |
exit(1); |
330 |
} |
331 |
if (rbf_colorimetry == RBCtristimulus) { |
332 |
for (ix = sqres*sqres*sqres/2; ix--; ) { |
333 |
fputs("\t0\n", uvfp[0]); |
334 |
fputs("\t0\n", uvfp[1]); |
335 |
} |
336 |
fputs("}\n", uvfp[0]); |
337 |
fputs("}\n", uvfp[1]); |
338 |
if (fclose(uvfp[0]) || fclose(uvfp[1])) { |
339 |
fprintf(stderr, "%s: error writing uv file(s)\n", |
340 |
progname); |
341 |
exit(1); |
342 |
} |
343 |
} |
344 |
} |
345 |
} |
346 |
|
347 |
/* Interpolate and output anisotropic BSDF data */ |
348 |
static void |
349 |
eval_anisotropic(char *funame) |
350 |
{ |
351 |
const int sqres = 1<<samp_order; |
352 |
const double sqfact = 1./(double)sqres; |
353 |
FILE *ofp, *uvfp[2]; |
354 |
int assignD = 0; |
355 |
char cmd[128]; |
356 |
int ix, iy, ox, oy; |
357 |
double iovec[6]; |
358 |
float bsdf, uv[2]; |
359 |
|
360 |
if (pctcull >= 0) { |
361 |
const char *avgopt = (input_orient>0 ^ output_orient>0) |
362 |
? "" : recip; |
363 |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
364 |
avgopt, pctcull, samp_order, |
365 |
create_component_file(0)); |
366 |
ofp = popen(cmd, "w"); |
367 |
if (ofp == NULL) { |
368 |
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
369 |
progname); |
370 |
exit(1); |
371 |
} |
372 |
SET_FILE_BINARY(ofp); |
373 |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
374 |
flockfile(ofp); |
375 |
#endif |
376 |
if (rbf_colorimetry == RBCtristimulus) { |
377 |
double uvcull = 100. - (100.-pctcull)*.25; |
378 |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
379 |
avgopt, uvcull, samp_order, |
380 |
create_component_file(1)); |
381 |
uvfp[0] = popen(cmd, "w"); |
382 |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
383 |
avgopt, uvcull, samp_order, |
384 |
create_component_file(2)); |
385 |
uvfp[1] = popen(cmd, "w"); |
386 |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
387 |
fprintf(stderr, "%s: cannot open pipes to uv output\n", |
388 |
progname); |
389 |
exit(1); |
390 |
} |
391 |
SET_FILE_BINARY(uvfp[0]); SET_FILE_BINARY(uvfp[1]); |
392 |
#ifdef getc_unlocked |
393 |
flockfile(uvfp[0]); flockfile(uvfp[1]); |
394 |
#endif |
395 |
} |
396 |
} else { |
397 |
ofp = fopen(create_component_file(0), "w"); |
398 |
if (ofp == NULL) { |
399 |
fprintf(stderr, "%s: cannot create Y output file\n", |
400 |
progname); |
401 |
exit(1); |
402 |
} |
403 |
fputs("{\n", ofp); |
404 |
if (rbf_colorimetry == RBCtristimulus) { |
405 |
uvfp[0] = fopen(create_component_file(1), "w"); |
406 |
uvfp[1] = fopen(create_component_file(2), "w"); |
407 |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
408 |
fprintf(stderr, "%s: cannot create uv output file(s)\n", |
409 |
progname); |
410 |
exit(1); |
411 |
} |
412 |
fputs("{\n", uvfp[0]); |
413 |
fputs("{\n", uvfp[1]); |
414 |
} |
415 |
} |
416 |
if (funame != NULL) /* need to assign Dx, Dy, Dz? */ |
417 |
assignD = (fundefined(funame) < 6); |
418 |
/* run through directions */ |
419 |
for (ix = 0; ix < sqres; ix++) |
420 |
for (iy = 0; iy < sqres; iy++) { |
421 |
RBFNODE *rbf = NULL; /* Klems reversal */ |
422 |
SDsquare2disk(iovec, 1.-(ix+.5)*sqfact, 1.-(iy+.5)*sqfact); |
423 |
iovec[2] = input_orient * |
424 |
sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]); |
425 |
if (funame == NULL) |
426 |
rbf = advect_rbf(iovec, lobe_lim); |
427 |
for (ox = 0; ox < sqres; ox++) { |
428 |
float last_bsdf = -1; |
429 |
for (oy = 0; oy < sqres; oy++) { |
430 |
SDsquare2disk(iovec+3, (ox+.5)*sqfact, (oy+.5)*sqfact); |
431 |
iovec[5] = output_orient * |
432 |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
433 |
if (funame == NULL) { |
434 |
SDValue sdv; |
435 |
eval_rbfcol(&sdv, rbf, iovec+3); |
436 |
bsdf = sdv.cieY; |
437 |
#if (NSSAMP > 0) |
438 |
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
439 |
int ssi; |
440 |
double ssa[2], sum = 0, usum = 0, vsum = 0; |
441 |
/* super-sample voxel */ |
442 |
for (ssi = NSSAMP; ssi--; ) { |
443 |
SDmultiSamp(ssa, 2, (ssi+frandom()) * |
444 |
(1./NSSAMP)); |
445 |
SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, |
446 |
(oy+ssa[1])*sqfact); |
447 |
iovec[5] = output_orient * |
448 |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
449 |
eval_rbfcol(&sdv, rbf, iovec+3); |
450 |
sum += sdv.cieY; |
451 |
if (rbf_colorimetry == RBCtristimulus) { |
452 |
sdv.cieY /= |
453 |
-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.; |
454 |
usum += 4.*sdv.spec.cx * sdv.cieY; |
455 |
vsum += 9.*sdv.spec.cy * sdv.cieY; |
456 |
} |
457 |
} |
458 |
bsdf = sum * (1./NSSAMP); |
459 |
if (rbf_colorimetry == RBCtristimulus) { |
460 |
uv[0] = usum / (sum+FTINY); |
461 |
uv[1] = vsum / (sum+FTINY); |
462 |
} |
463 |
} else |
464 |
#endif |
465 |
if (rbf_colorimetry == RBCtristimulus) { |
466 |
uv[0] = uv[1] = 1. / |
467 |
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
468 |
uv[0] *= 4.*sdv.spec.cx; |
469 |
uv[1] *= 9.*sdv.spec.cy; |
470 |
} |
471 |
} else { |
472 |
if (assignD) { |
473 |
varset("Dx", '=', -iovec[3]); |
474 |
varset("Dy", '=', -iovec[4]); |
475 |
varset("Dz", '=', -iovec[5]); |
476 |
++eclock; |
477 |
} |
478 |
bsdf = funvalue(funame, 6, iovec); |
479 |
#if (NSSAMP > 0) |
480 |
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
481 |
int ssi; |
482 |
double ssa[4], ssvec[6], sum = 0; |
483 |
/* super-sample voxel */ |
484 |
for (ssi = NSSAMP; ssi--; ) { |
485 |
SDmultiSamp(ssa, 4, (ssi+frandom()) * |
486 |
(1./NSSAMP)); |
487 |
SDsquare2disk(ssvec, 1.-(ix+ssa[0])*sqfact, |
488 |
1.-(iy+ssa[1])*sqfact); |
489 |
ssvec[2] = input_orient * |
490 |
sqrt(1. - ssvec[0]*ssvec[0] - |
491 |
ssvec[1]*ssvec[1]); |
492 |
SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact, |
493 |
(oy+ssa[3])*sqfact); |
494 |
ssvec[5] = output_orient * |
495 |
sqrt(1. - ssvec[3]*ssvec[3] - |
496 |
ssvec[4]*ssvec[4]); |
497 |
if (assignD) { |
498 |
varset("Dx", '=', -ssvec[3]); |
499 |
varset("Dy", '=', -ssvec[4]); |
500 |
varset("Dz", '=', -ssvec[5]); |
501 |
++eclock; |
502 |
} |
503 |
sum += funvalue(funame, 6, ssvec); |
504 |
} |
505 |
bsdf = sum * (1./NSSAMP); |
506 |
} |
507 |
#endif |
508 |
} |
509 |
if (pctcull >= 0) |
510 |
putbinary(&bsdf, sizeof(bsdf), 1, ofp); |
511 |
else |
512 |
fprintf(ofp, "\t%.3e\n", bsdf); |
513 |
|
514 |
if (rbf_colorimetry == RBCtristimulus) { |
515 |
if (pctcull >= 0) { |
516 |
putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]); |
517 |
putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]); |
518 |
} else { |
519 |
fprintf(uvfp[0], "\t%.3e\n", uv[0]); |
520 |
fprintf(uvfp[1], "\t%.3e\n", uv[1]); |
521 |
} |
522 |
} |
523 |
last_bsdf = bsdf; |
524 |
} |
525 |
} |
526 |
if (rbf != NULL) |
527 |
free(rbf); |
528 |
prog_show((ix*sqres+iy+1.)/(sqres*sqres)); |
529 |
} |
530 |
prog_done(); |
531 |
if (pctcull >= 0) { /* finish output */ |
532 |
if (pclose(ofp)) { |
533 |
fprintf(stderr, "%s: error running rttree_reduce on Y\n", |
534 |
progname); |
535 |
exit(1); |
536 |
} |
537 |
if (rbf_colorimetry == RBCtristimulus && |
538 |
(pclose(uvfp[0]) || pclose(uvfp[1]))) { |
539 |
fprintf(stderr, "%s: error running rttree_reduce on uv\n", |
540 |
progname); |
541 |
exit(1); |
542 |
} |
543 |
} else { |
544 |
fputs("}\n", ofp); |
545 |
if (fclose(ofp)) { |
546 |
fprintf(stderr, "%s: error writing Y file\n", |
547 |
progname); |
548 |
exit(1); |
549 |
} |
550 |
if (rbf_colorimetry == RBCtristimulus) { |
551 |
fputs("}\n", uvfp[0]); |
552 |
fputs("}\n", uvfp[1]); |
553 |
if (fclose(uvfp[0]) || fclose(uvfp[1])) { |
554 |
fprintf(stderr, "%s: error writing uv file(s)\n", |
555 |
progname); |
556 |
exit(1); |
557 |
} |
558 |
} |
559 |
} |
560 |
} |
561 |
|
562 |
#if defined(_WIN32) || defined(_WIN64) |
563 |
/* Execute wrapBSDF command (may never return) */ |
564 |
static int |
565 |
wrap_up(void) |
566 |
{ |
567 |
char cmd[8192]; |
568 |
|
569 |
if (bsdf_manuf[0]) { |
570 |
add_wbsdf("-f", 1); |
571 |
strcpy(cmd, "m="); |
572 |
strcpy(cmd+2, bsdf_manuf); |
573 |
add_wbsdf(cmd, 0); |
574 |
} |
575 |
if (bsdf_name[0]) { |
576 |
add_wbsdf("-f", 1); |
577 |
strcpy(cmd, "n="); |
578 |
strcpy(cmd+2, bsdf_name); |
579 |
add_wbsdf(cmd, 0); |
580 |
} |
581 |
if (!convert_commandline(cmd, sizeof(cmd), wrapBSDF)) { |
582 |
fputs(progname, stderr); |
583 |
fputs(": command line too long in wrap_up()\n", stderr); |
584 |
return(1); |
585 |
} |
586 |
return(system(cmd)); |
587 |
} |
588 |
#else |
589 |
/* Execute wrapBSDF command (may never return) */ |
590 |
static int |
591 |
wrap_up(void) |
592 |
{ |
593 |
char buf[256]; |
594 |
char *compath = getpath((char *)wrapBSDF[0], getenv("PATH"), X_OK); |
595 |
|
596 |
if (compath == NULL) { |
597 |
fprintf(stderr, "%s: cannot locate %s\n", progname, wrapBSDF[0]); |
598 |
return(1); |
599 |
} |
600 |
if (bsdf_manuf[0]) { |
601 |
add_wbsdf("-f", 1); |
602 |
strcpy(buf, "m="); |
603 |
strcpy(buf+2, bsdf_manuf); |
604 |
add_wbsdf(buf, 0); |
605 |
} |
606 |
if (bsdf_name[0]) { |
607 |
add_wbsdf("-f", 1); |
608 |
strcpy(buf, "n="); |
609 |
strcpy(buf+2, bsdf_name); |
610 |
add_wbsdf(buf, 0); |
611 |
} |
612 |
execv(compath, wrapBSDF); /* successful call never returns */ |
613 |
perror(compath); |
614 |
return(1); |
615 |
} |
616 |
#endif |
617 |
|
618 |
/* Read in BSDF and interpolate as tensor tree representation */ |
619 |
int |
620 |
main(int argc, char *argv[]) |
621 |
{ |
622 |
static char tfmt[2][4] = {"t4", "t3"}; |
623 |
int dofwd = 0, dobwd = 1; |
624 |
char buf[2048]; |
625 |
int i, na; |
626 |
|
627 |
progname = argv[0]; |
628 |
esupport |= E_VARIABLE|E_FUNCTION|E_RCONST; |
629 |
esupport &= ~(E_INCHAN|E_OUTCHAN); |
630 |
scompile("PI:3.14159265358979323846", NULL, 0); |
631 |
biggerlib(); |
632 |
for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
633 |
switch (argv[i][1]) { /* get options */ |
634 |
case 'e': |
635 |
scompile(argv[++i], NULL, 0); |
636 |
if (single_plane_incident < 0) |
637 |
single_plane_incident = 0; |
638 |
break; |
639 |
case 'f': |
640 |
if (!argv[i][2]) { |
641 |
if (strchr(argv[++i], '=') != NULL) { |
642 |
add_wbsdf("-f", 1); |
643 |
add_wbsdf(argv[i], 1); |
644 |
} else { |
645 |
char *fpath = getpath(argv[i], |
646 |
getrlibpath(), 0); |
647 |
if (fpath == NULL) { |
648 |
fprintf(stderr, |
649 |
"%s: cannot find file '%s'\n", |
650 |
argv[0], argv[i]); |
651 |
return(1); |
652 |
} |
653 |
fcompile(fpath); |
654 |
if (single_plane_incident < 0) |
655 |
single_plane_incident = 0; |
656 |
} |
657 |
} else |
658 |
dofwd = (argv[i][0] == '+'); |
659 |
break; |
660 |
case 'a': |
661 |
recip = (argv[i][0] == '+') ? " -a" : ""; |
662 |
break; |
663 |
case 'b': |
664 |
dobwd = (argv[i][0] == '+'); |
665 |
break; |
666 |
case 't': |
667 |
switch (argv[i][2]) { |
668 |
case '3': |
669 |
single_plane_incident = 1; |
670 |
break; |
671 |
case '4': |
672 |
single_plane_incident = 0; |
673 |
break; |
674 |
case '\0': |
675 |
pctcull = atof(argv[++i]); |
676 |
break; |
677 |
default: |
678 |
goto userr; |
679 |
} |
680 |
break; |
681 |
case 'g': |
682 |
samp_order = atoi(argv[++i]); |
683 |
break; |
684 |
case 'l': |
685 |
lobe_lim = atoi(argv[++i]); |
686 |
break; |
687 |
case 'p': |
688 |
do_prog = atoi(argv[i]+2); |
689 |
break; |
690 |
case 'W': |
691 |
add_wbsdf(argv[i], 1); |
692 |
break; |
693 |
case 'u': |
694 |
case 'C': |
695 |
add_wbsdf(argv[i], 1); |
696 |
add_wbsdf(argv[++i], 1); |
697 |
break; |
698 |
default: |
699 |
goto userr; |
700 |
} |
701 |
strcpy(buf, "File produced by: "); |
702 |
if (convert_commandline(buf+18, sizeof(buf)-18, argv) != NULL) { |
703 |
add_wbsdf("-C", 1); add_wbsdf(buf, 0); |
704 |
} |
705 |
if (single_plane_incident >= 0) { /* function-based BSDF? */ |
706 |
void (*evf)(char *s) = single_plane_incident ? |
707 |
eval_isotropic : eval_anisotropic; |
708 |
if (i != argc-1 || fundefined(argv[i]) < 3) { |
709 |
fprintf(stderr, |
710 |
"%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n", |
711 |
progname); |
712 |
fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n"); |
713 |
goto userr; |
714 |
} |
715 |
++eclock; |
716 |
add_wbsdf("-a", 1); |
717 |
add_wbsdf(tfmt[single_plane_incident], 1); |
718 |
if (dofwd) { |
719 |
input_orient = -1; |
720 |
output_orient = -1; |
721 |
prog_start("Evaluating outside reflectance"); |
722 |
(*evf)(argv[i]); |
723 |
output_orient = 1; |
724 |
prog_start("Evaluating outside->inside transmission"); |
725 |
(*evf)(argv[i]); |
726 |
} |
727 |
if (dobwd) { |
728 |
input_orient = 1; |
729 |
output_orient = 1; |
730 |
prog_start("Evaluating inside reflectance"); |
731 |
(*evf)(argv[i]); |
732 |
output_orient = -1; |
733 |
prog_start("Evaluating inside->outside transmission"); |
734 |
(*evf)(argv[i]); |
735 |
} |
736 |
return(wrap_up()); |
737 |
} |
738 |
if (i < argc) { /* open input files if given */ |
739 |
int nbsdf = 0; |
740 |
for ( ; i < argc; i++) { /* interpolate each component */ |
741 |
char pbuf[256]; |
742 |
FILE *fpin = fopen(argv[i], "rb"); |
743 |
if (fpin == NULL) { |
744 |
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
745 |
progname, argv[i]); |
746 |
return(1); |
747 |
} |
748 |
if (!load_bsdf_rep(fpin)) |
749 |
return(1); |
750 |
fclose(fpin); |
751 |
sprintf(pbuf, "Interpolating component '%s'", argv[i]); |
752 |
prog_start(pbuf); |
753 |
if (!nbsdf++) { |
754 |
add_wbsdf("-a", 1); |
755 |
add_wbsdf(tfmt[single_plane_incident], 1); |
756 |
} |
757 |
if (single_plane_incident) |
758 |
eval_isotropic(NULL); |
759 |
else |
760 |
eval_anisotropic(NULL); |
761 |
} |
762 |
return(wrap_up()); |
763 |
} |
764 |
SET_FILE_BINARY(stdin); /* load from stdin */ |
765 |
if (!load_bsdf_rep(stdin)) |
766 |
return(1); |
767 |
prog_start("Interpolating from standard input"); |
768 |
add_wbsdf("-a", 1); |
769 |
add_wbsdf(tfmt[single_plane_incident], 1); |
770 |
if (single_plane_incident) /* resample dist. */ |
771 |
eval_isotropic(NULL); |
772 |
else |
773 |
eval_anisotropic(NULL); |
774 |
|
775 |
return(wrap_up()); |
776 |
userr: |
777 |
fprintf(stderr, |
778 |
"Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
779 |
progname); |
780 |
fprintf(stderr, |
781 |
" or: %s -t{3|4} [{+|-}a][-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |
782 |
progname); |
783 |
return(1); |
784 |
} |