point2d.c 4.8 KB

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