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

Comparing ray/src/cv/bsdfquery.c (file contents):
Revision 2.4 by greg, Thu Aug 21 10:33:48 2014 UTC vs.
Revision 2.14 by greg, Sat Jun 7 05:09:45 2025 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   *  Query values from the given BSDF (scattering interpolant or XML repres.)
6   *  Input query is incident and exiting vectors directed away from surface.
7   *  We normalize.  Output is a BSDF value for the vector pair.
8 + *  A zero length in or out vector is ignored, causing output to be flushed.
9   *  It is wise to sort the input directions to keep identical ones together
10   *  when using a scattering interpolant representation.
11   */
12  
13   #define _USE_MATH_DEFINES
13 #include <stdio.h>
14 #include <string.h>
14   #include <stdlib.h>
15   #include "rtmath.h"
16 + #include "rtio.h"
17   #include "bsdfrep.h"
18  
19 char    *progname;
20
19   /* Read in a vector pair */
20   static int
21   readIOdir(FVECT idir, FVECT odir, FILE *fp, int fmt)
22   {
23          double  dvec[6];
24          float   fvec[6];
25 <
25 > tryagain:
26          switch (fmt) {
27          case 'a':
28 <                if (fscanf(fp, FVFORMAT, dvec, dvec+1, dvec+2) != 3 ||
29 <                                fscanf(fp, FVFORMAT, dvec+3, dvec+4, dvec+5) != 3)
28 >                if (fscanf(fp, FVFORMAT, &idir[0], &idir[1], &idir[2]) != 3 ||
29 >                                fscanf(fp, FVFORMAT, &odir[0], &odir[1], &odir[2]) != 3)
30                          return(0);
33                VCOPY(idir, dvec);
34                VCOPY(odir, dvec+3);
31                  break;
32          case 'd':
33 <                if (fread(dvec, sizeof(double), 6, fp) != 6)
33 >                if (getbinary(dvec, sizeof(double), 6, fp) != 6)
34                          return(0);
35                  VCOPY(idir, dvec);
36                  VCOPY(odir, dvec+3);
37                  break;
38          case 'f':
39 <                if (fread(fvec, sizeof(float), 6, fp) != 6)
39 >                if (getbinary(fvec, sizeof(float), 6, fp) != 6)
40                          return(0);
41                  VCOPY(idir, fvec);
42                  VCOPY(odir, fvec+3);
43                  break;
44          }
45          if ((normalize(idir) == 0) | (normalize(odir) == 0)) {
46 <                fprintf(stderr, "%s: zero input vector!\n", progname);
47 <                return(0);
46 >                fflush(stdout);         /* desired side-effect? */
47 >                goto tryagain;
48          }
49          return(1);
50   }
# Line 57 | Line 53 | readIOdir(FVECT idir, FVECT odir, FILE *fp, int fmt)
53   int
54   main(int argc, char *argv[])
55   {
56 +        int     unbuffered = 0;
57 +        int     repXYZ = 0;
58          int     inpXML = -1;
59          int     inpfmt = 'a';
60          int     outfmt = 'a';
# Line 65 | Line 63 | main(int argc, char *argv[])
63          SDData  myBSDF;
64          FVECT   idir, odir;
65          int     n;
66 +                                                /* set global progname */
67 +        fixargv0(argv[0]);
68                                                  /* check arguments */
69 <        progname = argv[0];
70 <        if (argc > 2 && argv[1][0] == '-' && argv[1][1] == 'f' &&
71 <                        argv[1][2] && strchr("afd", argv[1][2]) != NULL) {
72 <                inpfmt = outfmt = argv[1][2];
73 <                if (argv[1][3] && strchr("afd", argv[1][3]) != NULL)
74 <                        outfmt = argv[1][3];
69 >        while (argc > 2 && argv[1][0] == '-') {
70 >                switch (argv[1][1]) {
71 >                case 'u':                       /* unbuffered output */
72 >                        unbuffered = 1;
73 >                        break;
74 >                case 'c':                       /* color output */
75 >                        repXYZ = 1;
76 >                        break;
77 >                case 'f':                       /* i/o format */
78 >                        if (!argv[1][2] || strchr("afd", argv[1][2]) == NULL)
79 >                                goto userr;
80 >                        inpfmt = outfmt = argv[1][2];
81 >                        if (argv[1][3] && strchr("afd", argv[1][3]) != NULL)
82 >                                outfmt = argv[1][3];
83 >                        break;
84 >                default:
85 >                        goto userr;
86 >                }
87                  ++argv; --argc;
88          }
89          if (argc > 1 && (n = strlen(argv[1])-4) > 0) {
# Line 80 | Line 92 | main(int argc, char *argv[])
92                  else if (!strcasecmp(argv[1]+n, ".sir"))
93                          inpXML = 0;
94          }
95 <        if ((argc != 2) | (inpXML < 0)) {
96 <                fprintf(stderr, "Usage: %s [-fio] bsdf.{sir|xml}\n", progname);
85 <                return(1);
86 <        }
95 >        if ((argc != 2) | (inpXML < 0))
96 >                goto userr;
97                                                  /* load BSDF representation */
98          if (inpXML) {
99                  SDclearBSDF(&myBSDF, argv[1]);
# Line 102 | Line 112 | main(int argc, char *argv[])
112          }
113                                                  /* query BSDF values */
114          while (readIOdir(idir, odir, stdin, inpfmt)) {
115 <                double  bsdf;
106 <                float   fval;
115 >                SDValue sval;
116                  if (inpXML) {
108                        SDValue sval;
117                          if (SDreportError(SDevalBSDF(&sval, odir,
118                                                  idir, &myBSDF), stderr))
119                                  return(1);
112                        bsdf = sval.cieY;
120                  } else {
121                          int32   inpDir = encodedir(idir);
122                          if (inpDir != prevInpDir) {
# Line 127 | Line 134 | main(int argc, char *argv[])
134                                                  progname);
135                                  return(1);
136                          }
137 <                        bsdf = eval_rbfrep(rbf, odir);
137 >                        if (SDreportError(eval_rbfcol(&sval, rbf, odir), stderr))
138 >                                return(1);
139                  }
140 +
141                  switch (outfmt) {               /* write to stdout */
142                  case 'a':
143 <                        printf("%.6e\n", bsdf);
143 >                        if (repXYZ) {
144 >                                double  cieX = sval.spec.cx/sval.spec.cy*sval.cieY;
145 >                                double  cieZ = (1. - sval.spec.cx - sval.spec.cy) /
146 >                                                sval.spec.cy * sval.cieY;
147 >                                printf("%.6e %.6e %.6e\n", cieX, sval.cieY, cieZ);
148 >                        } else
149 >                                printf("%.6e\n", sval.cieY);
150                          break;
151                  case 'd':
152 <                        fwrite(&bsdf, sizeof(double), 1, stdout);
152 >                        if (repXYZ) {
153 >                                double  cieXYZ[3];
154 >                                cieXYZ[0] = sval.spec.cx/sval.spec.cy*sval.cieY;
155 >                                cieXYZ[1] = sval.cieY;
156 >                                cieXYZ[2] = (1. - sval.spec.cx - sval.spec.cy) /
157 >                                                sval.spec.cy * sval.cieY;
158 >                                putbinary(cieXYZ, sizeof(double), 3, stdout);
159 >                        } else
160 >                                putbinary(&sval.cieY, sizeof(double), 1, stdout);
161                          break;
162                  case 'f':
163 <                        fval = bsdf;
164 <                        fwrite(&fval, sizeof(float), 1, stdout);
163 >                        if (repXYZ) {
164 >                                float   cieXYZ[3];
165 >                                cieXYZ[0] = sval.spec.cx/sval.spec.cy*sval.cieY;
166 >                                cieXYZ[1] = sval.cieY;
167 >                                cieXYZ[2] = (1. - sval.spec.cx - sval.spec.cy) /
168 >                                                sval.spec.cy * sval.cieY;
169 >                                putbinary(cieXYZ, sizeof(float), 3, stdout);
170 >                        } else {
171 >                                float   cieY = sval.cieY;
172 >                                putbinary(&cieY, sizeof(float), 1, stdout);
173 >                        }
174                          break;
175                  }
176 +                if (unbuffered)
177 +                        fflush(stdout);
178          }
179 +        /* if (rbf != NULL) free(rbf); */
180          return(0);
181 + userr:
182 +        fprintf(stderr, "Usage: %s [-u][-c][-fio] bsdf.{sir|xml}\n", progname);
183 +        return(1);
184   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines