ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfmesh.c
Revision: 2.22
Committed: Mon Mar 10 20:58:29 2014 UTC (10 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.21: +6 -2 lines
Log Message:
Fixed evaluation order bug -- first time I've done that!

File Contents

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