Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Sep 5, 2023
1 parent 5ef7c0f commit a35d51d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/basic_recipes/contours.jl
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,12 @@ function plot!(plot::T) where T <: Union{Contour, Contour3d}
end

bboxes = lift(labels, texts.text; ignore_equal_values=true) do labels, _
labels || return
return broadcast(texts.plots[1][1].val, texts.positions.val, texts.rotation.val) do gc, pt, rot
labels || return Rect2f()
broadcast(texts.plots[1][1].val, texts.positions.val, texts.rotation.val) do gc, pt, rot
# drop the depth component of the bounding box for 3D
any(isnan, pt) && return Rect2f()
px_pos = project(scene, apply_transform(transform_func(plot), pt, space))
bb = unsafe_boundingbox(gc, to_ndim(Point3f, px_pos, 0f0), to_rotation(rot))
bb = unchecked_boundingbox(gc, to_ndim(Point3f, px_pos, 0f0), to_rotation(rot))
isfinite_rect(bb) || return Rect2f()
Rect2f(bb)
end
Expand Down
37 changes: 16 additions & 21 deletions src/layouting/boundingbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,11 @@ end

_inkboundingbox(ext::GlyphExtent) = ext.ink_bounding_box

function unsafe_boundingbox(glyphcollection::GlyphCollection, position::Point3f, rotation::Quaternion)
return unsafe_boundingbox(glyphcollection, rotation) + position
end
unchecked_boundingbox(glyphcollection::GlyphCollection, position::Point3f, rotation::Quaternion) =
unchecked_boundingbox(glyphcollection, rotation) + position

function unsafe_boundingbox(glyphcollection::GlyphCollection, rotation::Quaternion)
if isempty(glyphcollection.glyphs)
return Rect3f(Point3f(0), Vec3f(0))
end
function unchecked_boundingbox(glyphcollection::GlyphCollection, rotation::Quaternion)
isempty(glyphcollection.glyphs) && return Rect3f(Point3f(0), Vec3f(0))

glyphorigins = glyphcollection.origins
glyphbbs = gl_bboxes(glyphcollection)
Expand All @@ -72,24 +69,22 @@ function unsafe_boundingbox(glyphcollection::GlyphCollection, rotation::Quaterni
return bb
end

function unsafe_boundingbox(layouts::AbstractArray{<:GlyphCollection}, positions, rotations)
if isempty(layouts)
return Rect3f((0, 0, 0), (0, 0, 0))
else
bb = Rect3f()
broadcast_foreach(layouts, positions, rotations) do layout, pos, rot
if !isfinite_rect(bb)
bb = boundingbox(layout, pos, rot)
else
bb = union(bb, boundingbox(layout, pos, rot))
end
function unchecked_boundingbox(layouts::AbstractArray{<:GlyphCollection}, positions, rotations)
isempty(layouts) && return Rect3f((0, 0, 0), (0, 0, 0))

bb = Rect3f()
broadcast_foreach(layouts, positions, rotations) do layout, pos, rot
if !isfinite_rect(bb)
bb = boundingbox(layout, pos, rot)
else
bb = union(bb, boundingbox(layout, pos, rot))
end
return bb
end
return bb
end

function boundingbox(args...)
bb = unsafe_boundingbox(args...)
function boundingbox(x::Union{GlyphCollection,AbstractArray{<:GlyphCollection}}, args...)
bb = unchecked_boundingbox(x, args...)
isfinite_rect(bb) || error("Invalid text boundingbox")
bb
end
Expand Down
8 changes: 7 additions & 1 deletion test/boundingboxes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,10 @@
bb = boundingbox(p)
@test bb.origin Point3f(340, 341, 0)
@test bb.widths Vec3f(32.24, 23.3, 0)
end
end

@testset "invalid contour bounding box" begin
a = b = 1:3
c = [0 1 2;1 2 3;4 5 NaN]
contour(a, b, c; levels = collect(1:3), labels = true)
end

0 comments on commit a35d51d

Please sign in to comment.