1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: bsdfquery.c,v 2.7 2016/08/18 00:52:48 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 |
|
139 |
switch (outfmt) { /* write to stdout */ |
140 |
case 'a': |
141 |
if (repXYZ) { |
142 |
double cieX = sval.spec.cx/sval.spec.cy*sval.cieY; |
143 |
double cieZ = (1. - sval.spec.cx - sval.spec.cy) / |
144 |
sval.spec.cy * sval.cieY; |
145 |
printf("%.6e %.6e %.6e\n", cieX, sval.cieY, cieZ); |
146 |
} else |
147 |
printf("%.6e\n", sval.cieY); |
148 |
break; |
149 |
case 'd': |
150 |
if (repXYZ) { |
151 |
double cieXYZ[3]; |
152 |
cieXYZ[0] = sval.spec.cx/sval.spec.cy*sval.cieY; |
153 |
cieXYZ[1] = sval.cieY; |
154 |
cieXYZ[2] = (1. - sval.spec.cx - sval.spec.cy) / |
155 |
sval.spec.cy * sval.cieY; |
156 |
putbinary(cieXYZ, sizeof(double), 3, stdout); |
157 |
} else |
158 |
putbinary(&sval.cieY, sizeof(double), 1, stdout); |
159 |
break; |
160 |
case 'f': |
161 |
if (repXYZ) { |
162 |
float cieXYZ[3]; |
163 |
cieXYZ[0] = sval.spec.cx/sval.spec.cy*sval.cieY; |
164 |
cieXYZ[1] = sval.cieY; |
165 |
cieXYZ[2] = (1. - sval.spec.cx - sval.spec.cy) / |
166 |
sval.spec.cy * sval.cieY; |
167 |
putbinary(cieXYZ, sizeof(float), 3, stdout); |
168 |
} else { |
169 |
float cieY = sval.cieY; |
170 |
putbinary(&cieY, sizeof(float), 1, stdout); |
171 |
} |
172 |
break; |
173 |
} |
174 |
} |
175 |
/* if (rbf != NULL) free(rbf); */ |
176 |
return(0); |
177 |
userr: |
178 |
fprintf(stderr, "Usage: %s [-c][-fio] bsdf.{sir|xml}\n", progname); |
179 |
return(1); |
180 |
} |