ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2rad.c
Revision: 2.22
Committed: Tue Apr 11 02:21:37 2017 UTC (7 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.21: +19 -9 lines
Log Message:
Fixed issue with up vector that was misorienting 3-D plots

File Contents

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