break_lines.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*!
  2. * \file lib/vector/Vlib/break_lines.c
  3. *
  4. * \brief Vector library - Clean vector map (break lines)
  5. *
  6. * (C) 2001-2009 by the GRASS Development Team
  7. *
  8. * This program is free software under the
  9. * GNU General Public License (>=v2).
  10. * Read the file COPYING that comes with GRASS
  11. * for details.
  12. *
  13. * \author Radim Blazek
  14. */
  15. #include <stdlib.h>
  16. #include <grass/vector.h>
  17. #include <grass/glocale.h>
  18. static int break_lines(struct Map_info *, struct ilist *, struct ilist *,
  19. int, struct Map_info *, int);
  20. /*!
  21. \brief Break lines in vector map at each intersection.
  22. For details see Vect_break_lines_list().
  23. \param Map input vector map
  24. \param type feature type
  25. \param[out] Err vector map where points at intersections will be written or NULL
  26. \return
  27. */
  28. void
  29. Vect_break_lines(struct Map_info *Map, int type, struct Map_info *Err)
  30. {
  31. break_lines(Map, NULL, NULL, type, Err, 0);
  32. return;
  33. }
  34. /*!
  35. \brief Break selected lines in vector map at each intersection.
  36. Breaks selected lines specified by type in vector map. Points at
  37. intersections may be optionally written to error map. Input vector map
  38. must be opened on level 2 for update at least on GV_BUILD_BASE.
  39. The function also breaks lines forming collapsed loop, for example
  40. 0,0;1,0;0,0 is broken at 1,0.
  41. If reference lines are given (<i>List_ref</i>) break only lines
  42. which intersect reference lines.
  43. \param Map input vector map
  44. \param List_break list of lines (NULL for all lines in vector map)
  45. \param List_ref list of reference lines or NULL
  46. \param type feature type
  47. \param[out] Err vector map where points at intersections will be written or NULL
  48. \return number of intersections
  49. */
  50. int
  51. Vect_break_lines_list(struct Map_info *Map, struct ilist *List_break,
  52. struct ilist *List_ref, int type,
  53. struct Map_info *Err)
  54. {
  55. return break_lines(Map, List_break, List_ref, type, Err, 0);
  56. }
  57. /*!
  58. \brief Check for and count intersecting lines, do not break.
  59. For details see Vect_break_lines_list().
  60. \param Map input vector map
  61. \param type feature type
  62. \param[out] Err vector map where points at intersections will be written or NULL
  63. \return
  64. */
  65. int
  66. Vect_check_line_breaks(struct Map_info *Map, int type, struct Map_info *Err)
  67. {
  68. return break_lines(Map, NULL, NULL, type, Err, 1);
  69. }
  70. /*!
  71. \brief Check for and count intersecting lines, do not break.
  72. If <i>List_break</i> is given, only lines in the list are checked for
  73. intersections.
  74. If reference lines are given (<i>List_ref</i>) break only lines
  75. which intersect reference lines.
  76. \param Map input vector map
  77. \param List_break list of lines (NULL for all lines in vector map)
  78. \param List_ref list of reference lines or NULL
  79. \param type feature type
  80. \param[out] Err vector map where points at intersections will be written or NULL
  81. \return number of intersections
  82. */
  83. int
  84. Vect_check_line_breaks_list(struct Map_info *Map, struct ilist *List_break,
  85. struct ilist *List_ref, int type,
  86. struct Map_info *Err)
  87. {
  88. return break_lines(Map, List_break, List_ref, type, Err, 1);
  89. }
  90. static int cmp(const void *a, const void *b)
  91. {
  92. int ai = *(int *)a;
  93. int bi = *(int *)b;
  94. return (ai - bi);
  95. }
  96. static void sort_ilist(struct ilist *List)
  97. {
  98. int i, j, is_sorted = 1;
  99. for (i = 1; i < List->n_values; i++) {
  100. if (List->value[i - 1] > List->value[i]) {
  101. is_sorted = 0;
  102. break;
  103. }
  104. }
  105. if (!is_sorted)
  106. qsort(List->value, List->n_values, sizeof(int), cmp);
  107. if (List->n_values > 1) {
  108. j = 1;
  109. for (i = 1; i < List->n_values; i++) {
  110. if (List->value[j - 1] != List->value[i]) {
  111. List->value[j] = List->value[i];
  112. j++;
  113. }
  114. }
  115. List->n_values = j;
  116. }
  117. }
  118. int
  119. break_lines(struct Map_info *Map, struct ilist *List_break,
  120. struct ilist *List_ref, int type,
  121. struct Map_info *Err, int check)
  122. {
  123. struct line_pnts *APoints, *BPoints, *Points;
  124. struct line_pnts **AXLines, **BXLines;
  125. struct line_cats *ACats, *BCats, *Cats;
  126. int i, j, k, l, ret, atype, btype, aline, bline, found, iline;
  127. int nlines, nlines_org;
  128. int naxlines, nbxlines, nx;
  129. double *xx = NULL, *yx = NULL, *zx = NULL;
  130. struct bound_box ABox, *BBox;
  131. struct boxlist *List;
  132. int nbreaks;
  133. int touch1_n = 0, touch1_s = 0, touch1_e = 0, touch1_w = 0; /* other vertices except node1 touching box */
  134. int touch2_n = 0, touch2_s = 0, touch2_e = 0, touch2_w = 0; /* other vertices except node2 touching box */
  135. int is3d;
  136. int node, anode1, anode2, bnode1, bnode2;
  137. double nodex, nodey;
  138. int a_is_ref, b_is_ref, break_a, break_b;
  139. APoints = Vect_new_line_struct();
  140. BPoints = Vect_new_line_struct();
  141. Points = Vect_new_line_struct();
  142. ACats = Vect_new_cats_struct();
  143. BCats = Vect_new_cats_struct();
  144. Cats = Vect_new_cats_struct();
  145. List = Vect_new_boxlist(1);
  146. is3d = Vect_is_3d(Map);
  147. if (List_ref)
  148. sort_ilist(List_ref);
  149. if (List_break)
  150. sort_ilist(List_break);
  151. if (List_ref) {
  152. nlines = List_ref->n_values;
  153. nlines_org = List_ref->value[List_ref->n_values - 1];
  154. }
  155. else if (List_break) {
  156. nlines = List_break->n_values;
  157. nlines_org = List_break->value[List_break->n_values - 1];
  158. }
  159. else {
  160. nlines = Vect_get_num_lines(Map);
  161. nlines_org = nlines;
  162. }
  163. G_debug(3, "nlines = %d", nlines);
  164. /* TODO:
  165. * 1. It seems that lines/boundaries are not broken at intersections
  166. * with points/centroids. Check if true, if yes, skip GV_POINTS
  167. * 2. list of lines to break and list of reference lines
  168. * aline: reference line, if List_ref == NULL, use all
  169. * break aline only if it is in the list of lines to break
  170. * bline: line to break, if List_break == NULL, break all
  171. */
  172. /* To find intersection of two lines (Vect_line_intersection) is quite slow.
  173. * Fortunately usual lines/boundaries in GIS often forms a network where lines
  174. * are connected by end points, and touch by MBR. This function checks and occasionaly
  175. * skips such cases. This is currently done for 2D only
  176. */
  177. /* Go through all lines in vector, for each select lines which overlap MBR of
  178. * this line exclude those connected by one endpoint (see above)
  179. * and try to intersect, if lines intersect write new lines at the end of
  180. * the file, and process next line (remaining lines overlapping box are skipped)
  181. */
  182. nbreaks = 0;
  183. for (iline = 0; iline < nlines; iline++) {
  184. G_percent(iline, nlines, 1);
  185. /* aline: reference line */
  186. if (List_ref) {
  187. aline = List_ref->value[iline];
  188. }
  189. else if (List_break) {
  190. aline = List_break->value[iline];
  191. }
  192. else {
  193. aline = iline + 1;
  194. }
  195. G_debug(3, "aline = %d", aline);
  196. if (!Vect_line_alive(Map, aline))
  197. continue;
  198. a_is_ref = 0;
  199. break_a = 1;
  200. if (List_ref) {
  201. a_is_ref = 1;
  202. }
  203. if (List_break) {
  204. break_a = 0;
  205. if (bsearch(&aline, List_break->value, List_break->n_values, sizeof(int), cmp)) {
  206. break_a = 1;
  207. }
  208. }
  209. atype = Vect_read_line(Map, APoints, ACats, aline);
  210. if (!(atype & type))
  211. continue;
  212. Vect_line_prune(APoints);
  213. Vect_line_box(APoints, &ABox);
  214. /* Find which sides of the box are touched by intermediate (non-end) points of line */
  215. if (!is3d) {
  216. touch1_n = touch1_s = touch1_e = touch1_w = 0;
  217. for (j = 1; j < APoints->n_points; j++) {
  218. if (APoints->y[j] == ABox.N)
  219. touch1_n = 1;
  220. if (APoints->y[j] == ABox.S)
  221. touch1_s = 1;
  222. if (APoints->x[j] == ABox.E)
  223. touch1_e = 1;
  224. if (APoints->x[j] == ABox.W)
  225. touch1_w = 1;
  226. }
  227. G_debug(3, "touch1: n = %d s = %d e = %d w = %d", touch1_n,
  228. touch1_s, touch1_e, touch1_w);
  229. touch2_n = touch2_s = touch2_e = touch2_w = 0;
  230. for (j = 0; j < APoints->n_points - 1; j++) {
  231. if (APoints->y[j] == ABox.N)
  232. touch2_n = 1;
  233. if (APoints->y[j] == ABox.S)
  234. touch2_s = 1;
  235. if (APoints->x[j] == ABox.E)
  236. touch2_e = 1;
  237. if (APoints->x[j] == ABox.W)
  238. touch2_w = 1;
  239. }
  240. G_debug(3, "touch2: n = %d s = %d e = %d w = %d", touch2_n,
  241. touch2_s, touch2_e, touch2_w);
  242. }
  243. Vect_select_lines_by_box(Map, &ABox, type, List);
  244. G_debug(3, " %d lines selected by box", List->n_values);
  245. for (j = 0; j < List->n_values; j++) {
  246. /* bline: line to break */
  247. bline = List->id[j];
  248. b_is_ref = 0;
  249. break_b = 1;
  250. if (List_ref &&
  251. bsearch(&bline, List_ref->value, List_ref->n_values, sizeof(int), cmp)) {
  252. b_is_ref = 1;
  253. /* reference bline will be broken when it is aline */
  254. break_b = 0;
  255. }
  256. if (List_break) {
  257. break_b = 0;
  258. if (bsearch(&bline, List_break->value, List_break->n_values, sizeof(int), cmp)) {
  259. break_b = 1;
  260. }
  261. }
  262. if (!break_a && !break_b)
  263. continue;
  264. /* check intersection of aline with bline only once */
  265. if (break_a && break_b && aline < bline && (!List_ref || b_is_ref)) {
  266. continue;
  267. }
  268. G_debug(3, " j = %d bline = %d", j, bline);
  269. btype = Vect_read_line(Map, BPoints, BCats, bline);
  270. Vect_line_prune(BPoints);
  271. BBox = &List->box[j];
  272. /* Check if touch by end node only */
  273. if (!is3d) {
  274. Vect_get_line_nodes(Map, aline, &anode1, &anode2);
  275. Vect_get_line_nodes(Map, bline, &bnode1, &bnode2);
  276. node = 0;
  277. if (anode1 == bnode1 || anode1 == bnode2)
  278. node = anode1;
  279. else if (anode2 == bnode1 || anode2 == bnode2)
  280. node = anode2;
  281. if (node) {
  282. Vect_get_node_coor(Map, node, &nodex, &nodey, NULL);
  283. if ((node == anode1 && nodey == ABox.N &&
  284. !touch1_n && nodey == BBox->S) ||
  285. (node == anode2 && nodey == ABox.N &&
  286. !touch2_n && nodey == BBox->S) ||
  287. (node == anode1 && nodey == ABox.S &&
  288. !touch1_s && nodey == BBox->N) ||
  289. (node == anode2 && nodey == ABox.S &&
  290. !touch2_s && nodey == BBox->N) ||
  291. (node == anode1 && nodex == ABox.E &&
  292. !touch1_e && nodex == BBox->W) ||
  293. (node == anode2 && nodex == ABox.E &&
  294. !touch2_e && nodex == BBox->W) ||
  295. (node == anode1 && nodex == ABox.W &&
  296. !touch1_w && nodex == BBox->E) ||
  297. (node == anode2 && nodex == ABox.W &&
  298. !touch2_w && nodex == BBox->E)) {
  299. G_debug(3,
  300. "lines %d and %d touching by end nodes only -> no intersection",
  301. aline, bline);
  302. continue;
  303. }
  304. }
  305. }
  306. AXLines = NULL;
  307. BXLines = NULL;
  308. Vect_line_intersection(APoints, BPoints, &ABox, BBox,
  309. &AXLines, &BXLines,
  310. &naxlines, &nbxlines, 0);
  311. G_debug(3, " naxlines = %d nbxlines = %d", naxlines, nbxlines);
  312. /* This part handles a special case when aline == bline, no other intersection was found
  313. * and the line is forming collapsed loop, for example 0,0;1,0;0,0 should be broken at 1,0.
  314. * ---> */
  315. if (aline == bline && naxlines == 0 && nbxlines == 0 &&
  316. APoints->n_points >= 3 && break_a) {
  317. int centre;
  318. G_debug(3, " Check collapsed loop");
  319. if (APoints->n_points % 2) { /* odd number of vertices */
  320. centre = APoints->n_points / 2; /* index of centre */
  321. if (APoints->x[centre - 1] == APoints->x[centre + 1] && APoints->y[centre - 1] == APoints->y[centre + 1] && APoints->z[centre - 1] == APoints->z[centre + 1]) { /* -> break */
  322. AXLines =
  323. (struct line_pnts **)G_malloc(2 *
  324. sizeof(struct
  325. line_pnts
  326. *));
  327. AXLines[0] = Vect_new_line_struct();
  328. AXLines[1] = Vect_new_line_struct();
  329. for (i = 0; i <= centre; i++)
  330. Vect_append_point(AXLines[0], APoints->x[i],
  331. APoints->y[i], APoints->z[i]);
  332. for (i = centre; i < APoints->n_points; i++)
  333. Vect_append_point(AXLines[1], APoints->x[i],
  334. APoints->y[i], APoints->z[i]);
  335. naxlines = 2;
  336. }
  337. }
  338. }
  339. /* <--- */
  340. if (Err) { /* array for intersections (more than needed */
  341. xx = (double *)G_malloc((naxlines + nbxlines) *
  342. sizeof(double));
  343. yx = (double *)G_malloc((naxlines + nbxlines) *
  344. sizeof(double));
  345. zx = (double *)G_malloc((naxlines + nbxlines) *
  346. sizeof(double));
  347. }
  348. nx = 0; /* number of intersections to be written to Err */
  349. if (naxlines > 0) { /* intersection -> write out */
  350. if (!check && break_a)
  351. Vect_delete_line(Map, aline);
  352. for (k = 0; k < naxlines; k++) {
  353. /* Write new line segments */
  354. /* line may collapse, don't write zero length lines */
  355. Vect_line_prune(AXLines[k]);
  356. if ((atype & GV_POINTS) || AXLines[k]->n_points > 1) {
  357. if (!check && break_a) {
  358. ret = Vect_write_line(Map, atype, AXLines[k],
  359. ACats);
  360. G_debug(3, "Line %d written, npoints = %d", ret,
  361. AXLines[k]->n_points);
  362. if (List_ref && a_is_ref) {
  363. G_ilist_add(List_ref, ret);
  364. }
  365. if (List_break && break_a) {
  366. G_ilist_add(List_break, ret);
  367. }
  368. }
  369. }
  370. /* Write intersection points */
  371. if (Err) {
  372. if (k > 0) {
  373. xx[nx] = AXLines[k]->x[0];
  374. yx[nx] = AXLines[k]->y[0];
  375. zx[nx] = AXLines[k]->z[0];
  376. nx++;
  377. }
  378. }
  379. Vect_destroy_line_struct(AXLines[k]);
  380. }
  381. nbreaks += naxlines - 1;
  382. }
  383. if (AXLines)
  384. G_free(AXLines);
  385. if (nbxlines > 0) {
  386. if (aline != bline) { /* Self intersection, do not write twice, TODO: is it OK? */
  387. if (!check && break_b)
  388. Vect_delete_line(Map, bline);
  389. for (k = 0; k < nbxlines; k++) {
  390. /* Write new line segments */
  391. /* line may collapse, don't write zero length lines */
  392. Vect_line_prune(BXLines[k]);
  393. if ((btype & GV_POINTS) || BXLines[k]->n_points > 1) {
  394. if (!check && break_b) {
  395. ret =
  396. Vect_write_line(Map, btype, BXLines[k],
  397. BCats);
  398. G_debug(5, "Line %d written", ret);
  399. if (List_ref && b_is_ref) {
  400. G_ilist_add(List_ref, ret);
  401. }
  402. if (List_break) {
  403. G_ilist_add(List_break, ret);
  404. }
  405. }
  406. }
  407. /* Write intersection points */
  408. if (Err) {
  409. if (k > 0) {
  410. found = 0;
  411. for (l = 0; l < nx; l++) {
  412. if (xx[l] == BXLines[k]->x[0] &&
  413. yx[l] == BXLines[k]->y[0] &&
  414. zx[l] == BXLines[k]->z[0]) {
  415. found = 1;
  416. break;
  417. }
  418. }
  419. if (!found) {
  420. xx[nx] = BXLines[k]->x[0];
  421. yx[nx] = BXLines[k]->y[0];
  422. zx[nx] = BXLines[k]->z[0];
  423. nx++;
  424. }
  425. }
  426. }
  427. }
  428. nbreaks += nbxlines - 1;
  429. }
  430. for (k = 0; k < nbxlines; k++)
  431. Vect_destroy_line_struct(BXLines[k]);
  432. }
  433. if (BXLines)
  434. G_free(BXLines);
  435. if (Err) {
  436. for (l = 0; l < nx; l++) { /* Write out errors */
  437. Vect_reset_line(Points);
  438. Vect_append_point(Points, xx[l], yx[l], zx[l]);
  439. ret = Vect_write_line(Err, GV_POINT, Points, Cats);
  440. }
  441. G_free(xx);
  442. G_free(yx);
  443. G_free(zx);
  444. }
  445. if (naxlines > 0 && !check && break_a) {
  446. G_debug(3, "aline was broken, use next one");
  447. break; /* first line was broken and deleted -> take the next one */
  448. }
  449. }
  450. if (List_ref) {
  451. nlines = List_ref->n_values;
  452. }
  453. else if (List_break) {
  454. nlines = List_break->n_values;
  455. }
  456. else {
  457. nlines = Vect_get_num_lines(Map);
  458. }
  459. G_debug(3, "nlines = %d", nlines);
  460. } /* for each line */
  461. G_percent(nlines, nlines, 1); /* finish it */
  462. G_verbose_message(_("Intersections: %d"), nbreaks);
  463. Vect_destroy_line_struct(APoints);
  464. Vect_destroy_line_struct(BPoints);
  465. Vect_destroy_line_struct(Points);
  466. Vect_destroy_cats_struct(ACats);
  467. Vect_destroy_cats_struct(BCats);
  468. Vect_destroy_cats_struct(Cats);
  469. Vect_destroy_boxlist(List);
  470. return nbreaks;
  471. }