ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/testBSDF.c
Revision: 1.4
Committed: Fri Jun 26 22:07:53 2015 UTC (9 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R0
Changes since 1.3: +2 -9 lines
Log Message:
Simplified switch statement

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 1.4 static const char RCSid[] = "$Id: testBSDF.c,v 1.3 2015/06/06 16:30:26 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     printf(" l bsdf.xml\t\t\t Load (make active) given BSDF input file\n");
24     printf(" q theta_i phi_i theta_o phi_o\t Query BSDF for given path\n");
25     printf(" s N theta phi\t\t\t Generate N ray directions at given incidence\n");
26     printf(" h theta phi\t\t\t Report hemispherical total at given incidence\n");
27     printf(" r theta phi\t\t\t Report hemispherical reflection at given incidence\n");
28     printf(" t theta phi\t\t\t Report hemispherical transmission at given incidence\n");
29     printf(" a theta phi [t2 p2]\t\t Report resolution (in proj. steradians) for given direction(s)\n");
30     printf(" ^D\t\t\t\t Quit program\n");
31     }
32    
33     static void
34     vec_from_deg(FVECT v, double theta, double phi)
35     {
36     const double DEG = M_PI/180.;
37    
38     theta *= DEG; phi *= DEG;
39     v[0] = v[1] = sin(theta);
40     v[0] *= cos(phi);
41     v[1] *= sin(phi);
42     v[2] = cos(theta);
43     }
44    
45     int
46     main(int argc, char *argv[])
47     {
48 greg 1.3 const char *directory = NULL;
49 greg 1.1 char inp[512], path[512];
50     const SDData *bsdf = NULL;
51    
52     if (argc > 2 || (argc == 2 && argv[1][0] == '-')) {
53     Usage(argv[0]);
54     return 1;
55     }
56     if (argc == 2)
57     directory = argv[1];
58    
59 greg 1.3 SDretainSet = SDretainBSDFs; /* keep BSDFs in memory */
60 greg 1.1
61     /* loop on command */
62     while (fgets(inp, sizeof(inp), stdin) != NULL) {
63     int sflags = SDsampAll;
64     char *cp = inp;
65     char *cp2;
66     FVECT vin, vout;
67     double proja[2];
68     int n;
69     SDValue val;
70    
71     while (isspace(*cp)) cp++;
72    
73 greg 1.4 switch (toupper(*cp)) {
74 greg 1.1 case 'L': /* load/activate BSDF input */
75     cp2 = cp = sskip2(cp, 1);
76     if (!*cp)
77     break;
78     while (*cp) cp++;
79     while (isspace(*--cp)) *cp = '\0';
80 greg 1.3 if (directory)
81 greg 1.1 sprintf(path, "%s/%s", directory, cp2);
82     else
83     strcpy(path, cp2);
84 greg 1.3 if (bsdf)
85 greg 1.2 SDfreeCache(bsdf);
86 greg 1.1 bsdf = SDcacheFile(path);
87     continue;
88     case 'Q': /* query BSDF value */
89     if (bsdf == NULL)
90     goto noBSDFerr;
91     if (!*sskip2(cp,4))
92     break;
93     vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
94     vec_from_deg(vout, atof(sskip2(cp,3)), atof(sskip2(cp,4)));
95     if (!SDreportError(SDevalBSDF(&val, vout, vin, bsdf), stderr))
96     printf("%.3e\n", val.cieY);
97     continue;
98     case 'S': /* sample BSDF */
99     if (bsdf == NULL)
100     goto noBSDFerr;
101     if (!*sskip2(cp,3))
102     break;
103     n = atoi(sskip2(cp,1));
104     vec_from_deg(vin, atof(sskip2(cp,2)), atof(sskip2(cp,3)));
105     while (n-- > 0) {
106     if (SDreportError(SDsampBSDF(&val, vin,
107     rand()*(1./(RAND_MAX+.5)),
108     sflags, bsdf), stderr))
109     break;
110     printf("%.8f %.8f %.8f\n", vin[0], vin[1], vin[2]);
111     }
112     continue;
113     case 'H': /* hemispherical totals */
114     case 'R':
115     case 'T':
116     if (bsdf == NULL)
117     goto noBSDFerr;
118     if (!*sskip2(cp,2))
119     break;
120     if (tolower(*cp) == 'r')
121 greg 1.3 sflags &= ~SDsampT;
122 greg 1.1 else if (tolower(*cp) == 't')
123 greg 1.3 sflags &= ~SDsampR;
124 greg 1.1 vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
125     printf("%.4e\n", SDdirectHemi(vin, sflags, bsdf));
126     continue;
127 greg 1.3 case 'A': /* resolution in proj. steradians */
128 greg 1.1 if (bsdf == NULL)
129     goto noBSDFerr;
130     if (!*sskip2(cp,2))
131     break;
132     vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
133     if (*sskip2(cp,4)) {
134     vec_from_deg(vout, atof(sskip2(cp,3)), atof(sskip2(cp,4)));
135     if (SDreportError(SDsizeBSDF(proja, vin, vout,
136     SDqueryMin+SDqueryMax, bsdf), stderr))
137     continue;
138     } else if (SDreportError(SDsizeBSDF(proja, vin, NULL,
139     SDqueryMin+SDqueryMax, bsdf), stderr))
140     continue;
141     printf("%.4e %.4e\n", proja[0], proja[1]);
142     continue;
143     }
144     Usage(argv[0]);
145     continue;
146     noBSDFerr:
147     fprintf(stderr, "%s: need to use 'l' command to load BSDF\n", argv[0]);
148     }
149     return 0;
150     }