| 1 | |
| 2 | /* |
| 3 | * Argyll Color Management System |
| 4 | * Multi-dimensional multilevel spline data fitter |
| 5 | * mlbs base version. |
| 6 | * |
| 7 | * Author: Graeme W. Gill |
| 8 | * Date: 2000/11/10 |
| 9 | * |
| 10 | * Copyright 1996 - 2000 Graeme W. Gill |
| 11 | * All rights reserved. |
| 12 | * This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :- |
| 13 | * see the License.txt file for licencing details. |
| 14 | */ |
| 15 | |
| 16 | /* This file contains the scattered data solution specific code */ |
| 17 | |
| 18 | /* TTBD: |
| 19 | * |
| 20 | * mlbs code doesn't work. Results are rubbish. |
| 21 | * |
| 22 | * Fix bugs ? |
| 23 | * merge stest.c into this file. |
| 24 | * |
| 25 | * Get rid of error() calls - return status instead |
| 26 | */ |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <stdarg.h> |
| 31 | #include <math.h> |
| 32 | #include <time.h> |
| 33 | |
| 34 | #include "rspl_imp.h" |
| 35 | #include "numlib.h" |
| 36 | #include "mlbs.h" |
| 37 | |
| 38 | extern void error(char *fmt, ...), warning(char *fmt, ...); |
| 39 | |
| 40 | #undef DEBUG |
| 41 | |
| 42 | #undef NEVER |
| 43 | #define ALWAYS |
| 44 | |
| 45 | /* Implemented in rspl.c: */ |
| 46 | extern void alloc_grid(rspl *s); |
| 47 | |
| 48 | extern int is_mono(rspl *s); |
| 49 | |
| 50 | /* ============================================ */ |
| 51 | void set_from_mlbs(void *cbctx, double *out, double *in) { |
| 52 | mlbs *p = (mlbs *)cbctx; |
| 53 | co tp; |
| 54 | int i; |
| 55 | |
| 56 | for (i = 0; i < p->di; i++) |
| 57 | tp.p[i] = in[i]; |
| 58 | |
| 59 | if (p->lookup(p, &tp)) |
| 60 | error("Internal, set_from_mlbs failed!"); |
| 61 | |
| 62 | for (i = 0; i < p->di; i++) |
| 63 | out[i] = tp.v[i]; |
| 64 | } |
| 65 | |
| 66 | /* Initialise the regular spline from scattered data */ |
| 67 | /* Return non-zero if non-monotonic */ |
| 68 | static int |
| 69 | fit_rspl_imp( |
| 70 | rspl *s, /* this */ |
| 71 | int flags, /* Combination of flags */ |
| 72 | void *d, /* Array holding position and function values of data points */ |
| 73 | int dtp, /* Flag indicating data type, 0 = (co *), 1 = (cow *) */ |
| 74 | int dno, /* Number of data points */ |
| 75 | datai glow, /* Grid low scale - will be expanded to enclose data, NULL = default 0.0 */ |
| 76 | datai ghigh, /* Grid high scale - will be expanded to enclose data, NULL = default 1.0 */ |
| 77 | int gres, /* Spline grid resolution */ |
| 78 | datao vlow, /* Data value low normalize, NULL = default 0.0 */ |
| 79 | datao vhigh, /* Data value high normalize - NULL = default 1.0 */ |
| 80 | double smooth /* Smoothing factor, nominal = 1.0 */ |
| 81 | ) { |
| 82 | int di = s->di, fdi = s->fdi; |
| 83 | int i, n, e, f; |
| 84 | int rv; |
| 85 | int nigc; |
| 86 | int bres; |
| 87 | mlbs *p; |
| 88 | |
| 89 | /* set debug level */ |
| 90 | s->debug = (flags >> 24); |
| 91 | |
| 92 | /* Init other flags */ |
| 93 | if (flags & RSPL_NONMON) /* Enable elimination of non-monoticities */ |
| 94 | s->nm = 1; |
| 95 | else |
| 96 | s->nm = 0; |
| 97 | |
| 98 | if (flags & RSPL_VERBOSE) /* Turn on progress messages to stdout */ |
| 99 | s->verbose = 1; |
| 100 | else |
| 101 | s->verbose = 0; |
| 102 | |
| 103 | /* Save smoothing factor */ |
| 104 | s->smooth = smooth; |
| 105 | |
| 106 | /* Stash the data points away */ |
| 107 | s->d.no = dno; /* Number of data points */ |
| 108 | |
| 109 | /* Allocate the scattered data space */ |
| 110 | if ((s->d.a = (dpnts *) malloc(sizeof(dpnts) * s->d.no)) == NULL) |
| 111 | error("rspl malloc failed - data points"); |
| 112 | |
| 113 | if (dtp == 0) { /* Default weight */ |
| 114 | co *dp = (co *)d; |
| 115 | |
| 116 | /* Copy the list into data points */ |
| 117 | for (n = 0; n < s->d.no; n++) { |
| 118 | for (e = 0; e < s->di; e++) |
| 119 | s->d.a[n].p[e] = dp[n].p[e]; |
| 120 | for (f = 0; f < s->fdi; f++) |
| 121 | s->d.a[n].v[f] = dp[n].v[f]; |
| 122 | s->d.a[n].k = 1.0; /* Assume all data points have same weight */ |
| 123 | } |
| 124 | } else { /* Per data point weight */ |
| 125 | cow *dp = (cow *)d; |
| 126 | |
| 127 | /* Copy the list into data points */ |
| 128 | for (n = 0; n < s->d.no; n++) { |
| 129 | for (e = 0; e < s->di; e++) |
| 130 | s->d.a[n].p[e] = dp[n].p[e]; |
| 131 | for (f = 0; f < s->fdi; f++) |
| 132 | s->d.a[n].v[f] = dp[n].v[f]; |
| 133 | s->d.a[n].k = dp[n].w; /* Weight specified */ |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /* Compute target B-Spline resolution */ |
| 138 | /* Make it worst case half the target rspl resolution */ |
| 139 | for (bres = 2; (2 * bres) < gres; bres = 2 * bres -1) |
| 140 | ; |
| 141 | |
| 142 | /* Create multilevel B-Spline fit */ |
| 143 | p = new_mlbs(di, fdi, bres, s->d.a, s->d.no, glow, ghigh, smooth); |
| 144 | if (p == NULL) |
| 145 | error("new_mlbs() failed"); |
| 146 | |
| 147 | /* Create rspl grid points by looking up the B-Spline values */ |
| 148 | rv = s->set_rspl(s, 0, (void *)p, set_from_mlbs, glow, ghigh, gres, vlow, vhigh); |
| 149 | |
| 150 | /* Don't need B-Spline any more */ |
| 151 | p->del(p); |
| 152 | |
| 153 | return rv; |
| 154 | } |
| 155 | |
| 156 | /* Initialise the regular spline from scattered data */ |
| 157 | /* Return non-zero if non-monotonic */ |
| 158 | int |
| 159 | fit_rspl( |
| 160 | rspl *s, /* this */ |
| 161 | int flags, /* Combination of flags */ |
| 162 | co *d, /* Array holding position and function values of data points */ |
| 163 | int dno, /* Number of data points */ |
| 164 | datai glow, /* Grid low scale - will be expanded to enclose data, NULL = default 0.0 */ |
| 165 | datai ghigh, /* Grid high scale - will be expanded to enclose data, NULL = default 1.0 */ |
| 166 | int gres, /* Spline grid resolution */ |
| 167 | datao vlow, /* Data value low normalize, NULL = default 0.0 */ |
| 168 | datao vhigh, /* Data value high normalize - NULL = default 1.0 */ |
| 169 | double smooth /* Smoothing factor, nominal = 1.0 */ |
| 170 | ) { |
| 171 | /* Call implementation with (co *) data */ |
| 172 | return fit_rspl_imp(s, flags, (void *)d, 0, dno, glow, ghigh, gres, vlow, vhigh, smooth); |
| 173 | } |
| 174 | |
| 175 | /* Initialise the regular spline from scattered data with weights */ |
| 176 | /* Return non-zero if non-monotonic */ |
| 177 | int |
| 178 | fit_rspl_w( |
| 179 | rspl *s, /* this */ |
| 180 | int flags, /* Combination of flags */ |
| 181 | cow *d, /* Array holding position, function and weight values of data points */ |
| 182 | int dno, /* Number of data points */ |
| 183 | datai glow, /* Grid low scale - will be expanded to enclose data, NULL = default 0.0 */ |
| 184 | datai ghigh, /* Grid high scale - will be expanded to enclose data, NULL = default 1.0 */ |
| 185 | int gres, /* Spline grid resolution */ |
| 186 | datao vlow, /* Data value low normalize, NULL = default 0.0 */ |
| 187 | datao vhigh, /* Data value high normalize - NULL = default 1.0 */ |
| 188 | double smooth /* Smoothing factor, nominal = 1.0 */ |
| 189 | ) { |
| 190 | /* Call implementation with (cow *) data */ |
| 191 | return fit_rspl_imp(s, flags, (void *)d, 1, dno, glow, ghigh, gres, vlow, vhigh, smooth); |
| 192 | } |
| 193 | |
| 194 | /* Init scattered data elements in rspl */ |
| 195 | void |
| 196 | init_data(rspl *s) { |
| 197 | s->d.no = 0; |
| 198 | s->d.a = NULL; |
| 199 | s->fit_rspl = fit_rspl; |
| 200 | s->fit_rspl_w = fit_rspl_w; |
| 201 | } |
| 202 | |
| 203 | /* Free the scattered data allocation */ |
| 204 | void |
| 205 | free_data(rspl *s) { |
| 206 | if (s->d.a != NULL) { |
| 207 | free((void *)s->d.a); |
| 208 | s->d.a = NULL; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /* ============================================ */ |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 |