plus_node.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * \file plus_node.c
  3. *
  4. * \brief Vector library - update topo for nodes (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 <math.h>
  17. #include <grass/vector.h>
  18. #include <grass/glocale.h>
  19. static double dist_squared(double, double, double, double);
  20. /*!
  21. * \brief Add line info to node
  22. *
  23. * Line will be negative if END node
  24. *
  25. * 'node' must of course already exist space will be alloced to add 'line' to array
  26. *
  27. * Lines are sorted in increasing angle order and
  28. * degenerated lines are set to -9 (ignored).
  29. *
  30. * \param[in] plus pointer to Plus_head structure
  31. * \param[in] nodeid node id
  32. * \param[in] lineid line id
  33. * \param[in] points line geometry
  34. * \param[in] type line type
  35. *
  36. * \return -1 on error
  37. * \return 0 line not added (degenerate)
  38. * \return new number of lines in node
  39. */
  40. int
  41. dig_node_add_line(struct Plus_head *plus, int nodeid, int lineid,
  42. const struct line_pnts *points, int type)
  43. {
  44. register int i, j, nlines;
  45. float angle;
  46. int ret;
  47. struct P_node *node;
  48. G_debug(3, "dig_node_add_line(): node = %d line = %d", nodeid, lineid);
  49. node = plus->Node[nodeid];
  50. nlines = node->n_lines;
  51. /* reallocate memory */
  52. ret = dig_node_alloc_line(node, 1);
  53. if (ret == -1)
  54. return -1;
  55. angle = -9.;
  56. if (type & GV_LINES) {
  57. if (lineid < 0)
  58. angle = dig_calc_end_angle(points, 0);
  59. else
  60. angle = dig_calc_begin_angle(points, 0);
  61. }
  62. G_debug(3, " angle = %f", angle);
  63. i = nlines;
  64. while (i > 0) {
  65. if (angle >= node->angles[i - 1])
  66. break;
  67. node->angles[i] = node->angles[i - 1];
  68. node->lines[i] = node->lines[i - 1];
  69. i--;
  70. }
  71. node->angles[i] = angle;
  72. node->lines[i] = lineid;
  73. node->n_lines++;
  74. G_debug(3,
  75. "dig_node_add_line(): line %d added position %d n_lines: %d angle %f",
  76. lineid, i, node->n_lines, angle);
  77. return ((int)node->n_lines);
  78. }
  79. /*!
  80. * \brief Add new node to plus structure
  81. *
  82. * \param[in] plus pointer to Plus_head structure
  83. * \param[in] x,y,z coordinates
  84. *
  85. * \return -1 on error
  86. * \return number of node
  87. */
  88. int dig_add_node(struct Plus_head *plus, double x, double y, double z)
  89. {
  90. int nnum;
  91. struct P_node *node;
  92. /* First look if we have space in array of pointers to nodes
  93. * and reallocate if necessary */
  94. G_debug(3, "dig_add_node(): n_nodes = %d, alloc_nodes = %d",
  95. plus->n_nodes, plus->alloc_nodes);
  96. if (plus->n_nodes >= plus->alloc_nodes) { /* array is full */
  97. if (dig_alloc_nodes(plus, 1000) == -1)
  98. return -1;
  99. }
  100. /* allocate node structure */
  101. nnum = plus->n_nodes + 1;
  102. plus->Node[nnum] = dig_alloc_node();
  103. node = plus->Node[nnum];
  104. node->x = x;
  105. node->y = y;
  106. node->z = z;
  107. dig_spidx_add_node(plus, nnum, x, y, z);
  108. plus->n_nodes++;
  109. G_debug(3, "new node = %d, n_nodes = %d, alloc_nodes = %d", nnum,
  110. plus->n_nodes, plus->alloc_nodes);
  111. return (nnum);
  112. }
  113. /*!
  114. * \brief Return actual index into node arrays of the first set of matching coordinates
  115. *
  116. * \param[in] plus pointer to Plus_head structure
  117. * \param[in] x,y coordinates
  118. * \param[in] thresh threshold value
  119. *
  120. * \return node index
  121. * \return -1 if no node found
  122. */
  123. int dig_which_node(struct Plus_head *plus, double x, double y, double thresh)
  124. {
  125. register int i;
  126. register int first_time;
  127. register int have_match;
  128. int winner;
  129. double least_dist, dist;
  130. struct P_node *node;
  131. first_time = 1;
  132. have_match = 0;
  133. winner = 0;
  134. least_dist = 0.0;
  135. for (i = 1; i <= plus->n_nodes; i++) {
  136. if (plus->Node[i] == NULL)
  137. continue;
  138. node = plus->Node[i];
  139. if ((fabs(node->x - x) <= thresh) && (fabs(node->y - y) <= thresh)) {
  140. dist = dist_squared(x, y, node->x, node->y);
  141. if (first_time) {
  142. least_dist = dist;
  143. first_time = 0;
  144. winner = i;
  145. have_match = 1;
  146. }
  147. if (dist < least_dist) {
  148. least_dist = dist;
  149. winner = i;
  150. }
  151. }
  152. }
  153. if (!have_match)
  154. return (-1);
  155. return (winner);
  156. } /* which_node () */
  157. /*!
  158. * \brief Return line angle
  159. *
  160. * Lines is specified by line id in topology, NOT by order number.
  161. * Negative id if looking for line end point.
  162. *
  163. * \param[in] plus pointer to Plus_head structure
  164. * \param[in] nodeid node id
  165. * \param[in] lineid line id
  166. *
  167. * \return line angle <-PI,PI>
  168. * \return 0 not reached
  169. */
  170. float dig_node_line_angle(struct Plus_head *plus, int nodeid, int lineid)
  171. {
  172. int i, nlines;
  173. struct P_node *node;
  174. G_debug(3, "dig_node_line_angle: node = %d line = %d", nodeid, lineid);
  175. node = plus->Node[nodeid];
  176. nlines = node->n_lines;
  177. for (i = 0; i < nlines; i++) {
  178. if (node->lines[i] == lineid)
  179. return (node->angles[i]);
  180. }
  181. G_fatal_error(_("Attempt to read line angle for the line which is not connected to the node: "
  182. "node %d, line %d"), nodeid, lineid);
  183. return 0.0; /* not reached */
  184. }
  185. static double dist_squared(double x1, double y1, double x2, double y2)
  186. {
  187. double dx, dy;
  188. dx = x1 - x2;
  189. dy = y1 - y2;
  190. return (dx * dx + dy * dy);
  191. }