break.c 8.7 KB

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