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