point2d.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*!
  2. * \file point2d.c
  3. *
  4. * \author
  5. * Lubos Mitas (original program and various modifications)
  6. *
  7. * \author
  8. * H. Mitasova,
  9. * I. Kosinovsky,
  10. * D. Gerdes,
  11. * D. McCauley
  12. * (GRASS4.1 version of the program and GRASS4.2 modifications)
  13. *
  14. * \author modified by McCauley in August 1995
  15. * \author modified by Mitasova in August 1995, Nov. 1996
  16. *
  17. * \copyright
  18. * (C) 1993-2006 by Helena Mitasova and the GRASS Development Team
  19. *
  20. * \copyright
  21. * This program is free software under the
  22. * GNU General Public License (>=v2).
  23. * Read the file COPYING that comes with GRASS for details.
  24. */
  25. #include <stdio.h>
  26. #include <math.h>
  27. #include <unistd.h>
  28. #include <grass/gis.h>
  29. #include <grass/vector.h>
  30. #include <grass/dbmi.h>
  31. #define POINT2D_C
  32. #include <grass/interpf.h>
  33. /* needed for AIX */
  34. #ifdef hz
  35. #undef hz
  36. #endif
  37. /*!
  38. * Checks if interpolating function interp() evaluates correct z-values at
  39. * given points. If smoothing is used calculate the maximum error caused
  40. * by smoothing.
  41. *
  42. * *ertot* is a RMS deviation of the interpolated surface.
  43. *
  44. * \todo
  45. * Alternative description:
  46. * ...calculate the maximum and RMS deviation caused by smoothing.
  47. */
  48. int IL_check_at_points_2d(struct interp_params *params,
  49. struct quaddata *data, /*!< current region */
  50. double *b, /*!< solution of linear equations */
  51. double *ertot, /*!< total error */
  52. double zmin, /*!< min z-value */
  53. double dnorm,
  54. struct triple skip_point
  55. )
  56. {
  57. int n_points = data->n_points; /* number of points */
  58. struct triple *points = data->points; /* points for interpolation */
  59. double east = data->xmax;
  60. double west = data->x_orig;
  61. double north = data->ymax;
  62. double south = data->y_orig;
  63. double rfsta2, errmax, h, xx, yy, r2, hz, zz, err, xmm, ymm, r;
  64. double skip_err;
  65. int n1, mm, m, cat;
  66. double fstar2;
  67. int inside;
  68. /* Site *site; */
  69. char buf[1024];
  70. /* if ((site = G_site_new_struct (-1, 2, 0, 1)) == NULL)
  71. G_fatal_error ("Memory error for site struct"); */
  72. fstar2 = params->fi * params->fi / 4.;
  73. errmax = .0;
  74. n1 = n_points + 1;
  75. for (mm = 1; mm <= n_points; mm++) {
  76. h = b[0];
  77. for (m = 1; m <= n_points; m++) {
  78. xx = points[mm - 1].x - points[m - 1].x;
  79. yy = points[mm - 1].y - points[m - 1].y;
  80. r2 = yy * yy + xx * xx;
  81. if (r2 != 0.) {
  82. rfsta2 = fstar2 * r2;
  83. r = r2;
  84. h = h + b[m] * params->interp(r, params->fi);
  85. }
  86. }
  87. /* modified by helena january 1997 - normalization of z was
  88. removed from segm2d.c and interp2d.c
  89. hz = (h * dnorm) + zmin;
  90. zz = (points[mm - 1].z * dnorm) + zmin;
  91. */
  92. hz = h + zmin;
  93. zz = points[mm - 1].z + zmin;
  94. err = hz - zz;
  95. xmm = points[mm - 1].x * dnorm + params->x_orig + west;
  96. ymm = points[mm - 1].y * dnorm + params->y_orig + south;
  97. if ((xmm >= west + params->x_orig) && (xmm <= east + params->x_orig)
  98. && (ymm >= south + params->y_orig) &&
  99. (ymm <= north + params->y_orig))
  100. inside = 1;
  101. else
  102. inside = 0;
  103. if (params->fddevi != NULL) {
  104. if (inside) { /* if the point is inside the region */
  105. Vect_reset_line(Pnts);
  106. Vect_reset_cats(Cats2);
  107. Vect_append_point(Pnts, xmm, ymm, zz);
  108. cat = count;
  109. Vect_cat_set(Cats2, 1, cat);
  110. Vect_write_line(&Map2, GV_POINT, Pnts, Cats2);
  111. db_zero_string(&sql2);
  112. sprintf(buf, "insert into %s values ( %d ", ff->table, cat);
  113. db_append_string(&sql2, buf);
  114. sprintf(buf, ", %f", err);
  115. db_append_string(&sql2, buf);
  116. db_append_string(&sql2, ")");
  117. G_debug(3, "IL_check_at_points_2d: %s", db_get_string(&sql2));
  118. if (db_execute_immediate(driver2, &sql2) != DB_OK) {
  119. db_close_database(driver2);
  120. db_shutdown_driver(driver2);
  121. G_fatal_error("Cannot insert new row: %s",
  122. db_get_string(&sql2));
  123. }
  124. count++;
  125. }
  126. }
  127. (*ertot) += err * err;
  128. }
  129. /* cv stuff */
  130. if (params->cv) {
  131. h = b[0];
  132. for (m = 1; m <= n_points - 1; m++) {
  133. xx = points[m - 1].x - skip_point.x;
  134. yy = points[m - 1].y - skip_point.y;
  135. r2 = yy * yy + xx * xx;
  136. if (r2 != 0.) {
  137. rfsta2 = fstar2 * r2;
  138. r = r2;
  139. h = h + b[m] * params->interp(r, params->fi);
  140. }
  141. }
  142. hz = h + zmin;
  143. zz = skip_point.z + zmin;
  144. skip_err = hz - zz;
  145. xmm = skip_point.x * dnorm + params->x_orig + west;
  146. ymm = skip_point.y * dnorm + params->y_orig + south;
  147. if ((xmm >= west + params->x_orig) && (xmm <= east + params->x_orig)
  148. && (ymm >= south + params->y_orig) &&
  149. (ymm <= north + params->y_orig))
  150. inside = 1;
  151. else
  152. inside = 0;
  153. if (inside) { /* if the point is inside the region */
  154. Vect_reset_line(Pnts);
  155. Vect_reset_cats(Cats2);
  156. Vect_append_point(Pnts, xmm, ymm, zz);
  157. cat = count;
  158. Vect_cat_set(Cats2, 1, cat);
  159. Vect_write_line(&Map2, GV_POINT, Pnts, Cats2);
  160. db_zero_string(&sql2);
  161. sprintf(buf, "insert into %s values ( %d ", ff->table, cat);
  162. db_append_string(&sql2, buf);
  163. sprintf(buf, ", %f", skip_err);
  164. db_append_string(&sql2, buf);
  165. db_append_string(&sql2, ")");
  166. G_debug(3, "IL_check_at_points_2d: %s", db_get_string(&sql2));
  167. if (db_execute_immediate(driver2, &sql2) != DB_OK) {
  168. db_close_database(driver2);
  169. db_shutdown_driver(driver2);
  170. G_fatal_error("Cannot insert new row: %s",
  171. db_get_string(&sql2));
  172. }
  173. count++;
  174. }
  175. } /* cv */
  176. return 1;
  177. }