break_polygons.c 8.7 KB

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