JuliaReach/LazySets.jl

convex_hull of two 1D points modifies points in-place

schillic opened this issue · 0 comments

_two_points_1d! modifies points in-place. This does not work if the vertices have type SVector. It is generally better to sort the list of points instead of modifying the points themselves.


Background

convex_hull of non-convex sets (wrt. type information) calls vertices_list, copies the vertices, and, in the 1D and 2D case, sorts them in-place. However, if a set returns vertices of type SVector (e.g., HPolytope if the Polyhedra backend is used), the in-place sorting fails (see the example below).

The easy fix for this example is to let vertices_list of a 1D HPolytope return normal Vectors (this is already done in the 2D case). The general fix is to not modify in-place if the vertex type does not support it.

julia> U = UnionSetArray([convert(HPolytope, Interval(1, 2))]);

julia> vertices_list(U)
2-element Vector{StaticArraysCore.SVector{1, Float64}}:
 [2.0]
 [1.0]

julia> convex_hull(U)
ERROR: setindex!(::StaticArraysCore.SVector{1, Float64}, value, ::Int) is not defined.
 Hint: Use `MArray` or `SizedArray` to create a mutable static array
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] setindex!(a::StaticArraysCore.SVector{1, Float64}, value::Float64, i::Int64)
    @ StaticArrays ~/.julia/packages/StaticArrays/PLKkM/src/indexing.jl:3
  [3] _two_points_1d!(points::Vector{Vector{Float64}})
    @ LazySets ~/.julia/dev/LazySets/src/ConcreteOperations/convex_hull.jl:188 [inlined]
  [4] convex_hull!(points::Vector{StaticArraysCore.SVector{…}}; algorithm::Nothing, backend::Nothing, solver::Nothing)
    @ LazySets ~/.julia/dev/LazySets/src/ConcreteOperations/convex_hull.jl:151
  [5] convex_hull!
    @ LazySets ~/.julia/dev/LazySets/src/ConcreteOperations/convex_hull.jl:137 [inlined]
  [6] #convex_hull#438
    @ LazySets ~/.julia/dev/LazySets/src/ConcreteOperations/convex_hull.jl:133 [inlined]
  [7] convex_hull(points::Vector{StaticArraysCore.SVector{1, Float64}})
    @ LazySets ~/.julia/dev/LazySets/src/ConcreteOperations/convex_hull.jl:131
  [8] _convex_hull_polytopes(X::UnionSetArray{Float64, HPolytope{…}}; kwargs::@Kwargs{})
    @ LazySets ~/.julia/dev/LazySets/src/Interfaces/LazySet.jl:434
  [9] _convex_hull_polytopes
    @ LazySets ~/.julia/dev/LazySets/src/Interfaces/LazySet.jl:433 [inlined]
 [10] #convex_hull#35
    @ LazySets ~/.julia/dev/LazySets/src/Interfaces/LazySet.jl:430 [inlined]
 [11] convex_hull(X::UnionSetArray{Float64, HPolytope{Float64, ReachabilityBase.Arrays.SingleEntryVector{Float64}}})
    @ LazySets ~/.julia/dev/LazySets/src/Interfaces/LazySet.jl:426
 [12] top-level scope
    @ REPL[33]:1