diff --git a/Project.toml b/Project.toml index 776239f..8ff0ae2 100644 --- a/Project.toml +++ b/Project.toml @@ -1,12 +1,22 @@ name = "IntervalBoxes" uuid = "43d83c95-ebbb-40ec-8188-24586a1458ed" -version = "0.3.0" +version = "0.3.1" [deps] IntervalArithmetic = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" +[weakdeps] +Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" +RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01" + +[extensions] +IntervalBoxesMakieExt = "Makie" +IntervalBoxesPlotsExt = "RecipesBase" + [compat] IntervalArithmetic = "1" +Makie = "0.21, 0.22" +RecipesBase = "1" StaticArrays = "1" julia = "1.10" diff --git a/ext/IntervalBoxesMakieExt.jl b/ext/IntervalBoxesMakieExt.jl new file mode 100644 index 0000000..8f65da8 --- /dev/null +++ b/ext/IntervalBoxesMakieExt.jl @@ -0,0 +1,42 @@ +module IntervalBoxesMakieExt + +using IntervalBoxes: IntervalBox +using Makie +using IntervalArithmetic: inf, diam + +function _to_rect(b::IntervalBox{2}) + Rect2f(inf(b[1]), inf(b[2]), diam(b[1]), diam(b[2])) +end + +@recipe(PlotIntervalBox, intervalboxes) do scene + Attributes( + color = (:blue, 0.3), + strokecolor = :black, + strokewidth = 1, + ) +end + +function Makie.plot!(p::PlotIntervalBox{<:Tuple{<:IntervalBox{2}}}) + box = p.intervalboxes + rect = @lift [_to_rect($box)] + poly!(p, rect; color=p.color, strokecolor=p.strokecolor, strokewidth=p.strokewidth) + return p +end + +function Makie.plot!(p::PlotIntervalBox{<:Tuple{AbstractVector{<:IntervalBox{2}}}}) + boxes = p.intervalboxes + rects = @lift _to_rect.($boxes) + poly!(p, rects; color=p.color, strokecolor=p.strokecolor, strokewidth=p.strokewidth) + return p +end + +# Also support poly() directly +function Makie.convert_arguments(P::Type{<:Poly}, box::IntervalBox{2}) + return Makie.convert_arguments(P, [_to_rect(box)]) +end + +function Makie.convert_arguments(P::Type{<:Poly}, boxes::AbstractVector{<:IntervalBox{2}}) + return Makie.convert_arguments(P, _to_rect.(boxes)) +end + +end diff --git a/ext/IntervalBoxesPlotsExt.jl b/ext/IntervalBoxesPlotsExt.jl new file mode 100644 index 0000000..304c02a --- /dev/null +++ b/ext/IntervalBoxesPlotsExt.jl @@ -0,0 +1,27 @@ +module IntervalBoxesPlotsExt + +using IntervalBoxes: IntervalBox +using RecipesBase +using IntervalArithmetic: inf, sup + +@recipe function f(box::IntervalBox{2}) + seriestype := :shape + legend --> false + fillalpha --> 0.3 + linecolor --> :black + + x_lo, x_hi = inf(box[1]), sup(box[1]) + y_lo, y_hi = inf(box[2]), sup(box[2]) + + [x_lo, x_hi, x_hi, x_lo], [y_lo, y_lo, y_hi, y_hi] +end + +@recipe function f(boxes::AbstractVector{<:IntervalBox{2}}) + for box in boxes + @series begin + box + end + end +end + +end diff --git a/src/IntervalBoxes.jl b/src/IntervalBoxes.jl index b1971ab..a8e61ae 100644 --- a/src/IntervalBoxes.jl +++ b/src/IntervalBoxes.jl @@ -10,7 +10,7 @@ import IntervalArithmetic: import IntervalArithmetic.Symbols: ⊓, ⊔ import Base: - ⊆, +, -, *, /, ==, !=, eltype, length, size, getindex, setindex, iterate, + ⊆, ∈, +, -, *, /, ==, !=, eltype, length, size, getindex, setindex, iterate, broadcasted, setdiff, big, isempty, zero export IntervalBox diff --git a/src/arithmetic.jl b/src/arithmetic.jl index c7104e4..fdd1c02 100644 --- a/src/arithmetic.jl +++ b/src/arithmetic.jl @@ -14,7 +14,7 @@ /(a::IntervalBox, b::Real) = IntervalBox( a.v ./ b ) -Base.:∈(v::AbstractVector, X::IntervalBox) = all(in_interval.(v, X)) +∈(v::AbstractVector, X::IntervalBox) = all(in_interval.(v, X)) ⊓(X::IntervalBox, Y::IntervalBox) = IntervalBox( X.v .⊓ Y.v ) ⊔(X::IntervalBox, Y::IntervalBox) = IntervalBox( X.v .⊔ Y.v ) diff --git a/src/intervalbox.jl b/src/intervalbox.jl index 15b2804..28c44a5 100644 --- a/src/intervalbox.jl +++ b/src/intervalbox.jl @@ -87,7 +87,7 @@ big(X::IntervalBox) = big.(X) ⊔(X::IntervalBox{N}, Y::IntervalBox{N}) where {N} = IntervalBox(hull.(X.v, Y.v)) -∈(X::AbstractVector, Y::IntervalBox{N,T}) where {N,T} = all(in_interval.(X, Y)) +∈(X::IntervalType, Y::IntervalBox{N,T}) where {N,T} = throw(ArgumentError("$X ∈ $Y is not defined")) ∈(X, Y::IntervalBox{N,T}) where {N,T} = throw(ArgumentError("$X ∈ $Y is not defined")) # mixing intervals with one-dimensional interval boxes