ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2rad.c
Revision: 2.20
Committed: Mon Apr 10 06:09:14 2017 UTC (7 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.19: +26 -28 lines
Log Message:
Swapped Front & Back hemisphere placement and additional tweaks

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdf2rad.c,v 2.19 2017/04/10 01:31:37 greg Exp $";
3 #endif
4 /*
5 * Plot 3-D BSDF output based on scattering interpolant or XML representation
6 */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include "paths.h"
12 #include "rtmath.h"
13 #include "resolu.h"
14 #include "bsdfrep.h"
15
16 #define NINCIDENT 37 /* number of samples/hemisphere */
17
18 #define GRIDSTEP 2 /* our grid step size */
19 #define SAMPRES (GRIDRES/GRIDSTEP)
20
21 int front_comp = 0; /* front component flags (SDsamp*) */
22 int back_comp = 0; /* back component flags */
23 double overall_min = 1./PI; /* overall minimum BSDF value */
24 double min_log10; /* smallest log10 value for plotting */
25 double overall_max = .0; /* overall maximum BSDF value */
26
27 char ourTempDir[TEMPLEN] = ""; /* our temporary directory */
28
29 const FVECT Yaxis = {0., 1., 0.};
30
31 const char frpref[] = "frefl";
32 const char ftpref[] = "ftrans";
33 const char brpref[] = "brefl";
34 const char btpref[] = "btrans";
35 const char dsuffix[] = ".txt";
36
37 const char sph_mat[] = "BSDFmat";
38 const double sph_rad = 10.;
39 const double sph_xoffset = 15.;
40
41 #define bsdf_rad (sph_rad*.25)
42 #define arrow_rad (bsdf_rad*.015)
43
44 #define FEQ(a,b) ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
45
46 #define set_minlog() (min_log10 = log10(overall_min + 1e-5) - .1)
47
48 char *progname;
49
50 /* Get Fibonacci sphere vector (0 to NINCIDENT-1) */
51 static RREAL *
52 get_ivector(FVECT iv, int i)
53 {
54 const double phistep = PI*(3. - 2.236067978);
55 double r;
56
57 iv[2] = 1. - (i+.5)*(1./NINCIDENT);
58 r = sqrt(1. - iv[2]*iv[2]);
59 iv[0] = r * cos((i+1.)*phistep);
60 iv[1] = r * sin((i+1.)*phistep);
61
62 return(iv);
63 }
64
65 /* Convert incident vector into sphere position */
66 static RREAL *
67 cvt_sposition(FVECT sp, const FVECT iv, int inc_side)
68 {
69 sp[0] = -iv[0]*sph_rad + inc_side*sph_xoffset;
70 sp[1] = -iv[1]*sph_rad;
71 sp[2] = iv[2]*sph_rad;
72
73 return(sp);
74 }
75
76 /* Get temporary file name */
77 static char *
78 tfile_name(const char *prefix, const char *suffix, int i)
79 {
80 static char buf[128];
81
82 if (!ourTempDir[0]) { /* create temporary directory */
83 mktemp(strcpy(ourTempDir,TEMPLATE));
84 if (mkdir(ourTempDir, 0777) < 0) {
85 perror("mkdir");
86 exit(1);
87 }
88 }
89 if (!prefix) prefix = "T";
90 if (!suffix) suffix = "";
91 sprintf(buf, "%s/%s%03d%s", ourTempDir, prefix, i, suffix);
92 return(buf);
93 }
94
95 /* Remove temporary directory & contents */
96 static void
97 cleanup_tmp(void)
98 {
99 char buf[128];
100
101 if (!ourTempDir[0])
102 return;
103 #if defined(_WIN32) || defined(_WIN64)
104 sprintf(buf, "RMDIR %s /S /Q", ourTempDir);
105 #else
106 sprintf(buf, "rm -rf %s", ourTempDir);
107 #endif
108 system(buf);
109 }
110
111 /* Run the specified command, returning 1 if OK */
112 static int
113 run_cmd(const char *cmd)
114 {
115 fflush(stdout);
116 if (system(cmd)) {
117 fprintf(stderr, "%s: error running: %s\n", progname, cmd);
118 return(0);
119 }
120 return(1);
121 }
122
123 /* Plot surface points for the given BSDF incident angle */
124 static int
125 plotBSDF(const char *fname, const FVECT ivec, int dfl, const SDData *sd)
126 {
127 FILE *fp = fopen(fname, "w");
128 int i, j;
129
130 if (fp == NULL) {
131 fprintf(stderr, "%s: cannot open '%s' for writing\n",
132 progname, fname);
133 return(0);
134 }
135 if (ivec[2] > 0) {
136 input_orient = 1;
137 output_orient = dfl&SDsampR ? 1 : -1;
138 } else {
139 input_orient = -1;
140 output_orient = dfl&SDsampR ? -1 : 1;
141 }
142 for (i = SAMPRES; i--; )
143 for (j = 0; j < SAMPRES; j++) {
144 FVECT ovec;
145 SDValue sval;
146 double bsdf;
147 ovec_from_pos(ovec, i*GRIDSTEP, j*GRIDSTEP);
148 if (SDreportError(SDevalBSDF(&sval, ovec,
149 ivec, sd), stderr))
150 return(0);
151 if (sval.cieY > overall_max)
152 overall_max = sval.cieY;
153 bsdf = (sval.cieY < overall_min) ? overall_min : sval.cieY;
154 bsdf = log10(bsdf) - min_log10;
155 fprintf(fp, "%.5f %.5f %.5f\n",
156 ovec[0]*bsdf, ovec[1]*bsdf, ovec[2]*bsdf);
157 }
158 if (fclose(fp) == EOF) {
159 fprintf(stderr, "%s: error writing data to '%s'\n",
160 progname, fname);
161 return(0);
162 }
163 return(1);
164 }
165
166 /* Build BSDF values from loaded XML file */
167 static int
168 build_wBSDF(const SDData *sd)
169 {
170 FVECT ivec;
171 int i;
172
173 if (front_comp & SDsampR)
174 for (i = 0; i < NINCIDENT; i++) {
175 get_ivector(ivec, i);
176 if (!plotBSDF(tfile_name(frpref, dsuffix, i),
177 ivec, SDsampR, sd))
178 return(0);
179 }
180 if (front_comp & SDsampT)
181 for (i = 0; i < NINCIDENT; i++) {
182 get_ivector(ivec, i);
183 if (!plotBSDF(tfile_name(ftpref, dsuffix, i),
184 ivec, SDsampT, sd))
185 return(0);
186 }
187 if (back_comp & SDsampR)
188 for (i = 0; i < NINCIDENT; i++) {
189 get_ivector(ivec, i);
190 ivec[0] = -ivec[0]; ivec[2] = -ivec[2];
191 if (!plotBSDF(tfile_name(brpref, dsuffix, i),
192 ivec, SDsampR, sd))
193 return(0);
194 }
195 if (back_comp & SDsampT)
196 for (i = 0; i < NINCIDENT; i++) {
197 get_ivector(ivec, i);
198 ivec[0] = -ivec[0]; ivec[2] = -ivec[2];
199 if (!plotBSDF(tfile_name(btpref, dsuffix, i),
200 ivec, SDsampT, sd))
201 return(0);
202 }
203 return(1);
204 }
205
206 /* Plot surface points using radial basis function */
207 static int
208 plotRBF(const char *fname, const RBFNODE *rbf)
209 {
210 FILE *fp = fopen(fname, "w");
211 int i, j;
212
213 if (fp == NULL) {
214 fprintf(stderr, "%s: cannot open '%s' for writing\n",
215 progname, fname);
216 return(0);
217 }
218 for (i = SAMPRES; i--; )
219 for (j = 0; j < SAMPRES; j++) {
220 FVECT ovec;
221 double bsdf;
222 ovec_from_pos(ovec, i*GRIDSTEP, j*GRIDSTEP);
223 bsdf = eval_rbfrep(rbf, ovec);
224 if (bsdf > overall_max)
225 overall_max = bsdf;
226 else if (bsdf < overall_min)
227 bsdf = overall_min;
228 bsdf = log10(bsdf) - min_log10;
229 fprintf(fp, "%.5f %.5f %.5f\n",
230 ovec[0]*bsdf, ovec[1]*bsdf, ovec[2]*bsdf);
231 }
232 if (fclose(fp) == EOF) {
233 fprintf(stderr, "%s: error writing data to '%s'\n",
234 progname, fname);
235 return(0);
236 }
237 return(1);
238 }
239
240 /* Build BSDF values from scattering interpolant representation */
241 static int
242 build_wRBF(void)
243 {
244 const char *pref;
245 int i;
246
247 if (input_orient > 0) {
248 if (output_orient > 0)
249 pref = frpref;
250 else
251 pref = ftpref;
252 } else if (output_orient < 0)
253 pref = brpref;
254 else
255 pref = btpref;
256
257 for (i = 0; i < NINCIDENT; i++) {
258 FVECT ivec;
259 RBFNODE *rbf;
260 get_ivector(ivec, i);
261 if (input_orient < 0) {
262 ivec[0] = -ivec[0]; ivec[2] = -ivec[2];
263 }
264 rbf = advect_rbf(ivec, 15000);
265 if (!plotRBF(tfile_name(pref, dsuffix, i), rbf))
266 return(0);
267 if (rbf) free(rbf);
268 }
269 return(1); /* next call frees */
270 }
271
272 /* Put out mirror arrow for the given incident vector */
273 static void
274 put_mirror_arrow(const FVECT ivec, int inc_side)
275 {
276 const double arrow_len = 1.2*bsdf_rad;
277 const double tip_len = 0.2*bsdf_rad;
278 FVECT origin, refl;
279 int i;
280
281 cvt_sposition(origin, ivec, inc_side);
282
283 refl[0] = -2.*ivec[2]*ivec[0];
284 refl[1] = -2.*ivec[2]*ivec[1];
285 refl[2] = 2.*ivec[2]*ivec[2] - 1.;
286
287 printf("\n# Mirror arrow\n");
288 printf("\narrow_mat cylinder inc_dir\n0\n0\n7");
289 printf("\n\t%f %f %f\n\t%f %f %f\n\t%f\n",
290 origin[0], origin[1], origin[2]+arrow_len,
291 origin[0], origin[1], origin[2],
292 arrow_rad);
293 printf("\narrow_mat cylinder mir_dir\n0\n0\n7");
294 printf("\n\t%f %f %f\n\t%f %f %f\n\t%f\n",
295 origin[0], origin[1], origin[2],
296 origin[0] + arrow_len*refl[0],
297 origin[1] + arrow_len*refl[1],
298 origin[2] + arrow_len*refl[2],
299 arrow_rad);
300 printf("\narrow_mat cone mir_tip\n0\n0\n8");
301 printf("\n\t%f %f %f\n\t%f %f %f\n\t%f 0\n",
302 origin[0] + (arrow_len-.5*tip_len)*refl[0],
303 origin[1] + (arrow_len-.5*tip_len)*refl[1],
304 origin[2] + (arrow_len-.5*tip_len)*refl[2],
305 origin[0] + (arrow_len+.5*tip_len)*refl[0],
306 origin[1] + (arrow_len+.5*tip_len)*refl[1],
307 origin[2] + (arrow_len+.5*tip_len)*refl[2],
308 2.*arrow_rad);
309 }
310
311 /* Put out transmitted direction arrow for the given incident vector */
312 static void
313 put_trans_arrow(const FVECT ivec, int inc_side)
314 {
315 const double arrow_len = 1.2*bsdf_rad;
316 const double tip_len = 0.2*bsdf_rad;
317 FVECT origin;
318 int i;
319
320 cvt_sposition(origin, ivec, inc_side);
321
322 printf("\n# Transmission arrow\n");
323 printf("\narrow_mat cylinder trans_dir\n0\n0\n7");
324 printf("\n\t%f %f %f\n\t%f %f %f\n\t%f\n",
325 origin[0], origin[1], origin[2],
326 origin[0], origin[1], origin[2]-arrow_len,
327 arrow_rad);
328 printf("\narrow_mat cone trans_tip\n0\n0\n8");
329 printf("\n\t%f %f %f\n\t%f %f %f\n\t%f 0\n",
330 origin[0], origin[1], origin[2]-arrow_len+.5*tip_len,
331 origin[0], origin[1], origin[2]-arrow_len-.5*tip_len,
332 2.*arrow_rad);
333 }
334
335 /* Compute rotation (x,y,z) => (xp,yp,zp) */
336 static int
337 addrot(char *xf, const FVECT xp, const FVECT yp, const FVECT zp)
338 {
339 int n = 0;
340 double theta;
341
342 if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) {
343 /* Special case for X' along Z-axis */
344 theta = -atan2(yp[0], yp[1]);
345 sprintf(xf, " -ry %f -rz %f",
346 xp[2] < 0.0 ? 90.0 : -90.0,
347 theta*(180./PI));
348 return(4);
349 }
350 theta = atan2(yp[2], zp[2]);
351 if (!FEQ(theta,0.0)) {
352 sprintf(xf, " -rx %f", theta*(180./PI));
353 while (*xf) ++xf;
354 n += 2;
355 }
356 theta = Asin(-xp[2]);
357 if (!FEQ(theta,0.0)) {
358 sprintf(xf, " -ry %f", theta*(180./PI));
359 while (*xf) ++xf;
360 n += 2;
361 }
362 theta = atan2(xp[1], xp[0]);
363 if (!FEQ(theta,0.0)) {
364 sprintf(xf, " -rz %f", theta*(180./PI));
365 /* while (*xf) ++xf; */
366 n += 2;
367 }
368 return(n);
369 }
370
371 /* Put out BSDF surfaces */
372 static int
373 put_BSDFs(void)
374 {
375 const double scalef = bsdf_rad/(log10(overall_max) - min_log10);
376 FVECT ivec, sorg;
377 RREAL vMtx[3][3];
378 char *fname;
379 char cmdbuf[256];
380 char xfargs[128];
381 int nxfa;
382 int i;
383
384 printf("\n# Gensurf output corresponding to %d incident directions\n",
385 NINCIDENT);
386
387 printf("\nvoid glow arrow_glow\n0\n0\n4 1 0 1 0\n");
388 printf("\nvoid mixfunc arrow_mat\n4 arrow_glow void 0.5 .\n0\n0\n");
389
390 if (front_comp & SDsampR) /* front reflection */
391 for (i = 0; i < NINCIDENT; i++) {
392 get_ivector(ivec, i);
393 put_mirror_arrow(ivec, 1);
394 cvt_sposition(sorg, ivec, 1);
395 ivec[0] = -ivec[0]; ivec[1] = -ivec[1]; /* normal */
396 sprintf(xfargs, "-s %f -t %f %f %f", bsdf_rad,
397 sorg[0], sorg[1], sorg[2]);
398 nxfa = 6;
399 printf("\nvoid colorfunc scale_pat\n");
400 printf("%d bsdf_red bsdf_grn bsdf_blu bsdf2rad.cal\n\t%s\n0\n0\n",
401 4+nxfa, xfargs);
402 printf("\nscale_pat glow scale_mat\n0\n0\n4 1 1 1 0\n");
403 SDcompXform(vMtx, ivec, Yaxis);
404 nxfa = addrot(xfargs, vMtx[0], vMtx[1], vMtx[2]);
405 sprintf(xfargs+strlen(xfargs), " -s %f -t %f %f %f",
406 scalef, sorg[0], sorg[1], sorg[2]);
407 nxfa += 6;
408 fname = tfile_name(frpref, dsuffix, i);
409 sprintf(cmdbuf, "gensurf scale_mat %s%d %s %s %s %d %d | xform %s",
410 frpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
411 xfargs);
412 if (!run_cmd(cmdbuf))
413 return(0);
414 }
415 if (front_comp & SDsampT) /* front transmission */
416 for (i = 0; i < NINCIDENT; i++) {
417 get_ivector(ivec, i);
418 put_trans_arrow(ivec, 1);
419 cvt_sposition(sorg, ivec, 1);
420 ivec[0] = -ivec[0]; ivec[1] = -ivec[1]; /* normal */
421 sprintf(xfargs, "-s %f -t %f %f %f", bsdf_rad,
422 sorg[0], sorg[1], sorg[2]);
423 nxfa = 6;
424 printf("\nvoid colorfunc scale_pat\n");
425 printf("%d bsdf_red bsdf_grn bsdf_blu bsdf2rad.cal\n\t%s\n0\n0\n",
426 4+nxfa, xfargs);
427 printf("\nscale_pat glow scale_mat\n0\n0\n4 1 1 1 0\n");
428 SDcompXform(vMtx, ivec, Yaxis);
429 nxfa = addrot(xfargs, vMtx[0], vMtx[1], vMtx[2]);
430 sprintf(xfargs+strlen(xfargs), " -s %f -t %f %f %f",
431 scalef, sorg[0], sorg[1], sorg[2]);
432 nxfa += 6;
433 fname = tfile_name(ftpref, dsuffix, i);
434 sprintf(cmdbuf, "gensurf scale_mat %s%d %s %s %s %d %d | xform -I %s",
435 ftpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
436 xfargs);
437 if (!run_cmd(cmdbuf))
438 return(0);
439 }
440 if (back_comp & SDsampR) /* rear reflection */
441 for (i = 0; i < NINCIDENT; i++) {
442 get_ivector(ivec, i);
443 put_mirror_arrow(ivec, -1);
444 cvt_sposition(sorg, ivec, -1);
445 ivec[0] = -ivec[0]; ivec[1] = -ivec[1]; /* normal */
446 sprintf(xfargs, "-s %f -t %f %f %f", bsdf_rad,
447 sorg[0], sorg[1], sorg[2]);
448 nxfa = 6;
449 printf("\nvoid colorfunc scale_pat\n");
450 printf("%d bsdf_red bsdf_grn bsdf_blu bsdf2rad.cal\n\t%s\n0\n0\n",
451 4+nxfa, xfargs);
452 printf("\nscale_pat glow scale_mat\n0\n0\n4 1 1 1 0\n");
453 SDcompXform(vMtx, ivec, Yaxis);
454 nxfa = addrot(xfargs, vMtx[0], vMtx[1], vMtx[2]);
455 sprintf(xfargs+strlen(xfargs), " -s %f -t %f %f %f",
456 scalef, sorg[0], sorg[1], sorg[2]);
457 nxfa += 6;
458 fname = tfile_name(brpref, dsuffix, i);
459 sprintf(cmdbuf, "gensurf scale_mat %s%d %s %s %s %d %d | xform -I -ry 180 %s",
460 brpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
461 xfargs);
462 if (!run_cmd(cmdbuf))
463 return(0);
464 }
465 if (back_comp & SDsampT) /* rear transmission */
466 for (i = 0; i < NINCIDENT; i++) {
467 get_ivector(ivec, i);
468 put_trans_arrow(ivec, -1);
469 cvt_sposition(sorg, ivec, -1);
470 ivec[0] = -ivec[0]; ivec[1] = -ivec[1]; /* normal */
471 sprintf(xfargs, "-s %f -t %f %f %f", bsdf_rad,
472 sorg[0], sorg[1], sorg[2]);
473 nxfa = 6;
474 printf("\nvoid colorfunc scale_pat\n");
475 printf("%d bsdf_red bsdf_grn bsdf_blu bsdf2rad.cal\n\t%s\n0\n0\n",
476 4+nxfa, xfargs);
477 printf("\nscale_pat glow scale_mat\n0\n0\n4 1 1 1 0\n");
478 SDcompXform(vMtx, ivec, Yaxis);
479 nxfa = addrot(xfargs, vMtx[0], vMtx[1], vMtx[2]);
480 sprintf(xfargs+strlen(xfargs), " -s %f -t %f %f %f",
481 scalef, sorg[0], sorg[1], sorg[2]);
482 nxfa += 6;
483 fname = tfile_name(btpref, dsuffix, i);
484 sprintf(cmdbuf, "gensurf scale_mat %s%d %s %s %s %d %d | xform -ry 180 %s",
485 btpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
486 xfargs);
487 if (!run_cmd(cmdbuf))
488 return(0);
489 }
490 return(1);
491 }
492
493 /* Put our hemisphere material */
494 static void
495 put_matBSDF(const char *XMLfile)
496 {
497 const char *curdir = "./";
498
499 if (!XMLfile) { /* simple material */
500 printf("\n# Simplified material because we have no XML input\n");
501 printf("\nvoid brightfunc latlong\n2 latlong bsdf2rad.cal\n0\n0\n");
502 if ((front_comp|back_comp) & SDsampT)
503 printf("\nlatlong trans %s\n0\n0\n7 .75 .75 .75 0 .04 .5 .8\n",
504 sph_mat);
505 else
506 printf("\nlatlong plastic %s\n0\n0\n5 .5 .5 .5 0 0\n",
507 sph_mat);
508 return;
509 }
510 switch (XMLfile[0]) { /* avoid RAYPATH search */
511 case '.':
512 CASEDIRSEP:
513 curdir = "";
514 break;
515 case '\0':
516 fprintf(stderr, "%s: empty file name in put_matBSDF\n", progname);
517 exit(1);
518 break;
519 }
520 printf("\n# Actual BSDF material for rendering the hemispheres\n");
521 printf("\nvoid BSDF BSDFmat\n6 0 \"%s%s\" 0 1 0 .\n0\n0\n",
522 curdir, XMLfile);
523 printf("\nvoid plastic black\n0\n0\n5 0 0 0 0 0\n");
524 printf("\nvoid mixfunc %s\n4 BSDFmat black latlong bsdf2rad.cal\n0\n0\n",
525 sph_mat);
526 }
527
528 /* Put out overhead parallel light source */
529 static void
530 put_source(void)
531 {
532 printf("\n# Overhead parallel light source\n");
533 printf("\nvoid light bright\n0\n0\n3 2500 2500 2500\n");
534 printf("\nbright source light\n0\n0\n4 0 0 1 2\n");
535 printf("\n# Material used for labels\n");
536 printf("\nvoid trans vellum\n0\n0\n7 1 1 1 0 0 .5 0\n");
537 }
538
539 /* Put out hemisphere(s) */
540 static void
541 put_hemispheres(void)
542 {
543 printf("\n# Hemisphere(s) for showing BSDF appearance (if XML file)\n");
544 printf("\nvoid antimatter anti_sph\n2 void %s\n0\n0\n", sph_mat);
545 if (front_comp) {
546 printf("\n%s sphere Front\n0\n0\n4 %f 0 0 %f\n",
547 sph_mat, sph_xoffset, sph_rad);
548 printf("\n!genbox anti_sph sph_eraser %f %f %f | xform -t %f %f %f\n",
549 2.02*sph_rad, 2.02*sph_rad, 1.02*sph_rad,
550 -1.01*sph_rad + sph_xoffset, -1.01*sph_rad, -1.01*sph_rad);
551 printf("\nvoid brighttext front_text\n3 helvet.fnt . FRONT\n0\n");
552 printf("12\n\t%f %f 0\n\t%f 0 0\n\t0 %f 0\n\t.01 1 -.1\n",
553 -.22*sph_rad + sph_xoffset, -1.4*sph_rad,
554 .35/5.*sph_rad, -1.6*.35/5.*sph_rad);
555 printf("\nfront_text alias front_label_mat vellum\n");
556 printf("\nfront_label_mat polygon front_label\n0\n0\n12");
557 printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
558 -.25*sph_rad + sph_xoffset, -1.3*sph_rad,
559 -.25*sph_rad + sph_xoffset, (-1.4-1.6*.35/5.-.1)*sph_rad,
560 .25*sph_rad + sph_xoffset, (-1.4-1.6*.35/5.-.1)*sph_rad,
561 .25*sph_rad + sph_xoffset, -1.3*sph_rad );
562 }
563 if (back_comp) {
564 printf("\n%s bubble Back\n0\n0\n4 %f 0 0 %f\n",
565 sph_mat, -sph_xoffset, sph_rad);
566 printf("\n!genbox anti_sph sph_eraser %f %f %f | xform -t %f %f %f\n",
567 2.02*sph_rad, 2.02*sph_rad, 1.02*sph_rad,
568 -1.01*sph_rad - sph_xoffset, -1.01*sph_rad, -1.01*sph_rad);
569 printf("\nvoid brighttext back_text\n3 helvet.fnt . BACK\n0\n");
570 printf("12\n\t%f %f 0\n\t%f 0 0\n\t0 %f 0\n\t.01 1 -.1\n",
571 -.22*sph_rad - sph_xoffset, -1.4*sph_rad,
572 .35/4.*sph_rad, -1.6*.35/4.*sph_rad);
573 printf("\nback_text alias back_label_mat vellum\n");
574 printf("\nback_label_mat polygon back_label\n0\n0\n12");
575 printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
576 -.25*sph_rad - sph_xoffset, -1.3*sph_rad,
577 -.25*sph_rad - sph_xoffset, (-1.4-1.6*.35/4.-.1)*sph_rad,
578 .25*sph_rad - sph_xoffset, (-1.4-1.6*.35/4.-.1)*sph_rad,
579 .25*sph_rad - sph_xoffset, -1.3*sph_rad );
580 }
581 }
582
583 /* Put out falsecolor scale and name label */
584 static void
585 put_scale(void)
586 {
587 const double max_log10 = log10(overall_max);
588 const double leg_width = 2.*.75*(fabs(sph_xoffset) - sph_rad);
589 const double leg_height = 2.*sph_rad;
590 const int text_lines = 6;
591 const int text_digits = 8;
592 char fmt[16];
593 int i;
594
595 printf("\n# BSDF legend with falsecolor scale\n");
596 printf("\nvoid colorfunc lscale\n10 sca_red(Py) sca_grn(Py) sca_blu(Py)");
597 printf("\n\tbsdf2rad.cal -s %f -t 0 %f 0\n0\n0\n", leg_height, -.5*leg_height);
598 sprintf(fmt, "%%.%df", text_digits-3);
599 for (i = 0; i < text_lines; i++) {
600 char vbuf[16];
601 sprintf(vbuf, fmt, pow(10., (i+.5)/text_lines*(max_log10-min_log10)+min_log10));
602 printf("\nlscale brighttext lscale\n");
603 printf("3 helvet.fnt . %s\n0\n12\n", vbuf);
604 printf("\t%f %f 0\n", -.45*leg_width, ((i+.9)/text_lines-.5)*leg_height);
605 printf("\t%f 0 0\n", .8*leg_width/strlen(vbuf));
606 printf("\t0 %f 0\n", -.9/text_lines*leg_height);
607 printf("\t.01 1 -.1\n");
608 }
609 printf("\nlscale alias legend_mat vellum\n");
610 printf("\nlegend_mat polygon legend\n0\n0\n12");
611 printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
612 -.5*leg_width, .5*leg_height,
613 -.5*leg_width, -.5*leg_height,
614 .5*leg_width, -.5*leg_height,
615 .5*leg_width, .5*leg_height);
616 printf("\nvoid brighttext BSDFtitle\n3 helvet.fnt . BSDF\n0\n12\n");
617 printf("\t%f %f 0\n", -.25*leg_width, .7*leg_height);
618 printf("\t%f 0 0\n", .4/4.*leg_width);
619 printf("\t0 %f 0\n", -.1*leg_height);
620 printf("\t.01 1 -.1\n");
621 printf("\nBSDFtitle alias title_mat vellum\n");
622 printf("\ntitle_mat polygon title\n0\n0\n12");
623 printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
624 -.3*leg_width, .75*leg_height,
625 -.3*leg_width, .55*leg_height,
626 .3*leg_width, .55*leg_height,
627 .3*leg_width, .75*leg_height);
628 if (!bsdf_name[0])
629 return;
630 printf("\nvoid brighttext BSDFname\n3 helvet.fnt . \"%s\"\n0\n12\n", bsdf_name);
631 printf("\t%f %f 0\n", -.95*leg_width, -.6*leg_height);
632 printf("\t%f 0 0\n", 1.8/strlen(bsdf_name)*leg_width);
633 printf("\t0 %f 0\n", -.1*leg_height);
634 printf("\t.01 1 -.1\n");
635 printf("\nBSDFname alias name_mat vellum\n");
636 printf("\nname_mat polygon name\n0\n0\n12");
637 printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
638 -leg_width, -.55*leg_height,
639 -leg_width, -.75*leg_height,
640 leg_width, -.75*leg_height,
641 leg_width, -.55*leg_height);
642 }
643
644 /* Convert MGF to Radiance in output */
645 static void
646 convert_mgf(const char *mgfdata)
647 {
648 int len = strlen(mgfdata);
649 char mgfn[128];
650 char radfn[128];
651 char cmdbuf[256];
652 float xmin, xmax, ymin, ymax, zmin, zmax;
653 double max_dim;
654 int fd;
655 FILE *fp;
656
657 if (!len) return;
658 strcpy(mgfn, tfile_name("geom", ".mgf", 0));
659 fd = open(mgfn, O_WRONLY|O_CREAT, 0666);
660 if (fd < 0 || write(fd, mgfdata, len) != len) {
661 fprintf(stderr, "%s: cannot write file '%s'\n",
662 progname, mgfn);
663 return;
664 }
665 close(fd);
666 strcpy(radfn, tfile_name("geom", ".rad", 0));
667 sprintf(cmdbuf, "mgf2rad %s > %s", mgfn, radfn);
668 if (!run_cmd(cmdbuf))
669 return;
670 sprintf(cmdbuf, "getbbox -w -h %s", radfn);
671 if ((fp = popen(cmdbuf, "r")) == NULL ||
672 fscanf(fp, "%f %f %f %f %f %f",
673 &xmin, &xmax, &ymin, &ymax, &zmin, &zmax) != 6
674 || pclose(fp) < 0) {
675 fprintf(stderr, "%s: error reading from command: %s\n",
676 progname, cmdbuf);
677 return;
678 }
679 max_dim = ymax - ymin;
680 if (xmax - xmin > max_dim)
681 max_dim = xmax - xmin;
682 if (front_comp) {
683 printf("\n# BSDF system geometry (front view)\n");
684 sprintf(cmdbuf, "xform -t %f %f %f -s %f -t %f %f 0 %s",
685 -.5*(xmin+xmax), -.5*(ymin+ymax), -zmax,
686 1.5*sph_rad/max_dim,
687 sph_xoffset, -2.5*sph_rad,
688 radfn);
689 if (!run_cmd(cmdbuf))
690 return;
691 }
692 if (back_comp) {
693 printf("\n# BSDF system geometry (back view)\n");
694 sprintf(cmdbuf, "xform -t %f %f %f -s %f -ry 180 -t %f %f 0 %s",
695 -.5*(xmin+xmax), -.5*(ymin+ymax), -zmin,
696 1.5*sph_rad/max_dim,
697 -sph_xoffset, -2.5*sph_rad,
698 radfn);
699 if (!run_cmd(cmdbuf))
700 return;
701 }
702 }
703
704 /* Check RBF input header line & get minimum BSDF value */
705 static int
706 rbf_headline(char *s, void *p)
707 {
708 char fmt[64];
709
710 if (formatval(fmt, s)) {
711 if (strcmp(fmt, BSDFREP_FMT))
712 return(-1);
713 return(0);
714 }
715 if (!strncmp(s, "IO_SIDES=", 9)) {
716 sscanf(s+9, "%d %d", &input_orient, &output_orient);
717 if (input_orient == output_orient) {
718 if (input_orient > 0)
719 front_comp |= SDsampR;
720 else
721 back_comp |= SDsampR;
722 } else if (input_orient > 0)
723 front_comp |= SDsampT;
724 else
725 back_comp |= SDsampT;
726 return(0);
727 }
728 if (!strncmp(s, "BSDFMIN=", 8)) {
729 sscanf(s+8, "%lf", &bsdf_min);
730 if (bsdf_min < overall_min)
731 overall_min = bsdf_min;
732 return(0);
733 }
734 return(0);
735 }
736
737 /* Produce a Radiance model plotting the given BSDF representation */
738 int
739 main(int argc, char *argv[])
740 {
741 int inpXML = -1;
742 SDData myBSDF;
743 int n;
744 /* check arguments */
745 progname = argv[0];
746 if (argc > 1 && (n = strlen(argv[1])-4) > 0) {
747 if (!strcasecmp(argv[1]+n, ".xml"))
748 inpXML = 1;
749 else if (!strcasecmp(argv[1]+n, ".sir"))
750 inpXML = 0;
751 }
752 if (inpXML < 0 || inpXML & (argc > 2)) {
753 fprintf(stderr, "Usage: %s bsdf.xml > output.rad\n", progname);
754 fprintf(stderr, " Or: %s hemi1.sir hemi2.sir .. > output.rad\n", progname);
755 return(1);
756 }
757 fputs("# ", stdout); /* copy our command */
758 printargs(argc, argv, stdout);
759 /* evaluate BSDF */
760 if (inpXML) {
761 SDclearBSDF(&myBSDF, argv[1]);
762 if (SDreportError(SDloadFile(&myBSDF, argv[1]), stderr))
763 return(1);
764 if (myBSDF.rf != NULL) front_comp |= SDsampR;
765 if (myBSDF.tf != NULL) front_comp |= SDsampT;
766 if (myBSDF.rb != NULL) back_comp |= SDsampR;
767 if (myBSDF.tb != NULL) back_comp |= SDsampT;
768 if (!front_comp & !back_comp) {
769 fprintf(stderr, "%s: nothing to plot in '%s'\n",
770 progname, argv[1]);
771 return(1);
772 }
773 if (front_comp & SDsampR && myBSDF.rLambFront.cieY < overall_min*PI)
774 overall_min = myBSDF.rLambFront.cieY/PI;
775 if (back_comp & SDsampR && myBSDF.rLambBack.cieY < overall_min*PI)
776 overall_min = myBSDF.rLambBack.cieY/PI;
777 if ((front_comp|back_comp) & SDsampT &&
778 myBSDF.tLamb.cieY < overall_min*PI)
779 overall_min = myBSDF.tLamb.cieY/PI;
780 set_minlog();
781 if (!build_wBSDF(&myBSDF))
782 return(1);
783 if (myBSDF.matn[0])
784 strcpy(bsdf_name, myBSDF.matn);
785 else
786 strcpy(bsdf_name, myBSDF.name);
787 strcpy(bsdf_manuf, myBSDF.makr);
788 put_matBSDF(argv[1]);
789 } else {
790 FILE *fp;
791 for (n = 1; n < argc; n++) {
792 fp = fopen(argv[n], "rb");
793 if (fp == NULL) {
794 fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
795 progname, argv[n]);
796 return(1);
797 }
798 if (getheader(fp, rbf_headline, NULL) < 0) {
799 fprintf(stderr, "%s: bad BSDF interpolant '%s'\n",
800 progname, argv[n]);
801 return(1);
802 }
803 fclose(fp);
804 }
805 set_minlog();
806 for (n = 1; n < argc; n++) {
807 fp = fopen(argv[n], "rb");
808 if (!load_bsdf_rep(fp))
809 return(1);
810 fclose(fp);
811 if (!build_wRBF())
812 return(1);
813 }
814 put_matBSDF(NULL);
815 }
816 put_source(); /* before hemispheres & labels */
817 put_hemispheres();
818 put_scale();
819 if (inpXML && myBSDF.mgf)
820 convert_mgf(myBSDF.mgf);
821 if (!put_BSDFs())
822 return(1);
823 cleanup_tmp();
824 return(0);
825 }