break_polygons.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*!
  2. \file break_polygons.c
  3. \brief Vector library - clean geometry (break polygons)
  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. \author Update for GRASS 7 Markus Metz
  12. */
  13. #include <grass/config.h>
  14. #include <stdlib.h>
  15. #include <math.h>
  16. #include <grass/gis.h>
  17. #include <grass/vector.h>
  18. #include <grass/glocale.h>
  19. /* TODO: 3D support
  20. *
  21. * atan2() gives angle from x-axis
  22. * this is unambiguous only in 2D, not in 3D
  23. *
  24. * one possibility would be to store unit vectors of length 1
  25. * in struct XPNT
  26. * double a1[3], a2[3];
  27. *
  28. * length = sqrt(dx * dx + dy * dy + dz * dz);
  29. * dx /= length; dy /= length; dz /=length;
  30. * a1[0] = dx; a1[1] = dy; a1[2] = dz;
  31. *
  32. * get second dx, dy, dz
  33. * length = sqrt(dx * dx + dy * dy + dz * dz);
  34. * dx /= length; dy /= length; dz /=length;
  35. * a2[0] = dx; a2[1] = dy; a2[2] = dz;
  36. *
  37. * equal angles
  38. * if (a1[0] == a2[0] && a1[1] == a2[1] && a1[2] == a2[2])
  39. *
  40. * disadvantage: increased memory consumption
  41. *
  42. * new function Vect_break_faces() ?
  43. *
  44. */
  45. typedef struct
  46. {
  47. double x, y; /* coords */
  48. double a1, a2; /* angles */
  49. char cross; /* 0 - do not break, 1 - break */
  50. } XPNT;
  51. /* function used by binary tree to compare items */
  52. static int compare_xpnts(const void *Xpnta, const void *Xpntb)
  53. {
  54. XPNT *a, *b;
  55. a = (XPNT *)Xpnta;
  56. b = (XPNT *)Xpntb;
  57. if (a->x > b->x)
  58. return 1;
  59. else if (a->x < b->x)
  60. return -1;
  61. else {
  62. if (a->y > b->y)
  63. return 1;
  64. else if (a->y < b->y)
  65. return -1;
  66. else
  67. return 0;
  68. }
  69. G_warning(_("Break polygons: Bug in binary tree!"));
  70. return 1;
  71. }
  72. /*!
  73. \brief Break polygons in vector map.
  74. Breaks lines specified by type in vector map. Points at
  75. intersections may be optionally written to error map. Input map
  76. must be opened on level 2 for update at least on GV_BUILD_BASE.
  77. Function is optimized for closed polygons rigs (e.g. imported from
  78. OGR) but with clean geometry - adjacent polygons mostly have
  79. identical boundary. Function creates database of ALL points in the
  80. map, and then is looking for those where polygons should be broken.
  81. Lines may be broken only at points existing in input map!
  82. \param Map input map where polygons will be broken
  83. \param type type of line to be broken
  84. \param Err vector map where points at intersections will be written or NULL
  85. \return
  86. */
  87. void
  88. Vect_break_polygons(struct Map_info *Map, int type, struct Map_info *Err)
  89. {
  90. struct line_pnts *BPoints, *Points;
  91. struct line_cats *Cats, *ErrCats;
  92. int i, j, k, ret, ltype, broken, last, nlines;
  93. int nbreaks;
  94. struct RB_TREE *RBTree;
  95. int apoints, npoints, nallpoints, nmarks;
  96. XPNT *XPnt_found, XPnt_search;
  97. double dx, dy, a1 = 0, a2 = 0;
  98. int closed, last_point, cross;
  99. RBTree = rbtree_create(compare_xpnts, sizeof(XPNT));
  100. BPoints = Vect_new_line_struct();
  101. Points = Vect_new_line_struct();
  102. Cats = Vect_new_cats_struct();
  103. ErrCats = Vect_new_cats_struct();
  104. nlines = Vect_get_num_lines(Map);
  105. G_debug(3, "nlines = %d", nlines);
  106. /* Go through all lines in vector, and add each point to structure of points,
  107. * if such point already exists check angles of segments and if differ mark for break */
  108. apoints = 0;
  109. nmarks = 0;
  110. npoints = 0;
  111. nallpoints = 0;
  112. G_verbose_message(_("Break polygons Pass 1: select break points"));
  113. for (i = 1; i <= nlines; i++) {
  114. G_percent(i, nlines, 1);
  115. G_debug(3, "i = %d", i);
  116. if (!Vect_line_alive(Map, i))
  117. continue;
  118. ltype = Vect_read_line(Map, Points, Cats, i);
  119. if (!(ltype & type))
  120. continue;
  121. /* This would be confused by duplicate coordinates (angle cannot be calculated) ->
  122. * prune line first */
  123. Vect_line_prune(Points);
  124. /* If first and last point are identical it is close polygon, we dont need to register last point
  125. * and we can calculate angle for first.
  126. * If first and last point are not identical we have to mark for break both */
  127. last_point = Points->n_points - 1;
  128. if (Points->x[0] == Points->x[last_point] &&
  129. Points->y[0] == Points->y[last_point])
  130. closed = 1;
  131. else
  132. closed = 0;
  133. for (j = 0; j < Points->n_points; j++) {
  134. G_debug(3, "j = %d", j);
  135. nallpoints++;
  136. if (j == last_point && closed)
  137. continue; /* do not register last of close polygon */
  138. XPnt_search.x = Points->x[j];
  139. XPnt_search.y = Points->y[j];
  140. /* Already in DB? */
  141. XPnt_found = rbtree_find(RBTree, &XPnt_search);
  142. if (Points->n_points <= 2 ||
  143. (!closed && (j == 0 || j == last_point))) {
  144. cross = 1; /* mark for cross in any case */
  145. }
  146. else { /* calculate angles */
  147. cross = 0;
  148. if (j == 0 && closed) { /* closed polygon */
  149. dx = Points->x[last_point] - Points->x[0];
  150. dy = Points->y[last_point] - Points->y[0];
  151. a1 = atan2(dy, dx);
  152. dx = Points->x[1] - Points->x[0];
  153. dy = Points->y[1] - Points->y[0];
  154. a2 = atan2(dy, dx);
  155. }
  156. else {
  157. dx = Points->x[j - 1] - Points->x[j];
  158. dy = Points->y[j - 1] - Points->y[j];
  159. a1 = atan2(dy, dx);
  160. dx = Points->x[j + 1] - Points->x[j];
  161. dy = Points->y[j + 1] - Points->y[j];
  162. a2 = atan2(dy, dx);
  163. }
  164. }
  165. if (XPnt_found) { /* found */
  166. if (XPnt_found->cross == 1)
  167. continue; /* already marked */
  168. /* check angles */
  169. if (cross) {
  170. XPnt_found->cross = 1;
  171. nmarks++;
  172. }
  173. else {
  174. G_debug(3, "a1 = %f xa1 = %f a2 = %f xa2 = %f", a1,
  175. XPnt_found->a1, a2, XPnt_found->a2);
  176. if ((a1 == XPnt_found->a1 && a2 == XPnt_found->a2) ||
  177. (a1 == XPnt_found->a2 && a2 == XPnt_found->a1)) { /* identical */
  178. }
  179. else {
  180. XPnt_found->cross = 1;
  181. nmarks++;
  182. }
  183. }
  184. }
  185. else {
  186. if (j == 0 || j == (Points->n_points - 1) ||
  187. Points->n_points < 3) {
  188. XPnt_search.a1 = 0;
  189. XPnt_search.a2 = 0;
  190. XPnt_search.cross = 1;
  191. nmarks++;
  192. }
  193. else {
  194. XPnt_search.a1 = a1;
  195. XPnt_search.a2 = a2;
  196. XPnt_search.cross = 0;
  197. }
  198. /* Add to tree */
  199. rbtree_insert(RBTree, &XPnt_search);
  200. npoints++;
  201. }
  202. }
  203. }
  204. /* G_sleep (10); */
  205. nbreaks = 0;
  206. nallpoints = 0;
  207. /* uncomment to check if search tree is healthy */
  208. /* if (rbtree_debug(RBTree, RBTree->root) == 0)
  209. G_warning("Break polygons: RBTree not ok"); */
  210. /* Second loop through lines (existing when loop is started, no need to process lines written again)
  211. * and break at points marked for break */
  212. G_verbose_message(_("Break polygons Pass 2: break at selected points"));
  213. for (i = 1; i <= nlines; i++) {
  214. int n_orig_points;
  215. G_percent(i, nlines, 1);
  216. G_debug(3, "i = %d", i);
  217. if (!Vect_line_alive(Map, i))
  218. continue;
  219. ltype = Vect_read_line(Map, Points, Cats, i);
  220. if (!(ltype & type))
  221. continue;
  222. if (!(ltype & GV_LINES))
  223. continue; /* Nonsense to break points */
  224. /* Duplicates would result in zero length lines -> prune line first */
  225. n_orig_points = Points->n_points;
  226. Vect_line_prune(Points);
  227. broken = 0;
  228. last = 0;
  229. G_debug(3, "n_points = %d", Points->n_points);
  230. for (j = 1; j < Points->n_points; j++) {
  231. G_debug(3, "j = %d", j);
  232. nallpoints++;
  233. if (Points->n_points <= 1 ||
  234. (j == (Points->n_points - 1) && !broken))
  235. break;
  236. /* One point only or
  237. * last point and line is not broken, do nothing */
  238. XPnt_search.x = Points->x[j];
  239. XPnt_search.y = Points->y[j];
  240. XPnt_found = rbtree_find(RBTree, &XPnt_search);
  241. /* all points must be in the search tree, without duplicates */
  242. if (XPnt_found == NULL)
  243. G_fatal_error(_("Point not in search tree!"));
  244. /* break or write last segment of broken line */
  245. if ((j == (Points->n_points - 1) && broken) ||
  246. XPnt_found->cross) {
  247. Vect_reset_line(BPoints);
  248. for (k = last; k <= j; k++) {
  249. Vect_append_point(BPoints, Points->x[k], Points->y[k],
  250. Points->z[k]);
  251. }
  252. /* Result may collapse to one point */
  253. Vect_line_prune(BPoints);
  254. if (BPoints->n_points > 1) {
  255. ret = Vect_write_line(Map, ltype, BPoints, Cats);
  256. G_debug(3,
  257. "Line %d written j = %d n_points(orig,pruned) = %d n_points(new) = %d",
  258. ret, j, Points->n_points, BPoints->n_points);
  259. }
  260. if (!broken)
  261. Vect_delete_line(Map, i); /* not yet deleted */
  262. /* Write points on breaks */
  263. if (Err) {
  264. if (j < (Points->n_points - 1)) {
  265. Vect_reset_line(BPoints);
  266. Vect_append_point(BPoints, Points->x[j], Points->y[j], 0);
  267. Vect_write_line(Err, GV_POINT, BPoints, ErrCats);
  268. }
  269. }
  270. last = j;
  271. broken = 1;
  272. nbreaks++;
  273. }
  274. }
  275. if (!broken && n_orig_points > Points->n_points) { /* was pruned before -> rewrite */
  276. if (Points->n_points > 1) {
  277. Vect_rewrite_line(Map, i, ltype, Points, Cats);
  278. G_debug(3, "Line %d pruned, npoints = %d", i,
  279. Points->n_points);
  280. }
  281. else {
  282. Vect_delete_line(Map, i);
  283. G_debug(3, "Line %d was deleted", i);
  284. }
  285. }
  286. else {
  287. G_debug(3, "Line %d was not changed", i);
  288. }
  289. }
  290. rbtree_destroy(RBTree);
  291. G_verbose_message(_("Breaks: %d"), nbreaks);
  292. }