snap.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. \file vector/vedit/snap.c
  3. \brief Vedit library - snapping
  4. (C) 2007-2008 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Martin Landa <landa.martin gmail.com>
  8. \date 2007-2008
  9. */
  10. #include <grass/vedit.h>
  11. /**
  12. \brief Snap given point to the nearest primitive
  13. \param Map vector map
  14. \param line line to be excluded (point on line)
  15. \param x,y,z point on line to be snapped
  16. \param thresh snapping threshold (>0)
  17. \param vertex snap also to vertex (non-zero)
  18. \return 1 snapped
  19. \return 0 not snapped
  20. */
  21. int Vedit_snap_point(struct Map_info *Map,
  22. int line, double *x, double *y, double *z, double thresh,
  23. int vertex)
  24. {
  25. struct line_pnts *Points;
  26. int i, snapped;
  27. int line2snap, mindist_idx;
  28. double dist, mindist;
  29. snapped = 0;
  30. mindist_idx = -1;
  31. mindist = thresh;
  32. Points = Vect_new_line_struct();
  33. line2snap = Vect_find_line(Map, *x, *y, *z, -1, thresh, WITHOUT_Z, line);
  34. if (line2snap > 0) {
  35. Vect_read_line(Map, Points, NULL, line2snap);
  36. if (!Vect_line_alive(Map, line2snap)) {
  37. Vect_destroy_line_struct(Points);
  38. return snapped;
  39. }
  40. for (i = 0; i < Points->n_points; i++) {
  41. if (i > 0 && i < Points->n_points - 1)
  42. if (!vertex)
  43. continue;
  44. dist = Vect_points_distance(*x, *y, *z,
  45. Points->x[i], Points->y[i],
  46. Points->z[i], WITHOUT_Z);
  47. if (mindist >= dist) {
  48. mindist = dist;
  49. mindist_idx = i;
  50. }
  51. }
  52. if (mindist_idx > -1) {
  53. *x = Points->x[mindist_idx];
  54. *y = Points->y[mindist_idx];
  55. *z = Points->z[mindist_idx];
  56. snapped = 1;
  57. }
  58. }
  59. G_debug(3, "Vedit_snap_point(): map=%s, line2snap=%d, snapped=%d",
  60. Map->name, line2snap, snapped);
  61. Vect_destroy_line_struct(Points);
  62. return snapped;
  63. }
  64. /**
  65. \brief Snap selected primitive to its nearest primitive
  66. \param Map vector map
  67. \param BgMap,nbgmaps list of background maps used for snapping
  68. \param line line id to be snapped (if already written, otherwise -1)
  69. \param Points line geometry
  70. \param layer layer number
  71. \param thresh threshold value used for snapping (>0)
  72. \param to_vertex allow snapping also to vertex
  73. \return 1 line snapped
  74. \return 0 line not snapped
  75. \return -1 line is dead (if 'line' is > 0)
  76. */
  77. int Vedit_snap_line(struct Map_info *Map, struct Map_info **BgMap,
  78. int nbgmaps, int line, struct line_pnts *Points,
  79. double thresh, int to_vertex)
  80. {
  81. int i, npoints, node, rewrite;
  82. double *x, *y, *z;
  83. struct line_cats *Cats;
  84. Cats = Vect_new_cats_struct();
  85. G_debug(3, "Vedit_snap_line(): thresh=%g, to_vertex=%d", thresh,
  86. to_vertex);
  87. if (line > 0 && !Vect_line_alive(Map, line))
  88. return -1;
  89. npoints = Points->n_points;
  90. x = Points->x;
  91. y = Points->y;
  92. z = Points->z;
  93. rewrite = 0;
  94. for (node = 0; node < npoints; node++) {
  95. if ((node > 0 && node < npoints - 1) && !to_vertex)
  96. continue;
  97. if (Vedit_snap_point(Map, line, &x[node], &y[node], &z[node], thresh,
  98. to_vertex)) {
  99. rewrite = 1;
  100. }
  101. else {
  102. /* check also background maps */
  103. for (i = 0; i < nbgmaps; i++) {
  104. if (Vedit_snap_point
  105. (BgMap[i], -1, &x[node], &y[node], &z[node], thresh,
  106. to_vertex)) {
  107. rewrite = 1;
  108. break; /* snapped, don't continue */
  109. }
  110. }
  111. }
  112. } /* for each line vertex */
  113. /* close boundaries or lines */
  114. if (!rewrite &&
  115. Vect_points_distance(x[0], y[0], z[0],
  116. x[npoints - 1], y[npoints - 1], z[npoints - 1],
  117. WITHOUT_Z) <= thresh) {
  118. x[npoints - 1] = x[0];
  119. y[npoints - 1] = y[0];
  120. z[npoints - 1] = z[0];
  121. rewrite = 1;
  122. }
  123. G_debug(3, "Vedit_snap_line(): line=%d, snapped=%d", line, rewrite);
  124. Vect_destroy_cats_struct(Cats);
  125. return rewrite;
  126. }
  127. /**
  128. \brief Snap lines/boundaries
  129. \param Map vector map
  130. \param BgMap,nbgmaps list of background maps used for snapping
  131. \param List list of lines to be snapped
  132. \param layer layer number
  133. \param thresh threshold value used for snapping (>0)
  134. \param to_vertex allow snapping also to vertex
  135. \return number of snapped lines
  136. \return -1 on error
  137. */
  138. int Vedit_snap_lines(struct Map_info *Map, struct Map_info **BgMap,
  139. int nbgmaps, struct ilist *List, double thresh,
  140. int to_vertex)
  141. {
  142. int i, line, type;
  143. int nlines_modified = 0;
  144. struct line_pnts *Points;
  145. struct line_cats *Cats;
  146. Points = Vect_new_line_struct();
  147. Cats = Vect_new_cats_struct();
  148. for (i = 0; i < List->n_values; i++) {
  149. line = List->value[i];
  150. type = Vect_read_line(Map, Points, Cats, line);
  151. if (!(type & (GV_POINT | GV_LINES))) {
  152. continue;
  153. }
  154. if (Vedit_snap_line(Map, BgMap, nbgmaps,
  155. line, Points, thresh, to_vertex) == 1) {
  156. if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
  157. return -1;
  158. }
  159. nlines_modified++;
  160. }
  161. }
  162. Vect_destroy_line_struct(Points);
  163. Vect_destroy_cats_struct(Cats);
  164. return nlines_modified;
  165. }