ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfinterp.c
Revision: 2.8
Committed: Wed Dec 12 04:49:59 2012 UTC (11 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.7: +8 -8 lines
Log Message:
Fixed main bug, which was forgetting Klems axis reversal

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdfinterp.c,v 2.7 2012/11/26 07:02:20 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[2] == NULL) | (vert[3] != 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 an edge */
89 static int
90 on_edge(const MIGRATION *ej, const FVECT ivec)
91 {
92 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
102 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 }
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 /* Test (and set) bitmap for edge */
130 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 /* 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 MIGRATION *ej1, *ej2;
155 RBFNODE *tv;
156 /* check visitation record */
157 if (!check_edge(emap, nedges, mig, 1))
158 return(0);
159 if (on_edge(mig, ivec)) {
160 miga[0] = mig; /* close enough to edge */
161 return(1);
162 }
163 /* do triangles either side */
164 for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL;
165 ej1 = nextedge(mig->rbfv[0],ej1)) {
166 if (ej1 == mig)
167 continue;
168 tv = opp_rbf(mig->rbfv[0],ej1);
169 for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2))
170 if (opp_rbf(tv,ej2) == mig->rbfv[1]) {
171 int do_ej1 = check_edge(emap, nedges, ej1, 0);
172 int do_ej2 = check_edge(emap, nedges, ej2, 0);
173 if (do_ej1 && in_mesh(miga, emap, nedges, ivec, ej1))
174 return(1);
175 if (do_ej2 && in_mesh(miga, emap, nedges, ivec, ej2))
176 return(1);
177 /* 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);
184 }
185 }
186 }
187 return(0); /* not near this edge */
188 }
189
190 /* Find edge(s) for interpolating the given vector, applying symmetry */
191 int
192 get_interp(MIGRATION *miga[3], FVECT invec)
193 {
194 miga[0] = miga[1] = miga[2] = NULL;
195 if (single_plane_incident) { /* isotropic BSDF? */
196 RBFNODE *rbf; /* find edge we're on */
197 for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) {
198 if (input_orient*rbf->invec[2] < input_orient*invec[2])
199 break;
200 if (rbf->next != NULL &&
201 input_orient*rbf->next->invec[2] <
202 input_orient*invec[2]) {
203 for (miga[0] = rbf->ejl; miga[0] != NULL;
204 miga[0] = nextedge(rbf,miga[0]))
205 if (opp_rbf(rbf,miga[0]) == rbf->next) {
206 double nf = 1.-rbf->invec[2]*rbf->invec[2];
207 if (nf > FTINY) {
208 nf = sqrt((1.-invec[2]*invec[2])/nf);
209 invec[0] = nf*rbf->invec[0];
210 invec[1] = nf*rbf->invec[1];
211 }
212 return(0);
213 }
214 break;
215 }
216 }
217 return(-1); /* outside range! */
218 }
219 { /* else use triangle mesh */
220 int sym = use_symmetry(invec);
221 int nedges = 0;
222 MIGRATION *mep;
223 unsigned char *emap;
224 /* clear visitation map */
225 for (mep = mig_list; mep != NULL; mep = mep->next)
226 ++nedges;
227 emap = (unsigned char *)calloc((nedges*(nedges-1) + 7)>>3, 1);
228 if (emap == NULL) {
229 fprintf(stderr, "%s: Out of memory in get_interp()\n",
230 progname);
231 exit(1);
232 }
233 /* identify intersection */
234 if (!in_mesh(miga, emap, nedges, invec, mig_list))
235 sym = -1; /* outside mesh */
236 else if (miga[1] != NULL &&
237 (miga[2] == NULL || !order_triangle(miga))) {
238 #ifdef DEBUG
239 fputs("Munged triangle in get_interp()\n", stderr);
240 #endif
241 sym = -1;
242 }
243 free(emap);
244 return(sym); /* return in standard order */
245 }
246 }
247
248 /* Advect and allocate new RBF along edge */
249 static RBFNODE *
250 e_advect_rbf(const MIGRATION *mig, const FVECT invec)
251 {
252 RBFNODE *rbf;
253 int n, i, j;
254 double t, full_dist;
255 /* get relative position */
256 t = acos(DOT(invec, mig->rbfv[0]->invec));
257 if (t < M_PI/grid_res) { /* near first DSF */
258 n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[0]->nrbf-1);
259 rbf = (RBFNODE *)malloc(n);
260 if (rbf == NULL)
261 goto memerr;
262 memcpy(rbf, mig->rbfv[0], n); /* just duplicate */
263 rbf->next = NULL; rbf->ejl = NULL;
264 return(rbf);
265 }
266 full_dist = acos(DOT(mig->rbfv[0]->invec, mig->rbfv[1]->invec));
267 if (t > full_dist-M_PI/grid_res) { /* near second DSF */
268 n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[1]->nrbf-1);
269 rbf = (RBFNODE *)malloc(n);
270 if (rbf == NULL)
271 goto memerr;
272 memcpy(rbf, mig->rbfv[1], n); /* just duplicate */
273 rbf->next = NULL; rbf->ejl = NULL;
274 return(rbf);
275 }
276 t /= full_dist;
277 n = 0; /* count migrating particles */
278 for (i = 0; i < mtx_nrows(mig); i++)
279 for (j = 0; j < mtx_ncols(mig); j++)
280 n += (mtx_coef(mig,i,j) > FTINY);
281 #ifdef DEBUG
282 fprintf(stderr, "Input RBFs have %d, %d nodes -> output has %d\n",
283 mig->rbfv[0]->nrbf, mig->rbfv[1]->nrbf, n);
284 #endif
285 rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1));
286 if (rbf == NULL)
287 goto memerr;
288 rbf->next = NULL; rbf->ejl = NULL;
289 VCOPY(rbf->invec, invec);
290 rbf->nrbf = n;
291 rbf->vtotal = 1.-t + t*mig->rbfv[1]->vtotal/mig->rbfv[0]->vtotal;
292 n = 0; /* advect RBF lobes */
293 for (i = 0; i < mtx_nrows(mig); i++) {
294 const RBFVAL *rbf0i = &mig->rbfv[0]->rbfa[i];
295 const float peak0 = rbf0i->peak;
296 const double rad0 = R2ANG(rbf0i->crad);
297 FVECT v0;
298 float mv;
299 ovec_from_pos(v0, rbf0i->gx, rbf0i->gy);
300 for (j = 0; j < mtx_ncols(mig); j++)
301 if ((mv = mtx_coef(mig,i,j)) > FTINY) {
302 const RBFVAL *rbf1j = &mig->rbfv[1]->rbfa[j];
303 double rad1 = R2ANG(rbf1j->crad);
304 FVECT v;
305 int pos[2];
306 rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal;
307 rbf->rbfa[n].crad = ANG2R(sqrt(rad0*rad0*(1.-t) +
308 rad1*rad1*t));
309 ovec_from_pos(v, rbf1j->gx, rbf1j->gy);
310 geodesic(v, v0, v, t, GEOD_REL);
311 pos_from_vec(pos, v);
312 rbf->rbfa[n].gx = pos[0];
313 rbf->rbfa[n].gy = pos[1];
314 ++n;
315 }
316 }
317 rbf->vtotal *= mig->rbfv[0]->vtotal; /* turn ratio into actual */
318 return(rbf);
319 memerr:
320 fprintf(stderr, "%s: Out of memory in e_advect_rbf()\n", progname);
321 exit(1);
322 return(NULL); /* pro forma return */
323 }
324
325 /* Partially advect between recorded incident angles and allocate new RBF */
326 RBFNODE *
327 advect_rbf(const FVECT invec)
328 {
329 FVECT sivec;
330 MIGRATION *miga[3];
331 RBFNODE *rbf;
332 int sym;
333 float mbfact, mcfact;
334 int n, i, j, k;
335 FVECT v0, v1, v2;
336 double s, t;
337
338 VCOPY(sivec, invec); /* find triangle/edge */
339 sym = get_interp(miga, sivec);
340 if (sym < 0) /* can't interpolate? */
341 return(NULL);
342 if (miga[1] == NULL) { /* advect along edge? */
343 rbf = e_advect_rbf(miga[0], sivec);
344 if (single_plane_incident)
345 rotate_rbf(rbf, invec);
346 else
347 rev_rbf_symmetry(rbf, sym);
348 return(rbf);
349 }
350 #ifdef DEBUG
351 if (miga[0]->rbfv[0] != miga[2]->rbfv[0] |
352 miga[0]->rbfv[1] != miga[1]->rbfv[0] |
353 miga[1]->rbfv[1] != miga[2]->rbfv[1]) {
354 fprintf(stderr, "%s: Triangle vertex screw-up!\n", progname);
355 exit(1);
356 }
357 #endif
358 /* figure out position */
359 fcross(v0, miga[2]->rbfv[0]->invec, miga[2]->rbfv[1]->invec);
360 normalize(v0);
361 fcross(v2, miga[1]->rbfv[0]->invec, miga[1]->rbfv[1]->invec);
362 normalize(v2);
363 fcross(v1, sivec, miga[1]->rbfv[1]->invec);
364 normalize(v1);
365 s = acos(DOT(v0,v1)) / acos(DOT(v0,v2));
366 geodesic(v1, miga[0]->rbfv[0]->invec, miga[0]->rbfv[1]->invec,
367 s, GEOD_REL);
368 t = acos(DOT(v1,sivec)) / acos(DOT(v1,miga[1]->rbfv[1]->invec));
369 n = 0; /* count migrating particles */
370 for (i = 0; i < mtx_nrows(miga[0]); i++)
371 for (j = 0; j < mtx_ncols(miga[0]); j++)
372 for (k = (mtx_coef(miga[0],i,j) > FTINY) *
373 mtx_ncols(miga[2]); k--; )
374 n += (mtx_coef(miga[2],i,k) > FTINY ||
375 mtx_coef(miga[1],j,k) > FTINY);
376 #ifdef DEBUG
377 fprintf(stderr, "Input RBFs have %d, %d, %d nodes -> output has %d\n",
378 miga[0]->rbfv[0]->nrbf, miga[0]->rbfv[1]->nrbf,
379 miga[2]->rbfv[1]->nrbf, n);
380 #endif
381 rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1));
382 if (rbf == NULL) {
383 fprintf(stderr, "%s: Out of memory in advect_rbf()\n", progname);
384 exit(1);
385 }
386 rbf->next = NULL; rbf->ejl = NULL;
387 VCOPY(rbf->invec, sivec);
388 rbf->nrbf = n;
389 n = 0; /* compute RBF lobes */
390 mbfact = s * miga[0]->rbfv[1]->vtotal/miga[0]->rbfv[0]->vtotal *
391 (1.-t + t*miga[1]->rbfv[1]->vtotal/miga[1]->rbfv[0]->vtotal);
392 mcfact = (1.-s) *
393 (1.-t + t*miga[2]->rbfv[1]->vtotal/miga[2]->rbfv[0]->vtotal);
394 for (i = 0; i < mtx_nrows(miga[0]); i++) {
395 const RBFVAL *rbf0i = &miga[0]->rbfv[0]->rbfa[i];
396 const float w0i = rbf0i->peak;
397 const double rad0i = R2ANG(rbf0i->crad);
398 ovec_from_pos(v0, rbf0i->gx, rbf0i->gy);
399 for (j = 0; j < mtx_ncols(miga[0]); j++) {
400 const float ma = mtx_coef(miga[0],i,j);
401 const RBFVAL *rbf1j;
402 double rad1j, srad2;
403 if (ma <= FTINY)
404 continue;
405 rbf1j = &miga[0]->rbfv[1]->rbfa[j];
406 rad1j = R2ANG(rbf1j->crad);
407 srad2 = (1.-s)*(1.-t)*rad0i*rad0i + s*(1.-t)*rad1j*rad1j;
408 ovec_from_pos(v1, rbf1j->gx, rbf1j->gy);
409 geodesic(v1, v0, v1, s, GEOD_REL);
410 for (k = 0; k < mtx_ncols(miga[2]); k++) {
411 float mb = mtx_coef(miga[1],j,k);
412 float mc = mtx_coef(miga[2],i,k);
413 const RBFVAL *rbf2k;
414 double rad2k;
415 int pos[2];
416 if ((mb <= FTINY) & (mc <= FTINY))
417 continue;
418 rbf2k = &miga[2]->rbfv[1]->rbfa[k];
419 rbf->rbfa[n].peak = w0i * ma * (mb*mbfact + mc*mcfact);
420 rad2k = R2ANG(rbf2k->crad);
421 rbf->rbfa[n].crad = ANG2R(sqrt(srad2 + t*rad2k*rad2k));
422 ovec_from_pos(v2, rbf2k->gx, rbf2k->gy);
423 geodesic(v2, v1, v2, t, GEOD_REL);
424 pos_from_vec(pos, v2);
425 rbf->rbfa[n].gx = pos[0];
426 rbf->rbfa[n].gy = pos[1];
427 ++n;
428 }
429 }
430 }
431 rbf->vtotal = miga[0]->rbfv[0]->vtotal * (mbfact + mcfact);
432 rev_rbf_symmetry(rbf, sym);
433 return(rbf);
434 }