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.39 by greg, Tue Dec 7 23:55:02 2021 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
9 #include <stdio.h>
10 #include <string.h>
8   #include <stdlib.h>
9 < #include <math.h>
10 < #include "rtprocess.h"
9 > #include "rtio.h"
10 > #include "paths.h"
11 > #include "rtmath.h"
12   #include "bsdfrep.h"
13  
14 < 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.
23 <        };
14 > #ifndef NINCIDENT
15 > #define NINCIDENT       37              /* number of samples/hemisphere */
16 > #endif
17 > #ifndef GRIDSTEP
18 > #define GRIDSTEP        2               /* our grid step size */
19 > #endif
20 > #define SAMPRES         (GRIDRES/GRIDSTEP)
21  
22 < #ifdef _WIN32
23 < char    validf[] = "-e \"valid(s,t)=X`SYS(s,t)^2+Y`SYS(s,t)^2+Z`SYS(s,t)^2-1e-7\"";
22 > int     front_comp = 0;                 /* front component flags (SDsamp*) */
23 > int     back_comp = 0;                  /* back component flags */
24 > double  overall_min = 1./PI;            /* overall minimum BSDF value */
25 > double  min_log10;                      /* smallest log10 value for plotting */
26 > double  overall_max = .0;               /* overall maximum BSDF value */
27 >
28 > char    ourTempDir[TEMPLEN+1] = "";     /* our temporary directory */
29 >
30 > const char      frpref[] = "rf";
31 > const char      ftpref[] = "tf";
32 > const char      brpref[] = "rb";
33 > const char      btpref[] = "tb";
34 > const char      dsuffix[] = ".txt";
35 >
36 > const char      sph_fmat[] = "fBSDFmat";
37 > const char      sph_bmat[] = "bBSDFmat";
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 set_minlog()    overall_min = (overall_min < 1e-5) ? 1e-5 : overall_min; \
45 >                                min_log10 = log10(overall_min) - .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[256];
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, ivec,
148 >                                                ovec, 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 >        static int      cnt = 1;
278 >        FVECT           refl;
279 >        int             i;
280 >
281 >        refl[0] = 2.*nrm[2]*nrm[0];
282 >        refl[1] = 2.*nrm[2]*nrm[1];
283 >        refl[2] = 2.*nrm[2]*nrm[2] - 1.;
284 >
285 >        printf("\n# Mirror arrow #%d\n", cnt);
286 >        printf("\nshaft_mat cylinder inc_dir%d\n0\n0\n7", cnt);
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("\nshaft_mat cylinder mir_dir%d\n0\n0\n7", cnt);
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("\ntip_mat cone mir_tip%d\n0\n0\n8", cnt);
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 >        ++cnt;
308 > }
309 >
310 > /* Put out transmitted direction arrow for the given incident vector */
311 > static void
312 > put_trans_arrow(const FVECT origin)
313 > {
314 >        const double    arrow_len = 1.2*bsdf_rad;
315 >        const double    tip_len = 0.2*bsdf_rad;
316 >        static int      cnt = 1;
317 >        int             i;
318 >
319 >        printf("\n# Transmission arrow #%d\n", cnt);
320 >        printf("\nshaft_mat cylinder trans_dir%d\n0\n0\n7", cnt);
321 >        printf("\n\t%f %f %f\n\t%f %f %f\n\t%f\n",
322 >                        origin[0], origin[1], origin[2],
323 >                        origin[0], origin[1], origin[2]-arrow_len,
324 >                        arrow_rad);
325 >        printf("\ntip_mat cone trans_tip%d\n0\n0\n8", cnt);
326 >        printf("\n\t%f %f %f\n\t%f %f %f\n\t%f 0\n",
327 >                        origin[0], origin[1], origin[2]-arrow_len+.5*tip_len,
328 >                        origin[0], origin[1], origin[2]-arrow_len-.5*tip_len,
329 >                        2.*arrow_rad);
330 >        ++cnt;
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 (!FABSEQ(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 (!FABSEQ(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 (!FABSEQ(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, nrm, upv;
375 >        RREAL           vMtx[3][3];
376 >        char            *fname;
377 >        char            cmdbuf[256];
378 >        char            rotargs[64];
379 >        int             nrota;
380 >        int             i;
381 >
382 >        printf("\n# Gensurf output corresponding to %d incident directions\n",
383 >                        NINCIDENT);
384 >
385 >        printf("\nvoid glow tip_mat\n0\n0\n4 1 0 1 0\n");
386 >        printf("\nvoid mixfunc shaft_mat\n4 tip_mat void 0.25 .\n0\n0\n");
387 >
388 >        for (i = 0; i < NINCIDENT; i++) {
389 >                get_ivector(ivec, i);
390 >                nrm[0] = -ivec[0]; nrm[1] = -ivec[1]; nrm[2] = ivec[2];
391 >                upv[0] = nrm[0]*nrm[1]*(nrm[2] - 1.);
392 >                upv[1] = nrm[0]*nrm[0] + nrm[1]*nrm[1]*nrm[2];
393 >                upv[2] = -nrm[1]*(nrm[0]*nrm[0] + nrm[1]*nrm[1]);
394 >                if (SDcompXform(vMtx, nrm, upv) != SDEnone)
395 >                        continue;
396 >                nrota = addrot(rotargs, vMtx[0], vMtx[1], vMtx[2]);
397 >                if (front_comp) {
398 >                        cvt_sposition(sorg, ivec, 1);
399 >                        printf("\nvoid colorfunc scale_pat\n");
400 >                        printf("10 bsdf_red bsdf_grn bsdf_blu bsdf2rad.cal\n");
401 >                        printf("\t-s %f -t %f %f %f\n0\n0\n",
402 >                                        bsdf_rad, sorg[0], sorg[1], sorg[2]);
403 >                        printf("\nscale_pat glow scale_mat\n0\n0\n4 1 1 1 0\n");
404 >                }
405 >                if (front_comp & SDsampR) {
406 >                        put_mirror_arrow(sorg, nrm);
407 >                        fname = tfile_name(frpref, dsuffix, i);
408 >                        sprintf(cmdbuf,
409 >                "gensurf scale_mat %s%d %s %s %s %d %d | xform %s -s %f -t %f %f %f",
410 >                                        frpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
411 >                                        rotargs, scalef, sorg[0], sorg[1], sorg[2]);
412 >                        if (!run_cmd(cmdbuf))
413 >                                return(0);
414 >                }
415 >                if (front_comp & SDsampT) {
416 >                        put_trans_arrow(sorg);
417 >                        fname = tfile_name(ftpref, dsuffix, i);
418 >                        sprintf(cmdbuf,
419 >                "gensurf scale_mat %s%d %s %s %s %d %d | xform -I %s -s %f -t %f %f %f",
420 >                                        ftpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
421 >                                        rotargs, scalef, sorg[0], sorg[1], sorg[2]);
422 >                        if (!run_cmd(cmdbuf))
423 >                                return(0);
424 >                }
425 >                if (back_comp) {
426 >                        cvt_sposition(sorg, ivec, -1);
427 >                        printf("\nvoid colorfunc scale_pat\n");
428 >                        printf("10 bsdf_red bsdf_grn bsdf_blu bsdf2rad.cal\n");
429 >                        printf("\t-s %f -t %f %f %f\n0\n0\n",
430 >                                        bsdf_rad, sorg[0], sorg[1], sorg[2]);
431 >                        printf("\nscale_pat glow scale_mat\n0\n0\n4 1 1 1 0\n");
432 >                }
433 >                if (back_comp & SDsampR) {
434 >                        put_mirror_arrow(sorg, nrm);
435 >                        fname = tfile_name(brpref, dsuffix, i);
436 >                        sprintf(cmdbuf,
437 >                "gensurf scale_mat %s%d %s %s %s %d %d | xform -I -ry 180 %s -s %f -t %f %f %f",
438 >                                        brpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
439 >                                        rotargs, scalef, sorg[0], sorg[1], sorg[2]);
440 >                        if (!run_cmd(cmdbuf))
441 >                                return(0);
442 >                }
443 >                if (back_comp & SDsampT) {
444 >                        put_trans_arrow(sorg);
445 >                        fname = tfile_name(btpref, dsuffix, i);
446 >                        sprintf(cmdbuf,
447 >                "gensurf scale_mat %s%d %s %s %s %d %d | xform -ry 180 %s -s %f -t %f %f %f",
448 >                                        btpref, i, fname, fname, fname, SAMPRES-1, SAMPRES-1,
449 >                                        rotargs, scalef, sorg[0], sorg[1], sorg[2]);
450 >                        if (!run_cmd(cmdbuf))
451 >                                return(0);
452 >                }
453 >        }
454 >        return(1);
455 > }
456 >
457 > /* Put our hemisphere material */
458 > static void
459 > put_matBSDF(const char *XMLfile)
460 > {
461 >        const char      *curdir = "./";
462 >
463 >        if (!XMLfile) {                 /* simple material */
464 >                printf("\n# Simplified material because we have no XML input\n");
465 >                printf("\nvoid brightfunc latlong\n2 latlong bsdf2rad.cal\n0\n0\n");
466 >                if ((front_comp|back_comp) & SDsampT)
467 >                        printf("\nlatlong trans %s\n0\n0\n7 .75 .75 .75 0 .04 .5 .8\n",
468 >                                        sph_fmat);
469 >                else
470 >                        printf("\nlatlong plastic %s\n0\n0\n5 .5 .5 .5 0 0\n",
471 >                                        sph_fmat);
472 >                printf("\ninherit alias %s %s\n", sph_bmat, sph_fmat);
473 >                return;
474 >        }
475 >        switch (XMLfile[0]) {           /* avoid RAYPATH search */
476 >        case '.':
477 >        case '~':
478 >        CASEDIRSEP:
479 >                curdir = "";
480 >                break;
481 >        case '\0':
482 >                fprintf(stderr, "%s: empty file name in put_matBSDF\n", progname);
483 >                exit(1);
484 >                break;
485 >        }
486 >        printf("\n# Actual BSDF materials for rendering the hemispheres\n");
487 >        printf("\nvoid BSDF BSDF_f\n6 0 \"%s%s\" upx upy upz bsdf2rad.cal\n0\n0\n",
488 >                        curdir, XMLfile);
489 >        printf("\nvoid plastic black\n0\n0\n5 0 0 0 0 0\n");
490 >        printf("\nvoid mixfunc %s\n4 BSDF_f black latlong bsdf2rad.cal\n0\n0\n",
491 >                        sph_fmat);
492 >        printf("\nvoid BSDF BSDF_b\n8 0 \"%s%s\" upx upy upz bsdf2rad.cal -ry 180\n0\n0\n",
493 >                        curdir, XMLfile);
494 >        printf("\nvoid mixfunc %s\n4 BSDF_b black latlong bsdf2rad.cal\n0\n0\n",
495 >                        sph_bmat);
496 > }
497 >
498 > /* Put out overhead parallel light source */
499 > static void
500 > put_source(void)
501 > {
502 >        printf("\n# Overhead parallel light source\n");
503 >        printf("\nvoid light bright\n0\n0\n3 2500 2500 2500\n");
504 >        printf("\nbright source light\n0\n0\n4 0 0 1 2\n");
505 >        printf("\n# Material used for labels\n");
506 >        printf("\nvoid trans vellum\n0\n0\n7 1 1 1 0 0 .5 0\n");
507 > }
508 >
509 > /* Put out hemisphere(s) */
510 > static void
511 > put_hemispheres(void)
512 > {
513 >        const int       nsegs = 131;
514 >
515 >        printf("\n# Hemisphere(s) for showing BSDF appearance (if XML file)\n");
516 >        if (front_comp) {
517 >                printf(
518 > "\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",
519 >                                sph_fmat, nsegs, sph_rad, 0.5*PI, sph_xoffset);
520 >                printf("\nvoid brighttext front_text\n3 helvet.fnt . FRONT\n0\n");
521 >                printf("12\n\t%f %f 0\n\t%f 0 0\n\t0 %f 0\n\t.01 1 -.1\n",
522 >                                -.22*sph_rad + sph_xoffset, -1.4*sph_rad,
523 >                                .35/5.*sph_rad, -1.6*.35/5.*sph_rad);
524 >                printf("\nfront_text alias front_label_mat vellum\n");
525 >                printf("\nfront_label_mat polygon front_label\n0\n0\n12");
526 >                printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
527 >                                -.25*sph_rad + sph_xoffset, -1.3*sph_rad,
528 >                                -.25*sph_rad + sph_xoffset, (-1.4-1.6*.35/5.-.1)*sph_rad,
529 >                                .25*sph_rad + sph_xoffset, (-1.4-1.6*.35/5.-.1)*sph_rad,
530 >                                .25*sph_rad + sph_xoffset, -1.3*sph_rad );
531 >        }
532 >        if (back_comp) {
533 >                printf(
534 > "\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",
535 >                                sph_bmat, nsegs, sph_rad, 0.5*PI, -sph_xoffset);
536 >                printf("\nvoid brighttext back_text\n3 helvet.fnt . BACK\n0\n");
537 >                printf("12\n\t%f %f 0\n\t%f 0 0\n\t0 %f 0\n\t.01 1 -.1\n",
538 >                                -.22*sph_rad - sph_xoffset, -1.4*sph_rad,
539 >                                .35/4.*sph_rad, -1.6*.35/4.*sph_rad);
540 >                printf("\nback_text alias back_label_mat vellum\n");
541 >                printf("\nback_label_mat polygon back_label\n0\n0\n12");
542 >                printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
543 >                                -.25*sph_rad - sph_xoffset, -1.3*sph_rad,
544 >                                -.25*sph_rad - sph_xoffset, (-1.4-1.6*.35/4.-.1)*sph_rad,
545 >                                .25*sph_rad - sph_xoffset, (-1.4-1.6*.35/4.-.1)*sph_rad,
546 >                                .25*sph_rad - sph_xoffset, -1.3*sph_rad );
547 >        }
548 > }
549 >
550 > /* Put out falsecolor scale and name label */
551 > static void
552 > put_scale(void)
553 > {
554 >        const double    max_log10 = log10(overall_max);
555 >        const double    leg_width = 2.*.75*(fabs(sph_xoffset) - sph_rad);
556 >        const double    leg_height = 2.*sph_rad;
557 >        const int       text_lines = 6;
558 >        const int       text_digits = 8;
559 >        char            fmt[16];
560 >        int             i;
561 >
562 >        printf("\n# BSDF legend with falsecolor scale\n");
563 >        printf("\nvoid colorfunc lscale\n10 sca_red(Py) sca_grn(Py) sca_blu(Py)");
564 >        printf("\n\tbsdf2rad.cal -s %f -t 0 %f 0\n0\n0\n", leg_height, -.5*leg_height);
565 >        sprintf(fmt, "%%.%df", text_digits-3);
566 >        for (i = 0; i < text_lines; i++) {
567 >                char    vbuf[16];
568 >                sprintf(vbuf, fmt, pow(10., (i+.5)/text_lines*(max_log10-min_log10)+min_log10));
569 >                printf("\nlscale brighttext lscale\n");
570 >                printf("3 helvet.fnt . %s\n0\n12\n", vbuf);
571 >                printf("\t%f %f 0\n", -.45*leg_width, ((i+.9)/text_lines-.5)*leg_height);
572 >                printf("\t%f 0 0\n", .8*leg_width/strlen(vbuf));
573 >                printf("\t0 %f 0\n", -.9/text_lines*leg_height);
574 >                printf("\t.01 1 -.1\n");
575 >        }
576 >        printf("\nlscale alias legend_mat vellum\n");
577 >        printf("\nlegend_mat polygon legend\n0\n0\n12");
578 >        printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
579 >                        -.5*leg_width, .5*leg_height,
580 >                        -.5*leg_width, -.5*leg_height,
581 >                        .5*leg_width, -.5*leg_height,
582 >                        .5*leg_width, .5*leg_height);
583 >        printf("\nvoid brighttext BSDFtitle\n3 helvet.fnt . BSDF\n0\n12\n");
584 >        printf("\t%f %f 0\n", -.25*leg_width, .7*leg_height);
585 >        printf("\t%f 0 0\n", .4/4.*leg_width);
586 >        printf("\t0 %f 0\n", -.1*leg_height);
587 >        printf("\t.01 1 -.1\n");
588 >        printf("\nBSDFtitle alias title_mat vellum\n");
589 >        printf("\ntitle_mat polygon title\n0\n0\n12");
590 >        printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
591 >                        -.3*leg_width, .75*leg_height,
592 >                        -.3*leg_width, .55*leg_height,
593 >                        .3*leg_width, .55*leg_height,
594 >                        .3*leg_width, .75*leg_height);
595 >        if (!bsdf_name[0])
596 >                return;
597 >        printf("\nvoid brighttext BSDFname\n3 helvet.fnt . \"%s\"\n0\n12\n", bsdf_name);
598 >        printf("\t%f %f 0\n", -.95*leg_width, -.6*leg_height);
599 >        printf("\t%f 0 0\n", 1.8/strlen(bsdf_name)*leg_width);
600 >        printf("\t0 %f 0\n", -.1*leg_height);
601 >        printf("\t.01 1 -.1\n");
602 >        printf("\nBSDFname alias name_mat vellum\n");
603 >        printf("\nname_mat polygon name\n0\n0\n12");
604 >        printf("\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n\t%f %f 0\n",
605 >                        -leg_width, -.55*leg_height,
606 >                        -leg_width, -.75*leg_height,
607 >                        leg_width, -.75*leg_height,
608 >                        leg_width, -.55*leg_height);
609 > }
610 >
611 > /* Convert MGF to Radiance in output */
612 > static void
613 > convert_mgf(const char *mgfdata)
614 > {
615 >        int     len = strlen(mgfdata);
616 >        char    mgfn[128];
617 >        char    radfn[128];
618 >        char    cmdbuf[256];
619 >        float   xmin, xmax, ymin, ymax, zmin, zmax;
620 >        double  max_dim;
621 >        int     fd;
622 >        FILE    *fp;
623 >
624 >        if (!len) return;
625 >        strcpy(mgfn, tfile_name("geom", ".mgf", 0));
626 >        fd = open(mgfn, O_WRONLY|O_CREAT, 0666);
627 >        if (fd < 0 || write(fd, mgfdata, len) != len) {
628 >                fprintf(stderr, "%s: cannot write file '%s'\n",
629 >                                progname, mgfn);
630 >                return;
631 >        }
632 >        close(fd);
633 >        strcpy(radfn, tfile_name("geom", ".rad", 0));
634 >        sprintf(cmdbuf, "mgf2rad %s > %s", mgfn, radfn);
635 >        if (!run_cmd(cmdbuf))
636 >                return;
637 >        sprintf(cmdbuf, "getbbox -w -h %s", radfn);
638 >        if ((fp = popen(cmdbuf, "r")) == NULL ||
639 >                        fscanf(fp, "%f %f %f %f %f %f",
640 >                                &xmin, &xmax, &ymin, &ymax, &zmin, &zmax) != 6
641 >                        || pclose(fp) < 0) {
642 >                fprintf(stderr, "%s: error reading from command: %s\n",
643 >                                progname, cmdbuf);
644 >                return;
645 >        }
646 >        max_dim = ymax - ymin;
647 >        if (xmax - xmin > max_dim)
648 >                max_dim = xmax - xmin;
649 >        if (front_comp) {
650 >                printf("\n# BSDF system geometry (front view)\n");
651 >                sprintf(cmdbuf, "xform -t %f %f %f -s %f -t %f %f 0 %s",
652 >                                -.5*(xmin+xmax), -.5*(ymin+ymax), -zmax,
653 >                                1.5*sph_rad/max_dim,
654 >                                sph_xoffset, -2.5*sph_rad,
655 >                                radfn);
656 >                if (!run_cmd(cmdbuf))
657 >                        return;
658 >        }
659 >        if (back_comp) {
660 >                printf("\n# BSDF system geometry (back view)\n");
661 >                sprintf(cmdbuf, "xform -t %f %f %f -s %f -ry 180 -t %f %f 0 %s",
662 >                                -.5*(xmin+xmax), -.5*(ymin+ymax), -zmin,
663 >                                1.5*sph_rad/max_dim,
664 >                                -sph_xoffset, -2.5*sph_rad,
665 >                                radfn);
666 >                if (!run_cmd(cmdbuf))
667 >                        return;
668 >        }
669 > }
670 >
671 > /* Check RBF input header line & get minimum BSDF value */
672 > static int
673 > rbf_headline(char *s, void *p)
674 > {
675 >        char    fmt[MAXFMTLEN];
676 >
677 >        if (formatval(fmt, s)) {
678 >                if (strcmp(fmt, BSDFREP_FMT))
679 >                        return(-1);
680 >                return(0);
681 >        }
682 >        if (!strncmp(s, "IO_SIDES=", 9)) {
683 >                sscanf(s+9, "%d %d", &input_orient, &output_orient);
684 >                if (input_orient == output_orient) {
685 >                        if (input_orient > 0)
686 >                                front_comp |= SDsampR;
687 >                        else
688 >                                back_comp |= SDsampR;
689 >                } else if (input_orient > 0)
690 >                        front_comp |= SDsampT;
691 >                else
692 >                        back_comp |= SDsampT;
693 >                return(0);
694 >        }
695 >        if (!strncmp(s, "BSDFMIN=", 8)) {
696 >                sscanf(s+8, "%lf", &bsdf_min);
697 >                if (bsdf_min < overall_min)
698 >                        overall_min = bsdf_min;
699 >                return(0);
700 >        }
701 >        return(0);
702 > }
703 >
704 > /* Produce a Radiance model plotting the given BSDF representation */
705   int
706   main(int argc, char *argv[])
707   {
37        int     showPeaks = 0;
38        int     doTrans = 0;
708          int     inpXML = -1;
709 <        RBFNODE *rbf = NULL;
41 <        FILE    *fp;
42 <        char    buf[128];
709 >        double  myLim[2];
710          SDData  myBSDF;
711 <        double  bsdf, min_log;
45 <        FVECT   idir, odir;
46 <        int     i, j, n;
711 >        int     a, n;
712                                                  /* check arguments */
713          progname = argv[0];
714 <        if (argc > 1 && !strcmp(argv[1], "-p")) {
715 <                ++showPeaks;
716 <                ++argv; --argc;
714 >        a = 1;
715 >        myLim[0] = -1; myLim[1] = -2;           /* specified BSDF range? */
716 >        if (argc > a+3 && argv[a][0] == '-' && argv[a][1] == 'r') {
717 >                myLim[0] = atof(argv[++a]);
718 >                myLim[1] = atof(argv[++a]);
719 >                ++a;
720          }
721 <        if (argc > 1 && !strcmp(argv[1], "-t")) {
722 <                ++doTrans;
55 <                ++argv; --argc;
56 <        }
57 <        if (argc >= 4 && (n = strlen(argv[1])-4) > 0) {
58 <                if (!strcasecmp(argv[1]+n, ".xml"))
721 >        if (argc > a && (n = strlen(argv[a])-4) > 0) {
722 >                if (!strcasecmp(argv[a]+n, ".xml"))
723                          inpXML = 1;
724 <                else if (!strcasecmp(argv[1]+n, ".sir"))
724 >                else if (!strcasecmp(argv[a]+n, ".sir"))
725                          inpXML = 0;
726          }
727 <        if (inpXML < 0) {
728 <                fprintf(stderr, "Usage: %s [-p] bsdf.sir theta1 phi1 .. > output.rad\n", progname);
729 <                fprintf(stderr, "   Or: %s [-t] bsdf.xml theta1 phi1 .. > output.rad\n", progname);
727 >        if (inpXML < 0 || inpXML & (argc > a+1)) {
728 >                fprintf(stderr, "Usage: %s [-r min max] bsdf.xml > output.rad\n", progname);
729 >                fprintf(stderr, "   Or: %s [-r min max] hemi1.sir hemi2.sir .. > output.rad\n", progname);
730                  return(1);
731          }
732 <                                                /* load input */
732 >        fputs("# ", stdout);                    /* copy our command */
733 >        printargs(argc, argv, stdout);
734 >                                                /* evaluate BSDF */
735          if (inpXML) {
736 <                SDclearBSDF(&myBSDF, argv[1]);
737 <                if (SDreportError(SDloadFile(&myBSDF, argv[1]), stderr))
736 >                SDclearBSDF(&myBSDF, argv[a]);
737 >                if (SDreportError(SDloadFile(&myBSDF, argv[a]), stderr))
738                          return(1);
739 <                bsdf_min = 1./M_PI;
740 <                if (myBSDF.rf != NULL && myBSDF.rLambFront.cieY < bsdf_min*M_PI)
741 <                        bsdf_min = myBSDF.rLambFront.cieY/M_PI;
742 <                if (myBSDF.rb != NULL && myBSDF.rLambBack.cieY < bsdf_min*M_PI)
743 <                        bsdf_min = myBSDF.rLambBack.cieY/M_PI;
744 <                if ((myBSDF.tf != NULL) | (myBSDF.tb != NULL) &&
745 <                                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",
83 <                                        progname, argv[1]);
739 >                if (myBSDF.rf != NULL) front_comp |= SDsampR;
740 >                if (myBSDF.tf != NULL) front_comp |= SDsampT;
741 >                if (myBSDF.rb != NULL) back_comp |= SDsampR;
742 >                if (myBSDF.tb != NULL) back_comp |= SDsampT;
743 >                if (!front_comp & !back_comp) {
744 >                        fprintf(stderr, "%s: nothing to plot in '%s'\n",
745 >                                        progname, argv[a]);
746                          return(1);
747                  }
748 <        } else {
749 <                fp = fopen(argv[1], "rb");
750 <                if (fp == NULL) {
751 <                        fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
752 <                                        progname, argv[1]);
753 <                        return(1);
748 >                if (myLim[0] >= 0)
749 >                        overall_min = myLim[0];
750 >                else {
751 >                        if (front_comp & SDsampR && myBSDF.rLambFront.cieY < overall_min*PI)
752 >                                overall_min = myBSDF.rLambFront.cieY/PI;
753 >                        if (back_comp & SDsampR && myBSDF.rLambBack.cieY < overall_min*PI)
754 >                                overall_min = myBSDF.rLambBack.cieY/PI;
755 >                        if (front_comp & SDsampT && myBSDF.tLambFront.cieY < overall_min*PI)
756 >                                overall_min = myBSDF.tLambFront.cieY/PI;
757 >                        if (back_comp & SDsampT && myBSDF.tLambBack.cieY < overall_min*PI)
758 >                                overall_min = myBSDF.tLambBack.cieY/PI;
759                  }
760 <                if (!load_bsdf_rep(fp))
760 >                set_minlog();
761 >                if (!build_wBSDF(&myBSDF))
762                          return(1);
763 <                fclose(fp);
764 <        }
765 < #ifdef DEBUG
766 <        fprintf(stderr, "Minimum BSDF set to %.4f\n", bsdf_min);
767 < #endif
768 <        min_log = log(bsdf_min*.5 + 1e-5);
769 <                                                /* output BSDF rep. */
770 <        for (n = 0; (n < 6) & (2*n+3 < argc); n++) {
771 <                double  theta = (M_PI/180.)*atof(argv[2*n+2]);
772 <                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);
763 >                if (myBSDF.matn[0])
764 >                        strcpy(bsdf_name, myBSDF.matn);
765 >                else
766 >                        strcpy(bsdf_name, myBSDF.name);
767 >                strcpy(bsdf_manuf, myBSDF.makr);
768 >                put_matBSDF(argv[a]);
769 >        } else {
770 >                FILE    *fp[4];
771 >                if (argc > a+4) {
772 >                        fprintf(stderr, "%s: more than 4 hemispheres!\n", progname);
773                          return(1);
774                  }
775 <                if (inpXML) {
776 <                        input_orient = (theta <= M_PI/2.) ? 1 : -1;
777 <                        output_orient = doTrans ? -input_orient : input_orient;
775 >                for (n = a; n < argc; n++) {
776 >                        fp[n-a] = fopen(argv[n], "rb");
777 >                        if (fp[n-a] == NULL) {
778 >                                fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
779 >                                                progname, argv[n]);
780 >                                return(1);
781 >                        }
782 >                        if (getheader(fp[n-a], rbf_headline, NULL) < 0) {
783 >                                fprintf(stderr, "%s: bad BSDF interpolant '%s'\n",
784 >                                                progname, argv[n]);
785 >                                return(1);
786 >                        }
787                  }
788 <                idir[2] = sin(theta);
789 <                idir[0] = idir[2] * cos(phi);
790 <                idir[1] = idir[2] * sin(phi);
791 <                idir[2] = input_orient * sqrt(1. - idir[2]*idir[2]);
792 < #ifdef DEBUG
793 <                fprintf(stderr, "Computing BSDF for incident direction (%.1f,%.1f)\n",
794 <                                get_theta180(idir), get_phi360(idir));
795 < #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);
134 <                else
135 <                        fprintf(stderr, "Hemispherical %s: %.3f\n",
136 <                                (output_orient > 0 ^ input_orient > 0 ?
137 <                                        "transmission" : "reflection"),
138 <                                rbf->vtotal);
139 < #endif
140 <                printf("void trans tmat\n0\n0\n7 %f %f %f .04 .04 .9 1\n",
141 <                                colarr[n][0], colarr[n][1], colarr[n][2]);
142 <                if (showPeaks && rbf != NULL) {
143 <                        printf("void plastic pmat\n0\n0\n5 %f %f %f .04 .08\n",
144 <                                1.-colarr[n][0], 1.-colarr[n][1], 1.-colarr[n][2]);
145 <                        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);
788 >                if (myLim[0] >= 0)
789 >                        overall_min = myLim[0];
790 >                set_minlog();
791 >                for (n = a; n < argc; n++) {
792 >                        if (fseek(fp[n-a], 0L, SEEK_SET) < 0) {
793 >                                fprintf(stderr, "%s: cannot seek on '%s'\n",
794 >                                                progname, argv[n]);
795 >                                return(1);
796                          }
797 +                        if (!load_bsdf_rep(fp[n-a]))
798 +                                return(1);
799 +                        fclose(fp[n-a]);
800 +                        if (!build_wRBF())
801 +                                return(1);
802                  }
803 <                fflush(stdout);
155 <                sprintf(buf, "gensurf tmat bsdf%d - - - %d %d %s", n+1,
156 <                                                GRIDRES-1, GRIDRES-1, validf);
157 <                fp = popen(buf, "w");
158 <                if (fp == NULL) {
159 <                        fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf);
160 <                        return(1);
161 <                }
162 <                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);
803 >                put_matBSDF(NULL);
804          }
805 +        if (myLim[1] > myLim[0])        /* override maximum BSDF? */
806 +                overall_max = myLim[1];
807 +        put_source();                   /* before hemispheres & labels */
808 +        put_hemispheres();
809 +        put_scale();
810 +        if (inpXML && myBSDF.mgf)
811 +                convert_mgf(myBSDF.mgf);
812 +        if (!put_BSDFs())               /* most of the output happens here */
813 +                return(1);
814 +        cleanup_tmp();
815          return(0);
816   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines