9 |
|
|
10 |
|
#define _USE_MATH_DEFINES |
11 |
|
#include <stdlib.h> |
12 |
– |
#include <string.h> |
12 |
|
#include <math.h> |
13 |
|
#include "rtio.h" |
14 |
|
#include "resolu.h" |
15 |
|
#include "bsdfrep.h" |
16 |
+ |
#include "random.h" |
17 |
|
/* name and manufacturer if known */ |
18 |
|
char bsdf_name[256]; |
19 |
|
char bsdf_manuf[256]; |
29 |
|
int input_orient = 0; |
30 |
|
int output_orient = 0; |
31 |
|
|
32 |
+ |
/* represented color space */ |
33 |
+ |
RBColor rbf_colorimetry = RBCunknown; |
34 |
+ |
|
35 |
+ |
const char *RBCident[] = { |
36 |
+ |
"CIE-Y", "CIE-XYZ", "Spectral", "Unknown" |
37 |
+ |
}; |
38 |
+ |
|
39 |
|
/* BSDF histogram */ |
40 |
|
unsigned long bsdf_hist[HISTLEN]; |
41 |
|
|
42 |
|
/* BSDF value for boundary regions */ |
43 |
|
double bsdf_min = 0; |
44 |
+ |
double bsdf_spec_val = 0; |
45 |
+ |
double bsdf_spec_rad = 0; |
46 |
|
|
47 |
|
/* processed incident DSF measurements */ |
48 |
|
RBFNODE *dsf_list = NULL; |
53 |
|
/* current input direction */ |
54 |
|
double theta_in_deg, phi_in_deg; |
55 |
|
|
56 |
+ |
/* header line sharing callback */ |
57 |
+ |
int (*sir_headshare)(char *s) = NULL; |
58 |
+ |
|
59 |
|
/* Register new input direction */ |
60 |
|
int |
61 |
|
new_input_direction(double new_theta, double new_phi) |
62 |
|
{ |
51 |
– |
if (!input_orient) /* check input orientation */ |
52 |
– |
input_orient = 1 - 2*(new_theta > 90.); |
53 |
– |
else if (input_orient > 0 ^ new_theta < 90.) { |
54 |
– |
fprintf(stderr, |
55 |
– |
"%s: Cannot handle input angles on both sides of surface\n", |
56 |
– |
progname); |
57 |
– |
return(0); |
58 |
– |
} |
63 |
|
/* normalize angle ranges */ |
64 |
|
while (new_theta < -180.) |
65 |
|
new_theta += 360.; |
69 |
|
new_theta = -new_theta; |
70 |
|
new_phi += 180.; |
71 |
|
} |
68 |
– |
if ((theta_in_deg = new_theta) < 1.0) |
69 |
– |
return(1); /* don't rely on phi near normal */ |
72 |
|
while (new_phi < 0) |
73 |
|
new_phi += 360.; |
74 |
|
while (new_phi >= 360.) |
75 |
|
new_phi -= 360.; |
76 |
+ |
/* check input orientation */ |
77 |
+ |
if (!input_orient) |
78 |
+ |
input_orient = 1 - 2*(new_theta > 90.); |
79 |
+ |
else if (input_orient > 0 ^ new_theta < 90.) { |
80 |
+ |
fprintf(stderr, |
81 |
+ |
"%s: Cannot handle input angles on both sides of surface\n", |
82 |
+ |
progname); |
83 |
+ |
return(0); |
84 |
+ |
} |
85 |
+ |
if ((theta_in_deg = new_theta) < 1.0) |
86 |
+ |
return(1); /* don't rely on phi near normal */ |
87 |
|
if (single_plane_incident > 0) /* check input coverage */ |
88 |
|
single_plane_incident = (round(new_phi) == round(phi_in_deg)); |
89 |
|
else if (single_plane_incident < 0) |
211 |
|
int pos[2]; |
212 |
|
int n; |
213 |
|
|
214 |
< |
for (n = ((-.01 > phi) | (phi > .01))*rbf->nrbf; n-- > 0; ) { |
214 |
> |
for (n = (cos(phi) < 1.-FTINY)*rbf->nrbf; n-- > 0; ) { |
215 |
|
ovec_from_pos(outvec, rbf->rbfa[n].gx, rbf->rbfa[n].gy); |
216 |
|
spinvector(outvec, outvec, vnorm, phi); |
217 |
|
pos_from_vec(pos, outvec); |
270 |
|
return(integ); |
271 |
|
} |
272 |
|
|
273 |
< |
/* Evaluate RBF for DSF at the given normalized outgoing direction */ |
274 |
< |
double |
275 |
< |
eval_rbfrep(const RBFNODE *rp, const FVECT outvec) |
273 |
> |
/* Evaluate BSDF at the given normalized outgoing direction in color */ |
274 |
> |
SDError |
275 |
> |
eval_rbfcol(SDValue *sv, const RBFNODE *rp, const FVECT outvec) |
276 |
|
{ |
277 |
|
const double rfact2 = (38./M_PI/M_PI)*(grid_res*grid_res); |
265 |
– |
double minval = bsdf_min*output_orient*outvec[2]; |
278 |
|
int pos[2]; |
279 |
|
double res = 0; |
280 |
+ |
double usum = 0, vsum = 0; |
281 |
|
const RBFVAL *rbfp; |
282 |
|
FVECT odir; |
283 |
|
double rad2; |
284 |
|
int n; |
285 |
+ |
/* assign default value */ |
286 |
+ |
sv->spec = c_dfcolor; |
287 |
+ |
sv->cieY = bsdf_min; |
288 |
|
/* check for wrong side */ |
289 |
< |
if (outvec[2] > 0 ^ output_orient > 0) |
290 |
< |
return(.0); |
291 |
< |
/* use minimum if no information avail. */ |
292 |
< |
if (rp == NULL) |
293 |
< |
return(minval); |
289 |
> |
if (outvec[2] > 0 ^ output_orient > 0) { |
290 |
> |
strcpy(SDerrorDetail, "Wrong-side scattering query"); |
291 |
> |
return(SDEargument); |
292 |
> |
} |
293 |
> |
if (rp == NULL) /* return minimum if no information avail. */ |
294 |
> |
return(SDEnone); |
295 |
|
/* optimization for fast lobe culling */ |
296 |
|
pos_from_vec(pos, outvec); |
297 |
|
/* sum radial basis function */ |
299 |
|
for (n = rp->nrbf; n--; rbfp++) { |
300 |
|
int d2 = (pos[0]-rbfp->gx)*(pos[0]-rbfp->gx) + |
301 |
|
(pos[1]-rbfp->gy)*(pos[1]-rbfp->gy); |
302 |
+ |
double val; |
303 |
|
rad2 = R2ANG(rbfp->crad); |
304 |
|
rad2 *= rad2; |
305 |
|
if (d2 > rad2*rfact2) |
306 |
|
continue; |
307 |
|
ovec_from_pos(odir, rbfp->gx, rbfp->gy); |
308 |
< |
res += rbfp->peak * exp((DOT(odir,outvec) - 1.) / rad2); |
308 |
> |
val = rbfp->peak * exp((DOT(odir,outvec) - 1.) / rad2); |
309 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
310 |
> |
usum += val * (rbfp->chroma & 0xff); |
311 |
> |
vsum += val * (rbfp->chroma>>8 & 0xff); |
312 |
> |
} |
313 |
> |
res += val; |
314 |
|
} |
315 |
< |
if (res < minval) /* never return less than minval */ |
316 |
< |
return(minval); |
317 |
< |
return(res); |
315 |
> |
sv->cieY = res / COSF(outvec[2]); |
316 |
> |
if (sv->cieY < bsdf_min) { /* never return less than bsdf_min */ |
317 |
> |
sv->cieY = bsdf_min; |
318 |
> |
} else if (rbf_colorimetry == RBCtristimulus) { |
319 |
> |
C_CHROMA cres = (int)(usum/res + frandom()); |
320 |
> |
cres |= (int)(vsum/res + frandom()) << 8; |
321 |
> |
c_decodeChroma(&sv->spec, cres); |
322 |
> |
} |
323 |
> |
return(SDEnone); |
324 |
|
} |
325 |
|
|
326 |
+ |
/* Evaluate BSDF at the given normalized outgoing direction in Y */ |
327 |
+ |
double |
328 |
+ |
eval_rbfrep(const RBFNODE *rp, const FVECT outvec) |
329 |
+ |
{ |
330 |
+ |
SDValue sv; |
331 |
+ |
|
332 |
+ |
if (eval_rbfcol(&sv, rp, outvec) == SDEnone) |
333 |
+ |
return(sv.cieY); |
334 |
+ |
|
335 |
+ |
return(0.0); |
336 |
+ |
} |
337 |
+ |
|
338 |
|
/* Insert a new directional scattering function in our global list */ |
339 |
|
int |
340 |
|
insert_dsf(RBFNODE *newrbf) |
345 |
|
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) |
346 |
|
if (DOT(rbf->invec, newrbf->invec) >= 1.-FTINY) { |
347 |
|
fprintf(stderr, |
348 |
< |
"%s: Duplicate incident measurement (ignored)\n", |
349 |
< |
progname); |
348 |
> |
"%s: Duplicate incident measurement ignored at (%.1f,%.1f)\n", |
349 |
> |
progname, get_theta180(newrbf->invec), |
350 |
> |
get_phi360(newrbf->invec)); |
351 |
|
free(newrbf); |
352 |
|
return(-1); |
353 |
|
} |
435 |
|
return((rbfv[0] != NULL) + (rbfv[1] != NULL)); |
436 |
|
} |
437 |
|
|
438 |
+ |
/* Return single-lobe specular RBF for the given incident direction */ |
439 |
+ |
RBFNODE * |
440 |
+ |
def_rbf_spec(const FVECT invec) |
441 |
+ |
{ |
442 |
+ |
RBFNODE *rbf; |
443 |
+ |
FVECT ovec; |
444 |
+ |
int pos[2]; |
445 |
+ |
|
446 |
+ |
if (input_orient > 0 ^ invec[2] > 0) /* wrong side? */ |
447 |
+ |
return(NULL); |
448 |
+ |
if ((bsdf_spec_val <= bsdf_min) | (bsdf_spec_rad <= 0)) |
449 |
+ |
return(NULL); /* nothing set */ |
450 |
+ |
rbf = (RBFNODE *)malloc(sizeof(RBFNODE)); |
451 |
+ |
if (rbf == NULL) |
452 |
+ |
return(NULL); |
453 |
+ |
ovec[0] = -invec[0]; |
454 |
+ |
ovec[1] = -invec[1]; |
455 |
+ |
ovec[2] = invec[2]*(2*(input_orient==output_orient) - 1); |
456 |
+ |
pos_from_vec(pos, ovec); |
457 |
+ |
rbf->ord = 0; |
458 |
+ |
rbf->next = NULL; |
459 |
+ |
rbf->ejl = NULL; |
460 |
+ |
VCOPY(rbf->invec, invec); |
461 |
+ |
rbf->nrbf = 1; |
462 |
+ |
rbf->rbfa[0].peak = bsdf_spec_val * COSF(ovec[2]); |
463 |
+ |
rbf->rbfa[0].chroma = c_dfchroma; |
464 |
+ |
rbf->rbfa[0].crad = ANG2R(bsdf_spec_rad); |
465 |
+ |
rbf->rbfa[0].gx = pos[0]; |
466 |
+ |
rbf->rbfa[0].gy = pos[1]; |
467 |
+ |
rbf->vtotal = rbf_volume(rbf->rbfa); |
468 |
+ |
return(rbf); |
469 |
+ |
} |
470 |
+ |
|
471 |
|
/* Advect and allocate new RBF along edge (internal call) */ |
472 |
|
RBFNODE * |
473 |
|
e_advect_rbf(const MIGRATION *mig, const FVECT invec, int lobe_lim) |
524 |
|
const RBFVAL *rbf0i = &mig->rbfv[0]->rbfa[i]; |
525 |
|
const float peak0 = rbf0i->peak; |
526 |
|
const double rad0 = R2ANG(rbf0i->crad); |
527 |
+ |
C_COLOR cc0; |
528 |
|
FVECT v0; |
529 |
|
float mv; |
530 |
|
ovec_from_pos(v0, rbf0i->gx, rbf0i->gy); |
531 |
+ |
c_decodeChroma(&cc0, rbf0i->chroma); |
532 |
|
for (j = 0; j < mtx_ncols(mig); j++) |
533 |
|
if ((mv = mtx_coef(mig,i,j)) > cthresh) { |
534 |
|
const RBFVAL *rbf1j = &mig->rbfv[1]->rbfa[j]; |
539 |
|
rad2 = rad0*rad0*(1.-t) + rad2*rad2*t; |
540 |
|
rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal * |
541 |
|
rad0*rad0/rad2; |
542 |
+ |
if (rbf_colorimetry == RBCtristimulus) { |
543 |
+ |
C_COLOR cres; |
544 |
+ |
c_decodeChroma(&cres, rbf1j->chroma); |
545 |
+ |
c_cmix(&cres, 1.-t, &cc0, t, &cres); |
546 |
+ |
rbf->rbfa[n].chroma = c_encodeChroma(&cres); |
547 |
+ |
} else |
548 |
+ |
rbf->rbfa[n].chroma = c_dfchroma; |
549 |
|
rbf->rbfa[n].crad = ANG2R(sqrt(rad2)); |
550 |
|
ovec_from_pos(v, rbf1j->gx, rbf1j->gy); |
551 |
|
geodesic(v, v0, v, t, GEOD_REL); |
582 |
|
inp_coverage = 0; |
583 |
|
single_plane_incident = -1; |
584 |
|
input_orient = output_orient = 0; |
585 |
+ |
rbf_colorimetry = RBCunknown; |
586 |
|
grid_res = GRIDRES; |
587 |
+ |
memset(bsdf_hist, 0, sizeof(bsdf_hist)); |
588 |
+ |
bsdf_min = 0; |
589 |
+ |
bsdf_spec_val = 0; |
590 |
+ |
bsdf_spec_rad = 0; |
591 |
|
} |
592 |
|
|
593 |
|
/* Write our BSDF mesh interpolant out to the given binary stream */ |
604 |
|
fprintf(ofp, "MANUFACT=%s\n", bsdf_manuf); |
605 |
|
fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage); |
606 |
|
fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient); |
607 |
+ |
fprintf(ofp, "COLORIMETRY=%s\n", RBCident[rbf_colorimetry]); |
608 |
|
fprintf(ofp, "GRIDRES=%d\n", grid_res); |
609 |
|
fprintf(ofp, "BSDFMIN=%g\n", bsdf_min); |
610 |
+ |
if ((bsdf_spec_val > bsdf_min) & (bsdf_spec_rad > 0)) |
611 |
+ |
fprintf(ofp, "BSDFSPEC= %f %f\n", bsdf_spec_val, bsdf_spec_rad); |
612 |
|
fputformat(BSDFREP_FMT, ofp); |
613 |
|
fputc('\n', ofp); |
614 |
+ |
putint(BSDFREP_MAGIC, 2, ofp); |
615 |
|
/* write each DSF */ |
616 |
|
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) { |
617 |
|
putint(rbf->ord, 4, ofp); |
622 |
|
putint(rbf->nrbf, 4, ofp); |
623 |
|
for (i = 0; i < rbf->nrbf; i++) { |
624 |
|
putflt(rbf->rbfa[i].peak, ofp); |
625 |
+ |
putint(rbf->rbfa[i].chroma, 2, ofp); |
626 |
|
putint(rbf->rbfa[i].crad, 2, ofp); |
627 |
< |
putint(rbf->rbfa[i].gx, 1, ofp); |
628 |
< |
putint(rbf->rbfa[i].gy, 1, ofp); |
627 |
> |
putint(rbf->rbfa[i].gx, 2, ofp); |
628 |
> |
putint(rbf->rbfa[i].gy, 2, ofp); |
629 |
|
} |
630 |
|
} |
631 |
|
putint(-1, 4, ofp); /* terminator */ |
661 |
|
static int |
662 |
|
headline(char *s, void *p) |
663 |
|
{ |
664 |
< |
char fmt[32]; |
664 |
> |
char fmt[MAXFMTLEN]; |
665 |
> |
int i; |
666 |
|
|
667 |
+ |
if (isheadid(s)) |
668 |
+ |
return(0); |
669 |
|
if (!strncmp(s, "NAME=", 5)) { |
670 |
|
strcpy(bsdf_name, s+5); |
671 |
|
bsdf_name[strlen(bsdf_name)-1] = '\0'; |
672 |
+ |
return(1); |
673 |
|
} |
674 |
|
if (!strncmp(s, "MANUFACT=", 9)) { |
675 |
|
strcpy(bsdf_manuf, s+9); |
676 |
|
bsdf_manuf[strlen(bsdf_manuf)-1] = '\0'; |
677 |
+ |
return(1); |
678 |
|
} |
679 |
|
if (!strncmp(s, "SYMMETRY=", 9)) { |
680 |
|
inp_coverage = atoi(s+9); |
681 |
|
single_plane_incident = !inp_coverage; |
682 |
< |
return(0); |
682 |
> |
return(1); |
683 |
|
} |
684 |
|
if (!strncmp(s, "IO_SIDES=", 9)) { |
685 |
|
sscanf(s+9, "%d %d", &input_orient, &output_orient); |
686 |
< |
return(0); |
686 |
> |
return(1); |
687 |
|
} |
688 |
+ |
if (!strncmp(s, "COLORIMETRY=", 12)) { |
689 |
+ |
fmt[0] = '\0'; |
690 |
+ |
sscanf(s+12, "%s", fmt); |
691 |
+ |
for (i = RBCunknown; i >= 0; i--) |
692 |
+ |
if (!strcmp(fmt, RBCident[i])) |
693 |
+ |
break; |
694 |
+ |
if (i < 0) |
695 |
+ |
return(-1); |
696 |
+ |
rbf_colorimetry = i; |
697 |
+ |
return(1); |
698 |
+ |
} |
699 |
|
if (!strncmp(s, "GRIDRES=", 8)) { |
700 |
|
sscanf(s+8, "%d", &grid_res); |
701 |
< |
return(0); |
701 |
> |
return(1); |
702 |
|
} |
703 |
|
if (!strncmp(s, "BSDFMIN=", 8)) { |
704 |
|
sscanf(s+8, "%lf", &bsdf_min); |
705 |
< |
return(0); |
705 |
> |
return(1); |
706 |
|
} |
707 |
< |
if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT)) |
708 |
< |
return(-1); |
707 |
> |
if (!strncmp(s, "BSDFSPEC=", 9)) { |
708 |
> |
sscanf(s+9, "%lf %lf", &bsdf_spec_val, &bsdf_spec_rad); |
709 |
> |
return(1); |
710 |
> |
} |
711 |
> |
if (formatval(fmt, s)) |
712 |
> |
return (strcmp(fmt, BSDFREP_FMT) ? -1 : 0); |
713 |
> |
if (sir_headshare != NULL) |
714 |
> |
return ((*sir_headshare)(s)); |
715 |
|
return(0); |
716 |
|
} |
717 |
|
|
726 |
|
clear_bsdf_rep(); |
727 |
|
if (ifp == NULL) |
728 |
|
return(0); |
729 |
< |
if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 | |
730 |
< |
!input_orient | !output_orient) { |
729 |
> |
if (getheader(ifp, headline, NULL) < 0 || (single_plane_incident < 0) | |
730 |
> |
!input_orient | !output_orient | |
731 |
> |
(grid_res < 16) | (grid_res > 0xffff)) { |
732 |
|
fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n", |
733 |
|
progname); |
734 |
|
return(0); |
735 |
|
} |
736 |
+ |
if (getint(2, ifp) != BSDFREP_MAGIC) { |
737 |
+ |
fprintf(stderr, "%s: bad magic number for BSDF interpolant\n", |
738 |
+ |
progname); |
739 |
+ |
return(0); |
740 |
+ |
} |
741 |
|
memset(&rbfh, 0, sizeof(rbfh)); /* read each DSF */ |
742 |
|
while ((rbfh.ord = getint(4, ifp)) >= 0) { |
743 |
|
RBFNODE *newrbf; |
758 |
|
*newrbf = rbfh; |
759 |
|
for (i = 0; i < rbfh.nrbf; i++) { |
760 |
|
newrbf->rbfa[i].peak = getflt(ifp); |
761 |
+ |
newrbf->rbfa[i].chroma = getint(2, ifp) & 0xffff; |
762 |
|
newrbf->rbfa[i].crad = getint(2, ifp) & 0xffff; |
763 |
< |
newrbf->rbfa[i].gx = getint(1, ifp) & 0xff; |
764 |
< |
newrbf->rbfa[i].gy = getint(1, ifp) & 0xff; |
763 |
> |
newrbf->rbfa[i].gx = getint(2, ifp) & 0xffff; |
764 |
> |
newrbf->rbfa[i].gy = getint(2, ifp) & 0xffff; |
765 |
|
} |
766 |
|
if (feof(ifp)) |
767 |
|
goto badEOF; |