ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/interp2d.c
Revision: 2.11
Committed: Fri Feb 15 01:26:47 2013 UTC (11 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.10: +88 -48 lines
Log Message:
Additional tweaks -- now 11 times faster than original version

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.11 static const char RCSid[] = "$Id: interp2d.c,v 2.10 2013/02/14 19:57:10 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * General interpolation method for unstructured values on 2-D plane.
6     *
7     * G.Ward Feb 2013
8     */
9    
10     #include "copyright.h"
11    
12 greg 2.4 /***************************************************************
13 greg 2.1 * This is a general method for 2-D interpolation similar to
14     * radial basis functions but allowing for a good deal of local
15     * anisotropy in the point distribution. Each sample point
16     * is examined to determine the closest neighboring samples in
17     * each of NI2DIR surrounding directions. To speed this
18 greg 2.4 * calculation, we sort the data into half-planes and apply
19     * simple tests to see which neighbor is closest in each
20 greg 2.7 * angular slice. Once we have our approximate neighborhood
21 greg 2.3 * for a sample, we can use it in a modified Gaussian weighting
22 greg 2.4 * with allowing local anisotropy. Harmonic weighting is added
23 greg 2.3 * to reduce the influence of distant neighbors. This yields a
24     * smooth interpolation regardless of how the sample points are
25 greg 2.4 * initially distributed. Evaluation is accelerated by use of
26 greg 2.10 * a fast approximation to the atan2(y,x) function and an array
27     * of flags indicating where weights are (nearly) zero.
28 greg 2.4 ****************************************************************/
29 greg 2.1
30     #include <stdio.h>
31     #include <stdlib.h>
32     #include "rtmath.h"
33     #include "interp2d.h"
34    
35 greg 2.4 #define DECODE_DIA(ip,ed) ((ip)->dmin*(1. + .5*(ed)))
36     #define ENCODE_DIA(ip,d) ((int)(2.*(d)/(ip)->dmin) - 2)
37 greg 2.1
38     /* Sample order (private) */
39     typedef struct {
40     int si; /* sample index */
41     float dm; /* distance measure in this direction */
42     } SAMPORD;
43    
44 greg 2.2 /* Allocate a new set of interpolation samples (caller assigns spt[] array) */
45 greg 2.1 INTERP2 *
46     interp2_alloc(int nsamps)
47     {
48     INTERP2 *nip;
49    
50     if (nsamps <= 1)
51     return(NULL);
52    
53     nip = (INTERP2 *)malloc(sizeof(INTERP2) + sizeof(float)*2*(nsamps-1));
54     if (nip == NULL)
55     return(NULL);
56    
57     nip->ns = nsamps;
58 greg 2.4 nip->dmin = 1; /* default minimum diameter */
59 greg 2.1 nip->smf = NI2DSMF; /* default smoothing factor */
60 greg 2.4 nip->da = NULL;
61 greg 2.1 /* caller must assign spt[] array */
62     return(nip);
63     }
64    
65 greg 2.2 /* Resize interpolation array (caller must assign any new values) */
66     INTERP2 *
67     interp2_realloc(INTERP2 *ip, int nsamps)
68     {
69     if (ip == NULL)
70     return(interp2_alloc(nsamps));
71     if (nsamps <= 1) {
72     interp2_free(ip);
73     return(NULL);
74     }
75 greg 2.8 if (nsamps == ip->ns)
76 greg 2.2 return(ip);
77 greg 2.4 if (ip->da != NULL) { /* will need to recompute distribution */
78     free(ip->da);
79     ip->da = NULL;
80 greg 2.2 }
81     ip = (INTERP2 *)realloc(ip, sizeof(INTERP2)+sizeof(float)*2*(nsamps-1));
82     if (ip == NULL)
83     return(NULL);
84     ip->ns = nsamps;
85     return(ip);
86     }
87    
88 greg 2.5 /* Set minimum distance under which samples will start to merge */
89     void
90     interp2_spacing(INTERP2 *ip, double mind)
91     {
92     if (mind <= 0)
93     return;
94 greg 2.7 if ((.998*ip->dmin <= mind) & (mind <= 1.002*ip->dmin))
95 greg 2.5 return;
96     if (ip->da != NULL) { /* will need to recompute distribution */
97     free(ip->da);
98     ip->da = NULL;
99     }
100     ip->dmin = mind;
101     }
102    
103     /* Modify smoothing parameter by the given factor */
104     void
105     interp2_smooth(INTERP2 *ip, double sf)
106     {
107     if ((ip->smf *= sf) < NI2DSMF)
108     ip->smf = NI2DSMF;
109     }
110    
111 greg 2.1 /* private call-back to sort position index */
112     static int
113     cmp_spos(const void *p1, const void *p2)
114     {
115     const SAMPORD *so1 = (const SAMPORD *)p1;
116     const SAMPORD *so2 = (const SAMPORD *)p2;
117    
118     if (so1->dm > so2->dm)
119     return 1;
120     if (so1->dm < so2->dm)
121     return -1;
122     return 0;
123     }
124    
125 greg 2.2 /* private routine to order samples in a particular direction */
126     static void
127     sort_samples(SAMPORD *sord, const INTERP2 *ip, double ang)
128     {
129     const double cosd = cos(ang);
130     const double sind = sin(ang);
131     int i;
132    
133     for (i = ip->ns; i--; ) {
134     sord[i].si = i;
135     sord[i].dm = cosd*ip->spt[i][0] + sind*ip->spt[i][1];
136     }
137     qsort(sord, ip->ns, sizeof(SAMPORD), &cmp_spos);
138     }
139    
140 greg 2.4 /* private routine to encode sample diameter with range checks */
141 greg 2.1 static int
142 greg 2.4 encode_diameter(const INTERP2 *ip, double d)
143 greg 2.1 {
144 greg 2.4 const int ed = ENCODE_DIA(ip, d);
145 greg 2.1
146 greg 2.4 if (ed <= 0)
147 greg 2.1 return(0);
148 greg 2.4 if (ed >= 0xffff)
149 greg 2.1 return(0xffff);
150 greg 2.4 return(ed);
151 greg 2.1 }
152    
153 greg 2.2 /* (Re)compute anisotropic basis function interpolant (normally automatic) */
154     int
155     interp2_analyze(INTERP2 *ip)
156 greg 2.1 {
157     SAMPORD *sortord;
158 greg 2.2 int *rightrndx, *leftrndx, *endrndx;
159 greg 2.10 int i, bd;
160 greg 2.1 /* sanity checks */
161 greg 2.10 if (ip == NULL)
162     return(0);
163     if (ip->da != NULL) { /* free previous data if any */
164     free(ip->da);
165     ip->da = NULL;
166     }
167     if ((ip->ns <= 1) | (ip->dmin <= 0))
168 greg 2.1 return(0);
169 greg 2.10 /* compute sample domain */
170     ip->smin[0] = ip->smin[1] = FHUGE;
171 greg 2.11 ip->smax[0] = ip->smax[1] = -FHUGE;
172 greg 2.10 for (i = ip->ns; i--; ) {
173     if (ip->spt[i][0] < ip->smin[0])
174     ip->smin[0] = ip->spt[i][0];
175 greg 2.11 if (ip->spt[i][0] > ip->smax[0])
176     ip->smax[0] = ip->spt[i][0];
177 greg 2.10 if (ip->spt[i][1] < ip->smin[1])
178     ip->smin[1] = ip->spt[i][1];
179 greg 2.11 if (ip->spt[i][1] > ip->smax[1])
180     ip->smax[1] = ip->spt[i][1];
181 greg 2.1 }
182 greg 2.11 ip->grid2 = ((ip->smax[0]-ip->smin[0])*(ip->smax[0]-ip->smin[0]) +
183     (ip->smax[1]-ip->smin[1])*(ip->smax[1]-ip->smin[1])) *
184     (1./NI2DIM/NI2DIM);
185 greg 2.10 if (ip->grid2 <= FTINY*ip->dmin*ip->dmin)
186     return(0);
187     /* allocate analysis data */
188     ip->da = (struct interp2_samp *)calloc( ip->ns,
189     sizeof(struct interp2_samp) );
190     if (ip->da == NULL)
191     return(0);
192 greg 2.1 /* get temporary arrays */
193     sortord = (SAMPORD *)malloc(sizeof(SAMPORD)*ip->ns);
194     rightrndx = (int *)malloc(sizeof(int)*ip->ns);
195     leftrndx = (int *)malloc(sizeof(int)*ip->ns);
196 greg 2.2 endrndx = (int *)malloc(sizeof(int)*ip->ns);
197     if ((sortord == NULL) | (rightrndx == NULL) |
198     (leftrndx == NULL) | (endrndx == NULL))
199 greg 2.1 return(0);
200     /* run through bidirections */
201     for (bd = 0; bd < NI2DIR/2; bd++) {
202     const double ang = 2.*PI/NI2DIR*bd;
203 greg 2.2 int *sptr;
204 greg 2.1 /* create right reverse index */
205 greg 2.2 if (bd) { /* re-use from previous iteration? */
206     sptr = rightrndx;
207 greg 2.1 rightrndx = leftrndx;
208     leftrndx = sptr;
209 greg 2.2 } else { /* else sort first half-plane */
210     sort_samples(sortord, ip, PI/2. - PI/NI2DIR);
211     for (i = ip->ns; i--; )
212 greg 2.1 rightrndx[sortord[i].si] = i;
213 greg 2.2 /* & store reverse order for later */
214     for (i = ip->ns; i--; )
215     endrndx[sortord[i].si] = ip->ns-1 - i;
216 greg 2.1 }
217     /* create new left reverse index */
218 greg 2.2 if (bd == NI2DIR/2 - 1) { /* use order from first iteration? */
219     sptr = leftrndx;
220     leftrndx = endrndx;
221     endrndx = sptr;
222     } else { /* else compute new half-plane */
223     sort_samples(sortord, ip, ang + (PI/2. + PI/NI2DIR));
224     for (i = ip->ns; i--; )
225     leftrndx[sortord[i].si] = i;
226 greg 2.1 }
227     /* sort grid values in this direction */
228 greg 2.2 sort_samples(sortord, ip, ang);
229 greg 2.1 /* find nearest neighbors each side */
230 greg 2.2 for (i = ip->ns; i--; ) {
231 greg 2.3 const int ii = sortord[i].si;
232 greg 2.1 int j;
233 greg 2.3 /* preload with large radii */
234 greg 2.10 ip->da[ii].dia[bd] =
235     ip->da[ii].dia[bd+NI2DIR/2] = encode_diameter(ip,
236     .5*(sortord[ip->ns-1].dm - sortord[0].dm));
237 greg 2.1 for (j = i; ++j < ip->ns; ) /* nearest above */
238 greg 2.3 if (rightrndx[sortord[j].si] > rightrndx[ii] &&
239     leftrndx[sortord[j].si] < leftrndx[ii]) {
240 greg 2.10 ip->da[ii].dia[bd] = encode_diameter(ip,
241 greg 2.4 sortord[j].dm - sortord[i].dm);
242 greg 2.1 break;
243     }
244     for (j = i; j-- > 0; ) /* nearest below */
245 greg 2.3 if (rightrndx[sortord[j].si] < rightrndx[ii] &&
246     leftrndx[sortord[j].si] > leftrndx[ii]) {
247 greg 2.10 ip->da[ii].dia[bd+NI2DIR/2] = encode_diameter(ip,
248 greg 2.4 sortord[i].dm - sortord[j].dm);
249 greg 2.1 break;
250     }
251     }
252     }
253     free(sortord); /* clean up */
254     free(rightrndx);
255     free(leftrndx);
256 greg 2.2 free(endrndx);
257 greg 2.1 return(1);
258     }
259    
260 greg 2.10 /* Compute unnormalized weight for a position relative to a sample */
261     double
262     interp2_wti(INTERP2 *ip, const int i, double x, double y)
263 greg 2.1 {
264 greg 2.11 double dir, rd, r2, d2;
265     int ri;
266     /* get relative direction */
267     x -= ip->spt[i][0];
268 greg 2.1 y -= ip->spt[i][1];
269 greg 2.11 dir = atan2a(y, x);
270 greg 2.1 dir += 2.*PI*(dir < 0);
271     /* linear radius interpolation */
272     rd = dir * (NI2DIR/2./PI);
273     ri = (int)rd;
274     rd -= (double)ri;
275 greg 2.10 rd = (1.-rd)*ip->da[i].dia[ri] + rd*ip->da[i].dia[(ri+1)%NI2DIR];
276 greg 2.4 rd = ip->smf * DECODE_DIA(ip, rd);
277 greg 2.6 r2 = 2.*rd*rd;
278 greg 2.11 d2 = x*x + y*y;
279     if (d2 > 21.*r2) /* result would be < 1e-9 */
280 greg 2.6 return(.0);
281 greg 2.3 /* Gaussian times harmonic weighting */
282 greg 2.6 return( exp(-d2/r2) * ip->dmin/(ip->dmin + sqrt(d2)) );
283 greg 2.1 }
284    
285 greg 2.11 /* private call to get grid flag index */
286     static int
287     interp2_flagpos(int fgi[2], INTERP2 *ip, double x, double y)
288     {
289     int ingrid = 1;
290    
291     if (ip == NULL) /* paranoia */
292     return(-1);
293     /* need to compute interpolant? */
294     if (ip->da == NULL && !interp2_analyze(ip))
295     return(-1);
296     /* get grid position */
297     fgi[0] = (x - ip->smin[0]) * NI2DIM / (ip->smax[0] - ip->smin[0]);
298     if (fgi[0] >= NI2DIM) {
299     fgi[0] = NI2DIM-1;
300     ingrid = 0;
301     } else if (fgi[0] < 0) {
302     fgi[0] = 0;
303     ingrid = 0;
304     }
305     fgi[1] = (y - ip->smin[1]) * NI2DIM / (ip->smax[1] - ip->smin[1]);
306     if (fgi[1] >= NI2DIM) {
307     fgi[1] = NI2DIM-1;
308     ingrid = 0;
309     } else if (fgi[1] < 0) {
310     fgi[1] = 0;
311     ingrid = 0;
312     }
313     return(ingrid);
314     }
315    
316     /* private call to set black flag if not too close to the given sample */
317     static void
318     setblk(INTERP2 *ip, const int i, const int gi[2])
319     {
320     double dx = (gi[0]+.5)*(1./NI2DIM)*(ip->smax[0] - ip->smin[0]) +
321     ip->smin[0] - ip->spt[i][0];
322     double dy = (gi[1]+.5)*(1./NI2DIM)*(ip->smax[1] - ip->smin[1]) +
323     ip->smin[1] - ip->spt[i][1];
324    
325     if (dx*dx + dy*dy > 2.*ip->grid2)
326     ip->da[i].blkflg[gi[1]] |= 1<<gi[0];
327     }
328    
329     #define chkblk(ip,i,gi) ((ip)->da[i].blkflg[(gi)[1]]>>(gi)[0] & 1)
330    
331 greg 2.1 /* Assign full set of normalized weights to interpolate the given position */
332     int
333     interp2_weights(float wtv[], INTERP2 *ip, double x, double y)
334     {
335     double wnorm;
336 greg 2.11 int fgi[2];
337     int ingrid;
338 greg 2.1 int i;
339    
340 greg 2.11 if (wtv == NULL)
341     return(0);
342     /* get flag position */
343     if ((ingrid = interp2_flagpos(fgi, ip, x, y)) < 0)
344 greg 2.1 return(0);
345    
346     wnorm = 0; /* compute raw weights */
347 greg 2.11 for (i = ip->ns; i--; )
348     if (chkblk(ip, i, fgi)) {
349     wtv[i] = 0;
350     } else {
351 greg 2.10 double wt = interp2_wti(ip, i, x, y);
352 greg 2.1 wtv[i] = wt;
353     wnorm += wt;
354 greg 2.11 if (wt <= 1e-9 && ingrid)
355     setblk(ip, i, fgi);
356     }
357 greg 2.1 if (wnorm <= 0) /* too far from all our samples! */
358     return(0);
359     wnorm = 1./wnorm; /* normalize weights */
360     for (i = ip->ns; i--; )
361     wtv[i] *= wnorm;
362     return(ip->ns); /* all done */
363     }
364    
365    
366     /* Get normalized weights and indexes for n best samples in descending order */
367     int
368     interp2_topsamp(float wt[], int si[], const int n, INTERP2 *ip, double x, double y)
369     {
370     int nn = 0;
371 greg 2.11 int fgi[2];
372     int ingrid;
373 greg 2.1 double wnorm;
374     int i, j;
375    
376 greg 2.11 if ((n <= 0) | (wt == NULL) | (si == NULL))
377     return(0);
378     /* get flag position */
379     if ((ingrid = interp2_flagpos(fgi, ip, x, y)) < 0)
380 greg 2.1 return(0);
381     /* identify top n weights */
382 greg 2.11 for (i = ip->ns; i--; )
383     if (!chkblk(ip, i, fgi)) {
384 greg 2.10 const double wti = interp2_wti(ip, i, x, y);
385 greg 2.11 if (wti <= 1e-9) {
386     if (ingrid)
387     setblk(ip, i, fgi);
388 greg 2.9 continue;
389 greg 2.11 }
390 greg 2.1 for (j = nn; j > 0; j--) {
391 greg 2.3 if (wt[j-1] >= wti)
392 greg 2.1 break;
393     if (j < n) {
394     wt[j] = wt[j-1];
395     si[j] = si[j-1];
396     }
397     }
398     if (j < n) { /* add/insert sample */
399 greg 2.3 wt[j] = wti;
400 greg 2.1 si[j] = i;
401     nn += (nn < n);
402     }
403 greg 2.11 }
404 greg 2.3 wnorm = 0; /* normalize sample weights */
405     for (j = nn; j--; )
406     wnorm += wt[j];
407 greg 2.1 if (wnorm <= 0)
408     return(0);
409     wnorm = 1./wnorm;
410     for (j = nn; j--; )
411     wt[j] *= wnorm;
412     return(nn); /* return actual # samples */
413     }
414    
415     /* Free interpolant */
416     void
417     interp2_free(INTERP2 *ip)
418     {
419     if (ip == NULL)
420     return;
421 greg 2.4 if (ip->da != NULL)
422     free(ip->da);
423 greg 2.1 free(ip);
424     }