ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2rad.c
Revision: 2.18
Committed: Sun Apr 9 22:51:19 2017 UTC (7 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.17: +14 -15 lines
Log Message:
More tweaks and bug fixes

File Contents

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