ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfrep.c
Revision: 2.16
Committed: Tue Oct 22 05:48:05 2013 UTC (10 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.15: +12 -6 lines
Log Message:
Sped up RBF evaluation by about a factor of 4

File Contents

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