plus_node.c 5.3 KB

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