ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfinterp.c
Revision: 2.18
Committed: Mon Mar 24 17:22:33 2014 UTC (10 years ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2
Changes since 2.17: +6 -5 lines
Log Message:
Major bug in isotropic material interpolation near normal

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdfinterp.c,v 2.17 2014/02/19 05:16:06 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 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)
108 return(0);
109
110 cos_c = DOT(ej->rbfv[0]->invec, ej->rbfv[1]->invec);
111
112 return(cos_c - cos_aplusb < .001);
113 }
114
115 /* Determine if we are inside the given triangle */
116 static int
117 in_tri(const RBFNODE *v1, const RBFNODE *v2, const RBFNODE *v3, const FVECT p)
118 {
119 FVECT vc;
120 int sgn1, sgn2, sgn3;
121 /* signed volume test */
122 VCROSS(vc, v1->invec, v2->invec);
123 sgn1 = (DOT(p, vc) > 0);
124 VCROSS(vc, v2->invec, v3->invec);
125 sgn2 = (DOT(p, vc) > 0);
126 if (sgn1 != sgn2)
127 return(0);
128 VCROSS(vc, v3->invec, v1->invec);
129 sgn3 = (DOT(p, vc) > 0);
130 return(sgn2 == sgn3);
131 }
132
133 /* Test (and set) bitmap for edge */
134 static int
135 check_edge(unsigned char *emap, int nedges, const MIGRATION *mig, int mark)
136 {
137 int ejndx, bit2check;
138
139 if (mig->rbfv[0]->ord > mig->rbfv[1]->ord)
140 ejndx = mig->rbfv[1]->ord + (nedges-1)*mig->rbfv[0]->ord;
141 else
142 ejndx = mig->rbfv[0]->ord + (nedges-1)*mig->rbfv[1]->ord;
143
144 bit2check = 1<<(ejndx&07);
145
146 if (emap[ejndx>>3] & bit2check)
147 return(0);
148 if (mark)
149 emap[ejndx>>3] |= bit2check;
150 return(1);
151 }
152
153 /* Compute intersection with the given position over remaining mesh */
154 static int
155 in_mesh(MIGRATION *miga[3], unsigned char *emap, int nedges,
156 const FVECT ivec, MIGRATION *mig)
157 {
158 RBFNODE *tv[2];
159 MIGRATION *sej[2], *dej[2];
160 int i;
161 /* check visitation record */
162 if (!check_edge(emap, nedges, mig, 1))
163 return(0);
164 if (on_edge(mig, ivec)) {
165 miga[0] = mig; /* close enough to edge */
166 return(1);
167 }
168 if (!get_triangles(tv, mig)) /* do triangles either side? */
169 return(0);
170 for (i = 2; i--; ) { /* identify edges to check */
171 MIGRATION *ej;
172 sej[i] = dej[i] = NULL;
173 if (tv[i] == NULL)
174 continue;
175 for (ej = tv[i]->ejl; ej != NULL; ej = nextedge(tv[i],ej)) {
176 RBFNODE *rbfop = opp_rbf(tv[i],ej);
177 if (rbfop == mig->rbfv[0]) {
178 if (check_edge(emap, nedges, ej, 0))
179 sej[i] = ej;
180 } else if (rbfop == mig->rbfv[1]) {
181 if (check_edge(emap, nedges, ej, 0))
182 dej[i] = ej;
183 }
184 }
185 }
186 for (i = 2; i--; ) { /* check triangles just once */
187 if (sej[i] != NULL && in_mesh(miga, emap, nedges, ivec, sej[i]))
188 return(1);
189 if (dej[i] != NULL && in_mesh(miga, emap, nedges, ivec, dej[i]))
190 return(1);
191 if ((sej[i] == NULL) | (dej[i] == NULL))
192 continue;
193 if (in_tri(mig->rbfv[0], mig->rbfv[1], tv[i], ivec)) {
194 miga[0] = mig;
195 miga[1] = sej[i];
196 miga[2] = dej[i];
197 return(1);
198 }
199 }
200 return(0); /* not near this edge */
201 }
202
203 /* Find edge(s) for interpolating the given vector, applying symmetry */
204 int
205 get_interp(MIGRATION *miga[3], FVECT invec)
206 {
207 miga[0] = miga[1] = miga[2] = NULL;
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]-FTINY)
212 break;
213 if (rbf->next != NULL && input_orient*rbf->next->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. -
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->next->invec[0];
223 invec[1] = nf*rbf->next->invec[1];
224 }
225 return(0); /* rotational symmetry */
226 }
227 break;
228 }
229 }
230 return(-1); /* outside range! */
231 }
232 { /* else use triangle mesh */
233 int sym = use_symmetry(invec);
234 int nedges = 0;
235 MIGRATION *mep;
236 unsigned char *emap;
237 /* clear visitation map */
238 for (mep = mig_list; mep != NULL; mep = mep->next)
239 ++nedges;
240 emap = (unsigned char *)calloc((nedges*(nedges-1) + 7)>>3, 1);
241 if (emap == NULL) {
242 fprintf(stderr, "%s: Out of memory in get_interp()\n",
243 progname);
244 exit(1);
245 }
246 /* identify intersection */
247 if (!in_mesh(miga, emap, nedges, invec, mig_list)) {
248 #ifdef DEBUG
249 fprintf(stderr,
250 "Incident angle (%.1f,%.1f) deg. outside mesh\n",
251 get_theta180(invec), get_phi360(invec));
252 #endif
253 sym = -1; /* outside mesh */
254 } else if (miga[1] != NULL &&
255 (miga[2] == NULL || !order_triangle(miga))) {
256 #ifdef DEBUG
257 fputs("Munged triangle in get_interp()\n", stderr);
258 #endif
259 sym = -1;
260 }
261 free(emap);
262 return(sym); /* return in standard order */
263 }
264 }
265
266 /* Advect between recorded incident angles and allocate new RBF */
267 RBFNODE *
268 advect_rbf(const FVECT invec, int lobe_lim)
269 {
270 double cthresh = FTINY;
271 FVECT sivec;
272 MIGRATION *miga[3];
273 RBFNODE *rbf;
274 int sym;
275 float mbfact, mcfact;
276 int n, i, j, k;
277 FVECT v0, v1, v2;
278 double s, t;
279
280 VCOPY(sivec, invec); /* find triangle/edge */
281 sym = get_interp(miga, sivec);
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, lobe_lim);
286 if (single_plane_incident)
287 rotate_rbf(rbf, invec);
288 else
289 rev_rbf_symmetry(rbf, sym);
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])) {
296 fprintf(stderr, "%s: Triangle vertex screw-up!\n", progname);
297 exit(1);
298 }
299 #endif
300 /* figure out position */
301 fcross(v0, miga[2]->rbfv[0]->invec, miga[2]->rbfv[1]->invec);
302 normalize(v0);
303 fcross(v2, miga[1]->rbfv[0]->invec, miga[1]->rbfv[1]->invec);
304 normalize(v2);
305 fcross(v1, sivec, miga[1]->rbfv[1]->invec);
306 normalize(v1);
307 s = acos(DOT(v0,v1)) / acos(DOT(v0,v2));
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) > cthresh) *
316 mtx_ncols(miga[2]); k--; )
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,
327 miga[2]->rbfv[1]->nrbf, n);
328 #endif
329 rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1));
330 if (rbf == NULL) {
331 fprintf(stderr, "%s: Out of memory in advect_rbf()\n", progname);
332 exit(1);
333 }
334 rbf->next = NULL; rbf->ejl = NULL;
335 VCOPY(rbf->invec, sivec);
336 rbf->nrbf = n;
337 n = 0; /* compute RBF lobes */
338 mbfact = s * miga[0]->rbfv[1]->vtotal/miga[0]->rbfv[0]->vtotal *
339 (1.-t + t*miga[1]->rbfv[1]->vtotal/miga[1]->rbfv[0]->vtotal);
340 mcfact = (1.-s) *
341 (1.-t + t*miga[2]->rbfv[1]->vtotal/miga[2]->rbfv[0]->vtotal);
342 for (i = 0; i < mtx_nrows(miga[0]); i++) {
343 const RBFVAL *rbf0i = &miga[0]->rbfv[0]->rbfa[i];
344 const float w0i = rbf0i->peak;
345 const double rad0i = R2ANG(rbf0i->crad);
346 ovec_from_pos(v0, rbf0i->gx, rbf0i->gy);
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 srad2;
351 if (ma <= cthresh)
352 continue;
353 rbf1j = &miga[0]->rbfv[1]->rbfa[j];
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 rad2;
363 int pos[2];
364 if ((mb <= cthresh) & (mc <= cthresh))
365 continue;
366 rbf2k = &miga[2]->rbfv[1]->rbfa[k];
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);
375 rbf->rbfa[n].gx = pos[0];
376 rbf->rbfa[n].gy = pos[1];
377 ++n;
378 }
379 }
380 }
381 rbf->vtotal = miga[0]->rbfv[0]->vtotal * (mbfact + mcfact);
382 rev_rbf_symmetry(rbf, sym);
383 return(rbf);
384 }