plus_area.c 19 KB

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