Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge recipes for polygons and add polytopes #602

Merged
merged 4 commits into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 31 additions & 142 deletions src/plot_recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,22 @@ julia> plot([B1, B2], 1e-4);
end
end

# =========================
# Plot recipes for polygons
# =========================
# ==============================
# Plot recipes for 2D polytopes
# ==============================

"""
plot_polygon(P::Union{HPolygon, HPolygonOpt}; ...)
plot_polygon(P::AbstractPolytope; ...)

Plot a polygon in constraint representation.
Plot a 2D polytope as the convex hull of its vertices.

### Input

- `P` -- polygon in constraint representation
- `P` -- polygon or polytope

### Examples

```jldoctest
```jldoctest plotting_polytope
julia> using LazySets, Plots;

julia> P = HPolygon([LinearConstraint([1.0, 0.0], 0.6),
Expand All @@ -187,11 +187,22 @@ julia> P = HPolygon([LinearConstraint([1.0, 0.0], 0.6),

julia> plot(P);

```

This recipe also applies if the polygon is given in vertex representation:

```jldoctest plotting_polytope
julia> P = VPolygon([[0.6, 0.6], [0.4, 0.6], [0.4, 0.4], [0.6, 0.4]]);

julia> plot(P);

```
"""
@recipe function plot_polygon(P::Union{HPolygon, HPolygonOpt};
color="blue", label="", grid=true, alpha=0.5)
@recipe function plot_polytope(P::AbstractPolytope;
color="blue", label="", grid=true, alpha=0.5)

# for polytopes
@assert dim(P) == 2 "this recipe can only be used to plot two-dimensional sets"
seriestype := :shape

vlist = transpose(hcat(vertices_list(P)...))
Expand All @@ -201,17 +212,17 @@ julia> plot(P);
end

"""
plot_polygons(P::Vector{<:AbstractHPolygon}; ...)
plot_polytopes(P::Vector{<:AbstractPolytope}; ...)

Plot an array of polygons in constraint representation.
Plot an array of 2D polytopes.

### Input

- `P` -- array of polygons in constraint representation
- `P` -- array of polytopes

### Examples

```jldoctest
```jldoctest plotting_polytopes
julia> using LazySets, Plots;

julia> P1 = HPolygon([LinearConstraint([1.0, 0.0], 0.6),
Expand All @@ -227,76 +238,25 @@ julia> P2 = HPolygon([LinearConstraint([2.0, 0.0], 0.6),
julia> plot([P1, P2]);

```
"""
@recipe function plot_polygons(P::Vector{<:AbstractHPolygon};
seriescolor="blue", label="", grid=true,
alpha=0.5)

seriestype := :shape

for Pi in P
vlist = transpose(hcat(vertices_list(Pi)...))
@series (x, y) = vlist[:, 1], vlist[:, 2]
end
end

"""
plot_polygon(P::VPolygon; ...)

Plot a polygon in vertex representation.

### Input

- `P` -- polygon in vertex representation

### Examples

```jldoctest
julia> using LazySets, Plots;

julia> P = VPolygon([[0.6, 0.6], [0.4, 0.6], [0.4, 0.4], [0.6, 0.4]]);

julia> plot(P);

```
"""
@recipe function plot_polygon(P::VPolygon;
color="blue", label="", grid=true, alpha=0.5)

seriestype := :shape

vlist = transpose(hcat(vertices_list(P)...))
(x, y) = vlist[:, 1], vlist[:, 2]

x, y
end

"""
plot_polygons(P::Vector{<:VPolygon}; ...)

Plot an array of polygons in vertex representation.

### Input

- `P` -- array of polygons in vertex representation

### Examples

```jldoctest
julia> using LazySets, Plots;

```jldoctest plotting_polytopes
julia> P1 = VPolygon([[0.6, 0.6], [0.4, 0.6], [0.4, 0.4], [0.6, 0.4]]);

julia> P2 = VPolygon([[0.3, 0.3], [0.2, 0.3], [0.2, 0.2], [0.3, 0.2]]);

julia> plot([P1, P2]);

```

### Notes

It is assumed that the given vector of polytopes is two-dimensional.
"""
@recipe function plot_polygons(P::Vector{<:VPolygon};
@recipe function plot_polytopes(P::Vector{<:AbstractPolytope};
seriescolor="blue", label="", grid=true,
alpha=0.5)

# it is assumed that the polytopes are two-dimensional
seriestype := :shape

for Pi in P
Expand Down Expand Up @@ -373,77 +333,6 @@ julia> plot([Singleton(a), Singleton(b), Singleton(c)]);
[Tuple(element(point)) for point in arr]
end

# ============================
# Plot recipes for zonotopes
# ============================

"""
plot_polygon(Z::Zonotope; ...)

Plot a zonotope by enumerating its vertices.

### Input

- `Z` -- zonotope

### Examples

```jldoctest
julia> using LazySets, Plots;

julia> Z = Zonotope(ones(2), 0.2*[[1., 0], [0., 1], [1, 1]]);

julia> plot(Z);

```
"""
@recipe function plot_zonotope(Z::Zonotope;
color="blue", label="", grid=true, alpha=0.5)

seriestype := :shape

# we have to take the convex hull for the shape
vlist = transpose(hcat(vertices_list(Z)...))
(x, y) = vlist[:, 1], vlist[:, 2]

x, y
end

"""
plot_zonotopes(Z::Vector{<:Zonotope}; ...)

Plot an array of zonotopes.

### Input

- `Z` -- linear array of zonotopes

### Examples

```jldoctest
julia> using LazySets, Plots;

julia> Z1 = Zonotope(zeros(2), [[0.6, 0.6], [0.4, 0.6], [0.4, 0.4], [0.6, 0.4]]);

julia> Z2 = Zonotope(zeros(2), [[0.3, 0.3], [0.2, 0.3], [0.2, 0.2], [0.3, 0.2]]);

julia> plot([Z1, Z2]);

```
"""
@recipe function plot_zonotopes(Z::Vector{<:Zonotope};
seriescolor="blue", label="", grid=true,
alpha=0.5)

seriestype := :shape

for Zi in Z
# we have to take the convex hull for the shape
vlist = transpose(hcat(vertices_list(Zi)...))
@series (x, y) = vlist[:, 1], vlist[:, 2]
end
end

# =====================================
# Plot recipes for lines and intervals
# =====================================
Expand Down
2 changes: 1 addition & 1 deletion test/unit_plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ for N in [Float64, Rational{Int}, Float32]
plot(hpgo)
plot(hpt)
plot(vpg)
@test_throws AssertionError plot(vpt) # TODO see #574
plot(vpt)
@test_throws ErrorException plot(es) # TODO see #577
@test_throws ErrorException plot(hs) # TODO see #576
@test_throws ErrorException plot(hp) # TODO see #576
Expand Down