ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfinterp.c
Revision: 2.3
Committed: Tue Oct 23 05:10:42 2012 UTC (11 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +96 -184 lines
Log Message:
Hopeful bug fix in BSDF interpolation code

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdfinterp.c,v 2.2 2012/10/20 07:02:00 greg Exp $";
3 #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 if ((vert[3] == NULL) | (vert[4] != NULL))
55 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 /* Determine if we are close enough to the given edge */
89 static int
90 on_edge(const MIGRATION *ej, const FVECT ivec)
91 {
92 double cos_a = DOT(ej->rbfv[0]->invec, ivec);
93 double cos_b = DOT(ej->rbfv[1]->invec, ivec);
94 double cos_c = DOT(ej->rbfv[0]->invec, ej->rbfv[1]->invec);
95 double cos_aplusb = cos_a*cos_b -
96 sqrt((1.-cos_a*cos_a)*(1.-cos_b*cos_b));
97
98 return(cos_aplusb - cos_c < .01);
99 }
100
101 /* Determine if we are inside the given triangle */
102 static int
103 in_tri(const RBFNODE *v1, const RBFNODE *v2, const RBFNODE *v3, const FVECT p)
104 {
105 FVECT vc;
106 int sgn1, sgn2, sgn3;
107 /* signed volume test */
108 VCROSS(vc, v1->invec, v2->invec);
109 sgn1 = (DOT(p, vc) > 0);
110 VCROSS(vc, v2->invec, v3->invec);
111 sgn2 = (DOT(p, vc) > 0);
112 if (sgn1 != sgn2)
113 return(0);
114 VCROSS(vc, v3->invec, v1->invec);
115 sgn3 = (DOT(p, vc) > 0);
116 return(sgn2 == sgn3);
117 }
118
119 /* Compute intersection with the given position over remaining mesh */
120 static int
121 in_mesh(MIGRATION *miga[3], unsigned char *emap, int nedges,
122 const FVECT ivec, MIGRATION *mig)
123 {
124 MIGRATION *ej1, *ej2;
125 RBFNODE *tv;
126 int ejndx;
127 /* check visitation record */
128 if (mig->rbfv[0]->ord > mig->rbfv[1]->ord)
129 ejndx = mig->rbfv[1]->ord + (nedges-1)*mig->rbfv[0]->ord;
130 else
131 ejndx = mig->rbfv[0]->ord + (nedges-1)*mig->rbfv[1]->ord;
132 if (emap[ejndx>>3] & 1<<(ejndx&07)) /* tested already? */
133 return(0);
134 emap[ejndx>>3] |= 1<<(ejndx&07); /* else mark & test it */
135 if (on_edge(mig, ivec)) {
136 miga[0] = mig; /* close enough to edge */
137 return(1);
138 }
139 /* do triangles either side */
140 for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL;
141 ej1 = nextedge(mig->rbfv[0],ej1)) {
142 if (ej1 == mig)
143 continue;
144 tv = opp_rbf(mig->rbfv[0],ej1);
145 for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2))
146 if (opp_rbf(tv,ej2) == mig->rbfv[1]) {
147 if (in_mesh(miga, emap, nedges, ivec, ej1))
148 return(1);
149 if (in_mesh(miga, emap, nedges, ivec, ej2))
150 return(1);
151 if (in_tri(mig->rbfv[0], mig->rbfv[1],
152 tv, ivec)) {
153 miga[0] = mig;
154 miga[1] = ej1;
155 miga[2] = ej2;
156 return(1);
157 }
158 }
159 }
160 return(0);
161 }
162
163 /* Find edge(s) for interpolating the given vector, applying symmetry */
164 int
165 get_interp(MIGRATION *miga[3], FVECT invec)
166 {
167 miga[0] = miga[1] = miga[2] = NULL;
168 if (single_plane_incident) { /* isotropic BSDF? */
169 RBFNODE *rbf; /* find edge we're on */
170 for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) {
171 if (input_orient*rbf->invec[2] < input_orient*invec[2])
172 break;
173 if (rbf->next != NULL &&
174 input_orient*rbf->next->invec[2] <
175 input_orient*invec[2]) {
176 for (miga[0] = rbf->ejl; miga[0] != NULL;
177 miga[0] = nextedge(rbf,miga[0]))
178 if (opp_rbf(rbf,miga[0]) == rbf->next)
179 return(0);
180 break;
181 }
182 }
183 return(-1); /* outside range! */
184 }
185 { /* else use triangle mesh */
186 int sym = use_symmetry(invec);
187 int nedges = 0;
188 MIGRATION *mep;
189 unsigned char *emap;
190 /* clear visitation map */
191 for (mep = mig_list; mep != NULL; mep = mep->next)
192 ++nedges;
193 emap = (unsigned char *)calloc((nedges*(nedges-1) + 7)>>3, 1);
194 if (emap == NULL) {
195 fprintf(stderr, "%s: Out of memory in get_interp()\n",
196 progname);
197 exit(1);
198 }
199 /* identify intersection */
200 if (!in_mesh(miga, emap, nedges, invec, mig_list))
201 sym = -1; /* outside mesh */
202 else if (miga[1] != NULL &&
203 (miga[2] == NULL || !order_triangle(miga))) {
204 #ifdef DEBUG
205 fputs("Munged triangle in get_interp()\n", stderr);
206 #endif
207 sym = -1;
208 }
209 free(emap);
210 return(sym); /* return in standard order */
211 }
212 }
213
214 /* Advect and allocate new RBF along edge */
215 static RBFNODE *
216 e_advect_rbf(const MIGRATION *mig, const FVECT invec)
217 {
218 RBFNODE *rbf;
219 int n, i, j;
220 double t, full_dist;
221 /* get relative position */
222 t = acos(DOT(invec, mig->rbfv[0]->invec));
223 if (t < M_PI/GRIDRES) { /* near first DSF */
224 n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[0]->nrbf-1);
225 rbf = (RBFNODE *)malloc(n);
226 if (rbf == NULL)
227 goto memerr;
228 memcpy(rbf, mig->rbfv[0], n); /* just duplicate */
229 return(rbf);
230 }
231 full_dist = acos(DOT(mig->rbfv[0]->invec, mig->rbfv[1]->invec));
232 if (t > full_dist-M_PI/GRIDRES) { /* near second DSF */
233 n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[1]->nrbf-1);
234 rbf = (RBFNODE *)malloc(n);
235 if (rbf == NULL)
236 goto memerr;
237 memcpy(rbf, mig->rbfv[1], n); /* just duplicate */
238 return(rbf);
239 }
240 t /= full_dist;
241 n = 0; /* count migrating particles */
242 for (i = 0; i < mtx_nrows(mig); i++)
243 for (j = 0; j < mtx_ncols(mig); j++)
244 n += (mtx_coef(mig,i,j) > FTINY);
245 #ifdef DEBUG
246 fprintf(stderr, "Input RBFs have %d, %d nodes -> output has %d\n",
247 mig->rbfv[0]->nrbf, mig->rbfv[1]->nrbf, n);
248 #endif
249 rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1));
250 if (rbf == NULL)
251 goto memerr;
252 rbf->next = NULL; rbf->ejl = NULL;
253 VCOPY(rbf->invec, invec);
254 rbf->nrbf = n;
255 rbf->vtotal = 1.-t + t*mig->rbfv[1]->vtotal/mig->rbfv[0]->vtotal;
256 n = 0; /* advect RBF lobes */
257 for (i = 0; i < mtx_nrows(mig); i++) {
258 const RBFVAL *rbf0i = &mig->rbfv[0]->rbfa[i];
259 const float peak0 = rbf0i->peak;
260 const double rad0 = R2ANG(rbf0i->crad);
261 FVECT v0;
262 float mv;
263 ovec_from_pos(v0, rbf0i->gx, rbf0i->gy);
264 for (j = 0; j < mtx_ncols(mig); j++)
265 if ((mv = mtx_coef(mig,i,j)) > FTINY) {
266 const RBFVAL *rbf1j = &mig->rbfv[1]->rbfa[j];
267 double rad1 = R2ANG(rbf1j->crad);
268 FVECT v;
269 int pos[2];
270 rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal;
271 rbf->rbfa[n].crad = ANG2R(sqrt(rad0*rad0*(1.-t) +
272 rad1*rad1*t));
273 ovec_from_pos(v, rbf1j->gx, rbf1j->gy);
274 geodesic(v, v0, v, t, GEOD_REL);
275 pos_from_vec(pos, v);
276 rbf->rbfa[n].gx = pos[0];
277 rbf->rbfa[n].gy = pos[1];
278 ++n;
279 }
280 }
281 rbf->vtotal *= mig->rbfv[0]->vtotal; /* turn ratio into actual */
282 return(rbf);
283 memerr:
284 fprintf(stderr, "%s: Out of memory in e_advect_rbf()\n", progname);
285 exit(1);
286 return(NULL); /* pro forma return */
287 }
288
289 /* Partially advect between recorded incident angles and allocate new RBF */
290 RBFNODE *
291 advect_rbf(const FVECT invec)
292 {
293 FVECT sivec;
294 MIGRATION *miga[3];
295 RBFNODE *rbf;
296 int sym;
297 float mbfact, mcfact;
298 int n, i, j, k;
299 FVECT v0, v1, v2;
300 double s, t;
301
302 VCOPY(sivec, invec); /* find triangle/edge */
303 sym = get_interp(miga, sivec);
304 if (sym < 0) /* can't interpolate? */
305 return(NULL);
306 if (miga[1] == NULL) { /* advect along edge? */
307 rbf = e_advect_rbf(miga[0], sivec);
308 rev_rbf_symmetry(rbf, sym);
309 return(rbf);
310 }
311 #ifdef DEBUG
312 if (miga[0]->rbfv[0] != miga[2]->rbfv[0] |
313 miga[0]->rbfv[1] != miga[1]->rbfv[0] |
314 miga[1]->rbfv[1] != miga[2]->rbfv[1]) {
315 fprintf(stderr, "%s: Triangle vertex screw-up!\n", progname);
316 exit(1);
317 }
318 #endif
319 /* figure out position */
320 fcross(v0, miga[2]->rbfv[0]->invec, miga[2]->rbfv[1]->invec);
321 normalize(v0);
322 fcross(v2, miga[1]->rbfv[0]->invec, miga[1]->rbfv[1]->invec);
323 normalize(v2);
324 fcross(v1, sivec, miga[1]->rbfv[1]->invec);
325 normalize(v1);
326 s = acos(DOT(v0,v1)) / acos(DOT(v0,v2));
327 geodesic(v1, miga[0]->rbfv[0]->invec, miga[0]->rbfv[1]->invec,
328 s, GEOD_REL);
329 t = acos(DOT(v1,sivec)) / acos(DOT(v1,miga[1]->rbfv[1]->invec));
330 n = 0; /* count migrating particles */
331 for (i = 0; i < mtx_nrows(miga[0]); i++)
332 for (j = 0; j < mtx_ncols(miga[0]); j++)
333 for (k = (mtx_coef(miga[0],i,j) > FTINY) *
334 mtx_ncols(miga[2]); k--; )
335 n += (mtx_coef(miga[2],i,k) > FTINY &&
336 mtx_coef(miga[1],j,k) > FTINY);
337 #ifdef DEBUG
338 fprintf(stderr, "Input RBFs have %d, %d, %d nodes -> output has %d\n",
339 miga[0]->rbfv[0]->nrbf, miga[0]->rbfv[1]->nrbf,
340 miga[2]->rbfv[1]->nrbf, n);
341 #endif
342 rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1));
343 if (rbf == NULL) {
344 fprintf(stderr, "%s: Out of memory in advect_rbf()\n", progname);
345 exit(1);
346 }
347 rbf->next = NULL; rbf->ejl = NULL;
348 VCOPY(rbf->invec, sivec);
349 rbf->nrbf = n;
350 n = 0; /* compute RBF lobes */
351 mbfact = s * miga[0]->rbfv[1]->vtotal/miga[0]->rbfv[0]->vtotal *
352 (1.-t + t*miga[1]->rbfv[1]->vtotal/miga[1]->rbfv[0]->vtotal);
353 mcfact = (1.-s) *
354 (1.-t + t*miga[2]->rbfv[1]->vtotal/miga[2]->rbfv[0]->vtotal);
355 for (i = 0; i < mtx_nrows(miga[0]); i++) {
356 const RBFVAL *rbf0i = &miga[0]->rbfv[0]->rbfa[i];
357 const float w0i = rbf0i->peak;
358 const double rad0i = R2ANG(rbf0i->crad);
359 ovec_from_pos(v0, rbf0i->gx, rbf0i->gy);
360 for (j = 0; j < mtx_ncols(miga[0]); j++) {
361 const float ma = mtx_coef(miga[0],i,j);
362 const RBFVAL *rbf1j;
363 double rad1j, srad2;
364 if (ma <= FTINY)
365 continue;
366 rbf1j = &miga[0]->rbfv[1]->rbfa[j];
367 rad1j = R2ANG(rbf1j->crad);
368 srad2 = (1.-s)*(1.-t)*rad0i*rad0i + s*(1.-t)*rad1j*rad1j;
369 ovec_from_pos(v1, rbf1j->gx, rbf1j->gy);
370 geodesic(v1, v0, v1, s, GEOD_REL);
371 for (k = 0; k < mtx_ncols(miga[2]); k++) {
372 float mb = mtx_coef(miga[1],j,k);
373 float mc = mtx_coef(miga[2],i,k);
374 const RBFVAL *rbf2k;
375 double rad2k;
376 FVECT vout;
377 int pos[2];
378 if ((mb <= FTINY) | (mc <= FTINY))
379 continue;
380 rbf2k = &miga[2]->rbfv[1]->rbfa[k];
381 rbf->rbfa[n].peak = w0i * ma * (mb*mbfact + mc*mcfact);
382 rad2k = R2ANG(rbf2k->crad);
383 rbf->rbfa[n].crad = ANG2R(sqrt(srad2 + t*rad2k*rad2k));
384 ovec_from_pos(v2, rbf2k->gx, rbf2k->gy);
385 geodesic(vout, v1, v2, t, GEOD_REL);
386 pos_from_vec(pos, vout);
387 rbf->rbfa[n].gx = pos[0];
388 rbf->rbfa[n].gy = pos[1];
389 ++n;
390 }
391 }
392 }
393 rbf->vtotal = miga[0]->rbfv[0]->vtotal * (mbfact + mcfact);
394 rev_rbf_symmetry(rbf, sym);
395 return(rbf);
396 }