ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfrep.c
Revision: 2.34
Committed: Mon Oct 26 21:12:20 2020 UTC (3 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.33: +18 -9 lines
Log Message:
feat(bsdf2klems, bsdf2ttree): added XML header comments copying useful information from SIR inputs

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdfrep.c,v 2.33 2019/12/28 18:05:14 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 <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];
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 /* 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;
49
50 /* RBF-linking matrices (edges) */
51 MIGRATION *mig_list = NULL;
52
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 {
63 /* normalize angle ranges */
64 while (new_theta < -180.)
65 new_theta += 360.;
66 while (new_theta > 180.)
67 new_theta -= 360.;
68 if (new_theta < 0) {
69 new_theta = -new_theta;
70 new_phi += 180.;
71 }
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)
90 single_plane_incident = 1;
91 phi_in_deg = new_phi;
92 if ((1. < new_phi) & (new_phi < 89.))
93 inp_coverage |= INP_QUAD1;
94 else if ((91. < new_phi) & (new_phi < 179.))
95 inp_coverage |= INP_QUAD2;
96 else if ((181. < new_phi) & (new_phi < 269.))
97 inp_coverage |= INP_QUAD3;
98 else if ((271. < new_phi) & (new_phi < 359.))
99 inp_coverage |= INP_QUAD4;
100 return(1);
101 }
102
103 /* Apply symmetry to the given vector based on distribution */
104 int
105 use_symmetry(FVECT vec)
106 {
107 const double phi = get_phi360(vec);
108
109 switch (inp_coverage) {
110 case INP_QUAD1|INP_QUAD2|INP_QUAD3|INP_QUAD4:
111 break;
112 case INP_QUAD1|INP_QUAD2:
113 if ((-FTINY > phi) | (phi > 180.+FTINY))
114 goto mir_y;
115 break;
116 case INP_QUAD2|INP_QUAD3:
117 if ((90.-FTINY > phi) | (phi > 270.+FTINY))
118 goto mir_x;
119 break;
120 case INP_QUAD3|INP_QUAD4:
121 if ((180.-FTINY > phi) | (phi > 360.+FTINY))
122 goto mir_y;
123 break;
124 case INP_QUAD4|INP_QUAD1:
125 if ((270.-FTINY > phi) & (phi > 90.+FTINY))
126 goto mir_x;
127 break;
128 case INP_QUAD1:
129 if ((-FTINY > phi) | (phi > 90.+FTINY))
130 switch ((int)(phi*(1./90.))) {
131 case 1: goto mir_x;
132 case 2: goto mir_xy;
133 case 3: goto mir_y;
134 }
135 break;
136 case INP_QUAD2:
137 if ((90.-FTINY > phi) | (phi > 180.+FTINY))
138 switch ((int)(phi*(1./90.))) {
139 case 0: goto mir_x;
140 case 2: goto mir_y;
141 case 3: goto mir_xy;
142 }
143 break;
144 case INP_QUAD3:
145 if ((180.-FTINY > phi) | (phi > 270.+FTINY))
146 switch ((int)(phi*(1./90.))) {
147 case 0: goto mir_xy;
148 case 1: goto mir_y;
149 case 3: goto mir_x;
150 }
151 break;
152 case INP_QUAD4:
153 if ((270.-FTINY > phi) | (phi > 360.+FTINY))
154 switch ((int)(phi*(1./90.))) {
155 case 0: goto mir_y;
156 case 1: goto mir_xy;
157 case 2: goto mir_x;
158 }
159 break;
160 default:
161 fprintf(stderr, "%s: Illegal input coverage (%d)\n",
162 progname, inp_coverage);
163 exit(1);
164 }
165 return(0); /* in range */
166 mir_x:
167 vec[0] = -vec[0];
168 return(MIRROR_X);
169 mir_y:
170 vec[1] = -vec[1];
171 return(MIRROR_Y);
172 mir_xy:
173 vec[0] = -vec[0];
174 vec[1] = -vec[1];
175 return(MIRROR_X|MIRROR_Y);
176 }
177
178 /* Reverse symmetry based on what was done before */
179 void
180 rev_symmetry(FVECT vec, int sym)
181 {
182 if (sym & MIRROR_X)
183 vec[0] = -vec[0];
184 if (sym & MIRROR_Y)
185 vec[1] = -vec[1];
186 }
187
188 /* Reverse symmetry for an RBF distribution */
189 void
190 rev_rbf_symmetry(RBFNODE *rbf, int sym)
191 {
192 int n;
193
194 rev_symmetry(rbf->invec, sym);
195 if (sym & MIRROR_X)
196 for (n = rbf->nrbf; n-- > 0; )
197 rbf->rbfa[n].gx = grid_res-1 - rbf->rbfa[n].gx;
198 if (sym & MIRROR_Y)
199 for (n = rbf->nrbf; n-- > 0; )
200 rbf->rbfa[n].gy = grid_res-1 - rbf->rbfa[n].gy;
201 }
202
203 /* Rotate RBF to correspond to given incident vector */
204 void
205 rotate_rbf(RBFNODE *rbf, const FVECT invec)
206 {
207 static const FVECT vnorm = {.0, .0, 1.};
208 const double phi = atan2(invec[1],invec[0]) -
209 atan2(rbf->invec[1],rbf->invec[0]);
210 FVECT outvec;
211 int pos[2];
212 int n;
213
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);
218 rbf->rbfa[n].gx = pos[0];
219 rbf->rbfa[n].gy = pos[1];
220 }
221 VCOPY(rbf->invec, invec);
222 }
223
224 /* Compute outgoing vector from grid position */
225 void
226 ovec_from_pos(FVECT vec, int xpos, int ypos)
227 {
228 double uv[2];
229 double r2;
230
231 SDsquare2disk(uv, (xpos+.5)/grid_res, (ypos+.5)/grid_res);
232 /* uniform hemispherical projection */
233 r2 = uv[0]*uv[0] + uv[1]*uv[1];
234 vec[0] = vec[1] = sqrt(2. - r2);
235 vec[0] *= uv[0];
236 vec[1] *= uv[1];
237 vec[2] = output_orient*(1. - r2);
238 }
239
240 /* Compute grid position from normalized input/output vector */
241 void
242 pos_from_vec(int pos[2], const FVECT vec)
243 {
244 double sq[2]; /* uniform hemispherical projection */
245 double norm = 1./sqrt(1. + fabs(vec[2]));
246
247 SDdisk2square(sq, vec[0]*norm, vec[1]*norm);
248
249 pos[0] = (int)(sq[0]*grid_res);
250 pos[1] = (int)(sq[1]*grid_res);
251 }
252
253 /* Compute volume associated with Gaussian lobe */
254 double
255 rbf_volume(const RBFVAL *rbfp)
256 {
257 double rad = R2ANG(rbfp->crad);
258 FVECT odir;
259 double elev, integ;
260 /* infinite integral approximation */
261 integ = (2.*M_PI) * rbfp->peak * rad*rad;
262 /* check if we're near horizon */
263 ovec_from_pos(odir, rbfp->gx, rbfp->gy);
264 elev = output_orient*odir[2];
265 /* apply cut-off correction if > 1% */
266 if (elev < 2.8*rad) {
267 /* elev = asin(elev); /* this is so crude, anyway... */
268 integ *= 1. - .5*exp(-.5*elev*elev/(rad*rad));
269 }
270 return(integ);
271 }
272
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);
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 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 */
298 rbfp = rp->rbfa;
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 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 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)
341 {
342 RBFNODE *rbf, *rbf_last;
343 int pos;
344 /* check for redundant meas. */
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 at (%.1f,%.1f)\n",
349 progname, get_theta180(newrbf->invec),
350 get_phi360(newrbf->invec));
351 free(newrbf);
352 return(-1);
353 }
354 /* keep in ascending theta order */
355 for (rbf_last = NULL, rbf = dsf_list; rbf != NULL;
356 rbf_last = rbf, rbf = rbf->next)
357 if (single_plane_incident && input_orient*rbf->invec[2] <
358 input_orient*newrbf->invec[2])
359 break;
360 if (rbf_last == NULL) { /* insert new node in list */
361 newrbf->ord = 0;
362 newrbf->next = dsf_list;
363 dsf_list = newrbf;
364 } else {
365 newrbf->ord = rbf_last->ord + 1;
366 newrbf->next = rbf;
367 rbf_last->next = newrbf;
368 }
369 rbf_last = newrbf;
370 while (rbf != NULL) { /* update ordinal positions */
371 rbf->ord = rbf_last->ord + 1;
372 rbf_last = rbf;
373 rbf = rbf->next;
374 }
375 return(newrbf->ord);
376 }
377
378 /* Get the DSF indicated by its ordinal position */
379 RBFNODE *
380 get_dsf(int ord)
381 {
382 RBFNODE *rbf;
383
384 for (rbf = dsf_list; rbf != NULL; rbf = rbf->next)
385 if (rbf->ord == ord)
386 return(rbf);
387 return(NULL);
388 }
389
390 /* Get triangle surface orientation (unnormalized) */
391 void
392 tri_orient(FVECT vres, const FVECT v1, const FVECT v2, const FVECT v3)
393 {
394 FVECT v2minus1, v3minus2;
395
396 VSUB(v2minus1, v2, v1);
397 VSUB(v3minus2, v3, v2);
398 VCROSS(vres, v2minus1, v3minus2);
399 }
400
401 /* Determine if vertex order is reversed (inward normal) */
402 int
403 is_rev_tri(const FVECT v1, const FVECT v2, const FVECT v3)
404 {
405 FVECT tor;
406
407 tri_orient(tor, v1, v2, v3);
408
409 return(DOT(tor, v2) < 0.);
410 }
411
412 /* Find vertices completing triangles on either side of the given edge */
413 int
414 get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
415 {
416 const MIGRATION *ej1, *ej2;
417 RBFNODE *tv;
418
419 rbfv[0] = rbfv[1] = NULL;
420 if (mig == NULL)
421 return(0);
422 for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL;
423 ej1 = nextedge(mig->rbfv[0],ej1)) {
424 if (ej1 == mig)
425 continue;
426 tv = opp_rbf(mig->rbfv[0],ej1);
427 for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2))
428 if (opp_rbf(tv,ej2) == mig->rbfv[1]) {
429 rbfv[is_rev_tri(mig->rbfv[0]->invec,
430 mig->rbfv[1]->invec,
431 tv->invec)] = tv;
432 break;
433 }
434 }
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)
474 {
475 double cthresh = FTINY;
476 RBFNODE *rbf;
477 int n, i, j;
478 double t, full_dist;
479 /* get relative position */
480 t = Acos(DOT(invec, mig->rbfv[0]->invec));
481 if (t < M_PI/grid_res) { /* near first DSF */
482 n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[0]->nrbf-1);
483 rbf = (RBFNODE *)malloc(n);
484 if (rbf == NULL)
485 goto memerr;
486 memcpy(rbf, mig->rbfv[0], n); /* just duplicate */
487 rbf->next = NULL; rbf->ejl = NULL;
488 return(rbf);
489 }
490 full_dist = acos(DOT(mig->rbfv[0]->invec, mig->rbfv[1]->invec));
491 if (t > full_dist-M_PI/grid_res) { /* near second DSF */
492 n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[1]->nrbf-1);
493 rbf = (RBFNODE *)malloc(n);
494 if (rbf == NULL)
495 goto memerr;
496 memcpy(rbf, mig->rbfv[1], n); /* just duplicate */
497 rbf->next = NULL; rbf->ejl = NULL;
498 return(rbf);
499 }
500 t /= full_dist;
501 tryagain:
502 n = 0; /* count migrating particles */
503 for (i = 0; i < mtx_nrows(mig); i++)
504 for (j = 0; j < mtx_ncols(mig); j++)
505 n += (mtx_coef(mig,i,j) > cthresh);
506 /* are we over our limit? */
507 if ((lobe_lim > 0) & (n > lobe_lim)) {
508 cthresh = cthresh*2. + 10.*FTINY;
509 goto tryagain;
510 }
511 #ifdef DEBUG
512 fprintf(stderr, "Input RBFs have %d, %d nodes -> output has %d\n",
513 mig->rbfv[0]->nrbf, mig->rbfv[1]->nrbf, n);
514 #endif
515 rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1));
516 if (rbf == NULL)
517 goto memerr;
518 rbf->next = NULL; rbf->ejl = NULL;
519 VCOPY(rbf->invec, invec);
520 rbf->nrbf = n;
521 rbf->vtotal = 1.-t + t*mig->rbfv[1]->vtotal/mig->rbfv[0]->vtotal;
522 n = 0; /* advect RBF lobes */
523 for (i = 0; i < mtx_nrows(mig); i++) {
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];
535 double rad2;
536 FVECT v;
537 int pos[2];
538 rad2 = R2ANG(rbf1j->crad);
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);
552 pos_from_vec(pos, v);
553 rbf->rbfa[n].gx = pos[0];
554 rbf->rbfa[n].gy = pos[1];
555 ++n;
556 }
557 }
558 rbf->vtotal *= mig->rbfv[0]->vtotal; /* turn ratio into actual */
559 return(rbf);
560 memerr:
561 fprintf(stderr, "%s: Out of memory in e_advect_rbf()\n", progname);
562 exit(1);
563 return(NULL); /* pro forma return */
564 }
565
566 /* Clear our BSDF representation and free memory */
567 void
568 clear_bsdf_rep(void)
569 {
570 while (mig_list != NULL) {
571 MIGRATION *mig = mig_list;
572 mig_list = mig->next;
573 free(mig);
574 }
575 while (dsf_list != NULL) {
576 RBFNODE *rbf = dsf_list;
577 dsf_list = rbf->next;
578 free(rbf);
579 }
580 bsdf_name[0] = '\0';
581 bsdf_manuf[0] = '\0';
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 */
594 void
595 save_bsdf_rep(FILE *ofp)
596 {
597 RBFNODE *rbf;
598 MIGRATION *mig;
599 int i, n;
600 /* finish header */
601 if (bsdf_name[0])
602 fprintf(ofp, "NAME=%s\n", bsdf_name);
603 if (bsdf_manuf[0])
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);
618 putflt(rbf->invec[0], ofp);
619 putflt(rbf->invec[1], ofp);
620 putflt(rbf->invec[2], ofp);
621 putflt(rbf->vtotal, 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, 2, ofp);
628 putint(rbf->rbfa[i].gy, 2, ofp);
629 }
630 }
631 putint(-1, 4, ofp); /* terminator */
632 /* write each migration matrix */
633 for (mig = mig_list; mig != NULL; mig = mig->next) {
634 int zerocnt = 0;
635 putint(mig->rbfv[0]->ord, 4, ofp);
636 putint(mig->rbfv[1]->ord, 4, ofp);
637 /* write out as sparse data */
638 n = mtx_nrows(mig) * mtx_ncols(mig);
639 for (i = 0; i < n; i++) {
640 if (zerocnt == 0xff) {
641 putint(0xff, 1, ofp); zerocnt = 0;
642 }
643 if (mig->mtx[i] != 0) {
644 putint(zerocnt, 1, ofp); zerocnt = 0;
645 putflt(mig->mtx[i], ofp);
646 } else
647 ++zerocnt;
648 }
649 putint(zerocnt, 1, ofp);
650 }
651 putint(-1, 4, ofp); /* terminator */
652 putint(-1, 4, ofp);
653 if (fflush(ofp) == EOF) {
654 fprintf(stderr, "%s: error writing BSDF interpolant\n",
655 progname);
656 exit(1);
657 }
658 }
659
660 /* Check header line for critical information */
661 static int
662 headline(char *s, void *p)
663 {
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(1);
683 }
684 if (!strncmp(s, "IO_SIDES=", 9)) {
685 sscanf(s+9, "%d %d", &input_orient, &output_orient);
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(1);
702 }
703 if (!strncmp(s, "BSDFMIN=", 8)) {
704 sscanf(s+8, "%lf", &bsdf_min);
705 return(1);
706 }
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
718 /* Read a BSDF mesh interpolant from the given binary stream */
719 int
720 load_bsdf_rep(FILE *ifp)
721 {
722 RBFNODE rbfh;
723 int from_ord, to_ord;
724 int i;
725
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 |
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;
744
745 rbfh.invec[0] = getflt(ifp);
746 rbfh.invec[1] = getflt(ifp);
747 rbfh.invec[2] = getflt(ifp);
748 if (normalize(rbfh.invec) == 0) {
749 fprintf(stderr, "%s: zero incident vector\n", progname);
750 return(0);
751 }
752 rbfh.vtotal = getflt(ifp);
753 rbfh.nrbf = getint(4, ifp);
754 newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) +
755 sizeof(RBFVAL)*(rbfh.nrbf-1));
756 if (newrbf == NULL)
757 goto memerr;
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(2, ifp) & 0xffff;
764 newrbf->rbfa[i].gy = getint(2, ifp) & 0xffff;
765 }
766 if (feof(ifp))
767 goto badEOF;
768 /* insert in global list */
769 if (insert_dsf(newrbf) != rbfh.ord) {
770 fprintf(stderr, "%s: error adding DSF\n", progname);
771 return(0);
772 }
773 }
774 /* read each migration matrix */
775 while ((from_ord = getint(4, ifp)) >= 0 &&
776 (to_ord = getint(4, ifp)) >= 0) {
777 RBFNODE *from_rbf = get_dsf(from_ord);
778 RBFNODE *to_rbf = get_dsf(to_ord);
779 MIGRATION *newmig;
780 int n;
781
782 if ((from_rbf == NULL) | (to_rbf == NULL)) {
783 fprintf(stderr,
784 "%s: bad DSF reference in migration edge\n",
785 progname);
786 return(0);
787 }
788 n = from_rbf->nrbf * to_rbf->nrbf;
789 newmig = (MIGRATION *)malloc(sizeof(MIGRATION) +
790 sizeof(float)*(n-1));
791 if (newmig == NULL)
792 goto memerr;
793 newmig->rbfv[0] = from_rbf;
794 newmig->rbfv[1] = to_rbf;
795 memset(newmig->mtx, 0, sizeof(float)*n);
796 for (i = 0; ; ) { /* read sparse data */
797 int zc = getint(1, ifp) & 0xff;
798 if ((i += zc) >= n)
799 break;
800 if (zc == 0xff)
801 continue;
802 newmig->mtx[i++] = getflt(ifp);
803 }
804 if (feof(ifp))
805 goto badEOF;
806 /* insert in edge lists */
807 newmig->enxt[0] = from_rbf->ejl;
808 from_rbf->ejl = newmig;
809 newmig->enxt[1] = to_rbf->ejl;
810 to_rbf->ejl = newmig;
811 /* push onto global list */
812 newmig->next = mig_list;
813 mig_list = newmig;
814 }
815 return(1); /* success! */
816 memerr:
817 fprintf(stderr, "%s: Out of memory in load_bsdf_rep()\n", progname);
818 exit(1);
819 badEOF:
820 fprintf(stderr, "%s: Unexpected EOF in load_bsdf_rep()\n", progname);
821 return(0);
822 }