ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfinterp.c
Revision: 2.12
Committed: Thu Sep 26 17:05:00 2013 UTC (10 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.11: +13 -7 lines
Log Message:
Added -l option to limit maximum number of RBF lobes for interpolation

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.12 static const char RCSid[] = "$Id: bsdfinterp.c,v 2.11 2013/06/29 21:03:25 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * Interpolate BSDF data from radial basis functions in advection mesh.
6     *
7     * G. Ward
8     */
9    
10     #define _USE_MATH_DEFINES
11     #include <stdio.h>
12     #include <stdlib.h>
13     #include <string.h>
14     #include <math.h>
15     #include "bsdfrep.h"
16    
17     /* Insert vertex in ordered list */
18     static void
19     insert_vert(RBFNODE **vlist, RBFNODE *v)
20     {
21     int i, j;
22    
23     for (i = 0; vlist[i] != NULL; i++) {
24     if (v == vlist[i])
25     return;
26     if (v->ord < vlist[i]->ord)
27     break;
28     }
29     for (j = i; vlist[j] != NULL; j++)
30     ;
31     while (j > i) {
32     vlist[j] = vlist[j-1];
33     --j;
34     }
35     vlist[i] = v;
36     }
37    
38     /* Sort triangle edges in standard order */
39     static int
40     order_triangle(MIGRATION *miga[3])
41     {
42     RBFNODE *vert[7];
43     MIGRATION *ord[3];
44     int i;
45     /* order vertices, first */
46     memset(vert, 0, sizeof(vert));
47     for (i = 3; i--; ) {
48     if (miga[i] == NULL)
49     return(0);
50     insert_vert(vert, miga[i]->rbfv[0]);
51     insert_vert(vert, miga[i]->rbfv[1]);
52     }
53     /* should be just 3 vertices */
54 greg 2.4 if ((vert[2] == NULL) | (vert[3] != NULL))
55 greg 2.1 return(0);
56     /* identify edge 0 */
57     for (i = 3; i--; )
58     if (miga[i]->rbfv[0] == vert[0] &&
59     miga[i]->rbfv[1] == vert[1]) {
60     ord[0] = miga[i];
61     break;
62     }
63     if (i < 0)
64     return(0);
65     /* identify edge 1 */
66     for (i = 3; i--; )
67     if (miga[i]->rbfv[0] == vert[1] &&
68     miga[i]->rbfv[1] == vert[2]) {
69     ord[1] = miga[i];
70     break;
71     }
72     if (i < 0)
73     return(0);
74     /* identify edge 2 */
75     for (i = 3; i--; )
76     if (miga[i]->rbfv[0] == vert[0] &&
77     miga[i]->rbfv[1] == vert[2]) {
78     ord[2] = miga[i];
79     break;
80     }
81     if (i < 0)
82     return(0);
83     /* reassign order */
84     miga[0] = ord[0]; miga[1] = ord[1]; miga[2] = ord[2];
85     return(1);
86     }
87    
88 greg 2.4 /* Determine if we are close enough to an edge */
89 greg 2.3 static int
90     on_edge(const MIGRATION *ej, const FVECT ivec)
91     {
92 greg 2.4 double cos_a, cos_b, cos_c, cos_aplusb;
93     /* use triangle inequality */
94     cos_a = DOT(ej->rbfv[0]->invec, ivec);
95     if (cos_a <= 0)
96     return(0);
97    
98     cos_b = DOT(ej->rbfv[1]->invec, ivec);
99     if (cos_b <= 0)
100     return(0);
101 greg 2.3
102 greg 2.4 cos_aplusb = cos_a*cos_b - sqrt((1.-cos_a*cos_a)*(1.-cos_b*cos_b));
103     if (cos_aplusb <= 0)
104     return(0);
105    
106     cos_c = DOT(ej->rbfv[0]->invec, ej->rbfv[1]->invec);
107    
108     return(cos_c - cos_aplusb < .001);
109 greg 2.3 }
110    
111     /* Determine if we are inside the given triangle */
112     static int
113     in_tri(const RBFNODE *v1, const RBFNODE *v2, const RBFNODE *v3, const FVECT p)
114     {
115     FVECT vc;
116     int sgn1, sgn2, sgn3;
117     /* signed volume test */
118     VCROSS(vc, v1->invec, v2->invec);
119     sgn1 = (DOT(p, vc) > 0);
120     VCROSS(vc, v2->invec, v3->invec);
121     sgn2 = (DOT(p, vc) > 0);
122     if (sgn1 != sgn2)
123     return(0);
124     VCROSS(vc, v3->invec, v1->invec);
125     sgn3 = (DOT(p, vc) > 0);
126     return(sgn2 == sgn3);
127     }
128    
129 greg 2.6 /* Test (and set) bitmap for edge */
130 greg 2.4 static int
131     check_edge(unsigned char *emap, int nedges, const MIGRATION *mig, int mark)
132     {
133     int ejndx, bit2check;
134    
135     if (mig->rbfv[0]->ord > mig->rbfv[1]->ord)
136     ejndx = mig->rbfv[1]->ord + (nedges-1)*mig->rbfv[0]->ord;
137     else
138     ejndx = mig->rbfv[0]->ord + (nedges-1)*mig->rbfv[1]->ord;
139    
140     bit2check = 1<<(ejndx&07);
141    
142     if (emap[ejndx>>3] & bit2check)
143     return(0);
144     if (mark)
145     emap[ejndx>>3] |= bit2check;
146     return(1);
147     }
148    
149 greg 2.3 /* Compute intersection with the given position over remaining mesh */
150     static int
151     in_mesh(MIGRATION *miga[3], unsigned char *emap, int nedges,
152     const FVECT ivec, MIGRATION *mig)
153     {
154 greg 2.9 RBFNODE *tv[2];
155     MIGRATION *sej[2], *dej[2];
156     int i;
157 greg 2.3 /* check visitation record */
158 greg 2.4 if (!check_edge(emap, nedges, mig, 1))
159 greg 2.3 return(0);
160     if (on_edge(mig, ivec)) {
161     miga[0] = mig; /* close enough to edge */
162     return(1);
163     }
164 greg 2.9 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 greg 2.3 }
180 greg 2.4 }
181 greg 2.3 }
182 greg 2.9 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 greg 2.4 return(0); /* not near this edge */
197 greg 2.3 }
198    
199 greg 2.1 /* Find edge(s) for interpolating the given vector, applying symmetry */
200     int
201     get_interp(MIGRATION *miga[3], FVECT invec)
202     {
203     miga[0] = miga[1] = miga[2] = NULL;
204     if (single_plane_incident) { /* isotropic BSDF? */
205 greg 2.10 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 greg 2.1 input_orient*invec[2]) {
211 greg 2.10 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 greg 2.1 }
222 greg 2.10 break;
223 greg 2.1 }
224 greg 2.10 }
225     return(-1); /* outside range! */
226 greg 2.1 }
227     { /* else use triangle mesh */
228 greg 2.3 int sym = use_symmetry(invec);
229     int nedges = 0;
230     MIGRATION *mep;
231     unsigned char *emap;
232     /* clear visitation map */
233     for (mep = mig_list; mep != NULL; mep = mep->next)
234     ++nedges;
235     emap = (unsigned char *)calloc((nedges*(nedges-1) + 7)>>3, 1);
236     if (emap == NULL) {
237     fprintf(stderr, "%s: Out of memory in get_interp()\n",
238     progname);
239     exit(1);
240     }
241     /* identify intersection */
242 greg 2.9 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 greg 2.3 sym = -1; /* outside mesh */
249 greg 2.9 } else if (miga[1] != NULL &&
250 greg 2.3 (miga[2] == NULL || !order_triangle(miga))) {
251 greg 2.1 #ifdef DEBUG
252     fputs("Munged triangle in get_interp()\n", stderr);
253     #endif
254 greg 2.3 sym = -1;
255 greg 2.1 }
256 greg 2.3 free(emap);
257 greg 2.1 return(sym); /* return in standard order */
258     }
259     }
260    
261     /* 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 greg 2.11 t = Acos(DOT(invec, mig->rbfv[0]->invec));
270 greg 2.5 if (t < M_PI/grid_res) { /* near first DSF */
271 greg 2.1 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 greg 2.8 rbf->next = NULL; rbf->ejl = NULL;
277 greg 2.1 return(rbf);
278     }
279     full_dist = acos(DOT(mig->rbfv[0]->invec, mig->rbfv[1]->invec));
280 greg 2.5 if (t > full_dist-M_PI/grid_res) { /* near second DSF */
281 greg 2.1 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 greg 2.8 rbf->next = NULL; rbf->ejl = NULL;
287 greg 2.1 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 greg 2.2 n += (mtx_coef(mig,i,j) > FTINY);
294 greg 2.1 #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 greg 2.2 if ((mv = mtx_coef(mig,i,j)) > FTINY) {
315 greg 2.1 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 greg 2.8 geodesic(v, v0, v, t, GEOD_REL);
324 greg 2.1 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 */
339     RBFNODE *
340 greg 2.12 advect_rbf(const FVECT invec, int lobe_lim)
341 greg 2.1 {
342 greg 2.12 double cthresh = FTINY;
343 greg 2.1 FVECT sivec;
344     MIGRATION *miga[3];
345     RBFNODE *rbf;
346     int sym;
347     float mbfact, mcfact;
348     int n, i, j, k;
349     FVECT v0, v1, v2;
350 greg 2.8 double s, t;
351 greg 2.1
352     VCOPY(sivec, invec); /* find triangle/edge */
353     sym = get_interp(miga, sivec);
354     if (sym < 0) /* can't interpolate? */
355     return(NULL);
356     if (miga[1] == NULL) { /* advect along edge? */
357     rbf = e_advect_rbf(miga[0], sivec);
358 greg 2.5 if (single_plane_incident)
359     rotate_rbf(rbf, invec);
360     else
361     rev_rbf_symmetry(rbf, sym);
362 greg 2.1 return(rbf);
363     }
364     #ifdef DEBUG
365     if (miga[0]->rbfv[0] != miga[2]->rbfv[0] |
366     miga[0]->rbfv[1] != miga[1]->rbfv[0] |
367     miga[1]->rbfv[1] != miga[2]->rbfv[1]) {
368     fprintf(stderr, "%s: Triangle vertex screw-up!\n", progname);
369     exit(1);
370     }
371     #endif
372     /* figure out position */
373     fcross(v0, miga[2]->rbfv[0]->invec, miga[2]->rbfv[1]->invec);
374     normalize(v0);
375     fcross(v2, miga[1]->rbfv[0]->invec, miga[1]->rbfv[1]->invec);
376     normalize(v2);
377     fcross(v1, sivec, miga[1]->rbfv[1]->invec);
378     normalize(v1);
379 greg 2.7 s = acos(DOT(v0,v1)) / acos(DOT(v0,v2));
380 greg 2.1 geodesic(v1, miga[0]->rbfv[0]->invec, miga[0]->rbfv[1]->invec,
381 greg 2.7 s, GEOD_REL);
382 greg 2.8 t = acos(DOT(v1,sivec)) / acos(DOT(v1,miga[1]->rbfv[1]->invec));
383 greg 2.12 tryagain:
384 greg 2.1 n = 0; /* count migrating particles */
385     for (i = 0; i < mtx_nrows(miga[0]); i++)
386     for (j = 0; j < mtx_ncols(miga[0]); j++)
387 greg 2.12 for (k = (mtx_coef(miga[0],i,j) > cthresh) *
388 greg 2.1 mtx_ncols(miga[2]); k--; )
389 greg 2.12 n += (mtx_coef(miga[2],i,k) > cthresh ||
390     mtx_coef(miga[1],j,k) > cthresh);
391     if ((lobe_lim > 0) & (n > lobe_lim)) {
392     cthresh = cthresh*2. + 10.*FTINY;
393     goto tryagain;
394     }
395 greg 2.1 #ifdef DEBUG
396     fprintf(stderr, "Input RBFs have %d, %d, %d nodes -> output has %d\n",
397     miga[0]->rbfv[0]->nrbf, miga[0]->rbfv[1]->nrbf,
398     miga[2]->rbfv[1]->nrbf, n);
399     #endif
400     rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1));
401     if (rbf == NULL) {
402     fprintf(stderr, "%s: Out of memory in advect_rbf()\n", progname);
403     exit(1);
404     }
405     rbf->next = NULL; rbf->ejl = NULL;
406     VCOPY(rbf->invec, sivec);
407     rbf->nrbf = n;
408     n = 0; /* compute RBF lobes */
409     mbfact = s * miga[0]->rbfv[1]->vtotal/miga[0]->rbfv[0]->vtotal *
410     (1.-t + t*miga[1]->rbfv[1]->vtotal/miga[1]->rbfv[0]->vtotal);
411     mcfact = (1.-s) *
412     (1.-t + t*miga[2]->rbfv[1]->vtotal/miga[2]->rbfv[0]->vtotal);
413     for (i = 0; i < mtx_nrows(miga[0]); i++) {
414     const RBFVAL *rbf0i = &miga[0]->rbfv[0]->rbfa[i];
415     const float w0i = rbf0i->peak;
416     const double rad0i = R2ANG(rbf0i->crad);
417     ovec_from_pos(v0, rbf0i->gx, rbf0i->gy);
418     for (j = 0; j < mtx_ncols(miga[0]); j++) {
419 greg 2.2 const float ma = mtx_coef(miga[0],i,j);
420 greg 2.1 const RBFVAL *rbf1j;
421     double rad1j, srad2;
422 greg 2.12 if (ma <= cthresh)
423 greg 2.1 continue;
424     rbf1j = &miga[0]->rbfv[1]->rbfa[j];
425     rad1j = R2ANG(rbf1j->crad);
426     srad2 = (1.-s)*(1.-t)*rad0i*rad0i + s*(1.-t)*rad1j*rad1j;
427     ovec_from_pos(v1, rbf1j->gx, rbf1j->gy);
428 greg 2.7 geodesic(v1, v0, v1, s, GEOD_REL);
429 greg 2.1 for (k = 0; k < mtx_ncols(miga[2]); k++) {
430 greg 2.2 float mb = mtx_coef(miga[1],j,k);
431     float mc = mtx_coef(miga[2],i,k);
432 greg 2.1 const RBFVAL *rbf2k;
433     double rad2k;
434     int pos[2];
435 greg 2.12 if ((mb <= cthresh) & (mc <= cthresh))
436 greg 2.1 continue;
437     rbf2k = &miga[2]->rbfv[1]->rbfa[k];
438     rbf->rbfa[n].peak = w0i * ma * (mb*mbfact + mc*mcfact);
439     rad2k = R2ANG(rbf2k->crad);
440     rbf->rbfa[n].crad = ANG2R(sqrt(srad2 + t*rad2k*rad2k));
441     ovec_from_pos(v2, rbf2k->gx, rbf2k->gy);
442 greg 2.8 geodesic(v2, v1, v2, t, GEOD_REL);
443     pos_from_vec(pos, v2);
444 greg 2.1 rbf->rbfa[n].gx = pos[0];
445     rbf->rbfa[n].gy = pos[1];
446     ++n;
447     }
448     }
449     }
450     rbf->vtotal = miga[0]->rbfv[0]->vtotal * (mbfact + mcfact);
451     rev_rbf_symmetry(rbf, sym);
452     return(rbf);
453     }