Skip to content

Commit

Permalink
Merge pull request #2957 from JuliaReach/schillic/2111
Browse files Browse the repository at this point in the history
#2111 - Volume for polytopes
  • Loading branch information
schillic authored Apr 11, 2022
2 parents 393e67c + 0afdd1e commit 501c2d2
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/lib/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ This interface defines the following functions:
isbounded(::AbstractPolytope)
isuniversal(::AbstractPolytope{N}, ::Bool=false) where {N}
isempty(::AbstractPolytope)
volume(::AbstractPolytope)
```

### Implementations
Expand Down
1 change: 1 addition & 0 deletions docs/src/lib/sets/Ball1.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Inherited from [`LazySet`](@ref):
Inherited from [`AbstractPolytope`](@ref):
* [`isbounded`](@ref isbounded(::AbstractPolytope))
* [`isuniversal`](@ref isuniversal(::AbstractPolytope{N}, ::Bool=false) where {N})
* [`volume`](@ref volume(::AbstractPolytope))

Inherited from [`AbstractCentrallySymmetricPolytope`](@ref):
* [`dim`](@ref dim(::AbstractCentrallySymmetricPolytope))
Expand Down
1 change: 1 addition & 0 deletions docs/src/lib/sets/HParallelotope.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Inherited from [`LazySet`](@ref):
Inherited from [`AbstractPolytope`](@ref):
* [`isbounded`](@ref isbounded(::AbstractPolytope))
* [`isuniversal`](@ref isuniversal(::AbstractPolytope{N}, ::Bool=false) where {N})
* [`volume`](@ref volume(::AbstractPolytope))

Inherited from [`AbstractCentrallySymmetricPolytope`](@ref):
* [`dim`](@ref dim(::AbstractCentrallySymmetricPolytope))
Expand Down
1 change: 1 addition & 0 deletions docs/src/lib/sets/HPolygon.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Inherited from [`AbstractPolyhedron`](@ref):
Inherited from [`AbstractPolytope`](@ref):
* [`isempty`](@ref isempty(::AbstractPolytope))
* [`isuniversal`](@ref isuniversal(::AbstractPolytope{N}, ::Bool=false) where {N})
* [`volume`](@ref volume(::AbstractPolytope))

Inherited from [`AbstractPolygon`](@ref):
* [`dim`](@ref dim(::AbstractPolygon))
Expand Down
1 change: 1 addition & 0 deletions docs/src/lib/sets/HPolygonOpt.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Inherited from [`LazySet`](@ref):
Inherited from [`AbstractPolytope`](@ref):
* [`isempty`](@ref isempty(::AbstractPolytope))
* [`isuniversal`](@ref isuniversal(::AbstractPolytope{N}, ::Bool=false) where {N})
* [`volume`](@ref volume(::AbstractPolytope))

Inherited from [`AbstractPolygon`](@ref):
* [`dim`](@ref dim(::AbstractPolygon))
Expand Down
1 change: 1 addition & 0 deletions docs/src/lib/sets/HPolytope.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ isbounded(::HPolytope, ::Bool=true)

Inherited from [`AbstractPolytope`](@ref):
* [`isuniversal`](@ref isuniversal(::AbstractPolytope{N}, ::Bool=false) where {N})
* [`volume`](@ref volume(::AbstractPolytope))
1 change: 1 addition & 0 deletions docs/src/lib/sets/LineSegment.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Inherited from [`LazySet`](@ref):
Inherited from [`AbstractPolytope`](@ref):
* [`isbounded`](@ref isbounded(::AbstractPolytope))
* [`isuniversal`](@ref isuniversal(::AbstractPolytope, ::Bool=false))
* [`volume`](@ref volume(::AbstractPolytope))

Inherited from [`AbstractCentrallySymmetricPolytope`](@ref):
* [`isempty`](@ref isempty(::AbstractCentrallySymmetricPolytope))
Expand Down
1 change: 1 addition & 0 deletions docs/src/lib/sets/RotatedHyperrectangle.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Inherited from [`LazySet`](@ref):
Inherited from [`AbstractPolytope`](@ref):
* [`isbounded`](@ref isbounded(::AbstractPolytope))
* [`isuniversal`](@ref isuniversal(::AbstractPolytope{N}, ::Bool=false) where {N})
* [`volume`](@ref volume(::AbstractPolytope))

Inherited from [`AbstractCentrallySymmetricPolytope`](@ref):
* [`isempty`](@ref isempty(::AbstractCentrallySymmetricPolytope))
Expand Down
1 change: 1 addition & 0 deletions docs/src/lib/sets/VPolygon.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Inherited from [`AbstractPolytope`](@ref):
* [`isbounded`](@ref isbounded(::AbstractPolytope))
* [`isempty`](@ref isempty(::AbstractPolytope))
* [`isuniversal`](@ref isuniversal(::AbstractPolytope{N}, ::Bool=false) where {N})
* [`volume`](@ref volume(::AbstractPolytope))

Inherited from [`AbstractPolygon`](@ref):
* [`dim`](@ref dim(::AbstractPolygon))
1 change: 1 addition & 0 deletions docs/src/lib/sets/VPolytope.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ Inherited from [`AbstractPolytope`](@ref):
* [`isbounded`](@ref isbounded(::AbstractPolytope))
* [`isempty`](@ref isempty(::AbstractPolytope))
* [`isuniversal`](@ref isuniversal(::AbstractPolytope{N}, ::Bool=false) where {N})
* [`volume`](@ref volume(::AbstractPolytope))
1 change: 1 addition & 0 deletions docs/src/lib/sets/Zonotope.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Inherited from [`LazySet`](@ref):
Inherited from [`AbstractPolytope`](@ref):
* [`isbounded`](@ref isbounded(::AbstractPolytope))
* [`isuniversal`](@ref isuniversal(::AbstractPolytope{N}, ::Bool=false) where {N})
* [`volume`](@ref volume(::AbstractPolytope))

Inherited from [`AbstractCentrallySymmetricPolytope`](@ref):
* [`dim`](@ref dim(::AbstractCentrallySymmetricPolytope))
Expand Down
30 changes: 30 additions & 0 deletions src/Interfaces/AbstractPolytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,33 @@ end
function _vertices_list(P::AbstractPolytope, backend)
return vertices_list(P)
end

"""
volume(P::AbstractPolytope; backend=default_polyhedra_backend(P))
Compute the volume of a polytope.
### Input
- `P` -- polytope
- `backend` -- (optional, default: `default_polyhedra_backend(P)`) the backend
for polyhedral computations; see [Polyhedra's
documentation](https://juliapolyhedra.github.io/) for further
information
### Output
The volume of `P`.
### Algorithm
The volume is computed by the `Polyhedra` library.
"""
function volume(P::AbstractPolytope; backend=nothing)
require(:Polyhedra; fun_name="volume")
if isnothing(backend)
backend = default_polyhedra_backend(P)
end

return Polyhedra.volume(polyhedron(P; backend=backend))
end
4 changes: 4 additions & 0 deletions test/Sets/Polytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ for N in [Float64, Rational{Int}, Float32]
# ρ(::Vector{Float64}, ::HPolytope{Float32})
end
end

# -----
# V-rep
# -----
Expand Down Expand Up @@ -190,6 +191,9 @@ for N in [Float64, Rational{Int}, Float32]
Pe = polyhedron(Vempty, relative_dimension=2)
end

# volume
@test volume(p) == N(1//2)

# translation
@test translate(p, N[1, 2]) == VPolytope([N[1, 2], N[2, 2], N[1, 3]])
pp = copy(p)
Expand Down

0 comments on commit 501c2d2

Please sign in to comment.