| 1 | 
greg | 
2.1 | 
#ifndef lint | 
| 2 | 
  | 
  | 
static const char RCSid[] = "$Id: bsdf2rad.c,v 2.15 2016/03/06 01:13:17 schorsch Exp $"; | 
| 3 | 
  | 
  | 
#endif | 
| 4 | 
  | 
  | 
/* | 
| 5 | 
  | 
  | 
 *  Plot 3-D BSDF output based on scattering interpolant or XML representation | 
| 6 | 
  | 
  | 
 */ | 
| 7 | 
  | 
  | 
 | 
| 8 | 
  | 
  | 
#define _USE_MATH_DEFINES | 
| 9 | 
  | 
  | 
#include <stdio.h> | 
| 10 | 
  | 
  | 
#include <string.h> | 
| 11 | 
  | 
  | 
#include <stdlib.h> | 
| 12 | 
  | 
  | 
#include <math.h> | 
| 13 | 
  | 
  | 
#include "rtprocess.h" | 
| 14 | 
  | 
  | 
#include "bsdfrep.h" | 
| 15 | 
  | 
  | 
 | 
| 16 | 
  | 
  | 
const float     colarr[6][3] = { | 
| 17 | 
  | 
  | 
                .7, 1., .7, | 
| 18 | 
  | 
  | 
                1., .7, .7, | 
| 19 | 
  | 
  | 
                .7, .7, 1., | 
| 20 | 
  | 
  | 
                1., .5, 1., | 
| 21 | 
  | 
  | 
                1., 1., .5, | 
| 22 | 
  | 
  | 
                .5, 1., 1. | 
| 23 | 
  | 
  | 
        }; | 
| 24 | 
  | 
  | 
 | 
| 25 | 
  | 
  | 
#if defined(_WIN32) || defined(_WIN64) | 
| 26 | 
  | 
  | 
char    validf[] = "-e \"valid(s,t)=X`SYS(s,t)^2+Y`SYS(s,t)^2+Z`SYS(s,t)^2-1e-7\""; | 
| 27 | 
  | 
  | 
#else | 
| 28 | 
  | 
  | 
char    validf[] = "-e 'valid(s,t)=X`SYS(s,t)^2+Y`SYS(s,t)^2+Z`SYS(s,t)^2-1e-7'"; | 
| 29 | 
  | 
  | 
#endif | 
| 30 | 
  | 
  | 
 | 
| 31 | 
  | 
  | 
char    *progname; | 
| 32 | 
  | 
  | 
 | 
| 33 | 
  | 
  | 
/* Produce a Radiance model plotting the indicated incident direction(s) */ | 
| 34 | 
  | 
  | 
int | 
| 35 | 
  | 
  | 
main(int argc, char *argv[]) | 
| 36 | 
  | 
  | 
{ | 
| 37 | 
  | 
  | 
        int     showPeaks = 0; | 
| 38 | 
  | 
  | 
        int     doTrans = 0; | 
| 39 | 
  | 
  | 
        int     inpXML = -1; | 
| 40 | 
  | 
  | 
        RBFNODE *rbf = NULL; | 
| 41 | 
  | 
  | 
        FILE    *fp; | 
| 42 | 
  | 
  | 
        char    buf[128]; | 
| 43 | 
  | 
  | 
        SDData  myBSDF; | 
| 44 | 
  | 
  | 
        double  bsdf, min_log; | 
| 45 | 
  | 
  | 
        FVECT   idir, odir; | 
| 46 | 
  | 
  | 
        int     i, j, n; | 
| 47 | 
  | 
  | 
                                                /* check arguments */ | 
| 48 | 
  | 
  | 
        progname = argv[0]; | 
| 49 | 
  | 
  | 
        if (argc > 1 && !strcmp(argv[1], "-p")) { | 
| 50 | 
  | 
  | 
                ++showPeaks; | 
| 51 | 
  | 
  | 
                ++argv; --argc; | 
| 52 | 
  | 
  | 
        } | 
| 53 | 
  | 
  | 
        if (argc > 1 && !strcmp(argv[1], "-t")) { | 
| 54 | 
  | 
  | 
                ++doTrans; | 
| 55 | 
  | 
  | 
                ++argv; --argc; | 
| 56 | 
  | 
  | 
        } | 
| 57 | 
  | 
  | 
        if (argc >= 4 && (n = strlen(argv[1])-4) > 0) { | 
| 58 | 
  | 
  | 
                if (!strcasecmp(argv[1]+n, ".xml")) | 
| 59 | 
  | 
  | 
                        inpXML = 1; | 
| 60 | 
  | 
  | 
                else if (!strcasecmp(argv[1]+n, ".sir")) | 
| 61 | 
  | 
  | 
                        inpXML = 0; | 
| 62 | 
  | 
  | 
        } | 
| 63 | 
  | 
  | 
        if (inpXML < 0) { | 
| 64 | 
  | 
  | 
                fprintf(stderr, "Usage: %s [-p] bsdf.sir theta1 phi1 .. > output.rad\n", progname); | 
| 65 | 
  | 
  | 
                fprintf(stderr, "   Or: %s [-t] bsdf.xml theta1 phi1 .. > output.rad\n", progname); | 
| 66 | 
  | 
  | 
                return(1); | 
| 67 | 
  | 
  | 
        } | 
| 68 | 
  | 
  | 
                                                /* load input */ | 
| 69 | 
  | 
  | 
        if (inpXML) { | 
| 70 | 
  | 
  | 
                SDclearBSDF(&myBSDF, argv[1]); | 
| 71 | 
  | 
  | 
                if (SDreportError(SDloadFile(&myBSDF, argv[1]), stderr)) | 
| 72 | 
  | 
  | 
                        return(1); | 
| 73 | 
  | 
  | 
                bsdf_min = 1./M_PI; | 
| 74 | 
  | 
  | 
                if (myBSDF.rf != NULL && myBSDF.rLambFront.cieY < bsdf_min*M_PI) | 
| 75 | 
  | 
  | 
                        bsdf_min = myBSDF.rLambFront.cieY/M_PI; | 
| 76 | 
  | 
  | 
                if (myBSDF.rb != NULL && myBSDF.rLambBack.cieY < bsdf_min*M_PI) | 
| 77 | 
  | 
  | 
                        bsdf_min = myBSDF.rLambBack.cieY/M_PI; | 
| 78 | 
  | 
  | 
                if ((myBSDF.tf != NULL) | (myBSDF.tb != NULL) && | 
| 79 | 
  | 
  | 
                                myBSDF.tLamb.cieY < bsdf_min*M_PI) | 
| 80 | 
  | 
  | 
                        bsdf_min = myBSDF.tLamb.cieY/M_PI; | 
| 81 | 
  | 
  | 
                if (doTrans && (myBSDF.tf == NULL) & (myBSDF.tb == NULL)) { | 
| 82 | 
  | 
  | 
                        fprintf(stderr, "%s: no transmitted component in '%s'\n", | 
| 83 | 
  | 
  | 
                                        progname, argv[1]); | 
| 84 | 
  | 
  | 
                        return(1); | 
| 85 | 
  | 
  | 
                } | 
| 86 | 
  | 
  | 
        } else { | 
| 87 | 
  | 
  | 
                fp = fopen(argv[1], "rb"); | 
| 88 | 
  | 
  | 
                if (fp == NULL) { | 
| 89 | 
  | 
  | 
                        fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", | 
| 90 | 
  | 
  | 
                                        progname, argv[1]); | 
| 91 | 
  | 
  | 
                        return(1); | 
| 92 | 
  | 
  | 
                } | 
| 93 | 
  | 
  | 
                if (!load_bsdf_rep(fp)) | 
| 94 | 
  | 
  | 
                        return(1); | 
| 95 | 
  | 
  | 
                fclose(fp); | 
| 96 | 
  | 
  | 
        } | 
| 97 | 
  | 
  | 
#ifdef DEBUG | 
| 98 | 
  | 
  | 
        fprintf(stderr, "Minimum BSDF set to %.4f\n", bsdf_min); | 
| 99 | 
  | 
  | 
#endif | 
| 100 | 
  | 
  | 
        min_log = log(bsdf_min*.5 + 1e-5); | 
| 101 | 
  | 
  | 
                                                /* output BSDF rep. */ | 
| 102 | 
  | 
  | 
        for (n = 0; (n < 6) & (2*n+3 < argc); n++) { | 
| 103 | 
  | 
  | 
                double  theta = (M_PI/180.)*atof(argv[2*n+2]); | 
| 104 | 
  | 
  | 
                double  phi = (M_PI/180.)*atof(argv[2*n+3]); | 
| 105 | 
  | 
  | 
                if (theta < -FTINY) { | 
| 106 | 
  | 
  | 
                        fprintf(stderr, "%s: theta values must be positive\n", | 
| 107 | 
  | 
  | 
                                        progname); | 
| 108 | 
  | 
  | 
                        return(1); | 
| 109 | 
  | 
  | 
                } | 
| 110 | 
  | 
  | 
                if (inpXML) { | 
| 111 | 
  | 
  | 
                        input_orient = (theta <= M_PI/2.) ? 1 : -1; | 
| 112 | 
  | 
  | 
                        output_orient = doTrans ? -input_orient : input_orient; | 
| 113 | 
  | 
  | 
                } | 
| 114 | 
  | 
  | 
                idir[2] = sin(theta); | 
| 115 | 
  | 
  | 
                idir[0] = idir[2] * cos(phi); | 
| 116 | 
  | 
  | 
                idir[1] = idir[2] * sin(phi); | 
| 117 | 
  | 
  | 
                idir[2] = input_orient * sqrt(1. - idir[2]*idir[2]); | 
| 118 | 
  | 
  | 
#ifdef DEBUG | 
| 119 | 
  | 
  | 
                fprintf(stderr, "Computing BSDF for incident direction (%.1f,%.1f)\n", | 
| 120 | 
  | 
  | 
                                get_theta180(idir), get_phi360(idir)); | 
| 121 | 
  | 
  | 
#endif | 
| 122 | 
  | 
  | 
                if (!inpXML) | 
| 123 | 
  | 
  | 
                        rbf = advect_rbf(idir, 15000); | 
| 124 | 
  | 
  | 
#ifdef DEBUG | 
| 125 | 
  | 
  | 
                if (inpXML) | 
| 126 | 
  | 
  | 
                        fprintf(stderr, "Hemispherical %s: %.3f\n", | 
| 127 | 
  | 
  | 
                                (output_orient > 0 ^ input_orient > 0 ? | 
| 128 | 
  | 
  | 
                                        "transmission" : "reflection"), | 
| 129 | 
  | 
  | 
                                SDdirectHemi(idir, SDsampSp|SDsampDf | | 
| 130 | 
  | 
  | 
                                        (output_orient > 0 ^ input_orient > 0 ? | 
| 131 | 
  | 
  | 
                                                 SDsampT : SDsampR), &myBSDF)); | 
| 132 | 
  | 
  | 
                else if (rbf == NULL) | 
| 133 | 
  | 
  | 
                        fputs("Empty RBF\n", stderr); | 
| 134 | 
  | 
  | 
                else | 
| 135 | 
  | 
  | 
                        fprintf(stderr, "Hemispherical %s: %.3f\n", | 
| 136 | 
  | 
  | 
                                (output_orient > 0 ^ input_orient > 0 ? | 
| 137 | 
  | 
  | 
                                        "transmission" : "reflection"), | 
| 138 | 
  | 
  | 
                                rbf->vtotal); | 
| 139 | 
  | 
  | 
#endif | 
| 140 | 
  | 
  | 
                printf("# Incident direction (theta,phi) = (%.2f,%.2f) deg.\n\n", | 
| 141 | 
  | 
  | 
                                (180./M_PI)*theta, (180./M_PI)*phi); | 
| 142 | 
  | 
  | 
                printf("void trans tmat\n0\n0\n7 %f %f %f .04 .04 .9 1\n", | 
| 143 | 
  | 
  | 
                                colarr[n][0], colarr[n][1], colarr[n][2]); | 
| 144 | 
  | 
  | 
                if (showPeaks && rbf != NULL) { | 
| 145 | 
  | 
  | 
                        printf("void plastic pmat\n0\n0\n5 %f %f %f .04 .08\n", | 
| 146 | 
  | 
  | 
                                1.-colarr[n][0], 1.-colarr[n][1], 1.-colarr[n][2]); | 
| 147 | 
  | 
  | 
                        for (i = 0; i < rbf->nrbf; i++) { | 
| 148 | 
  | 
  | 
                                ovec_from_pos(odir, rbf->rbfa[i].gx, rbf->rbfa[i].gy); | 
| 149 | 
  | 
  | 
                                bsdf = eval_rbfrep(rbf, odir); | 
| 150 | 
  | 
  | 
                                bsdf = log(bsdf + 1e-5) - min_log; | 
| 151 | 
  | 
  | 
                                printf("pmat sphere p%d\n0\n0\n4 %f %f %f %f\n", | 
| 152 | 
  | 
  | 
                                        i+1, odir[0]*bsdf, odir[1]*bsdf, odir[2]*bsdf, | 
| 153 | 
  | 
  | 
                                                .007*bsdf); | 
| 154 | 
  | 
  | 
                        } | 
| 155 | 
  | 
  | 
                } | 
| 156 | 
  | 
  | 
                fflush(stdout); | 
| 157 | 
  | 
  | 
                sprintf(buf, "gensurf tmat bsdf%d - - - %d %d %s", n+1, | 
| 158 | 
  | 
  | 
                                                GRIDRES-1, GRIDRES-1, validf); | 
| 159 | 
  | 
  | 
                fp = popen(buf, "w"); | 
| 160 | 
  | 
  | 
                if (fp == NULL) { | 
| 161 | 
  | 
  | 
                        fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf); | 
| 162 | 
  | 
  | 
                        return(1); | 
| 163 | 
  | 
  | 
                } | 
| 164 | 
  | 
  | 
                for (i = 0; i < GRIDRES; i++) | 
| 165 | 
  | 
  | 
                    for (j = 0; j < GRIDRES; j++) { | 
| 166 | 
  | 
  | 
                        ovec_from_pos(odir, i, j); | 
| 167 | 
  | 
  | 
                        if (inpXML) { | 
| 168 | 
  | 
  | 
                                SDValue sval; | 
| 169 | 
  | 
  | 
                                if (SDreportError(SDevalBSDF(&sval, odir, | 
| 170 | 
  | 
  | 
                                                        idir, &myBSDF), stderr)) | 
| 171 | 
  | 
  | 
                                        return(1); | 
| 172 | 
  | 
  | 
                                bsdf = sval.cieY; | 
| 173 | 
  | 
  | 
                        } else | 
| 174 | 
  | 
  | 
                                bsdf = eval_rbfrep(rbf, odir); | 
| 175 | 
  | 
  | 
                        bsdf = log(bsdf + 1e-5) - min_log; | 
| 176 | 
  | 
  | 
                        fprintf(fp, "%.8e %.8e %.8e\n", | 
| 177 | 
  | 
  | 
                                        odir[0]*bsdf, odir[1]*bsdf, odir[2]*bsdf); | 
| 178 | 
  | 
  | 
                    } | 
| 179 | 
  | 
  | 
                if (rbf != NULL) | 
| 180 | 
  | 
  | 
                        free(rbf); | 
| 181 | 
  | 
  | 
                if (pclose(fp)) | 
| 182 | 
  | 
  | 
                        return(1); | 
| 183 | 
  | 
  | 
        } | 
| 184 | 
  | 
  | 
        return(0); | 
| 185 | 
  | 
  | 
} |