/
defaultlibrary.jl
117 lines (104 loc) · 4.7 KB
/
defaultlibrary.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
export DefaultLibrary, DefaultPolyhedron
"""
DefaultLibrary{T}
Default library for polyhedra of dimension larger than 1 ([`IntervalLibrary`](@ref) is the default for polyhedra of dimension 1).
The library implements the bare minimum and uses the fallback implementation for all operations.
"""
struct DefaultLibrary{T} <: Library
solver
function DefaultLibrary{T}(solver=nothing) where T
new{T}(solver)
end
end
similar_library(lib::DefaultLibrary, d::FullDim, ::Type{T}) where T = default_library(d, T) # default_library allows to fallback to Interval if d is FullDim{1}
mutable struct DefaultPolyhedron{T, HRepT<:HRepresentation{T}, VRepT<:VRepresentation{T}} <: Polyhedron{T}
hrep::Union{HRepT, Nothing}
vrep::Union{VRepT, Nothing}
solver
function DefaultPolyhedron{T, HRepT, VRepT}(hrep::Union{HRepT, Nothing}, vrep::Union{VRepT, Nothing}, solver) where {T, HRepT<:HRepresentation{T}, VRepT<:VRepresentation{T}}
new{T, HRepT, VRepT}(hrep, vrep, solver)
end
end
function DefaultPolyhedron{T, HRepT, VRepT}(hrep::HRepT, solver) where {T, HRepT<:HRepresentation{T}, VRepT<:VRepresentation{T}}
DefaultPolyhedron{T, HRepT, VRepT}(hrep, nothing, solver)
end
function DefaultPolyhedron{T, HRepT, VRepT}(vrep::VRepT, solver) where {T, HRepT<:HRepresentation{T}, VRepT<:VRepresentation{T}}
DefaultPolyhedron{T, HRepT, VRepT}(nothing, vrep, solver)
end
function DefaultPolyhedron{T, HRepT, VRepT}(hrep::HRepresentation, solver) where {T, HRepT<:HRepresentation{T}, VRepT<:VRepresentation{T}}
DefaultPolyhedron{T, HRepT, VRepT}(convert(HRepT, hrep), solver)
end
function DefaultPolyhedron{T, HRepT, VRepT}(vrep::VRepresentation, solver) where {T, HRepT<:HRepresentation{T}, VRepT<:VRepresentation{T}}
DefaultPolyhedron{T, HRepT, VRepT}(convert(VRepT, vrep), solver)
end
FullDim(p::DefaultPolyhedron) = FullDim_rep(p.hrep, p.vrep)
library(::Union{DefaultPolyhedron{T}, Type{<:DefaultPolyhedron{T}}}) where {T} = DefaultLibrary{T}()
default_solver(p::DefaultPolyhedron; T = nothing) = p.solver
supportssolver(::Type{<:DefaultPolyhedron}) = true
hvectortype(::Type{<:DefaultPolyhedron{T, HRepT}}) where {T, HRepT} = hvectortype(HRepT)
vvectortype(::Type{DefaultPolyhedron{T, HRepT, VRepT}}) where {T, HRepT, VRepT} = vvectortype(VRepT)
similar_type(::Type{<:DefaultPolyhedron{S, HRepT, VRepT}}, d::FullDim, ::Type{T}) where {S, HRepT, VRepT, T} = DefaultPolyhedron{T, similar_type(HRepT, d, T), similar_type(VRepT, d, T)}
function DefaultPolyhedron{T, HRepT, VRepT}(d::FullDim, hits::HIt...; solver=nothing) where {T, HRepT, VRepT}
DefaultPolyhedron{T, HRepT, VRepT}(HRepT(d, hits...), solver)
end
function DefaultPolyhedron{T, HRepT, VRepT}(d::FullDim, vits::VIt...; solver=nothing) where {T, HRepT, VRepT}
DefaultPolyhedron{T, HRepT, VRepT}(VRepT(d, vits...), solver)
end
# Need fulltype in case the use does `intersect!` with another element
DefaultPolyhedron{T}(rep::Representation, solver) where {T} = DefaultPolyhedron{T}(change_coefficient_type(rep, T), solver)
function DefaultPolyhedron{T}(rep::HRepresentation{T}, solver) where {T}
HRepT = fulltype(typeof(rep))
VRepT = dualtype(HRepT)
DefaultPolyhedron{T, HRepT, VRepT}(rep, solver)
end
function DefaultPolyhedron{T}(rep::VRepresentation{T}, solver) where {T}
VRepT = fulltype(typeof(rep))
HRepT = dualtype(VRepT)
DefaultPolyhedron{T, HRepT, VRepT}(rep, solver)
end
function polyhedron(rep::Representation, lib::DefaultLibrary{T}) where {T}
DefaultPolyhedron{polytypefor(T)}(rep, lib.solver)
end
function Base.copy(p::DefaultPolyhedron{T}) where {T}
if p.hrep !== nothing
DefaultPolyhedron{T}(p.hrep, p.solver)
else
DefaultPolyhedron{T}(p.vrep, p.solver)
end
end
hrepiscomputed(p::DefaultPolyhedron) = p.hrep !== nothing
function computehrep!(p::DefaultPolyhedron)
# vrep(p) could trigger an infinite loop if both vrep and hrep are null
p.hrep = doubledescription(p.vrep, p.solver)
end
function hrep(p::DefaultPolyhedron)
if !hrepiscomputed(p)
computehrep!(p)
end
return p.hrep
end
vrepiscomputed(p::DefaultPolyhedron) = p.vrep !== nothing
function computevrep!(p::DefaultPolyhedron)
# hrep(p) could trigger an infinite loop if both vrep and hrep are null
p.vrep = doubledescription(p.hrep, p.solver)
end
function vrep(p::DefaultPolyhedron)
if !vrepiscomputed(p)
computevrep!(p)
end
return p.vrep
end
function sethrep!(p::DefaultPolyhedron, h::HRepresentation)
p.hrep = h
end
function setvrep!(p::DefaultPolyhedron, v::VRepresentation)
p.vrep = v
end
function resethrep!(p::DefaultPolyhedron, h::HRepresentation)
p.hrep = h
p.vrep = nothing
end
function resetvrep!(p::DefaultPolyhedron, v::VRepresentation)
p.vrep = v
p.hrep = nothing
end