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