ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/testBSDF.c
Revision: 1.8
Committed: Thu Feb 2 00:34:55 2017 UTC (7 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.7: +13 -13 lines
Log Message:
Eliminated comparisons to NULL

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 1.8 static const char RCSid[] = "$Id: testBSDF.c,v 1.7 2017/02/02 00:30:16 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.1 printf(" s N theta phi\t\t\t Generate N ray directions at given incidence\n");
28     printf(" h theta phi\t\t\t Report hemispherical total at given incidence\n");
29     printf(" r theta phi\t\t\t Report hemispherical reflection at given incidence\n");
30     printf(" t theta phi\t\t\t Report hemispherical transmission at given incidence\n");
31     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     printf("%s%.3e %.3e %.3e\n", intro,
51     vp->spec.cx/vp->spec.cy*vp->cieY,
52     vp->cieY,
53     (1.-vp->spec.cx-vp->spec.cy)/
54     vp->spec.cy*vp->cieY);
55     }
56    
57 greg 1.1 int
58     main(int argc, char *argv[])
59     {
60 greg 1.3 const char *directory = NULL;
61 greg 1.1 char inp[512], path[512];
62     const SDData *bsdf = NULL;
63    
64     if (argc > 2 || (argc == 2 && argv[1][0] == '-')) {
65     Usage(argv[0]);
66     return 1;
67     }
68     if (argc == 2)
69     directory = argv[1];
70    
71 greg 1.3 SDretainSet = SDretainBSDFs; /* keep BSDFs in memory */
72 greg 1.1
73     /* loop on command */
74 greg 1.8 while (fgets(inp, sizeof(inp), stdin)) {
75 greg 1.1 int sflags = SDsampAll;
76     char *cp = inp;
77     char *cp2;
78     FVECT vin, vout;
79     double proja[2];
80     int n;
81     SDValue val;
82    
83     while (isspace(*cp)) cp++;
84    
85 greg 1.4 switch (toupper(*cp)) {
86 greg 1.1 case 'L': /* load/activate BSDF input */
87     cp2 = cp = sskip2(cp, 1);
88     if (!*cp)
89     break;
90     while (*cp) cp++;
91     while (isspace(*--cp)) *cp = '\0';
92 greg 1.3 if (directory)
93 greg 1.1 sprintf(path, "%s/%s", directory, cp2);
94     else
95     strcpy(path, cp2);
96 greg 1.3 if (bsdf)
97 greg 1.2 SDfreeCache(bsdf);
98 greg 1.1 bsdf = SDcacheFile(path);
99     continue;
100 greg 1.6 case 'I': /* report general info. */
101 greg 1.8 if (!bsdf)
102 greg 1.6 goto noBSDFerr;
103     printf("Material: '%s'\n", bsdf->matn);
104     printf("Manufacturer: '%s'\n", bsdf->makr);
105     printf("Width, Height, Thickness (m): %.4e, %.4e, %.4e\n",
106     bsdf->dim[0], bsdf->dim[1], bsdf->dim[2]);
107 greg 1.8 printf("Has geometry: %s\n", bsdf->mgf ? "yes" : "no");
108 greg 1.6 continue;
109     case 'C': /* report constant values */
110 greg 1.8 if (!bsdf)
111 greg 1.6 goto noBSDFerr;
112 greg 1.8 if (bsdf->rf)
113 greg 1.6 printf("Peak front hemispherical reflectance: %.3e\n",
114     bsdf->rLambFront.cieY +
115     bsdf->rf->maxHemi);
116 greg 1.8 if (bsdf->rb)
117 greg 1.6 printf("Peak back hemispherical reflectance: %.3e\n",
118     bsdf->rLambBack.cieY +
119     bsdf->rb->maxHemi);
120 greg 1.8 if (bsdf->tf)
121 greg 1.6 printf("Peak front hemispherical transmittance: %.3e\n",
122     bsdf->tLamb.cieY + bsdf->tf->maxHemi);
123 greg 1.8 if (bsdf->tb)
124 greg 1.6 printf("Peak back hemispherical transmittance: %.3e\n",
125     bsdf->tLamb.cieY + bsdf->tb->maxHemi);
126     printXYZ("Diffuse Front Reflectance: ", &bsdf->rLambFront);
127     printXYZ("Diffuse Back Reflectance: ", &bsdf->rLambBack);
128     printXYZ("Diffuse Transmittance: ", &bsdf->tLamb);
129     continue;
130 greg 1.1 case 'Q': /* query BSDF value */
131 greg 1.8 if (!bsdf)
132 greg 1.1 goto noBSDFerr;
133     if (!*sskip2(cp,4))
134     break;
135     vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
136     vec_from_deg(vout, atof(sskip2(cp,3)), atof(sskip2(cp,4)));
137 greg 1.6 if (!SDreportError(SDevalBSDF(&val, vout, vin, bsdf), stderr))
138     printXYZ("", &val);
139 greg 1.1 continue;
140     case 'S': /* sample BSDF */
141 greg 1.8 if (!bsdf)
142 greg 1.1 goto noBSDFerr;
143     if (!*sskip2(cp,3))
144     break;
145     n = atoi(sskip2(cp,1));
146     vec_from_deg(vin, atof(sskip2(cp,2)), atof(sskip2(cp,3)));
147     while (n-- > 0) {
148     if (SDreportError(SDsampBSDF(&val, vin,
149     rand()*(1./(RAND_MAX+.5)),
150     sflags, bsdf), stderr))
151     break;
152     printf("%.8f %.8f %.8f\n", vin[0], vin[1], vin[2]);
153     }
154     continue;
155     case 'H': /* hemispherical totals */
156     case 'R':
157     case 'T':
158 greg 1.8 if (!bsdf)
159 greg 1.1 goto noBSDFerr;
160     if (!*sskip2(cp,2))
161     break;
162     if (tolower(*cp) == 'r')
163 greg 1.3 sflags &= ~SDsampT;
164 greg 1.1 else if (tolower(*cp) == 't')
165 greg 1.3 sflags &= ~SDsampR;
166 greg 1.1 vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
167     printf("%.4e\n", SDdirectHemi(vin, sflags, bsdf));
168     continue;
169 greg 1.3 case 'A': /* resolution in proj. steradians */
170 greg 1.8 if (!bsdf)
171 greg 1.1 goto noBSDFerr;
172     if (!*sskip2(cp,2))
173     break;
174     vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
175     if (*sskip2(cp,4)) {
176     vec_from_deg(vout, atof(sskip2(cp,3)), atof(sskip2(cp,4)));
177     if (SDreportError(SDsizeBSDF(proja, vin, vout,
178     SDqueryMin+SDqueryMax, bsdf), stderr))
179     continue;
180     } else if (SDreportError(SDsizeBSDF(proja, vin, NULL,
181     SDqueryMin+SDqueryMax, bsdf), stderr))
182     continue;
183     printf("%.4e %.4e\n", proja[0], proja[1]);
184     continue;
185     }
186     Usage(argv[0]);
187     continue;
188     noBSDFerr:
189 greg 1.6 fprintf(stderr, "%s: First, use 'L' command to load BSDF\n", argv[0]);
190 greg 1.1 }
191     return 0;
192     }