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.13 by greg, Mon Sep 15 00:54:03 2014 UTC vs.
Revision 2.21 by greg, Mon Apr 10 15:44:19 2017 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines