GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
break.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/vedit/break.c
3
4 \brief Vedit library - split, break, connect lines
5
6 SPDX-FileCopyrightText: 2007-2008 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Martin Landa <landa.martin gmail.com>
10 */
11
12#include <math.h>
13#include <grass/vedit.h>
14
15static int connect_lines(struct Map_info *, int, int, int, double,
16 struct ilist *);
17
18/*!
19 \brief Split selected lines on given position
20
21 \param Map pointer to Map_info
22 \param List list of selected lines
23 \param coord points location
24 \param thresh threshold
25 \param[out] List_updated list of rewritten features (or NULL)
26
27 \return number of modified lines
28 \return -1 on error
29 */
31 struct line_pnts *coord, double thresh,
32 struct ilist *List_updated)
33{
34 int i, j, l;
35 int type, line, seg, newline;
37 double px, py, spdist, lpdist, dist;
38 double *x, *y, *z;
39
40 struct line_pnts *Points, *Points2;
41 struct line_cats *Cats;
42
44
45 Points = Vect_new_line_struct();
48
49 for (i = 0; i < List->n_values; i++) {
50 line = List->value[i];
51
52 if (!Vect_line_alive(Map, line))
53 continue;
54
55 type = Vect_read_line(Map, Points, Cats, line);
56
57 if (!(type & GV_LINES))
58 continue;
59
60 x = Points->x;
61 y = Points->y;
62 z = Points->z;
63
64 for (j = 0; j < coord->n_points; j++) {
65 seg = Vect_line_distance(Points, coord->x[j], coord->y[j],
66 coord->z[j], WITHOUT_Z, &px, &py, NULL,
67 &dist, &spdist, &lpdist);
68
69 if (dist > thresh) {
70 continue;
71 }
72
73 G_debug(3,
74 "Vedit_split_lines(): line=%d, x=%f, y=%f, px=%f, py=%f, "
75 "seg=%d, "
76 "dist=%f, spdist=%f, lpdist=%f",
77 line, coord->x[j], coord->y[j], px, py, seg, dist, spdist,
78 lpdist);
79
81 continue;
82
83 G_debug(3, "Vedit_split_lines(): line=%d", line);
84
85 /* copy first line part */
87 for (l = 0; l < seg; l++) {
88 Vect_append_point(Points2, x[l], y[l], z[l]);
89 }
90
91 /* add last vertex */
93
94 /* rewrite the line */
95 if (j == 0)
96 newline = Vect_rewrite_line(Map, line, type, Points2, Cats);
97 else
99 if (newline < 0) {
100 nlines_modified = -1;
101 goto free_exit;
102 }
103 if (List_updated)
106
107 /* add given vertex */
109
110 /* copy second line part */
111 for (l = seg; l < Points->n_points; l++) {
112 Vect_append_point(Points2, x[l], y[l], z[l]);
113 }
114
115 /* rewrite the line */
117 if (newline < 0) {
118 nlines_modified = -1;
119 goto free_exit;
120 }
121 if (List_updated)
123
125 } /* for each bounding box */
126 } /* for each selected line */
127
132
133 return nlines_modified;
134}
135
136/*!
137 \brief Connect lines in given threshold
138
139 \code
140 \ \
141 id1 \ -> \
142 \
143 id2 --------- -----+---
144 \endcode
145
146 If two lines are selected and <i>thresh</i> is -1, no limit is
147 applied.
148
149 \param Map pointer to Map_info
150 \param List list of selected lines
151 \param thresh threshold value
152
153 \return number of modified lines
154 \return -1 on error
155 */
156int Vedit_connect_lines(struct Map_info *Map, struct ilist *List, double thresh)
157{
159 int i, j, node[2], n_nodes;
160 int line, found;
161 double x, y, z;
162
163 struct ilist *List_exclude, *List_found;
164
165 nlines_modified = 0;
166
169
170 n_nodes = 2;
171
172 /* collect lines to be modified */
173 for (i = 0; i < List->n_values; i++) {
174 line = List->value[i];
175
176 if (!Vect_line_alive(Map, line))
177 continue;
178
179 if (Vect_get_line_type(Map, line) & GV_POINTS)
180 continue;
181
182 node[0] = node[1] = -1;
183 Vect_get_line_nodes(Map, line, &(node[0]), &(node[1]));
184 if (node[0] < 0 || node[1] < 0)
185 continue;
186
187 connected = 0;
190 for (j = 0; j < n_nodes && !connected; j++) {
191 /* for each line node find lines in threshold */
192 Vect_get_node_coor(Map, node[j], &x, &y, &z);
193
194 do {
195 /* find first nearest line */
196 found =
199
200 if (found > 0 && Vect_line_alive(Map, found)) {
201 /* try to connect lines (given node) */
202 G_debug(3, "Vedit_connect_lines(): lines=%d,%d", line,
203 found);
204 if (connect_lines(Map, !j, line, found, thresh, List)) {
205 G_debug(
206 3,
207 "Vedit_connect_lines(): lines=%d,%d -> connected",
208 line, found);
209 nlines_modified += 2;
210 connected = 1;
211 }
212 }
213
215 } while (List_found->n_values > 0 && !connected);
216 }
217 }
218
221
222 return nlines_modified;
223}
224
225int connect_lines(struct Map_info *Map, int first, int line_from, int line_to,
226 double thresh, struct ilist *List G_UNUSED)
227{
228 int line_new;
229 int type_from, type_to;
230 int n_points, seg, is;
231 double x, y, px, py, x1, y1;
232 double dist, spdist, lpdist, length, dist_p;
233 double angle_t, angle_f, angle;
234
236 struct line_cats *Cats_from, *Cats_to;
237
243
246
247 line_new = 0;
248 if (!(type_from & GV_LINES) || !(type_to & GV_LINES))
249 line_new = -1;
250
251 if (line_new > -1) {
252 n_points = Points_from->n_points - 1;
253
254 if (first) {
255 x = Points_from->x[0];
256 y = Points_from->y[0];
257 }
258 else {
259 x = Points_from->x[n_points];
260 y = Points_from->y[n_points];
261 }
262 seg = Vect_line_distance(Points_to, x, y, 0.0, WITHOUT_Z, &px, &py,
263 NULL, &dist, &spdist, &lpdist);
264
265 if (seg > 0 && dist > 0.0 && (thresh < 0. || dist <= thresh)) {
266 /* lines in threshold */
267 if (first)
268 length = 0;
269 else
271
273 &angle_f, NULL) > 0) {
275 &angle_t, NULL) > 0) {
277 dist_p = fabs(dist / sin(angle));
278
279 if (first) {
280 if (angle_f < 0)
281 angle_f -= M_PI;
282 else
283 angle_f += M_PI;
284 }
285
286 x1 = x + dist_p * cos(angle_f);
287 y1 = y + dist_p * sin(angle_f);
288
289 length = Vect_line_length(Points_to);
290 Vect_line_insert_point(Points_to, seg, x1, y1, 0.);
291 if (fabs(Vect_line_length(Points_to) - length) <
292 length * 1e-3) {
293 /* lines connected -> split line_to */
294 /* update line_from */
295 if (first) {
296 Points_from->x[0] = x1;
297 Points_from->y[0] = y1;
298 }
299 else {
300 Points_from->x[n_points] = x1;
301 Points_from->y[n_points] = y1;
302 }
303
306 /* Vect_list_append(List, line_new); */
307
308 /* update line_to -- first part */
310 for (is = 0; is < seg; is++) {
312 Points_to->y[is],
313 Points_to->z[is]);
314 }
315 Vect_append_point(Points_final, x1, y1, 0.0);
318 /* Vect_list_append(List, line_new); */
319
320 /* write second part */
322 Vect_append_point(Points_final, x1, y1, 0.0);
323 for (is = seg; is < Points_to->n_points; is++) {
325 Points_to->y[is],
326 Points_to->z[is]);
327 }
328
329 /* rewrite first part */
331 Cats_to);
332 /* Vect_list_append(List, line_new); */
333 }
334 }
335 }
336 }
337 }
338
344
345 return line_new > 0 ? 1 : 0;
346}
int Vedit_connect_lines(struct Map_info *Map, struct ilist *List, double thresh)
Connect lines in given threshold.
Definition break.c:156
int Vedit_split_lines(struct Map_info *Map, struct ilist *List, struct line_pnts *coord, double thresh, struct ilist *List_updated)
Split selected lines on given position.
Definition break.c:30
#define NULL
Definition ccmath.h:32
int G_debug(int, const char *,...) __attribute__((format(printf
void Vect_destroy_line_struct(struct line_pnts *)
Frees all memory associated with a line_pnts structure, including the structure itself.
Definition line.c:75
off_t Vect_rewrite_line(struct Map_info *, off_t, int, const struct line_pnts *, const struct line_cats *)
Rewrites existing feature (topological level required)
int Vect_get_line_nodes(struct Map_info *, int, int *, int *)
Get line nodes.
Definition level_two.c:302
int Vect_get_node_coor(struct Map_info *, int, double *, double *, double *)
Get node coordinates.
Definition level_two.c:272
double Vect_line_length(const struct line_pnts *)
Calculate line length, 3D-length in case of 3D vector line.
Definition line.c:573
int Vect_point_on_line(const struct line_pnts *, double, double *, double *, double *, double *, double *)
Find point on line in the specified distance.
Definition line.c:411
int Vect_get_line_type(struct Map_info *, int)
Get line type.
Definition level_two.c:252
int Vect_find_line_list(struct Map_info *, double, double, double, int, double, int, const struct ilist *, struct ilist *)
Find the nearest line(s).
void Vect_destroy_list(struct ilist *)
Frees all memory associated with a struct ilist, including the struct itself.
void Vect_destroy_cats_struct(struct line_cats *)
Frees all memory associated with line_cats structure, including the struct itself.
int Vect_list_append(struct ilist *, int)
Append new item to the end of list if not yet present.
int Vect_read_line(struct Map_info *, struct line_pnts *, struct line_cats *, int)
Read vector feature (topological level required)
int Vect_line_distance(const struct line_pnts *, double, double, double, int, double *, double *, double *, double *, double *, double *)
Calculate distance of point to line.
Definition line.c:646
int Vect_line_alive(struct Map_info *, int)
Check if feature is alive or dead (topological level required)
struct line_cats * Vect_new_cats_struct(void)
Creates and initializes line_cats structure.
struct ilist * Vect_new_list(void)
Creates and initializes a struct ilist.
int Vect_line_insert_point(struct line_pnts *, int, double, double, double)
Insert new point at index position and move all old points at that position and above up.
Definition line.c:174
off_t Vect_write_line(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes a new feature.
void Vect_reset_line(struct line_pnts *)
Reset line.
Definition line.c:127
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
Definition line.c:43
int Vect_reset_list(struct ilist *)
Reset ilist structure.
int Vect_append_point(struct line_pnts *, double, double, double)
Appends one point to the end of a line.
Definition line.c:146
#define GV_LINES
#define WITHOUT_Z
2D/3D vector data
#define GV_POINTS
#define G_UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition gis.h:43
#define M_PI
Definition gis.h:154
double l
Definition r_raster.c:37
Vector map info.
List of integers.
Definition gis.h:712
Feature category info.
Feature geometry info - coordinates.
double * y
Array of Y coordinates.
double * x
Array of X coordinates.
int n_points
Number of points.
double * z
Array of Z coordinates.
#define x