ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2rad.c
Revision: 2.36
Committed: Fri Jul 19 17:37:56 2019 UTC (4 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R3
Changes since 2.35: +2 -4 lines
Log Message:
Moved declarations and definitions for header.c from resolu.h to rtio.h

File Contents

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