-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathhull.c
220 lines (177 loc) · 5.39 KB
/
hull.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Twin - A Tiny Window System
* Copyright (c) 2004 Carl Worth <[email protected]>
* All rights reserved.
*/
#include <stdbool.h>
#include <stdlib.h>
#include "twin_private.h"
typedef struct twin_slope {
twin_sfixed_t dx;
twin_sfixed_t dy;
} twin_slope_t, twin_distance_t;
typedef struct _twin_hull {
twin_spoint_t point;
twin_slope_t slope;
bool discard;
} twin_hull_t;
static void _twin_slope_init(twin_slope_t *slope,
const twin_spoint_t *a,
const twin_spoint_t *b)
{
slope->dx = b->x - a->x;
slope->dy = b->y - a->y;
}
static twin_hull_t *_twin_hull_create(const twin_path_t *path, int *nhull)
{
int n = path->npoints;
const twin_spoint_t *p = path->points;
twin_hull_t *hull;
int e = 0;
for (int i = 1; i < n; i++)
if (p[i].y < p[e].y || (p[i].y == p[e].y && p[i].x < p[e].x))
e = i;
hull = malloc(n * sizeof(twin_hull_t));
if (!hull)
return NULL;
*nhull = n;
for (int i = 0, j; i < n; i++) {
/* place extremum first in array */
if (i == 0)
j = e;
else if (i == e)
j = 0;
else
j = i;
hull[i].point = p[j];
_twin_slope_init(&hull[i].slope, &hull[0].point, &hull[i].point);
/* Discard all points coincident with the extremal point */
if (i != 0 && hull[i].slope.dx == 0 && hull[i].slope.dy == 0)
hull[i].discard = true;
else
hull[i].discard = false;
}
return hull;
}
/* Compare two slopes. Slope angles begin at 0 in the direction of the
positive X axis and increase in the direction of the positive Y
axis.
WARNING: This function only gives correct results if the angular
difference between a and b is less than PI.
< 0 => a less positive than b
== 0 => a equal to be
> 0 => a more positive than b
*/
static int _twin_slope_compare(const twin_slope_t *a, const twin_slope_t *b)
{
twin_dfixed_t diff = ((twin_dfixed_t) a->dy * (twin_dfixed_t) b->dx -
(twin_dfixed_t) b->dy * (twin_dfixed_t) a->dx);
if (diff > 0)
return 1;
if (diff < 0)
return -1;
if (a->dx == 0 && a->dy == 0)
return 1;
if (b->dx == 0 && b->dy == 0)
return -1;
return 0;
}
static int _twin_hull_vertex_compare(const void *av, const void *bv)
{
twin_hull_t *a = (twin_hull_t *) av;
twin_hull_t *b = (twin_hull_t *) bv;
int ret = _twin_slope_compare(&a->slope, &b->slope);
/* In the case of two vertices with identical slope from the
extremal point discard the nearer point. */
if (ret == 0) {
twin_dfixed_t a_dist, b_dist;
a_dist = ((twin_dfixed_t) a->slope.dx * a->slope.dx +
(twin_dfixed_t) a->slope.dy * a->slope.dy);
b_dist = ((twin_dfixed_t) b->slope.dx * b->slope.dx +
(twin_dfixed_t) b->slope.dy * b->slope.dy);
if (a_dist < b_dist) {
a->discard = true;
ret = -1;
} else {
b->discard = true;
ret = 1;
}
}
return ret;
}
static int _twin_hull_prev_valid(const twin_hull_t *hull,
int maybe_unused num_hull,
int index)
{
do {
/* hull[0] is always valid, so don't test and wraparound */
index--;
} while (hull[index].discard);
return index;
}
static int _twin_hull_next_valid(const twin_hull_t *hull,
int num_hull,
int index)
{
do {
index = (index + 1) % num_hull;
} while (hull[index].discard);
return index;
}
/*
* Graham scan to compute convex hull
*/
static void _twin_hull_eliminate_concave(twin_hull_t *hull, int num_hull)
{
twin_slope_t slope_ij, slope_jk;
int i = 0;
int j = _twin_hull_next_valid(hull, num_hull, i);
int k = _twin_hull_next_valid(hull, num_hull, j);
do {
_twin_slope_init(&slope_ij, &hull[i].point, &hull[j].point);
_twin_slope_init(&slope_jk, &hull[j].point, &hull[k].point);
/* Is the angle formed by ij and jk concave? */
if (_twin_slope_compare(&slope_ij, &slope_jk) >= 0) {
if (i == k)
break;
hull[j].discard = true;
j = i;
i = _twin_hull_prev_valid(hull, num_hull, j);
} else {
i = j;
j = k;
k = _twin_hull_next_valid(hull, num_hull, j);
}
} while (j != 0);
}
/*
* Convert the hull structure back to a simple path
*/
static twin_path_t *_twin_hull_to_path(const twin_hull_t *hull, int num_hull)
{
twin_path_t *path = twin_path_create();
for (int i = 0; i < num_hull; i++) {
if (hull[i].discard)
continue;
_twin_path_sdraw(path, hull[i].point.x, hull[i].point.y);
}
return path;
}
/*
* Given a path, return the convex hull using the Graham scan algorithm.
*/
twin_path_t *twin_path_convex_hull(twin_path_t *path)
{
twin_hull_t *hull;
int num_hull;
twin_path_t *convex_path;
hull = _twin_hull_create(path, &num_hull);
if (!hull)
return NULL;
qsort(hull + 1, num_hull - 1, sizeof(twin_hull_t),
_twin_hull_vertex_compare);
_twin_hull_eliminate_concave(hull, num_hull);
convex_path = _twin_hull_to_path(hull, num_hull);
free(hull);
return convex_path;
}