break.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*!
  2. \file lib/vector/vedit/break.c
  3. \brief Vedit library - split, break, connect lines
  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 <math.h>
  10. #include <grass/vedit.h>
  11. static int connect_lines(struct Map_info *, int, int, int,
  12. double, struct ilist *);
  13. /*!
  14. \brief Split selected lines on given position
  15. \param Map pointer to Map_info
  16. \param List list of selected lines
  17. \param coord points location
  18. \param thresh threshold
  19. \param[out] List_updated list of rewritten features (or NULL)
  20. \return number of modified lines
  21. \return -1 on error
  22. */
  23. int Vedit_split_lines(struct Map_info *Map, struct ilist *List,
  24. struct line_pnts *coord, double thresh,
  25. struct ilist *List_updated)
  26. {
  27. int i, j, l;
  28. int type, line, seg, newline;
  29. int nlines_modified;
  30. double px, py, spdist, lpdist, dist;
  31. double *x, *y, *z;
  32. struct line_pnts *Points, *Points2;
  33. struct line_cats *Cats;
  34. struct ilist *List_in_box;
  35. nlines_modified = 0;
  36. Points = Vect_new_line_struct();
  37. Points2 = Vect_new_line_struct();
  38. Cats = Vect_new_cats_struct();
  39. List_in_box = Vect_new_list();
  40. for (i = 0; i < List->n_values; i++) {
  41. line = List->value[i];
  42. if (!Vect_line_alive(Map, line))
  43. continue;
  44. type = Vect_read_line(Map, Points, Cats, line);
  45. if (!(type & GV_LINES))
  46. continue;
  47. x = Points->x;
  48. y = Points->y;
  49. z = Points->z;
  50. for (j = 0; j < coord->n_points; j++) {
  51. seg =
  52. Vect_line_distance(Points, coord->x[j], coord->y[j],
  53. coord->z[j], WITHOUT_Z, &px, &py, NULL,
  54. &dist, &spdist, &lpdist);
  55. if (dist > thresh) {
  56. continue;
  57. }
  58. G_debug(3, "Vedit_split_lines(): line=%d, x=%f, y=%f, px=%f, py=%f, seg=%d, "
  59. "dist=%f, spdist=%f, lpdist=%f", line, coord->x[j],
  60. coord->y[j], px, py, seg, dist, spdist, lpdist);
  61. if (spdist <= 0.0 || spdist >= Vect_line_length(Points))
  62. continue;
  63. G_debug(3, "Vedit_split_lines(): line=%d", line);
  64. /* copy first line part */
  65. for (l = 0; l < seg; l++) {
  66. Vect_append_point(Points2, x[l], y[l], z[l]);
  67. }
  68. /* add last vertex */
  69. Vect_append_point(Points2, px, py, 0.0);
  70. /* rewrite the line */
  71. newline = Vect_rewrite_line(Map, line, type, Points2, Cats);
  72. if (newline < 0) {
  73. return -1;
  74. }
  75. if (List_updated)
  76. Vect_list_append(List_updated, newline);
  77. Vect_reset_line(Points2);
  78. /* add given vertex */
  79. Vect_append_point(Points2, px, py, 0.0);
  80. /* copy second line part */
  81. for (l = seg; l < Points->n_points; l++) {
  82. Vect_append_point(Points2, x[l], y[l], z[l]);
  83. }
  84. /* rewrite the line */
  85. newline = Vect_write_line(Map, type, Points2, Cats);
  86. if (newline < 0) {
  87. return -1;
  88. }
  89. if (List_updated)
  90. Vect_list_append(List_updated, newline);
  91. nlines_modified++;
  92. } /* for each bounding box */
  93. } /* for each selected line */
  94. Vect_destroy_line_struct(Points);
  95. Vect_destroy_line_struct(Points2);
  96. Vect_destroy_cats_struct(Cats);
  97. Vect_destroy_list(List_in_box);
  98. return nlines_modified;
  99. }
  100. /*!
  101. \brief Connect lines in given threshold
  102. \code
  103. \ \
  104. id1 \ -> \
  105. \
  106. id2 --------- -----+---
  107. \endcode
  108. If two lines are selected and <i>thresh</i> is -1, no limit is
  109. applied.
  110. \param Map pointer to Map_info
  111. \param List list of selected lines
  112. \param thresh threshold value
  113. \return number of modified lines
  114. \return -1 on error
  115. */
  116. int Vedit_connect_lines(struct Map_info *Map, struct ilist *List,
  117. double thresh)
  118. {
  119. int nlines_modified, connected;
  120. int i, j, node[2], n_nodes;
  121. int line, found;
  122. double x, y, z;
  123. struct ilist *List_exclude, *List_found;
  124. nlines_modified = 0;
  125. List_exclude = Vect_new_list();
  126. List_found = Vect_new_list();
  127. n_nodes = 2;
  128. /* collect lines to be modified */
  129. for (i = 0; i < List->n_values; i++) {
  130. line = List->value[i];
  131. if (!Vect_line_alive(Map, line))
  132. continue;
  133. if (Vect_get_line_type(Map, line) & GV_POINTS)
  134. continue;
  135. node[0] = node[1] = -1;
  136. Vect_get_line_nodes(Map, line, &(node[0]), &(node[1]));
  137. if (node[0] < 0 || node[1] < 0)
  138. continue;
  139. connected = 0;
  140. Vect_reset_list(List_exclude);
  141. Vect_list_append(List_exclude, line);
  142. for (j = 0; j < n_nodes && !connected; j++) {
  143. /* for each line node find lines in threshold */
  144. Vect_get_node_coor(Map, node[j], &x, &y, &z);
  145. do {
  146. /* find first nearest line */
  147. found = Vect_find_line_list(Map, x, y, z,
  148. GV_LINES, thresh, WITHOUT_Z,
  149. List_exclude, List_found);
  150. if (found > 0 && Vect_line_alive(Map, found)) {
  151. /* try to connect lines (given node) */
  152. G_debug(3, "Vedit_connect_lines(): lines=%d,%d", line, found);
  153. if (connect_lines(Map, !j, line, found, thresh, List)) {
  154. G_debug(3, "Vedit_connect_lines(): lines=%d,%d -> connected",
  155. line, found);
  156. nlines_modified += 2;
  157. connected = 1;
  158. }
  159. }
  160. Vect_list_append(List_exclude, found);
  161. } while(List_found->n_values > 0 && !connected);
  162. }
  163. }
  164. Vect_destroy_list(List_exclude);
  165. Vect_destroy_list(List_found);
  166. return nlines_modified;
  167. }
  168. int connect_lines(struct Map_info *Map, int first, int line_from, int line_to,
  169. double thresh, struct ilist *List)
  170. {
  171. int line_new;
  172. int type_from, type_to;
  173. int n_points, seg, is;
  174. double x, y, px, py, x1, y1;
  175. double dist, spdist, lpdist, length, dist_p;
  176. double angle_t, angle_f, angle;
  177. struct line_pnts *Points_from, *Points_to, *Points_final;
  178. struct line_cats *Cats_from, *Cats_to;
  179. Points_from = Vect_new_line_struct();
  180. Points_to = Vect_new_line_struct();
  181. Points_final = Vect_new_line_struct();
  182. Cats_from = Vect_new_cats_struct();
  183. Cats_to = Vect_new_cats_struct();
  184. type_from = Vect_read_line(Map, Points_from, Cats_from, line_from);
  185. type_to = Vect_read_line(Map, Points_to, Cats_to, line_to);
  186. line_new = 0;
  187. if (!(type_from & GV_LINES) || !(type_to & GV_LINES))
  188. line_new = -1;
  189. if (line_new > -1) {
  190. if (first) {
  191. x = Points_from->x[0];
  192. y = Points_from->y[0];
  193. }
  194. else {
  195. n_points = Points_from->n_points - 1;
  196. x = Points_from->x[n_points];
  197. y = Points_from->y[n_points];
  198. }
  199. seg = Vect_line_distance(Points_to, x, y, 0.0, WITHOUT_Z,
  200. &px, &py, NULL, &dist, &spdist, &lpdist);
  201. if (seg > 0 && dist > 0.0 && (thresh < 0. || dist <= thresh)) {
  202. /* lines in threshold */
  203. if (first)
  204. length = 0;
  205. else
  206. length = Vect_line_length(Points_from);
  207. if (Vect_point_on_line(Points_from, length,
  208. NULL, NULL, NULL, &angle_f, NULL) > 0) {
  209. if (Vect_point_on_line(Points_to, lpdist,
  210. NULL, NULL, NULL, &angle_t,
  211. NULL) > 0) {
  212. angle = angle_t - angle_f;
  213. dist_p = fabs(dist / sin(angle));
  214. if (first) {
  215. if (angle_f < 0)
  216. angle_f -= M_PI;
  217. else
  218. angle_f += M_PI;
  219. }
  220. x1 = x + dist_p * cos(angle_f);
  221. y1 = y + dist_p * sin(angle_f);
  222. length = Vect_line_length(Points_to);
  223. Vect_line_insert_point(Points_to, seg, x1, y1, 0.);
  224. if (fabs(Vect_line_length(Points_to) - length) < length * 1e-3) {
  225. /* lines connected -> split line_to */
  226. /* update line_from */
  227. if (first) {
  228. Points_from->x[0] = x1;
  229. Points_from->y[0] = y1;
  230. }
  231. else {
  232. Points_from->x[n_points] = x1;
  233. Points_from->y[n_points] = y1;
  234. }
  235. line_new = Vect_rewrite_line(Map, line_from, type_from,
  236. Points_from, Cats_from);
  237. /* Vect_list_append(List, line_new); */
  238. /* update line_to -- first part */
  239. Vect_reset_line(Points_final);
  240. for (is = 0; is < seg; is++) {
  241. Vect_append_point(Points_final, Points_to->x[is],
  242. Points_to->y[is],
  243. Points_to->z[is]);
  244. }
  245. Vect_append_point(Points_final, x1, y1, 0.0);
  246. line_new = Vect_rewrite_line(Map, line_to, type_to,
  247. Points_final, Cats_to);
  248. /* Vect_list_append(List, line_new); */
  249. /* write second part */
  250. Vect_reset_line(Points_final);
  251. Vect_append_point(Points_final, x1, y1, 0.0);
  252. for (is = seg; is < Points_to->n_points; is++) {
  253. Vect_append_point(Points_final, Points_to->x[is],
  254. Points_to->y[is],
  255. Points_to->z[is]);
  256. }
  257. /* rewrite first part */
  258. line_new = Vect_write_line(Map, type_to,
  259. Points_final, Cats_to);
  260. /* Vect_list_append(List, line_new); */
  261. }
  262. }
  263. }
  264. }
  265. }
  266. Vect_destroy_line_struct(Points_from);
  267. Vect_destroy_line_struct(Points_to);
  268. Vect_destroy_line_struct(Points_final);
  269. Vect_destroy_cats_struct(Cats_from);
  270. Vect_destroy_cats_struct(Cats_to);
  271. return line_new > 0 ? 1 : 0;
  272. }