ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2rad.c
Revision: 2.29
Committed: Wed Apr 12 05:01:45 2017 UTC (7 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.28: +8 -8 lines
Log Message:
Switched to brighter arrow tips

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.29 static const char RCSid[] = "$Id: bsdf2rad.c,v 2.28 2017/04/12 04:27:04 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 greg 2.26 const char frpref[] = "rf";
30     const char ftpref[] = "tf";
31     const char brpref[] = "rb";
32     const char btpref[] = "tb";
33 greg 2.16 const char dsuffix[] = ".txt";
34    
35 greg 2.24 const char sph_fmat[] = "fBSDFmat";
36     const char sph_bmat[] = "bBSDFmat";
37 greg 2.16 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 greg 2.19 static RREAL *
51 greg 2.16 get_ivector(FVECT iv, int i)
52     {
53     const double phistep = PI*(3. - 2.236067978);
54     double r;
55    
56 greg 2.18 iv[2] = 1. - (i+.5)*(1./NINCIDENT);
57 greg 2.16 r = sqrt(1. - iv[2]*iv[2]);
58     iv[0] = r * cos((i+1.)*phistep);
59     iv[1] = r * sin((i+1.)*phistep);
60 greg 2.19
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 greg 2.20 sp[0] = -iv[0]*sph_rad + inc_side*sph_xoffset;
69 greg 2.19 sp[1] = -iv[1]*sph_rad;
70     sp[2] = iv[2]*sph_rad;
71    
72     return(sp);
73 greg 2.16 }
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 schorsch 2.15 #if defined(_WIN32) || defined(_WIN64)
103 greg 2.16 sprintf(buf, "RMDIR %s /S /Q", ourTempDir);
104 greg 2.11 #else
105 greg 2.16 sprintf(buf, "rm -rf %s", ourTempDir);
106 greg 2.11 #endif
107 greg 2.16 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 greg 2.19 ivec[0] = -ivec[0]; ivec[2] = -ivec[2];
262 greg 2.16 }
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 greg 2.25 put_mirror_arrow(const FVECT origin, const FVECT nrm)
274 greg 2.16 {
275     const double arrow_len = 1.2*bsdf_rad;
276     const double tip_len = 0.2*bsdf_rad;
277 greg 2.25 FVECT refl;
278 greg 2.16 int i;
279    
280 greg 2.25 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 greg 2.16
284     printf("\n# Mirror arrow\n");
285 greg 2.29 printf("\nshaft_mat cylinder inc_dir\n0\n0\n7");
286 greg 2.16 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 greg 2.29 printf("\nshaft_mat cylinder mir_dir\n0\n0\n7");
291 greg 2.16 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 greg 2.29 printf("\ntip_mat cone mir_tip\n0\n0\n8");
298 greg 2.16 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 greg 2.25 put_trans_arrow(const FVECT origin)
311 greg 2.16 {
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 greg 2.29 printf("\nshaft_mat cylinder trans_dir\n0\n0\n7");
318 greg 2.16 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 greg 2.29 printf("\ntip_mat cone trans_tip\n0\n0\n8");
323 greg 2.16 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 greg 2.10
336 greg 2.16 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 greg 2.25 FVECT ivec, sorg, nrm, upv;
371 greg 2.16 RREAL vMtx[3][3];
372     char *fname;
373     char cmdbuf[256];
374 greg 2.25 char rotargs[64];
375     int nrota;
376 greg 2.16 int i;
377    
378     printf("\n# Gensurf output corresponding to %d incident directions\n",
379     NINCIDENT);
380    
381 greg 2.29 printf("\nvoid glow tip_mat\n0\n0\n4 1 0 1 0\n");
382     printf("\nvoid mixfunc shaft_mat\n4 tip_mat void 0.25 .\n0\n0\n");
383 greg 2.16
384 greg 2.25 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 greg 2.19 cvt_sposition(sorg, ivec, 1);
395 greg 2.16 printf("\nvoid colorfunc scale_pat\n");
396 greg 2.25 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 greg 2.16 printf("\nscale_pat glow scale_mat\n0\n0\n4 1 1 1 0\n");
400 greg 2.25 }
401     if (front_comp & SDsampR) {
402     put_mirror_arrow(sorg, nrm);
403 greg 2.16 fname = tfile_name(frpref, dsuffix, i);
404 greg 2.25 sprintf(cmdbuf,
405     "gensurf scale_mat %s%d %s %s %s %d %d | xform %s -s %f -t %f %f %f",
406 greg 2.20 frpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
407 greg 2.25 rotargs, scalef, sorg[0], sorg[1], sorg[2]);
408 greg 2.16 if (!run_cmd(cmdbuf))
409     return(0);
410     }
411 greg 2.25 if (front_comp & SDsampT) {
412     put_trans_arrow(sorg);
413 greg 2.16 fname = tfile_name(ftpref, dsuffix, i);
414 greg 2.25 sprintf(cmdbuf,
415     "gensurf scale_mat %s%d %s %s %s %d %d | xform -I %s -s %f -t %f %f %f",
416 greg 2.20 ftpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
417 greg 2.25 rotargs, scalef, sorg[0], sorg[1], sorg[2]);
418 greg 2.16 if (!run_cmd(cmdbuf))
419     return(0);
420     }
421 greg 2.25 if (back_comp) {
422 greg 2.19 cvt_sposition(sorg, ivec, -1);
423 greg 2.16 printf("\nvoid colorfunc scale_pat\n");
424 greg 2.25 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 greg 2.16 printf("\nscale_pat glow scale_mat\n0\n0\n4 1 1 1 0\n");
428 greg 2.25 }
429     if (back_comp & SDsampR) {
430     put_mirror_arrow(sorg, nrm);
431 greg 2.16 fname = tfile_name(brpref, dsuffix, i);
432 greg 2.25 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 greg 2.20 brpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
435 greg 2.25 rotargs, scalef, sorg[0], sorg[1], sorg[2]);
436 greg 2.16 if (!run_cmd(cmdbuf))
437     return(0);
438     }
439 greg 2.25 if (back_comp & SDsampT) {
440     put_trans_arrow(sorg);
441 greg 2.16 fname = tfile_name(btpref, dsuffix, i);
442 greg 2.25 sprintf(cmdbuf,
443     "gensurf scale_mat %s%d %s %s %s %d %d | xform -ry 180 %s -s %f -t %f %f %f",
444 greg 2.20 btpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
445 greg 2.25 rotargs, scalef, sorg[0], sorg[1], sorg[2]);
446 greg 2.16 if (!run_cmd(cmdbuf))
447     return(0);
448     }
449 greg 2.25 }
450 greg 2.16 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 greg 2.20 printf("\nlatlong trans %s\n0\n0\n7 .75 .75 .75 0 .04 .5 .8\n",
464 greg 2.24 sph_fmat);
465 greg 2.16 else
466     printf("\nlatlong plastic %s\n0\n0\n5 .5 .5 .5 0 0\n",
467 greg 2.24 sph_fmat);
468     printf("\ninherit alias %s %s\n", sph_bmat, sph_fmat);
469 greg 2.16 return;
470     }
471     switch (XMLfile[0]) { /* avoid RAYPATH search */
472     case '.':
473 greg 2.28 case '~':
474 greg 2.16 CASEDIRSEP:
475     curdir = "";
476     break;
477     case '\0':
478     fprintf(stderr, "%s: empty file name in put_matBSDF\n", progname);
479     exit(1);
480     break;
481     }
482 greg 2.24 printf("\n# Actual BSDF materials for rendering the hemispheres\n");
483     printf("\nvoid BSDF BSDF_f\n6 0 \"%s%s\" upx upy upz bsdf2rad.cal\n0\n0\n",
484 greg 2.16 curdir, XMLfile);
485     printf("\nvoid plastic black\n0\n0\n5 0 0 0 0 0\n");
486 greg 2.24 printf("\nvoid mixfunc %s\n4 BSDF_f black latlong bsdf2rad.cal\n0\n0\n",
487     sph_fmat);
488     printf("\nvoid BSDF BSDF_b\n8 0 \"%s%s\" upx upy upz bsdf2rad.cal -ry 180\n0\n0\n",
489     curdir, XMLfile);
490     printf("\nvoid mixfunc %s\n4 BSDF_b black latlong bsdf2rad.cal\n0\n0\n",
491     sph_bmat);
492 greg 2.16 }
493    
494     /* Put out overhead parallel light source */
495     static void
496     put_source(void)
497     {
498     printf("\n# Overhead parallel light source\n");
499 greg 2.20 printf("\nvoid light bright\n0\n0\n3 2500 2500 2500\n");
500 greg 2.16 printf("\nbright source light\n0\n0\n4 0 0 1 2\n");
501     printf("\n# Material used for labels\n");
502     printf("\nvoid trans vellum\n0\n0\n7 1 1 1 0 0 .5 0\n");
503     }
504    
505     /* Put out hemisphere(s) */
506     static void
507     put_hemispheres(void)
508     {
509 greg 2.21 const int nsegs = 131;
510    
511 greg 2.16 printf("\n# Hemisphere(s) for showing BSDF appearance (if XML file)\n");
512     if (front_comp) {
513 greg 2.21 printf(
514     "\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",
515 greg 2.24 sph_fmat, nsegs, sph_rad, 0.495*PI, sph_xoffset);
516 greg 2.16 printf("\nvoid brighttext front_text\n3 helvet.fnt . FRONT\n0\n");
517     printf("12\n\t%f %f 0\n\t%f 0 0\n\t0 %f 0\n\t.01 1 -.1\n",
518 greg 2.20 -.22*sph_rad + sph_xoffset, -1.4*sph_rad,
519 greg 2.16 .35/5.*sph_rad, -1.6*.35/5.*sph_rad);
520     printf("\nfront_text alias front_label_mat vellum\n");
521     printf("\nfront_label_mat polygon front_label\n0\n0\n12");
522     printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
523 greg 2.20 -.25*sph_rad + sph_xoffset, -1.3*sph_rad,
524     -.25*sph_rad + sph_xoffset, (-1.4-1.6*.35/5.-.1)*sph_rad,
525     .25*sph_rad + sph_xoffset, (-1.4-1.6*.35/5.-.1)*sph_rad,
526     .25*sph_rad + sph_xoffset, -1.3*sph_rad );
527 greg 2.16 }
528     if (back_comp) {
529 greg 2.21 printf(
530     "\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",
531 greg 2.24 sph_bmat, nsegs, sph_rad, 0.495*PI, -sph_xoffset);
532 greg 2.16 printf("\nvoid brighttext back_text\n3 helvet.fnt . BACK\n0\n");
533     printf("12\n\t%f %f 0\n\t%f 0 0\n\t0 %f 0\n\t.01 1 -.1\n",
534 greg 2.20 -.22*sph_rad - sph_xoffset, -1.4*sph_rad,
535 greg 2.16 .35/4.*sph_rad, -1.6*.35/4.*sph_rad);
536     printf("\nback_text alias back_label_mat vellum\n");
537     printf("\nback_label_mat polygon back_label\n0\n0\n12");
538     printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
539 greg 2.20 -.25*sph_rad - sph_xoffset, -1.3*sph_rad,
540     -.25*sph_rad - sph_xoffset, (-1.4-1.6*.35/4.-.1)*sph_rad,
541     .25*sph_rad - sph_xoffset, (-1.4-1.6*.35/4.-.1)*sph_rad,
542     .25*sph_rad - sph_xoffset, -1.3*sph_rad );
543 greg 2.16 }
544     }
545    
546     /* Put out falsecolor scale and name label */
547     static void
548     put_scale(void)
549     {
550     const double max_log10 = log10(overall_max);
551 greg 2.20 const double leg_width = 2.*.75*(fabs(sph_xoffset) - sph_rad);
552 greg 2.16 const double leg_height = 2.*sph_rad;
553     const int text_lines = 6;
554 greg 2.17 const int text_digits = 8;
555 greg 2.16 char fmt[16];
556     int i;
557    
558     printf("\n# BSDF legend with falsecolor scale\n");
559     printf("\nvoid colorfunc lscale\n10 sca_red(Py) sca_grn(Py) sca_blu(Py)");
560     printf("\n\tbsdf2rad.cal -s %f -t 0 %f 0\n0\n0\n", leg_height, -.5*leg_height);
561 greg 2.17 sprintf(fmt, "%%.%df", text_digits-3);
562 greg 2.16 for (i = 0; i < text_lines; i++) {
563     char vbuf[16];
564     sprintf(vbuf, fmt, pow(10., (i+.5)/text_lines*(max_log10-min_log10)+min_log10));
565     printf("\nlscale brighttext lscale\n");
566     printf("3 helvet.fnt . %s\n0\n12\n", vbuf);
567     printf("\t%f %f 0\n", -.45*leg_width, ((i+.9)/text_lines-.5)*leg_height);
568 greg 2.17 printf("\t%f 0 0\n", .8*leg_width/strlen(vbuf));
569 greg 2.16 printf("\t0 %f 0\n", -.9/text_lines*leg_height);
570     printf("\t.01 1 -.1\n");
571     }
572     printf("\nlscale alias legend_mat vellum\n");
573     printf("\nlegend_mat polygon legend\n0\n0\n12");
574     printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
575     -.5*leg_width, .5*leg_height,
576     -.5*leg_width, -.5*leg_height,
577     .5*leg_width, -.5*leg_height,
578     .5*leg_width, .5*leg_height);
579     printf("\nvoid brighttext BSDFtitle\n3 helvet.fnt . BSDF\n0\n12\n");
580     printf("\t%f %f 0\n", -.25*leg_width, .7*leg_height);
581     printf("\t%f 0 0\n", .4/4.*leg_width);
582     printf("\t0 %f 0\n", -.1*leg_height);
583     printf("\t.01 1 -.1\n");
584     printf("\nBSDFtitle alias title_mat vellum\n");
585     printf("\ntitle_mat polygon title\n0\n0\n12");
586     printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
587     -.3*leg_width, .75*leg_height,
588     -.3*leg_width, .55*leg_height,
589     .3*leg_width, .55*leg_height,
590     .3*leg_width, .75*leg_height);
591     if (!bsdf_name[0])
592     return;
593     printf("\nvoid brighttext BSDFname\n3 helvet.fnt . \"%s\"\n0\n12\n", bsdf_name);
594     printf("\t%f %f 0\n", -.95*leg_width, -.6*leg_height);
595 greg 2.18 printf("\t%f 0 0\n", 1.8/strlen(bsdf_name)*leg_width);
596 greg 2.16 printf("\t0 %f 0\n", -.1*leg_height);
597     printf("\t.01 1 -.1\n");
598     printf("\nBSDFname alias name_mat vellum\n");
599     printf("\nname_mat polygon name\n0\n0\n12");
600     printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
601     -leg_width, -.55*leg_height,
602     -leg_width, -.75*leg_height,
603     leg_width, -.75*leg_height,
604     leg_width, -.55*leg_height);
605     }
606    
607     /* Convert MGF to Radiance in output */
608     static void
609     convert_mgf(const char *mgfdata)
610     {
611     int len = strlen(mgfdata);
612     char mgfn[128];
613     char radfn[128];
614     char cmdbuf[256];
615     float xmin, xmax, ymin, ymax, zmin, zmax;
616     double max_dim;
617     int fd;
618     FILE *fp;
619    
620     if (!len) return;
621     strcpy(mgfn, tfile_name("geom", ".mgf", 0));
622     fd = open(mgfn, O_WRONLY|O_CREAT, 0666);
623     if (fd < 0 || write(fd, mgfdata, len) != len) {
624     fprintf(stderr, "%s: cannot write file '%s'\n",
625     progname, mgfn);
626     return;
627     }
628     close(fd);
629     strcpy(radfn, tfile_name("geom", ".rad", 0));
630     sprintf(cmdbuf, "mgf2rad %s > %s", mgfn, radfn);
631     if (!run_cmd(cmdbuf))
632     return;
633     sprintf(cmdbuf, "getbbox -w -h %s", radfn);
634     if ((fp = popen(cmdbuf, "r")) == NULL ||
635     fscanf(fp, "%f %f %f %f %f %f",
636     &xmin, &xmax, &ymin, &ymax, &zmin, &zmax) != 6
637     || pclose(fp) < 0) {
638     fprintf(stderr, "%s: error reading from command: %s\n",
639     progname, cmdbuf);
640     return;
641     }
642     max_dim = ymax - ymin;
643     if (xmax - xmin > max_dim)
644     max_dim = xmax - xmin;
645     if (front_comp) {
646     printf("\n# BSDF system geometry (front view)\n");
647     sprintf(cmdbuf, "xform -t %f %f %f -s %f -t %f %f 0 %s",
648     -.5*(xmin+xmax), -.5*(ymin+ymax), -zmax,
649     1.5*sph_rad/max_dim,
650 greg 2.20 sph_xoffset, -2.5*sph_rad,
651 greg 2.16 radfn);
652     if (!run_cmd(cmdbuf))
653     return;
654     }
655     if (back_comp) {
656     printf("\n# BSDF system geometry (back view)\n");
657     sprintf(cmdbuf, "xform -t %f %f %f -s %f -ry 180 -t %f %f 0 %s",
658     -.5*(xmin+xmax), -.5*(ymin+ymax), -zmin,
659     1.5*sph_rad/max_dim,
660 greg 2.20 -sph_xoffset, -2.5*sph_rad,
661 greg 2.16 radfn);
662     if (!run_cmd(cmdbuf))
663     return;
664     }
665     }
666    
667     /* Check RBF input header line & get minimum BSDF value */
668     static int
669     rbf_headline(char *s, void *p)
670     {
671     char fmt[64];
672    
673     if (formatval(fmt, s)) {
674     if (strcmp(fmt, BSDFREP_FMT))
675     return(-1);
676     return(0);
677     }
678     if (!strncmp(s, "IO_SIDES=", 9)) {
679     sscanf(s+9, "%d %d", &input_orient, &output_orient);
680     if (input_orient == output_orient) {
681     if (input_orient > 0)
682     front_comp |= SDsampR;
683     else
684     back_comp |= SDsampR;
685     } else if (input_orient > 0)
686     front_comp |= SDsampT;
687     else
688     back_comp |= SDsampT;
689     return(0);
690     }
691     if (!strncmp(s, "BSDFMIN=", 8)) {
692     sscanf(s+8, "%lf", &bsdf_min);
693     if (bsdf_min < overall_min)
694     overall_min = bsdf_min;
695     return(0);
696     }
697     return(0);
698     }
699 greg 2.1
700 greg 2.16 /* Produce a Radiance model plotting the given BSDF representation */
701 greg 2.1 int
702     main(int argc, char *argv[])
703     {
704 greg 2.4 int inpXML = -1;
705     SDData myBSDF;
706 greg 2.16 int n;
707 greg 2.4 /* check arguments */
708 greg 2.1 progname = argv[0];
709 greg 2.16 if (argc > 1 && (n = strlen(argv[1])-4) > 0) {
710 greg 2.4 if (!strcasecmp(argv[1]+n, ".xml"))
711     inpXML = 1;
712     else if (!strcasecmp(argv[1]+n, ".sir"))
713     inpXML = 0;
714     }
715 greg 2.16 if (inpXML < 0 || inpXML & (argc > 2)) {
716     fprintf(stderr, "Usage: %s bsdf.xml > output.rad\n", progname);
717     fprintf(stderr, " Or: %s hemi1.sir hemi2.sir .. > output.rad\n", progname);
718 greg 2.1 return(1);
719     }
720 greg 2.16 fputs("# ", stdout); /* copy our command */
721     printargs(argc, argv, stdout);
722     /* evaluate BSDF */
723 greg 2.4 if (inpXML) {
724     SDclearBSDF(&myBSDF, argv[1]);
725     if (SDreportError(SDloadFile(&myBSDF, argv[1]), stderr))
726     return(1);
727 greg 2.16 if (myBSDF.rf != NULL) front_comp |= SDsampR;
728     if (myBSDF.tf != NULL) front_comp |= SDsampT;
729     if (myBSDF.rb != NULL) back_comp |= SDsampR;
730     if (myBSDF.tb != NULL) back_comp |= SDsampT;
731     if (!front_comp & !back_comp) {
732     fprintf(stderr, "%s: nothing to plot in '%s'\n",
733 greg 2.4 progname, argv[1]);
734     return(1);
735     }
736 greg 2.16 if (front_comp & SDsampR && myBSDF.rLambFront.cieY < overall_min*PI)
737     overall_min = myBSDF.rLambFront.cieY/PI;
738     if (back_comp & SDsampR && myBSDF.rLambBack.cieY < overall_min*PI)
739     overall_min = myBSDF.rLambBack.cieY/PI;
740     if ((front_comp|back_comp) & SDsampT &&
741     myBSDF.tLamb.cieY < overall_min*PI)
742     overall_min = myBSDF.tLamb.cieY/PI;
743     set_minlog();
744     if (!build_wBSDF(&myBSDF))
745 greg 2.9 return(1);
746 greg 2.16 if (myBSDF.matn[0])
747     strcpy(bsdf_name, myBSDF.matn);
748 greg 2.1 else
749 greg 2.16 strcpy(bsdf_name, myBSDF.name);
750     strcpy(bsdf_manuf, myBSDF.makr);
751     put_matBSDF(argv[1]);
752     } else {
753 greg 2.27 FILE *fp[4];
754     if (argc > 5) {
755 greg 2.28 fprintf(stderr, "%s: more than 4 hemispheres!\n", progname);
756 greg 2.27 return(1);
757     }
758 greg 2.16 for (n = 1; n < argc; n++) {
759 greg 2.27 fp[n-1] = fopen(argv[n], "rb");
760     if (fp[n-1] == NULL) {
761 greg 2.16 fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
762     progname, argv[n]);
763     return(1);
764     }
765 greg 2.27 if (getheader(fp[n-1], rbf_headline, NULL) < 0) {
766 greg 2.16 fprintf(stderr, "%s: bad BSDF interpolant '%s'\n",
767     progname, argv[n]);
768     return(1);
769 greg 2.2 }
770     }
771 greg 2.16 set_minlog();
772     for (n = 1; n < argc; n++) {
773 greg 2.27 if (fseek(fp[n-1], 0L, SEEK_SET) < 0) {
774     fprintf(stderr, "%s: cannot seek on '%s'\n",
775     progname, argv[n]);
776     return(1);
777     }
778     if (!load_bsdf_rep(fp[n-1]))
779 greg 2.16 return(1);
780 greg 2.27 fclose(fp[n-1]);
781 greg 2.16 if (!build_wRBF())
782     return(1);
783 greg 2.2 }
784 greg 2.16 put_matBSDF(NULL);
785 greg 2.1 }
786 greg 2.16 put_source(); /* before hemispheres & labels */
787     put_hemispheres();
788     put_scale();
789     if (inpXML && myBSDF.mgf)
790     convert_mgf(myBSDF.mgf);
791 greg 2.28 if (!put_BSDFs()) /* most of the output happens here */
792 greg 2.16 return(1);
793     cleanup_tmp();
794 greg 2.1 return(0);
795     }