vertex.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*!
  2. \file lib/vector/vedit/vertex.c
  3. \brief Vedit library - vertex manipulation
  4. (C) 2006-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 Jachym Cepicky <jachym.cepicky gmail.com>
  8. \author Martin Landa <landa.martin gmail.com>
  9. */
  10. #include <grass/vedit.h>
  11. /*!
  12. \brief Move all vertices in bounding box(es)
  13. \param Map pointer to Map_info
  14. \param BgMap, nbgmaps list of background vector maps for snapping
  15. \param List list of selected lines
  16. \param coord points location
  17. \param thresh_coords threshold value for selecting lines
  18. \param thresh_snap threshold value used for snapping
  19. \param move_x,move_y,move_z direction (move_z is used when map is 3D)
  20. \param move_first move only first vertex found in the bounding box
  21. \param snap snapping mode (see vedit.h)
  22. \return number of moved vertices
  23. \return -1 on error
  24. */
  25. int Vedit_move_vertex(struct Map_info *Map, struct Map_info **BgMap,
  26. int nbgmaps, struct ilist *List,
  27. struct line_pnts *coord, double thresh_coords,
  28. double thresh_snap, double move_x, double move_y,
  29. double move_z, int move_first, int snap)
  30. {
  31. int nvertices_moved, nlines_modified, nvertices_snapped;
  32. int i, j, k;
  33. int line, type, rewrite;
  34. int npoints;
  35. double east, north, dist;
  36. double *x, *y, *z;
  37. char *moved;
  38. struct line_pnts *Points, *Points_snap;
  39. struct line_cats *Cats;
  40. nlines_modified = 0;
  41. nvertices_moved = nvertices_snapped = 0;
  42. moved = NULL;
  43. Points = Vect_new_line_struct();
  44. Points_snap = Vect_new_line_struct();
  45. Cats = Vect_new_cats_struct();
  46. for (i = 0; i < List->n_values; i++) {
  47. line = List->value[i];
  48. if (!Vect_line_alive(Map, line))
  49. continue;
  50. type = Vect_read_line(Map, Points, Cats, line);
  51. if (!(type & GV_LINES))
  52. continue;
  53. npoints = Points->n_points;
  54. x = Points->x;
  55. y = Points->y;
  56. z = Points->z;
  57. /* vertex moved
  58. 0 not moved
  59. 1 moved
  60. 2 moved and snapped
  61. */
  62. moved =
  63. (char *)G_realloc((void *)moved, Points->n_points * sizeof(char));
  64. G_zero((void *)moved, Points->n_points * sizeof(char));
  65. rewrite = 0;
  66. for (j = 0; j < coord->n_points; j++) {
  67. east = coord->x[j];
  68. north = coord->y[j];
  69. /* move all vertices in the bounding box */
  70. for (k = 0; k < Points->n_points; k++) {
  71. if (moved[k] == 0) {
  72. dist = Vect_points_distance(east, north, 0.0,
  73. x[k], y[k], z[k], WITHOUT_Z);
  74. if (dist <= thresh_coords) {
  75. G_debug(3,
  76. "Vedit_move_vertex(): line=%d; x=%f, y=%f -> x=%f, y=%f",
  77. line, x[k], y[k], x[k] + move_x,
  78. y[k] + move_y);
  79. x[k] += move_x;
  80. y[k] += move_y;
  81. if (Vect_is_3d(Map))
  82. z[k] += move_z;
  83. moved[k] = 1;
  84. G_debug(3, "Vedit_move_vertex(): line=%d, point=%d",
  85. line, k);
  86. if (snap != NO_SNAP) {
  87. if (Vedit_snap_point
  88. (Map, line, &x[k], &y[k], &z[k], thresh_snap,
  89. (snap == SNAPVERTEX) ? 1 : 0) == 0) {
  90. /* check also background maps */
  91. int bgi;
  92. for (bgi = 0; bgi < nbgmaps; bgi++) {
  93. if (Vedit_snap_point
  94. (BgMap[bgi], -1, &x[k], &y[k],
  95. &z[k], thresh_snap,
  96. (snap == SNAPVERTEX) ? 1 : 0))
  97. moved[k] = 2;
  98. break; /* snapped, don't continue */
  99. }
  100. }
  101. else {
  102. moved[k] = 2;
  103. }
  104. }
  105. rewrite = 1;
  106. nvertices_moved++;
  107. if (move_first)
  108. break;
  109. }
  110. }
  111. } /* for each line vertex */
  112. /* close line or boundary */
  113. if ((type & GV_LINES) &&
  114. Vect_points_distance(x[0], y[0], z[0],
  115. x[npoints - 1], y[npoints - 1],
  116. z[npoints - 1],
  117. WITHOUT_Z) <= thresh_snap) {
  118. if (moved[0] == 1) { /* first node moved */
  119. x[0] = x[npoints - 1];
  120. y[0] = y[npoints - 1];
  121. if (Vect_is_3d(Map))
  122. z[0] = z[npoints - 1];
  123. }
  124. else if (moved[npoints - 1] == 1) { /* last node moved */
  125. x[npoints - 1] = x[0];
  126. y[npoints - 1] = y[0];
  127. if (Vect_is_3d(Map))
  128. z[npoints - 1] = z[0];
  129. }
  130. }
  131. } /* for each coord */
  132. if (rewrite) {
  133. if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
  134. return -1;
  135. }
  136. nlines_modified++;
  137. }
  138. } /* for each selected line */
  139. /* destroy structures */
  140. Vect_destroy_line_struct(Points);
  141. Vect_destroy_line_struct(Points_snap);
  142. Vect_destroy_cats_struct(Cats);
  143. /* G_free ((void *) moved); */
  144. return nvertices_moved;
  145. }
  146. /*!
  147. \brief Add new vertex to line
  148. Shape of line is not changed.
  149. \todo 3D
  150. \param Map pointer to Map_info
  151. \param List list of lines
  152. \param coord points location
  153. \param thresh find line in given threshold
  154. \return number of add vertices
  155. \return -1 on error
  156. */
  157. int Vedit_add_vertex(struct Map_info *Map, struct ilist *List,
  158. struct line_pnts *coord, double thresh)
  159. {
  160. int i, j;
  161. int type, line, seg;
  162. int nlines_modified, nvertices_added, rewrite;
  163. double east, north, dist;
  164. double *x, *y, *z;
  165. double px, py;
  166. struct line_pnts *Points;
  167. struct line_cats *Cats;
  168. nlines_modified = 0;
  169. nvertices_added = 0;
  170. Points = Vect_new_line_struct();
  171. Cats = Vect_new_cats_struct();
  172. for (i = 0; i < List->n_values; i++) {
  173. line = List->value[i];
  174. if (!Vect_line_alive(Map, line))
  175. continue;
  176. type = Vect_read_line(Map, Points, Cats, line);
  177. if (!(type & GV_LINES))
  178. continue;
  179. G_debug(3, "Vedit_add_vertex(): line = %d, thresh = %f",
  180. line, thresh);
  181. x = Points->x;
  182. y = Points->y;
  183. z = Points->z;
  184. rewrite = FALSE;
  185. for (j = 0; j < coord->n_points; j++) {
  186. east = coord->x[j];
  187. north = coord->y[j];
  188. seg = Vect_line_distance(Points, east, north, 0.0, /* standpoint */
  189. WITHOUT_Z, &px, &py, NULL, /* point on line */
  190. &dist, /* distance to line */
  191. NULL, NULL);
  192. if (dist <= thresh &&
  193. Vect_points_distance(px, py, 0.0, x[seg], y[seg], z[seg],
  194. WITHOUT_Z) > 0 &&
  195. Vect_points_distance(px, py, 0.0, x[seg - 1], y[seg - 1],
  196. z[seg - 1], WITHOUT_Z) > 0) {
  197. /* add new vertex */
  198. Vect_line_insert_point(Points, seg, px, py, 0.0);
  199. G_debug(3, "Vedit_add_vertex(): line=%d; x=%f, y=%f, index=%d",
  200. line, px, py, seg);
  201. rewrite = TRUE;
  202. nvertices_added++;
  203. }
  204. } /* for each point */
  205. /* rewrite the line */
  206. if (rewrite) {
  207. Vect_line_prune(Points);
  208. if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
  209. return -1;
  210. }
  211. nlines_modified++;
  212. }
  213. } /* for each line */
  214. /* destroy structures */
  215. Vect_destroy_line_struct(Points);
  216. Vect_destroy_cats_struct(Cats);
  217. return nvertices_added;
  218. }
  219. /*!
  220. \brief Remove vertex from line
  221. \todo 3D
  222. \param Map pointer to Map_info
  223. \param List list of selected lines
  224. \param coord points location
  225. \param thresh threshold value to find a line
  226. \return number of removed vertices
  227. \return -1 on error
  228. */
  229. int Vedit_remove_vertex(struct Map_info *Map, struct ilist *List,
  230. struct line_pnts *coord, double thresh)
  231. {
  232. int i, j, k;
  233. int type, line;
  234. int nvertices_removed, rewrite, nlines_modified;
  235. double east, north;
  236. double dist;
  237. double *x, *y, *z;
  238. struct line_pnts *Points;
  239. struct line_cats *Cats;
  240. nvertices_removed = nlines_modified = 0;
  241. Points = Vect_new_line_struct();
  242. Cats = Vect_new_cats_struct();
  243. for (i = 0; i < List->n_values; i++) {
  244. line = List->value[i];
  245. if (!Vect_line_alive(Map, line))
  246. continue;
  247. type = Vect_read_line(Map, Points, Cats, line);
  248. if (!(type & GV_LINES))
  249. continue;
  250. x = Points->x;
  251. y = Points->y;
  252. z = Points->z;
  253. rewrite = 0;
  254. for (j = 0; j < coord->n_points; j++) {
  255. east = coord->x[j];
  256. north = coord->y[j];
  257. for (k = 0; k < Points->n_points; k++) {
  258. dist = Vect_points_distance(east, north, 0.0,
  259. x[k], y[k], z[k], WITHOUT_Z);
  260. if (dist <= thresh) {
  261. /* remove vertex */
  262. Vect_line_delete_point(Points, k);
  263. G_debug(3,
  264. "Vedit_remove_vertex(): line=%d; x=%f, y=%f, index=%d",
  265. line, x[k], y[k], k);
  266. k--;
  267. nvertices_removed++;
  268. rewrite = 1;
  269. }
  270. } /* for each point */
  271. } /* for each bounding box */
  272. if (rewrite) {
  273. /* rewrite the line */
  274. if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
  275. return -1;
  276. }
  277. nlines_modified++;
  278. }
  279. } /* for each line */
  280. /* destroy structures */
  281. Vect_destroy_line_struct(Points);
  282. Vect_destroy_cats_struct(Cats);
  283. return nvertices_removed;
  284. }