diff --git a/Project.toml b/Project.toml index 7d3903dc..c1984172 100644 --- a/Project.toml +++ b/Project.toml @@ -14,9 +14,11 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" [weakdeps] GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f" +IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953" [extensions] GeometryBasicsGeoInterfaceExt = "GeoInterface" +IntervalSetsExt = "IntervalSets" [compat] Aqua = "0.8" @@ -24,6 +26,7 @@ EarCut_jll = "2" Extents = "0.1" GeoInterface = "1.0.1" GeoJSON = "0.7, 0.8" +IntervalSets = "0.7" IterTools = "1.3.0" LinearAlgebra = "<0.0.1,1" OffsetArrays = "1" @@ -35,11 +38,12 @@ julia = "1.10" [extras] Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" -GeoJSON = "61d90e0f-e114-555e-ac52-39dfb47a3ef9" GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f" +GeoJSON = "61d90e0f-e114-555e-ac52-39dfb47a3ef9" +IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953" OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Aqua", "GeoInterface", "GeoJSON", "OffsetArrays", "Random", "Test"] +test = ["Aqua", "GeoInterface", "GeoJSON", "IntervalSets", "OffsetArrays", "Random", "Test"] diff --git a/ext/IntervalSetsExt.jl b/ext/IntervalSetsExt.jl new file mode 100644 index 00000000..06bfbd3e --- /dev/null +++ b/ext/IntervalSetsExt.jl @@ -0,0 +1,16 @@ +module IntervalSetsExt + +using IntervalSets +using GeometryBasics + +GeometryBasics.HyperRectangle(ints::Vararg{ClosedInterval, N}) where {N} = HyperRectangle{N}(ints...) +GeometryBasics.HyperRectangle{N}(ints::Vararg{ClosedInterval, N}) where {N} = HyperRectangle{N}( + Vec(leftendpoint.(ints)), + Vec(rightendpoint.(ints) .- leftendpoint.(ints)) +) +GeometryBasics.HyperRectangle{N,T}(ints::Vararg{ClosedInterval, N}) where {N,T} = HyperRectangle{N,T}( + Vec(leftendpoint.(ints)), + Vec(rightendpoint.(ints) .- leftendpoint.(ints)) +) + +end diff --git a/test/geometrytypes.jl b/test/geometrytypes.jl index bab22981..cb68c9e2 100644 --- a/test/geometrytypes.jl +++ b/test/geometrytypes.jl @@ -1,4 +1,5 @@ using Test, GeometryBasics +using IntervalSets: (..) @testset "Cylinder" begin @testset "constructors" begin @@ -180,6 +181,20 @@ end @test constructor(m) ≈ Rect3f(-1, -1, -1, 2, 2, 2) end end + + @testset "From intervals" begin + # 1D interval + @test HyperRectangle(1..3) == Rect{1, Float64}(Point(1.0), Vec(2.0)) + # 2D intervals + @test HyperRectangle(0..2, -1..1) == Rect{2, Float64}(Point(0.0, -1.0), Vec(2.0, 2.0)) + # different interval eltypes + @test HyperRectangle(0.0..2.0, -1..1) == Rect{2, Float64}(Point(0.0, -1.0), Vec(2.0, 2.0)) + # N-typed constructor + @test HyperRectangle{2}(0..2, -1..1) == HyperRectangle(0..2, -1..1) + # Rect constructor: + @test Rect(1..3, 4..5) == Rect{2, Float64}(Point(1.0, 4.0), Vec(2.0, 1.0)) + @test Rect{2, Float64}(1..3, 4..5) == Rect{2, Float64}(Point(1.0, 4.0), Vec(2.0, 1.0)) + end end # TODO: consider deprecating this