| 1 |
#ifndef lint |
| 2 |
static const char RCSid[] = "$Id: interp2d.c,v 2.14 2014/06/06 00:56:42 greg Exp $"; |
| 3 |
#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 |
/*************************************************************** |
| 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 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 and a low-res |
| 27 |
* map indicating where sample weights are significant. |
| 28 |
****************************************************************/ |
| 29 |
|
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include "rtmath.h" |
| 33 |
#include "interp2d.h" |
| 34 |
|
| 35 |
#define DECODE_DIA(ip,ed) ((ip)->dmin*(1. + .5*(ed))) |
| 36 |
#define ENCODE_DIA(ip,d) ((int)(2.*(d)/(ip)->dmin) - 2) |
| 37 |
|
| 38 |
/* Sample order (private) */ |
| 39 |
typedef struct { |
| 40 |
int si; /* sample index */ |
| 41 |
float dm; /* distance measure in this direction */ |
| 42 |
} SAMPORD; |
| 43 |
|
| 44 |
/* private routine to encode sample diameter with range checks */ |
| 45 |
static int |
| 46 |
encode_diameter(const INTERP2 *ip, double d) |
| 47 |
{ |
| 48 |
const int ed = ENCODE_DIA(ip, d); |
| 49 |
|
| 50 |
if (ed <= 0) |
| 51 |
return(0); |
| 52 |
if (ed >= 0xffff) |
| 53 |
return(0xffff); |
| 54 |
return(ed); |
| 55 |
} |
| 56 |
|
| 57 |
/* Allocate a new set of interpolation samples (caller assigns spt[] array) */ |
| 58 |
INTERP2 * |
| 59 |
interp2_alloc(int nsamps) |
| 60 |
{ |
| 61 |
INTERP2 *nip; |
| 62 |
|
| 63 |
if (nsamps <= 1) |
| 64 |
return(NULL); |
| 65 |
|
| 66 |
nip = (INTERP2 *)malloc(sizeof(INTERP2) + sizeof(float)*2*(nsamps-1)); |
| 67 |
if (nip == NULL) |
| 68 |
return(NULL); |
| 69 |
|
| 70 |
nip->ns = nsamps; |
| 71 |
nip->dmin = 1; /* default minimum diameter */ |
| 72 |
nip->smf = NI2DSMF; /* default smoothing factor */ |
| 73 |
nip->c_data = NULL; |
| 74 |
nip->da = NULL; |
| 75 |
/* caller must assign spt[] array */ |
| 76 |
return(nip); |
| 77 |
} |
| 78 |
|
| 79 |
/* Resize interpolation array (caller must assign any new values) */ |
| 80 |
INTERP2 * |
| 81 |
interp2_realloc(INTERP2 *ip, int nsamps) |
| 82 |
{ |
| 83 |
if (ip == NULL) |
| 84 |
return(interp2_alloc(nsamps)); |
| 85 |
if (nsamps <= 1) { |
| 86 |
interp2_free(ip); |
| 87 |
return(NULL); |
| 88 |
} |
| 89 |
if (nsamps == ip->ns) |
| 90 |
return(ip); |
| 91 |
if (ip->da != NULL) { /* will need to recompute distribution */ |
| 92 |
free(ip->da); |
| 93 |
ip->da = NULL; |
| 94 |
} |
| 95 |
ip = (INTERP2 *)realloc(ip, sizeof(INTERP2)+sizeof(float)*2*(nsamps-1)); |
| 96 |
if (ip == NULL) |
| 97 |
return(NULL); |
| 98 |
ip->ns = nsamps; |
| 99 |
return(ip); |
| 100 |
} |
| 101 |
|
| 102 |
/* Set minimum distance under which samples will start to merge */ |
| 103 |
void |
| 104 |
interp2_spacing(INTERP2 *ip, double mind) |
| 105 |
{ |
| 106 |
if (mind <= 0) |
| 107 |
return; |
| 108 |
if ((.998*ip->dmin <= mind) & (mind <= 1.002*ip->dmin)) |
| 109 |
return; |
| 110 |
if (ip->da != NULL) { /* will need to recompute distribution */ |
| 111 |
free(ip->da); |
| 112 |
ip->da = NULL; |
| 113 |
} |
| 114 |
ip->dmin = mind; |
| 115 |
} |
| 116 |
|
| 117 |
/* Compute unnormalized weight for a position relative to a sample */ |
| 118 |
double |
| 119 |
interp2_wti(INTERP2 *ip, const int i, double x, double y) |
| 120 |
{ |
| 121 |
double dir, rd, r2, d2; |
| 122 |
int ri; |
| 123 |
/* get relative direction */ |
| 124 |
x -= ip->spt[i][0]; |
| 125 |
y -= ip->spt[i][1]; |
| 126 |
dir = atan2a(y, x); |
| 127 |
dir += 2.*PI*(dir < 0); |
| 128 |
/* linear radius interpolation */ |
| 129 |
rd = dir * (NI2DIR/2./PI); |
| 130 |
ri = (int)rd; |
| 131 |
rd -= (double)ri; |
| 132 |
rd = (1.-rd)*ip->da[i].dia[ri] + rd*ip->da[i].dia[(ri+1)%NI2DIR]; |
| 133 |
rd = ip->smf * DECODE_DIA(ip, rd); |
| 134 |
r2 = 2.*rd*rd; |
| 135 |
d2 = x*x + y*y; |
| 136 |
if (d2 > 21.*r2) /* result would be < 1e-9 */ |
| 137 |
return(.0); |
| 138 |
/* Gaussian times harmonic weighting */ |
| 139 |
return( exp(-d2/r2) * ip->dmin/(ip->dmin + sqrt(d2)) ); |
| 140 |
} |
| 141 |
|
| 142 |
/* private call to get grid flag index */ |
| 143 |
static int |
| 144 |
interp2_flagpos(int fgi[2], INTERP2 *ip, double x, double y) |
| 145 |
{ |
| 146 |
int inbounds = 0; |
| 147 |
|
| 148 |
if (ip == NULL) /* paranoia */ |
| 149 |
return(-1); |
| 150 |
/* need to compute interpolant? */ |
| 151 |
if (ip->da == NULL && !interp2_analyze(ip)) |
| 152 |
return(-1); |
| 153 |
/* get x & y grid positions */ |
| 154 |
fgi[0] = (x - ip->smin[0]) * NI2DIM / (ip->smax[0] - ip->smin[0]); |
| 155 |
|
| 156 |
if (fgi[0] >= NI2DIM) |
| 157 |
fgi[0] = NI2DIM-1; |
| 158 |
else if (fgi[0] < 0) |
| 159 |
fgi[0] = 0; |
| 160 |
else |
| 161 |
++inbounds; |
| 162 |
|
| 163 |
fgi[1] = (y - ip->smin[1]) * NI2DIM / (ip->smax[1] - ip->smin[1]); |
| 164 |
|
| 165 |
if (fgi[1] >= NI2DIM) |
| 166 |
fgi[1] = NI2DIM-1; |
| 167 |
else if (fgi[1] < 0) |
| 168 |
fgi[1] = 0; |
| 169 |
else |
| 170 |
++inbounds; |
| 171 |
|
| 172 |
return(inbounds == 2); |
| 173 |
} |
| 174 |
|
| 175 |
#define setflg(fl,xi,yi) ((fl)[yi] |= 1<<(xi)) |
| 176 |
|
| 177 |
#define chkflg(fl,xi,yi) ((fl)[yi]>>(xi) & 1) |
| 178 |
|
| 179 |
/* private flood function to determine sample influence */ |
| 180 |
static void |
| 181 |
influence_flood(INTERP2 *ip, const int i, unsigned short visited[NI2DIM], |
| 182 |
int xfi, int yfi) |
| 183 |
{ |
| 184 |
double gx = (xfi+.5)*(1./NI2DIM)*(ip->smax[0] - ip->smin[0]) + |
| 185 |
ip->smin[0]; |
| 186 |
double gy = (yfi+.5)*(1./NI2DIM)*(ip->smax[1] - ip->smin[1]) + |
| 187 |
ip->smin[1]; |
| 188 |
double dx = gx - ip->spt[i][0]; |
| 189 |
double dy = gy - ip->spt[i][1]; |
| 190 |
|
| 191 |
setflg(visited, xfi, yfi); |
| 192 |
|
| 193 |
if (dx*dx + dy*dy > 2.*ip->grid2 && interp2_wti(ip, i, gx, gy) <= 1e-7) |
| 194 |
return; |
| 195 |
|
| 196 |
setflg(ip->da[i].infl, xfi, yfi); |
| 197 |
|
| 198 |
if (xfi > 0 && !chkflg(visited, xfi-1, yfi)) |
| 199 |
influence_flood(ip, i, visited, xfi-1, yfi); |
| 200 |
|
| 201 |
if (yfi > 0 && !chkflg(visited, xfi, yfi-1)) |
| 202 |
influence_flood(ip, i, visited, xfi, yfi-1); |
| 203 |
|
| 204 |
if (xfi < NI2DIM-1 && !chkflg(visited, xfi+1, yfi)) |
| 205 |
influence_flood(ip, i, visited, xfi+1, yfi); |
| 206 |
|
| 207 |
if (yfi < NI2DIM-1 && !chkflg(visited, xfi, yfi+1)) |
| 208 |
influence_flood(ip, i, visited, xfi, yfi+1); |
| 209 |
} |
| 210 |
|
| 211 |
/* private call to compute sample influence maps */ |
| 212 |
static void |
| 213 |
map_influence(INTERP2 *ip) |
| 214 |
{ |
| 215 |
unsigned short visited[NI2DIM]; |
| 216 |
int fgi[2]; |
| 217 |
int i, j; |
| 218 |
|
| 219 |
for (i = ip->ns; i--; ) { |
| 220 |
for (j = NI2DIM; j--; ) { |
| 221 |
ip->da[i].infl[j] = 0; |
| 222 |
visited[j] = 0; |
| 223 |
} |
| 224 |
interp2_flagpos(fgi, ip, ip->spt[i][0], ip->spt[i][1]); |
| 225 |
|
| 226 |
influence_flood(ip, i, visited, fgi[0], fgi[1]); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
/* Modify smoothing parameter by the given factor */ |
| 231 |
void |
| 232 |
interp2_smooth(INTERP2 *ip, double sf) |
| 233 |
{ |
| 234 |
float old_smf = ip->smf; |
| 235 |
|
| 236 |
if ((ip->smf *= sf) < NI2DSMF) |
| 237 |
ip->smf = NI2DSMF; |
| 238 |
/* need to recompute influence maps? */ |
| 239 |
if (ip->da != NULL && (old_smf*.85 > ip->smf) | |
| 240 |
(ip->smf > old_smf*1.15)) |
| 241 |
map_influence(ip); |
| 242 |
} |
| 243 |
|
| 244 |
/* private call-back to sort position index */ |
| 245 |
static int |
| 246 |
cmp_spos(const void *p1, const void *p2) |
| 247 |
{ |
| 248 |
const SAMPORD *so1 = (const SAMPORD *)p1; |
| 249 |
const SAMPORD *so2 = (const SAMPORD *)p2; |
| 250 |
|
| 251 |
if (so1->dm > so2->dm) |
| 252 |
return 1; |
| 253 |
if (so1->dm < so2->dm) |
| 254 |
return -1; |
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
/* private routine to order samples in a particular direction */ |
| 259 |
static void |
| 260 |
sort_samples(SAMPORD *sord, const INTERP2 *ip, double ang) |
| 261 |
{ |
| 262 |
const double cosd = cos(ang); |
| 263 |
const double sind = sin(ang); |
| 264 |
int i; |
| 265 |
|
| 266 |
for (i = ip->ns; i--; ) { |
| 267 |
sord[i].si = i; |
| 268 |
sord[i].dm = cosd*ip->spt[i][0] + sind*ip->spt[i][1]; |
| 269 |
} |
| 270 |
qsort(sord, ip->ns, sizeof(SAMPORD), cmp_spos); |
| 271 |
} |
| 272 |
|
| 273 |
/* (Re)compute anisotropic basis function interpolant (normally automatic) */ |
| 274 |
int |
| 275 |
interp2_analyze(INTERP2 *ip) |
| 276 |
{ |
| 277 |
SAMPORD *sortord; |
| 278 |
int *rightrndx, *leftrndx, *endrndx; |
| 279 |
int i, bd; |
| 280 |
/* sanity checks */ |
| 281 |
if (ip == NULL) |
| 282 |
return(0); |
| 283 |
if (ip->da != NULL) { /* free previous data if any */ |
| 284 |
free(ip->da); |
| 285 |
ip->da = NULL; |
| 286 |
} |
| 287 |
if ((ip->ns <= 1) | (ip->dmin <= 0)) |
| 288 |
return(0); |
| 289 |
/* compute sample domain */ |
| 290 |
ip->smin[0] = ip->smin[1] = FHUGE; |
| 291 |
ip->smax[0] = ip->smax[1] = -FHUGE; |
| 292 |
for (i = ip->ns; i--; ) { |
| 293 |
if (ip->spt[i][0] < ip->smin[0]) ip->smin[0] = ip->spt[i][0]; |
| 294 |
if (ip->spt[i][0] > ip->smax[0]) ip->smax[0] = ip->spt[i][0]; |
| 295 |
if (ip->spt[i][1] < ip->smin[1]) ip->smin[1] = ip->spt[i][1]; |
| 296 |
if (ip->spt[i][1] > ip->smax[1]) ip->smax[1] = ip->spt[i][1]; |
| 297 |
} |
| 298 |
ip->grid2 = ((ip->smax[0]-ip->smin[0])*(ip->smax[0]-ip->smin[0]) + |
| 299 |
(ip->smax[1]-ip->smin[1])*(ip->smax[1]-ip->smin[1])) * |
| 300 |
(1./NI2DIM/NI2DIM); |
| 301 |
if (ip->grid2 <= FTINY*ip->dmin*ip->dmin) |
| 302 |
return(0); |
| 303 |
/* allocate analysis data */ |
| 304 |
ip->da = (struct interp2_samp *)malloc( |
| 305 |
sizeof(struct interp2_samp)*ip->ns ); |
| 306 |
if (ip->da == NULL) |
| 307 |
return(0); |
| 308 |
/* allocate temporary arrays */ |
| 309 |
sortord = (SAMPORD *)malloc(sizeof(SAMPORD)*ip->ns); |
| 310 |
rightrndx = (int *)malloc(sizeof(int)*ip->ns); |
| 311 |
leftrndx = (int *)malloc(sizeof(int)*ip->ns); |
| 312 |
endrndx = (int *)malloc(sizeof(int)*ip->ns); |
| 313 |
if ((sortord == NULL) | (rightrndx == NULL) | |
| 314 |
(leftrndx == NULL) | (endrndx == NULL)) |
| 315 |
return(0); |
| 316 |
/* run through bidirections */ |
| 317 |
for (bd = 0; bd < NI2DIR/2; bd++) { |
| 318 |
const double ang = 2.*PI/NI2DIR*bd; |
| 319 |
int *sptr; |
| 320 |
/* create right reverse index */ |
| 321 |
if (bd) { /* re-use from previous iteration? */ |
| 322 |
sptr = rightrndx; |
| 323 |
rightrndx = leftrndx; |
| 324 |
leftrndx = sptr; |
| 325 |
} else { /* else sort first half-plane */ |
| 326 |
sort_samples(sortord, ip, PI/2. - PI/NI2DIR); |
| 327 |
for (i = ip->ns; i--; ) |
| 328 |
rightrndx[sortord[i].si] = i; |
| 329 |
/* & store reverse order for later */ |
| 330 |
for (i = ip->ns; i--; ) |
| 331 |
endrndx[sortord[i].si] = ip->ns-1 - i; |
| 332 |
} |
| 333 |
/* create new left reverse index */ |
| 334 |
if (bd == NI2DIR/2 - 1) { /* use order from first iteration? */ |
| 335 |
sptr = leftrndx; |
| 336 |
leftrndx = endrndx; |
| 337 |
endrndx = sptr; |
| 338 |
} else { /* else compute new half-plane */ |
| 339 |
sort_samples(sortord, ip, ang + (PI/2. + PI/NI2DIR)); |
| 340 |
for (i = ip->ns; i--; ) |
| 341 |
leftrndx[sortord[i].si] = i; |
| 342 |
} |
| 343 |
/* sort grid values in this direction */ |
| 344 |
sort_samples(sortord, ip, ang); |
| 345 |
/* find nearest neighbors each side */ |
| 346 |
for (i = ip->ns; i--; ) { |
| 347 |
const int ii = sortord[i].si; |
| 348 |
int j; |
| 349 |
/* preload with large radii */ |
| 350 |
ip->da[ii].dia[bd] = |
| 351 |
ip->da[ii].dia[bd+NI2DIR/2] = encode_diameter(ip, |
| 352 |
.5*(sortord[ip->ns-1].dm - sortord[0].dm)); |
| 353 |
for (j = i; ++j < ip->ns; ) /* nearest above */ |
| 354 |
if (rightrndx[sortord[j].si] > rightrndx[ii] && |
| 355 |
leftrndx[sortord[j].si] < leftrndx[ii]) { |
| 356 |
ip->da[ii].dia[bd] = encode_diameter(ip, |
| 357 |
sortord[j].dm - sortord[i].dm); |
| 358 |
break; |
| 359 |
} |
| 360 |
for (j = i; j-- > 0; ) /* nearest below */ |
| 361 |
if (rightrndx[sortord[j].si] < rightrndx[ii] && |
| 362 |
leftrndx[sortord[j].si] > leftrndx[ii]) { |
| 363 |
ip->da[ii].dia[bd+NI2DIR/2] = encode_diameter(ip, |
| 364 |
sortord[i].dm - sortord[j].dm); |
| 365 |
break; |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
free(sortord); /* release temp arrays */ |
| 370 |
free(rightrndx); |
| 371 |
free(leftrndx); |
| 372 |
free(endrndx); |
| 373 |
/* map sample influence areas */ |
| 374 |
map_influence(ip); |
| 375 |
return(1); /* all done */ |
| 376 |
} |
| 377 |
|
| 378 |
/* Assign full set of normalized weights to interpolate the given position */ |
| 379 |
int |
| 380 |
interp2_weights(float wtv[], INTERP2 *ip, double x, double y) |
| 381 |
{ |
| 382 |
double wnorm; |
| 383 |
int fgi[2]; |
| 384 |
int i; |
| 385 |
|
| 386 |
if (wtv == NULL) |
| 387 |
return(0); |
| 388 |
/* get flag position */ |
| 389 |
if (interp2_flagpos(fgi, ip, x, y) < 0) |
| 390 |
return(0); |
| 391 |
|
| 392 |
wnorm = 0; /* compute raw weights */ |
| 393 |
for (i = ip->ns; i--; ) |
| 394 |
if (chkflg(ip->da[i].infl, fgi[0], fgi[1])) { |
| 395 |
double wt = interp2_wti(ip, i, x, y); |
| 396 |
wtv[i] = wt; |
| 397 |
wnorm += wt; |
| 398 |
} else |
| 399 |
wtv[i] = 0; |
| 400 |
if (wnorm <= 0) /* too far from all our samples! */ |
| 401 |
return(0); |
| 402 |
wnorm = 1./wnorm; /* normalize weights */ |
| 403 |
for (i = ip->ns; i--; ) |
| 404 |
wtv[i] *= wnorm; |
| 405 |
return(ip->ns); /* all done */ |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
/* Get normalized weights and indexes for n best samples in descending order */ |
| 410 |
int |
| 411 |
interp2_topsamp(float wt[], int si[], const int n, INTERP2 *ip, double x, double y) |
| 412 |
{ |
| 413 |
int nn = 0; |
| 414 |
int fgi[2]; |
| 415 |
double wnorm; |
| 416 |
int i, j; |
| 417 |
|
| 418 |
if ((n <= 0) | (wt == NULL) | (si == NULL)) |
| 419 |
return(0); |
| 420 |
/* get flag position */ |
| 421 |
if (interp2_flagpos(fgi, ip, x, y) < 0) |
| 422 |
return(0); |
| 423 |
/* identify top n weights */ |
| 424 |
for (i = ip->ns; i--; ) |
| 425 |
if (chkflg(ip->da[i].infl, fgi[0], fgi[1])) { |
| 426 |
const double wti = interp2_wti(ip, i, x, y); |
| 427 |
for (j = nn; j > 0; j--) { |
| 428 |
if (wt[j-1] >= wti) |
| 429 |
break; |
| 430 |
if (j < n) { |
| 431 |
wt[j] = wt[j-1]; |
| 432 |
si[j] = si[j-1]; |
| 433 |
} |
| 434 |
} |
| 435 |
if (j < n) { /* add/insert sample */ |
| 436 |
wt[j] = wti; |
| 437 |
si[j] = i; |
| 438 |
nn += (nn < n); |
| 439 |
} |
| 440 |
} |
| 441 |
wnorm = 0; /* normalize sample weights */ |
| 442 |
for (j = nn; j--; ) |
| 443 |
wnorm += wt[j]; |
| 444 |
if (wnorm <= 0) |
| 445 |
return(0); |
| 446 |
wnorm = 1./wnorm; |
| 447 |
for (j = nn; j--; ) |
| 448 |
wt[j] *= wnorm; |
| 449 |
return(nn); /* return actual # samples */ |
| 450 |
} |
| 451 |
|
| 452 |
/* Free interpolant */ |
| 453 |
void |
| 454 |
interp2_free(INTERP2 *ip) |
| 455 |
{ |
| 456 |
if (ip == NULL) |
| 457 |
return; |
| 458 |
if (ip->da != NULL) |
| 459 |
free(ip->da); |
| 460 |
free(ip); |
| 461 |
} |