14 |
|
#include "rtio.h" |
15 |
|
#include "resolu.h" |
16 |
|
#include "bsdfrep.h" |
17 |
< |
/* which quadrants are represented */ |
17 |
> |
/* active grid resolution */ |
18 |
> |
int grid_res = GRIDRES; |
19 |
> |
|
20 |
> |
/* coverage/symmetry using INP_QUAD? flags */ |
21 |
|
int inp_coverage = 0; |
22 |
|
/* all incident angles in-plane so far? */ |
23 |
|
int single_plane_incident = -1; |
56 |
|
new_theta = -new_theta; |
57 |
|
new_phi += 180.; |
58 |
|
} |
59 |
+ |
if ((theta_in_deg = new_theta) < 1.0) |
60 |
+ |
return(1); /* don't rely on phi near normal */ |
61 |
|
while (new_phi < 0) |
62 |
|
new_phi += 360.; |
63 |
|
while (new_phi >= 360.) |
66 |
|
single_plane_incident = (round(new_phi) == round(phi_in_deg)); |
67 |
|
else if (single_plane_incident < 0) |
68 |
|
single_plane_incident = 1; |
64 |
– |
theta_in_deg = new_theta; /* assume it's OK */ |
69 |
|
phi_in_deg = new_phi; |
70 |
|
if ((1. < new_phi) & (new_phi < 89.)) |
71 |
|
inp_coverage |= INP_QUAD1; |
172 |
|
rev_symmetry(rbf->invec, sym); |
173 |
|
if (sym & MIRROR_X) |
174 |
|
for (n = rbf->nrbf; n-- > 0; ) |
175 |
< |
rbf->rbfa[n].gx = GRIDRES-1 - rbf->rbfa[n].gx; |
175 |
> |
rbf->rbfa[n].gx = grid_res-1 - rbf->rbfa[n].gx; |
176 |
|
if (sym & MIRROR_Y) |
177 |
|
for (n = rbf->nrbf; n-- > 0; ) |
178 |
< |
rbf->rbfa[n].gy = GRIDRES-1 - rbf->rbfa[n].gy; |
178 |
> |
rbf->rbfa[n].gy = grid_res-1 - rbf->rbfa[n].gy; |
179 |
|
} |
180 |
|
|
181 |
+ |
/* Rotate RBF to correspond to given incident vector */ |
182 |
+ |
void |
183 |
+ |
rotate_rbf(RBFNODE *rbf, const FVECT invec) |
184 |
+ |
{ |
185 |
+ |
static const FVECT vnorm = {.0, .0, 1.}; |
186 |
+ |
const double phi = atan2(invec[1],invec[0]) - |
187 |
+ |
atan2(rbf->invec[1],rbf->invec[0]); |
188 |
+ |
FVECT outvec; |
189 |
+ |
int pos[2]; |
190 |
+ |
int n; |
191 |
+ |
#ifdef DEBUG |
192 |
+ |
{ |
193 |
+ |
double tdiff = 180./M_PI*fabs(acos(invec[2])-acos(rbf->invec[2])); |
194 |
+ |
if (tdiff >= 1.5) |
195 |
+ |
fprintf(stderr, |
196 |
+ |
"%s: Warning - rotated theta differs by %.1f degrees\n", |
197 |
+ |
progname, tdiff); |
198 |
+ |
} |
199 |
+ |
#endif |
200 |
+ |
for (n = rbf->nrbf; n-- > 0; ) { |
201 |
+ |
ovec_from_pos(outvec, rbf->rbfa[n].gx, rbf->rbfa[n].gy); |
202 |
+ |
spinvector(outvec, outvec, vnorm, phi); |
203 |
+ |
pos_from_vec(pos, outvec); |
204 |
+ |
rbf->rbfa[n].gx = pos[0]; |
205 |
+ |
rbf->rbfa[n].gy = pos[1]; |
206 |
+ |
} |
207 |
+ |
VCOPY(rbf->invec, invec); |
208 |
+ |
} |
209 |
+ |
|
210 |
|
/* Compute volume associated with Gaussian lobe */ |
211 |
|
double |
212 |
|
rbf_volume(const RBFVAL *rbfp) |
223 |
|
double uv[2]; |
224 |
|
double r2; |
225 |
|
|
226 |
< |
SDsquare2disk(uv, (1./GRIDRES)*(xpos+.5), (1./GRIDRES)*(ypos+.5)); |
226 |
> |
SDsquare2disk(uv, (1./grid_res)*(xpos+.5), (1./grid_res)*(ypos+.5)); |
227 |
|
/* uniform hemispherical projection */ |
228 |
|
r2 = uv[0]*uv[0] + uv[1]*uv[1]; |
229 |
|
vec[0] = vec[1] = sqrt(2. - r2); |
241 |
|
|
242 |
|
SDdisk2square(sq, vec[0]*norm, vec[1]*norm); |
243 |
|
|
244 |
< |
pos[0] = (int)(sq[0]*GRIDRES); |
245 |
< |
pos[1] = (int)(sq[1]*GRIDRES); |
244 |
> |
pos[0] = (int)(sq[0]*grid_res); |
245 |
> |
pos[1] = (int)(sq[1]*grid_res); |
246 |
|
} |
247 |
|
|
248 |
|
/* Evaluate RBF for DSF at the given normalized outgoing direction */ |
314 |
|
RBFNODE *rbf; |
315 |
|
|
316 |
|
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) |
317 |
< |
if (rbf->ord == ord); |
317 |
> |
if (rbf->ord == ord) |
318 |
|
return(rbf); |
319 |
|
return(NULL); |
320 |
|
} |
345 |
|
int |
346 |
|
get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig) |
347 |
|
{ |
348 |
< |
const MIGRATION *ej, *ej2; |
348 |
> |
const MIGRATION *ej1, *ej2; |
349 |
|
RBFNODE *tv; |
350 |
|
|
351 |
|
rbfv[0] = rbfv[1] = NULL; |
352 |
|
if (mig == NULL) |
353 |
|
return(0); |
354 |
< |
for (ej = mig->rbfv[0]->ejl; ej != NULL; |
355 |
< |
ej = nextedge(mig->rbfv[0],ej)) { |
356 |
< |
if (ej == mig) |
354 |
> |
for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL; |
355 |
> |
ej1 = nextedge(mig->rbfv[0],ej1)) { |
356 |
> |
if (ej1 == mig) |
357 |
|
continue; |
358 |
< |
tv = opp_rbf(mig->rbfv[0],ej); |
358 |
> |
tv = opp_rbf(mig->rbfv[0],ej1); |
359 |
|
for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2)) |
360 |
|
if (opp_rbf(tv,ej2) == mig->rbfv[1]) { |
361 |
|
rbfv[is_rev_tri(mig->rbfv[0]->invec, |
367 |
|
return((rbfv[0] != NULL) + (rbfv[1] != NULL)); |
368 |
|
} |
369 |
|
|
370 |
+ |
/* Clear our BSDF representation and free memory */ |
371 |
+ |
void |
372 |
+ |
clear_bsdf_rep(void) |
373 |
+ |
{ |
374 |
+ |
while (mig_list != NULL) { |
375 |
+ |
MIGRATION *mig = mig_list; |
376 |
+ |
mig_list = mig->next; |
377 |
+ |
free(mig); |
378 |
+ |
} |
379 |
+ |
while (dsf_list != NULL) { |
380 |
+ |
RBFNODE *rbf = dsf_list; |
381 |
+ |
dsf_list = rbf->next; |
382 |
+ |
free(rbf); |
383 |
+ |
} |
384 |
+ |
inp_coverage = 0; |
385 |
+ |
single_plane_incident = -1; |
386 |
+ |
input_orient = output_orient = 0; |
387 |
+ |
grid_res = GRIDRES; |
388 |
+ |
} |
389 |
+ |
|
390 |
|
/* Write our BSDF mesh interpolant out to the given binary stream */ |
391 |
|
void |
392 |
|
save_bsdf_rep(FILE *ofp) |
397 |
|
/* finish header */ |
398 |
|
fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage); |
399 |
|
fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient); |
400 |
+ |
fprintf(ofp, "GRIDRES=%d\n", grid_res); |
401 |
|
fputformat(BSDFREP_FMT, ofp); |
402 |
|
fputc('\n', ofp); |
403 |
|
/* write each DSF */ |
424 |
|
/* write out as sparse data */ |
425 |
|
n = mtx_nrows(mig) * mtx_ncols(mig); |
426 |
|
for (i = 0; i < n; i++) { |
427 |
< |
if (zerocnt >= 0xff) { |
428 |
< |
putint(zerocnt, 1, ofp); zerocnt = 0; |
427 |
> |
if (zerocnt == 0xff) { |
428 |
> |
putint(0xff, 1, ofp); zerocnt = 0; |
429 |
|
} |
430 |
|
if (mig->mtx[i] != 0) { |
431 |
|
putint(zerocnt, 1, ofp); zerocnt = 0; |
459 |
|
sscanf(s+9, "%d %d", &input_orient, &output_orient); |
460 |
|
return(0); |
461 |
|
} |
462 |
+ |
if (!strncmp(s, "GRIDRES=", 8)) { |
463 |
+ |
sscanf(s+8, "%d", &grid_res); |
464 |
+ |
return(0); |
465 |
+ |
} |
466 |
|
if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT)) |
467 |
|
return(-1); |
468 |
|
return(0); |
475 |
|
RBFNODE rbfh; |
476 |
|
int from_ord, to_ord; |
477 |
|
int i; |
478 |
< |
#ifdef DEBUG |
479 |
< |
if ((dsf_list != NULL) | (mig_list != NULL)) { |
480 |
< |
fprintf(stderr, |
423 |
< |
"%s: attempt to load BSDF interpolant over existing\n", |
424 |
< |
progname); |
478 |
> |
|
479 |
> |
clear_bsdf_rep(); |
480 |
> |
if (ifp == NULL) |
481 |
|
return(0); |
426 |
– |
} |
427 |
– |
#endif |
428 |
– |
input_orient = output_orient = 0; |
429 |
– |
single_plane_incident = -1; |
482 |
|
if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 | |
483 |
|
!input_orient | !output_orient) { |
484 |
|
fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n", |
493 |
|
rbfh.invec[0] = getflt(ifp); |
494 |
|
rbfh.invec[1] = getflt(ifp); |
495 |
|
rbfh.invec[2] = getflt(ifp); |
496 |
+ |
rbfh.vtotal = getflt(ifp); |
497 |
|
rbfh.nrbf = getint(4, ifp); |
445 |
– |
if (!new_input_vector(rbfh.invec)) |
446 |
– |
return(0); |
498 |
|
newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) + |
499 |
|
sizeof(RBFVAL)*(rbfh.nrbf-1)); |
500 |
|
if (newrbf == NULL) |
538 |
|
memset(newmig->mtx, 0, sizeof(float)*n); |
539 |
|
for (i = 0; ; ) { /* read sparse data */ |
540 |
|
int zc = getint(1, ifp) & 0xff; |
490 |
– |
if (zc == 0xff) { |
491 |
– |
i += 0xff; |
492 |
– |
continue; |
493 |
– |
} |
541 |
|
if ((i += zc) >= n) |
542 |
|
break; |
543 |
+ |
if (zc == 0xff) |
544 |
+ |
continue; |
545 |
|
newmig->mtx[i++] = getflt(ifp); |
546 |
|
} |
547 |
|
if (feof(ifp)) |