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

Extend convex hull docs #1285

Merged
merged 7 commits into from
Apr 13, 2019
Merged
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
56 changes: 55 additions & 1 deletion docs/src/man/convex_hulls.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
In this section we illustrate the
[convex hull operation](https://en.wikipedia.org/wiki/Convex_hull).
We give examples of the symbolic implementation, and the concrete convex hull in
low dimensions.
low dimensions. We show how to test if a point lies in the convex
hull of a set of points in the plane using `LazySets`. Moreover, we give examples
of creating the convex hull of sets whose vertices are represented as *static* vectors,
which can dramatically improve performance in many use cases. Finally, we give
an example of creating the convex hull of points in higher dimensions.

```@contents
Pages = ["convex_hulls.md"]
Expand Down Expand Up @@ -88,6 +92,12 @@ p = plot([Singleton(vi) for vi in v])
plot!(p, VPolygon(hull), alpha=0.2)
```

## Test point in convex hull

One can check whether a point lies inside or outside of a convex hull efficiently
in two dimensions, using the fact that the output of `convex_hull` returns
the points ordered in counter-clockwise fashion.

!!! note
To check if a point `p::AbstractVector` is in another set, e.g. a polygon in
vertex representation `V`, use `p ∈ V`. However, if you are working with a
Expand Down Expand Up @@ -118,6 +128,10 @@ Or use set inclusion between the singleton and the right-hand side set:
Singleton(v[1]) ⊆ VPolygon(hull)
```

Let us note that one can also make the point-in-convex-hull test by solving
a feasibility problem; actually, this is the fallback implementation used for
in any dimension. However, the specialized approach in 2D is more efficient.

## Using static vectors

Usual vectors are such that you can `push!` and `pop!` without changing its
Expand Down Expand Up @@ -149,3 +163,43 @@ convex_hull([@SVector(rand(2)) for i in 1:3]) # warm-up
v_static = [SVector{2, Float64}(vi) for vi in v]
@time convex_hull(v_static)
```

## Higher-dimensional convex hull

One can compute the convex hull of points in higher dimensions using `convex_hull`.
The appropriate algorithm is decided based on the dimensionality of the given
points.

```@example example_ch
using Polyhedra

v = [randn(3) for _ in 1:30]
hull = convex_hull(v)
typeof(hull), length(v), length(hull)
```

Here, `convex_hull` is now using the concrete polyhedra library
[Polyhedra](https://github.com/JuliaPolyhedra/Polyhedra.jl), hence it needs to be
loaded beforehand.

One can check whether a point belongs to the convex hull using `∈` as follows:

```@example example_ch
P = VPolytope(hull)
x = sum(hull)/length(hull)

x ∈ P
```

Here `x ∈ P` solves a feasibility problem; see the docs of `?∈` for details.
Equivalently, using set inclusion:

```@example example_ch
Singleton(x) ⊆ P
```

If no additional arguments are passed, `convex_hull` uses the default polyhedra
library from `default_polyhedra_backend` for the given input; different options
can be passed through the `backend` keyword; see the
[Julia polyhedra website](https://juliapolyhedra.github.io/) for all the available
backends.