Debian Codegraph @

rspl/mlbs.c

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