19 #include <grass/gis.h>
20 #include <grass/Vect.h>
21 #include <grass/glocale.h>
67 struct ilist *List_ref,
int type,
struct Map_info *Err)
69 struct line_pnts *APoints, *BPoints, *Points;
70 struct line_pnts **AXLines, **BXLines;
71 struct line_cats *ACats, *BCats, *Cats;
72 int j, k,
l, ret, atype, btype, aline, bline, found, iline, nlines;
73 int naxlines, nbxlines, nx;
78 int touch1_n = 0, touch1_s = 0, touch1_e = 0, touch1_w = 0;
79 int touch2_n = 0, touch2_s = 0, touch2_e = 0, touch2_w = 0;
81 int node, anode1, anode2, bnode1, bnode2;
95 nlines = List_break->n_values;
100 G_debug(3,
"nlines = %d", nlines);
115 for (iline = 0; iline < nlines; iline++) {
118 aline = List_break->value[iline];
127 G_debug(3,
"aline = %d", aline);
139 touch1_n = touch1_s = touch1_e = touch1_w = 0;
140 for (j = 1; j < APoints->n_points; j++) {
141 if (APoints->y[j] == ABox.N)
143 if (APoints->y[j] == ABox.S)
145 if (APoints->x[j] == ABox.E)
147 if (APoints->x[j] == ABox.W)
150 G_debug(3,
"touch1: n = %d s = %d e = %d w = %d", touch1_n,
151 touch1_s, touch1_e, touch1_w);
152 touch2_n = touch2_s = touch2_e = touch2_w = 0;
153 for (j = 0; j < APoints->n_points - 1; j++) {
154 if (APoints->y[j] == ABox.N)
156 if (APoints->y[j] == ABox.S)
158 if (APoints->x[j] == ABox.E)
160 if (APoints->x[j] == ABox.W)
163 G_debug(3,
"touch2: n = %d s = %d e = %d w = %d", touch2_n,
164 touch2_s, touch2_e, touch2_w);
168 G_debug(3,
" %d lines selected by box", List->n_values);
170 for (j = 0; j < List->n_values; j++) {
171 bline = List->value[j];
175 G_debug(3,
" j = %d bline = %d", j, bline);
183 if (anode1 == bnode1 || anode1 == bnode2)
185 else if (anode2 == bnode1 || anode2 == bnode2)
192 if ((node == anode1 && nodey == ABox.N && !touch1_n &&
193 nodey == BBox.S) || (node == anode2 &&
194 nodey == ABox.N && !touch2_n &&
196 (node == anode1 && nodey == ABox.S && !touch1_s &&
197 nodey == BBox.N) || (node == anode2 &&
198 nodey == ABox.S && !touch2_s &&
200 (node == anode1 && nodex == ABox.E && !touch1_e &&
201 nodex == BBox.W) || (node == anode2 &&
202 nodex == ABox.E && !touch2_e &&
204 (node == anode1 && nodex == ABox.W && !touch1_w &&
205 nodex == BBox.E) || (node == anode2 &&
206 nodex == ABox.W && !touch2_w &&
209 "lines %d and %d touching by end nodes only -> no intersection",
221 &naxlines, &nbxlines, 0);
222 G_debug(3,
" naxlines = %d nbxlines = %d", naxlines, nbxlines);
227 if (aline == bline && naxlines == 0 && nbxlines == 0 &&
228 APoints->n_points >= 3) {
232 G_debug(3,
" Check collapsed loop");
233 if (APoints->n_points % 2) {
234 centre = APoints->n_points / 2;
235 if (APoints->x[centre - 1] == APoints->x[centre + 1] && APoints->y[centre - 1] == APoints->y[centre + 1] && APoints->z[centre - 1] == APoints->z[centre + 1]) {
237 (
struct line_pnts **)G_malloc(2 *
244 for (i = 0; i <= centre; i++)
246 APoints->y[i], APoints->z[i]);
248 for (i = centre; i < APoints->n_points; i++)
250 APoints->y[i], APoints->z[i]);
259 xx = (
double *)G_malloc((naxlines + nbxlines) *
261 yx = (
double *)G_malloc((naxlines + nbxlines) *
263 zx = (
double *)G_malloc((naxlines + nbxlines) *
269 for (k = 0; k < naxlines; k++) {
273 if ((atype & GV_POINTS) || AXLines[k]->n_points > 1) {
278 G_debug(3,
"Line %d written, npoints = %d", ret,
279 AXLines[k]->n_points);
288 xx[nx] = AXLines[k]->x[0];
289 yx[nx] = AXLines[k]->y[0];
290 zx[nx] = AXLines[k]->z[0];
296 nbreaks += naxlines - 1;
302 if (aline != bline) {
304 for (k = 0; k < nbxlines; k++) {
308 if ((btype & GV_POINTS) || BXLines[k]->n_points > 1) {
312 G_debug(5,
"Line %d written", ret);
322 for (l = 0; l < nx; l++) {
323 if (xx[l] == BXLines[k]->x[0] &&
324 yx[l] == BXLines[k]->
y[0] &&
325 zx[l] == BXLines[k]->z[0]) {
331 xx[nx] = BXLines[k]->x[0];
332 yx[nx] = BXLines[k]->y[0];
333 zx[nx] = BXLines[k]->z[0];
340 nbreaks += nbxlines - 1;
343 for (k = 0; k < nbxlines; k++)
350 for (l = 0; l < nx; l++) {
365 nlines = List_break->n_values;
370 G_debug(3,
"nlines = %d", nlines);
int Vect_destroy_list(struct ilist *list)
Frees all memory associated with a struct ilist, including the struct itself.
void G_free(void *buf)
Free allocated memory.
int Vect_get_line_nodes(struct Map_info *Map, int line, int *n1, int *n2)
Get line nodes.
struct line_pnts * Vect_new_line_struct()
Creates and initializes a struct line_pnts.
struct ilist * Vect_new_list(void)
Creates and initializes a struct ilist.
int Vect_reset_line(struct line_pnts *Points)
Reset line.
int Vect_append_point(struct line_pnts *Points, double x, double y, double z)
Appends one point to the end of a line.
int G_percent(long n, long d, int s)
Print percent complete messages.
void G_verbose_message(const char *msg,...)
Print a message to stderr but only if module is in verbose mode.
int Vect_list_append(struct ilist *list, int val)
Append new item to the end of list if not yet present.
int Vect_break_lines_list(struct Map_info *Map, struct ilist *List_break, struct ilist *List_ref, int type, struct Map_info *Err)
Break selected lines in vector map at each intersection.
int Vect_line_alive(struct Map_info *Map, int line)
Check if feature is alive or dead.
int Vect_select_lines_by_box(struct Map_info *Map, BOUND_BOX *Box, int type, struct ilist *list)
Select lines by box.
struct line_cats * Vect_new_cats_struct()
Creates and initializes line_cats structure.
int Vect_line_prune(struct line_pnts *Points)
Remove duplicate points, i.e. zero length segments.
int Vect_get_num_lines(struct Map_info *map)
Fetch number of features (points, lines, boundaries, centroids) in vector map.
int G_debug(int level, const char *msg,...)
Print debugging message.
int Vect_line_intersection(struct line_pnts *APoints, struct line_pnts *BPoints, struct line_pnts ***ALines, struct line_pnts ***BLines, int *nalines, int *nblines, int with_z)
Intersect 2 lines.
int Vect_delete_line(struct Map_info *Map, int line)
Delete feature.
int Vect_get_line_box(struct Map_info *Map, int line, BOUND_BOX *Box)
Get boundary box of line.
long Vect_write_line(struct Map_info *Map, int type, struct line_pnts *points, struct line_cats *cats)
Writes new feature to the end of file (table)
int Vect_val_in_list(struct ilist *list, int val)
Find a given item in the list.
int Vect_get_node_coor(struct Map_info *map, int num, double *x, double *y, double *z)
Get node coordinates.
void Vect_break_lines(struct Map_info *Map, int type, struct Map_info *Err)
Break lines in vector map at each intersection.
int Vect_destroy_line_struct(struct line_pnts *p)
Frees all memory associated with a struct line_pnts, including the struct itself. ...
int Vect_read_line(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Read vector feature.