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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines