plus_line.c 9.7 KB

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