322 |
|
int nbadohm = 0; |
323 |
|
int nneg = 0; |
324 |
|
int r, c; |
325 |
< |
/* reciprocity is "transparent" */ |
325 |
> |
/* loop over incident angles */ |
326 |
|
for (c = 0; c < cm->ncols; c++) { |
327 |
|
const double dom = mBSDF_incohm(bsdf,c); |
328 |
|
/* projected solid angle */ |
351 |
|
return(cm); |
352 |
|
} |
353 |
|
|
354 |
+ |
/* Convert between input and output indices for reciprocity */ |
355 |
+ |
static int |
356 |
+ |
recip_out_from_in(const SDMat *bsdf, int in_recip) |
357 |
+ |
{ |
358 |
+ |
FVECT v; |
359 |
+ |
|
360 |
+ |
if (!mBSDF_incvec(v, bsdf, in_recip+.5)) |
361 |
+ |
return(in_recip); /* XXX should be error! */ |
362 |
+ |
v[2] = -v[2]; |
363 |
+ |
return(mBSDF_outndx(bsdf, v)); |
364 |
+ |
} |
365 |
+ |
|
366 |
+ |
/* Convert between output and input indices for reciprocity */ |
367 |
+ |
static int |
368 |
+ |
recip_in_from_out(const SDMat *bsdf, int out_recip) |
369 |
+ |
{ |
370 |
+ |
FVECT v; |
371 |
+ |
|
372 |
+ |
if (!mBSDF_outvec(v, bsdf, out_recip+.5)) |
373 |
+ |
return(out_recip); /* XXX should be error! */ |
374 |
+ |
v[2] = -v[2]; |
375 |
+ |
return(mBSDF_incndx(bsdf, v)); |
376 |
+ |
} |
377 |
+ |
|
378 |
+ |
/* Convert a BSDF to our matrix representation, applying reciprocity */ |
379 |
+ |
static CMATRIX * |
380 |
+ |
cm_bsdf_recip(const COLOR bsdfLamb, const COLOR specCol, const SDMat *bsdf) |
381 |
+ |
{ |
382 |
+ |
CMATRIX *cm = cm_alloc(bsdf->ninc, bsdf->nout); |
383 |
+ |
int nbadohm = 0; |
384 |
+ |
int nneg = 0; |
385 |
+ |
int r, c; |
386 |
+ |
/* loop over incident angles */ |
387 |
+ |
for (c = 0; c < cm->ncols; c++) { |
388 |
+ |
const int ro = recip_out_from_in(bsdf,c); |
389 |
+ |
const double dom = mBSDF_outohm(bsdf,ro); |
390 |
+ |
/* projected solid angle */ |
391 |
+ |
nbadohm += (dom <= 0); |
392 |
+ |
|
393 |
+ |
for (r = 0; r < cm->nrows; r++) { |
394 |
+ |
const int ri = recip_in_from_out(bsdf,r); |
395 |
+ |
float f = mBSDF_value(bsdf,ri,ro); |
396 |
+ |
COLORV *mp = cm_lval(cm,r,c); |
397 |
+ |
/* check BSDF value */ |
398 |
+ |
if ((f <= 0) | (dom <= 0)) { |
399 |
+ |
nneg += (f < -FTINY); |
400 |
+ |
f = .0f; |
401 |
+ |
} |
402 |
+ |
copycolor(mp, specCol); |
403 |
+ |
scalecolor(mp, f); |
404 |
+ |
addcolor(mp, bsdfLamb); |
405 |
+ |
scalecolor(mp, dom); |
406 |
+ |
} |
407 |
+ |
} |
408 |
+ |
if (nneg | nbadohm) { |
409 |
+ |
sprintf(errmsg, |
410 |
+ |
"BTDF has %d negatives and %d bad incoming solid angles", |
411 |
+ |
nneg, nbadohm); |
412 |
+ |
error(WARNING, errmsg); |
413 |
+ |
} |
414 |
+ |
return(cm); |
415 |
+ |
} |
416 |
+ |
|
417 |
|
/* Load and convert a matrix BSDF from the given XML file */ |
418 |
|
static CMATRIX * |
419 |
|
cm_loadBSDF(char *fname, COLOR cLamb) |
420 |
|
{ |
421 |
< |
CMATRIX *Tmat; |
422 |
< |
char *fpath; |
423 |
< |
SDError ec; |
424 |
< |
SDData myBSDF; |
425 |
< |
COLOR bsdfLamb, specCol; |
421 |
> |
CMATRIX *Tmat; |
422 |
> |
char *fpath; |
423 |
> |
int recip; |
424 |
> |
SDError ec; |
425 |
> |
SDData myBSDF; |
426 |
> |
SDSpectralDF *tdf; |
427 |
> |
COLOR bsdfLamb, specCol; |
428 |
|
/* find path to BSDF file */ |
429 |
|
fpath = getpath(fname, getrlibpath(), R_OK); |
430 |
|
if (fpath == NULL) { |
436 |
|
if (ec) |
437 |
|
error(USER, transSDError(ec)); |
438 |
|
ccy2rgb(&myBSDF.tLamb.spec, myBSDF.tLamb.cieY/PI, bsdfLamb); |
439 |
< |
if (myBSDF.tf == NULL) { /* no non-Lambertian transmission? */ |
439 |
> |
recip = (myBSDF.tf == NULL); |
440 |
> |
tdf = recip ? myBSDF.tb : myBSDF.tf; |
441 |
> |
if (tdf == NULL) { /* no non-Lambertian transmission? */ |
442 |
|
if (cLamb != NULL) |
443 |
|
copycolor(cLamb, bsdfLamb); |
444 |
|
SDfreeBSDF(&myBSDF); |
445 |
|
return(NULL); |
446 |
|
} |
447 |
< |
if (myBSDF.tf->ncomp != 1 || myBSDF.tf->comp[0].func != &SDhandleMtx) { |
447 |
> |
if (tdf->ncomp != 1 || tdf->comp[0].func != &SDhandleMtx) { |
448 |
|
sprintf(errmsg, "unsupported BSDF '%s'", fpath); |
449 |
|
error(USER, errmsg); |
450 |
|
} |
451 |
|
/* convert BTDF to matrix */ |
452 |
< |
ccy2rgb(&myBSDF.tf->comp[0].cspec[0], 1., specCol); |
453 |
< |
Tmat = cm_bsdf(bsdfLamb, specCol, (SDMat *)myBSDF.tf->comp[0].dist); |
452 |
> |
ccy2rgb(&tdf->comp[0].cspec[0], 1., specCol); |
453 |
> |
Tmat = recip ? cm_bsdf_recip(bsdfLamb, specCol, (SDMat *)tdf->comp[0].dist) |
454 |
> |
: cm_bsdf(bsdfLamb, specCol, (SDMat *)tdf->comp[0].dist); |
455 |
|
if (cLamb != NULL) /* Lambertian is included */ |
456 |
|
setcolor(cLamb, .0, .0, .0); |
457 |
|
/* free BSDF and return */ |