plus_node.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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;
  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. /* reallocate memory */
  51. ret = dig_node_alloc_line(node, 1);
  52. if (ret == -1)
  53. return -1;
  54. angle = -9.;
  55. if (type & GV_LINES) {
  56. if (lineid < 0)
  57. angle = dig_calc_end_angle(points, 0);
  58. else
  59. angle = dig_calc_begin_angle(points, 0);
  60. }
  61. G_debug(3, " angle = %f", angle);
  62. i = node->n_lines;
  63. while (i > 0) {
  64. if (angle >= node->angles[i - 1])
  65. break;
  66. node->angles[i] = node->angles[i - 1];
  67. node->lines[i] = node->lines[i - 1];
  68. i--;
  69. }
  70. node->angles[i] = angle;
  71. node->lines[i] = lineid;
  72. node->n_lines++;
  73. G_debug(3,
  74. "dig_node_add_line(): line %d added position %d n_lines: %d angle %f",
  75. lineid, i, node->n_lines, angle);
  76. return ((int)node->n_lines);
  77. }
  78. /*!
  79. * \brief Add new node to plus structure
  80. *
  81. * \param[in] plus pointer to Plus_head structure
  82. * \param[in] x,y,z coordinates
  83. *
  84. * \return -1 on error
  85. * \return number of node
  86. */
  87. int dig_add_node(struct Plus_head *plus, double x, double y, double z)
  88. {
  89. int nnum;
  90. struct P_node *node;
  91. /* First look if we have space in array of pointers to nodes
  92. * and reallocate if necessary */
  93. G_debug(3, "dig_add_node(): n_nodes = %d, alloc_nodes = %d",
  94. plus->n_nodes, plus->alloc_nodes);
  95. if (plus->n_nodes >= plus->alloc_nodes) { /* array is full */
  96. if (dig_alloc_nodes(plus, 1000) == -1)
  97. return -1;
  98. }
  99. /* allocate node structure */
  100. nnum = plus->n_nodes + 1;
  101. plus->Node[nnum] = dig_alloc_node();
  102. node = plus->Node[nnum];
  103. node->x = x;
  104. node->y = y;
  105. node->z = z;
  106. dig_spidx_add_node(plus, nnum, x, y, z);
  107. plus->n_nodes++;
  108. G_debug(3, "new node = %d, n_nodes = %d, alloc_nodes = %d", nnum,
  109. plus->n_nodes, plus->alloc_nodes);
  110. return (nnum);
  111. }
  112. /*!
  113. * \brief Return actual index into node arrays of the first set of matching coordinates
  114. *
  115. * \param[in] plus pointer to Plus_head structure
  116. * \param[in] x,y coordinates
  117. * \param[in] thresh threshold value
  118. *
  119. * \return node index
  120. * \return -1 if no node found
  121. */
  122. int dig_which_node(struct Plus_head *plus, double x, double y, double thresh)
  123. {
  124. register int i;
  125. register int first_time;
  126. register int have_match;
  127. int winner;
  128. double least_dist, dist;
  129. struct P_node *node;
  130. first_time = 1;
  131. have_match = 0;
  132. winner = 0;
  133. least_dist = 0.0;
  134. for (i = 1; i <= plus->n_nodes; i++) {
  135. if (plus->Node[i] == NULL)
  136. continue;
  137. node = plus->Node[i];
  138. if ((fabs(node->x - x) <= thresh) && (fabs(node->y - y) <= thresh)) {
  139. dist = dist_squared(x, y, node->x, node->y);
  140. if (first_time) {
  141. least_dist = dist;
  142. first_time = 0;
  143. winner = i;
  144. have_match = 1;
  145. }
  146. if (dist < least_dist) {
  147. least_dist = dist;
  148. winner = i;
  149. }
  150. }
  151. }
  152. if (!have_match)
  153. return (-1);
  154. return (winner);
  155. } /* which_node () */
  156. /*!
  157. * \brief Return line angle
  158. *
  159. * Lines is specified by line id in topology, NOT by order number.
  160. * Negative id if looking for line end point.
  161. *
  162. * \param[in] plus pointer to Plus_head structure
  163. * \param[in] nodeid node id
  164. * \param[in] lineid line id
  165. *
  166. * \return line angle <-PI,PI>
  167. * \return 0 not reached
  168. */
  169. float dig_node_line_angle(struct Plus_head *plus, int nodeid, int lineid)
  170. {
  171. int i, nlines;
  172. struct P_node *node;
  173. G_debug(3, "dig_node_line_angle: node = %d line = %d", nodeid, lineid);
  174. node = plus->Node[nodeid];
  175. nlines = node->n_lines;
  176. for (i = 0; i < nlines; i++) {
  177. if (node->lines[i] == lineid)
  178. return (node->angles[i]);
  179. }
  180. G_fatal_error(_("Attempt to read line angle for the line which is not connected to the node: "
  181. "node %d, line %d"), nodeid, lineid);
  182. return 0.0; /* not reached */
  183. }
  184. static double dist_squared(double x1, double y1, double x2, double y2)
  185. {
  186. double dx, dy;
  187. dx = x1 - x2;
  188. dy = y1 - y2;
  189. return (dx * dx + dy * dy);
  190. }