| 11 |
|
#include <stdio.h> |
| 12 |
|
#include <stdlib.h> |
| 13 |
|
#include <math.h> |
| 14 |
+ |
#include "random.h" |
| 15 |
|
#include "platform.h" |
| 16 |
+ |
#include "rtprocess.h" |
| 17 |
|
#include "calcomp.h" |
| 18 |
|
#include "bsdfrep.h" |
| 19 |
|
/* global argv[0] */ |
| 25 |
|
/* super-sampling threshold */ |
| 26 |
|
const double ssamp_thresh = 0.35; |
| 27 |
|
/* number of super-samples */ |
| 28 |
< |
const int nssamp = 100; |
| 28 |
> |
#ifndef NSSAMP |
| 29 |
> |
#define NSSAMP 100 |
| 30 |
> |
#endif |
| 31 |
> |
/* limit on number of RBF lobes */ |
| 32 |
> |
static int lobe_lim = 15000; |
| 33 |
> |
/* progress bar length */ |
| 34 |
> |
static int do_prog = 79; |
| 35 |
|
|
| 36 |
+ |
|
| 37 |
+ |
/* Start new progress bar */ |
| 38 |
+ |
#define prog_start(s) if (do_prog) fprintf(stderr, "%s: %s...\n", progname, s); else |
| 39 |
+ |
|
| 40 |
+ |
/* Draw progress bar of the appropriate length */ |
| 41 |
+ |
static void |
| 42 |
+ |
prog_show(double frac) |
| 43 |
+ |
{ |
| 44 |
+ |
char pbar[256]; |
| 45 |
+ |
int nchars; |
| 46 |
+ |
|
| 47 |
+ |
if (do_prog <= 0) return; |
| 48 |
+ |
if (do_prog > sizeof(pbar)-2) |
| 49 |
+ |
do_prog = sizeof(pbar)-2; |
| 50 |
+ |
if (frac < 0) frac = 0; |
| 51 |
+ |
else if (frac > 1) frac = 1; |
| 52 |
+ |
nchars = do_prog*frac + .5; |
| 53 |
+ |
pbar[0] = '\r'; |
| 54 |
+ |
memset(pbar+1, '*', nchars); |
| 55 |
+ |
memset(pbar+1+nchars, '-', do_prog-nchars); |
| 56 |
+ |
pbar[do_prog+1] = '\0'; |
| 57 |
+ |
fputs(pbar, stderr); |
| 58 |
+ |
} |
| 59 |
+ |
|
| 60 |
+ |
/* Finish progress bar */ |
| 61 |
+ |
static void |
| 62 |
+ |
prog_done(void) |
| 63 |
+ |
{ |
| 64 |
+ |
int n = do_prog; |
| 65 |
+ |
|
| 66 |
+ |
if (n <= 1) return; |
| 67 |
+ |
fputc('\r', stderr); |
| 68 |
+ |
while (n--) |
| 69 |
+ |
fputc(' ', stderr); |
| 70 |
+ |
fputc('\r', stderr); |
| 71 |
+ |
} |
| 72 |
+ |
|
| 73 |
|
/* Output XML prologue to stdout */ |
| 74 |
|
static void |
| 75 |
|
xml_prologue(int ac, char *av[]) |
| 87 |
|
puts("<Optical>"); |
| 88 |
|
puts("<Layer>"); |
| 89 |
|
puts("\t<Material>"); |
| 90 |
< |
puts("\t\t<Name>Name</Name>"); |
| 91 |
< |
puts("\t\t<Manufacturer>Manufacturer</Manufacturer>"); |
| 90 |
> |
printf("\t\t<Name>%s</Name>\n", bsdf_name[0] ? bsdf_name : "Unknown"); |
| 91 |
> |
printf("\t\t<Manufacturer>%s</Manufacturer>\n", |
| 92 |
> |
bsdf_manuf[0] ? bsdf_manuf : "Unknown"); |
| 93 |
|
puts("\t\t<DeviceType>Other</DeviceType>"); |
| 94 |
|
puts("\t</Material>"); |
| 95 |
|
puts("\t<DataDefinition>"); |
| 158 |
|
{ |
| 159 |
|
const int sqres = 1<<samp_order; |
| 160 |
|
FILE *ofp = NULL; |
| 161 |
+ |
int assignD = 0; |
| 162 |
|
char cmd[128]; |
| 163 |
|
int ix, ox, oy; |
| 164 |
|
double iovec[6]; |
| 166 |
|
|
| 167 |
|
data_prologue(); /* begin output */ |
| 168 |
|
if (pctcull >= 0) { |
| 169 |
< |
sprintf(cmd, "rttree_reduce -h -a -ff -r 3 -t %f -g %d", |
| 169 |
> |
sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d", |
| 170 |
|
pctcull, samp_order); |
| 171 |
|
fflush(stdout); |
| 172 |
|
ofp = popen(cmd, "w"); |
| 176 |
|
exit(1); |
| 177 |
|
} |
| 178 |
|
SET_FILE_BINARY(ofp); |
| 179 |
+ |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
| 180 |
+ |
flockfile(ofp); |
| 181 |
+ |
#endif |
| 182 |
|
} else |
| 183 |
|
fputs("{\n", stdout); |
| 184 |
+ |
/* need to assign Dx, Dy, Dz? */ |
| 185 |
+ |
if (funame != NULL) |
| 186 |
+ |
assignD = (fundefined(funame) < 6); |
| 187 |
|
/* run through directions */ |
| 188 |
|
for (ix = 0; ix < sqres/2; ix++) { |
| 189 |
|
RBFNODE *rbf = NULL; |
| 191 |
|
iovec[1] = .0; |
| 192 |
|
iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]); |
| 193 |
|
if (funame == NULL) |
| 194 |
< |
rbf = advect_rbf(iovec); |
| 194 |
> |
rbf = advect_rbf(iovec, lobe_lim); |
| 195 |
|
for (ox = 0; ox < sqres; ox++) { |
| 196 |
|
float last_bsdf = -1; |
| 197 |
|
for (oy = 0; oy < sqres; oy++) { |
| 199 |
|
iovec[5] = output_orient * |
| 200 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
| 201 |
|
if (funame == NULL) |
| 202 |
< |
bsdf = eval_rbfrep(rbf, iovec+3) * |
| 150 |
< |
output_orient/iovec[5]; |
| 202 |
> |
bsdf = eval_rbfrep(rbf, iovec+3); |
| 203 |
|
else { |
| 204 |
< |
double ssa[3], ssvec[6], sum; |
| 205 |
< |
int ssi; |
| 204 |
> |
if (assignD) { |
| 205 |
> |
varset("Dx", '=', -iovec[3]); |
| 206 |
> |
varset("Dy", '=', -iovec[4]); |
| 207 |
> |
varset("Dz", '=', -iovec[5]); |
| 208 |
> |
++eclock; |
| 209 |
> |
} |
| 210 |
|
bsdf = funvalue(funame, 6, iovec); |
| 211 |
+ |
#if (NSSAMP > 0) |
| 212 |
|
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
| 213 |
< |
sum = 0; /* super-sample voxel */ |
| 214 |
< |
for (ssi = nssamp; ssi--; ) { |
| 215 |
< |
SDmultiSamp(ssa, 3, (ssi+drand48())/nssamp); |
| 213 |
> |
int ssi; |
| 214 |
> |
double ssa[3], ssvec[6], sum = 0; |
| 215 |
> |
/* super-sample voxel */ |
| 216 |
> |
for (ssi = NSSAMP; ssi--; ) { |
| 217 |
> |
SDmultiSamp(ssa, 3, (ssi+frandom()) * |
| 218 |
> |
(1./NSSAMP)); |
| 219 |
|
ssvec[0] = 2.*(ix+ssa[0])/sqres - 1.; |
| 220 |
|
ssvec[1] = .0; |
| 221 |
|
ssvec[2] = input_orient * |
| 225 |
|
ssvec[5] = output_orient * |
| 226 |
|
sqrt(1. - ssvec[3]*ssvec[3] - |
| 227 |
|
ssvec[4]*ssvec[4]); |
| 228 |
+ |
if (assignD) { |
| 229 |
+ |
varset("Dx", '=', -ssvec[3]); |
| 230 |
+ |
varset("Dy", '=', -ssvec[4]); |
| 231 |
+ |
varset("Dz", '=', -ssvec[5]); |
| 232 |
+ |
++eclock; |
| 233 |
+ |
} |
| 234 |
|
sum += funvalue(funame, 6, ssvec); |
| 235 |
|
} |
| 236 |
< |
bsdf = sum/nssamp; |
| 236 |
> |
bsdf = sum/NSSAMP; |
| 237 |
|
} |
| 238 |
+ |
#endif |
| 239 |
|
} |
| 240 |
|
if (pctcull >= 0) |
| 241 |
|
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
| 246 |
|
} |
| 247 |
|
if (rbf != NULL) |
| 248 |
|
free(rbf); |
| 249 |
+ |
prog_show((ix+1.)*(2./sqres)); |
| 250 |
|
} |
| 251 |
|
if (pctcull >= 0) { /* finish output */ |
| 252 |
|
if (pclose(ofp)) { |
| 260 |
|
fputs("}\n", stdout); |
| 261 |
|
} |
| 262 |
|
data_epilogue(); |
| 263 |
+ |
prog_done(); |
| 264 |
|
} |
| 265 |
|
|
| 266 |
|
/* Interpolate and output anisotropic BSDF data */ |
| 269 |
|
{ |
| 270 |
|
const int sqres = 1<<samp_order; |
| 271 |
|
FILE *ofp = NULL; |
| 272 |
+ |
int assignD = 0; |
| 273 |
|
char cmd[128]; |
| 274 |
|
int ix, iy, ox, oy; |
| 275 |
|
double iovec[6]; |
| 277 |
|
|
| 278 |
|
data_prologue(); /* begin output */ |
| 279 |
|
if (pctcull >= 0) { |
| 280 |
< |
sprintf(cmd, "rttree_reduce -h -a -ff -r 4 -t %f -g %d", |
| 280 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d", |
| 281 |
> |
(input_orient>0 ^ output_orient>0) ? "" : " -a", |
| 282 |
|
pctcull, samp_order); |
| 283 |
|
fflush(stdout); |
| 284 |
|
ofp = popen(cmd, "w"); |
| 287 |
|
progname); |
| 288 |
|
exit(1); |
| 289 |
|
} |
| 290 |
+ |
SET_FILE_BINARY(ofp); |
| 291 |
+ |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
| 292 |
+ |
flockfile(ofp); |
| 293 |
+ |
#endif |
| 294 |
|
} else |
| 295 |
|
fputs("{\n", stdout); |
| 296 |
+ |
/* need to assign Dx, Dy, Dz? */ |
| 297 |
+ |
if (funame != NULL) |
| 298 |
+ |
assignD = (fundefined(funame) < 6); |
| 299 |
|
/* run through directions */ |
| 300 |
|
for (ix = 0; ix < sqres; ix++) |
| 301 |
|
for (iy = 0; iy < sqres; iy++) { |
| 304 |
|
iovec[2] = input_orient * |
| 305 |
|
sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]); |
| 306 |
|
if (funame == NULL) |
| 307 |
< |
rbf = advect_rbf(iovec); |
| 307 |
> |
rbf = advect_rbf(iovec, lobe_lim); |
| 308 |
|
for (ox = 0; ox < sqres; ox++) { |
| 309 |
|
float last_bsdf = -1; |
| 310 |
|
for (oy = 0; oy < sqres; oy++) { |
| 312 |
|
iovec[5] = output_orient * |
| 313 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
| 314 |
|
if (funame == NULL) |
| 315 |
< |
bsdf = eval_rbfrep(rbf, iovec+3) * |
| 238 |
< |
output_orient/iovec[5]; |
| 315 |
> |
bsdf = eval_rbfrep(rbf, iovec+3); |
| 316 |
|
else { |
| 317 |
< |
double ssa[4], ssvec[6], sum; |
| 318 |
< |
int ssi; |
| 317 |
> |
if (assignD) { |
| 318 |
> |
varset("Dx", '=', -iovec[3]); |
| 319 |
> |
varset("Dy", '=', -iovec[4]); |
| 320 |
> |
varset("Dz", '=', -iovec[5]); |
| 321 |
> |
++eclock; |
| 322 |
> |
} |
| 323 |
|
bsdf = funvalue(funame, 6, iovec); |
| 324 |
+ |
#if (NSSAMP > 0) |
| 325 |
|
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
| 326 |
< |
sum = 0; /* super-sample voxel */ |
| 327 |
< |
for (ssi = nssamp; ssi--; ) { |
| 328 |
< |
SDmultiSamp(ssa, 4, (ssi+drand48())/nssamp); |
| 326 |
> |
int ssi; |
| 327 |
> |
double ssa[4], ssvec[6], sum = 0; |
| 328 |
> |
/* super-sample voxel */ |
| 329 |
> |
for (ssi = NSSAMP; ssi--; ) { |
| 330 |
> |
SDmultiSamp(ssa, 4, (ssi+frandom()) * |
| 331 |
> |
(1./NSSAMP)); |
| 332 |
|
SDsquare2disk(ssvec, 1.-(ix+ssa[0])/sqres, |
| 333 |
|
1.-(iy+ssa[1])/sqres); |
| 334 |
< |
ssvec[2] = output_orient * |
| 334 |
> |
ssvec[2] = input_orient * |
| 335 |
|
sqrt(1. - ssvec[0]*ssvec[0] - |
| 336 |
|
ssvec[1]*ssvec[1]); |
| 337 |
|
SDsquare2disk(ssvec+3, (ox+ssa[2])/sqres, |
| 339 |
|
ssvec[5] = output_orient * |
| 340 |
|
sqrt(1. - ssvec[3]*ssvec[3] - |
| 341 |
|
ssvec[4]*ssvec[4]); |
| 342 |
+ |
if (assignD) { |
| 343 |
+ |
varset("Dx", '=', -ssvec[3]); |
| 344 |
+ |
varset("Dy", '=', -ssvec[4]); |
| 345 |
+ |
varset("Dz", '=', -ssvec[5]); |
| 346 |
+ |
++eclock; |
| 347 |
+ |
} |
| 348 |
|
sum += funvalue(funame, 6, ssvec); |
| 349 |
|
} |
| 350 |
< |
bsdf = sum/nssamp; |
| 350 |
> |
bsdf = sum/NSSAMP; |
| 351 |
|
} |
| 352 |
+ |
#endif |
| 353 |
|
} |
| 354 |
|
if (pctcull >= 0) |
| 355 |
|
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
| 360 |
|
} |
| 361 |
|
if (rbf != NULL) |
| 362 |
|
free(rbf); |
| 363 |
+ |
prog_show((ix*sqres+iy+1.)/(sqres*sqres)); |
| 364 |
|
} |
| 365 |
|
if (pctcull >= 0) { /* finish output */ |
| 366 |
|
if (pclose(ofp)) { |
| 371 |
|
} else |
| 372 |
|
fputs("}\n", stdout); |
| 373 |
|
data_epilogue(); |
| 374 |
+ |
prog_done(); |
| 375 |
|
} |
| 376 |
|
|
| 377 |
|
/* Read in BSDF and interpolate as tensor tree representation */ |
| 418 |
|
case 'g': |
| 419 |
|
samp_order = atoi(argv[++i]); |
| 420 |
|
break; |
| 421 |
+ |
case 'l': |
| 422 |
+ |
lobe_lim = atoi(argv[++i]); |
| 423 |
+ |
break; |
| 424 |
+ |
case 'p': |
| 425 |
+ |
do_prog = atoi(argv[i]+2); |
| 426 |
+ |
break; |
| 427 |
|
default: |
| 428 |
|
goto userr; |
| 429 |
|
} |
| 430 |
|
if (single_plane_incident >= 0) { /* function-based BSDF? */ |
| 431 |
|
void (*evf)(char *s) = single_plane_incident ? |
| 432 |
|
&eval_isotropic : &eval_anisotropic; |
| 433 |
< |
if (i != argc-1 || fundefined(argv[i]) != 6) { |
| 433 |
> |
if (i != argc-1 || fundefined(argv[i]) < 3) { |
| 434 |
|
fprintf(stderr, |
| 435 |
|
"%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n", |
| 436 |
|
progname); |
| 437 |
+ |
fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n"); |
| 438 |
|
goto userr; |
| 439 |
|
} |
| 440 |
+ |
++eclock; |
| 441 |
|
xml_prologue(argc, argv); /* start XML output */ |
| 442 |
|
if (dofwd) { |
| 443 |
|
input_orient = -1; |
| 444 |
|
output_orient = -1; |
| 445 |
< |
(*evf)(argv[i]); /* outside reflectance */ |
| 445 |
> |
prog_start("Evaluating outside reflectance"); |
| 446 |
> |
(*evf)(argv[i]); |
| 447 |
|
output_orient = 1; |
| 448 |
< |
(*evf)(argv[i]); /* outside -> inside */ |
| 448 |
> |
prog_start("Evaluating outside->inside transmission"); |
| 449 |
> |
(*evf)(argv[i]); |
| 450 |
|
} |
| 451 |
|
if (dobwd) { |
| 452 |
|
input_orient = 1; |
| 453 |
|
output_orient = 1; |
| 454 |
< |
(*evf)(argv[i]); /* inside reflectance */ |
| 454 |
> |
prog_start("Evaluating inside reflectance"); |
| 455 |
> |
(*evf)(argv[i]); |
| 456 |
|
output_orient = -1; |
| 457 |
< |
(*evf)(argv[i]); /* inside -> outside */ |
| 457 |
> |
prog_start("Evaluating inside->outside transmission"); |
| 458 |
> |
(*evf)(argv[i]); |
| 459 |
|
} |
| 460 |
|
xml_epilogue(); /* finish XML output & exit */ |
| 461 |
|
return(0); |
| 463 |
|
if (i < argc) { /* open input files if given */ |
| 464 |
|
int nbsdf = 0; |
| 465 |
|
for ( ; i < argc; i++) { /* interpolate each component */ |
| 466 |
+ |
char pbuf[256]; |
| 467 |
|
FILE *fpin = fopen(argv[i], "rb"); |
| 468 |
|
if (fpin == NULL) { |
| 469 |
|
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
| 475 |
|
fclose(fpin); |
| 476 |
|
if (!nbsdf++) /* start XML on first dist. */ |
| 477 |
|
xml_prologue(argc, argv); |
| 478 |
+ |
sprintf(pbuf, "Interpolating component '%s'", argv[i]); |
| 479 |
+ |
prog_start(pbuf); |
| 480 |
|
if (single_plane_incident) |
| 481 |
|
eval_isotropic(NULL); |
| 482 |
|
else |
| 489 |
|
if (!load_bsdf_rep(stdin)) |
| 490 |
|
return(1); |
| 491 |
|
xml_prologue(argc, argv); /* start XML output */ |
| 492 |
+ |
prog_start("Interpolating from standard input"); |
| 493 |
|
if (single_plane_incident) /* resample dist. */ |
| 494 |
|
eval_isotropic(NULL); |
| 495 |
|
else |
| 498 |
|
return(0); |
| 499 |
|
userr: |
| 500 |
|
fprintf(stderr, |
| 501 |
< |
"Usage: %s [-g Nlog2][-t pctcull] [bsdf.sir ..] > bsdf.xml\n", |
| 501 |
> |
"Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
| 502 |
|
progname); |
| 503 |
|
fprintf(stderr, |
| 504 |
|
" or: %s -t{3|4} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |