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

Comparing ray/src/cv/bsdfinterp.c (file contents):
Revision 2.11 by greg, Sat Jun 29 21:03:25 2013 UTC vs.
Revision 2.18 by greg, Mon Mar 24 17:22:33 2014 UTC

# Line 94 | Line 94 | on_edge(const MIGRATION *ej, const FVECT ivec)
94          cos_a = DOT(ej->rbfv[0]->invec, ivec);
95          if (cos_a <= 0)
96                  return(0);
97 +        if (cos_a >= 1.)                /* handles rounding error */
98 +                return(1);
99  
100          cos_b = DOT(ej->rbfv[1]->invec, ivec);
101          if (cos_b <= 0)
102                  return(0);
103 +        if (cos_b >= 1.)
104 +                return(1);
105  
106          cos_aplusb = cos_a*cos_b - sqrt((1.-cos_a*cos_a)*(1.-cos_b*cos_b));
107          if (cos_aplusb <= 0)
# Line 204 | Line 208 | get_interp(MIGRATION *miga[3], FVECT invec)
208          if (single_plane_incident) {            /* isotropic BSDF? */
209              RBFNODE     *rbf;                   /* find edge we're on */
210              for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) {
211 <                if (input_orient*rbf->invec[2] < input_orient*invec[2])
211 >                if (input_orient*rbf->invec[2] < input_orient*invec[2]-FTINY)
212                          break;
213                  if (rbf->next != NULL && input_orient*rbf->next->invec[2] <
214 <                                                        input_orient*invec[2]) {
214 >                                                input_orient*invec[2]+FTINY) {
215                      for (miga[0] = rbf->ejl; miga[0] != NULL;
216                                          miga[0] = nextedge(rbf,miga[0]))
217                          if (opp_rbf(rbf,miga[0]) == rbf->next) {
218 <                                double  nf = 1. - rbf->invec[2]*rbf->invec[2];
218 >                                double  nf = 1. -
219 >                                        rbf->next->invec[2]*rbf->next->invec[2];
220                                  if (nf > FTINY) {       /* rotate to match */
221                                          nf = sqrt((1.-invec[2]*invec[2])/nf);
222 <                                        invec[0] = nf*rbf->invec[0];
223 <                                        invec[1] = nf*rbf->invec[1];
222 >                                        invec[0] = nf*rbf->next->invec[0];
223 >                                        invec[1] = nf*rbf->next->invec[1];
224                                  }
225 <                                return(0);
225 >                                return(0);      /* rotational symmetry */
226                          }
227                      break;
228                  }
# Line 258 | Line 263 | get_interp(MIGRATION *miga[3], FVECT invec)
263          }
264   }
265  
266 < /* Advect and allocate new RBF along edge */
262 < static RBFNODE *
263 < e_advect_rbf(const MIGRATION *mig, const FVECT invec)
264 < {
265 <        RBFNODE         *rbf;
266 <        int             n, i, j;
267 <        double          t, full_dist;
268 <                                                /* get relative position */
269 <        t = Acos(DOT(invec, mig->rbfv[0]->invec));
270 <        if (t < M_PI/grid_res) {                /* near first DSF */
271 <                n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[0]->nrbf-1);
272 <                rbf = (RBFNODE *)malloc(n);
273 <                if (rbf == NULL)
274 <                        goto memerr;
275 <                memcpy(rbf, mig->rbfv[0], n);   /* just duplicate */
276 <                rbf->next = NULL; rbf->ejl = NULL;
277 <                return(rbf);
278 <        }
279 <        full_dist = acos(DOT(mig->rbfv[0]->invec, mig->rbfv[1]->invec));
280 <        if (t > full_dist-M_PI/grid_res) {      /* near second DSF */
281 <                n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[1]->nrbf-1);
282 <                rbf = (RBFNODE *)malloc(n);
283 <                if (rbf == NULL)
284 <                        goto memerr;
285 <                memcpy(rbf, mig->rbfv[1], n);   /* just duplicate */
286 <                rbf->next = NULL; rbf->ejl = NULL;
287 <                return(rbf);
288 <        }
289 <        t /= full_dist;
290 <        n = 0;                                  /* count migrating particles */
291 <        for (i = 0; i < mtx_nrows(mig); i++)
292 <            for (j = 0; j < mtx_ncols(mig); j++)
293 <                n += (mtx_coef(mig,i,j) > FTINY);
294 < #ifdef DEBUG
295 <        fprintf(stderr, "Input RBFs have %d, %d nodes -> output has %d\n",
296 <                        mig->rbfv[0]->nrbf, mig->rbfv[1]->nrbf, n);
297 < #endif
298 <        rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1));
299 <        if (rbf == NULL)
300 <                goto memerr;
301 <        rbf->next = NULL; rbf->ejl = NULL;
302 <        VCOPY(rbf->invec, invec);
303 <        rbf->nrbf = n;
304 <        rbf->vtotal = 1.-t + t*mig->rbfv[1]->vtotal/mig->rbfv[0]->vtotal;
305 <        n = 0;                                  /* advect RBF lobes */
306 <        for (i = 0; i < mtx_nrows(mig); i++) {
307 <            const RBFVAL        *rbf0i = &mig->rbfv[0]->rbfa[i];
308 <            const float         peak0 = rbf0i->peak;
309 <            const double        rad0 = R2ANG(rbf0i->crad);
310 <            FVECT               v0;
311 <            float               mv;
312 <            ovec_from_pos(v0, rbf0i->gx, rbf0i->gy);
313 <            for (j = 0; j < mtx_ncols(mig); j++)
314 <                if ((mv = mtx_coef(mig,i,j)) > FTINY) {
315 <                        const RBFVAL    *rbf1j = &mig->rbfv[1]->rbfa[j];
316 <                        double          rad1 = R2ANG(rbf1j->crad);
317 <                        FVECT           v;
318 <                        int             pos[2];
319 <                        rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal;
320 <                        rbf->rbfa[n].crad = ANG2R(sqrt(rad0*rad0*(1.-t) +
321 <                                                        rad1*rad1*t));
322 <                        ovec_from_pos(v, rbf1j->gx, rbf1j->gy);
323 <                        geodesic(v, v0, v, t, GEOD_REL);
324 <                        pos_from_vec(pos, v);
325 <                        rbf->rbfa[n].gx = pos[0];
326 <                        rbf->rbfa[n].gy = pos[1];
327 <                        ++n;
328 <                }
329 <        }
330 <        rbf->vtotal *= mig->rbfv[0]->vtotal;    /* turn ratio into actual */
331 <        return(rbf);
332 < memerr:
333 <        fprintf(stderr, "%s: Out of memory in e_advect_rbf()\n", progname);
334 <        exit(1);
335 <        return(NULL);   /* pro forma return */
336 < }
337 <
338 < /* Partially advect between recorded incident angles and allocate new RBF */
266 > /* Advect between recorded incident angles and allocate new RBF */
267   RBFNODE *
268 < advect_rbf(const FVECT invec)
268 > advect_rbf(const FVECT invec, int lobe_lim)
269   {
270 +        double          cthresh = FTINY;
271          FVECT           sivec;
272          MIGRATION       *miga[3];
273          RBFNODE         *rbf;
# Line 353 | Line 282 | advect_rbf(const FVECT invec)
282          if (sym < 0)                            /* can't interpolate? */
283                  return(NULL);
284          if (miga[1] == NULL) {                  /* advect along edge? */
285 <                rbf = e_advect_rbf(miga[0], sivec);
285 >                rbf = e_advect_rbf(miga[0], sivec, lobe_lim);
286                  if (single_plane_incident)
287                          rotate_rbf(rbf, invec);
288                  else
# Line 361 | Line 290 | advect_rbf(const FVECT invec)
290                  return(rbf);
291          }
292   #ifdef DEBUG
293 <        if (miga[0]->rbfv[0] != miga[2]->rbfv[0] |
294 <                        miga[0]->rbfv[1] != miga[1]->rbfv[0] |
295 <                        miga[1]->rbfv[1] != miga[2]->rbfv[1]) {
293 >        if ((miga[0]->rbfv[0] != miga[2]->rbfv[0]) |
294 >                        (miga[0]->rbfv[1] != miga[1]->rbfv[0]) |
295 >                        (miga[1]->rbfv[1] != miga[2]->rbfv[1])) {
296                  fprintf(stderr, "%s: Triangle vertex screw-up!\n", progname);
297                  exit(1);
298          }
# Line 379 | Line 308 | advect_rbf(const FVECT invec)
308          geodesic(v1, miga[0]->rbfv[0]->invec, miga[0]->rbfv[1]->invec,
309                          s, GEOD_REL);
310          t = acos(DOT(v1,sivec)) / acos(DOT(v1,miga[1]->rbfv[1]->invec));
311 + tryagain:
312          n = 0;                                  /* count migrating particles */
313          for (i = 0; i < mtx_nrows(miga[0]); i++)
314              for (j = 0; j < mtx_ncols(miga[0]); j++)
315 <                for (k = (mtx_coef(miga[0],i,j) > FTINY) *
315 >                for (k = (mtx_coef(miga[0],i,j) > cthresh) *
316                                          mtx_ncols(miga[2]); k--; )
317 <                        n += (mtx_coef(miga[2],i,k) > FTINY ||
318 <                                mtx_coef(miga[1],j,k) > FTINY);
317 >                        n += (mtx_coef(miga[2],i,k) > cthresh ||
318 >                                mtx_coef(miga[1],j,k) > cthresh);
319 >                                                /* are we over our limit? */
320 >        if ((lobe_lim > 0) & (n > lobe_lim)) {
321 >                cthresh = cthresh*2. + 10.*FTINY;
322 >                goto tryagain;
323 >        }
324   #ifdef DEBUG
325          fprintf(stderr, "Input RBFs have %d, %d, %d nodes -> output has %d\n",
326                          miga[0]->rbfv[0]->nrbf, miga[0]->rbfv[1]->nrbf,
# Line 412 | Line 347 | advect_rbf(const FVECT invec)
347              for (j = 0; j < mtx_ncols(miga[0]); j++) {
348                  const float     ma = mtx_coef(miga[0],i,j);
349                  const RBFVAL    *rbf1j;
350 <                double          rad1j, srad2;
351 <                if (ma <= FTINY)
350 >                double          srad2;
351 >                if (ma <= cthresh)
352                          continue;
353                  rbf1j = &miga[0]->rbfv[1]->rbfa[j];
354 <                rad1j = R2ANG(rbf1j->crad);
355 <                srad2 = (1.-s)*(1.-t)*rad0i*rad0i + s*(1.-t)*rad1j*rad1j;
354 >                srad2 = R2ANG(rbf1j->crad);
355 >                srad2 = (1.-s)*(1.-t)*rad0i*rad0i + s*(1.-t)*srad2*srad2;
356                  ovec_from_pos(v1, rbf1j->gx, rbf1j->gy);
357                  geodesic(v1, v0, v1, s, GEOD_REL);
358                  for (k = 0; k < mtx_ncols(miga[2]); k++) {
359                      float               mb = mtx_coef(miga[1],j,k);
360                      float               mc = mtx_coef(miga[2],i,k);
361                      const RBFVAL        *rbf2k;
362 <                    double              rad2k;
362 >                    double              rad2;
363                      int                 pos[2];
364 <                    if ((mb <= FTINY) & (mc <= FTINY))
364 >                    if ((mb <= cthresh) & (mc <= cthresh))
365                          continue;
366                      rbf2k = &miga[2]->rbfv[1]->rbfa[k];
367 <                    rbf->rbfa[n].peak = w0i * ma * (mb*mbfact + mc*mcfact);
368 <                    rad2k = R2ANG(rbf2k->crad);
369 <                    rbf->rbfa[n].crad = ANG2R(sqrt(srad2 + t*rad2k*rad2k));
367 >                    rad2 = R2ANG(rbf2k->crad);
368 >                    rad2 = srad2 + t*rad2*rad2;
369 >                    rbf->rbfa[n].peak = w0i * ma * (mb*mbfact + mc*mcfact) *
370 >                                        rad0i*rad0i/rad2;
371 >                    rbf->rbfa[n].crad = ANG2R(sqrt(rad2));
372                      ovec_from_pos(v2, rbf2k->gx, rbf2k->gy);
373                      geodesic(v2, v1, v2, t, GEOD_REL);
374                      pos_from_vec(pos, v2);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines