/
interval.jl
209 lines (188 loc) · 6.04 KB
/
interval.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
export IntervalLibrary, Interval
"""
IntervalLibrary{T}
Default library for polyhedra of dimension 1. Many aspect of polyhedral computation become trivial in one dimension. This library exploits this fact.
The library is also used as a fallback for libraries that do not support 1-dimensional polyhedra (e.g. qhull). That is projecting a polyhedron using such library produces a polyhedron using `IntervalLibrary`.
"""
struct IntervalLibrary{T} <: Library
end
similar_library(lib::IntervalLibrary, d::FullDim, ::Type{T}) where T = default_library(d, T) # default_library allows to fallback to DefaultLibrary if d is not FullDim(1)
mutable struct Interval{T, AT, D} <: Polyhedron{T}
hrep::Intersection{T, AT, D}
vrep::Hull{T, AT, D}
length::T
end
Interval{T, AT, D}(d::FullDim, it::HIt...) where {T, AT, D} = _hinterval(Intersection{T, AT, D}(d, it...), AT, D)
Interval{T, AT, D}(d::FullDim, it::VIt...) where {T, AT, D} = _vinterval(Hull{T, AT, D}(d, it...), AT, D)
# If AT is an SVector, it will be StaticArrays.Size{(1,)}
# otherwise, it will be 1
FullDim(p::Interval) = FullDim(p.hrep)
library(::Union{Interval{T}, Type{<:Interval{T}}}) where T = IntervalLibrary{T}()
hvectortype(::Type{<:Interval{T, AT}}) where {T, AT} = AT
vvectortype(::Type{<:Interval{T, AT}}) where {T, AT} = AT
similar_type(::Type{<:Interval}, d::FullDim, ::Type{T}) where T = default_type(d, T)
surface(::Interval{T}) where {T} = zero(T)
volume(p::Interval) = p.length
Base.isempty(p::Interval) = isempty(p.vrep)
function _interval(AT, haslb::Bool, lb::T, hasub::Bool, ub::T, isempty::Bool) where {T}
if haslb && hasub && _gt(lb, ub)
isempty = true
end
hps = HyperPlane{T, AT}[]
hss = HalfSpace{T, AT}[]
ps = AT[]
ls = Line{T, AT}[]
rs = Ray{T, AT}[]
if !isempty
if hasub
push!(ps, StaticArrays.SVector(ub))
if haslb && _isapprox(lb, ub)
push!(hps, HyperPlane(StaticArrays.SVector(one(T)), ub))
else
push!(hss, HalfSpace(StaticArrays.SVector(one(T)), ub))
end
if !haslb
push!(rs, Ray(StaticArrays.SVector(-one(T))))
end
else
if haslb
push!(rs, Ray(StaticArrays.SVector(one(T))))
else
push!(ps, origin(AT, 1))
push!(ls, Line(StaticArrays.SVector(one(T))))
end
end
if haslb
if !_isapprox(lb, ub)
push!(hss, HalfSpace(StaticArrays.SVector(-one(T)), -lb))
push!(ps, StaticArrays.SVector(lb))
end
end
else
# The dimension should be -1 so 1 - nhyperplanes == -1 so nhyperplanes == 2
push!(hps, HyperPlane(StaticArrays.SVector(one(T)), zero(T)))
push!(hps, HyperPlane(StaticArrays.SVector(zero(T)), one(T)))
end
h = hrep(hps, hss)
v = vrep(ps, ls, rs)
volume = isempty ? zero(T) : (haslb && hasub ? max(zero(T), ub - lb) : -one(T))
return h, v, volume
end
function Interval{T, AT, D}(haslb::Bool, lb::T, hasub::Bool, ub::T, isempty::Bool) where {T, AT, D}
return Interval{T, AT, D}(_interval(AT, haslb, lb, hasub, ub, isempty)...)
end
function _hinterval(rep::HRep{T}, ::Type{AT}) where {T, AT}
haslb = false
lb = zero(T)
hasub = false
ub = zero(T)
empty = false
function _setlb(newlb)
if !haslb
haslb = true
lb = T(newlb)
else
lb = T(max(lb, newlb))
end
end
function _setub(newub)
if !hasub
hasub = true
ub = T(newub)
else
ub = T(min(ub, newub))
end
end
for hp in hyperplanes(rep)
α = hp.a[1]
if isapproxzero(α)
if !isapproxzero(hp.β)
empty = true
end
else
_setlb(hp.β / α)
_setub(hp.β / α)
end
end
for hs in halfspaces(rep)
α = hs.a[1]
if isapproxzero(α)
if hs.β < 0
empty = true
end
elseif α < 0
_setlb(hs.β / α)
else
_setub(hs.β / α)
end
end
return _interval(AT, haslb, lb, hasub, ub, empty)
end
function _hinterval(rep::HRep{T}, ::Type{AT}, D) where {T, AT}
return Interval{T, AT, D}(_hinterval(rep, AT)...)
end
function _vinterval(v::VRep{T}, ::Type{AT}, D) where {T, AT}
haslb = true
lb = zero(T)
hasub = true
ub = zero(T)
isempty = true
for p in points(v)
x = coord(p)[1]
if isempty
isempty = false
lb = x
ub = x
else
lb = min(lb, x)
ub = max(ub, x)
end
end
for l in lines(v)
if !isapproxzero(l)
isempty = false
haslb = false
hasub = false
end
end
for r in rays(v)
x = coord(r)[1]
if !isapproxzero(x)
isempty = false
if x > 0
hasub = false
else
haslb = false
end
end
end
Interval{T, AT, D}(haslb, lb, hasub, ub, isempty)
end
Interval{T, AT, D}(p::HRepresentation{T}) where {T, AT, D} = _hinterval(p, AT, D)
Interval{T, AT, D}(p::VRepresentation{T}) where {T, AT, D} = _vinterval(p, AT, D)
function Interval{T, AT, D}(p::Polyhedron{T}) where {T, AT, D}
if hrepiscomputed(p)
_hinterval(p, AT, D)
else
_vinterval(p, AT, D)
end
end
function polyhedron(rep::Rep{T}, ::IntervalLibrary{T}) where T
Interval{T, StaticArrays.SVector{1, T}, StaticArrays.Size{(1,)}}(rep)
end
hrep(p::Interval) = p.hrep
vrep(p::Interval) = p.vrep
hrepiscomputed(::Interval) = true
vrepiscomputed(::Interval) = true
# Nothing to do
function detecthlinearity!(::Interval) end
function detectvlinearity!(::Interval) end
function removehredundancy!(::Interval) end
function removevredundancy!(::Interval) end
function sethrep!(p::Interval{T, AT}, h::HRep) where {T, AT}
hnew, v, volume = _hinterval(h, AT)
p.hrep = hnew
p.vrep = v
p.length = volume
return p
end