transform.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. * \file transform.c
  3. *
  4. * \brief This file contains routines which perform (affine?)
  5. * transformations from one coordinate system into another.
  6. *
  7. * The second system may be translated, stretched, and rotated relative
  8. * to the first. The input system is system <em>a</em> and the output
  9. * system is <em>b</em>.
  10. *
  11. * This program is free software under the GNU General Public License
  12. * (>=v2). Read the file COPYING that comes with GRASS for details.
  13. *
  14. * \author GRASS GIS Development Team
  15. *
  16. * \date 1987-2007
  17. */
  18. /****************************************************************
  19. note: uses sqrt() from math library
  20. *****************************************************************
  21. Points from one system may be converted into the second by
  22. use of one of the two equation routines.
  23. transform_a_into_b (ax,ay,bx,by)
  24. double ax,ay; input point from system a
  25. double *bx,*by; resultant point in system b
  26. transform_b_into_a (bx,by,ax,ay)
  27. double bx,by; input point from system b
  28. double *ax,*ay; resultant point in system a
  29. *****************************************************************
  30. Residual analysis on the equation can be run to test how well
  31. the equations work. Either test how well b is predicted by a
  32. or vice versa.
  33. residuals_a_predicts_b (ax,ay,bx,by,use,n,residuals,rms)
  34. residuals_b_predicts_a (ax,ay,bx,by,use,n,residuals,rms)
  35. double ax[], ay[]; coordinate from system a
  36. double bx[], by[]; coordinate from system b
  37. char use[]; use point flags
  38. int n; number of points in ax,ay,bx,by
  39. double residual[] residual error for each point
  40. double *rms; overall root mean square error
  41. ****************************************************************/
  42. #include <stdio.h>
  43. #include <math.h>
  44. #include <grass/transform.h>
  45. /* the coefficients */
  46. static double A0, A1, A2, A3, A4, A5;
  47. static double B0, B1, B2, B3, B4, B5;
  48. /* function prototypes */
  49. static int resid(double *, double *, double *, double *, int *, int, double *,
  50. double *, int);
  51. /**
  52. * \fn int compute_transformation_coef (double ax[], double ay[], double bx[], double by[], char *use, int n)
  53. *
  54. * \brief The first step is to compute coefficients for a set of equations
  55. * which are then used to convert from the one system to the other.
  56. *
  57. * A set of x,y points from both systems is input into the equation
  58. * generator which determines the equation coefficients which most
  59. * nearly represent the original points. These coefficients are kept
  60. * in a static variables internal to this file.
  61. *
  62. * NOTE: use[i] must be true for ax[i],ay[i],bx[i],by[i] to be used
  63. * in the equation. Also, the total number of used points must be
  64. * 4 or larger.
  65. *
  66. * \param[in] ax coordinate from system a
  67. * \param[in] ay coordinate from system a
  68. * \param[in] bx coordinate from system b
  69. * \param[in] by coordinate from system b
  70. * \param[in] use use point flags
  71. * \param[in] n number of points in ax, ay, bx, by
  72. * \return int 1 if successful
  73. * \return int -1 if could not solve equation. Points probably colinear.
  74. * \return int -2 if less than 4 points used
  75. */
  76. int compute_transformation_coef(double ax[], double ay[], double bx[],
  77. double by[], int *use, int n)
  78. {
  79. int i;
  80. int j;
  81. int count;
  82. double aa[3];
  83. double aar[3];
  84. double bb[3];
  85. double bbr[3];
  86. double cc[3][3];
  87. double x;
  88. count = 0;
  89. for (i = 0; i < n; i++)
  90. if (use[i])
  91. count++;
  92. if (count < 4)
  93. return -2; /* must have at least 4 points */
  94. for (i = 0; i < 3; i++) {
  95. aa[i] = bb[i] = 0.0;
  96. for (j = 0; j < 3; j++)
  97. cc[i][j] = 0.0;
  98. }
  99. for (i = 0; i < n; i++) {
  100. if (!use[i])
  101. continue; /* skip this point */
  102. cc[0][0] += 1;
  103. cc[0][1] += bx[i];
  104. cc[0][2] += by[i];
  105. cc[1][1] += bx[i] * bx[i];
  106. cc[1][2] += bx[i] * by[i];
  107. cc[2][2] += by[i] * by[i];
  108. aa[0] += ay[i];
  109. aa[1] += ay[i] * bx[i];
  110. aa[2] += ay[i] * by[i];
  111. bb[0] += ax[i];
  112. bb[1] += ax[i] * bx[i];
  113. bb[2] += ax[i] * by[i];
  114. }
  115. cc[1][0] = cc[0][1];
  116. cc[2][0] = cc[0][2];
  117. cc[2][1] = cc[1][2];
  118. /* aa and bb are solved */
  119. if (inverse(cc) < 0)
  120. return (-1);
  121. if (m_mult(cc, aa, aar) < 0 || m_mult(cc, bb, bbr) < 0)
  122. return (-1);
  123. /* the equation coefficients */
  124. B0 = aar[0];
  125. B1 = aar[1];
  126. B2 = aar[2];
  127. B3 = bbr[0];
  128. B4 = bbr[1];
  129. B5 = bbr[2];
  130. /* the inverse equation */
  131. x = B2 * B4 - B1 * B5;
  132. if (!x)
  133. return (-1);
  134. A0 = (B1 * B3 - B0 * B4) / x;
  135. A1 = -B1 / x;
  136. A2 = B4 / x;
  137. A3 = (B0 * B5 - B2 * B3) / x;
  138. A4 = B2 / x;
  139. A5 = -B5 / x;
  140. return 1;
  141. }
  142. int transform_a_into_b(double ax, double ay, double *bx, double *by)
  143. {
  144. *by = A0 + A1 * ax + A2 * ay;
  145. *bx = A3 + A4 * ax + A5 * ay;
  146. return 0;
  147. }
  148. int transform_b_into_a(double bx, double by, double *ax, double *ay)
  149. {
  150. *ay = B0 + B1 * bx + B2 * by;
  151. *ax = B3 + B4 * bx + B5 * by;
  152. return 0;
  153. }
  154. /**************************************************************
  155. These routines are internal to this source code
  156. solve (a, b)
  157. double a[3][3]
  158. double b[3]
  159. equation solver used by compute_transformation_coef()
  160. **************************************************************/
  161. /* #define abs(xx) (xx >= 0 ? xx : -xx) */
  162. /* #define N 3 */
  163. int residuals_a_predicts_b(double ax[], double ay[], double bx[], double by[],
  164. int use[], int n, double residuals[], double *rms)
  165. {
  166. resid(ax, ay, bx, by, use, n, residuals, rms, 1);
  167. return 0;
  168. }
  169. int residuals_b_predicts_a(double ax[], double ay[], double bx[], double by[],
  170. int use[], int n, double residuals[], double *rms)
  171. {
  172. resid(ax, ay, bx, by, use, n, residuals, rms, 0);
  173. return 0;
  174. }
  175. /**
  176. * \fn int print_transform_matrix (void)
  177. *
  178. * \brief Prints matrix to stdout in human readable format.
  179. *
  180. * \return int 1
  181. */
  182. int print_transform_matrix(void)
  183. {
  184. fprintf(stdout, "\nTransformation Matrix\n");
  185. fprintf(stdout, "| xoff a b |\n");
  186. fprintf(stdout, "| yoff d e |\n");
  187. fprintf(stdout, "-------------------------------------------\n");
  188. fprintf(stdout, "%f %f %f \n", -B3, B2, -B5);
  189. fprintf(stdout, "%f %f %f \n", -B0, -B1, B4);
  190. fprintf(stdout, "-------------------------------------------\n");
  191. return 1;
  192. }
  193. static int resid(double ax[], double ay[], double bx[], double by[],
  194. int use[], int n, double residuals[], double *rms, int atob)
  195. {
  196. double x, y;
  197. int i;
  198. int count;
  199. double sum;
  200. double delta;
  201. double dx, dy;
  202. count = 0;
  203. sum = 0.0;
  204. for (i = 0; i < n; i++) {
  205. if (!use[i])
  206. continue;
  207. count++;
  208. if (atob) {
  209. transform_a_into_b(ax[i], ay[i], &x, &y);
  210. dx = x - bx[i];
  211. dy = y - by[i];
  212. }
  213. else {
  214. transform_b_into_a(bx[i], by[i], &x, &y);
  215. dx = x - ax[i];
  216. dy = y - ay[i];
  217. }
  218. delta = dx * dx + dy * dy;
  219. residuals[i] = sqrt(delta);
  220. sum += delta;
  221. }
  222. *rms = sqrt(sum / count);
  223. return 0;
  224. }