| 8 |
|
*/ |
| 9 |
|
|
| 10 |
|
#define _USE_MATH_DEFINES |
| 11 |
– |
#include <stdio.h> |
| 11 |
|
#include <stdlib.h> |
| 12 |
|
#include <math.h> |
| 13 |
|
#include "random.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.35; |
| 28 |
> |
const double ssamp_thresh = 0.25; |
| 29 |
|
/* number of super-samples */ |
| 30 |
|
#ifndef NSSAMP |
| 31 |
< |
#define NSSAMP 100 |
| 31 |
> |
#define NSSAMP 256 |
| 32 |
|
#endif |
| 33 |
|
/* limit on number of RBF lobes */ |
| 34 |
|
static int lobe_lim = 15000; |
| 39 |
|
static char *wrapBSDF[MAXCARG] = {"wrapBSDF", "-U"}; |
| 40 |
|
static int wbsdfac = 2; |
| 41 |
|
|
| 42 |
< |
/* Add argument to wrapBSDF, allocating space if isstatic */ |
| 42 |
> |
/* Add argument to wrapBSDF, allocating space if !isstatic */ |
| 43 |
|
static void |
| 44 |
|
add_wbsdf(const char *arg, int isstatic) |
| 45 |
|
{ |
| 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]; |
| 137 |
|
float bsdf, uv[2]; |
| 138 |
|
|
| 139 |
|
if (pctcull >= 0) { |
| 140 |
< |
sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d > %s", |
| 141 |
< |
pctcull, samp_order, create_component_file(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", |
| 151 |
|
#endif |
| 152 |
|
if (rbf_colorimetry == RBCtristimulus) { |
| 153 |
|
double uvcull = 100. - (100.-pctcull)*.25; |
| 154 |
< |
sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d > %s", |
| 155 |
< |
uvcull, samp_order, create_component_file(1)); |
| 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 -a -h -ff -r 3 -t %f -g %d > %s", |
| 158 |
< |
uvcull, samp_order, create_component_file(2)); |
| 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", |
| 191 |
|
assignD = (fundefined(funame) < 6); |
| 192 |
|
/* run through directions */ |
| 193 |
|
for (ix = 0; ix < sqres/2; ix++) { |
| 194 |
< |
RBFNODE *rbf = NULL; |
| 195 |
< |
iovec[0] = 2.*(ix+.5)/sqres - 1.; |
| 196 |
< |
iovec[1] = .0; |
| 197 |
< |
iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]); |
| 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)/sqres, (oy+.5)/sqres); |
| 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) { |
| 209 |
– |
c_ccvt(&sdv.spec, C_CSXY); |
| 241 |
|
uv[0] = uv[1] = 1. / |
| 242 |
|
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
| 243 |
|
uv[0] *= 4.*sdv.spec.cx; |
| 254 |
|
#if (NSSAMP > 0) |
| 255 |
|
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
| 256 |
|
int ssi; |
| 257 |
< |
double ssa[3], ssvec[6], sum = 0; |
| 257 |
> |
double ssa[4], ssvec[6], sum = 0; |
| 258 |
|
/* super-sample voxel */ |
| 259 |
|
for (ssi = NSSAMP; ssi--; ) { |
| 260 |
< |
SDmultiSamp(ssa, 3, (ssi+frandom()) * |
| 260 |
> |
SDmultiSamp(ssa, 4, (ssi+frandom()) * |
| 261 |
|
(1./NSSAMP)); |
| 262 |
< |
ssvec[0] = 2.*(ix+ssa[0])/sqres - 1.; |
| 263 |
< |
ssvec[1] = .0; |
| 264 |
< |
ssvec[2] = input_orient * |
| 265 |
< |
sqrt(1. - ssvec[0]*ssvec[0]); |
| 266 |
< |
SDsquare2disk(ssvec+3, (ox+ssa[1])/sqres, |
| 267 |
< |
(oy+ssa[2])/sqres); |
| 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]); |
| 281 |
|
} |
| 282 |
|
sum += funvalue(funame, 6, ssvec); |
| 283 |
|
} |
| 284 |
< |
bsdf = sum/NSSAMP; |
| 284 |
> |
bsdf = sum * (1./NSSAMP); |
| 285 |
|
} |
| 286 |
|
#endif |
| 287 |
|
} |
| 288 |
|
if (pctcull >= 0) |
| 289 |
< |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
| 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 |
< |
fwrite(&uv[0], sizeof(*uv), 1, uvfp[0]); |
| 296 |
< |
fwrite(&uv[1], sizeof(*uv), 1, uvfp[1]); |
| 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]); |
| 304 |
|
} |
| 305 |
|
if (rbf != NULL) |
| 306 |
|
free(rbf); |
| 307 |
< |
prog_show((ix+1.)*(2./sqres)); |
| 307 |
> |
prog_show((ix+1.)*(2.*sqfact)); |
| 308 |
|
} |
| 309 |
|
prog_done(); |
| 310 |
|
if (pctcull >= 0) { /* finish output */ |
| 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]; |
| 359 |
|
|
| 360 |
|
if (pctcull >= 0) { |
| 361 |
|
const char *avgopt = (input_orient>0 ^ output_orient>0) |
| 362 |
< |
? "" : " -a"; |
| 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)); |
| 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)/sqres, 1.-(iy+.5)/sqres); |
| 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) |
| 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)/sqres, (oy+.5)/sqres); |
| 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) { |
| 401 |
– |
c_ccvt(&sdv.spec, C_CSXY); |
| 466 |
|
uv[0] = uv[1] = 1. / |
| 467 |
|
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
| 468 |
|
uv[0] *= 4.*sdv.spec.cx; |
| 484 |
|
for (ssi = NSSAMP; ssi--; ) { |
| 485 |
|
SDmultiSamp(ssa, 4, (ssi+frandom()) * |
| 486 |
|
(1./NSSAMP)); |
| 487 |
< |
SDsquare2disk(ssvec, 1.-(ix+ssa[0])/sqres, |
| 488 |
< |
1.-(iy+ssa[1])/sqres); |
| 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])/sqres, |
| 493 |
< |
(oy+ssa[3])/sqres); |
| 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]); |
| 502 |
|
} |
| 503 |
|
sum += funvalue(funame, 6, ssvec); |
| 504 |
|
} |
| 505 |
< |
bsdf = sum/NSSAMP; |
| 505 |
> |
bsdf = sum * (1./NSSAMP); |
| 506 |
|
} |
| 507 |
|
#endif |
| 508 |
|
} |
| 509 |
|
if (pctcull >= 0) |
| 510 |
< |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
| 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 |
< |
fwrite(&uv[0], sizeof(*uv), 1, uvfp[0]); |
| 517 |
< |
fwrite(&uv[1], sizeof(*uv), 1, uvfp[1]); |
| 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]); |
| 629 |
|
esupport &= ~(E_INCHAN|E_OUTCHAN); |
| 630 |
|
scompile("PI:3.14159265358979323846", NULL, 0); |
| 631 |
|
biggerlib(); |
| 632 |
< |
for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
| 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 |
< |
fcompile(argv[i]); |
| 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; |
| 775 |
|
return(wrap_up()); |
| 776 |
|
userr: |
| 777 |
|
fprintf(stderr, |
| 778 |
< |
"Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
| 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} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |
| 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 |
|
} |