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