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 |
< |
typedef struct { |
31 |
< |
int nrows, ncols; /* array size (matches migration) */ |
32 |
< |
float *price; /* migration prices */ |
33 |
< |
short *sord; /* sort for each row, low to high */ |
34 |
< |
} PRICEMAT; /* sorted pricing matrix */ |
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 |
> |
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 |
> |
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 |
> |
if (n < 2) /* should never happen! */ |
58 |
> |
return(sum); |
59 |
> |
return(sum/(double)n); |
60 |
> |
} |
61 |
|
|
62 |
< |
#define pricerow(p,i) ((p)->price + (i)*(p)->ncols) |
63 |
< |
#define psortrow(p,i) ((p)->sord + (i)*(p)->ncols) |
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 |
> |
const double rad_epsilon = 0.01; |
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) | (test_rad <= inside_rad)) |
80 |
> |
test_rad = .5*(inside_rad + outside_rad); |
81 |
> |
DSFtest = eval_DSFsurround(rbf, outvec, test_rad); |
82 |
> |
if (DSFtest > DSFtarget) { |
83 |
> |
inside_rad = test_rad; |
84 |
> |
DSFinside = DSFtest; |
85 |
> |
} else { |
86 |
> |
outside_rad = test_rad; |
87 |
> |
DSFoutside = DSFtest; |
88 |
> |
} |
89 |
> |
} while (outside_rad-inside_rad > rad_epsilon); |
90 |
|
|
91 |
+ |
return(.5*(inside_rad + outside_rad)); |
92 |
+ |
#undef interp_rad |
93 |
+ |
} |
94 |
+ |
|
95 |
+ |
static int |
96 |
+ |
dbl_cmp(const void *p1, const void *p2) |
97 |
+ |
{ |
98 |
+ |
double d1 = *(const double *)p1; |
99 |
+ |
double d2 = *(const double *)p2; |
100 |
+ |
|
101 |
+ |
if (d1 > d2) return(1); |
102 |
+ |
if (d1 < d2) return(-1); |
103 |
+ |
return(0); |
104 |
+ |
} |
105 |
+ |
|
106 |
+ |
/* Conservative estimate of average BSDF value from current DSF's */ |
107 |
+ |
static void |
108 |
+ |
comp_bsdf_spec(void) |
109 |
+ |
{ |
110 |
+ |
double vmod_sum = 0; |
111 |
+ |
double rad_sum = 0; |
112 |
+ |
int n = 0; |
113 |
+ |
double *cost_list = NULL; |
114 |
+ |
double max_cost = 1.; |
115 |
+ |
RBFNODE *rbf; |
116 |
+ |
FVECT sdv; |
117 |
+ |
/* sort by incident altitude */ |
118 |
+ |
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) |
119 |
+ |
n++; |
120 |
+ |
if (n >= 10) |
121 |
+ |
cost_list = (double *)malloc(sizeof(double)*n); |
122 |
+ |
if (cost_list == NULL) { |
123 |
+ |
bsdf_spec_val = 0; |
124 |
+ |
bsdf_spec_rad = 0; |
125 |
+ |
return; |
126 |
+ |
} |
127 |
+ |
n = 0; |
128 |
+ |
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) |
129 |
+ |
cost_list[n++] = rbf->invec[2]*input_orient; |
130 |
+ |
qsort(cost_list, n, sizeof(double), dbl_cmp); |
131 |
+ |
max_cost = cost_list[(n+3)/4]; /* accept 25% nearest grazing */ |
132 |
+ |
free(cost_list); |
133 |
+ |
n = 0; |
134 |
+ |
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) { |
135 |
+ |
double this_rad, cosfact, vest; |
136 |
+ |
if (rbf->invec[2]*input_orient > max_cost) |
137 |
+ |
continue; |
138 |
+ |
sdv[0] = -rbf->invec[0]; |
139 |
+ |
sdv[1] = -rbf->invec[1]; |
140 |
+ |
sdv[2] = rbf->invec[2]*(2*(input_orient==output_orient) - 1); |
141 |
+ |
cosfact = COSF(sdv[2]); |
142 |
+ |
this_rad = est_DSFrad(rbf, sdv); |
143 |
+ |
vest = eval_rbfrep(rbf, sdv) * cosfact * |
144 |
+ |
(2.*M_PI) * this_rad*this_rad; |
145 |
+ |
if (vest > rbf->vtotal) /* don't over-estimate energy */ |
146 |
+ |
vest = rbf->vtotal; |
147 |
+ |
vmod_sum += vest / cosfact; /* remove cosine factor */ |
148 |
+ |
rad_sum += this_rad; |
149 |
+ |
++n; |
150 |
+ |
} |
151 |
+ |
bsdf_spec_rad = rad_sum/(double)n; |
152 |
+ |
bsdf_spec_val = vmod_sum/(2.*M_PI*n*bsdf_spec_rad*bsdf_spec_rad); |
153 |
+ |
} |
154 |
+ |
|
155 |
|
/* Create a new migration holder (sharing memory for multiprocessing) */ |
156 |
|
static MIGRATION * |
157 |
|
new_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
159 |
|
size_t memlen = sizeof(MIGRATION) + |
160 |
|
sizeof(float)*(from_rbf->nrbf*to_rbf->nrbf - 1); |
161 |
|
MIGRATION *newmig; |
162 |
< |
#ifdef _WIN32 |
162 |
> |
#if defined(_WIN32) || defined(_WIN64) |
163 |
|
if (nprocs > 1) |
164 |
|
fprintf(stderr, "%s: warning - multiprocessing not supported\n", |
165 |
|
progname); |
190 |
|
return(mig_list = newmig); |
191 |
|
} |
192 |
|
|
193 |
< |
#ifdef _WIN32 |
193 |
> |
#if defined(_WIN32) || defined(_WIN64) |
194 |
|
#define await_children(n) (void)(n) |
195 |
|
#define run_subprocess() 0 |
196 |
|
#define end_subprocess() (void)0 |
239 |
|
if (pid < 0) { |
240 |
|
fprintf(stderr, "%s: cannot fork subprocess\n", |
241 |
|
progname); |
242 |
+ |
await_children(nchild); |
243 |
|
exit(1); |
244 |
|
} |
245 |
|
++nchild; /* subprocess started */ |
254 |
|
|
255 |
|
#endif /* ! _WIN32 */ |
256 |
|
|
257 |
< |
/* Comparison routine needed for sorting price row */ |
137 |
< |
static int |
138 |
< |
msrt_cmp(void *b, const void *p1, const void *p2) |
139 |
< |
{ |
140 |
< |
PRICEMAT *pm = (PRICEMAT *)b; |
141 |
< |
int ri = ((const short *)p1 - pm->sord) / pm->ncols; |
142 |
< |
float c1 = pricerow(pm,ri)[*(const short *)p1]; |
143 |
< |
float c2 = pricerow(pm,ri)[*(const short *)p2]; |
144 |
< |
|
145 |
< |
if (c1 > c2) return(1); |
146 |
< |
if (c1 < c2) return(-1); |
147 |
< |
return(0); |
148 |
< |
} |
149 |
< |
|
150 |
< |
/* Compute (and allocate) migration price matrix for optimization */ |
257 |
> |
/* Compute normalized distribution scattering functions for comparison */ |
258 |
|
static void |
259 |
< |
price_routes(PRICEMAT *pm, const RBFNODE *from_rbf, const RBFNODE *to_rbf) |
259 |
> |
compute_nDSFs(const RBFNODE *rbf0, const RBFNODE *rbf1) |
260 |
|
{ |
261 |
< |
FVECT *vto = (FVECT *)malloc(sizeof(FVECT) * to_rbf->nrbf); |
262 |
< |
int i, j; |
261 |
> |
const double nf0 = (GRIDRES*GRIDRES) / rbf0->vtotal; |
262 |
> |
const double nf1 = (GRIDRES*GRIDRES) / rbf1->vtotal; |
263 |
> |
int x, y; |
264 |
> |
FVECT dv; |
265 |
|
|
266 |
< |
pm->nrows = from_rbf->nrbf; |
267 |
< |
pm->ncols = to_rbf->nrbf; |
268 |
< |
pm->price = (float *)malloc(sizeof(float) * pm->nrows*pm->ncols); |
269 |
< |
pm->sord = (short *)malloc(sizeof(short) * pm->nrows*pm->ncols); |
270 |
< |
|
162 |
< |
if ((pm->price == NULL) | (pm->sord == NULL) | (vto == NULL)) { |
163 |
< |
fprintf(stderr, "%s: Out of memory in migration_costs()\n", |
164 |
< |
progname); |
165 |
< |
exit(1); |
166 |
< |
} |
167 |
< |
for (j = to_rbf->nrbf; j--; ) /* save repetitive ops. */ |
168 |
< |
ovec_from_pos(vto[j], to_rbf->rbfa[j].gx, to_rbf->rbfa[j].gy); |
169 |
< |
|
170 |
< |
for (i = from_rbf->nrbf; i--; ) { |
171 |
< |
const double from_ang = R2ANG(from_rbf->rbfa[i].crad); |
172 |
< |
FVECT vfrom; |
173 |
< |
ovec_from_pos(vfrom, from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy); |
174 |
< |
for (j = to_rbf->nrbf; j--; ) { |
175 |
< |
pricerow(pm,i)[j] = acos(DOT(vfrom, vto[j])) + |
176 |
< |
fabs(R2ANG(to_rbf->rbfa[j].crad) - from_ang); |
177 |
< |
psortrow(pm,i)[j] = j; |
266 |
> |
for (x = GRIDRES; x--; ) |
267 |
> |
for (y = GRIDRES; y--; ) { |
268 |
> |
ovec_from_pos(dv, x, y); /* cube root (brightness) */ |
269 |
> |
dsf_grid[x][y].val[0] = pow(nf0*eval_rbfrep(rbf0, dv), .3333); |
270 |
> |
dsf_grid[x][y].val[1] = pow(nf1*eval_rbfrep(rbf1, dv), .3333); |
271 |
|
} |
272 |
< |
qsort_r(psortrow(pm,i), pm->ncols, sizeof(short), pm, &msrt_cmp); |
180 |
< |
} |
181 |
< |
free(vto); |
182 |
< |
} |
272 |
> |
} |
273 |
|
|
274 |
< |
/* Free price matrix */ |
185 |
< |
static void |
186 |
< |
free_routes(PRICEMAT *pm) |
187 |
< |
{ |
188 |
< |
free(pm->price); pm->price = NULL; |
189 |
< |
free(pm->sord); pm->sord = NULL; |
190 |
< |
} |
191 |
< |
|
192 |
< |
/* Compute minimum (optimistic) cost for moving the given source material */ |
274 |
> |
/* Compute neighborhood distance-squared (dissimilarity) */ |
275 |
|
static double |
276 |
< |
min_cost(double amt2move, const double *avail, const PRICEMAT *pm, int s) |
276 |
> |
neighborhood_dist2(int x0, int y0, int x1, int y1) |
277 |
|
{ |
278 |
< |
double total_cost = 0; |
279 |
< |
int j; |
280 |
< |
|
281 |
< |
if (amt2move <= FTINY) /* pre-emptive check */ |
282 |
< |
return(0.); |
283 |
< |
/* move cheapest first */ |
284 |
< |
for (j = 0; j < pm->ncols && amt2move > FTINY; j++) { |
285 |
< |
int d = psortrow(pm,s)[j]; |
286 |
< |
double amt = (amt2move < avail[d]) ? amt2move : avail[d]; |
287 |
< |
|
206 |
< |
total_cost += amt * pricerow(pm,s)[d]; |
207 |
< |
amt2move -= amt; |
278 |
> |
int rad = GRIDRES>>5; |
279 |
> |
double sum2 = 0.; |
280 |
> |
double d; |
281 |
> |
int p[4]; |
282 |
> |
int i, j; |
283 |
> |
/* check radius */ |
284 |
> |
p[0] = x0; p[1] = y0; p[2] = x1; p[3] = y1; |
285 |
> |
for (i = 4; i--; ) { |
286 |
> |
if (p[i] < rad) rad = p[i]; |
287 |
> |
if (GRIDRES-1-p[i] < rad) rad = GRIDRES-1-p[i]; |
288 |
|
} |
289 |
< |
return(total_cost); |
289 |
> |
for (i = -rad; i <= rad; i++) |
290 |
> |
for (j = -rad; j <= rad; j++) { |
291 |
> |
d = dsf_grid[x0+i][y0+j].val[0] - |
292 |
> |
dsf_grid[x1+i][y1+j].val[1]; |
293 |
> |
sum2 += d*d; |
294 |
> |
} |
295 |
> |
return(sum2 / (4*rad*(rad+1) + 1)); |
296 |
|
} |
297 |
|
|
298 |
< |
/* Take a step in migration by choosing optimal bucket to transfer */ |
299 |
< |
static double |
300 |
< |
migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, const PRICEMAT *pm) |
298 |
> |
/* Compute distance between two RBF lobes */ |
299 |
> |
double |
300 |
> |
lobe_distance(RBFVAL *rbf1, RBFVAL *rbf2) |
301 |
|
{ |
302 |
< |
const double maxamt = .1; |
303 |
< |
const double minamt = maxamt*5e-6; |
304 |
< |
static double *src_cost = NULL; |
305 |
< |
static int n_alloc = 0; |
306 |
< |
struct { |
307 |
< |
int s, d; /* source and destination */ |
308 |
< |
double price; /* price estimate per amount moved */ |
309 |
< |
double amt; /* amount we can move */ |
310 |
< |
} cur, best; |
311 |
< |
int i; |
312 |
< |
|
313 |
< |
if (pm->nrows > n_alloc) { /* allocate cost array */ |
314 |
< |
if (n_alloc) |
229 |
< |
free(src_cost); |
230 |
< |
src_cost = (double *)malloc(sizeof(double)*pm->nrows); |
231 |
< |
if (src_cost == NULL) { |
232 |
< |
fprintf(stderr, "%s: Out of memory in migration_step()\n", |
233 |
< |
progname); |
234 |
< |
exit(1); |
235 |
< |
} |
236 |
< |
n_alloc = pm->nrows; |
237 |
< |
} |
238 |
< |
for (i = pm->nrows; i--; ) /* starting costs for diff. */ |
239 |
< |
src_cost[i] = min_cost(src_rem[i], dst_rem, pm, i); |
240 |
< |
|
241 |
< |
/* find best source & dest. */ |
242 |
< |
best.s = best.d = -1; best.price = FHUGE; best.amt = 0; |
243 |
< |
for (cur.s = pm->nrows; cur.s--; ) { |
244 |
< |
const float *price = pricerow(pm,cur.s); |
245 |
< |
double cost_others = 0; |
246 |
< |
if (src_rem[cur.s] <= minamt) |
247 |
< |
continue; |
248 |
< |
cur.d = -1; /* examine cheapest dest. */ |
249 |
< |
for (i = pm->ncols; i--; ) |
250 |
< |
if (dst_rem[i] > minamt && |
251 |
< |
(cur.d < 0 || price[i] < price[cur.d])) |
252 |
< |
cur.d = i; |
253 |
< |
if (cur.d < 0) |
254 |
< |
return(.0); |
255 |
< |
if ((cur.price = price[cur.d]) >= best.price) |
256 |
< |
continue; /* no point checking further */ |
257 |
< |
cur.amt = (src_rem[cur.s] < dst_rem[cur.d]) ? |
258 |
< |
src_rem[cur.s] : dst_rem[cur.d]; |
259 |
< |
if (cur.amt > maxamt) cur.amt = maxamt; |
260 |
< |
dst_rem[cur.d] -= cur.amt; /* add up differential costs */ |
261 |
< |
for (i = pm->nrows; i--; ) |
262 |
< |
if (i != cur.s) |
263 |
< |
cost_others += min_cost(src_rem[i], dst_rem, pm, i) |
264 |
< |
- src_cost[i]; |
265 |
< |
dst_rem[cur.d] += cur.amt; /* undo trial move */ |
266 |
< |
cur.price += cost_others/cur.amt; /* adjust effective price */ |
267 |
< |
if (cur.price < best.price) /* are we better than best? */ |
268 |
< |
best = cur; |
269 |
< |
} |
270 |
< |
if ((best.s < 0) | (best.d < 0)) |
271 |
< |
return(.0); |
272 |
< |
/* make the actual move */ |
273 |
< |
mtx_coef(mig,best.s,best.d) += best.amt; |
274 |
< |
src_rem[best.s] -= best.amt; |
275 |
< |
dst_rem[best.d] -= best.amt; |
276 |
< |
return(best.amt); |
302 |
> |
FVECT vfrom, vto; |
303 |
> |
double d, res; |
304 |
> |
/* quadratic cost function */ |
305 |
> |
ovec_from_pos(vfrom, rbf1->gx, rbf1->gy); |
306 |
> |
ovec_from_pos(vto, rbf2->gx, rbf2->gy); |
307 |
> |
d = Acos(DOT(vfrom, vto)); |
308 |
> |
res = d*d; |
309 |
> |
d = R2ANG(rbf2->crad) - R2ANG(rbf1->crad); |
310 |
> |
res += d*d; |
311 |
> |
/* neighborhood difference */ |
312 |
> |
res += NEIGH_FACT2 * neighborhood_dist2( rbf1->gx, rbf1->gy, |
313 |
> |
rbf2->gx, rbf2->gy ); |
314 |
> |
return(res); |
315 |
|
} |
316 |
|
|
279 |
– |
#ifdef DEBUG |
280 |
– |
static char * |
281 |
– |
thetaphi(const FVECT v) |
282 |
– |
{ |
283 |
– |
static char buf[128]; |
284 |
– |
double theta, phi; |
317 |
|
|
286 |
– |
theta = 180./M_PI*acos(v[2]); |
287 |
– |
phi = 180./M_PI*atan2(v[1],v[0]); |
288 |
– |
sprintf(buf, "(%.0f,%.0f)", theta, phi); |
289 |
– |
|
290 |
– |
return(buf); |
291 |
– |
} |
292 |
– |
#endif |
293 |
– |
|
318 |
|
/* Compute and insert migration along directed edge (may fork child) */ |
319 |
|
static MIGRATION * |
320 |
|
create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
321 |
|
{ |
298 |
– |
const double end_thresh = 5e-6; |
299 |
– |
PRICEMAT pmtx; |
322 |
|
MIGRATION *newmig; |
323 |
< |
double *src_rem, *dst_rem; |
302 |
< |
double total_rem = 1., move_amt; |
303 |
< |
int i; |
323 |
> |
int i, j; |
324 |
|
/* check if exists already */ |
325 |
|
for (newmig = from_rbf->ejl; newmig != NULL; |
326 |
|
newmig = nextedge(from_rbf,newmig)) |
327 |
|
if (newmig->rbfv[1] == to_rbf) |
328 |
|
return(NULL); |
329 |
|
/* else allocate */ |
310 |
– |
newmig = new_migration(from_rbf, to_rbf); |
311 |
– |
if (run_subprocess()) |
312 |
– |
return(newmig); /* child continues */ |
313 |
– |
price_routes(&pmtx, from_rbf, to_rbf); |
314 |
– |
src_rem = (double *)malloc(sizeof(double)*from_rbf->nrbf); |
315 |
– |
dst_rem = (double *)malloc(sizeof(double)*to_rbf->nrbf); |
316 |
– |
if ((src_rem == NULL) | (dst_rem == NULL)) { |
317 |
– |
fprintf(stderr, "%s: Out of memory in create_migration()\n", |
318 |
– |
progname); |
319 |
– |
exit(1); |
320 |
– |
} |
330 |
|
#ifdef DEBUG |
331 |
< |
fprintf(stderr, "Building path from (theta,phi) %s ", |
332 |
< |
thetaphi(from_rbf->invec)); |
333 |
< |
fprintf(stderr, "to %s with %d x %d matrix\n", |
334 |
< |
thetaphi(to_rbf->invec), |
331 |
> |
fprintf(stderr, "Building path from (theta,phi) (%.1f,%.1f) ", |
332 |
> |
get_theta180(from_rbf->invec), |
333 |
> |
get_phi360(from_rbf->invec)); |
334 |
> |
fprintf(stderr, "to (%.1f,%.1f) with %d x %d matrix\n", |
335 |
> |
get_theta180(to_rbf->invec), |
336 |
> |
get_phi360(to_rbf->invec), |
337 |
|
from_rbf->nrbf, to_rbf->nrbf); |
338 |
|
#endif |
339 |
< |
/* starting quantities */ |
340 |
< |
memset(newmig->mtx, 0, sizeof(float)*from_rbf->nrbf*to_rbf->nrbf); |
341 |
< |
for (i = from_rbf->nrbf; i--; ) |
342 |
< |
src_rem[i] = rbf_volume(&from_rbf->rbfa[i]) / from_rbf->vtotal; |
343 |
< |
for (i = to_rbf->nrbf; i--; ) |
344 |
< |
dst_rem[i] = rbf_volume(&to_rbf->rbfa[i]) / to_rbf->vtotal; |
345 |
< |
do { /* move a bit at a time */ |
346 |
< |
move_amt = migration_step(newmig, src_rem, dst_rem, &pmtx); |
336 |
< |
total_rem -= move_amt; |
337 |
< |
#ifdef DEBUG |
338 |
< |
if (!nchild) |
339 |
< |
fprintf(stderr, "\r%.9f remaining...", total_rem); |
340 |
< |
#endif |
341 |
< |
} while ((total_rem > end_thresh) & (move_amt > 0)); |
342 |
< |
#ifdef DEBUG |
343 |
< |
if (!nchild) fputs("done.\n", stderr); |
344 |
< |
else fprintf(stderr, "finished with %.9f remaining\n", total_rem); |
345 |
< |
#endif |
339 |
> |
newmig = new_migration(from_rbf, to_rbf); |
340 |
> |
if (run_subprocess()) |
341 |
> |
return(newmig); /* child continues */ |
342 |
> |
|
343 |
> |
/* compute transport plan */ |
344 |
> |
compute_nDSFs(from_rbf, to_rbf); |
345 |
> |
plan_transport(newmig); |
346 |
> |
|
347 |
|
for (i = from_rbf->nrbf; i--; ) { /* normalize final matrix */ |
348 |
< |
float nf = rbf_volume(&from_rbf->rbfa[i]); |
348 |
< |
int j; |
348 |
> |
double nf = rbf_volume(&from_rbf->rbfa[i]); |
349 |
|
if (nf <= FTINY) continue; |
350 |
|
nf = from_rbf->vtotal / nf; |
351 |
|
for (j = to_rbf->nrbf; j--; ) |
352 |
< |
mtx_coef(newmig,i,j) *= nf; |
352 |
> |
mtx_coef(newmig,i,j) *= nf; /* row now sums to 1.0 */ |
353 |
|
} |
354 |
|
end_subprocess(); /* exit here if subprocess */ |
355 |
– |
free_routes(&pmtx); /* free working arrays */ |
356 |
– |
free(src_rem); |
357 |
– |
free(dst_rem); |
355 |
|
return(newmig); |
356 |
|
} |
357 |
|
|
381 |
|
return(vother[im_rev] != NULL); |
382 |
|
} |
383 |
|
|
384 |
< |
/* Find context hull vertex to complete triangle (oriented call) */ |
384 |
> |
/* Find convex hull vertex to complete triangle (oriented call) */ |
385 |
|
static RBFNODE * |
386 |
|
find_chull_vert(const RBFNODE *rbf0, const RBFNODE *rbf1) |
387 |
|
{ |
402 |
|
if (DOT(vp, vmid) <= FTINY) |
403 |
|
continue; /* wrong orientation */ |
404 |
|
area2 = .25*DOT(vp,vp); |
405 |
< |
VSUB(vp, rbf->invec, rbf0->invec); |
405 |
> |
VSUB(vp, rbf->invec, vmid); |
406 |
|
dprod = -DOT(vp, vejn); |
407 |
|
VSUM(vp, vp, vejn, dprod); /* above guarantees non-zero */ |
408 |
|
dprod = DOT(vp, vmid) / VLEN(vp); |
441 |
|
ej1 = create_migration(tvert[0], edge->rbfv[1]); |
442 |
|
mesh_from_edge(ej0); |
443 |
|
mesh_from_edge(ej1); |
444 |
+ |
return; |
445 |
|
} |
446 |
< |
} else if (tvert[1] == NULL) { /* grow mesh on left */ |
446 |
> |
} |
447 |
> |
if (tvert[1] == NULL) { /* grow mesh on left */ |
448 |
|
tvert[1] = find_chull_vert(edge->rbfv[1], edge->rbfv[0]); |
449 |
|
if (tvert[1] != NULL) { |
450 |
|
if (tvert[1]->ord > edge->rbfv[0]->ord) |
460 |
|
} |
461 |
|
} |
462 |
|
} |
463 |
+ |
|
464 |
+ |
/* Add normal direction if missing */ |
465 |
+ |
static void |
466 |
+ |
check_normal_incidence(void) |
467 |
+ |
{ |
468 |
+ |
static FVECT norm_vec = {.0, .0, 1.}; |
469 |
+ |
const int saved_nprocs = nprocs; |
470 |
+ |
RBFNODE *near_rbf, *mir_rbf, *rbf; |
471 |
+ |
double bestd; |
472 |
+ |
int n; |
473 |
+ |
|
474 |
+ |
if (dsf_list == NULL) |
475 |
+ |
return; /* XXX should be error? */ |
476 |
+ |
near_rbf = dsf_list; |
477 |
+ |
bestd = input_orient*near_rbf->invec[2]; |
478 |
+ |
if (single_plane_incident) { /* ordered plane incidence? */ |
479 |
+ |
if (bestd >= 1.-2.*FTINY) |
480 |
+ |
return; /* already have normal */ |
481 |
+ |
} else { |
482 |
+ |
switch (inp_coverage) { |
483 |
+ |
case INP_QUAD1: |
484 |
+ |
case INP_QUAD2: |
485 |
+ |
case INP_QUAD3: |
486 |
+ |
case INP_QUAD4: |
487 |
+ |
break; /* quadrilateral symmetry? */ |
488 |
+ |
default: |
489 |
+ |
return; /* else we can interpolate */ |
490 |
+ |
} |
491 |
+ |
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) { |
492 |
+ |
const double d = input_orient*rbf->invec[2]; |
493 |
+ |
if (d >= 1.-2.*FTINY) |
494 |
+ |
return; /* seems we have normal */ |
495 |
+ |
if (d > bestd) { |
496 |
+ |
near_rbf = rbf; |
497 |
+ |
bestd = d; |
498 |
+ |
} |
499 |
+ |
} |
500 |
+ |
} |
501 |
+ |
if (mig_list != NULL) { /* need to be called first */ |
502 |
+ |
fprintf(stderr, "%s: Late call to check_normal_incidence()\n", |
503 |
+ |
progname); |
504 |
+ |
exit(1); |
505 |
+ |
} |
506 |
+ |
#ifdef DEBUG |
507 |
+ |
fprintf(stderr, "Interpolating normal incidence by mirroring (%.1f,%.1f)\n", |
508 |
+ |
get_theta180(near_rbf->invec), get_phi360(near_rbf->invec)); |
509 |
+ |
#endif |
510 |
+ |
/* mirror nearest incidence */ |
511 |
+ |
n = sizeof(RBFNODE) + sizeof(RBFVAL)*(near_rbf->nrbf-1); |
512 |
+ |
mir_rbf = (RBFNODE *)malloc(n); |
513 |
+ |
if (mir_rbf == NULL) |
514 |
+ |
goto memerr; |
515 |
+ |
memcpy(mir_rbf, near_rbf, n); |
516 |
+ |
mir_rbf->ord = near_rbf->ord - 1; /* not used, I think */ |
517 |
+ |
mir_rbf->next = NULL; |
518 |
+ |
mir_rbf->ejl = NULL; |
519 |
+ |
rev_rbf_symmetry(mir_rbf, MIRROR_X|MIRROR_Y); |
520 |
+ |
nprocs = 1; /* compute migration matrix */ |
521 |
+ |
if (create_migration(mir_rbf, near_rbf) == NULL) |
522 |
+ |
exit(1); /* XXX should never happen! */ |
523 |
+ |
norm_vec[2] = input_orient; /* interpolate normal dist. */ |
524 |
+ |
rbf = e_advect_rbf(mig_list, norm_vec, 0); |
525 |
+ |
nprocs = saved_nprocs; /* final clean-up */ |
526 |
+ |
free(mir_rbf); |
527 |
+ |
free(mig_list); |
528 |
+ |
mig_list = near_rbf->ejl = NULL; |
529 |
+ |
insert_dsf(rbf); /* insert interpolated normal */ |
530 |
+ |
return; |
531 |
+ |
memerr: |
532 |
+ |
fprintf(stderr, "%s: Out of memory in check_normal_incidence()\n", |
533 |
+ |
progname); |
534 |
+ |
exit(1); |
535 |
+ |
} |
536 |
|
|
537 |
|
/* Build our triangle mesh from recorded RBFs */ |
538 |
|
void |
541 |
|
double best2 = M_PI*M_PI; |
542 |
|
RBFNODE *shrt_edj[2]; |
543 |
|
RBFNODE *rbf0, *rbf1; |
544 |
+ |
/* average specular peak */ |
545 |
+ |
comp_bsdf_spec(); |
546 |
+ |
/* add normal if needed */ |
547 |
+ |
check_normal_incidence(); |
548 |
|
/* check if isotropic */ |
549 |
|
if (single_plane_incident) { |
550 |
|
for (rbf0 = dsf_list; rbf0 != NULL; rbf0 = rbf0->next) |