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

Comparing ray/src/cv/bsdf2ttree.c (file contents):
Revision 2.5 by greg, Fri Nov 9 02:16:29 2012 UTC vs.
Revision 2.51 by greg, Fri May 15 03:02:32 2020 UTC

# Line 8 | Line 8 | static const char RCSid[] = "$Id$";
8   */
9  
10   #define _USE_MATH_DEFINES
11 #include <stdio.h>
11   #include <stdlib.h>
12   #include <math.h>
13 + #include "random.h"
14   #include "platform.h"
15 + #include "paths.h"
16 + #include "rtio.h"
17 + #include "calcomp.h"
18   #include "bsdfrep.h"
19                                  /* global argv[0] */
20   char                    *progname;
21 +                                /* reciprocity averaging option */
22 + static const char       *recip = " -a";
23                                  /* percentage to cull (<0 to turn off) */
24 < int                     pctcull = 90;
24 > static double           pctcull = 90.;
25                                  /* sampling order */
26 < int                     samp_order = 6;
26 > static int              samp_order = 6;
27 >                                /* super-sampling threshold */
28 > static double           ssamp_thresh = 0.35;
29 >                                /* number of super-samples */
30 > #ifndef NSSAMP
31 > #define NSSAMP          256
32 > #endif
33 >                                /* limit on number of RBF lobes */
34 > static int              lobe_lim = 15000;
35 >                                /* progress bar length */
36 > static int              do_prog = 79;
37  
38 + #define MAXCARG         512     /* wrapBSDF command */
39 + static char             *wrapBSDF[MAXCARG] = {"wrapBSDF", "-U"};
40 + static int              wbsdfac = 2;
41 +
42 + /* Add argument to wrapBSDF, allocating space if !isstatic */
43 + static void
44 + add_wbsdf(const char *arg, int isstatic)
45 + {
46 +        if (arg == NULL)
47 +                return;
48 +        if (wbsdfac >= MAXCARG-1) {
49 +                fputs(progname, stderr);
50 +                fputs(": too many command arguments to wrapBSDF\n", stderr);
51 +                exit(1);
52 +        }
53 +        if (!*arg)
54 +                arg = "";
55 +        else if (!isstatic)
56 +                arg = savqstr((char *)arg);
57 +
58 +        wrapBSDF[wbsdfac++] = (char *)arg;
59 + }
60 +
61 + /* Create Yuv component file and add appropriate arguments */
62 + static char *
63 + create_component_file(int c)
64 + {
65 +        static const char       sname[3][6] = {"CIE-Y", "CIE-u", "CIE-v"};
66 +        static const char       cname[4][4] = {"-rf", "-tf", "-tb", "-rb"};
67 +        char                    *tfname = mktemp(savqstr(TEMPLATE));
68 +
69 +        add_wbsdf("-s", 1); add_wbsdf(sname[c], 1);
70 +        add_wbsdf(cname[(input_orient>0)<<1 | (output_orient>0)], 1);
71 +        add_wbsdf(tfname, 1);
72 +        return(tfname);
73 + }
74 +
75 + /* Start new progress bar */
76 + #define prog_start(s)   if (do_prog) fprintf(stderr, "%s: %s...\n", progname, s); else
77 +
78 + /* Draw progress bar of the appropriate length */
79 + static void
80 + prog_show(double frac)
81 + {
82 +        static unsigned call_cnt = 0;
83 +        static char     lastc[] = "-\\|/";
84 +        char            pbar[256];
85 +        int             nchars;
86 +
87 +        if (do_prog <= 1) return;
88 +        if (do_prog > sizeof(pbar)-2)
89 +                do_prog = sizeof(pbar)-2;
90 +        if (frac < 0) frac = 0;
91 +        else if (frac >= 1) frac = .9999;
92 +        nchars = do_prog*frac;
93 +        pbar[0] = '\r';
94 +        memset(pbar+1, '*', nchars);
95 +        pbar[nchars+1] = lastc[call_cnt++ & 3];
96 +        memset(pbar+2+nchars, '-', do_prog-nchars-1);
97 +        pbar[do_prog+1] = '\0';
98 +        fputs(pbar, stderr);
99 + }
100 +
101 + /* Finish progress bar */
102 + static void
103 + prog_done(void)
104 + {
105 +        int     n = do_prog;
106 +
107 +        if (n <= 1) return;
108 +        fputc('\r', stderr);
109 +        while (n--)
110 +                fputc(' ', stderr);
111 +        fputc('\r', stderr);
112 + }
113 +
114 + /* Compute absolute relative difference */
115 + static double
116 + abs_diff(double v1, double v0)
117 + {
118 +        if ((v0 < 0) | (v1 < 0))
119 +                return(.0);
120 +        v1 = (v1-v0)*2./(v0+v1+.0001);
121 +        if (v1 < 0)
122 +                return(-v1);
123 +        return(v1);
124 + }
125 +
126   /* Interpolate and output isotropic BSDF data */
127   static void
128 < interp_isotropic()
128 > eval_isotropic(char *funame)
129   {
130          const int       sqres = 1<<samp_order;
131 <        FILE            *ofp = NULL;
131 >        const double    sqfact = 1./(double)sqres;
132 >        float           *val_last = NULL;
133 >        float           *val_next = NULL;
134 >        SDValue         *sdv_next = NULL;
135 >        FILE            *ofp, *uvfp[2];
136 >        int             assignD = 0;
137          char            cmd[128];
138          int             ix, ox, oy;
139 <        FVECT           ivec, ovec;
140 <        float           bsdf;
141 < #if DEBUG
142 <        fprintf(stderr, "Writing isotropic order %d ", samp_order);
143 <        if (pctcull >= 0) fprintf(stderr, "data with %d%% culling\n", pctcull);
144 <        else fputs("raw data\n", stderr);
37 < #endif
38 <        if (pctcull >= 0) {                     /* begin output */
39 <                sprintf(cmd, "rttree_reduce -h -a -ff -r 3 -t %d -g %d",
40 <                                pctcull, samp_order);
41 <                fflush(stdout);
139 >        double          iovec[6];
140 >        float           bsdf, uv[2];
141 >
142 >        if (pctcull >= 0) {
143 >                sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s",
144 >                                recip, pctcull, samp_order, create_component_file(0));
145                  ofp = popen(cmd, "w");
146                  if (ofp == NULL) {
147                          fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n",
# Line 46 | Line 149 | interp_isotropic()
149                          exit(1);
150                  }
151                  SET_FILE_BINARY(ofp);
152 <        } else
153 <                fputs("{\n", stdout);
152 > #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
153 >                flockfile(ofp);
154 > #endif
155 >                if (rbf_colorimetry == RBCtristimulus) {
156 >                        double  uvcull = 100. - (100.-pctcull)*.25;
157 >                        sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s",
158 >                                        recip, uvcull, samp_order, create_component_file(1));
159 >                        uvfp[0] = popen(cmd, "w");
160 >                        sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s",
161 >                                        recip, uvcull, samp_order, create_component_file(2));
162 >                        uvfp[1] = popen(cmd, "w");
163 >                        if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) {
164 >                                fprintf(stderr, "%s: cannot open pipes to uv output\n",
165 >                                                progname);
166 >                                exit(1);
167 >                        }
168 >                        SET_FILE_BINARY(uvfp[0]); SET_FILE_BINARY(uvfp[1]);
169 > #ifdef getc_unlocked
170 >                        flockfile(uvfp[0]); flockfile(uvfp[1]);
171 > #endif
172 >                }
173 >        } else {
174 >                ofp = fopen(create_component_file(0), "w");
175 >                if (ofp == NULL) {
176 >                        fprintf(stderr, "%s: cannot create Y output file\n",
177 >                                        progname);
178 >                        exit(1);
179 >                }
180 >                fputs("{\n", ofp);
181 >                if (rbf_colorimetry == RBCtristimulus) {
182 >                        uvfp[0] = fopen(create_component_file(1), "w");
183 >                        uvfp[1] = fopen(create_component_file(2), "w");
184 >                        if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) {
185 >                                fprintf(stderr, "%s: cannot create uv output file(s)\n",
186 >                                                progname);
187 >                                exit(1);
188 >                        }
189 >                        fputs("{\n", uvfp[0]);
190 >                        fputs("{\n", uvfp[1]);
191 >                }
192 >        }
193 >        if (funame != NULL)                     /* need to assign Dx, Dy, Dz? */
194 >                assignD = (fundefined(funame) < 6);
195 > #if (NSSAMP > 0)
196 >        val_last = (float *)calloc(sqres, sizeof(float));
197 >        if (funame == NULL)
198 >                sdv_next = (SDValue *)malloc(sizeof(SDValue)*sqres);
199 >        else
200 >                val_next = (float *)malloc(sizeof(float)*sqres);
201 > #endif
202                                                  /* run through directions */
203          for (ix = 0; ix < sqres/2; ix++) {
204 <                RBFNODE *rbf;
205 <                SDsquare2disk(ivec, (ix+.5)/sqres, .5);
206 <                ivec[2] = input_orient *
207 <                                sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]);
208 <                rbf = advect_rbf(ivec);
209 <                for (ox = 0; ox < sqres; ox++)
204 >                const int       zipsgn = (ix & 1)*2 - 1;
205 >                RBFNODE         *rbf = NULL;
206 >                iovec[0] = 2.*sqfact*(ix+.5) - 1.;
207 >                iovec[1] = zipsgn*sqfact*.5;
208 >                iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]
209 >                                                - iovec[1]*iovec[1]);
210 >                if (funame == NULL)
211 >                        rbf = advect_rbf(iovec, lobe_lim);
212 >                                                /* presample first row */
213 >                for (oy = 0; oy < sqres; oy++) {
214 >                    SDsquare2disk(iovec+3, .5*sqfact, (oy+.5)*sqfact);
215 >                    iovec[5] = output_orient *
216 >                                sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
217 >                    if (funame == NULL) {
218 >                        eval_rbfcol(&sdv_next[oy], rbf, iovec+3);
219 >                    } else {
220 >                        if (assignD) {
221 >                            varset("Dx", '=', -iovec[3]);
222 >                            varset("Dy", '=', -iovec[4]);
223 >                            varset("Dz", '=', -iovec[5]);
224 >                            ++eclock;
225 >                        }
226 >                        val_next[oy] = funvalue(funame, 6, iovec);
227 >                    }
228 >                }
229 >                for (ox = 0; ox < sqres; ox++) {
230 >                    /*
231 >                     * Super-sample when we detect a difference from before
232 >                     * or after in this row, above or below.
233 >                     */
234                      for (oy = 0; oy < sqres; oy++) {
235 <                        SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres);
236 <                        ovec[2] = output_orient *
237 <                                sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]);
238 <                        bsdf = eval_rbfrep(rbf, ovec) * output_orient/ovec[2];
235 >                        if (ox < sqres-1) {     /* keeping one row ahead... */
236 >                            SDsquare2disk(iovec+3, (ox+1.5)*sqfact, (oy+.5)*sqfact);
237 >                            iovec[5] = output_orient *
238 >                                sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
239 >                        }
240 >                        if (funame == NULL) {
241 >                            SDValue     sdv = sdv_next[oy];
242 >                            bsdf = sdv.cieY;
243 >                            if (ox < sqres-1)
244 >                                eval_rbfcol(&sdv_next[oy], rbf, iovec+3);
245 > #if (NSSAMP > 0)
246 >                            if (abs_diff(bsdf, sdv_next[oy].cieY) > ssamp_thresh ||
247 >                                (ox && abs_diff(bsdf, val_last[oy]) > ssamp_thresh) ||
248 >                                (oy && abs_diff(bsdf, val_last[oy-1]) > ssamp_thresh) ||
249 >                                (oy < sqres-1 &&
250 >                                    abs_diff(bsdf, sdv_next[oy+1].cieY) > ssamp_thresh)) {
251 >                                int     ssi;
252 >                                double  ssa[2], sum = 0, usum = 0, vsum = 0;
253 >                                                /* super-sample voxel */
254 >                                for (ssi = NSSAMP; ssi--; ) {
255 >                                    SDmultiSamp(ssa, 2, (ssi+frandom()) *
256 >                                                        (1./NSSAMP));
257 >                                    SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact,
258 >                                                        (oy+ssa[1])*sqfact);
259 >                                    iovec[5] = output_orient *
260 >                                        sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
261 >                                    eval_rbfcol(&sdv, rbf, iovec+3);
262 >                                    sum += sdv.cieY;
263 >                                    if (rbf_colorimetry == RBCtristimulus) {
264 >                                        sdv.cieY /=
265 >                                            -2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.;
266 >                                        usum += 4.*sdv.spec.cx * sdv.cieY;
267 >                                        vsum += 9.*sdv.spec.cy * sdv.cieY;
268 >                                    }
269 >                                }
270 >                                bsdf = sum * (1./NSSAMP);
271 >                                if (rbf_colorimetry == RBCtristimulus) {
272 >                                    uv[0] = usum / (sum+FTINY);
273 >                                    uv[1] = vsum / (sum+FTINY);
274 >                                }
275 >                            } else
276 > #endif
277 >                            if (rbf_colorimetry == RBCtristimulus) {
278 >                                uv[0] = uv[1] = 1. /
279 >                                    (-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.);
280 >                                uv[0] *= 4.*sdv.spec.cx;
281 >                                uv[1] *= 9.*sdv.spec.cy;
282 >                            }
283 >                        } else {
284 >                            bsdf = val_next[oy];
285 >                            if (ox < sqres-1) {
286 >                                if (assignD) {
287 >                                    varset("Dx", '=', -iovec[3]);
288 >                                    varset("Dy", '=', -iovec[4]);
289 >                                    varset("Dz", '=', -iovec[5]);
290 >                                    ++eclock;
291 >                                }
292 >                                val_next[oy] = funvalue(funame, 6, iovec);
293 >                            }
294 > #if (NSSAMP > 0)
295 >                            if (abs_diff(bsdf, val_next[oy]) > ssamp_thresh ||
296 >                                (ox && abs_diff(bsdf, val_last[oy]) > ssamp_thresh) ||
297 >                                (oy && abs_diff(bsdf, val_last[oy-1]) > ssamp_thresh) ||
298 >                                (oy < sqres-1 &&
299 >                                    abs_diff(bsdf, val_next[oy+1]) > ssamp_thresh)) {
300 >                                int     ssi;
301 >                                double  ssa[4], ssvec[6], sum = 0;
302 >                                                /* super-sample voxel */
303 >                                for (ssi = NSSAMP; ssi--; ) {
304 >                                    SDmultiSamp(ssa, 4, (ssi+frandom()) *
305 >                                                        (1./NSSAMP));
306 >                                    ssvec[0] = 2.*sqfact*(ix+ssa[0]) - 1.;
307 >                                    ssvec[1] = zipsgn*sqfact*ssa[1];
308 >                                    ssvec[2] = 1. - ssvec[0]*ssvec[0]
309 >                                                        - ssvec[1]*ssvec[1];
310 >                                    if (ssvec[2] < .0) {
311 >                                        ssvec[1] = 0;
312 >                                        ssvec[2] = 1. - ssvec[0]*ssvec[0];
313 >                                    }
314 >                                    ssvec[2] = input_orient * sqrt(ssvec[2]);
315 >                                    SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact,
316 >                                                (oy+ssa[3])*sqfact);
317 >                                    ssvec[5] = output_orient *
318 >                                                sqrt(1. - ssvec[3]*ssvec[3] -
319 >                                                        ssvec[4]*ssvec[4]);
320 >                                    if (assignD) {
321 >                                        varset("Dx", '=', -ssvec[3]);
322 >                                        varset("Dy", '=', -ssvec[4]);
323 >                                        varset("Dz", '=', -ssvec[5]);
324 >                                        ++eclock;
325 >                                    }
326 >                                    sum += funvalue(funame, 6, ssvec);
327 >                                }
328 >                                bsdf = sum * (1./NSSAMP);
329 >                            }
330 > #endif
331 >                        }
332                          if (pctcull >= 0)
333 <                                fwrite(&bsdf, sizeof(bsdf), 1, ofp);
333 >                                putbinary(&bsdf, sizeof(bsdf), 1, ofp);
334                          else
335 <                                printf("\t%.3e\n", bsdf);
335 >                                fprintf(ofp, "\t%.3e\n", bsdf);
336 >
337 >                        if (rbf_colorimetry == RBCtristimulus) {
338 >                                if (pctcull >= 0) {
339 >                                        putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]);
340 >                                        putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]);
341 >                                } else {
342 >                                        fprintf(uvfp[0], "\t%.3e\n", uv[0]);
343 >                                        fprintf(uvfp[1], "\t%.3e\n", uv[1]);
344 >                                }
345 >                        }
346 >                        if (val_last != NULL)
347 >                                val_last[oy] = bsdf;
348                      }
349 +                }
350                  if (rbf != NULL)
351                          free(rbf);
352 +                prog_show((ix+1.)*(2.*sqfact));
353          }
354 +        prog_done();
355 +        if (val_last != NULL) {
356 +                free(val_last);
357 +                if (val_next != NULL) free(val_next);
358 +                if (sdv_next != NULL) free(sdv_next);
359 +        }
360          if (pctcull >= 0) {                     /* finish output */
361                  if (pclose(ofp)) {
362 <                        fprintf(stderr, "%s: error running '%s'\n",
363 <                                        progname, cmd);
362 >                        fprintf(stderr, "%s: error running rttree_reduce on Y\n",
363 >                                        progname);
364                          exit(1);
365                  }
366 +                if (rbf_colorimetry == RBCtristimulus &&
367 +                                (pclose(uvfp[0]) || pclose(uvfp[1]))) {
368 +                        fprintf(stderr, "%s: error running rttree_reduce on uv\n",
369 +                                        progname);
370 +                        exit(1);
371 +                }
372          } else {
373                  for (ix = sqres*sqres*sqres/2; ix--; )
374 <                        fputs("\t0\n", stdout);
375 <                fputs("}\n", stdout);
374 >                        fputs("\t0\n", ofp);
375 >                fputs("}\n", ofp);
376 >                if (fclose(ofp)) {
377 >                        fprintf(stderr, "%s: error writing Y file\n",
378 >                                        progname);
379 >                        exit(1);
380 >                }
381 >                if (rbf_colorimetry == RBCtristimulus) {
382 >                        for (ix = sqres*sqres*sqres/2; ix--; ) {
383 >                                fputs("\t0\n", uvfp[0]);
384 >                                fputs("\t0\n", uvfp[1]);
385 >                        }
386 >                        fputs("}\n", uvfp[0]);
387 >                        fputs("}\n", uvfp[1]);
388 >                        if (fclose(uvfp[0]) || fclose(uvfp[1])) {
389 >                                fprintf(stderr, "%s: error writing uv file(s)\n",
390 >                                                progname);
391 >                                exit(1);
392 >                        }
393 >                }
394          }
395   }
396  
397   /* Interpolate and output anisotropic BSDF data */
398   static void
399 < interp_anisotropic()
399 > eval_anisotropic(char *funame)
400   {
401          const int       sqres = 1<<samp_order;
402 <        FILE            *ofp = NULL;
402 >        const double    sqfact = 1./(double)sqres;
403 >        float           *val_last = NULL;
404 >        float           *val_next = NULL;
405 >        SDValue         *sdv_next = NULL;
406 >        FILE            *ofp, *uvfp[2];
407 >        int             assignD = 0;
408          char            cmd[128];
409          int             ix, iy, ox, oy;
410 <        FVECT           ivec, ovec;
411 <        float           bsdf;
412 < #if DEBUG
413 <        fprintf(stderr, "Writing anisotropic order %d ", samp_order);
414 <        if (pctcull >= 0) fprintf(stderr, "data with %d%% culling\n", pctcull);
415 <        else fputs("raw data\n", stderr);
416 < #endif
417 <        if (pctcull >= 0) {                     /* begin output */
418 <                sprintf(cmd, "rttree_reduce -h -a -ff -r 4 -t %d -g %d",
102 <                                pctcull, samp_order);
103 <                fflush(stdout);
410 >        double          iovec[6];
411 >        float           bsdf, uv[2];
412 >
413 >        if (pctcull >= 0) {
414 >                const char      *avgopt = (input_orient>0 ^ output_orient>0)
415 >                                                ? "" : recip;
416 >                sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s",
417 >                                avgopt, pctcull, samp_order,
418 >                                create_component_file(0));
419                  ofp = popen(cmd, "w");
420                  if (ofp == NULL) {
421                          fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n",
422                                          progname);
423                          exit(1);
424                  }
425 <        } else
426 <                fputs("{\n", stdout);
425 >                SET_FILE_BINARY(ofp);
426 > #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
427 >                flockfile(ofp);
428 > #endif
429 >                if (rbf_colorimetry == RBCtristimulus) {
430 >                        double  uvcull = 100. - (100.-pctcull)*.25;
431 >                        sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s",
432 >                                        avgopt, uvcull, samp_order,
433 >                                        create_component_file(1));
434 >                        uvfp[0] = popen(cmd, "w");
435 >                        sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s",
436 >                                        avgopt, uvcull, samp_order,
437 >                                        create_component_file(2));
438 >                        uvfp[1] = popen(cmd, "w");
439 >                        if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) {
440 >                                fprintf(stderr, "%s: cannot open pipes to uv output\n",
441 >                                                progname);
442 >                                exit(1);
443 >                        }
444 >                        SET_FILE_BINARY(uvfp[0]); SET_FILE_BINARY(uvfp[1]);
445 > #ifdef getc_unlocked
446 >                        flockfile(uvfp[0]); flockfile(uvfp[1]);
447 > #endif
448 >                }
449 >        } else {
450 >                ofp = fopen(create_component_file(0), "w");
451 >                if (ofp == NULL) {
452 >                        fprintf(stderr, "%s: cannot create Y output file\n",
453 >                                        progname);
454 >                        exit(1);
455 >                }
456 >                fputs("{\n", ofp);
457 >                if (rbf_colorimetry == RBCtristimulus) {
458 >                        uvfp[0] = fopen(create_component_file(1), "w");
459 >                        uvfp[1] = fopen(create_component_file(2), "w");
460 >                        if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) {
461 >                                fprintf(stderr, "%s: cannot create uv output file(s)\n",
462 >                                                progname);
463 >                                exit(1);
464 >                        }
465 >                        fputs("{\n", uvfp[0]);
466 >                        fputs("{\n", uvfp[1]);
467 >                }
468 >        }
469 >        if (funame != NULL)                     /* need to assign Dx, Dy, Dz? */
470 >                assignD = (fundefined(funame) < 6);
471 > #if (NSSAMP > 0)
472 >        val_last = (float *)calloc(sqres, sizeof(float));
473 >        if (funame == NULL)
474 >                sdv_next = (SDValue *)malloc(sizeof(SDValue)*sqres);
475 >        else
476 >                val_next = (float *)malloc(sizeof(float)*sqres);
477 > #endif
478                                                  /* run through directions */
479          for (ix = 0; ix < sqres; ix++)
480              for (iy = 0; iy < sqres; iy++) {
481 <                RBFNODE *rbf;
482 <                SDsquare2disk(ivec, (ix+.5)/sqres, (iy+.5)/sqres);
483 <                ivec[2] = input_orient *
484 <                                sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]);
485 <                rbf = advect_rbf(ivec);
486 <                for (ox = 0; ox < sqres; ox++)
481 >                RBFNODE *rbf = NULL;            /* Klems reversal */
482 >                SDsquare2disk(iovec, 1.-(ix+.5)*sqfact, 1.-(iy+.5)*sqfact);
483 >                iovec[2] = input_orient *
484 >                                sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]);
485 >                if (funame == NULL)
486 >                        rbf = advect_rbf(iovec, lobe_lim);
487 >                                                /* presample first row */
488 >                for (oy = 0; oy < sqres; oy++) {
489 >                    SDsquare2disk(iovec+3, .5*sqfact, (oy+.5)*sqfact);
490 >                    iovec[5] = output_orient *
491 >                                sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
492 >                    if (funame == NULL) {
493 >                        eval_rbfcol(&sdv_next[oy], rbf, iovec+3);
494 >                    } else {
495 >                        if (assignD) {
496 >                            varset("Dx", '=', -iovec[3]);
497 >                            varset("Dy", '=', -iovec[4]);
498 >                            varset("Dz", '=', -iovec[5]);
499 >                            ++eclock;
500 >                        }
501 >                        val_next[oy] = funvalue(funame, 6, iovec);
502 >                    }
503 >                }
504 >                for (ox = 0; ox < sqres; ox++) {
505 >                    /*
506 >                     * Super-sample when we detect a difference from before
507 >                     * or after in this row, above or below.
508 >                     */
509                      for (oy = 0; oy < sqres; oy++) {
510 <                        SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres);
511 <                        ovec[2] = output_orient *
512 <                                sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]);
513 <                        bsdf = eval_rbfrep(rbf, ovec) * output_orient/ovec[2];
510 >                        if (ox < sqres-1) {     /* keeping one row ahead... */
511 >                            SDsquare2disk(iovec+3, (ox+1.5)*sqfact, (oy+.5)*sqfact);
512 >                            iovec[5] = output_orient *
513 >                                sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
514 >                        }
515 >                        if (funame == NULL) {
516 >                            SDValue     sdv = sdv_next[oy];
517 >                            bsdf = sdv.cieY;
518 >                            if (ox < sqres-1)
519 >                                eval_rbfcol(&sdv_next[oy], rbf, iovec+3);
520 > #if (NSSAMP > 0)
521 >                            if (abs_diff(bsdf, sdv_next[oy].cieY) > ssamp_thresh ||
522 >                                (ox && abs_diff(bsdf, val_last[oy]) > ssamp_thresh) ||
523 >                                (oy && abs_diff(bsdf, val_last[oy-1]) > ssamp_thresh) ||
524 >                                (oy < sqres-1 &&
525 >                                    abs_diff(bsdf, sdv_next[oy+1].cieY) > ssamp_thresh)) {
526 >                                int     ssi;
527 >                                double  ssa[2], sum = 0, usum = 0, vsum = 0;
528 >                                                /* super-sample voxel */
529 >                                for (ssi = NSSAMP; ssi--; ) {
530 >                                    SDmultiSamp(ssa, 2, (ssi+frandom()) *
531 >                                                        (1./NSSAMP));
532 >                                    SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact,
533 >                                                        (oy+ssa[1])*sqfact);
534 >                                    iovec[5] = output_orient *
535 >                                        sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
536 >                                    eval_rbfcol(&sdv, rbf, iovec+3);
537 >                                    sum += sdv.cieY;
538 >                                    if (rbf_colorimetry == RBCtristimulus) {
539 >                                        sdv.cieY /=
540 >                                            -2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.;
541 >                                        usum += 4.*sdv.spec.cx * sdv.cieY;
542 >                                        vsum += 9.*sdv.spec.cy * sdv.cieY;
543 >                                    }
544 >                                }
545 >                                bsdf = sum * (1./NSSAMP);
546 >                                if (rbf_colorimetry == RBCtristimulus) {
547 >                                    uv[0] = usum / (sum+FTINY);
548 >                                    uv[1] = vsum / (sum+FTINY);
549 >                                }
550 >                            } else
551 > #endif
552 >                            if (rbf_colorimetry == RBCtristimulus) {
553 >                                uv[0] = uv[1] = 1. /
554 >                                    (-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.);
555 >                                uv[0] *= 4.*sdv.spec.cx;
556 >                                uv[1] *= 9.*sdv.spec.cy;
557 >                            }
558 >                        } else {
559 >                            bsdf = val_next[oy];
560 >                            if (ox < sqres-1) {
561 >                                if (assignD) {
562 >                                    varset("Dx", '=', -iovec[3]);
563 >                                    varset("Dy", '=', -iovec[4]);
564 >                                    varset("Dz", '=', -iovec[5]);
565 >                                    ++eclock;
566 >                                }
567 >                                val_next[oy] = funvalue(funame, 6, iovec);
568 >                            }
569 > #if (NSSAMP > 0)
570 >                            if (abs_diff(bsdf, val_next[oy]) > ssamp_thresh ||
571 >                                (ox && abs_diff(bsdf, val_last[oy]) > ssamp_thresh) ||
572 >                                (oy && abs_diff(bsdf, val_last[oy-1]) > ssamp_thresh) ||
573 >                                (oy < sqres-1 &&
574 >                                    abs_diff(bsdf, val_next[oy+1]) > ssamp_thresh)) {
575 >                                int     ssi;
576 >                                double  ssa[4], ssvec[6], sum = 0;
577 >                                                /* super-sample voxel */
578 >                                for (ssi = NSSAMP; ssi--; ) {
579 >                                    SDmultiSamp(ssa, 4, (ssi+frandom()) *
580 >                                                        (1./NSSAMP));
581 >                                    SDsquare2disk(ssvec, 1.-(ix+ssa[0])*sqfact,
582 >                                                1.-(iy+ssa[1])*sqfact);
583 >                                    ssvec[2] = input_orient *
584 >                                                sqrt(1. - ssvec[0]*ssvec[0] -
585 >                                                        ssvec[1]*ssvec[1]);
586 >                                    SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact,
587 >                                                (oy+ssa[3])*sqfact);
588 >                                    ssvec[5] = output_orient *
589 >                                                sqrt(1. - ssvec[3]*ssvec[3] -
590 >                                                        ssvec[4]*ssvec[4]);
591 >                                    if (assignD) {
592 >                                        varset("Dx", '=', -ssvec[3]);
593 >                                        varset("Dy", '=', -ssvec[4]);
594 >                                        varset("Dz", '=', -ssvec[5]);
595 >                                        ++eclock;
596 >                                    }
597 >                                    sum += funvalue(funame, 6, ssvec);
598 >                                }
599 >                                bsdf = sum * (1./NSSAMP);
600 >                            }
601 > #endif
602 >                        }
603                          if (pctcull >= 0)
604 <                                fwrite(&bsdf, sizeof(bsdf), 1, ofp);
604 >                                putbinary(&bsdf, sizeof(bsdf), 1, ofp);
605                          else
606 <                                printf("\t%.3e\n", bsdf);
606 >                                fprintf(ofp, "\t%.3e\n", bsdf);
607 >
608 >                        if (rbf_colorimetry == RBCtristimulus) {
609 >                                if (pctcull >= 0) {
610 >                                        putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]);
611 >                                        putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]);
612 >                                } else {
613 >                                        fprintf(uvfp[0], "\t%.3e\n", uv[0]);
614 >                                        fprintf(uvfp[1], "\t%.3e\n", uv[1]);
615 >                                }
616 >                        }
617 >                        if (val_last != NULL)
618 >                                val_last[oy] = bsdf;
619                      }
620 +                }
621                  if (rbf != NULL)
622                          free(rbf);
623 +                prog_show((ix*sqres+iy+1.)/(sqres*sqres));
624              }
625 +        prog_done();
626 +        if (val_last != NULL) {
627 +                free(val_last);
628 +                if (val_next != NULL) free(val_next);
629 +                if (sdv_next != NULL) free(sdv_next);
630 +        }
631          if (pctcull >= 0) {                     /* finish output */
632                  if (pclose(ofp)) {
633 <                        fprintf(stderr, "%s: error running '%s'\n",
634 <                                        progname, cmd);
633 >                        fprintf(stderr, "%s: error running rttree_reduce on Y\n",
634 >                                        progname);
635                          exit(1);
636                  }
637 <        } else
638 <                fputs("}\n", stdout);
637 >                if (rbf_colorimetry == RBCtristimulus &&
638 >                                (pclose(uvfp[0]) || pclose(uvfp[1]))) {
639 >                        fprintf(stderr, "%s: error running rttree_reduce on uv\n",
640 >                                        progname);
641 >                        exit(1);
642 >                }
643 >        } else {
644 >                fputs("}\n", ofp);
645 >                if (fclose(ofp)) {
646 >                        fprintf(stderr, "%s: error writing Y file\n",
647 >                                        progname);
648 >                        exit(1);
649 >                }
650 >                if (rbf_colorimetry == RBCtristimulus) {
651 >                        fputs("}\n", uvfp[0]);
652 >                        fputs("}\n", uvfp[1]);
653 >                        if (fclose(uvfp[0]) || fclose(uvfp[1])) {
654 >                                fprintf(stderr, "%s: error writing uv file(s)\n",
655 >                                                progname);
656 >                                exit(1);
657 >                        }
658 >                }
659 >        }
660   }
661  
662 < /* Output XML prologue to stdout */
663 < static void
664 < xml_prologue(int ac, char *av[])
662 > #if defined(_WIN32) || defined(_WIN64)
663 > /* Execute wrapBSDF command (may never return) */
664 > static int
665 > wrap_up(void)
666   {
667 <        static const char       *prologue0[] = {
149 < "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
150 < "<WindowElement xmlns=\"http://windows.lbl.gov\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://windows.lbl.gov/BSDF-v1.4.xsd\">",
151 <                                NULL};
152 <        static const char       *prologue1[] = {
153 < "<WindowElementType>System</WindowElementType>",
154 < "<FileType>BSDF</FileType>",
155 < "<Optical>",
156 < "<Layer>",
157 < "\t<Material>",
158 < "\t\t<Name>Name</Name>",
159 < "\t\t<Manufacturer>Manufacturer</Manufacturer>",
160 < "\t\t<DeviceType>Other</DeviceType>",
161 < "\t</Material>",
162 <                                NULL};
163 <        static const char       *prologue2[] = {
164 < "\t<WavelengthData>",
165 < "\t\t<LayerNumber>System</LayerNumber>",
166 < "\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>",
167 < "\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>",
168 < "\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>",
169 < "\t\t<WavelengthDataBlock>",
170 < "\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>",
171 < "\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>",
172 <                                NULL};
173 <        static const char       *bsdf_type[4] = {
174 <                                        "Reflection Back",
175 <                                        "Transmission Back",
176 <                                        "Transmission Front",
177 <                                        "Reflection Front"
178 <                                };
179 <        int                     i;
667 >        char    cmd[8192];
668  
669 <        for (i = 0; prologue0[i] != NULL; i++)
670 <                puts(prologue0[i]);
671 <        fputs("<!-- File produced by:", stdout);
672 <        while (ac-- > 0) {
673 <                fputc(' ', stdout);
186 <                fputs(*av++, stdout);
669 >        if (bsdf_manuf[0]) {
670 >                add_wbsdf("-f", 1);
671 >                strcpy(cmd, "m=");
672 >                strcpy(cmd+2, bsdf_manuf);
673 >                add_wbsdf(cmd, 0);
674          }
675 <        puts(" -->");
676 <        for (i = 0; prologue1[i] != NULL; i++)
677 <                puts(prologue1[i]);
678 <        puts("\t<DataDefinition>");
679 <        printf("\t\t<IncidentDataStructure>TensorTree%c</IncidentDataStructure>\n",
680 <                        single_plane_incident ? '3' : '4');
681 <        puts("\t</DataDefinition>");
682 <        for (i = 0; prologue2[i] != NULL; i++)
683 <                puts(prologue2[i]);
684 <        printf("\t\t\t<WavelengthDataDirection>%s</WavelengthDataDirection>\n",
685 <                bsdf_type[(input_orient>0)<<1 | (output_orient>0)]);
686 <        puts("\t\t\t<ScatteringData>");
675 >        if (bsdf_name[0]) {
676 >                add_wbsdf("-f", 1);
677 >                strcpy(cmd, "n=");
678 >                strcpy(cmd+2, bsdf_name);
679 >                add_wbsdf(cmd, 0);
680 >        }
681 >        if (!convert_commandline(cmd, sizeof(cmd), wrapBSDF)) {
682 >                fputs(progname, stderr);
683 >                fputs(": command line too long in wrap_up()\n", stderr);
684 >                return(1);
685 >        }
686 >        return(system(cmd));
687   }
688 <
689 < /* Output XML epilogue to stdout */
690 < static void
691 < xml_epilogue(void)
688 > #else
689 > /* Execute wrapBSDF command (may never return) */
690 > static int
691 > wrap_up(void)
692   {
693 <        static const char       *epilogue[] = {
694 < "\t\t\t</ScatteringData>",
208 < "\t\t</WavelengthDataBlock>",
209 < "\t</WavelengthData>",
210 < "</Layer>",
211 < "</Optical>",
212 < "</WindowElement>",
213 <                                NULL};
214 <        int                     i;
693 >        char    buf[256];
694 >        char    *compath = getpath((char *)wrapBSDF[0], getenv("PATH"), X_OK);
695  
696 <        for (i = 0; epilogue[i] != NULL; i++)
697 <                puts(epilogue[i]);
696 >        if (compath == NULL) {
697 >                fprintf(stderr, "%s: cannot locate %s\n", progname, wrapBSDF[0]);
698 >                return(1);
699 >        }
700 >        if (bsdf_manuf[0]) {
701 >                add_wbsdf("-f", 1);
702 >                strcpy(buf, "m=");
703 >                strcpy(buf+2, bsdf_manuf);
704 >                add_wbsdf(buf, 0);
705 >        }
706 >        if (bsdf_name[0]) {
707 >                add_wbsdf("-f", 1);
708 >                strcpy(buf, "n=");
709 >                strcpy(buf+2, bsdf_name);
710 >                add_wbsdf(buf, 0);
711 >        }
712 >        execv(compath, wrapBSDF);       /* successful call never returns */
713 >        perror(compath);
714 >        return(1);
715   }
716 + #endif
717  
718   /* Read in BSDF and interpolate as tensor tree representation */
719   int
720   main(int argc, char *argv[])
721   {
722 <        FILE    *fpin = stdin;
723 <        int     i;
722 >        static char     tfmt[2][4] = {"t4", "t3"};
723 >        int     dofwd = 0, dobwd = 1;
724 >        char    buf[2048];
725 >        int     i, na;
726  
727          progname = argv[0];
728 <        for (i = 1; i < argc-1 && argv[i][0] == '-'; i++)
729 <                switch (argv[i][1]) {           /* get option */
728 >        esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
729 >        esupport &= ~(E_INCHAN|E_OUTCHAN);
730 >        scompile("PI:3.14159265358979323846", NULL, 0);
731 >        biggerlib();
732 >        for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
733 >                switch (argv[i][1]) {           /* get options */
734 >                case 'e':
735 >                        scompile(argv[++i], NULL, 0);
736 >                        if (single_plane_incident < 0)
737 >                                single_plane_incident = 0;
738 >                        break;
739 >                case 'f':
740 >                        if (!argv[i][2]) {
741 >                                if (strchr(argv[++i], '=') != NULL) {
742 >                                        add_wbsdf("-f", 1);
743 >                                        add_wbsdf(argv[i], 1);
744 >                                } else {
745 >                                        char    *fpath = getpath(argv[i],
746 >                                                            getrlibpath(), 0);
747 >                                        if (fpath == NULL) {
748 >                                                fprintf(stderr,
749 >                                                "%s: cannot find file '%s'\n",
750 >                                                        argv[0], argv[i]);
751 >                                                return(1);
752 >                                        }
753 >                                        fcompile(fpath);
754 >                                        if (single_plane_incident < 0)
755 >                                                single_plane_incident = 0;
756 >                                }
757 >                        } else
758 >                                dofwd = (argv[i][0] == '+');
759 >                        break;
760 >                case 'a':
761 >                        recip = (argv[i][0] == '+') ? " -a" : "";
762 >                        break;
763 >                case 'b':
764 >                        dobwd = (argv[i][0] == '+');
765 >                        break;
766                  case 't':
767 <                        pctcull = atoi(argv[++i]);
767 >                        switch (argv[i][2]) {
768 >                        case '3':
769 >                                single_plane_incident = 1;
770 >                                break;
771 >                        case '4':
772 >                                single_plane_incident = 0;
773 >                                break;
774 >                        case '\0':
775 >                                pctcull = atof(argv[++i]);
776 >                                break;
777 >                        default:
778 >                                goto userr;
779 >                        }
780                          break;
781                  case 'g':
782                          samp_order = atoi(argv[++i]);
783                          break;
784 +                case 'l':
785 +                        lobe_lim = atoi(argv[++i]);
786 +                        break;
787 +                case 'p':
788 +                        do_prog = atoi(argv[i]+2);
789 +                        break;
790 +                case 'W':
791 +                        add_wbsdf(argv[i], 1);
792 +                        break;
793 +                case 'u':
794 +                case 'C':
795 +                        add_wbsdf(argv[i], 1);
796 +                        add_wbsdf(argv[++i], 1);
797 +                        break;
798                  default:
799                          goto userr;
800                  }
801 <
802 <        if (i == argc-1) {                      /* open input if given */
803 <                fpin = fopen(argv[i], "r");
804 <                if (fpin == NULL) {
805 <                        fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
806 <                                        progname, argv[1]);
807 <                        return(1);
801 >        strcpy(buf, "File produced by: ");
802 >        if (convert_commandline(buf+18, sizeof(buf)-18, argv) != NULL) {
803 >                add_wbsdf("-C", 1); add_wbsdf(buf, 0);
804 >        }
805 >        if (single_plane_incident >= 0) {       /* function-based BSDF? */
806 >                void    (*evf)(char *s) = single_plane_incident ?
807 >                                eval_isotropic : eval_anisotropic;
808 >                if (i != argc-1 || fundefined(argv[i]) < 3) {
809 >                        fprintf(stderr,
810 >        "%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n",
811 >                                        progname);
812 >                        fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n");
813 >                        goto userr;
814                  }
815 <        } else if (i < argc-1)
816 <                goto userr;
817 <        SET_FILE_BINARY(fpin);                  /* load BSDF interpolant */
818 <        if (!load_bsdf_rep(fpin))
815 >                ++eclock;
816 >                ssamp_thresh *= 0.5;            /* lower sampling threshold */
817 >                add_wbsdf("-a", 1);
818 >                add_wbsdf(tfmt[single_plane_incident], 1);
819 >                if (dofwd) {
820 >                        input_orient = -1;
821 >                        output_orient = -1;
822 >                        prog_start("Evaluating outside reflectance");
823 >                        (*evf)(argv[i]);
824 >                        output_orient = 1;
825 >                        prog_start("Evaluating outside->inside transmission");
826 >                        (*evf)(argv[i]);
827 >                }
828 >                if (dobwd) {
829 >                        input_orient = 1;
830 >                        output_orient = 1;
831 >                        prog_start("Evaluating inside reflectance");
832 >                        (*evf)(argv[i]);
833 >                        output_orient = -1;
834 >                        prog_start("Evaluating inside->outside transmission");
835 >                        (*evf)(argv[i]);
836 >                }
837 >                return(wrap_up());
838 >        }
839 >        if (i < argc) {                         /* open input files if given */
840 >                int     nbsdf = 0;
841 >                for ( ; i < argc; i++) {        /* interpolate each component */
842 >                        char    pbuf[256];
843 >                        FILE    *fpin = fopen(argv[i], "rb");
844 >                        if (fpin == NULL) {
845 >                                fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
846 >                                                progname, argv[i]);
847 >                                return(1);
848 >                        }
849 >                        if (!load_bsdf_rep(fpin))
850 >                                return(1);
851 >                        fclose(fpin);
852 >                        sprintf(pbuf, "Interpolating component '%s'", argv[i]);
853 >                        prog_start(pbuf);
854 >                        if (!nbsdf++) {
855 >                                add_wbsdf("-a", 1);
856 >                                add_wbsdf(tfmt[single_plane_incident], 1);
857 >                        }
858 >                        if (single_plane_incident)
859 >                                eval_isotropic(NULL);
860 >                        else
861 >                                eval_anisotropic(NULL);
862 >                }
863 >                return(wrap_up());
864 >        }
865 >        SET_FILE_BINARY(stdin);                 /* load from stdin */
866 >        if (!load_bsdf_rep(stdin))
867                  return(1);
868 <        fclose(fpin);
869 <        xml_prologue(argc, argv);               /* start XML output */
868 >        prog_start("Interpolating from standard input");
869 >        add_wbsdf("-a", 1);
870 >        add_wbsdf(tfmt[single_plane_incident], 1);
871          if (single_plane_incident)              /* resample dist. */
872 <                interp_isotropic();
872 >                eval_isotropic(NULL);
873          else
874 <                interp_anisotropic();
875 <        xml_epilogue();                         /* finish XML output */
876 <        return(0);
874 >                eval_anisotropic(NULL);
875 >
876 >        return(wrap_up());
877   userr:
878          fprintf(stderr,
879 <        "Usage: %s [-t pctcull][-g log2grid] [bsdf.sir] > bsdf.xml\n",
879 >        "Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n",
880 >                                progname);
881 >        fprintf(stderr,
882 >        "   or: %s -t{3|4} [{+|-}a][-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",
883                                  progname);
884          return(1);
885   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines