plus_area.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /**
  2. * \file plus_area.c
  3. *
  4. * \brief Vector library - update topo for areas (lower level functions)
  5. *
  6. * Lower level functions for reading/writing/manipulating vectors.
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author CERL (probably Dave Gerdes), Radim Blazek
  12. *
  13. * \date 2001-2006
  14. */
  15. #include <grass/config.h>
  16. #include <stdlib.h>
  17. #include <grass/vector.h>
  18. #include <grass/glocale.h>
  19. /*!
  20. * \brief Build topo for area from lines
  21. *
  22. * Area is built in clockwise order.
  23. * Take a given line and start off to the RIGHT/LEFT and try to complete
  24. * an area.
  25. *
  26. * Possible Scenarios:
  27. * - I. path runs into first line. : AREA!
  28. * - II. path runs into a dead end (no other area lines at node) : no area
  29. * - III. path runs into a previous line that is not 1st line or to 1st line but not to start node : no area
  30. *
  31. * After we find an area then we call point_in_area() to see if the
  32. * specified point is w/in the area
  33. *
  34. * Old returns -1: error 0: no area (1: point in area)
  35. * -2: island !!
  36. *
  37. * \param[in] plus pointer to Plus_head structure
  38. * \param[in] first_line line id of first line
  39. * \param[in] side side of line to build area on (GV_LEFT | GV_RIGHT)
  40. * \param[in] lines pointer to array of lines
  41. *
  42. * \return -1 on error
  43. * \return 0 no area
  44. * \return number of lines
  45. */
  46. int
  47. dig_build_area_with_line(struct Plus_head *plus, plus_t first_line, int side,
  48. plus_t ** lines)
  49. {
  50. register int i;
  51. int prev_line, next_line;
  52. static plus_t *array;
  53. char *p;
  54. static int array_size; /* 0 on startup */
  55. int n_lines;
  56. struct P_line *Line;
  57. int node;
  58. const char *dstr;
  59. int debug_level;
  60. dstr = G__getenv("DEBUG");
  61. if (dstr != NULL)
  62. debug_level = atoi(dstr);
  63. else
  64. debug_level = 0;
  65. G_debug(3, "dig_build_area_with_line(): first_line = %d, side = %d",
  66. first_line, side);
  67. /* First check if line is not degenerated (degenerated lines have angle -9)
  68. * Following degenerated lines are skip by dig_angle_next_line() */
  69. Line = plus->Line[first_line];
  70. node = Line->N1; /* to check one is enough, because if degenerated N1 == N2 */
  71. if (dig_node_line_angle(plus, node, first_line) == -9.) {
  72. G_debug(3, "First line degenerated");
  73. return (0);
  74. }
  75. if (array_size == 0) { /* first time */
  76. array_size = 1000;
  77. array = (plus_t *) dig__falloc(array_size, sizeof(plus_t));
  78. if (array == NULL)
  79. return (dig_out_of_memory());
  80. }
  81. if (side == GV_LEFT) {
  82. first_line = -first_line; /* start at node1, reverse direction */
  83. }
  84. array[0] = first_line;
  85. prev_line = -first_line; /* start at node2 for direct and node1 for
  86. reverse direction */
  87. /* angle of first line */
  88. n_lines = 1;
  89. while (1) {
  90. next_line =
  91. dig_angle_next_line(plus, prev_line, GV_RIGHT, GV_BOUNDARY);
  92. G_debug(3, "next_line = %d", next_line);
  93. if (next_line == 0)
  94. return (-1); /* Not found */
  95. /* Check if adjacent lines do not have the same angle */
  96. if (!dig_node_angle_check(plus, next_line, GV_BOUNDARY)) {
  97. G_debug(3,
  98. "Cannot build area, a neighbour of the line %d has the same angle at the node",
  99. next_line);
  100. return 0;
  101. }
  102. /* I. Area closed. This also handles the problem w/ 1 single area line */
  103. if (first_line == next_line) {
  104. /* GOT ONE! fill area struct and return */
  105. G_debug(3, "Got one! :");
  106. /* avoid loop when not debugging */
  107. if (debug_level > 2) {
  108. for (i = 0; i < n_lines; i++) {
  109. G_debug(3, " area line (%d) = %d", i, array[i]);
  110. }
  111. }
  112. *lines = array;
  113. return (n_lines);
  114. }
  115. /* II. Note this is a dead end */
  116. /* ( if prev_line != -first_line so it goes after the previous test) ? */
  117. if (prev_line == next_line) {
  118. G_debug(3, "Dead_end:");
  119. return (0); /* dead end */
  120. }
  121. /* III. Unclosed ?, I would say started from free end */
  122. for (i = 0; i < n_lines; i++)
  123. if (abs(next_line) == abs(array[i])) {
  124. G_debug(3, "Unclosed area:");
  125. return (0); /* ran into a different area */
  126. }
  127. /* otherwise keep going */
  128. if (n_lines >= array_size) {
  129. p = dig__frealloc(array, array_size + 100, sizeof(plus_t),
  130. array_size);
  131. if (p == NULL)
  132. return (dig_out_of_memory());
  133. array = (plus_t *) p;
  134. array_size += 100;
  135. }
  136. array[n_lines++] = next_line;
  137. prev_line = -next_line;
  138. }
  139. return 0;
  140. }
  141. /*!
  142. * \brief Allocate space for new area and create boundary info from array.
  143. *
  144. * Then for each line in area, update line (right,left) info.
  145. *
  146. * Neither islands nor centroids area filled.
  147. *
  148. * \param[in] plus pointer to Plus_head structure
  149. * \param[in] n_lines number of lines
  150. * \param[in] lines array of lines, negative for reverse direction
  151. *
  152. * \return number of new area
  153. * \return -1 on error
  154. */
  155. int dig_add_area(struct Plus_head *plus, int n_lines, plus_t * lines)
  156. {
  157. register int i;
  158. register int area, line;
  159. struct P_area *Area;
  160. struct P_line *Line;
  161. struct bound_box box, abox;
  162. G_debug(3, "dig_add_area():");
  163. /* First look if we have space in array of pointers to areas
  164. * and reallocate if necessary */
  165. if (plus->n_areas >= plus->alloc_areas) { /* array is full */
  166. if (dig_alloc_areas(plus, 1000) == -1)
  167. return -1;
  168. }
  169. /* allocate area structure */
  170. area = plus->n_areas + 1;
  171. G_debug(3, " new area = %d", area);
  172. Area = dig_alloc_area();
  173. if (Area == NULL)
  174. return -1;
  175. if (dig_area_alloc_line(Area, n_lines) == -1)
  176. return -1;
  177. for (i = 0; i < n_lines; i++) {
  178. line = lines[i];
  179. Area->lines[i] = line;
  180. Line = plus->Line[abs(line)];
  181. if (plus->do_uplist)
  182. dig_line_add_updated(plus, abs(line));
  183. if (line < 0) { /* revers direction -> area on left */
  184. if (Line->left != 0) {
  185. G_warning(_("Line %d already has area/isle %d to left"), line,
  186. Line->left);
  187. return -1;
  188. }
  189. G_debug(3, " Line %d left set to %d.", line, area);
  190. Line->left = area;
  191. }
  192. else {
  193. if (Line->right != 0) {
  194. G_warning(_("Line %d already has area/isle %d to right"),
  195. line, Line->right);
  196. return -1;
  197. }
  198. G_debug(3, " Line %d right set to %d.", line, area);
  199. Line->right = area;
  200. }
  201. dig_line_get_box(plus, abs(line), &box);
  202. if (i == 0)
  203. dig_box_copy(&abox, &box);
  204. else
  205. dig_box_extend(&abox, &box);
  206. }
  207. Area->n_lines = n_lines;
  208. Area->centroid = 0;
  209. plus->Area[area] = Area;
  210. dig_area_set_box(plus, area, &abox);
  211. dig_spidx_add_area(plus, area, &abox);
  212. plus->n_areas++;
  213. return (area);
  214. }
  215. /*!
  216. * \brief Add isle to area if does not exist yet.
  217. *
  218. * \param[in] plus pointer to Plus_head structure
  219. * \param[in] area area id
  220. * \param[in] isle isle id
  221. *
  222. * \return 0
  223. */
  224. int dig_area_add_isle(struct Plus_head *plus, int area, int isle)
  225. {
  226. int i;
  227. struct P_area *Area;
  228. G_debug(3, "dig_area_add_isle(): area = %d isle = %d", area, isle);
  229. Area = plus->Area[area];
  230. if (Area == NULL)
  231. G_fatal_error("Attempt to add isle to dead area");
  232. for (i = 0; i < Area->n_isles; i++) {
  233. if (Area->isles[i] == isle) { /* Already exist */
  234. G_debug(3, "isle already registered in area");
  235. return 0;
  236. }
  237. }
  238. if (Area->alloc_isles <= Area->n_isles) /* array is full */
  239. dig_area_alloc_isle(Area, 1);
  240. Area->isles[Area->n_isles] = isle;
  241. Area->n_isles++;
  242. G_debug(3, " -> n_isles = %d", Area->n_isles);
  243. return 0;
  244. }
  245. /*!
  246. * \brief Delete isle from area.
  247. *
  248. * \param[in] plus pointer to Plus_head structure
  249. * \param[in] area area id
  250. * \param[in] isle isle id
  251. *
  252. * \return 0
  253. */
  254. int dig_area_del_isle(struct Plus_head *plus, int area, int isle)
  255. {
  256. int i, mv;
  257. struct P_area *Area;
  258. G_debug(3, "dig_area_del_isle(): area = %d isle = %d", area, isle);
  259. Area = plus->Area[area];
  260. if (Area == NULL)
  261. G_fatal_error(_("Attempt to delete isle from dead area"));
  262. mv = 0;
  263. for (i = 0; i < Area->n_isles; i++) {
  264. if (mv) {
  265. Area->isles[i - 1] = Area->isles[i];
  266. }
  267. else {
  268. if (Area->isles[i] == isle)
  269. mv = 1;
  270. }
  271. }
  272. if (mv) {
  273. Area->n_isles--;
  274. }
  275. else {
  276. G_fatal_error(_("Attempt to delete not registered isle %d from area %d"),
  277. isle, area);
  278. }
  279. return 0;
  280. }
  281. /*!
  282. * \brief Delete area from Plus_head structure
  283. *
  284. * This function deletes area from the topo structure and resets references
  285. * to this area in lines, isles (within) to 0.
  286. * Possible new area is not created by this function, so that
  287. * old boundaries participating in this area are left without area information
  288. * even if form new area.
  289. * Not enabled now: If area is inside other area, area info for islands within
  290. * deleted area is reset to that area outside.
  291. * (currently area info of isles is set to 0)
  292. *
  293. * \param[in] plus pointer to Plus_head structure
  294. * \param[in] area area id
  295. *
  296. * \return 0 on error
  297. * \return 1 on success
  298. */
  299. int dig_del_area(struct Plus_head *plus, int area)
  300. {
  301. int i, line;
  302. /* int isle, area_out; */
  303. struct P_area *Area;
  304. struct P_line *Line;
  305. struct P_isle *Isle;
  306. G_debug(3, "dig_del_area() area = %d", area);
  307. Area = plus->Area[area];
  308. if (Area == NULL) {
  309. G_warning(_("Attempt to delete dead area"));
  310. return 0;
  311. }
  312. dig_spidx_del_area(plus, area);
  313. /* Set area for all lines to 0 */
  314. /* isle = 0; */
  315. for (i = 0; i < Area->n_lines; i++) {
  316. line = Area->lines[i]; /* >0 = clockwise -> right, <0 = counterclockwise ->left */
  317. Line = plus->Line[abs(line)];
  318. if (plus->do_uplist)
  319. dig_line_add_updated(plus, abs(line));
  320. if (line > 0) {
  321. G_debug(3, " Set line %d right side to 0", line);
  322. Line->right = 0;
  323. }
  324. else {
  325. G_debug(3, " Set line %d left side to 0", line);
  326. Line->left = 0;
  327. }
  328. /* Find the isle this area is part of (used late below) */
  329. /*
  330. if ( line > 0 ) {
  331. if ( Line->left < 0 ) isle = Line->left;
  332. } else {
  333. if ( Line->right < 0 ) isle = Line->right;
  334. }
  335. */
  336. }
  337. /* Unset area information of centroid */
  338. /* TODO: duplicate centroids have also area information ->
  339. * 1) do not save such info
  340. * 2) find all by box and reset info */
  341. line = Area->centroid;
  342. if (line > 0) {
  343. Line = plus->Line[line];
  344. if (!Line) {
  345. G_warning(_("Dead centroid %d registered for area (bug in the vector library)"),
  346. line);
  347. }
  348. else {
  349. Line->left = 0;
  350. if (plus->do_uplist)
  351. dig_line_add_updated(plus, line);
  352. }
  353. }
  354. /* Find the area this area is within */
  355. /*
  356. area_out = 0;
  357. if ( isle > 0 ) {
  358. Isle = plus->Isle[abs(isle)];
  359. area_out = Isle->area;
  360. }
  361. */
  362. /* Reset information about area outside for isles within this area */
  363. G_debug(3, " n_isles = %d", Area->n_isles);
  364. for (i = 0; i < Area->n_isles; i++) {
  365. Isle = plus->Isle[Area->isles[i]];
  366. if (Isle == NULL) {
  367. G_fatal_error(_("Attempt to delete area %d info from dead isle %d"),
  368. area, Area->isles[i]);
  369. }
  370. else {
  371. /* Isle->area = area_out; */
  372. Isle->area = 0;
  373. }
  374. }
  375. /* free structures */
  376. dig_free_area(Area);
  377. plus->Area[area] = NULL;
  378. return 1;
  379. }
  380. /*!
  381. * \brief Set area bounding box
  382. *
  383. * \param[in] plus pointer to Plus_head structure
  384. * \param[in] area area id
  385. * \param[in] Box bounding box
  386. *
  387. * \return 1
  388. */
  389. int dig_area_set_box(struct Plus_head *plus, plus_t area, struct bound_box * Box)
  390. {
  391. struct P_area *Area;
  392. Area = plus->Area[area];
  393. Area->N = Box->N;
  394. Area->S = Box->S;
  395. Area->E = Box->E;
  396. Area->W = Box->W;
  397. Area->T = Box->T;
  398. Area->B = Box->B;
  399. return (1);
  400. }
  401. /*!
  402. * \brief Find number line of next angle to follow an line
  403. *
  404. * Assume that lines are sorted in increasing angle order and angles
  405. * of points and degenerated lines are set to 9 (ignored).
  406. *
  407. * \param[in] plus pointer to Plus_head structure
  408. * \param[in] current_line current line id, negative if request for node 2
  409. * \param[in] side side GV_RIGHT or GV_LEFT
  410. * \param[in] type line type (GV_LINE, GV_BOUNDARY or both)
  411. *
  412. * \return line number of next angle to follow an line (negative if connected by node2)
  413. * (number of current line may be return if dangle - this is used in build)
  414. * \return 0 on error or not found
  415. */
  416. int
  417. dig_angle_next_line(struct Plus_head *plus, plus_t current_line, int side,
  418. int type)
  419. {
  420. int i, next;
  421. int current;
  422. int line;
  423. plus_t node;
  424. struct P_node *Node;
  425. struct P_line *Line;
  426. const char *dstr;
  427. int debug_level;
  428. dstr = G__getenv("DEBUG");
  429. if (dstr != NULL)
  430. debug_level = atoi(dstr);
  431. else
  432. debug_level = 0;
  433. G_debug(3, "dig__angle_next_line: line = %d, side = %d, type = %d",
  434. current_line, side, type);
  435. Line = plus->Line[abs(current_line)];
  436. if (current_line > 0)
  437. node = Line->N1;
  438. else
  439. node = Line->N2;
  440. G_debug(3, " node = %d", node);
  441. Node = plus->Node[node];
  442. G_debug(3, " n_lines = %d", Node->n_lines);
  443. /* avoid loop when not debugging */
  444. if (debug_level > 2) {
  445. for (i = 0; i < Node->n_lines; i++) {
  446. G_debug(3, " i = %d line = %d angle = %f", i, Node->lines[i],
  447. Node->angles[i]);
  448. }
  449. }
  450. /* first find index for that line */
  451. next = -1;
  452. for (current = 0; current < Node->n_lines; current++) {
  453. if (Node->lines[current] == current_line)
  454. next = current;
  455. }
  456. if (next == -1)
  457. return 0; /* not found */
  458. G_debug(3, " current position = %d", next);
  459. while (1) {
  460. if (side == GV_RIGHT) { /* go up (greater angle) */
  461. if (next == Node->n_lines - 1)
  462. next = 0;
  463. else
  464. next++;
  465. }
  466. else { /* go down (smaller angle) */
  467. if (next == 0)
  468. next = Node->n_lines - 1;
  469. else
  470. next--;
  471. }
  472. G_debug(3, " next = %d line = %d angle = %f", next,
  473. Node->lines[next], Node->angles[next]);
  474. if (Node->angles[next] == -9.) { /* skip points and degenerated */
  475. G_debug(3, " point/degenerated -> skip");
  476. if (Node->lines[next] == current_line)
  477. break; /* Yes, that may happen if input line is degenerated and isolated and this breaks loop */
  478. else
  479. continue;
  480. }
  481. line = abs(Node->lines[next]);
  482. Line = plus->Line[line];
  483. if (Line->type & type) { /* line found */
  484. G_debug(3, " this one");
  485. return (Node->lines[next]);
  486. }
  487. /* input line reached, this must be last, because current_line may be correct return value (dangle) */
  488. if (Node->lines[next] == current_line)
  489. break;
  490. }
  491. G_debug(3, " Line NOT found at node %d", (int)node);
  492. return 0;
  493. }
  494. /*!
  495. * \brief Checks if angles of adjacent lines differ.
  496. *
  497. * Negative line number for end point. Assume that lines are sorted
  498. * in increasing angle order and angles of points and degenerated
  499. * lines are set to 9 (ignored).
  500. *
  501. * \param[in] plus pointer to Plus_head structure
  502. * \param[in] line current line id, negative if request for node 2
  503. * \param[in] type line type (GV_LINE, GV_BOUNDARY or both)
  504. *
  505. * \return 1 angles differ
  506. * \return 0 angle of a line up or down is identical
  507. */
  508. int dig_node_angle_check(struct Plus_head *plus, plus_t line, int type)
  509. {
  510. int next, prev;
  511. float angle1, angle2;
  512. plus_t node;
  513. struct P_line *Line;
  514. G_debug(3, "dig_node_angle_check: line = %d, type = %d", line, type);
  515. Line = plus->Line[abs(line)];
  516. if (line > 0)
  517. node = Line->N1;
  518. else
  519. node = Line->N2;
  520. angle1 = dig_node_line_angle(plus, node, line);
  521. /* Next */
  522. next = dig_angle_next_line(plus, line, GV_RIGHT, type);
  523. angle2 = dig_node_line_angle(plus, node, next);
  524. if (angle1 == angle2) {
  525. G_debug(3,
  526. " The line to the right has the same angle: node = %d, line = %d",
  527. node, next);
  528. return 0;
  529. }
  530. /* Previous */
  531. prev = dig_angle_next_line(plus, line, GV_LEFT, type);
  532. angle2 = dig_node_line_angle(plus, node, prev);
  533. if (angle1 == angle2) {
  534. G_debug(3,
  535. " The line to the left has the same angle: node = %d, line = %d",
  536. node, next);
  537. return 0;
  538. }
  539. return 1; /* OK */
  540. }
  541. /*!
  542. * \brief Allocate space for new island and create boundary info from array.
  543. *
  544. * The order of input lines is expected to be counter clockwise.
  545. * Then for each line in isle, update line (right,left) info.
  546. *
  547. * Area number the island is within is not filled.
  548. *
  549. * \param[in] plus pointer to Plus_head structure
  550. * \param[in] n_lines number of lines
  551. * \param[in] lines array of lines, negative for reverse direction
  552. *
  553. * \return number of new isle
  554. * \return -1 on error
  555. */
  556. int dig_add_isle(struct Plus_head *plus, int n_lines, plus_t * lines)
  557. {
  558. register int i;
  559. register int isle, line;
  560. struct P_isle *Isle;
  561. struct P_line *Line;
  562. struct bound_box box, abox;
  563. G_debug(3, "dig_add_isle():");
  564. /* First look if we have space in array of pointers to isles
  565. * and reallocate if necessary */
  566. if (plus->n_isles >= plus->alloc_isles) { /* array is full */
  567. if (dig_alloc_isles(plus, 1000) == -1)
  568. return -1;
  569. }
  570. /* allocate isle structure */
  571. isle = plus->n_isles + 1;
  572. Isle = dig_alloc_isle();
  573. if (Isle == NULL)
  574. return -1;
  575. if ((dig_isle_alloc_line(Isle, n_lines)) == -1)
  576. return -1;
  577. Isle->area = 0;
  578. Isle->N = 0;
  579. Isle->S = 0;
  580. Isle->E = 0;
  581. Isle->W = 0;
  582. for (i = 0; i < n_lines; i++) {
  583. line = lines[i];
  584. G_debug(3, " i = %d line = %d", i, line);
  585. Isle->lines[i] = line;
  586. Line = plus->Line[abs(line)];
  587. if (plus->do_uplist)
  588. dig_line_add_updated(plus, abs(line));
  589. if (line < 0) { /* revers direction -> isle on left */
  590. if (Line->left != 0) {
  591. G_warning(_("Line %d already has area/isle %d to left"), line,
  592. Line->left);
  593. return -1;
  594. }
  595. Line->left = -isle;
  596. }
  597. else {
  598. if (Line->right != 0) {
  599. G_warning(_("Line %d already has area/isle %d to left"), line,
  600. Line->right);
  601. return -1;
  602. }
  603. Line->right = -isle;
  604. }
  605. dig_line_get_box(plus, abs(line), &box);
  606. if (i == 0)
  607. dig_box_copy(&abox, &box);
  608. else
  609. dig_box_extend(&abox, &box);
  610. }
  611. Isle->n_lines = n_lines;
  612. plus->Isle[isle] = Isle;
  613. dig_isle_set_box(plus, isle, &abox);
  614. dig_spidx_add_isle(plus, isle, &abox);
  615. plus->n_isles++;
  616. return (isle);
  617. }
  618. /*!
  619. * \brief Set isle bounding box
  620. *
  621. * \param[in] plus pointer to Plus_head structure
  622. * \param[in] isle isle id
  623. * \param[in] Box bounding box
  624. *
  625. * \return 1
  626. */
  627. int dig_isle_set_box(struct Plus_head *plus, plus_t isle, struct bound_box * Box)
  628. {
  629. struct P_isle *Isle;
  630. Isle = plus->Isle[isle];
  631. Isle->N = Box->N;
  632. Isle->S = Box->S;
  633. Isle->E = Box->E;
  634. Isle->W = Box->W;
  635. Isle->T = Box->T;
  636. Isle->B = Box->B;
  637. return (1);
  638. }
  639. /*!
  640. * \brief Delete island from Plus_head structure
  641. *
  642. * Reset references to it in lines and area outside.
  643. *
  644. * \param[in] plus pointer to Plus_head structure
  645. * \param[in] isle isle id
  646. *
  647. * \return 1
  648. */
  649. int dig_del_isle(struct Plus_head *plus, int isle)
  650. {
  651. int i, line;
  652. struct P_line *Line;
  653. struct P_isle *Isle;
  654. G_debug(3, "dig_del_isle() isle = %d", isle);
  655. Isle = plus->Isle[isle];
  656. dig_spidx_del_isle(plus, isle);
  657. /* Set area for all lines to 0 */
  658. for (i = 0; i < Isle->n_lines; i++) {
  659. line = Isle->lines[i]; /* >0 = clockwise -> right, <0 = counterclockwise ->left */
  660. Line = plus->Line[abs(line)];
  661. if (plus->do_uplist)
  662. dig_line_add_updated(plus, abs(line));
  663. if (line > 0)
  664. Line->right = 0;
  665. else
  666. Line->left = 0;
  667. }
  668. /* Delete reference from area it is within */
  669. G_debug(3, " area outside isle = %d", Isle->area);
  670. if (Isle->area > 0) {
  671. if (plus->Area[Isle->area] == NULL) {
  672. G_fatal_error(_("Attempt to delete isle %d info from dead area %d"),
  673. isle, Isle->area);
  674. }
  675. else {
  676. dig_area_del_isle(plus, Isle->area, isle);
  677. }
  678. }
  679. /* free structures */
  680. dig_free_isle(Isle);
  681. plus->Isle[isle] = NULL;
  682. return 1;
  683. }