break_polygons.c 9.0 KB

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