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.8 by greg, Wed Dec 12 04:49:59 2012 UTC vs.
Revision 2.14 by greg, Tue Oct 22 04:29:27 2013 UTC

# Line 151 | Line 151 | static int
151   in_mesh(MIGRATION *miga[3], unsigned char *emap, int nedges,
152                          const FVECT ivec, MIGRATION *mig)
153   {
154 <        MIGRATION       *ej1, *ej2;
155 <        RBFNODE         *tv;
154 >        RBFNODE         *tv[2];
155 >        MIGRATION       *sej[2], *dej[2];
156 >        int             i;
157                                                  /* check visitation record */
158          if (!check_edge(emap, nedges, mig, 1))
159                  return(0);
# Line 160 | Line 161 | in_mesh(MIGRATION *miga[3], unsigned char *emap, int n
161                  miga[0] = mig;                  /* close enough to edge */
162                  return(1);
163          }
164 <                                                /* do triangles either side */
165 <        for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL;
166 <                                ej1 = nextedge(mig->rbfv[0],ej1)) {
167 <            if (ej1 == mig)
168 <                continue;
169 <            tv = opp_rbf(mig->rbfv[0],ej1);
170 <            for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2))
171 <                if (opp_rbf(tv,ej2) == mig->rbfv[1]) {
172 <                        int     do_ej1 = check_edge(emap, nedges, ej1, 0);
173 <                        int     do_ej2 = check_edge(emap, nedges, ej2, 0);
174 <                        if (do_ej1 && in_mesh(miga, emap, nedges, ivec, ej1))
175 <                                return(1);
176 <                        if (do_ej2 && in_mesh(miga, emap, nedges, ivec, ej2))
177 <                                return(1);
178 <                                                /* check just once */
178 <                        if (do_ej1 & do_ej2 && in_tri(mig->rbfv[0],
179 <                                                mig->rbfv[1], tv, ivec)) {
180 <                                miga[0] = mig;
181 <                                miga[1] = ej1;
182 <                                miga[2] = ej2;
183 <                                return(1);
164 >        if (!get_triangles(tv, mig))            /* do triangles either side? */
165 >                return(0);
166 >        for (i = 2; i--; ) {                    /* identify edges to check */
167 >                MIGRATION       *ej;
168 >                sej[i] = dej[i] = NULL;
169 >                if (tv[i] == NULL)
170 >                        continue;
171 >                for (ej = tv[i]->ejl; ej != NULL; ej = nextedge(tv[i],ej)) {
172 >                        RBFNODE *rbfop = opp_rbf(tv[i],ej);
173 >                        if (rbfop == mig->rbfv[0]) {
174 >                                if (check_edge(emap, nedges, ej, 0))
175 >                                        sej[i] = ej;
176 >                        } else if (rbfop == mig->rbfv[1]) {
177 >                                if (check_edge(emap, nedges, ej, 0))
178 >                                        dej[i] = ej;
179                          }
180                  }
181          }
182 +        for (i = 2; i--; ) {                    /* check triangles just once */
183 +                if (sej[i] != NULL && in_mesh(miga, emap, nedges, ivec, sej[i]))
184 +                        return(1);
185 +                if (dej[i] != NULL && in_mesh(miga, emap, nedges, ivec, dej[i]))
186 +                        return(1);
187 +                if ((sej[i] == NULL) | (dej[i] == NULL))
188 +                        continue;
189 +                if (in_tri(mig->rbfv[0], mig->rbfv[1], tv[i], ivec)) {
190 +                        miga[0] = mig;
191 +                        miga[1] = sej[i];
192 +                        miga[2] = dej[i];
193 +                        return(1);
194 +                }
195 +        }
196          return(0);                              /* not near this edge */
197   }
198  
# Line 193 | Line 202 | get_interp(MIGRATION *miga[3], FVECT invec)
202   {
203          miga[0] = miga[1] = miga[2] = NULL;
204          if (single_plane_incident) {            /* isotropic BSDF? */
205 <                RBFNODE *rbf;                   /* find edge we're on */
206 <                for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) {
207 <                        if (input_orient*rbf->invec[2] < input_orient*invec[2])
208 <                                break;
209 <                        if (rbf->next != NULL &&
201 <                                        input_orient*rbf->next->invec[2] <
205 >            RBFNODE     *rbf;                   /* find edge we're on */
206 >            for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) {
207 >                if (input_orient*rbf->invec[2] < input_orient*invec[2])
208 >                        break;
209 >                if (rbf->next != NULL && input_orient*rbf->next->invec[2] <
210                                                          input_orient*invec[2]) {
211 <                                for (miga[0] = rbf->ejl; miga[0] != NULL;
212 <                                                miga[0] = nextedge(rbf,miga[0]))
213 <                                        if (opp_rbf(rbf,miga[0]) == rbf->next) {
214 <                                                double  nf = 1.-rbf->invec[2]*rbf->invec[2];
215 <                                                if (nf > FTINY) {
216 <                                                        nf = sqrt((1.-invec[2]*invec[2])/nf);
217 <                                                        invec[0] = nf*rbf->invec[0];
218 <                                                        invec[1] = nf*rbf->invec[1];
219 <                                                }
220 <                                                return(0);
213 <                                        }
214 <                                break;
211 >                    for (miga[0] = rbf->ejl; miga[0] != NULL;
212 >                                        miga[0] = nextedge(rbf,miga[0]))
213 >                        if (opp_rbf(rbf,miga[0]) == rbf->next) {
214 >                                double  nf = 1. - rbf->invec[2]*rbf->invec[2];
215 >                                if (nf > FTINY) {       /* rotate to match */
216 >                                        nf = sqrt((1.-invec[2]*invec[2])/nf);
217 >                                        invec[0] = nf*rbf->invec[0];
218 >                                        invec[1] = nf*rbf->invec[1];
219 >                                }
220 >                                return(0);
221                          }
222 +                    break;
223                  }
224 <                return(-1);                     /* outside range! */
224 >            }
225 >            return(-1);                         /* outside range! */
226          }
227          {                                       /* else use triangle mesh */
228                  int             sym = use_symmetry(invec);
# Line 231 | Line 239 | get_interp(MIGRATION *miga[3], FVECT invec)
239                          exit(1);
240                  }
241                                                  /* identify intersection  */
242 <                if (!in_mesh(miga, emap, nedges, invec, mig_list))
242 >                if (!in_mesh(miga, emap, nedges, invec, mig_list)) {
243 > #ifdef DEBUG
244 >                        fprintf(stderr,
245 >                        "Incident angle (%.1f,%.1f) deg. outside mesh\n",
246 >                                        get_theta180(invec), get_phi360(invec));
247 > #endif
248                          sym = -1;               /* outside mesh */
249 <                else if (miga[1] != NULL &&
249 >                } else if (miga[1] != NULL &&
250                                  (miga[2] == NULL || !order_triangle(miga))) {
251   #ifdef DEBUG
252                          fputs("Munged triangle in get_interp()\n", stderr);
# Line 247 | Line 260 | get_interp(MIGRATION *miga[3], FVECT invec)
260  
261   /* Advect and allocate new RBF along edge */
262   static RBFNODE *
263 < e_advect_rbf(const MIGRATION *mig, const FVECT invec)
263 > e_advect_rbf(const MIGRATION *mig, const FVECT invec, int lobe_lim)
264   {
265 +        double          cthresh = FTINY;
266          RBFNODE         *rbf;
267          int             n, i, j;
268          double          t, full_dist;
269                                                  /* get relative position */
270 <        t = acos(DOT(invec, mig->rbfv[0]->invec));
270 >        t = Acos(DOT(invec, mig->rbfv[0]->invec));
271          if (t < M_PI/grid_res) {                /* near first DSF */
272                  n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[0]->nrbf-1);
273                  rbf = (RBFNODE *)malloc(n);
# Line 273 | Line 287 | e_advect_rbf(const MIGRATION *mig, const FVECT invec)
287                  rbf->next = NULL; rbf->ejl = NULL;
288                  return(rbf);
289          }
290 <        t /= full_dist;
290 >        t /= full_dist;
291 > tryagain:
292          n = 0;                                  /* count migrating particles */
293          for (i = 0; i < mtx_nrows(mig); i++)
294              for (j = 0; j < mtx_ncols(mig); j++)
295 <                n += (mtx_coef(mig,i,j) > FTINY);
295 >                n += (mtx_coef(mig,i,j) > cthresh);
296 >                                                /* are we over our limit? */
297 >        if ((lobe_lim > 0) & (n > lobe_lim)) {
298 >                cthresh = cthresh*2. + 10.*FTINY;
299 >                goto tryagain;
300 >        }
301   #ifdef DEBUG
302          fprintf(stderr, "Input RBFs have %d, %d nodes -> output has %d\n",
303                          mig->rbfv[0]->nrbf, mig->rbfv[1]->nrbf, n);
# Line 298 | Line 318 | e_advect_rbf(const MIGRATION *mig, const FVECT invec)
318              float               mv;
319              ovec_from_pos(v0, rbf0i->gx, rbf0i->gy);
320              for (j = 0; j < mtx_ncols(mig); j++)
321 <                if ((mv = mtx_coef(mig,i,j)) > FTINY) {
321 >                if ((mv = mtx_coef(mig,i,j)) > cthresh) {
322                          const RBFVAL    *rbf1j = &mig->rbfv[1]->rbfa[j];
323 <                        double          rad1 = R2ANG(rbf1j->crad);
323 >                        double          rad2;
324                          FVECT           v;
325                          int             pos[2];
326 <                        rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal;
327 <                        rbf->rbfa[n].crad = ANG2R(sqrt(rad0*rad0*(1.-t) +
328 <                                                        rad1*rad1*t));
326 >                        rad2 = R2ANG(rbf1j->crad);
327 >                        rad2 = rad0*rad0*(1.-t) + rad2*rad2*t;
328 >                        rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal *
329 >                                                rad0*rad0/rad2;
330 >                        rbf->rbfa[n].crad = ANG2R(sqrt(rad2));
331                          ovec_from_pos(v, rbf1j->gx, rbf1j->gy);
332                          geodesic(v, v0, v, t, GEOD_REL);
333                          pos_from_vec(pos, v);
# Line 324 | Line 346 | memerr:
346  
347   /* Partially advect between recorded incident angles and allocate new RBF */
348   RBFNODE *
349 < advect_rbf(const FVECT invec)
349 > advect_rbf(const FVECT invec, int lobe_lim)
350   {
351 +        double          cthresh = FTINY;
352          FVECT           sivec;
353          MIGRATION       *miga[3];
354          RBFNODE         *rbf;
# Line 340 | Line 363 | advect_rbf(const FVECT invec)
363          if (sym < 0)                            /* can't interpolate? */
364                  return(NULL);
365          if (miga[1] == NULL) {                  /* advect along edge? */
366 <                rbf = e_advect_rbf(miga[0], sivec);
366 >                rbf = e_advect_rbf(miga[0], sivec, lobe_lim);
367                  if (single_plane_incident)
368                          rotate_rbf(rbf, invec);
369                  else
# Line 366 | Line 389 | advect_rbf(const FVECT invec)
389          geodesic(v1, miga[0]->rbfv[0]->invec, miga[0]->rbfv[1]->invec,
390                          s, GEOD_REL);
391          t = acos(DOT(v1,sivec)) / acos(DOT(v1,miga[1]->rbfv[1]->invec));
392 + tryagain:
393          n = 0;                                  /* count migrating particles */
394          for (i = 0; i < mtx_nrows(miga[0]); i++)
395              for (j = 0; j < mtx_ncols(miga[0]); j++)
396 <                for (k = (mtx_coef(miga[0],i,j) > FTINY) *
396 >                for (k = (mtx_coef(miga[0],i,j) > cthresh) *
397                                          mtx_ncols(miga[2]); k--; )
398 <                        n += (mtx_coef(miga[2],i,k) > FTINY ||
399 <                                mtx_coef(miga[1],j,k) > FTINY);
398 >                        n += (mtx_coef(miga[2],i,k) > cthresh ||
399 >                                mtx_coef(miga[1],j,k) > cthresh);
400 >                                                /* are we over our limit? */
401 >        if ((lobe_lim > 0) & (n > lobe_lim)) {
402 >                cthresh = cthresh*2. + 10.*FTINY;
403 >                goto tryagain;
404 >        }
405   #ifdef DEBUG
406          fprintf(stderr, "Input RBFs have %d, %d, %d nodes -> output has %d\n",
407                          miga[0]->rbfv[0]->nrbf, miga[0]->rbfv[1]->nrbf,
# Line 399 | Line 428 | advect_rbf(const FVECT invec)
428              for (j = 0; j < mtx_ncols(miga[0]); j++) {
429                  const float     ma = mtx_coef(miga[0],i,j);
430                  const RBFVAL    *rbf1j;
431 <                double          rad1j, srad2;
432 <                if (ma <= FTINY)
431 >                double          srad2;
432 >                if (ma <= cthresh)
433                          continue;
434                  rbf1j = &miga[0]->rbfv[1]->rbfa[j];
435 <                rad1j = R2ANG(rbf1j->crad);
436 <                srad2 = (1.-s)*(1.-t)*rad0i*rad0i + s*(1.-t)*rad1j*rad1j;
435 >                srad2 = R2ANG(rbf1j->crad);
436 >                srad2 = (1.-s)*(1.-t)*rad0i*rad0i + s*(1.-t)*srad2*srad2;
437                  ovec_from_pos(v1, rbf1j->gx, rbf1j->gy);
438                  geodesic(v1, v0, v1, s, GEOD_REL);
439                  for (k = 0; k < mtx_ncols(miga[2]); k++) {
440                      float               mb = mtx_coef(miga[1],j,k);
441                      float               mc = mtx_coef(miga[2],i,k);
442                      const RBFVAL        *rbf2k;
443 <                    double              rad2k;
443 >                    double              rad2;
444                      int                 pos[2];
445 <                    if ((mb <= FTINY) & (mc <= FTINY))
445 >                    if ((mb <= cthresh) & (mc <= cthresh))
446                          continue;
447                      rbf2k = &miga[2]->rbfv[1]->rbfa[k];
448 <                    rbf->rbfa[n].peak = w0i * ma * (mb*mbfact + mc*mcfact);
449 <                    rad2k = R2ANG(rbf2k->crad);
450 <                    rbf->rbfa[n].crad = ANG2R(sqrt(srad2 + t*rad2k*rad2k));
448 >                    rad2 = R2ANG(rbf2k->crad);
449 >                    rad2 = srad2 + t*rad2*rad2;
450 >                    rbf->rbfa[n].peak = w0i * ma * (mb*mbfact + mc*mcfact) *
451 >                                        rad0i*rad0i/rad2;
452 >                    rbf->rbfa[n].crad = ANG2R(sqrt(rad2));
453                      ovec_from_pos(v2, rbf2k->gx, rbf2k->gy);
454                      geodesic(v2, v1, v2, t, GEOD_REL);
455                      pos_from_vec(pos, v2);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines