ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2rad.c
Revision: 2.32
Committed: Thu Aug 3 19:50:12 2017 UTC (6 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R1
Changes since 2.31: +13 -9 lines
Log Message:
Added numbering to arrows

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdf2rad.c,v 2.31 2017/05/31 02:41:52 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 char frpref[] = "rf";
30 const char ftpref[] = "tf";
31 const char brpref[] = "rb";
32 const char btpref[] = "tb";
33 const char dsuffix[] = ".txt";
34
35 const char sph_fmat[] = "fBSDFmat";
36 const char sph_bmat[] = "bBSDFmat";
37 const double sph_rad = 10.;
38 const double sph_xoffset = 15.;
39
40 #define bsdf_rad (sph_rad*.25)
41 #define arrow_rad (bsdf_rad*.015)
42
43 #define FEQ(a,b) ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
44
45 #define set_minlog() overall_min = (overall_min < 1e-5) ? 1e-5 : overall_min; \
46 min_log10 = log10(overall_min) - .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 origin, const FVECT nrm)
275 {
276 const double arrow_len = 1.2*bsdf_rad;
277 const double tip_len = 0.2*bsdf_rad;
278 static int cnt = 1;
279 FVECT refl;
280 int i;
281
282 refl[0] = 2.*nrm[2]*nrm[0];
283 refl[1] = 2.*nrm[2]*nrm[1];
284 refl[2] = 2.*nrm[2]*nrm[2] - 1.;
285
286 printf("\n# Mirror arrow #%d\n", cnt);
287 printf("\nshaft_mat cylinder inc_dir%d\n0\n0\n7", cnt);
288 printf("\n\t%f %f %f\n\t%f %f %f\n\t%f\n",
289 origin[0], origin[1], origin[2]+arrow_len,
290 origin[0], origin[1], origin[2],
291 arrow_rad);
292 printf("\nshaft_mat cylinder mir_dir%d\n0\n0\n7", cnt);
293 printf("\n\t%f %f %f\n\t%f %f %f\n\t%f\n",
294 origin[0], origin[1], origin[2],
295 origin[0] + arrow_len*refl[0],
296 origin[1] + arrow_len*refl[1],
297 origin[2] + arrow_len*refl[2],
298 arrow_rad);
299 printf("\ntip_mat cone mir_tip%d\n0\n0\n8", cnt);
300 printf("\n\t%f %f %f\n\t%f %f %f\n\t%f 0\n",
301 origin[0] + (arrow_len-.5*tip_len)*refl[0],
302 origin[1] + (arrow_len-.5*tip_len)*refl[1],
303 origin[2] + (arrow_len-.5*tip_len)*refl[2],
304 origin[0] + (arrow_len+.5*tip_len)*refl[0],
305 origin[1] + (arrow_len+.5*tip_len)*refl[1],
306 origin[2] + (arrow_len+.5*tip_len)*refl[2],
307 2.*arrow_rad);
308 ++cnt;
309 }
310
311 /* Put out transmitted direction arrow for the given incident vector */
312 static void
313 put_trans_arrow(const FVECT origin)
314 {
315 const double arrow_len = 1.2*bsdf_rad;
316 const double tip_len = 0.2*bsdf_rad;
317 static int cnt = 1;
318 int i;
319
320 printf("\n# Transmission arrow #%d\n", cnt);
321 printf("\nshaft_mat cylinder trans_dir%d\n0\n0\n7", cnt);
322 printf("\n\t%f %f %f\n\t%f %f %f\n\t%f\n",
323 origin[0], origin[1], origin[2],
324 origin[0], origin[1], origin[2]-arrow_len,
325 arrow_rad);
326 printf("\ntip_mat cone trans_tip%d\n0\n0\n8", cnt);
327 printf("\n\t%f %f %f\n\t%f %f %f\n\t%f 0\n",
328 origin[0], origin[1], origin[2]-arrow_len+.5*tip_len,
329 origin[0], origin[1], origin[2]-arrow_len-.5*tip_len,
330 2.*arrow_rad);
331 ++cnt;
332 }
333
334 /* Compute rotation (x,y,z) => (xp,yp,zp) */
335 static int
336 addrot(char *xf, const FVECT xp, const FVECT yp, const FVECT zp)
337 {
338 int n = 0;
339 double theta;
340
341 if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) {
342 /* Special case for X' along Z-axis */
343 theta = -atan2(yp[0], yp[1]);
344 sprintf(xf, " -ry %f -rz %f",
345 xp[2] < 0.0 ? 90.0 : -90.0,
346 theta*(180./PI));
347 return(4);
348 }
349 theta = atan2(yp[2], zp[2]);
350 if (!FEQ(theta,0.0)) {
351 sprintf(xf, " -rx %f", theta*(180./PI));
352 while (*xf) ++xf;
353 n += 2;
354 }
355 theta = Asin(-xp[2]);
356 if (!FEQ(theta,0.0)) {
357 sprintf(xf, " -ry %f", theta*(180./PI));
358 while (*xf) ++xf;
359 n += 2;
360 }
361 theta = atan2(xp[1], xp[0]);
362 if (!FEQ(theta,0.0)) {
363 sprintf(xf, " -rz %f", theta*(180./PI));
364 /* while (*xf) ++xf; */
365 n += 2;
366 }
367 return(n);
368 }
369
370 /* Put out BSDF surfaces */
371 static int
372 put_BSDFs(void)
373 {
374 const double scalef = bsdf_rad/(log10(overall_max) - min_log10);
375 FVECT ivec, sorg, nrm, upv;
376 RREAL vMtx[3][3];
377 char *fname;
378 char cmdbuf[256];
379 char rotargs[64];
380 int nrota;
381 int i;
382
383 printf("\n# Gensurf output corresponding to %d incident directions\n",
384 NINCIDENT);
385
386 printf("\nvoid glow tip_mat\n0\n0\n4 1 0 1 0\n");
387 printf("\nvoid mixfunc shaft_mat\n4 tip_mat void 0.25 .\n0\n0\n");
388
389 for (i = 0; i < NINCIDENT; i++) {
390 get_ivector(ivec, i);
391 nrm[0] = -ivec[0]; nrm[1] = -ivec[1]; nrm[2] = ivec[2];
392 upv[0] = nrm[0]*nrm[1]*(nrm[2] - 1.);
393 upv[1] = nrm[0]*nrm[0] + nrm[1]*nrm[1]*nrm[2];
394 upv[2] = -nrm[1]*(nrm[0]*nrm[0] + nrm[1]*nrm[1]);
395 if (SDcompXform(vMtx, nrm, upv) != SDEnone)
396 continue;
397 nrota = addrot(rotargs, vMtx[0], vMtx[1], vMtx[2]);
398 if (front_comp) {
399 cvt_sposition(sorg, ivec, 1);
400 printf("\nvoid colorfunc scale_pat\n");
401 printf("10 bsdf_red bsdf_grn bsdf_blu bsdf2rad.cal\n");
402 printf("\t-s %f -t %f %f %f\n0\n0\n",
403 bsdf_rad, sorg[0], sorg[1], sorg[2]);
404 printf("\nscale_pat glow scale_mat\n0\n0\n4 1 1 1 0\n");
405 }
406 if (front_comp & SDsampR) {
407 put_mirror_arrow(sorg, nrm);
408 fname = tfile_name(frpref, dsuffix, i);
409 sprintf(cmdbuf,
410 "gensurf scale_mat %s%d %s %s %s %d %d | xform %s -s %f -t %f %f %f",
411 frpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
412 rotargs, scalef, sorg[0], sorg[1], sorg[2]);
413 if (!run_cmd(cmdbuf))
414 return(0);
415 }
416 if (front_comp & SDsampT) {
417 put_trans_arrow(sorg);
418 fname = tfile_name(ftpref, dsuffix, i);
419 sprintf(cmdbuf,
420 "gensurf scale_mat %s%d %s %s %s %d %d | xform -I %s -s %f -t %f %f %f",
421 ftpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
422 rotargs, scalef, sorg[0], sorg[1], sorg[2]);
423 if (!run_cmd(cmdbuf))
424 return(0);
425 }
426 if (back_comp) {
427 cvt_sposition(sorg, ivec, -1);
428 printf("\nvoid colorfunc scale_pat\n");
429 printf("10 bsdf_red bsdf_grn bsdf_blu bsdf2rad.cal\n");
430 printf("\t-s %f -t %f %f %f\n0\n0\n",
431 bsdf_rad, sorg[0], sorg[1], sorg[2]);
432 printf("\nscale_pat glow scale_mat\n0\n0\n4 1 1 1 0\n");
433 }
434 if (back_comp & SDsampR) {
435 put_mirror_arrow(sorg, nrm);
436 fname = tfile_name(brpref, dsuffix, i);
437 sprintf(cmdbuf,
438 "gensurf scale_mat %s%d %s %s %s %d %d | xform -I -ry 180 %s -s %f -t %f %f %f",
439 brpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
440 rotargs, scalef, sorg[0], sorg[1], sorg[2]);
441 if (!run_cmd(cmdbuf))
442 return(0);
443 }
444 if (back_comp & SDsampT) {
445 put_trans_arrow(sorg);
446 fname = tfile_name(btpref, dsuffix, i);
447 sprintf(cmdbuf,
448 "gensurf scale_mat %s%d %s %s %s %d %d | xform -ry 180 %s -s %f -t %f %f %f",
449 btpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
450 rotargs, scalef, sorg[0], sorg[1], sorg[2]);
451 if (!run_cmd(cmdbuf))
452 return(0);
453 }
454 }
455 return(1);
456 }
457
458 /* Put our hemisphere material */
459 static void
460 put_matBSDF(const char *XMLfile)
461 {
462 const char *curdir = "./";
463
464 if (!XMLfile) { /* simple material */
465 printf("\n# Simplified material because we have no XML input\n");
466 printf("\nvoid brightfunc latlong\n2 latlong bsdf2rad.cal\n0\n0\n");
467 if ((front_comp|back_comp) & SDsampT)
468 printf("\nlatlong trans %s\n0\n0\n7 .75 .75 .75 0 .04 .5 .8\n",
469 sph_fmat);
470 else
471 printf("\nlatlong plastic %s\n0\n0\n5 .5 .5 .5 0 0\n",
472 sph_fmat);
473 printf("\ninherit alias %s %s\n", sph_bmat, sph_fmat);
474 return;
475 }
476 switch (XMLfile[0]) { /* avoid RAYPATH search */
477 case '.':
478 case '~':
479 CASEDIRSEP:
480 curdir = "";
481 break;
482 case '\0':
483 fprintf(stderr, "%s: empty file name in put_matBSDF\n", progname);
484 exit(1);
485 break;
486 }
487 printf("\n# Actual BSDF materials for rendering the hemispheres\n");
488 printf("\nvoid BSDF BSDF_f\n6 0 \"%s%s\" upx upy upz bsdf2rad.cal\n0\n0\n",
489 curdir, XMLfile);
490 printf("\nvoid plastic black\n0\n0\n5 0 0 0 0 0\n");
491 printf("\nvoid mixfunc %s\n4 BSDF_f black latlong bsdf2rad.cal\n0\n0\n",
492 sph_fmat);
493 printf("\nvoid BSDF BSDF_b\n8 0 \"%s%s\" upx upy upz bsdf2rad.cal -ry 180\n0\n0\n",
494 curdir, XMLfile);
495 printf("\nvoid mixfunc %s\n4 BSDF_b black latlong bsdf2rad.cal\n0\n0\n",
496 sph_bmat);
497 }
498
499 /* Put out overhead parallel light source */
500 static void
501 put_source(void)
502 {
503 printf("\n# Overhead parallel light source\n");
504 printf("\nvoid light bright\n0\n0\n3 2500 2500 2500\n");
505 printf("\nbright source light\n0\n0\n4 0 0 1 2\n");
506 printf("\n# Material used for labels\n");
507 printf("\nvoid trans vellum\n0\n0\n7 1 1 1 0 0 .5 0\n");
508 }
509
510 /* Put out hemisphere(s) */
511 static void
512 put_hemispheres(void)
513 {
514 const int nsegs = 131;
515
516 printf("\n# Hemisphere(s) for showing BSDF appearance (if XML file)\n");
517 if (front_comp) {
518 printf(
519 "\n!genrev %s Front \"R*sin(A*t)\" \"R*cos(A*t)\" %d -e \"R:%g;A:%f\" -s | xform -t %g 0 0\n",
520 sph_fmat, nsegs, sph_rad, 0.5*PI, sph_xoffset);
521 printf("\nvoid brighttext front_text\n3 helvet.fnt . FRONT\n0\n");
522 printf("12\n\t%f %f 0\n\t%f 0 0\n\t0 %f 0\n\t.01 1 -.1\n",
523 -.22*sph_rad + sph_xoffset, -1.4*sph_rad,
524 .35/5.*sph_rad, -1.6*.35/5.*sph_rad);
525 printf("\nfront_text alias front_label_mat vellum\n");
526 printf("\nfront_label_mat polygon front_label\n0\n0\n12");
527 printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
528 -.25*sph_rad + sph_xoffset, -1.3*sph_rad,
529 -.25*sph_rad + sph_xoffset, (-1.4-1.6*.35/5.-.1)*sph_rad,
530 .25*sph_rad + sph_xoffset, (-1.4-1.6*.35/5.-.1)*sph_rad,
531 .25*sph_rad + sph_xoffset, -1.3*sph_rad );
532 }
533 if (back_comp) {
534 printf(
535 "\n!genrev %s Back \"R*cos(A*t)\" \"R*sin(A*t)\" %d -e \"R:%g;A:%f\" -s | xform -t %g 0 0\n",
536 sph_bmat, nsegs, sph_rad, 0.5*PI, -sph_xoffset);
537 printf("\nvoid brighttext back_text\n3 helvet.fnt . BACK\n0\n");
538 printf("12\n\t%f %f 0\n\t%f 0 0\n\t0 %f 0\n\t.01 1 -.1\n",
539 -.22*sph_rad - sph_xoffset, -1.4*sph_rad,
540 .35/4.*sph_rad, -1.6*.35/4.*sph_rad);
541 printf("\nback_text alias back_label_mat vellum\n");
542 printf("\nback_label_mat polygon back_label\n0\n0\n12");
543 printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
544 -.25*sph_rad - sph_xoffset, -1.3*sph_rad,
545 -.25*sph_rad - sph_xoffset, (-1.4-1.6*.35/4.-.1)*sph_rad,
546 .25*sph_rad - sph_xoffset, (-1.4-1.6*.35/4.-.1)*sph_rad,
547 .25*sph_rad - sph_xoffset, -1.3*sph_rad );
548 }
549 }
550
551 /* Put out falsecolor scale and name label */
552 static void
553 put_scale(void)
554 {
555 const double max_log10 = log10(overall_max);
556 const double leg_width = 2.*.75*(fabs(sph_xoffset) - sph_rad);
557 const double leg_height = 2.*sph_rad;
558 const int text_lines = 6;
559 const int text_digits = 8;
560 char fmt[16];
561 int i;
562
563 printf("\n# BSDF legend with falsecolor scale\n");
564 printf("\nvoid colorfunc lscale\n10 sca_red(Py) sca_grn(Py) sca_blu(Py)");
565 printf("\n\tbsdf2rad.cal -s %f -t 0 %f 0\n0\n0\n", leg_height, -.5*leg_height);
566 sprintf(fmt, "%%.%df", text_digits-3);
567 for (i = 0; i < text_lines; i++) {
568 char vbuf[16];
569 sprintf(vbuf, fmt, pow(10., (i+.5)/text_lines*(max_log10-min_log10)+min_log10));
570 printf("\nlscale brighttext lscale\n");
571 printf("3 helvet.fnt . %s\n0\n12\n", vbuf);
572 printf("\t%f %f 0\n", -.45*leg_width, ((i+.9)/text_lines-.5)*leg_height);
573 printf("\t%f 0 0\n", .8*leg_width/strlen(vbuf));
574 printf("\t0 %f 0\n", -.9/text_lines*leg_height);
575 printf("\t.01 1 -.1\n");
576 }
577 printf("\nlscale alias legend_mat vellum\n");
578 printf("\nlegend_mat polygon legend\n0\n0\n12");
579 printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
580 -.5*leg_width, .5*leg_height,
581 -.5*leg_width, -.5*leg_height,
582 .5*leg_width, -.5*leg_height,
583 .5*leg_width, .5*leg_height);
584 printf("\nvoid brighttext BSDFtitle\n3 helvet.fnt . BSDF\n0\n12\n");
585 printf("\t%f %f 0\n", -.25*leg_width, .7*leg_height);
586 printf("\t%f 0 0\n", .4/4.*leg_width);
587 printf("\t0 %f 0\n", -.1*leg_height);
588 printf("\t.01 1 -.1\n");
589 printf("\nBSDFtitle alias title_mat vellum\n");
590 printf("\ntitle_mat polygon title\n0\n0\n12");
591 printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
592 -.3*leg_width, .75*leg_height,
593 -.3*leg_width, .55*leg_height,
594 .3*leg_width, .55*leg_height,
595 .3*leg_width, .75*leg_height);
596 if (!bsdf_name[0])
597 return;
598 printf("\nvoid brighttext BSDFname\n3 helvet.fnt . \"%s\"\n0\n12\n", bsdf_name);
599 printf("\t%f %f 0\n", -.95*leg_width, -.6*leg_height);
600 printf("\t%f 0 0\n", 1.8/strlen(bsdf_name)*leg_width);
601 printf("\t0 %f 0\n", -.1*leg_height);
602 printf("\t.01 1 -.1\n");
603 printf("\nBSDFname alias name_mat vellum\n");
604 printf("\nname_mat polygon name\n0\n0\n12");
605 printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
606 -leg_width, -.55*leg_height,
607 -leg_width, -.75*leg_height,
608 leg_width, -.75*leg_height,
609 leg_width, -.55*leg_height);
610 }
611
612 /* Convert MGF to Radiance in output */
613 static void
614 convert_mgf(const char *mgfdata)
615 {
616 int len = strlen(mgfdata);
617 char mgfn[128];
618 char radfn[128];
619 char cmdbuf[256];
620 float xmin, xmax, ymin, ymax, zmin, zmax;
621 double max_dim;
622 int fd;
623 FILE *fp;
624
625 if (!len) return;
626 strcpy(mgfn, tfile_name("geom", ".mgf", 0));
627 fd = open(mgfn, O_WRONLY|O_CREAT, 0666);
628 if (fd < 0 || write(fd, mgfdata, len) != len) {
629 fprintf(stderr, "%s: cannot write file '%s'\n",
630 progname, mgfn);
631 return;
632 }
633 close(fd);
634 strcpy(radfn, tfile_name("geom", ".rad", 0));
635 sprintf(cmdbuf, "mgf2rad %s > %s", mgfn, radfn);
636 if (!run_cmd(cmdbuf))
637 return;
638 sprintf(cmdbuf, "getbbox -w -h %s", radfn);
639 if ((fp = popen(cmdbuf, "r")) == NULL ||
640 fscanf(fp, "%f %f %f %f %f %f",
641 &xmin, &xmax, &ymin, &ymax, &zmin, &zmax) != 6
642 || pclose(fp) < 0) {
643 fprintf(stderr, "%s: error reading from command: %s\n",
644 progname, cmdbuf);
645 return;
646 }
647 max_dim = ymax - ymin;
648 if (xmax - xmin > max_dim)
649 max_dim = xmax - xmin;
650 if (front_comp) {
651 printf("\n# BSDF system geometry (front view)\n");
652 sprintf(cmdbuf, "xform -t %f %f %f -s %f -t %f %f 0 %s",
653 -.5*(xmin+xmax), -.5*(ymin+ymax), -zmax,
654 1.5*sph_rad/max_dim,
655 sph_xoffset, -2.5*sph_rad,
656 radfn);
657 if (!run_cmd(cmdbuf))
658 return;
659 }
660 if (back_comp) {
661 printf("\n# BSDF system geometry (back view)\n");
662 sprintf(cmdbuf, "xform -t %f %f %f -s %f -ry 180 -t %f %f 0 %s",
663 -.5*(xmin+xmax), -.5*(ymin+ymax), -zmin,
664 1.5*sph_rad/max_dim,
665 -sph_xoffset, -2.5*sph_rad,
666 radfn);
667 if (!run_cmd(cmdbuf))
668 return;
669 }
670 }
671
672 /* Check RBF input header line & get minimum BSDF value */
673 static int
674 rbf_headline(char *s, void *p)
675 {
676 char fmt[64];
677
678 if (formatval(fmt, s)) {
679 if (strcmp(fmt, BSDFREP_FMT))
680 return(-1);
681 return(0);
682 }
683 if (!strncmp(s, "IO_SIDES=", 9)) {
684 sscanf(s+9, "%d %d", &input_orient, &output_orient);
685 if (input_orient == output_orient) {
686 if (input_orient > 0)
687 front_comp |= SDsampR;
688 else
689 back_comp |= SDsampR;
690 } else if (input_orient > 0)
691 front_comp |= SDsampT;
692 else
693 back_comp |= SDsampT;
694 return(0);
695 }
696 if (!strncmp(s, "BSDFMIN=", 8)) {
697 sscanf(s+8, "%lf", &bsdf_min);
698 if (bsdf_min < overall_min)
699 overall_min = bsdf_min;
700 return(0);
701 }
702 return(0);
703 }
704
705 /* Produce a Radiance model plotting the given BSDF representation */
706 int
707 main(int argc, char *argv[])
708 {
709 int inpXML = -1;
710 SDData myBSDF;
711 int n;
712 /* check arguments */
713 progname = argv[0];
714 if (argc > 1 && (n = strlen(argv[1])-4) > 0) {
715 if (!strcasecmp(argv[1]+n, ".xml"))
716 inpXML = 1;
717 else if (!strcasecmp(argv[1]+n, ".sir"))
718 inpXML = 0;
719 }
720 if (inpXML < 0 || inpXML & (argc > 2)) {
721 fprintf(stderr, "Usage: %s bsdf.xml > output.rad\n", progname);
722 fprintf(stderr, " Or: %s hemi1.sir hemi2.sir .. > output.rad\n", progname);
723 return(1);
724 }
725 fputs("# ", stdout); /* copy our command */
726 printargs(argc, argv, stdout);
727 /* evaluate BSDF */
728 if (inpXML) {
729 SDclearBSDF(&myBSDF, argv[1]);
730 if (SDreportError(SDloadFile(&myBSDF, argv[1]), stderr))
731 return(1);
732 if (myBSDF.rf != NULL) front_comp |= SDsampR;
733 if (myBSDF.tf != NULL) front_comp |= SDsampT;
734 if (myBSDF.rb != NULL) back_comp |= SDsampR;
735 if (myBSDF.tb != NULL) back_comp |= SDsampT;
736 if (!front_comp & !back_comp) {
737 fprintf(stderr, "%s: nothing to plot in '%s'\n",
738 progname, argv[1]);
739 return(1);
740 }
741 if (front_comp & SDsampR && myBSDF.rLambFront.cieY < overall_min*PI)
742 overall_min = myBSDF.rLambFront.cieY/PI;
743 if (back_comp & SDsampR && myBSDF.rLambBack.cieY < overall_min*PI)
744 overall_min = myBSDF.rLambBack.cieY/PI;
745 if ((front_comp|back_comp) & SDsampT &&
746 myBSDF.tLamb.cieY < overall_min*PI)
747 overall_min = myBSDF.tLamb.cieY/PI;
748 set_minlog();
749 if (!build_wBSDF(&myBSDF))
750 return(1);
751 if (myBSDF.matn[0])
752 strcpy(bsdf_name, myBSDF.matn);
753 else
754 strcpy(bsdf_name, myBSDF.name);
755 strcpy(bsdf_manuf, myBSDF.makr);
756 put_matBSDF(argv[1]);
757 } else {
758 FILE *fp[4];
759 if (argc > 5) {
760 fprintf(stderr, "%s: more than 4 hemispheres!\n", progname);
761 return(1);
762 }
763 for (n = 1; n < argc; n++) {
764 fp[n-1] = fopen(argv[n], "rb");
765 if (fp[n-1] == NULL) {
766 fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
767 progname, argv[n]);
768 return(1);
769 }
770 if (getheader(fp[n-1], rbf_headline, NULL) < 0) {
771 fprintf(stderr, "%s: bad BSDF interpolant '%s'\n",
772 progname, argv[n]);
773 return(1);
774 }
775 }
776 set_minlog();
777 for (n = 1; n < argc; n++) {
778 if (fseek(fp[n-1], 0L, SEEK_SET) < 0) {
779 fprintf(stderr, "%s: cannot seek on '%s'\n",
780 progname, argv[n]);
781 return(1);
782 }
783 if (!load_bsdf_rep(fp[n-1]))
784 return(1);
785 fclose(fp[n-1]);
786 if (!build_wRBF())
787 return(1);
788 }
789 put_matBSDF(NULL);
790 }
791 put_source(); /* before hemispheres & labels */
792 put_hemispheres();
793 put_scale();
794 if (inpXML && myBSDF.mgf)
795 convert_mgf(myBSDF.mgf);
796 if (!put_BSDFs()) /* most of the output happens here */
797 return(1);
798 cleanup_tmp();
799 return(0);
800 }