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