9 |
|
|
10 |
|
#include "copyright.h" |
11 |
|
|
12 |
< |
/************************************************************* |
12 |
> |
/*************************************************************** |
13 |
|
* 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 |
< |
* calculation, we sort the data into 3 half-planes and |
19 |
< |
* perform simple tests to see which neighbor is closest in |
20 |
< |
* a each direction. Once we have our approximate neighborhood |
21 |
< |
* for a sample, we can use it in a Gaussian weighting scheme |
22 |
< |
* with anisotropic surround. This gives us a fairly smooth |
23 |
< |
* interpolation however the sample points may be initially |
24 |
< |
* distributed. Evaluation is accelerated by use of a fast |
25 |
< |
* approximation to the atan2(y,x) function. |
26 |
< |
**************************************************************/ |
18 |
> |
* calculation, we sort the data into half-planes and apply |
19 |
> |
* simple tests to see which neighbor is closest in each |
20 |
> |
* angular slice. Once we have our approximate neighborhood |
21 |
> |
* for a sample, we can use it in a modified Gaussian weighting |
22 |
> |
* with allowing local anisotropy. Harmonic weighting is added |
23 |
> |
* to reduce the influence of distant neighbors. This yields a |
24 |
> |
* smooth interpolation regardless of how the sample points are |
25 |
> |
* initially distributed. Evaluation is accelerated by use of |
26 |
> |
* a fast approximation to the atan2(y,x) function. |
27 |
> |
****************************************************************/ |
28 |
|
|
29 |
|
#include <stdio.h> |
30 |
|
#include <stdlib.h> |
31 |
|
#include "rtmath.h" |
32 |
|
#include "interp2d.h" |
33 |
|
|
34 |
< |
#define DECODE_RAD(ip,er) ((ip)->rmin*(1. + .5*(er))) |
35 |
< |
#define ENCODE_RAD(ip,r) ((int)(2.*(r)/(ip)->rmin) - 2) |
34 |
> |
#define DECODE_DIA(ip,ed) ((ip)->dmin*(1. + .5*(ed))) |
35 |
> |
#define ENCODE_DIA(ip,d) ((int)(2.*(d)/(ip)->dmin) - 2) |
36 |
|
|
37 |
|
/* Sample order (private) */ |
38 |
|
typedef struct { |
54 |
|
return(NULL); |
55 |
|
|
56 |
|
nip->ns = nsamps; |
57 |
< |
nip->rmin = .5; /* default radius minimum */ |
57 |
> |
nip->dmin = 1; /* default minimum diameter */ |
58 |
|
nip->smf = NI2DSMF; /* default smoothing factor */ |
59 |
< |
nip->ra = NULL; |
59 |
> |
nip->da = NULL; |
60 |
|
/* caller must assign spt[] array */ |
61 |
|
return(nip); |
62 |
|
} |
71 |
|
interp2_free(ip); |
72 |
|
return(NULL); |
73 |
|
} |
74 |
< |
if (nsamps == ip->ns); |
74 |
> |
if (nsamps == ip->ns) |
75 |
|
return(ip); |
76 |
< |
if (ip->ra != NULL) { /* will need to recompute distribution */ |
77 |
< |
free(ip->ra); |
78 |
< |
ip->ra = NULL; |
76 |
> |
if (ip->da != NULL) { /* will need to recompute distribution */ |
77 |
> |
free(ip->da); |
78 |
> |
ip->da = NULL; |
79 |
|
} |
80 |
|
ip = (INTERP2 *)realloc(ip, sizeof(INTERP2)+sizeof(float)*2*(nsamps-1)); |
81 |
|
if (ip == NULL) |
84 |
|
return(ip); |
85 |
|
} |
86 |
|
|
87 |
+ |
/* Set minimum distance under which samples will start to merge */ |
88 |
+ |
void |
89 |
+ |
interp2_spacing(INTERP2 *ip, double mind) |
90 |
+ |
{ |
91 |
+ |
if (mind <= 0) |
92 |
+ |
return; |
93 |
+ |
if ((.998*ip->dmin <= mind) & (mind <= 1.002*ip->dmin)) |
94 |
+ |
return; |
95 |
+ |
if (ip->da != NULL) { /* will need to recompute distribution */ |
96 |
+ |
free(ip->da); |
97 |
+ |
ip->da = NULL; |
98 |
+ |
} |
99 |
+ |
ip->dmin = mind; |
100 |
+ |
} |
101 |
+ |
|
102 |
+ |
/* Modify smoothing parameter by the given factor */ |
103 |
+ |
void |
104 |
+ |
interp2_smooth(INTERP2 *ip, double sf) |
105 |
+ |
{ |
106 |
+ |
if ((ip->smf *= sf) < NI2DSMF) |
107 |
+ |
ip->smf = NI2DSMF; |
108 |
+ |
} |
109 |
+ |
|
110 |
|
/* private call-back to sort position index */ |
111 |
|
static int |
112 |
|
cmp_spos(const void *p1, const void *p2) |
136 |
|
qsort(sord, ip->ns, sizeof(SAMPORD), &cmp_spos); |
137 |
|
} |
138 |
|
|
139 |
< |
/* private routine to encode radius with range checks */ |
139 |
> |
/* private routine to encode sample diameter with range checks */ |
140 |
|
static int |
141 |
< |
encode_radius(const INTERP2 *ip, double r) |
141 |
> |
encode_diameter(const INTERP2 *ip, double d) |
142 |
|
{ |
143 |
< |
const int er = ENCODE_RAD(ip, r); |
143 |
> |
const int ed = ENCODE_DIA(ip, d); |
144 |
|
|
145 |
< |
if (er <= 0) |
145 |
> |
if (ed <= 0) |
146 |
|
return(0); |
147 |
< |
if (er >= 0xffff) |
147 |
> |
if (ed >= 0xffff) |
148 |
|
return(0xffff); |
149 |
< |
return(er); |
149 |
> |
return(ed); |
150 |
|
} |
151 |
|
|
152 |
|
/* (Re)compute anisotropic basis function interpolant (normally automatic) */ |
157 |
|
int *rightrndx, *leftrndx, *endrndx; |
158 |
|
int bd; |
159 |
|
/* sanity checks */ |
160 |
< |
if (ip == NULL || (ip->ns <= 1) | (ip->rmin <= 0)) |
160 |
> |
if (ip == NULL || (ip->ns <= 1) | (ip->dmin <= 0)) |
161 |
|
return(0); |
162 |
|
/* need to allocate? */ |
163 |
< |
if (ip->ra == NULL) { |
164 |
< |
ip->ra = (unsigned short (*)[NI2DIR])malloc( |
163 |
> |
if (ip->da == NULL) { |
164 |
> |
ip->da = (unsigned short (*)[NI2DIR])malloc( |
165 |
|
sizeof(unsigned short)*NI2DIR*ip->ns); |
166 |
< |
if (ip->ra == NULL) |
166 |
> |
if (ip->da == NULL) |
167 |
|
return(0); |
168 |
|
} |
169 |
|
/* get temporary arrays */ |
206 |
|
sort_samples(sortord, ip, ang); |
207 |
|
/* find nearest neighbors each side */ |
208 |
|
for (i = ip->ns; i--; ) { |
209 |
< |
const int rpos = rightrndx[sortord[i].si]; |
186 |
< |
const int lpos = leftrndx[sortord[i].si]; |
209 |
> |
const int ii = sortord[i].si; |
210 |
|
int j; |
211 |
< |
/* preload with large radius */ |
212 |
< |
ip->ra[i][bd] = ip->ra[i][bd+NI2DIR/2] = encode_radius(ip, |
213 |
< |
.25*(sortord[ip->ns-1].dm - sortord[0].dm)); |
211 |
> |
/* preload with large radii */ |
212 |
> |
ip->da[ii][bd] = ip->da[ii][bd+NI2DIR/2] = encode_diameter(ip, |
213 |
> |
.5*(sortord[ip->ns-1].dm - sortord[0].dm)); |
214 |
|
for (j = i; ++j < ip->ns; ) /* nearest above */ |
215 |
< |
if (rightrndx[sortord[j].si] > rpos && |
216 |
< |
leftrndx[sortord[j].si] < lpos) { |
217 |
< |
ip->ra[i][bd] = encode_radius(ip, |
218 |
< |
.5*(sortord[j].dm - sortord[i].dm)); |
215 |
> |
if (rightrndx[sortord[j].si] > rightrndx[ii] && |
216 |
> |
leftrndx[sortord[j].si] < leftrndx[ii]) { |
217 |
> |
ip->da[ii][bd] = encode_diameter(ip, |
218 |
> |
sortord[j].dm - sortord[i].dm); |
219 |
|
break; |
220 |
|
} |
221 |
|
for (j = i; j-- > 0; ) /* nearest below */ |
222 |
< |
if (rightrndx[sortord[j].si] < rpos && |
223 |
< |
leftrndx[sortord[j].si] > lpos) { |
224 |
< |
ip->ra[i][bd+NI2DIR/2] = encode_radius(ip, |
225 |
< |
.5*(sortord[i].dm - sortord[j].dm)); |
222 |
> |
if (rightrndx[sortord[j].si] < rightrndx[ii] && |
223 |
> |
leftrndx[sortord[j].si] > leftrndx[ii]) { |
224 |
> |
ip->da[ii][bd+NI2DIR/2] = encode_diameter(ip, |
225 |
> |
sortord[i].dm - sortord[j].dm); |
226 |
|
break; |
227 |
|
} |
228 |
|
} |
234 |
|
return(1); |
235 |
|
} |
236 |
|
|
237 |
< |
/* private call returns log of raw weight for a particular sample */ |
237 |
> |
/* private call returns raw weight for a particular sample */ |
238 |
|
static double |
239 |
< |
get_ln_wt(const INTERP2 *ip, const int i, double x, double y) |
239 |
> |
get_wt(const INTERP2 *ip, const int i, double x, double y) |
240 |
|
{ |
241 |
< |
double dir, rd; |
241 |
> |
double dir, rd, r2, d2; |
242 |
|
int ri; |
243 |
|
/* get relative direction */ |
244 |
|
x -= ip->spt[i][0]; |
249 |
|
rd = dir * (NI2DIR/2./PI); |
250 |
|
ri = (int)rd; |
251 |
|
rd -= (double)ri; |
252 |
< |
rd = (1.-rd)*ip->ra[i][ri] + rd*ip->ra[i][(ri+1)%NI2DIR]; |
253 |
< |
rd = ip->smf * DECODE_RAD(ip, rd); |
254 |
< |
/* return log of Gaussian weight */ |
255 |
< |
return( (x*x + y*y) / (-2.*rd*rd) ); |
252 |
> |
rd = (1.-rd)*ip->da[i][ri] + rd*ip->da[i][(ri+1)%NI2DIR]; |
253 |
> |
rd = ip->smf * DECODE_DIA(ip, rd); |
254 |
> |
r2 = 2.*rd*rd; |
255 |
> |
d2 = x*x + y*y; |
256 |
> |
if (d2 > 21.*r2) /* result would be < 1e-9 */ |
257 |
> |
return(.0); |
258 |
> |
/* Gaussian times harmonic weighting */ |
259 |
> |
return( exp(-d2/r2) * ip->dmin/(ip->dmin + sqrt(d2)) ); |
260 |
|
} |
261 |
|
|
262 |
|
/* Assign full set of normalized weights to interpolate the given position */ |
269 |
|
if ((wtv == NULL) | (ip == NULL)) |
270 |
|
return(0); |
271 |
|
/* need to compute interpolant? */ |
272 |
< |
if (ip->ra == NULL && !interp2_analyze(ip)) |
272 |
> |
if (ip->da == NULL && !interp2_analyze(ip)) |
273 |
|
return(0); |
274 |
|
|
275 |
|
wnorm = 0; /* compute raw weights */ |
276 |
|
for (i = ip->ns; i--; ) { |
277 |
< |
double wt = get_ln_wt(ip, i, x, y); |
251 |
< |
if (wt < -21.) { |
252 |
< |
wtv[i] = 0; /* ignore weights < 1e-9 */ |
253 |
< |
continue; |
254 |
< |
} |
255 |
< |
wt = exp(wt); /* Gaussian weight */ |
277 |
> |
double wt = get_wt(ip, i, x, y); |
278 |
|
wtv[i] = wt; |
279 |
|
wnorm += wt; |
280 |
|
} |
298 |
|
if ((n <= 0) | (wt == NULL) | (si == NULL) | (ip == NULL)) |
299 |
|
return(0); |
300 |
|
/* need to compute interpolant? */ |
301 |
< |
if (ip->ra == NULL && !interp2_analyze(ip)) |
301 |
> |
if (ip->da == NULL && !interp2_analyze(ip)) |
302 |
|
return(0); |
303 |
|
/* identify top n weights */ |
304 |
|
for (i = ip->ns; i--; ) { |
305 |
< |
const double lnwt = get_ln_wt(ip, i, x, y); |
305 |
> |
const double wti = get_wt(ip, i, x, y); |
306 |
> |
if (wti <= 1e-9) |
307 |
> |
continue; |
308 |
|
for (j = nn; j > 0; j--) { |
309 |
< |
if (wt[j-1] >= lnwt) |
309 |
> |
if (wt[j-1] >= wti) |
310 |
|
break; |
311 |
|
if (j < n) { |
312 |
|
wt[j] = wt[j-1]; |
314 |
|
} |
315 |
|
} |
316 |
|
if (j < n) { /* add/insert sample */ |
317 |
< |
wt[j] = lnwt; |
317 |
> |
wt[j] = wti; |
318 |
|
si[j] = i; |
319 |
|
nn += (nn < n); |
320 |
|
} |
321 |
|
} |
322 |
< |
wnorm = 0; /* exponentiate and normalize */ |
323 |
< |
for (j = nn; j--; ) { |
324 |
< |
double dwt = exp(wt[j]); |
301 |
< |
wt[j] = dwt; |
302 |
< |
wnorm += dwt; |
303 |
< |
} |
322 |
> |
wnorm = 0; /* normalize sample weights */ |
323 |
> |
for (j = nn; j--; ) |
324 |
> |
wnorm += wt[j]; |
325 |
|
if (wnorm <= 0) |
326 |
|
return(0); |
327 |
|
wnorm = 1./wnorm; |
336 |
|
{ |
337 |
|
if (ip == NULL) |
338 |
|
return; |
339 |
< |
if (ip->ra != NULL) |
340 |
< |
free(ip->ra); |
339 |
> |
if (ip->da != NULL) |
340 |
> |
free(ip->da); |
341 |
|
free(ip); |
342 |
|
} |