plus_area.c 20 KB

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