bridges.c 5.0 KB

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