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

#3631 - Support VPolygon constructor from SMatrix #3632

Merged
merged 1 commit into from
Aug 8, 2024
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
1 change: 1 addition & 0 deletions src/Initialization/init_StaticArraysCore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ using StaticArraysCore: SMatrix, SVector, MMatrix, MVector

eval(load_genmat_2D_static())
eval(load_reduce_order_static())
eval(load_StaticArraysCore_to_colVector())
2 changes: 1 addition & 1 deletion src/Sets/VPolygon/VPolygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function VPolygon(vertices_matrix::MT; apply_convex_hull::Bool=true,
@assert size(vertices_matrix, 1) == 2 "the number of rows of the matrix " *
"of vertices should be 2, but it is $(size(vertices_matrix, 1))"

vertices = [vertices_matrix[:, j] for j in axes(vertices_matrix, 2)]
vertices = _to_colVector(vertices_matrix)
return VPolygon(vertices; apply_convex_hull=apply_convex_hull,
algorithm=algorithm)
end
2 changes: 1 addition & 1 deletion src/Sets/VPolygon/VPolygonModule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using Reexport, Requires

using ..LazySets: AbstractPolygon, LazySet, AbstractHPolygon, halfspace_left,
is_right_turn, _area_vlist, _intersection_vrep_2d,
_linear_map_vrep, _minkowski_sum_vrep_2d
_linear_map_vrep, _minkowski_sum_vrep_2d, _to_colVector
using ..HPolygonModule: HPolygon
using LinearAlgebra: dot
using Random: AbstractRNG, GLOBAL_RNG, shuffle
Expand Down
9 changes: 9 additions & 0 deletions src/Utils/helper_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,12 @@ function _intersection_line2d end
function _minkowski_sum_hrep_preprocess end
function _minkowski_sum_vrep_2d end
function _minkowski_sum_vrep_nd end

_to_colVector(M::AbstractMatrix) = convert(Vector, [M[:, j] for j in axes(M, 2)])
_to_colVector(M::Matrix) = [M[:, j] for j in axes(M, 2)]

function load_StaticArraysCore_to_colVector()
return quote
_to_colVector(M::SMatrix) = collect(eachcol(M))
end
end # load_StaticArraysCore_to_colVector()
13 changes: 13 additions & 0 deletions test/Sets/Polygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,19 @@ for N in [Float64, Float32, Rational{Int}]
@test area(P) == volume(P) == N(21)
Q = tohrep(P)
@test area(Q) == volume(Q) == N(21)

# Matrix to VPolygon
M = N[0 1 0; 0 0 1]
Vs = [N[0, 0], N[1, 0], N[0, 1]]
P = VPolygon(M)
@test P == VPolygon(Vs)
@test eltype(P.vertices) == eltype(Vs)
# StaticArraysCore.SMatrix to VPolygon
M = @SMatrix N[0 1 0; 0 0 1]
Vs = [@SVector[N(0), N(0)], @SVector[N(1), N(0)], @SVector[N(0), N(1)]]
Q = VPolygon(M)
@test Q == VPolygon(Vs) == P
@test eltype(Q.vertices) == eltype(Vs)
end

for N in [Float64, Float32]
Expand Down
Loading