snap.c 4.9 KB

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