break_lines.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*!
  2. * \file break_lines.c
  3. *
  4. * \brief Vector library - Clean vector map (break lines)
  5. *
  6. * (C) 2001-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the
  9. * GNU General Public License (>=v2).
  10. * Read the file COPYING that comes with GRASS
  11. * for details.
  12. *
  13. * \author Radim Blazek
  14. *
  15. * \date 2001
  16. */
  17. #include <stdlib.h>
  18. #include <grass/gis.h>
  19. #include <grass/Vect.h>
  20. #include <grass/glocale.h>
  21. /*!
  22. \brief Break lines in vector map at each intersection.
  23. For details see Vect_break_lines_list().
  24. \param Map input vector map
  25. \param type feature type
  26. \param[out] Err vector map where points at intersections will be written or NULL
  27. \param[out] msgout file pointer where messages will be written or NULL
  28. \return
  29. */
  30. void
  31. Vect_break_lines ( struct Map_info *Map, int type, struct Map_info *Err, FILE *msgout )
  32. {
  33. Vect_break_lines_list (Map, NULL, NULL, type, Err, msgout);
  34. return;
  35. }
  36. /*!
  37. \brief Break selected lines in vector map at each intersection.
  38. Breaks selected lines specified by type in vector map. Points at
  39. intersections may be optionaly written to error map. Input vector map
  40. must be opened on level 2 for update at least on GV_BUILD_BASE.
  41. The function also breaks lines forming collapsed loop, for example
  42. 0,0;1,0;0,0 is broken at 1,0.
  43. If reference lines are given (<i>List_ref</i>) break only lines
  44. which intersect reference lines.
  45. \param Map input vector map
  46. \param List_break list of lines (NULL for all lines in vector map)
  47. \param List_ref list of reference lines or NULL
  48. \param type feature type
  49. \param[out] Err vector map where points at intersections will be written or NULL
  50. \param[out] msgout file pointer where messages will be written or NULL
  51. \return number of intersections
  52. */
  53. int
  54. Vect_break_lines_list ( struct Map_info *Map, struct ilist *List_break, struct ilist *List_ref,
  55. int type, struct Map_info *Err, FILE *msgout )
  56. {
  57. struct line_pnts *APoints, *BPoints, *Points;
  58. struct line_pnts **AXLines, **BXLines;
  59. struct line_cats *ACats, *BCats, *Cats;
  60. int j, k, l, ret, atype, btype, aline, bline, found, iline, nlines;
  61. int naxlines, nbxlines, nx;
  62. double *xx=NULL, *yx=NULL, *zx=NULL;
  63. BOUND_BOX ABox, BBox;
  64. struct ilist *List;
  65. int nbreaks;
  66. int touch1_n=0, touch1_s=0, touch1_e=0, touch1_w=0; /* other vertices except node1 touching box */
  67. int touch2_n=0, touch2_s=0, touch2_e=0, touch2_w=0; /* other vertices except node2 touching box */
  68. int is3d;
  69. int node, anode1, anode2, bnode1, bnode2;
  70. double nodex, nodey;
  71. int printed;
  72. APoints = Vect_new_line_struct ();
  73. BPoints = Vect_new_line_struct ();
  74. Points = Vect_new_line_struct ();
  75. ACats = Vect_new_cats_struct ();
  76. BCats = Vect_new_cats_struct ();
  77. Cats = Vect_new_cats_struct ();
  78. List = Vect_new_list ();
  79. is3d = Vect_is_3d ( Map );
  80. if (List_break) {
  81. nlines = List_break->n_values;
  82. }
  83. else {
  84. nlines = Vect_get_num_lines(Map);
  85. }
  86. G_debug (3, "nlines = %d", nlines );
  87. /* To find intersection of two lines (Vect_line_intersection) is quite slow.
  88. * Fortunately usual lines/boundaries in GIS often forms a network where lines
  89. * are connected by end points, and touch by MBR. This function checks and occasionaly
  90. * skips such cases. This is currently done for 2D only
  91. */
  92. /* Go through all lines in vector, for each select lines which overlap MBR of
  93. * this line exclude those connected by one endpoint (see above)
  94. * and try to intersect, if lines intersect write new lines at the end of
  95. * the file, and process next line (remainining lines overlaping box are skipped)
  96. */
  97. nbreaks = 0;
  98. printed = 0;
  99. if (msgout)
  100. fprintf (msgout, _("%s %5d"), _("Intersections:"), nbreaks );
  101. for ( iline = 0; iline < nlines; iline++ ) {
  102. if (List_break) {
  103. aline = List_break->value[iline];
  104. }
  105. else {
  106. aline = iline + 1;
  107. }
  108. if (List_ref && !Vect_val_in_list(List_ref, aline))
  109. continue;
  110. G_debug (3, "aline = %d", aline);
  111. if ( !Vect_line_alive ( Map, aline ) )
  112. continue;
  113. atype = Vect_read_line (Map, APoints, ACats, aline);
  114. if ( !(atype & type) )
  115. continue;
  116. Vect_get_line_box ( Map, aline, &ABox );
  117. /* Find which sides of the box are touched by intermediate (non-end) points of line */
  118. if ( !is3d ) {
  119. touch1_n = touch1_s = touch1_e = touch1_w = 0;
  120. for ( j = 1; j < APoints->n_points; j++ ){
  121. if ( APoints->y[j] == ABox.N ) touch1_n = 1;
  122. if ( APoints->y[j] == ABox.S ) touch1_s = 1;
  123. if ( APoints->x[j] == ABox.E ) touch1_e = 1;
  124. if ( APoints->x[j] == ABox.W ) touch1_w = 1;
  125. }
  126. G_debug(3, "touch1: n = %d s = %d e = %d w = %d", touch1_n, touch1_s, touch1_e, touch1_w );
  127. touch2_n = touch2_s = touch2_e = touch2_w = 0;
  128. for ( j = 0; j < APoints->n_points-1; j++ ){
  129. if ( APoints->y[j] == ABox.N ) touch2_n = 1;
  130. if ( APoints->y[j] == ABox.S ) touch2_s = 1;
  131. if ( APoints->x[j] == ABox.E ) touch2_e = 1;
  132. if ( APoints->x[j] == ABox.W ) touch2_w = 1;
  133. }
  134. G_debug(3, "touch2: n = %d s = %d e = %d w = %d", touch2_n, touch2_s, touch2_e, touch2_w );
  135. }
  136. Vect_select_lines_by_box ( Map, &ABox, type, List);
  137. G_debug (3, " %d lines selected by box", List->n_values);
  138. for ( j = 0; j < List->n_values; j++ ){
  139. bline = List->value[j];
  140. if (List_break && !Vect_val_in_list(List_break, bline)) {
  141. continue;
  142. }
  143. G_debug (3, " j = %d bline = %d", j, bline);
  144. /* Check if thouch by end node only */
  145. if ( !is3d ) {
  146. Vect_get_line_nodes ( Map, aline, &anode1, &anode2 );
  147. Vect_get_line_nodes ( Map, bline, &bnode1, &bnode2 );
  148. Vect_get_line_box ( Map, bline, &BBox );
  149. if ( anode1 == bnode1 || anode1 == bnode2 )
  150. node = anode1;
  151. else if ( anode2 == bnode1 || anode2 == bnode2 )
  152. node = anode2;
  153. else
  154. node = 0;
  155. if ( node ) {
  156. Vect_get_node_coor ( Map, node, &nodex, &nodey, NULL);
  157. if ( ( node == anode1 && nodey == ABox.N && !touch1_n && nodey == BBox.S ) ||
  158. ( node == anode2 && nodey == ABox.N && !touch2_n && nodey == BBox.S ) ||
  159. ( node == anode1 && nodey == ABox.S && !touch1_s && nodey == BBox.N ) ||
  160. ( node == anode2 && nodey == ABox.S && !touch2_s && nodey == BBox.N ) ||
  161. ( node == anode1 && nodex == ABox.E && !touch1_e && nodex == BBox.W ) ||
  162. ( node == anode2 && nodex == ABox.E && !touch2_e && nodex == BBox.W ) ||
  163. ( node == anode1 && nodex == ABox.W && !touch1_w && nodex == BBox.E ) ||
  164. ( node == anode2 && nodex == ABox.W && !touch2_w && nodex == BBox.E ) )
  165. {
  166. G_debug(3, "lines %d and %d touching by end nodes only -> no intersection",
  167. aline, bline);
  168. continue;
  169. }
  170. }
  171. }
  172. btype = Vect_read_line (Map, BPoints, BCats, bline);
  173. Vect_line_intersection(APoints, BPoints, &AXLines, &BXLines, &naxlines, &nbxlines, 0);
  174. G_debug(3, " naxlines = %d nbxlines = %d", naxlines, nbxlines);
  175. /* This part handles a special case when aline == bline, no other intersection was found
  176. * and the line is forming collapsed loop, for example 0,0;1,0;0,0 should be broken at 1,0.
  177. * ---> */
  178. if ( aline == bline && naxlines == 0 && nbxlines == 0 && APoints->n_points >= 3 ) {
  179. int centre;
  180. int i;
  181. G_debug(3, " Check collapsed loop" );
  182. if ( APoints->n_points % 2 ) { /* odd number of vertices */
  183. centre = APoints->n_points / 2; /* index of centre */
  184. if ( APoints->x[centre-1] == APoints->x[centre+1] &&
  185. APoints->y[centre-1] == APoints->y[centre+1] &&
  186. APoints->z[centre-1] == APoints->z[centre+1] ) /* -> break */
  187. {
  188. AXLines = (struct line_pnts **) G_malloc ( 2 * sizeof(struct line_pnts *) );
  189. AXLines[0] = Vect_new_line_struct ();
  190. AXLines[1] = Vect_new_line_struct ();
  191. for ( i = 0; i <= centre; i++ )
  192. Vect_append_point ( AXLines[0], APoints->x[i], APoints->y[i], APoints->z[i]);
  193. for ( i = centre; i < APoints->n_points; i++ )
  194. Vect_append_point ( AXLines[1], APoints->x[i], APoints->y[i], APoints->z[i]);
  195. naxlines = 2;
  196. }
  197. }
  198. }
  199. /* <--- */
  200. if ( Err ) { /* array for intersections (more than needed */
  201. xx = (double *) G_malloc ( (naxlines + nbxlines) * sizeof ( double ) );
  202. yx = (double *) G_malloc ( (naxlines + nbxlines) * sizeof ( double ) );
  203. zx = (double *) G_malloc ( (naxlines + nbxlines) * sizeof ( double ) );
  204. }
  205. nx = 0; /* number of intersections to be written to Err */
  206. if ( naxlines > 0 ) { /* intersection -> write out */
  207. Vect_delete_line (Map, aline);
  208. for ( k = 0; k < naxlines; k++ ){
  209. /* Write new line segments */
  210. /* line may collapse, don't write zero length lines */
  211. Vect_line_prune ( AXLines[k] );
  212. if ( (atype & GV_POINTS) || AXLines[k]->n_points > 1 ) {
  213. ret = Vect_write_line ( Map, atype, AXLines[k], ACats );
  214. if (List_ref) {
  215. Vect_list_append(List_ref, ret);
  216. }
  217. G_debug (3, "Line %d written, npoints = %d", ret, AXLines[k]->n_points);
  218. if (List_break) {
  219. Vect_list_append(List_break, ret);
  220. }
  221. }
  222. /* Write intersection points */
  223. if ( Err ) {
  224. if ( k > 0 ) {
  225. xx[nx] = AXLines[k]->x[0];
  226. yx[nx] = AXLines[k]->y[0];
  227. zx[nx] = AXLines[k]->z[0];
  228. nx++;
  229. }
  230. }
  231. Vect_destroy_line_struct ( AXLines[k] );
  232. }
  233. G_free ( AXLines );
  234. nbreaks += naxlines - 1;
  235. }
  236. if ( nbxlines > 0 ) {
  237. if ( aline != bline ) { /* Self intersection, do not write twice, TODO: is it OK? */
  238. Vect_delete_line (Map, bline);
  239. for ( k = 0; k < nbxlines; k++ ){
  240. /* Write new line segments */
  241. /* line may collapse, don't write zero length lines */
  242. Vect_line_prune ( BXLines[k] );
  243. if ( (btype & GV_POINTS) || BXLines[k]->n_points > 1 ) {
  244. ret = Vect_write_line ( Map, btype, BXLines[k], BCats );
  245. G_debug (5, "Line %d written", ret);
  246. if (List_break) {
  247. Vect_list_append(List_break, ret);
  248. }
  249. }
  250. /* Write intersection points */
  251. if ( Err ) {
  252. if ( k > 0 ) {
  253. found = 0;
  254. for ( l = 0; l < nx; l++ ) {
  255. if ( xx[l] == BXLines[k]->x[0] && yx[l] == BXLines[k]->y[0] &&
  256. zx[l] == BXLines[k]->z[0] )
  257. {
  258. found = 1;
  259. break;
  260. }
  261. }
  262. if ( !found ) {
  263. xx[nx] = BXLines[k]->x[0];
  264. yx[nx] = BXLines[k]->y[0];
  265. zx[nx] = BXLines[k]->z[0];
  266. nx++;
  267. }
  268. }
  269. }
  270. Vect_destroy_line_struct ( BXLines[k] );
  271. }
  272. nbreaks += nbxlines - 1;
  273. } else {
  274. for ( k = 0; k < nbxlines; k++ )
  275. Vect_destroy_line_struct ( BXLines[k] );
  276. }
  277. G_free ( BXLines );
  278. }
  279. if ( Err ) {
  280. for ( l = 0; l < nx; l++ ) { /* Write out errors */
  281. Vect_reset_line ( Points );
  282. Vect_append_point ( Points, xx[l], yx[l], zx[l] );
  283. ret = Vect_write_line ( Err, GV_POINT, Points, Cats );
  284. }
  285. G_free ( xx );
  286. G_free ( yx );
  287. G_free ( zx );
  288. }
  289. if (msgout && printed > 1000) {
  290. fprintf (msgout, "\r%s %5d (line %d)", _("Intersections:"), nbreaks, aline );
  291. fflush ( msgout );
  292. printed = 0;
  293. }
  294. printed++;
  295. if ( naxlines > 0 ) break; /* first line was broken and deleted -> take the next one */
  296. }
  297. if (List_break) {
  298. nlines = List_break->n_values;
  299. }
  300. else {
  301. nlines = Vect_get_num_lines(Map);
  302. }
  303. G_debug (3, "nlines = %d", nlines);
  304. } /* for each line */
  305. if (msgout)
  306. fprintf (msgout, "\r%s %5d \n", _("Intersections:"), nbreaks );
  307. Vect_destroy_list ( List );
  308. return nbreaks;
  309. }