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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines