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.29 by greg, Thu Aug 21 10:33:48 2014 UTC vs.
Revision 2.50 by greg, Mon May 11 20:26:58 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   #ifndef NSSAMP
31 < #define NSSAMP          100
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  
# Line 41 | Line 79 | static int             do_prog = 79;
79   static void
80   prog_show(double frac)
81   {
82 <        char    pbar[256];
83 <        int     nchars;
82 >        static unsigned call_cnt = 0;
83 >        static char     lastc[] = "-\\|/";
84 >        char            pbar[256];
85 >        int             nchars;
86  
87 <        if (do_prog <= 0) return;
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 = 1;
92 <        nchars = do_prog*frac + .5;
91 >        else if (frac >= 1) frac = .9999;
92 >        nchars = do_prog*frac;
93          pbar[0] = '\r';
94          memset(pbar+1, '*', nchars);
95 <        memset(pbar+1+nchars, '-', do_prog-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   }
# Line 70 | Line 111 | prog_done(void)
111          fputc('\r', stderr);
112   }
113  
73 /* Output XML prologue to stdout */
74 static void
75 xml_prologue(int ac, char *av[])
76 {
77        puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
78        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\">");
79        fputs("<!-- File produced by:", stdout);
80        while (ac-- > 0) {
81                fputc(' ', stdout);
82                fputs(*av++, stdout);
83        }
84        puts(" -->");
85        puts("<WindowElementType>System</WindowElementType>");
86        puts("<FileType>BSDF</FileType>");
87        puts("<Optical>");
88        puts("<Layer>");
89        puts("\t<Material>");
90        printf("\t\t<Name>%s</Name>\n", bsdf_name[0] ? bsdf_name : "Unknown");
91        printf("\t\t<Manufacturer>%s</Manufacturer>\n",
92                        bsdf_manuf[0] ? bsdf_manuf : "Unknown");
93        puts("\t\t<DeviceType>Other</DeviceType>");
94        puts("\t</Material>");
95        puts("\t<DataDefinition>");
96        printf("\t\t<IncidentDataStructure>TensorTree%c</IncidentDataStructure>\n",
97                        single_plane_incident ? '3' : '4');
98        puts("\t</DataDefinition>");
99 }
100
101 /* Output XML data prologue to stdout */
102 static void
103 data_prologue()
104 {
105        static const char       *bsdf_type[4] = {
106                                        "Reflection Front",
107                                        "Transmission Front",
108                                        "Transmission Back",
109                                        "Reflection Back"
110                                };
111
112        puts("\t<WavelengthData>");
113        puts("\t\t<LayerNumber>System</LayerNumber>");
114        puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>");
115        puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
116        puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>");
117        puts("\t\t<WavelengthDataBlock>");
118        printf("\t\t\t<WavelengthDataDirection>%s</WavelengthDataDirection>\n",
119                        bsdf_type[(input_orient>0)<<1 | (output_orient>0)]);
120        puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>");
121        puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>");
122        puts("\t\t\t<ScatteringData>");
123 }
124
125 /* Output XML data epilogue to stdout */
126 static void
127 data_epilogue(void)
128 {
129        puts("\t\t\t</ScatteringData>");
130        puts("\t\t</WavelengthDataBlock>");
131        puts("\t</WavelengthData>");
132 }
133
134 /* Output XML epilogue to stdout */
135 static void
136 xml_epilogue(void)
137 {
138        puts("</Layer>");
139        puts("</Optical>");
140        puts("</WindowElement>");
141 }
142
114   /* Compute absolute relative difference */
115   static double
116   abs_diff(double v1, double v0)
# Line 157 | Line 128 | static void
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          double          iovec[6];
140 <        float           bsdf;
140 >        float           bsdf, uv[2];
141  
167        data_prologue();                        /* begin output */
142          if (pctcull >= 0) {
143 <                sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d",
144 <                                pctcull, samp_order);
171 <                fflush(stdout);
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 179 | Line 152 | eval_isotropic(char *funame)
152   #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
153                  flockfile(ofp);
154   #endif
155 <        } else
156 <                fputs("{\n", stdout);
157 <                                                /* need to assign Dx, Dy, Dz? */
158 <        if (funame != NULL)
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 = NULL;
205 <                iovec[0] = 2.*(ix+.5)/sqres - 1.;
206 <                iovec[1] = .0;
207 <                iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]);
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 <                    float       last_bsdf = -1;
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(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres);
236 <                        iovec[5] = output_orient *
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 <                        if (funame == NULL)
240 <                            bsdf = eval_rbfrep(rbf, iovec+3);
241 <                        else {
242 <                            if (assignD) {
243 <                                varset("Dx", '=', -iovec[3]);
244 <                                varset("Dy", '=', -iovec[4]);
245 <                                varset("Dz", '=', -iovec[5]);
246 <                                ++eclock;
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 <                            bsdf = funvalue(funame, 6, iovec);
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, last_bsdf) > ssamp_thresh) {
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[3], ssvec[6], sum = 0;
301 >                                double  ssa[4], ssvec[6], sum = 0;
302                                                  /* super-sample voxel */
303                                  for (ssi = NSSAMP; ssi--; ) {
304 <                                    SDmultiSamp(ssa, 3, (ssi+frandom()) *
304 >                                    SDmultiSamp(ssa, 4, (ssi+frandom()) *
305                                                          (1./NSSAMP));
306 <                                    ssvec[0] = 2.*(ix+ssa[0])/sqres - 1.;
307 <                                    ssvec[1] = .0;
308 <                                    ssvec[2] = input_orient *
309 <                                                sqrt(1. - ssvec[0]*ssvec[0]);
310 <                                    SDsquare2disk(ssvec+3, (ox+ssa[1])/sqres,
311 <                                                (oy+ssa[2])/sqres);
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]);
# Line 233 | Line 325 | eval_isotropic(char *funame)
325                                      }
326                                      sum += funvalue(funame, 6, ssvec);
327                                  }
328 <                                bsdf = sum/NSSAMP;
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);
336 <                        last_bsdf = 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./sqres));
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          }
262        data_epilogue();
263        prog_done();
395   }
396  
397   /* Interpolate and output anisotropic BSDF data */
# Line 268 | Line 399 | static void
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          double          iovec[6];
411 <        float           bsdf;
411 >        float           bsdf, uv[2];
412  
278        data_prologue();                        /* begin output */
413          if (pctcull >= 0) {
414 <                sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d",
415 <                                (input_orient>0 ^ output_orient>0) ? "" : " -a",
416 <                                pctcull, samp_order);
417 <                fflush(stdout);
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",
# Line 291 | Line 426 | eval_anisotropic(char *funame)
426   #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
427                  flockfile(ofp);
428   #endif
429 <        } else
430 <                fputs("{\n", stdout);
431 <                                                /* need to assign Dx, Dy, Dz? */
432 <        if (funame != NULL)
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 = NULL;            /* Klems reversal */
482 <                SDsquare2disk(iovec, 1.-(ix+.5)/sqres, 1.-(iy+.5)/sqres);
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 <                    float       last_bsdf = -1;
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(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres);
511 <                        iovec[5] = output_orient *
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 <                        if (funame == NULL)
515 <                            bsdf = eval_rbfrep(rbf, iovec+3);
516 <                        else {
517 <                            if (assignD) {
518 <                                varset("Dx", '=', -iovec[3]);
519 <                                varset("Dy", '=', -iovec[4]);
520 <                                varset("Dz", '=', -iovec[5]);
521 <                                ++eclock;
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 <                            bsdf = funvalue(funame, 6, iovec);
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, last_bsdf) > ssamp_thresh) {
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])/sqres,
582 <                                                1.-(iy+ssa[1])/sqres);
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])/sqres,
587 <                                                (oy+ssa[3])/sqres);
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]);
# Line 347 | Line 596 | eval_anisotropic(char *funame)
596                                      }
597                                      sum += funvalue(funame, 6, ssvec);
598                                  }
599 <                                bsdf = sum/NSSAMP;
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);
607 <                        last_bsdf = 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                      }
360                }
620                  if (rbf != NULL)
621                          free(rbf);
622                  prog_show((ix*sqres+iy+1.)/(sqres*sqres));
623              }
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);
639 <        data_epilogue();
640 <        prog_done();
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 + #if defined(_WIN32) || defined(_WIN64)
663 + /* Execute wrapBSDF command (may never return) */
664 + static int
665 + wrap_up(void)
666 + {
667 +        char    cmd[8192];
668 +
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 +        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 + #else
689 + /* Execute wrapBSDF command (may never return) */
690 + static int
691 + wrap_up(void)
692 + {
693 +        char    buf[256];
694 +        char    *compath = getpath((char *)wrapBSDF[0], getenv("PATH"), X_OK);
695 +
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 +        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];
# Line 386 | Line 729 | main(int argc, char *argv[])
729          esupport &= ~(E_INCHAN|E_OUTCHAN);
730          scompile("PI:3.14159265358979323846", NULL, 0);
731          biggerlib();
732 <        for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
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 <                                fcompile(argv[++i]);
742 <                        else
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;
# Line 424 | Line 787 | main(int argc, char *argv[])
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 +        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;
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",
# Line 438 | Line 813 | main(int argc, char *argv[])
813                          goto userr;
814                  }
815                  ++eclock;
816 <                xml_prologue(argc, argv);       /* start XML output */
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;
# Line 457 | Line 834 | main(int argc, char *argv[])
834                          prog_start("Evaluating inside->outside transmission");
835                          (*evf)(argv[i]);
836                  }
837 <                xml_epilogue();                 /* finish XML output & exit */
461 <                return(0);
837 >                return(wrap_up());
838          }
839          if (i < argc) {                         /* open input files if given */
840                  int     nbsdf = 0;
# Line 473 | Line 849 | main(int argc, char *argv[])
849                          if (!load_bsdf_rep(fpin))
850                                  return(1);
851                          fclose(fpin);
476                        if (!nbsdf++)           /* start XML on first dist. */
477                                xml_prologue(argc, argv);
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 <                xml_epilogue();                 /* finish XML output & exit */
486 <                return(0);
863 >                return(wrap_up());
864          }
865          SET_FILE_BINARY(stdin);                 /* load from stdin */
866          if (!load_bsdf_rep(stdin))
867                  return(1);
491        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                  eval_isotropic(NULL);
873          else
874                  eval_anisotropic(NULL);
875 <        xml_epilogue();                         /* finish XML output & exit */
876 <        return(0);
875 >
876 >        return(wrap_up());
877   userr:
878          fprintf(stderr,
879 <        "Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [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} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",
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