ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfrep.c
(Generate patch)

Comparing ray/src/cv/bsdfrep.c (file contents):
Revision 2.1 by greg, Fri Oct 19 04:14:29 2012 UTC vs.
Revision 2.35 by greg, Tue Sep 7 20:13:13 2021 UTC

# Line 13 | Line 13 | static const char RCSid[] = "$Id$";
13   #include "rtio.h"
14   #include "resolu.h"
15   #include "bsdfrep.h"
16 <                                /* which quadrants are represented */
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;
# Line 22 | Line 29 | int                    single_plane_incident = -1;
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  
# Line 31 | Line 53 | MIGRATION              *mig_list = NULL;
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   {
38        if (!input_orient)              /* check input orientation */
39                input_orient = 1 - 2*(new_theta > 90.);
40        else if (input_orient > 0 ^ new_theta < 90.) {
41                fprintf(stderr,
42                "%s: Cannot handle input angles on both sides of surface\n",
43                                progname);
44                return(0);
45        }
63                                          /* normalize angle ranges */
64          while (new_theta < -180.)
65                  new_theta += 360.;
# Line 56 | Line 73 | new_input_direction(double new_theta, double new_phi)
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;
63        theta_in_deg = new_theta;       /* assume it's OK */
91          phi_in_deg = new_phi;
92          if ((1. < new_phi) & (new_phi < 89.))
93                  inp_coverage |= INP_QUAD1;
# Line 77 | Line 104 | new_input_direction(double new_theta, double new_phi)
104   int
105   use_symmetry(FVECT vec)
106   {
107 <        double  phi = get_phi360(vec);
107 >        const double    phi = get_phi360(vec);
108  
109          switch (inp_coverage) {
110          case INP_QUAD1|INP_QUAD2|INP_QUAD3|INP_QUAD4:
# Line 167 | Line 194 | rev_rbf_symmetry(RBFNODE *rbf, int sym)
194          rev_symmetry(rbf->invec, sym);
195          if (sym & MIRROR_X)
196                  for (n = rbf->nrbf; n-- > 0; )
197 <                        rbf->rbfa[n].gx = GRIDRES-1 - rbf->rbfa[n].gx;
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 = GRIDRES-1 - rbf->rbfa[n].gy;
200 >                        rbf->rbfa[n].gy = grid_res-1 - rbf->rbfa[n].gy;
201   }
202  
203 < /* Compute volume associated with Gaussian lobe */
204 < double
205 < rbf_volume(const RBFVAL *rbfp)
203 > /* Rotate RBF to correspond to given incident vector */
204 > void
205 > rotate_rbf(RBFNODE *rbf, const FVECT invec)
206   {
207 <        double  rad = R2ANG(rbfp->crad);
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 <        return((2.*M_PI) * rbfp->peak * rad*rad);
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 */
# Line 189 | Line 228 | ovec_from_pos(FVECT vec, int xpos, int ypos)
228          double  uv[2];
229          double  r2;
230          
231 <        SDsquare2disk(uv, (1./GRIDRES)*(xpos+.5), (1./GRIDRES)*(ypos+.5));
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);
# Line 207 | Line 246 | pos_from_vec(int pos[2], const FVECT vec)
246  
247          SDdisk2square(sq, vec[0]*norm, vec[1]*norm);
248  
249 <        pos[0] = (int)(sq[0]*GRIDRES);
250 <        pos[1] = (int)(sq[1]*GRIDRES);
249 >        pos[0] = (int)(sq[0]*grid_res);
250 >        pos[1] = (int)(sq[1]*grid_res);
251   }
252  
253 < /* Evaluate RBF for DSF at the given normalized outgoing direction */
253 > /* Compute volume associated with Gaussian lobe */
254   double
255 < eval_rbfrep(const RBFNODE *rp, const FVECT outvec)
255 > rbf_volume(const RBFVAL *rbfp)
256   {
257 <        double          res = .0;
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          sig2;
283 >        double          rad2;
284          int             n;
285 <
286 <        if (rp == NULL)
287 <                return(.0);
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 <                sig2 = R2ANG(rbfp->crad);
309 <                sig2 = (DOT(odir,outvec) - 1.) / (sig2*sig2);
310 <                if (sig2 > -19.)
311 <                        res += rbfp->peak * exp(sig2);
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 <        return(res);
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)
# Line 244 | Line 345 | insert_dsf(RBFNODE *newrbf)
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)\n",
349 <                                        progname);
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                  }
# Line 280 | Line 382 | get_dsf(int ord)
382          RBFNODE         *rbf;
383  
384          for (rbf = dsf_list; rbf != NULL; rbf = rbf->next)
385 <                if (rbf->ord == ord);
385 >                if (rbf->ord == ord)
386                          return(rbf);
387          return(NULL);
388   }
# Line 311 | Line 413 | is_rev_tri(const FVECT v1, const FVECT v2, const FVECT
413   int
414   get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
415   {
416 <        const MIGRATION *ej, *ej2;
416 >        const MIGRATION *ej1, *ej2;
417          RBFNODE         *tv;
418  
419          rbfv[0] = rbfv[1] = NULL;
420          if (mig == NULL)
421                  return(0);
422 <        for (ej = mig->rbfv[0]->ejl; ej != NULL;
423 <                                ej = nextedge(mig->rbfv[0],ej)) {
424 <                if (ej == mig)
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],ej);
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,
# Line 333 | Line 435 | get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
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 <= .001) {                        /* 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-.001) {              /* 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)
# Line 341 | Line 598 | save_bsdf_rep(FILE *ofp)
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);
# Line 353 | Line 622 | save_bsdf_rep(FILE *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, 1, ofp);
628 <                        putint(rbf->rbfa[i].gy, 1, 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_list->next) {
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 <                        putflt(mig->mtx[i], ofp);
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);
# Line 376 | Line 657 | save_bsdf_rep(FILE *ofp)
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)
# Line 383 | Line 722 | load_bsdf_rep(FILE *ifp)
722          RBFNODE         rbfh;
723          int             from_ord, to_ord;
724          int             i;
725 < #ifdef DEBUG
726 <        if ((dsf_list != NULL) | (mig_list != NULL)) {
727 <                fprintf(stderr,
728 <                "%s: attempt to load BSDF interpolant over existing\n",
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 < #endif
737 <        if (checkheader(ifp, BSDFREP_FMT, NULL) <= 0) {
395 <                fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n",
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 <        rbfh.next = NULL;               /* read each DSF */
400 <        rbfh.ejl = NULL;
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 <                rbfh.nrbf = getint(4, ifp);
749 <                if (!new_input_vector(rbfh.invec))
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 <                memcpy(newrbf, &rbfh, sizeof(RBFNODE));
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(1, ifp) & 0xff;
764 <                        newrbf->rbfa[i].gy = getint(1, ifp) & 0xff;
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;
# Line 447 | Line 792 | load_bsdf_rep(FILE *ifp)
792                          goto memerr;
793                  newmig->rbfv[0] = from_rbf;
794                  newmig->rbfv[1] = to_rbf;
795 <                                        /* read matrix coefficients */
796 <                for (i = 0; i < n; i++)
797 <                        newmig->mtx[i] = getflt(ifp);
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 */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines