11 |
|
* |
12 |
|
*/ |
13 |
|
|
14 |
< |
#include <stdio.h> |
14 |
> |
#include "rtio.h" |
15 |
|
#include <stdlib.h> |
16 |
|
#include <math.h> |
17 |
– |
#include <strings.h> |
17 |
|
#include <ctype.h> |
18 |
|
#include "ezxml.h" |
19 |
|
#include "bsdf.h" |
20 |
|
#include "bsdf_m.h" |
21 |
|
|
23 |
– |
#ifndef FTINY |
24 |
– |
#define FTINY 1e-6 |
25 |
– |
#endif |
26 |
– |
|
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; |
248 |
< |
|
249 |
< |
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 || |
355 |
< |
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) |
406 |
|
return (df->maxHemi <= 1.01); |
407 |
|
} |
408 |
|
|
364 |
– |
/* skip integer in string */ |
365 |
– |
static char * |
366 |
– |
i_skip(char *s) |
367 |
– |
{ |
368 |
– |
while (isspace(*s)) |
369 |
– |
s++; |
370 |
– |
if (*s == '-' || *s == '+') |
371 |
– |
s++; |
372 |
– |
if (!isdigit(*s)) |
373 |
– |
return(NULL); |
374 |
– |
do |
375 |
– |
s++; |
376 |
– |
while (isdigit(*s)); |
377 |
– |
return(s); |
378 |
– |
} |
379 |
– |
|
380 |
– |
/* skip float in string */ |
381 |
– |
static char * |
382 |
– |
f_skip(char *s) |
383 |
– |
{ |
384 |
– |
register char *cp; |
385 |
– |
|
386 |
– |
while (isspace(*s)) |
387 |
– |
s++; |
388 |
– |
if (*s == '-' || *s == '+') |
389 |
– |
s++; |
390 |
– |
cp = s; |
391 |
– |
while (isdigit(*cp)) |
392 |
– |
cp++; |
393 |
– |
if (*cp == '.') { |
394 |
– |
cp++; s++; |
395 |
– |
while (isdigit(*cp)) |
396 |
– |
cp++; |
397 |
– |
} |
398 |
– |
if (cp == s) |
399 |
– |
return(NULL); |
400 |
– |
if (*cp == 'e' || *cp == 'E') |
401 |
– |
return(i_skip(cp+1)); |
402 |
– |
return(cp); |
403 |
– |
} |
404 |
– |
|
409 |
|
/* load BSDF distribution for this wavelength */ |
410 |
|
static int |
411 |
|
load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc) |
412 |
|
{ |
409 |
– |
char *cbasis = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis")); |
410 |
– |
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; |
416 |
– |
|
417 |
– |
if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) { |
418 |
– |
sprintf(SDerrorDetail, "Missing column/row basis for BSDF '%s'", |
419 |
– |
sd->name); |
420 |
– |
return RC_FORMERR; |
421 |
– |
} |
419 |
|
/* allocate BSDF component */ |
420 |
|
sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection")); |
421 |
< |
if (!strcasecmp(sdata, "Transmission Front")) { |
421 |
> |
if (!sdata) |
422 |
> |
return RC_FAIL; |
423 |
> |
/* |
424 |
> |
* Remember that front and back are reversed from WINDOW 6 orientations |
425 |
> |
* Favor their "Front" (incoming light) since that's more often valid |
426 |
> |
*/ |
427 |
> |
tfront = !strcasecmp(sdata, "Transmission Back"); |
428 |
> |
if (!strcasecmp(sdata, "Transmission Front") || |
429 |
> |
tfront & (sd->tf == NULL)) { |
430 |
|
if (sd->tf != NULL) |
431 |
|
SDfreeSpectralDF(sd->tf); |
432 |
|
if ((sd->tf = SDnewSpectralDF(1)) == NULL) |
433 |
|
return RC_MEMERR; |
434 |
|
df = sd->tf; |
435 |
|
} else if (!strcasecmp(sdata, "Reflection Front")) { |
436 |
< |
if (sd->rf != NULL) |
432 |
< |
SDfreeSpectralDF(sd->rf); |
433 |
< |
if ((sd->rf = SDnewSpectralDF(1)) == NULL) |
434 |
< |
return RC_MEMERR; |
435 |
< |
df = sd->rf; |
436 |
< |
} else if (!strcasecmp(sdata, "Reflection Back")) { |
437 |
< |
if (sd->rb != NULL) |
436 |
> |
if (sd->rb != NULL) /* note back-front reversal */ |
437 |
|
SDfreeSpectralDF(sd->rb); |
438 |
|
if ((sd->rb = SDnewSpectralDF(1)) == NULL) |
439 |
|
return RC_MEMERR; |
440 |
|
df = sd->rb; |
441 |
+ |
} else if (!strcasecmp(sdata, "Reflection Back")) { |
442 |
+ |
if (sd->rf != NULL) /* note front-back reversal */ |
443 |
+ |
SDfreeSpectralDF(sd->rf); |
444 |
+ |
if ((sd->rf = SDnewSpectralDF(1)) == NULL) |
445 |
+ |
return RC_MEMERR; |
446 |
+ |
df = sd->rf; |
447 |
|
} else |
448 |
|
return RC_FAIL; |
449 |
+ |
/* XXX should also check "ScatteringDataType" for consistency? */ |
450 |
|
/* get angle bases */ |
451 |
|
sdata = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis")); |
452 |
|
if (!sdata || !*sdata) { |
455 |
|
return RC_FORMERR; |
456 |
|
} |
457 |
|
for (inbi = nabases; inbi--; ) |
458 |
< |
if (!strcasecmp(cbasis, abase_list[inbi].name)) |
458 |
> |
if (!strcasecmp(sdata, abase_list[inbi].name)) |
459 |
|
break; |
460 |
|
if (inbi < 0) { |
461 |
< |
sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'", |
456 |
< |
cbasis); |
461 |
> |
sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'", sdata); |
462 |
|
return RC_FORMERR; |
463 |
|
} |
464 |
|
sdata = ezxml_txt(ezxml_child(wdb,"RowAngleBasis")); |
468 |
|
return RC_FORMERR; |
469 |
|
} |
470 |
|
for (outbi = nabases; outbi--; ) |
471 |
< |
if (!strcasecmp(rbasis, abase_list[outbi].name)) |
471 |
> |
if (!strcasecmp(sdata, abase_list[outbi].name)) |
472 |
|
break; |
473 |
|
if (outbi < 0) { |
474 |
< |
sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", cbasis); |
474 |
> |
sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", sdata); |
475 |
|
return RC_FORMERR; |
476 |
|
} |
477 |
|
/* allocate BSDF matrix */ |
478 |
|
dp = SDnewMatrix(abase_list[inbi].nangles, abase_list[outbi].nangles); |
479 |
|
if (dp == NULL) |
480 |
|
return RC_MEMERR; |
481 |
< |
dp->ib_priv = (void *)&abase_list[inbi]; |
482 |
< |
dp->ob_priv = (void *)&abase_list[outbi]; |
481 |
> |
dp->ib_priv = &abase_list[inbi]; |
482 |
> |
dp->ob_priv = &abase_list[outbi]; |
483 |
|
if (df == sd->tf) { |
484 |
< |
dp->ib_vec = ab_getvecR; |
485 |
< |
dp->ib_ndx = ab_getndxR; |
486 |
< |
dp->ob_vec = ab_getvec; |
487 |
< |
dp->ob_ndx = ab_getndx; |
484 |
> |
if (tfront) { |
485 |
> |
dp->ib_vec = &fi_getvec; |
486 |
> |
dp->ib_ndx = &fi_getndx; |
487 |
> |
dp->ob_vec = &bo_getvec; |
488 |
> |
dp->ob_ndx = &bo_getndx; |
489 |
> |
} else { |
490 |
> |
dp->ib_vec = &bi_getvec; |
491 |
> |
dp->ib_ndx = &bi_getndx; |
492 |
> |
dp->ob_vec = &fo_getvec; |
493 |
> |
dp->ob_ndx = &fo_getndx; |
494 |
> |
} |
495 |
|
} else if (df == sd->rf) { |
496 |
< |
dp->ib_vec = ab_getvec; |
497 |
< |
dp->ib_ndx = ab_getndx; |
498 |
< |
dp->ob_vec = ab_getvec; |
499 |
< |
dp->ob_ndx = ab_getndx; |
496 |
> |
dp->ib_vec = &fi_getvec; |
497 |
> |
dp->ib_ndx = &fi_getndx; |
498 |
> |
dp->ob_vec = &fo_getvec; |
499 |
> |
dp->ob_ndx = &fo_getndx; |
500 |
|
} else /* df == sd->rb */ { |
501 |
< |
dp->ib_vec = ab_getvecR; |
502 |
< |
dp->ib_ndx = ab_getndxR; |
503 |
< |
dp->ob_vec = ab_getvecR; |
504 |
< |
dp->ob_ndx = ab_getndxR; |
501 |
> |
dp->ib_vec = &bi_getvec; |
502 |
> |
dp->ib_ndx = &bi_getndx; |
503 |
> |
dp->ob_vec = &bo_getvec; |
504 |
> |
dp->ob_ndx = &bo_getndx; |
505 |
|
} |
506 |
< |
dp->ib_ohm = ab_getohm; |
507 |
< |
dp->ob_ohm = ab_getohm; |
506 |
> |
dp->ib_ohm = &io_getohm; |
507 |
> |
dp->ob_ohm = &io_getohm; |
508 |
|
df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */ |
509 |
|
df->comp[0].dist = dp; |
510 |
|
df->comp[0].func = &SDhandleMtx; |
511 |
|
/* read BSDF data */ |
512 |
< |
sdata = ezxml_txt(ezxml_child(wdb,"ScatteringData")); |
512 |
> |
sdata = ezxml_txt(ezxml_child(wdb, "ScatteringData")); |
513 |
|
if (!sdata || !*sdata) { |
514 |
|
sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'", |
515 |
|
sd->name); |
516 |
|
return RC_FORMERR; |
517 |
|
} |
518 |
|
for (i = 0; i < dp->ninc*dp->nout; i++) { |
519 |
< |
char *sdnext = f_skip(sdata); |
519 |
> |
char *sdnext = fskip(sdata); |
520 |
|
if (sdnext == NULL) { |
521 |
|
sprintf(SDerrorDetail, |
522 |
|
"Bad/missing BSDF ScatteringData in '%s'", |
523 |
|
sd->name); |
524 |
|
return RC_FORMERR; |
525 |
|
} |
526 |
< |
while (*sdnext && isspace(*sdnext)) |
526 |
> |
while (isspace(*sdnext)) |
527 |
|
sdnext++; |
528 |
|
if (*sdnext == ',') sdnext++; |
529 |
|
if (rowinc) { |
548 |
|
for (i = n; --i; ) |
549 |
|
if (sm->bsdf[i] < minv) |
550 |
|
minv = sm->bsdf[i]; |
551 |
+ |
|
552 |
+ |
if (minv <= FTINY) |
553 |
+ |
return .0; |
554 |
+ |
|
555 |
|
for (i = n; i--; ) |
556 |
|
sm->bsdf[i] -= minv; |
557 |
|
|
577 |
|
c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]); |
578 |
|
dv->cieY += ymin; |
579 |
|
} |
580 |
< |
df->maxHemi -= dv->cieY; /* correct minimum hemispherical */ |
581 |
< |
dv->spec.clock++; /* make sure everything is set */ |
580 |
> |
df->maxHemi -= dv->cieY; /* adjust maximum hemispherical */ |
581 |
> |
/* make sure everything is set */ |
582 |
|
c_ccvt(&dv->spec, C_CSXY+C_CSSPEC); |
583 |
|
} |
584 |
|
|
585 |
|
/* Load a BSDF matrix from an open XML file */ |
586 |
|
SDError |
587 |
< |
SDloadMtx(SDData *sd, ezxml_t fl) |
587 |
> |
SDloadMtx(SDData *sd, ezxml_t wtl) |
588 |
|
{ |
589 |
< |
ezxml_t wtl, wld, wdb; |
590 |
< |
int rowIn; |
591 |
< |
struct BSDF_data *dp; |
592 |
< |
char *txt; |
593 |
< |
int rval; |
594 |
< |
|
595 |
< |
if (strcmp(ezxml_name(fl), "WindowElement")) { |
589 |
> |
ezxml_t wld, wdb; |
590 |
> |
int rowIn; |
591 |
> |
char *txt; |
592 |
> |
int rval; |
593 |
> |
/* basic checks and data ordering */ |
594 |
> |
txt = ezxml_txt(ezxml_child(ezxml_child(wtl, |
595 |
> |
"DataDefinition"), "IncidentDataStructure")); |
596 |
> |
if (txt == NULL || !*txt) { |
597 |
|
sprintf(SDerrorDetail, |
598 |
< |
"BSDF \"%s\": top level node not 'WindowElement'", |
598 |
> |
"BSDF \"%s\": missing IncidentDataStructure", |
599 |
|
sd->name); |
600 |
|
return SDEformat; |
601 |
|
} |
585 |
– |
wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer"); |
586 |
– |
txt = ezxml_txt(ezxml_child(ezxml_child(wtl, |
587 |
– |
"DataDefinition"), "IncidentDataStructure")); |
602 |
|
if (!strcasecmp(txt, "Rows")) |
603 |
|
rowIn = 1; |
604 |
|
else if (!strcasecmp(txt, "Columns")) |
609 |
|
sd->name); |
610 |
|
return SDEsupport; |
611 |
|
} |
612 |
< |
/* get angle basis */ |
612 |
> |
/* get angle basis */ |
613 |
|
rval = load_angle_basis(ezxml_child(ezxml_child(wtl, |
614 |
|
"DataDefinition"), "AngleBasis")); |
615 |
|
if (rval < 0) |
616 |
< |
goto err_return; |
617 |
< |
/* load BSDF components */ |
616 |
> |
return convert_errcode(rval); |
617 |
> |
/* load BSDF components */ |
618 |
|
for (wld = ezxml_child(wtl, "WavelengthData"); |
619 |
|
wld != NULL; wld = wld->next) { |
620 |
|
if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")), |
623 |
|
for (wdb = ezxml_child(wld, "WavelengthDataBlock"); |
624 |
|
wdb != NULL; wdb = wdb->next) |
625 |
|
if ((rval = load_bsdf_data(sd, wdb, rowIn)) < 0) |
626 |
< |
goto err_return; |
626 |
> |
return convert_errcode(rval); |
627 |
|
} |
628 |
< |
/* separate diffuse components */ |
628 |
> |
/* separate diffuse components */ |
629 |
|
extract_diffuse(&sd->rLambFront, sd->rf); |
630 |
|
extract_diffuse(&sd->rLambBack, sd->rb); |
631 |
|
extract_diffuse(&sd->tLamb, sd->tf); |
632 |
< |
/* return success */ |
632 |
> |
/* return success */ |
633 |
|
return SDEnone; |
620 |
– |
err_return: /* jump here on failure */ |
621 |
– |
if (sd->rf != NULL) { |
622 |
– |
SDfreeSpectralDF(sd->rf); |
623 |
– |
sd->rf = NULL; |
624 |
– |
} |
625 |
– |
if (sd->rb != NULL) { |
626 |
– |
SDfreeSpectralDF(sd->rb); |
627 |
– |
sd->rb = NULL; |
628 |
– |
} |
629 |
– |
if (sd->tf != NULL) { |
630 |
– |
SDfreeSpectralDF(sd->tf); |
631 |
– |
sd->tf = NULL; |
632 |
– |
} |
633 |
– |
return convert_errcode(rval); |
634 |
|
} |
635 |
|
|
636 |
|
/* Get Matrix BSDF value */ |
637 |
|
static int |
638 |
|
SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec, |
639 |
< |
const FVECT inVec, const void *dist) |
639 |
> |
const FVECT inVec, SDComponent *sdc) |
640 |
|
{ |
641 |
< |
const SDMat *dp = (const SDMat *)dist; |
641 |
> |
const SDMat *dp; |
642 |
|
int i_ndx, o_ndx; |
643 |
+ |
/* check arguments */ |
644 |
+ |
if ((coef == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL) |
645 |
+ |
|| (dp = (SDMat *)sdc->dist) == NULL) |
646 |
+ |
return 0; |
647 |
|
/* get angle indices */ |
648 |
|
i_ndx = mBSDF_incndx(dp, inVec); |
649 |
|
o_ndx = mBSDF_outndx(dp, outVec); |
658 |
|
return 1; /* XXX monochrome for now */ |
659 |
|
} |
660 |
|
|
661 |
< |
/* Query solid angle for vector */ |
661 |
> |
/* Query solid angle for vector(s) */ |
662 |
|
static SDError |
663 |
< |
SDqueryMtxProjSA(double *psa, const FVECT vec, int qflags, const void *dist) |
663 |
> |
SDqueryMtxProjSA(double *psa, const FVECT v1, const RREAL *v2, |
664 |
> |
int qflags, SDComponent *sdc) |
665 |
|
{ |
666 |
< |
const SDMat *dp = (const SDMat *)dist; |
667 |
< |
|
668 |
< |
if (!(qflags & SDqueryInc+SDqueryOut)) |
666 |
> |
const SDMat *dp; |
667 |
> |
double inc_psa, out_psa; |
668 |
> |
/* check arguments */ |
669 |
> |
if ((psa == NULL) | (v1 == NULL) | (sdc == NULL) || |
670 |
> |
(dp = (SDMat *)sdc->dist) == NULL) |
671 |
|
return SDEargument; |
672 |
< |
if (qflags & SDqueryInc) { |
673 |
< |
double inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, vec)); |
674 |
< |
if (inc_psa < .0) |
675 |
< |
return SDEinternal; |
676 |
< |
switch (qflags & SDqueryMin+SDqueryMax) { |
677 |
< |
case SDqueryMax: |
678 |
< |
if (inc_psa > psa[0]) |
679 |
< |
psa[0] = inc_psa; |
680 |
< |
break; |
681 |
< |
case SDqueryMin+SDqueryMax: |
682 |
< |
if (inc_psa > psa[1]) |
683 |
< |
psa[1] = inc_psa; |
684 |
< |
/* fall through */ |
678 |
< |
case SDqueryMin: |
679 |
< |
if (inc_psa < psa[0]) |
680 |
< |
psa[0] = inc_psa; |
681 |
< |
break; |
682 |
< |
case 0: |
672 |
> |
if (v2 == NULL) |
673 |
> |
v2 = v1; |
674 |
> |
/* get projected solid angles */ |
675 |
> |
out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v1)); |
676 |
> |
inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v2)); |
677 |
> |
if ((v1 != v2) & (out_psa <= 0) & (inc_psa <= 0)) { |
678 |
> |
inc_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v2)); |
679 |
> |
out_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v1)); |
680 |
> |
} |
681 |
> |
|
682 |
> |
switch (qflags) { /* record based on flag settings */ |
683 |
> |
case SDqueryMax: |
684 |
> |
if (inc_psa > psa[0]) |
685 |
|
psa[0] = inc_psa; |
686 |
< |
break; |
687 |
< |
} |
686 |
> |
if (out_psa > psa[0]) |
687 |
> |
psa[0] = out_psa; |
688 |
> |
break; |
689 |
> |
case SDqueryMin+SDqueryMax: |
690 |
> |
if (inc_psa > psa[1]) |
691 |
> |
psa[1] = inc_psa; |
692 |
> |
if (out_psa > psa[1]) |
693 |
> |
psa[1] = out_psa; |
694 |
> |
/* fall through */ |
695 |
> |
case SDqueryVal: |
696 |
> |
if (qflags == SDqueryVal) |
697 |
> |
psa[0] = M_PI; |
698 |
> |
/* fall through */ |
699 |
> |
case SDqueryMin: |
700 |
> |
if ((inc_psa > 0) & (inc_psa < psa[0])) |
701 |
> |
psa[0] = inc_psa; |
702 |
> |
if ((out_psa > 0) & (out_psa < psa[0])) |
703 |
> |
psa[0] = out_psa; |
704 |
> |
break; |
705 |
|
} |
706 |
< |
if (qflags & SDqueryOut) { |
707 |
< |
double out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, vec)); |
689 |
< |
if (out_psa < .0) |
690 |
< |
return SDEinternal; |
691 |
< |
switch (qflags & SDqueryMin+SDqueryMax) { |
692 |
< |
case SDqueryMax: |
693 |
< |
if (out_psa > psa[0]) |
694 |
< |
psa[0] = out_psa; |
695 |
< |
break; |
696 |
< |
case SDqueryMin+SDqueryMax: |
697 |
< |
if (out_psa > psa[1]) |
698 |
< |
psa[1] = out_psa; |
699 |
< |
/* fall through */ |
700 |
< |
case SDqueryMin: |
701 |
< |
if (out_psa < psa[0]) |
702 |
< |
psa[0] = out_psa; |
703 |
< |
break; |
704 |
< |
case 0: |
705 |
< |
psa[(qflags&SDqueryInc)!=0] = out_psa; |
706 |
< |
break; |
707 |
< |
} |
708 |
< |
} |
709 |
< |
return SDEnone; |
706 |
> |
/* make sure it's legal */ |
707 |
> |
return (psa[0] <= 0) ? SDEinternal : SDEnone; |
708 |
|
} |
709 |
|
|
710 |
|
/* Compute new cumulative distribution from BSDF */ |
742 |
|
static const SDCDst * |
743 |
|
SDgetMtxCDist(const FVECT inVec, SDComponent *sdc) |
744 |
|
{ |
745 |
< |
SDMat *dp = (SDMat *)sdc->dist; |
745 |
> |
SDMat *dp; |
746 |
|
int reverse; |
747 |
|
SDMatCDst myCD; |
748 |
|
SDMatCDst *cd, *cdlast; |
749 |
< |
|
750 |
< |
if (dp == NULL) |
749 |
> |
/* check arguments */ |
750 |
> |
if ((inVec == NULL) | (sdc == NULL) || |
751 |
> |
(dp = (SDMat *)sdc->dist) == NULL) |
752 |
|
return NULL; |
753 |
|
memset(&myCD, 0, sizeof(myCD)); |
754 |
|
myCD.indx = mBSDF_incndx(dp, inVec); |
767 |
|
reverse = 1; |
768 |
|
} |
769 |
|
cdlast = NULL; /* check for it in cache list */ |
770 |
< |
for (cd = (SDMatCDst *)sdc->cdList; |
771 |
< |
cd != NULL; cd = (SDMatCDst *)cd->next) { |
770 |
> |
for (cd = (SDMatCDst *)sdc->cdList; cd != NULL; |
771 |
> |
cdlast = cd, cd = (SDMatCDst *)cd->next) |
772 |
|
if (cd->indx == myCD.indx && (cd->calen == myCD.calen) & |
773 |
|
(cd->ob_priv == myCD.ob_priv) & |
774 |
|
(cd->ob_vec == myCD.ob_vec)) |
775 |
|
break; |
777 |
– |
cdlast = cd; |
778 |
– |
} |
776 |
|
if (cd == NULL) { /* need to allocate new entry */ |
777 |
|
cd = (SDMatCDst *)malloc(sizeof(SDMatCDst) + |
778 |
< |
myCD.calen*sizeof(myCD.carr[0])); |
778 |
> |
sizeof(myCD.carr[0])*myCD.calen); |
779 |
|
if (cd == NULL) |
780 |
|
return NULL; |
781 |
|
*cd = myCD; /* compute cumulative distribution */ |
795 |
|
|
796 |
|
/* Sample cumulative distribution */ |
797 |
|
static SDError |
798 |
< |
SDsampMtxCDist(FVECT outVec, double randX, const SDCDst *cdp) |
798 |
> |
SDsampMtxCDist(FVECT ioVec, double randX, const SDCDst *cdp) |
799 |
|
{ |
800 |
|
const unsigned maxval = ~0; |
801 |
|
const SDMatCDst *mcd = (const SDMatCDst *)cdp; |
802 |
|
const unsigned target = randX*maxval; |
803 |
|
int i, iupper, ilower; |
804 |
+ |
/* check arguments */ |
805 |
+ |
if ((ioVec == NULL) | (mcd == NULL)) |
806 |
+ |
return SDEargument; |
807 |
|
/* binary search to find index */ |
808 |
|
ilower = 0; iupper = mcd->calen; |
809 |
|
while ((i = (iupper + ilower) >> 1) != ilower) |
815 |
|
randX = (randX*maxval - mcd->carr[ilower]) / |
816 |
|
(double)(mcd->carr[iupper] - mcd->carr[ilower]); |
817 |
|
/* convert index to vector */ |
818 |
< |
if ((*mcd->ob_vec)(outVec, i, randX, mcd->ob_priv)) |
818 |
> |
if ((*mcd->ob_vec)(ioVec, i+randX, mcd->ob_priv)) |
819 |
|
return SDEnone; |
820 |
< |
strcpy(SDerrorDetail, "BSDF sampling fault"); |
820 |
> |
strcpy(SDerrorDetail, "Matrix BSDF sampling fault"); |
821 |
|
return SDEinternal; |
822 |
|
} |
823 |
|
|