| 1 | greg | 3.2 | #ifndef lint | 
| 2 | greg | 3.9 | static const char RCSid[] = "$Id: bsdf_m.c,v 3.8 2011/02/24 20:14:26 greg Exp $"; | 
| 3 | greg | 3.2 | #endif | 
| 4 | greg | 3.1 | /* | 
| 5 |  |  | *  bsdf_m.c | 
| 6 |  |  | * | 
| 7 |  |  | *  Definitions supporting BSDF matrices | 
| 8 |  |  | * | 
| 9 |  |  | *  Created by Greg Ward on 2/2/11. | 
| 10 |  |  | *  Copyright 2011 Anyhere Software. All rights reserved. | 
| 11 |  |  | * | 
| 12 |  |  | */ | 
| 13 |  |  |  | 
| 14 | greg | 3.3 | #include "rtio.h" | 
| 15 | greg | 3.1 | #include <stdlib.h> | 
| 16 |  |  | #include <math.h> | 
| 17 |  |  | #include <ctype.h> | 
| 18 |  |  | #include "ezxml.h" | 
| 19 |  |  | #include "bsdf.h" | 
| 20 |  |  | #include "bsdf_m.h" | 
| 21 |  |  |  | 
| 22 |  |  | /* Function return codes */ | 
| 23 |  |  | #define RC_GOOD         1 | 
| 24 |  |  | #define RC_FAIL         0 | 
| 25 |  |  | #define RC_FORMERR      (-1) | 
| 26 |  |  | #define RC_DATERR       (-2) | 
| 27 |  |  | #define RC_UNSUPP       (-3) | 
| 28 |  |  | #define RC_INTERR       (-4) | 
| 29 |  |  | #define RC_MEMERR       (-5) | 
| 30 |  |  |  | 
| 31 |  |  | #define MAXLATS         46              /* maximum number of latitudes */ | 
| 32 |  |  |  | 
| 33 |  |  | /* BSDF angle specification */ | 
| 34 |  |  | typedef struct { | 
| 35 |  |  | char    name[64];               /* basis name */ | 
| 36 |  |  | int     nangles;                /* total number of directions */ | 
| 37 |  |  | struct { | 
| 38 |  |  | float   tmin;                   /* starting theta */ | 
| 39 |  |  | int     nphis;                  /* number of phis (0 term) */ | 
| 40 |  |  | } lat[MAXLATS+1];               /* latitudes */ | 
| 41 |  |  | } ANGLE_BASIS; | 
| 42 |  |  |  | 
| 43 |  |  | #define MAXABASES       7               /* limit on defined bases */ | 
| 44 |  |  |  | 
| 45 |  |  | static ANGLE_BASIS      abase_list[MAXABASES] = { | 
| 46 |  |  | { | 
| 47 |  |  | "LBNL/Klems Full", 145, | 
| 48 |  |  | { {-5., 1}, | 
| 49 |  |  | {5., 8}, | 
| 50 |  |  | {15., 16}, | 
| 51 |  |  | {25., 20}, | 
| 52 |  |  | {35., 24}, | 
| 53 |  |  | {45., 24}, | 
| 54 |  |  | {55., 24}, | 
| 55 |  |  | {65., 16}, | 
| 56 |  |  | {75., 12}, | 
| 57 |  |  | {90., 0} } | 
| 58 |  |  | }, { | 
| 59 |  |  | "LBNL/Klems Half", 73, | 
| 60 |  |  | { {-6.5, 1}, | 
| 61 |  |  | {6.5, 8}, | 
| 62 |  |  | {19.5, 12}, | 
| 63 |  |  | {32.5, 16}, | 
| 64 |  |  | {46.5, 20}, | 
| 65 |  |  | {61.5, 12}, | 
| 66 |  |  | {76.5, 4}, | 
| 67 |  |  | {90., 0} } | 
| 68 |  |  | }, { | 
| 69 |  |  | "LBNL/Klems Quarter", 41, | 
| 70 |  |  | { {-9., 1}, | 
| 71 |  |  | {9., 8}, | 
| 72 |  |  | {27., 12}, | 
| 73 |  |  | {46., 12}, | 
| 74 |  |  | {66., 8}, | 
| 75 |  |  | {90., 0} } | 
| 76 |  |  | } | 
| 77 |  |  | }; | 
| 78 |  |  |  | 
| 79 |  |  | static int      nabases = 3;    /* current number of defined bases */ | 
| 80 |  |  |  | 
| 81 |  |  | static int | 
| 82 |  |  | fequal(double a, double b) | 
| 83 |  |  | { | 
| 84 |  |  | if (b != .0) | 
| 85 |  |  | a = a/b - 1.; | 
| 86 |  |  | return (a <= 1e-6) & (a >= -1e-6); | 
| 87 |  |  | } | 
| 88 |  |  |  | 
| 89 | greg | 3.9 | /* Returns the name of the given tag */ | 
| 90 | greg | 3.1 | #ifdef ezxml_name | 
| 91 |  |  | #undef ezxml_name | 
| 92 |  |  | static char * | 
| 93 |  |  | ezxml_name(ezxml_t xml) | 
| 94 |  |  | { | 
| 95 |  |  | if (xml == NULL) | 
| 96 |  |  | return NULL; | 
| 97 |  |  | return xml->name; | 
| 98 |  |  | } | 
| 99 |  |  | #endif | 
| 100 |  |  |  | 
| 101 | greg | 3.9 | /* Returns the given tag's character content or empty string if none */ | 
| 102 | greg | 3.1 | #ifdef ezxml_txt | 
| 103 |  |  | #undef ezxml_txt | 
| 104 |  |  | static char * | 
| 105 |  |  | ezxml_txt(ezxml_t xml) | 
| 106 |  |  | { | 
| 107 |  |  | if (xml == NULL) | 
| 108 |  |  | return ""; | 
| 109 |  |  | return xml->txt; | 
| 110 |  |  | } | 
| 111 |  |  | #endif | 
| 112 |  |  |  | 
| 113 |  |  | /* Convert error to standard BSDF code */ | 
| 114 |  |  | static SDError | 
| 115 |  |  | convert_errcode(int ec) | 
| 116 |  |  | { | 
| 117 |  |  | switch (ec) { | 
| 118 |  |  | case RC_GOOD: | 
| 119 |  |  | return SDEnone; | 
| 120 |  |  | case RC_FORMERR: | 
| 121 |  |  | return SDEformat; | 
| 122 |  |  | case RC_DATERR: | 
| 123 |  |  | return SDEdata; | 
| 124 |  |  | case RC_UNSUPP: | 
| 125 |  |  | return SDEsupport; | 
| 126 |  |  | case RC_INTERR: | 
| 127 |  |  | return SDEinternal; | 
| 128 |  |  | case RC_MEMERR: | 
| 129 |  |  | return SDEmemory; | 
| 130 |  |  | } | 
| 131 |  |  | return SDEunknown; | 
| 132 |  |  | } | 
| 133 |  |  |  | 
| 134 |  |  | /* Allocate a BSDF matrix of the given size */ | 
| 135 |  |  | static SDMat * | 
| 136 |  |  | SDnewMatrix(int ni, int no) | 
| 137 |  |  | { | 
| 138 |  |  | SDMat           *sm; | 
| 139 |  |  |  | 
| 140 |  |  | if ((ni <= 0) | (no <= 0)) { | 
| 141 |  |  | strcpy(SDerrorDetail, "Empty BSDF matrix request"); | 
| 142 |  |  | return NULL; | 
| 143 |  |  | } | 
| 144 |  |  | sm = (SDMat *)malloc(sizeof(SDMat) + (ni*no - 1)*sizeof(float)); | 
| 145 |  |  | if (sm == NULL) { | 
| 146 |  |  | sprintf(SDerrorDetail, "Cannot allocate %dx%d BSDF matrix", | 
| 147 |  |  | ni, no); | 
| 148 |  |  | return NULL; | 
| 149 |  |  | } | 
| 150 |  |  | memset(sm, 0, sizeof(SDMat)-sizeof(float)); | 
| 151 |  |  | sm->ninc = ni; | 
| 152 |  |  | sm->nout = no; | 
| 153 |  |  |  | 
| 154 |  |  | return sm; | 
| 155 |  |  | } | 
| 156 |  |  |  | 
| 157 |  |  | /* Free a BSDF matrix */ | 
| 158 |  |  | #define SDfreeMatrix            free | 
| 159 |  |  |  | 
| 160 | greg | 3.8 | /* get vector for this angle basis index (front exiting) */ | 
| 161 | greg | 3.1 | static int | 
| 162 | greg | 3.9 | fo_getvec(FVECT v, double ndxr, void *p) | 
| 163 | greg | 3.1 | { | 
| 164 | greg | 3.9 | ANGLE_BASIS     *ab = (ANGLE_BASIS *)p; | 
| 165 |  |  | int             ndx = (int)ndxr; | 
| 166 |  |  | double          randX = ndxr - ndx; | 
| 167 | greg | 3.1 | double          rx[2]; | 
| 168 |  |  | int             li; | 
| 169 |  |  | double          pol, azi, d; | 
| 170 |  |  |  | 
| 171 | greg | 3.9 | if ((ndxr < 0) | (ndx >= ab->nangles)) | 
| 172 | greg | 3.1 | return RC_FAIL; | 
| 173 |  |  | for (li = 0; ndx >= ab->lat[li].nphis; li++) | 
| 174 |  |  | ndx -= ab->lat[li].nphis; | 
| 175 |  |  | SDmultiSamp(rx, 2, randX); | 
| 176 |  |  | pol = M_PI/180.*( (1.-rx[0])*ab->lat[li].tmin + | 
| 177 |  |  | rx[0]*ab->lat[li+1].tmin ); | 
| 178 |  |  | azi = 2.*M_PI*(ndx + rx[1] - .5)/ab->lat[li].nphis; | 
| 179 |  |  | v[2] = d = cos(pol); | 
| 180 |  |  | d = sqrt(1. - d*d);     /* sin(pol) */ | 
| 181 |  |  | v[0] = cos(azi)*d; | 
| 182 |  |  | v[1] = sin(azi)*d; | 
| 183 |  |  | return RC_GOOD; | 
| 184 |  |  | } | 
| 185 |  |  |  | 
| 186 | greg | 3.8 | /* get index corresponding to the given vector (front exiting) */ | 
| 187 | greg | 3.1 | static int | 
| 188 | greg | 3.8 | fo_getndx(const FVECT v, void *p) | 
| 189 | greg | 3.1 | { | 
| 190 | greg | 3.9 | ANGLE_BASIS     *ab = (ANGLE_BASIS *)p; | 
| 191 | greg | 3.1 | int     li, ndx; | 
| 192 |  |  | double  pol, azi, d; | 
| 193 |  |  |  | 
| 194 |  |  | if (v == NULL) | 
| 195 |  |  | return -1; | 
| 196 |  |  | if ((v[2] < .0) | (v[2] > 1.0)) | 
| 197 |  |  | return -1; | 
| 198 |  |  | pol = 180.0/M_PI*acos(v[2]); | 
| 199 |  |  | azi = 180.0/M_PI*atan2(v[1], v[0]); | 
| 200 |  |  | if (azi < 0.0) azi += 360.0; | 
| 201 |  |  | for (li = 1; ab->lat[li].tmin <= pol; li++) | 
| 202 |  |  | if (!ab->lat[li].nphis) | 
| 203 |  |  | return -1; | 
| 204 |  |  | --li; | 
| 205 |  |  | ndx = (int)((1./360.)*azi*ab->lat[li].nphis + 0.5); | 
| 206 |  |  | if (ndx >= ab->lat[li].nphis) ndx = 0; | 
| 207 |  |  | while (li--) | 
| 208 |  |  | ndx += ab->lat[li].nphis; | 
| 209 |  |  | return ndx; | 
| 210 |  |  | } | 
| 211 |  |  |  | 
| 212 |  |  | /* compute square of real value */ | 
| 213 |  |  | static double sq(double x) { return x*x; } | 
| 214 |  |  |  | 
| 215 | greg | 3.8 | /* get projected solid angle for this angle basis index (universal) */ | 
| 216 | greg | 3.1 | static double | 
| 217 | greg | 3.8 | io_getohm(int ndx, void *p) | 
| 218 | greg | 3.1 | { | 
| 219 |  |  | static int      last_li = -1; | 
| 220 |  |  | static double   last_ohm; | 
| 221 |  |  | ANGLE_BASIS     *ab = (ANGLE_BASIS *)p; | 
| 222 |  |  | int             li; | 
| 223 |  |  | double          theta, theta1; | 
| 224 |  |  |  | 
| 225 |  |  | if ((ndx < 0) | (ndx >= ab->nangles)) | 
| 226 |  |  | return -1.; | 
| 227 |  |  | for (li = 0; ndx >= ab->lat[li].nphis; li++) | 
| 228 |  |  | ndx -= ab->lat[li].nphis; | 
| 229 |  |  | if (li == last_li)                      /* cached latitude? */ | 
| 230 |  |  | return last_ohm; | 
| 231 |  |  | last_li = li; | 
| 232 |  |  | theta1 = M_PI/180. * ab->lat[li+1].tmin; | 
| 233 |  |  | if (ab->lat[li].nphis == 1)             /* special case */ | 
| 234 |  |  | return last_ohm = M_PI*(1. - sq(cos(theta1))); | 
| 235 |  |  | theta = M_PI/180. * ab->lat[li].tmin; | 
| 236 |  |  | return last_ohm = M_PI*(sq(cos(theta)) - sq(cos(theta1))) / | 
| 237 |  |  | (double)ab->lat[li].nphis; | 
| 238 |  |  | } | 
| 239 |  |  |  | 
| 240 | greg | 3.8 | /* get vector for this angle basis index (back incident) */ | 
| 241 | greg | 3.1 | static int | 
| 242 | greg | 3.9 | bi_getvec(FVECT v, double ndxr, void *p) | 
| 243 | greg | 3.1 | { | 
| 244 | greg | 3.9 | if (!fo_getvec(v, ndxr, p)) | 
| 245 | greg | 3.1 | return RC_FAIL; | 
| 246 |  |  |  | 
| 247 |  |  | v[0] = -v[0]; | 
| 248 |  |  | v[1] = -v[1]; | 
| 249 |  |  | v[2] = -v[2]; | 
| 250 |  |  |  | 
| 251 |  |  | return RC_GOOD; | 
| 252 |  |  | } | 
| 253 |  |  |  | 
| 254 | greg | 3.8 | /* get index corresponding to the vector (back incident) */ | 
| 255 | greg | 3.1 | static int | 
| 256 | greg | 3.8 | bi_getndx(const FVECT v, void *p) | 
| 257 | greg | 3.1 | { | 
| 258 |  |  | FVECT  v2; | 
| 259 |  |  |  | 
| 260 |  |  | v2[0] = -v[0]; | 
| 261 |  |  | v2[1] = -v[1]; | 
| 262 |  |  | v2[2] = -v[2]; | 
| 263 |  |  |  | 
| 264 | greg | 3.8 | return fo_getndx(v2, p); | 
| 265 |  |  | } | 
| 266 |  |  |  | 
| 267 |  |  | /* get vector for this angle basis index (back exiting) */ | 
| 268 |  |  | static int | 
| 269 | greg | 3.9 | bo_getvec(FVECT v, double ndxr, void *p) | 
| 270 | greg | 3.8 | { | 
| 271 | greg | 3.9 | if (!fo_getvec(v, ndxr, p)) | 
| 272 | greg | 3.8 | return RC_FAIL; | 
| 273 |  |  |  | 
| 274 |  |  | v[2] = -v[2]; | 
| 275 |  |  |  | 
| 276 |  |  | return RC_GOOD; | 
| 277 |  |  | } | 
| 278 |  |  |  | 
| 279 |  |  | /* get index corresponding to the vector (back exiting) */ | 
| 280 |  |  | static int | 
| 281 |  |  | bo_getndx(const FVECT v, void *p) | 
| 282 |  |  | { | 
| 283 |  |  | FVECT  v2; | 
| 284 |  |  |  | 
| 285 |  |  | v2[0] = v[0]; | 
| 286 |  |  | v2[1] = v[1]; | 
| 287 |  |  | v2[2] = -v[2]; | 
| 288 |  |  |  | 
| 289 |  |  | return fo_getndx(v2, p); | 
| 290 |  |  | } | 
| 291 |  |  |  | 
| 292 |  |  | /* get vector for this angle basis index (front incident) */ | 
| 293 |  |  | static int | 
| 294 | greg | 3.9 | fi_getvec(FVECT v, double ndxr, void *p) | 
| 295 | greg | 3.8 | { | 
| 296 | greg | 3.9 | if (!fo_getvec(v, ndxr, p)) | 
| 297 | greg | 3.8 | return RC_FAIL; | 
| 298 |  |  |  | 
| 299 |  |  | v[0] = -v[0]; | 
| 300 |  |  | v[1] = -v[1]; | 
| 301 |  |  |  | 
| 302 |  |  | return RC_GOOD; | 
| 303 |  |  | } | 
| 304 |  |  |  | 
| 305 |  |  | /* get index corresponding to the vector (front incident) */ | 
| 306 |  |  | static int | 
| 307 |  |  | fi_getndx(const FVECT v, void *p) | 
| 308 |  |  | { | 
| 309 |  |  | FVECT  v2; | 
| 310 |  |  |  | 
| 311 |  |  | v2[0] = -v[0]; | 
| 312 |  |  | v2[1] = -v[1]; | 
| 313 |  |  | v2[2] = v[2]; | 
| 314 |  |  |  | 
| 315 |  |  | return fo_getndx(v2, p); | 
| 316 | greg | 3.1 | } | 
| 317 |  |  |  | 
| 318 |  |  | /* load custom BSDF angle basis */ | 
| 319 |  |  | static int | 
| 320 |  |  | load_angle_basis(ezxml_t wab) | 
| 321 |  |  | { | 
| 322 |  |  | char    *abname = ezxml_txt(ezxml_child(wab, "AngleBasisName")); | 
| 323 |  |  | ezxml_t wbb; | 
| 324 |  |  | int     i; | 
| 325 |  |  |  | 
| 326 |  |  | if (!abname || !*abname) | 
| 327 |  |  | return RC_FAIL; | 
| 328 |  |  | for (i = nabases; i--; ) | 
| 329 |  |  | if (!strcasecmp(abname, abase_list[i].name)) | 
| 330 |  |  | return RC_GOOD; /* assume it's the same */ | 
| 331 |  |  | if (nabases >= MAXABASES) { | 
| 332 |  |  | sprintf(SDerrorDetail, "Out of angle bases reading '%s'", | 
| 333 |  |  | abname); | 
| 334 |  |  | return RC_INTERR; | 
| 335 |  |  | } | 
| 336 |  |  | strcpy(abase_list[nabases].name, abname); | 
| 337 |  |  | abase_list[nabases].nangles = 0; | 
| 338 |  |  | for (i = 0, wbb = ezxml_child(wab, "AngleBasisBlock"); | 
| 339 |  |  | wbb != NULL; i++, wbb = wbb->next) { | 
| 340 |  |  | if (i >= MAXLATS) { | 
| 341 |  |  | sprintf(SDerrorDetail, "Too many latitudes for '%s'", | 
| 342 |  |  | abname); | 
| 343 |  |  | return RC_INTERR; | 
| 344 |  |  | } | 
| 345 |  |  | abase_list[nabases].lat[i+1].tmin = atof(ezxml_txt( | 
| 346 |  |  | ezxml_child(ezxml_child(wbb, | 
| 347 |  |  | "ThetaBounds"), "UpperTheta"))); | 
| 348 |  |  | if (!i) | 
| 349 |  |  | abase_list[nabases].lat[i].tmin = | 
| 350 |  |  | -abase_list[nabases].lat[i+1].tmin; | 
| 351 |  |  | else if (!fequal(atof(ezxml_txt(ezxml_child(ezxml_child(wbb, | 
| 352 |  |  | "ThetaBounds"), "LowerTheta"))), | 
| 353 |  |  | abase_list[nabases].lat[i].tmin)) { | 
| 354 |  |  | sprintf(SDerrorDetail, "Theta values disagree in '%s'", | 
| 355 |  |  | abname); | 
| 356 |  |  | return RC_DATERR; | 
| 357 |  |  | } | 
| 358 |  |  | abase_list[nabases].nangles += | 
| 359 |  |  | abase_list[nabases].lat[i].nphis = | 
| 360 |  |  | atoi(ezxml_txt(ezxml_child(wbb, "nPhis"))); | 
| 361 |  |  | if (abase_list[nabases].lat[i].nphis <= 0 || | 
| 362 |  |  | (abase_list[nabases].lat[i].nphis == 1 && | 
| 363 |  |  | abase_list[nabases].lat[i].tmin > FTINY)) { | 
| 364 |  |  | sprintf(SDerrorDetail, "Illegal phi count in '%s'", | 
| 365 |  |  | abname); | 
| 366 |  |  | return RC_DATERR; | 
| 367 |  |  | } | 
| 368 |  |  | } | 
| 369 |  |  | abase_list[nabases++].lat[i].nphis = 0; | 
| 370 |  |  | return RC_GOOD; | 
| 371 |  |  | } | 
| 372 |  |  |  | 
| 373 |  |  | /* compute min. proj. solid angle and max. direct hemispherical scattering */ | 
| 374 |  |  | static int | 
| 375 |  |  | get_extrema(SDSpectralDF *df) | 
| 376 |  |  | { | 
| 377 |  |  | SDMat   *dp = (SDMat *)df->comp[0].dist; | 
| 378 |  |  | double  *ohma; | 
| 379 |  |  | int     i, o; | 
| 380 |  |  | /* initialize extrema */ | 
| 381 |  |  | df->minProjSA = M_PI; | 
| 382 |  |  | df->maxHemi = .0; | 
| 383 |  |  | ohma = (double *)malloc(dp->nout*sizeof(double)); | 
| 384 |  |  | if (ohma == NULL) | 
| 385 |  |  | return RC_MEMERR; | 
| 386 |  |  | /* get outgoing solid angles */ | 
| 387 |  |  | for (o = dp->nout; o--; ) | 
| 388 |  |  | if ((ohma[o] = mBSDF_outohm(dp,o)) < df->minProjSA) | 
| 389 |  |  | df->minProjSA = ohma[o]; | 
| 390 |  |  | /* compute hemispherical sums */ | 
| 391 |  |  | for (i = dp->ninc; i--; ) { | 
| 392 |  |  | double  hemi = .0; | 
| 393 |  |  | for (o = dp->nout; o--; ) | 
| 394 |  |  | hemi += ohma[o] * mBSDF_value(dp, i, o); | 
| 395 |  |  | if (hemi > df->maxHemi) | 
| 396 |  |  | df->maxHemi = hemi; | 
| 397 |  |  | } | 
| 398 |  |  | free(ohma); | 
| 399 |  |  | /* need incoming solid angles, too? */ | 
| 400 | greg | 3.5 | if ((dp->ib_ohm != dp->ob_ohm) | (dp->ib_priv != dp->ob_priv)) { | 
| 401 | greg | 3.1 | double  ohm; | 
| 402 |  |  | for (i = dp->ninc; i--; ) | 
| 403 |  |  | if ((ohm = mBSDF_incohm(dp,i)) < df->minProjSA) | 
| 404 |  |  | df->minProjSA = ohm; | 
| 405 |  |  | } | 
| 406 |  |  | return (df->maxHemi <= 1.01); | 
| 407 |  |  | } | 
| 408 |  |  |  | 
| 409 |  |  | /* load BSDF distribution for this wavelength */ | 
| 410 |  |  | static int | 
| 411 |  |  | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc) | 
| 412 |  |  | { | 
| 413 |  |  | SDSpectralDF    *df; | 
| 414 |  |  | SDMat           *dp; | 
| 415 |  |  | char            *sdata; | 
| 416 | greg | 3.8 | int             tfront; | 
| 417 | greg | 3.1 | int             inbi, outbi; | 
| 418 |  |  | int             i; | 
| 419 |  |  | /* allocate BSDF component */ | 
| 420 |  |  | sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection")); | 
| 421 | greg | 3.8 | /* | 
| 422 |  |  | * Remember that front and back are reversed from WINDOW 6 orientations | 
| 423 | greg | 3.9 | * Favor their "Front" (incoming light) since that's more often valid | 
| 424 | greg | 3.8 | */ | 
| 425 | greg | 3.9 | tfront = !strcasecmp(sdata, "Transmission Back"); | 
| 426 |  |  | if (!strcasecmp(sdata, "Transmission Front") || | 
| 427 |  |  | tfront & (sd->tf == NULL)) { | 
| 428 | greg | 3.1 | if (sd->tf != NULL) | 
| 429 |  |  | SDfreeSpectralDF(sd->tf); | 
| 430 |  |  | if ((sd->tf = SDnewSpectralDF(1)) == NULL) | 
| 431 |  |  | return RC_MEMERR; | 
| 432 |  |  | df = sd->tf; | 
| 433 |  |  | } else if (!strcasecmp(sdata, "Reflection Front")) { | 
| 434 | greg | 3.6 | if (sd->rb != NULL)     /* note back-front reversal */ | 
| 435 |  |  | SDfreeSpectralDF(sd->rb); | 
| 436 |  |  | if ((sd->rb = SDnewSpectralDF(1)) == NULL) | 
| 437 |  |  | return RC_MEMERR; | 
| 438 |  |  | df = sd->rb; | 
| 439 |  |  | } else if (!strcasecmp(sdata, "Reflection Back")) { | 
| 440 |  |  | if (sd->rf != NULL)     /* note front-back reversal */ | 
| 441 | greg | 3.1 | SDfreeSpectralDF(sd->rf); | 
| 442 |  |  | if ((sd->rf = SDnewSpectralDF(1)) == NULL) | 
| 443 |  |  | return RC_MEMERR; | 
| 444 |  |  | df = sd->rf; | 
| 445 |  |  | } else | 
| 446 |  |  | return RC_FAIL; | 
| 447 | greg | 3.4 | /* XXX should also check "ScatteringDataType" for consistency? */ | 
| 448 | greg | 3.1 | /* get angle bases */ | 
| 449 |  |  | sdata = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis")); | 
| 450 |  |  | if (!sdata || !*sdata) { | 
| 451 |  |  | sprintf(SDerrorDetail, "Missing column basis for BSDF '%s'", | 
| 452 |  |  | sd->name); | 
| 453 |  |  | return RC_FORMERR; | 
| 454 |  |  | } | 
| 455 |  |  | for (inbi = nabases; inbi--; ) | 
| 456 | greg | 3.4 | if (!strcasecmp(sdata, abase_list[inbi].name)) | 
| 457 | greg | 3.1 | break; | 
| 458 |  |  | if (inbi < 0) { | 
| 459 | greg | 3.4 | sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'", sdata); | 
| 460 | greg | 3.1 | return RC_FORMERR; | 
| 461 |  |  | } | 
| 462 |  |  | sdata = ezxml_txt(ezxml_child(wdb,"RowAngleBasis")); | 
| 463 |  |  | if (!sdata || !*sdata) { | 
| 464 |  |  | sprintf(SDerrorDetail, "Missing row basis for BSDF '%s'", | 
| 465 |  |  | sd->name); | 
| 466 |  |  | return RC_FORMERR; | 
| 467 |  |  | } | 
| 468 |  |  | for (outbi = nabases; outbi--; ) | 
| 469 | greg | 3.4 | if (!strcasecmp(sdata, abase_list[outbi].name)) | 
| 470 | greg | 3.1 | break; | 
| 471 |  |  | if (outbi < 0) { | 
| 472 | greg | 3.4 | sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", sdata); | 
| 473 | greg | 3.1 | return RC_FORMERR; | 
| 474 |  |  | } | 
| 475 |  |  | /* allocate BSDF matrix */ | 
| 476 |  |  | dp = SDnewMatrix(abase_list[inbi].nangles, abase_list[outbi].nangles); | 
| 477 |  |  | if (dp == NULL) | 
| 478 |  |  | return RC_MEMERR; | 
| 479 | greg | 3.5 | dp->ib_priv = &abase_list[inbi]; | 
| 480 |  |  | dp->ob_priv = &abase_list[outbi]; | 
| 481 | greg | 3.1 | if (df == sd->tf) { | 
| 482 | greg | 3.8 | if (tfront) { | 
| 483 |  |  | dp->ib_vec = &fi_getvec; | 
| 484 |  |  | dp->ib_ndx = &fi_getndx; | 
| 485 |  |  | dp->ob_vec = &bo_getvec; | 
| 486 |  |  | dp->ob_ndx = &bo_getndx; | 
| 487 | greg | 3.7 | } else { | 
| 488 | greg | 3.8 | dp->ib_vec = &bi_getvec; | 
| 489 |  |  | dp->ib_ndx = &bi_getndx; | 
| 490 |  |  | dp->ob_vec = &fo_getvec; | 
| 491 |  |  | dp->ob_ndx = &fo_getndx; | 
| 492 | greg | 3.7 | } | 
| 493 | greg | 3.1 | } else if (df == sd->rf) { | 
| 494 | greg | 3.8 | dp->ib_vec = &fi_getvec; | 
| 495 |  |  | dp->ib_ndx = &fi_getndx; | 
| 496 |  |  | dp->ob_vec = &fo_getvec; | 
| 497 |  |  | dp->ob_ndx = &fo_getndx; | 
| 498 | greg | 3.1 | } else /* df == sd->rb */ { | 
| 499 | greg | 3.8 | dp->ib_vec = &bi_getvec; | 
| 500 |  |  | dp->ib_ndx = &bi_getndx; | 
| 501 |  |  | dp->ob_vec = &bo_getvec; | 
| 502 |  |  | dp->ob_ndx = &bo_getndx; | 
| 503 | greg | 3.1 | } | 
| 504 | greg | 3.8 | dp->ib_ohm = &io_getohm; | 
| 505 |  |  | dp->ob_ohm = &io_getohm; | 
| 506 | greg | 3.1 | df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */ | 
| 507 |  |  | df->comp[0].dist = dp; | 
| 508 |  |  | df->comp[0].func = &SDhandleMtx; | 
| 509 |  |  | /* read BSDF data */ | 
| 510 |  |  | sdata  = ezxml_txt(ezxml_child(wdb,"ScatteringData")); | 
| 511 |  |  | if (!sdata || !*sdata) { | 
| 512 |  |  | sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'", | 
| 513 |  |  | sd->name); | 
| 514 |  |  | return RC_FORMERR; | 
| 515 |  |  | } | 
| 516 |  |  | for (i = 0; i < dp->ninc*dp->nout; i++) { | 
| 517 | greg | 3.3 | char  *sdnext = fskip(sdata); | 
| 518 | greg | 3.1 | if (sdnext == NULL) { | 
| 519 |  |  | sprintf(SDerrorDetail, | 
| 520 |  |  | "Bad/missing BSDF ScatteringData in '%s'", | 
| 521 |  |  | sd->name); | 
| 522 |  |  | return RC_FORMERR; | 
| 523 |  |  | } | 
| 524 |  |  | while (*sdnext && isspace(*sdnext)) | 
| 525 |  |  | sdnext++; | 
| 526 |  |  | if (*sdnext == ',') sdnext++; | 
| 527 |  |  | if (rowinc) { | 
| 528 |  |  | int     r = i/dp->nout; | 
| 529 |  |  | int     c = i - c*dp->nout; | 
| 530 |  |  | mBSDF_value(dp,r,c) = atof(sdata); | 
| 531 |  |  | } else | 
| 532 |  |  | dp->bsdf[i] = atof(sdata); | 
| 533 |  |  | sdata = sdnext; | 
| 534 |  |  | } | 
| 535 |  |  | return get_extrema(df); | 
| 536 |  |  | } | 
| 537 |  |  |  | 
| 538 |  |  | /* Subtract minimum (diffuse) scattering amount from BSDF */ | 
| 539 |  |  | static double | 
| 540 |  |  | subtract_min(SDMat *sm) | 
| 541 |  |  | { | 
| 542 |  |  | float   minv = sm->bsdf[0]; | 
| 543 |  |  | int     n = sm->ninc*sm->nout; | 
| 544 |  |  | int     i; | 
| 545 |  |  |  | 
| 546 |  |  | for (i = n; --i; ) | 
| 547 |  |  | if (sm->bsdf[i] < minv) | 
| 548 |  |  | minv = sm->bsdf[i]; | 
| 549 |  |  | for (i = n; i--; ) | 
| 550 |  |  | sm->bsdf[i] -= minv; | 
| 551 |  |  |  | 
| 552 |  |  | return minv*M_PI;               /* be sure to include multiplier */ | 
| 553 |  |  | } | 
| 554 |  |  |  | 
| 555 |  |  | /* Extract and separate diffuse portion of BSDF */ | 
| 556 |  |  | static void | 
| 557 |  |  | extract_diffuse(SDValue *dv, SDSpectralDF *df) | 
| 558 |  |  | { | 
| 559 |  |  | int     n; | 
| 560 |  |  |  | 
| 561 |  |  | if (df == NULL || df->ncomp <= 0) { | 
| 562 |  |  | dv->spec = c_dfcolor; | 
| 563 |  |  | dv->cieY = .0; | 
| 564 |  |  | return; | 
| 565 |  |  | } | 
| 566 |  |  | dv->spec = df->comp[0].cspec[0]; | 
| 567 |  |  | dv->cieY = subtract_min((SDMat *)df->comp[0].dist); | 
| 568 |  |  | /* in case of multiple components */ | 
| 569 |  |  | for (n = df->ncomp; --n; ) { | 
| 570 |  |  | double  ymin = subtract_min((SDMat *)df->comp[n].dist); | 
| 571 |  |  | c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]); | 
| 572 |  |  | dv->cieY += ymin; | 
| 573 |  |  | } | 
| 574 | greg | 3.4 | df->maxHemi -= dv->cieY;        /* adjust minimum hemispherical */ | 
| 575 |  |  | /* make sure everything is set */ | 
| 576 | greg | 3.1 | c_ccvt(&dv->spec, C_CSXY+C_CSSPEC); | 
| 577 |  |  | } | 
| 578 |  |  |  | 
| 579 |  |  | /* Load a BSDF matrix from an open XML file */ | 
| 580 |  |  | SDError | 
| 581 | greg | 3.4 | SDloadMtx(SDData *sd, ezxml_t wtl) | 
| 582 | greg | 3.1 | { | 
| 583 | greg | 3.4 | ezxml_t                 wld, wdb; | 
| 584 | greg | 3.1 | int                     rowIn; | 
| 585 |  |  | struct BSDF_data        *dp; | 
| 586 |  |  | char                    *txt; | 
| 587 |  |  | int                     rval; | 
| 588 |  |  |  | 
| 589 | greg | 3.4 | txt = ezxml_txt(ezxml_child(ezxml_child(wtl, | 
| 590 |  |  | "DataDefinition"), "IncidentDataStructure")); | 
| 591 |  |  | if (txt == NULL || !*txt) { | 
| 592 | greg | 3.1 | sprintf(SDerrorDetail, | 
| 593 | greg | 3.4 | "BSDF \"%s\": missing IncidentDataStructure", | 
| 594 | greg | 3.1 | sd->name); | 
| 595 |  |  | return SDEformat; | 
| 596 |  |  | } | 
| 597 |  |  | if (!strcasecmp(txt, "Rows")) | 
| 598 |  |  | rowIn = 1; | 
| 599 |  |  | else if (!strcasecmp(txt, "Columns")) | 
| 600 |  |  | rowIn = 0; | 
| 601 |  |  | else { | 
| 602 |  |  | sprintf(SDerrorDetail, | 
| 603 |  |  | "BSDF \"%s\": unsupported IncidentDataStructure", | 
| 604 |  |  | sd->name); | 
| 605 |  |  | return SDEsupport; | 
| 606 |  |  | } | 
| 607 |  |  | /* get angle basis */ | 
| 608 |  |  | rval = load_angle_basis(ezxml_child(ezxml_child(wtl, | 
| 609 |  |  | "DataDefinition"), "AngleBasis")); | 
| 610 |  |  | if (rval < 0) | 
| 611 | greg | 3.4 | return convert_errcode(rval); | 
| 612 | greg | 3.1 | /* load BSDF components */ | 
| 613 |  |  | for (wld = ezxml_child(wtl, "WavelengthData"); | 
| 614 |  |  | wld != NULL; wld = wld->next) { | 
| 615 |  |  | if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")), | 
| 616 |  |  | "Visible")) | 
| 617 |  |  | continue;       /* just visible for now */ | 
| 618 |  |  | for (wdb = ezxml_child(wld, "WavelengthDataBlock"); | 
| 619 |  |  | wdb != NULL; wdb = wdb->next) | 
| 620 |  |  | if ((rval = load_bsdf_data(sd, wdb, rowIn)) < 0) | 
| 621 | greg | 3.4 | return convert_errcode(rval); | 
| 622 | greg | 3.1 | } | 
| 623 |  |  | /* separate diffuse components */ | 
| 624 |  |  | extract_diffuse(&sd->rLambFront, sd->rf); | 
| 625 |  |  | extract_diffuse(&sd->rLambBack, sd->rb); | 
| 626 |  |  | extract_diffuse(&sd->tLamb, sd->tf); | 
| 627 |  |  | /* return success */ | 
| 628 |  |  | return SDEnone; | 
| 629 |  |  | } | 
| 630 |  |  |  | 
| 631 |  |  | /* Get Matrix BSDF value */ | 
| 632 |  |  | static int | 
| 633 |  |  | SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec, | 
| 634 |  |  | const FVECT inVec, const void *dist) | 
| 635 |  |  | { | 
| 636 |  |  | const SDMat     *dp = (const SDMat *)dist; | 
| 637 |  |  | int             i_ndx, o_ndx; | 
| 638 |  |  | /* get angle indices */ | 
| 639 |  |  | i_ndx = mBSDF_incndx(dp, inVec); | 
| 640 |  |  | o_ndx = mBSDF_outndx(dp, outVec); | 
| 641 |  |  | /* try reciprocity if necessary */ | 
| 642 |  |  | if ((i_ndx < 0) & (o_ndx < 0)) { | 
| 643 |  |  | i_ndx = mBSDF_incndx(dp, outVec); | 
| 644 |  |  | o_ndx = mBSDF_outndx(dp, inVec); | 
| 645 |  |  | } | 
| 646 |  |  | if ((i_ndx < 0) | (o_ndx < 0)) | 
| 647 |  |  | return 0;               /* nothing from this component */ | 
| 648 |  |  | coef[0] = mBSDF_value(dp, i_ndx, o_ndx); | 
| 649 |  |  | return 1;                       /* XXX monochrome for now */ | 
| 650 |  |  | } | 
| 651 |  |  |  | 
| 652 |  |  | /* Query solid angle for vector */ | 
| 653 |  |  | static SDError | 
| 654 |  |  | SDqueryMtxProjSA(double *psa, const FVECT vec, int qflags, const void *dist) | 
| 655 |  |  | { | 
| 656 |  |  | const SDMat     *dp = (const SDMat *)dist; | 
| 657 | greg | 3.5 | double          inc_psa, out_psa; | 
| 658 |  |  | /* check arguments */ | 
| 659 |  |  | if ((psa == NULL) | (vec == NULL) | (dp == NULL)) | 
| 660 | greg | 3.1 | return SDEargument; | 
| 661 | greg | 3.5 | /* get projected solid angles */ | 
| 662 |  |  | inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, vec)); | 
| 663 |  |  | out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, vec)); | 
| 664 |  |  |  | 
| 665 |  |  | switch (qflags) {               /* record based on flag settings */ | 
| 666 |  |  | case SDqueryVal: | 
| 667 |  |  | psa[0] = .0; | 
| 668 |  |  | /* fall through */ | 
| 669 |  |  | case SDqueryMax: | 
| 670 |  |  | if (inc_psa > psa[0]) | 
| 671 |  |  | psa[0] = inc_psa; | 
| 672 |  |  | if (out_psa > psa[0]) | 
| 673 |  |  | psa[0] = out_psa; | 
| 674 |  |  | break; | 
| 675 |  |  | case SDqueryMin+SDqueryMax: | 
| 676 |  |  | if (inc_psa > psa[0]) | 
| 677 |  |  | psa[1] = inc_psa; | 
| 678 |  |  | if (out_psa > psa[0]) | 
| 679 |  |  | psa[1] = out_psa; | 
| 680 |  |  | /* fall through */ | 
| 681 |  |  | case SDqueryMin: | 
| 682 |  |  | if ((inc_psa > .0) & (inc_psa < psa[0])) | 
| 683 | greg | 3.1 | psa[0] = inc_psa; | 
| 684 | greg | 3.5 | if ((out_psa > .0) & (out_psa < psa[0])) | 
| 685 |  |  | psa[0] = out_psa; | 
| 686 |  |  | break; | 
| 687 | greg | 3.1 | } | 
| 688 | greg | 3.5 | /* make sure it's legal */ | 
| 689 |  |  | return (psa[0] <= .0) ? SDEinternal : SDEnone; | 
| 690 | greg | 3.1 | } | 
| 691 |  |  |  | 
| 692 |  |  | /* Compute new cumulative distribution from BSDF */ | 
| 693 |  |  | static int | 
| 694 |  |  | make_cdist(SDMatCDst *cd, const FVECT inVec, SDMat *dp, int rev) | 
| 695 |  |  | { | 
| 696 |  |  | const unsigned  maxval = ~0; | 
| 697 |  |  | double          *cmtab, scale; | 
| 698 |  |  | int             o; | 
| 699 |  |  |  | 
| 700 |  |  | cmtab = (double *)malloc((cd->calen+1)*sizeof(double)); | 
| 701 |  |  | if (cmtab == NULL) | 
| 702 |  |  | return 0; | 
| 703 |  |  | cmtab[0] = .0; | 
| 704 |  |  | for (o = 0; o < cd->calen; o++) { | 
| 705 |  |  | if (rev) | 
| 706 |  |  | cmtab[o+1] = mBSDF_value(dp, o, cd->indx) * | 
| 707 |  |  | (*dp->ib_ohm)(o, dp->ib_priv); | 
| 708 |  |  | else | 
| 709 |  |  | cmtab[o+1] = mBSDF_value(dp, cd->indx, o) * | 
| 710 |  |  | (*dp->ob_ohm)(o, dp->ob_priv); | 
| 711 |  |  | cmtab[o+1] += cmtab[o]; | 
| 712 |  |  | } | 
| 713 |  |  | cd->cTotal = cmtab[cd->calen]; | 
| 714 |  |  | scale = (double)maxval / cd->cTotal; | 
| 715 |  |  | cd->carr[0] = 0; | 
| 716 |  |  | for (o = 1; o < cd->calen; o++) | 
| 717 |  |  | cd->carr[o] = scale*cmtab[o] + .5; | 
| 718 |  |  | cd->carr[cd->calen] = maxval; | 
| 719 |  |  | free(cmtab); | 
| 720 |  |  | return 1; | 
| 721 |  |  | } | 
| 722 |  |  |  | 
| 723 |  |  | /* Get cumulative distribution for matrix BSDF */ | 
| 724 |  |  | static const SDCDst * | 
| 725 |  |  | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc) | 
| 726 |  |  | { | 
| 727 |  |  | SDMat           *dp = (SDMat *)sdc->dist; | 
| 728 |  |  | int             reverse; | 
| 729 |  |  | SDMatCDst       myCD; | 
| 730 |  |  | SDMatCDst       *cd, *cdlast; | 
| 731 | greg | 3.5 | /* check arguments */ | 
| 732 |  |  | if ((inVec == NULL) | (dp == NULL)) | 
| 733 | greg | 3.1 | return NULL; | 
| 734 |  |  | memset(&myCD, 0, sizeof(myCD)); | 
| 735 |  |  | myCD.indx = mBSDF_incndx(dp, inVec); | 
| 736 |  |  | if (myCD.indx >= 0) { | 
| 737 |  |  | myCD.ob_priv = dp->ob_priv; | 
| 738 |  |  | myCD.ob_vec = dp->ob_vec; | 
| 739 |  |  | myCD.calen = dp->nout; | 
| 740 |  |  | reverse = 0; | 
| 741 |  |  | } else {                        /* try reciprocity */ | 
| 742 |  |  | myCD.indx = mBSDF_outndx(dp, inVec); | 
| 743 |  |  | if (myCD.indx < 0) | 
| 744 |  |  | return NULL; | 
| 745 |  |  | myCD.ob_priv = dp->ib_priv; | 
| 746 |  |  | myCD.ob_vec = dp->ib_vec; | 
| 747 |  |  | myCD.calen = dp->ninc; | 
| 748 |  |  | reverse = 1; | 
| 749 |  |  | } | 
| 750 |  |  | cdlast = NULL;                  /* check for it in cache list */ | 
| 751 |  |  | for (cd = (SDMatCDst *)sdc->cdList; | 
| 752 |  |  | cd != NULL; cd = (SDMatCDst *)cd->next) { | 
| 753 |  |  | if (cd->indx == myCD.indx && (cd->calen == myCD.calen) & | 
| 754 |  |  | (cd->ob_priv == myCD.ob_priv) & | 
| 755 |  |  | (cd->ob_vec == myCD.ob_vec)) | 
| 756 |  |  | break; | 
| 757 |  |  | cdlast = cd; | 
| 758 |  |  | } | 
| 759 |  |  | if (cd == NULL) {               /* need to allocate new entry */ | 
| 760 |  |  | cd = (SDMatCDst *)malloc(sizeof(SDMatCDst) + | 
| 761 |  |  | myCD.calen*sizeof(myCD.carr[0])); | 
| 762 |  |  | if (cd == NULL) | 
| 763 |  |  | return NULL; | 
| 764 |  |  | *cd = myCD;             /* compute cumulative distribution */ | 
| 765 |  |  | if (!make_cdist(cd, inVec, dp, reverse)) { | 
| 766 |  |  | free(cd); | 
| 767 |  |  | return NULL; | 
| 768 |  |  | } | 
| 769 |  |  | cdlast = cd; | 
| 770 |  |  | } | 
| 771 |  |  | if (cdlast != NULL) {           /* move entry to head of cache list */ | 
| 772 |  |  | cdlast->next = cd->next; | 
| 773 |  |  | cd->next = sdc->cdList; | 
| 774 |  |  | sdc->cdList = (SDCDst *)cd; | 
| 775 |  |  | } | 
| 776 |  |  | return (SDCDst *)cd;            /* ready to go */ | 
| 777 |  |  | } | 
| 778 |  |  |  | 
| 779 |  |  | /* Sample cumulative distribution */ | 
| 780 |  |  | static SDError | 
| 781 |  |  | SDsampMtxCDist(FVECT outVec, double randX, const SDCDst *cdp) | 
| 782 |  |  | { | 
| 783 |  |  | const unsigned  maxval = ~0; | 
| 784 |  |  | const SDMatCDst *mcd = (const SDMatCDst *)cdp; | 
| 785 |  |  | const unsigned  target = randX*maxval; | 
| 786 |  |  | int             i, iupper, ilower; | 
| 787 | greg | 3.5 | /* check arguments */ | 
| 788 |  |  | if ((outVec == NULL) | (mcd == NULL)) | 
| 789 |  |  | return SDEargument; | 
| 790 | greg | 3.1 | /* binary search to find index */ | 
| 791 |  |  | ilower = 0; iupper = mcd->calen; | 
| 792 |  |  | while ((i = (iupper + ilower) >> 1) != ilower) | 
| 793 |  |  | if ((long)target >= (long)mcd->carr[i]) | 
| 794 |  |  | ilower = i; | 
| 795 |  |  | else | 
| 796 |  |  | iupper = i; | 
| 797 |  |  | /* localize random position */ | 
| 798 |  |  | randX = (randX*maxval - mcd->carr[ilower]) / | 
| 799 |  |  | (double)(mcd->carr[iupper] - mcd->carr[ilower]); | 
| 800 |  |  | /* convert index to vector */ | 
| 801 | greg | 3.9 | if ((*mcd->ob_vec)(outVec, i+randX, mcd->ob_priv)) | 
| 802 | greg | 3.1 | return SDEnone; | 
| 803 |  |  | strcpy(SDerrorDetail, "BSDF sampling fault"); | 
| 804 |  |  | return SDEinternal; | 
| 805 |  |  | } | 
| 806 |  |  |  | 
| 807 |  |  | /* Fixed resolution BSDF methods */ | 
| 808 |  |  | SDFunc                  SDhandleMtx = { | 
| 809 |  |  | &SDgetMtxBSDF, | 
| 810 |  |  | &SDqueryMtxProjSA, | 
| 811 |  |  | &SDgetMtxCDist, | 
| 812 |  |  | &SDsampMtxCDist, | 
| 813 |  |  | &SDfreeMatrix, | 
| 814 |  |  | }; |