ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfrep.c
Revision: 2.6
Committed: Thu Nov 8 00:31:17 2012 UTC (11 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +32 -2 lines
Log Message:
Isotropic BRDF interpolation seems to work now

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdfrep.c,v 2.5 2012/11/07 03:04:23 greg Exp $";
3 #endif
4 /*
5 * Support BSDF representation as radial basis functions.
6 *
7 * G. Ward
8 */
9
10 #define _USE_MATH_DEFINES
11 #include <stdlib.h>
12 #include <string.h>
13 #include <math.h>
14 #include "rtio.h"
15 #include "resolu.h"
16 #include "bsdfrep.h"
17 /* active grid resolution */
18 int grid_res = GRIDRES;
19
20 /* coverage/symmetry using INP_QUAD? flags */
21 int inp_coverage = 0;
22 /* all incident angles in-plane so far? */
23 int single_plane_incident = -1;
24
25 /* input/output orientations */
26 int input_orient = 0;
27 int output_orient = 0;
28
29 /* processed incident DSF measurements */
30 RBFNODE *dsf_list = NULL;
31
32 /* RBF-linking matrices (edges) */
33 MIGRATION *mig_list = NULL;
34
35 /* current input direction */
36 double theta_in_deg, phi_in_deg;
37
38 /* Register new input direction */
39 int
40 new_input_direction(double new_theta, double new_phi)
41 {
42 if (!input_orient) /* check input orientation */
43 input_orient = 1 - 2*(new_theta > 90.);
44 else if (input_orient > 0 ^ new_theta < 90.) {
45 fprintf(stderr,
46 "%s: Cannot handle input angles on both sides of surface\n",
47 progname);
48 return(0);
49 }
50 /* normalize angle ranges */
51 while (new_theta < -180.)
52 new_theta += 360.;
53 while (new_theta > 180.)
54 new_theta -= 360.;
55 if (new_theta < 0) {
56 new_theta = -new_theta;
57 new_phi += 180.;
58 }
59 if ((theta_in_deg = new_theta) < 1.0)
60 return(1); /* don't rely on phi near normal */
61 while (new_phi < 0)
62 new_phi += 360.;
63 while (new_phi >= 360.)
64 new_phi -= 360.;
65 if (single_plane_incident > 0) /* check input coverage */
66 single_plane_incident = (round(new_phi) == round(phi_in_deg));
67 else if (single_plane_incident < 0)
68 single_plane_incident = 1;
69 phi_in_deg = new_phi;
70 if ((1. < new_phi) & (new_phi < 89.))
71 inp_coverage |= INP_QUAD1;
72 else if ((91. < new_phi) & (new_phi < 179.))
73 inp_coverage |= INP_QUAD2;
74 else if ((181. < new_phi) & (new_phi < 269.))
75 inp_coverage |= INP_QUAD3;
76 else if ((271. < new_phi) & (new_phi < 359.))
77 inp_coverage |= INP_QUAD4;
78 return(1);
79 }
80
81 /* Apply symmetry to the given vector based on distribution */
82 int
83 use_symmetry(FVECT vec)
84 {
85 double phi = get_phi360(vec);
86
87 switch (inp_coverage) {
88 case INP_QUAD1|INP_QUAD2|INP_QUAD3|INP_QUAD4:
89 break;
90 case INP_QUAD1|INP_QUAD2:
91 if ((-FTINY > phi) | (phi > 180.+FTINY))
92 goto mir_y;
93 break;
94 case INP_QUAD2|INP_QUAD3:
95 if ((90.-FTINY > phi) | (phi > 270.+FTINY))
96 goto mir_x;
97 break;
98 case INP_QUAD3|INP_QUAD4:
99 if ((180.-FTINY > phi) | (phi > 360.+FTINY))
100 goto mir_y;
101 break;
102 case INP_QUAD4|INP_QUAD1:
103 if ((270.-FTINY > phi) & (phi > 90.+FTINY))
104 goto mir_x;
105 break;
106 case INP_QUAD1:
107 if ((-FTINY > phi) | (phi > 90.+FTINY))
108 switch ((int)(phi*(1./90.))) {
109 case 1: goto mir_x;
110 case 2: goto mir_xy;
111 case 3: goto mir_y;
112 }
113 break;
114 case INP_QUAD2:
115 if ((90.-FTINY > phi) | (phi > 180.+FTINY))
116 switch ((int)(phi*(1./90.))) {
117 case 0: goto mir_x;
118 case 2: goto mir_y;
119 case 3: goto mir_xy;
120 }
121 break;
122 case INP_QUAD3:
123 if ((180.-FTINY > phi) | (phi > 270.+FTINY))
124 switch ((int)(phi*(1./90.))) {
125 case 0: goto mir_xy;
126 case 1: goto mir_y;
127 case 3: goto mir_x;
128 }
129 break;
130 case INP_QUAD4:
131 if ((270.-FTINY > phi) | (phi > 360.+FTINY))
132 switch ((int)(phi*(1./90.))) {
133 case 0: goto mir_y;
134 case 1: goto mir_xy;
135 case 2: goto mir_x;
136 }
137 break;
138 default:
139 fprintf(stderr, "%s: Illegal input coverage (%d)\n",
140 progname, inp_coverage);
141 exit(1);
142 }
143 return(0); /* in range */
144 mir_x:
145 vec[0] = -vec[0];
146 return(MIRROR_X);
147 mir_y:
148 vec[1] = -vec[1];
149 return(MIRROR_Y);
150 mir_xy:
151 vec[0] = -vec[0];
152 vec[1] = -vec[1];
153 return(MIRROR_X|MIRROR_Y);
154 }
155
156 /* Reverse symmetry based on what was done before */
157 void
158 rev_symmetry(FVECT vec, int sym)
159 {
160 if (sym & MIRROR_X)
161 vec[0] = -vec[0];
162 if (sym & MIRROR_Y)
163 vec[1] = -vec[1];
164 }
165
166 /* Reverse symmetry for an RBF distribution */
167 void
168 rev_rbf_symmetry(RBFNODE *rbf, int sym)
169 {
170 int n;
171
172 rev_symmetry(rbf->invec, sym);
173 if (sym & MIRROR_X)
174 for (n = rbf->nrbf; n-- > 0; )
175 rbf->rbfa[n].gx = grid_res-1 - rbf->rbfa[n].gx;
176 if (sym & MIRROR_Y)
177 for (n = rbf->nrbf; n-- > 0; )
178 rbf->rbfa[n].gy = grid_res-1 - rbf->rbfa[n].gy;
179 }
180
181 /* Rotate RBF to correspond to given incident vector */
182 void
183 rotate_rbf(RBFNODE *rbf, const FVECT invec)
184 {
185 static const FVECT vnorm = {.0, .0, 1.};
186 const double phi = atan2(invec[1],invec[0]) -
187 atan2(rbf->invec[1],rbf->invec[0]);
188 FVECT outvec;
189 int pos[2];
190 int n;
191 #ifdef DEBUG
192 {
193 double tdiff = 180./M_PI*fabs(acos(invec[2])-acos(rbf->invec[2]));
194 if (tdiff >= 1.5)
195 fprintf(stderr,
196 "%s: Warning - rotated theta differs by %.1f degrees\n",
197 progname, tdiff);
198 }
199 #endif
200 for (n = rbf->nrbf; n-- > 0; ) {
201 ovec_from_pos(outvec, rbf->rbfa[n].gx, rbf->rbfa[n].gy);
202 spinvector(outvec, outvec, vnorm, phi);
203 pos_from_vec(pos, outvec);
204 rbf->rbfa[n].gx = pos[0];
205 rbf->rbfa[n].gy = pos[1];
206 }
207 VCOPY(rbf->invec, invec);
208 }
209
210 /* Compute volume associated with Gaussian lobe */
211 double
212 rbf_volume(const RBFVAL *rbfp)
213 {
214 double rad = R2ANG(rbfp->crad);
215
216 return((2.*M_PI) * rbfp->peak * rad*rad);
217 }
218
219 /* Compute outgoing vector from grid position */
220 void
221 ovec_from_pos(FVECT vec, int xpos, int ypos)
222 {
223 double uv[2];
224 double r2;
225
226 SDsquare2disk(uv, (1./grid_res)*(xpos+.5), (1./grid_res)*(ypos+.5));
227 /* uniform hemispherical projection */
228 r2 = uv[0]*uv[0] + uv[1]*uv[1];
229 vec[0] = vec[1] = sqrt(2. - r2);
230 vec[0] *= uv[0];
231 vec[1] *= uv[1];
232 vec[2] = output_orient*(1. - r2);
233 }
234
235 /* Compute grid position from normalized input/output vector */
236 void
237 pos_from_vec(int pos[2], const FVECT vec)
238 {
239 double sq[2]; /* uniform hemispherical projection */
240 double norm = 1./sqrt(1. + fabs(vec[2]));
241
242 SDdisk2square(sq, vec[0]*norm, vec[1]*norm);
243
244 pos[0] = (int)(sq[0]*grid_res);
245 pos[1] = (int)(sq[1]*grid_res);
246 }
247
248 /* Evaluate RBF for DSF at the given normalized outgoing direction */
249 double
250 eval_rbfrep(const RBFNODE *rp, const FVECT outvec)
251 {
252 double res = .0;
253 const RBFVAL *rbfp;
254 FVECT odir;
255 double sig2;
256 int n;
257
258 if (rp == NULL)
259 return(.0);
260 rbfp = rp->rbfa;
261 for (n = rp->nrbf; n--; rbfp++) {
262 ovec_from_pos(odir, rbfp->gx, rbfp->gy);
263 sig2 = R2ANG(rbfp->crad);
264 sig2 = (DOT(odir,outvec) - 1.) / (sig2*sig2);
265 if (sig2 > -19.)
266 res += rbfp->peak * exp(sig2);
267 }
268 return(res);
269 }
270
271 /* Insert a new directional scattering function in our global list */
272 int
273 insert_dsf(RBFNODE *newrbf)
274 {
275 RBFNODE *rbf, *rbf_last;
276 int pos;
277 /* check for redundant meas. */
278 for (rbf = dsf_list; rbf != NULL; rbf = rbf->next)
279 if (DOT(rbf->invec, newrbf->invec) >= 1.-FTINY) {
280 fprintf(stderr,
281 "%s: Duplicate incident measurement (ignored)\n",
282 progname);
283 free(newrbf);
284 return(-1);
285 }
286 /* keep in ascending theta order */
287 for (rbf_last = NULL, rbf = dsf_list; rbf != NULL;
288 rbf_last = rbf, rbf = rbf->next)
289 if (single_plane_incident && input_orient*rbf->invec[2] <
290 input_orient*newrbf->invec[2])
291 break;
292 if (rbf_last == NULL) { /* insert new node in list */
293 newrbf->ord = 0;
294 newrbf->next = dsf_list;
295 dsf_list = newrbf;
296 } else {
297 newrbf->ord = rbf_last->ord + 1;
298 newrbf->next = rbf;
299 rbf_last->next = newrbf;
300 }
301 rbf_last = newrbf;
302 while (rbf != NULL) { /* update ordinal positions */
303 rbf->ord = rbf_last->ord + 1;
304 rbf_last = rbf;
305 rbf = rbf->next;
306 }
307 return(newrbf->ord);
308 }
309
310 /* Get the DSF indicated by its ordinal position */
311 RBFNODE *
312 get_dsf(int ord)
313 {
314 RBFNODE *rbf;
315
316 for (rbf = dsf_list; rbf != NULL; rbf = rbf->next)
317 if (rbf->ord == ord)
318 return(rbf);
319 return(NULL);
320 }
321
322 /* Get triangle surface orientation (unnormalized) */
323 void
324 tri_orient(FVECT vres, const FVECT v1, const FVECT v2, const FVECT v3)
325 {
326 FVECT v2minus1, v3minus2;
327
328 VSUB(v2minus1, v2, v1);
329 VSUB(v3minus2, v3, v2);
330 VCROSS(vres, v2minus1, v3minus2);
331 }
332
333 /* Determine if vertex order is reversed (inward normal) */
334 int
335 is_rev_tri(const FVECT v1, const FVECT v2, const FVECT v3)
336 {
337 FVECT tor;
338
339 tri_orient(tor, v1, v2, v3);
340
341 return(DOT(tor, v2) < 0.);
342 }
343
344 /* Find vertices completing triangles on either side of the given edge */
345 int
346 get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
347 {
348 const MIGRATION *ej1, *ej2;
349 RBFNODE *tv;
350
351 rbfv[0] = rbfv[1] = NULL;
352 if (mig == NULL)
353 return(0);
354 for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL;
355 ej1 = nextedge(mig->rbfv[0],ej1)) {
356 if (ej1 == mig)
357 continue;
358 tv = opp_rbf(mig->rbfv[0],ej1);
359 for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2))
360 if (opp_rbf(tv,ej2) == mig->rbfv[1]) {
361 rbfv[is_rev_tri(mig->rbfv[0]->invec,
362 mig->rbfv[1]->invec,
363 tv->invec)] = tv;
364 break;
365 }
366 }
367 return((rbfv[0] != NULL) + (rbfv[1] != NULL));
368 }
369
370 /* Clear our BSDF representation and free memory */
371 void
372 clear_bsdf_rep(void)
373 {
374 while (mig_list != NULL) {
375 MIGRATION *mig = mig_list;
376 mig_list = mig->next;
377 free(mig);
378 }
379 while (dsf_list != NULL) {
380 RBFNODE *rbf = dsf_list;
381 dsf_list = rbf->next;
382 free(rbf);
383 }
384 inp_coverage = 0;
385 single_plane_incident = -1;
386 input_orient = output_orient = 0;
387 grid_res = GRIDRES;
388 }
389
390 /* Write our BSDF mesh interpolant out to the given binary stream */
391 void
392 save_bsdf_rep(FILE *ofp)
393 {
394 RBFNODE *rbf;
395 MIGRATION *mig;
396 int i, n;
397 /* finish header */
398 fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage);
399 fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient);
400 fprintf(ofp, "GRIDRES=%d\n", grid_res);
401 fputformat(BSDFREP_FMT, ofp);
402 fputc('\n', ofp);
403 /* write each DSF */
404 for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) {
405 putint(rbf->ord, 4, ofp);
406 putflt(rbf->invec[0], ofp);
407 putflt(rbf->invec[1], ofp);
408 putflt(rbf->invec[2], ofp);
409 putflt(rbf->vtotal, ofp);
410 putint(rbf->nrbf, 4, ofp);
411 for (i = 0; i < rbf->nrbf; i++) {
412 putflt(rbf->rbfa[i].peak, ofp);
413 putint(rbf->rbfa[i].crad, 2, ofp);
414 putint(rbf->rbfa[i].gx, 1, ofp);
415 putint(rbf->rbfa[i].gy, 1, ofp);
416 }
417 }
418 putint(-1, 4, ofp); /* terminator */
419 /* write each migration matrix */
420 for (mig = mig_list; mig != NULL; mig = mig->next) {
421 int zerocnt = 0;
422 putint(mig->rbfv[0]->ord, 4, ofp);
423 putint(mig->rbfv[1]->ord, 4, ofp);
424 /* write out as sparse data */
425 n = mtx_nrows(mig) * mtx_ncols(mig);
426 for (i = 0; i < n; i++) {
427 if (zerocnt == 0xff) {
428 putint(0xff, 1, ofp); zerocnt = 0;
429 }
430 if (mig->mtx[i] != 0) {
431 putint(zerocnt, 1, ofp); zerocnt = 0;
432 putflt(mig->mtx[i], ofp);
433 } else
434 ++zerocnt;
435 }
436 putint(zerocnt, 1, ofp);
437 }
438 putint(-1, 4, ofp); /* terminator */
439 putint(-1, 4, ofp);
440 if (fflush(ofp) == EOF) {
441 fprintf(stderr, "%s: error writing BSDF interpolant\n",
442 progname);
443 exit(1);
444 }
445 }
446
447 /* Check header line for critical information */
448 static int
449 headline(char *s, void *p)
450 {
451 char fmt[32];
452
453 if (!strncmp(s, "SYMMETRY=", 9)) {
454 inp_coverage = atoi(s+9);
455 single_plane_incident = !inp_coverage;
456 return(0);
457 }
458 if (!strncmp(s, "IO_SIDES=", 9)) {
459 sscanf(s+9, "%d %d", &input_orient, &output_orient);
460 return(0);
461 }
462 if (!strncmp(s, "GRIDRES=", 8)) {
463 sscanf(s+8, "%d", &grid_res);
464 return(0);
465 }
466 if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT))
467 return(-1);
468 return(0);
469 }
470
471 /* Read a BSDF mesh interpolant from the given binary stream */
472 int
473 load_bsdf_rep(FILE *ifp)
474 {
475 RBFNODE rbfh;
476 int from_ord, to_ord;
477 int i;
478
479 clear_bsdf_rep();
480 if (ifp == NULL)
481 return(0);
482 if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 |
483 !input_orient | !output_orient) {
484 fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n",
485 progname);
486 return(0);
487 }
488 rbfh.next = NULL; /* read each DSF */
489 rbfh.ejl = NULL;
490 while ((rbfh.ord = getint(4, ifp)) >= 0) {
491 RBFNODE *newrbf;
492
493 rbfh.invec[0] = getflt(ifp);
494 rbfh.invec[1] = getflt(ifp);
495 rbfh.invec[2] = getflt(ifp);
496 rbfh.vtotal = getflt(ifp);
497 rbfh.nrbf = getint(4, ifp);
498 newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) +
499 sizeof(RBFVAL)*(rbfh.nrbf-1));
500 if (newrbf == NULL)
501 goto memerr;
502 memcpy(newrbf, &rbfh, sizeof(RBFNODE));
503 for (i = 0; i < rbfh.nrbf; i++) {
504 newrbf->rbfa[i].peak = getflt(ifp);
505 newrbf->rbfa[i].crad = getint(2, ifp) & 0xffff;
506 newrbf->rbfa[i].gx = getint(1, ifp) & 0xff;
507 newrbf->rbfa[i].gy = getint(1, ifp) & 0xff;
508 }
509 if (feof(ifp))
510 goto badEOF;
511 /* insert in global list */
512 if (insert_dsf(newrbf) != rbfh.ord) {
513 fprintf(stderr, "%s: error adding DSF\n", progname);
514 return(0);
515 }
516 }
517 /* read each migration matrix */
518 while ((from_ord = getint(4, ifp)) >= 0 &&
519 (to_ord = getint(4, ifp)) >= 0) {
520 RBFNODE *from_rbf = get_dsf(from_ord);
521 RBFNODE *to_rbf = get_dsf(to_ord);
522 MIGRATION *newmig;
523 int n;
524
525 if ((from_rbf == NULL) | (to_rbf == NULL)) {
526 fprintf(stderr,
527 "%s: bad DSF reference in migration edge\n",
528 progname);
529 return(0);
530 }
531 n = from_rbf->nrbf * to_rbf->nrbf;
532 newmig = (MIGRATION *)malloc(sizeof(MIGRATION) +
533 sizeof(float)*(n-1));
534 if (newmig == NULL)
535 goto memerr;
536 newmig->rbfv[0] = from_rbf;
537 newmig->rbfv[1] = to_rbf;
538 memset(newmig->mtx, 0, sizeof(float)*n);
539 for (i = 0; ; ) { /* read sparse data */
540 int zc = getint(1, ifp) & 0xff;
541 if ((i += zc) >= n)
542 break;
543 if (zc == 0xff)
544 continue;
545 newmig->mtx[i++] = getflt(ifp);
546 }
547 if (feof(ifp))
548 goto badEOF;
549 /* insert in edge lists */
550 newmig->enxt[0] = from_rbf->ejl;
551 from_rbf->ejl = newmig;
552 newmig->enxt[1] = to_rbf->ejl;
553 to_rbf->ejl = newmig;
554 /* push onto global list */
555 newmig->next = mig_list;
556 mig_list = newmig;
557 }
558 return(1); /* success! */
559 memerr:
560 fprintf(stderr, "%s: Out of memory in load_bsdf_rep()\n", progname);
561 exit(1);
562 badEOF:
563 fprintf(stderr, "%s: Unexpected EOF in load_bsdf_rep()\n", progname);
564 return(0);
565 }