1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.18 |
static const char RCSid[] = "$Id: bsdfmesh.c,v 2.17 2014/03/05 22:47:16 greg Exp $"; |
3 |
greg |
2.1 |
#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 |
greg |
2.3 |
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 |
greg |
2.10 |
float *prow; /* current price row */ |
31 |
greg |
2.3 |
} PRICEMAT; /* sorted pricing matrix */ |
32 |
|
|
|
33 |
|
|
#define pricerow(p,i) ((p)->price + (i)*(p)->ncols) |
34 |
|
|
#define psortrow(p,i) ((p)->sord + (i)*(p)->ncols) |
35 |
|
|
|
36 |
greg |
2.2 |
/* Create a new migration holder (sharing memory for multiprocessing) */ |
37 |
|
|
static MIGRATION * |
38 |
|
|
new_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
39 |
|
|
{ |
40 |
|
|
size_t memlen = sizeof(MIGRATION) + |
41 |
|
|
sizeof(float)*(from_rbf->nrbf*to_rbf->nrbf - 1); |
42 |
|
|
MIGRATION *newmig; |
43 |
|
|
#ifdef _WIN32 |
44 |
|
|
if (nprocs > 1) |
45 |
|
|
fprintf(stderr, "%s: warning - multiprocessing not supported\n", |
46 |
|
|
progname); |
47 |
|
|
nprocs = 1; |
48 |
|
|
newmig = (MIGRATION *)malloc(memlen); |
49 |
|
|
#else |
50 |
|
|
if (nprocs <= 1) { /* single process? */ |
51 |
|
|
newmig = (MIGRATION *)malloc(memlen); |
52 |
|
|
} else { /* else need to share memory */ |
53 |
|
|
newmig = (MIGRATION *)mmap(NULL, memlen, PROT_READ|PROT_WRITE, |
54 |
|
|
MAP_ANON|MAP_SHARED, -1, 0); |
55 |
|
|
if ((void *)newmig == MAP_FAILED) |
56 |
|
|
newmig = NULL; |
57 |
|
|
} |
58 |
|
|
#endif |
59 |
|
|
if (newmig == NULL) { |
60 |
|
|
fprintf(stderr, "%s: cannot allocate new migration\n", progname); |
61 |
|
|
exit(1); |
62 |
|
|
} |
63 |
|
|
newmig->rbfv[0] = from_rbf; |
64 |
|
|
newmig->rbfv[1] = to_rbf; |
65 |
|
|
/* insert in edge lists */ |
66 |
|
|
newmig->enxt[0] = from_rbf->ejl; |
67 |
|
|
from_rbf->ejl = newmig; |
68 |
|
|
newmig->enxt[1] = to_rbf->ejl; |
69 |
|
|
to_rbf->ejl = newmig; |
70 |
|
|
newmig->next = mig_list; /* push onto global list */ |
71 |
|
|
return(mig_list = newmig); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
#ifdef _WIN32 |
75 |
|
|
#define await_children(n) (void)(n) |
76 |
|
|
#define run_subprocess() 0 |
77 |
|
|
#define end_subprocess() (void)0 |
78 |
|
|
#else |
79 |
|
|
|
80 |
|
|
/* Wait for the specified number of child processes to complete */ |
81 |
|
|
static void |
82 |
|
|
await_children(int n) |
83 |
|
|
{ |
84 |
|
|
int exit_status = 0; |
85 |
|
|
|
86 |
|
|
if (n > nchild) |
87 |
|
|
n = nchild; |
88 |
|
|
while (n-- > 0) { |
89 |
|
|
int status; |
90 |
|
|
if (wait(&status) < 0) { |
91 |
|
|
fprintf(stderr, "%s: missing child(ren)!\n", progname); |
92 |
|
|
nchild = 0; |
93 |
|
|
break; |
94 |
|
|
} |
95 |
|
|
--nchild; |
96 |
|
|
if (status) { /* something wrong */ |
97 |
|
|
if ((status = WEXITSTATUS(status))) |
98 |
|
|
exit_status = status; |
99 |
|
|
else |
100 |
|
|
exit_status += !exit_status; |
101 |
|
|
fprintf(stderr, "%s: subprocess died\n", progname); |
102 |
|
|
n = nchild; /* wait for the rest */ |
103 |
|
|
} |
104 |
|
|
} |
105 |
|
|
if (exit_status) |
106 |
|
|
exit(exit_status); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
/* Start child process if multiprocessing selected */ |
110 |
|
|
static pid_t |
111 |
|
|
run_subprocess(void) |
112 |
|
|
{ |
113 |
|
|
int status; |
114 |
|
|
pid_t pid; |
115 |
|
|
|
116 |
|
|
if (nprocs <= 1) /* any children requested? */ |
117 |
|
|
return(0); |
118 |
|
|
await_children(nchild + 1 - nprocs); /* free up child process */ |
119 |
|
|
if ((pid = fork())) { |
120 |
|
|
if (pid < 0) { |
121 |
|
|
fprintf(stderr, "%s: cannot fork subprocess\n", |
122 |
|
|
progname); |
123 |
greg |
2.6 |
await_children(nchild); |
124 |
greg |
2.2 |
exit(1); |
125 |
|
|
} |
126 |
|
|
++nchild; /* subprocess started */ |
127 |
|
|
return(pid); |
128 |
|
|
} |
129 |
|
|
nchild = -1; |
130 |
|
|
return(0); /* put child to work */ |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
/* If we are in subprocess, call exit */ |
134 |
|
|
#define end_subprocess() if (nchild < 0) _exit(0); else |
135 |
|
|
|
136 |
|
|
#endif /* ! _WIN32 */ |
137 |
|
|
|
138 |
greg |
2.3 |
/* Comparison routine needed for sorting price row */ |
139 |
|
|
static int |
140 |
|
|
msrt_cmp(void *b, const void *p1, const void *p2) |
141 |
|
|
{ |
142 |
|
|
PRICEMAT *pm = (PRICEMAT *)b; |
143 |
greg |
2.10 |
float c1 = pm->prow[*(const short *)p1]; |
144 |
|
|
float c2 = pm->prow[*(const short *)p2]; |
145 |
greg |
2.3 |
|
146 |
|
|
if (c1 > c2) return(1); |
147 |
|
|
if (c1 < c2) return(-1); |
148 |
|
|
return(0); |
149 |
|
|
} |
150 |
|
|
|
151 |
greg |
2.1 |
/* Compute (and allocate) migration price matrix for optimization */ |
152 |
greg |
2.3 |
static void |
153 |
|
|
price_routes(PRICEMAT *pm, const RBFNODE *from_rbf, const RBFNODE *to_rbf) |
154 |
greg |
2.1 |
{ |
155 |
|
|
FVECT *vto = (FVECT *)malloc(sizeof(FVECT) * to_rbf->nrbf); |
156 |
|
|
int i, j; |
157 |
|
|
|
158 |
greg |
2.3 |
pm->nrows = from_rbf->nrbf; |
159 |
|
|
pm->ncols = to_rbf->nrbf; |
160 |
|
|
pm->price = (float *)malloc(sizeof(float) * pm->nrows*pm->ncols); |
161 |
|
|
pm->sord = (short *)malloc(sizeof(short) * pm->nrows*pm->ncols); |
162 |
|
|
|
163 |
|
|
if ((pm->price == NULL) | (pm->sord == NULL) | (vto == NULL)) { |
164 |
greg |
2.1 |
fprintf(stderr, "%s: Out of memory in migration_costs()\n", |
165 |
|
|
progname); |
166 |
|
|
exit(1); |
167 |
|
|
} |
168 |
|
|
for (j = to_rbf->nrbf; j--; ) /* save repetitive ops. */ |
169 |
|
|
ovec_from_pos(vto[j], to_rbf->rbfa[j].gx, to_rbf->rbfa[j].gy); |
170 |
|
|
|
171 |
|
|
for (i = from_rbf->nrbf; i--; ) { |
172 |
|
|
const double from_ang = R2ANG(from_rbf->rbfa[i].crad); |
173 |
|
|
FVECT vfrom; |
174 |
greg |
2.10 |
short *srow; |
175 |
greg |
2.1 |
ovec_from_pos(vfrom, from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy); |
176 |
greg |
2.10 |
pm->prow = pricerow(pm,i); |
177 |
|
|
srow = psortrow(pm,i); |
178 |
greg |
2.3 |
for (j = to_rbf->nrbf; j--; ) { |
179 |
greg |
2.13 |
double d; /* quadratic cost function */ |
180 |
greg |
2.18 |
d = Acos(DOT(vfrom, vto[j])); |
181 |
greg |
2.13 |
pm->prow[j] = d*d; |
182 |
|
|
d = R2ANG(to_rbf->rbfa[j].crad) - from_ang; |
183 |
|
|
pm->prow[j] += d*d; |
184 |
greg |
2.10 |
srow[j] = j; |
185 |
greg |
2.3 |
} |
186 |
greg |
2.10 |
qsort_r(srow, pm->ncols, sizeof(short), pm, &msrt_cmp); |
187 |
greg |
2.1 |
} |
188 |
|
|
free(vto); |
189 |
|
|
} |
190 |
|
|
|
191 |
greg |
2.3 |
/* Free price matrix */ |
192 |
|
|
static void |
193 |
|
|
free_routes(PRICEMAT *pm) |
194 |
greg |
2.1 |
{ |
195 |
greg |
2.3 |
free(pm->price); pm->price = NULL; |
196 |
|
|
free(pm->sord); pm->sord = NULL; |
197 |
greg |
2.1 |
} |
198 |
|
|
|
199 |
|
|
/* Compute minimum (optimistic) cost for moving the given source material */ |
200 |
|
|
static double |
201 |
greg |
2.3 |
min_cost(double amt2move, const double *avail, const PRICEMAT *pm, int s) |
202 |
greg |
2.1 |
{ |
203 |
greg |
2.11 |
const short *srow = psortrow(pm,s); |
204 |
|
|
const float *prow = pricerow(pm,s); |
205 |
greg |
2.1 |
double total_cost = 0; |
206 |
greg |
2.3 |
int j; |
207 |
greg |
2.1 |
/* move cheapest first */ |
208 |
greg |
2.11 |
for (j = 0; (j < pm->ncols) & (amt2move > FTINY); j++) { |
209 |
|
|
int d = srow[j]; |
210 |
greg |
2.1 |
double amt = (amt2move < avail[d]) ? amt2move : avail[d]; |
211 |
|
|
|
212 |
greg |
2.11 |
total_cost += amt * prow[d]; |
213 |
greg |
2.1 |
amt2move -= amt; |
214 |
|
|
} |
215 |
|
|
return(total_cost); |
216 |
|
|
} |
217 |
|
|
|
218 |
greg |
2.17 |
typedef struct { |
219 |
|
|
short s, d; /* source and destination */ |
220 |
|
|
float dc; /* discount to push inventory */ |
221 |
|
|
} ROWSENT; /* row sort entry */ |
222 |
|
|
|
223 |
|
|
/* Compare entries by discounted moving price */ |
224 |
greg |
2.11 |
static int |
225 |
|
|
rmovcmp(void *b, const void *p1, const void *p2) |
226 |
|
|
{ |
227 |
|
|
PRICEMAT *pm = (PRICEMAT *)b; |
228 |
greg |
2.17 |
const ROWSENT *re1 = (const ROWSENT *)p1; |
229 |
|
|
const ROWSENT *re2 = (const ROWSENT *)p2; |
230 |
|
|
double price_diff; |
231 |
|
|
|
232 |
|
|
if (re1->d < 0) return(re2->d >= 0); |
233 |
|
|
if (re2->d < 0) return(-1); |
234 |
|
|
price_diff = re1->dc*pricerow(pm,re1->s)[re1->d] - |
235 |
|
|
re2->dc*pricerow(pm,re2->s)[re2->d]; |
236 |
greg |
2.11 |
if (price_diff > 0) return(1); |
237 |
|
|
if (price_diff < 0) return(-1); |
238 |
|
|
return(0); |
239 |
|
|
} |
240 |
|
|
|
241 |
|
|
/* Take a step in migration by choosing reasonable bucket to transfer */ |
242 |
greg |
2.1 |
static double |
243 |
greg |
2.11 |
migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, PRICEMAT *pm) |
244 |
greg |
2.1 |
{ |
245 |
greg |
2.11 |
const int max2check = 100; |
246 |
greg |
2.4 |
const double maxamt = 1./(double)pm->ncols; |
247 |
greg |
2.12 |
const double minamt = maxamt*1e-4; |
248 |
greg |
2.5 |
double *src_cost; |
249 |
greg |
2.17 |
ROWSENT *rord; |
250 |
greg |
2.1 |
struct { |
251 |
|
|
int s, d; /* source and destination */ |
252 |
greg |
2.17 |
double price; /* cost per amount moved */ |
253 |
greg |
2.1 |
double amt; /* amount we can move */ |
254 |
|
|
} cur, best; |
255 |
greg |
2.11 |
int r2check, i, ri; |
256 |
|
|
/* |
257 |
|
|
* Check cheapest available routes only -- a higher adjusted |
258 |
|
|
* destination price implies that another source is closer, so |
259 |
|
|
* we can hold off considering more expensive options until |
260 |
|
|
* some other (hopefully better) moves have been made. |
261 |
greg |
2.17 |
* A discount based on source remaining is supposed to prioritize |
262 |
|
|
* movement from large lobes, but it doesn't seem to do much, |
263 |
|
|
* so we have it set to 1.0 at the moment. |
264 |
greg |
2.11 |
*/ |
265 |
greg |
2.17 |
#define discount(qr) 1.0 |
266 |
greg |
2.11 |
/* most promising row order */ |
267 |
greg |
2.17 |
rord = (ROWSENT *)malloc(sizeof(ROWSENT)*pm->nrows); |
268 |
greg |
2.11 |
if (rord == NULL) |
269 |
|
|
goto memerr; |
270 |
|
|
for (ri = pm->nrows; ri--; ) { |
271 |
greg |
2.17 |
rord[ri].s = ri; |
272 |
|
|
rord[ri].d = -1; |
273 |
|
|
rord[ri].dc = 1.f; |
274 |
greg |
2.11 |
if (src_rem[ri] <= minamt) /* enough source material? */ |
275 |
|
|
continue; |
276 |
|
|
for (i = 0; i < pm->ncols; i++) |
277 |
greg |
2.17 |
if (dst_rem[ rord[ri].d = psortrow(pm,ri)[i] ] > minamt) |
278 |
greg |
2.11 |
break; |
279 |
|
|
if (i >= pm->ncols) { /* moved all we can? */ |
280 |
|
|
free(rord); |
281 |
|
|
return(.0); |
282 |
|
|
} |
283 |
greg |
2.17 |
rord[ri].dc = discount(src_rem[ri]); |
284 |
greg |
2.11 |
} |
285 |
|
|
if (pm->nrows > max2check) /* sort if too many sources */ |
286 |
greg |
2.17 |
qsort_r(rord, pm->nrows, sizeof(ROWSENT), pm, &rmovcmp); |
287 |
greg |
2.5 |
/* allocate cost array */ |
288 |
|
|
src_cost = (double *)malloc(sizeof(double)*pm->nrows); |
289 |
greg |
2.11 |
if (src_cost == NULL) |
290 |
|
|
goto memerr; |
291 |
greg |
2.3 |
for (i = pm->nrows; i--; ) /* starting costs for diff. */ |
292 |
|
|
src_cost[i] = min_cost(src_rem[i], dst_rem, pm, i); |
293 |
greg |
2.1 |
/* find best source & dest. */ |
294 |
|
|
best.s = best.d = -1; best.price = FHUGE; best.amt = 0; |
295 |
greg |
2.11 |
if ((r2check = pm->nrows) > max2check) |
296 |
|
|
r2check = max2check; /* put a limit on search */ |
297 |
|
|
for (ri = 0; ri < r2check; ri++) { /* check each source row */ |
298 |
greg |
2.1 |
double cost_others = 0; |
299 |
greg |
2.17 |
cur.s = rord[ri].s; |
300 |
|
|
if ((cur.d = rord[ri].d) < 0 || |
301 |
|
|
rord[ri].dc*pricerow(pm,cur.s)[cur.d] >= best.price) { |
302 |
greg |
2.11 |
if (pm->nrows > max2check) break; /* sorted end */ |
303 |
|
|
continue; /* else skip this one */ |
304 |
|
|
} |
305 |
greg |
2.1 |
cur.amt = (src_rem[cur.s] < dst_rem[cur.d]) ? |
306 |
|
|
src_rem[cur.s] : dst_rem[cur.d]; |
307 |
greg |
2.11 |
/* don't just leave smidgen */ |
308 |
|
|
if (cur.amt > maxamt*1.02) cur.amt = maxamt; |
309 |
|
|
dst_rem[cur.d] -= cur.amt; /* add up opportunity costs */ |
310 |
greg |
2.3 |
for (i = pm->nrows; i--; ) |
311 |
greg |
2.1 |
if (i != cur.s) |
312 |
greg |
2.11 |
cost_others += min_cost(src_rem[i], dst_rem, pm, i) |
313 |
greg |
2.1 |
- src_cost[i]; |
314 |
|
|
dst_rem[cur.d] += cur.amt; /* undo trial move */ |
315 |
greg |
2.17 |
/* discount effective price */ |
316 |
|
|
cur.price = ( pricerow(pm,cur.s)[cur.d] + cost_others/cur.amt ) * |
317 |
|
|
rord[ri].dc; |
318 |
greg |
2.1 |
if (cur.price < best.price) /* are we better than best? */ |
319 |
greg |
2.11 |
best = cur; |
320 |
greg |
2.1 |
} |
321 |
greg |
2.11 |
free(src_cost); /* clean up */ |
322 |
|
|
free(rord); |
323 |
greg |
2.5 |
if ((best.s < 0) | (best.d < 0)) /* nothing left to move? */ |
324 |
greg |
2.1 |
return(.0); |
325 |
greg |
2.5 |
/* else make the actual move */ |
326 |
greg |
2.2 |
mtx_coef(mig,best.s,best.d) += best.amt; |
327 |
greg |
2.1 |
src_rem[best.s] -= best.amt; |
328 |
|
|
dst_rem[best.d] -= best.amt; |
329 |
|
|
return(best.amt); |
330 |
greg |
2.11 |
memerr: |
331 |
|
|
fprintf(stderr, "%s: Out of memory in migration_step()\n", progname); |
332 |
|
|
exit(1); |
333 |
greg |
2.17 |
#undef discount |
334 |
greg |
2.1 |
} |
335 |
|
|
|
336 |
|
|
/* Compute and insert migration along directed edge (may fork child) */ |
337 |
|
|
static MIGRATION * |
338 |
|
|
create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
339 |
|
|
{ |
340 |
greg |
2.2 |
const double end_thresh = 5e-6; |
341 |
greg |
2.3 |
PRICEMAT pmtx; |
342 |
greg |
2.1 |
MIGRATION *newmig; |
343 |
|
|
double *src_rem, *dst_rem; |
344 |
|
|
double total_rem = 1., move_amt; |
345 |
greg |
2.6 |
int i, j; |
346 |
greg |
2.1 |
/* check if exists already */ |
347 |
|
|
for (newmig = from_rbf->ejl; newmig != NULL; |
348 |
|
|
newmig = nextedge(from_rbf,newmig)) |
349 |
|
|
if (newmig->rbfv[1] == to_rbf) |
350 |
|
|
return(NULL); |
351 |
|
|
/* else allocate */ |
352 |
greg |
2.7 |
#ifdef DEBUG |
353 |
greg |
2.14 |
fprintf(stderr, "Building path from (theta,phi) (%.1f,%.1f) ", |
354 |
greg |
2.7 |
get_theta180(from_rbf->invec), |
355 |
|
|
get_phi360(from_rbf->invec)); |
356 |
greg |
2.14 |
fprintf(stderr, "to (%.1f,%.1f) with %d x %d matrix\n", |
357 |
greg |
2.7 |
get_theta180(to_rbf->invec), |
358 |
|
|
get_phi360(to_rbf->invec), |
359 |
|
|
from_rbf->nrbf, to_rbf->nrbf); |
360 |
|
|
#endif |
361 |
greg |
2.1 |
newmig = new_migration(from_rbf, to_rbf); |
362 |
|
|
if (run_subprocess()) |
363 |
|
|
return(newmig); /* child continues */ |
364 |
greg |
2.3 |
price_routes(&pmtx, from_rbf, to_rbf); |
365 |
greg |
2.1 |
src_rem = (double *)malloc(sizeof(double)*from_rbf->nrbf); |
366 |
|
|
dst_rem = (double *)malloc(sizeof(double)*to_rbf->nrbf); |
367 |
|
|
if ((src_rem == NULL) | (dst_rem == NULL)) { |
368 |
|
|
fprintf(stderr, "%s: Out of memory in create_migration()\n", |
369 |
|
|
progname); |
370 |
|
|
exit(1); |
371 |
|
|
} |
372 |
|
|
/* starting quantities */ |
373 |
|
|
memset(newmig->mtx, 0, sizeof(float)*from_rbf->nrbf*to_rbf->nrbf); |
374 |
|
|
for (i = from_rbf->nrbf; i--; ) |
375 |
|
|
src_rem[i] = rbf_volume(&from_rbf->rbfa[i]) / from_rbf->vtotal; |
376 |
greg |
2.6 |
for (j = to_rbf->nrbf; j--; ) |
377 |
|
|
dst_rem[j] = rbf_volume(&to_rbf->rbfa[j]) / to_rbf->vtotal; |
378 |
|
|
|
379 |
greg |
2.1 |
do { /* move a bit at a time */ |
380 |
greg |
2.3 |
move_amt = migration_step(newmig, src_rem, dst_rem, &pmtx); |
381 |
greg |
2.1 |
total_rem -= move_amt; |
382 |
greg |
2.2 |
} while ((total_rem > end_thresh) & (move_amt > 0)); |
383 |
greg |
2.6 |
|
384 |
greg |
2.1 |
for (i = from_rbf->nrbf; i--; ) { /* normalize final matrix */ |
385 |
greg |
2.6 |
double nf = rbf_volume(&from_rbf->rbfa[i]); |
386 |
greg |
2.1 |
if (nf <= FTINY) continue; |
387 |
|
|
nf = from_rbf->vtotal / nf; |
388 |
|
|
for (j = to_rbf->nrbf; j--; ) |
389 |
greg |
2.6 |
mtx_coef(newmig,i,j) *= nf; /* row now sums to 1.0 */ |
390 |
greg |
2.1 |
} |
391 |
|
|
end_subprocess(); /* exit here if subprocess */ |
392 |
greg |
2.3 |
free_routes(&pmtx); /* free working arrays */ |
393 |
greg |
2.1 |
free(src_rem); |
394 |
|
|
free(dst_rem); |
395 |
|
|
return(newmig); |
396 |
|
|
} |
397 |
|
|
|
398 |
|
|
/* Check if prospective vertex would create overlapping triangle */ |
399 |
|
|
static int |
400 |
|
|
overlaps_tri(const RBFNODE *bv0, const RBFNODE *bv1, const RBFNODE *pv) |
401 |
|
|
{ |
402 |
|
|
const MIGRATION *ej; |
403 |
|
|
RBFNODE *vother[2]; |
404 |
|
|
int im_rev; |
405 |
|
|
/* find shared edge in mesh */ |
406 |
|
|
for (ej = pv->ejl; ej != NULL; ej = nextedge(pv,ej)) { |
407 |
|
|
const RBFNODE *tv = opp_rbf(pv,ej); |
408 |
|
|
if (tv == bv0) { |
409 |
|
|
im_rev = is_rev_tri(ej->rbfv[0]->invec, |
410 |
|
|
ej->rbfv[1]->invec, bv1->invec); |
411 |
|
|
break; |
412 |
|
|
} |
413 |
|
|
if (tv == bv1) { |
414 |
|
|
im_rev = is_rev_tri(ej->rbfv[0]->invec, |
415 |
|
|
ej->rbfv[1]->invec, bv0->invec); |
416 |
|
|
break; |
417 |
|
|
} |
418 |
|
|
} |
419 |
|
|
if (!get_triangles(vother, ej)) /* triangle on same side? */ |
420 |
|
|
return(0); |
421 |
|
|
return(vother[im_rev] != NULL); |
422 |
|
|
} |
423 |
|
|
|
424 |
greg |
2.14 |
/* Find convex hull vertex to complete triangle (oriented call) */ |
425 |
greg |
2.1 |
static RBFNODE * |
426 |
|
|
find_chull_vert(const RBFNODE *rbf0, const RBFNODE *rbf1) |
427 |
|
|
{ |
428 |
|
|
FVECT vmid, vejn, vp; |
429 |
|
|
RBFNODE *rbf, *rbfbest = NULL; |
430 |
|
|
double dprod, area2, bestarea2 = FHUGE, bestdprod = -.5; |
431 |
|
|
|
432 |
|
|
VSUB(vejn, rbf1->invec, rbf0->invec); |
433 |
|
|
VADD(vmid, rbf0->invec, rbf1->invec); |
434 |
|
|
if (normalize(vejn) == 0 || normalize(vmid) == 0) |
435 |
|
|
return(NULL); |
436 |
|
|
/* XXX exhaustive search */ |
437 |
|
|
/* Find triangle with minimum rotation from perpendicular */ |
438 |
|
|
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) { |
439 |
|
|
if ((rbf == rbf0) | (rbf == rbf1)) |
440 |
|
|
continue; |
441 |
|
|
tri_orient(vp, rbf0->invec, rbf1->invec, rbf->invec); |
442 |
|
|
if (DOT(vp, vmid) <= FTINY) |
443 |
|
|
continue; /* wrong orientation */ |
444 |
|
|
area2 = .25*DOT(vp,vp); |
445 |
greg |
2.14 |
VSUB(vp, rbf->invec, vmid); |
446 |
greg |
2.1 |
dprod = -DOT(vp, vejn); |
447 |
|
|
VSUM(vp, vp, vejn, dprod); /* above guarantees non-zero */ |
448 |
|
|
dprod = DOT(vp, vmid) / VLEN(vp); |
449 |
|
|
if (dprod <= bestdprod + FTINY*(1 - 2*(area2 < bestarea2))) |
450 |
|
|
continue; /* found better already */ |
451 |
|
|
if (overlaps_tri(rbf0, rbf1, rbf)) |
452 |
|
|
continue; /* overlaps another triangle */ |
453 |
|
|
rbfbest = rbf; |
454 |
|
|
bestdprod = dprod; /* new one to beat */ |
455 |
|
|
bestarea2 = area2; |
456 |
|
|
} |
457 |
|
|
return(rbfbest); |
458 |
|
|
} |
459 |
|
|
|
460 |
|
|
/* Create new migration edge and grow mesh recursively around it */ |
461 |
|
|
static void |
462 |
|
|
mesh_from_edge(MIGRATION *edge) |
463 |
|
|
{ |
464 |
|
|
MIGRATION *ej0, *ej1; |
465 |
|
|
RBFNODE *tvert[2]; |
466 |
|
|
|
467 |
|
|
if (edge == NULL) |
468 |
|
|
return; |
469 |
|
|
/* triangle on either side? */ |
470 |
|
|
get_triangles(tvert, edge); |
471 |
|
|
if (tvert[0] == NULL) { /* grow mesh on right */ |
472 |
|
|
tvert[0] = find_chull_vert(edge->rbfv[0], edge->rbfv[1]); |
473 |
|
|
if (tvert[0] != NULL) { |
474 |
|
|
if (tvert[0]->ord > edge->rbfv[0]->ord) |
475 |
|
|
ej0 = create_migration(edge->rbfv[0], tvert[0]); |
476 |
|
|
else |
477 |
|
|
ej0 = create_migration(tvert[0], edge->rbfv[0]); |
478 |
|
|
if (tvert[0]->ord > edge->rbfv[1]->ord) |
479 |
|
|
ej1 = create_migration(edge->rbfv[1], tvert[0]); |
480 |
|
|
else |
481 |
|
|
ej1 = create_migration(tvert[0], edge->rbfv[1]); |
482 |
|
|
mesh_from_edge(ej0); |
483 |
|
|
mesh_from_edge(ej1); |
484 |
|
|
} |
485 |
|
|
} else if (tvert[1] == NULL) { /* grow mesh on left */ |
486 |
|
|
tvert[1] = find_chull_vert(edge->rbfv[1], edge->rbfv[0]); |
487 |
|
|
if (tvert[1] != NULL) { |
488 |
|
|
if (tvert[1]->ord > edge->rbfv[0]->ord) |
489 |
|
|
ej0 = create_migration(edge->rbfv[0], tvert[1]); |
490 |
|
|
else |
491 |
|
|
ej0 = create_migration(tvert[1], edge->rbfv[0]); |
492 |
|
|
if (tvert[1]->ord > edge->rbfv[1]->ord) |
493 |
|
|
ej1 = create_migration(edge->rbfv[1], tvert[1]); |
494 |
|
|
else |
495 |
|
|
ej1 = create_migration(tvert[1], edge->rbfv[1]); |
496 |
|
|
mesh_from_edge(ej0); |
497 |
|
|
mesh_from_edge(ej1); |
498 |
|
|
} |
499 |
|
|
} |
500 |
|
|
} |
501 |
greg |
2.15 |
|
502 |
|
|
/* Add normal direction if missing */ |
503 |
|
|
static void |
504 |
|
|
check_normal_incidence(void) |
505 |
|
|
{ |
506 |
greg |
2.16 |
static const FVECT norm_vec = {.0, .0, 1.}; |
507 |
|
|
const int saved_nprocs = nprocs; |
508 |
|
|
RBFNODE *near_rbf, *mir_rbf, *rbf; |
509 |
|
|
double bestd; |
510 |
|
|
int n; |
511 |
greg |
2.15 |
|
512 |
|
|
if (dsf_list == NULL) |
513 |
|
|
return; /* XXX should be error? */ |
514 |
|
|
near_rbf = dsf_list; |
515 |
|
|
bestd = input_orient*near_rbf->invec[2]; |
516 |
|
|
if (single_plane_incident) { /* ordered plane incidence? */ |
517 |
|
|
if (bestd >= 1.-2.*FTINY) |
518 |
|
|
return; /* already have normal */ |
519 |
|
|
} else { |
520 |
|
|
switch (inp_coverage) { |
521 |
|
|
case INP_QUAD1: |
522 |
|
|
case INP_QUAD2: |
523 |
|
|
case INP_QUAD3: |
524 |
|
|
case INP_QUAD4: |
525 |
|
|
break; /* quadrilateral symmetry? */ |
526 |
|
|
default: |
527 |
|
|
return; /* else we can interpolate */ |
528 |
|
|
} |
529 |
|
|
for (rbf = near_rbf->next; rbf != NULL; rbf = rbf->next) { |
530 |
|
|
const double d = input_orient*rbf->invec[2]; |
531 |
|
|
if (d >= 1.-2.*FTINY) |
532 |
|
|
return; /* seems we have normal */ |
533 |
|
|
if (d > bestd) { |
534 |
|
|
near_rbf = rbf; |
535 |
|
|
bestd = d; |
536 |
|
|
} |
537 |
|
|
} |
538 |
|
|
} |
539 |
|
|
if (mig_list != NULL) { /* need to be called first */ |
540 |
|
|
fprintf(stderr, "%s: Late call to check_normal_incidence()\n", |
541 |
|
|
progname); |
542 |
|
|
exit(1); |
543 |
|
|
} |
544 |
|
|
#ifdef DEBUG |
545 |
|
|
fprintf(stderr, "Interpolating normal incidence by mirroring (%.1f,%.1f)\n", |
546 |
|
|
get_theta180(near_rbf->invec), get_phi360(near_rbf->invec)); |
547 |
|
|
#endif |
548 |
|
|
/* mirror nearest incidence */ |
549 |
|
|
n = sizeof(RBFNODE) + sizeof(RBFVAL)*(near_rbf->nrbf-1); |
550 |
|
|
mir_rbf = (RBFNODE *)malloc(n); |
551 |
|
|
if (mir_rbf == NULL) |
552 |
|
|
goto memerr; |
553 |
|
|
memcpy(mir_rbf, near_rbf, n); |
554 |
|
|
mir_rbf->ord = near_rbf->ord - 1; /* not used, I think */ |
555 |
|
|
mir_rbf->next = NULL; |
556 |
|
|
rev_rbf_symmetry(mir_rbf, MIRROR_X|MIRROR_Y); |
557 |
|
|
nprocs = 1; /* compute migration matrix */ |
558 |
|
|
if (mig_list != create_migration(mir_rbf, near_rbf)) |
559 |
|
|
exit(1); /* XXX should never happen! */ |
560 |
greg |
2.16 |
/* interpolate normal dist. */ |
561 |
|
|
rbf = e_advect_rbf(mig_list, norm_vec, 2*near_rbf->nrbf); |
562 |
greg |
2.15 |
nprocs = saved_nprocs; /* final clean-up */ |
563 |
|
|
free(mir_rbf); |
564 |
|
|
free(mig_list); |
565 |
|
|
mig_list = near_rbf->ejl = NULL; |
566 |
|
|
insert_dsf(rbf); /* insert interpolated normal */ |
567 |
|
|
return; |
568 |
|
|
memerr: |
569 |
|
|
fprintf(stderr, "%s: Out of memory in check_normal_incidence()\n", |
570 |
|
|
progname); |
571 |
|
|
exit(1); |
572 |
|
|
} |
573 |
greg |
2.1 |
|
574 |
|
|
/* Build our triangle mesh from recorded RBFs */ |
575 |
|
|
void |
576 |
|
|
build_mesh(void) |
577 |
|
|
{ |
578 |
|
|
double best2 = M_PI*M_PI; |
579 |
|
|
RBFNODE *shrt_edj[2]; |
580 |
|
|
RBFNODE *rbf0, *rbf1; |
581 |
greg |
2.15 |
/* add normal if needed */ |
582 |
|
|
check_normal_incidence(); |
583 |
greg |
2.1 |
/* check if isotropic */ |
584 |
|
|
if (single_plane_incident) { |
585 |
|
|
for (rbf0 = dsf_list; rbf0 != NULL; rbf0 = rbf0->next) |
586 |
|
|
if (rbf0->next != NULL) |
587 |
|
|
create_migration(rbf0, rbf0->next); |
588 |
|
|
await_children(nchild); |
589 |
|
|
return; |
590 |
|
|
} |
591 |
|
|
shrt_edj[0] = shrt_edj[1] = NULL; /* start w/ shortest edge */ |
592 |
|
|
for (rbf0 = dsf_list; rbf0 != NULL; rbf0 = rbf0->next) |
593 |
|
|
for (rbf1 = rbf0->next; rbf1 != NULL; rbf1 = rbf1->next) { |
594 |
|
|
double dist2 = 2. - 2.*DOT(rbf0->invec,rbf1->invec); |
595 |
|
|
if (dist2 < best2) { |
596 |
|
|
shrt_edj[0] = rbf0; |
597 |
|
|
shrt_edj[1] = rbf1; |
598 |
|
|
best2 = dist2; |
599 |
|
|
} |
600 |
|
|
} |
601 |
|
|
if (shrt_edj[0] == NULL) { |
602 |
|
|
fprintf(stderr, "%s: Cannot find shortest edge\n", progname); |
603 |
|
|
exit(1); |
604 |
|
|
} |
605 |
|
|
/* build mesh from this edge */ |
606 |
|
|
if (shrt_edj[0]->ord < shrt_edj[1]->ord) |
607 |
|
|
mesh_from_edge(create_migration(shrt_edj[0], shrt_edj[1])); |
608 |
|
|
else |
609 |
|
|
mesh_from_edge(create_migration(shrt_edj[1], shrt_edj[0])); |
610 |
|
|
/* complete migrations */ |
611 |
|
|
await_children(nchild); |
612 |
|
|
} |