Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bounding Box Prototyping #3540

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MakieCore/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Simon Danisch"]
version = "0.7.2"

[deps]
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
Observables = "510215fc-4207-5dde-b226-833fc4488ee2"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

Expand Down
48 changes: 48 additions & 0 deletions MakieCore/src/LazyObservable.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
LazyObservable{T}(update::Function)
LazyObservable(update::Function, init::T)

Creates a lazy observables which updates using the passed `update` Function when
its value is requested by `lazyobs[]`.

Notes:
- `on()`, `map()` etc do not pass the value of the LazyObservable to the
attached function. Instead they pass the LazyObservable itself, so that you
need to explicitly call `lazyobs[]` to trigger an update.

TODO: Should this just update and have something like
`no_update(l::LazyObserbale) = l.up_to_date` for listening without updates?
"""
mutable struct LazyObservable{T}
updater::Function
up_to_date::Observable{Bool}
val::T

LazyObservable{T}(f::Function) where T = new{T}(f, Observable{Bool}(false))
LazyObservable(f::Function, init::T) where T = new{T}(f, Observable{Bool}(false), init)
end

function update!(l::LazyObservable{T}) where T
if !l.up_to_date[]
l.val = l.updater()::T
l.up_to_date.val = true
end
return l.val
end

function Observables.notify(l::LazyObservable)
l.up_to_date[] = false
return
end

Base.getindex(l::LazyObservable) = update!(l)
Observables.listeners(l::LazyObservable) = listeners(l.up_to_date)

function Observables.on(@nospecialize(f), l::LazyObservable; kwargs...)
return on(l.up_to_date; kwargs...) do state
state || f(l) # TODO: should this update? e.g. use l[]?
return
end
end

Observables.off(l::LazyObservable, @nospecialize(f)) = off(l.up_to_date, f)
3 changes: 2 additions & 1 deletion MakieCore/src/MakieCore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ end

using Observables
using Observables: to_value
using GeometryBasics
using Base: RefValue
# Needing REPL for Base.Docs.doc on julia
# https://github.com/MakieOrg/Makie.jl/issues/3276
using REPL


include("LazyObservable.jl")
include("types.jl")
include("attributes.jl")
include("recipes.jl")
Expand Down
24 changes: 22 additions & 2 deletions MakieCore/src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ struct Attributes
attributes::Dict{Symbol, Observable}
end

# If a bounding box does not extend in a dimension it's bounding box is NaN in
# that dimension. TODO: how does this combine with e.g. vlines?
struct LazyBoundingBox
# parent::SceneLike TODO: probably unneccessary?
cache::Dict{Symbol, LazyObservable} # e.g. marker bounding box
# space => lazy bbox, only considering marker sizes if it's easy
fast_bbox::Dict{Symbol, LazyObservable{Rect3f}}
# space => lazy bbox, always considering marker sizes
full_bbox::Dict{Symbol, LazyObservable{Rect3f}}
end

function LazyBoundingBox()
return LazyBoundingBox(
Dict{Symbol, LazyObservable}(),
Dict{Symbol, LazyObservable{Rect3f}}(),
Dict{Symbol, LazyObservable{Rect3f}}()
)
end

"""
Plot{PlotFunc}(args::Tuple, kw::Dict{Symbol, Any})

Expand All @@ -65,6 +84,7 @@ Scatter((1:4,), Dict{Symbol, Any}(:color => :red))
"""
mutable struct Plot{PlotFunc, T} <: ScenePlot{PlotFunc}
transformation::Union{Nothing, Transformable}
bbox::LazyBoundingBox

# Unprocessed arguments directly from the user command e.g. `plot(args...; kw...)``
kw::Dict{Symbol,Any}
Expand All @@ -79,8 +99,8 @@ mutable struct Plot{PlotFunc, T} <: ScenePlot{PlotFunc}
parent::Union{AbstractScene,Plot}

function Plot{Typ,T}(kw::Dict{Symbol, Any}, args::Vector{Any}, converted::NTuple{N, Observable}) where {Typ,T,N}
return new{Typ,T}(nothing, kw, args, converted, Attributes(), Plot[],
Observables.ObserverFunction[])
return new{Typ,T}(nothing, LazyBoundingBox(), kw, args, converted,
Attributes(), Plot[], Observables.ObserverFunction[])
end
end

Expand Down
1 change: 1 addition & 0 deletions src/Makie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import MakieCore: create_axis_like, create_axis_like!, figurelike_return, figure
import MakieCore: arrows, heatmap, image, lines, linesegments, mesh, meshscatter, poly, scatter, surface, text, volume
import MakieCore: arrows!, heatmap!, image!, lines!, linesegments!, mesh!, meshscatter!, poly!, scatter!, surface!, text!, volume!
import MakieCore: convert_arguments, convert_attribute, default_theme, conversion_trait
import MakieCore: LazyObservable, LazyBoundingBox

export @L_str, @colorant_str
export ConversionTrait, NoConversion, PointBased, GridBased, VertexGrid, CellGrid, ImageLike, VolumeLike
Expand Down
49 changes: 49 additions & 0 deletions src/layouting/BoundingBox.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function init(parent::SceneLike)
for space in (:data, :transformed, :pixel, :relative, :clip) # TODO: use spaces() once it has :transformed
bb.fast_bbox[space] = LazyObservable(_ -> calculate_fast_bbox(parent, space), Rect3f())
bb.fast_bbox[space] = LazyObservable(_ -> calculate_full_bbox(parent, space), Rect3f())
end
# TODO: attach notifiers:
# Plot:
# - all have onany(plot.converted)
# - :transformed, ... on(transform_func)
# - :pixel, ... on(camera.projectionview)
# Scene:
# - on(plot.bbox) per space for every child
return
end

boundingbox(obj::SceneLike; space = :data, mode = :full) = boundingbox(obj, space, mode)
function boundingbox(obj::SceneLike, space = :data, mode = :full)
if mode == :fast
return obj.fast_bbox[space][]
elseif mode == :full
return obj.full_bbox[space][]
else
throw(ArgumentError("Mode $mode not recognized."))
end
end

data_limits(obj::SceneLike) = boundingbox(obj, :data, :fast)

# TODO: maybe also:
# boundingbox(obj::SceneLike; space = :data, mode = :full) = boundingbox(obj, space, mode)
# function boundingbox(obj::SceneLike, space = :data, mode = :full)
# # use obj.cache[:marker_bbox] or something like this?
# end

function calculate_fast_bbox(plot::Combined, space::Symbol)
# TODO: what exactly do we do with this?
# - :data bbox from plot, other from :data bbox (this would generally make bbox less tight)
# - bbox from transformed plot data (more expensive)
# - should we include marker bboxes if they are simple to include (e.g. linear transform in 2D?)
# - use only max marker bbox to reduce cost?

end

function calculate_full_bbox(plot::Combined, space::Symbol)
# TODO
# include everything we reasonably can:
# - transform data
# - with markers, transform data to markerspace, add marker bbox, transform to target
end
Loading