ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfmesh.c
Revision: 2.30
Committed: Thu Aug 21 10:33:48 2014 UTC (9 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.29: +91 -1 lines
Log Message:
Added grazing angle extrapolation to BSDF interpolation

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.30 static const char RCSid[] = "$Id: bsdfmesh.c,v 2.29 2014/03/27 03:49:14 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 greg 2.19
22     #ifndef NEIGH_FACT2
23 greg 2.21 #define NEIGH_FACT2 0.1 /* empirical neighborhood distance weight */
24 greg 2.19 #endif
25 greg 2.1 /* number of processes to run */
26     int nprocs = 1;
27     /* number of children (-1 in child) */
28     static int nchild = 0;
29    
30 greg 2.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) * output_orient*tvec[2];
55     ++n;
56     }
57     if (n < 2) /* should never happen! */
58     return(sum);
59     return(sum/(double)n);
60     }
61    
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.03;
67     const double DSFtarget = 0.60653066 * eval_rbfrep(rbf,outvec)
68     * output_orient*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     /* interpolation search */
76     while (outside_rad-inside_rad > rad_epsilon) {
77     double test_rad = interp_rad;
78     double DSFtest = eval_DSFsurround(rbf, outvec, test_rad);
79     if (DSFtarget < DSFtest) {
80     inside_rad = test_rad;
81     DSFinside = DSFtest;
82     } else {
83     outside_rad = test_rad;
84     DSFoutside = DSFtest;
85     }
86     }
87     return(interp_rad);
88     #undef interp_rad
89     }
90    
91     /* Compute average BSDF peak from current DSF's */
92     static void
93     comp_bsdf_spec(void)
94     {
95     double peak_sum = 0;
96     double rad_sum = 0;
97     int n = 0;
98     RBFNODE *rbf;
99     FVECT sdv;
100    
101     if (dsf_list == NULL) {
102     bsdf_spec_peak = 0;
103     bsdf_spec_crad = 0;
104     return;
105     }
106     for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) {
107     sdv[0] = -rbf->invec[0];
108     sdv[1] = -rbf->invec[1];
109     sdv[2] = rbf->invec[2]*(2*(input_orient==output_orient) - 1);
110     peak_sum += eval_rbfrep(rbf, sdv);
111     rad_sum += est_DSFrad(rbf, sdv);
112     ++n;
113     }
114     bsdf_spec_peak = peak_sum/(double)n;
115     bsdf_spec_crad = ANG2R( rad_sum/(double)n );
116     }
117    
118 greg 2.2 /* Create a new migration holder (sharing memory for multiprocessing) */
119     static MIGRATION *
120     new_migration(RBFNODE *from_rbf, RBFNODE *to_rbf)
121     {
122     size_t memlen = sizeof(MIGRATION) +
123     sizeof(float)*(from_rbf->nrbf*to_rbf->nrbf - 1);
124     MIGRATION *newmig;
125     #ifdef _WIN32
126     if (nprocs > 1)
127     fprintf(stderr, "%s: warning - multiprocessing not supported\n",
128     progname);
129     nprocs = 1;
130     newmig = (MIGRATION *)malloc(memlen);
131     #else
132     if (nprocs <= 1) { /* single process? */
133     newmig = (MIGRATION *)malloc(memlen);
134     } else { /* else need to share memory */
135     newmig = (MIGRATION *)mmap(NULL, memlen, PROT_READ|PROT_WRITE,
136     MAP_ANON|MAP_SHARED, -1, 0);
137     if ((void *)newmig == MAP_FAILED)
138     newmig = NULL;
139     }
140     #endif
141     if (newmig == NULL) {
142     fprintf(stderr, "%s: cannot allocate new migration\n", progname);
143     exit(1);
144     }
145     newmig->rbfv[0] = from_rbf;
146     newmig->rbfv[1] = to_rbf;
147     /* insert in edge lists */
148     newmig->enxt[0] = from_rbf->ejl;
149     from_rbf->ejl = newmig;
150     newmig->enxt[1] = to_rbf->ejl;
151     to_rbf->ejl = newmig;
152     newmig->next = mig_list; /* push onto global list */
153     return(mig_list = newmig);
154     }
155    
156     #ifdef _WIN32
157     #define await_children(n) (void)(n)
158     #define run_subprocess() 0
159     #define end_subprocess() (void)0
160     #else
161    
162     /* Wait for the specified number of child processes to complete */
163     static void
164     await_children(int n)
165     {
166     int exit_status = 0;
167    
168     if (n > nchild)
169     n = nchild;
170     while (n-- > 0) {
171     int status;
172     if (wait(&status) < 0) {
173     fprintf(stderr, "%s: missing child(ren)!\n", progname);
174     nchild = 0;
175     break;
176     }
177     --nchild;
178     if (status) { /* something wrong */
179     if ((status = WEXITSTATUS(status)))
180     exit_status = status;
181     else
182     exit_status += !exit_status;
183     fprintf(stderr, "%s: subprocess died\n", progname);
184     n = nchild; /* wait for the rest */
185     }
186     }
187     if (exit_status)
188     exit(exit_status);
189     }
190    
191     /* Start child process if multiprocessing selected */
192     static pid_t
193     run_subprocess(void)
194     {
195     int status;
196     pid_t pid;
197    
198     if (nprocs <= 1) /* any children requested? */
199     return(0);
200     await_children(nchild + 1 - nprocs); /* free up child process */
201     if ((pid = fork())) {
202     if (pid < 0) {
203     fprintf(stderr, "%s: cannot fork subprocess\n",
204     progname);
205 greg 2.6 await_children(nchild);
206 greg 2.2 exit(1);
207     }
208     ++nchild; /* subprocess started */
209     return(pid);
210     }
211     nchild = -1;
212     return(0); /* put child to work */
213     }
214    
215     /* If we are in subprocess, call exit */
216     #define end_subprocess() if (nchild < 0) _exit(0); else
217    
218     #endif /* ! _WIN32 */
219    
220 greg 2.19 /* Compute normalized distribution scattering functions for comparison */
221     static void
222     compute_nDSFs(const RBFNODE *rbf0, const RBFNODE *rbf1)
223     {
224     const double nf0 = (GRIDRES*GRIDRES) / rbf0->vtotal;
225     const double nf1 = (GRIDRES*GRIDRES) / rbf1->vtotal;
226     int x, y;
227     FVECT dv;
228    
229     for (x = GRIDRES; x--; )
230     for (y = GRIDRES; y--; ) {
231 greg 2.20 ovec_from_pos(dv, x, y); /* cube root (brightness) */
232     dsf_grid[x][y].val[0] = pow(nf0*eval_rbfrep(rbf0, dv), .3333);
233     dsf_grid[x][y].val[1] = pow(nf1*eval_rbfrep(rbf1, dv), .3333);
234 greg 2.19 }
235     }
236    
237     /* Compute neighborhood distance-squared (dissimilarity) */
238     static double
239     neighborhood_dist2(int x0, int y0, int x1, int y1)
240     {
241     int rad = GRIDRES>>5;
242     double sum2 = 0.;
243     double d;
244     int p[4];
245     int i, j;
246     /* check radius */
247     p[0] = x0; p[1] = y0; p[2] = x1; p[3] = y1;
248     for (i = 4; i--; ) {
249     if (p[i] < rad) rad = p[i];
250     if (GRIDRES-1-p[i] < rad) rad = GRIDRES-1-p[i];
251     }
252     for (i = -rad; i <= rad; i++)
253     for (j = -rad; j <= rad; j++) {
254     d = dsf_grid[x0+i][y0+j].val[0] -
255     dsf_grid[x1+i][y1+j].val[1];
256     sum2 += d*d;
257     }
258     return(sum2 / (4*rad*(rad+1) + 1));
259     }
260    
261 greg 2.27 /* Compute distance between two RBF lobes */
262     double
263     lobe_distance(RBFVAL *rbf1, RBFVAL *rbf2)
264     {
265     FVECT vfrom, vto;
266     double d, res;
267     /* quadratic cost function */
268     ovec_from_pos(vfrom, rbf1->gx, rbf1->gy);
269     ovec_from_pos(vto, rbf2->gx, rbf2->gy);
270     d = Acos(DOT(vfrom, vto));
271     res = d*d;
272     d = R2ANG(rbf2->crad) - R2ANG(rbf1->crad);
273     res += d*d;
274     /* neighborhood difference */
275     res += NEIGH_FACT2 * neighborhood_dist2( rbf1->gx, rbf1->gy,
276     rbf2->gx, rbf2->gy );
277     return(res);
278 greg 2.1 }
279    
280 greg 2.26
281 greg 2.1 /* Compute and insert migration along directed edge (may fork child) */
282     static MIGRATION *
283     create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf)
284     {
285     MIGRATION *newmig;
286 greg 2.6 int i, j;
287 greg 2.1 /* check if exists already */
288     for (newmig = from_rbf->ejl; newmig != NULL;
289     newmig = nextedge(from_rbf,newmig))
290     if (newmig->rbfv[1] == to_rbf)
291     return(NULL);
292     /* else allocate */
293 greg 2.7 #ifdef DEBUG
294 greg 2.14 fprintf(stderr, "Building path from (theta,phi) (%.1f,%.1f) ",
295 greg 2.7 get_theta180(from_rbf->invec),
296     get_phi360(from_rbf->invec));
297 greg 2.14 fprintf(stderr, "to (%.1f,%.1f) with %d x %d matrix\n",
298 greg 2.7 get_theta180(to_rbf->invec),
299     get_phi360(to_rbf->invec),
300     from_rbf->nrbf, to_rbf->nrbf);
301     #endif
302 greg 2.1 newmig = new_migration(from_rbf, to_rbf);
303     if (run_subprocess())
304     return(newmig); /* child continues */
305 greg 2.27
306     /* compute transport plan */
307     compute_nDSFs(from_rbf, to_rbf);
308     plan_transport(newmig);
309 greg 2.6
310 greg 2.1 for (i = from_rbf->nrbf; i--; ) { /* normalize final matrix */
311 greg 2.6 double nf = rbf_volume(&from_rbf->rbfa[i]);
312 greg 2.1 if (nf <= FTINY) continue;
313     nf = from_rbf->vtotal / nf;
314     for (j = to_rbf->nrbf; j--; )
315 greg 2.6 mtx_coef(newmig,i,j) *= nf; /* row now sums to 1.0 */
316 greg 2.1 }
317     end_subprocess(); /* exit here if subprocess */
318     return(newmig);
319     }
320    
321     /* Check if prospective vertex would create overlapping triangle */
322     static int
323     overlaps_tri(const RBFNODE *bv0, const RBFNODE *bv1, const RBFNODE *pv)
324     {
325     const MIGRATION *ej;
326     RBFNODE *vother[2];
327     int im_rev;
328     /* find shared edge in mesh */
329     for (ej = pv->ejl; ej != NULL; ej = nextedge(pv,ej)) {
330     const RBFNODE *tv = opp_rbf(pv,ej);
331     if (tv == bv0) {
332     im_rev = is_rev_tri(ej->rbfv[0]->invec,
333     ej->rbfv[1]->invec, bv1->invec);
334     break;
335     }
336     if (tv == bv1) {
337     im_rev = is_rev_tri(ej->rbfv[0]->invec,
338     ej->rbfv[1]->invec, bv0->invec);
339     break;
340     }
341     }
342     if (!get_triangles(vother, ej)) /* triangle on same side? */
343     return(0);
344     return(vother[im_rev] != NULL);
345     }
346    
347 greg 2.14 /* Find convex hull vertex to complete triangle (oriented call) */
348 greg 2.1 static RBFNODE *
349     find_chull_vert(const RBFNODE *rbf0, const RBFNODE *rbf1)
350     {
351     FVECT vmid, vejn, vp;
352     RBFNODE *rbf, *rbfbest = NULL;
353     double dprod, area2, bestarea2 = FHUGE, bestdprod = -.5;
354    
355     VSUB(vejn, rbf1->invec, rbf0->invec);
356     VADD(vmid, rbf0->invec, rbf1->invec);
357     if (normalize(vejn) == 0 || normalize(vmid) == 0)
358     return(NULL);
359     /* XXX exhaustive search */
360     /* Find triangle with minimum rotation from perpendicular */
361     for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) {
362     if ((rbf == rbf0) | (rbf == rbf1))
363     continue;
364     tri_orient(vp, rbf0->invec, rbf1->invec, rbf->invec);
365     if (DOT(vp, vmid) <= FTINY)
366     continue; /* wrong orientation */
367     area2 = .25*DOT(vp,vp);
368 greg 2.14 VSUB(vp, rbf->invec, vmid);
369 greg 2.1 dprod = -DOT(vp, vejn);
370     VSUM(vp, vp, vejn, dprod); /* above guarantees non-zero */
371     dprod = DOT(vp, vmid) / VLEN(vp);
372     if (dprod <= bestdprod + FTINY*(1 - 2*(area2 < bestarea2)))
373     continue; /* found better already */
374     if (overlaps_tri(rbf0, rbf1, rbf))
375     continue; /* overlaps another triangle */
376     rbfbest = rbf;
377     bestdprod = dprod; /* new one to beat */
378     bestarea2 = area2;
379     }
380     return(rbfbest);
381     }
382    
383     /* Create new migration edge and grow mesh recursively around it */
384     static void
385     mesh_from_edge(MIGRATION *edge)
386     {
387     MIGRATION *ej0, *ej1;
388     RBFNODE *tvert[2];
389    
390     if (edge == NULL)
391     return;
392     /* triangle on either side? */
393     get_triangles(tvert, edge);
394     if (tvert[0] == NULL) { /* grow mesh on right */
395     tvert[0] = find_chull_vert(edge->rbfv[0], edge->rbfv[1]);
396     if (tvert[0] != NULL) {
397     if (tvert[0]->ord > edge->rbfv[0]->ord)
398     ej0 = create_migration(edge->rbfv[0], tvert[0]);
399     else
400     ej0 = create_migration(tvert[0], edge->rbfv[0]);
401     if (tvert[0]->ord > edge->rbfv[1]->ord)
402     ej1 = create_migration(edge->rbfv[1], tvert[0]);
403     else
404     ej1 = create_migration(tvert[0], edge->rbfv[1]);
405     mesh_from_edge(ej0);
406     mesh_from_edge(ej1);
407 greg 2.28 return;
408 greg 2.1 }
409 greg 2.28 }
410     if (tvert[1] == NULL) { /* grow mesh on left */
411 greg 2.1 tvert[1] = find_chull_vert(edge->rbfv[1], edge->rbfv[0]);
412     if (tvert[1] != NULL) {
413     if (tvert[1]->ord > edge->rbfv[0]->ord)
414     ej0 = create_migration(edge->rbfv[0], tvert[1]);
415     else
416     ej0 = create_migration(tvert[1], edge->rbfv[0]);
417     if (tvert[1]->ord > edge->rbfv[1]->ord)
418     ej1 = create_migration(edge->rbfv[1], tvert[1]);
419     else
420     ej1 = create_migration(tvert[1], edge->rbfv[1]);
421     mesh_from_edge(ej0);
422     mesh_from_edge(ej1);
423     }
424     }
425     }
426 greg 2.15
427     /* Add normal direction if missing */
428     static void
429     check_normal_incidence(void)
430     {
431 greg 2.25 static FVECT norm_vec = {.0, .0, 1.};
432 greg 2.16 const int saved_nprocs = nprocs;
433     RBFNODE *near_rbf, *mir_rbf, *rbf;
434     double bestd;
435     int n;
436 greg 2.15
437     if (dsf_list == NULL)
438     return; /* XXX should be error? */
439     near_rbf = dsf_list;
440     bestd = input_orient*near_rbf->invec[2];
441     if (single_plane_incident) { /* ordered plane incidence? */
442     if (bestd >= 1.-2.*FTINY)
443     return; /* already have normal */
444     } else {
445     switch (inp_coverage) {
446     case INP_QUAD1:
447     case INP_QUAD2:
448     case INP_QUAD3:
449     case INP_QUAD4:
450     break; /* quadrilateral symmetry? */
451     default:
452     return; /* else we can interpolate */
453     }
454     for (rbf = near_rbf->next; rbf != NULL; rbf = rbf->next) {
455     const double d = input_orient*rbf->invec[2];
456     if (d >= 1.-2.*FTINY)
457     return; /* seems we have normal */
458     if (d > bestd) {
459     near_rbf = rbf;
460     bestd = d;
461     }
462     }
463     }
464     if (mig_list != NULL) { /* need to be called first */
465     fprintf(stderr, "%s: Late call to check_normal_incidence()\n",
466     progname);
467     exit(1);
468     }
469     #ifdef DEBUG
470     fprintf(stderr, "Interpolating normal incidence by mirroring (%.1f,%.1f)\n",
471     get_theta180(near_rbf->invec), get_phi360(near_rbf->invec));
472     #endif
473     /* mirror nearest incidence */
474     n = sizeof(RBFNODE) + sizeof(RBFVAL)*(near_rbf->nrbf-1);
475     mir_rbf = (RBFNODE *)malloc(n);
476     if (mir_rbf == NULL)
477     goto memerr;
478     memcpy(mir_rbf, near_rbf, n);
479     mir_rbf->ord = near_rbf->ord - 1; /* not used, I think */
480     mir_rbf->next = NULL;
481 greg 2.22 mir_rbf->ejl = NULL;
482 greg 2.15 rev_rbf_symmetry(mir_rbf, MIRROR_X|MIRROR_Y);
483     nprocs = 1; /* compute migration matrix */
484 greg 2.22 if (create_migration(mir_rbf, near_rbf) == NULL)
485 greg 2.15 exit(1); /* XXX should never happen! */
486 greg 2.25 norm_vec[2] = input_orient; /* interpolate normal dist. */
487 greg 2.29 rbf = e_advect_rbf(mig_list, norm_vec, 0);
488 greg 2.15 nprocs = saved_nprocs; /* final clean-up */
489     free(mir_rbf);
490     free(mig_list);
491     mig_list = near_rbf->ejl = NULL;
492     insert_dsf(rbf); /* insert interpolated normal */
493     return;
494     memerr:
495     fprintf(stderr, "%s: Out of memory in check_normal_incidence()\n",
496     progname);
497     exit(1);
498     }
499 greg 2.1
500     /* Build our triangle mesh from recorded RBFs */
501     void
502     build_mesh(void)
503     {
504     double best2 = M_PI*M_PI;
505     RBFNODE *shrt_edj[2];
506     RBFNODE *rbf0, *rbf1;
507 greg 2.30 /* average specular peak */
508     comp_bsdf_spec();
509 greg 2.15 /* add normal if needed */
510     check_normal_incidence();
511 greg 2.1 /* check if isotropic */
512     if (single_plane_incident) {
513     for (rbf0 = dsf_list; rbf0 != NULL; rbf0 = rbf0->next)
514     if (rbf0->next != NULL)
515     create_migration(rbf0, rbf0->next);
516     await_children(nchild);
517     return;
518     }
519     shrt_edj[0] = shrt_edj[1] = NULL; /* start w/ shortest edge */
520     for (rbf0 = dsf_list; rbf0 != NULL; rbf0 = rbf0->next)
521     for (rbf1 = rbf0->next; rbf1 != NULL; rbf1 = rbf1->next) {
522     double dist2 = 2. - 2.*DOT(rbf0->invec,rbf1->invec);
523     if (dist2 < best2) {
524     shrt_edj[0] = rbf0;
525     shrt_edj[1] = rbf1;
526     best2 = dist2;
527     }
528     }
529     if (shrt_edj[0] == NULL) {
530     fprintf(stderr, "%s: Cannot find shortest edge\n", progname);
531     exit(1);
532     }
533     /* build mesh from this edge */
534     if (shrt_edj[0]->ord < shrt_edj[1]->ord)
535     mesh_from_edge(create_migration(shrt_edj[0], shrt_edj[1]));
536     else
537     mesh_from_edge(create_migration(shrt_edj[1], shrt_edj[0]));
538     /* complete migrations */
539     await_children(nchild);
540     }