9 |
|
|
10 |
|
#define _USE_MATH_DEFINES |
11 |
|
#include <stdlib.h> |
12 |
+ |
#include <string.h> |
13 |
|
#include <math.h> |
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; |
171 |
|
rev_symmetry(rbf->invec, sym); |
172 |
|
if (sym & MIRROR_X) |
173 |
|
for (n = rbf->nrbf; n-- > 0; ) |
174 |
< |
rbf->rbfa[n].gx = GRIDRES-1 - rbf->rbfa[n].gx; |
174 |
> |
rbf->rbfa[n].gx = grid_res-1 - rbf->rbfa[n].gx; |
175 |
|
if (sym & MIRROR_Y) |
176 |
|
for (n = rbf->nrbf; n-- > 0; ) |
177 |
< |
rbf->rbfa[n].gy = GRIDRES-1 - rbf->rbfa[n].gy; |
177 |
> |
rbf->rbfa[n].gy = grid_res-1 - rbf->rbfa[n].gy; |
178 |
|
} |
179 |
|
|
180 |
|
/* Compute volume associated with Gaussian lobe */ |
193 |
|
double uv[2]; |
194 |
|
double r2; |
195 |
|
|
196 |
< |
SDsquare2disk(uv, (1./GRIDRES)*(xpos+.5), (1./GRIDRES)*(ypos+.5)); |
196 |
> |
SDsquare2disk(uv, (1./grid_res)*(xpos+.5), (1./grid_res)*(ypos+.5)); |
197 |
|
/* uniform hemispherical projection */ |
198 |
|
r2 = uv[0]*uv[0] + uv[1]*uv[1]; |
199 |
|
vec[0] = vec[1] = sqrt(2. - r2); |
211 |
|
|
212 |
|
SDdisk2square(sq, vec[0]*norm, vec[1]*norm); |
213 |
|
|
214 |
< |
pos[0] = (int)(sq[0]*GRIDRES); |
215 |
< |
pos[1] = (int)(sq[1]*GRIDRES); |
214 |
> |
pos[0] = (int)(sq[0]*grid_res); |
215 |
> |
pos[1] = (int)(sq[1]*grid_res); |
216 |
|
} |
217 |
|
|
218 |
|
/* Evaluate RBF for DSF at the given normalized outgoing direction */ |
284 |
|
RBFNODE *rbf; |
285 |
|
|
286 |
|
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) |
287 |
< |
if (rbf->ord == ord); |
287 |
> |
if (rbf->ord == ord) |
288 |
|
return(rbf); |
289 |
|
return(NULL); |
290 |
|
} |
315 |
|
int |
316 |
|
get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig) |
317 |
|
{ |
318 |
< |
const MIGRATION *ej, *ej2; |
318 |
> |
const MIGRATION *ej1, *ej2; |
319 |
|
RBFNODE *tv; |
320 |
|
|
321 |
|
rbfv[0] = rbfv[1] = NULL; |
322 |
|
if (mig == NULL) |
323 |
|
return(0); |
324 |
< |
for (ej = mig->rbfv[0]->ejl; ej != NULL; |
325 |
< |
ej = nextedge(mig->rbfv[0],ej)) { |
326 |
< |
if (ej == mig) |
324 |
> |
for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL; |
325 |
> |
ej1 = nextedge(mig->rbfv[0],ej1)) { |
326 |
> |
if (ej1 == mig) |
327 |
|
continue; |
328 |
< |
tv = opp_rbf(mig->rbfv[0],ej); |
328 |
> |
tv = opp_rbf(mig->rbfv[0],ej1); |
329 |
|
for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2)) |
330 |
|
if (opp_rbf(tv,ej2) == mig->rbfv[1]) { |
331 |
|
rbfv[is_rev_tri(mig->rbfv[0]->invec, |
337 |
|
return((rbfv[0] != NULL) + (rbfv[1] != NULL)); |
338 |
|
} |
339 |
|
|
340 |
+ |
/* Clear our BSDF representation and free memory */ |
341 |
+ |
void |
342 |
+ |
clear_bsdf_rep(void) |
343 |
+ |
{ |
344 |
+ |
while (mig_list != NULL) { |
345 |
+ |
MIGRATION *mig = mig_list; |
346 |
+ |
mig_list = mig->next; |
347 |
+ |
free(mig); |
348 |
+ |
} |
349 |
+ |
while (dsf_list != NULL) { |
350 |
+ |
RBFNODE *rbf = dsf_list; |
351 |
+ |
dsf_list = rbf->next; |
352 |
+ |
free(rbf); |
353 |
+ |
} |
354 |
+ |
inp_coverage = 0; |
355 |
+ |
single_plane_incident = -1; |
356 |
+ |
input_orient = output_orient = 0; |
357 |
+ |
grid_res = GRIDRES; |
358 |
+ |
} |
359 |
+ |
|
360 |
|
/* Write our BSDF mesh interpolant out to the given binary stream */ |
361 |
|
void |
362 |
|
save_bsdf_rep(FILE *ofp) |
365 |
|
MIGRATION *mig; |
366 |
|
int i, n; |
367 |
|
/* finish header */ |
368 |
+ |
fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage); |
369 |
+ |
fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient); |
370 |
+ |
fprintf(ofp, "GRIDRES=%d\n", grid_res); |
371 |
|
fputformat(BSDFREP_FMT, ofp); |
372 |
|
fputc('\n', ofp); |
373 |
|
/* write each DSF */ |
387 |
|
} |
388 |
|
putint(-1, 4, ofp); /* terminator */ |
389 |
|
/* write each migration matrix */ |
390 |
< |
for (mig = mig_list; mig != NULL; mig = mig_list->next) { |
390 |
> |
for (mig = mig_list; mig != NULL; mig = mig->next) { |
391 |
> |
int zerocnt = 0; |
392 |
|
putint(mig->rbfv[0]->ord, 4, ofp); |
393 |
|
putint(mig->rbfv[1]->ord, 4, ofp); |
394 |
+ |
/* write out as sparse data */ |
395 |
|
n = mtx_nrows(mig) * mtx_ncols(mig); |
396 |
< |
for (i = 0; i < n; i++) |
397 |
< |
putflt(mig->mtx[i], ofp); |
396 |
> |
for (i = 0; i < n; i++) { |
397 |
> |
if (zerocnt == 0xff) { |
398 |
> |
putint(0xff, 1, ofp); zerocnt = 0; |
399 |
> |
} |
400 |
> |
if (mig->mtx[i] != 0) { |
401 |
> |
putint(zerocnt, 1, ofp); zerocnt = 0; |
402 |
> |
putflt(mig->mtx[i], ofp); |
403 |
> |
} else |
404 |
> |
++zerocnt; |
405 |
> |
} |
406 |
> |
putint(zerocnt, 1, ofp); |
407 |
|
} |
408 |
|
putint(-1, 4, ofp); /* terminator */ |
409 |
|
putint(-1, 4, ofp); |
414 |
|
} |
415 |
|
} |
416 |
|
|
417 |
+ |
/* Check header line for critical information */ |
418 |
+ |
static int |
419 |
+ |
headline(char *s, void *p) |
420 |
+ |
{ |
421 |
+ |
char fmt[32]; |
422 |
+ |
|
423 |
+ |
if (!strncmp(s, "SYMMETRY=", 9)) { |
424 |
+ |
inp_coverage = atoi(s+9); |
425 |
+ |
single_plane_incident = !inp_coverage; |
426 |
+ |
return(0); |
427 |
+ |
} |
428 |
+ |
if (!strncmp(s, "IO_SIDES=", 9)) { |
429 |
+ |
sscanf(s+9, "%d %d", &input_orient, &output_orient); |
430 |
+ |
return(0); |
431 |
+ |
} |
432 |
+ |
if (!strncmp(s, "GRIDRES=", 8)) { |
433 |
+ |
sscanf(s+8, "%d", &grid_res); |
434 |
+ |
return(0); |
435 |
+ |
} |
436 |
+ |
if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT)) |
437 |
+ |
return(-1); |
438 |
+ |
return(0); |
439 |
+ |
} |
440 |
+ |
|
441 |
|
/* Read a BSDF mesh interpolant from the given binary stream */ |
442 |
|
int |
443 |
|
load_bsdf_rep(FILE *ifp) |
445 |
|
RBFNODE rbfh; |
446 |
|
int from_ord, to_ord; |
447 |
|
int i; |
448 |
< |
#ifdef DEBUG |
449 |
< |
if ((dsf_list != NULL) | (mig_list != NULL)) { |
450 |
< |
fprintf(stderr, |
389 |
< |
"%s: attempt to load BSDF interpolant over existing\n", |
390 |
< |
progname); |
448 |
> |
|
449 |
> |
clear_bsdf_rep(); |
450 |
> |
if (ifp == NULL) |
451 |
|
return(0); |
452 |
< |
} |
453 |
< |
#endif |
394 |
< |
if (checkheader(ifp, BSDFREP_FMT, NULL) <= 0) { |
452 |
> |
if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 | |
453 |
> |
!input_orient | !output_orient) { |
454 |
|
fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n", |
455 |
|
progname); |
456 |
|
return(0); |
463 |
|
rbfh.invec[0] = getflt(ifp); |
464 |
|
rbfh.invec[1] = getflt(ifp); |
465 |
|
rbfh.invec[2] = getflt(ifp); |
466 |
+ |
rbfh.vtotal = getflt(ifp); |
467 |
|
rbfh.nrbf = getint(4, ifp); |
408 |
– |
if (!new_input_vector(rbfh.invec)) |
409 |
– |
return(0); |
468 |
|
newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) + |
469 |
|
sizeof(RBFVAL)*(rbfh.nrbf-1)); |
470 |
|
if (newrbf == NULL) |
505 |
|
goto memerr; |
506 |
|
newmig->rbfv[0] = from_rbf; |
507 |
|
newmig->rbfv[1] = to_rbf; |
508 |
< |
/* read matrix coefficients */ |
509 |
< |
for (i = 0; i < n; i++) |
510 |
< |
newmig->mtx[i] = getflt(ifp); |
508 |
> |
memset(newmig->mtx, 0, sizeof(float)*n); |
509 |
> |
for (i = 0; ; ) { /* read sparse data */ |
510 |
> |
int zc = getint(1, ifp) & 0xff; |
511 |
> |
if ((i += zc) >= n) |
512 |
> |
break; |
513 |
> |
if (zc == 0xff) |
514 |
> |
continue; |
515 |
> |
newmig->mtx[i++] = getflt(ifp); |
516 |
> |
} |
517 |
|
if (feof(ifp)) |
518 |
|
goto badEOF; |
519 |
|
/* insert in edge lists */ |