1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.46 2020/05/06 02:28:21 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.30; |
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 |
float *ryval = NULL; |
133 |
char *rtrip = 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, uv[2]; |
140 |
|
141 |
if (pctcull >= 0) { |
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", |
147 |
progname); |
148 |
exit(1); |
149 |
} |
150 |
SET_FILE_BINARY(ofp); |
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 |
#if (NSSAMP > 0) |
195 |
rtrip = (char *)calloc(sqres, 1); /* track sample triggerings */ |
196 |
ryval = (float *)calloc(sqres, sizeof(float)); |
197 |
#endif |
198 |
/* run through directions */ |
199 |
for (ix = 0; ix < sqres/2; ix++) { |
200 |
const int zipsgn = (ix & 1)*2 - 1; |
201 |
RBFNODE *rbf = NULL; |
202 |
iovec[0] = 2.*sqfact*(ix+.5) - 1.; |
203 |
iovec[1] = zipsgn*sqfact*.5; |
204 |
iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0] |
205 |
- iovec[1]*iovec[1]); |
206 |
if (funame == NULL) |
207 |
rbf = advect_rbf(iovec, lobe_lim); |
208 |
for (ox = 0; ox < sqres; ox++) { |
209 |
SDValue sdv_next; |
210 |
SDsquare2disk(iovec+3, (ox+.5)*sqfact, .5*sqfact); |
211 |
iovec[5] = output_orient * |
212 |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
213 |
if (funame == NULL) { |
214 |
eval_rbfcol(&sdv_next, rbf, iovec+3); |
215 |
} else { |
216 |
sdv_next.spec = c_dfcolor; |
217 |
if (assignD) { |
218 |
varset("Dx", '=', -iovec[3]); |
219 |
varset("Dy", '=', -iovec[4]); |
220 |
varset("Dz", '=', -iovec[5]); |
221 |
++eclock; |
222 |
} |
223 |
sdv_next.cieY = funvalue(funame, 6, iovec); |
224 |
} |
225 |
for (oy = 0; oy < sqres; oy++) { |
226 |
int trip; |
227 |
bsdf = sdv_next.cieY; /* keeping one step ahead... */ |
228 |
if (oy < sqres-1) { |
229 |
SDsquare2disk(iovec+3, (ox+.5)*sqfact, (oy+1.5)*sqfact); |
230 |
iovec[5] = output_orient * |
231 |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
232 |
} |
233 |
if (funame == NULL) { |
234 |
SDValue sdv = sdv_next; |
235 |
if (oy < sqres-1) |
236 |
eval_rbfcol(&sdv_next, rbf, iovec+3); |
237 |
#if (NSSAMP > 0) |
238 |
trip = (abs_diff(bsdf, sdv_next.cieY) > ssamp_thresh || |
239 |
(ox && abs_diff(bsdf, ryval[oy]) > ssamp_thresh) || |
240 |
(oy && abs_diff(bsdf, ryval[oy-1]) > ssamp_thresh)); |
241 |
if (trip | rtrip[oy] || (oy && rtrip[oy-1])) { |
242 |
int ssi; |
243 |
double ssa[2], sum = 0, usum = 0, vsum = 0; |
244 |
/* super-sample voxel */ |
245 |
for (ssi = NSSAMP; ssi--; ) { |
246 |
SDmultiSamp(ssa, 2, (ssi+frandom()) * |
247 |
(1./NSSAMP)); |
248 |
SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, |
249 |
(oy+ssa[1])*sqfact); |
250 |
iovec[5] = output_orient * |
251 |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
252 |
eval_rbfcol(&sdv, rbf, iovec+3); |
253 |
sum += sdv.cieY; |
254 |
if (rbf_colorimetry == RBCtristimulus) { |
255 |
sdv.cieY /= |
256 |
-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.; |
257 |
usum += 4.*sdv.spec.cx * sdv.cieY; |
258 |
vsum += 9.*sdv.spec.cy * sdv.cieY; |
259 |
} |
260 |
} |
261 |
bsdf = sum * (1./NSSAMP); |
262 |
if (rbf_colorimetry == RBCtristimulus) { |
263 |
uv[0] = usum / (sum+FTINY); |
264 |
uv[1] = vsum / (sum+FTINY); |
265 |
} |
266 |
} else |
267 |
#endif |
268 |
if (rbf_colorimetry == RBCtristimulus) { |
269 |
uv[0] = uv[1] = 1. / |
270 |
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
271 |
uv[0] *= 4.*sdv.spec.cx; |
272 |
uv[1] *= 9.*sdv.spec.cy; |
273 |
} |
274 |
} else { |
275 |
if (oy < sqres-1) { |
276 |
if (assignD) { |
277 |
varset("Dx", '=', -iovec[3]); |
278 |
varset("Dy", '=', -iovec[4]); |
279 |
varset("Dz", '=', -iovec[5]); |
280 |
++eclock; |
281 |
} |
282 |
sdv_next.cieY = funvalue(funame, 6, iovec); |
283 |
} |
284 |
#if (NSSAMP > 0) |
285 |
trip = (abs_diff(bsdf, sdv_next.cieY) > ssamp_thresh || |
286 |
(ox && abs_diff(bsdf, ryval[oy]) > ssamp_thresh) || |
287 |
(oy && abs_diff(bsdf, ryval[oy-1]) > ssamp_thresh)); |
288 |
if (trip | rtrip[oy] || (oy && rtrip[oy-1])) { |
289 |
int ssi; |
290 |
double ssa[4], ssvec[6], sum = 0; |
291 |
/* super-sample voxel */ |
292 |
for (ssi = NSSAMP; ssi--; ) { |
293 |
SDmultiSamp(ssa, 4, (ssi+frandom()) * |
294 |
(1./NSSAMP)); |
295 |
ssvec[0] = 2.*sqfact*(ix+ssa[0]) - 1.; |
296 |
ssvec[1] = zipsgn*sqfact*ssa[1]; |
297 |
ssvec[2] = 1. - ssvec[0]*ssvec[0] |
298 |
- ssvec[1]*ssvec[1]; |
299 |
if (ssvec[2] < .0) { |
300 |
ssvec[1] = 0; |
301 |
ssvec[2] = 1. - ssvec[0]*ssvec[0]; |
302 |
} |
303 |
ssvec[2] = input_orient * sqrt(ssvec[2]); |
304 |
SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact, |
305 |
(oy+ssa[3])*sqfact); |
306 |
ssvec[5] = output_orient * |
307 |
sqrt(1. - ssvec[3]*ssvec[3] - |
308 |
ssvec[4]*ssvec[4]); |
309 |
if (assignD) { |
310 |
varset("Dx", '=', -ssvec[3]); |
311 |
varset("Dy", '=', -ssvec[4]); |
312 |
varset("Dz", '=', -ssvec[5]); |
313 |
++eclock; |
314 |
} |
315 |
sum += funvalue(funame, 6, ssvec); |
316 |
} |
317 |
bsdf = sum * (1./NSSAMP); |
318 |
} |
319 |
#endif |
320 |
} |
321 |
if (pctcull >= 0) |
322 |
putbinary(&bsdf, sizeof(bsdf), 1, ofp); |
323 |
else |
324 |
fprintf(ofp, "\t%.3e\n", bsdf); |
325 |
|
326 |
if (rbf_colorimetry == RBCtristimulus) { |
327 |
if (pctcull >= 0) { |
328 |
putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]); |
329 |
putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]); |
330 |
} else { |
331 |
fprintf(uvfp[0], "\t%.3e\n", uv[0]); |
332 |
fprintf(uvfp[1], "\t%.3e\n", uv[1]); |
333 |
} |
334 |
} |
335 |
if (ryval != NULL) { |
336 |
rtrip[oy] = trip; |
337 |
ryval[oy] = bsdf; |
338 |
} |
339 |
} |
340 |
} |
341 |
if (rbf != NULL) |
342 |
free(rbf); |
343 |
prog_show((ix+1.)*(2.*sqfact)); |
344 |
} |
345 |
prog_done(); |
346 |
if (ryval != NULL) { |
347 |
free(rtrip); |
348 |
free(ryval); |
349 |
} |
350 |
if (pctcull >= 0) { /* finish output */ |
351 |
if (pclose(ofp)) { |
352 |
fprintf(stderr, "%s: error running rttree_reduce on Y\n", |
353 |
progname); |
354 |
exit(1); |
355 |
} |
356 |
if (rbf_colorimetry == RBCtristimulus && |
357 |
(pclose(uvfp[0]) || pclose(uvfp[1]))) { |
358 |
fprintf(stderr, "%s: error running rttree_reduce on uv\n", |
359 |
progname); |
360 |
exit(1); |
361 |
} |
362 |
} else { |
363 |
for (ix = sqres*sqres*sqres/2; ix--; ) |
364 |
fputs("\t0\n", ofp); |
365 |
fputs("}\n", ofp); |
366 |
if (fclose(ofp)) { |
367 |
fprintf(stderr, "%s: error writing Y file\n", |
368 |
progname); |
369 |
exit(1); |
370 |
} |
371 |
if (rbf_colorimetry == RBCtristimulus) { |
372 |
for (ix = sqres*sqres*sqres/2; ix--; ) { |
373 |
fputs("\t0\n", uvfp[0]); |
374 |
fputs("\t0\n", uvfp[1]); |
375 |
} |
376 |
fputs("}\n", uvfp[0]); |
377 |
fputs("}\n", uvfp[1]); |
378 |
if (fclose(uvfp[0]) || fclose(uvfp[1])) { |
379 |
fprintf(stderr, "%s: error writing uv file(s)\n", |
380 |
progname); |
381 |
exit(1); |
382 |
} |
383 |
} |
384 |
} |
385 |
} |
386 |
|
387 |
/* Interpolate and output anisotropic BSDF data */ |
388 |
static void |
389 |
eval_anisotropic(char *funame) |
390 |
{ |
391 |
const int sqres = 1<<samp_order; |
392 |
const double sqfact = 1./(double)sqres; |
393 |
float *ryval = NULL; |
394 |
char *rtrip = NULL; |
395 |
FILE *ofp, *uvfp[2]; |
396 |
int assignD = 0; |
397 |
char cmd[128]; |
398 |
int ix, iy, ox, oy; |
399 |
double iovec[6]; |
400 |
float bsdf, uv[2]; |
401 |
|
402 |
if (pctcull >= 0) { |
403 |
const char *avgopt = (input_orient>0 ^ output_orient>0) |
404 |
? "" : recip; |
405 |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
406 |
avgopt, pctcull, samp_order, |
407 |
create_component_file(0)); |
408 |
ofp = popen(cmd, "w"); |
409 |
if (ofp == NULL) { |
410 |
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
411 |
progname); |
412 |
exit(1); |
413 |
} |
414 |
SET_FILE_BINARY(ofp); |
415 |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
416 |
flockfile(ofp); |
417 |
#endif |
418 |
if (rbf_colorimetry == RBCtristimulus) { |
419 |
double uvcull = 100. - (100.-pctcull)*.25; |
420 |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
421 |
avgopt, uvcull, samp_order, |
422 |
create_component_file(1)); |
423 |
uvfp[0] = popen(cmd, "w"); |
424 |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
425 |
avgopt, uvcull, samp_order, |
426 |
create_component_file(2)); |
427 |
uvfp[1] = popen(cmd, "w"); |
428 |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
429 |
fprintf(stderr, "%s: cannot open pipes to uv output\n", |
430 |
progname); |
431 |
exit(1); |
432 |
} |
433 |
SET_FILE_BINARY(uvfp[0]); SET_FILE_BINARY(uvfp[1]); |
434 |
#ifdef getc_unlocked |
435 |
flockfile(uvfp[0]); flockfile(uvfp[1]); |
436 |
#endif |
437 |
} |
438 |
} else { |
439 |
ofp = fopen(create_component_file(0), "w"); |
440 |
if (ofp == NULL) { |
441 |
fprintf(stderr, "%s: cannot create Y output file\n", |
442 |
progname); |
443 |
exit(1); |
444 |
} |
445 |
fputs("{\n", ofp); |
446 |
if (rbf_colorimetry == RBCtristimulus) { |
447 |
uvfp[0] = fopen(create_component_file(1), "w"); |
448 |
uvfp[1] = fopen(create_component_file(2), "w"); |
449 |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
450 |
fprintf(stderr, "%s: cannot create uv output file(s)\n", |
451 |
progname); |
452 |
exit(1); |
453 |
} |
454 |
fputs("{\n", uvfp[0]); |
455 |
fputs("{\n", uvfp[1]); |
456 |
} |
457 |
} |
458 |
if (funame != NULL) /* need to assign Dx, Dy, Dz? */ |
459 |
assignD = (fundefined(funame) < 6); |
460 |
#if (NSSAMP > 0) |
461 |
rtrip = (char *)calloc(sqres, 1); /* track sample triggerings */ |
462 |
ryval = (float *)calloc(sqres, sizeof(float)); |
463 |
#endif |
464 |
/* run through directions */ |
465 |
for (ix = 0; ix < sqres; ix++) |
466 |
for (iy = 0; iy < sqres; iy++) { |
467 |
RBFNODE *rbf = NULL; /* Klems reversal */ |
468 |
SDsquare2disk(iovec, 1.-(ix+.5)*sqfact, 1.-(iy+.5)*sqfact); |
469 |
iovec[2] = input_orient * |
470 |
sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]); |
471 |
if (funame == NULL) |
472 |
rbf = advect_rbf(iovec, lobe_lim); |
473 |
for (ox = 0; ox < sqres; ox++) { |
474 |
SDValue sdv_next; |
475 |
SDsquare2disk(iovec+3, (ox+.5)*sqfact, .5*sqfact); |
476 |
iovec[5] = output_orient * |
477 |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
478 |
if (funame == NULL) { |
479 |
eval_rbfcol(&sdv_next, rbf, iovec+3); |
480 |
} else { |
481 |
sdv_next.spec = c_dfcolor; |
482 |
if (assignD) { |
483 |
varset("Dx", '=', -iovec[3]); |
484 |
varset("Dy", '=', -iovec[4]); |
485 |
varset("Dz", '=', -iovec[5]); |
486 |
++eclock; |
487 |
} |
488 |
sdv_next.cieY = funvalue(funame, 6, iovec); |
489 |
} |
490 |
for (oy = 0; oy < sqres; oy++) { |
491 |
int trip; |
492 |
bsdf = sdv_next.cieY; /* keeping one step ahead... */ |
493 |
if (oy < sqres-1) { |
494 |
SDsquare2disk(iovec+3, (ox+.5)*sqfact, (oy+1.5)*sqfact); |
495 |
iovec[5] = output_orient * |
496 |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
497 |
} |
498 |
if (funame == NULL) { |
499 |
SDValue sdv = sdv_next; |
500 |
if (oy < sqres-1) |
501 |
eval_rbfcol(&sdv_next, rbf, iovec+3); |
502 |
#if (NSSAMP > 0) |
503 |
trip = (abs_diff(bsdf, sdv_next.cieY) > ssamp_thresh || |
504 |
(ox && abs_diff(bsdf, ryval[oy]) > ssamp_thresh) || |
505 |
(oy && abs_diff(bsdf, ryval[oy-1]) > ssamp_thresh)); |
506 |
if (trip | rtrip[oy] || (oy && rtrip[oy-1])) { |
507 |
int ssi; |
508 |
double ssa[2], sum = 0, usum = 0, vsum = 0; |
509 |
/* super-sample voxel */ |
510 |
for (ssi = NSSAMP; ssi--; ) { |
511 |
SDmultiSamp(ssa, 2, (ssi+frandom()) * |
512 |
(1./NSSAMP)); |
513 |
SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, |
514 |
(oy+ssa[1])*sqfact); |
515 |
iovec[5] = output_orient * |
516 |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
517 |
eval_rbfcol(&sdv, rbf, iovec+3); |
518 |
sum += sdv.cieY; |
519 |
if (rbf_colorimetry == RBCtristimulus) { |
520 |
sdv.cieY /= |
521 |
-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.; |
522 |
usum += 4.*sdv.spec.cx * sdv.cieY; |
523 |
vsum += 9.*sdv.spec.cy * sdv.cieY; |
524 |
} |
525 |
} |
526 |
bsdf = sum * (1./NSSAMP); |
527 |
if (rbf_colorimetry == RBCtristimulus) { |
528 |
uv[0] = usum / (sum+FTINY); |
529 |
uv[1] = vsum / (sum+FTINY); |
530 |
} |
531 |
} else |
532 |
#endif |
533 |
if (rbf_colorimetry == RBCtristimulus) { |
534 |
uv[0] = uv[1] = 1. / |
535 |
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
536 |
uv[0] *= 4.*sdv.spec.cx; |
537 |
uv[1] *= 9.*sdv.spec.cy; |
538 |
} |
539 |
} else { |
540 |
if (oy < sqres-1) { |
541 |
if (assignD) { |
542 |
varset("Dx", '=', -iovec[3]); |
543 |
varset("Dy", '=', -iovec[4]); |
544 |
varset("Dz", '=', -iovec[5]); |
545 |
++eclock; |
546 |
} |
547 |
sdv_next.cieY = funvalue(funame, 6, iovec); |
548 |
} |
549 |
#if (NSSAMP > 0) |
550 |
trip = (abs_diff(bsdf, sdv_next.cieY) > ssamp_thresh || |
551 |
(ox && abs_diff(bsdf, ryval[oy]) > ssamp_thresh) || |
552 |
(oy && abs_diff(bsdf, ryval[oy-1]) > ssamp_thresh)); |
553 |
if (trip | rtrip[oy] || (oy && rtrip[oy-1])) { |
554 |
int ssi; |
555 |
double ssa[4], ssvec[6], sum = 0; |
556 |
/* super-sample voxel */ |
557 |
for (ssi = NSSAMP; ssi--; ) { |
558 |
SDmultiSamp(ssa, 4, (ssi+frandom()) * |
559 |
(1./NSSAMP)); |
560 |
SDsquare2disk(ssvec, 1.-(ix+ssa[0])*sqfact, |
561 |
1.-(iy+ssa[1])*sqfact); |
562 |
ssvec[2] = input_orient * |
563 |
sqrt(1. - ssvec[0]*ssvec[0] - |
564 |
ssvec[1]*ssvec[1]); |
565 |
SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact, |
566 |
(oy+ssa[3])*sqfact); |
567 |
ssvec[5] = output_orient * |
568 |
sqrt(1. - ssvec[3]*ssvec[3] - |
569 |
ssvec[4]*ssvec[4]); |
570 |
if (assignD) { |
571 |
varset("Dx", '=', -ssvec[3]); |
572 |
varset("Dy", '=', -ssvec[4]); |
573 |
varset("Dz", '=', -ssvec[5]); |
574 |
++eclock; |
575 |
} |
576 |
sum += funvalue(funame, 6, ssvec); |
577 |
} |
578 |
bsdf = sum * (1./NSSAMP); |
579 |
} |
580 |
#endif |
581 |
} |
582 |
if (pctcull >= 0) |
583 |
putbinary(&bsdf, sizeof(bsdf), 1, ofp); |
584 |
else |
585 |
fprintf(ofp, "\t%.3e\n", bsdf); |
586 |
|
587 |
if (rbf_colorimetry == RBCtristimulus) { |
588 |
if (pctcull >= 0) { |
589 |
putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]); |
590 |
putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]); |
591 |
} else { |
592 |
fprintf(uvfp[0], "\t%.3e\n", uv[0]); |
593 |
fprintf(uvfp[1], "\t%.3e\n", uv[1]); |
594 |
} |
595 |
} |
596 |
if (ryval != NULL) { |
597 |
rtrip[oy] = trip; |
598 |
ryval[oy] = bsdf; |
599 |
} |
600 |
} |
601 |
if (rbf != NULL) |
602 |
free(rbf); |
603 |
prog_show((ix*sqres+iy+1.)/(sqres*sqres)); |
604 |
} |
605 |
} |
606 |
prog_done(); |
607 |
if (ryval != NULL) { |
608 |
free(rtrip); |
609 |
free(ryval); |
610 |
} |
611 |
if (pctcull >= 0) { /* finish output */ |
612 |
if (pclose(ofp)) { |
613 |
fprintf(stderr, "%s: error running rttree_reduce on Y\n", |
614 |
progname); |
615 |
exit(1); |
616 |
} |
617 |
if (rbf_colorimetry == RBCtristimulus && |
618 |
(pclose(uvfp[0]) || pclose(uvfp[1]))) { |
619 |
fprintf(stderr, "%s: error running rttree_reduce on uv\n", |
620 |
progname); |
621 |
exit(1); |
622 |
} |
623 |
} else { |
624 |
fputs("}\n", ofp); |
625 |
if (fclose(ofp)) { |
626 |
fprintf(stderr, "%s: error writing Y file\n", |
627 |
progname); |
628 |
exit(1); |
629 |
} |
630 |
if (rbf_colorimetry == RBCtristimulus) { |
631 |
fputs("}\n", uvfp[0]); |
632 |
fputs("}\n", uvfp[1]); |
633 |
if (fclose(uvfp[0]) || fclose(uvfp[1])) { |
634 |
fprintf(stderr, "%s: error writing uv file(s)\n", |
635 |
progname); |
636 |
exit(1); |
637 |
} |
638 |
} |
639 |
} |
640 |
} |
641 |
|
642 |
#if defined(_WIN32) || defined(_WIN64) |
643 |
/* Execute wrapBSDF command (may never return) */ |
644 |
static int |
645 |
wrap_up(void) |
646 |
{ |
647 |
char cmd[8192]; |
648 |
|
649 |
if (bsdf_manuf[0]) { |
650 |
add_wbsdf("-f", 1); |
651 |
strcpy(cmd, "m="); |
652 |
strcpy(cmd+2, bsdf_manuf); |
653 |
add_wbsdf(cmd, 0); |
654 |
} |
655 |
if (bsdf_name[0]) { |
656 |
add_wbsdf("-f", 1); |
657 |
strcpy(cmd, "n="); |
658 |
strcpy(cmd+2, bsdf_name); |
659 |
add_wbsdf(cmd, 0); |
660 |
} |
661 |
if (!convert_commandline(cmd, sizeof(cmd), wrapBSDF)) { |
662 |
fputs(progname, stderr); |
663 |
fputs(": command line too long in wrap_up()\n", stderr); |
664 |
return(1); |
665 |
} |
666 |
return(system(cmd)); |
667 |
} |
668 |
#else |
669 |
/* Execute wrapBSDF command (may never return) */ |
670 |
static int |
671 |
wrap_up(void) |
672 |
{ |
673 |
char buf[256]; |
674 |
char *compath = getpath((char *)wrapBSDF[0], getenv("PATH"), X_OK); |
675 |
|
676 |
if (compath == NULL) { |
677 |
fprintf(stderr, "%s: cannot locate %s\n", progname, wrapBSDF[0]); |
678 |
return(1); |
679 |
} |
680 |
if (bsdf_manuf[0]) { |
681 |
add_wbsdf("-f", 1); |
682 |
strcpy(buf, "m="); |
683 |
strcpy(buf+2, bsdf_manuf); |
684 |
add_wbsdf(buf, 0); |
685 |
} |
686 |
if (bsdf_name[0]) { |
687 |
add_wbsdf("-f", 1); |
688 |
strcpy(buf, "n="); |
689 |
strcpy(buf+2, bsdf_name); |
690 |
add_wbsdf(buf, 0); |
691 |
} |
692 |
execv(compath, wrapBSDF); /* successful call never returns */ |
693 |
perror(compath); |
694 |
return(1); |
695 |
} |
696 |
#endif |
697 |
|
698 |
/* Read in BSDF and interpolate as tensor tree representation */ |
699 |
int |
700 |
main(int argc, char *argv[]) |
701 |
{ |
702 |
static char tfmt[2][4] = {"t4", "t3"}; |
703 |
int dofwd = 0, dobwd = 1; |
704 |
char buf[2048]; |
705 |
int i, na; |
706 |
|
707 |
progname = argv[0]; |
708 |
esupport |= E_VARIABLE|E_FUNCTION|E_RCONST; |
709 |
esupport &= ~(E_INCHAN|E_OUTCHAN); |
710 |
scompile("PI:3.14159265358979323846", NULL, 0); |
711 |
biggerlib(); |
712 |
for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
713 |
switch (argv[i][1]) { /* get options */ |
714 |
case 'e': |
715 |
scompile(argv[++i], NULL, 0); |
716 |
if (single_plane_incident < 0) |
717 |
single_plane_incident = 0; |
718 |
break; |
719 |
case 'f': |
720 |
if (!argv[i][2]) { |
721 |
if (strchr(argv[++i], '=') != NULL) { |
722 |
add_wbsdf("-f", 1); |
723 |
add_wbsdf(argv[i], 1); |
724 |
} else { |
725 |
char *fpath = getpath(argv[i], |
726 |
getrlibpath(), 0); |
727 |
if (fpath == NULL) { |
728 |
fprintf(stderr, |
729 |
"%s: cannot find file '%s'\n", |
730 |
argv[0], argv[i]); |
731 |
return(1); |
732 |
} |
733 |
fcompile(fpath); |
734 |
if (single_plane_incident < 0) |
735 |
single_plane_incident = 0; |
736 |
} |
737 |
} else |
738 |
dofwd = (argv[i][0] == '+'); |
739 |
break; |
740 |
case 'a': |
741 |
recip = (argv[i][0] == '+') ? " -a" : ""; |
742 |
break; |
743 |
case 'b': |
744 |
dobwd = (argv[i][0] == '+'); |
745 |
break; |
746 |
case 't': |
747 |
switch (argv[i][2]) { |
748 |
case '3': |
749 |
single_plane_incident = 1; |
750 |
break; |
751 |
case '4': |
752 |
single_plane_incident = 0; |
753 |
break; |
754 |
case '\0': |
755 |
pctcull = atof(argv[++i]); |
756 |
break; |
757 |
default: |
758 |
goto userr; |
759 |
} |
760 |
break; |
761 |
case 'g': |
762 |
samp_order = atoi(argv[++i]); |
763 |
break; |
764 |
case 'l': |
765 |
lobe_lim = atoi(argv[++i]); |
766 |
break; |
767 |
case 'p': |
768 |
do_prog = atoi(argv[i]+2); |
769 |
break; |
770 |
case 'W': |
771 |
add_wbsdf(argv[i], 1); |
772 |
break; |
773 |
case 'u': |
774 |
case 'C': |
775 |
add_wbsdf(argv[i], 1); |
776 |
add_wbsdf(argv[++i], 1); |
777 |
break; |
778 |
default: |
779 |
goto userr; |
780 |
} |
781 |
strcpy(buf, "File produced by: "); |
782 |
if (convert_commandline(buf+18, sizeof(buf)-18, argv) != NULL) { |
783 |
add_wbsdf("-C", 1); add_wbsdf(buf, 0); |
784 |
} |
785 |
if (single_plane_incident >= 0) { /* function-based BSDF? */ |
786 |
void (*evf)(char *s) = single_plane_incident ? |
787 |
eval_isotropic : eval_anisotropic; |
788 |
if (i != argc-1 || fundefined(argv[i]) < 3) { |
789 |
fprintf(stderr, |
790 |
"%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n", |
791 |
progname); |
792 |
fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n"); |
793 |
goto userr; |
794 |
} |
795 |
++eclock; |
796 |
add_wbsdf("-a", 1); |
797 |
add_wbsdf(tfmt[single_plane_incident], 1); |
798 |
if (dofwd) { |
799 |
input_orient = -1; |
800 |
output_orient = -1; |
801 |
prog_start("Evaluating outside reflectance"); |
802 |
(*evf)(argv[i]); |
803 |
output_orient = 1; |
804 |
prog_start("Evaluating outside->inside transmission"); |
805 |
(*evf)(argv[i]); |
806 |
} |
807 |
if (dobwd) { |
808 |
input_orient = 1; |
809 |
output_orient = 1; |
810 |
prog_start("Evaluating inside reflectance"); |
811 |
(*evf)(argv[i]); |
812 |
output_orient = -1; |
813 |
prog_start("Evaluating inside->outside transmission"); |
814 |
(*evf)(argv[i]); |
815 |
} |
816 |
return(wrap_up()); |
817 |
} |
818 |
if (i < argc) { /* open input files if given */ |
819 |
int nbsdf = 0; |
820 |
for ( ; i < argc; i++) { /* interpolate each component */ |
821 |
char pbuf[256]; |
822 |
FILE *fpin = fopen(argv[i], "rb"); |
823 |
if (fpin == NULL) { |
824 |
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
825 |
progname, argv[i]); |
826 |
return(1); |
827 |
} |
828 |
if (!load_bsdf_rep(fpin)) |
829 |
return(1); |
830 |
fclose(fpin); |
831 |
sprintf(pbuf, "Interpolating component '%s'", argv[i]); |
832 |
prog_start(pbuf); |
833 |
if (!nbsdf++) { |
834 |
add_wbsdf("-a", 1); |
835 |
add_wbsdf(tfmt[single_plane_incident], 1); |
836 |
} |
837 |
if (single_plane_incident) |
838 |
eval_isotropic(NULL); |
839 |
else |
840 |
eval_anisotropic(NULL); |
841 |
} |
842 |
return(wrap_up()); |
843 |
} |
844 |
SET_FILE_BINARY(stdin); /* load from stdin */ |
845 |
if (!load_bsdf_rep(stdin)) |
846 |
return(1); |
847 |
prog_start("Interpolating from standard input"); |
848 |
add_wbsdf("-a", 1); |
849 |
add_wbsdf(tfmt[single_plane_incident], 1); |
850 |
if (single_plane_incident) /* resample dist. */ |
851 |
eval_isotropic(NULL); |
852 |
else |
853 |
eval_anisotropic(NULL); |
854 |
|
855 |
return(wrap_up()); |
856 |
userr: |
857 |
fprintf(stderr, |
858 |
"Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
859 |
progname); |
860 |
fprintf(stderr, |
861 |
" or: %s -t{3|4} [{+|-}a][-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |
862 |
progname); |
863 |
return(1); |
864 |
} |