ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/Development/ray/src/gen/mkillum4.c
Revision: 2.16
Committed: Thu May 28 18:38:52 2009 UTC (16 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.15: +2 -1 lines
Log Message:
Bug fix in source sampling

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: mkillum4.c,v 2.15 2009/03/04 00:12:25 greg Exp $";
3 #endif
4 /*
5 * Routines for handling BSDF data within mkillum
6 */
7
8 #include "mkillum.h"
9 #include "paths.h"
10 #include "ezxml.h"
11 #include <ctype.h>
12
13 #define MAXLATS 46 /* maximum number of latitudes */
14
15 /* BSDF angle specification */
16 typedef struct {
17 char name[64]; /* basis name */
18 int nangles; /* total number of directions */
19 struct {
20 float tmin; /* starting theta */
21 short nphis; /* number of phis (0 term) */
22 } lat[MAXLATS+1]; /* latitudes */
23 } ANGLE_BASIS;
24
25 #define MAXABASES 3 /* limit on defined bases */
26
27 static ANGLE_BASIS abase_list[MAXABASES] = {
28 {
29 "LBNL/Klems Full", 145,
30 { {-5., 1},
31 {5., 8},
32 {15., 16},
33 {25., 20},
34 {35., 24},
35 {45., 24},
36 {55., 24},
37 {65., 16},
38 {75., 12},
39 {90., 0} }
40 }, {
41 "LBNL/Klems Half", 73,
42 { {-6.5, 1},
43 {6.5, 8},
44 {19.5, 12},
45 {32.5, 16},
46 {46.5, 20},
47 {61.5, 12},
48 {76.5, 4},
49 {90., 0} }
50 }, {
51 "LBNL/Klems Quarter", 41,
52 { {-9., 1},
53 {9., 8},
54 {27., 12},
55 {46., 12},
56 {66., 8},
57 {90., 0} }
58 }
59 };
60
61 static int nabases = 3; /* current number of defined bases */
62
63
64 static int
65 ab_getvec( /* get vector for this angle basis index */
66 FVECT v,
67 int ndx,
68 void *p
69 )
70 {
71 ANGLE_BASIS *ab = (ANGLE_BASIS *)p;
72 int li;
73 double alt, azi, d;
74
75 if ((ndx < 0) | (ndx >= ab->nangles))
76 return(0);
77 for (li = 0; ndx >= ab->lat[li].nphis; li++)
78 ndx -= ab->lat[li].nphis;
79 alt = PI/180.*0.5*(ab->lat[li].tmin + ab->lat[li+1].tmin);
80 azi = 2.*PI*ndx/ab->lat[li].nphis;
81 v[2] = d = cos(alt);
82 d = sqrt(1. - d*d); /* sin(alt) */
83 v[0] = cos(azi)*d;
84 v[1] = sin(azi)*d;
85 return(1);
86 }
87
88
89 static int
90 ab_getndx( /* get index corresponding to the given vector */
91 FVECT v,
92 void *p
93 )
94 {
95 ANGLE_BASIS *ab = (ANGLE_BASIS *)p;
96 int li, ndx;
97 double alt, azi, d;
98
99 if ((v[2] < -1.0) | (v[2] > 1.0))
100 return(-1);
101 alt = 180.0/PI*acos(v[2]);
102 azi = 180.0/PI*atan2(v[1], v[0]);
103 if (azi < 0.0) azi += 360.0;
104 for (li = 1; ab->lat[li].tmin <= alt; li++)
105 if (!ab->lat[li].nphis)
106 return(-1);
107 --li;
108 ndx = (int)((1./360.)*azi*ab->lat[li].nphis + 0.5);
109 if (ndx >= ab->lat[li].nphis) ndx = 0;
110 while (li--)
111 ndx += ab->lat[li].nphis;
112 return(ndx);
113 }
114
115
116 static double
117 ab_getohm( /* get solid angle for this angle basis index */
118 int ndx,
119 void *p
120 )
121 {
122 ANGLE_BASIS *ab = (ANGLE_BASIS *)p;
123 int li;
124 double tdia, pdia;
125
126 if ((ndx < 0) | (ndx >= ab->nangles))
127 return(0);
128 for (li = 0; ndx >= ab->lat[li].nphis; li++)
129 ndx -= ab->lat[li].nphis;
130 if (ab->lat[li].nphis == 1) { /* special case */
131 if (ab->lat[li].tmin > FTINY)
132 error(USER, "unsupported BSDF coordinate system");
133 tdia = PI/180. * ab->lat[li+1].tmin;
134 return(PI*tdia*tdia);
135 }
136 tdia = PI/180.*(ab->lat[li+1].tmin - ab->lat[li].tmin);
137 tdia *= sin(PI/180.*(ab->lat[li].tmin + ab->lat[li+1].tmin));
138 pdia = 2.*PI/ab->lat[li].nphis;
139 return(tdia*pdia);
140 }
141
142
143 static int
144 ab_getvecR( /* get reverse vector for this angle basis index */
145 FVECT v,
146 int ndx,
147 void *p
148 )
149 {
150 if (!ab_getvec(v, ndx, p))
151 return(0);
152
153 v[0] = -v[0];
154 v[1] = -v[1];
155 v[2] = -v[2];
156
157 return(1);
158 }
159
160
161 static int
162 ab_getndxR( /* get index corresponding to the reverse vector */
163 FVECT v,
164 void *p
165 )
166 {
167 FVECT v2;
168
169 v2[0] = -v[0];
170 v2[1] = -v[1];
171 v2[2] = -v[2];
172
173 return ab_getndx(v2, p);
174 }
175
176
177 static void
178 load_bsdf_data( /* load BSDF distribution for this wavelength */
179 struct BSDF_data *dp,
180 ezxml_t wdb
181 )
182 {
183 char *cbasis = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis"));
184 char *rbasis = ezxml_txt(ezxml_child(wdb,"RowAngleBasis"));
185 char *sdata;
186 int i;
187
188 if ((cbasis == NULL) | (rbasis == NULL)) {
189 error(WARNING, "missing column/row basis for BSDF");
190 return;
191 }
192 /* XXX need to add routines for loading in foreign bases */
193 for (i = nabases; i--; )
194 if (!strcmp(cbasis, abase_list[i].name)) {
195 dp->ninc = abase_list[i].nangles;
196 dp->ib_priv = (void *)&abase_list[i];
197 dp->ib_vec = ab_getvecR;
198 dp->ib_ndx = ab_getndxR;
199 dp->ib_ohm = ab_getohm;
200 break;
201 }
202 if (i < 0) {
203 sprintf(errmsg, "unsupported ColumnAngleBasis '%s'", cbasis);
204 error(WARNING, errmsg);
205 return;
206 }
207 for (i = nabases; i--; )
208 if (!strcmp(rbasis, abase_list[i].name)) {
209 dp->nout = abase_list[i].nangles;
210 dp->ob_priv = (void *)&abase_list[i];
211 dp->ob_vec = ab_getvec;
212 dp->ob_ndx = ab_getndx;
213 dp->ob_ohm = ab_getohm;
214 break;
215 }
216 if (i < 0) {
217 sprintf(errmsg, "unsupported RowAngleBasis '%s'", cbasis);
218 error(WARNING, errmsg);
219 return;
220 }
221 /* read BSDF data */
222 sdata = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
223 if (sdata == NULL) {
224 error(WARNING, "missing BSDF ScatteringData");
225 return;
226 }
227 dp->bsdf = (float *)malloc(sizeof(float)*dp->ninc*dp->nout);
228 if (dp->bsdf == NULL)
229 error(SYSTEM, "out of memory in load_bsdf_data");
230 for (i = 0; i < dp->ninc*dp->nout; i++) {
231 char *sdnext = fskip(sdata);
232 if (sdnext == NULL) {
233 error(WARNING, "bad/missing BSDF ScatteringData");
234 free(dp->bsdf); dp->bsdf = NULL;
235 return;
236 }
237 while (*sdnext && isspace(*sdnext))
238 sdnext++;
239 if (*sdnext == ',') sdnext++;
240 dp->bsdf[i] = atof(sdata);
241 sdata = sdnext;
242 }
243 while (isspace(*sdata))
244 sdata++;
245 if (*sdata) {
246 sprintf(errmsg, "%d extra characters after BSDF ScatteringData",
247 strlen(sdata));
248 error(WARNING, errmsg);
249 }
250 /* XXX should check that BSDF*dOMEGA doesn't sum to more than PI? */
251 }
252
253
254 struct BSDF_data *
255 load_BSDF( /* load BSDF data from file */
256 char *fname
257 )
258 {
259 char *path;
260 ezxml_t fl, wtl, wld, wdb;
261 struct BSDF_data *dp;
262
263 path = getpath(fname, getrlibpath(), R_OK);
264 if (path == NULL) {
265 sprintf(errmsg, "cannot find BSDF file \"%s\"", fname);
266 error(WARNING, errmsg);
267 return(NULL);
268 }
269 fl = ezxml_parse_file(path);
270 if (fl == NULL) {
271 sprintf(errmsg, "cannot open BSDF \"%s\"", path);
272 error(WARNING, errmsg);
273 return(NULL);
274 }
275 if (ezxml_error(fl)[0]) {
276 sprintf(errmsg, "BSDF \"%s\" %s", path, ezxml_error(fl));
277 error(WARNING, errmsg);
278 ezxml_free(fl);
279 return(NULL);
280 }
281 if (strcmp(ezxml_name(fl), "WindowElement")) {
282 sprintf(errmsg,
283 "BSDF \"%s\": top level node not 'WindowElement'",
284 path);
285 error(WARNING, errmsg);
286 ezxml_free(fl);
287 return(NULL);
288 }
289 wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
290 dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
291 for (wld = ezxml_child(wtl, "WavelengthData");
292 wld != NULL; wld = wld->next) {
293 if (strcmp(ezxml_txt(ezxml_child(wld,"Wavelength")), "Visible"))
294 continue;
295 wdb = ezxml_child(wld, "WavelengthDataBlock");
296 if (wdb == NULL) continue;
297 if (strcmp(ezxml_txt(ezxml_child(wdb,"WavelengthDataDirection")),
298 "Transmission Front"))
299 continue;
300 load_bsdf_data(dp, wdb); /* load front BTDF */
301 break; /* ignore the rest */
302 }
303 ezxml_free(fl); /* done with XML file */
304 if (dp->bsdf == NULL) {
305 sprintf(errmsg, "bad/missing BTDF data in \"%s\"", path);
306 error(WARNING, errmsg);
307 free_BSDF(dp);
308 dp = NULL;
309 }
310 return(dp);
311 }
312
313
314 void
315 free_BSDF( /* free BSDF data structure */
316 struct BSDF_data *b
317 )
318 {
319 if (b == NULL)
320 return;
321 if (b->bsdf != NULL)
322 free(b->bsdf);
323 free(b);
324 }
325
326
327 int
328 r_BSDF_incvec( /* compute random input vector at given location */
329 FVECT v,
330 struct BSDF_data *b,
331 int i,
332 double rv,
333 MAT4 xm
334 )
335 {
336 FVECT pert;
337 double rad;
338 int j;
339
340 if (!getBSDF_incvec(v, b, i))
341 return(0);
342 rad = sqrt(getBSDF_incohm(b, i) / PI);
343 multisamp(pert, 3, rv);
344 for (j = 0; j < 3; j++)
345 v[j] += rad*(2.*pert[j] - 1.);
346 if (xm != NULL)
347 multv3(v, v, xm);
348 return(normalize(v) != 0.0);
349 }
350
351
352 int
353 r_BSDF_outvec( /* compute random output vector at given location */
354 FVECT v,
355 struct BSDF_data *b,
356 int o,
357 double rv,
358 MAT4 xm
359 )
360 {
361 FVECT pert;
362 double rad;
363 int j;
364
365 if (!getBSDF_outvec(v, b, o))
366 return(0);
367 rad = sqrt(getBSDF_outohm(b, o) / PI);
368 multisamp(pert, 3, rv);
369 for (j = 0; j < 3; j++)
370 v[j] += rad*(2.*pert[j] - 1.);
371 if (xm != NULL)
372 multv3(v, v, xm);
373 return(normalize(v) != 0.0);
374 }
375
376
377 #define FEQ(a,b) ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
378
379 static int
380 addrot( /* compute rotation (x,y,z) => (xp,yp,zp) */
381 char *xfarg[],
382 FVECT xp,
383 FVECT yp,
384 FVECT zp
385 )
386 {
387 static char bufs[3][16];
388 int bn = 0;
389 char **xfp = xfarg;
390 double theta;
391
392 if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) {
393 /* Special case for X' along Z-axis */
394 theta = -atan2(yp[0], yp[1]);
395 *xfp++ = "-ry";
396 *xfp++ = xp[2] < 0.0 ? "90" : "-90";
397 *xfp++ = "-rz";
398 sprintf(bufs[bn], "%f", theta*(180./PI));
399 *xfp++ = bufs[bn++];
400 return(xfp - xfarg);
401 }
402 theta = atan2(yp[2], zp[2]);
403 if (!FEQ(theta,0.0)) {
404 *xfp++ = "-rx";
405 sprintf(bufs[bn], "%f", theta*(180./PI));
406 *xfp++ = bufs[bn++];
407 }
408 theta = asin(-xp[2]);
409 if (!FEQ(theta,0.0)) {
410 *xfp++ = "-ry";
411 sprintf(bufs[bn], " %f", theta*(180./PI));
412 *xfp++ = bufs[bn++];
413 }
414 theta = atan2(xp[1], xp[0]);
415 if (!FEQ(theta,0.0)) {
416 *xfp++ = "-rz";
417 sprintf(bufs[bn], "%f", theta*(180./PI));
418 *xfp++ = bufs[bn++];
419 }
420 *xfp = NULL;
421 return(xfp - xfarg);
422 }
423
424
425 int
426 getBSDF_xfm( /* compute BSDF orient. -> world orient. transform */
427 MAT4 xm,
428 FVECT nrm,
429 UpDir ud
430 )
431 {
432 char *xfargs[7];
433 XF myxf;
434 FVECT updir, xdest, ydest;
435
436 updir[0] = updir[1] = updir[2] = 0.;
437 switch (ud) {
438 case UDzneg:
439 updir[2] = -1.;
440 break;
441 case UDyneg:
442 updir[1] = -1.;
443 break;
444 case UDxneg:
445 updir[0] = -1.;
446 break;
447 case UDxpos:
448 updir[0] = 1.;
449 break;
450 case UDypos:
451 updir[1] = 1.;
452 break;
453 case UDzpos:
454 updir[2] = 1.;
455 break;
456 case UDunknown:
457 return(0);
458 }
459 fcross(xdest, updir, nrm);
460 if (normalize(xdest) == 0.0)
461 return(0);
462 fcross(ydest, nrm, xdest);
463 xf(&myxf, addrot(xfargs, xdest, ydest, nrm), xfargs);
464 copymat4(xm, myxf.xfm);
465 return(1);
466 }
467
468
469 void
470 redistribute( /* pass distarr ray sums through BSDF */
471 struct BSDF_data *b,
472 int nalt,
473 int nazi,
474 FVECT u,
475 FVECT v,
476 FVECT w,
477 MAT4 xm
478 )
479 {
480 int nout = 0;
481 MAT4 mymat, inmat;
482 COLORV *idist;
483 COLORV *cp;
484 FVECT dv;
485 double wt;
486 int i, j, k, o;
487 COLOR col, cinc;
488 /* copy incoming distribution */
489 if (b->ninc > distsiz)
490 error(INTERNAL, "error 1 in redistribute");
491 idist = (COLORV *)malloc(sizeof(COLOR)*b->ninc);
492 if (idist == NULL)
493 error(SYSTEM, "out of memory in redistribute");
494 memcpy(idist, distarr, sizeof(COLOR)*b->ninc);
495 /* compose direction transform */
496 for (i = 3; i--; ) {
497 mymat[i][0] = u[i];
498 mymat[i][1] = v[i];
499 mymat[i][2] = w[i];
500 mymat[i][3] = 0.;
501 }
502 mymat[3][0] = mymat[3][1] = mymat[3][2] = 0.;
503 mymat[3][3] = 1.;
504 if (xm != NULL)
505 multmat4(mymat, xm, mymat);
506 for (i = 3; i--; ) { /* make sure it's normalized */
507 wt = 1./sqrt( mymat[0][i]*mymat[0][i] +
508 mymat[1][i]*mymat[1][i] +
509 mymat[2][i]*mymat[2][i] );
510 for (j = 3; j--; )
511 mymat[j][i] *= wt;
512 }
513 if (!invmat4(inmat, mymat)) /* need inverse as well */
514 error(INTERNAL, "cannot invert BSDF transform");
515 newdist(nalt*nazi); /* resample distribution */
516 for (i = b->ninc; i--; ) {
517 int direct_out = -1;
518 COLOR cdir;
519 getBSDF_incvec(dv, b, i); /* compute incident irrad. */
520 multv3(dv, dv, mymat);
521 if (dv[2] < 0.0) {
522 dv[0] = -dv[0]; dv[1] = -dv[1]; dv[2] = -dv[2];
523 direct_out += (direct_discount != NULL);
524 }
525 wt = getBSDF_incohm(b, i);
526 wt *= dv[2]; /* solid_angle*cosine(theta) */
527 cp = &idist[3*i];
528 copycolor(cinc, cp);
529 scalecolor(cinc, wt);
530 if (!direct_out) { /* discount direct contr. */
531 cp = &direct_discount[3*i];
532 copycolor(cdir, cp);
533 scalecolor(cdir, -wt);
534 direct_out = flatindex(dv, nalt, nazi);
535 }
536 for (k = nalt; k--; ) /* loop over distribution */
537 for (j = nazi; j--; ) {
538 flatdir(dv, (k + .5)/nalt, (double)j/nazi);
539 multv3(dv, dv, inmat);
540 /* evaluate BSDF @ outgoing */
541 o = getBSDF_outndx(b, dv);
542 if (o < 0) {
543 nout++;
544 continue;
545 }
546 wt = BSDF_value(b, i, o);
547 copycolor(col, cinc);
548 o = k*nazi + j;
549 if (o == direct_out)
550 addcolor(col, cdir); /* minus direct */
551 scalecolor(col, wt);
552 cp = &distarr[3*o];
553 addcolor(cp, col); /* sum into distribution */
554 }
555 }
556 free(idist); /* free temp space */
557 if (nout) {
558 sprintf(errmsg, "missing %.1f%% of BSDF directions",
559 100.*nout/(b->ninc*nalt*nazi));
560 error(WARNING, errmsg);
561 }
562 }