plus_node.c 5.1 KB

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