bridges.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*!
  2. \file lib/vector/Vlib/bridges.c
  3. \brief Vector library - clean geometry (bridges)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009 by the GRASS Development Team
  6. This program is free software under the
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Radim Blazek
  11. */
  12. #include <stdlib.h>
  13. #include <grass/vector.h>
  14. #include <grass/rbtree.h>
  15. #include <grass/glocale.h>
  16. static int cmp_int(const void *a, const void *b)
  17. {
  18. const int *ai = a;
  19. const int *bi = b;
  20. return (*ai < *bi ? -1 : (*ai > *bi));
  21. }
  22. static void remove_bridges(struct Map_info *Map, int chtype, struct Map_info *Err,
  23. int *lrm, int *brm);
  24. /*!
  25. \brief Remove bridges from vector map.
  26. Remove bridges (type boundary) connecting areas to islands or 2
  27. islands. Islands and areas must be already clean, i.e. without
  28. dangles. Bridge may be formed by more lines. Optionally deleted
  29. bridges are written to error map. Input map must be opened on
  30. level 2 for update at least on level GV_BUILD_BASE
  31. \param Map input map where bridges are deleted
  32. \param Err vector map where deleted bridges are written or NULL
  33. \param lines_removed number of lines removed
  34. \param bridges_removed Err number of bridges removed
  35. */
  36. void Vect_remove_bridges(struct Map_info *Map, struct Map_info *Err,
  37. int *lines_removed, int *bridges_removed)
  38. {
  39. remove_bridges(Map, 0, Err, lines_removed, bridges_removed);
  40. }
  41. /*!
  42. \brief Change type of bridges in vector map.
  43. Change the type of bridges (type boundary) connecting areas to
  44. islands or 2 islands. Islands and areas must be already clean,
  45. i.e. without dangles. Bridge may be formed by more lines.
  46. Optionally changed bridges are written to error map. Input map
  47. must be opened on level 2 for update at least on level
  48. GV_BUILD_BASE.
  49. \param Map input map where bridges are changed
  50. \param Err vector map where changed bridges are written or NULL
  51. \param lines_changed number of lines changed
  52. \param bridges_changed Err number of bridges changed
  53. */
  54. void Vect_chtype_bridges(struct Map_info *Map, struct Map_info *Err,
  55. int *lines_changed, int *bridges_changed)
  56. {
  57. remove_bridges(Map, 1, Err, lines_changed, bridges_changed);
  58. }
  59. /*
  60. Called by Vect_remove_bridges() and Vect_chtype_bridges():
  61. chtype = 0 -> works like Vect_remove_bridges()
  62. chtype = 1 -> works like Vect_chtype_bridges()
  63. Algorithm: Go thorough all lines,
  64. if both sides of the line have left and side 0 (candidate) do this check:
  65. follow adjacent lines in one direction (nearest to the right at the end node),
  66. if we reach this line again without dangle in the way, but with this line
  67. traversed from other side it is a bridge.
  68. List of all lines in chain is created during the cycle.
  69. */
  70. void remove_bridges(struct Map_info *Map, int chtype, struct Map_info *Err,
  71. int *lrm, int *brm)
  72. {
  73. int type, nlines, line, *bline;
  74. int left, right, node1, node2, current_line, next_line, abs_line;
  75. int bridges_removed = 0; /* number of removed bridges */
  76. int lines_removed = 0; /* number of lines removed */
  77. struct Plus_head *Plus;
  78. struct line_pnts *Points;
  79. struct line_cats *Cats;
  80. struct RB_TREE *CycleTree, *BridgeTree;
  81. struct RB_TRAV trav;
  82. int dangle, other_side;
  83. Plus = &(Map->plus);
  84. Points = Vect_new_line_struct();
  85. Cats = Vect_new_cats_struct();
  86. CycleTree = rbtree_create(cmp_int, sizeof(int));
  87. BridgeTree = rbtree_create(cmp_int, sizeof(int));
  88. nlines = Vect_get_num_lines(Map);
  89. G_debug(1, "nlines = %d", nlines);
  90. for (line = 1; line <= nlines; line++) {
  91. G_percent(line, nlines, 1);
  92. if (!Vect_line_alive(Map, line))
  93. continue;
  94. type = Vect_read_line(Map, NULL, NULL, line);
  95. if (!(type & GV_BOUNDARY))
  96. continue;
  97. Vect_get_line_areas(Map, line, &left, &right);
  98. if (left != 0 || right != 0)
  99. continue; /* Cannot be bridge */
  100. G_debug(2, "line %d - bridge candidate", line);
  101. Vect_get_line_nodes(Map, line, &node1, &node2);
  102. if (abs(node1) == abs(node2))
  103. continue; /* either zero length or loop -> cannot be a bridge */
  104. current_line = -line; /* we start with negative (go forward, node2 ) */
  105. G_debug(3, "current line: %d", line);
  106. dangle = 0;
  107. other_side = 0;
  108. rbtree_clear(CycleTree);
  109. rbtree_clear(BridgeTree);
  110. while (1) {
  111. next_line =
  112. dig_angle_next_line(Plus, current_line, GV_RIGHT,
  113. GV_BOUNDARY, NULL);
  114. abs_line = abs(next_line);
  115. /* Add this line to the list */
  116. if (rbtree_find(CycleTree, (void *)&abs_line)) {
  117. if (!rbtree_find(BridgeTree, (void *)&abs_line)) {
  118. rbtree_insert(BridgeTree, (void *)&abs_line);
  119. }
  120. }
  121. else {
  122. rbtree_insert(CycleTree, (void *)&abs_line);
  123. }
  124. if (abs(next_line) == abs(current_line)) {
  125. G_debug(4, " dangle -> no bridge");
  126. dangle = 1;
  127. break;
  128. }
  129. if (abs(next_line) == line) { /* start line reached */
  130. /* which side */
  131. if (next_line < 0) { /* other side (connected by node 2) */
  132. G_debug(5, " other side reached");
  133. other_side = 1;
  134. }
  135. else { /* start side */
  136. break;
  137. }
  138. }
  139. current_line = -next_line; /* change the sign to look at the next node in following cycle */
  140. }
  141. if (!dangle && other_side) {
  142. G_debug(3, " line %d is part of bridge chain", line);
  143. rbtree_init_trav(&trav, BridgeTree);
  144. /* for (i = 0; i < BridgeList->n_values; i++) { */
  145. while ((bline = rbtree_traverse(&trav))) {
  146. Vect_read_line(Map, Points, Cats, *bline);
  147. if (Err) {
  148. Vect_write_line(Err, GV_BOUNDARY, Points, Cats);
  149. }
  150. if (!chtype)
  151. Vect_delete_line(Map, *bline);
  152. else
  153. Vect_rewrite_line(Map, *bline, GV_LINE,
  154. Points, Cats);
  155. lines_removed++;
  156. }
  157. bridges_removed++;
  158. }
  159. }
  160. Vect_destroy_line_struct(Points);
  161. Vect_destroy_cats_struct(Cats);
  162. rbtree_destroy(CycleTree);
  163. rbtree_destroy(BridgeTree);
  164. if (lrm)
  165. *lrm = lines_removed;
  166. if (brm)
  167. *brm = bridges_removed;
  168. G_verbose_message(_("Removed lines: %d"), lines_removed);
  169. G_verbose_message(_("Removed bridges: %d"), bridges_removed);
  170. }