plus_area.c 20 KB

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