ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfquery.c
Revision: 2.6
Committed: Sat Jan 30 17:34:17 2016 UTC (8 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +56 -19 lines
Log Message:
Added -c option to output XYZ color

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdfquery.c,v 2.5 2016/01/20 19:43:34 greg Exp $";
3 #endif
4 /*
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 * It is wise to sort the input directions to keep identical ones together
9 * when using a scattering interpolant representation.
10 */
11
12 #define _USE_MATH_DEFINES
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include "rtmath.h"
17 #include "bsdfrep.h"
18
19 char *progname;
20
21 /* Read in a vector pair */
22 static int
23 readIOdir(FVECT idir, FVECT odir, FILE *fp, int fmt)
24 {
25 double dvec[6];
26 float fvec[6];
27
28 switch (fmt) {
29 case 'a':
30 if (fscanf(fp, FVFORMAT, &idir[0], &idir[1], &idir[2]) != 3 ||
31 fscanf(fp, FVFORMAT, &odir[0], &odir[1], &odir[2]) != 3)
32 return(0);
33 break;
34 case 'd':
35 if (fread(dvec, sizeof(double), 6, fp) != 6)
36 return(0);
37 VCOPY(idir, dvec);
38 VCOPY(odir, dvec+3);
39 break;
40 case 'f':
41 if (fread(fvec, sizeof(float), 6, fp) != 6)
42 return(0);
43 VCOPY(idir, fvec);
44 VCOPY(odir, fvec+3);
45 break;
46 }
47 if ((normalize(idir) == 0) | (normalize(odir) == 0)) {
48 fprintf(stderr, "%s: zero input vector!\n", progname);
49 return(0);
50 }
51 return(1);
52 }
53
54 /* Get BSDF values for the input and output directions given on stdin */
55 int
56 main(int argc, char *argv[])
57 {
58 int repXYZ = 0;
59 int inpXML = -1;
60 int inpfmt = 'a';
61 int outfmt = 'a';
62 RBFNODE *rbf = NULL;
63 int32 prevInpDir = 0;
64 SDData myBSDF;
65 FVECT idir, odir;
66 int n;
67 /* check arguments */
68 progname = argv[0];
69 while (argc > 2 && argv[1][0] == '-') {
70 switch (argv[1][1]) {
71 case 'c': /* color output */
72 repXYZ = 1;
73 break;
74 case 'f': /* i/o format */
75 if (!argv[1][2] || strchr("afd", argv[1][2]) == NULL)
76 goto userr;
77 inpfmt = outfmt = argv[1][2];
78 if (argv[1][3] && strchr("afd", argv[1][3]) != NULL)
79 outfmt = argv[1][3];
80 break;
81 default:
82 goto userr;
83 }
84 ++argv; --argc;
85 }
86 if (argc > 1 && (n = strlen(argv[1])-4) > 0) {
87 if (!strcasecmp(argv[1]+n, ".xml"))
88 inpXML = 1;
89 else if (!strcasecmp(argv[1]+n, ".sir"))
90 inpXML = 0;
91 }
92 if ((argc != 2) | (inpXML < 0))
93 goto userr;
94 /* load BSDF representation */
95 if (inpXML) {
96 SDclearBSDF(&myBSDF, argv[1]);
97 if (SDreportError(SDloadFile(&myBSDF, argv[1]), stderr))
98 return(1);
99 } else {
100 FILE *fp = fopen(argv[1], "rb");
101 if (fp == NULL) {
102 fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
103 progname, argv[1]);
104 return(1);
105 }
106 if (!load_bsdf_rep(fp))
107 return(1);
108 fclose(fp);
109 }
110 /* query BSDF values */
111 while (readIOdir(idir, odir, stdin, inpfmt)) {
112 SDValue sval;
113 if (inpXML) {
114 if (SDreportError(SDevalBSDF(&sval, odir,
115 idir, &myBSDF), stderr))
116 return(1);
117 } else {
118 int32 inpDir = encodedir(idir);
119 if (inpDir != prevInpDir) {
120 if (idir[2] > 0 ^ input_orient > 0) {
121 fprintf(stderr, "%s: input hemisphere error\n",
122 progname);
123 return(1);
124 }
125 if (rbf != NULL) free(rbf);
126 rbf = advect_rbf(idir, 15000);
127 prevInpDir = inpDir;
128 }
129 if (odir[2] > 0 ^ output_orient > 0) {
130 fprintf(stderr, "%s: output hemisphere error\n",
131 progname);
132 return(1);
133 }
134 if (SDreportError(eval_rbfcol(&sval, rbf, odir), stderr))
135 return(1);
136 }
137 if (repXYZ) /* ensure we have CIE (x,y) */
138 c_ccvt(&sval.spec, C_CSXY);
139
140 switch (outfmt) { /* write to stdout */
141 case 'a':
142 if (repXYZ) {
143 double cieX = sval.spec.cx/sval.spec.cy*sval.cieY;
144 double cieZ = (1. - sval.spec.cx - sval.spec.cy) /
145 sval.spec.cy * sval.cieY;
146 printf("%.6e %.6e %.6e\n", cieX, sval.cieY, cieZ);
147 } else
148 printf("%.6e\n", sval.cieY);
149 break;
150 case 'd':
151 if (repXYZ) {
152 double cieXYZ[3];
153 cieXYZ[0] = sval.spec.cx/sval.spec.cy*sval.cieY;
154 cieXYZ[1] = sval.cieY;
155 cieXYZ[2] = (1. - sval.spec.cx - sval.spec.cy) /
156 sval.spec.cy * sval.cieY;
157 fwrite(cieXYZ, sizeof(double), 3, stdout);
158 } else
159 fwrite(&sval.cieY, sizeof(double), 1, stdout);
160 break;
161 case 'f':
162 if (repXYZ) {
163 float cieXYZ[3];
164 cieXYZ[0] = sval.spec.cx/sval.spec.cy*sval.cieY;
165 cieXYZ[1] = sval.cieY;
166 cieXYZ[2] = (1. - sval.spec.cx - sval.spec.cy) /
167 sval.spec.cy * sval.cieY;
168 fwrite(cieXYZ, sizeof(float), 3, stdout);
169 } else {
170 float cieY = sval.cieY;
171 fwrite(&cieY, sizeof(float), 1, stdout);
172 }
173 break;
174 }
175 }
176 /* if (rbf != NULL) free(rbf); */
177 return(0);
178 userr:
179 fprintf(stderr, "Usage: %s [-c][-fio] bsdf.{sir|xml}\n", progname);
180 return(1);
181 }