ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfrep.c
Revision: 2.28
Committed: Thu May 28 15:46:28 2015 UTC (8 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R0
Changes since 2.27: +12 -11 lines
Log Message:
Put angle normalization before test that assumes it

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdfrep.c,v 2.27 2014/08/22 05:38:44 greg Exp $";
3 #endif
4 /*
5 * Support BSDF representation as radial basis functions.
6 *
7 * G. Ward
8 */
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 /* name and manufacturer if known */
18 char bsdf_name[256];
19 char bsdf_manuf[256];
20 /* active grid resolution */
21 int grid_res = GRIDRES;
22
23 /* coverage/symmetry using INP_QUAD? flags */
24 int inp_coverage = 0;
25 /* all incident angles in-plane so far? */
26 int single_plane_incident = -1;
27
28 /* input/output orientations */
29 int input_orient = 0;
30 int output_orient = 0;
31
32 /* BSDF histogram */
33 unsigned long bsdf_hist[HISTLEN];
34
35 /* BSDF value for boundary regions */
36 double bsdf_min = 0;
37 double bsdf_spec_peak = 0;
38 double bsdf_spec_rad = 0;
39
40 /* processed incident DSF measurements */
41 RBFNODE *dsf_list = NULL;
42
43 /* RBF-linking matrices (edges) */
44 MIGRATION *mig_list = NULL;
45
46 /* current input direction */
47 double theta_in_deg, phi_in_deg;
48
49 /* Register new input direction */
50 int
51 new_input_direction(double new_theta, double new_phi)
52 {
53 /* normalize angle ranges */
54 while (new_theta < -180.)
55 new_theta += 360.;
56 while (new_theta > 180.)
57 new_theta -= 360.;
58 if (new_theta < 0) {
59 new_theta = -new_theta;
60 new_phi += 180.;
61 }
62 while (new_phi < 0)
63 new_phi += 360.;
64 while (new_phi >= 360.)
65 new_phi -= 360.;
66 /* check input orientation */
67 if (!input_orient)
68 input_orient = 1 - 2*(new_theta > 90.);
69 else if (input_orient > 0 ^ new_theta < 90.) {
70 fprintf(stderr,
71 "%s: Cannot handle input angles on both sides of surface\n",
72 progname);
73 return(0);
74 }
75 if ((theta_in_deg = new_theta) < 1.0)
76 return(1); /* don't rely on phi near normal */
77 if (single_plane_incident > 0) /* check input coverage */
78 single_plane_incident = (round(new_phi) == round(phi_in_deg));
79 else if (single_plane_incident < 0)
80 single_plane_incident = 1;
81 phi_in_deg = new_phi;
82 if ((1. < new_phi) & (new_phi < 89.))
83 inp_coverage |= INP_QUAD1;
84 else if ((91. < new_phi) & (new_phi < 179.))
85 inp_coverage |= INP_QUAD2;
86 else if ((181. < new_phi) & (new_phi < 269.))
87 inp_coverage |= INP_QUAD3;
88 else if ((271. < new_phi) & (new_phi < 359.))
89 inp_coverage |= INP_QUAD4;
90 return(1);
91 }
92
93 /* Apply symmetry to the given vector based on distribution */
94 int
95 use_symmetry(FVECT vec)
96 {
97 const double phi = get_phi360(vec);
98
99 switch (inp_coverage) {
100 case INP_QUAD1|INP_QUAD2|INP_QUAD3|INP_QUAD4:
101 break;
102 case INP_QUAD1|INP_QUAD2:
103 if ((-FTINY > phi) | (phi > 180.+FTINY))
104 goto mir_y;
105 break;
106 case INP_QUAD2|INP_QUAD3:
107 if ((90.-FTINY > phi) | (phi > 270.+FTINY))
108 goto mir_x;
109 break;
110 case INP_QUAD3|INP_QUAD4:
111 if ((180.-FTINY > phi) | (phi > 360.+FTINY))
112 goto mir_y;
113 break;
114 case INP_QUAD4|INP_QUAD1:
115 if ((270.-FTINY > phi) & (phi > 90.+FTINY))
116 goto mir_x;
117 break;
118 case INP_QUAD1:
119 if ((-FTINY > phi) | (phi > 90.+FTINY))
120 switch ((int)(phi*(1./90.))) {
121 case 1: goto mir_x;
122 case 2: goto mir_xy;
123 case 3: goto mir_y;
124 }
125 break;
126 case INP_QUAD2:
127 if ((90.-FTINY > phi) | (phi > 180.+FTINY))
128 switch ((int)(phi*(1./90.))) {
129 case 0: goto mir_x;
130 case 2: goto mir_y;
131 case 3: goto mir_xy;
132 }
133 break;
134 case INP_QUAD3:
135 if ((180.-FTINY > phi) | (phi > 270.+FTINY))
136 switch ((int)(phi*(1./90.))) {
137 case 0: goto mir_xy;
138 case 1: goto mir_y;
139 case 3: goto mir_x;
140 }
141 break;
142 case INP_QUAD4:
143 if ((270.-FTINY > phi) | (phi > 360.+FTINY))
144 switch ((int)(phi*(1./90.))) {
145 case 0: goto mir_y;
146 case 1: goto mir_xy;
147 case 2: goto mir_x;
148 }
149 break;
150 default:
151 fprintf(stderr, "%s: Illegal input coverage (%d)\n",
152 progname, inp_coverage);
153 exit(1);
154 }
155 return(0); /* in range */
156 mir_x:
157 vec[0] = -vec[0];
158 return(MIRROR_X);
159 mir_y:
160 vec[1] = -vec[1];
161 return(MIRROR_Y);
162 mir_xy:
163 vec[0] = -vec[0];
164 vec[1] = -vec[1];
165 return(MIRROR_X|MIRROR_Y);
166 }
167
168 /* Reverse symmetry based on what was done before */
169 void
170 rev_symmetry(FVECT vec, int sym)
171 {
172 if (sym & MIRROR_X)
173 vec[0] = -vec[0];
174 if (sym & MIRROR_Y)
175 vec[1] = -vec[1];
176 }
177
178 /* Reverse symmetry for an RBF distribution */
179 void
180 rev_rbf_symmetry(RBFNODE *rbf, int sym)
181 {
182 int n;
183
184 rev_symmetry(rbf->invec, sym);
185 if (sym & MIRROR_X)
186 for (n = rbf->nrbf; n-- > 0; )
187 rbf->rbfa[n].gx = grid_res-1 - rbf->rbfa[n].gx;
188 if (sym & MIRROR_Y)
189 for (n = rbf->nrbf; n-- > 0; )
190 rbf->rbfa[n].gy = grid_res-1 - rbf->rbfa[n].gy;
191 }
192
193 /* Rotate RBF to correspond to given incident vector */
194 void
195 rotate_rbf(RBFNODE *rbf, const FVECT invec)
196 {
197 static const FVECT vnorm = {.0, .0, 1.};
198 const double phi = atan2(invec[1],invec[0]) -
199 atan2(rbf->invec[1],rbf->invec[0]);
200 FVECT outvec;
201 int pos[2];
202 int n;
203
204 for (n = (cos(phi) < 1.-FTINY)*rbf->nrbf; n-- > 0; ) {
205 ovec_from_pos(outvec, rbf->rbfa[n].gx, rbf->rbfa[n].gy);
206 spinvector(outvec, outvec, vnorm, phi);
207 pos_from_vec(pos, outvec);
208 rbf->rbfa[n].gx = pos[0];
209 rbf->rbfa[n].gy = pos[1];
210 }
211 VCOPY(rbf->invec, invec);
212 }
213
214 /* Compute outgoing vector from grid position */
215 void
216 ovec_from_pos(FVECT vec, int xpos, int ypos)
217 {
218 double uv[2];
219 double r2;
220
221 SDsquare2disk(uv, (xpos+.5)/grid_res, (ypos+.5)/grid_res);
222 /* uniform hemispherical projection */
223 r2 = uv[0]*uv[0] + uv[1]*uv[1];
224 vec[0] = vec[1] = sqrt(2. - r2);
225 vec[0] *= uv[0];
226 vec[1] *= uv[1];
227 vec[2] = output_orient*(1. - r2);
228 }
229
230 /* Compute grid position from normalized input/output vector */
231 void
232 pos_from_vec(int pos[2], const FVECT vec)
233 {
234 double sq[2]; /* uniform hemispherical projection */
235 double norm = 1./sqrt(1. + fabs(vec[2]));
236
237 SDdisk2square(sq, vec[0]*norm, vec[1]*norm);
238
239 pos[0] = (int)(sq[0]*grid_res);
240 pos[1] = (int)(sq[1]*grid_res);
241 }
242
243 /* Compute volume associated with Gaussian lobe */
244 double
245 rbf_volume(const RBFVAL *rbfp)
246 {
247 double rad = R2ANG(rbfp->crad);
248 FVECT odir;
249 double elev, integ;
250 /* infinite integral approximation */
251 integ = (2.*M_PI) * rbfp->peak * rad*rad;
252 /* check if we're near horizon */
253 ovec_from_pos(odir, rbfp->gx, rbfp->gy);
254 elev = output_orient*odir[2];
255 /* apply cut-off correction if > 1% */
256 if (elev < 2.8*rad) {
257 /* elev = asin(elev); /* this is so crude, anyway... */
258 integ *= 1. - .5*exp(-.5*elev*elev/(rad*rad));
259 }
260 return(integ);
261 }
262
263 /* Evaluate BSDF at the given normalized outgoing direction */
264 double
265 eval_rbfrep(const RBFNODE *rp, const FVECT outvec)
266 {
267 const double rfact2 = (38./M_PI/M_PI)*(grid_res*grid_res);
268 int pos[2];
269 double res = 0;
270 const RBFVAL *rbfp;
271 FVECT odir;
272 double rad2;
273 int n;
274 /* check for wrong side */
275 if (outvec[2] > 0 ^ output_orient > 0)
276 return(.0);
277 /* use minimum if no information avail. */
278 if (rp == NULL)
279 return(bsdf_min);
280 /* optimization for fast lobe culling */
281 pos_from_vec(pos, outvec);
282 /* sum radial basis function */
283 rbfp = rp->rbfa;
284 for (n = rp->nrbf; n--; rbfp++) {
285 int d2 = (pos[0]-rbfp->gx)*(pos[0]-rbfp->gx) +
286 (pos[1]-rbfp->gy)*(pos[1]-rbfp->gy);
287 rad2 = R2ANG(rbfp->crad);
288 rad2 *= rad2;
289 if (d2 > rad2*rfact2)
290 continue;
291 ovec_from_pos(odir, rbfp->gx, rbfp->gy);
292 res += rbfp->peak * exp((DOT(odir,outvec) - 1.) / rad2);
293 }
294 res /= COSF(outvec[2]);
295 if (res < bsdf_min) /* never return less than bsdf_min */
296 return(bsdf_min);
297 return(res);
298 }
299
300 /* Insert a new directional scattering function in our global list */
301 int
302 insert_dsf(RBFNODE *newrbf)
303 {
304 RBFNODE *rbf, *rbf_last;
305 int pos;
306 /* check for redundant meas. */
307 for (rbf = dsf_list; rbf != NULL; rbf = rbf->next)
308 if (DOT(rbf->invec, newrbf->invec) >= 1.-FTINY) {
309 fprintf(stderr,
310 "%s: Duplicate incident measurement ignored at (%.1f,%.1f)\n",
311 progname, get_theta180(newrbf->invec),
312 get_phi360(newrbf->invec));
313 free(newrbf);
314 return(-1);
315 }
316 /* keep in ascending theta order */
317 for (rbf_last = NULL, rbf = dsf_list; rbf != NULL;
318 rbf_last = rbf, rbf = rbf->next)
319 if (single_plane_incident && input_orient*rbf->invec[2] <
320 input_orient*newrbf->invec[2])
321 break;
322 if (rbf_last == NULL) { /* insert new node in list */
323 newrbf->ord = 0;
324 newrbf->next = dsf_list;
325 dsf_list = newrbf;
326 } else {
327 newrbf->ord = rbf_last->ord + 1;
328 newrbf->next = rbf;
329 rbf_last->next = newrbf;
330 }
331 rbf_last = newrbf;
332 while (rbf != NULL) { /* update ordinal positions */
333 rbf->ord = rbf_last->ord + 1;
334 rbf_last = rbf;
335 rbf = rbf->next;
336 }
337 return(newrbf->ord);
338 }
339
340 /* Get the DSF indicated by its ordinal position */
341 RBFNODE *
342 get_dsf(int ord)
343 {
344 RBFNODE *rbf;
345
346 for (rbf = dsf_list; rbf != NULL; rbf = rbf->next)
347 if (rbf->ord == ord)
348 return(rbf);
349 return(NULL);
350 }
351
352 /* Get triangle surface orientation (unnormalized) */
353 void
354 tri_orient(FVECT vres, const FVECT v1, const FVECT v2, const FVECT v3)
355 {
356 FVECT v2minus1, v3minus2;
357
358 VSUB(v2minus1, v2, v1);
359 VSUB(v3minus2, v3, v2);
360 VCROSS(vres, v2minus1, v3minus2);
361 }
362
363 /* Determine if vertex order is reversed (inward normal) */
364 int
365 is_rev_tri(const FVECT v1, const FVECT v2, const FVECT v3)
366 {
367 FVECT tor;
368
369 tri_orient(tor, v1, v2, v3);
370
371 return(DOT(tor, v2) < 0.);
372 }
373
374 /* Find vertices completing triangles on either side of the given edge */
375 int
376 get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
377 {
378 const MIGRATION *ej1, *ej2;
379 RBFNODE *tv;
380
381 rbfv[0] = rbfv[1] = NULL;
382 if (mig == NULL)
383 return(0);
384 for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL;
385 ej1 = nextedge(mig->rbfv[0],ej1)) {
386 if (ej1 == mig)
387 continue;
388 tv = opp_rbf(mig->rbfv[0],ej1);
389 for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2))
390 if (opp_rbf(tv,ej2) == mig->rbfv[1]) {
391 rbfv[is_rev_tri(mig->rbfv[0]->invec,
392 mig->rbfv[1]->invec,
393 tv->invec)] = tv;
394 break;
395 }
396 }
397 return((rbfv[0] != NULL) + (rbfv[1] != NULL));
398 }
399
400 /* Return single-lobe specular RBF for the given incident direction */
401 RBFNODE *
402 def_rbf_spec(const FVECT invec)
403 {
404 RBFNODE *rbf;
405 FVECT ovec;
406 int pos[2];
407
408 if (input_orient > 0 ^ invec[2] > 0) /* wrong side? */
409 return(NULL);
410 if ((bsdf_spec_peak <= bsdf_min) | (bsdf_spec_rad <= 0))
411 return(NULL); /* nothing set */
412 rbf = (RBFNODE *)malloc(sizeof(RBFNODE));
413 if (rbf == NULL)
414 return(NULL);
415 ovec[0] = -invec[0];
416 ovec[1] = -invec[1];
417 ovec[2] = invec[2]*(2*(input_orient==output_orient) - 1);
418 pos_from_vec(pos, ovec);
419 rbf->ord = 0;
420 rbf->next = NULL;
421 rbf->ejl = NULL;
422 VCOPY(rbf->invec, invec);
423 rbf->nrbf = 1;
424 rbf->rbfa[0].peak = bsdf_spec_peak * output_orient*ovec[2];
425 rbf->rbfa[0].crad = ANG2R(bsdf_spec_rad);
426 rbf->rbfa[0].gx = pos[0];
427 rbf->rbfa[0].gy = pos[1];
428 rbf->vtotal = rbf_volume(rbf->rbfa);
429 return(rbf);
430 }
431
432 /* Advect and allocate new RBF along edge (internal call) */
433 RBFNODE *
434 e_advect_rbf(const MIGRATION *mig, const FVECT invec, int lobe_lim)
435 {
436 double cthresh = FTINY;
437 RBFNODE *rbf;
438 int n, i, j;
439 double t, full_dist;
440 /* get relative position */
441 t = Acos(DOT(invec, mig->rbfv[0]->invec));
442 if (t < M_PI/grid_res) { /* near first DSF */
443 n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[0]->nrbf-1);
444 rbf = (RBFNODE *)malloc(n);
445 if (rbf == NULL)
446 goto memerr;
447 memcpy(rbf, mig->rbfv[0], n); /* just duplicate */
448 rbf->next = NULL; rbf->ejl = NULL;
449 return(rbf);
450 }
451 full_dist = acos(DOT(mig->rbfv[0]->invec, mig->rbfv[1]->invec));
452 if (t > full_dist-M_PI/grid_res) { /* near second DSF */
453 n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[1]->nrbf-1);
454 rbf = (RBFNODE *)malloc(n);
455 if (rbf == NULL)
456 goto memerr;
457 memcpy(rbf, mig->rbfv[1], n); /* just duplicate */
458 rbf->next = NULL; rbf->ejl = NULL;
459 return(rbf);
460 }
461 t /= full_dist;
462 tryagain:
463 n = 0; /* count migrating particles */
464 for (i = 0; i < mtx_nrows(mig); i++)
465 for (j = 0; j < mtx_ncols(mig); j++)
466 n += (mtx_coef(mig,i,j) > cthresh);
467 /* are we over our limit? */
468 if ((lobe_lim > 0) & (n > lobe_lim)) {
469 cthresh = cthresh*2. + 10.*FTINY;
470 goto tryagain;
471 }
472 #ifdef DEBUG
473 fprintf(stderr, "Input RBFs have %d, %d nodes -> output has %d\n",
474 mig->rbfv[0]->nrbf, mig->rbfv[1]->nrbf, n);
475 #endif
476 rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1));
477 if (rbf == NULL)
478 goto memerr;
479 rbf->next = NULL; rbf->ejl = NULL;
480 VCOPY(rbf->invec, invec);
481 rbf->nrbf = n;
482 rbf->vtotal = 1.-t + t*mig->rbfv[1]->vtotal/mig->rbfv[0]->vtotal;
483 n = 0; /* advect RBF lobes */
484 for (i = 0; i < mtx_nrows(mig); i++) {
485 const RBFVAL *rbf0i = &mig->rbfv[0]->rbfa[i];
486 const float peak0 = rbf0i->peak;
487 const double rad0 = R2ANG(rbf0i->crad);
488 FVECT v0;
489 float mv;
490 ovec_from_pos(v0, rbf0i->gx, rbf0i->gy);
491 for (j = 0; j < mtx_ncols(mig); j++)
492 if ((mv = mtx_coef(mig,i,j)) > cthresh) {
493 const RBFVAL *rbf1j = &mig->rbfv[1]->rbfa[j];
494 double rad2;
495 FVECT v;
496 int pos[2];
497 rad2 = R2ANG(rbf1j->crad);
498 rad2 = rad0*rad0*(1.-t) + rad2*rad2*t;
499 rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal *
500 rad0*rad0/rad2;
501 rbf->rbfa[n].crad = ANG2R(sqrt(rad2));
502 ovec_from_pos(v, rbf1j->gx, rbf1j->gy);
503 geodesic(v, v0, v, t, GEOD_REL);
504 pos_from_vec(pos, v);
505 rbf->rbfa[n].gx = pos[0];
506 rbf->rbfa[n].gy = pos[1];
507 ++n;
508 }
509 }
510 rbf->vtotal *= mig->rbfv[0]->vtotal; /* turn ratio into actual */
511 return(rbf);
512 memerr:
513 fprintf(stderr, "%s: Out of memory in e_advect_rbf()\n", progname);
514 exit(1);
515 return(NULL); /* pro forma return */
516 }
517
518 /* Clear our BSDF representation and free memory */
519 void
520 clear_bsdf_rep(void)
521 {
522 while (mig_list != NULL) {
523 MIGRATION *mig = mig_list;
524 mig_list = mig->next;
525 free(mig);
526 }
527 while (dsf_list != NULL) {
528 RBFNODE *rbf = dsf_list;
529 dsf_list = rbf->next;
530 free(rbf);
531 }
532 bsdf_name[0] = '\0';
533 bsdf_manuf[0] = '\0';
534 inp_coverage = 0;
535 single_plane_incident = -1;
536 input_orient = output_orient = 0;
537 grid_res = GRIDRES;
538 bsdf_min = 0;
539 bsdf_spec_peak = 0;
540 bsdf_spec_rad = 0;
541 }
542
543 /* Write our BSDF mesh interpolant out to the given binary stream */
544 void
545 save_bsdf_rep(FILE *ofp)
546 {
547 RBFNODE *rbf;
548 MIGRATION *mig;
549 int i, n;
550 /* finish header */
551 if (bsdf_name[0])
552 fprintf(ofp, "NAME=%s\n", bsdf_name);
553 if (bsdf_manuf[0])
554 fprintf(ofp, "MANUFACT=%s\n", bsdf_manuf);
555 fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage);
556 fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient);
557 fprintf(ofp, "GRIDRES=%d\n", grid_res);
558 fprintf(ofp, "BSDFMIN=%g\n", bsdf_min);
559 if ((bsdf_spec_peak > bsdf_min) & (bsdf_spec_rad > 0))
560 fprintf(ofp, "BSDFSPEC= %f %f\n", bsdf_spec_peak, bsdf_spec_rad);
561 fputformat(BSDFREP_FMT, ofp);
562 fputc('\n', ofp);
563 /* write each DSF */
564 for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) {
565 putint(rbf->ord, 4, ofp);
566 putflt(rbf->invec[0], ofp);
567 putflt(rbf->invec[1], ofp);
568 putflt(rbf->invec[2], ofp);
569 putflt(rbf->vtotal, ofp);
570 putint(rbf->nrbf, 4, ofp);
571 for (i = 0; i < rbf->nrbf; i++) {
572 putflt(rbf->rbfa[i].peak, ofp);
573 putint(rbf->rbfa[i].crad, 2, ofp);
574 putint(rbf->rbfa[i].gx, 1, ofp);
575 putint(rbf->rbfa[i].gy, 1, ofp);
576 }
577 }
578 putint(-1, 4, ofp); /* terminator */
579 /* write each migration matrix */
580 for (mig = mig_list; mig != NULL; mig = mig->next) {
581 int zerocnt = 0;
582 putint(mig->rbfv[0]->ord, 4, ofp);
583 putint(mig->rbfv[1]->ord, 4, ofp);
584 /* write out as sparse data */
585 n = mtx_nrows(mig) * mtx_ncols(mig);
586 for (i = 0; i < n; i++) {
587 if (zerocnt == 0xff) {
588 putint(0xff, 1, ofp); zerocnt = 0;
589 }
590 if (mig->mtx[i] != 0) {
591 putint(zerocnt, 1, ofp); zerocnt = 0;
592 putflt(mig->mtx[i], ofp);
593 } else
594 ++zerocnt;
595 }
596 putint(zerocnt, 1, ofp);
597 }
598 putint(-1, 4, ofp); /* terminator */
599 putint(-1, 4, ofp);
600 if (fflush(ofp) == EOF) {
601 fprintf(stderr, "%s: error writing BSDF interpolant\n",
602 progname);
603 exit(1);
604 }
605 }
606
607 /* Check header line for critical information */
608 static int
609 headline(char *s, void *p)
610 {
611 char fmt[32];
612
613 if (!strncmp(s, "NAME=", 5)) {
614 strcpy(bsdf_name, s+5);
615 bsdf_name[strlen(bsdf_name)-1] = '\0';
616 }
617 if (!strncmp(s, "MANUFACT=", 9)) {
618 strcpy(bsdf_manuf, s+9);
619 bsdf_manuf[strlen(bsdf_manuf)-1] = '\0';
620 }
621 if (!strncmp(s, "SYMMETRY=", 9)) {
622 inp_coverage = atoi(s+9);
623 single_plane_incident = !inp_coverage;
624 return(0);
625 }
626 if (!strncmp(s, "IO_SIDES=", 9)) {
627 sscanf(s+9, "%d %d", &input_orient, &output_orient);
628 return(0);
629 }
630 if (!strncmp(s, "GRIDRES=", 8)) {
631 sscanf(s+8, "%d", &grid_res);
632 return(0);
633 }
634 if (!strncmp(s, "BSDFMIN=", 8)) {
635 sscanf(s+8, "%lf", &bsdf_min);
636 return(0);
637 }
638 if (!strncmp(s, "BSDFSPEC=", 9)) {
639 sscanf(s+9, "%lf %lf", &bsdf_spec_peak, &bsdf_spec_rad);
640 return(0);
641 }
642 if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT))
643 return(-1);
644 return(0);
645 }
646
647 /* Read a BSDF mesh interpolant from the given binary stream */
648 int
649 load_bsdf_rep(FILE *ifp)
650 {
651 RBFNODE rbfh;
652 int from_ord, to_ord;
653 int i;
654
655 clear_bsdf_rep();
656 if (ifp == NULL)
657 return(0);
658 if (getheader(ifp, headline, NULL) < 0 || (single_plane_incident < 0) |
659 !input_orient | !output_orient |
660 (grid_res < 16) | (grid_res > 256)) {
661 fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n",
662 progname);
663 return(0);
664 }
665 memset(&rbfh, 0, sizeof(rbfh)); /* read each DSF */
666 while ((rbfh.ord = getint(4, ifp)) >= 0) {
667 RBFNODE *newrbf;
668
669 rbfh.invec[0] = getflt(ifp);
670 rbfh.invec[1] = getflt(ifp);
671 rbfh.invec[2] = getflt(ifp);
672 if (normalize(rbfh.invec) == 0) {
673 fprintf(stderr, "%s: zero incident vector\n", progname);
674 return(0);
675 }
676 rbfh.vtotal = getflt(ifp);
677 rbfh.nrbf = getint(4, ifp);
678 newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) +
679 sizeof(RBFVAL)*(rbfh.nrbf-1));
680 if (newrbf == NULL)
681 goto memerr;
682 *newrbf = rbfh;
683 for (i = 0; i < rbfh.nrbf; i++) {
684 newrbf->rbfa[i].peak = getflt(ifp);
685 newrbf->rbfa[i].crad = getint(2, ifp) & 0xffff;
686 newrbf->rbfa[i].gx = getint(1, ifp) & 0xff;
687 newrbf->rbfa[i].gy = getint(1, ifp) & 0xff;
688 }
689 if (feof(ifp))
690 goto badEOF;
691 /* insert in global list */
692 if (insert_dsf(newrbf) != rbfh.ord) {
693 fprintf(stderr, "%s: error adding DSF\n", progname);
694 return(0);
695 }
696 }
697 /* read each migration matrix */
698 while ((from_ord = getint(4, ifp)) >= 0 &&
699 (to_ord = getint(4, ifp)) >= 0) {
700 RBFNODE *from_rbf = get_dsf(from_ord);
701 RBFNODE *to_rbf = get_dsf(to_ord);
702 MIGRATION *newmig;
703 int n;
704
705 if ((from_rbf == NULL) | (to_rbf == NULL)) {
706 fprintf(stderr,
707 "%s: bad DSF reference in migration edge\n",
708 progname);
709 return(0);
710 }
711 n = from_rbf->nrbf * to_rbf->nrbf;
712 newmig = (MIGRATION *)malloc(sizeof(MIGRATION) +
713 sizeof(float)*(n-1));
714 if (newmig == NULL)
715 goto memerr;
716 newmig->rbfv[0] = from_rbf;
717 newmig->rbfv[1] = to_rbf;
718 memset(newmig->mtx, 0, sizeof(float)*n);
719 for (i = 0; ; ) { /* read sparse data */
720 int zc = getint(1, ifp) & 0xff;
721 if ((i += zc) >= n)
722 break;
723 if (zc == 0xff)
724 continue;
725 newmig->mtx[i++] = getflt(ifp);
726 }
727 if (feof(ifp))
728 goto badEOF;
729 /* insert in edge lists */
730 newmig->enxt[0] = from_rbf->ejl;
731 from_rbf->ejl = newmig;
732 newmig->enxt[1] = to_rbf->ejl;
733 to_rbf->ejl = newmig;
734 /* push onto global list */
735 newmig->next = mig_list;
736 mig_list = newmig;
737 }
738 return(1); /* success! */
739 memerr:
740 fprintf(stderr, "%s: Out of memory in load_bsdf_rep()\n", progname);
741 exit(1);
742 badEOF:
743 fprintf(stderr, "%s: Unexpected EOF in load_bsdf_rep()\n", progname);
744 return(0);
745 }