ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfquery.c
Revision: 2.7
Committed: Thu Aug 18 00:52:48 2016 UTC (7 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +8 -7 lines
Log Message:
Switched over to more efficient fread/fwrite replacements getbinary/putbinary

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdfquery.c,v 2.6 2016/01/30 17:34:17 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 "rtio.h"
18 #include "bsdfrep.h"
19
20 char *progname;
21
22 /* Read in a vector pair */
23 static int
24 readIOdir(FVECT idir, FVECT odir, FILE *fp, int fmt)
25 {
26 double dvec[6];
27 float fvec[6];
28
29 switch (fmt) {
30 case 'a':
31 if (fscanf(fp, FVFORMAT, &idir[0], &idir[1], &idir[2]) != 3 ||
32 fscanf(fp, FVFORMAT, &odir[0], &odir[1], &odir[2]) != 3)
33 return(0);
34 break;
35 case 'd':
36 if (getbinary(dvec, sizeof(double), 6, fp) != 6)
37 return(0);
38 VCOPY(idir, dvec);
39 VCOPY(odir, dvec+3);
40 break;
41 case 'f':
42 if (getbinary(fvec, sizeof(float), 6, fp) != 6)
43 return(0);
44 VCOPY(idir, fvec);
45 VCOPY(odir, fvec+3);
46 break;
47 }
48 if ((normalize(idir) == 0) | (normalize(odir) == 0)) {
49 fprintf(stderr, "%s: zero input vector!\n", progname);
50 return(0);
51 }
52 return(1);
53 }
54
55 /* Get BSDF values for the input and output directions given on stdin */
56 int
57 main(int argc, char *argv[])
58 {
59 int repXYZ = 0;
60 int inpXML = -1;
61 int inpfmt = 'a';
62 int outfmt = 'a';
63 RBFNODE *rbf = NULL;
64 int32 prevInpDir = 0;
65 SDData myBSDF;
66 FVECT idir, odir;
67 int n;
68 /* check arguments */
69 progname = argv[0];
70 while (argc > 2 && argv[1][0] == '-') {
71 switch (argv[1][1]) {
72 case 'c': /* color output */
73 repXYZ = 1;
74 break;
75 case 'f': /* i/o format */
76 if (!argv[1][2] || strchr("afd", argv[1][2]) == NULL)
77 goto userr;
78 inpfmt = outfmt = argv[1][2];
79 if (argv[1][3] && strchr("afd", argv[1][3]) != NULL)
80 outfmt = argv[1][3];
81 break;
82 default:
83 goto userr;
84 }
85 ++argv; --argc;
86 }
87 if (argc > 1 && (n = strlen(argv[1])-4) > 0) {
88 if (!strcasecmp(argv[1]+n, ".xml"))
89 inpXML = 1;
90 else if (!strcasecmp(argv[1]+n, ".sir"))
91 inpXML = 0;
92 }
93 if ((argc != 2) | (inpXML < 0))
94 goto userr;
95 /* load BSDF representation */
96 if (inpXML) {
97 SDclearBSDF(&myBSDF, argv[1]);
98 if (SDreportError(SDloadFile(&myBSDF, argv[1]), stderr))
99 return(1);
100 } else {
101 FILE *fp = fopen(argv[1], "rb");
102 if (fp == NULL) {
103 fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
104 progname, argv[1]);
105 return(1);
106 }
107 if (!load_bsdf_rep(fp))
108 return(1);
109 fclose(fp);
110 }
111 /* query BSDF values */
112 while (readIOdir(idir, odir, stdin, inpfmt)) {
113 SDValue sval;
114 if (inpXML) {
115 if (SDreportError(SDevalBSDF(&sval, odir,
116 idir, &myBSDF), stderr))
117 return(1);
118 } else {
119 int32 inpDir = encodedir(idir);
120 if (inpDir != prevInpDir) {
121 if (idir[2] > 0 ^ input_orient > 0) {
122 fprintf(stderr, "%s: input hemisphere error\n",
123 progname);
124 return(1);
125 }
126 if (rbf != NULL) free(rbf);
127 rbf = advect_rbf(idir, 15000);
128 prevInpDir = inpDir;
129 }
130 if (odir[2] > 0 ^ output_orient > 0) {
131 fprintf(stderr, "%s: output hemisphere error\n",
132 progname);
133 return(1);
134 }
135 if (SDreportError(eval_rbfcol(&sval, rbf, odir), stderr))
136 return(1);
137 }
138 if (repXYZ) /* ensure we have CIE (x,y) */
139 c_ccvt(&sval.spec, C_CSXY);
140
141 switch (outfmt) { /* write to stdout */
142 case 'a':
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 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 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 }
177 /* if (rbf != NULL) free(rbf); */
178 return(0);
179 userr:
180 fprintf(stderr, "Usage: %s [-c][-fio] bsdf.{sir|xml}\n", progname);
181 return(1);
182 }