1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: bsdfmesh.c,v 2.4 2012/11/08 23:11:41 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Create BSDF advection mesh from radial basis functions. |
6 |
* |
7 |
* G. Ward |
8 |
*/ |
9 |
|
10 |
#ifndef _WIN32 |
11 |
#include <unistd.h> |
12 |
#include <sys/wait.h> |
13 |
#include <sys/mman.h> |
14 |
#endif |
15 |
#define _USE_MATH_DEFINES |
16 |
#include <stdio.h> |
17 |
#include <stdlib.h> |
18 |
#include <string.h> |
19 |
#include <math.h> |
20 |
#include "bsdfrep.h" |
21 |
/* number of processes to run */ |
22 |
int nprocs = 1; |
23 |
/* number of children (-1 in child) */ |
24 |
static int nchild = 0; |
25 |
|
26 |
typedef struct { |
27 |
int nrows, ncols; /* array size (matches migration) */ |
28 |
float *price; /* migration prices */ |
29 |
short *sord; /* sort for each row, low to high */ |
30 |
} PRICEMAT; /* sorted pricing matrix */ |
31 |
|
32 |
#define pricerow(p,i) ((p)->price + (i)*(p)->ncols) |
33 |
#define psortrow(p,i) ((p)->sord + (i)*(p)->ncols) |
34 |
|
35 |
/* Create a new migration holder (sharing memory for multiprocessing) */ |
36 |
static MIGRATION * |
37 |
new_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
38 |
{ |
39 |
size_t memlen = sizeof(MIGRATION) + |
40 |
sizeof(float)*(from_rbf->nrbf*to_rbf->nrbf - 1); |
41 |
MIGRATION *newmig; |
42 |
#ifdef _WIN32 |
43 |
if (nprocs > 1) |
44 |
fprintf(stderr, "%s: warning - multiprocessing not supported\n", |
45 |
progname); |
46 |
nprocs = 1; |
47 |
newmig = (MIGRATION *)malloc(memlen); |
48 |
#else |
49 |
if (nprocs <= 1) { /* single process? */ |
50 |
newmig = (MIGRATION *)malloc(memlen); |
51 |
} else { /* else need to share memory */ |
52 |
newmig = (MIGRATION *)mmap(NULL, memlen, PROT_READ|PROT_WRITE, |
53 |
MAP_ANON|MAP_SHARED, -1, 0); |
54 |
if ((void *)newmig == MAP_FAILED) |
55 |
newmig = NULL; |
56 |
} |
57 |
#endif |
58 |
if (newmig == NULL) { |
59 |
fprintf(stderr, "%s: cannot allocate new migration\n", progname); |
60 |
exit(1); |
61 |
} |
62 |
newmig->rbfv[0] = from_rbf; |
63 |
newmig->rbfv[1] = to_rbf; |
64 |
/* insert in edge lists */ |
65 |
newmig->enxt[0] = from_rbf->ejl; |
66 |
from_rbf->ejl = newmig; |
67 |
newmig->enxt[1] = to_rbf->ejl; |
68 |
to_rbf->ejl = newmig; |
69 |
newmig->next = mig_list; /* push onto global list */ |
70 |
return(mig_list = newmig); |
71 |
} |
72 |
|
73 |
#ifdef _WIN32 |
74 |
#define await_children(n) (void)(n) |
75 |
#define run_subprocess() 0 |
76 |
#define end_subprocess() (void)0 |
77 |
#else |
78 |
|
79 |
/* Wait for the specified number of child processes to complete */ |
80 |
static void |
81 |
await_children(int n) |
82 |
{ |
83 |
int exit_status = 0; |
84 |
|
85 |
if (n > nchild) |
86 |
n = nchild; |
87 |
while (n-- > 0) { |
88 |
int status; |
89 |
if (wait(&status) < 0) { |
90 |
fprintf(stderr, "%s: missing child(ren)!\n", progname); |
91 |
nchild = 0; |
92 |
break; |
93 |
} |
94 |
--nchild; |
95 |
if (status) { /* something wrong */ |
96 |
if ((status = WEXITSTATUS(status))) |
97 |
exit_status = status; |
98 |
else |
99 |
exit_status += !exit_status; |
100 |
fprintf(stderr, "%s: subprocess died\n", progname); |
101 |
n = nchild; /* wait for the rest */ |
102 |
} |
103 |
} |
104 |
if (exit_status) |
105 |
exit(exit_status); |
106 |
} |
107 |
|
108 |
/* Start child process if multiprocessing selected */ |
109 |
static pid_t |
110 |
run_subprocess(void) |
111 |
{ |
112 |
int status; |
113 |
pid_t pid; |
114 |
|
115 |
if (nprocs <= 1) /* any children requested? */ |
116 |
return(0); |
117 |
await_children(nchild + 1 - nprocs); /* free up child process */ |
118 |
if ((pid = fork())) { |
119 |
if (pid < 0) { |
120 |
fprintf(stderr, "%s: cannot fork subprocess\n", |
121 |
progname); |
122 |
exit(1); |
123 |
} |
124 |
++nchild; /* subprocess started */ |
125 |
return(pid); |
126 |
} |
127 |
nchild = -1; |
128 |
return(0); /* put child to work */ |
129 |
} |
130 |
|
131 |
/* If we are in subprocess, call exit */ |
132 |
#define end_subprocess() if (nchild < 0) _exit(0); else |
133 |
|
134 |
#endif /* ! _WIN32 */ |
135 |
|
136 |
/* 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 */ |
151 |
static void |
152 |
price_routes(PRICEMAT *pm, const RBFNODE *from_rbf, const RBFNODE *to_rbf) |
153 |
{ |
154 |
FVECT *vto = (FVECT *)malloc(sizeof(FVECT) * to_rbf->nrbf); |
155 |
int i, j; |
156 |
|
157 |
pm->nrows = from_rbf->nrbf; |
158 |
pm->ncols = to_rbf->nrbf; |
159 |
pm->price = (float *)malloc(sizeof(float) * pm->nrows*pm->ncols); |
160 |
pm->sord = (short *)malloc(sizeof(short) * pm->nrows*pm->ncols); |
161 |
|
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; |
178 |
} |
179 |
qsort_r(psortrow(pm,i), pm->ncols, sizeof(short), pm, &msrt_cmp); |
180 |
} |
181 |
free(vto); |
182 |
} |
183 |
|
184 |
/* 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 */ |
193 |
static double |
194 |
min_cost(double amt2move, const double *avail, const PRICEMAT *pm, int s) |
195 |
{ |
196 |
double total_cost = 0; |
197 |
int j; |
198 |
|
199 |
if (amt2move <= FTINY) /* pre-emptive check */ |
200 |
return(.0); |
201 |
/* move cheapest first */ |
202 |
for (j = 0; j < pm->ncols && amt2move > FTINY; j++) { |
203 |
int d = psortrow(pm,s)[j]; |
204 |
double amt = (amt2move < avail[d]) ? amt2move : avail[d]; |
205 |
|
206 |
total_cost += amt * pricerow(pm,s)[d]; |
207 |
amt2move -= amt; |
208 |
} |
209 |
return(total_cost); |
210 |
} |
211 |
|
212 |
/* Take a step in migration by choosing optimal bucket to transfer */ |
213 |
static double |
214 |
migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, const PRICEMAT *pm) |
215 |
{ |
216 |
const double maxamt = 1./(double)pm->ncols; |
217 |
const double minamt = maxamt*5e-6; |
218 |
double *src_cost; |
219 |
struct { |
220 |
int s, d; /* source and destination */ |
221 |
double price; /* price estimate per amount moved */ |
222 |
double amt; /* amount we can move */ |
223 |
} cur, best; |
224 |
int i; |
225 |
/* allocate cost array */ |
226 |
src_cost = (double *)malloc(sizeof(double)*pm->nrows); |
227 |
if (src_cost == NULL) { |
228 |
fprintf(stderr, "%s: Out of memory in migration_step()\n", |
229 |
progname); |
230 |
exit(1); |
231 |
} |
232 |
for (i = pm->nrows; i--; ) /* starting costs for diff. */ |
233 |
src_cost[i] = min_cost(src_rem[i], dst_rem, pm, i); |
234 |
|
235 |
/* find best source & dest. */ |
236 |
best.s = best.d = -1; best.price = FHUGE; best.amt = 0; |
237 |
for (cur.s = pm->nrows; cur.s--; ) { |
238 |
double cost_others = 0; |
239 |
|
240 |
if (src_rem[cur.s] <= minamt) |
241 |
continue; |
242 |
/* examine cheapest dest. */ |
243 |
for (i = 0; i < pm->ncols; i++) |
244 |
if (dst_rem[ cur.d = psortrow(pm,cur.s)[i] ] > minamt) |
245 |
break; |
246 |
if (i >= pm->ncols) |
247 |
break; |
248 |
if ((cur.price = pricerow(pm,cur.s)[cur.d]) >= best.price) |
249 |
continue; /* no point checking further */ |
250 |
cur.amt = (src_rem[cur.s] < dst_rem[cur.d]) ? |
251 |
src_rem[cur.s] : dst_rem[cur.d]; |
252 |
if (cur.amt > maxamt) cur.amt = maxamt; |
253 |
dst_rem[cur.d] -= cur.amt; /* add up differential costs */ |
254 |
for (i = pm->nrows; i--; ) |
255 |
if (i != cur.s) |
256 |
cost_others += min_cost(src_rem[i], dst_rem, pm, i) |
257 |
- src_cost[i]; |
258 |
dst_rem[cur.d] += cur.amt; /* undo trial move */ |
259 |
cur.price += cost_others/cur.amt; /* adjust effective price */ |
260 |
if (cur.price < best.price) /* are we better than best? */ |
261 |
best = cur; |
262 |
} |
263 |
free(src_cost); /* finish up */ |
264 |
|
265 |
if ((best.s < 0) | (best.d < 0)) /* nothing left to move? */ |
266 |
return(.0); |
267 |
/* else make the actual move */ |
268 |
mtx_coef(mig,best.s,best.d) += best.amt; |
269 |
src_rem[best.s] -= best.amt; |
270 |
dst_rem[best.d] -= best.amt; |
271 |
return(best.amt); |
272 |
} |
273 |
|
274 |
#ifdef DEBUG |
275 |
static char * |
276 |
thetaphi(const FVECT v) |
277 |
{ |
278 |
static char buf[128]; |
279 |
double theta, phi; |
280 |
|
281 |
theta = 180./M_PI*acos(v[2]); |
282 |
phi = 180./M_PI*atan2(v[1],v[0]); |
283 |
sprintf(buf, "(%.0f,%.0f)", theta, phi); |
284 |
|
285 |
return(buf); |
286 |
} |
287 |
#endif |
288 |
|
289 |
/* Compute and insert migration along directed edge (may fork child) */ |
290 |
static MIGRATION * |
291 |
create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
292 |
{ |
293 |
const double end_thresh = 5e-6; |
294 |
PRICEMAT pmtx; |
295 |
MIGRATION *newmig; |
296 |
double *src_rem, *dst_rem; |
297 |
double total_rem = 1., move_amt; |
298 |
int i; |
299 |
/* check if exists already */ |
300 |
for (newmig = from_rbf->ejl; newmig != NULL; |
301 |
newmig = nextedge(from_rbf,newmig)) |
302 |
if (newmig->rbfv[1] == to_rbf) |
303 |
return(NULL); |
304 |
/* else allocate */ |
305 |
newmig = new_migration(from_rbf, to_rbf); |
306 |
if (run_subprocess()) |
307 |
return(newmig); /* child continues */ |
308 |
price_routes(&pmtx, from_rbf, to_rbf); |
309 |
src_rem = (double *)malloc(sizeof(double)*from_rbf->nrbf); |
310 |
dst_rem = (double *)malloc(sizeof(double)*to_rbf->nrbf); |
311 |
if ((src_rem == NULL) | (dst_rem == NULL)) { |
312 |
fprintf(stderr, "%s: Out of memory in create_migration()\n", |
313 |
progname); |
314 |
exit(1); |
315 |
} |
316 |
#ifdef DEBUG |
317 |
fprintf(stderr, "Building path from (theta,phi) %s ", |
318 |
thetaphi(from_rbf->invec)); |
319 |
fprintf(stderr, "to %s with %d x %d matrix\n", |
320 |
thetaphi(to_rbf->invec), |
321 |
from_rbf->nrbf, to_rbf->nrbf); |
322 |
#endif |
323 |
/* starting quantities */ |
324 |
memset(newmig->mtx, 0, sizeof(float)*from_rbf->nrbf*to_rbf->nrbf); |
325 |
for (i = from_rbf->nrbf; i--; ) |
326 |
src_rem[i] = rbf_volume(&from_rbf->rbfa[i]) / from_rbf->vtotal; |
327 |
for (i = to_rbf->nrbf; i--; ) |
328 |
dst_rem[i] = rbf_volume(&to_rbf->rbfa[i]) / to_rbf->vtotal; |
329 |
do { /* move a bit at a time */ |
330 |
move_amt = migration_step(newmig, src_rem, dst_rem, &pmtx); |
331 |
total_rem -= move_amt; |
332 |
#ifdef DEBUG |
333 |
if (!nchild) |
334 |
fprintf(stderr, "\r%.9f remaining...", total_rem); |
335 |
#endif |
336 |
} while ((total_rem > end_thresh) & (move_amt > 0)); |
337 |
#ifdef DEBUG |
338 |
if (!nchild) fputs("done.\n", stderr); |
339 |
else fprintf(stderr, "finished with %.9f remaining\n", total_rem); |
340 |
#endif |
341 |
for (i = from_rbf->nrbf; i--; ) { /* normalize final matrix */ |
342 |
float nf = rbf_volume(&from_rbf->rbfa[i]); |
343 |
int j; |
344 |
if (nf <= FTINY) continue; |
345 |
nf = from_rbf->vtotal / nf; |
346 |
for (j = to_rbf->nrbf; j--; ) |
347 |
mtx_coef(newmig,i,j) *= nf; |
348 |
} |
349 |
end_subprocess(); /* exit here if subprocess */ |
350 |
free_routes(&pmtx); /* free working arrays */ |
351 |
free(src_rem); |
352 |
free(dst_rem); |
353 |
return(newmig); |
354 |
} |
355 |
|
356 |
/* Check if prospective vertex would create overlapping triangle */ |
357 |
static int |
358 |
overlaps_tri(const RBFNODE *bv0, const RBFNODE *bv1, const RBFNODE *pv) |
359 |
{ |
360 |
const MIGRATION *ej; |
361 |
RBFNODE *vother[2]; |
362 |
int im_rev; |
363 |
/* find shared edge in mesh */ |
364 |
for (ej = pv->ejl; ej != NULL; ej = nextedge(pv,ej)) { |
365 |
const RBFNODE *tv = opp_rbf(pv,ej); |
366 |
if (tv == bv0) { |
367 |
im_rev = is_rev_tri(ej->rbfv[0]->invec, |
368 |
ej->rbfv[1]->invec, bv1->invec); |
369 |
break; |
370 |
} |
371 |
if (tv == bv1) { |
372 |
im_rev = is_rev_tri(ej->rbfv[0]->invec, |
373 |
ej->rbfv[1]->invec, bv0->invec); |
374 |
break; |
375 |
} |
376 |
} |
377 |
if (!get_triangles(vother, ej)) /* triangle on same side? */ |
378 |
return(0); |
379 |
return(vother[im_rev] != NULL); |
380 |
} |
381 |
|
382 |
/* Find context hull vertex to complete triangle (oriented call) */ |
383 |
static RBFNODE * |
384 |
find_chull_vert(const RBFNODE *rbf0, const RBFNODE *rbf1) |
385 |
{ |
386 |
FVECT vmid, vejn, vp; |
387 |
RBFNODE *rbf, *rbfbest = NULL; |
388 |
double dprod, area2, bestarea2 = FHUGE, bestdprod = -.5; |
389 |
|
390 |
VSUB(vejn, rbf1->invec, rbf0->invec); |
391 |
VADD(vmid, rbf0->invec, rbf1->invec); |
392 |
if (normalize(vejn) == 0 || normalize(vmid) == 0) |
393 |
return(NULL); |
394 |
/* XXX exhaustive search */ |
395 |
/* Find triangle with minimum rotation from perpendicular */ |
396 |
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) { |
397 |
if ((rbf == rbf0) | (rbf == rbf1)) |
398 |
continue; |
399 |
tri_orient(vp, rbf0->invec, rbf1->invec, rbf->invec); |
400 |
if (DOT(vp, vmid) <= FTINY) |
401 |
continue; /* wrong orientation */ |
402 |
area2 = .25*DOT(vp,vp); |
403 |
VSUB(vp, rbf->invec, rbf0->invec); |
404 |
dprod = -DOT(vp, vejn); |
405 |
VSUM(vp, vp, vejn, dprod); /* above guarantees non-zero */ |
406 |
dprod = DOT(vp, vmid) / VLEN(vp); |
407 |
if (dprod <= bestdprod + FTINY*(1 - 2*(area2 < bestarea2))) |
408 |
continue; /* found better already */ |
409 |
if (overlaps_tri(rbf0, rbf1, rbf)) |
410 |
continue; /* overlaps another triangle */ |
411 |
rbfbest = rbf; |
412 |
bestdprod = dprod; /* new one to beat */ |
413 |
bestarea2 = area2; |
414 |
} |
415 |
return(rbfbest); |
416 |
} |
417 |
|
418 |
/* Create new migration edge and grow mesh recursively around it */ |
419 |
static void |
420 |
mesh_from_edge(MIGRATION *edge) |
421 |
{ |
422 |
MIGRATION *ej0, *ej1; |
423 |
RBFNODE *tvert[2]; |
424 |
|
425 |
if (edge == NULL) |
426 |
return; |
427 |
/* triangle on either side? */ |
428 |
get_triangles(tvert, edge); |
429 |
if (tvert[0] == NULL) { /* grow mesh on right */ |
430 |
tvert[0] = find_chull_vert(edge->rbfv[0], edge->rbfv[1]); |
431 |
if (tvert[0] != NULL) { |
432 |
if (tvert[0]->ord > edge->rbfv[0]->ord) |
433 |
ej0 = create_migration(edge->rbfv[0], tvert[0]); |
434 |
else |
435 |
ej0 = create_migration(tvert[0], edge->rbfv[0]); |
436 |
if (tvert[0]->ord > edge->rbfv[1]->ord) |
437 |
ej1 = create_migration(edge->rbfv[1], tvert[0]); |
438 |
else |
439 |
ej1 = create_migration(tvert[0], edge->rbfv[1]); |
440 |
mesh_from_edge(ej0); |
441 |
mesh_from_edge(ej1); |
442 |
} |
443 |
} else if (tvert[1] == NULL) { /* grow mesh on left */ |
444 |
tvert[1] = find_chull_vert(edge->rbfv[1], edge->rbfv[0]); |
445 |
if (tvert[1] != NULL) { |
446 |
if (tvert[1]->ord > edge->rbfv[0]->ord) |
447 |
ej0 = create_migration(edge->rbfv[0], tvert[1]); |
448 |
else |
449 |
ej0 = create_migration(tvert[1], edge->rbfv[0]); |
450 |
if (tvert[1]->ord > edge->rbfv[1]->ord) |
451 |
ej1 = create_migration(edge->rbfv[1], tvert[1]); |
452 |
else |
453 |
ej1 = create_migration(tvert[1], edge->rbfv[1]); |
454 |
mesh_from_edge(ej0); |
455 |
mesh_from_edge(ej1); |
456 |
} |
457 |
} |
458 |
} |
459 |
|
460 |
/* Build our triangle mesh from recorded RBFs */ |
461 |
void |
462 |
build_mesh(void) |
463 |
{ |
464 |
double best2 = M_PI*M_PI; |
465 |
RBFNODE *shrt_edj[2]; |
466 |
RBFNODE *rbf0, *rbf1; |
467 |
/* check if isotropic */ |
468 |
if (single_plane_incident) { |
469 |
for (rbf0 = dsf_list; rbf0 != NULL; rbf0 = rbf0->next) |
470 |
if (rbf0->next != NULL) |
471 |
create_migration(rbf0, rbf0->next); |
472 |
await_children(nchild); |
473 |
return; |
474 |
} |
475 |
shrt_edj[0] = shrt_edj[1] = NULL; /* start w/ shortest edge */ |
476 |
for (rbf0 = dsf_list; rbf0 != NULL; rbf0 = rbf0->next) |
477 |
for (rbf1 = rbf0->next; rbf1 != NULL; rbf1 = rbf1->next) { |
478 |
double dist2 = 2. - 2.*DOT(rbf0->invec,rbf1->invec); |
479 |
if (dist2 < best2) { |
480 |
shrt_edj[0] = rbf0; |
481 |
shrt_edj[1] = rbf1; |
482 |
best2 = dist2; |
483 |
} |
484 |
} |
485 |
if (shrt_edj[0] == NULL) { |
486 |
fprintf(stderr, "%s: Cannot find shortest edge\n", progname); |
487 |
exit(1); |
488 |
} |
489 |
/* build mesh from this edge */ |
490 |
if (shrt_edj[0]->ord < shrt_edj[1]->ord) |
491 |
mesh_from_edge(create_migration(shrt_edj[0], shrt_edj[1])); |
492 |
else |
493 |
mesh_from_edge(create_migration(shrt_edj[1], shrt_edj[0])); |
494 |
/* complete migrations */ |
495 |
await_children(nchild); |
496 |
} |