From 3e0a88c0006ab9184f53a62adc157cd6b9f001c8 Mon Sep 17 00:00:00 2001 From: mforets Date: Sat, 13 Apr 2019 11:30:22 -0300 Subject: [PATCH 1/7] extend convex hull docs --- docs/src/man/convex_hulls.md | 58 +++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/docs/src/man/convex_hulls.md b/docs/src/man/convex_hulls.md index bd7649f9fe..8457f1cb2f 100644 --- a/docs/src/man/convex_hulls.md +++ b/docs/src/man/convex_hulls.md @@ -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 lines in the convex +hull of a set of points in the plane using `LazySets`. Moreover, we give examples +of creating 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"] @@ -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 @@ -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 @@ -149,3 +163,45 @@ 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. The code looks the same as for dimension 2, and is not restricted to +dimension 3; one can also use 4 or higher, in principle, although the computational +complexity of `convex_hull` also increases. + +```@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 algorithm; see the +[Julia polyhedra website](https://juliapolyhedra.github.io/) for all the available +backends. From 4fddbd3d029972b18f8defa0ca2814ad69c9346e Mon Sep 17 00:00:00 2001 From: Christian Schilling Date: Sat, 13 Apr 2019 11:38:38 -0300 Subject: [PATCH 2/7] Update docs/src/man/convex_hulls.md Co-Authored-By: mforets --- docs/src/man/convex_hulls.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/man/convex_hulls.md b/docs/src/man/convex_hulls.md index 8457f1cb2f..5c1f4d5f8e 100644 --- a/docs/src/man/convex_hulls.md +++ b/docs/src/man/convex_hulls.md @@ -3,7 +3,7 @@ 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. We show how to test if a point lines in the convex +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 convex hull of sets whose vertices are represented as *static* vectors, which can dramatically improve performance in many use cases. Finally, we give From 7c91376eaf33b8e9526825cdc750a27f5f21047c Mon Sep 17 00:00:00 2001 From: Christian Schilling Date: Sat, 13 Apr 2019 11:38:46 -0300 Subject: [PATCH 3/7] Update docs/src/man/convex_hulls.md Co-Authored-By: mforets --- docs/src/man/convex_hulls.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/man/convex_hulls.md b/docs/src/man/convex_hulls.md index 5c1f4d5f8e..79951fc126 100644 --- a/docs/src/man/convex_hulls.md +++ b/docs/src/man/convex_hulls.md @@ -5,7 +5,7 @@ In this section we illustrate the We give examples of the symbolic implementation, and the concrete convex hull in 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 convex hull of sets whose vertices are represented as *static* vectors, +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 From d021b0cbcaed79b529633de17d3e967da653c19c Mon Sep 17 00:00:00 2001 From: Christian Schilling Date: Sat, 13 Apr 2019 11:38:52 -0300 Subject: [PATCH 4/7] Update docs/src/man/convex_hulls.md Co-Authored-By: mforets --- docs/src/man/convex_hulls.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/man/convex_hulls.md b/docs/src/man/convex_hulls.md index 79951fc126..41d19ee279 100644 --- a/docs/src/man/convex_hulls.md +++ b/docs/src/man/convex_hulls.md @@ -7,7 +7,7 @@ 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 +an example of creating the convex hull of points in higher dimensions. ```@contents Pages = ["convex_hulls.md"] From de579170098701ed4be290783f2ee2fdfdc3221f Mon Sep 17 00:00:00 2001 From: Christian Schilling Date: Sat, 13 Apr 2019 11:39:15 -0300 Subject: [PATCH 5/7] Update docs/src/man/convex_hulls.md Co-Authored-By: mforets --- docs/src/man/convex_hulls.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/man/convex_hulls.md b/docs/src/man/convex_hulls.md index 41d19ee279..4dd68da9b7 100644 --- a/docs/src/man/convex_hulls.md +++ b/docs/src/man/convex_hulls.md @@ -193,7 +193,7 @@ x = sum(hull)/length(hull) x ∈ P ``` -Here `x ∈ P` solves a feasibility problem, see the docs of `?∈` for details. +Here `x ∈ P` solves a feasibility problem; see the docs of `?∈` for details. Equivalently, using set inclusion: ```@example example_ch From 2d56e99a8d9d893c59473cf1ef5b5926d36a8148 Mon Sep 17 00:00:00 2001 From: Christian Schilling Date: Sat, 13 Apr 2019 11:39:22 -0300 Subject: [PATCH 6/7] Update docs/src/man/convex_hulls.md Co-Authored-By: mforets --- docs/src/man/convex_hulls.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/man/convex_hulls.md b/docs/src/man/convex_hulls.md index 4dd68da9b7..e2dbd1f0ce 100644 --- a/docs/src/man/convex_hulls.md +++ b/docs/src/man/convex_hulls.md @@ -202,6 +202,6 @@ 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 algorithm; see the +can be passed through the `backend` keyword; see the [Julia polyhedra website](https://juliapolyhedra.github.io/) for all the available backends. From 42669cadb5975442fa667f30d4cfac7026069acc Mon Sep 17 00:00:00 2001 From: mforets Date: Sat, 13 Apr 2019 11:39:59 -0300 Subject: [PATCH 7/7] revise docs --- docs/src/man/convex_hulls.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/src/man/convex_hulls.md b/docs/src/man/convex_hulls.md index e2dbd1f0ce..a42961ab05 100644 --- a/docs/src/man/convex_hulls.md +++ b/docs/src/man/convex_hulls.md @@ -168,9 +168,7 @@ v_static = [SVector{2, Float64}(vi) for vi in v] 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. The code looks the same as for dimension 2, and is not restricted to -dimension 3; one can also use 4 or higher, in principle, although the computational -complexity of `convex_hull` also increases. +points. ```@example example_ch using Polyhedra