/
HPolygon.jl
185 lines (147 loc) · 6.14 KB
/
HPolygon.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
185
import Base.<=
export HPolygon
"""
HPolygon{N<:Real} <: AbstractHPolygon{N}
Type that represents a convex polygon in constraint representation whose edges
are sorted in counter-clockwise fashion with respect to their normal directions.
### Fields
- `constraints` -- list of linear constraints, sorted by the angle
- `sort_constraints` -- (optional, default: `true`) flag for sorting the
constraints (sortedness is a running assumption of this
type)
- `check_boundedness` -- (optional, default: `false`) flag for checking if the
constraints make the polygon bounded; (boundedness is a
running assumption of this type)
### Notes
The default constructor assumes that the given list of edges is sorted.
It *does not perform* any sorting.
Use `addconstraint!` to iteratively add the edges in a sorted way.
- `HPolygon(constraints::Vector{LinearConstraint{<:Real}})`
-- default constructor
- `HPolygon()`
-- constructor with no constraints
"""
struct HPolygon{N<:Real} <: AbstractHPolygon{N}
constraints::Vector{LinearConstraint{N}}
# default constructor that applies sorting of the given constraints and
# (checks for and) removes redundant constraints
function HPolygon{N}(constraints::Vector{LinearConstraint{N}};
sort_constraints::Bool=true,
check_boundedness::Bool=false,
prune::Bool=true) where {N<:Real}
if sort_constraints
sorted_constraints = Vector{LinearConstraint{N}}()
sizehint!(sorted_constraints, length(constraints))
for ci in constraints
addconstraint!(sorted_constraints, ci; prune=prune)
end
P = new{N}(sorted_constraints)
else
P = new{N}(constraints)
end
@assert (!check_boundedness ||
isbounded(P, false)) "the polygon is not bounded"
return P
end
end
# convenience constructor without type parameter
HPolygon(constraints::Vector{LinearConstraint{N}};
sort_constraints::Bool=true,
check_boundedness::Bool=false,
prune::Bool=true) where {N<:Real} =
HPolygon{N}(constraints;
sort_constraints=sort_constraints,
check_boundedness=check_boundedness,
prune=prune)
# constructor for an HPolygon with no constraints
HPolygon{N}() where {N<:Real} = HPolygon{N}(Vector{LinearConstraint{N}}())
# constructor for an HPolygon with no constraints of type Float64
HPolygon() = HPolygon{Float64}()
# constructor from a simple H-representation
HPolygon(A::AbstractMatrix{N},
b::AbstractVector{N};
sort_constraints::Bool=true,
check_boundedness::Bool=false,
prune::Bool=true) where {N<:Real} =
HPolygon(constraints_list(A, b); sort_constraints=sort_constraints,
check_boundedness=check_boundedness, prune=prune)
# constructor from a simple H-representation with type parameter
HPolygon{N}(A::AbstractMatrix{N},
b::AbstractVector{N};
sort_constraints::Bool=true,
check_boundedness::Bool=false,
prune::Bool=true) where {N<:Real} =
HPolygon(A, b; sort_constraints=sort_constraints,
check_boundedness=check_boundedness, prune=prune)
# --- LazySet interface functions ---
"""
σ(d::AbstractVector{N}, P::HPolygon{N};
[linear_search]::Bool=(length(P.constraints) < BINARY_SEARCH_THRESHOLD)
) where {N<:Real}
Return the support vector of a polygon in a given direction.
### Input
- `d` -- direction
- `P` -- polygon in constraint representation
- `linear_search` -- (optional, default: see below) flag for controlling whether
to perform a linear search or a binary search
### Output
The support vector in the given direction.
The result is always one of the vertices; in particular, if the direction has
norm zero, any vertex is returned.
### Algorithm
Comparison of directions is performed using polar angles; see the overload of
`<=` for two-dimensional vectors.
For polygons with `BINARY_SEARCH_THRESHOLD = 10` or more constraints we use a
binary search by default.
"""
function σ(d::AbstractVector{N}, P::HPolygon{N};
linear_search::Bool=(length(P.constraints) < BINARY_SEARCH_THRESHOLD)
) where {N<:Real}
n = length(P.constraints)
@assert n > 0 "the polygon has no constraints"
if linear_search
# linear search
k = 1
while k <= n && P.constraints[k].a <= d
k += 1
end
else
# binary search
k = binary_search_constraints(d, P.constraints, n, 1 + div(n, 2))
end
if k == 1 || k == n+1
# corner cases: wrap-around in constraints list
return element(intersection(Line(P.constraints[1]),
Line(P.constraints[n])))
else
return element(intersection(Line(P.constraints[k]),
Line(P.constraints[k-1])))
end
end
"""
translate(v::AbstractVector{N}, P::HPolygon{N}; share::Bool=false
) where {N<:Real}
Translate (i.e., shift) a polygon in constraint representation by a given
vector.
### Input
- `P` -- polygon in constraint representation
- `v` -- translation vector
- `share` -- (optional, default: `false`) flag for sharing unmodified parts of
the original set representation
### Output
A translated polygon in constraint representation.
### Notes
The normal vectors of the constraints (vector `a` in `a⋅x ≤ b`) are shared with
the original constraints if `share == true`.
### Algorithm
We translate every constraint.
"""
function translate(P::HPolygon{N}, v::AbstractVector{N}; share::Bool=false
) where {N<:Real}
@assert length(v) == dim(P) "cannot translate a $(dim(P))-dimensional " *
"set by a $(length(v))-dimensional vector"
constraints = [translate(c, v; share=share) for c in constraints_list(P)]
return HPolygon(constraints;
sort_constraints=false, check_boundedness=false,
prune=false)
end