| 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 |
|
static double pctcull = 90.; |
| 26 |
|
/* sampling order */ |
| 29 |
|
const double ssamp_thresh = 0.35; |
| 30 |
|
/* number of super-samples */ |
| 31 |
|
#ifndef NSSAMP |
| 32 |
< |
#define NSSAMP 100 |
| 32 |
> |
#define NSSAMP 64 |
| 33 |
|
#endif |
| 34 |
|
/* limit on number of RBF lobes */ |
| 35 |
|
static int lobe_lim = 15000; |
| 40 |
|
static char *wrapBSDF[MAXCARG] = {"wrapBSDF", "-U"}; |
| 41 |
|
static int wbsdfac = 2; |
| 42 |
|
|
| 43 |
< |
/* Add argument to wrapBSDF, allocating space if isstatic */ |
| 43 |
> |
/* Add argument to wrapBSDF, allocating space if !isstatic */ |
| 44 |
|
static void |
| 45 |
|
add_wbsdf(const char *arg, int isstatic) |
| 46 |
|
{ |
| 129 |
|
eval_isotropic(char *funame) |
| 130 |
|
{ |
| 131 |
|
const int sqres = 1<<samp_order; |
| 132 |
+ |
const double sqfact = 1./(double)sqres; |
| 133 |
|
FILE *ofp, *uvfp[2]; |
| 134 |
|
int assignD = 0; |
| 135 |
|
char cmd[128]; |
| 138 |
|
float bsdf, uv[2]; |
| 139 |
|
|
| 140 |
|
if (pctcull >= 0) { |
| 141 |
< |
sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d > %s", |
| 142 |
< |
pctcull, samp_order, create_component_file(0)); |
| 141 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s", |
| 142 |
> |
recip, pctcull, samp_order, create_component_file(0)); |
| 143 |
|
ofp = popen(cmd, "w"); |
| 144 |
|
if (ofp == NULL) { |
| 145 |
|
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
| 152 |
|
#endif |
| 153 |
|
if (rbf_colorimetry == RBCtristimulus) { |
| 154 |
|
double uvcull = 100. - (100.-pctcull)*.25; |
| 155 |
< |
sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d > %s", |
| 156 |
< |
uvcull, samp_order, create_component_file(1)); |
| 155 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s", |
| 156 |
> |
recip, uvcull, samp_order, create_component_file(1)); |
| 157 |
|
uvfp[0] = popen(cmd, "w"); |
| 158 |
< |
sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d > %s", |
| 159 |
< |
uvcull, samp_order, create_component_file(2)); |
| 158 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s", |
| 159 |
> |
recip, uvcull, samp_order, create_component_file(2)); |
| 160 |
|
uvfp[1] = popen(cmd, "w"); |
| 161 |
|
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
| 162 |
|
fprintf(stderr, "%s: cannot open pipes to uv output\n", |
| 188 |
|
fputs("{\n", uvfp[1]); |
| 189 |
|
} |
| 190 |
|
} |
| 191 |
< |
/* need to assign Dx, Dy, Dz? */ |
| 189 |
< |
if (funame != NULL) |
| 191 |
> |
if (funame != NULL) /* need to assign Dx, Dy, Dz? */ |
| 192 |
|
assignD = (fundefined(funame) < 6); |
| 193 |
|
/* run through directions */ |
| 194 |
|
for (ix = 0; ix < sqres/2; ix++) { |
| 195 |
|
RBFNODE *rbf = NULL; |
| 196 |
< |
iovec[0] = 2.*(ix+.5)/sqres - 1.; |
| 196 |
> |
iovec[0] = 2.*sqfact*(ix+.5) - 1.; |
| 197 |
|
iovec[1] = .0; |
| 198 |
|
iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]); |
| 199 |
|
if (funame == NULL) |
| 201 |
|
for (ox = 0; ox < sqres; ox++) { |
| 202 |
|
float last_bsdf = -1; |
| 203 |
|
for (oy = 0; oy < sqres; oy++) { |
| 204 |
< |
SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); |
| 204 |
> |
SDsquare2disk(iovec+3, (ox+.5)*sqfact, (oy+.5)*sqfact); |
| 205 |
|
iovec[5] = output_orient * |
| 206 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
| 207 |
|
if (funame == NULL) { |
| 208 |
|
SDValue sdv; |
| 209 |
|
eval_rbfcol(&sdv, rbf, iovec+3); |
| 210 |
|
bsdf = sdv.cieY; |
| 211 |
+ |
#if (NSSAMP > 0) |
| 212 |
+ |
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
| 213 |
+ |
int ssi; |
| 214 |
+ |
double ssa[2], sum = 0, usum = 0, vsum = 0; |
| 215 |
+ |
/* super-sample voxel */ |
| 216 |
+ |
for (ssi = NSSAMP; ssi--; ) { |
| 217 |
+ |
SDmultiSamp(ssa, 2, (ssi+frandom()) * |
| 218 |
+ |
(1./NSSAMP)); |
| 219 |
+ |
SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, |
| 220 |
+ |
(oy+ssa[1])*sqfact); |
| 221 |
+ |
iovec[5] = output_orient * |
| 222 |
+ |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
| 223 |
+ |
eval_rbfcol(&sdv, rbf, iovec+3); |
| 224 |
+ |
sum += sdv.cieY; |
| 225 |
+ |
if (rbf_colorimetry == RBCtristimulus) { |
| 226 |
+ |
sdv.cieY /= |
| 227 |
+ |
-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.; |
| 228 |
+ |
usum += 4.*sdv.spec.cx * sdv.cieY; |
| 229 |
+ |
vsum += 9.*sdv.spec.cy * sdv.cieY; |
| 230 |
+ |
} |
| 231 |
+ |
} |
| 232 |
+ |
bsdf = sum * (1./NSSAMP); |
| 233 |
+ |
if (rbf_colorimetry == RBCtristimulus) { |
| 234 |
+ |
uv[0] = usum / (sum+FTINY); |
| 235 |
+ |
uv[1] = vsum / (sum+FTINY); |
| 236 |
+ |
} |
| 237 |
+ |
} else |
| 238 |
+ |
#endif |
| 239 |
|
if (rbf_colorimetry == RBCtristimulus) { |
| 210 |
– |
c_ccvt(&sdv.spec, C_CSXY); |
| 240 |
|
uv[0] = uv[1] = 1. / |
| 241 |
|
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
| 242 |
|
uv[0] *= 4.*sdv.spec.cx; |
| 258 |
|
for (ssi = NSSAMP; ssi--; ) { |
| 259 |
|
SDmultiSamp(ssa, 3, (ssi+frandom()) * |
| 260 |
|
(1./NSSAMP)); |
| 261 |
< |
ssvec[0] = 2.*(ix+ssa[0])/sqres - 1.; |
| 261 |
> |
ssvec[0] = 2.*sqfact*(ix+ssa[0]) - 1.; |
| 262 |
|
ssvec[1] = .0; |
| 263 |
|
ssvec[2] = input_orient * |
| 264 |
|
sqrt(1. - ssvec[0]*ssvec[0]); |
| 265 |
< |
SDsquare2disk(ssvec+3, (ox+ssa[1])/sqres, |
| 266 |
< |
(oy+ssa[2])/sqres); |
| 265 |
> |
SDsquare2disk(ssvec+3, (ox+ssa[1])*sqfact, |
| 266 |
> |
(oy+ssa[2])*sqfact); |
| 267 |
|
ssvec[5] = output_orient * |
| 268 |
|
sqrt(1. - ssvec[3]*ssvec[3] - |
| 269 |
|
ssvec[4]*ssvec[4]); |
| 275 |
|
} |
| 276 |
|
sum += funvalue(funame, 6, ssvec); |
| 277 |
|
} |
| 278 |
< |
bsdf = sum/NSSAMP; |
| 278 |
> |
bsdf = sum * (1./NSSAMP); |
| 279 |
|
} |
| 280 |
|
#endif |
| 281 |
|
} |
| 282 |
|
if (pctcull >= 0) |
| 283 |
< |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
| 283 |
> |
putbinary(&bsdf, sizeof(bsdf), 1, ofp); |
| 284 |
|
else |
| 285 |
|
fprintf(ofp, "\t%.3e\n", bsdf); |
| 286 |
|
|
| 287 |
|
if (rbf_colorimetry == RBCtristimulus) { |
| 288 |
|
if (pctcull >= 0) { |
| 289 |
< |
fwrite(&uv[0], sizeof(*uv), 1, uvfp[0]); |
| 290 |
< |
fwrite(&uv[1], sizeof(*uv), 1, uvfp[1]); |
| 289 |
> |
putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]); |
| 290 |
> |
putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]); |
| 291 |
|
} else { |
| 292 |
|
fprintf(uvfp[0], "\t%.3e\n", uv[0]); |
| 293 |
|
fprintf(uvfp[1], "\t%.3e\n", uv[1]); |
| 298 |
|
} |
| 299 |
|
if (rbf != NULL) |
| 300 |
|
free(rbf); |
| 301 |
< |
prog_show((ix+1.)*(2./sqres)); |
| 301 |
> |
prog_show((ix+1.)*(2.*sqfact)); |
| 302 |
|
} |
| 303 |
+ |
prog_done(); |
| 304 |
|
if (pctcull >= 0) { /* finish output */ |
| 305 |
|
if (pclose(ofp)) { |
| 306 |
|
fprintf(stderr, "%s: error running rttree_reduce on Y\n", |
| 336 |
|
} |
| 337 |
|
} |
| 338 |
|
} |
| 309 |
– |
prog_done(); |
| 339 |
|
} |
| 340 |
|
|
| 341 |
|
/* Interpolate and output anisotropic BSDF data */ |
| 343 |
|
eval_anisotropic(char *funame) |
| 344 |
|
{ |
| 345 |
|
const int sqres = 1<<samp_order; |
| 346 |
+ |
const double sqfact = 1./(double)sqres; |
| 347 |
|
FILE *ofp, *uvfp[2]; |
| 348 |
|
int assignD = 0; |
| 349 |
|
char cmd[128]; |
| 352 |
|
float bsdf, uv[2]; |
| 353 |
|
|
| 354 |
|
if (pctcull >= 0) { |
| 355 |
+ |
const char *avgopt = (input_orient>0 ^ output_orient>0) |
| 356 |
+ |
? "" : recip; |
| 357 |
|
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
| 358 |
< |
(input_orient>0 ^ output_orient>0) ? "" : " -a", |
| 359 |
< |
pctcull, samp_order, create_component_file(0)); |
| 358 |
> |
avgopt, pctcull, samp_order, |
| 359 |
> |
create_component_file(0)); |
| 360 |
|
ofp = popen(cmd, "w"); |
| 361 |
|
if (ofp == NULL) { |
| 362 |
|
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
| 370 |
|
if (rbf_colorimetry == RBCtristimulus) { |
| 371 |
|
double uvcull = 100. - (100.-pctcull)*.25; |
| 372 |
|
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
| 373 |
< |
(input_orient>0 ^ output_orient>0) ? "" : " -a", |
| 374 |
< |
uvcull, samp_order, create_component_file(1)); |
| 373 |
> |
avgopt, uvcull, samp_order, |
| 374 |
> |
create_component_file(1)); |
| 375 |
|
uvfp[0] = popen(cmd, "w"); |
| 376 |
|
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
| 377 |
< |
(input_orient>0 ^ output_orient>0) ? "" : " -a", |
| 378 |
< |
uvcull, samp_order, create_component_file(2)); |
| 377 |
> |
avgopt, uvcull, samp_order, |
| 378 |
> |
create_component_file(2)); |
| 379 |
|
uvfp[1] = popen(cmd, "w"); |
| 380 |
|
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
| 381 |
|
fprintf(stderr, "%s: cannot open pipes to uv output\n", |
| 407 |
|
fputs("{\n", uvfp[1]); |
| 408 |
|
} |
| 409 |
|
} |
| 410 |
< |
/* need to assign Dx, Dy, Dz? */ |
| 379 |
< |
if (funame != NULL) |
| 410 |
> |
if (funame != NULL) /* need to assign Dx, Dy, Dz? */ |
| 411 |
|
assignD = (fundefined(funame) < 6); |
| 412 |
|
/* run through directions */ |
| 413 |
|
for (ix = 0; ix < sqres; ix++) |
| 414 |
|
for (iy = 0; iy < sqres; iy++) { |
| 415 |
|
RBFNODE *rbf = NULL; /* Klems reversal */ |
| 416 |
< |
SDsquare2disk(iovec, 1.-(ix+.5)/sqres, 1.-(iy+.5)/sqres); |
| 416 |
> |
SDsquare2disk(iovec, 1.-(ix+.5)*sqfact, 1.-(iy+.5)*sqfact); |
| 417 |
|
iovec[2] = input_orient * |
| 418 |
|
sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]); |
| 419 |
|
if (funame == NULL) |
| 421 |
|
for (ox = 0; ox < sqres; ox++) { |
| 422 |
|
float last_bsdf = -1; |
| 423 |
|
for (oy = 0; oy < sqres; oy++) { |
| 424 |
< |
SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); |
| 424 |
> |
SDsquare2disk(iovec+3, (ox+.5)*sqfact, (oy+.5)*sqfact); |
| 425 |
|
iovec[5] = output_orient * |
| 426 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
| 427 |
|
if (funame == NULL) { |
| 428 |
|
SDValue sdv; |
| 429 |
|
eval_rbfcol(&sdv, rbf, iovec+3); |
| 430 |
|
bsdf = sdv.cieY; |
| 431 |
+ |
#if (NSSAMP > 0) |
| 432 |
+ |
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
| 433 |
+ |
int ssi; |
| 434 |
+ |
double ssa[2], sum = 0, usum = 0, vsum = 0; |
| 435 |
+ |
/* super-sample voxel */ |
| 436 |
+ |
for (ssi = NSSAMP; ssi--; ) { |
| 437 |
+ |
SDmultiSamp(ssa, 2, (ssi+frandom()) * |
| 438 |
+ |
(1./NSSAMP)); |
| 439 |
+ |
SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, |
| 440 |
+ |
(oy+ssa[1])*sqfact); |
| 441 |
+ |
iovec[5] = output_orient * |
| 442 |
+ |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
| 443 |
+ |
eval_rbfcol(&sdv, rbf, iovec+3); |
| 444 |
+ |
sum += sdv.cieY; |
| 445 |
+ |
if (rbf_colorimetry == RBCtristimulus) { |
| 446 |
+ |
sdv.cieY /= |
| 447 |
+ |
-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.; |
| 448 |
+ |
usum += 4.*sdv.spec.cx * sdv.cieY; |
| 449 |
+ |
vsum += 9.*sdv.spec.cy * sdv.cieY; |
| 450 |
+ |
} |
| 451 |
+ |
} |
| 452 |
+ |
bsdf = sum * (1./NSSAMP); |
| 453 |
+ |
if (rbf_colorimetry == RBCtristimulus) { |
| 454 |
+ |
uv[0] = usum / (sum+FTINY); |
| 455 |
+ |
uv[1] = vsum / (sum+FTINY); |
| 456 |
+ |
} |
| 457 |
+ |
} else |
| 458 |
+ |
#endif |
| 459 |
|
if (rbf_colorimetry == RBCtristimulus) { |
| 401 |
– |
c_ccvt(&sdv.spec, C_CSXY); |
| 460 |
|
uv[0] = uv[1] = 1. / |
| 461 |
|
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
| 462 |
|
uv[0] *= 4.*sdv.spec.cx; |
| 478 |
|
for (ssi = NSSAMP; ssi--; ) { |
| 479 |
|
SDmultiSamp(ssa, 4, (ssi+frandom()) * |
| 480 |
|
(1./NSSAMP)); |
| 481 |
< |
SDsquare2disk(ssvec, 1.-(ix+ssa[0])/sqres, |
| 482 |
< |
1.-(iy+ssa[1])/sqres); |
| 481 |
> |
SDsquare2disk(ssvec, 1.-(ix+ssa[0])*sqfact, |
| 482 |
> |
1.-(iy+ssa[1])*sqfact); |
| 483 |
|
ssvec[2] = input_orient * |
| 484 |
|
sqrt(1. - ssvec[0]*ssvec[0] - |
| 485 |
|
ssvec[1]*ssvec[1]); |
| 486 |
< |
SDsquare2disk(ssvec+3, (ox+ssa[2])/sqres, |
| 487 |
< |
(oy+ssa[3])/sqres); |
| 486 |
> |
SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact, |
| 487 |
> |
(oy+ssa[3])*sqfact); |
| 488 |
|
ssvec[5] = output_orient * |
| 489 |
|
sqrt(1. - ssvec[3]*ssvec[3] - |
| 490 |
|
ssvec[4]*ssvec[4]); |
| 496 |
|
} |
| 497 |
|
sum += funvalue(funame, 6, ssvec); |
| 498 |
|
} |
| 499 |
< |
bsdf = sum/NSSAMP; |
| 499 |
> |
bsdf = sum * (1./NSSAMP); |
| 500 |
|
} |
| 501 |
|
#endif |
| 502 |
|
} |
| 503 |
|
if (pctcull >= 0) |
| 504 |
< |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
| 504 |
> |
putbinary(&bsdf, sizeof(bsdf), 1, ofp); |
| 505 |
|
else |
| 506 |
|
fprintf(ofp, "\t%.3e\n", bsdf); |
| 507 |
|
|
| 508 |
|
if (rbf_colorimetry == RBCtristimulus) { |
| 509 |
|
if (pctcull >= 0) { |
| 510 |
< |
fwrite(&uv[0], sizeof(*uv), 1, uvfp[0]); |
| 511 |
< |
fwrite(&uv[1], sizeof(*uv), 1, uvfp[1]); |
| 510 |
> |
putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]); |
| 511 |
> |
putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]); |
| 512 |
|
} else { |
| 513 |
|
fprintf(uvfp[0], "\t%.3e\n", uv[0]); |
| 514 |
|
fprintf(uvfp[1], "\t%.3e\n", uv[1]); |
| 521 |
|
free(rbf); |
| 522 |
|
prog_show((ix*sqres+iy+1.)/(sqres*sqres)); |
| 523 |
|
} |
| 524 |
+ |
prog_done(); |
| 525 |
|
if (pctcull >= 0) { /* finish output */ |
| 526 |
|
if (pclose(ofp)) { |
| 527 |
|
fprintf(stderr, "%s: error running rttree_reduce on Y\n", |
| 551 |
|
} |
| 552 |
|
} |
| 553 |
|
} |
| 495 |
– |
prog_done(); |
| 554 |
|
} |
| 555 |
|
|
| 556 |
< |
#ifdef _WIN32 |
| 556 |
> |
#if defined(_WIN32) || defined(_WIN64) |
| 557 |
|
/* Execute wrapBSDF command (may never return) */ |
| 558 |
|
static int |
| 559 |
|
wrap_up(void) |
| 623 |
|
esupport &= ~(E_INCHAN|E_OUTCHAN); |
| 624 |
|
scompile("PI:3.14159265358979323846", NULL, 0); |
| 625 |
|
biggerlib(); |
| 626 |
< |
for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
| 626 |
> |
for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
| 627 |
|
switch (argv[i][1]) { /* get options */ |
| 628 |
|
case 'e': |
| 629 |
|
scompile(argv[++i], NULL, 0); |
| 638 |
|
} else |
| 639 |
|
dofwd = (argv[i][0] == '+'); |
| 640 |
|
break; |
| 641 |
+ |
case 'a': |
| 642 |
+ |
recip = (argv[i][0] == '+') ? " -a" : ""; |
| 643 |
+ |
break; |
| 644 |
|
case 'b': |
| 645 |
|
dobwd = (argv[i][0] == '+'); |
| 646 |
|
break; |
| 756 |
|
return(wrap_up()); |
| 757 |
|
userr: |
| 758 |
|
fprintf(stderr, |
| 759 |
< |
"Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
| 759 |
> |
"Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
| 760 |
|
progname); |
| 761 |
|
fprintf(stderr, |
| 762 |
< |
" or: %s -t{3|4} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |
| 762 |
> |
" or: %s -t{3|4} [{+|-}a][-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |
| 763 |
|
progname); |
| 764 |
|
return(1); |
| 765 |
|
} |