merge.c 5.8 KB

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