bridges.c 5.2 KB

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