merge.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. \file vector/vedit/merge.c
  3. \brief Vedit library - merge lines
  4. (C) 2006-2008 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Jachym Cepicky <jachym.cepicky gmail.com>
  8. \author Martin Landa <landa.martin gmail.com>
  9. \date 2006-2008
  10. */
  11. #include <grass/vedit.h>
  12. /**
  13. \brief Merge two given lines a, b
  14. a : Points1/Cats1
  15. b : Points2/Cats2
  16. merged line : Points/Cats
  17. \param Points1,Cats1 first line
  18. \param Points2,Cats2 second line
  19. \param thresh threshold value
  20. \param[out] Points result line
  21. \return 1 on success
  22. \return 0 on error
  23. */
  24. static int merge_lines(struct line_pnts *Points1, struct line_cats *Cats1,
  25. struct line_pnts *Points2, struct line_cats *Cats2,
  26. double thresh, struct line_pnts **Points);
  27. /**
  28. \brief Merge lines/boundaries
  29. At least two lines need to be given.
  30. \param Map vector map
  31. \param List list of selected lines
  32. \return number of merged lines
  33. \return -1 on error
  34. */
  35. int Vedit_merge_lines(struct Map_info *Map, struct ilist *List)
  36. {
  37. struct ilist *List_in_box;
  38. struct line_pnts *Points1, *Points2, *Points;
  39. struct line_cats *Cats1, *Cats2;
  40. int line_i, i, j;
  41. int line, line1, type1, line2, type2;
  42. int do_merge;
  43. /* number of lines (original, selected, merged) */
  44. int nlines, nlines_selected, nlines_merged;
  45. nlines_merged = 0;
  46. if (List->n_values < 2) {
  47. return 0;
  48. }
  49. Points1 = Vect_new_line_struct();
  50. Cats1 = Vect_new_cats_struct();
  51. Points2 = Vect_new_line_struct();
  52. Cats2 = Vect_new_cats_struct();
  53. Points = Vect_new_line_struct();
  54. List_in_box = Vect_new_list();
  55. nlines = Vect_get_num_lines(Map);
  56. nlines_selected = List->n_values;
  57. /* merge lines */
  58. for (line_i = 0; line_i < List->n_values; line_i++) {
  59. line1 = List->value[line_i];
  60. if (!Vect_line_alive(Map, line1))
  61. continue;
  62. type1 = Vect_read_line(Map, Points1, Cats1, line1);
  63. if (!(type1 & GV_LINES))
  64. continue;
  65. Vect_reset_line(Points);
  66. for (i = 0; i < Points1->n_points; i += Points1->n_points - 1) {
  67. Vect_reset_list(List_in_box);
  68. /* define searching region */
  69. Vect_reset_line(Points2);
  70. /*
  71. Vect_append_point (Points2, Points1 -> x[i] - thresh,
  72. Points1 -> y[i] + thresh, Points1 -> z[i]);
  73. Vect_append_point (Points2, Points1 -> x[i] + thresh,
  74. Points1 -> y[i] + thresh, Points1 -> z[i]);
  75. Vect_append_point (Points2, Points1 -> x[i] + thresh,
  76. Points1 -> y[i] - thresh, Points1 -> z[i]);
  77. Vect_append_point (Points2, Points1 -> x[i] - thresh,
  78. Points1 -> y[i] - thresh, Points1 -> z[i]);
  79. */
  80. Vect_append_point(Points2, Points1->x[i],
  81. Points1->y[i], Points1->z[i]);
  82. /*
  83. * merge lines only if two lines found in the region
  84. * i.e. the current line and an adjacent line
  85. */
  86. if (1 < Vect_select_lines_by_polygon(Map, Points2, 0, NULL,
  87. GV_LINES, List_in_box)) {
  88. do_merge = 1;
  89. line2 = -1;
  90. for (j = 0; do_merge && j < List->n_values; j++) {
  91. if (List->value[j] == line1 ||
  92. !Vect_line_alive(Map, List->value[j]))
  93. continue;
  94. if (Vect_val_in_list(List_in_box, List->value[j])) {
  95. if (line2 > 0) {
  96. /* three lines found
  97. * selected lines will be not merged
  98. */
  99. do_merge = 0;
  100. }
  101. else {
  102. line2 = List->value[j];
  103. }
  104. }
  105. }
  106. if (!do_merge || line2 < 0)
  107. continue;
  108. type2 = Vect_read_line(Map, Points2, Cats2, line2);
  109. merge_lines(Points1, Cats1, Points2, Cats2, -1.0, &Points); /* do not use threshold value */
  110. G_debug(3, "Vedit_merge_lines(): lines=%d,%d", line1, line2);
  111. if (Points->n_points > 0) {
  112. if (Vect_delete_line(Map, line2) == -1) {
  113. return -1;
  114. }
  115. if (line2 <= nlines)
  116. nlines_merged++;
  117. }
  118. }
  119. } /* for each node */
  120. if (Points->n_points > 0) {
  121. line = Vect_rewrite_line(Map, line1, type1, Points, Cats1);
  122. if (line < 0) {
  123. return -1;
  124. }
  125. if (line1 <= nlines)
  126. nlines_merged++;
  127. /* update number of lines */
  128. Vect_list_append(List, line);
  129. }
  130. } /* for each line */
  131. /* destroy stuctures */
  132. Vect_destroy_line_struct(Points1);
  133. Vect_destroy_line_struct(Points2);
  134. Vect_destroy_line_struct(Points);
  135. Vect_destroy_cats_struct(Cats1);
  136. Vect_destroy_cats_struct(Cats2);
  137. return nlines_merged;
  138. }
  139. static int merge_lines(struct line_pnts *Points1, struct line_cats *Cats1,
  140. struct line_pnts *Points2, struct line_cats *Cats2,
  141. double thresh, struct line_pnts **Points)
  142. {
  143. struct line_pnts *ps = *Points;
  144. struct line_cats *cs = Cats1;
  145. int i, mindistidx;
  146. double mindist;
  147. /* find mininal distance and its index */
  148. mindist = Vedit_get_min_distance(Points1, Points2, 0, /* TODO 3D */
  149. &mindistidx);
  150. G_debug(3, " merge line ? index: %d, mindist: %g, thresh: %g",
  151. mindistidx, mindist, thresh);
  152. if (thresh > 0 && mindist > thresh) {
  153. return 0;
  154. }
  155. /* set index and other things */
  156. switch (mindistidx) {
  157. /* for each mindistidx create new line */
  158. case 0:
  159. Vect_append_points(ps, Points2, GV_BACKWARD);
  160. if (ps->n_points == Points2->n_points)
  161. Vect_append_points(ps, Points1, GV_FORWARD);
  162. break;
  163. case 1:
  164. Vect_append_points(ps, Points2, GV_FORWARD);
  165. if (ps->n_points == Points2->n_points)
  166. Vect_append_points(ps, Points1, GV_FORWARD);
  167. break;
  168. case 2:
  169. if (ps->n_points == 0)
  170. Vect_append_points(ps, Points1, GV_FORWARD);
  171. Vect_append_points(ps, Points2, GV_FORWARD);
  172. break;
  173. case 3:
  174. if (ps->n_points == 0)
  175. Vect_append_points(ps, Points1, GV_FORWARD);
  176. Vect_append_points(ps, Points2, GV_BACKWARD);
  177. break;
  178. default:
  179. break;
  180. }
  181. /* remove duplicate points */
  182. Vect_line_prune(ps);
  183. /* copy categories if needed */
  184. for (i = 0; i < Cats2->n_cats; i++) {
  185. Vect_cat_set(cs, Cats2->field[i], Cats2->cat[i]);
  186. }
  187. return 1;
  188. }