ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfrep.c
Revision: 2.7
Committed: Thu Nov 8 22:05:04 2012 UTC (11 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +5 -7 lines
Log Message:
Took sort out so it is done only once

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdfrep.c,v 2.6 2012/11/08 00:31:17 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 double tdiff = 180./M_PI*fabs(acos(invec[2])-acos(rbf->invec[2]));
193 if (tdiff >= 1.5)
194 fprintf(stderr,
195 "%s: Warning - rotated theta differs by %.1f degrees\n",
196 progname, tdiff);
197 #endif
198 for (n = 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 volume associated with Gaussian lobe */
209 double
210 rbf_volume(const RBFVAL *rbfp)
211 {
212 double rad = R2ANG(rbfp->crad);
213
214 return((2.*M_PI) * rbfp->peak * rad*rad);
215 }
216
217 /* Compute outgoing vector from grid position */
218 void
219 ovec_from_pos(FVECT vec, int xpos, int ypos)
220 {
221 double uv[2];
222 double r2;
223
224 SDsquare2disk(uv, (1./grid_res)*(xpos+.5), (1./grid_res)*(ypos+.5));
225 /* uniform hemispherical projection */
226 r2 = uv[0]*uv[0] + uv[1]*uv[1];
227 vec[0] = vec[1] = sqrt(2. - r2);
228 vec[0] *= uv[0];
229 vec[1] *= uv[1];
230 vec[2] = output_orient*(1. - r2);
231 }
232
233 /* Compute grid position from normalized input/output vector */
234 void
235 pos_from_vec(int pos[2], const FVECT vec)
236 {
237 double sq[2]; /* uniform hemispherical projection */
238 double norm = 1./sqrt(1. + fabs(vec[2]));
239
240 SDdisk2square(sq, vec[0]*norm, vec[1]*norm);
241
242 pos[0] = (int)(sq[0]*grid_res);
243 pos[1] = (int)(sq[1]*grid_res);
244 }
245
246 /* Evaluate RBF for DSF at the given normalized outgoing direction */
247 double
248 eval_rbfrep(const RBFNODE *rp, const FVECT outvec)
249 {
250 double res = .0;
251 const RBFVAL *rbfp;
252 FVECT odir;
253 double sig2;
254 int n;
255
256 if (rp == NULL)
257 return(.0);
258 rbfp = rp->rbfa;
259 for (n = rp->nrbf; n--; rbfp++) {
260 ovec_from_pos(odir, rbfp->gx, rbfp->gy);
261 sig2 = R2ANG(rbfp->crad);
262 sig2 = (DOT(odir,outvec) - 1.) / (sig2*sig2);
263 if (sig2 > -19.)
264 res += rbfp->peak * exp(sig2);
265 }
266 return(res);
267 }
268
269 /* Insert a new directional scattering function in our global list */
270 int
271 insert_dsf(RBFNODE *newrbf)
272 {
273 RBFNODE *rbf, *rbf_last;
274 int pos;
275 /* check for redundant meas. */
276 for (rbf = dsf_list; rbf != NULL; rbf = rbf->next)
277 if (DOT(rbf->invec, newrbf->invec) >= 1.-FTINY) {
278 fprintf(stderr,
279 "%s: Duplicate incident measurement (ignored)\n",
280 progname);
281 free(newrbf);
282 return(-1);
283 }
284 /* keep in ascending theta order */
285 for (rbf_last = NULL, rbf = dsf_list; rbf != NULL;
286 rbf_last = rbf, rbf = rbf->next)
287 if (single_plane_incident && input_orient*rbf->invec[2] <
288 input_orient*newrbf->invec[2])
289 break;
290 if (rbf_last == NULL) { /* insert new node in list */
291 newrbf->ord = 0;
292 newrbf->next = dsf_list;
293 dsf_list = newrbf;
294 } else {
295 newrbf->ord = rbf_last->ord + 1;
296 newrbf->next = rbf;
297 rbf_last->next = newrbf;
298 }
299 rbf_last = newrbf;
300 while (rbf != NULL) { /* update ordinal positions */
301 rbf->ord = rbf_last->ord + 1;
302 rbf_last = rbf;
303 rbf = rbf->next;
304 }
305 return(newrbf->ord);
306 }
307
308 /* Get the DSF indicated by its ordinal position */
309 RBFNODE *
310 get_dsf(int ord)
311 {
312 RBFNODE *rbf;
313
314 for (rbf = dsf_list; rbf != NULL; rbf = rbf->next)
315 if (rbf->ord == ord)
316 return(rbf);
317 return(NULL);
318 }
319
320 /* Get triangle surface orientation (unnormalized) */
321 void
322 tri_orient(FVECT vres, const FVECT v1, const FVECT v2, const FVECT v3)
323 {
324 FVECT v2minus1, v3minus2;
325
326 VSUB(v2minus1, v2, v1);
327 VSUB(v3minus2, v3, v2);
328 VCROSS(vres, v2minus1, v3minus2);
329 }
330
331 /* Determine if vertex order is reversed (inward normal) */
332 int
333 is_rev_tri(const FVECT v1, const FVECT v2, const FVECT v3)
334 {
335 FVECT tor;
336
337 tri_orient(tor, v1, v2, v3);
338
339 return(DOT(tor, v2) < 0.);
340 }
341
342 /* Find vertices completing triangles on either side of the given edge */
343 int
344 get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
345 {
346 const MIGRATION *ej1, *ej2;
347 RBFNODE *tv;
348
349 rbfv[0] = rbfv[1] = NULL;
350 if (mig == NULL)
351 return(0);
352 for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL;
353 ej1 = nextedge(mig->rbfv[0],ej1)) {
354 if (ej1 == mig)
355 continue;
356 tv = opp_rbf(mig->rbfv[0],ej1);
357 for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2))
358 if (opp_rbf(tv,ej2) == mig->rbfv[1]) {
359 rbfv[is_rev_tri(mig->rbfv[0]->invec,
360 mig->rbfv[1]->invec,
361 tv->invec)] = tv;
362 break;
363 }
364 }
365 return((rbfv[0] != NULL) + (rbfv[1] != NULL));
366 }
367
368 /* Clear our BSDF representation and free memory */
369 void
370 clear_bsdf_rep(void)
371 {
372 while (mig_list != NULL) {
373 MIGRATION *mig = mig_list;
374 mig_list = mig->next;
375 free(mig);
376 }
377 while (dsf_list != NULL) {
378 RBFNODE *rbf = dsf_list;
379 dsf_list = rbf->next;
380 free(rbf);
381 }
382 inp_coverage = 0;
383 single_plane_incident = -1;
384 input_orient = output_orient = 0;
385 grid_res = GRIDRES;
386 }
387
388 /* Write our BSDF mesh interpolant out to the given binary stream */
389 void
390 save_bsdf_rep(FILE *ofp)
391 {
392 RBFNODE *rbf;
393 MIGRATION *mig;
394 int i, n;
395 /* finish header */
396 fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage);
397 fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient);
398 fprintf(ofp, "GRIDRES=%d\n", grid_res);
399 fputformat(BSDFREP_FMT, ofp);
400 fputc('\n', ofp);
401 /* write each DSF */
402 for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) {
403 putint(rbf->ord, 4, ofp);
404 putflt(rbf->invec[0], ofp);
405 putflt(rbf->invec[1], ofp);
406 putflt(rbf->invec[2], ofp);
407 putflt(rbf->vtotal, ofp);
408 putint(rbf->nrbf, 4, ofp);
409 for (i = 0; i < rbf->nrbf; i++) {
410 putflt(rbf->rbfa[i].peak, ofp);
411 putint(rbf->rbfa[i].crad, 2, ofp);
412 putint(rbf->rbfa[i].gx, 1, ofp);
413 putint(rbf->rbfa[i].gy, 1, ofp);
414 }
415 }
416 putint(-1, 4, ofp); /* terminator */
417 /* write each migration matrix */
418 for (mig = mig_list; mig != NULL; mig = mig->next) {
419 int zerocnt = 0;
420 putint(mig->rbfv[0]->ord, 4, ofp);
421 putint(mig->rbfv[1]->ord, 4, ofp);
422 /* write out as sparse data */
423 n = mtx_nrows(mig) * mtx_ncols(mig);
424 for (i = 0; i < n; i++) {
425 if (zerocnt == 0xff) {
426 putint(0xff, 1, ofp); zerocnt = 0;
427 }
428 if (mig->mtx[i] != 0) {
429 putint(zerocnt, 1, ofp); zerocnt = 0;
430 putflt(mig->mtx[i], ofp);
431 } else
432 ++zerocnt;
433 }
434 putint(zerocnt, 1, ofp);
435 }
436 putint(-1, 4, ofp); /* terminator */
437 putint(-1, 4, ofp);
438 if (fflush(ofp) == EOF) {
439 fprintf(stderr, "%s: error writing BSDF interpolant\n",
440 progname);
441 exit(1);
442 }
443 }
444
445 /* Check header line for critical information */
446 static int
447 headline(char *s, void *p)
448 {
449 char fmt[32];
450
451 if (!strncmp(s, "SYMMETRY=", 9)) {
452 inp_coverage = atoi(s+9);
453 single_plane_incident = !inp_coverage;
454 return(0);
455 }
456 if (!strncmp(s, "IO_SIDES=", 9)) {
457 sscanf(s+9, "%d %d", &input_orient, &output_orient);
458 return(0);
459 }
460 if (!strncmp(s, "GRIDRES=", 8)) {
461 sscanf(s+8, "%d", &grid_res);
462 return(0);
463 }
464 if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT))
465 return(-1);
466 return(0);
467 }
468
469 /* Read a BSDF mesh interpolant from the given binary stream */
470 int
471 load_bsdf_rep(FILE *ifp)
472 {
473 RBFNODE rbfh;
474 int from_ord, to_ord;
475 int i;
476
477 clear_bsdf_rep();
478 if (ifp == NULL)
479 return(0);
480 if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 |
481 !input_orient | !output_orient) {
482 fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n",
483 progname);
484 return(0);
485 }
486 rbfh.next = NULL; /* read each DSF */
487 rbfh.ejl = NULL;
488 while ((rbfh.ord = getint(4, ifp)) >= 0) {
489 RBFNODE *newrbf;
490
491 rbfh.invec[0] = getflt(ifp);
492 rbfh.invec[1] = getflt(ifp);
493 rbfh.invec[2] = getflt(ifp);
494 rbfh.vtotal = getflt(ifp);
495 rbfh.nrbf = getint(4, ifp);
496 newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) +
497 sizeof(RBFVAL)*(rbfh.nrbf-1));
498 if (newrbf == NULL)
499 goto memerr;
500 memcpy(newrbf, &rbfh, sizeof(RBFNODE));
501 for (i = 0; i < rbfh.nrbf; i++) {
502 newrbf->rbfa[i].peak = getflt(ifp);
503 newrbf->rbfa[i].crad = getint(2, ifp) & 0xffff;
504 newrbf->rbfa[i].gx = getint(1, ifp) & 0xff;
505 newrbf->rbfa[i].gy = getint(1, ifp) & 0xff;
506 }
507 if (feof(ifp))
508 goto badEOF;
509 /* insert in global list */
510 if (insert_dsf(newrbf) != rbfh.ord) {
511 fprintf(stderr, "%s: error adding DSF\n", progname);
512 return(0);
513 }
514 }
515 /* read each migration matrix */
516 while ((from_ord = getint(4, ifp)) >= 0 &&
517 (to_ord = getint(4, ifp)) >= 0) {
518 RBFNODE *from_rbf = get_dsf(from_ord);
519 RBFNODE *to_rbf = get_dsf(to_ord);
520 MIGRATION *newmig;
521 int n;
522
523 if ((from_rbf == NULL) | (to_rbf == NULL)) {
524 fprintf(stderr,
525 "%s: bad DSF reference in migration edge\n",
526 progname);
527 return(0);
528 }
529 n = from_rbf->nrbf * to_rbf->nrbf;
530 newmig = (MIGRATION *)malloc(sizeof(MIGRATION) +
531 sizeof(float)*(n-1));
532 if (newmig == NULL)
533 goto memerr;
534 newmig->rbfv[0] = from_rbf;
535 newmig->rbfv[1] = to_rbf;
536 memset(newmig->mtx, 0, sizeof(float)*n);
537 for (i = 0; ; ) { /* read sparse data */
538 int zc = getint(1, ifp) & 0xff;
539 if ((i += zc) >= n)
540 break;
541 if (zc == 0xff)
542 continue;
543 newmig->mtx[i++] = getflt(ifp);
544 }
545 if (feof(ifp))
546 goto badEOF;
547 /* insert in edge lists */
548 newmig->enxt[0] = from_rbf->ejl;
549 from_rbf->ejl = newmig;
550 newmig->enxt[1] = to_rbf->ejl;
551 to_rbf->ejl = newmig;
552 /* push onto global list */
553 newmig->next = mig_list;
554 mig_list = newmig;
555 }
556 return(1); /* success! */
557 memerr:
558 fprintf(stderr, "%s: Out of memory in load_bsdf_rep()\n", progname);
559 exit(1);
560 badEOF:
561 fprintf(stderr, "%s: Unexpected EOF in load_bsdf_rep()\n", progname);
562 return(0);
563 }