From eb480ccbd0ba6a2d5f5c027764cd6e82efcee850 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Thu, 28 Nov 2024 09:35:07 -0300 Subject: [PATCH] Update '_pboxes' function --- src/boundingboxes.jl | 44 ++++++++++++++++++++++++++++++++----------- test/boundingboxes.jl | 16 ++++++++-------- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/boundingboxes.jl b/src/boundingboxes.jl index 332a742e6..b01f6bedd 100644 --- a/src/boundingboxes.jl +++ b/src/boundingboxes.jl @@ -88,20 +88,42 @@ _bboxes(boxes) = _pboxes(point for box in boxes for point in extrema(box)) _pboxes(points) = _pboxes(manifold(first(points)), points) -function _pboxes(::Type{𝔼{N}}, points) where {N} - p = first(points) - ℒ = lentype(p) - cmin = fill(typemax(ℒ), N) - cmax = fill(typemin(ℒ), N) +@generated function _pboxes(::Type{𝔼{N}}, points) where {N} + minvars = ntuple(i -> Symbol(:cmin, i), N) + maxvars = ntuple(i -> Symbol(:cmax, i), N) - for p in points - c = CoordRefSystems.values(convert(Cartesian, coords(p))) - for i in 1:N - cmin[i] = min(c[i], cmin[i]) - cmax[i] = max(c[i], cmax[i]) + mininit = ntuple(N) do i + minvar = minvars[i] + :($minvar = typemax(ℒ)) + end + maxinit = ntuple(N) do i + maxvar = maxvars[i] + :($maxvar = typemin(ℒ)) + end + + minupdate = ntuple(N) do i + minvar = minvars[i] + :($minvar = min(c[$i], $minvar)) + end + maxupdate = ntuple(N) do i + maxvar = maxvars[i] + :($maxvar = max(c[$i], $maxvar)) + end + + quote + p = first(points) + ℒ = lentype(p) + $(mininit...) + $(maxinit...) + + for p in points + c = CoordRefSystems.values(convert(Cartesian, coords(p))) + $(minupdate...) + $(maxupdate...) end + + Box(withcrs(p, ($(minvars...),)), withcrs(p, ($(maxvars...),))) end - Box(withcrs(p, Tuple(cmin)), withcrs(p, Tuple(cmax))) end function _pboxes(::Type{🌐}, points) diff --git a/test/boundingboxes.jl b/test/boundingboxes.jl index a0e03571e..7e42c01d9 100644 --- a/test/boundingboxes.jl +++ b/test/boundingboxes.jl @@ -60,8 +60,8 @@ @test @allocated(boundingbox(m)) < 4100 @test @allocated(boundingbox(d)) < 4100 else - @test @allocated(boundingbox(m)) < 3200 - @test @allocated(boundingbox(d)) < 3200 + @test @allocated(boundingbox(m)) < 2700 + @test @allocated(boundingbox(d)) < 2700 end b1 = Box(cart(0, 0), cart(1, 1)) @@ -70,15 +70,15 @@ d = GeometrySet([b1, b2]) @test boundingbox(m) == Box(cart(-1, -1), cart(1, 1)) @test boundingbox(d) == Box(cart(-1, -1), cart(1, 1)) - @test @allocated(boundingbox(m)) < 500 - @test @allocated(boundingbox(d)) < 500 + @test @allocated(boundingbox(m)) < 50 + @test @allocated(boundingbox(d)) < 50 d = PointSet(cart(0, 0), cart(1, 2), cart(2, 1)) @test boundingbox(d) == Box(cart(0, 0), cart(2, 2)) - @test @allocated(boundingbox(d)) < 500 + @test @allocated(boundingbox(d)) < 50 d = PointSet(cart(1, 2), cart(2, 1)) @test boundingbox(d) == Box(cart(1, 1), cart(2, 2)) - @test @allocated(boundingbox(d)) < 500 + @test @allocated(boundingbox(d)) < 50 d = cartgrid(10, 10) @test boundingbox(d) == Box(cart(0, 0), cart(10, 10)) @@ -93,7 +93,7 @@ d = PointSet(cart(0, 0), cart(1, 2), cart(2, 1)) v = view(d, 1:2) @test boundingbox(v) == Box(cart(0, 0), cart(1, 2)) - @test @allocated(boundingbox(v)) < 500 + @test @allocated(boundingbox(v)) < 50 d = cartgrid(10, 10) v = view(d, 1:2) @@ -119,7 +119,7 @@ g = cartgrid(10, 10) m = convert(SimpleMesh, g) @test boundingbox(m) == Box(cart(0, 0), cart(10, 10)) - @test @allocated(boundingbox(m)) < 500 + @test @allocated(boundingbox(m)) < 50 p = ParaboloidSurface(cart(1, 2, 3), T(5), T(4)) @test boundingbox(p) ≈ Box(cart(-4, -3, 3), cart(6, 7, 73 / 16))