plus_node.c 5.3 KB

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