ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/testBSDF.c
Revision: 1.1
Committed: Fri Jun 5 17:55:18 2015 UTC (8 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Created testBSDF main module to demonstrate BSDF library

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2     static const char RCSid[] = "$Id$";
3     #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     const char *directory = "";
49     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     SDretainSet = SDretainBSDFs; /* keep BSDFs loaded */
60    
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     switch (*cp) {
74     case 'l':
75     case 'L': /* load/activate BSDF input */
76     cp2 = cp = sskip2(cp, 1);
77     if (!*cp)
78     break;
79     while (*cp) cp++;
80     while (isspace(*--cp)) *cp = '\0';
81     if (directory[0])
82     sprintf(path, "%s/%s", directory, cp2);
83     else
84     strcpy(path, cp2);
85     bsdf = SDcacheFile(path);
86     continue;
87     case 'q':
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':
99     case 'S': /* sample BSDF */
100     if (bsdf == NULL)
101     goto noBSDFerr;
102     if (!*sskip2(cp,3))
103     break;
104     n = atoi(sskip2(cp,1));
105     vec_from_deg(vin, atof(sskip2(cp,2)), atof(sskip2(cp,3)));
106     while (n-- > 0) {
107     if (SDreportError(SDsampBSDF(&val, vin,
108     rand()*(1./(RAND_MAX+.5)),
109     sflags, bsdf), stderr))
110     break;
111     printf("%.8f %.8f %.8f\n", vin[0], vin[1], vin[2]);
112     }
113     continue;
114     case 'h':
115     case 'H': /* hemispherical totals */
116     case 'r':
117     case 'R':
118     case 't':
119     case 'T':
120     if (bsdf == NULL)
121     goto noBSDFerr;
122     if (!*sskip2(cp,2))
123     break;
124     if (tolower(*cp) == 'r')
125     sflags ^= SDsampT;
126     else if (tolower(*cp) == 't')
127     sflags ^= SDsampR;
128     vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
129     printf("%.4e\n", SDdirectHemi(vin, sflags, bsdf));
130     continue;
131     case 'a':
132     case 'A': /* resolution in degrees */
133     if (bsdf == NULL)
134     goto noBSDFerr;
135     if (!*sskip2(cp,2))
136     break;
137     vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
138     if (*sskip2(cp,4)) {
139     vec_from_deg(vout, atof(sskip2(cp,3)), atof(sskip2(cp,4)));
140     if (SDreportError(SDsizeBSDF(proja, vin, vout,
141     SDqueryMin+SDqueryMax, bsdf), stderr))
142     continue;
143     } else if (SDreportError(SDsizeBSDF(proja, vin, NULL,
144     SDqueryMin+SDqueryMax, bsdf), stderr))
145     continue;
146     printf("%.4e %.4e\n", proja[0], proja[1]);
147     continue;
148     }
149     Usage(argv[0]);
150     continue;
151     noBSDFerr:
152     fprintf(stderr, "%s: need to use 'l' command to load BSDF\n", argv[0]);
153     }
154     return 0;
155     }