Debian Codegraph @

rspl/stest.c

raw symbols SCIP
1
2 /*
3 * Scattered Data Interpolation
4 * with multilevel B-splines
5 * research.
6 *
7 * This is from the paper
8 * "Scattered Data Interpolation with Multilevel B-Splines"
9 * by Seungyong Lee, George Wolberg and Sung Yong Shin,
10 */
11
12 /*
13 * Author: Graeme W. Gill
14 * Date: 2001/1/1
15 *
16 * Copyright 2000 - 2001 Graeme W. Gill
17 * All rights reserved.
18 *
19 * This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
20 * see the License.txt file for licencing details.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <math.h>
29 #include "../numeric/numlib.h"
30
31
32 #ifndef NUMSUP_H
33 void error(char *fmt, ...), warning(char *fmt, ...);
34 #endif
35
36 #define MXDI 4
37 #define MXDO 4
38
39 /* A control point */
40 typedef struct {
41 double p[MXDI];
42 double v[MXDO];
43 } co;
44
45 /* Neighborhood latice cache data */
46 typedef struct {
47 int c[MXDI]; /* Coordinate */
48 int xo; /* Offset into srbs latice */
49 double w; /* B-spline basis weight */
50 } neigh;
51
52 /* Structure that represents a resolution level of B-splines */
53 struct _srbs {
54 struct _mrbs *p; /* Parent structure */
55 int res; /* Basic resolution */
56 int coi[MXDI]; /* Double increment for each input dimension into latice */
57 double *lat; /* Control latice, extending from +/- 1 from 0..res-1 */
58 double *_lat; /* Allocation base of lat */
59 int lsize, loff; /* Number of doubles in _lat, offset of lat from _lat */
60 double w[MXDI]; /* Input data cell width */
61 neigh *n; /* Neighborhood latice cache */
62 int nsize; /* Number of n entries */
63 }; typedef struct _srbs srbs;
64
65 /* Structure that represents the whole scattered interpolation state */
66 struct _mrbs {
67 int di; /* Input dimensions */
68 int fdi; /* Output dimensions */
69 int tres; /* Target resolution */
70 double smf; /* Smoothing factor */
71 int npts; /* Number of data points */
72 co *pts; /* Coordinate points */
73 double l[MXDI], h[MXDI]; /* Input data range, cell width */
74 srbs *s; /* Current B-spline latice */
75 }; typedef struct _mrbs mrbs;
76
77 static void delete_srbs(srbs *s);
78
79 /* Create a new empty mrbs */
80 static mrbs *new_mrbs(
81 int di, /* Input dimensionality */
82 int fdi, /* Output dimesionality */
83 int res, /* Target resolution */
84 double smf /* Smoothing factor */
85 ) {
86 mrbs *p;
87 if ((p = (mrbs *)malloc(sizeof(mrbs))) == NULL)
88 error("Malloc mrbs failed");
89
90 p->di = di;
91 p->fdi = fdi;
92 p->tres = res;
93 p->smf = smf;
94 p->s = NULL;
95
96 return p;
97 }
98
99 static void delete_mrbs(mrbs *p) {
100
101 if (p != NULL) {
102 delete_srbs(p->s);
103 free(p);
104 }
105 }
106
107 /* Create a new empty srbs */
108 static srbs *new_srbs(
109 mrbs *p, /* Parent mrbs */
110 int res /* Resolution of this srbs */
111 ) {
112 srbs *s;
113 int e, f;
114 double *_lat, *lat; /* Latice base address */
115 int ix, oe, oo[MXDI]; /* Neighborhood offset index, counter */
116
117 if ((s = (srbs *)malloc(sizeof(srbs))) == NULL)
118 error("Malloc srbs failed");
119
120 s->p = p;
121 s->res = res;
122
123 for (s->lsize = p->fdi, s->nsize = 1, e = 0; e < p->di; e++) {
124 s->coi[e] = s->lsize; /* (double) increment in this input dimension */
125 s->lsize *= (res + 2); /* Latice in 1D +/- 1 */
126 s->nsize *= 4; /* Neighborhood of 4 */
127 }
128
129 if ((s->_lat = (double *)malloc(s->lsize * sizeof(double))) == NULL)
130 error("Malloc srbs latice failed");
131
132 /* Compute the base address */
133 for (s->loff = 0, e = 0; e < p->di; e++) {
134 s->loff += s->coi[e]; /* Offset by 1 in each input dimension */
135 }
136 s->lat = s->_lat + s->loff;
137
138 /* Figure the cell width */
139 for (e = 0; e < p->di; e++)
140 s->w[e] = (p->h[e] - p->l[e])/(res-1.0);
141
142 /* Setup neighborhood cache info */
143 if ((s->n = (neigh *)malloc(s->nsize * sizeof(neigh))) == NULL)
144 error("Malloc srbs neighborhood failed");
145
146 for (oe = 0; oe < p->di; oe++)
147 oo[oe] = 0;
148
149 for(ix = oe = 0; oe < p->di; ix++) {
150 int xo;
151 for (xo = e = 0; e < p->di; e++) {
152 s->n[ix].c[e] = oo[e];
153 xo += s->coi[e] * oo[e]; /* Accumulate latice offset */
154 }
155 s->n[ix].xo = xo;
156 s->n[ix].w = 0.0;
157
158 /* Increment destination offset counter */
159 for (oe = 0; oe < p->di; oe++) {
160 if (++oo[oe] <= 3) /* Counting from 0 ... 3 */
161 break;
162 oo[oe] = 0;
163 }
164 }
165
166 return s;
167 }
168
169 /* Destroy a srbs */
170 static void delete_srbs(srbs *s) {
171 if (s != NULL) {
172 free(s->_lat);
173 free(s->n);
174 free(s);
175 }
176 }
177
178 /* Dump the 2D -> 1D contents of an srbs */
179 static void dump_srbs(srbs *s) {
180 int e, f;
181 int ce, co[MXDI]; /* latice counter */
182 mrbs *p = s->p; /* Parent object */
183
184 /* Init the counter */
185 for (ce = 0; ce < p->di; ce++)
186 co[ce] = -1;
187 ce = 0;
188
189 f = 0;
190 while(ce < p->di) {
191 double v;
192 int off = 0; /* Latice offset */
193 for (e = 0; e < p->di; e++) {
194 off += co[e] * s->coi[e]; /* Accumulate latice offset */
195 }
196 v = s->lat[off + f]; /* Value of this latice point */
197
198 printf("Latice at [%d][%d] = %f\n",co[1],co[0],v);
199
200 /* Increment the latice counter */
201 for (ce = 0; ce < p->di; ce++) {
202 if (++co[ce] <= s->res) /* Counting from -1 ... s->res */
203 break;
204 co[ce] = -1;
205 }
206 }
207 }
208
209 /* Initialise an srbs with a linear approximation to the scattered data */
210 static void linear_srbs(
211 srbs *s
212 ) {
213 int i, e, f;
214 mrbs *p = s->p; /* Parent object */
215 double **A; /* A matrix holding scattered data points */
216 double *B; /* B matrix holding RHS & solution */
217
218 /* Allocate the matricies */
219 B = dvector(0, p->npts-1);
220 A = dmatrix(0, p->npts-1, 0, p->di);
221
222 /* For each output dimension, solve the linear equation coeficients */
223 for (f = 0; f < p->fdi; f++) {
224 int ce, co[MXDI]; /* latice counter */
225
226 /* Init A[][] with the scattered data points positions */
227 /* Also init B[] with the value for this output dimension */
228 for (i = 0; i < p->npts; i++) {
229 for (e = 0; e < p->di; e++)
230 A[i][e] = p->pts[i].p[e];
231 A[i][e] = 1.0;
232 B[i] = p->pts[i].v[f];
233 }
234
235 /* Solve the equation A.x = b using SVD */
236 /* (The w[] values are thresholded for best accuracy) */
237 /* Return non-zero if no solution found */
238 if (svdsolve(A, B, p->npts, p->di+1) != 0)
239 error("SVD least squares failed");
240 /* A[][] will have been changed, and B[] holds the p->di+1 coefficients */
241
242 /* Use the coefficients to initialise the srbs values */
243 for (ce = 0; ce < p->di; ce++)
244 co[ce] = -1;
245 ce = 0;
246
247 while(ce < p->di) {
248 double v = B[p->di]; /* Constant */
249 int off = 0; /* Latice offset */
250 for (e = 0; e < p->di; e++) {
251 double lv;
252 lv = p->l[e] + s->w[e] * co[e]; /* Input value for this latice location */
253 v += B[e] * lv;
254 off += co[e] * s->coi[e]; /* Accumulate latice offset */
255 }
256 s->lat[off + f] = v; /* Value of this latice point */
257
258 /* Increment the latice counter */
259 for (ce = 0; ce < p->di; ce++) {
260 if (++co[ce] <= s->res) /* Counting from -1 ... s->res */
261 break;
262 co[ce] = -1;
263 }
264 }
265 }
266 free_dmatrix(A, 0, p->npts-1, 0, p->di);
267 free_dvector(B, 0, p->npts-1);
268 }
269
270 /* Do a latice refinement - upsample the current */
271 /* source latice to the destination latice. */
272 static void refine_srbs(
273 srbs *ds, /* Destination srbs */
274 srbs *ss /* Source srbs */
275 ) {
276 mrbs *p = ss->p; /* Parent object */
277 int ce, co[MXDI]; /* Source coordinate counter */
278 int six; /* Source index */
279 int dix; /* destination index */
280 static double _wt[5] = { 1.0/8.0, 4.0/8.0, 6.0/8.0, 4.0/8.0, 1.0/8.0 };
281 static double *wt = &_wt[2]; /* 1D Distribution weighting */
282
283 /* Zero the destination latice before accumulating values */
284 for (dix = 0; dix < ds->lsize; dix++)
285 ds->_lat[dix] = 0.0;
286
287 /* Now for each source latice entry, add weighted portions */
288 /* to the associated destination points */
289
290 /* Init the source coordinate counter */
291 for (ce = 0; ce < p->di; ce++)
292 co[ce] = -1;
293 ce = 0;
294 six = -ss->loff;
295
296 while(ce < p->di) {
297 int oe, oo[MXDI]; /* Destination offset counter */
298
299 //printf("Source coord %d %d, offset %d, value %f\n",co[0], co[1], six, ss->lat[six]);
300 /* calc destination index, and init offest counter */
301 for (dix = oe = 0; oe < p->di; oe++) {
302 oo[oe] = -2;
303 dix += co[oe] * 2 * ds->coi[oe]; /* Accumulate dest offset */
304 }
305 oe = 0;
306
307 //printf("Dest coord %d %d\n",co[0] * 2, co[1] * 2);
308 /* For all the offsets from the destination point */
309 while(oe < p->di) {
310 int e, f, dixo; /* Destination index offset */
311 double w = 1.0; /* Weighting */
312
313 //printf("dest offset %d %d\n",oo[0], oo[1]);
314 /* Compute dest index offset, and check that we are not outside the destination */
315 for (dixo = e = 0; e < p->di; e++) {
316 int x = co[e] * 2 + oo[e]; /* dest coord */
317 dixo += oo[e] * ds->coi[e]; /* Accumulate dest offset */
318 //printf("x[%d] = %d\n",e, x);
319 w *= wt[oo[e]]; /* Compute distribution weighting */
320 if (x < -1 || x > ds->res)
321 break; /* No good */
322 }
323 if (e >= p->di) { /* We are within the destination latice */
324 //if ((co[0] * 2 + oo[0]) == 0 && (co[1] * 2 + oo[1]) == 0) {
325 //printf("Source coord %d %d, offset %d, value %f\n",co[0], co[1], six, ss->lat[six]);
326 //printf("Dest coord %d %d ix %d, weight %f\n",co[0] * 2 + oo[0], co[1] * 2 + oo[1], dix+dixo, w);
327 //}
328
329 for (f = 0; f < p->fdi; f++) { /* Distribute weighted values */
330 double v = ss->lat[six + f];
331 //if ((co[0] * 2 + oo[0]) == 0 && (co[1] * 2 + oo[1]) == 0)
332 //printf("Value being dist %f, weighted value %f\n", v, v * w);
333 ds->lat[dix + dixo + f] += v * w;
334 }
335 }
336
337 /* Increment destination offset counter */
338 for (oe = 0; oe < p->di; oe++) {
339 if (++oo[oe] <= 2) /* Counting from -2 ... +2 */
340 break;
341 oo[oe] = -2;
342 }
343 }
344
345 /* Increment the source index and coordinat counter */
346 six += p->fdi;
347 for (ce = 0; ce < p->di; ce++) {
348 if (++co[ce] <= ss->res) /* Counting from -1 ... ss->res */
349 break;
350 co[ce] = -1;
351 }
352 }
353 }
354
355 /* Compute the Cubic B-spline weightings for a given t */
356 void basis(double b[4], double t) {
357 double _t3, _t2, _t1, _3t3, _3t2, _3t1, _6t2;
358
359 _t1 = t/6.0;
360 _t2 = _t1 * _t1;
361 _t3 = _t2 * _t1;
362 _3t1 = 3.0 * _t1;
363 _3t2 = 3.0 * _t2;
364 _3t3 = 3.0 * _t3;
365 _6t2 = 6.0 * _t2;
366
367 b[0] = - _t3 + _3t2 - _3t1 + 1.0/6.0;
368 b[1] = _3t3 - _6t2 + 4.0/6.0;
369 b[2] = -_3t3 + _3t2 + _3t1 + 1.0/6.0;
370 b[3] = _t3;
371 }
372
373
374 /* Improve an srbs to make it closer to the scattered data */
375 static void improve_srbs(
376 srbs *s
377 ) {
378 int i, e, f;
379 mrbs *p = s->p; /* Parent object */
380 double *delta; /* Delta accumulation */
381 double *omega; /* Omega accumulation */
382
383 /* Allocate temporary accumulation arrays */
384 if ((delta = (double *)calloc(sizeof(double), s->lsize)) == NULL)
385 error("Malloc srbs temp latice failed");
386 delta += s->loff;
387 if ((omega = (double *)calloc(sizeof(double), s->lsize)) == NULL)
388 error("Malloc srbs temp latice failed");
389 omega += s->loff;
390
391 /* For each scattered data point */
392 for (i = 0; i < p->npts; i++) {
393 int ix; /* Latice index of base of neighborhood */
394 double b[MXDI][4]; /* B-spline basis factors for each dimension */
395 double sws; /* Sum of all the basis factors squared */
396 double ve[MXDO]; /* Current output value error */
397 int nn; /* Neighbor counter */
398
399 /* Figure out our neighborhood */
400 for (ix = e = 0; e < p->di; e++) {
401 int x;
402 double t, sp, fp;
403 sp = (p->pts[i].p[e] - p->l[e])/s->w[e]; /* Scaled position */
404 fp = floor(sp);
405 x = (int)(fp - 1.0); /* Grid coordinate */
406 ix += s->coi[e] * x; /* Accume latice offset */
407 t = sp - fp; /* Spline parameter */
408 basis(b[e], t); /* Compute basis function values */
409 }
410
411 /* Compute the grid basis weight functions, */
412 /* the sum of the weights squared, and the current */
413 /* output value estimate. */
414 for (f = 0; f < p->fdi; f++)
415 ve[f] = p->pts[i].v[f]; /* Target output value */
416 for (sws = 0.0, nn = 0; nn < s->nsize; nn++) {
417 double w;
418 for (w = 1.0, e = 0; e < p->di; e++)
419 w *= b[e][s->n[nn].c[e]];
420 s->n[nn].w = w; /* cache weighting */
421 sws += w * w;
422 for (f = 0; f < p->fdi; f++)
423 ve[f] -= w * s->lat[ix + s->n[nn].xo + f]; /* Subtract current aprox value */
424 }
425 printf("Error at point %d = %f\n",i,ve[0]);
426
427 /* Accumulate the delta and omega factors */
428 /* for this resolutions improvement. */
429 for (nn = 0; nn < s->nsize; nn++) {
430 double ws, ww, w = s->n[nn].w;
431 int xo = ix + s->n[nn].xo; /* Latice offset */
432 ww = w * w;
433 ws = ww * w/sws; /* Scale factor for delta */
434 omega[xo] += ww; /* Accumulate omega */
435 for (f = 0; f < p->fdi; f++)
436 delta[xo + f] += ws * ve[f]; /* Accumulate delta */
437 printf("Distributing omega %f to %d %d\n",ww,s->n[nn].c[0],s->n[nn].c[1]);
438 printf("Distributing delta %f to %d %d\n",ws * ve[0],s->n[nn].c[0],s->n[nn].c[1]);
439 }
440 }
441
442 omega -= s->loff; /* Base them back to -1 corner */
443 delta -= s->loff;
444
445 /* Go through the delta and omega arrays, */
446 /* compute and add the refinements to the current */
447 /* B-spline control latice. */
448 for (i = 0; i < s->lsize; i++) {
449 double om = omega[i];
450 if (om != 0.0) {
451 for (f = 0; f < p->fdi; f++)
452 s->_lat[i] += delta[i + f]/om;
453 printf("Adjusting latice index %d by %f to give %f\n",i, delta[i]/om, s->_lat[i]);
454 }
455 }
456
457 /* Done with temporary arrays */
458 free(omega);
459 free(delta);
460 }
461
462 /* Return the interpolated value for a given point */
463 /* Return NZ if input point is out of range */
464 static int lookup_srbs(
465 mrbs *p,
466 co *c /* Point to interpolate */
467 ) {
468 srbs *s = p->s;
469 int e, f;
470 int ix; /* Latice index of base of neighborhood */
471 double b[MXDI][4]; /* B-spline basis factors for each dimension */
472 int nn; /* Neighbor counter */
473
474 /* Figure out our neighborhood */
475 for (ix = e = 0; e < p->di; e++) {
476 int x;
477 double t, sp, fp;
478 sp = c->p[e];
479 if (sp < p->l[e] || sp > p->h[e])
480 return 1;
481 sp = (sp - p->l[e])/s->w[e]; /* Scaled position */
482 fp = floor(sp);
483 x = (int)(fp - 1.0); /* Grid coordinate */
484 ix += s->coi[e] * x; /* Accume latice offset */
485 t = sp - fp; /* Spline parameter */
486 basis(b[e], t); /* Compute basis function values */
487 }
488
489 /* Compute the the current output value. */
490 for (f = 0; f < p->fdi; f++)
491 c->v[f] = 0.0;
492 for (nn = 0; nn < s->nsize; nn++) {
493 double w;
494 for (w = 1.0, e = 0; e < p->di; e++)
495 w *= b[e][s->n[nn].c[e]];
496 for (f = 0; f < p->fdi; f++)
497 c->v[f] += w * s->lat[ix + s->n[nn].xo + f]; /* Accume spline value */
498 }
499
500 return 0;
501 }
502
503 /* Take a list of scattered data points, */
504 /* and setup the mrbs. */
505 void set_mrbs(
506 mrbs *p, /* mrbs to set up */
507 co *pts, /* scattered data points (taken) */
508 int npts, /* number of scattered data points */
509 double *l, /* Input data range, low (May be NULL) */
510 double *h /* Input data range, high (May be NULL) */
511 ) {
512 int res;
513 int i, e, f;
514 srbs *s0 = NULL, *s1;
515
516 /* Establish the input data range */
517 for (e = 0; e < p->di; e++) {
518 if (l == NULL)
519 p->l[e] = 1e60;
520 else
521 p->l[e] = l[e];
522 if (h == NULL)
523 p->h[e] = -1e60;
524 else
525 p->h[e] = h[e];
526 }
527 for (i = 0; i < npts; i++) {
528 for (e = 0; e < p->di; e++) {
529 if (pts[i].p[e] < p->l[e])
530 p->l[e] = pts[i].p[e];
531 if (pts[i].p[e] > p->h[e])
532 p->h[e] = pts[i].p[e];
533 }
534 }
535
536 /* Take ownership the data */
537 p->pts = pts;
538 p->npts = npts;
539
540 /* Create an initial srbs */
541 res = 2;
542 if ((s1 = new_srbs(p, 2)) == NULL)
543 error("new_srbs failed");
544
545 /* Set it up with a linear first approximation */
546 linear_srbs(s1);
547 dump_srbs(s1);
548
549 /* Build up the resolution */
550 for (res = 2 * res -1; res < p->tres; res = 2 * res -1) {
551
552 printf("~1 doing resolution %d\n",res);
553 delete_srbs(s0);
554 s0 = s1;
555 if ((s1 = new_srbs(p, res)) == NULL)
556 error("new_srbs failed");
557
558 refine_srbs(s1, s0);
559 dump_srbs(s1);
560 improve_srbs(s1);
561 }
562
563 delete_srbs(s0);
564 p->s = s1; /* Final resolution */
565 }
566
567
568 /* - - - - - - - - - - - - - - - - */
569
570 /* Some test points */
571 #define NP 5
572 co tpts[NP] = {
573 { { 1, 5 }, { 3 } },
574 { { 2, 9 }, { 6 } },
575 { { 8, 0 }, { 1 } },
576 { { 5, 4 }, { 10 } },
577 { { 3, 9 }, { 4 } }
578 };
579
580 #define FRES 33
581
582 int
583 main(void) {
584 double ll[MXDI], hh[MXDI];
585 mrbs *p;
586 int i;
587
588 #ifdef NEVER /* Make points be on a plane */
589 {
590 for (i = 0; i < NP; i++) {
591 tpts[i].v[0] = 0.5 * tpts[i].p[0] + 0.7 * tpts[i].p[1] + 3.0;
592 printf("%f, %f -> %f\n", tpts[i].p[0], tpts[i].p[1], tpts[i].v[0]);
593 }
594 }
595 #endif
596 error_program = "stest";
597
598 printf("Starting test\n");
599 p = new_mrbs(2, 1, FRES, 1.0);
600
601 ll[0] = 0.0;
602 ll[1] = 0.0;
603 hh[0] = 10.0;
604 hh[1] = 10.0;
605 set_mrbs(p, tpts, NP, ll, hh);
606
607 printf("Interpolation setup\n");
608
609 /* Check out the accuracy */
610 for (i = 0; i < NP; i++) {
611 co tp;
612 tp.p[0] = tpts[i].p[0];
613 tp.p[1] = tpts[i].p[1];
614 if (lookup_srbs(p, &tp))
615 printf("Point %d, %f %f outside domain\n",i,tpts[i].p[0],tpts[i].p[1]);
616 else {
617 printf("Point %d has value %f, should be %f\n",i,tp.v[0], tpts[i].v[0]);
618 }
619 }
620
621 printf("Test done\n");
622 return 0;
623 }
624
625 #ifndef NUMSUP_H
626 /* Basic printf type error() and warning() routines */
627
628 void
629 error(char *fmt, ...)
630 {
631 va_list args;
632
633 fprintf(stderr,"stest: Error - ");
634 va_start(args, fmt);
635 vfprintf(stderr, fmt, args);
636 va_end(args);
637 fprintf(stderr, "\n");
638 exit (-1);
639 }
640
641 void
642 warning(char *fmt, ...)
643 {
644 va_list args;
645
646 fprintf(stderr,"stest: Warning - ");
647 va_start(args, fmt);
648 vfprintf(stderr, fmt, args);
649 va_end(args);
650 fprintf(stderr, "\n");
651 }
652
653 #endif /* NUMSUP_H */
654