File: plus_node.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (230 lines) | stat: -rw-r--r-- 5,402 bytes parent folder | download | duplicates (3)
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
229
230

/**
 * \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/Vect.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 alloced to add 'line' to array
 *
 * \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,
		  struct line_pnts *points, int type)
{
    register int i, j, nlines;
    float angle;
    int ret;
    P_NODE *node;

    G_debug(3, "dig_node_add_line(): node = %d line = %d", nodeid, lineid);

    node = plus->Node[nodeid];
    nlines = node->n_lines;

    /* reallocate memory */
    ret = dig_node_alloc_line(node, 1);
    if (ret == -1)
	return -1;

    if (type & GV_LINES) {
	if (lineid < 0)
	    angle = dig_calc_end_angle(points, 0);
	else
	    angle = dig_calc_begin_angle(points, 0);
    }
    else {
	angle = -9.;
    }
    G_debug(3, "    angle = %f", angle);

    /* make sure the new angle is less than the empty space at end */
    node->angles[nlines] = 999.;

    for (i = 0; i <= nlines; i++) {	/* alloced for 1 more */
	if (angle < node->angles[i]) {
	    /* make room for insertion */
	    for (j = nlines - 1; j >= i; j--) {
		node->angles[j + 1] = node->angles[j];
		node->lines[j + 1] = node->lines[j];
	    }
	    node->angles[i] = angle;
	    node->lines[i] = lineid;
	    break;
	}
    }

    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;
    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;
    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;
    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);
}