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> |
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 |
< |
float *prow; /* current price row */ |
35 |
< |
} 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 |
295 |
|
return(sum2 / (4*rad*(rad+1) + 1)); |
296 |
|
} |
297 |
|
|
298 |
< |
/* Comparison routine needed for sorting price row */ |
299 |
< |
static int |
300 |
< |
msrt_cmp(void *b, const void *p1, const void *p2) |
298 |
> |
/* Compute distance between two RBF lobes */ |
299 |
> |
double |
300 |
> |
lobe_distance(RBFVAL *rbf1, RBFVAL *rbf2) |
301 |
|
{ |
302 |
< |
PRICEMAT *pm = (PRICEMAT *)b; |
303 |
< |
float c1 = pm->prow[*(const short *)p1]; |
304 |
< |
float c2 = pm->prow[*(const short *)p2]; |
305 |
< |
|
306 |
< |
if (c1 > c2) return(1); |
307 |
< |
if (c1 < c2) return(-1); |
308 |
< |
return(0); |
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 |
|
|
196 |
– |
/* Compute (and allocate) migration price matrix for optimization */ |
197 |
– |
static void |
198 |
– |
price_routes(PRICEMAT *pm, const RBFNODE *from_rbf, const RBFNODE *to_rbf) |
199 |
– |
{ |
200 |
– |
FVECT *vto = (FVECT *)malloc(sizeof(FVECT) * to_rbf->nrbf); |
201 |
– |
int i, j; |
317 |
|
|
203 |
– |
compute_nDSFs(from_rbf, to_rbf); |
204 |
– |
pm->nrows = from_rbf->nrbf; |
205 |
– |
pm->ncols = to_rbf->nrbf; |
206 |
– |
pm->price = (float *)malloc(sizeof(float) * pm->nrows*pm->ncols); |
207 |
– |
pm->sord = (short *)malloc(sizeof(short) * pm->nrows*pm->ncols); |
208 |
– |
|
209 |
– |
if ((pm->price == NULL) | (pm->sord == NULL) | (vto == NULL)) { |
210 |
– |
fprintf(stderr, "%s: Out of memory in migration_costs()\n", |
211 |
– |
progname); |
212 |
– |
exit(1); |
213 |
– |
} |
214 |
– |
for (j = to_rbf->nrbf; j--; ) /* save repetitive ops. */ |
215 |
– |
ovec_from_pos(vto[j], to_rbf->rbfa[j].gx, to_rbf->rbfa[j].gy); |
216 |
– |
|
217 |
– |
for (i = from_rbf->nrbf; i--; ) { |
218 |
– |
const double from_ang = R2ANG(from_rbf->rbfa[i].crad); |
219 |
– |
FVECT vfrom; |
220 |
– |
short *srow; |
221 |
– |
ovec_from_pos(vfrom, from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy); |
222 |
– |
pm->prow = pricerow(pm,i); |
223 |
– |
srow = psortrow(pm,i); |
224 |
– |
for (j = to_rbf->nrbf; j--; ) { |
225 |
– |
double d; /* quadratic cost function */ |
226 |
– |
d = Acos(DOT(vfrom, vto[j])); |
227 |
– |
pm->prow[j] = d*d; |
228 |
– |
d = R2ANG(to_rbf->rbfa[j].crad) - from_ang; |
229 |
– |
pm->prow[j] += d*d; |
230 |
– |
/* neighborhood difference */ |
231 |
– |
pm->prow[j] += NEIGH_FACT2 * neighborhood_dist2( |
232 |
– |
from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy, |
233 |
– |
to_rbf->rbfa[j].gx, to_rbf->rbfa[j].gy ); |
234 |
– |
srow[j] = j; |
235 |
– |
} |
236 |
– |
qsort_r(srow, pm->ncols, sizeof(short), pm, &msrt_cmp); |
237 |
– |
} |
238 |
– |
free(vto); |
239 |
– |
} |
240 |
– |
|
241 |
– |
/* Free price matrix */ |
242 |
– |
static void |
243 |
– |
free_routes(PRICEMAT *pm) |
244 |
– |
{ |
245 |
– |
free(pm->price); pm->price = NULL; |
246 |
– |
free(pm->sord); pm->sord = NULL; |
247 |
– |
} |
248 |
– |
|
249 |
– |
/* Compute minimum (optimistic) cost for moving the given source material */ |
250 |
– |
static double |
251 |
– |
min_cost(double amt2move, const double *avail, const PRICEMAT *pm, int s) |
252 |
– |
{ |
253 |
– |
const short *srow = psortrow(pm,s); |
254 |
– |
const float *prow = pricerow(pm,s); |
255 |
– |
double total_cost = 0; |
256 |
– |
int j; |
257 |
– |
/* move cheapest first */ |
258 |
– |
for (j = 0; (j < pm->ncols) & (amt2move > FTINY); j++) { |
259 |
– |
int d = srow[j]; |
260 |
– |
double amt = (amt2move < avail[d]) ? amt2move : avail[d]; |
261 |
– |
|
262 |
– |
total_cost += amt * prow[d]; |
263 |
– |
amt2move -= amt; |
264 |
– |
} |
265 |
– |
return(total_cost); |
266 |
– |
} |
267 |
– |
|
268 |
– |
typedef struct { |
269 |
– |
short s, d; /* source and destination */ |
270 |
– |
float dc; /* discount to push inventory */ |
271 |
– |
} ROWSENT; /* row sort entry */ |
272 |
– |
|
273 |
– |
/* Compare entries by discounted moving price */ |
274 |
– |
static int |
275 |
– |
rmovcmp(void *b, const void *p1, const void *p2) |
276 |
– |
{ |
277 |
– |
PRICEMAT *pm = (PRICEMAT *)b; |
278 |
– |
const ROWSENT *re1 = (const ROWSENT *)p1; |
279 |
– |
const ROWSENT *re2 = (const ROWSENT *)p2; |
280 |
– |
double price_diff; |
281 |
– |
|
282 |
– |
if (re1->d < 0) return(re2->d >= 0); |
283 |
– |
if (re2->d < 0) return(-1); |
284 |
– |
price_diff = re1->dc*pricerow(pm,re1->s)[re1->d] - |
285 |
– |
re2->dc*pricerow(pm,re2->s)[re2->d]; |
286 |
– |
if (price_diff > 0) return(1); |
287 |
– |
if (price_diff < 0) return(-1); |
288 |
– |
return(0); |
289 |
– |
} |
290 |
– |
|
291 |
– |
/* Take a step in migration by choosing reasonable bucket to transfer */ |
292 |
– |
static double |
293 |
– |
migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, PRICEMAT *pm) |
294 |
– |
{ |
295 |
– |
const int max2check = 100; |
296 |
– |
const double maxamt = 1./(double)pm->ncols; |
297 |
– |
const double minamt = maxamt*1e-4; |
298 |
– |
double *src_cost; |
299 |
– |
ROWSENT *rord; |
300 |
– |
struct { |
301 |
– |
int s, d; /* source and destination */ |
302 |
– |
double price; /* cost per amount moved */ |
303 |
– |
double amt; /* amount we can move */ |
304 |
– |
} cur, best; |
305 |
– |
int r2check, i, ri; |
306 |
– |
/* |
307 |
– |
* Check cheapest available routes only -- a higher adjusted |
308 |
– |
* destination price implies that another source is closer, so |
309 |
– |
* we can hold off considering more expensive options until |
310 |
– |
* some other (hopefully better) moves have been made. |
311 |
– |
* A discount based on source remaining is supposed to prioritize |
312 |
– |
* movement from large lobes, but it doesn't seem to do much, |
313 |
– |
* so we have it set to 1.0 at the moment. |
314 |
– |
*/ |
315 |
– |
#define discount(qr) 1.0 |
316 |
– |
/* most promising row order */ |
317 |
– |
rord = (ROWSENT *)malloc(sizeof(ROWSENT)*pm->nrows); |
318 |
– |
if (rord == NULL) |
319 |
– |
goto memerr; |
320 |
– |
for (ri = pm->nrows; ri--; ) { |
321 |
– |
rord[ri].s = ri; |
322 |
– |
rord[ri].d = -1; |
323 |
– |
rord[ri].dc = 1.f; |
324 |
– |
if (src_rem[ri] <= minamt) /* enough source material? */ |
325 |
– |
continue; |
326 |
– |
for (i = 0; i < pm->ncols; i++) |
327 |
– |
if (dst_rem[ rord[ri].d = psortrow(pm,ri)[i] ] > minamt) |
328 |
– |
break; |
329 |
– |
if (i >= pm->ncols) { /* moved all we can? */ |
330 |
– |
free(rord); |
331 |
– |
return(.0); |
332 |
– |
} |
333 |
– |
rord[ri].dc = discount(src_rem[ri]); |
334 |
– |
} |
335 |
– |
if (pm->nrows > max2check) /* sort if too many sources */ |
336 |
– |
qsort_r(rord, pm->nrows, sizeof(ROWSENT), pm, &rmovcmp); |
337 |
– |
/* allocate cost array */ |
338 |
– |
src_cost = (double *)malloc(sizeof(double)*pm->nrows); |
339 |
– |
if (src_cost == NULL) |
340 |
– |
goto memerr; |
341 |
– |
for (i = pm->nrows; i--; ) /* starting costs for diff. */ |
342 |
– |
src_cost[i] = min_cost(src_rem[i], dst_rem, pm, i); |
343 |
– |
/* find best source & dest. */ |
344 |
– |
best.s = best.d = -1; best.price = FHUGE; best.amt = 0; |
345 |
– |
if ((r2check = pm->nrows) > max2check) |
346 |
– |
r2check = max2check; /* put a limit on search */ |
347 |
– |
for (ri = 0; ri < r2check; ri++) { /* check each source row */ |
348 |
– |
double cost_others = 0; |
349 |
– |
cur.s = rord[ri].s; |
350 |
– |
if ((cur.d = rord[ri].d) < 0 || |
351 |
– |
rord[ri].dc*pricerow(pm,cur.s)[cur.d] >= best.price) { |
352 |
– |
if (pm->nrows > max2check) break; /* sorted end */ |
353 |
– |
continue; /* else skip this one */ |
354 |
– |
} |
355 |
– |
cur.amt = (src_rem[cur.s] < dst_rem[cur.d]) ? |
356 |
– |
src_rem[cur.s] : dst_rem[cur.d]; |
357 |
– |
/* don't just leave smidgen */ |
358 |
– |
if (cur.amt > maxamt*1.02) cur.amt = maxamt; |
359 |
– |
dst_rem[cur.d] -= cur.amt; /* add up opportunity costs */ |
360 |
– |
for (i = pm->nrows; i--; ) |
361 |
– |
if (i != cur.s) |
362 |
– |
cost_others += min_cost(src_rem[i], dst_rem, pm, i) |
363 |
– |
- src_cost[i]; |
364 |
– |
dst_rem[cur.d] += cur.amt; /* undo trial move */ |
365 |
– |
/* discount effective price */ |
366 |
– |
cur.price = ( pricerow(pm,cur.s)[cur.d] + cost_others/cur.amt ) * |
367 |
– |
rord[ri].dc; |
368 |
– |
if (cur.price < best.price) /* are we better than best? */ |
369 |
– |
best = cur; |
370 |
– |
} |
371 |
– |
free(src_cost); /* clean up */ |
372 |
– |
free(rord); |
373 |
– |
if ((best.s < 0) | (best.d < 0)) /* nothing left to move? */ |
374 |
– |
return(.0); |
375 |
– |
/* else make the actual move */ |
376 |
– |
mtx_coef(mig,best.s,best.d) += best.amt; |
377 |
– |
src_rem[best.s] -= best.amt; |
378 |
– |
dst_rem[best.d] -= best.amt; |
379 |
– |
return(best.amt); |
380 |
– |
memerr: |
381 |
– |
fprintf(stderr, "%s: Out of memory in migration_step()\n", progname); |
382 |
– |
exit(1); |
383 |
– |
#undef discount |
384 |
– |
} |
385 |
– |
|
318 |
|
/* Compute and insert migration along directed edge (may fork child) */ |
319 |
|
static MIGRATION * |
320 |
|
create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
321 |
|
{ |
390 |
– |
const double end_thresh = 5e-6; |
391 |
– |
PRICEMAT pmtx; |
322 |
|
MIGRATION *newmig; |
393 |
– |
double *src_rem, *dst_rem; |
394 |
– |
double total_rem = 1., move_amt; |
323 |
|
int i, j; |
324 |
|
/* check if exists already */ |
325 |
|
for (newmig = from_rbf->ejl; newmig != NULL; |
339 |
|
newmig = new_migration(from_rbf, to_rbf); |
340 |
|
if (run_subprocess()) |
341 |
|
return(newmig); /* child continues */ |
414 |
– |
price_routes(&pmtx, from_rbf, to_rbf); |
415 |
– |
src_rem = (double *)malloc(sizeof(double)*from_rbf->nrbf); |
416 |
– |
dst_rem = (double *)malloc(sizeof(double)*to_rbf->nrbf); |
417 |
– |
if ((src_rem == NULL) | (dst_rem == NULL)) { |
418 |
– |
fprintf(stderr, "%s: Out of memory in create_migration()\n", |
419 |
– |
progname); |
420 |
– |
exit(1); |
421 |
– |
} |
422 |
– |
/* starting quantities */ |
423 |
– |
memset(newmig->mtx, 0, sizeof(float)*from_rbf->nrbf*to_rbf->nrbf); |
424 |
– |
for (i = from_rbf->nrbf; i--; ) |
425 |
– |
src_rem[i] = rbf_volume(&from_rbf->rbfa[i]) / from_rbf->vtotal; |
426 |
– |
for (j = to_rbf->nrbf; j--; ) |
427 |
– |
dst_rem[j] = rbf_volume(&to_rbf->rbfa[j]) / to_rbf->vtotal; |
342 |
|
|
343 |
< |
do { /* move a bit at a time */ |
344 |
< |
move_amt = migration_step(newmig, src_rem, dst_rem, &pmtx); |
345 |
< |
total_rem -= move_amt; |
432 |
< |
} while ((total_rem > end_thresh) & (move_amt > 0)); |
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 |
|
double nf = rbf_volume(&from_rbf->rbfa[i]); |
352 |
|
mtx_coef(newmig,i,j) *= nf; /* row now sums to 1.0 */ |
353 |
|
} |
354 |
|
end_subprocess(); /* exit here if subprocess */ |
442 |
– |
free_routes(&pmtx); /* free working arrays */ |
443 |
– |
free(src_rem); |
444 |
– |
free(dst_rem); |
355 |
|
return(newmig); |
356 |
|
} |
357 |
|
|
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) |
488 |
|
default: |
489 |
|
return; /* else we can interpolate */ |
490 |
|
} |
491 |
< |
for (rbf = near_rbf->next; rbf != NULL; rbf = rbf->next) { |
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 */ |
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, 2*near_rbf->nrbf); |
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); |
538 |
|
void |
539 |
|
build_mesh(void) |
540 |
|
{ |
541 |
+ |
int nrbfs = 0, nmigs = 0; |
542 |
|
double best2 = M_PI*M_PI; |
543 |
|
RBFNODE *shrt_edj[2]; |
544 |
|
RBFNODE *rbf0, *rbf1; |
545 |
+ |
const MIGRATION *ej; |
546 |
+ |
/* average specular peak */ |
547 |
+ |
comp_bsdf_spec(); |
548 |
|
/* add normal if needed */ |
549 |
|
check_normal_incidence(); |
550 |
|
/* check if isotropic */ |
556 |
|
return; |
557 |
|
} |
558 |
|
shrt_edj[0] = shrt_edj[1] = NULL; /* start w/ shortest edge */ |
559 |
< |
for (rbf0 = dsf_list; rbf0 != NULL; rbf0 = rbf0->next) |
559 |
> |
for (rbf0 = dsf_list; rbf0 != NULL; rbf0 = rbf0->next) { |
560 |
|
for (rbf1 = rbf0->next; rbf1 != NULL; rbf1 = rbf1->next) { |
561 |
|
double dist2 = 2. - 2.*DOT(rbf0->invec,rbf1->invec); |
562 |
|
if (dist2 < best2) { |
564 |
|
shrt_edj[1] = rbf1; |
565 |
|
best2 = dist2; |
566 |
|
} |
567 |
+ |
} |
568 |
+ |
++nrbfs; |
569 |
|
} |
570 |
|
if (shrt_edj[0] == NULL) { |
571 |
|
fprintf(stderr, "%s: Cannot find shortest edge\n", progname); |
576 |
|
mesh_from_edge(create_migration(shrt_edj[0], shrt_edj[1])); |
577 |
|
else |
578 |
|
mesh_from_edge(create_migration(shrt_edj[1], shrt_edj[0])); |
579 |
+ |
/* count up edges */ |
580 |
+ |
for (ej = mig_list; ej != NULL; ej = ej->next) |
581 |
+ |
++nmigs; |
582 |
+ |
if (nmigs < nrbfs-1) /* did meshing fail? */ |
583 |
+ |
fprintf(stderr, |
584 |
+ |
"%s: warning - %d incident directions but only %d interpolant(s)\n", |
585 |
+ |
progname, nrbfs, nmigs); |
586 |
|
/* complete migrations */ |
587 |
|
await_children(nchild); |
588 |
|
} |