From 10fbc0671c5214fa9fc0c5ce6d514c6c914c0ef5 Mon Sep 17 00:00:00 2001 From: schillic Date: Wed, 7 Aug 2024 17:48:25 +0200 Subject: [PATCH] support VPolygon constructor from SMatrix --- src/Initialization/init_StaticArraysCore.jl | 1 + src/Sets/VPolygon/VPolygon.jl | 2 +- src/Sets/VPolygon/VPolygonModule.jl | 2 +- src/Utils/helper_functions.jl | 9 +++++++++ test/Sets/Polygon.jl | 13 +++++++++++++ 5 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Initialization/init_StaticArraysCore.jl b/src/Initialization/init_StaticArraysCore.jl index 2565d6e388..0eb46a3810 100644 --- a/src/Initialization/init_StaticArraysCore.jl +++ b/src/Initialization/init_StaticArraysCore.jl @@ -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()) diff --git a/src/Sets/VPolygon/VPolygon.jl b/src/Sets/VPolygon/VPolygon.jl index fae610cc68..d6d6879947 100644 --- a/src/Sets/VPolygon/VPolygon.jl +++ b/src/Sets/VPolygon/VPolygon.jl @@ -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 diff --git a/src/Sets/VPolygon/VPolygonModule.jl b/src/Sets/VPolygon/VPolygonModule.jl index 4ac8645ebc..6292f5e708 100644 --- a/src/Sets/VPolygon/VPolygonModule.jl +++ b/src/Sets/VPolygon/VPolygonModule.jl @@ -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 diff --git a/src/Utils/helper_functions.jl b/src/Utils/helper_functions.jl index 774ada4871..fa7a05b90f 100644 --- a/src/Utils/helper_functions.jl +++ b/src/Utils/helper_functions.jl @@ -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() diff --git a/test/Sets/Polygon.jl b/test/Sets/Polygon.jl index 35fbfd2767..124475b558 100644 --- a/test/Sets/Polygon.jl +++ b/test/Sets/Polygon.jl @@ -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]