23#define LENGTH(DX, DY) (sqrt((DX * DX) + (DY * DY)))
26struct intersection_point {
32struct seg_intersection {
39struct seg_intersection_list {
42 struct seg_intersection *a;
45struct seg_intersections {
48 struct intersection_point *ip;
50 struct seg_intersection_list *il;
56 struct seg_intersections *
si;
60 si =
G_malloc(
sizeof(
struct seg_intersections));
63 si->ip =
G_malloc((
si->ipallocated) *
sizeof(
struct intersection_point));
68 si->il[i].allocated = 0;
79 for (i = 0; i <
si->ilcount; i++)
89void add_ipoint1(
struct seg_intersection_list *il,
int with,
double dist,
92 struct seg_intersection *s;
94 if (il->count == il->allocated) {
97 G_realloc(il->a, (il->allocated) *
sizeof(
struct seg_intersection));
99 s = &(il->a[il->count]);
110 double x,
double y,
struct seg_intersections *
si)
112 struct intersection_point *
t;
118 if (
si->ipcount ==
si->ipallocated) {
119 si->ipallocated += 16;
121 sizeof(
struct intersection_point));
143 struct seg_intersection
t;
145 G_debug(4,
"sort_intersection_list()");
149 for (i = 0; i < n - 1; i++) {
151 for (
j = i + 1;
j < n;
j++) {
152 if (il->a[
j].dist < il->a[
min].dist) {
158 il->a[i] = il->a[
min];
168 struct intersection_point *
aa, *
bb;
170 aa = *((
struct intersection_point **)a);
171 bb = *((
struct intersection_point **)
b);
175 else if (
aa->x >
bb->x)
178 return (
aa->y <
bb->y) ? -1 : ((
aa->y >
bb->y) ? 1 : 0);
195 for (i = 1; i <= np - 2; i++) {
197 if ((
t > 0) && (
t <
min)) {
203 return min * 0.000001;
216 double x1, y1, x2, y2;
221 struct seg_intersections *
si;
222 struct seg_intersection_list *il;
223 struct intersection_point **sorted;
225 G_debug(3,
"find_all_intersections()");
233 looped = ((
x[0] ==
x[np - 1]) && (y[0] == y[np - 1]));
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);
243 y[
j],
x[
j + 1], y[
j + 1], &x1, &y1,
254 G_debug(4,
" intersection type = %d", res);
258 else if ((res >= 2) && (res <= 5)) {
267 add_ipoint(Points, np - 2, -1, Points->
x[np - 1], Points->
y[np - 1],
270 G_debug(3,
" finding intersections...done");
272 G_debug(3,
" postprocessing...");
273 if (
si->ipallocated >
si->ipcount) {
274 si->ipallocated =
si->ipcount;
276 (
si->ipcount) *
sizeof(
struct intersection_point));
278 for (i = 0; i <
si->ilcount; i++) {
280 if (il->allocated > il->count) {
281 il->allocated = il->count;
283 G_realloc(il->a, (il->count) *
sizeof(
struct seg_intersection));
293 sorted =
G_malloc((
si->ipcount) *
sizeof(
struct intersection_point *));
294 for (i = 0; i <
si->ipcount; i++)
295 sorted[i] = &(
si->ip[i]);
297 qsort(sorted,
si->ipcount,
sizeof(
struct intersection_point *),
compare);
301 for (i = 0; i <
si->ipcount; i++) {
304 for (
j = i - 1;
j >= 0;
j--) {
312 t = sorted[
j]->group;
317 (
int)(sorted[i] - &(
si->ip[0])));
318 sorted[i]->group =
t;
322 si->group_count = group;
324 G_debug(3,
" postprocessing...done");
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);
364 for (i = 0; i < pg->
vcount; i++) {
380 if (pg->
v[
v1].ecount <= pg->
v[
v2].ecount)
386 for (i = 0; i < ecount; i++) {
388 if (((e->
v1 ==
v1) && (e->
v2 ==
v2)) ||
425 "than the initial allocation size allows"));
444 struct seg_intersections *
si;
446 struct intersection_point *ip;
458 for (i = 0; i <
si->ipcount; i++) {
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;
478 for (i = 0; i < pg->
vcount; i++) {
481 for (
j = 0;
j <
vert->ecount;
j++) {
482 edge =
vert->edges[
j];
483 t = (edge->
v1 != i) ? (edge->
v1) : (edge->
v2);
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);
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 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_struct(int n, int e)
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)
struct planar_graph * pg_create(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.