| 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 |
> |
/* coverage/symmetry using INP_QUAD? flags */ |
| 18 |
|
int inp_coverage = 0; |
| 19 |
|
/* all incident angles in-plane so far? */ |
| 20 |
|
int single_plane_incident = -1; |
| 281 |
|
RBFNODE *rbf; |
| 282 |
|
|
| 283 |
|
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) |
| 284 |
< |
if (rbf->ord == ord); |
| 284 |
> |
if (rbf->ord == ord) |
| 285 |
|
return(rbf); |
| 286 |
|
return(NULL); |
| 287 |
|
} |
| 312 |
|
int |
| 313 |
|
get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig) |
| 314 |
|
{ |
| 315 |
< |
const MIGRATION *ej, *ej2; |
| 315 |
> |
const MIGRATION *ej1, *ej2; |
| 316 |
|
RBFNODE *tv; |
| 317 |
|
|
| 318 |
|
rbfv[0] = rbfv[1] = NULL; |
| 319 |
|
if (mig == NULL) |
| 320 |
|
return(0); |
| 321 |
< |
for (ej = mig->rbfv[0]->ejl; ej != NULL; |
| 322 |
< |
ej = nextedge(mig->rbfv[0],ej)) { |
| 323 |
< |
if (ej == mig) |
| 321 |
> |
for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL; |
| 322 |
> |
ej1 = nextedge(mig->rbfv[0],ej1)) { |
| 323 |
> |
if (ej1 == mig) |
| 324 |
|
continue; |
| 325 |
< |
tv = opp_rbf(mig->rbfv[0],ej); |
| 325 |
> |
tv = opp_rbf(mig->rbfv[0],ej1); |
| 326 |
|
for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2)) |
| 327 |
|
if (opp_rbf(tv,ej2) == mig->rbfv[1]) { |
| 328 |
|
rbfv[is_rev_tri(mig->rbfv[0]->invec, |
| 334 |
|
return((rbfv[0] != NULL) + (rbfv[1] != NULL)); |
| 335 |
|
} |
| 336 |
|
|
| 337 |
+ |
/* Clear our BSDF representation and free memory */ |
| 338 |
+ |
void |
| 339 |
+ |
clear_bsdf_rep(void) |
| 340 |
+ |
{ |
| 341 |
+ |
while (mig_list != NULL) { |
| 342 |
+ |
MIGRATION *mig = mig_list; |
| 343 |
+ |
mig_list = mig->next; |
| 344 |
+ |
free(mig); |
| 345 |
+ |
} |
| 346 |
+ |
while (dsf_list != NULL) { |
| 347 |
+ |
RBFNODE *rbf = dsf_list; |
| 348 |
+ |
dsf_list = rbf->next; |
| 349 |
+ |
free(rbf); |
| 350 |
+ |
} |
| 351 |
+ |
inp_coverage = 0; |
| 352 |
+ |
single_plane_incident = -1; |
| 353 |
+ |
input_orient = output_orient = 0; |
| 354 |
+ |
} |
| 355 |
+ |
|
| 356 |
|
/* Write our BSDF mesh interpolant out to the given binary stream */ |
| 357 |
|
void |
| 358 |
|
save_bsdf_rep(FILE *ofp) |
| 361 |
|
MIGRATION *mig; |
| 362 |
|
int i, n; |
| 363 |
|
/* finish header */ |
| 364 |
+ |
fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage); |
| 365 |
+ |
fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient); |
| 366 |
|
fputformat(BSDFREP_FMT, ofp); |
| 367 |
|
fputc('\n', ofp); |
| 368 |
|
/* write each DSF */ |
| 382 |
|
} |
| 383 |
|
putint(-1, 4, ofp); /* terminator */ |
| 384 |
|
/* write each migration matrix */ |
| 385 |
< |
for (mig = mig_list; mig != NULL; mig = mig_list->next) { |
| 385 |
> |
for (mig = mig_list; mig != NULL; mig = mig->next) { |
| 386 |
> |
int zerocnt = 0; |
| 387 |
|
putint(mig->rbfv[0]->ord, 4, ofp); |
| 388 |
|
putint(mig->rbfv[1]->ord, 4, ofp); |
| 389 |
+ |
/* write out as sparse data */ |
| 390 |
|
n = mtx_nrows(mig) * mtx_ncols(mig); |
| 391 |
< |
for (i = 0; i < n; i++) |
| 392 |
< |
putflt(mig->mtx[i], ofp); |
| 391 |
> |
for (i = 0; i < n; i++) { |
| 392 |
> |
if (zerocnt == 0xff) { |
| 393 |
> |
putint(0xff, 1, ofp); zerocnt = 0; |
| 394 |
> |
} |
| 395 |
> |
if (mig->mtx[i] != 0) { |
| 396 |
> |
putint(zerocnt, 1, ofp); zerocnt = 0; |
| 397 |
> |
putflt(mig->mtx[i], ofp); |
| 398 |
> |
} else |
| 399 |
> |
++zerocnt; |
| 400 |
> |
} |
| 401 |
> |
putint(zerocnt, 1, ofp); |
| 402 |
|
} |
| 403 |
|
putint(-1, 4, ofp); /* terminator */ |
| 404 |
|
putint(-1, 4, ofp); |
| 409 |
|
} |
| 410 |
|
} |
| 411 |
|
|
| 412 |
+ |
/* Check header line for critical information */ |
| 413 |
+ |
static int |
| 414 |
+ |
headline(char *s, void *p) |
| 415 |
+ |
{ |
| 416 |
+ |
char fmt[32]; |
| 417 |
+ |
|
| 418 |
+ |
if (!strncmp(s, "SYMMETRY=", 9)) { |
| 419 |
+ |
inp_coverage = atoi(s+9); |
| 420 |
+ |
single_plane_incident = !inp_coverage; |
| 421 |
+ |
return(0); |
| 422 |
+ |
} |
| 423 |
+ |
if (!strncmp(s, "IO_SIDES=", 9)) { |
| 424 |
+ |
sscanf(s+9, "%d %d", &input_orient, &output_orient); |
| 425 |
+ |
return(0); |
| 426 |
+ |
} |
| 427 |
+ |
if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT)) |
| 428 |
+ |
return(-1); |
| 429 |
+ |
return(0); |
| 430 |
+ |
} |
| 431 |
+ |
|
| 432 |
|
/* Read a BSDF mesh interpolant from the given binary stream */ |
| 433 |
|
int |
| 434 |
|
load_bsdf_rep(FILE *ifp) |
| 436 |
|
RBFNODE rbfh; |
| 437 |
|
int from_ord, to_ord; |
| 438 |
|
int i; |
| 439 |
< |
#ifdef DEBUG |
| 440 |
< |
if ((dsf_list != NULL) | (mig_list != NULL)) { |
| 441 |
< |
fprintf(stderr, |
| 442 |
< |
"%s: attempt to load BSDF interpolant over existing\n", |
| 390 |
< |
progname); |
| 391 |
< |
return(0); |
| 392 |
< |
} |
| 393 |
< |
#endif |
| 394 |
< |
if (checkheader(ifp, BSDFREP_FMT, NULL) <= 0) { |
| 439 |
> |
|
| 440 |
> |
clear_bsdf_rep(); |
| 441 |
> |
if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 | |
| 442 |
> |
!input_orient | !output_orient) { |
| 443 |
|
fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n", |
| 444 |
|
progname); |
| 445 |
|
return(0); |
| 452 |
|
rbfh.invec[0] = getflt(ifp); |
| 453 |
|
rbfh.invec[1] = getflt(ifp); |
| 454 |
|
rbfh.invec[2] = getflt(ifp); |
| 455 |
+ |
rbfh.vtotal = getflt(ifp); |
| 456 |
|
rbfh.nrbf = getint(4, ifp); |
| 408 |
– |
if (!new_input_vector(rbfh.invec)) |
| 409 |
– |
return(0); |
| 457 |
|
newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) + |
| 458 |
|
sizeof(RBFVAL)*(rbfh.nrbf-1)); |
| 459 |
|
if (newrbf == NULL) |
| 494 |
|
goto memerr; |
| 495 |
|
newmig->rbfv[0] = from_rbf; |
| 496 |
|
newmig->rbfv[1] = to_rbf; |
| 497 |
< |
/* read matrix coefficients */ |
| 498 |
< |
for (i = 0; i < n; i++) |
| 499 |
< |
newmig->mtx[i] = getflt(ifp); |
| 497 |
> |
memset(newmig->mtx, 0, sizeof(float)*n); |
| 498 |
> |
for (i = 0; ; ) { /* read sparse data */ |
| 499 |
> |
int zc = getint(1, ifp) & 0xff; |
| 500 |
> |
if ((i += zc) >= n) |
| 501 |
> |
break; |
| 502 |
> |
if (zc == 0xff) |
| 503 |
> |
continue; |
| 504 |
> |
newmig->mtx[i++] = getflt(ifp); |
| 505 |
> |
} |
| 506 |
|
if (feof(ifp)) |
| 507 |
|
goto badEOF; |
| 508 |
|
/* insert in edge lists */ |