GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
dgraph.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/dgraph.c
3
4 \brief Vector library - intersection (lower level functions)
5
6 Higher level functions for reading/writing/manipulating vectors.
7
8 SPDX-FileCopyrightText: 2008-2009 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Rewritten by Rosen Matev (Google Summer of Code 2008)
12 */
13
14#include <stdlib.h>
15#include <string.h>
16#include <math.h>
17#include <grass/gis.h>
18#include <grass/vector.h>
19#include <grass/glocale.h>
20#include "dgraph.h"
21#include "e_intersect.h"
22
23#define LENGTH(DX, DY) (sqrt((DX * DX) + (DY * DY)))
24#define PI M_PI
25
26struct intersection_point {
27 double x;
28 double y;
29 int group; /* IPs with very similar dist will be in the same group */
30};
31
32struct seg_intersection {
33 int with; /* second segment */
34 int ip; /* index of the IP */
35 double dist; /* distance from first point of first segment to intersection
36 point (IP) */
37};
38
39struct seg_intersection_list {
40 int count;
41 int allocated;
42 struct seg_intersection *a;
43};
44
45struct seg_intersections {
46 int ipcount;
47 int ipallocated;
48 struct intersection_point *ip;
49 int ilcount;
50 struct seg_intersection_list *il;
51 int group_count;
52};
53
54struct seg_intersections *create_si_struct(int segments_count)
55{
56 struct seg_intersections *si;
57
58 int i;
59
60 si = G_malloc(sizeof(struct seg_intersections));
61 si->ipcount = 0;
62 si->ipallocated = segments_count + 16;
63 si->ip = G_malloc((si->ipallocated) * sizeof(struct intersection_point));
64 si->ilcount = segments_count;
65 si->il = G_malloc(segments_count * sizeof(struct seg_intersection_list));
66 for (i = 0; i < segments_count; i++) {
67 si->il[i].count = 0;
68 si->il[i].allocated = 0;
69 si->il[i].a = NULL;
70 }
71
72 return si;
73}
74
75void destroy_si_struct(struct seg_intersections *si)
76{
77 int i;
78
79 for (i = 0; i < si->ilcount; i++)
80 G_free(si->il[i].a);
81 G_free(si->il);
82 G_free(si->ip);
83 G_free(si);
84
85 return;
86}
87
88/* internal use */
89void add_ipoint1(struct seg_intersection_list *il, int with, double dist,
90 int ip)
91{
92 struct seg_intersection *s;
93
94 if (il->count == il->allocated) {
95 il->allocated += 4;
96 il->a =
97 G_realloc(il->a, (il->allocated) * sizeof(struct seg_intersection));
98 }
99 s = &(il->a[il->count]);
100 s->with = with;
101 s->ip = ip;
102 s->dist = dist;
103 il->count++;
104
105 return;
106}
107
108/* adds intersection point to the structure */
109void add_ipoint(const struct line_pnts *Points, int first_seg, int second_seg,
110 double x, double y, struct seg_intersections *si)
111{
112 struct intersection_point *t;
113
114 int ip;
115
116 G_debug(4, "add_ipoint()");
117
118 if (si->ipcount == si->ipallocated) {
119 si->ipallocated += 16;
120 si->ip = G_realloc(si->ip, (si->ipallocated) *
121 sizeof(struct intersection_point));
122 }
123 ip = si->ipcount;
124 t = &(si->ip[ip]);
125 t->x = x;
126 t->y = y;
127 t->group = -1;
128 si->ipcount++;
129
131 LENGTH((Points->x[first_seg] - x), (Points->y[first_seg] - y)),
132 ip);
133 if (second_seg >= 0)
135 &(si->il[second_seg]), first_seg,
136 LENGTH((Points->x[second_seg] - x), (Points->y[second_seg] - y)),
137 ip);
138}
139
140void sort_intersection_list(struct seg_intersection_list *il)
141{
142 int n, i, j, min;
143 struct seg_intersection t;
144
145 G_debug(4, "sort_intersection_list()");
146
147 n = il->count;
148 G_debug(4, " n=%d", n);
149 for (i = 0; i < n - 1; i++) {
150 min = i;
151 for (j = i + 1; j < n; j++) {
152 if (il->a[j].dist < il->a[min].dist) {
153 min = j;
154 }
155 }
156 if (min != i) {
157 t = il->a[i];
158 il->a[i] = il->a[min];
159 il->a[min] = t;
160 }
161 }
162
163 return;
164}
165
166int compare(const void *a, const void *b)
167{
168 struct intersection_point *aa, *bb;
169
170 aa = *((struct intersection_point **)a);
171 bb = *((struct intersection_point **)b);
172
173 if (aa->x < bb->x)
174 return -1;
175 else if (aa->x > bb->x)
176 return 1;
177 else
178 return (aa->y < bb->y) ? -1 : ((aa->y > bb->y) ? 1 : 0);
179}
180
181/* O(Points->n_points) time */
182double get_epsilon(struct line_pnts *Points)
183{
184 int i, np;
185
186 double min, t;
187
188 double *x, *y;
189
190 np = Points->n_points;
191 x = Points->x;
192 y = Points->y;
193
194 min = MAX(fabs(x[1] - x[0]), fabs(y[1] - y[0]));
195 for (i = 1; i <= np - 2; i++) {
196 t = MAX(fabs(x[i + 1] - x[i]), fabs(y[i + 1] - y[i]));
197 if ((t > 0) && (t < min)) {
198 min = t;
199 }
200 }
201
202 /* ??? is 0.001 ok ??? */
203 return min * 0.000001;
204}
205
206/* currently O(n*n); future implementation O(nlogn) */
207struct seg_intersections *find_all_intersections(const struct line_pnts *Points)
208{
209 int i, j, np;
210 int group, t;
211 int looped;
212
213 /* double EPSILON = 0.00000001; */
214 double EPSILON = GRASS_EPSILON;
215 double *x, *y;
216 double x1, y1, x2, y2;
217 int res;
218
219 /*int res2
220 double x1_, y1_, x2_, y2_, z1_, z2_; */
221 struct seg_intersections *si;
222 struct seg_intersection_list *il;
223 struct intersection_point **sorted;
224
225 G_debug(3, "find_all_intersections()");
226
227 np = Points->n_points;
228 x = Points->x;
229 y = Points->y;
230
231 si = create_si_struct(np - 1);
232
233 looped = ((x[0] == x[np - 1]) && (y[0] == y[np - 1]));
234 G_debug(3, " looped=%d", looped);
235
236 G_debug(3, " finding intersections...");
237 for (i = 0; i < np - 1; i++) {
238 for (j = i + 1; j < np - 1; j++) {
239 G_debug(4, " checking %d-%d %d-%d", i, i + 1, j, j + 1);
240 /*res = segment_intersection_2d_e(x[i], y[i], x[i+1], y[i+1], x[j],
241 * y[j], x[j+1], y[j+1], &x1, &y1, &x2, &y2); */
242 res = segment_intersection_2d(x[i], y[i], x[i + 1], y[i + 1], x[j],
243 y[j], x[j + 1], y[j + 1], &x1, &y1,
244 &x2, &y2);
245
246 /* res2 = segment_intersection_2d_e(x[i], y[i], x[i+1],
247 y[i+1], x[j], y[j], x[j+1], y[j+1], &x1_, &y1_, &x2_, &y2_); if
248 ((res != res2) || ((res != 0) && (x1!=x1_ || y1!=y1_)) ) {
249 G_debug(1, "exact=%d orig=%d", res, res2);
250 segment_intersection_2d_test(x[i], y[i], x[i+1], y[i+1], x[j],
251 y[j], x[j+1], y[j+1], &x1, &y1, &x2, &y2);
252 }
253 */
254 G_debug(4, " intersection type = %d", res);
255 if (res == 1) {
256 add_ipoint(Points, i, j, x1, y1, si);
257 }
258 else if ((res >= 2) && (res <= 5)) {
259 add_ipoint(Points, i, j, x1, y1, si);
260 add_ipoint(Points, i, j, x2, y2, si);
261 }
262 }
263 }
264 if (!looped) {
265 /* these are not really intersection points */
266 add_ipoint(Points, 0, -1, Points->x[0], Points->y[0], si);
267 add_ipoint(Points, np - 2, -1, Points->x[np - 1], Points->y[np - 1],
268 si);
269 }
270 G_debug(3, " finding intersections...done");
271
272 G_debug(3, " postprocessing...");
273 if (si->ipallocated > si->ipcount) {
274 si->ipallocated = si->ipcount;
275 si->ip = G_realloc(si->ip,
276 (si->ipcount) * sizeof(struct intersection_point));
277 }
278 for (i = 0; i < si->ilcount; i++) {
279 il = &(si->il[i]);
280 if (il->allocated > il->count) {
281 il->allocated = il->count;
282 il->a =
283 G_realloc(il->a, (il->count) * sizeof(struct seg_intersection));
284 }
285
286 if (il->count > 0) {
288 /* is it ok to use qsort here ? */
289 }
290 }
291
292 /* si->ip will not be reallocated any more so we can use pointers */
293 sorted = G_malloc((si->ipcount) * sizeof(struct intersection_point *));
294 for (i = 0; i < si->ipcount; i++)
295 sorted[i] = &(si->ip[i]);
296
297 qsort(sorted, si->ipcount, sizeof(struct intersection_point *), compare);
298
299 /* assign groups */
300 group = 0; /* next available group number */
301 for (i = 0; i < si->ipcount; i++) {
302
303 t = group;
304 for (j = i - 1; j >= 0; j--) {
305 if (!FEQUAL(sorted[j]->x, sorted[i]->x, EPSILON))
306 /* if (!almost_equal(sorted[j]->x, sorted[i]->x, 16))
307 */
308 break;
309 if (FEQUAL(sorted[j]->y, sorted[i]->y, EPSILON)) {
310 /* if (almost_equal(sorted[j]->y, sorted[i]->y, 16))
311 * { */
312 t = sorted[j]->group;
313 break;
314 }
315 }
316 G_debug(4, " group=%d, ip=%d", t,
317 (int)(sorted[i] - &(si->ip[0])));
318 sorted[i]->group = t;
319 if (t == group)
320 group++;
321 }
322 si->group_count = group;
323
324 G_debug(3, " postprocessing...done");
325
326 /* output contents of si */
327 for (i = 0; i < si->ilcount; i++) {
328 G_debug(4, "%d-%d :", i, i + 1);
329 for (j = 0; j < si->il[i].count; j++) {
330 G_debug(4, " %d-%d, group=%d", si->il[i].a[j].with,
331 si->il[i].a[j].with + 1, si->ip[si->il[i].a[j].ip].group);
332 G_debug(4, " dist=%.18f", si->il[i].a[j].dist);
333 G_debug(4, " x=%.18f, y=%.18f",
334 si->ip[si->il[i].a[j].ip].x, si->ip[si->il[i].a[j].ip].y);
335 }
336 }
337 G_free(sorted);
338
339 return si;
340}
341
342/* create's graph with n vertices and allocates memory for e edges */
343/* trying to add more than e edges, produces fatal error */
344struct planar_graph *pg_create_struct(int n, int e)
345{
346 struct planar_graph *pg;
347
348 pg = G_malloc(sizeof(struct planar_graph));
349 pg->vcount = n;
350 pg->v = G_malloc(n * sizeof(struct pg_vertex));
351 memset(pg->v, 0, n * sizeof(struct pg_vertex));
352 pg->ecount = 0;
353 pg->eallocated = MAX(e, 0);
354 pg->e = NULL;
355 pg->e = G_malloc(e * sizeof(struct pg_edge));
356
357 return pg;
358}
359
361{
362 int i;
363
364 for (i = 0; i < pg->vcount; i++) {
365 G_free(pg->v[i].edges);
366 G_free(pg->v[i].angles);
367 }
368 G_free(pg->v);
369 G_free(pg->e);
370 G_free(pg);
371}
372
373/* v1 and v2 must be valid */
374int pg_existsedge(struct planar_graph *pg, int v1, int v2)
375{
376 struct pg_vertex *v;
377 struct pg_edge *e;
378 int i, ecount;
379
380 if (pg->v[v1].ecount <= pg->v[v2].ecount)
381 v = &(pg->v[v1]);
382 else
383 v = &(pg->v[v2]);
384
385 ecount = v->ecount;
386 for (i = 0; i < ecount; i++) {
387 e = v->edges[i];
388 if (((e->v1 == v1) && (e->v2 == v2)) ||
389 ((e->v1 == v2) && (e->v2 == v1)))
390 return 1;
391 }
392
393 return 0;
394}
395
396/* for internal use */
397void pg_addedge1(struct pg_vertex *v, struct pg_edge *e)
398{
399 if (v->ecount == v->eallocated) {
400 v->eallocated += 4;
401 v->edges =
402 G_realloc(v->edges, (v->eallocated) * sizeof(struct pg_edge *));
403 }
404 v->edges[v->ecount] = e;
405 v->ecount++;
406}
407
408void pg_addedge(struct planar_graph *pg, int v1, int v2)
409{
410 struct pg_edge *e;
411
412 G_debug(4, "pg_addedge(), v1=%d, v2=%d", v1, v2);
413
414 if ((v1 == v2) || (v1 < 0) || (v1 >= pg->vcount) || (v2 < 0) ||
415 (v2 >= pg->vcount)) {
416 G_fatal_error(" pg_addedge(), v1 and/or v2 is invalid");
417 return;
418 }
419
420 if (pg_existsedge(pg, v1, v2))
421 return;
422
423 if (pg->ecount == pg->eallocated) {
424 G_fatal_error(_("Trying to add more edges to the planar_graph "
425 "than the initial allocation size allows"));
426 }
427 e = &(pg->e[pg->ecount]);
428 e->v1 = v1;
429 e->v2 = v2;
430 e->winding_left =
431 0; /* winding is undefined if the corresponding side is not visited */
432 e->winding_right = 0;
433 e->visited_left = 0;
434 e->visited_right = 0;
435 pg->ecount++;
436 pg_addedge1(&(pg->v[v1]), e);
437 pg_addedge1(&(pg->v[v2]), e);
438
439 return;
440}
441
442struct planar_graph *pg_create(const struct line_pnts *Points)
443{
444 struct seg_intersections *si;
445 struct planar_graph *pg;
446 struct intersection_point *ip;
447 struct pg_vertex *vert;
448 struct pg_edge *edge;
449
450 int i, j, t, v;
451
452 G_debug(3, "pg_create()");
453
454 si = find_all_intersections(Points);
455 pg = pg_create_struct(si->group_count, 2 * (si->ipcount));
456
457 /* set vertices info */
458 for (i = 0; i < si->ipcount; i++) {
459 ip = &(si->ip[i]);
460 t = ip->group;
461 pg->v[t].x = ip->x;
462 pg->v[t].y = ip->y;
463 }
464
465 /* add all edges */
466 for (i = 0; i < si->ilcount; i++) {
467 v = si->ip[si->il[i].a[0].ip].group;
468 for (j = 1; j < si->il[i].count; j++) {
469 t = si->ip[si->il[i].a[j].ip].group;
470 if (t != v) {
471 pg_addedge(pg, v, t); /* edge direction is v ---> t */
472 v = t;
473 }
474 }
475 }
476
477 /* precalculate angles with 0x */
478 for (i = 0; i < pg->vcount; i++) {
479 vert = &(pg->v[i]);
480 vert->angles = G_malloc((vert->ecount) * sizeof(double));
481 for (j = 0; j < vert->ecount; j++) {
482 edge = vert->edges[j];
483 t = (edge->v1 != i) ? (edge->v1) : (edge->v2);
484 vert->angles[j] = atan2(pg->v[t].y - vert->y, pg->v[t].x - vert->x);
485 }
486 }
487
489 /*
490 I'm not sure if shrinking of the allocated memory always preserves it's
491 physical place. That's why I don't want to do this: if (pg->ecount <
492 pg->eallocated) { pg->eallocated = pg->ecount; pg->e = G_realloc(pg->e,
493 (pg->ecount)*sizeof(struct pg_edge));
494 }
495 */
496
497 /* very time consuming */
498 /*
499 for (i = 0; i < pg->vcount; i++) {
500 if (pg->v[i].ecount < pg->v[i].eallocated) {
501 pg->v[i].eallocated = pg->v[i].ecount;
502 pg->v[i].edges = G_realloc(pg->v[i].edges,
503 (pg->v[i].ecount)*sizeof(struct pg_edges));
504 }
505 }
506 */
507
508 /* output pg */
509 for (i = 0; i < pg->vcount; i++) {
510 G_debug(4, " vertex %d (%g, %g)", i, pg->v[i].x, pg->v[i].y);
511 for (j = 0; j < pg->v[i].ecount; j++) {
512 G_debug(4, " edge %d-%d", pg->v[i].edges[j]->v1,
513 pg->v[i].edges[j]->v2);
514 }
515 }
516
517 return pg;
518}
#define EPSILON
#define NULL
Definition ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
#define G_realloc(p, n)
Definition defs/gis.h:138
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition defs/gis.h:136
int G_debug(int, const char *,...) __attribute__((format(printf
void add_ipoint1(struct seg_intersection_list *il, int with, double dist, int ip)
Definition dgraph.c:89
#define LENGTH(DX, DY)
Definition dgraph.c:23
struct seg_intersections * create_si_struct(int segments_count)
Definition dgraph.c:54
void add_ipoint(const struct line_pnts *Points, int first_seg, int second_seg, double x, double y, struct seg_intersections *si)
Definition dgraph.c:109
int pg_existsedge(struct planar_graph *pg, int v1, int v2)
Definition dgraph.c:374
void sort_intersection_list(struct seg_intersection_list *il)
Definition dgraph.c:140
void pg_addedge(struct planar_graph *pg, int v1, int v2)
Definition dgraph.c:408
double get_epsilon(struct line_pnts *Points)
Definition dgraph.c:182
struct planar_graph * pg_create_struct(int n, int e)
Definition dgraph.c:344
void destroy_si_struct(struct seg_intersections *si)
Definition dgraph.c:75
int compare(const void *a, const void *b)
Definition dgraph.c:166
void pg_destroy_struct(struct planar_graph *pg)
Definition dgraph.c:360
struct seg_intersections * find_all_intersections(const struct line_pnts *Points)
Definition dgraph.c:207
struct planar_graph * pg_create(const struct line_pnts *Points)
Definition dgraph.c:442
void pg_addedge1(struct pg_vertex *v, struct pg_edge *e)
Definition dgraph.c:397
#define min(x, y)
Definition draw2.c:29
int segment_intersection_2d(double ax1, double ay1, double ax2, double ay2, double bx1, double by1, double bx2, double by2, double *x1, double *y1, double *x2, double *y2)
#define FEQUAL(X, Y, TOL)
Definition e_intersect.h:5
#define GRASS_EPSILON
Definition gis.h:175
#define MAX(a, b)
Definition gis.h:145
#define _(str)
Definition glocale.h:10
int count
double b
Definition r_raster.c:37
double t
Definition r_raster.c:37
Feature geometry info - coordinates.
double * y
Array of Y coordinates.
double * x
Array of X coordinates.
int n_points
Number of points.
char winding_left
Definition dgraph.h:11
int v1
Definition dgraph.h:7
int v2
Definition dgraph.h:8
char winding_right
Definition dgraph.h:12
char visited_right
Definition dgraph.h:10
char visited_left
Definition dgraph.h:9
int ecount
Definition dgraph.h:18
struct pg_edge ** edges
Definition dgraph.h:20
int eallocated
Definition dgraph.h:19
int ecount
Definition dgraph.h:27
struct pg_edge * e
Definition dgraph.h:29
int eallocated
Definition dgraph.h:28
struct pg_vertex * v
Definition dgraph.h:26
int vcount
Definition dgraph.h:25
#define x