ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/testBSDF.c
Revision: 1.5
Committed: Wed Feb 3 18:33:18 2016 UTC (8 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +12 -6 lines
Log Message:
Added testBSDF build and provided for color (XYZ) output

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 1.5 static const char RCSid[] = "$Id: testBSDF.c,v 1.4 2015/06/26 22:07:53 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     printf(" q theta_i phi_i theta_o phi_o\t Query BSDF for given path (CIE-XYZ)\n");
25 greg 1.1 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 greg 1.5 if (!SDreportError(SDevalBSDF(&val, vout, vin, bsdf), stderr)) {
96     c_ccvt(&val.spec, C_CSXY);
97     printf("%.3e %.3e %.3e\n",
98     val.spec.cx/val.spec.cy*val.cieY,
99     val.cieY,
100     (1.-val.spec.cx-val.spec.cy)/
101     val.spec.cy*val.cieY);
102     }
103 greg 1.1 continue;
104     case 'S': /* sample BSDF */
105     if (bsdf == NULL)
106     goto noBSDFerr;
107     if (!*sskip2(cp,3))
108     break;
109     n = atoi(sskip2(cp,1));
110     vec_from_deg(vin, atof(sskip2(cp,2)), atof(sskip2(cp,3)));
111     while (n-- > 0) {
112     if (SDreportError(SDsampBSDF(&val, vin,
113     rand()*(1./(RAND_MAX+.5)),
114     sflags, bsdf), stderr))
115     break;
116     printf("%.8f %.8f %.8f\n", vin[0], vin[1], vin[2]);
117     }
118     continue;
119     case 'H': /* hemispherical totals */
120     case 'R':
121     case 'T':
122     if (bsdf == NULL)
123     goto noBSDFerr;
124     if (!*sskip2(cp,2))
125     break;
126     if (tolower(*cp) == 'r')
127 greg 1.3 sflags &= ~SDsampT;
128 greg 1.1 else if (tolower(*cp) == 't')
129 greg 1.3 sflags &= ~SDsampR;
130 greg 1.1 vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
131     printf("%.4e\n", SDdirectHemi(vin, sflags, bsdf));
132     continue;
133 greg 1.3 case 'A': /* resolution in proj. steradians */
134 greg 1.1 if (bsdf == NULL)
135     goto noBSDFerr;
136     if (!*sskip2(cp,2))
137     break;
138     vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
139     if (*sskip2(cp,4)) {
140     vec_from_deg(vout, atof(sskip2(cp,3)), atof(sskip2(cp,4)));
141     if (SDreportError(SDsizeBSDF(proja, vin, vout,
142     SDqueryMin+SDqueryMax, bsdf), stderr))
143     continue;
144     } else if (SDreportError(SDsizeBSDF(proja, vin, NULL,
145     SDqueryMin+SDqueryMax, bsdf), stderr))
146     continue;
147     printf("%.4e %.4e\n", proja[0], proja[1]);
148     continue;
149     }
150     Usage(argv[0]);
151     continue;
152     noBSDFerr:
153 greg 1.5 fprintf(stderr, "%s: need to use 'L' command to load BSDF\n", argv[0]);
154 greg 1.1 }
155     return 0;
156     }