ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2rad.c
Revision: 2.30
Committed: Mon May 15 19:17:27 2017 UTC (7 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.29: +3 -2 lines
Log Message:
Fixed issue with zero minimum BSDF values

File Contents

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