ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfmesh.c
Revision: 2.17
Committed: Wed Mar 5 22:47:16 2014 UTC (10 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.16: +35 -20 lines
Log Message:
Failed attempt to improve results by prioritizing displacement of large lobes

File Contents

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