19 |
|
#include "bsdf.h" |
20 |
|
#include "bsdf_m.h" |
21 |
|
|
22 |
– |
#ifndef FTINY |
23 |
– |
#define FTINY 1e-6 |
24 |
– |
#endif |
25 |
– |
|
22 |
|
/* Function return codes */ |
23 |
|
#define RC_GOOD 1 |
24 |
|
#define RC_FAIL 0 |
81 |
|
static int |
82 |
|
fequal(double a, double b) |
83 |
|
{ |
84 |
< |
if (b != .0) |
84 |
> |
if (b != 0) |
85 |
|
a = a/b - 1.; |
86 |
|
return (a <= 1e-6) & (a >= -1e-6); |
87 |
|
} |
88 |
|
|
89 |
< |
/* returns the name of the given tag */ |
89 |
> |
/* Returns the name of the given tag */ |
90 |
|
#ifdef ezxml_name |
91 |
|
#undef ezxml_name |
92 |
|
static char * |
98 |
|
} |
99 |
|
#endif |
100 |
|
|
101 |
< |
/* returns the given tag's character content or empty string if none */ |
101 |
> |
/* Returns the given tag's character content or empty string if none */ |
102 |
|
#ifdef ezxml_txt |
103 |
|
#undef ezxml_txt |
104 |
|
static char * |
157 |
|
/* Free a BSDF matrix */ |
158 |
|
#define SDfreeMatrix free |
159 |
|
|
160 |
< |
/* get vector for this angle basis index */ |
160 |
> |
/* get vector for this angle basis index (front exiting) */ |
161 |
|
static int |
162 |
< |
ab_getvec(FVECT v, int ndx, double randX, void *p) |
162 |
> |
fo_getvec(FVECT v, double ndxr, void *p) |
163 |
|
{ |
164 |
< |
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
164 |
> |
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
165 |
> |
int ndx = (int)ndxr; |
166 |
> |
double randX = ndxr - ndx; |
167 |
|
double rx[2]; |
168 |
|
int li; |
169 |
|
double pol, azi, d; |
170 |
|
|
171 |
< |
if ((ndx < 0) | (ndx >= ab->nangles)) |
171 |
> |
if ((ndxr < 0) | (ndx >= ab->nangles)) |
172 |
|
return RC_FAIL; |
173 |
|
for (li = 0; ndx >= ab->lat[li].nphis; li++) |
174 |
|
ndx -= ab->lat[li].nphis; |
183 |
|
return RC_GOOD; |
184 |
|
} |
185 |
|
|
186 |
< |
/* get index corresponding to the given vector */ |
186 |
> |
/* get index corresponding to the given vector (front exiting) */ |
187 |
|
static int |
188 |
< |
ab_getndx(const FVECT v, void *p) |
188 |
> |
fo_getndx(const FVECT v, void *p) |
189 |
|
{ |
190 |
< |
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
190 |
> |
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
191 |
|
int li, ndx; |
192 |
|
double pol, azi, d; |
193 |
|
|
194 |
|
if (v == NULL) |
195 |
|
return -1; |
196 |
< |
if ((v[2] < .0) | (v[2] > 1.0)) |
196 |
> |
if ((v[2] < 0) | (v[2] > 1.)) |
197 |
|
return -1; |
198 |
|
pol = 180.0/M_PI*acos(v[2]); |
199 |
|
azi = 180.0/M_PI*atan2(v[1], v[0]); |
212 |
|
/* compute square of real value */ |
213 |
|
static double sq(double x) { return x*x; } |
214 |
|
|
215 |
< |
/* get projected solid angle for this angle basis index */ |
215 |
> |
/* get projected solid angle for this angle basis index (universal) */ |
216 |
|
static double |
217 |
< |
ab_getohm(int ndx, void *p) |
217 |
> |
io_getohm(int ndx, void *p) |
218 |
|
{ |
219 |
|
static int last_li = -1; |
220 |
|
static double last_ohm; |
237 |
|
(double)ab->lat[li].nphis; |
238 |
|
} |
239 |
|
|
240 |
< |
/* get reverse vector for this angle basis index */ |
240 |
> |
/* get vector for this angle basis index (back incident) */ |
241 |
|
static int |
242 |
< |
ab_getvecR(FVECT v, int ndx, double randX, void *p) |
242 |
> |
bi_getvec(FVECT v, double ndxr, void *p) |
243 |
|
{ |
244 |
< |
int na = (*(ANGLE_BASIS *)p).nangles; |
247 |
< |
|
248 |
< |
if (!ab_getvec(v, ndx, randX, p)) |
244 |
> |
if (!fo_getvec(v, ndxr, p)) |
245 |
|
return RC_FAIL; |
246 |
|
|
247 |
|
v[0] = -v[0]; |
251 |
|
return RC_GOOD; |
252 |
|
} |
253 |
|
|
254 |
< |
/* get index corresponding to the reverse vector */ |
254 |
> |
/* get index corresponding to the vector (back incident) */ |
255 |
|
static int |
256 |
< |
ab_getndxR(const FVECT v, void *p) |
256 |
> |
bi_getndx(const FVECT v, void *p) |
257 |
|
{ |
258 |
|
FVECT v2; |
259 |
|
|
261 |
|
v2[1] = -v[1]; |
262 |
|
v2[2] = -v[2]; |
263 |
|
|
264 |
< |
return ab_getndx(v2, p); |
264 |
> |
return fo_getndx(v2, p); |
265 |
|
} |
266 |
|
|
267 |
+ |
/* get vector for this angle basis index (back exiting) */ |
268 |
+ |
static int |
269 |
+ |
bo_getvec(FVECT v, double ndxr, void *p) |
270 |
+ |
{ |
271 |
+ |
if (!fo_getvec(v, ndxr, p)) |
272 |
+ |
return RC_FAIL; |
273 |
+ |
|
274 |
+ |
v[2] = -v[2]; |
275 |
+ |
|
276 |
+ |
return RC_GOOD; |
277 |
+ |
} |
278 |
+ |
|
279 |
+ |
/* get index corresponding to the vector (back exiting) */ |
280 |
+ |
static int |
281 |
+ |
bo_getndx(const FVECT v, void *p) |
282 |
+ |
{ |
283 |
+ |
FVECT v2; |
284 |
+ |
|
285 |
+ |
v2[0] = v[0]; |
286 |
+ |
v2[1] = v[1]; |
287 |
+ |
v2[2] = -v[2]; |
288 |
+ |
|
289 |
+ |
return fo_getndx(v2, p); |
290 |
+ |
} |
291 |
+ |
|
292 |
+ |
/* get vector for this angle basis index (front incident) */ |
293 |
+ |
static int |
294 |
+ |
fi_getvec(FVECT v, double ndxr, void *p) |
295 |
+ |
{ |
296 |
+ |
if (!fo_getvec(v, ndxr, p)) |
297 |
+ |
return RC_FAIL; |
298 |
+ |
|
299 |
+ |
v[0] = -v[0]; |
300 |
+ |
v[1] = -v[1]; |
301 |
+ |
|
302 |
+ |
return RC_GOOD; |
303 |
+ |
} |
304 |
+ |
|
305 |
+ |
/* get index corresponding to the vector (front incident) */ |
306 |
+ |
static int |
307 |
+ |
fi_getndx(const FVECT v, void *p) |
308 |
+ |
{ |
309 |
+ |
FVECT v2; |
310 |
+ |
|
311 |
+ |
v2[0] = -v[0]; |
312 |
+ |
v2[1] = -v[1]; |
313 |
+ |
v2[2] = v[2]; |
314 |
+ |
|
315 |
+ |
return fo_getndx(v2, p); |
316 |
+ |
} |
317 |
+ |
|
318 |
|
/* load custom BSDF angle basis */ |
319 |
|
static int |
320 |
|
load_angle_basis(ezxml_t wab) |
352 |
|
"ThetaBounds"), "LowerTheta"))), |
353 |
|
abase_list[nabases].lat[i].tmin)) { |
354 |
|
sprintf(SDerrorDetail, "Theta values disagree in '%s'", |
355 |
< |
abname); |
355 |
> |
abname); |
356 |
|
return RC_DATERR; |
357 |
|
} |
358 |
|
abase_list[nabases].nangles += |
362 |
|
(abase_list[nabases].lat[i].nphis == 1 && |
363 |
|
abase_list[nabases].lat[i].tmin > FTINY)) { |
364 |
|
sprintf(SDerrorDetail, "Illegal phi count in '%s'", |
365 |
< |
abname); |
365 |
> |
abname); |
366 |
|
return RC_DATERR; |
367 |
|
} |
368 |
|
} |
397 |
|
} |
398 |
|
free(ohma); |
399 |
|
/* need incoming solid angles, too? */ |
400 |
< |
if (dp->ninc < dp->nout || dp->ib_ohm != dp->ob_ohm || |
354 |
< |
dp->ib_priv != dp->ob_priv) { |
400 |
> |
if ((dp->ib_ohm != dp->ob_ohm) | (dp->ib_priv != dp->ob_priv)) { |
401 |
|
double ohm; |
402 |
|
for (i = dp->ninc; i--; ) |
403 |
|
if ((ohm = mBSDF_incohm(dp,i)) < df->minProjSA) |
410 |
|
static int |
411 |
|
load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc) |
412 |
|
{ |
367 |
– |
char *cbasis = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis")); |
368 |
– |
char *rbasis = ezxml_txt(ezxml_child(wdb,"RowAngleBasis")); |
413 |
|
SDSpectralDF *df; |
414 |
|
SDMat *dp; |
415 |
|
char *sdata; |
416 |
+ |
int tfront; |
417 |
|
int inbi, outbi; |
418 |
|
int i; |
374 |
– |
|
375 |
– |
if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) { |
376 |
– |
sprintf(SDerrorDetail, "Missing column/row basis for BSDF '%s'", |
377 |
– |
sd->name); |
378 |
– |
return RC_FORMERR; |
379 |
– |
} |
419 |
|
/* allocate BSDF component */ |
420 |
|
sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection")); |
421 |
< |
if (!strcasecmp(sdata, "Transmission Front")) { |
421 |
> |
/* |
422 |
> |
* Remember that front and back are reversed from WINDOW 6 orientations |
423 |
> |
* Favor their "Front" (incoming light) since that's more often valid |
424 |
> |
*/ |
425 |
> |
tfront = !strcasecmp(sdata, "Transmission Back"); |
426 |
> |
if (!strcasecmp(sdata, "Transmission Front") || |
427 |
> |
tfront & (sd->tf == NULL)) { |
428 |
|
if (sd->tf != NULL) |
429 |
|
SDfreeSpectralDF(sd->tf); |
430 |
|
if ((sd->tf = SDnewSpectralDF(1)) == NULL) |
431 |
|
return RC_MEMERR; |
432 |
|
df = sd->tf; |
433 |
|
} else if (!strcasecmp(sdata, "Reflection Front")) { |
434 |
< |
if (sd->rf != NULL) |
390 |
< |
SDfreeSpectralDF(sd->rf); |
391 |
< |
if ((sd->rf = SDnewSpectralDF(1)) == NULL) |
392 |
< |
return RC_MEMERR; |
393 |
< |
df = sd->rf; |
394 |
< |
} else if (!strcasecmp(sdata, "Reflection Back")) { |
395 |
< |
if (sd->rb != NULL) |
434 |
> |
if (sd->rb != NULL) /* note back-front reversal */ |
435 |
|
SDfreeSpectralDF(sd->rb); |
436 |
|
if ((sd->rb = SDnewSpectralDF(1)) == NULL) |
437 |
|
return RC_MEMERR; |
438 |
|
df = sd->rb; |
439 |
+ |
} else if (!strcasecmp(sdata, "Reflection Back")) { |
440 |
+ |
if (sd->rf != NULL) /* note front-back reversal */ |
441 |
+ |
SDfreeSpectralDF(sd->rf); |
442 |
+ |
if ((sd->rf = SDnewSpectralDF(1)) == NULL) |
443 |
+ |
return RC_MEMERR; |
444 |
+ |
df = sd->rf; |
445 |
|
} else |
446 |
|
return RC_FAIL; |
447 |
+ |
/* XXX should also check "ScatteringDataType" for consistency? */ |
448 |
|
/* get angle bases */ |
449 |
|
sdata = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis")); |
450 |
|
if (!sdata || !*sdata) { |
453 |
|
return RC_FORMERR; |
454 |
|
} |
455 |
|
for (inbi = nabases; inbi--; ) |
456 |
< |
if (!strcasecmp(cbasis, abase_list[inbi].name)) |
456 |
> |
if (!strcasecmp(sdata, abase_list[inbi].name)) |
457 |
|
break; |
458 |
|
if (inbi < 0) { |
459 |
< |
sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'", |
414 |
< |
cbasis); |
459 |
> |
sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'", sdata); |
460 |
|
return RC_FORMERR; |
461 |
|
} |
462 |
|
sdata = ezxml_txt(ezxml_child(wdb,"RowAngleBasis")); |
466 |
|
return RC_FORMERR; |
467 |
|
} |
468 |
|
for (outbi = nabases; outbi--; ) |
469 |
< |
if (!strcasecmp(rbasis, abase_list[outbi].name)) |
469 |
> |
if (!strcasecmp(sdata, abase_list[outbi].name)) |
470 |
|
break; |
471 |
|
if (outbi < 0) { |
472 |
< |
sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", cbasis); |
472 |
> |
sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", sdata); |
473 |
|
return RC_FORMERR; |
474 |
|
} |
475 |
|
/* allocate BSDF matrix */ |
476 |
|
dp = SDnewMatrix(abase_list[inbi].nangles, abase_list[outbi].nangles); |
477 |
|
if (dp == NULL) |
478 |
|
return RC_MEMERR; |
479 |
< |
dp->ib_priv = (void *)&abase_list[inbi]; |
480 |
< |
dp->ob_priv = (void *)&abase_list[outbi]; |
479 |
> |
dp->ib_priv = &abase_list[inbi]; |
480 |
> |
dp->ob_priv = &abase_list[outbi]; |
481 |
|
if (df == sd->tf) { |
482 |
< |
dp->ib_vec = ab_getvecR; |
483 |
< |
dp->ib_ndx = ab_getndxR; |
484 |
< |
dp->ob_vec = ab_getvec; |
485 |
< |
dp->ob_ndx = ab_getndx; |
482 |
> |
if (tfront) { |
483 |
> |
dp->ib_vec = &fi_getvec; |
484 |
> |
dp->ib_ndx = &fi_getndx; |
485 |
> |
dp->ob_vec = &bo_getvec; |
486 |
> |
dp->ob_ndx = &bo_getndx; |
487 |
> |
} else { |
488 |
> |
dp->ib_vec = &bi_getvec; |
489 |
> |
dp->ib_ndx = &bi_getndx; |
490 |
> |
dp->ob_vec = &fo_getvec; |
491 |
> |
dp->ob_ndx = &fo_getndx; |
492 |
> |
} |
493 |
|
} else if (df == sd->rf) { |
494 |
< |
dp->ib_vec = ab_getvec; |
495 |
< |
dp->ib_ndx = ab_getndx; |
496 |
< |
dp->ob_vec = ab_getvec; |
497 |
< |
dp->ob_ndx = ab_getndx; |
494 |
> |
dp->ib_vec = &fi_getvec; |
495 |
> |
dp->ib_ndx = &fi_getndx; |
496 |
> |
dp->ob_vec = &fo_getvec; |
497 |
> |
dp->ob_ndx = &fo_getndx; |
498 |
|
} else /* df == sd->rb */ { |
499 |
< |
dp->ib_vec = ab_getvecR; |
500 |
< |
dp->ib_ndx = ab_getndxR; |
501 |
< |
dp->ob_vec = ab_getvecR; |
502 |
< |
dp->ob_ndx = ab_getndxR; |
499 |
> |
dp->ib_vec = &bi_getvec; |
500 |
> |
dp->ib_ndx = &bi_getndx; |
501 |
> |
dp->ob_vec = &bo_getvec; |
502 |
> |
dp->ob_ndx = &bo_getndx; |
503 |
|
} |
504 |
< |
dp->ib_ohm = ab_getohm; |
505 |
< |
dp->ob_ohm = ab_getohm; |
504 |
> |
dp->ib_ohm = &io_getohm; |
505 |
> |
dp->ob_ohm = &io_getohm; |
506 |
|
df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */ |
507 |
|
df->comp[0].dist = dp; |
508 |
|
df->comp[0].func = &SDhandleMtx; |
509 |
|
/* read BSDF data */ |
510 |
< |
sdata = ezxml_txt(ezxml_child(wdb,"ScatteringData")); |
510 |
> |
sdata = ezxml_txt(ezxml_child(wdb,"ScatteringData")); |
511 |
|
if (!sdata || !*sdata) { |
512 |
|
sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'", |
513 |
|
sd->name); |
571 |
|
c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]); |
572 |
|
dv->cieY += ymin; |
573 |
|
} |
574 |
< |
df->maxHemi -= dv->cieY; /* correct minimum hemispherical */ |
575 |
< |
dv->spec.clock++; /* make sure everything is set */ |
574 |
> |
df->maxHemi -= dv->cieY; /* adjust minimum hemispherical */ |
575 |
> |
/* make sure everything is set */ |
576 |
|
c_ccvt(&dv->spec, C_CSXY+C_CSSPEC); |
577 |
|
} |
578 |
|
|
579 |
|
/* Load a BSDF matrix from an open XML file */ |
580 |
|
SDError |
581 |
< |
SDloadMtx(SDData *sd, ezxml_t fl) |
581 |
> |
SDloadMtx(SDData *sd, ezxml_t wtl) |
582 |
|
{ |
583 |
< |
ezxml_t wtl, wld, wdb; |
583 |
> |
ezxml_t wld, wdb; |
584 |
|
int rowIn; |
585 |
|
struct BSDF_data *dp; |
586 |
|
char *txt; |
587 |
|
int rval; |
588 |
|
|
589 |
< |
if (strcmp(ezxml_name(fl), "WindowElement")) { |
589 |
> |
txt = ezxml_txt(ezxml_child(ezxml_child(wtl, |
590 |
> |
"DataDefinition"), "IncidentDataStructure")); |
591 |
> |
if (txt == NULL || !*txt) { |
592 |
|
sprintf(SDerrorDetail, |
593 |
< |
"BSDF \"%s\": top level node not 'WindowElement'", |
593 |
> |
"BSDF \"%s\": missing IncidentDataStructure", |
594 |
|
sd->name); |
595 |
|
return SDEformat; |
596 |
|
} |
543 |
– |
wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer"); |
544 |
– |
txt = ezxml_txt(ezxml_child(ezxml_child(wtl, |
545 |
– |
"DataDefinition"), "IncidentDataStructure")); |
597 |
|
if (!strcasecmp(txt, "Rows")) |
598 |
|
rowIn = 1; |
599 |
|
else if (!strcasecmp(txt, "Columns")) |
608 |
|
rval = load_angle_basis(ezxml_child(ezxml_child(wtl, |
609 |
|
"DataDefinition"), "AngleBasis")); |
610 |
|
if (rval < 0) |
611 |
< |
goto err_return; |
611 |
> |
return convert_errcode(rval); |
612 |
|
/* load BSDF components */ |
613 |
|
for (wld = ezxml_child(wtl, "WavelengthData"); |
614 |
|
wld != NULL; wld = wld->next) { |
618 |
|
for (wdb = ezxml_child(wld, "WavelengthDataBlock"); |
619 |
|
wdb != NULL; wdb = wdb->next) |
620 |
|
if ((rval = load_bsdf_data(sd, wdb, rowIn)) < 0) |
621 |
< |
goto err_return; |
621 |
> |
return convert_errcode(rval); |
622 |
|
} |
623 |
|
/* separate diffuse components */ |
624 |
|
extract_diffuse(&sd->rLambFront, sd->rf); |
626 |
|
extract_diffuse(&sd->tLamb, sd->tf); |
627 |
|
/* return success */ |
628 |
|
return SDEnone; |
578 |
– |
err_return: /* jump here on failure */ |
579 |
– |
if (sd->rf != NULL) { |
580 |
– |
SDfreeSpectralDF(sd->rf); |
581 |
– |
sd->rf = NULL; |
582 |
– |
} |
583 |
– |
if (sd->rb != NULL) { |
584 |
– |
SDfreeSpectralDF(sd->rb); |
585 |
– |
sd->rb = NULL; |
586 |
– |
} |
587 |
– |
if (sd->tf != NULL) { |
588 |
– |
SDfreeSpectralDF(sd->tf); |
589 |
– |
sd->tf = NULL; |
590 |
– |
} |
591 |
– |
return convert_errcode(rval); |
629 |
|
} |
630 |
|
|
631 |
|
/* Get Matrix BSDF value */ |
632 |
|
static int |
633 |
|
SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec, |
634 |
< |
const FVECT inVec, const void *dist) |
634 |
> |
const FVECT inVec, SDComponent *sdc) |
635 |
|
{ |
636 |
< |
const SDMat *dp = (const SDMat *)dist; |
636 |
> |
const SDMat *dp; |
637 |
|
int i_ndx, o_ndx; |
638 |
+ |
/* check arguments */ |
639 |
+ |
if ((coef == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL) |
640 |
+ |
|| (dp = (SDMat *)sdc->dist) == NULL) |
641 |
+ |
return 0; |
642 |
|
/* get angle indices */ |
643 |
|
i_ndx = mBSDF_incndx(dp, inVec); |
644 |
|
o_ndx = mBSDF_outndx(dp, outVec); |
653 |
|
return 1; /* XXX monochrome for now */ |
654 |
|
} |
655 |
|
|
656 |
< |
/* Query solid angle for vector */ |
656 |
> |
/* Query solid angle for vector(s) */ |
657 |
|
static SDError |
658 |
< |
SDqueryMtxProjSA(double *psa, const FVECT vec, int qflags, const void *dist) |
658 |
> |
SDqueryMtxProjSA(double *psa, const FVECT v1, const RREAL *v2, |
659 |
> |
int qflags, SDComponent *sdc) |
660 |
|
{ |
661 |
< |
const SDMat *dp = (const SDMat *)dist; |
662 |
< |
|
663 |
< |
if (!(qflags & SDqueryInc+SDqueryOut)) |
661 |
> |
const SDMat *dp; |
662 |
> |
double inc_psa, out_psa; |
663 |
> |
/* check arguments */ |
664 |
> |
if ((psa == NULL) | (v1 == NULL) | (sdc == NULL) || |
665 |
> |
(dp = (SDMat *)sdc->dist) == NULL) |
666 |
|
return SDEargument; |
667 |
< |
if (qflags & SDqueryInc) { |
668 |
< |
double inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, vec)); |
669 |
< |
if (inc_psa < .0) |
670 |
< |
return SDEinternal; |
671 |
< |
switch (qflags & SDqueryMin+SDqueryMax) { |
672 |
< |
case SDqueryMax: |
673 |
< |
if (inc_psa > psa[0]) |
674 |
< |
psa[0] = inc_psa; |
675 |
< |
break; |
676 |
< |
case SDqueryMin+SDqueryMax: |
677 |
< |
if (inc_psa > psa[1]) |
678 |
< |
psa[1] = inc_psa; |
679 |
< |
/* fall through */ |
636 |
< |
case SDqueryMin: |
637 |
< |
if (inc_psa < psa[0]) |
638 |
< |
psa[0] = inc_psa; |
639 |
< |
break; |
640 |
< |
case 0: |
667 |
> |
if (v2 == NULL) |
668 |
> |
v2 = v1; |
669 |
> |
/* get projected solid angles */ |
670 |
> |
out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v1)); |
671 |
> |
inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v2)); |
672 |
> |
if ((v1 != v2) & (out_psa <= 0) & (inc_psa <= 0)) { |
673 |
> |
inc_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v2)); |
674 |
> |
out_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v1)); |
675 |
> |
} |
676 |
> |
|
677 |
> |
switch (qflags) { /* record based on flag settings */ |
678 |
> |
case SDqueryMax: |
679 |
> |
if (inc_psa > psa[0]) |
680 |
|
psa[0] = inc_psa; |
681 |
< |
break; |
682 |
< |
} |
681 |
> |
if (out_psa > psa[0]) |
682 |
> |
psa[0] = out_psa; |
683 |
> |
break; |
684 |
> |
case SDqueryMin+SDqueryMax: |
685 |
> |
if (inc_psa > psa[1]) |
686 |
> |
psa[1] = inc_psa; |
687 |
> |
if (out_psa > psa[1]) |
688 |
> |
psa[1] = out_psa; |
689 |
> |
/* fall through */ |
690 |
> |
case SDqueryVal: |
691 |
> |
if (qflags == SDqueryVal) |
692 |
> |
psa[0] = M_PI; |
693 |
> |
/* fall through */ |
694 |
> |
case SDqueryMin: |
695 |
> |
if ((inc_psa > 0) & (inc_psa < psa[0])) |
696 |
> |
psa[0] = inc_psa; |
697 |
> |
if ((out_psa > 0) & (out_psa < psa[0])) |
698 |
> |
psa[0] = out_psa; |
699 |
> |
break; |
700 |
|
} |
701 |
< |
if (qflags & SDqueryOut) { |
702 |
< |
double out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, vec)); |
647 |
< |
if (out_psa < .0) |
648 |
< |
return SDEinternal; |
649 |
< |
switch (qflags & SDqueryMin+SDqueryMax) { |
650 |
< |
case SDqueryMax: |
651 |
< |
if (out_psa > psa[0]) |
652 |
< |
psa[0] = out_psa; |
653 |
< |
break; |
654 |
< |
case SDqueryMin+SDqueryMax: |
655 |
< |
if (out_psa > psa[1]) |
656 |
< |
psa[1] = out_psa; |
657 |
< |
/* fall through */ |
658 |
< |
case SDqueryMin: |
659 |
< |
if (out_psa < psa[0]) |
660 |
< |
psa[0] = out_psa; |
661 |
< |
break; |
662 |
< |
case 0: |
663 |
< |
psa[(qflags&SDqueryInc)!=0] = out_psa; |
664 |
< |
break; |
665 |
< |
} |
666 |
< |
} |
667 |
< |
return SDEnone; |
701 |
> |
/* make sure it's legal */ |
702 |
> |
return (psa[0] <= 0) ? SDEinternal : SDEnone; |
703 |
|
} |
704 |
|
|
705 |
|
/* Compute new cumulative distribution from BSDF */ |
737 |
|
static const SDCDst * |
738 |
|
SDgetMtxCDist(const FVECT inVec, SDComponent *sdc) |
739 |
|
{ |
740 |
< |
SDMat *dp = (SDMat *)sdc->dist; |
740 |
> |
SDMat *dp; |
741 |
|
int reverse; |
742 |
|
SDMatCDst myCD; |
743 |
|
SDMatCDst *cd, *cdlast; |
744 |
< |
|
745 |
< |
if (dp == NULL) |
744 |
> |
/* check arguments */ |
745 |
> |
if ((inVec == NULL) | (sdc == NULL) || |
746 |
> |
(dp = (SDMat *)sdc->dist) == NULL) |
747 |
|
return NULL; |
748 |
|
memset(&myCD, 0, sizeof(myCD)); |
749 |
|
myCD.indx = mBSDF_incndx(dp, inVec); |
762 |
|
reverse = 1; |
763 |
|
} |
764 |
|
cdlast = NULL; /* check for it in cache list */ |
765 |
< |
for (cd = (SDMatCDst *)sdc->cdList; |
766 |
< |
cd != NULL; cd = (SDMatCDst *)cd->next) { |
765 |
> |
for (cd = (SDMatCDst *)sdc->cdList; cd != NULL; |
766 |
> |
cdlast = cd, cd = (SDMatCDst *)cd->next) |
767 |
|
if (cd->indx == myCD.indx && (cd->calen == myCD.calen) & |
768 |
|
(cd->ob_priv == myCD.ob_priv) & |
769 |
|
(cd->ob_vec == myCD.ob_vec)) |
770 |
|
break; |
735 |
– |
cdlast = cd; |
736 |
– |
} |
771 |
|
if (cd == NULL) { /* need to allocate new entry */ |
772 |
|
cd = (SDMatCDst *)malloc(sizeof(SDMatCDst) + |
773 |
< |
myCD.calen*sizeof(myCD.carr[0])); |
773 |
> |
sizeof(myCD.carr[0])*myCD.calen); |
774 |
|
if (cd == NULL) |
775 |
|
return NULL; |
776 |
|
*cd = myCD; /* compute cumulative distribution */ |
790 |
|
|
791 |
|
/* Sample cumulative distribution */ |
792 |
|
static SDError |
793 |
< |
SDsampMtxCDist(FVECT outVec, double randX, const SDCDst *cdp) |
793 |
> |
SDsampMtxCDist(FVECT ioVec, double randX, const SDCDst *cdp) |
794 |
|
{ |
795 |
|
const unsigned maxval = ~0; |
796 |
|
const SDMatCDst *mcd = (const SDMatCDst *)cdp; |
797 |
|
const unsigned target = randX*maxval; |
798 |
|
int i, iupper, ilower; |
799 |
+ |
/* check arguments */ |
800 |
+ |
if ((ioVec == NULL) | (mcd == NULL)) |
801 |
+ |
return SDEargument; |
802 |
|
/* binary search to find index */ |
803 |
|
ilower = 0; iupper = mcd->calen; |
804 |
|
while ((i = (iupper + ilower) >> 1) != ilower) |
810 |
|
randX = (randX*maxval - mcd->carr[ilower]) / |
811 |
|
(double)(mcd->carr[iupper] - mcd->carr[ilower]); |
812 |
|
/* convert index to vector */ |
813 |
< |
if ((*mcd->ob_vec)(outVec, i, randX, mcd->ob_priv)) |
813 |
> |
if ((*mcd->ob_vec)(ioVec, i+randX, mcd->ob_priv)) |
814 |
|
return SDEnone; |
815 |
< |
strcpy(SDerrorDetail, "BSDF sampling fault"); |
815 |
> |
strcpy(SDerrorDetail, "Matrix BSDF sampling fault"); |
816 |
|
return SDEinternal; |
817 |
|
} |
818 |
|
|