Bounding Boxes
You can generate an axis aligned bounding box for any AbstractGeometry
by calling Rect(geom)
. Depending on the object this will either rely on coordinates(geom)
or a specialized method. You can also create a bounding box of set dimension or type by adding the related parameters.
julia> using GeometryBasics
julia> s = Circle(Point2f(0), 1f0)
Circle{Float32}(Float32[0.0, 0.0], 1.0f0)
julia> Rect(s) # specialized, exact bounding box
HyperRectangle{2, Float32}(Float32[-1.0, -1.0], Float32[2.0, 2.0])
julia> Rect3(s)
HyperRectangle{3, Float32}(Float32[-1.0, -1.0, 0.0], Float32[2.0, 2.0, 0.0])
julia> Rect3d(s)
HyperRectangle{3, Float64}([-1.0, -1.0, 0.0], [2.0, 2.0, 0.0])
julia> RectT{Float64}(s)
HyperRectangle{2, Float64}([-1.0, -1.0], [2.0, 2.0])
julia> Rect(GeometryBasics.mesh(s)) # using generated coordinates in mesh
HyperRectangle{2, Float32}(Float32[-0.99875695, -0.99968916], Float32[1.9987569, 1.9993783])
Extending
If you want to add a specialized bounding box method you should implement Rect{N, T}(geom) = ...
. All other methods funnel into that one, defaulting to the same N, T
that the given AbstractGeometry{N, T}
has. GeometryBasics allows the user given dimension N
to be smaller or equal to that of the geometry. This is checked with GeometryBasics.bbox_dim_check(user_dim, geom_dim)
which you may reuse.
function Rect{N, T}(a::HyperSphere{N2}) where {N, N2, T}
GeometryBasics.bbox_dim_check(N, N2)
return Rect{N, T}(minimum(a), widths(a))
end