georef_tps.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /****************************************************************************
  2. *
  3. * MODULE: imagery library
  4. * AUTHOR(S): Markus Metz
  5. *
  6. * PURPOSE: Image processing library
  7. * COPYRIGHT: (C) 2013 by the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General Public
  10. * License (>=v2). Read the file COPYING that comes with GRASS
  11. * for details.
  12. *
  13. *****************************************************************************/
  14. #include <stdlib.h>
  15. #include <math.h>
  16. #include <grass/gis.h>
  17. #include <grass/imagery.h>
  18. #include <grass/glocale.h>
  19. #include <signal.h>
  20. /* STRUCTURE FOR USE INTERNALLY WITH THESE FUNCTIONS. THESE FUNCTIONS EXPECT
  21. SQUARE MATRICES SO ONLY ONE VARIABLE IS GIVEN (N) FOR THE MATRIX SIZE */
  22. struct MATRIX
  23. {
  24. int n; /* SIZE OF THIS MATRIX (N x N) */
  25. double *v;
  26. };
  27. /* CALCULATE OFFSET INTO ARRAY BASED ON R/C */
  28. #define M(row,col) m->v[(((row)-1)*(m->n))+(col)-1]
  29. #define MSUCCESS 1 /* SUCCESS */
  30. #define MNPTERR 0 /* NOT ENOUGH POINTS */
  31. #define MUNSOLVABLE -1 /* NOT SOLVABLE */
  32. #define MMEMERR -2 /* NOT ENOUGH MEMORY */
  33. #define MPARMERR -3 /* PARAMETER ERROR */
  34. #define MINTERR -4 /* INTERNAL ERROR */
  35. #define MAXORDER 3 /* HIGHEST SUPPORTED ORDER OF TRANSFORMATION */
  36. #ifndef MAX
  37. #define MAX(x,y) ((x) > (y) ? (x) : (y))
  38. #endif
  39. #ifndef MIN
  40. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  41. #endif
  42. /***********************************************************************
  43. FUNCTION PROTOTYPES FOR STATIC (INTERNAL) FUNCTIONS
  44. ************************************************************************/
  45. static int calccoef(struct Control_Points *, double **, double **);
  46. static int calcls(struct Control_Points *, struct MATRIX *, double *,
  47. double *, double *, double *);
  48. static double tps_base_func(const double x1, const double y1,
  49. const double x2, const double y2);
  50. static int solvemat(struct MATRIX *, double *, double *, double *, double *);
  51. /***********************************************************************
  52. TRANSFORM A SINGLE COORDINATE PAIR.
  53. ************************************************************************/
  54. int I_georef_tps(double e1, /* EASTING TO BE TRANSFORMED */
  55. double n1, /* NORTHING TO BE TRANSFORMED */
  56. double *e, /* EASTING, TRANSFORMED */
  57. double *n, /* NORTHING, TRANSFORMED */
  58. double *E, /* EASTING COEFFICIENTS */
  59. double *N, /* NORTHING COEFFICIENTS */
  60. struct Control_Points *cp,
  61. int fwd
  62. )
  63. {
  64. int i, j;
  65. double dist, *pe, *pn;
  66. if (fwd) {
  67. pe = cp->e1;
  68. pn = cp->n1;
  69. }
  70. else {
  71. pe = cp->e2;
  72. pn = cp->n2;
  73. }
  74. /* global affine (1st order poly) */
  75. *e = E[0] + e1 * E[1] + n1 * E[2];
  76. *n = N[0] + e1 * N[1] + n1 * N[2];
  77. for (i = 0, j = 0; i < cp->count; i++) {
  78. if (cp->status[i] > 0) {
  79. dist = tps_base_func(e1, n1, pe[i], pn[i]);
  80. *e += E[j + 3] * dist;
  81. *n += N[j + 3] * dist;
  82. j++;
  83. }
  84. }
  85. return MSUCCESS;
  86. }
  87. /***********************************************************************
  88. COMPUTE THE FORWARD AND BACKWARD GEOREFFERENCING COEFFICIENTS
  89. BASED ON A SET OF CONTROL POINTS
  90. ************************************************************************/
  91. int I_compute_georef_equations_tps(struct Control_Points *cp,
  92. double **E12tps, double **N12tps,
  93. double **E21tps, double **N21tps)
  94. {
  95. double *tempptr;
  96. int numactive; /* NUMBER OF ACTIVE CONTROL POINTS */
  97. int status, i;
  98. double xmax, xmin, ymax, ymin;
  99. double delx, dely;
  100. double xx, yy;
  101. double sumx, sumy, sumx2, sumy2, sumxy;
  102. double SSxx, SSyy, SSxy;
  103. /* CALCULATE THE NUMBER OF VALID CONTROL POINTS */
  104. for (i = numactive = 0; i < cp->count; i++) {
  105. if (cp->status[i] > 0)
  106. numactive++;
  107. }
  108. if (numactive < 3)
  109. return MNPTERR;
  110. if (numactive > 100000) /* arbitrary, admittedly */
  111. return MNPTERR;
  112. xmin = xmax = cp->e1[0];
  113. ymin = ymax = cp->n1[0];
  114. sumx = sumy = sumx2 = sumy2 = sumxy = 0.0;
  115. for (i = 0; i < cp->count; i++ ) {
  116. if (cp->status[i] > 0) {
  117. xx = cp->e1[i];
  118. yy = cp->n1[i];
  119. xmax = MAX(xmax, xx);
  120. xmin = MIN(xmin, xx);
  121. ymax = MAX(ymax, yy);
  122. ymin = MIN(ymin, yy);
  123. sumx += xx;
  124. sumx2 += xx * xx;
  125. sumy += yy;
  126. sumy2 += yy * yy;
  127. sumxy += xx * yy;
  128. }
  129. }
  130. delx = xmax - xmin;
  131. dely = ymax - ymin;
  132. SSxx = sumx2 - sumx * sumx / numactive;
  133. SSyy = sumy2 - sumy * sumy / numactive;
  134. SSxy = sumxy - sumx * sumy / numactive;
  135. if (delx < 0.001 * dely || dely < 0.001 * delx ||
  136. fabs(SSxy * SSxy / (SSxx * SSyy)) > 0.99) {
  137. /* points are colinear */
  138. return MUNSOLVABLE;
  139. }
  140. xmin = xmax = cp->e2[0];
  141. ymin = ymax = cp->n2[0];
  142. sumx = sumy = sumx2 = sumy2 = sumxy = 0.0;
  143. for (i = 0; i < cp->count; i++ ) {
  144. if (cp->status[i] > 0) {
  145. xx = cp->e2[i];
  146. yy = cp->n2[i];
  147. xmax = MAX(xmax, xx);
  148. xmin = MIN(xmin, xx);
  149. ymax = MAX(ymax, yy);
  150. ymin = MIN(ymin, yy);
  151. sumx += xx;
  152. sumx2 += xx * xx;
  153. sumy += yy;
  154. sumy2 += yy * yy;
  155. sumxy += xx * yy;
  156. }
  157. }
  158. delx = xmax - xmin;
  159. dely = ymax - ymin;
  160. SSxx = sumx2 - sumx * sumx / numactive;
  161. SSyy = sumy2 - sumy * sumy / numactive;
  162. SSxy = sumxy - sumx * sumy / numactive;
  163. if (delx < 0.001 * dely || dely < 0.001 * delx ||
  164. fabs(SSxy * SSxy / (SSxx * SSyy)) > 0.99) {
  165. /* points are colinear */
  166. return MUNSOLVABLE;
  167. }
  168. /* CALCULATE THE FORWARD TRANSFORMATION COEFFICIENTS */
  169. G_message(_("Calculating forward transformation coefficients"));
  170. status = calccoef(cp, E12tps, N12tps);
  171. if (status != MSUCCESS)
  172. return status;
  173. /* SWITCH THE 1 AND 2 EASTING AND NORTHING ARRAYS */
  174. tempptr = cp->e1;
  175. cp->e1 = cp->e2;
  176. cp->e2 = tempptr;
  177. tempptr = cp->n1;
  178. cp->n1 = cp->n2;
  179. cp->n2 = tempptr;
  180. /* CALCULATE THE BACKWARD TRANSFORMATION COEFFICIENTS */
  181. G_message(_("Calculating backward transformation coefficients"));
  182. status = calccoef(cp, E21tps, N21tps);
  183. /* SWITCH THE 1 AND 2 EASTING AND NORTHING ARRAYS BACK */
  184. tempptr = cp->e1;
  185. cp->e1 = cp->e2;
  186. cp->e2 = tempptr;
  187. tempptr = cp->n1;
  188. cp->n1 = cp->n2;
  189. cp->n2 = tempptr;
  190. return status;
  191. }
  192. /***********************************************************************
  193. COMPUTE THE GEOREFFERENCING COEFFICIENTS
  194. BASED ON A SET OF CONTROL POINTS
  195. ************************************************************************/
  196. static int calccoef(struct Control_Points *cp, double **E, double **N)
  197. {
  198. struct MATRIX m;
  199. double *a;
  200. double *b;
  201. int numactive; /* NUMBER OF ACTIVE CONTROL POINTS */
  202. int status, i;
  203. /* CALCULATE THE NUMBER OF VALID CONTROL POINTS */
  204. for (i = numactive = 0; i < cp->count; i++) {
  205. if (cp->status[i] > 0)
  206. numactive++;
  207. }
  208. /* INITIALIZE MATRIX */
  209. m.n = numactive + 3;
  210. m.v = G_calloc(m.n * m.n, sizeof(double));
  211. if (m.v == NULL)
  212. G_fatal_error(_("%s: out of memory"), "I_compute_georef_equations_tps()");
  213. a = G_calloc(m.n, sizeof(double));
  214. if (a == NULL)
  215. G_fatal_error(_("%s: out of memory"), "I_compute_georef_equations_tps()");
  216. b = G_calloc(m.n, sizeof(double));
  217. if (b == NULL)
  218. G_fatal_error(_("%s: out of memory"), "I_compute_georef_equations_tps()");
  219. /* equation coefficients */
  220. *E = G_calloc(m.n, sizeof(double));
  221. if (*E == NULL)
  222. G_fatal_error(_("%s: out of memory"), "I_compute_georef_equations_tps()");
  223. *N = G_calloc(m.n, sizeof(double));
  224. if (*N == NULL)
  225. G_fatal_error(_("%s: out of memory"), "I_compute_georef_equations_tps()");
  226. status = calcls(cp, &m, a, b, *E, *N);
  227. G_free(m.v);
  228. G_free(a);
  229. G_free(b);
  230. return status;
  231. }
  232. /***********************************************************************
  233. CALCULATE THE TRANSFORMATION COEFFICIENTS FOR THIN PLATE SPLINE
  234. INTERPOLATION.
  235. THIS ROUTINE USES THE LEAST SQUARES METHOD TO COMPUTE THE COEFFICIENTS.
  236. ************************************************************************/
  237. static int calcls(struct Control_Points *cp, struct MATRIX *m,
  238. double a[], double b[],
  239. double E[], /* EASTING COEFFICIENTS */
  240. double N[] /* NORTHING COEFFICIENTS */
  241. )
  242. {
  243. int i, j, n, o, numactive = 0;
  244. double dist = 0.0, dx, dy, regularization;
  245. /* INITIALIZE THE MATRIX AND THE TWO COLUMN VECTORS */
  246. for (i = 1; i <= m->n; i++) {
  247. for (j = i; j <= m->n; j++) {
  248. M(i, j) = 0.0;
  249. if (i != j)
  250. M(j, i) = 0.0;
  251. }
  252. a[i - 1] = b[i - 1] = 0.0;
  253. }
  254. /* SUM THE UPPER HALF OF THE MATRIX AND THE COLUMN VECTORS ACCORDING TO
  255. THE LEAST SQUARES METHOD OF SOLVING OVER DETERMINED SYSTEMS */
  256. for (n = 0; n < cp->count; n++) {
  257. if (cp->status[n] > 0) {
  258. a[numactive + 3] = cp->e2[n];
  259. b[numactive + 3] = cp->n2[n];
  260. numactive++;
  261. M(1, numactive + 3) = 1.0;
  262. M(2, numactive + 3) = cp->e1[n];
  263. M(3, numactive + 3) = cp->n1[n];
  264. M(numactive + 3, 1) = 1.0;
  265. M(numactive + 3, 2) = cp->e1[n];
  266. M(numactive + 3, 3) = cp->n1[n];
  267. }
  268. }
  269. if (numactive < m->n - 3)
  270. return MINTERR;
  271. i = 0;
  272. for (n = 0; n < cp->count; n++) {
  273. if (cp->status[n] > 0) {
  274. i++;
  275. j = 0;
  276. for (o = 0; o <= n; o++) {
  277. if (cp->status[o] > 0) {
  278. j++;
  279. M(i + 3, j + 3) = tps_base_func(cp->e1[n], cp->n1[n],
  280. cp->e1[o], cp->n1[o]);
  281. if (i != j)
  282. M(j + 3, i + 3) = M(i + 3, j + 3);
  283. dx = cp->e1[n] - cp->e1[o];
  284. dy = cp->n1[n] - cp->n1[o];
  285. dist += sqrt(dx * dx + dy * dy);
  286. }
  287. }
  288. }
  289. }
  290. /* regularization */
  291. dist /= (numactive * numactive);
  292. regularization = 0.01 * dist * dist;
  293. /* set diagonal to regularization, but not the first 3x3 (global affine) */
  294. return solvemat(m, a, b, E, N);
  295. }
  296. /***********************************************************************
  297. SOLVE FOR THE 'E' AND 'N' COEFFICIENTS BY USING A SOMEWHAT MODIFIED
  298. GAUSSIAN ELIMINATION METHOD.
  299. | M11 M12 ... M1n | | E0 | | a0 |
  300. | M21 M22 ... M2n | | E1 | = | a1 |
  301. | . . . . | | . | | . |
  302. | Mn1 Mn2 ... Mnn | | En-1 | | an-1 |
  303. and
  304. | M11 M12 ... M1n | | N0 | | b0 |
  305. | M21 M22 ... M2n | | N1 | = | b1 |
  306. | . . . . | | . | | . |
  307. | Mn1 Mn2 ... Mnn | | Nn-1 | | bn-1 |
  308. ************************************************************************/
  309. static int solvemat(struct MATRIX *m, double a[], double b[], double E[],
  310. double N[])
  311. {
  312. int i, j, i2, j2, imark;
  313. double factor, temp;
  314. double pivot; /* ACTUAL VALUE OF THE LARGEST PIVOT CANDIDATE */
  315. for (i = 1; i <= m->n; i++) {
  316. G_percent(i - 1, m->n, 4);
  317. j = i;
  318. /* find row with largest magnitude value for pivot value */
  319. pivot = M(i, j);
  320. imark = i;
  321. for (i2 = i + 1; i2 <= m->n; i2++) {
  322. temp = fabs(M(i2, j));
  323. if (temp > fabs(pivot)) {
  324. pivot = M(i2, j);
  325. imark = i2;
  326. }
  327. }
  328. /* if the pivot is very small then the points are nearly co-linear */
  329. /* co-linear points result in an undefined matrix, and nearly */
  330. /* co-linear points results in a solution with rounding error */
  331. if (pivot == 0.0)
  332. return MUNSOLVABLE;
  333. /* if row with highest pivot is not the current row, switch them */
  334. if (imark != i) {
  335. for (j2 = 1; j2 <= m->n; j2++) {
  336. temp = M(imark, j2);
  337. M(imark, j2) = M(i, j2);
  338. M(i, j2) = temp;
  339. }
  340. temp = a[imark - 1];
  341. a[imark - 1] = a[i - 1];
  342. a[i - 1] = temp;
  343. temp = b[imark - 1];
  344. b[imark - 1] = b[i - 1];
  345. b[i - 1] = temp;
  346. }
  347. /* compute zeros above and below the pivot, and compute
  348. values for the rest of the row as well */
  349. for (i2 = 1; i2 <= m->n; i2++) {
  350. if (i2 != i) {
  351. factor = M(i2, j) / pivot;
  352. for (j2 = j; j2 <= m->n; j2++)
  353. M(i2, j2) -= factor * M(i, j2);
  354. a[i2 - 1] -= factor * a[i - 1];
  355. b[i2 - 1] -= factor * b[i - 1];
  356. }
  357. }
  358. }
  359. G_percent(1, 1, 1);
  360. /* SINCE ALL OTHER VALUES IN THE MATRIX ARE ZERO NOW, CALCULATE THE
  361. COEFFICIENTS BY DIVIDING THE COLUMN VECTORS BY THE DIAGONAL VALUES. */
  362. for (i = 1; i <= m->n; i++) {
  363. E[i - 1] = a[i - 1] / M(i, i);
  364. N[i - 1] = b[i - 1] / M(i, i);
  365. }
  366. return MSUCCESS;
  367. }
  368. static double tps_base_func(const double x1, const double y1,
  369. const double x2, const double y2)
  370. {
  371. /* official: r * r * log(r) */
  372. double dist;
  373. if ((x1 == x2) && (y1 == y2))
  374. return 0.0;
  375. dist = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
  376. return dist * log(dist) * 0.5;
  377. }