ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2rad.c
Revision: 2.42
Committed: Sat Jun 7 05:09:45 2025 UTC (12 hours, 33 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.41: +1 -2 lines
Log Message:
refactor: Put some declarations into "paths.h" and included in "platform.h"

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdf2rad.c,v 2.41 2025/06/03 21:31:51 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 "rtmath.h"
11 #include "bsdfrep.h"
12
13 #ifndef NINCIDENT
14 #define NINCIDENT 37 /* number of samples/hemisphere */
15 #endif
16 #ifndef GRIDSTEP
17 #define GRIDSTEP 2 /* our grid step size */
18 #endif
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+1] = ""; /* 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 set_minlog() overall_min = (overall_min < 1e-5) ? 1e-5 : overall_min; \
44 min_log10 = log10(overall_min) - .1
45
46 /* Get Fibonacci sphere vector (0 to NINCIDENT-1) */
47 static RREAL *
48 get_ivector(FVECT iv, int i)
49 {
50 const double phistep = PI*(3. - 2.236067978);
51 double r;
52
53 iv[2] = 1. - (i+.5)*(1./NINCIDENT);
54 r = sqrt(1. - iv[2]*iv[2]);
55 iv[0] = r * cos((i+1.)*phistep);
56 iv[1] = r * sin((i+1.)*phistep);
57
58 return(iv);
59 }
60
61 /* Convert incident vector into sphere position */
62 static RREAL *
63 cvt_sposition(FVECT sp, const FVECT iv, int inc_side)
64 {
65 sp[0] = -iv[0]*sph_rad + inc_side*sph_xoffset;
66 sp[1] = -iv[1]*sph_rad;
67 sp[2] = iv[2]*sph_rad;
68
69 return(sp);
70 }
71
72 /* Get temporary file name */
73 static char *
74 tfile_name(const char *prefix, const char *suffix, int i)
75 {
76 static char buf[256];
77
78 if (!ourTempDir[0]) { /* create temporary directory */
79 mktemp(strcpy(ourTempDir,TEMPLATE));
80 if (mkdir(ourTempDir, 0777) < 0) {
81 perror("mkdir");
82 exit(1);
83 }
84 }
85 if (!prefix) prefix = "T";
86 if (!suffix) suffix = "";
87 sprintf(buf, "%s/%s%03d%s", ourTempDir, prefix, i, suffix);
88 return(buf);
89 }
90
91 /* Remove temporary directory & contents */
92 static void
93 cleanup_tmp(void)
94 {
95 char buf[128];
96
97 if (!ourTempDir[0])
98 return;
99 #if defined(_WIN32) || defined(_WIN64)
100 sprintf(buf, "RMDIR %s /S /Q", ourTempDir);
101 #else
102 sprintf(buf, "rm -rf %s", ourTempDir);
103 #endif
104 system(buf);
105 }
106
107 /* Run the specified command, returning 1 if OK */
108 static int
109 run_cmd(const char *cmd)
110 {
111 fflush(stdout);
112 if (system(cmd)) {
113 fprintf(stderr, "%s: error running: %s\n", progname, cmd);
114 return(0);
115 }
116 return(1);
117 }
118
119 /* Plot surface points for the given BSDF incident angle */
120 static int
121 plotBSDF(const char *fname, const FVECT ivec, int dfl, const SDData *sd)
122 {
123 FILE *fp = fopen(fname, "w");
124 int i, j;
125
126 if (fp == NULL) {
127 fprintf(stderr, "%s: cannot open '%s' for writing\n",
128 progname, fname);
129 return(0);
130 }
131 if (ivec[2] > 0) {
132 input_orient = 1;
133 output_orient = dfl&SDsampR ? 1 : -1;
134 } else {
135 input_orient = -1;
136 output_orient = dfl&SDsampR ? -1 : 1;
137 }
138 for (i = SAMPRES; i--; )
139 for (j = 0; j < SAMPRES; j++) {
140 FVECT ovec;
141 SDValue sval;
142 double bsdf;
143 ovec_from_pos(ovec, i*GRIDSTEP, j*GRIDSTEP);
144 if (SDreportError(SDevalBSDF(&sval, ivec,
145 ovec, sd), stderr))
146 return(0);
147 if (sval.cieY > overall_max)
148 overall_max = sval.cieY;
149 bsdf = (sval.cieY < overall_min) ? overall_min : sval.cieY;
150 bsdf = log10(bsdf) - min_log10;
151 fprintf(fp, "%.5f %.5f %.5f\n",
152 ovec[0]*bsdf, ovec[1]*bsdf, ovec[2]*bsdf);
153 }
154 if (fclose(fp) == EOF) {
155 fprintf(stderr, "%s: error writing data to '%s'\n",
156 progname, fname);
157 return(0);
158 }
159 return(1);
160 }
161
162 /* Build BSDF values from loaded XML file */
163 static int
164 build_wBSDF(const SDData *sd)
165 {
166 FVECT ivec;
167 int i;
168
169 if (front_comp & SDsampR)
170 for (i = 0; i < NINCIDENT; i++) {
171 get_ivector(ivec, i);
172 if (!plotBSDF(tfile_name(frpref, dsuffix, i),
173 ivec, SDsampR, sd))
174 return(0);
175 }
176 if (front_comp & SDsampT)
177 for (i = 0; i < NINCIDENT; i++) {
178 get_ivector(ivec, i);
179 if (!plotBSDF(tfile_name(ftpref, dsuffix, i),
180 ivec, SDsampT, sd))
181 return(0);
182 }
183 if (back_comp & SDsampR)
184 for (i = 0; i < NINCIDENT; i++) {
185 get_ivector(ivec, i);
186 ivec[0] = -ivec[0]; ivec[2] = -ivec[2];
187 if (!plotBSDF(tfile_name(brpref, dsuffix, i),
188 ivec, SDsampR, sd))
189 return(0);
190 }
191 if (back_comp & SDsampT)
192 for (i = 0; i < NINCIDENT; i++) {
193 get_ivector(ivec, i);
194 ivec[0] = -ivec[0]; ivec[2] = -ivec[2];
195 if (!plotBSDF(tfile_name(btpref, dsuffix, i),
196 ivec, SDsampT, sd))
197 return(0);
198 }
199 return(1);
200 }
201
202 /* Plot surface points using radial basis function */
203 static int
204 plotRBF(const char *fname, const RBFNODE *rbf)
205 {
206 FILE *fp = fopen(fname, "w");
207 int i, j;
208
209 if (fp == NULL) {
210 fprintf(stderr, "%s: cannot open '%s' for writing\n",
211 progname, fname);
212 return(0);
213 }
214 for (i = SAMPRES; i--; )
215 for (j = 0; j < SAMPRES; j++) {
216 FVECT ovec;
217 double bsdf;
218 ovec_from_pos(ovec, i*GRIDSTEP, j*GRIDSTEP);
219 bsdf = eval_rbfrep(rbf, ovec);
220 if (bsdf > overall_max)
221 overall_max = bsdf;
222 else if (bsdf < overall_min)
223 bsdf = overall_min;
224 bsdf = log10(bsdf) - min_log10;
225 fprintf(fp, "%.5f %.5f %.5f\n",
226 ovec[0]*bsdf, ovec[1]*bsdf, ovec[2]*bsdf);
227 }
228 if (fclose(fp) == EOF) {
229 fprintf(stderr, "%s: error writing data to '%s'\n",
230 progname, fname);
231 return(0);
232 }
233 return(1);
234 }
235
236 /* Build BSDF values from scattering interpolant representation */
237 static int
238 build_wRBF(void)
239 {
240 const char *pref;
241 int i;
242
243 if (input_orient > 0) {
244 if (output_orient > 0)
245 pref = frpref;
246 else
247 pref = ftpref;
248 } else if (output_orient < 0)
249 pref = brpref;
250 else
251 pref = btpref;
252
253 for (i = 0; i < NINCIDENT; i++) {
254 FVECT ivec;
255 RBFNODE *rbf;
256 get_ivector(ivec, i);
257 if (input_orient < 0) {
258 ivec[0] = -ivec[0]; ivec[2] = -ivec[2];
259 }
260 rbf = advect_rbf(ivec, 15000);
261 if (!plotRBF(tfile_name(pref, dsuffix, i), rbf))
262 return(0);
263 if (rbf) free(rbf);
264 }
265 return(1); /* next call frees */
266 }
267
268 /* Put out mirror arrow for the given incident vector */
269 static void
270 put_mirror_arrow(const FVECT origin, const FVECT nrm)
271 {
272 const double arrow_len = 1.2*bsdf_rad;
273 const double tip_len = 0.2*bsdf_rad;
274 static int cnt = 1;
275 FVECT refl;
276 int i;
277
278 refl[0] = 2.*nrm[2]*nrm[0];
279 refl[1] = 2.*nrm[2]*nrm[1];
280 refl[2] = 2.*nrm[2]*nrm[2] - 1.;
281
282 printf("\n# Mirror arrow #%d\n", cnt);
283 printf("\nshaft_mat cylinder inc_dir%d\n0\n0\n7", cnt);
284 printf("\n\t%f %f %f\n\t%f %f %f\n\t%f\n",
285 origin[0], origin[1], origin[2]+arrow_len,
286 origin[0], origin[1], origin[2],
287 arrow_rad);
288 printf("\nshaft_mat cylinder mir_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],
291 origin[0] + arrow_len*refl[0],
292 origin[1] + arrow_len*refl[1],
293 origin[2] + arrow_len*refl[2],
294 arrow_rad);
295 printf("\ntip_mat cone mir_tip%d\n0\n0\n8", cnt);
296 printf("\n\t%f %f %f\n\t%f %f %f\n\t%f 0\n",
297 origin[0] + (arrow_len-.5*tip_len)*refl[0],
298 origin[1] + (arrow_len-.5*tip_len)*refl[1],
299 origin[2] + (arrow_len-.5*tip_len)*refl[2],
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 2.*arrow_rad);
304 ++cnt;
305 }
306
307 /* Put out transmitted direction arrow for the given incident vector */
308 static void
309 put_trans_arrow(const FVECT origin)
310 {
311 const double arrow_len = 1.2*bsdf_rad;
312 const double tip_len = 0.2*bsdf_rad;
313 static int cnt = 1;
314 int i;
315
316 printf("\n# Transmission arrow #%d\n", cnt);
317 printf("\nshaft_mat cylinder trans_dir%d\n0\n0\n7", cnt);
318 printf("\n\t%f %f %f\n\t%f %f %f\n\t%f\n",
319 origin[0], origin[1], origin[2],
320 origin[0], origin[1], origin[2]-arrow_len,
321 arrow_rad);
322 printf("\ntip_mat cone trans_tip%d\n0\n0\n8", cnt);
323 printf("\n\t%f %f %f\n\t%f %f %f\n\t%f 0\n",
324 origin[0], origin[1], origin[2]-arrow_len+.5*tip_len,
325 origin[0], origin[1], origin[2]-arrow_len-.5*tip_len,
326 2.*arrow_rad);
327 ++cnt;
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 (!FABSEQ(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 (!FABSEQ(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 (!FABSEQ(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.5*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.5*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[MAXFMTLEN];
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 double myLim[2];
707 SDData myBSDF;
708 int a, n;
709 /* set global progname */
710 fixargv0(argv[0]);
711 /* check arguments */
712 a = 1;
713 myLim[0] = -1; myLim[1] = -2; /* specified BSDF range? */
714 if (argc > a+3 && argv[a][0] == '-' && argv[a][1] == 'r') {
715 myLim[0] = atof(argv[++a]);
716 myLim[1] = atof(argv[++a]);
717 ++a;
718 }
719 if (argc > a && (n = strlen(argv[a])-4) > 0) {
720 if (!strcasecmp(argv[a]+n, ".xml"))
721 inpXML = 1;
722 else if (!strcasecmp(argv[a]+n, ".sir"))
723 inpXML = 0;
724 }
725 if (inpXML < 0 || inpXML & (argc > a+1)) {
726 fprintf(stderr, "Usage: %s [-r min max] bsdf.xml > output.rad\n", progname);
727 fprintf(stderr, " Or: %s [-r min max] hemi1.sir hemi2.sir .. > output.rad\n", progname);
728 return(1);
729 }
730 fputs("# ", stdout); /* copy our command */
731 printargs(argc, argv, stdout);
732 /* evaluate BSDF */
733 if (inpXML) {
734 SDclearBSDF(&myBSDF, argv[a]);
735 if (SDreportError(SDloadFile(&myBSDF, argv[a]), stderr))
736 return(1);
737 if (myBSDF.rf != NULL) front_comp |= SDsampR;
738 if (myBSDF.tf != NULL) front_comp |= SDsampT;
739 if (myBSDF.rb != NULL) back_comp |= SDsampR;
740 if (myBSDF.tb != NULL) back_comp |= SDsampT;
741 if (!front_comp & !back_comp) {
742 fprintf(stderr, "%s: nothing to plot in '%s'\n",
743 progname, argv[a]);
744 return(1);
745 }
746 if (myLim[0] >= 0)
747 overall_min = myLim[0];
748 else {
749 if (front_comp & SDsampR && myBSDF.rLambFront.cieY < overall_min*PI)
750 overall_min = myBSDF.rLambFront.cieY/PI;
751 if (back_comp & SDsampR && myBSDF.rLambBack.cieY < overall_min*PI)
752 overall_min = myBSDF.rLambBack.cieY/PI;
753 if (front_comp & SDsampT && myBSDF.tLambFront.cieY < overall_min*PI)
754 overall_min = myBSDF.tLambFront.cieY/PI;
755 if (back_comp & SDsampT && myBSDF.tLambBack.cieY < overall_min*PI)
756 overall_min = myBSDF.tLambBack.cieY/PI;
757 }
758 set_minlog();
759 if (!build_wBSDF(&myBSDF))
760 return(1);
761 if (myBSDF.matn[0])
762 strcpy(bsdf_name, myBSDF.matn);
763 else
764 strcpy(bsdf_name, myBSDF.name);
765 strcpy(bsdf_manuf, myBSDF.makr);
766 put_matBSDF(argv[a]);
767 } else {
768 FILE *fp[4];
769 if (argc > a+4) {
770 fprintf(stderr, "%s: more than 4 hemispheres!\n", progname);
771 return(1);
772 }
773 for (n = a; n < argc; n++) {
774 fp[n-a] = fopen(argv[n], "rb");
775 if (fp[n-a] == NULL) {
776 fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
777 progname, argv[n]);
778 return(1);
779 }
780 if (getheader(fp[n-a], rbf_headline, NULL) < 0) {
781 fprintf(stderr, "%s: bad BSDF interpolant '%s'\n",
782 progname, argv[n]);
783 return(1);
784 }
785 }
786 if (myLim[0] >= 0)
787 overall_min = myLim[0];
788 set_minlog();
789 for (n = a; n < argc; n++) {
790 if (fseek(fp[n-a], 0L, SEEK_SET) < 0) {
791 fprintf(stderr, "%s: cannot seek on '%s'\n",
792 progname, argv[n]);
793 return(1);
794 }
795 if (!load_bsdf_rep(fp[n-a]))
796 return(1);
797 fclose(fp[n-a]);
798 if (!build_wRBF())
799 return(1);
800 }
801 put_matBSDF(NULL);
802 }
803 if (myLim[1] > myLim[0]) /* override maximum BSDF? */
804 overall_max = myLim[1];
805 put_source(); /* before hemispheres & labels */
806 put_hemispheres();
807 put_scale();
808 if (inpXML && myBSDF.mgf)
809 convert_mgf(myBSDF.mgf);
810 if (!put_BSDFs()) /* most of the output happens here */
811 return(1);
812 cleanup_tmp();
813 return(0);
814 }