ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2rad.c
Revision: 2.27
Committed: Wed Apr 12 04:15:08 2017 UTC (7 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.26: +16 -9 lines
Log Message:
Keep file open until done with it

File Contents

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