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