-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathinterfaces.jl
419 lines (369 loc) · 14.8 KB
/
interfaces.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
function add_cycle_attribute!(plot::Plot, scene::Scene, cycle=get_cycle_for_plottype(plot.cycle[]))
cycler = scene.cycler
palette = scene.theme.palette
add_cycle_attributes!(plot, cycle, cycler, palette)
return
end
function color_and_colormap!(plot, colors = plot.color)
scene = parent_scene(plot)
if !isnothing(scene) && haskey(plot, :cycle)
add_cycle_attribute!(plot, scene)
end
colors = assemble_colors(colors[], colors, plot)
attributes(plot.attributes)[:calculated_colors] = colors
end
function calculated_attributes!(::Type{<: AbstractPlot}, plot)
scene = parent_scene(plot)
if !isnothing(scene) && haskey(plot, :cycle)
add_cycle_attribute!(plot, scene)
end
end
function calculated_attributes!(::Type{<: Mesh}, plot)
mesha = lift(GeometryBasics.attributes, plot, plot.mesh)
color = haskey(mesha[], :color) ? lift(x-> x[:color], plot, mesha) : plot.color
color_and_colormap!(plot, color)
return
end
function calculated_attributes!(::Type{<: Union{Heatmap, Image}}, plot)
color_and_colormap!(plot, plot[3])
end
function calculated_attributes!(::Type{<: Surface}, plot)
colors = plot[3]
if haskey(plot, :color)
color = plot[:color][]
if isa(color, AbstractMatrix) && !(color === to_value(colors))
colors = plot[:color]
end
end
color_and_colormap!(plot, colors)
end
function calculated_attributes!(::Type{<: MeshScatter}, plot)
color_and_colormap!(plot)
return
end
function calculated_attributes!(::Type{<:Volume}, plot)
color_and_colormap!(plot, plot[4])
return
end
function calculated_attributes!(::Type{<:Text}, plot)
color_and_colormap!(plot)
return
end
function calculated_attributes!(::Type{<: Scatter}, plot)
# calculate base case
color_and_colormap!(plot)
replace_automatic!(plot, :marker_offset) do
# default to middle
return lift(plot, plot[:markersize]) do msize
return to_2d_scale(map(x -> x .* -0.5f0, msize))
end
end
replace_automatic!(plot, :markerspace) do
lift(plot, plot.markersize) do ms
if ms isa Pixel || (ms isa AbstractVector && all(x-> ms isa Pixel, ms))
return :pixel
else
return :data
end
end
end
end
function calculated_attributes!(::Type{T}, plot) where {T<:Union{Lines, LineSegments}}
pos = plot[1][]
# extend one color/linewidth per linesegment to be one (the same) color/linewidth per vertex
if T <: LineSegments
for attr in [:color, :linewidth]
# taken from @edljk in PR #77
if haskey(plot, attr) && isa(plot[attr][], AbstractVector) && (length(pos) ÷ 2) == length(plot[attr][])
# TODO, this is actually buggy for `plot.color = new_colors`, since we're overwriting the origin color input
attributes(plot.attributes)[attr] = lift(plot, plot[attr]) do cols
map(i -> cols[(i + 1) ÷ 2], 1:(length(cols) * 2))
end
end
end
end
color_and_colormap!(plot)
return
end
const atomic_functions = (
text, meshscatter, scatter, mesh, linesegments,
lines, surface, volume, heatmap, image, voxels
)
const Atomic{Arg} = Union{map(x-> Plot{x, Arg}, atomic_functions)...}
function get_kw_values(func, names, kw)
return NamedTuple([Pair{Symbol,Any}(k, func(kw[k]))
for k in names if haskey(kw, k)])
end
function get_kw_obs(names, kw)
isempty(names) && return Observable((;))
kw_copy = copy(kw)
init = get_kw_values(to_value, names, kw_copy)
obs = Observable(init; ignore_equal_values=true)
in_obs = [kw_copy[k] for k in names if haskey(kw_copy, k)]
onany(in_obs...) do args...
obs[] = get_kw_values(to_value, names, kw_copy)
return
end
return obs
end
"""
expand_dimensions(plottrait, plotargs...)
Expands the dims for e.g. `scatter(1:4)` becoming `scatter(1:4, 1:4)` for 2D plots.
We're separating this state from convert_arguments, to better apply `dim_converts` before convert_arguments.
"""
expand_dimensions(trait, args...) = nothing
expand_dimensions(::PointBased, y::VecTypes) = nothing # VecTypes are nd points
expand_dimensions(::PointBased, y::RealVector) = (keys(y), y)
function expand_dimensions(::Union{ImageLike, GridBased}, data::AbstractMatrix{<:Union{<:Real, <:Colorant}})
# Float32, because all ploteable sizes should fit into float32
x, y = map(x-> (0f0, Float32(x)), size(data))
return (x, y, data)
end
function expand_dimensions(::Union{CellGrid, VertexGrid}, data::AbstractMatrix{<:Union{<:Real,<:Colorant}})
x, y = map(x-> (1f0, Float32(x)), size(data))
return (x, y, data)
end
function expand_dimensions(::VolumeLike, data::RealArray{3})
x, y, z = map(x-> (0f0, Float32(x)), size(data))
return (x, y, z, data)
end
function apply_expand_dimensions(trait, args, args_obs, deregister)
expanded = expand_dimensions(trait, args...)
if isnothing(expanded)
return args_obs
else
new_obs = map(Observable{Any}, expanded)
fs = onany(args_obs...) do args...
expanded = expand_dimensions(trait, args...)
for (obs, arg) in zip(new_obs, expanded)
obs.val = arg
end
foreach(notify, new_obs)
return
end
append!(deregister, fs)
return new_obs
end
end
# Internal function to apply convert_arguments to observable arguments
function convert_observable_args(P, args_obs, kw_obs, converted, deregister)
# Fully converted arguments to target type for Plot
new_args_obs = map(Observable, converted)
fs = onany(kw_obs, args_obs...) do kw, args...
conv = convert_arguments(P, args...; kw...)
if !(conv isa Tuple)
conv = (conv,) # for PlotSpec
end
for (obs, arg) in zip(new_args_obs, conv)
obs.val = arg
end
foreach(notify, new_args_obs)
return
end
append!(deregister, fs)
return new_args_obs
end
function got_converted(P::Type, PTrait::ConversionTrait, result)
if result isa Union{PlotSpec,BlockSpec,GridLayoutSpec,AbstractVector{PlotSpec}}
return SpecApi
end
types = MakieCore.types_for_plot_arguments(P, PTrait)
if !isnothing(types)
return result isa types
end
return nothing
end
"""
conversion_pipeline(P::Type{<:Plot}, used_attrs::Tuple, args::Tuple,
args_obs::Tuple, user_attributes::Dict{Symbol, Any}, deregister, recursion=1)
The main conversion pipeline for converting arguments for a plot type.
Applies dim_converts, expand_dimensions (in `try_dim_convert`), convert_arguments and checks if the conversion was successful.
"""
function conversion_pipeline(
P::Type{<:Plot}, used_attrs::Tuple, args::Tuple,
args_obs::Tuple, user_attributes::Dict{Symbol, Any}, deregister, recursion=1)
if recursion === 3
error("Recursion limit reached. This should not happen, please open an issue with Makie.jl and provide a minimal working example.")
return P, args_obs
end
kw_obs = get_kw_obs(used_attrs, user_attributes)
kw = to_value(kw_obs)
PTrait = conversion_trait(P, args...)
dim_converted = try_dim_convert(P, PTrait, user_attributes, args_obs, deregister)
args = map(to_value, dim_converted)
converted = convert_arguments(P, args...; kw...)
status = got_converted(P, PTrait, converted)
if status === true
# We're done converting!
return convert_observable_args(P, dim_converted, kw_obs, converted, deregister)
elseif status === SpecApi
return convert_observable_args(P, dim_converted, kw_obs, (converted,), deregister)
elseif status === false && recursion === 1
# We haven't reached a target type, so we try to apply convert arguments again and try_dim_convert
# This is the case for e.g. convert_arguments returning types that need dim_convert
new_args_obs = convert_observable_args(P, dim_converted, kw_obs, converted, deregister)
return conversion_pipeline(P, used_attrs, map(to_value, new_args_obs), new_args_obs, user_attributes, deregister,
recursion + 1)
elseif status === false && recursion === 2
kw_str = isempty(kw) ? "" : " and kw: $(typeof(kw))"
kw_convert = isempty(kw) ? "" : "; kw..."
conv_trait = PTrait isa NoConversion ? "" : " (With conversion trait $(PTrait))"
types = MakieCore.types_for_plot_arguments(P, PTrait)
throw(ArgumentError("""
Conversion failed for $(P)$(conv_trait) with args: $(typeof(args)) $(kw_str).
$(P) requires to convert to argument types $(types), which convert_arguments didn't succeed in.
To fix this overload convert_arguments(P, args...$(kw_convert)) for $(P) or $(PTrait) and return an object of type $(types).`
"""))
elseif isnothing(status)
# No types_for_plot_arguments defined, so we don't know what we need to convert to.
# This is for backwards compatibility for recipes that don't define argument types
return convert_observable_args(P, dim_converted, kw_obs, converted, deregister)
else
error("Unknown status: $(status)")
end
end
function Plot{Func}(user_args::Tuple, user_attributes::Dict) where {Func}
# Handle plot!(plot, attributes::Attributes, args...) here
if !isempty(user_args) && first(user_args) isa Attributes
attr = attributes(first(user_args))
merge!(user_attributes, attr)
return Plot{Func}(Base.tail(user_args), user_attributes)
end
P = Plot{Func}
args = map(to_value, user_args)
attr = used_attributes(P, args...)
# don't use convert(Observable{Any}, x) here,
# We assume if a user passes the observable, they type it correctly
# And if they pass a value, they may want to change the type, so we need Observable{Any}
args_obs = map(x -> x isa Observable ? x : Observable{Any}(x), user_args)
deregister = Observables.ObserverFunction[]
PTrait = conversion_trait(P, args...)
expanded_args_obs = apply_expand_dimensions(PTrait, args, args_obs, deregister)
converted_obs = conversion_pipeline(P, attr, args, expanded_args_obs, user_attributes, deregister)
args2 = map(to_value, converted_obs)
ArgTyp = MakieCore.argtypes(args2)
FinalPlotFunc = plotfunc(plottype(P, args2...))
foreach(x -> delete!(user_attributes, x), attr)
return Plot{FinalPlotFunc,ArgTyp}(user_attributes, Any[args_obs...], Observable[converted_obs...],
deregister)
end
"""
used_attributes(args...) = ()
Function used to indicate what keyword args one wants to get passed in `convert_arguments`.
Those attributes will not be forwarded to the backend, but only used during the
conversion pipeline.
Usage:
```julia
struct MyType end
used_attributes(::MyType) = (:attribute,)
function convert_arguments(x::MyType; attribute = 1)
...
end
# attribute will get passed to convert_arguments
# without keyword_verload, this wouldn't happen
plot(MyType, attribute = 2)
#You can also use the convenience macro, to overload convert_arguments in one step:
@keywords convert_arguments(x::MyType; attribute = 1)
...
end
```
"""
used_attributes(::Type{<:Plot}, args...) = used_attributes(args...)
used_attributes(args...) = ()
## generic definitions
# Chose the more specific plot type from arguments or input type
# Note the plottype(Scatter, Plot{plot}) will return Scatter
# And plottype(args...) falls back to Plot{plot}
plottype(P::Type{<: Plot{T}}, argvalues...) where T = plottype(P, plottype(argvalues...))
plottype(P::Type{<:Plot{T}}) where {T} = P
plottype(P1::Type{<:Plot{T1}}, ::Type{<:Plot{T2}}) where {T1, T2} = P1
plottype(::Type{Plot{plot}}, ::Type{Plot{plot}}) = Plot{plot}
"""
plottype(P1::Type{<: Plot{T1}}, P2::Type{<: Plot{T2}})
Chooses the more concrete plot type
```julia
function convert_arguments(P::PlotFunc, args...)
ptype = plottype(P, Lines)
...
end
```
"""
plottype(::Type{Plot{plot}}, P::Type{<:Plot{T}}) where {T} = P
plottype(P::Type{<:Plot{T}}, ::Type{Plot{plot}}) where {T} = P
## specialized definitions for types
plottype(::AbstractVector, ::AbstractVector, ::AbstractVector) = Scatter
plottype(::AbstractVector, ::AbstractVector) = Scatter
plottype(::AbstractVector) = Scatter
plottype(::AbstractMatrix{<: Real}) = Heatmap
plottype(::Array{<: AbstractFloat, 3}) = Volume
plottype(::AbstractString) = Text
plottype(::LineString) = Lines
plottype(::AbstractVector{<:LineString}) = Lines
plottype(::MultiLineString) = Lines
plottype(::Polygon) = Poly
plottype(::GeometryBasics.AbstractPolygon) = Poly
plottype(::AbstractVector{<:GeometryBasics.AbstractPolygon}) = Poly
plottype(::MultiPolygon) = Lines
clip_planes_obs(parent::AbstractPlot) = attributes(parent).clip_planes
clip_planes_obs(parent::Scene) = parent.theme[:clip_planes]
# all the plotting functions that get a plot type
const PlotFunc = Type{<:AbstractPlot}
function plot!(::Plot{F, Args}) where {F, Args}
if !(F in atomic_functions)
error("No recipe for $(F) with args: $(Args)")
end
end
function connect_plot!(parent::SceneLike, plot::Plot{F}) where {F}
plot.parent = parent
scene = parent_scene(parent)
apply_theme!(scene, plot)
t_user = to_value(get(attributes(plot), :transformation, automatic))
if t_user isa Transformation
plot.transformation = t_user
else
if t_user isa Automatic
plot.transformation = Transformation()
else
t = Transformation()
transform!(t, t_user)
plot.transformation = t
end
if is_space_compatible(plot, parent)
obsfunc = connect!(transformation(parent), transformation(plot))
append!(plot.deregister_callbacks, obsfunc)
end
end
plot.model = transformationmatrix(plot)
calculated_attributes!(Plot{F}, plot)
default_shading!(plot, parent_scene(parent))
if to_value(get(attributes(plot), :clip_planes, automatic)) === automatic
attributes(plot)[:clip_planes] = map(identity, plot, clip_planes_obs(parent))
end
plot!(plot)
conversions = get_conversions(plot)
if !isnothing(conversions)
connect_conversions!(scene.conversions, conversions)
end
return plot
end
function plot!(scene::SceneLike, plot::Plot)
connect_plot!(scene, plot)
push!(scene, plot)
return plot
end
function apply_theme!(scene::Scene, plot::P) where {P<: Plot}
raw_attr = attributes(plot.attributes)
plot_theme = default_theme(scene, P)
plot_sym = plotsym(P)
if haskey(theme(scene), plot_sym)
merge_without_obs_reverse!(plot_theme, theme(scene, plot_sym))
end
for (k, v) in plot.kw
if v isa NamedTuple
raw_attr[k] = Attributes(v)
else
raw_attr[k] = convert(Observable{Any}, v)
end
end
return merge!(plot.attributes, plot_theme)
end