break_lines.c 16 KB

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