clean_nodes.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*!
  2. \file clean_nodes.c
  3. \brief Vector library - Clean boundaries at nodes
  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 GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Radim Blazek
  9. */
  10. #include <grass/config.h>
  11. #include <stdlib.h>
  12. #include <grass/gis.h>
  13. #include <grass/vector.h>
  14. #include <grass/glocale.h>
  15. /*!
  16. \brief Clean small angles at nodes.
  17. It may happen that even if the angle between 2 boundaries at node
  18. is very small, the calculated angle is 0 because of representation
  19. error. The map must be built at least on level GV_BUILD_BASE
  20. \param Map input map
  21. \param otype feature type
  22. \param[out] Err vector map where error line segments are written
  23. \return number of line modifications
  24. */
  25. int
  26. Vect_clean_small_angles_at_nodes(struct Map_info *Map, int otype,
  27. struct Map_info *Err)
  28. {
  29. int node, nnodes;
  30. int nmodif = 0;
  31. struct line_pnts *Points;
  32. struct line_cats *SCats, *LCats, *OCats;
  33. Points = Vect_new_line_struct();
  34. SCats = Vect_new_cats_struct();
  35. LCats = Vect_new_cats_struct();
  36. OCats = Vect_new_cats_struct();
  37. nnodes = Vect_get_num_nodes(Map);
  38. for (node = 1; node <= Vect_get_num_nodes(Map); node++) {
  39. int i, nlines;
  40. if (node <= nnodes)
  41. G_percent(node, nnodes, 1);
  42. G_debug(3, "node = %d", node);
  43. if (!Vect_node_alive(Map, node))
  44. continue;
  45. while (1) {
  46. float angle1 = -100;
  47. int line1 = -999; /* value not important, just for debug */
  48. int clean = 1;
  49. nlines = Vect_get_node_n_lines(Map, node);
  50. G_debug(3, "nlines = %d", nlines);
  51. for (i = 0; i < nlines; i++) {
  52. struct P_line *Line;
  53. int line2;
  54. float angle2;
  55. line2 = Vect_get_node_line(Map, node, i);
  56. Line = Map->plus.Line[abs(line2)];
  57. if (!Line)
  58. continue;
  59. G_debug(4, " type = %d", Line->type);
  60. if (!(Line->type & (otype & GV_LINES)))
  61. continue;
  62. angle2 = Vect_get_node_line_angle(Map, node, i);
  63. if (angle2 == -9.0)
  64. continue; /* Degenerated line */
  65. G_debug(4, " line1 = %d angle1 = %e line2 = %d angle2 = %e",
  66. line1, angle1, line2, angle2);
  67. if (angle2 == angle1) {
  68. int j;
  69. double length1, length2;
  70. int short_line; /* line with shorter end segment */
  71. int long_line; /* line with longer end segment */
  72. int new_short_line = 0; /* line number of short line after rewrite */
  73. int short_type, long_type, type;
  74. double x, y, z, nx, ny, nz;
  75. G_debug(4, " identical angles -> clean");
  76. /* Length of end segments for both lines */
  77. Vect_read_line(Map, Points, NULL, abs(line1));
  78. if (line1 > 0) {
  79. length1 =
  80. Vect_points_distance(Points->x[0], Points->y[0],
  81. 0.0, Points->x[1],
  82. Points->y[1], 0.0, 0);
  83. }
  84. else {
  85. int np;
  86. np = Points->n_points;
  87. length1 =
  88. Vect_points_distance(Points->x[np - 1],
  89. Points->y[np - 1], 0.0,
  90. Points->x[np - 2],
  91. Points->y[np - 2], 0.0, 0);
  92. }
  93. Vect_read_line(Map, Points, NULL, abs(line2));
  94. if (line2 > 0) {
  95. length2 =
  96. Vect_points_distance(Points->x[0], Points->y[0],
  97. 0.0, Points->x[1],
  98. Points->y[1], 0.0, 0);
  99. }
  100. else {
  101. int np;
  102. np = Points->n_points;
  103. length2 =
  104. Vect_points_distance(Points->x[np - 1],
  105. Points->y[np - 1], 0.0,
  106. Points->x[np - 2],
  107. Points->y[np - 2], 0.0, 0);
  108. }
  109. G_debug(4, " length1 = %f length2 = %f", length1,
  110. length2);
  111. if (length1 < length2) {
  112. short_line = line1;
  113. long_line = line2;
  114. }
  115. else {
  116. short_line = line2;
  117. long_line = line1;
  118. }
  119. /* Remove end segment from short_line */
  120. short_type =
  121. Vect_read_line(Map, Points, SCats, abs(short_line));
  122. if (short_line > 0) {
  123. x = Points->x[1];
  124. y = Points->y[1];
  125. z = Points->z[1];
  126. Vect_line_delete_point(Points, 0); /* first */
  127. }
  128. else {
  129. x = Points->x[Points->n_points - 2];
  130. y = Points->y[Points->n_points - 2];
  131. z = Points->z[Points->n_points - 2];
  132. Vect_line_delete_point(Points, Points->n_points - 1); /* last */
  133. }
  134. if (Points->n_points > 1) {
  135. new_short_line =
  136. Vect_rewrite_line(Map, abs(short_line),
  137. short_type, Points, SCats);
  138. }
  139. else {
  140. Vect_delete_line(Map, abs(short_line));
  141. }
  142. /* It may happen that it is one line, in that case we have to take the new
  143. * short line as long line, orientation is not changed */
  144. if (abs(line1) == abs(line2)) {
  145. if (long_line > 0)
  146. long_line = new_short_line;
  147. else
  148. long_line = -new_short_line;
  149. }
  150. /* Add new line (must be before rewrite of long_line otherwise node could be deleted) */
  151. long_type =
  152. Vect_read_line(Map, NULL, LCats, abs(long_line));
  153. Vect_reset_cats(OCats);
  154. for (j = 0; j < SCats->n_cats; j++) {
  155. Vect_cat_set(OCats, SCats->field[j], SCats->cat[j]);
  156. }
  157. for (j = 0; j < LCats->n_cats; j++) {
  158. Vect_cat_set(OCats, LCats->field[j], LCats->cat[j]);
  159. }
  160. if (long_type == GV_BOUNDARY || short_type == GV_BOUNDARY) {
  161. type = GV_BOUNDARY;
  162. }
  163. else {
  164. type = GV_LINE;
  165. }
  166. Vect_get_node_coor(Map, node, &nx, &ny, &nz);
  167. Vect_reset_line(Points);
  168. Vect_append_point(Points, nx, ny, nz);
  169. Vect_append_point(Points, x, y, z);
  170. Vect_write_line(Map, type, Points, OCats);
  171. if (Err) {
  172. Vect_write_line(Err, type, Points, OCats);
  173. }
  174. /* Snap long_line to the new short_line end */
  175. long_type =
  176. Vect_read_line(Map, Points, LCats, abs(long_line));
  177. if (long_line > 0) {
  178. Points->x[0] = x;
  179. Points->y[0] = y;
  180. Points->z[0] = z;
  181. }
  182. else {
  183. Points->x[Points->n_points - 1] = x;
  184. Points->y[Points->n_points - 1] = y;
  185. Points->z[Points->n_points - 1] = z;
  186. }
  187. Vect_line_prune(Points);
  188. if (Points->n_points > 1) {
  189. Vect_rewrite_line(Map, abs(long_line), long_type,
  190. Points, LCats);
  191. }
  192. else {
  193. Vect_delete_line(Map, abs(long_line));
  194. }
  195. nmodif += 3;
  196. clean = 0;
  197. break;
  198. }
  199. line1 = line2;
  200. angle1 = angle2;
  201. }
  202. if (clean)
  203. break;
  204. }
  205. }
  206. G_verbose_message("Modifications: %d", nmodif);
  207. return (nmodif);
  208. }