ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/bsdf.c
(Generate patch)

Comparing ray/src/common/bsdf.c (file contents):
Revision 2.42 by greg, Sun Sep 2 15:33:15 2012 UTC vs.
Revision 2.46 by greg, Thu Jul 4 15:14:45 2013 UTC

# Line 35 | Line 35 | const char             *SDerrorEnglish[] = {
35                                  "Unknown error"
36                          };
37  
38 + /* Pointer to error list in preferred language */
39 + const char              **SDerrorList = SDerrorEnglish;
40 +
41   /* Additional information on last error (ASCII English) */
42   char                    SDerrorDetail[256];
43  
# Line 47 | Line 50 | struct SDCache_s       *SDcacheList = NULL;
50   /* Retain BSDFs in cache list */
51   int                     SDretainSet = SDretainNone;
52  
53 < /* Report any error to the indicated stream (in English) */
53 > /* Report any error to the indicated stream */
54   SDError
55 < SDreportEnglish(SDError ec, FILE *fp)
55 > SDreportError(SDError ec, FILE *fp)
56   {
57          if (!ec)
58                  return SDEnone;
# Line 59 | Line 62 | SDreportEnglish(SDError ec, FILE *fp)
62          }
63          if (fp == NULL)
64                  return ec;
65 <        fputs(SDerrorEnglish[ec], fp);
65 >        fputs(SDerrorList[ec], fp);
66          if (SDerrorDetail[0]) {
67                  fputs(": ", fp);
68                  fputs(SDerrorDetail, fp);
# Line 87 | Line 90 | to_meters(             /* return factor to convert given unit to
90  
91   /* Load geometric dimensions and description (if any) */
92   static SDError
93 < SDloadGeometry(SDData *sd, ezxml_t wdb)
93 > SDloadGeometry(SDData *sd, ezxml_t wtl)
94   {
95 <        ezxml_t         geom;
95 >        ezxml_t         node, matl, geom;
96          double          cfact;
97 <        const char      *fmt, *mgfstr;
97 >        const char      *fmt = NULL, *mgfstr;
98  
96        if (wdb == NULL)                /* no geometry section? */
97                return SDEnone;
98        if ((geom = ezxml_child(wdb, "Name")) != NULL) {
99                strncpy(sd->matn, ezxml_txt(geom), SDnameLn);
100                if (sd->matn[SDnameLn-1])
101                        strcpy(sd->matn+(SDnameLn-4), "...");
102        }
103        if ((geom = ezxml_child(wdb, "Manufacturer")) != NULL) {
104                strncpy(sd->makr, ezxml_txt(geom), SDnameLn);
105                if (sd->makr[SDnameLn-1])
106                        strcpy(sd->makr+(SDnameLn-4), "...");
107        }
108        sd->dim[0] = sd->dim[1] = sd->dim[2] = .0;
99          SDerrorDetail[0] = '\0';
100 <        if ((geom = ezxml_child(wdb, "Width")) != NULL)
101 <                sd->dim[0] = atof(ezxml_txt(geom)) *
102 <                                to_meters(ezxml_attr(geom, "unit"));
103 <        if ((geom = ezxml_child(wdb, "Height")) != NULL)
104 <                sd->dim[1] = atof(ezxml_txt(geom)) *
105 <                                to_meters(ezxml_attr(geom, "unit"));
106 <        if ((geom = ezxml_child(wdb, "Thickness")) != NULL)
107 <                sd->dim[2] = atof(ezxml_txt(geom)) *
108 <                                to_meters(ezxml_attr(geom, "unit"));
109 <        if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) {
110 <                if (!SDerrorDetail[0])
111 <                        sprintf(SDerrorDetail, "Negative dimension in \"%s\"",
112 <                                                sd->name);
113 <                return SDEdata;
100 >        sd->matn[0] = '\0'; sd->makr[0] = '\0';
101 >        sd->dim[0] = sd->dim[1] = sd->dim[2] = 0;
102 >        matl = ezxml_child(wtl, "Material");
103 >        if (matl != NULL) {                     /* get material info. */
104 >                if ((node = ezxml_child(matl, "Name")) != NULL) {
105 >                        strncpy(sd->matn, ezxml_txt(node), SDnameLn);
106 >                        if (sd->matn[SDnameLn-1])
107 >                                strcpy(sd->matn+(SDnameLn-4), "...");
108 >                }
109 >                if ((node = ezxml_child(matl, "Manufacturer")) != NULL) {
110 >                        strncpy(sd->makr, ezxml_txt(node), SDnameLn);
111 >                        if (sd->makr[SDnameLn-1])
112 >                                strcpy(sd->makr+(SDnameLn-4), "...");
113 >                }
114 >                if ((node = ezxml_child(matl, "Width")) != NULL)
115 >                        sd->dim[0] = atof(ezxml_txt(node)) *
116 >                                        to_meters(ezxml_attr(node, "unit"));
117 >                if ((node = ezxml_child(matl, "Height")) != NULL)
118 >                        sd->dim[1] = atof(ezxml_txt(node)) *
119 >                                        to_meters(ezxml_attr(node, "unit"));
120 >                if ((node = ezxml_child(matl, "Thickness")) != NULL)
121 >                        sd->dim[2] = atof(ezxml_txt(node)) *
122 >                                        to_meters(ezxml_attr(node, "unit"));
123 >                if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) {
124 >                        if (!SDerrorDetail[0])
125 >                                sprintf(SDerrorDetail, "Negative dimension in \"%s\"",
126 >                                                        sd->name);
127 >                        return SDEdata;
128 >                }
129          }
130 <        if ((geom = ezxml_child(wdb, "Geometry")) == NULL ||
131 <                        (mgfstr = ezxml_txt(geom)) == NULL)
130 >        sd->mgf = NULL;
131 >        geom = ezxml_child(wtl, "Geometry");
132 >        if (geom == NULL)                       /* no actual geometry? */
133                  return SDEnone;
134 <        while (isspace(*mgfstr))
135 <                ++mgfstr;
130 <        if (!*mgfstr)
131 <                return SDEnone;
132 <        if ((fmt = ezxml_attr(geom, "format")) != NULL &&
133 <                        strcasecmp(fmt, "MGF")) {
134 >        fmt = ezxml_attr(geom, "format");
135 >        if (fmt != NULL && strcasecmp(fmt, "MGF")) {
136                  sprintf(SDerrorDetail,
137                          "Unrecognized geometry format '%s' in \"%s\"",
138                                          fmt, sd->name);
139                  return SDEsupport;
140          }
141 <        cfact = to_meters(ezxml_attr(geom, "unit"));
141 >        if ((node = ezxml_child(geom, "MGFblock")) == NULL ||
142 >                        (mgfstr = ezxml_txt(node)) == NULL)
143 >                return SDEnone;
144 >        while (isspace(*mgfstr))
145 >                ++mgfstr;
146 >        if (!*mgfstr)
147 >                return SDEnone;
148 >        cfact = to_meters(ezxml_attr(node, "unit"));
149          if (cfact <= 0)
150                  return SDEformat;
151          sd->mgf = (char *)malloc(strlen(mgfstr)+32);
# Line 180 | Line 189 | SDloadFile(SDData *sd, const char *fname)
189                  ezxml_free(fl);
190                  return SDEformat;
191          }
192 +        wtl = ezxml_child(fl, "FileType");
193 +        if (wtl != NULL && strcmp(ezxml_txt(wtl), "BSDF")) {
194 +                sprintf(SDerrorDetail,
195 +                        "XML \"%s\": wrong FileType (must be 'BSDF')",
196 +                                sd->name);
197 +                ezxml_free(fl);
198 +                return SDEformat;
199 +        }
200          wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
201          if (wtl == NULL) {
202                  sprintf(SDerrorDetail, "BSDF \"%s\": no optical layers'",
# Line 188 | Line 205 | SDloadFile(SDData *sd, const char *fname)
205                  return SDEformat;
206          }
207                                  /* load geometry if present */
208 <        lastErr = SDloadGeometry(sd, ezxml_child(wtl, "Material"));
208 >        lastErr = SDloadGeometry(sd, wtl);
209          if (lastErr) {
210                  ezxml_free(fl);
211                  return lastErr;
# Line 406 | Line 423 | SDcacheFile(const char *fname)
423                  return NULL;
424          SDerrorDetail[0] = '\0';
425          if ((sd = SDgetCache(fname)) == NULL) {
426 <                SDreportEnglish(SDEmemory, stderr);
426 >                SDreportError(SDEmemory, stderr);
427                  return NULL;
428          }
429          if (!SDisLoaded(sd) && (ec = SDloadFile(sd, fname))) {
430 <                SDreportEnglish(ec, stderr);
430 >                SDreportError(ec, stderr);
431                  SDfreeCache(sd);
432                  return NULL;
433          }
# Line 471 | Line 488 | SDsampComponent(SDValue *sv, FVECT ioVec, double randX
488                  return SDEargument;
489                                          /* get cumulative distribution */
490          VCOPY(inVec, ioVec);
491 +        sv->cieY = 0;
492          cd = (*sdc->func->getCDist)(inVec, sdc);
493 <        if (cd == NULL)
494 <                return SDEmemory;
495 <        if (cd->cTotal <= 1e-6) {       /* anything to sample? */
493 >        if (cd != NULL)
494 >                sv->cieY = cd->cTotal;
495 >        if (sv->cieY <= 1e-6) {         /* nothing to sample? */
496                  sv->spec = c_dfcolor;
479                sv->cieY = .0;
497                  memset(ioVec, 0, 3*sizeof(double));
498                  return SDEnone;
499          }
483        sv->cieY = cd->cTotal;
500                                          /* compute sample direction */
501          ec = (*sdc->func->sampCDist)(ioVec, randX, cd);
502          if (ec)
# Line 602 | Line 618 | SDsizeBSDF(double *projSA, const FVECT v1, const RREAL
618                  projSA[0] = M_PI;
619                  if (qflags == SDqueryMin+SDqueryMax)
620                          projSA[1] = M_PI;
621 <        }
621 >        } else if (qflags == SDqueryMin+SDqueryMax && projSA[0] > projSA[1])
622 >                projSA[0] = projSA[1];
623          return SDEnone;
624   }
625  
# Line 739 | Line 756 | SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int
756                  return SDEmemory;
757          while (j-- > 0) {               /* non-diffuse transmission */
758                  cdarr[i+j] = (*tdf->comp[j].func->getCDist)(inVec, &tdf->comp[j]);
759 <                if (cdarr[i+j] == NULL) {
760 <                        free(cdarr);
744 <                        return SDEmemory;
745 <                }
759 >                if (cdarr[i+j] == NULL)
760 >                        cdarr[i+j] = &SDemptyCD;
761                  sv->cieY += cdarr[i+j]->cTotal;
762          }
763          while (i-- > 0) {               /* non-diffuse reflection */
764                  cdarr[i] = (*rdf->comp[i].func->getCDist)(inVec, &rdf->comp[i]);
765 <                if (cdarr[i] == NULL) {
766 <                        free(cdarr);
767 <                        return SDEmemory;
768 <                }
754 <                sv->cieY += cdarr[i]->cTotal;
765 >                if (cdarr[i] == NULL)
766 >                        cdarr[i] = &SDemptyCD;
767 >                else
768 >                        sv->cieY += cdarr[i]->cTotal;
769          }
770          if (sv->cieY <= 1e-6) {         /* anything to sample? */
771                  sv->cieY = .0;
# Line 873 | Line 887 | SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT i
887          VCOPY(resVec, vTmp);
888          return SDEnone;
889   }
876
877 /*################################################################*/
878 /*######### DEPRECATED ROUTINES AWAITING PERMANENT REMOVAL #######*/
879
880 /*
881 * Routines for handling BSDF data
882 */
883
884 #include "standard.h"
885 #include "paths.h"
886
887 #define MAXLATS         46              /* maximum number of latitudes */
888
889 /* BSDF angle specification */
890 typedef struct {
891        char    name[64];               /* basis name */
892        int     nangles;                /* total number of directions */
893        struct {
894                float   tmin;                   /* starting theta */
895                short   nphis;                  /* number of phis (0 term) */
896        }       lat[MAXLATS+1];         /* latitudes */
897 } ANGLE_BASIS;
898
899 #define MAXABASES       7               /* limit on defined bases */
900
901 static ANGLE_BASIS      abase_list[MAXABASES] = {
902        {
903                "LBNL/Klems Full", 145,
904                { {-5., 1},
905                {5., 8},
906                {15., 16},
907                {25., 20},
908                {35., 24},
909                {45., 24},
910                {55., 24},
911                {65., 16},
912                {75., 12},
913                {90., 0} }
914        }, {
915                "LBNL/Klems Half", 73,
916                { {-6.5, 1},
917                {6.5, 8},
918                {19.5, 12},
919                {32.5, 16},
920                {46.5, 20},
921                {61.5, 12},
922                {76.5, 4},
923                {90., 0} }
924        }, {
925                "LBNL/Klems Quarter", 41,
926                { {-9., 1},
927                {9., 8},
928                {27., 12},
929                {46., 12},
930                {66., 8},
931                {90., 0} }
932        }
933 };
934
935 static int      nabases = 3;    /* current number of defined bases */
936
937 #define  FEQ(a,b)       ((a)-(b) <= 1e-6 && (b)-(a) <= 1e-6)
938
939 static int
940 fequal(double a, double b)
941 {
942        if (b != 0)
943                a = a/b - 1.;
944        return((a <= 1e-6) & (a >= -1e-6));
945 }
946
947 /* Returns the name of the given tag */
948 #ifdef ezxml_name
949 #undef ezxml_name
950 static char *
951 ezxml_name(ezxml_t xml)
952 {
953        if (xml == NULL)
954                return(NULL);
955        return(xml->name);
956 }
957 #endif
958
959 /* Returns the given tag's character content or empty string if none */
960 #ifdef ezxml_txt
961 #undef ezxml_txt
962 static char *
963 ezxml_txt(ezxml_t xml)
964 {
965        if (xml == NULL)
966                return("");
967        return(xml->txt);
968 }
969 #endif
970
971
972 static int
973 ab_getvec(              /* get vector for this angle basis index */
974        FVECT v,
975        int ndx,
976        void *p
977 )
978 {
979        ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
980        int     li;
981        double  pol, azi, d;
982        
983        if ((ndx < 0) | (ndx >= ab->nangles))
984                return(0);
985        for (li = 0; ndx >= ab->lat[li].nphis; li++)
986                ndx -= ab->lat[li].nphis;
987        pol = PI/180.*0.5*(ab->lat[li].tmin + ab->lat[li+1].tmin);
988        azi = 2.*PI*ndx/ab->lat[li].nphis;
989        v[2] = d = cos(pol);
990        d = sqrt(1. - d*d);     /* sin(pol) */
991        v[0] = cos(azi)*d;
992        v[1] = sin(azi)*d;
993        return(1);
994 }
995
996
997 static int
998 ab_getndx(              /* get index corresponding to the given vector */
999        FVECT v,
1000        void *p
1001 )
1002 {
1003        ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
1004        int     li, ndx;
1005        double  pol, azi;
1006
1007        if ((v[2] < -1.0) | (v[2] > 1.0))
1008                return(-1);
1009        pol = 180.0/PI*acos(v[2]);
1010        azi = 180.0/PI*atan2(v[1], v[0]);
1011        if (azi < 0.0) azi += 360.0;
1012        for (li = 1; ab->lat[li].tmin <= pol; li++)
1013                if (!ab->lat[li].nphis)
1014                        return(-1);
1015        --li;
1016        ndx = (int)((1./360.)*azi*ab->lat[li].nphis + 0.5);
1017        if (ndx >= ab->lat[li].nphis) ndx = 0;
1018        while (li--)
1019                ndx += ab->lat[li].nphis;
1020        return(ndx);
1021 }
1022
1023
1024 static double
1025 ab_getohm(              /* get solid angle for this angle basis index */
1026        int ndx,
1027        void *p
1028 )
1029 {
1030        ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
1031        int     li;
1032        double  theta, theta1;
1033        
1034        if ((ndx < 0) | (ndx >= ab->nangles))
1035                return(0);
1036        for (li = 0; ndx >= ab->lat[li].nphis; li++)
1037                ndx -= ab->lat[li].nphis;
1038        theta1 = PI/180. * ab->lat[li+1].tmin;
1039        if (ab->lat[li].nphis == 1) {           /* special case */
1040                if (ab->lat[li].tmin > FTINY)
1041                        error(USER, "unsupported BSDF coordinate system");
1042                return(2.*PI*(1. - cos(theta1)));
1043        }
1044        theta = PI/180. * ab->lat[li].tmin;
1045        return(2.*PI*(cos(theta) - cos(theta1))/(double)ab->lat[li].nphis);
1046 }
1047
1048
1049 static int
1050 ab_getvecR(             /* get reverse vector for this angle basis index */
1051        FVECT v,
1052        int ndx,
1053        void *p
1054 )
1055 {
1056        if (!ab_getvec(v, ndx, p))
1057                return(0);
1058
1059        v[0] = -v[0];
1060        v[1] = -v[1];
1061        v[2] = -v[2];
1062
1063        return(1);
1064 }
1065
1066
1067 static int
1068 ab_getndxR(             /* get index corresponding to the reverse vector */
1069        FVECT v,
1070        void *p
1071 )
1072 {
1073        FVECT  v2;
1074        
1075        v2[0] = -v[0];
1076        v2[1] = -v[1];
1077        v2[2] = -v[2];
1078
1079        return ab_getndx(v2, p);
1080 }
1081
1082
1083 static void
1084 load_angle_basis(       /* load custom BSDF angle basis */
1085        ezxml_t wab
1086 )
1087 {
1088        char    *abname = ezxml_txt(ezxml_child(wab, "AngleBasisName"));
1089        ezxml_t wbb;
1090        int     i;
1091        
1092        if (!abname || !*abname)
1093                return;
1094        for (i = nabases; i--; )
1095                if (!strcasecmp(abname, abase_list[i].name))
1096                        return;         /* assume it's the same */
1097        if (nabases >= MAXABASES)
1098                error(INTERNAL, "too many angle bases");
1099        strcpy(abase_list[nabases].name, abname);
1100        abase_list[nabases].nangles = 0;
1101        for (i = 0, wbb = ezxml_child(wab, "AngleBasisBlock");
1102                        wbb != NULL; i++, wbb = wbb->next) {
1103                if (i >= MAXLATS)
1104                        error(INTERNAL, "too many latitudes in custom basis");
1105                abase_list[nabases].lat[i+1].tmin = atof(ezxml_txt(
1106                                ezxml_child(ezxml_child(wbb,
1107                                        "ThetaBounds"), "UpperTheta")));
1108                if (!i)
1109                        abase_list[nabases].lat[i].tmin =
1110                                        -abase_list[nabases].lat[i+1].tmin;
1111                else if (!fequal(atof(ezxml_txt(ezxml_child(ezxml_child(wbb,
1112                                        "ThetaBounds"), "LowerTheta"))),
1113                                abase_list[nabases].lat[i].tmin))
1114                        error(WARNING, "theta values disagree in custom basis");
1115                abase_list[nabases].nangles +=
1116                        abase_list[nabases].lat[i].nphis =
1117                                atoi(ezxml_txt(ezxml_child(wbb, "nPhis")));
1118        }
1119        abase_list[nabases++].lat[i].nphis = 0;
1120 }
1121
1122
1123 static void
1124 load_geometry(          /* load geometric dimensions and description (if any) */
1125        struct BSDF_data *dp,
1126        ezxml_t wdb
1127 )
1128 {
1129        ezxml_t         geom;
1130        double          cfact;
1131        const char      *fmt, *mgfstr;
1132
1133        dp->dim[0] = dp->dim[1] = dp->dim[2] = 0;
1134        dp->mgf = NULL;
1135        if ((geom = ezxml_child(wdb, "Width")) != NULL)
1136                dp->dim[0] = atof(ezxml_txt(geom)) *
1137                                to_meters(ezxml_attr(geom, "unit"));
1138        if ((geom = ezxml_child(wdb, "Height")) != NULL)
1139                dp->dim[1] = atof(ezxml_txt(geom)) *
1140                                to_meters(ezxml_attr(geom, "unit"));
1141        if ((geom = ezxml_child(wdb, "Thickness")) != NULL)
1142                dp->dim[2] = atof(ezxml_txt(geom)) *
1143                                to_meters(ezxml_attr(geom, "unit"));
1144        if ((geom = ezxml_child(wdb, "Geometry")) == NULL ||
1145                        (mgfstr = ezxml_txt(geom)) == NULL)
1146                return;
1147        if ((fmt = ezxml_attr(geom, "format")) != NULL &&
1148                        strcasecmp(fmt, "MGF")) {
1149                sprintf(errmsg, "unrecognized geometry format '%s'", fmt);
1150                error(WARNING, errmsg);
1151                return;
1152        }
1153        cfact = to_meters(ezxml_attr(geom, "unit"));
1154        dp->mgf = (char *)malloc(strlen(mgfstr)+32);
1155        if (dp->mgf == NULL)
1156                error(SYSTEM, "out of memory in load_geometry");
1157        if (cfact < 0.99 || cfact > 1.01)
1158                sprintf(dp->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr);
1159        else
1160                strcpy(dp->mgf, mgfstr);
1161 }
1162
1163
1164 static void
1165 load_bsdf_data(         /* load BSDF distribution for this wavelength */
1166        struct BSDF_data *dp,
1167        ezxml_t wdb
1168 )
1169 {
1170        char  *cbasis = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis"));
1171        char  *rbasis = ezxml_txt(ezxml_child(wdb,"RowAngleBasis"));
1172        char  *sdata;
1173        int  i;
1174        
1175        if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) {
1176                error(WARNING, "missing column/row basis for BSDF");
1177                return;
1178        }
1179        for (i = nabases; i--; )
1180                if (!strcasecmp(cbasis, abase_list[i].name)) {
1181                        dp->ninc = abase_list[i].nangles;
1182                        dp->ib_priv = (void *)&abase_list[i];
1183                        dp->ib_vec = ab_getvecR;
1184                        dp->ib_ndx = ab_getndxR;
1185                        dp->ib_ohm = ab_getohm;
1186                        break;
1187                }
1188        if (i < 0) {
1189                sprintf(errmsg, "undefined ColumnAngleBasis '%s'", cbasis);
1190                error(WARNING, errmsg);
1191                return;
1192        }
1193        for (i = nabases; i--; )
1194                if (!strcasecmp(rbasis, abase_list[i].name)) {
1195                        dp->nout = abase_list[i].nangles;
1196                        dp->ob_priv = (void *)&abase_list[i];
1197                        dp->ob_vec = ab_getvec;
1198                        dp->ob_ndx = ab_getndx;
1199                        dp->ob_ohm = ab_getohm;
1200                        break;
1201                }
1202        if (i < 0) {
1203                sprintf(errmsg, "undefined RowAngleBasis '%s'", rbasis);
1204                error(WARNING, errmsg);
1205                return;
1206        }
1207                                /* read BSDF data */
1208        sdata  = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
1209        if (!sdata || !*sdata) {
1210                error(WARNING, "missing BSDF ScatteringData");
1211                return;
1212        }
1213        dp->bsdf = (float *)malloc(sizeof(float)*dp->ninc*dp->nout);
1214        if (dp->bsdf == NULL)
1215                error(SYSTEM, "out of memory in load_bsdf_data");
1216        for (i = 0; i < dp->ninc*dp->nout; i++) {
1217                char  *sdnext = fskip(sdata);
1218                if (sdnext == NULL) {
1219                        error(WARNING, "bad/missing BSDF ScatteringData");
1220                        free(dp->bsdf); dp->bsdf = NULL;
1221                        return;
1222                }
1223                while (*sdnext && isspace(*sdnext))
1224                        sdnext++;
1225                if (*sdnext == ',') sdnext++;
1226                dp->bsdf[i] = atof(sdata);
1227                sdata = sdnext;
1228        }
1229        while (isspace(*sdata))
1230                sdata++;
1231        if (*sdata) {
1232                sprintf(errmsg, "%d extra characters after BSDF ScatteringData",
1233                                (int)strlen(sdata));
1234                error(WARNING, errmsg);
1235        }
1236 }
1237
1238
1239 static int
1240 check_bsdf_data(        /* check that BSDF data is sane */
1241        struct BSDF_data *dp
1242 )
1243 {
1244        double          *omega_iarr, *omega_oarr;
1245        double          dom, hemi_total, full_total;
1246        int             nneg;
1247        FVECT           v;
1248        int             i, o;
1249
1250        if (dp == NULL || dp->bsdf == NULL)
1251                return(0);
1252        omega_iarr = (double *)calloc(dp->ninc, sizeof(double));
1253        omega_oarr = (double *)calloc(dp->nout, sizeof(double));
1254        if ((omega_iarr == NULL) | (omega_oarr == NULL))
1255                error(SYSTEM, "out of memory in check_bsdf_data");
1256                                        /* incoming projected solid angles */
1257        hemi_total = .0;
1258        for (i = dp->ninc; i--; ) {
1259                dom = getBSDF_incohm(dp,i);
1260                if (dom <= 0) {
1261                        error(WARNING, "zero/negative incoming solid angle");
1262                        continue;
1263                }
1264                if (!getBSDF_incvec(v,dp,i) || v[2] > FTINY) {
1265                        error(WARNING, "illegal incoming BSDF direction");
1266                        free(omega_iarr); free(omega_oarr);
1267                        return(0);
1268                }
1269                hemi_total += omega_iarr[i] = dom * -v[2];
1270        }
1271        if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) {
1272                sprintf(errmsg, "incoming BSDF hemisphere off by %.1f%%",
1273                                100.*(hemi_total/PI - 1.));
1274                error(WARNING, errmsg);
1275        }
1276        dom = PI / hemi_total;          /* fix normalization */
1277        for (i = dp->ninc; i--; )
1278                omega_iarr[i] *= dom;
1279                                        /* outgoing projected solid angles */
1280        hemi_total = .0;
1281        for (o = dp->nout; o--; ) {
1282                dom = getBSDF_outohm(dp,o);
1283                if (dom <= 0) {
1284                        error(WARNING, "zero/negative outgoing solid angle");
1285                        continue;
1286                }
1287                if (!getBSDF_outvec(v,dp,o) || v[2] < -FTINY) {
1288                        error(WARNING, "illegal outgoing BSDF direction");
1289                        free(omega_iarr); free(omega_oarr);
1290                        return(0);
1291                }
1292                hemi_total += omega_oarr[o] = dom * v[2];
1293        }
1294        if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) {
1295                sprintf(errmsg, "outgoing BSDF hemisphere off by %.1f%%",
1296                                100.*(hemi_total/PI - 1.));
1297                error(WARNING, errmsg);
1298        }
1299        dom = PI / hemi_total;          /* fix normalization */
1300        for (o = dp->nout; o--; )
1301                omega_oarr[o] *= dom;
1302        nneg = 0;                       /* check outgoing totals */
1303        for (i = 0; i < dp->ninc; i++) {
1304                hemi_total = .0;
1305                for (o = dp->nout; o--; ) {
1306                        double  f = BSDF_value(dp,i,o);
1307                        if (f >= 0)
1308                                hemi_total += f*omega_oarr[o];
1309                        else {
1310                                nneg += (f < -FTINY);
1311                                BSDF_value(dp,i,o) = .0f;
1312                        }
1313                }
1314                if (hemi_total > 1.01) {
1315                        sprintf(errmsg,
1316                        "incoming BSDF direction %d passes %.1f%% of light",
1317                                        i, 100.*hemi_total);
1318                        error(WARNING, errmsg);
1319                }
1320        }
1321        if (nneg) {
1322                sprintf(errmsg, "%d negative BSDF values (ignored)", nneg);
1323                error(WARNING, errmsg);
1324        }
1325        full_total = .0;                /* reverse roles and check again */
1326        for (o = 0; o < dp->nout; o++) {
1327                hemi_total = .0;
1328                for (i = dp->ninc; i--; )
1329                        hemi_total += BSDF_value(dp,i,o) * omega_iarr[i];
1330
1331                if (hemi_total > 1.01) {
1332                        sprintf(errmsg,
1333                        "outgoing BSDF direction %d collects %.1f%% of light",
1334                                        o, 100.*hemi_total);
1335                        error(WARNING, errmsg);
1336                }
1337                full_total += hemi_total*omega_oarr[o];
1338        }
1339        full_total /= PI;
1340        if (full_total > 1.00001) {
1341                sprintf(errmsg, "BSDF transfers %.4f%% of light",
1342                                100.*full_total);
1343                error(WARNING, errmsg);
1344        }
1345        free(omega_iarr); free(omega_oarr);
1346        return(1);
1347 }
1348
1349
1350 struct BSDF_data *
1351 load_BSDF(              /* load BSDF data from file */
1352        char *fname
1353 )
1354 {
1355        char                    *path;
1356        ezxml_t                 fl, wtl, wld, wdb;
1357        struct BSDF_data        *dp;
1358        
1359        path = getpath(fname, getrlibpath(), R_OK);
1360        if (path == NULL) {
1361                sprintf(errmsg, "cannot find BSDF file \"%s\"", fname);
1362                error(WARNING, errmsg);
1363                return(NULL);
1364        }
1365        fl = ezxml_parse_file(path);
1366        if (fl == NULL) {
1367                sprintf(errmsg, "cannot open BSDF \"%s\"", path);
1368                error(WARNING, errmsg);
1369                return(NULL);
1370        }
1371        if (ezxml_error(fl)[0]) {
1372                sprintf(errmsg, "BSDF \"%s\" %s", path, ezxml_error(fl));
1373                error(WARNING, errmsg);
1374                ezxml_free(fl);
1375                return(NULL);
1376        }
1377        if (strcmp(ezxml_name(fl), "WindowElement")) {
1378                sprintf(errmsg,
1379                        "BSDF \"%s\": top level node not 'WindowElement'",
1380                                path);
1381                error(WARNING, errmsg);
1382                ezxml_free(fl);
1383                return(NULL);
1384        }
1385        wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
1386        if (strcasecmp(ezxml_txt(ezxml_child(ezxml_child(wtl,
1387                        "DataDefinition"), "IncidentDataStructure")),
1388                        "Columns")) {
1389                sprintf(errmsg,
1390                        "BSDF \"%s\": unsupported IncidentDataStructure",
1391                                path);
1392                error(WARNING, errmsg);
1393                ezxml_free(fl);
1394                return(NULL);
1395        }
1396        for (wld = ezxml_child(ezxml_child(wtl,
1397                                "DataDefinition"), "AngleBasis");
1398                        wld != NULL; wld = wld->next)
1399                load_angle_basis(wld);
1400        dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
1401        load_geometry(dp, ezxml_child(wtl, "Material"));
1402        for (wld = ezxml_child(wtl, "WavelengthData");
1403                                wld != NULL; wld = wld->next) {
1404                if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")),
1405                                "Visible"))
1406                        continue;
1407                for (wdb = ezxml_child(wld, "WavelengthDataBlock");
1408                                wdb != NULL; wdb = wdb->next)
1409                        if (!strcasecmp(ezxml_txt(ezxml_child(wdb,
1410                                        "WavelengthDataDirection")),
1411                                        "Transmission Front"))
1412                                break;
1413                if (wdb != NULL) {              /* load front BTDF */
1414                        load_bsdf_data(dp, wdb);
1415                        break;                  /* ignore the rest */
1416                }
1417        }
1418        ezxml_free(fl);                         /* done with XML file */
1419        if (!check_bsdf_data(dp)) {
1420                sprintf(errmsg, "bad/missing BTDF data in \"%s\"", path);
1421                error(WARNING, errmsg);
1422                free_BSDF(dp);
1423                dp = NULL;
1424        }
1425        return(dp);
1426 }
1427
1428
1429 void
1430 free_BSDF(              /* free BSDF data structure */
1431        struct BSDF_data *b
1432 )
1433 {
1434        if (b == NULL)
1435                return;
1436        if (b->mgf != NULL)
1437                free(b->mgf);
1438        if (b->bsdf != NULL)
1439                free(b->bsdf);
1440        free(b);
1441 }
1442
1443
1444 int
1445 r_BSDF_incvec(          /* compute random input vector at given location */
1446        FVECT v,
1447        struct BSDF_data *b,
1448        int i,
1449        double rv,
1450        MAT4 xm
1451 )
1452 {
1453        FVECT   pert;
1454        double  rad;
1455        int     j;
1456        
1457        if (!getBSDF_incvec(v, b, i))
1458                return(0);
1459        rad = sqrt(getBSDF_incohm(b, i) / PI);
1460        SDmultiSamp(pert, 3, rv);
1461        for (j = 0; j < 3; j++)
1462                v[j] += rad*(2.*pert[j] - 1.);
1463        if (xm != NULL)
1464                multv3(v, v, xm);
1465        return(normalize(v) != 0.0);
1466 }
1467
1468
1469 int
1470 r_BSDF_outvec(          /* compute random output vector at given location */
1471        FVECT v,
1472        struct BSDF_data *b,
1473        int o,
1474        double rv,
1475        MAT4 xm
1476 )
1477 {
1478        FVECT   pert;
1479        double  rad;
1480        int     j;
1481        
1482        if (!getBSDF_outvec(v, b, o))
1483                return(0);
1484        rad = sqrt(getBSDF_outohm(b, o) / PI);
1485        SDmultiSamp(pert, 3, rv);
1486        for (j = 0; j < 3; j++)
1487                v[j] += rad*(2.*pert[j] - 1.);
1488        if (xm != NULL)
1489                multv3(v, v, xm);
1490        return(normalize(v) != 0.0);
1491 }
1492
1493
1494 static int
1495 addrot(                 /* compute rotation (x,y,z) => (xp,yp,zp) */
1496        char *xfarg[],
1497        FVECT xp,
1498        FVECT yp,
1499        FVECT zp
1500 )
1501 {
1502        static char     bufs[3][16];
1503        int     bn = 0;
1504        char    **xfp = xfarg;
1505        double  theta;
1506
1507        if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) {
1508                /* Special case for X' along Z-axis */
1509                theta = -atan2(yp[0], yp[1]);
1510                *xfp++ = "-ry";
1511                *xfp++ = xp[2] < 0.0 ? "90" : "-90";
1512                *xfp++ = "-rz";
1513                sprintf(bufs[bn], "%f", theta*(180./PI));
1514                *xfp++ = bufs[bn++];
1515                return(xfp - xfarg);
1516        }
1517        theta = atan2(yp[2], zp[2]);
1518        if (!FEQ(theta,0.0)) {
1519                *xfp++ = "-rx";
1520                sprintf(bufs[bn], "%f", theta*(180./PI));
1521                *xfp++ = bufs[bn++];
1522        }
1523        theta = asin(-xp[2]);
1524        if (!FEQ(theta,0.0)) {
1525                *xfp++ = "-ry";
1526                sprintf(bufs[bn], " %f", theta*(180./PI));
1527                *xfp++ = bufs[bn++];
1528        }
1529        theta = atan2(xp[1], xp[0]);
1530        if (!FEQ(theta,0.0)) {
1531                *xfp++ = "-rz";
1532                sprintf(bufs[bn], "%f", theta*(180./PI));
1533                *xfp++ = bufs[bn++];
1534        }
1535        *xfp = NULL;
1536        return(xfp - xfarg);
1537 }
1538
1539
1540 int
1541 getBSDF_xfm(            /* compute BSDF orient. -> world orient. transform */
1542        MAT4 xm,
1543        FVECT nrm,
1544        UpDir ud,
1545        char *xfbuf
1546 )
1547 {
1548        char    *xfargs[7];
1549        XF      myxf;
1550        FVECT   updir, xdest, ydest;
1551        int     i;
1552
1553        updir[0] = updir[1] = updir[2] = 0.;
1554        switch (ud) {
1555        case UDzneg:
1556                updir[2] = -1.;
1557                break;
1558        case UDyneg:
1559                updir[1] = -1.;
1560                break;
1561        case UDxneg:
1562                updir[0] = -1.;
1563                break;
1564        case UDxpos:
1565                updir[0] = 1.;
1566                break;
1567        case UDypos:
1568                updir[1] = 1.;
1569                break;
1570        case UDzpos:
1571                updir[2] = 1.;
1572                break;
1573        case UDunknown:
1574                return(0);
1575        }
1576        fcross(xdest, updir, nrm);
1577        if (normalize(xdest) == 0.0)
1578                return(0);
1579        fcross(ydest, nrm, xdest);
1580        xf(&myxf, addrot(xfargs, xdest, ydest, nrm), xfargs);
1581        copymat4(xm, myxf.xfm);
1582        if (xfbuf == NULL)
1583                return(1);
1584                                /* return xf arguments as well */
1585        for (i = 0; xfargs[i] != NULL; i++) {
1586                *xfbuf++ = ' ';
1587                strcpy(xfbuf, xfargs[i]);
1588                while (*xfbuf) ++xfbuf;
1589        }
1590        return(1);
1591 }
1592
1593 /*######### END DEPRECATED ROUTINES #######*/
1594 /*################################################################*/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines