-
Notifications
You must be signed in to change notification settings - Fork 27
/
planar.jl
184 lines (178 loc) · 6.33 KB
/
planar.jl
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
function getsemihull(ps::Vector{PT}, sign_sense, counterclockwise, yray::Nothing = nothing) where PT
hull = PT[]
if length(ps) == 0
return hull
end
prev = sign_sense == 1 ? first(ps) : last(ps)
cur = prev
# Invariant:
# We either have:
# * `hull` is empty and `cur == prev` or
# * `hull` is nonempty and `prev = last(hull)`.
# In any case, the semihull of `ps[1:(j-1)]` is given by `[hull; cur]`.
for j in (sign_sense == 1 ? (2:length(ps)) : ((length(ps)-1):-1:1))
skip = false
while prev != cur
cur_vec = cur - prev
psj_vec = ps[j] - prev
cc = counterclockwise(cur_vec, psj_vec)
if isapproxzero(cc)
# `cur` and `ps[j]` are on the same ray starting from `prev`.
# The one that is closer to `prev` is redundant.
# If `j` is the last index and redundant (it may happen if this
# ray is perpendicular to the direction of sorting) then we should
# also avoid adding `ps[j]` to `hull` so we set `skip` to `true`.
if norm(cur_vec, 1) > norm(psj_vec, 1)
skip = true
break
end
elseif cc < 0
break
end
# `cur` is redundant so `cur` is dropped and `prev` becomes `cur`
cur = prev
pop!(hull)
if !isempty(hull)
prev = last(hull)
end
end
if !skip
push!(hull, cur)
prev = cur
cur = ps[j]
end
end
push!(hull, cur)
return hull
end
function getsemihull(ps::Vector{PT}, sign_sense, counterclockwise, yray) where PT
hull = getsemihull(ps, sign_sense, counterclockwise)
while length(hull) >= 2 && counterclockwise(hull[end] - hull[end - 1], yray) >= 0
pop!(hull)
end
return hull
end
function _planar_hull(d::FullDim, points, lines, rays, counterclockwise, rotate)
line = nothing
lineleft = false
lineright = false
function checkleftright(r::Union{Ray, Line})
cc = counterclockwise(r, line)
if !isapproxzero(cc)
if cc < 0 || islin(r)
lineleft = true
end
if cc > 0 || islin(r)
lineright = true
end
end
end
for l in lines
if !isapproxzero(l)
if line === nothing
line = l
else
checkleftright(l)
end
end
end
xray = yray = nothing
for r in rays
isapproxzero(r) && continue
if line === nothing
if xray === nothing
xray = yray = coord(r)
else
if Line(xray) ≈ linearize(r) && !(Ray(xray) ≈ r)
line = Line(xray)
checkleftright(Ray(yray))
elseif Line(yray) ≈ linearize(r) && !(Ray(yray) ≈ r)
line = Line(yray)
checkleftright(Ray(xray))
else
x_right = counterclockwise(r, xray) > 0
y_left = counterclockwise(r, yray) < 0
if x_right
if y_left
line = Line(xray)
lineleft = lineright = true
else
xray = coord(r)
end
else
if y_left
yray = coord(r)
end
end
end
end
else
checkleftright(r)
end
end
if line === nothing
if xray === nothing
sweep_norm = rotate(basis(eltype(points), d, 1))
if iszero(sum(abs, sweep_norm))
sweep_norm = rotate(basis(eltype(points), d, 2))
end
else
sweep_norm = rotate(xray)
end
else
sweep_norm = rotate(coord(line))
end
sort!(points, by = x -> dot(x, sweep_norm))
if !isempty(points)
# `getsemihull` fails if `points` starts or end with several points on the same sweep line that are not ordered clockwise
start_line = dot(points[1], sweep_norm)
# `isapprox` won't work well with `atol=0` (which is the default) if `start_line` is zero, so we set a nonzero `atol`.
# TODO We should also multiply it with a scaling.
end_start = something(findfirst(x -> !isapprox(dot(x, sweep_norm), start_line, atol=Base.rtoldefault(typeof(start_line))), points), length(points) + 1) - 1
if end_start > 1
sort!(view(points, 1:end_start), by = x -> counterclockwise(x, sweep_norm), rev=true)
end
end_line = dot(points[end], sweep_norm)
start_end = something(findlast(x -> !isapprox(dot(x, sweep_norm), end_line, atol=Base.rtoldefault(typeof(start_line))), points), 0) + 1
if start_end < length(points)
sort!(view(points, start_end:length(points)), by = x -> counterclockwise(x, sweep_norm))
end
end
_points = eltype(points)[]
_lines = eltype(lines)[]
_rays = eltype(rays)[]
if line === nothing
append!(_points, getsemihull(points, 1, counterclockwise, yray))
if yray === nothing
append!(_points, getsemihull(points, -1, counterclockwise, yray)[2:end-1])
else
push!(_rays, Ray(xray))
if !(Ray(xray) ≈ Ray(yray))
push!(_rays, Ray(yray))
end
end
else
push!(_lines, line)
push!(_points, first(points))
if lineleft
if lineright
push!(_lines, Line(sweep_norm))
else
push!(_rays, Ray(sweep_norm))
end
elseif lineright
push!(_rays, Ray(-sweep_norm))
else
if !(dot(first(points), sweep_norm) ≈ dot(last(points), sweep_norm))
push!(_points, last(points))
end
end
end
return _points, _lines, _rays
end
counterclockwise(x, y) = x[1] * y[2] - x[2] * y[1]
rotate(x) = convert(typeof(x), StaticArrays.SVector(-x[2], x[1]))
function planar_hull(vr::VRepresentation)
d = FullDim(vr)
vrep(_planar_hull(FullDim(vr), collect(points(vr)), lines(vr), rays(vr), counterclockwise, rotate)...; d = d)
end