ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/testBSDF.c
Revision: 1.12
Committed: Mon May 15 22:15:40 2017 UTC (7 years ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R1
Changes since 1.11: +2 -2 lines
Log Message:
Reversed ordering of in/out vectors for consistency between "A" and "Q" commands

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 1.12 static const char RCSid[] = "$Id: testBSDF.c,v 1.11 2017/04/11 16:14:05 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * Simple test program to demonstrate BSDF operation.
6     *
7     * G. Ward June 2015
8     */
9    
10     #define _USE_MATH_DEFINES
11     #include <stdio.h>
12     #include <stdlib.h>
13     #include <math.h>
14     #include <ctype.h>
15     #include "rtio.h"
16     #include "bsdf.h"
17    
18     static void
19     Usage(const char *prog)
20     {
21     printf("Usage: %s [bsdf_directory]\n", prog);
22     printf("Input commands:\n");
23 greg 1.5 printf(" L bsdf.xml\t\t\t Load (make active) given BSDF input file\n");
24 greg 1.6 printf(" i\t\t\t\t Report general information (metadata)\n");
25     printf(" c\t\t\t\t Report diffuse and specular components\n");
26 greg 1.5 printf(" q theta_i phi_i theta_o phi_o\t Query BSDF for given path (CIE-XYZ)\n");
27 greg 1.10 printf(" s[r|t][s|d] N theta phi\t Generate N ray directions & colors at given incidence\n");
28     printf(" h[s|d] theta phi\t\t Report hemispherical scattering at given incidence\n");
29     printf(" r[s|d] theta phi\t\t Report hemispherical reflection at given incidence\n");
30     printf(" t[s|d] theta phi\t\t Report hemispherical transmission at given incidence\n");
31 greg 1.1 printf(" a theta phi [t2 p2]\t\t Report resolution (in proj. steradians) for given direction(s)\n");
32     printf(" ^D\t\t\t\t Quit program\n");
33     }
34    
35     static void
36     vec_from_deg(FVECT v, double theta, double phi)
37     {
38     const double DEG = M_PI/180.;
39    
40     theta *= DEG; phi *= DEG;
41     v[0] = v[1] = sin(theta);
42     v[0] *= cos(phi);
43     v[1] *= sin(phi);
44     v[2] = cos(theta);
45     }
46    
47 greg 1.6 static void
48     printXYZ(const char *intro, const SDValue *vp)
49     {
50 greg 1.11 if (vp->cieY <= 1e-9) {
51     printf("%s0 0 0\n", intro);
52     return;
53     }
54 greg 1.6 printf("%s%.3e %.3e %.3e\n", intro,
55     vp->spec.cx/vp->spec.cy*vp->cieY,
56     vp->cieY,
57     (1.-vp->spec.cx-vp->spec.cy)/
58     vp->spec.cy*vp->cieY);
59     }
60    
61 greg 1.1 int
62     main(int argc, char *argv[])
63     {
64 greg 1.3 const char *directory = NULL;
65 greg 1.1 char inp[512], path[512];
66     const SDData *bsdf = NULL;
67    
68     if (argc > 2 || (argc == 2 && argv[1][0] == '-')) {
69     Usage(argv[0]);
70     return 1;
71     }
72     if (argc == 2)
73     directory = argv[1];
74    
75 greg 1.3 SDretainSet = SDretainBSDFs; /* keep BSDFs in memory */
76 greg 1.1
77     /* loop on command */
78 greg 1.8 while (fgets(inp, sizeof(inp), stdin)) {
79 greg 1.1 int sflags = SDsampAll;
80     char *cp = inp;
81     char *cp2;
82     FVECT vin, vout;
83     double proja[2];
84 greg 1.9 int n, i;
85 greg 1.1 SDValue val;
86    
87     while (isspace(*cp)) cp++;
88    
89 greg 1.4 switch (toupper(*cp)) {
90 greg 1.1 case 'L': /* load/activate BSDF input */
91     cp2 = cp = sskip2(cp, 1);
92     if (!*cp)
93     break;
94     while (*cp) cp++;
95     while (isspace(*--cp)) *cp = '\0';
96 greg 1.3 if (directory)
97 greg 1.1 sprintf(path, "%s/%s", directory, cp2);
98     else
99     strcpy(path, cp2);
100 greg 1.3 if (bsdf)
101 greg 1.2 SDfreeCache(bsdf);
102 greg 1.1 bsdf = SDcacheFile(path);
103     continue;
104 greg 1.6 case 'I': /* report general info. */
105 greg 1.8 if (!bsdf)
106 greg 1.6 goto noBSDFerr;
107     printf("Material: '%s'\n", bsdf->matn);
108     printf("Manufacturer: '%s'\n", bsdf->makr);
109     printf("Width, Height, Thickness (m): %.4e, %.4e, %.4e\n",
110     bsdf->dim[0], bsdf->dim[1], bsdf->dim[2]);
111 greg 1.8 printf("Has geometry: %s\n", bsdf->mgf ? "yes" : "no");
112 greg 1.6 continue;
113     case 'C': /* report constant values */
114 greg 1.8 if (!bsdf)
115 greg 1.6 goto noBSDFerr;
116 greg 1.8 if (bsdf->rf)
117 greg 1.6 printf("Peak front hemispherical reflectance: %.3e\n",
118     bsdf->rLambFront.cieY +
119     bsdf->rf->maxHemi);
120 greg 1.8 if (bsdf->rb)
121 greg 1.6 printf("Peak back hemispherical reflectance: %.3e\n",
122     bsdf->rLambBack.cieY +
123     bsdf->rb->maxHemi);
124 greg 1.8 if (bsdf->tf)
125 greg 1.6 printf("Peak front hemispherical transmittance: %.3e\n",
126     bsdf->tLamb.cieY + bsdf->tf->maxHemi);
127 greg 1.8 if (bsdf->tb)
128 greg 1.6 printf("Peak back hemispherical transmittance: %.3e\n",
129     bsdf->tLamb.cieY + bsdf->tb->maxHemi);
130     printXYZ("Diffuse Front Reflectance: ", &bsdf->rLambFront);
131     printXYZ("Diffuse Back Reflectance: ", &bsdf->rLambBack);
132     printXYZ("Diffuse Transmittance: ", &bsdf->tLamb);
133     continue;
134 greg 1.1 case 'Q': /* query BSDF value */
135 greg 1.8 if (!bsdf)
136 greg 1.1 goto noBSDFerr;
137     if (!*sskip2(cp,4))
138     break;
139     vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
140     vec_from_deg(vout, atof(sskip2(cp,3)), atof(sskip2(cp,4)));
141 greg 1.6 if (!SDreportError(SDevalBSDF(&val, vout, vin, bsdf), stderr))
142     printXYZ("", &val);
143 greg 1.1 continue;
144     case 'S': /* sample BSDF */
145 greg 1.8 if (!bsdf)
146 greg 1.1 goto noBSDFerr;
147     if (!*sskip2(cp,3))
148     break;
149 greg 1.9 if (toupper(cp[1]) == 'R') {
150     sflags &= ~SDsampT;
151     ++cp;
152     } else if (toupper(cp[1]) == 'T') {
153     sflags &= ~SDsampR;
154     ++cp;
155     }
156     if (toupper(cp[1]) == 'S')
157     sflags &= ~SDsampDf;
158     else if (toupper(cp[1]) == 'D')
159     sflags &= ~SDsampSp;
160     i = n = atoi(sskip2(cp,1));
161 greg 1.1 vec_from_deg(vin, atof(sskip2(cp,2)), atof(sskip2(cp,3)));
162 greg 1.9 while (i-- > 0) {
163     VCOPY(vout, vin);
164     if (SDreportError(SDsampBSDF(&val, vout,
165     (i+rand()*(1./(RAND_MAX+.5)))/(double)n,
166 greg 1.1 sflags, bsdf), stderr))
167     break;
168 greg 1.9 printf("%.8f %.8f %.8f ", vout[0], vout[1], vout[2]);
169     printXYZ("", &val);
170 greg 1.1 }
171     continue;
172 greg 1.9 case 'H': /* hemispherical values */
173 greg 1.1 case 'R':
174     case 'T':
175 greg 1.8 if (!bsdf)
176 greg 1.1 goto noBSDFerr;
177     if (!*sskip2(cp,2))
178     break;
179 greg 1.9 if (toupper(cp[0]) == 'R')
180 greg 1.3 sflags &= ~SDsampT;
181 greg 1.9 else if (toupper(cp[0]) == 'T')
182 greg 1.3 sflags &= ~SDsampR;
183 greg 1.9 if (toupper(cp[1]) == 'S')
184     sflags &= ~SDsampDf;
185     else if (toupper(cp[1]) == 'D')
186     sflags &= ~SDsampSp;
187 greg 1.1 vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
188     printf("%.4e\n", SDdirectHemi(vin, sflags, bsdf));
189     continue;
190 greg 1.3 case 'A': /* resolution in proj. steradians */
191 greg 1.8 if (!bsdf)
192 greg 1.1 goto noBSDFerr;
193     if (!*sskip2(cp,2))
194     break;
195     vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
196     if (*sskip2(cp,4)) {
197     vec_from_deg(vout, atof(sskip2(cp,3)), atof(sskip2(cp,4)));
198 greg 1.12 if (SDreportError(SDsizeBSDF(proja, vout, vin,
199 greg 1.1 SDqueryMin+SDqueryMax, bsdf), stderr))
200     continue;
201     } else if (SDreportError(SDsizeBSDF(proja, vin, NULL,
202     SDqueryMin+SDqueryMax, bsdf), stderr))
203     continue;
204     printf("%.4e %.4e\n", proja[0], proja[1]);
205     continue;
206     }
207     Usage(argv[0]);
208     continue;
209     noBSDFerr:
210 greg 1.6 fprintf(stderr, "%s: First, use 'L' command to load BSDF\n", argv[0]);
211 greg 1.1 }
212     return 0;
213     }