point2d.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. int IL_check_at_points_2d(struct interp_params *params, struct quaddata *data, /* current region */
  32. double *b, /* solution of linear equations */
  33. double *ertot, /* total error */
  34. double zmin, /* min z-value */
  35. double dnorm, struct triple skip_point)
  36. /*
  37. * Checks if interpolating function interp() evaluates correct z-values at
  38. * given points. If smoothing is used calculate the maximum error caused
  39. * by smoothing.
  40. */
  41. {
  42. int n_points = data->n_points; /* number of points */
  43. struct triple *points = data->points; /* points for interpolation */
  44. double east = data->xmax;
  45. double west = data->x_orig;
  46. double north = data->ymax;
  47. double south = data->y_orig;
  48. double rfsta2, errmax, h, xx, yy, r2, hz, zz, err, xmm, ymm, r;
  49. double skip_err;
  50. int n1, mm, m, cat;
  51. double fstar2;
  52. int inside;
  53. /* Site *site; */
  54. char buf[1024];
  55. /* if ((site = G_site_new_struct (-1, 2, 0, 1)) == NULL)
  56. G_fatal_error ("Memory error for site struct"); */
  57. fstar2 = params->fi * params->fi / 4.;
  58. errmax = .0;
  59. n1 = n_points + 1;
  60. for (mm = 1; mm <= n_points; mm++) {
  61. h = b[0];
  62. for (m = 1; m <= n_points; m++) {
  63. xx = points[mm - 1].x - points[m - 1].x;
  64. yy = points[mm - 1].y - points[m - 1].y;
  65. r2 = yy * yy + xx * xx;
  66. if (r2 != 0.) {
  67. rfsta2 = fstar2 * r2;
  68. r = r2;
  69. h = h + b[m] * params->interp(r, params->fi);
  70. }
  71. }
  72. /* modified by helena january 1997 - normalization of z was
  73. removed from segm2d.c and interp2d.c
  74. hz = (h * dnorm) + zmin;
  75. zz = (points[mm - 1].z * dnorm) + zmin;
  76. */
  77. hz = h + zmin;
  78. zz = points[mm - 1].z + zmin;
  79. err = hz - zz;
  80. xmm = points[mm - 1].x * dnorm + params->x_orig + west;
  81. ymm = points[mm - 1].y * dnorm + params->y_orig + south;
  82. if ((xmm >= west + params->x_orig) && (xmm <= east + params->x_orig)
  83. && (ymm >= south + params->y_orig) &&
  84. (ymm <= north + params->y_orig))
  85. inside = 1;
  86. else
  87. inside = 0;
  88. if (params->fddevi != NULL) {
  89. if (inside) { /* if the point is inside the region */
  90. Vect_reset_line(Pnts);
  91. Vect_reset_cats(Cats2);
  92. Vect_append_point(Pnts, xmm, ymm, zz);
  93. cat = count;
  94. Vect_cat_set(Cats2, 1, cat);
  95. Vect_write_line(&Map2, GV_POINT, Pnts, Cats2);
  96. db_zero_string(&sql2);
  97. sprintf(buf, "insert into %s values ( %d ", ff->table, cat);
  98. db_append_string(&sql2, buf);
  99. sprintf(buf, ", %f", err);
  100. db_append_string(&sql2, buf);
  101. db_append_string(&sql2, ")");
  102. G_debug(3, db_get_string(&sql2));
  103. if (db_execute_immediate(driver2, &sql2) != DB_OK) {
  104. db_close_database(driver2);
  105. db_shutdown_driver(driver2);
  106. G_fatal_error("Cannot insert new row: %s",
  107. db_get_string(&sql2));
  108. }
  109. count++;
  110. }
  111. }
  112. (*ertot) += err * err;
  113. }
  114. /* cv stuff */
  115. if (params->cv) {
  116. h = b[0];
  117. for (m = 1; m <= n_points - 1; m++) {
  118. xx = points[m - 1].x - skip_point.x;
  119. yy = points[m - 1].y - skip_point.y;
  120. r2 = yy * yy + xx * xx;
  121. if (r2 != 0.) {
  122. rfsta2 = fstar2 * r2;
  123. r = r2;
  124. h = h + b[m] * params->interp(r, params->fi);
  125. }
  126. }
  127. hz = h + zmin;
  128. zz = skip_point.z + zmin;
  129. skip_err = hz - zz;
  130. xmm = skip_point.x * dnorm + params->x_orig + west;
  131. ymm = skip_point.y * dnorm + params->y_orig + south;
  132. if ((xmm >= west + params->x_orig) && (xmm <= east + params->x_orig)
  133. && (ymm >= south + params->y_orig) &&
  134. (ymm <= north + params->y_orig))
  135. inside = 1;
  136. else
  137. inside = 0;
  138. if (inside) { /* if the point is inside the region */
  139. Vect_reset_line(Pnts);
  140. Vect_reset_cats(Cats2);
  141. Vect_append_point(Pnts, xmm, ymm, zz);
  142. cat = count;
  143. Vect_cat_set(Cats2, 1, cat);
  144. Vect_write_line(&Map2, GV_POINT, Pnts, Cats2);
  145. db_zero_string(&sql2);
  146. sprintf(buf, "insert into %s values ( %d ", ff->table, cat);
  147. db_append_string(&sql2, buf);
  148. sprintf(buf, ", %f", skip_err);
  149. db_append_string(&sql2, buf);
  150. db_append_string(&sql2, ")");
  151. G_debug(3, db_get_string(&sql2));
  152. if (db_execute_immediate(driver2, &sql2) != DB_OK) {
  153. db_close_database(driver2);
  154. db_shutdown_driver(driver2);
  155. G_fatal_error("Cannot insert new row: %s",
  156. db_get_string(&sql2));
  157. }
  158. count++;
  159. }
  160. } /* cv */
  161. return 1;
  162. }