ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2rad.c
(Generate patch)

Comparing ray/src/cv/bsdf2rad.c (file contents):
Revision 2.1 by greg, Tue Oct 22 04:29:27 2013 UTC vs.
Revision 2.23 by greg, Tue Apr 11 03:47:23 2017 UTC

# Line 2 | Line 2
2   static const char RCSid[] = "$Id$";
3   #endif
4   /*
5 < *  Plot 3-D BSDF output based on scattering interpolant representation
5 > *  Plot 3-D BSDF output based on scattering interpolant or XML representation
6   */
7  
8 #define _USE_MATH_DEFINES
8   #include <stdio.h>
9 + #include <string.h>
10   #include <stdlib.h>
11 < #include <math.h>
11 > #include "paths.h"
12 > #include "rtmath.h"
13 > #include "resolu.h"
14   #include "bsdfrep.h"
15  
16 < const float     colarr[6][3] = {
15 <                .7, 1., .7,
16 <                1., .7, .7,
17 <                .7, .7, 1.,
18 <                1., .5, 1.,
19 <                1., 1., .5,
20 <                .5, 1., 1.
21 <        };
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 < /* Produce a Radiance model plotting the indicated incident direction(s) */
49 < int
50 < main(int argc, char *argv[])
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.25 .\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 +                        if (SDcompXform(vMtx, ivec, upv) != SDEnone)
405 +                                continue;
406 +                        nxfa = addrot(xfargs, vMtx[0], vMtx[1], vMtx[2]);
407 +                        sprintf(xfargs+strlen(xfargs), " -s %f -t %f %f %f",
408 +                                        scalef, sorg[0], sorg[1], sorg[2]);
409 +                        nxfa += 6;
410 +                        fname = tfile_name(frpref, dsuffix, i);
411 +                        sprintf(cmdbuf, "gensurf scale_mat %s%d %s %s %s %d %d | xform %s",
412 +                                        frpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
413 +                                        xfargs);
414 +                        if (!run_cmd(cmdbuf))
415 +                                return(0);
416 +                }
417 +        if (front_comp & SDsampT)                       /* front transmission */
418 +                for (i = 0; i < NINCIDENT; i++) {
419 +                        get_ivector(ivec, i);
420 +                        put_trans_arrow(ivec, 1);
421 +                        cvt_sposition(sorg, ivec, 1);
422 +                        ivec[0] = -ivec[0]; ivec[1] = -ivec[1]; /* normal */
423 +                        upv[0] = ivec[0]*ivec[1]*(ivec[2] - 1.);
424 +                        upv[1] = ivec[0]*ivec[0] + ivec[1]*ivec[1]*ivec[2];
425 +                        upv[2] = -ivec[1]*(ivec[0]*ivec[0] + ivec[1]*ivec[1]);
426 +                        sprintf(xfargs, "-s %f -t %f %f %f", bsdf_rad,
427 +                                        sorg[0], sorg[1], sorg[2]);
428 +                        nxfa = 6;
429 +                        printf("\nvoid colorfunc scale_pat\n");
430 +                        printf("%d bsdf_red bsdf_grn bsdf_blu bsdf2rad.cal\n\t%s\n0\n0\n",
431 +                                        4+nxfa, xfargs);
432 +                        printf("\nscale_pat glow scale_mat\n0\n0\n4 1 1 1 0\n");
433 +                        if (SDcompXform(vMtx, ivec, upv) != SDEnone)
434 +                                continue;
435 +                        nxfa = addrot(xfargs, vMtx[0], vMtx[1], vMtx[2]);
436 +                        sprintf(xfargs+strlen(xfargs), " -s %f -t %f %f %f",
437 +                                        scalef, sorg[0], sorg[1], sorg[2]);
438 +                        nxfa += 6;
439 +                        fname = tfile_name(ftpref, dsuffix, i);
440 +                        sprintf(cmdbuf, "gensurf scale_mat %s%d %s %s %s %d %d | xform -I %s",
441 +                                        ftpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
442 +                                        xfargs);
443 +                        if (!run_cmd(cmdbuf))
444 +                                return(0);
445 +                }
446 +        if (back_comp & SDsampR)                        /* rear reflection */
447 +                for (i = 0; i < NINCIDENT; i++) {
448 +                        get_ivector(ivec, i);
449 +                        put_mirror_arrow(ivec, -1);
450 +                        cvt_sposition(sorg, ivec, -1);
451 +                        ivec[0] = -ivec[0]; ivec[1] = -ivec[1]; /* normal */
452 +                        upv[0] = ivec[0]*ivec[1]*(ivec[2] - 1.);
453 +                        upv[1] = ivec[0]*ivec[0] + ivec[1]*ivec[1]*ivec[2];
454 +                        upv[2] = -ivec[1]*(ivec[0]*ivec[0] + ivec[1]*ivec[1]);
455 +                        sprintf(xfargs, "-s %f -t %f %f %f", bsdf_rad,
456 +                                        sorg[0], sorg[1], sorg[2]);
457 +                        nxfa = 6;
458 +                        printf("\nvoid colorfunc scale_pat\n");
459 +                        printf("%d bsdf_red bsdf_grn bsdf_blu bsdf2rad.cal\n\t%s\n0\n0\n",
460 +                                        4+nxfa, xfargs);
461 +                        printf("\nscale_pat glow scale_mat\n0\n0\n4 1 1 1 0\n");
462 +                        if (SDcompXform(vMtx, ivec, upv) != SDEnone)
463 +                                continue;
464 +                        nxfa = addrot(xfargs, vMtx[0], vMtx[1], vMtx[2]);
465 +                        sprintf(xfargs+strlen(xfargs), " -s %f -t %f %f %f",
466 +                                        scalef, sorg[0], sorg[1], sorg[2]);
467 +                        nxfa += 6;
468 +                        fname = tfile_name(brpref, dsuffix, i);
469 +                        sprintf(cmdbuf, "gensurf scale_mat %s%d %s %s %s %d %d | xform -I -ry 180 %s",
470 +                                        brpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
471 +                                        xfargs);
472 +                        if (!run_cmd(cmdbuf))
473 +                                return(0);
474 +                }
475 +        if (back_comp & SDsampT)                        /* rear transmission */
476 +                for (i = 0; i < NINCIDENT; i++) {
477 +                        get_ivector(ivec, i);
478 +                        put_trans_arrow(ivec, -1);
479 +                        cvt_sposition(sorg, ivec, -1);
480 +                        ivec[0] = -ivec[0]; ivec[1] = -ivec[1]; /* normal */
481 +                        upv[0] = ivec[0]*ivec[1]*(ivec[2] - 1.);
482 +                        upv[1] = ivec[0]*ivec[0] + ivec[1]*ivec[1]*ivec[2];
483 +                        upv[2] = -ivec[1]*(ivec[0]*ivec[0] + ivec[1]*ivec[1]);
484 +                        sprintf(xfargs, "-s %f -t %f %f %f", bsdf_rad,
485 +                                        sorg[0], sorg[1], sorg[2]);
486 +                        nxfa = 6;
487 +                        printf("\nvoid colorfunc scale_pat\n");
488 +                        printf("%d bsdf_red bsdf_grn bsdf_blu bsdf2rad.cal\n\t%s\n0\n0\n",
489 +                                        4+nxfa, xfargs);
490 +                        printf("\nscale_pat glow scale_mat\n0\n0\n4 1 1 1 0\n");
491 +                        if (SDcompXform(vMtx, ivec, upv) != SDEnone)
492 +                                continue;
493 +                        nxfa = addrot(xfargs, vMtx[0], vMtx[1], vMtx[2]);
494 +                        sprintf(xfargs+strlen(xfargs), " -s %f -t %f %f %f",
495 +                                        scalef, sorg[0], sorg[1], sorg[2]);
496 +                        nxfa += 6;
497 +                        fname = tfile_name(btpref, dsuffix, i);
498 +                        sprintf(cmdbuf, "gensurf scale_mat %s%d %s %s %s %d %d | xform -ry 180 %s",
499 +                                        btpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
500 +                                        xfargs);
501 +                        if (!run_cmd(cmdbuf))
502 +                                return(0);
503 +                }
504 +        return(1);
505 + }
506 +
507 + /* Put our hemisphere material */
508 + static void
509 + put_matBSDF(const char *XMLfile)
510 + {
511 +        const char      *curdir = "./";
512 +
513 +        if (!XMLfile) {                 /* simple material */
514 +                printf("\n# Simplified material because we have no XML input\n");
515 +                printf("\nvoid brightfunc latlong\n2 latlong bsdf2rad.cal\n0\n0\n");
516 +                if ((front_comp|back_comp) & SDsampT)
517 +                        printf("\nlatlong trans %s\n0\n0\n7 .75 .75 .75 0 .04 .5 .8\n",
518 +                                        sph_mat);
519 +                else
520 +                        printf("\nlatlong plastic %s\n0\n0\n5 .5 .5 .5 0 0\n",
521 +                                        sph_mat);
522 +                return;
523 +        }
524 +        switch (XMLfile[0]) {           /* avoid RAYPATH search */
525 +        case '.':
526 +        CASEDIRSEP:
527 +                curdir = "";
528 +                break;
529 +        case '\0':
530 +                fprintf(stderr, "%s: empty file name in put_matBSDF\n", progname);
531 +                exit(1);
532 +                break;
533 +        }
534 +        printf("\n# Actual BSDF material for rendering the hemispheres\n");
535 +        printf("\nvoid BSDF BSDFmat\n6 0 \"%s%s\" upx upy upz bsdf2rad.cal\n0\n0\n",
536 +                        curdir, XMLfile);
537 +        printf("\nvoid plastic black\n0\n0\n5 0 0 0 0 0\n");
538 +        printf("\nvoid mixfunc %s\n4 BSDFmat black latlong bsdf2rad.cal\n0\n0\n",
539 +                        sph_mat);
540 + }
541 +
542 + /* Put out overhead parallel light source */
543 + static void
544 + put_source(void)
545 + {
546 +        printf("\n# Overhead parallel light source\n");
547 +        printf("\nvoid light bright\n0\n0\n3 2500 2500 2500\n");
548 +        printf("\nbright source light\n0\n0\n4 0 0 1 2\n");
549 +        printf("\n# Material used for labels\n");
550 +        printf("\nvoid trans vellum\n0\n0\n7 1 1 1 0 0 .5 0\n");
551 + }
552 +
553 + /* Put out hemisphere(s) */
554 + static void
555 + put_hemispheres(void)
556 + {
557 +        const int       nsegs = 131;
558 +
559 +        printf("\n# Hemisphere(s) for showing BSDF appearance (if XML file)\n");
560 +        if (front_comp) {
561 +                printf(
562 + "\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",
563 +                                sph_mat, nsegs, sph_rad, 0.495*PI, sph_xoffset);
564 +                printf("\nvoid brighttext front_text\n3 helvet.fnt . FRONT\n0\n");
565 +                printf("12\n\t%f %f 0\n\t%f 0 0\n\t0 %f 0\n\t.01 1 -.1\n",
566 +                                -.22*sph_rad + sph_xoffset, -1.4*sph_rad,
567 +                                .35/5.*sph_rad, -1.6*.35/5.*sph_rad);
568 +                printf("\nfront_text alias front_label_mat vellum\n");
569 +                printf("\nfront_label_mat polygon front_label\n0\n0\n12");
570 +                printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
571 +                                -.25*sph_rad + sph_xoffset, -1.3*sph_rad,
572 +                                -.25*sph_rad + sph_xoffset, (-1.4-1.6*.35/5.-.1)*sph_rad,
573 +                                .25*sph_rad + sph_xoffset, (-1.4-1.6*.35/5.-.1)*sph_rad,
574 +                                .25*sph_rad + sph_xoffset, -1.3*sph_rad );
575 +        }
576 +        if (back_comp) {
577 +                printf(
578 + "\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",
579 +                                sph_mat, nsegs, sph_rad, 0.495*PI, -sph_xoffset);
580 +                printf("\nvoid brighttext back_text\n3 helvet.fnt . BACK\n0\n");
581 +                printf("12\n\t%f %f 0\n\t%f 0 0\n\t0 %f 0\n\t.01 1 -.1\n",
582 +                                -.22*sph_rad - sph_xoffset, -1.4*sph_rad,
583 +                                .35/4.*sph_rad, -1.6*.35/4.*sph_rad);
584 +                printf("\nback_text alias back_label_mat vellum\n");
585 +                printf("\nback_label_mat polygon back_label\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 +                                -.25*sph_rad - sph_xoffset, -1.3*sph_rad,
588 +                                -.25*sph_rad - sph_xoffset, (-1.4-1.6*.35/4.-.1)*sph_rad,
589 +                                .25*sph_rad - sph_xoffset, (-1.4-1.6*.35/4.-.1)*sph_rad,
590 +                                .25*sph_rad - sph_xoffset, -1.3*sph_rad );
591 +        }
592 + }
593 +
594 + /* Put out falsecolor scale and name label */
595 + static void
596 + put_scale(void)
597 + {
598 +        const double    max_log10 = log10(overall_max);
599 +        const double    leg_width = 2.*.75*(fabs(sph_xoffset) - sph_rad);
600 +        const double    leg_height = 2.*sph_rad;
601 +        const int       text_lines = 6;
602 +        const int       text_digits = 8;
603 +        char            fmt[16];
604 +        int             i;
605 +
606 +        printf("\n# BSDF legend with falsecolor scale\n");
607 +        printf("\nvoid colorfunc lscale\n10 sca_red(Py) sca_grn(Py) sca_blu(Py)");
608 +        printf("\n\tbsdf2rad.cal -s %f -t 0 %f 0\n0\n0\n", leg_height, -.5*leg_height);
609 +        sprintf(fmt, "%%.%df", text_digits-3);
610 +        for (i = 0; i < text_lines; i++) {
611 +                char    vbuf[16];
612 +                sprintf(vbuf, fmt, pow(10., (i+.5)/text_lines*(max_log10-min_log10)+min_log10));
613 +                printf("\nlscale brighttext lscale\n");
614 +                printf("3 helvet.fnt . %s\n0\n12\n", vbuf);
615 +                printf("\t%f %f 0\n", -.45*leg_width, ((i+.9)/text_lines-.5)*leg_height);
616 +                printf("\t%f 0 0\n", .8*leg_width/strlen(vbuf));
617 +                printf("\t0 %f 0\n", -.9/text_lines*leg_height);
618 +                printf("\t.01 1 -.1\n");
619 +        }
620 +        printf("\nlscale alias legend_mat vellum\n");
621 +        printf("\nlegend_mat polygon legend\n0\n0\n12");
622 +        printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
623 +                        -.5*leg_width, .5*leg_height,
624 +                        -.5*leg_width, -.5*leg_height,
625 +                        .5*leg_width, -.5*leg_height,
626 +                        .5*leg_width, .5*leg_height);
627 +        printf("\nvoid brighttext BSDFtitle\n3 helvet.fnt . BSDF\n0\n12\n");
628 +        printf("\t%f %f 0\n", -.25*leg_width, .7*leg_height);
629 +        printf("\t%f 0 0\n", .4/4.*leg_width);
630 +        printf("\t0 %f 0\n", -.1*leg_height);
631 +        printf("\t.01 1 -.1\n");
632 +        printf("\nBSDFtitle alias title_mat vellum\n");
633 +        printf("\ntitle_mat polygon title\n0\n0\n12");
634 +        printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
635 +                        -.3*leg_width, .75*leg_height,
636 +                        -.3*leg_width, .55*leg_height,
637 +                        .3*leg_width, .55*leg_height,
638 +                        .3*leg_width, .75*leg_height);
639 +        if (!bsdf_name[0])
640 +                return;
641 +        printf("\nvoid brighttext BSDFname\n3 helvet.fnt . \"%s\"\n0\n12\n", bsdf_name);
642 +        printf("\t%f %f 0\n", -.95*leg_width, -.6*leg_height);
643 +        printf("\t%f 0 0\n", 1.8/strlen(bsdf_name)*leg_width);
644 +        printf("\t0 %f 0\n", -.1*leg_height);
645 +        printf("\t.01 1 -.1\n");
646 +        printf("\nBSDFname alias name_mat vellum\n");
647 +        printf("\nname_mat polygon name\n0\n0\n12");
648 +        printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
649 +                        -leg_width, -.55*leg_height,
650 +                        -leg_width, -.75*leg_height,
651 +                        leg_width, -.75*leg_height,
652 +                        leg_width, -.55*leg_height);
653 + }
654 +
655 + /* Convert MGF to Radiance in output */
656 + static void
657 + convert_mgf(const char *mgfdata)
658 + {
659 +        int     len = strlen(mgfdata);
660 +        char    mgfn[128];
661 +        char    radfn[128];
662 +        char    cmdbuf[256];
663 +        float   xmin, xmax, ymin, ymax, zmin, zmax;
664 +        double  max_dim;
665 +        int     fd;
666          FILE    *fp;
31        RBFNODE *rbf;
32        double  bsdf, min_log;
33        FVECT   dir;
34        int     i, j, n;
667  
668 +        if (!len) return;
669 +        strcpy(mgfn, tfile_name("geom", ".mgf", 0));
670 +        fd = open(mgfn, O_WRONLY|O_CREAT, 0666);
671 +        if (fd < 0 || write(fd, mgfdata, len) != len) {
672 +                fprintf(stderr, "%s: cannot write file '%s'\n",
673 +                                progname, mgfn);
674 +                return;
675 +        }
676 +        close(fd);
677 +        strcpy(radfn, tfile_name("geom", ".rad", 0));
678 +        sprintf(cmdbuf, "mgf2rad %s > %s", mgfn, radfn);
679 +        if (!run_cmd(cmdbuf))
680 +                return;
681 +        sprintf(cmdbuf, "getbbox -w -h %s", radfn);
682 +        if ((fp = popen(cmdbuf, "r")) == NULL ||
683 +                        fscanf(fp, "%f %f %f %f %f %f",
684 +                                &xmin, &xmax, &ymin, &ymax, &zmin, &zmax) != 6
685 +                        || pclose(fp) < 0) {
686 +                fprintf(stderr, "%s: error reading from command: %s\n",
687 +                                progname, cmdbuf);
688 +                return;
689 +        }
690 +        max_dim = ymax - ymin;
691 +        if (xmax - xmin > max_dim)
692 +                max_dim = xmax - xmin;
693 +        if (front_comp) {
694 +                printf("\n# BSDF system geometry (front view)\n");
695 +                sprintf(cmdbuf, "xform -t %f %f %f -s %f -t %f %f 0 %s",
696 +                                -.5*(xmin+xmax), -.5*(ymin+ymax), -zmax,
697 +                                1.5*sph_rad/max_dim,
698 +                                sph_xoffset, -2.5*sph_rad,
699 +                                radfn);
700 +                if (!run_cmd(cmdbuf))
701 +                        return;
702 +        }
703 +        if (back_comp) {
704 +                printf("\n# BSDF system geometry (back view)\n");
705 +                sprintf(cmdbuf, "xform -t %f %f %f -s %f -ry 180 -t %f %f 0 %s",
706 +                                -.5*(xmin+xmax), -.5*(ymin+ymax), -zmin,
707 +                                1.5*sph_rad/max_dim,
708 +                                -sph_xoffset, -2.5*sph_rad,
709 +                                radfn);
710 +                if (!run_cmd(cmdbuf))
711 +                        return;
712 +        }
713 + }
714 +
715 + /* Check RBF input header line & get minimum BSDF value */
716 + static int
717 + rbf_headline(char *s, void *p)
718 + {
719 +        char    fmt[64];
720 +
721 +        if (formatval(fmt, s)) {
722 +                if (strcmp(fmt, BSDFREP_FMT))
723 +                        return(-1);
724 +                return(0);
725 +        }
726 +        if (!strncmp(s, "IO_SIDES=", 9)) {
727 +                sscanf(s+9, "%d %d", &input_orient, &output_orient);
728 +                if (input_orient == output_orient) {
729 +                        if (input_orient > 0)
730 +                                front_comp |= SDsampR;
731 +                        else
732 +                                back_comp |= SDsampR;
733 +                } else if (input_orient > 0)
734 +                        front_comp |= SDsampT;
735 +                else
736 +                        back_comp |= SDsampT;
737 +                return(0);
738 +        }
739 +        if (!strncmp(s, "BSDFMIN=", 8)) {
740 +                sscanf(s+8, "%lf", &bsdf_min);
741 +                if (bsdf_min < overall_min)
742 +                        overall_min = bsdf_min;
743 +                return(0);
744 +        }
745 +        return(0);
746 + }
747 +
748 + /* Produce a Radiance model plotting the given BSDF representation */
749 + int
750 + main(int argc, char *argv[])
751 + {
752 +        int     inpXML = -1;
753 +        SDData  myBSDF;
754 +        int     n;
755 +                                                /* check arguments */
756          progname = argv[0];
757 <        if (argc < 4) {
758 <                fprintf(stderr, "Usage: %s bsdf.sir theta1 phi1 .. > output.rad\n", argv[0]);
759 <                return(1);
757 >        if (argc > 1 && (n = strlen(argv[1])-4) > 0) {
758 >                if (!strcasecmp(argv[1]+n, ".xml"))
759 >                        inpXML = 1;
760 >                else if (!strcasecmp(argv[1]+n, ".sir"))
761 >                        inpXML = 0;
762          }
763 <                                                /* load input */
764 <        if ((fp = fopen(argv[1], "rb")) == NULL) {
765 <                fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
44 <                                argv[0], argv[1]);
763 >        if (inpXML < 0 || inpXML & (argc > 2)) {
764 >                fprintf(stderr, "Usage: %s bsdf.xml > output.rad\n", progname);
765 >                fprintf(stderr, "   Or: %s hemi1.sir hemi2.sir .. > output.rad\n", progname);
766                  return(1);
767          }
768 <        if (!load_bsdf_rep(fp))
769 <                return(1);
770 <        fclose(fp);
771 <        min_log = log(bsdf_min*.5);
772 <                                                /* output surface(s) */
773 <        for (n = 0; (n < 6) & (2*n+3 < argc); n++) {
53 <                printf("void trans tmat\n0\n0\n7 %f %f %f .04 .04 .9 1\n",
54 <                                colarr[n][0], colarr[n][1], colarr[n][2]);
55 <                fflush(stdout);
56 <                sprintf(buf, "gensurf tmat bsdf - - - %d %d", GRIDRES-1, GRIDRES-1);
57 <                fp = popen(buf, "w");
58 <                if (fp == NULL) {
59 <                        fprintf(stderr, "%s: cannot open '| %s'\n", argv[0], buf);
768 >        fputs("# ", stdout);                    /* copy our command */
769 >        printargs(argc, argv, stdout);
770 >                                                /* evaluate BSDF */
771 >        if (inpXML) {
772 >                SDclearBSDF(&myBSDF, argv[1]);
773 >                if (SDreportError(SDloadFile(&myBSDF, argv[1]), stderr))
774                          return(1);
775 +                if (myBSDF.rf != NULL) front_comp |= SDsampR;
776 +                if (myBSDF.tf != NULL) front_comp |= SDsampT;
777 +                if (myBSDF.rb != NULL) back_comp |= SDsampR;
778 +                if (myBSDF.tb != NULL) back_comp |= SDsampT;
779 +                if (!front_comp & !back_comp) {
780 +                        fprintf(stderr, "%s: nothing to plot in '%s'\n",
781 +                                        progname, argv[1]);
782 +                        return(1);
783                  }
784 <                dir[2] = sin((M_PI/180.)*atof(argv[2*n+2]));
785 <                dir[0] = dir[2] * cos((M_PI/180.)*atof(argv[2*n+3]));
786 <                dir[1] = dir[2] * sin((M_PI/180.)*atof(argv[2*n+3]));
787 <                dir[2] = input_orient * sqrt(1. - dir[2]*dir[2]);
788 <                fprintf(stderr, "Computing DSF for incident direction (%.1f,%.1f)\n",
789 <                                get_theta180(dir), get_phi360(dir));
790 <                rbf = advect_rbf(dir, 15000);
791 <                if (rbf == NULL)
792 <                        fputs("NULL RBF\n", stderr);
71 <                else
72 <                        fprintf(stderr, "Hemispherical reflectance: %.3f\n", rbf->vtotal);
73 <                for (i = 0; i < GRIDRES; i++)
74 <                    for (j = 0; j < GRIDRES; j++) {
75 <                        ovec_from_pos(dir, i, j);
76 <                        bsdf = eval_rbfrep(rbf, dir) / (output_orient*dir[2]);
77 <                        bsdf = log(bsdf) - min_log;
78 <                        fprintf(fp, "%.8e %.8e %.8e\n",
79 <                                        dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf);
80 <                    }
81 <                if (rbf != NULL)
82 <                        free(rbf);
83 <                if (pclose(fp))
784 >                if (front_comp & SDsampR && myBSDF.rLambFront.cieY < overall_min*PI)
785 >                        overall_min = myBSDF.rLambFront.cieY/PI;
786 >                if (back_comp & SDsampR && myBSDF.rLambBack.cieY < overall_min*PI)
787 >                        overall_min = myBSDF.rLambBack.cieY/PI;
788 >                if ((front_comp|back_comp) & SDsampT &&
789 >                                myBSDF.tLamb.cieY < overall_min*PI)
790 >                        overall_min = myBSDF.tLamb.cieY/PI;
791 >                set_minlog();
792 >                if (!build_wBSDF(&myBSDF))
793                          return(1);
794 +                if (myBSDF.matn[0])
795 +                        strcpy(bsdf_name, myBSDF.matn);
796 +                else
797 +                        strcpy(bsdf_name, myBSDF.name);
798 +                strcpy(bsdf_manuf, myBSDF.makr);
799 +                put_matBSDF(argv[1]);
800 +        } else {
801 +                FILE    *fp;
802 +                for (n = 1; n < argc; n++) {
803 +                        fp = fopen(argv[n], "rb");
804 +                        if (fp == NULL) {
805 +                                fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
806 +                                                progname, argv[n]);
807 +                                return(1);
808 +                        }
809 +                        if (getheader(fp, rbf_headline, NULL) < 0) {
810 +                                fprintf(stderr, "%s: bad BSDF interpolant '%s'\n",
811 +                                                progname, argv[n]);
812 +                                return(1);
813 +                        }
814 +                        fclose(fp);
815 +                }
816 +                set_minlog();
817 +                for (n = 1; n < argc; n++) {
818 +                        fp = fopen(argv[n], "rb");
819 +                        if (!load_bsdf_rep(fp))
820 +                                return(1);
821 +                        fclose(fp);
822 +                        if (!build_wRBF())
823 +                                return(1);
824 +                }
825 +                put_matBSDF(NULL);
826          }
827 +        put_source();                   /* before hemispheres & labels */
828 +        put_hemispheres();
829 +        put_scale();
830 +        if (inpXML && myBSDF.mgf)
831 +                convert_mgf(myBSDF.mgf);
832 +        if (!put_BSDFs())
833 +                return(1);
834 +        cleanup_tmp();
835          return(0);
836   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines