ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2rad.c
Revision: 2.19
Committed: Mon Apr 10 01:31:37 2017 UTC (7 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.18: +45 -36 lines
Log Message:
Corrected angles (I hope)

File Contents

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