Debian Codegraph @

rspl/rspl1.c

raw symbols SCIP
1
2 /* Single dimension regularized spline data structure */
3
4 /*
5 * Argyll Color Management System
6 * Author: Graeme W. Gill
7 * Date: 2000/10/29
8 *
9 * Copyright 1996 - 2010 Graeme W. Gill
10 * All rights reserved.
11 *
12 * This material is licenced under the GNU GENERAL PUBLIC LICENSE Version 2 or later :-
13 * see the License2.txt file for licencing details.
14 *
15 * This is a simple 1D version of rspl, useful for standalone purposes.
16 *
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <math.h>
23 #include "numsup.h"
24 #include "rspl1.h"
25
26 #undef DEBUG
27
28 #ifdef DEBUG
29 # define DBGA g_log, 0 /* First argument to DBGF() */
30 # define DBGF(xx) a1logd xx
31 #else
32 # define DBGF(xx)
33 #endif
34
35
36 /* Do an interpolation based on the grid */
37 /* Use a linear interp between grid points. */
38 /* If the input is outside the grid range, it will */
39 /* be clamped to the nearest grid point. */
40 static int interp(
41 rspl *t,
42 co *p
43 ) {
44 int rv = 0;
45 double x, y, xx, w1;
46 int i;
47
48 x = p->p[0];
49
50 if (x < t->gl) {
51 x = t->gl;
52 rv = 1;
53 } else if (x > t->gh) {
54 x = t->gh;
55 rv = 1;
56 }
57
58 xx = (x - t->gl)/t->gw; /* Grid location of point */
59 i = (int)floor(xx); /* Lower grid of point */
60 if (i >= (t->nig-2))
61 i = t->nig-2;
62
63 w1 = xx - (double)i; /* Weight to upper grid point */
64
65 y = ((1.0 - w1) * t->x[i]) + (w1 * t->x[i+1]);
66
67 p->v[0] = y * t->vw + t->vl; /* Rescale the data */
68
69 return rv;
70 }
71
72 /* Use a linear interp between grid points. */
73 /* If the input is outside the grid range, it will */
74 /* be clamped to the nearest grid point. */
75 static double interp1(rspl *t, double in) {
76 co p = { 0 };
77
78 p.p[0] = in;
79 t->interp(t, &p);
80 return p.v[0];
81 }
82
83
84 /* Destructor */
85 static void del_rspl(rspl *t) {
86 if (t != NULL) {
87 if (t->x != NULL)
88 free_dvector(t->x, 0, t->nig);
89 free(t);
90 }
91 }
92
93 /* Initialise the regular spline from scattered data */
94 /* Return nz on error */
95 static int fit_rspl_imp(
96 struct _rspl *t,/* this */
97 int flags, /* (Not used) */
98 void *d, /* Array holding position and function values of data points */
99 int dtp, /* Flag indicating data type, 0 = (co *), 1 = (cow *), 2 = (coww *) */
100 int ndp, /* Number of data points */
101 datai glow, /* Grid low scale - will expand to enclose data, NULL = default 0.0 */
102 datai ghigh, /* Grid high scale - will expand to enclose data, NULL = default 1.0 */
103 int *gres, /* Spline grid resolution, ncells = gres-1 */
104 datao vlow, /* Data value low normalize, NULL = default 0.0 */
105 datao vhigh, /* Data value high normalize - NULL = default 1.0 */
106 double smooth, /* Smoothing factor, 0.0 = default 1.0 */
107 double *avgdev, /* (Not used) */
108 double **ipos /* (not used) */
109 ) {
110 int n;
111 double cw;
112
113 DBGF((DBGA, "rspl1:fit_rspl_imp() with %d points called, dtp = %d\n",ndp,dtp));
114
115 /* Allocate space for interpolation grid */
116 t->nig = *gres;
117
118 if ((t->x = dvector(0, t->nig)) == NULL) {
119 DBGF((DBGA, "rspl1:Malloc of vector x failed\n"));
120 return 1;
121 }
122
123 /* Normalize curve weight to grid resolution. */
124 cw = 0.0000005 * smooth * pow((t->nig-1),4.0) / (t->nig - 2);
125 DBGF((DBGA, "rspl1:cw = %e\n",cw));
126
127 /* cw is multiplied by the sum of grid curvature errors squared to keep */
128 /* the same ratio with the sum of data position errors squared */
129
130 /* Determine the data range */
131 t->xl = 1e300;
132 t->xh = -1e300;
133 t->dl = 1e300;
134 t->dh = -1e300;
135 if (dtp == 0) {
136 co *dd = (co *)d;
137
138 for (n = 0; n < ndp; n++) {
139 if (dd[n].p[0] < t->xl)
140 t->xl = dd[n].p[0];
141 if (dd[n].p[0] > t->xh)
142 t->xh = dd[n].p[0];
143 if (dd[n].v[0] < t->dl)
144 t->dl = dd[n].v[0];
145 if (dd[n].v[0] > t->dh)
146 t->dh = dd[n].v[0];
147
148 DBGF((DBGA, "rspl1:Point %d = %f, %f\n",n,dd[n].p[0],dd[n].v[0]));
149 }
150 } else if (dtp == 1) {
151 cow *dd = (cow *)d;
152
153 for (n = 0; n < ndp; n++) {
154 if (dd[n].p[0] < t->xl)
155 t->xl = dd[n].p[0];
156 if (dd[n].p[0] > t->xh)
157 t->xh = dd[n].p[0];
158 if (dd[n].v[0] < t->dl)
159 t->dl = dd[n].v[0];
160 if (dd[n].v[0] > t->dh)
161 t->dh = dd[n].v[0];
162 DBGF((DBGA, "rspl1:Point %d = %f, %f (%f)\n",n,dd[n].p[0],dd[n].v[0],dd[n].w));
163 }
164 } else {
165 DBGF((DBGA, "rspl1:Internal error, unknown dtp value %d\n",dtp));
166 return 1;
167 }
168
169 t->gl = glow != NULL ? *glow : 0.0;
170 t->gh = ghigh != NULL ? *ghigh : 1.0;
171
172 /* adjust input ranges to encompass data */
173 if (t->xl < t->gl)
174 t->gl = t->xl;
175 if (t->xh > t->gh)
176 t->gh = t->xh;
177
178 /* Set the input and output scaling */
179 t->gw = (t->gh - t->gl)/(double)(t->nig-1);
180
181 t->vl = vlow != NULL ? *vlow : 0.0;
182 t->vw = ((vhigh != NULL ? *vhigh : 1.0) - t->vl);
183
184 DBGF((DBGA, "rspl1:gl %f, gh %f, gw %f, vl %f, vw %f\n",t->gl,t->gh,t->gw,t->vl,t->vw));
185
186 /* create smoothed grid data */
187 {
188 int n,i,k;
189 double **A; /* A matrix of interpoint weights */
190 double *b; /* b vector for RHS of simultabeous equation */
191
192 /* We just store the diagonal of the A matrix */
193 if ((A = dmatrix(0, t->nig, 0, 2)) == NULL) {
194 DBGF((DBGA, "rspl1:Malloc of matrix A failed\n"));
195 return 1;
196 }
197
198 if ((b = dvector(0,t->nig)) == NULL) {
199 free_dvector(b,0,t->nig);
200 DBGF((DBGA, "rspl1:Malloc of vector b failed\n"));
201 return 1;
202 }
203
204 /* Initialize the A and b matricies */
205 for (i = 0; i < t->nig; i++) {
206 for (k = 0; k < 3; k++)
207 A[i][k] = 0.0;
208 t->x[i] = b[i] = 0.0;
209 }
210
211 /* Accumulate data dependent factors */
212 for (n = 0; n < ndp; n++) {
213 double bf, cbf;
214 double xv, yv, wv;
215
216 if (dtp == 0) {
217 co *dd = (co *)d;
218
219 xv = dd[n].p[0];
220 yv = dd[n].v[0];
221 wv = 1.0;
222 } else if (dtp == 1) {
223 cow *dd = (cow *)d;
224
225 xv = dd[n].p[0];
226 yv = dd[n].v[0];
227 wv = dd[n].w;
228 } else {
229 DBGF((DBGA, "rspl1:Internal error, unknown dtp value %d\n",dtp));
230 return 1;
231 }
232 yv = (yv - t->vl)/t->vw; /* Normalize the value */
233
234 /* Figure out which grid cell data is in */
235 i = (int)((xv - t->gl)/t->gw); /* Index of next lowest data point */
236
237 bf = ((((double)(i+1) * t->gw) + t->gl) - xv)/t->gw; /* weight to lower grid point */
238 cbf = 1.0 - bf; /* weight to upper grid point */
239
240 b[i] -= 2.0 * bf * -yv * wv; /* dui component due to dn */
241 A[i][0] += 2.0 * bf * bf * wv; /* dui component due to ui */
242 A[i][1] += 2.0 * bf * cbf * wv; /* dui component due to ui+1 */
243
244 if ((i+1) < t->nig) {
245 b[i+1] -= 2.0 * cbf * -yv * wv; /* dui component due to dn */
246 A[i+1][0] += 2.0 * cbf * cbf * wv; /* dui component due to ui */
247 }
248 }
249
250 /* Accumulate curvature dependent factors */
251 for (i = 0; i < t->nig; i++) {
252
253 if ((i-2) >= 0) { /* Curvature of cell below */
254 A[i][0] += 2.0 * cw;
255 }
256
257 if ((i-1) >= 0 && (i+1) < t->nig) { /* Curvature of t cell */
258 A[i][0] += 8.0 * cw;
259 A[i][1] += -4.0 * cw;
260 }
261 if ((i+2) < t->nig) { /* Curvature of cell above */
262 A[i][0] += 2.0 * cw;
263 A[i][1] += -4.0 * cw;
264 A[i][2] += 2.0 * cw;
265 }
266 }
267
268 #ifdef DEBUG
269 DBGF((DBGA, "A matrix:\n"));
270 for (i = 0; i < t->nig; i++) {
271 for (k = 0; k < 3; k++)
272 DBGF((DBGA, "A[%d][%d] = %f\n",i,k,A[i][k]));
273 }
274 DBGF((DBGA, "b vector:\n"));
275 for (i = 0; i < t->nig; i++)
276 DBGF((DBGA, "b[%d] = %f\n",i,b[i]));
277 #endif /* DEBUG */
278
279 /* Apply Cholesky decomposition to A[][] to create L[][] */
280 for (i = 0; i < t->nig; i++) {
281 double sm;
282 for (n = 0; n < 3; n++) {
283 sm = A[i][n];
284 for (k = 1; (n+k) < 3 && (i-k) >=0; k++) {
285 sm -= A[i-k][n+k] * A[i-k][k];
286 }
287 if (n == 0) {
288 if (sm <= 0.0) {
289 free_dvector(b,0,t->nig);
290 free_dmatrix(A,0,t->nig,0,2);
291 DBGF((DBGA, "rspl1:Sum is -ve - loss of accuracy ?\n"));
292 return 1;
293 }
294 A[i][0] = sqrt(sm);
295 } else {
296 A[i][n] = sm/A[i][0];
297 }
298 }
299 }
300
301 /* Solve L . y = b, storing y in x */
302 for (i = 0; i < t->nig; i++) {
303 double sm;
304 sm = b[i];
305 for (k = 1; k < 3 && (i-k) >= 0; k++) {
306 sm -= A[i-k][k] * t->x[i-k];
307 }
308 t->x[i] = sm/A[i][0];
309 }
310
311 /* Solve LT . x = y */
312 for (i = t->nig-1; i >= 0; i--) {
313 double sm;
314 sm = t->x[i];
315 for (k = 1; k < 3 && (i+k) < t->nig; k++) {
316 sm -= A[i][k] * t->x[i+k];
317 }
318 t->x[i] = sm/A[i][0];
319 }
320 #ifdef DEBUG
321 DBGF((DBGA, "Solution vector:\n"));
322 for (i = 0; i < t->nig; i++) {
323 DBGF((DBGA, "x[%d] = %f\n",i,t->x[i]));
324 }
325 #endif /* DEBUG */
326
327 free_dvector(b,0,t->nig);
328 free_dmatrix(A,0,t->nig,0,2);
329 }
330 return 0;
331 }
332
333 /* Initialise from scattered data. */
334 /* Return nz on error */
335 static int fit_rspl(
336 struct _rspl *t,/* this */
337 int flags, /* (Not used) */
338 co *d, /* Array holding position and function values of data points */
339 int ndp, /* Number of data points */
340 datai glow, /* Grid low scale - will expand to enclose data, NULL = default 0.0 */
341 datai ghigh, /* Grid high scale - will expand to enclose data, NULL = default 1.0 */
342 int *gres, /* Spline grid resolution, ncells = gres-1 */
343 datao vlow, /* Data value low normalize, NULL = default 0.0 */
344 datao vhigh, /* Data value high normalize - NULL = default 1.0 */
345 double smooth, /* Smoothing factor, 0.0 = default 1.0 */
346 double *avgdev, /* (Not used) */
347 double **ipos /* (not used) */
348 ) {
349 /* Call implementation with (co *) data */
350 return fit_rspl_imp(t, flags, (void *)d, 0, ndp, glow, ghigh, gres, vlow, vhigh,
351 smooth, avgdev, ipos);
352 }
353
354 /* Return a pointer to the resolution array */
355 static int *get_res(rspl *s) {
356 return &s->nig;
357 }
358
359 /* Initialise the regular spline from scattered data with weights */
360 /* Return nz on error */
361 static int
362 fit_rspl_w(
363 rspl *t, /* this */
364 int flags, /* Combination of flags */
365 cow *d, /* Array holding position, function and weight values of data points */
366 int dno, /* Number of data points */
367 ratai glow, /* Grid low scale - will be expanded to enclose data, NULL = default 0.0 */
368 ratai ghigh, /* Grid high scale - will be expanded to enclose data, NULL = default 1.0 */
369 int *gres, /* Spline grid resolution */
370 ratao vlow, /* Data value low normalize, NULL = default 0.0 */
371 ratao vhigh, /* Data value high normalize - NULL = default 1.0 */
372 double smooth, /* Smoothing factor, 0.0 = default 1.0 */
373 double *avgdev, /* (Not used) */
374 double **ipos /* (not used) */
375 ) {
376 /* Call implementation with (cow *) data */
377 return fit_rspl_imp(t, flags, (void *)d, 1, dno, glow, ghigh, gres, vlow, vhigh,
378 smooth, avgdev, ipos);
379 }
380
381 /* Initialize the grid from a provided function. */
382 /* Grid index values are supplied "under" in[] at *((int*)&iv[-e-1]) */
383 static int set_rspl(
384 struct _rspl *s,/* this */
385 int flags, /* (Not used) */
386 void *cbctx, /* Opaque function context */
387 void (*func)(void *cbctx, double *out, double *in), /* Function to set from */
388 datai glow, /* Grid low scale, NULL = default 0.0 */
389 datai ghigh, /* Grid high scale, NULL = default 1.0 */
390 int *gres, /* Spline grid resolution for each dimension */
391 datao vlow, /* Data value low normalize, NULL = default 0.0 */
392 datao vhigh /* Data value high normalize, NULL = default 1.0 */
393 ) {
394 int n;
395 double _iv[2 * MXDI], *iv = &_iv[MXDI]; /* Real index value/table value */
396 double ov[MXDO];
397
398 DBGF((DBGA, "rspl1:set_rspl() callen"));
399
400 /* Allocate space for interpolation grid */
401 s->nig = *gres;
402
403 if ((s->x = dvector(0, s->nig)) == NULL) {
404 DBGF((DBGA, "rspl1:Malloc of vector x failed\n"));
405 return 1;
406 }
407
408 s->xl = s->gl = glow != NULL ? *glow : 0.0;
409 s->xh = s->gh = ghigh != NULL ? *ghigh : 1.0;
410
411 /* Set the input scaling */
412 s->gw = (s->gh - s->gl)/(double)(s->nig-1);
413
414 /* Set the default output scaling */
415 s->vl = vlow != NULL ? *vlow : 0.0;
416 s->vw = ((vhigh != NULL ? *vhigh : 1.0) - s->vl);
417
418 DBGF((DBGA, "rspl1:gl %f, gh %f, gw %f, vl %f, vw %f\n",s->gl,s->gh,s->gw,s->vl,s->vw));
419
420 /* Lookup the values at the grid points */
421 for (n = 0; n < s->nig; n++) {
422 double vv;
423
424 /* Compute grid pointer and input sample values */
425 iv[0] = s->gl + n * s->gw; /* Input sample values */
426 *((int *)&iv[-1-1]) = n; /* Trick to supply grid index in iv[] */
427
428 /* Apply incolor -> outcolor function we want to represent */
429 func(cbctx, ov, iv);
430
431 s->x[n] = (float)ov[0]; /* Set unscaled output value */
432
433 if (s->x[n] < s->dl)
434 s->dl = s->x[n];
435 if (s->x[n] > s->dh)
436 s->dh = s->x[n];
437 }
438
439 /* Adjust output scaling */
440 s->vw += s->vl; /* Convert to high */
441 if (s->dl < s->vl)
442 s->vl = s->dl;
443 if (s->dh < s->vw)
444 s->vw = s->dh;
445 s->vw -= s->vl; /* Convert to width */
446
447 /* Apply scaling to data */
448 for (n = 0; n < s->nig; n++) {
449 s->x[n] = (s->x[n] - s->vl)/s->vw;
450 }
451
452 return 0;
453 }
454
455 /* Construct an empty rspl1 */
456 /* Return NULL if something goes wrong. */
457 rspl *new_rspl(int flags, int di, int fdi) {
458 rspl *t; /* this */
459
460 if (flags != RSPL_NOFLAGS || di != 1 || fdi != 1) {
461 DBGF((DBGA, "rspl1:Can't handle general rspl: flags %d, di %d, do %d\n",flags,di,fdi));
462 return NULL;
463 }
464
465 if ((t = (rspl *)calloc(1, sizeof(rspl))) == NULL) {
466 DBGF((DBGA, "rspl1:Malloc of structure failed\n"));
467 return NULL;
468 }
469
470 /* Initialise the classes methods */
471 t->interp = interp;
472 t->interp1 = interp1;
473 t->fit_rspl = fit_rspl;
474 t->fit_rspl_w = fit_rspl_w;
475 t->set_rspl = set_rspl;
476 t->get_res = get_res;
477 t->del = del_rspl;
478
479 return t;
480 }
481
482
483
484
485