7 |
|
* G. Ward |
8 |
|
*/ |
9 |
|
|
10 |
< |
#ifndef _WIN32 |
10 |
> |
#if !defined(_WIN32) && !defined(_WIN64) |
11 |
|
#include <unistd.h> |
12 |
|
#include <sys/wait.h> |
13 |
|
#include <sys/mman.h> |
18 |
|
#include <string.h> |
19 |
|
#include <math.h> |
20 |
|
#include "bsdfrep.h" |
21 |
+ |
|
22 |
+ |
#ifndef NEIGH_FACT2 |
23 |
+ |
#define NEIGH_FACT2 0.1 /* empirical neighborhood distance weight */ |
24 |
+ |
#endif |
25 |
|
/* number of processes to run */ |
26 |
|
int nprocs = 1; |
27 |
|
/* number of children (-1 in child) */ |
28 |
|
static int nchild = 0; |
29 |
|
|
30 |
< |
/* Compute (and allocate) migration price matrix for optimization */ |
31 |
< |
static float * |
32 |
< |
price_routes(const RBFNODE *from_rbf, const RBFNODE *to_rbf) |
30 |
> |
/* Compute average DSF value at the given radius from central vector */ |
31 |
> |
static double |
32 |
> |
eval_DSFsurround(const RBFNODE *rbf, const FVECT outvec, const double rad) |
33 |
|
{ |
34 |
< |
float *pmtx = (float *)malloc(sizeof(float) * |
35 |
< |
from_rbf->nrbf * to_rbf->nrbf); |
36 |
< |
FVECT *vto = (FVECT *)malloc(sizeof(FVECT) * to_rbf->nrbf); |
37 |
< |
int i, j; |
38 |
< |
|
39 |
< |
if ((pmtx == NULL) | (vto == NULL)) { |
40 |
< |
fprintf(stderr, "%s: Out of memory in migration_costs()\n", |
41 |
< |
progname); |
42 |
< |
exit(1); |
34 |
> |
const int ninc = 12; |
35 |
> |
const double phinc = 2.*M_PI/ninc; |
36 |
> |
double sum = 0; |
37 |
> |
int n = 0; |
38 |
> |
FVECT tvec; |
39 |
> |
int i; |
40 |
> |
/* compute initial vector */ |
41 |
> |
if (output_orient*outvec[2] >= 1.-FTINY) { |
42 |
> |
tvec[0] = tvec[2] = 0; |
43 |
> |
tvec[1] = 1; |
44 |
> |
} else { |
45 |
> |
tvec[0] = tvec[1] = 0; |
46 |
> |
tvec[2] = 1; |
47 |
|
} |
48 |
< |
for (j = to_rbf->nrbf; j--; ) /* save repetitive ops. */ |
49 |
< |
ovec_from_pos(vto[j], to_rbf->rbfa[j].gx, to_rbf->rbfa[j].gy); |
50 |
< |
|
51 |
< |
for (i = from_rbf->nrbf; i--; ) { |
52 |
< |
const double from_ang = R2ANG(from_rbf->rbfa[i].crad); |
53 |
< |
FVECT vfrom; |
54 |
< |
ovec_from_pos(vfrom, from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy); |
55 |
< |
for (j = to_rbf->nrbf; j--; ) |
48 |
< |
pmtx[i*to_rbf->nrbf + j] = acos(DOT(vfrom, vto[j])) + |
49 |
< |
fabs(R2ANG(to_rbf->rbfa[j].crad) - from_ang); |
48 |
> |
geodesic(tvec, outvec, tvec, rad, GEOD_RAD); |
49 |
> |
/* average surrounding DSF */ |
50 |
> |
for (i = 0; i < ninc; i++) { |
51 |
> |
if (i) spinvector(tvec, tvec, outvec, phinc); |
52 |
> |
if (tvec[2] > 0 ^ output_orient > 0) |
53 |
> |
continue; |
54 |
> |
sum += eval_rbfrep(rbf, tvec) * COSF(tvec[2]); |
55 |
> |
++n; |
56 |
|
} |
57 |
< |
free(vto); |
58 |
< |
return(pmtx); |
57 |
> |
if (n < 2) /* should never happen! */ |
58 |
> |
return(sum); |
59 |
> |
return(sum/(double)n); |
60 |
|
} |
61 |
|
|
62 |
< |
/* Comparison routine needed for sorting price row */ |
63 |
< |
static const float *price_arr; |
64 |
< |
static int |
58 |
< |
msrt_cmp(const void *p1, const void *p2) |
62 |
> |
/* Estimate single-lobe radius for DSF at the given outgoing angle */ |
63 |
> |
static double |
64 |
> |
est_DSFrad(const RBFNODE *rbf, const FVECT outvec) |
65 |
|
{ |
66 |
< |
float c1 = price_arr[*(const int *)p1]; |
67 |
< |
float c2 = price_arr[*(const int *)p2]; |
68 |
< |
|
69 |
< |
if (c1 > c2) return(1); |
70 |
< |
if (c1 < c2) return(-1); |
71 |
< |
return(0); |
66 |
> |
const double rad_epsilon = 0.03; |
67 |
> |
const double DSFtarget = 0.60653066 * eval_rbfrep(rbf,outvec) * |
68 |
> |
COSF(outvec[2]); |
69 |
> |
double inside_rad = rad_epsilon; |
70 |
> |
double outside_rad = 0.5; |
71 |
> |
double DSFinside = eval_DSFsurround(rbf, outvec, inside_rad); |
72 |
> |
double DSFoutside = eval_DSFsurround(rbf, outvec, outside_rad); |
73 |
> |
#define interp_rad inside_rad + (outside_rad-inside_rad) * \ |
74 |
> |
(DSFtarget-DSFinside) / (DSFoutside-DSFinside) |
75 |
> |
/* Newton's method (sort of) */ |
76 |
> |
do { |
77 |
> |
double test_rad = interp_rad; |
78 |
> |
double DSFtest; |
79 |
> |
if (test_rad >= outside_rad) |
80 |
> |
return(test_rad); |
81 |
> |
if (test_rad <= inside_rad) |
82 |
> |
return(test_rad*(test_rad>0)); |
83 |
> |
DSFtest = eval_DSFsurround(rbf, outvec, test_rad); |
84 |
> |
if (DSFtest > DSFtarget) { |
85 |
> |
inside_rad = test_rad; |
86 |
> |
DSFinside = DSFtest; |
87 |
> |
} else { |
88 |
> |
outside_rad = test_rad; |
89 |
> |
DSFoutside = DSFtest; |
90 |
> |
} |
91 |
> |
if (DSFoutside >= DSFinside) |
92 |
> |
return(test_rad); |
93 |
> |
} while (outside_rad-inside_rad > rad_epsilon); |
94 |
> |
return(interp_rad); |
95 |
> |
#undef interp_rad |
96 |
|
} |
97 |
|
|
98 |
< |
/* Compute minimum (optimistic) cost for moving the given source material */ |
99 |
< |
static double |
70 |
< |
min_cost(double amt2move, const double *avail, const float *price, int n) |
98 |
> |
static int |
99 |
> |
dbl_cmp(const void *p1, const void *p2) |
100 |
|
{ |
101 |
< |
static int *price_sort = NULL; |
102 |
< |
static int n_alloc = 0; |
74 |
< |
double total_cost = 0; |
75 |
< |
int i; |
101 |
> |
double d1 = *(const double *)p1; |
102 |
> |
double d2 = *(const double *)p2; |
103 |
|
|
104 |
< |
if (amt2move <= FTINY) /* pre-emptive check */ |
105 |
< |
return(0.); |
106 |
< |
if (n > n_alloc) { /* (re)allocate sort array */ |
80 |
< |
if (n_alloc) free(price_sort); |
81 |
< |
price_sort = (int *)malloc(sizeof(int)*n); |
82 |
< |
if (price_sort == NULL) { |
83 |
< |
fprintf(stderr, "%s: Out of memory in min_cost()\n", |
84 |
< |
progname); |
85 |
< |
exit(1); |
86 |
< |
} |
87 |
< |
n_alloc = n; |
88 |
< |
} |
89 |
< |
for (i = n; i--; ) |
90 |
< |
price_sort[i] = i; |
91 |
< |
price_arr = price; |
92 |
< |
qsort(price_sort, n, sizeof(int), &msrt_cmp); |
93 |
< |
/* move cheapest first */ |
94 |
< |
for (i = 0; i < n && amt2move > FTINY; i++) { |
95 |
< |
int d = price_sort[i]; |
96 |
< |
double amt = (amt2move < avail[d]) ? amt2move : avail[d]; |
97 |
< |
|
98 |
< |
total_cost += amt * price[d]; |
99 |
< |
amt2move -= amt; |
100 |
< |
} |
101 |
< |
return(total_cost); |
104 |
> |
if (d1 > d2) return(1); |
105 |
> |
if (d1 < d2) return(-1); |
106 |
> |
return(0); |
107 |
|
} |
108 |
|
|
109 |
< |
/* Take a step in migration by choosing optimal bucket to transfer */ |
110 |
< |
static double |
111 |
< |
migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, const float *pmtx) |
109 |
> |
/* Conservative estimate of average BSDF value from current DSF's */ |
110 |
> |
static void |
111 |
> |
comp_bsdf_spec(void) |
112 |
|
{ |
113 |
< |
const double maxamt = .1; |
114 |
< |
const double minamt = maxamt*.0001; |
115 |
< |
static double *src_cost = NULL; |
116 |
< |
static int n_alloc = 0; |
117 |
< |
struct { |
118 |
< |
int s, d; /* source and destination */ |
119 |
< |
double price; /* price estimate per amount moved */ |
120 |
< |
double amt; /* amount we can move */ |
121 |
< |
} cur, best; |
122 |
< |
int i; |
123 |
< |
|
124 |
< |
if (mtx_nrows(mig) > n_alloc) { /* allocate cost array */ |
125 |
< |
if (n_alloc) |
126 |
< |
free(src_cost); |
127 |
< |
src_cost = (double *)malloc(sizeof(double)*mtx_nrows(mig)); |
128 |
< |
if (src_cost == NULL) { |
124 |
< |
fprintf(stderr, "%s: Out of memory in migration_step()\n", |
125 |
< |
progname); |
126 |
< |
exit(1); |
127 |
< |
} |
128 |
< |
n_alloc = mtx_nrows(mig); |
113 |
> |
double vmod_sum = 0; |
114 |
> |
double rad_sum = 0; |
115 |
> |
int n = 0; |
116 |
> |
double *cost_list = NULL; |
117 |
> |
double max_cost = 1.; |
118 |
> |
RBFNODE *rbf; |
119 |
> |
FVECT sdv; |
120 |
> |
/* sort by incident altitude */ |
121 |
> |
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) |
122 |
> |
n++; |
123 |
> |
if (n >= 10) |
124 |
> |
cost_list = (double *)malloc(sizeof(double)*n); |
125 |
> |
if (cost_list == NULL) { |
126 |
> |
bsdf_spec_val = 0; |
127 |
> |
bsdf_spec_rad = 0; |
128 |
> |
return; |
129 |
|
} |
130 |
< |
for (i = mtx_nrows(mig); i--; ) /* starting costs for diff. */ |
131 |
< |
src_cost[i] = min_cost(src_rem[i], dst_rem, |
132 |
< |
pmtx+i*mtx_ncols(mig), mtx_ncols(mig)); |
133 |
< |
|
134 |
< |
/* find best source & dest. */ |
135 |
< |
best.s = best.d = -1; best.price = FHUGE; best.amt = 0; |
136 |
< |
for (cur.s = mtx_nrows(mig); cur.s--; ) { |
137 |
< |
const float *price = pmtx + cur.s*mtx_ncols(mig); |
138 |
< |
double cost_others = 0; |
139 |
< |
if (src_rem[cur.s] < minamt) |
140 |
< |
continue; |
141 |
< |
cur.d = -1; /* examine cheapest dest. */ |
142 |
< |
for (i = mtx_ncols(mig); i--; ) |
143 |
< |
if (dst_rem[i] > minamt && |
144 |
< |
(cur.d < 0 || price[i] < price[cur.d])) |
145 |
< |
cur.d = i; |
146 |
< |
if (cur.d < 0) |
147 |
< |
return(.0); |
148 |
< |
if ((cur.price = price[cur.d]) >= best.price) |
149 |
< |
continue; /* no point checking further */ |
150 |
< |
cur.amt = (src_rem[cur.s] < dst_rem[cur.d]) ? |
151 |
< |
src_rem[cur.s] : dst_rem[cur.d]; |
152 |
< |
if (cur.amt > maxamt) cur.amt = maxamt; |
153 |
< |
dst_rem[cur.d] -= cur.amt; /* add up differential costs */ |
154 |
< |
for (i = mtx_nrows(mig); i--; ) |
155 |
< |
if (i != cur.s) |
156 |
< |
cost_others += min_cost(src_rem[i], dst_rem, |
157 |
< |
price, mtx_ncols(mig)) |
158 |
< |
- src_cost[i]; |
159 |
< |
dst_rem[cur.d] += cur.amt; /* undo trial move */ |
160 |
< |
cur.price += cost_others/cur.amt; /* adjust effective price */ |
161 |
< |
if (cur.price < best.price) /* are we better than best? */ |
162 |
< |
best = cur; |
130 |
> |
n = 0; |
131 |
> |
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) |
132 |
> |
cost_list[n++] = rbf->invec[2]*input_orient; |
133 |
> |
qsort(cost_list, n, sizeof(double), dbl_cmp); |
134 |
> |
max_cost = cost_list[(n+3)/4]; /* accept 25% nearest grazing */ |
135 |
> |
free(cost_list); |
136 |
> |
n = 0; |
137 |
> |
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) { |
138 |
> |
double this_rad, cosfact, vest; |
139 |
> |
if (rbf->invec[2]*input_orient > max_cost) |
140 |
> |
continue; |
141 |
> |
sdv[0] = -rbf->invec[0]; |
142 |
> |
sdv[1] = -rbf->invec[1]; |
143 |
> |
sdv[2] = rbf->invec[2]*(2*(input_orient==output_orient) - 1); |
144 |
> |
cosfact = COSF(sdv[2]); |
145 |
> |
this_rad = est_DSFrad(rbf, sdv); |
146 |
> |
vest = eval_rbfrep(rbf, sdv) * cosfact * |
147 |
> |
(2.*M_PI) * this_rad*this_rad; |
148 |
> |
if (vest > rbf->vtotal) /* don't over-estimate energy */ |
149 |
> |
vest = rbf->vtotal; |
150 |
> |
vmod_sum += vest / cosfact; /* remove cosine factor */ |
151 |
> |
rad_sum += this_rad; |
152 |
> |
++n; |
153 |
|
} |
154 |
< |
if ((best.s < 0) | (best.d < 0)) |
155 |
< |
return(.0); |
166 |
< |
/* make the actual move */ |
167 |
< |
mig->mtx[mtx_ndx(mig,best.s,best.d)] += best.amt; |
168 |
< |
src_rem[best.s] -= best.amt; |
169 |
< |
dst_rem[best.d] -= best.amt; |
170 |
< |
return(best.amt); |
154 |
> |
bsdf_spec_rad = rad_sum/(double)n; |
155 |
> |
bsdf_spec_val = vmod_sum/(2.*M_PI*n*bsdf_spec_rad*bsdf_spec_rad); |
156 |
|
} |
157 |
|
|
173 |
– |
#ifdef DEBUG |
174 |
– |
static char * |
175 |
– |
thetaphi(const FVECT v) |
176 |
– |
{ |
177 |
– |
static char buf[128]; |
178 |
– |
double theta, phi; |
179 |
– |
|
180 |
– |
theta = 180./M_PI*acos(v[2]); |
181 |
– |
phi = 180./M_PI*atan2(v[1],v[0]); |
182 |
– |
sprintf(buf, "(%.0f,%.0f)", theta, phi); |
183 |
– |
|
184 |
– |
return(buf); |
185 |
– |
} |
186 |
– |
#endif |
187 |
– |
|
158 |
|
/* Create a new migration holder (sharing memory for multiprocessing) */ |
159 |
|
static MIGRATION * |
160 |
|
new_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
162 |
|
size_t memlen = sizeof(MIGRATION) + |
163 |
|
sizeof(float)*(from_rbf->nrbf*to_rbf->nrbf - 1); |
164 |
|
MIGRATION *newmig; |
165 |
< |
#ifdef _WIN32 |
165 |
> |
#if defined(_WIN32) || defined(_WIN64) |
166 |
|
if (nprocs > 1) |
167 |
|
fprintf(stderr, "%s: warning - multiprocessing not supported\n", |
168 |
|
progname); |
193 |
|
return(mig_list = newmig); |
194 |
|
} |
195 |
|
|
196 |
< |
#ifdef _WIN32 |
196 |
> |
#if defined(_WIN32) || defined(_WIN64) |
197 |
|
#define await_children(n) (void)(n) |
198 |
|
#define run_subprocess() 0 |
199 |
|
#define end_subprocess() (void)0 |
242 |
|
if (pid < 0) { |
243 |
|
fprintf(stderr, "%s: cannot fork subprocess\n", |
244 |
|
progname); |
245 |
+ |
await_children(nchild); |
246 |
|
exit(1); |
247 |
|
} |
248 |
|
++nchild; /* subprocess started */ |
257 |
|
|
258 |
|
#endif /* ! _WIN32 */ |
259 |
|
|
260 |
+ |
/* Compute normalized distribution scattering functions for comparison */ |
261 |
+ |
static void |
262 |
+ |
compute_nDSFs(const RBFNODE *rbf0, const RBFNODE *rbf1) |
263 |
+ |
{ |
264 |
+ |
const double nf0 = (GRIDRES*GRIDRES) / rbf0->vtotal; |
265 |
+ |
const double nf1 = (GRIDRES*GRIDRES) / rbf1->vtotal; |
266 |
+ |
int x, y; |
267 |
+ |
FVECT dv; |
268 |
+ |
|
269 |
+ |
for (x = GRIDRES; x--; ) |
270 |
+ |
for (y = GRIDRES; y--; ) { |
271 |
+ |
ovec_from_pos(dv, x, y); /* cube root (brightness) */ |
272 |
+ |
dsf_grid[x][y].val[0] = pow(nf0*eval_rbfrep(rbf0, dv), .3333); |
273 |
+ |
dsf_grid[x][y].val[1] = pow(nf1*eval_rbfrep(rbf1, dv), .3333); |
274 |
+ |
} |
275 |
+ |
} |
276 |
+ |
|
277 |
+ |
/* Compute neighborhood distance-squared (dissimilarity) */ |
278 |
+ |
static double |
279 |
+ |
neighborhood_dist2(int x0, int y0, int x1, int y1) |
280 |
+ |
{ |
281 |
+ |
int rad = GRIDRES>>5; |
282 |
+ |
double sum2 = 0.; |
283 |
+ |
double d; |
284 |
+ |
int p[4]; |
285 |
+ |
int i, j; |
286 |
+ |
/* check radius */ |
287 |
+ |
p[0] = x0; p[1] = y0; p[2] = x1; p[3] = y1; |
288 |
+ |
for (i = 4; i--; ) { |
289 |
+ |
if (p[i] < rad) rad = p[i]; |
290 |
+ |
if (GRIDRES-1-p[i] < rad) rad = GRIDRES-1-p[i]; |
291 |
+ |
} |
292 |
+ |
for (i = -rad; i <= rad; i++) |
293 |
+ |
for (j = -rad; j <= rad; j++) { |
294 |
+ |
d = dsf_grid[x0+i][y0+j].val[0] - |
295 |
+ |
dsf_grid[x1+i][y1+j].val[1]; |
296 |
+ |
sum2 += d*d; |
297 |
+ |
} |
298 |
+ |
return(sum2 / (4*rad*(rad+1) + 1)); |
299 |
+ |
} |
300 |
+ |
|
301 |
+ |
/* Compute distance between two RBF lobes */ |
302 |
+ |
double |
303 |
+ |
lobe_distance(RBFVAL *rbf1, RBFVAL *rbf2) |
304 |
+ |
{ |
305 |
+ |
FVECT vfrom, vto; |
306 |
+ |
double d, res; |
307 |
+ |
/* quadratic cost function */ |
308 |
+ |
ovec_from_pos(vfrom, rbf1->gx, rbf1->gy); |
309 |
+ |
ovec_from_pos(vto, rbf2->gx, rbf2->gy); |
310 |
+ |
d = Acos(DOT(vfrom, vto)); |
311 |
+ |
res = d*d; |
312 |
+ |
d = R2ANG(rbf2->crad) - R2ANG(rbf1->crad); |
313 |
+ |
res += d*d; |
314 |
+ |
/* neighborhood difference */ |
315 |
+ |
res += NEIGH_FACT2 * neighborhood_dist2( rbf1->gx, rbf1->gy, |
316 |
+ |
rbf2->gx, rbf2->gy ); |
317 |
+ |
return(res); |
318 |
+ |
} |
319 |
+ |
|
320 |
+ |
|
321 |
|
/* Compute and insert migration along directed edge (may fork child) */ |
322 |
|
static MIGRATION * |
323 |
|
create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
324 |
|
{ |
293 |
– |
const double end_thresh = 0.1/(from_rbf->nrbf*to_rbf->nrbf); |
294 |
– |
const double check_thresh = 0.01; |
295 |
– |
const double rel_thresh = 5e-6; |
296 |
– |
float *pmtx; |
325 |
|
MIGRATION *newmig; |
326 |
< |
double *src_rem, *dst_rem; |
299 |
< |
double total_rem = 1., move_amt; |
300 |
< |
int i; |
326 |
> |
int i, j; |
327 |
|
/* check if exists already */ |
328 |
|
for (newmig = from_rbf->ejl; newmig != NULL; |
329 |
|
newmig = nextedge(from_rbf,newmig)) |
330 |
|
if (newmig->rbfv[1] == to_rbf) |
331 |
|
return(NULL); |
332 |
|
/* else allocate */ |
333 |
+ |
#ifdef DEBUG |
334 |
+ |
fprintf(stderr, "Building path from (theta,phi) (%.1f,%.1f) ", |
335 |
+ |
get_theta180(from_rbf->invec), |
336 |
+ |
get_phi360(from_rbf->invec)); |
337 |
+ |
fprintf(stderr, "to (%.1f,%.1f) with %d x %d matrix\n", |
338 |
+ |
get_theta180(to_rbf->invec), |
339 |
+ |
get_phi360(to_rbf->invec), |
340 |
+ |
from_rbf->nrbf, to_rbf->nrbf); |
341 |
+ |
#endif |
342 |
|
newmig = new_migration(from_rbf, to_rbf); |
343 |
|
if (run_subprocess()) |
344 |
|
return(newmig); /* child continues */ |
345 |
< |
pmtx = price_routes(from_rbf, to_rbf); |
346 |
< |
src_rem = (double *)malloc(sizeof(double)*from_rbf->nrbf); |
347 |
< |
dst_rem = (double *)malloc(sizeof(double)*to_rbf->nrbf); |
348 |
< |
if ((src_rem == NULL) | (dst_rem == NULL)) { |
349 |
< |
fprintf(stderr, "%s: Out of memory in create_migration()\n", |
315 |
< |
progname); |
316 |
< |
exit(1); |
317 |
< |
} |
318 |
< |
#ifdef DEBUG |
319 |
< |
fprintf(stderr, "Building path from (theta,phi) %s ", |
320 |
< |
thetaphi(from_rbf->invec)); |
321 |
< |
fprintf(stderr, "to %s", thetaphi(to_rbf->invec)); |
322 |
< |
/* if (nchild) */ fputc('\n', stderr); |
323 |
< |
#endif |
324 |
< |
/* starting quantities */ |
325 |
< |
memset(newmig->mtx, 0, sizeof(float)*from_rbf->nrbf*to_rbf->nrbf); |
326 |
< |
for (i = from_rbf->nrbf; i--; ) |
327 |
< |
src_rem[i] = rbf_volume(&from_rbf->rbfa[i]) / from_rbf->vtotal; |
328 |
< |
for (i = to_rbf->nrbf; i--; ) |
329 |
< |
dst_rem[i] = rbf_volume(&to_rbf->rbfa[i]) / to_rbf->vtotal; |
330 |
< |
do { /* move a bit at a time */ |
331 |
< |
move_amt = migration_step(newmig, src_rem, dst_rem, pmtx); |
332 |
< |
total_rem -= move_amt; |
333 |
< |
#ifdef DEBUG |
334 |
< |
if (!nchild) |
335 |
< |
/* fputc('.', stderr); */ |
336 |
< |
fprintf(stderr, "%.9f remaining...\r", total_rem); |
337 |
< |
#endif |
338 |
< |
} while (total_rem > end_thresh && (total_rem > check_thresh) | |
339 |
< |
(move_amt > rel_thresh*total_rem)); |
340 |
< |
#ifdef DEBUG |
341 |
< |
if (!nchild) fputs("\ndone.\n", stderr); |
342 |
< |
else fprintf(stderr, "finished with %.9f remaining\n", total_rem); |
343 |
< |
#endif |
345 |
> |
|
346 |
> |
/* compute transport plan */ |
347 |
> |
compute_nDSFs(from_rbf, to_rbf); |
348 |
> |
plan_transport(newmig); |
349 |
> |
|
350 |
|
for (i = from_rbf->nrbf; i--; ) { /* normalize final matrix */ |
351 |
< |
float nf = rbf_volume(&from_rbf->rbfa[i]); |
346 |
< |
int j; |
351 |
> |
double nf = rbf_volume(&from_rbf->rbfa[i]); |
352 |
|
if (nf <= FTINY) continue; |
353 |
|
nf = from_rbf->vtotal / nf; |
354 |
|
for (j = to_rbf->nrbf; j--; ) |
355 |
< |
newmig->mtx[mtx_ndx(newmig,i,j)] *= nf; |
355 |
> |
mtx_coef(newmig,i,j) *= nf; /* row now sums to 1.0 */ |
356 |
|
} |
357 |
|
end_subprocess(); /* exit here if subprocess */ |
353 |
– |
free(pmtx); /* free working arrays */ |
354 |
– |
free(src_rem); |
355 |
– |
free(dst_rem); |
358 |
|
return(newmig); |
359 |
|
} |
360 |
|
|
384 |
|
return(vother[im_rev] != NULL); |
385 |
|
} |
386 |
|
|
387 |
< |
/* Find context hull vertex to complete triangle (oriented call) */ |
387 |
> |
/* Find convex hull vertex to complete triangle (oriented call) */ |
388 |
|
static RBFNODE * |
389 |
|
find_chull_vert(const RBFNODE *rbf0, const RBFNODE *rbf1) |
390 |
|
{ |
405 |
|
if (DOT(vp, vmid) <= FTINY) |
406 |
|
continue; /* wrong orientation */ |
407 |
|
area2 = .25*DOT(vp,vp); |
408 |
< |
VSUB(vp, rbf->invec, rbf0->invec); |
408 |
> |
VSUB(vp, rbf->invec, vmid); |
409 |
|
dprod = -DOT(vp, vejn); |
410 |
|
VSUM(vp, vp, vejn, dprod); /* above guarantees non-zero */ |
411 |
|
dprod = DOT(vp, vmid) / VLEN(vp); |
444 |
|
ej1 = create_migration(tvert[0], edge->rbfv[1]); |
445 |
|
mesh_from_edge(ej0); |
446 |
|
mesh_from_edge(ej1); |
447 |
+ |
return; |
448 |
|
} |
449 |
< |
} else if (tvert[1] == NULL) { /* grow mesh on left */ |
449 |
> |
} |
450 |
> |
if (tvert[1] == NULL) { /* grow mesh on left */ |
451 |
|
tvert[1] = find_chull_vert(edge->rbfv[1], edge->rbfv[0]); |
452 |
|
if (tvert[1] != NULL) { |
453 |
|
if (tvert[1]->ord > edge->rbfv[0]->ord) |
463 |
|
} |
464 |
|
} |
465 |
|
} |
466 |
+ |
|
467 |
+ |
/* Add normal direction if missing */ |
468 |
+ |
static void |
469 |
+ |
check_normal_incidence(void) |
470 |
+ |
{ |
471 |
+ |
static FVECT norm_vec = {.0, .0, 1.}; |
472 |
+ |
const int saved_nprocs = nprocs; |
473 |
+ |
RBFNODE *near_rbf, *mir_rbf, *rbf; |
474 |
+ |
double bestd; |
475 |
+ |
int n; |
476 |
+ |
|
477 |
+ |
if (dsf_list == NULL) |
478 |
+ |
return; /* XXX should be error? */ |
479 |
+ |
near_rbf = dsf_list; |
480 |
+ |
bestd = input_orient*near_rbf->invec[2]; |
481 |
+ |
if (single_plane_incident) { /* ordered plane incidence? */ |
482 |
+ |
if (bestd >= 1.-2.*FTINY) |
483 |
+ |
return; /* already have normal */ |
484 |
+ |
} else { |
485 |
+ |
switch (inp_coverage) { |
486 |
+ |
case INP_QUAD1: |
487 |
+ |
case INP_QUAD2: |
488 |
+ |
case INP_QUAD3: |
489 |
+ |
case INP_QUAD4: |
490 |
+ |
break; /* quadrilateral symmetry? */ |
491 |
+ |
default: |
492 |
+ |
return; /* else we can interpolate */ |
493 |
+ |
} |
494 |
+ |
for (rbf = near_rbf->next; rbf != NULL; rbf = rbf->next) { |
495 |
+ |
const double d = input_orient*rbf->invec[2]; |
496 |
+ |
if (d >= 1.-2.*FTINY) |
497 |
+ |
return; /* seems we have normal */ |
498 |
+ |
if (d > bestd) { |
499 |
+ |
near_rbf = rbf; |
500 |
+ |
bestd = d; |
501 |
+ |
} |
502 |
+ |
} |
503 |
+ |
} |
504 |
+ |
if (mig_list != NULL) { /* need to be called first */ |
505 |
+ |
fprintf(stderr, "%s: Late call to check_normal_incidence()\n", |
506 |
+ |
progname); |
507 |
+ |
exit(1); |
508 |
+ |
} |
509 |
+ |
#ifdef DEBUG |
510 |
+ |
fprintf(stderr, "Interpolating normal incidence by mirroring (%.1f,%.1f)\n", |
511 |
+ |
get_theta180(near_rbf->invec), get_phi360(near_rbf->invec)); |
512 |
+ |
#endif |
513 |
+ |
/* mirror nearest incidence */ |
514 |
+ |
n = sizeof(RBFNODE) + sizeof(RBFVAL)*(near_rbf->nrbf-1); |
515 |
+ |
mir_rbf = (RBFNODE *)malloc(n); |
516 |
+ |
if (mir_rbf == NULL) |
517 |
+ |
goto memerr; |
518 |
+ |
memcpy(mir_rbf, near_rbf, n); |
519 |
+ |
mir_rbf->ord = near_rbf->ord - 1; /* not used, I think */ |
520 |
+ |
mir_rbf->next = NULL; |
521 |
+ |
mir_rbf->ejl = NULL; |
522 |
+ |
rev_rbf_symmetry(mir_rbf, MIRROR_X|MIRROR_Y); |
523 |
+ |
nprocs = 1; /* compute migration matrix */ |
524 |
+ |
if (create_migration(mir_rbf, near_rbf) == NULL) |
525 |
+ |
exit(1); /* XXX should never happen! */ |
526 |
+ |
norm_vec[2] = input_orient; /* interpolate normal dist. */ |
527 |
+ |
rbf = e_advect_rbf(mig_list, norm_vec, 0); |
528 |
+ |
nprocs = saved_nprocs; /* final clean-up */ |
529 |
+ |
free(mir_rbf); |
530 |
+ |
free(mig_list); |
531 |
+ |
mig_list = near_rbf->ejl = NULL; |
532 |
+ |
insert_dsf(rbf); /* insert interpolated normal */ |
533 |
+ |
return; |
534 |
+ |
memerr: |
535 |
+ |
fprintf(stderr, "%s: Out of memory in check_normal_incidence()\n", |
536 |
+ |
progname); |
537 |
+ |
exit(1); |
538 |
+ |
} |
539 |
|
|
540 |
|
/* Build our triangle mesh from recorded RBFs */ |
541 |
|
void |
544 |
|
double best2 = M_PI*M_PI; |
545 |
|
RBFNODE *shrt_edj[2]; |
546 |
|
RBFNODE *rbf0, *rbf1; |
547 |
+ |
/* average specular peak */ |
548 |
+ |
comp_bsdf_spec(); |
549 |
+ |
/* add normal if needed */ |
550 |
+ |
check_normal_incidence(); |
551 |
|
/* check if isotropic */ |
552 |
|
if (single_plane_incident) { |
553 |
|
for (rbf0 = dsf_list; rbf0 != NULL; rbf0 = rbf0->next) |