| 1 | /* RCSid $Id: bsdfrep.h,v 2.21 2014/08/21 13:44:05 greg Exp $ */ | 
| 2 | /* | 
| 3 | * Definitions for BSDF representation used to interpolate measured data. | 
| 4 | * | 
| 5 | *      G. Ward | 
| 6 | */ | 
| 7 |  | 
| 8 | #ifndef _BSDFREP_H_ | 
| 9 | #define _BSDFREP_H_ | 
| 10 |  | 
| 11 | #include "bsdf.h" | 
| 12 |  | 
| 13 | #ifdef __cplusplus | 
| 14 | extern "C" { | 
| 15 | #endif | 
| 16 |  | 
| 17 | #ifndef GRIDRES | 
| 18 | #define GRIDRES         (1<<8)          /* grid resolution per side */ | 
| 19 | #endif | 
| 20 | /* convert to/from coded radians */ | 
| 21 | #define ANG2R(r)        (int)((r)*((1<<16)/M_PI)) | 
| 22 | #define R2ANG(c)        (((c)+.5)*(M_PI/(1<<16))) | 
| 23 |  | 
| 24 | /* moderated cosine factor */ | 
| 25 | #define COSF(z)         (fabs(z)*0.98 + 0.02) | 
| 26 |  | 
| 27 | typedef union { | 
| 28 | struct { | 
| 29 | float           v;              /* DSF sum */ | 
| 30 | unsigned int    n;              /* number of values in sum */ | 
| 31 | }       sum;                    /* sum for averaging */ | 
| 32 | float   val[2];                 /* comparison values */ | 
| 33 | } GRIDVAL;                      /* grid value */ | 
| 34 |  | 
| 35 | typedef struct { | 
| 36 | float           peak;           /* lobe value at peak */ | 
| 37 | unsigned short  crad;           /* radius (coded angle) */ | 
| 38 | unsigned char   gx, gy;         /* grid position */ | 
| 39 | } RBFVAL;                       /* radial basis function value */ | 
| 40 |  | 
| 41 | struct s_rbfnode;               /* forward declaration of RBF struct */ | 
| 42 |  | 
| 43 | typedef struct s_migration { | 
| 44 | struct s_migration      *next;          /* next in global edge list */ | 
| 45 | struct s_rbfnode        *rbfv[2];       /* from,to vertex */ | 
| 46 | struct s_migration      *enxt[2];       /* next from,to sibling */ | 
| 47 | float                   mtx[1];         /* matrix (extends struct) */ | 
| 48 | } MIGRATION;                    /* migration link (winged edge structure) */ | 
| 49 |  | 
| 50 | typedef struct s_rbfnode { | 
| 51 | int                     ord;            /* ordinal position in list */ | 
| 52 | struct s_rbfnode        *next;          /* next in global RBF list */ | 
| 53 | MIGRATION               *ejl;           /* edge list for this vertex */ | 
| 54 | FVECT                   invec;          /* incident vector direction */ | 
| 55 | double                  vtotal;         /* volume for normalization */ | 
| 56 | int                     nrbf;           /* number of RBFs */ | 
| 57 | RBFVAL                  rbfa[1];        /* RBF array (extends struct) */ | 
| 58 | } RBFNODE;                      /* RBF representation of DSF @ 1 incidence */ | 
| 59 |  | 
| 60 | /* symmetry operations */ | 
| 61 | #define MIRROR_X        1               /* mirror(ed) x-coordinate */ | 
| 62 | #define MIRROR_Y        2               /* mirror(ed) y-coordinate */ | 
| 63 |  | 
| 64 | /* represented incident quadrants */ | 
| 65 | #define INP_QUAD1       1               /* 0-90 degree quadrant */ | 
| 66 | #define INP_QUAD2       2               /* 90-180 degree quadrant */ | 
| 67 | #define INP_QUAD3       4               /* 180-270 degree quadrant */ | 
| 68 | #define INP_QUAD4       8               /* 270-360 degree quadrant */ | 
| 69 |  | 
| 70 | /* name and manufacturer if known */ | 
| 71 | extern char             bsdf_name[]; | 
| 72 | extern char             bsdf_manuf[]; | 
| 73 | /* active grid resolution */ | 
| 74 | extern int              grid_res; | 
| 75 | /* coverage/symmetry using INP_QUAD? flags */ | 
| 76 | extern int              inp_coverage; | 
| 77 |  | 
| 78 | /* all incident angles in-plane so far? */ | 
| 79 | extern int              single_plane_incident; | 
| 80 |  | 
| 81 | /* input/output orientations */ | 
| 82 | extern int              input_orient; | 
| 83 | extern int              output_orient; | 
| 84 |  | 
| 85 | /* log BSDF histogram */ | 
| 86 | #define HISTLEN         256 | 
| 87 | #define BSDF2BIG        (1./M_PI) | 
| 88 | #define BSDF2SML        1e-8 | 
| 89 | #define HISTLNR         17.2759509              /* log(BSDF2BIG/BSDF2SML) */ | 
| 90 | extern unsigned long    bsdf_hist[HISTLEN]; | 
| 91 | #define histndx(v)      (int)(log((v)*(1./BSDF2SML))*(HISTLEN/HISTLNR)) | 
| 92 | #define histval(i)      (exp(((i)+.5)*(HISTLNR/HISTLEN))*BSDF2SML) | 
| 93 |  | 
| 94 | /* BSDF value for boundary regions */ | 
| 95 | extern double           bsdf_min; | 
| 96 | extern double           bsdf_spec_peak; | 
| 97 | extern double           bsdf_spec_rad; | 
| 98 |  | 
| 99 | /* processed incident DSF measurements */ | 
| 100 | extern RBFNODE          *dsf_list; | 
| 101 |  | 
| 102 | /* RBF-linking matrices (edges) */ | 
| 103 | extern MIGRATION        *mig_list; | 
| 104 |  | 
| 105 | #define mtx_nrows(m)    (m)->rbfv[0]->nrbf | 
| 106 | #define mtx_ncols(m)    (m)->rbfv[1]->nrbf | 
| 107 | #define mtx_coef(m,i,j) (m)->mtx[(i)*mtx_ncols(m) + (j)] | 
| 108 | #define is_src(rbf,m)   ((rbf) == (m)->rbfv[0]) | 
| 109 | #define is_dest(rbf,m)  ((rbf) == (m)->rbfv[1]) | 
| 110 | #define nextedge(rbf,m) (m)->enxt[is_dest(rbf,m)] | 
| 111 | #define opp_rbf(rbf,m)  (m)->rbfv[is_src(rbf,m)] | 
| 112 |  | 
| 113 | #define round(v)        (int)((v) + .5 - ((v) < -.5)) | 
| 114 |  | 
| 115 | #define BSDFREP_FMT     "BSDF_RBFmesh" | 
| 116 |  | 
| 117 | /* global argv[0] */ | 
| 118 | extern char             *progname; | 
| 119 |  | 
| 120 | /* get theta value in degrees [0,180) range */ | 
| 121 | #define get_theta180(v) ((180./M_PI)*Acos((v)[2])) | 
| 122 | /* get phi value in degrees, [0,360) range */ | 
| 123 | #define get_phi360(v)   ((180./M_PI)*atan2((v)[1],(v)[0]) + 360.*((v)[1]<0)) | 
| 124 |  | 
| 125 | /* our loaded grid for this incident angle */ | 
| 126 | extern double           theta_in_deg, phi_in_deg; | 
| 127 | extern GRIDVAL          dsf_grid[GRIDRES][GRIDRES]; | 
| 128 |  | 
| 129 | /* Register new input direction */ | 
| 130 | extern int              new_input_direction(double new_theta, double new_phi); | 
| 131 |  | 
| 132 | #define new_input_vector(v)\ | 
| 133 | new_input_direction(get_theta180(v),get_phi360(v)) | 
| 134 |  | 
| 135 | /* Apply symmetry to the given vector based on distribution */ | 
| 136 | extern int              use_symmetry(FVECT vec); | 
| 137 |  | 
| 138 | /* Reverse symmetry based on what was done before */ | 
| 139 | extern void             rev_symmetry(FVECT vec, int sym); | 
| 140 |  | 
| 141 | /* Reverse symmetry for an RBF distribution */ | 
| 142 | extern void             rev_rbf_symmetry(RBFNODE *rbf, int sym); | 
| 143 |  | 
| 144 | /* Rotate RBF to correspond to given incident vector */ | 
| 145 | extern void             rotate_rbf(RBFNODE *rbf, const FVECT invec); | 
| 146 |  | 
| 147 | /* Compute volume associated with Gaussian lobe */ | 
| 148 | extern double           rbf_volume(const RBFVAL *rbfp); | 
| 149 |  | 
| 150 | /* Compute outgoing vector from grid position */ | 
| 151 | extern void             ovec_from_pos(FVECT vec, int xpos, int ypos); | 
| 152 |  | 
| 153 | /* Compute grid position from normalized input/output vector */ | 
| 154 | extern void             pos_from_vec(int pos[2], const FVECT vec); | 
| 155 |  | 
| 156 | /* Evaluate BSDF at the given normalized outgoing direction */ | 
| 157 | extern double           eval_rbfrep(const RBFNODE *rp, const FVECT outvec); | 
| 158 |  | 
| 159 | /* Insert a new directional scattering function in our global list */ | 
| 160 | extern int              insert_dsf(RBFNODE *newrbf); | 
| 161 |  | 
| 162 | /* Get the DSF indicated by its ordinal position */ | 
| 163 | extern RBFNODE *        get_dsf(int ord); | 
| 164 |  | 
| 165 | /* Get triangle surface orientation (unnormalized) */ | 
| 166 | extern void             tri_orient(FVECT vres, const FVECT v1, | 
| 167 | const FVECT v2, const FVECT v3); | 
| 168 |  | 
| 169 | /* Determine if vertex order is reversed (inward normal) */ | 
| 170 | extern int              is_rev_tri(const FVECT v1, | 
| 171 | const FVECT v2, const FVECT v3); | 
| 172 |  | 
| 173 | /* Find vertices completing triangles on either side of the given edge */ | 
| 174 | extern int              get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig); | 
| 175 |  | 
| 176 | /* Clear our BSDF representation and free memory */ | 
| 177 | extern void             clear_bsdf_rep(void); | 
| 178 |  | 
| 179 | /* Write our BSDF mesh interpolant out to the given binary stream */ | 
| 180 | extern void             save_bsdf_rep(FILE *ofp); | 
| 181 |  | 
| 182 | /* Read a BSDF mesh interpolant from the given binary stream */ | 
| 183 | extern int              load_bsdf_rep(FILE *ifp); | 
| 184 |  | 
| 185 | /* Start new DSF input grid */ | 
| 186 | extern void             new_bsdf_data(double new_theta, double new_phi); | 
| 187 |  | 
| 188 | /* Add BSDF data point */ | 
| 189 | extern void             add_bsdf_data(double theta_out, double phi_out, | 
| 190 | double val, int isDSF); | 
| 191 |  | 
| 192 | /* Count up filled nodes and build RBF representation from current grid */ | 
| 193 | extern RBFNODE *        make_rbfrep(void); | 
| 194 |  | 
| 195 | /* Build our triangle mesh from recorded RBFs */ | 
| 196 | extern void             build_mesh(void); | 
| 197 |  | 
| 198 | /* Find edge(s) for interpolating the given vector, applying symmetry */ | 
| 199 | extern int              get_interp(MIGRATION *miga[3], FVECT invec); | 
| 200 |  | 
| 201 | /* Return single-lobe specular RBF for the given incident direction */ | 
| 202 | extern RBFNODE *        def_rbf_spec(const FVECT invec); | 
| 203 |  | 
| 204 | /* Advect and allocate new RBF along edge (internal call) */ | 
| 205 | extern RBFNODE *        e_advect_rbf(const MIGRATION *mig, | 
| 206 | const FVECT invec, int lobe_lim); | 
| 207 |  | 
| 208 | /* Compute distance between two RBF lobes (internal call) */ | 
| 209 | extern double           lobe_distance(RBFVAL *rbf1, RBFVAL *rbf2); | 
| 210 |  | 
| 211 | /* Compute mass transport plan (internal call) */ | 
| 212 | extern void             plan_transport(MIGRATION *mig); | 
| 213 |  | 
| 214 | /* Partially advect between recorded incident angles and allocate new RBF */ | 
| 215 | extern RBFNODE *        advect_rbf(const FVECT invec, int lobe_lim); | 
| 216 |  | 
| 217 | #ifdef __cplusplus | 
| 218 | } | 
| 219 | #endif | 
| 220 | #endif  /* _BSDFREP_H_ */ |