bridges.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*!
  2. \file 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 <grass/config.h>
  13. #include <stdlib.h>
  14. #include <grass/gis.h>
  15. #include <grass/vector.h>
  16. #include <grass/glocale.h>
  17. static void
  18. remove_bridges(struct Map_info *Map, int chtype, struct Map_info *Err);
  19. /*!
  20. \brief Remove bridges from vector map.
  21. Remove bridges (type boundary) connecting areas to islands or 2
  22. islands. Islands and areas must be already clean, i.e. without
  23. dangles. Bridge may be formed by more lines. Optionally deleted
  24. bridges are written to error map. Input map must be opened on
  25. level 2 for update at least on level GV_BUILD_BASE
  26. \param Map input map where bridges are deleted
  27. \param Err vector map where deleted bridges are written or NULL
  28. \return
  29. */
  30. void
  31. Vect_remove_bridges(struct Map_info *Map, struct Map_info *Err)
  32. {
  33. remove_bridges(Map, 0, Err);
  34. }
  35. /*!
  36. \brief Change type of bridges in vector map.
  37. Change the type of bridges (type boundary) connecting areas to
  38. islands or 2 islands. Islands and areas must be already clean,
  39. i.e. without dangles. Bridge may be formed by more lines.
  40. Optionally changed bridges are written to error map. Input map
  41. must be opened on level 2 for update at least on level
  42. GV_BUILD_BASE.
  43. \param Map input map where bridges are changed
  44. \param Err vector map where changed bridges are written or NULL
  45. \return
  46. */
  47. void
  48. Vect_chtype_bridges(struct Map_info *Map, struct Map_info *Err)
  49. {
  50. remove_bridges(Map, 1, Err);
  51. }
  52. /*
  53. Called by Vect_remove_bridges() and Vect_chtype_bridges():
  54. chtype = 0 -> works like Vect_remove_bridges()
  55. chtype = 1 -> works like Vect_chtype_bridges()
  56. Algorithm: Go thorough all lines,
  57. if both sides of the line have left and side 0 (candidate) do this check:
  58. follow adjacent lines in one direction (nearest to the right at the end node),
  59. if we reach this line again without dangle in the way, but with this line
  60. traversed from other side it is a bridge.
  61. List of all lines in chain is created during the cycle.
  62. */
  63. void
  64. remove_bridges(struct Map_info *Map, int chtype, struct Map_info *Err)
  65. {
  66. int i, type, nlines, line;
  67. int left, right, node1, node2, current_line, next_line;
  68. int bridges_removed = 0; /* number of removed bridges */
  69. int lines_removed = 0; /* number of lines removed */
  70. char *lmsg;
  71. struct Plus_head *Plus;
  72. struct line_pnts *Points;
  73. struct line_cats *Cats;
  74. struct ilist *CycleList;
  75. struct ilist *BridgeList;
  76. int dangle, other_side;
  77. if (chtype)
  78. lmsg = "changed lines";
  79. else
  80. lmsg = "removed lines";
  81. Plus = &(Map->plus);
  82. Points = Vect_new_line_struct();
  83. Cats = Vect_new_cats_struct();
  84. CycleList = Vect_new_list();
  85. BridgeList = Vect_new_list();
  86. nlines = Vect_get_num_lines(Map);
  87. G_debug(1, "nlines = %d", nlines);
  88. for (line = 1; line <= nlines; line++) {
  89. G_percent(line, nlines, 1);
  90. if (!Vect_line_alive(Map, line))
  91. continue;
  92. type = Vect_read_line(Map, NULL, NULL, line);
  93. if (!(type & GV_BOUNDARY))
  94. continue;
  95. Vect_get_line_areas(Map, line, &left, &right);
  96. if (left != 0 || right != 0)
  97. continue; /* Cannot be bridge */
  98. G_debug(2, "line %d - bridge candidate", line);
  99. Vect_get_line_nodes(Map, line, &node1, &node2);
  100. if (abs(node1) == abs(node2))
  101. continue; /* either zero length or loop -> cannot be a bridge */
  102. current_line = -line; /* we start with negative (go forward, node2 ) */
  103. dangle = 0;
  104. other_side = 0;
  105. Vect_reset_list(CycleList);
  106. Vect_reset_list(BridgeList);
  107. while (1) {
  108. next_line =
  109. dig_angle_next_line(Plus, current_line, GV_RIGHT,
  110. GV_BOUNDARY);
  111. /* Add this line to the list */
  112. if (Vect_val_in_list(CycleList, abs(next_line))) /* other side -> bridge chain */
  113. Vect_list_append(BridgeList, abs(next_line));
  114. else
  115. Vect_list_append(CycleList, abs(next_line));
  116. if (abs(next_line) == abs(current_line)) {
  117. G_debug(4, " dangle -> no bridge");
  118. dangle = 1;
  119. break;
  120. }
  121. if (abs(next_line) == line) { /* start line reached */
  122. /* which side */
  123. if (next_line < 0) { /* other side (connected by node 2) */
  124. G_debug(5, " other side reached");
  125. other_side = 1;
  126. }
  127. else { /* start side */
  128. break;
  129. }
  130. }
  131. current_line = -next_line; /* change the sign to look at the next node in following cycle */
  132. }
  133. if (!dangle && other_side) {
  134. G_debug(3, " line %d is part of bridge chain", line);
  135. for (i = 0; i < BridgeList->n_values; i++) {
  136. Vect_read_line(Map, Points, Cats, BridgeList->value[i]);
  137. if (Err) {
  138. Vect_write_line(Err, GV_BOUNDARY, Points, Cats);
  139. }
  140. if (!chtype)
  141. Vect_delete_line(Map, BridgeList->value[i]);
  142. else
  143. Vect_rewrite_line(Map, BridgeList->value[i], GV_LINE,
  144. Points, Cats);
  145. lines_removed++;
  146. }
  147. bridges_removed++;
  148. }
  149. }
  150. G_verbose_message("Removed lines: %d", lines_removed);
  151. G_verbose_message("Removed bridges: %d", bridges_removed);
  152. }