plus_line.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /**
  2. * \file plus_line.c
  3. *
  4. * \brief Vector library - update topo for lines (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-2008
  14. */
  15. #include <sys/types.h>
  16. #include <stdlib.h>
  17. #include <grass/vector.h>
  18. static int add_line(struct Plus_head *plus, int lineid, int type, const struct line_pnts *Points,
  19. struct bound_box *box, off_t offset)
  20. {
  21. int node, lp;
  22. struct P_line *line;
  23. plus->Line[lineid] = dig_alloc_line();
  24. line = plus->Line[lineid];
  25. line->type = type;
  26. line->offset = offset;
  27. dig_spidx_add_line(plus, lineid, box);
  28. if (plus->do_uplist)
  29. dig_line_add_updated(plus, lineid);
  30. if (type & GV_POINT) {
  31. line->topo = NULL;
  32. return (lineid);
  33. }
  34. line->topo = dig_alloc_topo(type);
  35. if (type & GV_CENTROID) {
  36. struct P_topo_c *topo = (struct P_topo_c *)line->topo;
  37. topo->area = 0;
  38. return (lineid);
  39. }
  40. /* Add nodes for lines */
  41. G_debug(3, "Register node: type = %d, %f,%f", type, Points->x[0],
  42. Points->y[0]);
  43. node = dig_find_node(plus, Points->x[0], Points->y[0], Points->z[0]);
  44. G_debug(3, "node = %d", node);
  45. if (node == 0) {
  46. node = dig_add_node(plus, Points->x[0], Points->y[0], Points->z[0]);
  47. G_debug(3, "Add new node: %d", node);
  48. }
  49. else {
  50. G_debug(3, "Old node found: %d", node);
  51. }
  52. if (type == GV_LINE) {
  53. struct P_topo_l *topo = (struct P_topo_l *)line->topo;
  54. topo->N1 = node;
  55. topo->N2 = 0;
  56. }
  57. else if (type == GV_BOUNDARY) {
  58. struct P_topo_b *topo = (struct P_topo_b *)line->topo;
  59. topo->N1 = node;
  60. topo->N2 = 0;
  61. topo->left = 0;
  62. topo->right = 0;
  63. }
  64. dig_node_add_line(plus, node, lineid, Points, type);
  65. if (plus->do_uplist)
  66. dig_node_add_updated(plus, node);
  67. lp = Points->n_points - 1;
  68. G_debug(3, "Register node %f,%f", Points->x[lp], Points->y[lp]);
  69. node = dig_find_node(plus, Points->x[lp], Points->y[lp],
  70. Points->z[lp]);
  71. G_debug(3, "node = %d", node);
  72. if (node == 0) {
  73. node = dig_add_node(plus, Points->x[lp], Points->y[lp],
  74. Points->z[lp]);
  75. G_debug(3, "Add new node: %d", node);
  76. }
  77. else {
  78. G_debug(3, "Old node found: %d", node);
  79. }
  80. if (type == GV_LINE) {
  81. struct P_topo_l *topo = (struct P_topo_l *)line->topo;
  82. topo->N2 = node;
  83. }
  84. else if (type == GV_BOUNDARY) {
  85. struct P_topo_b *topo = (struct P_topo_b *)line->topo;
  86. topo->N2 = node;
  87. }
  88. dig_node_add_line(plus, node, -lineid, Points, type);
  89. if (plus->do_uplist)
  90. dig_node_add_updated(plus, node);
  91. return (lineid);
  92. }
  93. /*!
  94. * \brief Add new line to Plus_head structure.
  95. *
  96. * \param[in,out] plus pointer to Plus_head structure
  97. * \param[in] type feature type
  98. * \param[in] Points line geometry
  99. * \param[in] offset line offset
  100. *
  101. * \return -1 on error
  102. * \return line id
  103. */
  104. int
  105. dig_add_line(struct Plus_head *plus, int type, const struct line_pnts *Points,
  106. struct bound_box *box, off_t offset)
  107. {
  108. int ret;
  109. /* First look if we have space in array of pointers to lines
  110. * and reallocate if necessary */
  111. if (plus->n_lines >= plus->alloc_lines) { /* array is full */
  112. if (dig_alloc_lines(plus, 1000) == -1)
  113. return -1;
  114. }
  115. ret = add_line(plus, plus->n_lines + 1, type, Points, box, offset);
  116. if (ret == -1)
  117. return ret;
  118. plus->n_lines++;
  119. switch (type) {
  120. case GV_POINT:
  121. plus->n_plines++;
  122. break;
  123. case GV_LINE:
  124. plus->n_llines++;
  125. break;
  126. case GV_BOUNDARY:
  127. plus->n_blines++;
  128. break;
  129. case GV_CENTROID:
  130. plus->n_clines++;
  131. break;
  132. case GV_FACE:
  133. plus->n_flines++;
  134. break;
  135. case GV_KERNEL:
  136. plus->n_klines++;
  137. break;
  138. }
  139. return ret;
  140. }
  141. /*!
  142. * \brief Restore line in Plus_head structure.
  143. *
  144. * \param[in,out] plus pointer to Plus_head structure
  145. * \param[in] type feature type
  146. * \param[in] Points line geometry
  147. * \param[in] offset line offset
  148. *
  149. * \return -1 on error
  150. * \return line id
  151. */
  152. int
  153. dig_restore_line(struct Plus_head *plus, int lineid,
  154. int type, struct line_pnts *Points,
  155. struct bound_box *box, off_t offset)
  156. {
  157. if (lineid < 1 || lineid > plus->n_lines) {
  158. return -1;
  159. }
  160. return add_line(plus, lineid, type, Points, box, offset);
  161. }
  162. /*!
  163. * \brief Delete line from Plus_head structure.
  164. *
  165. * Doesn't update area/isle references (dig_del_area() or dig_del_isle()) must be
  166. * run before the line is deleted if the line is part of such
  167. * structure). Update is info about line in nodes. If this line is
  168. * last in node then node is deleted.
  169. *
  170. * \param[in,out] plus pointer to Plus_head structure
  171. * \param[in] line line id
  172. *
  173. * \return -1 on error
  174. * \return 0 OK
  175. *
  176. */
  177. int dig_del_line(struct Plus_head *plus, int line, double x, double y, double z)
  178. {
  179. int i, mv;
  180. plus_t N1 = 0, N2 = 0;
  181. struct P_line *Line;
  182. struct P_node *Node;
  183. G_debug(3, "dig_del_line() line = %d", line);
  184. Line = plus->Line[line];
  185. dig_spidx_del_line(plus, line, x, y, z);
  186. if (!(Line->type & GV_LINES)) {
  187. /* Delete line */
  188. dig_free_line(Line);
  189. plus->Line[line] = NULL;
  190. return 0;
  191. }
  192. /* Delete from nodes (and nodes) */
  193. if (Line->type == GV_LINE) {
  194. struct P_topo_l *topo = (struct P_topo_l *)Line->topo;
  195. N1 = topo->N1;
  196. }
  197. else if (Line->type == GV_BOUNDARY) {
  198. struct P_topo_b *topo = (struct P_topo_b *)Line->topo;
  199. N1 = topo->N1;
  200. }
  201. Node = plus->Node[N1];
  202. mv = 0;
  203. for (i = 0; i < Node->n_lines; i++) {
  204. if (mv) {
  205. Node->lines[i - 1] = Node->lines[i];
  206. Node->angles[i - 1] = Node->angles[i];
  207. }
  208. else {
  209. if (abs(Node->lines[i]) == line)
  210. mv = 1;
  211. }
  212. }
  213. Node->n_lines--;
  214. if (Node->n_lines == 0) {
  215. G_debug(3, " node %d has 0 lines -> delete", N1);
  216. dig_spidx_del_node(plus, N1);
  217. /* free structures */
  218. dig_free_node(Node);
  219. plus->Node[N1] = NULL;
  220. }
  221. else {
  222. if (plus->do_uplist)
  223. dig_node_add_updated(plus, N1);
  224. }
  225. if (Line->type == GV_LINE) {
  226. struct P_topo_l *topo = (struct P_topo_l *)Line->topo;
  227. N2 = topo->N2;
  228. }
  229. else if (Line->type == GV_BOUNDARY) {
  230. struct P_topo_b *topo = (struct P_topo_b *)Line->topo;
  231. N2 = topo->N2;
  232. }
  233. Node = plus->Node[N2];
  234. mv = 0;
  235. for (i = 0; i < Node->n_lines; i++) {
  236. if (mv) {
  237. Node->lines[i - 1] = Node->lines[i];
  238. Node->angles[i - 1] = Node->angles[i];
  239. }
  240. else {
  241. if (abs(Node->lines[i]) == line)
  242. mv = 1;
  243. }
  244. }
  245. Node->n_lines--;
  246. if (Node->n_lines == 0) {
  247. G_debug(3, " node %d has 0 lines -> delete", N2);
  248. dig_spidx_del_node(plus, N2);
  249. /* free structures */
  250. dig_free_node(Node);
  251. plus->Node[N2] = NULL;
  252. }
  253. else {
  254. if (plus->do_uplist)
  255. dig_node_add_updated(plus, N2);
  256. }
  257. /* Delete line */
  258. dig_free_line(Line);
  259. plus->Line[line] = NULL;
  260. return 0;
  261. }
  262. /*!
  263. * \brief Get area number on line side.
  264. *
  265. * \param[in] plus pointer Plus_head structure
  266. * \param[in] line line id
  267. * \param[in] side side id (GV_LEFT || GV_RIGHT)
  268. *
  269. * \return area number
  270. * \return 0 no area
  271. * \return -1 on error
  272. */
  273. plus_t dig_line_get_area(struct Plus_head * plus, plus_t line, int side)
  274. {
  275. struct P_line *Line;
  276. struct P_topo_b *topo;
  277. Line = plus->Line[line];
  278. if (Line->type != GV_BOUNDARY)
  279. return (-1);
  280. topo = (struct P_topo_b *)Line->topo;
  281. if (side == GV_LEFT) {
  282. G_debug(3,
  283. "dig_line_get_area(): line = %d, side = %d (left), area = %d",
  284. line, side, topo->left);
  285. return (topo->left);
  286. }
  287. if (side == GV_RIGHT) {
  288. G_debug(3,
  289. "dig_line_get_area(): line = %d, side = %d (right), area = %d",
  290. line, side, topo->right);
  291. return (topo->right);
  292. }
  293. return (-1);
  294. }
  295. /*!
  296. * \brief Set area number on line side
  297. *
  298. * \param[in] plus pointer Plus_head structure
  299. * \param[in] line line id
  300. * \param[in] side side id (GV_LEFT || GV_RIGHT)
  301. * \param[in] area area id
  302. *
  303. * \return 1
  304. */
  305. int
  306. dig_line_set_area(struct Plus_head *plus, plus_t line, int side, plus_t area)
  307. {
  308. struct P_line *Line;
  309. struct P_topo_b *topo;
  310. Line = plus->Line[line];
  311. if (Line->type != GV_BOUNDARY)
  312. return (0);
  313. topo = (struct P_topo_b *)Line->topo;
  314. if (side == GV_LEFT) {
  315. topo->left = area;
  316. }
  317. else if (side == GV_RIGHT) {
  318. topo->right = area;
  319. }
  320. return (1);
  321. }
  322. /*!
  323. * \brief Set line bounding box
  324. *
  325. * \param[in] plus pointer Plus_head structure
  326. * \param[in] line line id
  327. * \param[in] Box bounding box
  328. *
  329. * \return 1
  330. */
  331. /*
  332. int dig_line_set_box(struct Plus_head *plus, plus_t line, struct bound_box * Box)
  333. {
  334. struct P_line *Line;
  335. Line = plus->Line[line];
  336. Line->N = Box->N;
  337. Line->S = Box->S;
  338. Line->E = Box->E;
  339. Line->W = Box->W;
  340. Line->T = Box->T;
  341. Line->B = Box->B;
  342. return (1);
  343. }
  344. */
  345. /*!
  346. * \brief Get line bounding box saved in topo
  347. *
  348. * \param[in] plus pointer Plus_head structure
  349. * \param[in] line line id
  350. * \param[in,out] Box bounding box
  351. *
  352. * \return 1
  353. */
  354. /*
  355. int dig_line_get_box(struct Plus_head *plus, plus_t line, struct bound_box * Box)
  356. {
  357. struct P_line *Line;
  358. Line = plus->Line[line];
  359. Box->N = Line->N;
  360. Box->S = Line->S;
  361. Box->E = Line->E;
  362. Box->W = Line->W;
  363. Box->T = Line->T;
  364. Box->B = Line->B;
  365. return (1);
  366. }
  367. */