25 #define LENGTH(DX, DY) (sqrt((DX * DX) + (DY * DY)))
28 struct intersection_point {
34 struct seg_intersection {
41 struct seg_intersection_list {
44 struct seg_intersection *a;
47 struct seg_intersections {
50 struct intersection_point *ip;
52 struct seg_intersection_list *il;
58 struct seg_intersections *si;
62 si =
G_malloc(
sizeof(
struct seg_intersections));
64 si->ipallocated = segments_count + 16;
65 si->ip =
G_malloc((si->ipallocated) *
sizeof(
struct intersection_point));
66 si->ilcount = segments_count;
67 si->il =
G_malloc(segments_count *
sizeof(
struct seg_intersection_list));
68 for (i = 0; i < segments_count; i++) {
70 si->il[i].allocated = 0;
81 for (i = 0; i < si->ilcount; i++)
91 void add_ipoint1(
struct seg_intersection_list *il,
int with,
double dist,
94 struct seg_intersection *s;
96 if (il->count == il->allocated) {
99 G_realloc(il->a, (il->allocated) *
sizeof(
struct seg_intersection));
101 s = &(il->a[il->count]);
112 double x,
double y,
struct seg_intersections *si)
114 struct intersection_point *
t;
120 if (si->ipcount == si->ipallocated) {
121 si->ipallocated += 16;
122 si->ip =
G_realloc(si->ip, (si->ipallocated) *
123 sizeof(
struct intersection_point));
133 LENGTH((Points->
x[first_seg] -
x), (Points->
y[first_seg] - y)),
137 &(si->il[second_seg]), first_seg,
138 LENGTH((Points->
x[second_seg] -
x), (Points->
y[second_seg] - y)),
145 struct seg_intersection
t;
147 G_debug(4,
"sort_intersection_list()");
151 for (i = 0; i < n - 1; i++) {
153 for (j = i + 1; j < n; j++) {
154 if (il->a[j].dist < il->a[
min].dist) {
160 il->a[i] = il->a[
min];
170 struct intersection_point *aa, *bb;
172 aa = *((
struct intersection_point **)a);
173 bb = *((
struct intersection_point **)
b);
177 else if (aa->x > bb->x)
180 return (aa->y < bb->y) ? -1 : ((aa->y > bb->y) ? 1 : 0);
196 min =
MAX(fabs(
x[1] -
x[0]), fabs(y[1] - y[0]));
197 for (i = 1; i <= np - 2; i++) {
198 t =
MAX(fabs(
x[i + 1] -
x[i]), fabs(y[i + 1] - y[i]));
199 if ((
t > 0) && (
t <
min)) {
205 return min * 0.000001;
218 double x1, y1, x2, y2;
223 struct seg_intersections *si;
224 struct seg_intersection_list *il;
225 struct intersection_point **sorted;
227 G_debug(3,
"find_all_intersections()");
235 looped = ((
x[0] ==
x[np - 1]) && (y[0] == y[np - 1]));
236 G_debug(3,
" looped=%d", looped);
238 G_debug(3,
" finding intersections...");
239 for (i = 0; i < np - 1; i++) {
240 for (j = i + 1; j < np - 1; j++) {
241 G_debug(4,
" checking %d-%d %d-%d", i, i + 1, j, j + 1);
245 y[j],
x[j + 1], y[j + 1], &x1, &y1,
256 G_debug(4,
" intersection type = %d", res);
260 else if ((res >= 2) && (res <= 5)) {
268 add_ipoint(Points, 0, -1, Points->
x[0], Points->
y[0], si);
269 add_ipoint(Points, np - 2, -1, Points->
x[np - 1], Points->
y[np - 1],
272 G_debug(3,
" finding intersections...done");
274 G_debug(3,
" postprocessing...");
275 if (si->ipallocated > si->ipcount) {
276 si->ipallocated = si->ipcount;
278 (si->ipcount) *
sizeof(
struct intersection_point));
280 for (i = 0; i < si->ilcount; i++) {
282 if (il->allocated > il->count) {
283 il->allocated = il->count;
285 G_realloc(il->a, (il->count) *
sizeof(
struct seg_intersection));
295 sorted =
G_malloc((si->ipcount) *
sizeof(
struct intersection_point *));
296 for (i = 0; i < si->ipcount; i++)
297 sorted[i] = &(si->ip[i]);
299 qsort(sorted, si->ipcount,
sizeof(
struct intersection_point *),
compare);
303 for (i = 0; i < si->ipcount; i++) {
306 for (j = i - 1; j >= 0; j--) {
314 t = sorted[j]->group;
319 (
int)(sorted[i] - &(si->ip[0])));
320 sorted[i]->group =
t;
324 si->group_count = group;
326 G_debug(3,
" postprocessing...done");
329 for (i = 0; i < si->ilcount; i++) {
330 G_debug(4,
"%d-%d :", i, i + 1);
331 for (j = 0; j < si->il[i].count; j++) {
332 G_debug(4,
" %d-%d, group=%d", si->il[i].a[j].with,
333 si->il[i].a[j].with + 1, si->ip[si->il[i].a[j].ip].group);
334 G_debug(4,
" dist=%.18f", si->il[i].a[j].dist);
335 G_debug(4,
" x=%.18f, y=%.18f",
336 si->ip[si->il[i].a[j].ip].x, si->ip[si->il[i].a[j].ip].y);
353 memset(pg->
v, 0, n *
sizeof(
struct pg_vertex));
366 for (i = 0; i < pg->
vcount; i++) {
388 for (i = 0; i < ecount; i++) {
390 if (((e->
v1 ==
v1) && (e->
v2 ==
v2)) ||
427 "than the initial allocation size allows"));
446 struct seg_intersections *si;
448 struct intersection_point *ip;
460 for (i = 0; i < si->ipcount; i++) {
468 for (i = 0; i < si->ilcount; i++) {
469 v = si->ip[si->il[i].a[0].ip].group;
470 for (j = 1; j < si->il[i].count; j++) {
471 t = si->ip[si->il[i].a[j].ip].group;
480 for (i = 0; i < pg->
vcount; i++) {
483 for (j = 0; j < vert->
ecount; j++) {
484 edge = vert->
edges[j];
485 t = (edge->
v1 != i) ? (edge->
v1) : (edge->
v2);
486 vert->
angles[j] = atan2(pg->
v[
t].
y - vert->
y, pg->
v[
t].
x - vert->
x);
511 for (i = 0; i < pg->
vcount; i++) {
512 G_debug(4,
" vertex %d (%g, %g)", i, pg->
v[i].
x, pg->
v[i].
y);
513 for (j = 0; j < pg->
v[i].
ecount; j++) {
void G_free(void *)
Free allocated memory.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
void add_ipoint1(struct seg_intersection_list *il, int with, double dist, int ip)
struct planar_graph * pg_create_struct(int n, int e)
struct seg_intersections * create_si_struct(int segments_count)
void add_ipoint(const struct line_pnts *Points, int first_seg, int second_seg, double x, double y, struct seg_intersections *si)
int pg_existsedge(struct planar_graph *pg, int v1, int v2)
void sort_intersection_list(struct seg_intersection_list *il)
void pg_addedge(struct planar_graph *pg, int v1, int v2)
double get_epsilon(struct line_pnts *Points)
struct planar_graph * pg_create(const struct line_pnts *Points)
void destroy_si_struct(struct seg_intersections *si)
int compare(const void *a, const void *b)
void pg_destroy_struct(struct planar_graph *pg)
struct seg_intersections * find_all_intersections(const struct line_pnts *Points)
void pg_addedge1(struct pg_vertex *v, struct pg_edge *e)
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)
Feature geometry info - coordinates.
double * y
Array of Y coordinates.
double * x
Array of X coordinates.
int n_points
Number of points.