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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines