| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: testBSDF.c,v 1.2 2015/06/05 18:58:37 greg Exp $";
|
| 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 = NULL;
|
| 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 in memory */
|
| 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)
|
| 82 |
sprintf(path, "%s/%s", directory, cp2);
|
| 83 |
else
|
| 84 |
strcpy(path, cp2);
|
| 85 |
if (bsdf)
|
| 86 |
SDfreeCache(bsdf);
|
| 87 |
bsdf = SDcacheFile(path);
|
| 88 |
continue;
|
| 89 |
case 'q':
|
| 90 |
case 'Q': /* query BSDF value */
|
| 91 |
if (bsdf == NULL)
|
| 92 |
goto noBSDFerr;
|
| 93 |
if (!*sskip2(cp,4))
|
| 94 |
break;
|
| 95 |
vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
|
| 96 |
vec_from_deg(vout, atof(sskip2(cp,3)), atof(sskip2(cp,4)));
|
| 97 |
if (!SDreportError(SDevalBSDF(&val, vout, vin, bsdf), stderr))
|
| 98 |
printf("%.3e\n", val.cieY);
|
| 99 |
continue;
|
| 100 |
case 's':
|
| 101 |
case 'S': /* sample BSDF */
|
| 102 |
if (bsdf == NULL)
|
| 103 |
goto noBSDFerr;
|
| 104 |
if (!*sskip2(cp,3))
|
| 105 |
break;
|
| 106 |
n = atoi(sskip2(cp,1));
|
| 107 |
vec_from_deg(vin, atof(sskip2(cp,2)), atof(sskip2(cp,3)));
|
| 108 |
while (n-- > 0) {
|
| 109 |
if (SDreportError(SDsampBSDF(&val, vin,
|
| 110 |
rand()*(1./(RAND_MAX+.5)),
|
| 111 |
sflags, bsdf), stderr))
|
| 112 |
break;
|
| 113 |
printf("%.8f %.8f %.8f\n", vin[0], vin[1], vin[2]);
|
| 114 |
}
|
| 115 |
continue;
|
| 116 |
case 'h':
|
| 117 |
case 'H': /* hemispherical totals */
|
| 118 |
case 'r':
|
| 119 |
case 'R':
|
| 120 |
case 't':
|
| 121 |
case 'T':
|
| 122 |
if (bsdf == NULL)
|
| 123 |
goto noBSDFerr;
|
| 124 |
if (!*sskip2(cp,2))
|
| 125 |
break;
|
| 126 |
if (tolower(*cp) == 'r')
|
| 127 |
sflags &= ~SDsampT;
|
| 128 |
else if (tolower(*cp) == 't')
|
| 129 |
sflags &= ~SDsampR;
|
| 130 |
vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
|
| 131 |
printf("%.4e\n", SDdirectHemi(vin, sflags, bsdf));
|
| 132 |
continue;
|
| 133 |
case 'a':
|
| 134 |
case 'A': /* resolution in proj. steradians */
|
| 135 |
if (bsdf == NULL)
|
| 136 |
goto noBSDFerr;
|
| 137 |
if (!*sskip2(cp,2))
|
| 138 |
break;
|
| 139 |
vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2)));
|
| 140 |
if (*sskip2(cp,4)) {
|
| 141 |
vec_from_deg(vout, atof(sskip2(cp,3)), atof(sskip2(cp,4)));
|
| 142 |
if (SDreportError(SDsizeBSDF(proja, vin, vout,
|
| 143 |
SDqueryMin+SDqueryMax, bsdf), stderr))
|
| 144 |
continue;
|
| 145 |
} else if (SDreportError(SDsizeBSDF(proja, vin, NULL,
|
| 146 |
SDqueryMin+SDqueryMax, bsdf), stderr))
|
| 147 |
continue;
|
| 148 |
printf("%.4e %.4e\n", proja[0], proja[1]);
|
| 149 |
continue;
|
| 150 |
}
|
| 151 |
Usage(argv[0]);
|
| 152 |
continue;
|
| 153 |
noBSDFerr:
|
| 154 |
fprintf(stderr, "%s: need to use 'l' command to load BSDF\n", argv[0]);
|
| 155 |
}
|
| 156 |
return 0;
|
| 157 |
}
|