Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: GDAL Warp API
4 : * Purpose: Implemenentation of 2D Thin Plate Spline transformer.
5 : * Author: VIZRT Development Team.
6 : *
7 : * This code was provided by Gilad Ronnen (gro at visrt dot com) with
8 : * permission to reuse under the following license.
9 : *
10 : ******************************************************************************
11 : * Copyright (c) 2004, VIZRT Inc.
12 : * Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys.com>
13 : *
14 : * SPDX-License-Identifier: MIT
15 : ****************************************************************************/
16 :
17 : /*! @cond Doxygen_Suppress */
18 :
19 : #include "cpl_port.h"
20 : #include "thinplatespline.h"
21 : #include "gdallinearsystem.h"
22 :
23 : #include <climits>
24 : #include <cstdio>
25 : #include <cstring>
26 :
27 : #include <algorithm>
28 : #include <limits>
29 : #include <utility>
30 :
31 : #include "cpl_error.h"
32 : #include "cpl_vsi.h"
33 :
34 : //////////////////////////////////////////////////////////////////////////////
35 : //// vizGeorefSpline2D
36 : //////////////////////////////////////////////////////////////////////////////
37 :
38 : // #define VIZ_GEOREF_SPLINE_DEBUG 0
39 :
40 108 : bool VizGeorefSpline2D::grow_points()
41 :
42 : {
43 108 : const int new_max = _max_nof_points * 2 + 2 + 3;
44 :
45 : double *new_x =
46 108 : static_cast<double *>(VSI_REALLOC_VERBOSE(x, sizeof(double) * new_max));
47 108 : if (!new_x)
48 0 : return false;
49 108 : x = new_x;
50 : double *new_y =
51 108 : static_cast<double *>(VSI_REALLOC_VERBOSE(y, sizeof(double) * new_max));
52 108 : if (!new_y)
53 0 : return false;
54 108 : y = new_y;
55 : double *new_u =
56 108 : static_cast<double *>(VSI_REALLOC_VERBOSE(u, sizeof(double) * new_max));
57 108 : if (!new_u)
58 0 : return false;
59 108 : u = new_u;
60 : int *new_unused =
61 108 : static_cast<int *>(VSI_REALLOC_VERBOSE(unused, sizeof(int) * new_max));
62 108 : if (!new_unused)
63 0 : return false;
64 108 : unused = new_unused;
65 : int *new_index =
66 108 : static_cast<int *>(VSI_REALLOC_VERBOSE(index, sizeof(int) * new_max));
67 108 : if (!new_index)
68 0 : return false;
69 108 : index = new_index;
70 324 : for (int i = 0; i < _nof_vars; i++)
71 : {
72 : double *rhs_i_new = static_cast<double *>(
73 216 : VSI_REALLOC_VERBOSE(rhs[i], sizeof(double) * new_max));
74 216 : if (!rhs_i_new)
75 0 : return false;
76 216 : rhs[i] = rhs_i_new;
77 : double *coef_i_new = static_cast<double *>(
78 216 : VSI_REALLOC_VERBOSE(coef[i], sizeof(double) * new_max));
79 216 : if (!coef_i_new)
80 0 : return false;
81 216 : coef[i] = coef_i_new;
82 216 : if (_max_nof_points == 0)
83 : {
84 76 : memset(rhs[i], 0, 3 * sizeof(double));
85 76 : memset(coef[i], 0, 3 * sizeof(double));
86 : }
87 : }
88 :
89 108 : _max_nof_points = new_max - 3;
90 108 : return true;
91 : }
92 :
93 4812 : bool VizGeorefSpline2D::add_point(const double Px, const double Py,
94 : const double *Pvars)
95 : {
96 4812 : type = VIZ_GEOREF_SPLINE_POINT_WAS_ADDED;
97 : int i;
98 :
99 4812 : if (_nof_points == _max_nof_points)
100 : {
101 70 : if (!grow_points())
102 0 : return false;
103 : }
104 :
105 4812 : i = _nof_points;
106 : // A new point is added.
107 4812 : x[i] = Px;
108 4812 : y[i] = Py;
109 14436 : for (int j = 0; j < _nof_vars; j++)
110 9624 : rhs[j][i + 3] = Pvars[j];
111 4812 : _nof_points++;
112 4812 : return true;
113 : }
114 :
115 : #if 0
116 : bool VizGeorefSpline2D::change_point( int index, double Px, double Py,
117 : double* Pvars )
118 : {
119 : if( index < _nof_points )
120 : {
121 : int i = index;
122 : x[i] = Px;
123 : y[i] = Py;
124 : for( int j = 0; j < _nof_vars; j++ )
125 : rhs[j][i+3] = Pvars[j];
126 : }
127 :
128 : return true;
129 : }
130 :
131 : bool VizGeorefSpline2D::get_xy( int index, double& outX, double& outY )
132 : {
133 : if( index < _nof_points )
134 : {
135 : ok = true;
136 : outX = x[index];
137 : outY = y[index];
138 : return true;
139 : }
140 :
141 : outX = 0.0;
142 : outY = 0.0;
143 :
144 : return false;
145 : }
146 :
147 : int VizGeorefSpline2D::delete_point( const double Px, const double Py )
148 : {
149 : for( int i = 0; i < _nof_points; i++ )
150 : {
151 : if( ( fabs(Px - x[i]) <= _tx ) && ( fabs(Py - y[i]) <= _ty ) )
152 : {
153 : for( int j = i; j < _nof_points - 1; j++ )
154 : {
155 : x[j] = x[j+1];
156 : y[j] = y[j+1];
157 : for( int k = 0; k < _nof_vars; k++ )
158 : rhs[k][j+3] = rhs[k][j+3+1];
159 : }
160 : _nof_points--;
161 : type = VIZ_GEOREF_SPLINE_POINT_WAS_DELETED;
162 : return 1;
163 : }
164 : }
165 : return 0;
166 : }
167 : #endif
168 :
169 63808660 : template <typename T> static inline T SQ(const T &x)
170 : {
171 63808660 : return x * x;
172 : }
173 :
174 4598680 : static inline double VizGeorefSpline2DBase_func(const double x1,
175 : const double y1,
176 : const double x2,
177 : const double y2)
178 : {
179 4598680 : const double dist = SQ(x2 - x1) + SQ(y2 - y1);
180 4598680 : return dist != 0.0 ? dist * log(dist) : 0.0;
181 : }
182 :
183 : #if defined(__GNUC__) && defined(__x86_64__)
184 : /* Some versions of ICC fail to compile VizGeorefSpline2DBase_func4 (#6350) */
185 : #if defined(__INTEL_COMPILER)
186 : #if __INTEL_COMPILER >= 1500
187 : #define USE_OPTIMIZED_VizGeorefSpline2DBase_func4
188 : #else
189 : #if (__INTEL_COMPILER == 1200) || (__INTEL_COMPILER == 1210)
190 : #define USE_OPTIMIZED_VizGeorefSpline2DBase_func4
191 : #else
192 : #undef USE_OPTIMIZED_VizGeorefSpline2DBase_func4
193 : #endif
194 : #endif
195 : #else // defined(__INTEL_COMPILER)
196 : #define USE_OPTIMIZED_VizGeorefSpline2DBase_func4
197 : #endif // defined(__INTEL_COMPILER)
198 : #endif
199 :
200 : #if defined(USE_OPTIMIZED_VizGeorefSpline2DBase_func4) && !defined(CPPCHECK)
201 :
202 : /* Derived and adapted from code originating from: */
203 :
204 : /* @(#)e_log.c 1.3 95/01/18 */
205 : /*
206 : * ====================================================
207 : * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
208 : *
209 : * Developed at SunSoft, a Sun Microsystems, Inc. business.
210 : * Permission to use, copy, modify, and distribute this
211 : * software is freely granted, provided that this notice
212 : * is preserved.
213 : * ====================================================
214 : */
215 :
216 : /* __ieee754_log(x)
217 : * Return the logarithm of x
218 : *
219 : * Method:
220 : * 1. Argument Reduction: find k and f such that
221 : * x = 2^k * (1+f),
222 : * where sqrt(2)/2 < 1+f < sqrt(2) .
223 : *
224 : * 2. Approximation of log(1+f).
225 : * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
226 : * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
227 : * = 2s + s*R
228 : * We use a special Reme algorithm on [0,0.1716] to generate
229 : * a polynomial of degree 14 to approximate R The maximum error
230 : * of this polynomial approximation is bounded by 2**-58.45. In
231 : * other words,
232 : * 2 4 6 8 10 12 14
233 : * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
234 : * (the values of Lg1 to Lg7 are listed in the program)
235 : * and
236 : * | 2 14 | -58.45
237 : * | Lg1*s +...+Lg7*s - R(z) | <= 2
238 : * | |
239 : * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
240 : * In order to guarantee error in log below 1ulp, we compute log
241 : * by
242 : * log(1+f) = f - s*(f - R) (if f is not too large)
243 : * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
244 : *
245 : * 3. Finally, log(x) = k*ln2 + log(1+f).
246 : * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
247 : * Here ln2 is split into two floating point number:
248 : * ln2_hi + ln2_lo,
249 : * where n*ln2_hi is always exact for |n| < 2000.
250 : *
251 : * Special cases:
252 : * log(x) is NaN with signal if x < 0 (including -INF) ;
253 : * log(+INF) is +INF; log(0) is -INF with signal;
254 : * log(NaN) is that NaN with no signal.
255 : *
256 : * Accuracy:
257 : * according to an error analysis, the error is always less than
258 : * 1 ulp (unit in the last place).
259 : *
260 : * Constants:
261 : * The hexadecimal values are the intended ones for the following
262 : * constants. The decimal values may be used, provided that the
263 : * compiler will convert from decimal to binary accurately enough
264 : * to produce the hexadecimal values shown.
265 : */
266 :
267 : typedef double V2DF __attribute__((__vector_size__(16)));
268 :
269 : typedef union
270 : {
271 : V2DF v2;
272 : double d[2];
273 : } v2dfunion;
274 :
275 : typedef union
276 : {
277 : int i[2];
278 : long long li;
279 : } i64union;
280 :
281 : static const V2DF v2_ln2_div_2pow20 = {6.93147180559945286e-01 / 1048576,
282 : 6.93147180559945286e-01 / 1048576};
283 : static const V2DF v2_Lg1 = {6.666666666666735130e-01, 6.666666666666735130e-01};
284 : static const V2DF v2_Lg2 = {3.999999999940941908e-01, 3.999999999940941908e-01};
285 : static const V2DF v2_Lg3 = {2.857142874366239149e-01, 2.857142874366239149e-01};
286 : static const V2DF v2_Lg4 = {2.222219843214978396e-01, 2.222219843214978396e-01};
287 : static const V2DF v2_Lg5 = {1.818357216161805012e-01, 1.818357216161805012e-01};
288 : static const V2DF v2_Lg6 = {1.531383769920937332e-01, 1.531383769920937332e-01};
289 : /*v2_Lg7 = {1.479819860511658591e-01, 1.479819860511658591e-01}, */
290 : static const V2DF v2_one = {1.0, 1.0};
291 : static const V2DF v2_const1023_mul_2pow20 = {1023.0 * 1048576,
292 : 1023.0 * 1048576};
293 :
294 : #define GET_HIGH_WORD(hx, x) memcpy(&hx, reinterpret_cast<char *>(&x) + 4, 4)
295 : #define SET_HIGH_WORD(x, hx) memcpy(reinterpret_cast<char *>(&x) + 4, &hx, 4)
296 :
297 : #define MAKE_WIDE_CST(x) (((static_cast<long long>(x)) << 32) | (x))
298 : constexpr long long cst_expmask = MAKE_WIDE_CST(0xfff00000);
299 : constexpr long long cst_0x95f64 = MAKE_WIDE_CST(0x00095f64);
300 : constexpr long long cst_0x100000 = MAKE_WIDE_CST(0x00100000);
301 : constexpr long long cst_0x3ff00000 = MAKE_WIDE_CST(0x3ff00000);
302 :
303 : // Modified version of __ieee754_log(), less precise than log() but a bit
304 : // faster, and computing 4 log() at a time. Assumes that the values are > 0.
305 13652800 : static void FastApproxLog4Val(v2dfunion *x)
306 : {
307 13652800 : i64union hx[2] = {};
308 13652800 : i64union k[2] = {};
309 13652800 : i64union i[2] = {};
310 13652800 : GET_HIGH_WORD(hx[0].i[0], x[0].d[0]);
311 13652800 : GET_HIGH_WORD(hx[0].i[1], x[0].d[1]);
312 :
313 : // coverity[uninit_use]
314 13652800 : k[0].li = hx[0].li & cst_expmask;
315 13652800 : hx[0].li &= ~cst_expmask;
316 13652800 : i[0].li = (hx[0].li + cst_0x95f64) & cst_0x100000;
317 13652800 : hx[0].li |= i[0].li ^ cst_0x3ff00000;
318 13652800 : SET_HIGH_WORD(x[0].d[0], hx[0].i[0]); // Normalize x or x/2.
319 13652800 : SET_HIGH_WORD(x[0].d[1], hx[0].i[1]); // Normalize x or x/2.
320 13652800 : k[0].li += i[0].li;
321 :
322 13652800 : v2dfunion dk[2] = {};
323 13652800 : dk[0].d[0] = static_cast<double>(k[0].i[0]);
324 13652800 : dk[0].d[1] = static_cast<double>(k[0].i[1]);
325 :
326 13652800 : GET_HIGH_WORD(hx[1].i[0], x[1].d[0]);
327 13652800 : GET_HIGH_WORD(hx[1].i[1], x[1].d[1]);
328 13652800 : k[1].li = hx[1].li & cst_expmask;
329 13652800 : hx[1].li &= ~cst_expmask;
330 13652800 : i[1].li = (hx[1].li + cst_0x95f64) & cst_0x100000;
331 13652800 : hx[1].li |= i[1].li ^ cst_0x3ff00000;
332 13652800 : SET_HIGH_WORD(x[1].d[0], hx[1].i[0]); // Normalize x or x/2.
333 13652800 : SET_HIGH_WORD(x[1].d[1], hx[1].i[1]); // Normalize x or x/2.
334 13652800 : k[1].li += i[1].li;
335 13652800 : dk[1].d[0] = static_cast<double>(k[1].i[0]);
336 13652800 : dk[1].d[1] = static_cast<double>(k[1].i[1]);
337 :
338 13652800 : V2DF f[2] = {};
339 13652800 : f[0] = x[0].v2 - v2_one;
340 13652800 : V2DF s[2] = {};
341 13652800 : s[0] = f[0] / (x[0].v2 + v2_one);
342 13652800 : V2DF z[2] = {};
343 13652800 : z[0] = s[0] * s[0];
344 13652800 : V2DF w[2] = {};
345 13652800 : w[0] = z[0] * z[0];
346 :
347 13652800 : V2DF t1[2] = {};
348 : // coverity[ptr_arith]
349 13652800 : t1[0] = w[0] * (v2_Lg2 + w[0] * (v2_Lg4 + w[0] * v2_Lg6));
350 :
351 13652800 : V2DF t2[2] = {};
352 : // coverity[ptr_arith]
353 13652800 : t2[0] =
354 13652800 : z[0] * (v2_Lg1 + w[0] * (v2_Lg3 + w[0] * (v2_Lg5 /*+w[0]*v2_Lg7*/)));
355 :
356 13652800 : V2DF R[2] = {};
357 13652800 : R[0] = t2[0] + t1[0];
358 13652800 : x[0].v2 = (dk[0].v2 - v2_const1023_mul_2pow20) * v2_ln2_div_2pow20 -
359 13652800 : (s[0] * (f[0] - R[0]) - f[0]);
360 :
361 13652800 : f[1] = x[1].v2 - v2_one;
362 13652800 : s[1] = f[1] / (x[1].v2 + v2_one);
363 13652800 : z[1] = s[1] * s[1];
364 13652800 : w[1] = z[1] * z[1];
365 : // coverity[ptr_arith]
366 13652800 : t1[1] = w[1] * (v2_Lg2 + w[1] * (v2_Lg4 + w[1] * v2_Lg6));
367 : // coverity[ptr_arith]
368 13652800 : t2[1] =
369 13652800 : z[1] * (v2_Lg1 + w[1] * (v2_Lg3 + w[1] * (v2_Lg5 /*+w[1]*v2_Lg7*/)));
370 13652800 : R[1] = t2[1] + t1[1];
371 13652800 : x[1].v2 = (dk[1].v2 - v2_const1023_mul_2pow20) * v2_ln2_div_2pow20 -
372 13652800 : (s[1] * (f[1] - R[1]) - f[1]);
373 13652800 : }
374 :
375 13652800 : static CPL_INLINE void VizGeorefSpline2DBase_func4(double *res,
376 : const double *pxy,
377 : const double *xr,
378 : const double *yr)
379 : {
380 13652800 : v2dfunion xv[2] = {};
381 13652800 : xv[0].d[0] = xr[0];
382 13652800 : xv[0].d[1] = xr[1];
383 13652800 : xv[1].d[0] = xr[2];
384 13652800 : xv[1].d[1] = xr[3];
385 13652800 : v2dfunion yv[2] = {};
386 13652800 : yv[0].d[0] = yr[0];
387 13652800 : yv[0].d[1] = yr[1];
388 13652800 : yv[1].d[0] = yr[2];
389 13652800 : yv[1].d[1] = yr[3];
390 : v2dfunion x1v;
391 13652800 : x1v.d[0] = pxy[0];
392 13652800 : x1v.d[1] = pxy[0];
393 : v2dfunion y1v;
394 13652800 : y1v.d[0] = pxy[1];
395 13652800 : y1v.d[1] = pxy[1];
396 13652800 : v2dfunion dist[2] = {};
397 13652800 : dist[0].v2 = SQ(xv[0].v2 - x1v.v2) + SQ(yv[0].v2 - y1v.v2);
398 13652800 : dist[1].v2 = SQ(xv[1].v2 - x1v.v2) + SQ(yv[1].v2 - y1v.v2);
399 13652800 : v2dfunion resv[2] = {dist[0], dist[1]};
400 13652800 : FastApproxLog4Val(dist);
401 13652800 : resv[0].v2 *= dist[0].v2;
402 13652800 : resv[1].v2 *= dist[1].v2;
403 13652800 : res[0] = resv[0].d[0];
404 13652800 : res[1] = resv[0].d[1];
405 13652800 : res[2] = resv[1].d[0];
406 13652800 : res[3] = resv[1].d[1];
407 13652800 : }
408 : #else // defined(USE_OPTIMIZED_VizGeorefSpline2DBase_func4)
409 : static void VizGeorefSpline2DBase_func4(double *res, const double *pxy,
410 : const double *xr, const double *yr)
411 : {
412 : double dist0 = SQ(xr[0] - pxy[0]) + SQ(yr[0] - pxy[1]);
413 : res[0] = dist0 != 0.0 ? dist0 * log(dist0) : 0.0;
414 : double dist1 = SQ(xr[1] - pxy[0]) + SQ(yr[1] - pxy[1]);
415 : res[1] = dist1 != 0.0 ? dist1 * log(dist1) : 0.0;
416 : double dist2 = SQ(xr[2] - pxy[0]) + SQ(yr[2] - pxy[1]);
417 : res[2] = dist2 != 0.0 ? dist2 * log(dist2) : 0.0;
418 : double dist3 = SQ(xr[3] - pxy[0]) + SQ(yr[3] - pxy[1]);
419 : res[3] = dist3 != 0.0 ? dist3 * log(dist3) : 0.0;
420 : }
421 : #endif // defined(USE_OPTIMIZED_VizGeorefSpline2DBase_func4)
422 :
423 38 : int VizGeorefSpline2D::solve()
424 : {
425 : // No points at all.
426 38 : if (_nof_points < 1)
427 : {
428 0 : type = VIZ_GEOREF_SPLINE_ZERO_POINTS;
429 0 : return 0;
430 : }
431 :
432 : // Only one point.
433 38 : if (_nof_points == 1)
434 : {
435 0 : type = VIZ_GEOREF_SPLINE_ONE_POINT;
436 0 : return 1;
437 : }
438 : // Just 2 points - it is necessarily 1D case.
439 38 : if (_nof_points == 2)
440 : {
441 0 : _dx = x[1] - x[0];
442 0 : _dy = y[1] - y[0];
443 0 : const double denom = _dx * _dx + _dy * _dy;
444 0 : if (denom == 0.0)
445 0 : return 0;
446 0 : const double fact = 1.0 / denom;
447 0 : _dx *= fact;
448 0 : _dy *= fact;
449 :
450 0 : type = VIZ_GEOREF_SPLINE_TWO_POINTS;
451 0 : return 2;
452 : }
453 :
454 : // More than 2 points - first we have to check if it is 1D or 2D case
455 :
456 38 : double xmax = x[0];
457 38 : double xmin = x[0];
458 38 : double ymax = y[0];
459 38 : double ymin = y[0];
460 38 : double sumx = 0.0;
461 38 : double sumy = 0.0;
462 38 : double sumx2 = 0.0;
463 38 : double sumy2 = 0.0;
464 38 : double sumxy = 0.0;
465 :
466 4850 : for (int p = 0; p < _nof_points; p++)
467 : {
468 4812 : const double xx = x[p];
469 4812 : const double yy = y[p];
470 :
471 4812 : xmax = std::max(xmax, xx);
472 4812 : xmin = std::min(xmin, xx);
473 4812 : ymax = std::max(ymax, yy);
474 4812 : ymin = std::min(ymin, yy);
475 :
476 4812 : sumx += xx;
477 4812 : sumx2 += xx * xx;
478 4812 : sumy += yy;
479 4812 : sumy2 += yy * yy;
480 4812 : sumxy += xx * yy;
481 : }
482 38 : const double delx = xmax - xmin;
483 38 : const double dely = ymax - ymin;
484 :
485 38 : const double SSxx = sumx2 - sumx * sumx / _nof_points;
486 38 : const double SSyy = sumy2 - sumy * sumy / _nof_points;
487 38 : const double SSxy = sumxy - sumx * sumy / _nof_points;
488 :
489 38 : if (SSxx * SSyy == 0.0)
490 : {
491 0 : CPLError(CE_Failure, CPLE_AppDefined,
492 : "Degenerate system. Computation aborted.");
493 0 : return 0;
494 : }
495 38 : if (delx < 0.001 * dely || dely < 0.001 * delx ||
496 38 : fabs(SSxy * SSxy / (SSxx * SSyy)) > 0.99)
497 : {
498 0 : type = VIZ_GEOREF_SPLINE_ONE_DIMENSIONAL;
499 :
500 0 : _dx = _nof_points * sumx2 - sumx * sumx;
501 0 : _dy = _nof_points * sumy2 - sumy * sumy;
502 0 : const double fact = 1.0 / sqrt(_dx * _dx + _dy * _dy);
503 0 : _dx *= fact;
504 0 : _dy *= fact;
505 :
506 0 : for (int p = 0; p < _nof_points; p++)
507 : {
508 0 : const double dxp = x[p] - x[0];
509 0 : const double dyp = y[p] - y[0];
510 0 : u[p] = _dx * dxp + _dy * dyp;
511 0 : unused[p] = 1;
512 : }
513 :
514 0 : for (int p = 0; p < _nof_points; p++)
515 : {
516 0 : int min_index = -1;
517 0 : double min_u = 0.0;
518 0 : for (int p1 = 0; p1 < _nof_points; p1++)
519 : {
520 0 : if (unused[p1])
521 : {
522 0 : if (min_index < 0 || u[p1] < min_u)
523 : {
524 0 : min_index = p1;
525 0 : min_u = u[p1];
526 : }
527 : }
528 : }
529 0 : index[p] = min_index;
530 0 : unused[min_index] = 0;
531 : }
532 :
533 0 : return 3;
534 : }
535 :
536 38 : type = VIZ_GEOREF_SPLINE_FULL;
537 : // Make the necessary memory allocations.
538 :
539 38 : _nof_eqs = _nof_points + 3;
540 :
541 38 : if (_nof_eqs > std::numeric_limits<int>::max() / _nof_eqs)
542 : {
543 0 : CPLError(CE_Failure, CPLE_AppDefined,
544 : "Too many coefficients. Computation aborted.");
545 0 : return 0;
546 : }
547 :
548 76 : GDALMatrix A(_nof_eqs, _nof_eqs);
549 38 : x_mean = 0;
550 38 : y_mean = 0;
551 4850 : for (int c = 0; c < _nof_points; c++)
552 : {
553 4812 : x_mean += x[c];
554 4812 : y_mean += y[c];
555 : }
556 38 : x_mean /= _nof_points;
557 38 : y_mean /= _nof_points;
558 :
559 4850 : for (int c = 0; c < _nof_points; c++)
560 : {
561 4812 : x[c] -= x_mean;
562 4812 : y[c] -= y_mean;
563 4812 : A(0, c + 3) = 1.0;
564 4812 : A(1, c + 3) = x[c];
565 4812 : A(2, c + 3) = y[c];
566 :
567 4812 : A(c + 3, 0) = 1.0;
568 4812 : A(c + 3, 1) = x[c];
569 4812 : A(c + 3, 2) = y[c];
570 : }
571 :
572 4850 : for (int r = 0; r < _nof_points; r++)
573 4525050 : for (int c = r; c < _nof_points; c++)
574 : {
575 9040480 : A(r + 3, c + 3) =
576 4520240 : VizGeorefSpline2DBase_func(x[r], y[r], x[c], y[c]);
577 4520240 : if (r != c)
578 4515430 : A(c + 3, r + 3) = A(r + 3, c + 3);
579 : }
580 :
581 : #if VIZ_GEOREF_SPLINE_DEBUG
582 :
583 : for (r = 0; r < _nof_eqs; r++)
584 : {
585 : for (c = 0; c < _nof_eqs; c++)
586 : fprintf(stderr, "%f", A(r, c)); /*ok*/
587 : fprintf(stderr, "\n"); /*ok*/
588 : }
589 :
590 : #endif
591 :
592 76 : GDALMatrix RHS(_nof_eqs, _nof_vars);
593 114 : for (int iRHS = 0; iRHS < _nof_vars; iRHS++)
594 9928 : for (int iRow = 0; iRow < _nof_eqs; iRow++)
595 9852 : RHS(iRow, iRHS) = rhs[iRHS][iRow];
596 :
597 76 : GDALMatrix Coef(_nof_eqs, _nof_vars);
598 :
599 38 : if (!GDALLinearSystemSolve(A, RHS, Coef))
600 : {
601 2 : return 0;
602 : }
603 :
604 108 : for (int iRHS = 0; iRHS < _nof_vars; iRHS++)
605 9892 : for (int iRow = 0; iRow < _nof_eqs; iRow++)
606 9820 : coef[iRHS][iRow] = Coef(iRow, iRHS);
607 :
608 36 : return 4;
609 : }
610 :
611 538613 : int VizGeorefSpline2D::get_point(const double Px, const double Py, double *vars)
612 : {
613 538613 : switch (type)
614 : {
615 0 : case VIZ_GEOREF_SPLINE_ZERO_POINTS:
616 : {
617 0 : for (int v = 0; v < _nof_vars; v++)
618 0 : vars[v] = 0.0;
619 0 : break;
620 : }
621 0 : case VIZ_GEOREF_SPLINE_ONE_POINT:
622 : {
623 0 : for (int v = 0; v < _nof_vars; v++)
624 0 : vars[v] = rhs[v][3];
625 0 : break;
626 : }
627 0 : case VIZ_GEOREF_SPLINE_TWO_POINTS:
628 : {
629 0 : const double fact = _dx * (Px - x[0]) + _dy * (Py - y[0]);
630 0 : for (int v = 0; v < _nof_vars; v++)
631 0 : vars[v] = (1 - fact) * rhs[v][3] + fact * rhs[v][4];
632 0 : break;
633 : }
634 0 : case VIZ_GEOREF_SPLINE_ONE_DIMENSIONAL:
635 : {
636 0 : int leftP = 0;
637 0 : int rightP = 0;
638 0 : const double Pu = _dx * (Px - x[0]) + _dy * (Py - y[0]);
639 0 : if (Pu <= u[index[0]])
640 : {
641 0 : leftP = index[0];
642 0 : rightP = index[1];
643 : }
644 0 : else if (Pu >= u[index[_nof_points - 1]])
645 : {
646 0 : leftP = index[_nof_points - 2];
647 0 : rightP = index[_nof_points - 1];
648 : }
649 : else
650 : {
651 0 : for (int r = 1; r < _nof_points; r++)
652 : {
653 0 : leftP = index[r - 1];
654 0 : rightP = index[r];
655 0 : if (Pu >= u[leftP] && Pu <= u[rightP])
656 0 : break; // Found.
657 : }
658 : }
659 :
660 0 : const double fact = (Pu - u[leftP]) / (u[rightP] - u[leftP]);
661 0 : for (int v = 0; v < _nof_vars; v++)
662 0 : vars[v] = (1.0 - fact) * rhs[v][leftP + 3] +
663 0 : fact * rhs[v][rightP + 3];
664 0 : break;
665 : }
666 538613 : case VIZ_GEOREF_SPLINE_FULL:
667 : {
668 538613 : const double Pxy[2] = {Px - x_mean, Py - y_mean};
669 1615840 : for (int v = 0; v < _nof_vars; v++)
670 1077230 : vars[v] =
671 1077230 : coef[v][0] + coef[v][1] * Pxy[0] + coef[v][2] * Pxy[1];
672 :
673 538613 : int r = 0; // Used after for.
674 14191400 : for (; r < (_nof_points & (~3)); r += 4)
675 : {
676 13652800 : double dfTmp[4] = {};
677 13652800 : VizGeorefSpline2DBase_func4(dfTmp, Pxy, &x[r], &y[r]);
678 40958400 : for (int v = 0; v < _nof_vars; v++)
679 27305600 : vars[v] += coef[v][r + 3] * dfTmp[0] +
680 27305600 : coef[v][r + 3 + 1] * dfTmp[1] +
681 27305600 : coef[v][r + 3 + 2] * dfTmp[2] +
682 27305600 : coef[v][r + 3 + 3] * dfTmp[3];
683 : }
684 617052 : for (; r < _nof_points; r++)
685 : {
686 : const double tmp =
687 78439 : VizGeorefSpline2DBase_func(Pxy[0], Pxy[1], x[r], y[r]);
688 235317 : for (int v = 0; v < _nof_vars; v++)
689 156878 : vars[v] += coef[v][r + 3] * tmp;
690 : }
691 538613 : break;
692 : }
693 0 : case VIZ_GEOREF_SPLINE_POINT_WAS_ADDED:
694 : {
695 0 : CPLError(CE_Failure, CPLE_AppDefined,
696 : "A point was added after the last solve."
697 : " NO interpolation - return values are zero");
698 0 : for (int v = 0; v < _nof_vars; v++)
699 0 : vars[v] = 0.0;
700 0 : return 0;
701 : }
702 0 : case VIZ_GEOREF_SPLINE_POINT_WAS_DELETED:
703 : {
704 0 : CPLError(CE_Failure, CPLE_AppDefined,
705 : "A point was deleted after the last solve."
706 : " NO interpolation - return values are zero");
707 0 : for (int v = 0; v < _nof_vars; v++)
708 0 : vars[v] = 0.0;
709 0 : return 0;
710 : }
711 0 : default:
712 : {
713 0 : return 0;
714 : }
715 : }
716 538613 : return 1;
717 : }
718 :
719 : /*! @endcond */
|