plus_line.c 9.5 KB

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