GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71924
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  (C) 2007-2008 by the GRASS Development Team
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 Martin Landa <landa.martin gmail.com>
12 */
13 
14 #include <math.h>
15 #include <grass/vedit.h>
16 
17 static int connect_lines(struct Map_info *, int, int, int,
18  double, struct ilist *);
19 
20 /*!
21  \brief Split selected lines on given position
22 
23  \param Map pointer to Map_info
24  \param List list of selected lines
25  \param coord points location
26  \param thresh threshold
27  \param[out] List_updated list of rewritten features (or NULL)
28 
29  \return number of modified lines
30  \return -1 on error
31 */
32 int Vedit_split_lines(struct Map_info *Map, struct ilist *List,
33  struct line_pnts *coord, double thresh,
34  struct ilist *List_updated)
35 {
36  int i, j, l;
37  int type, line, seg, newline;
38  int nlines_modified;
39  double px, py, spdist, lpdist, dist;
40  double *x, *y, *z;
41 
42  struct line_pnts *Points, *Points2;
43  struct line_cats *Cats;
44  struct ilist *List_in_box;
45 
46  nlines_modified = 0;
47 
48  Points = Vect_new_line_struct();
49  Points2 = Vect_new_line_struct();
50  Cats = Vect_new_cats_struct();
51  List_in_box = Vect_new_list();
52 
53  for (i = 0; i < List->n_values; i++) {
54  line = List->value[i];
55 
56  if (!Vect_line_alive(Map, line))
57  continue;
58 
59  type = Vect_read_line(Map, Points, Cats, line);
60 
61  if (!(type & GV_LINES))
62  continue;
63 
64  x = Points->x;
65  y = Points->y;
66  z = Points->z;
67 
68  for (j = 0; j < coord->n_points; j++) {
69  seg =
70  Vect_line_distance(Points, coord->x[j], coord->y[j],
71  coord->z[j], WITHOUT_Z, &px, &py, NULL,
72  &dist, &spdist, &lpdist);
73 
74  if (dist > thresh) {
75  continue;
76  }
77 
78  G_debug(3, "Vedit_split_lines(): line=%d, x=%f, y=%f, px=%f, py=%f, seg=%d, "
79  "dist=%f, spdist=%f, lpdist=%f", line, coord->x[j],
80  coord->y[j], px, py, seg, dist, spdist, lpdist);
81 
82  if (spdist <= 0.0 || spdist >= Vect_line_length(Points))
83  continue;
84 
85  G_debug(3, "Vedit_split_lines(): line=%d", line);
86 
87  /* copy first line part */
88  Vect_reset_line(Points2);
89  for (l = 0; l < seg; l++) {
90  Vect_append_point(Points2, x[l], y[l], z[l]);
91  }
92 
93  /* add last vertex */
94  Vect_append_point(Points2, px, py, 0.0);
95 
96  /* rewrite the line */
97  if (j == 0)
98  newline = Vect_rewrite_line(Map, line, type, Points2, Cats);
99  else
100  newline = Vect_write_line(Map, type, Points2, Cats);
101  if (newline < 0) {
102  return -1;
103  }
104  if (List_updated)
105  Vect_list_append(List_updated, newline);
106  Vect_reset_line(Points2);
107 
108  /* add given vertex */
109  Vect_append_point(Points2, px, py, 0.0);
110 
111  /* copy second line part */
112  for (l = seg; l < Points->n_points; l++) {
113  Vect_append_point(Points2, x[l], y[l], z[l]);
114  }
115 
116  /* rewrite the line */
117  newline = Vect_write_line(Map, type, Points2, Cats);
118  if (newline < 0) {
119  return -1;
120  }
121  if (List_updated)
122  Vect_list_append(List_updated, newline);
123 
124  nlines_modified++;
125  } /* for each bounding box */
126  } /* for each selected line */
127 
128  Vect_destroy_line_struct(Points);
129  Vect_destroy_line_struct(Points2);
131  Vect_destroy_list(List_in_box);
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  */
156 int Vedit_connect_lines(struct Map_info *Map, struct ilist *List,
157  double thresh)
158 {
159  int nlines_modified, connected;
160  int i, j, node[2], n_nodes;
161  int line, found;
162  double x, y, z;
163 
164  struct ilist *List_exclude, *List_found;
165 
166  nlines_modified = 0;
167 
168  List_exclude = Vect_new_list();
169  List_found = Vect_new_list();
170 
171  n_nodes = 2;
172 
173  /* collect lines to be modified */
174  for (i = 0; i < List->n_values; i++) {
175  line = List->value[i];
176 
177  if (!Vect_line_alive(Map, line))
178  continue;
179 
180  if (Vect_get_line_type(Map, line) & GV_POINTS)
181  continue;
182 
183  node[0] = node[1] = -1;
184  Vect_get_line_nodes(Map, line, &(node[0]), &(node[1]));
185  if (node[0] < 0 || node[1] < 0)
186  continue;
187 
188  connected = 0;
189  Vect_reset_list(List_exclude);
190  Vect_list_append(List_exclude, line);
191  for (j = 0; j < n_nodes && !connected; j++) {
192  /* for each line node find lines in threshold */
193  Vect_get_node_coor(Map, node[j], &x, &y, &z);
194 
195  do {
196  /* find first nearest line */
197  found = Vect_find_line_list(Map, x, y, z,
198  GV_LINES, thresh, WITHOUT_Z,
199  List_exclude, List_found);
200 
201  if (found > 0 && Vect_line_alive(Map, found)) {
202  /* try to connect lines (given node) */
203  G_debug(3, "Vedit_connect_lines(): lines=%d,%d", line, found);
204  if (connect_lines(Map, !j, line, found, thresh, List)) {
205  G_debug(3, "Vedit_connect_lines(): lines=%d,%d -> connected",
206  line, found);
207  nlines_modified += 2;
208  connected = 1;
209  }
210  }
211 
212  Vect_list_append(List_exclude, found);
213  } while(List_found->n_values > 0 && !connected);
214  }
215  }
216 
217  Vect_destroy_list(List_exclude);
218  Vect_destroy_list(List_found);
219 
220  return nlines_modified;
221 }
222 
223 int connect_lines(struct Map_info *Map, int first, int line_from, int line_to,
224  double thresh, struct ilist *List)
225 {
226  int line_new;
227  int type_from, type_to;
228  int n_points, seg, is;
229  double x, y, px, py, x1, y1;
230  double dist, spdist, lpdist, length, dist_p;
231  double angle_t, angle_f, angle;
232 
233  struct line_pnts *Points_from, *Points_to, *Points_final;
234  struct line_cats *Cats_from, *Cats_to;
235 
236  Points_from = Vect_new_line_struct();
237  Points_to = Vect_new_line_struct();
238  Points_final = Vect_new_line_struct();
239  Cats_from = Vect_new_cats_struct();
240  Cats_to = Vect_new_cats_struct();
241 
242  type_from = Vect_read_line(Map, Points_from, Cats_from, line_from);
243  type_to = Vect_read_line(Map, Points_to, Cats_to, line_to);
244 
245  line_new = 0;
246  if (!(type_from & GV_LINES) || !(type_to & GV_LINES))
247  line_new = -1;
248 
249  if (line_new > -1) {
250  n_points = Points_from->n_points - 1;
251 
252  if (first) {
253  x = Points_from->x[0];
254  y = Points_from->y[0];
255  }
256  else {
257  x = Points_from->x[n_points];
258  y = Points_from->y[n_points];
259  }
260  seg = Vect_line_distance(Points_to, x, y, 0.0, WITHOUT_Z,
261  &px, &py, NULL, &dist, &spdist, &lpdist);
262 
263  if (seg > 0 && dist > 0.0 && (thresh < 0. || dist <= thresh)) {
264  /* lines in threshold */
265  if (first)
266  length = 0;
267  else
268  length = Vect_line_length(Points_from);
269 
270  if (Vect_point_on_line(Points_from, length,
271  NULL, NULL, NULL, &angle_f, NULL) > 0) {
272  if (Vect_point_on_line(Points_to, lpdist,
273  NULL, NULL, NULL, &angle_t,
274  NULL) > 0) {
275  angle = angle_t - angle_f;
276  dist_p = fabs(dist / sin(angle));
277 
278  if (first) {
279  if (angle_f < 0)
280  angle_f -= M_PI;
281  else
282  angle_f += M_PI;
283  }
284 
285  x1 = x + dist_p * cos(angle_f);
286  y1 = y + dist_p * sin(angle_f);
287 
288  length = Vect_line_length(Points_to);
289  Vect_line_insert_point(Points_to, seg, x1, y1, 0.);
290  if (fabs(Vect_line_length(Points_to) - length) < length * 1e-3) {
291  /* lines connected -> split line_to */
292  /* update line_from */
293  if (first) {
294  Points_from->x[0] = x1;
295  Points_from->y[0] = y1;
296  }
297  else {
298  Points_from->x[n_points] = x1;
299  Points_from->y[n_points] = y1;
300  }
301 
302  line_new = Vect_rewrite_line(Map, line_from, type_from,
303  Points_from, Cats_from);
304  /* Vect_list_append(List, line_new); */
305 
306  /* update line_to -- first part */
307  Vect_reset_line(Points_final);
308  for (is = 0; is < seg; is++) {
309  Vect_append_point(Points_final, Points_to->x[is],
310  Points_to->y[is],
311  Points_to->z[is]);
312  }
313  Vect_append_point(Points_final, x1, y1, 0.0);
314  line_new = Vect_rewrite_line(Map, line_to, type_to,
315  Points_final, Cats_to);
316  /* Vect_list_append(List, line_new); */
317 
318  /* write second part */
319  Vect_reset_line(Points_final);
320  Vect_append_point(Points_final, x1, y1, 0.0);
321  for (is = seg; is < Points_to->n_points; is++) {
322  Vect_append_point(Points_final, Points_to->x[is],
323  Points_to->y[is],
324  Points_to->z[is]);
325  }
326 
327  /* rewrite first part */
328  line_new = Vect_write_line(Map, type_to,
329  Points_final, Cats_to);
330  /* Vect_list_append(List, line_new); */
331  }
332  }
333  }
334  }
335  }
336 
337  Vect_destroy_line_struct(Points_from);
338  Vect_destroy_line_struct(Points_to);
339  Vect_destroy_line_struct(Points_final);
340  Vect_destroy_cats_struct(Cats_from);
341  Vect_destroy_cats_struct(Cats_to);
342 
343  return line_new > 0 ? 1 : 0;
344 }
void Vect_destroy_list(struct ilist *list)
Frees all memory associated with a struct ilist, including the struct itself.
int Vect_get_node_coor(const struct Map_info *Map, int num, double *x, double *y, double *z)
Get node coordinates.
Definition: level_two.c:278
off_t Vect_rewrite_line(struct Map_info *Map, off_t line, int type, const struct line_pnts *points, const struct line_cats *cats)
Rewrites existing feature (topological level required)
struct line_pnts * Vect_new_line_struct()
Creates and initializes a line_pnts structure.
Definition: line.c:45
off_t Vect_write_line(struct Map_info *Map, int type, const struct line_pnts *points, const struct line_cats *cats)
Writes a new feature.
struct ilist * Vect_new_list(void)
Creates and initializes a struct ilist.
int n_points
Number of points.
Definition: dig_structs.h:1692
int Vect_line_alive(const struct Map_info *Map, int line)
Check if feature is alive or dead (topological level required)
int n_values
Number of values in the list.
Definition: gis.h:659
#define GV_POINTS
Definition: dig_defines.h:191
void Vect_destroy_line_struct(struct line_pnts *p)
Frees all memory associated with a line_pnts structure, including the structure itself.
Definition: line.c:77
void Vect_destroy_cats_struct(struct line_cats *p)
Frees all memory associated with line_cats structure, including the struct itself.
#define M_PI
Definition: gis.h:115
#define NULL
Definition: ccmath.h:32
#define x
double Vect_line_length(const struct line_pnts *Points)
Calculate line length, 3D-length in case of 3D vector line.
Definition: line.c:576
int Vect_append_point(struct line_pnts *Points, double x, double y, double z)
Appends one point to the end of a line.
Definition: line.c:149
Feature category info.
Definition: dig_structs.h:1702
double * x
Array of X coordinates.
Definition: dig_structs.h:1680
double l
Definition: r_raster.c:39
Feature geometry info - coordinates.
Definition: dig_structs.h:1675
int Vect_line_insert_point(struct line_pnts *Points, int index, double x, double y, double z)
Insert new point at index position and move all old points at that position and above up...
Definition: line.c:177
int Vect_find_line_list(struct Map_info *map, double ux, double uy, double uz, int type, double maxdist, int with_z, const struct ilist *exclude, struct ilist *found)
Find the nearest line(s).
int Vect_line_distance(const struct line_pnts *points, double ux, double uy, double uz, int with_z, double *px, double *py, double *pz, double *dist, double *spdist, double *lpdist)
Calculate distance of point to line.
Definition: line.c:651
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
int Vect_reset_list(struct ilist *list)
Reset ilist structure.
int Vect_list_append(struct ilist *list, int val)
Append new item to the end of list if not yet present.
int Vect_get_line_type(const struct Map_info *Map, int line)
Get line type.
Definition: level_two.c:258
#define WITHOUT_Z
2D/3D vector data
Definition: dig_defines.h:170
Vector map info.
Definition: dig_structs.h:1259
double * y
Array of Y coordinates.
Definition: dig_structs.h:1684
int Vect_get_line_nodes(const struct Map_info *Map, int line, int *n1, int *n2)
Get line nodes.
Definition: level_two.c:307
struct line_cats * Vect_new_cats_struct()
Creates and initializes line_cats structure.
int Vedit_connect_lines(struct Map_info *Map, struct ilist *List, double thresh)
Connect lines in given threshold.
Definition: break.c:156
double * z
Array of Z coordinates.
Definition: dig_structs.h:1688
List of integers.
Definition: gis.h:650
int Vect_read_line(const struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Read vector feature (topological level required)
int Vect_point_on_line(const struct line_pnts *Points, double distance, double *x, double *y, double *z, double *angle, double *slope)
Find point on line in the specified distance.
Definition: line.c:415
void Vect_reset_line(struct line_pnts *Points)
Reset line.
Definition: line.c:130
int * value
Array of values.
Definition: gis.h:655
#define GV_LINES
Definition: dig_defines.h:192
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:32