forked from MakieOrg/Makie.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspector.jl
1040 lines (843 loc) · 32.3 KB
/
inspector.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
### indicator data -> string
########################################
vec2string(p::StaticVector{2}) = @sprintf("(%0.3f, %0.3f)", p[1], p[2])
vec2string(p::StaticVector{3}) = @sprintf("(%0.3f, %0.3f, %0.3f)", p[1], p[2], p[3])
position2string(p::StaticVector{2}) = @sprintf("x: %0.6f\ny: %0.6f", p[1], p[2])
position2string(p::StaticVector{3}) = @sprintf("x: %0.6f\ny: %0.6f\nz: %0.6f", p[1], p[2], p[3])
function bbox2string(bbox::Rect3)
p0 = origin(bbox)
p1 = p0 .+ widths(bbox)
@sprintf(
"""
Bounding Box:
x: (%0.3f, %0.3f)
y: (%0.3f, %0.3f)
z: (%0.3f, %0.3f)
""",
p0[1], p1[1], p0[2], p1[2], p0[3], p1[3]
)
end
function bbox2string(bbox::Rect2)
p0 = origin(bbox)
p1 = p0 .+ widths(bbox)
@sprintf(
"""
Bounding Box:
x: (%0.3f, %0.3f)
y: (%0.3f, %0.3f)
""",
p0[1], p1[1], p0[2], p1[2]
)
end
color2text(c::AbstractFloat) = @sprintf("%0.3f", c)
color2text(c::Symbol) = string(c)
color2text(c) = color2text(to_color(c))
function color2text(c::RGBAf)
if c.alpha == 1.0
@sprintf("RGB(%0.2f, %0.2f, %0.2f)", c.r, c.g, c.b)
else
@sprintf("RGBA(%0.2f, %0.2f, %0.2f, %0.2f)", c.r, c.g, c.b, c.alpha)
end
end
color2text(name, i::Integer, j::Integer, c) = "$name[$i, $j] = $(color2text(c))"
function color2text(name, i, j, c)
idxs = @sprintf("%0.2f, %0.2f", i, j)
"$name[$idxs] = $(color2text(c))"
end
### dealing with markersize and rotations
########################################
_to_scale(f::AbstractFloat, idx) = Vec3f(f)
_to_scale(v::Vec2f, idx) = Vec3f(v[1], v[2], 1)
_to_scale(v::Vec3f, idx) = v
_to_scale(v::Vector, idx) = _to_scale(v[idx], idx)
_to_rotation(x, idx) = to_rotation(x)
_to_rotation(x::Vector, idx) = to_rotation(x[idx])
### Selecting a point on a nearby line
########################################
function closest_point_on_line(A::Point2f, B::Point2f, P::Point2f)
# This only works in 2D
AP = P .- A; AB = B .- A
A .+ AB * dot(AP, AB) / dot(AB, AB)
end
function point_in_triangle(A::Point2, B::Point2, C::Point2, P::Point2, ϵ = 1e-6)
# adjusted from ray_triangle_intersection
AO = A .- P
BO = B .- P
CO = C .- P
A1 = 0.5 * (BO[1] * CO[2] - BO[2] * CO[1])
A2 = 0.5 * (CO[1] * AO[2] - CO[2] * AO[1])
A3 = 0.5 * (AO[1] * BO[2] - AO[2] * BO[1])
return (A1 > -ϵ && A2 > -ϵ && A3 > -ϵ) || (A1 < ϵ && A2 < ϵ && A3 < ϵ)
end
### Mapping mesh vertex indices to Vector{Polygon} index
########################################
function vertexindex2poly(polys, idx)
counter = 0
for i in eachindex(polys)
step = ncoords(polys[i])
if idx <= counter + step
return i
else
counter += step
end
end
return length(polys)
end
ncoords(x) = length(coordinates(x))
ncoords(mesh::Mesh) = length(coordinates(mesh))
function ncoords(poly::Polygon)
N = length(poly.exterior) + 1
for int in poly.interiors
N += length(int) + 1
end
N
end
## Band Sections
########################################
"""
point_in_quad_parameter(A, B, C, D, P[; iterations = 20, epsilon = 1e-6])
Given a quad
A --- B
| |
D --- C
this computes parameter `f` such that the line from `A + f * (B - A)` to
`D + f * (C - D)` crosses through the given point `P`. This assumes that `P` is
inside the quad and that none of the edges cross.
"""
function point_in_quad_parameter(
A::Point2, B::Point2, C::Point2, D::Point2, P::Point2;
iterations = 50, epsilon = 1e-6
)
# Our initial guess is that P is in the center of the quad (in terms of AB and DC)
f = 0.5
AB = B - A
DC = C - D
for _ in 0:iterations
# vector between top and bottom point of the current line
dir = (D + f * (C - D)) - (A + f * (B - A))
# solves P + _ * dir = A + f1 * (B - A) (intersection point of ray & line)
f1, _ = inv(Mat2f(AB..., dir...)) * (P - A)
f2, _ = inv(Mat2f(DC..., dir...)) * (P - D)
# next fraction estimate should be between f1 and f2
# adding 2f to this helps avoid jumping between low and high values
old_f = f
f = 0.25 * (2f + f1 + f2)
if abs(old_f - f) < epsilon
return f
end
end
return f
end
## Shifted projection
########################################
@deprecate shift_project(scene, plot, pos) shift_project(scene, pos) false
function shift_project(scene, pos)
project(
camera(scene).projectionview[],
Vec2f(size(scene)),
pos
) .+ Vec2f(origin(viewport(scene)[]))
end
################################################################################
### Interactive selection via DataInspector
################################################################################
# TODO destructor?
mutable struct DataInspector
root::Scene
attributes::Attributes
temp_plots::Vector{AbstractPlot}
plot::Tooltip
selection::AbstractPlot
obsfuncs::Vector{Any}
end
function DataInspector(scene::Scene, plot::AbstractPlot, attributes)
x = DataInspector(scene, attributes, AbstractPlot[], plot, plot, Any[])
# finalizer(cleanup, x) # doesn't get triggered when this is dereferenced
x
end
function cleanup(inspector::DataInspector)
off.(inspector.obsfuncs)
empty!(inspector.obsfuncs)
delete!(inspector.root, inspector.plot)
clear_temporary_plots!(inspector, inspector.selection)
inspector
end
function Base.delete!(::Union{Scene, Figure}, inspector::DataInspector)
cleanup(inspector)
end
enable!(inspector::DataInspector) = inspector.attributes.enabled[] = true
disable!(inspector::DataInspector) = inspector.attributes.enabled[] = false
"""
DataInspector(figure_axis_or_scene = current_figure(); kwargs...)
Creates a data inspector which will show relevant information in a tooltip
when you hover over a plot.
This functionality can be disabled on a per-plot basis by setting
`plot.inspectable[] = false`. The displayed text can be adjusted by setting
`plot.inspector_label` to a function `(plot, index, position) -> "my_label"`
returning a label. See Makie documentation for more detail.
### Keyword Arguments:
- `range = 10`: Controls the snapping range for selecting an element of a plot.
- `priority = 100`: The priority of creating a tooltip on a mouse movement or
scrolling event.
- `enabled = true`: Disables inspection of plots when set to false. Can also be
adjusted with `enable!(inspector)` and `disable!(inspector)`.
- `indicator_color = :red`: Color of the selection indicator.
- `indicator_linewidth = 2`: Linewidth of the selection indicator.
- `indicator_linestyle = nothing`: Linestyle of the selection indicator
- `enable_indicators = true)`: Enables or disables indicators
- `depth = 9e3`: Depth value of the tooltip. This should be high so that the
tooltip is always in front.
- `apply_tooltip_offset = true`: Enables or disables offsetting tooltips based
on, for example, markersize.
- and all attributes from `Tooltip`
"""
function DataInspector(fig_or_block; kwargs...)
DataInspector(get_scene(fig_or_block); kwargs...)
end
function DataInspector(scene::Scene; priority = 100, kwargs...)
parent = root(scene)
@assert origin(viewport(parent)[]) == Vec2f(0)
attrib_dict = Dict(kwargs)
base_attrib = Attributes(
# General DataInspector settings
range = pop!(attrib_dict, :range, 10),
enabled = pop!(attrib_dict, :enabled, true),
depth = pop!(attrib_dict, :depth, 9e3),
enable_indicators = pop!(attrib_dict, :show_bbox_indicators, true),
offset = get(attrib_dict, :offset, 10f0),
apply_tooltip_offset = pop!(attrib_dict, :apply_tooltip_offset, true),
# Settings for indicators (plots that highlight the current selection)
indicator_color = pop!(attrib_dict, :indicator_color, :red),
indicator_linewidth = pop!(attrib_dict, :indicator_linewidth, 2),
indicator_linestyle = pop!(attrib_dict, :indicator_linestyle, nothing),
# Reusable values for creating indicators
indicator_visible = false,
# General reusable
_color = RGBAf(0,0,0,0),
)
plot = tooltip!(parent, Observable(Point2f(0)), text = Observable(""); visible=false, attrib_dict...)
on(z -> translate!(plot, 0, 0, z), base_attrib.depth)
notify(base_attrib.depth)
inspector = DataInspector(parent, plot, base_attrib)
e = events(parent)
f1 = on(_ -> on_hover(inspector), e.mouseposition, priority = priority)
f2 = on(_ -> on_hover(inspector), e.scroll, priority = priority)
push!(inspector.obsfuncs, f1, f2)
on(base_attrib.enable_indicators) do enabled
if !enabled
yield()
clear_temporary_plots!(inspector, inspector.selection)
end
return
end
inspector
end
DataInspector(; kwargs...) = DataInspector(current_figure(); kwargs...)
function on_hover(inspector)
parent = inspector.root
(inspector.attributes.enabled[] && is_mouseinside(parent)) || return Consume(false)
mp = mouseposition_px(parent)
should_clear = true
for (plt, idx) in pick_sorted(parent, mp, inspector.attributes.range[])
if to_value(get(plt.attributes, :inspectable, true))
# show_data should return true if it created a tooltip
if show_data_recursion(inspector, plt, idx)
should_clear = false
break
end
end
end
if should_clear
plot = inspector.selection
if haskey(plot, :inspector_clear)
plot[:inspector_clear][](inspector, plot)
end
inspector.plot.visible[] = false
inspector.attributes.indicator_visible[] = false
inspector.plot.offset.val = inspector.attributes.offset[]
end
return Consume(false)
end
function show_data_recursion(inspector, plot, idx)
processed = show_data_recursion(inspector, plot.parent, idx, plot)
if processed
return true
else
# Some show_data methods use the current selection to tell whether the
# temporary plots (indicator plots) are theirs or not, so we want to
# reset after processing them. We also don't want to reset when the
processed = if haskey(plot, :inspector_hover)
plot[:inspector_hover][](inspector, plot, idx)
else
show_data(inspector, plot, idx)
end
if processed && inspector.selection != plot
clear_temporary_plots!(inspector, plot)
end
return processed
end
end
show_data_recursion(inspector, plot, idx, source) = false
function show_data_recursion(inspector, plot::AbstractPlot, idx, source)
processed = show_data_recursion(inspector, plot.parent, idx, source)
if processed
return true
else
# Some show_data methods use the current selection to tell whether the
# temporary plots (indicator plots) are theirs or not, so we want to
# reset after processing them. We also don't want to reset when the
processed = if haskey(plot, :inspector_hover)
plot[:inspector_hover][](inspector, plot, idx, source)
else
show_data(inspector, plot, idx, source)
end
if processed && inspector.selection != plot
clear_temporary_plots!(inspector, plot)
end
return processed
end
end
# clears temporary plots (i.e. bboxes) and update selection
function clear_temporary_plots!(inspector::DataInspector, plot)
inspector.selection = plot
for i in length(inspector.obsfuncs):-1:3
off(pop!(inspector.obsfuncs))
end
for p in inspector.temp_plots
delete!(parent_scene(p), p)
end
# clear attributes which are reused for indicator plots
for key in (
:indicator_color, :indicator_linestyle,
:indicator_linewidth, :indicator_visible
)
empty!(inspector.attributes[key].listeners)
end
empty!(inspector.temp_plots)
return
end
# update alignment direction
function update_tooltip_alignment!(inspector, proj_pos)
inspector.plot[1][] = proj_pos
wx, wy = widths(viewport(inspector.root)[])
px, py = proj_pos
placement = py < 0.75wy ? (:above) : (:below)
px < 0.25wx && (placement = :right)
px > 0.75wx && (placement = :left)
inspector.plot.placement[] = placement
return
end
################################################################################
### show_data for primitive plots
################################################################################
# TODO: better 3D scaling
function show_data(inspector::DataInspector, plot::Scatter, idx)
a = inspector.attributes
tt = inspector.plot
scene = parent_scene(plot)
pos = position_on_plot(plot, idx)
proj_pos = shift_project(scene, pos)
update_tooltip_alignment!(inspector, proj_pos)
if haskey(plot, :inspector_label)
tt.text[] = plot[:inspector_label][](plot, idx, pos)
else
tt.text[] = position2string(pos)
end
tt.offset[] = ifelse(
a.apply_tooltip_offset[],
0.5 * maximum(sv_getindex(plot.markersize[], idx)) + 2,
a.offset[]
)
tt.visible[] = true
a.indicator_visible[] && (a.indicator_visible[] = false)
return true
end
function show_data(inspector::DataInspector, plot::MeshScatter, idx)
a = inspector.attributes
tt = inspector.plot
scene = parent_scene(plot)
if a.enable_indicators[]
translation = apply_transform_and_model(plot, plot[1][][idx])
rotation = _to_rotation(plot.rotations[], idx)
scale = _to_scale(plot.markersize[], idx)
if inspector.selection != plot
clear_temporary_plots!(inspector, plot)
cc = cameracontrols(scene)
if cc isa Camera3D
eyeposition = cc.eyeposition[]
lookat = cc.lookat[]
upvector = cc.upvector[]
end
bbox = Rect{3, Float32}(convert_attribute(
plot.marker[], Key{:marker}(), Key{Makie.plotkey(plot)}()
))
T = Transformation(
identity; translation = translation, rotation = rotation, scale = scale
)
p = wireframe!(
scene, bbox, transformation = T, color = a.indicator_color,
linewidth = a.indicator_linewidth, linestyle = a.indicator_linestyle,
visible = a.indicator_visible, inspectable = false
)
push!(inspector.temp_plots, p)
# Restore camera
cc isa Camera3D && update_cam!(scene, eyeposition, lookat, upvector)
elseif !isempty(inspector.temp_plots)
p = inspector.temp_plots[1]
transform!(p, translation = translation, scale = scale, rotation = rotation)
end
a.indicator_visible[] = true
end
pos = position_on_plot(plot, idx)
proj_pos = shift_project(scene, pos)
update_tooltip_alignment!(inspector, proj_pos)
if haskey(plot, :inspector_label)
tt.text[] = plot[:inspector_label][](plot, idx, pos)
else
tt.text[] = position2string(pos)
end
tt.visible[] = true
return true
end
function show_data(inspector::DataInspector, plot::Union{Lines, LineSegments}, idx)
a = inspector.attributes
tt = inspector.plot
scene = parent_scene(plot)
# cast ray from cursor into screen, find closest point to line
pos = position_on_plot(plot, idx)
proj_pos = shift_project(scene, pos)
update_tooltip_alignment!(inspector, proj_pos)
tt.offset[] = ifelse(
a.apply_tooltip_offset[],
sv_getindex(plot.linewidth[], idx) + 2,
a.offset[]
)
if haskey(plot, :inspector_label)
tt.text[] = plot[:inspector_label][](plot, idx, eltype(plot[1][])(pos))
else
tt.text[] = position2string(eltype(plot[1][])(pos))
end
tt.visible[] = true
a.indicator_visible[] && (a.indicator_visible[] = false)
return true
end
function show_data(inspector::DataInspector, plot::Mesh, idx)
a = inspector.attributes
tt = inspector.plot
scene = parent_scene(plot)
# Manual boundingbox including transfunc
bbox = let
points = point_iterator(plot)
trans_func = transform_func(plot)
model = plot.model[]
iter = iterate_transformed(points, model, to_value(get(plot, :space, :data)), trans_func)
limits_from_transformed_points(iter)
end
proj_pos = Point2f(mouseposition_px(inspector.root))
update_tooltip_alignment!(inspector, proj_pos)
if a.enable_indicators[]
if inspector.selection != plot
clear_temporary_plots!(inspector, plot)
cc = cameracontrols(scene)
if cc isa Camera3D
eyeposition = cc.eyeposition[]
lookat = cc.lookat[]
upvector = cc.upvector[]
end
p = wireframe!(
scene, bbox, color = a.indicator_color,
transformation = Transformation(),
linewidth = a.indicator_linewidth, linestyle = a.indicator_linestyle,
visible = a.indicator_visible, inspectable = false
)
push!(inspector.temp_plots, p)
# Restore camera
cc isa Camera3D && update_cam!(scene, eyeposition, lookat, upvector)
elseif !isempty(inspector.temp_plots)
p = inspector.temp_plots[1]
p[1][] = bbox
end
a.indicator_visible[] = true
end
tt[1][] = proj_pos
if haskey(plot, :inspector_label)
tt.text[] = plot[:inspector_label][](plot, idx, bbox)
else
tt.text[] = bbox2string(bbox)
end
tt.visible[] = true
return true
end
function show_data(inspector::DataInspector, plot::Surface, idx)
a = inspector.attributes
tt = inspector.plot
proj_pos = Point2f(mouseposition_px(inspector.root))
update_tooltip_alignment!(inspector, proj_pos)
pos = position_on_plot(plot, idx)
if !isnan(pos)
tt[1][] = proj_pos
if haskey(plot, :inspector_label)
tt.text[] = plot[:inspector_label][](plot, idx, pos)
else
tt.text[] = position2string(pos)
end
tt.visible[] = true
tt.offset[] = 0f0
else
tt.visible[] = false
end
a.indicator_visible[] && (a.indicator_visible[] = false)
return true
end
function show_data(inspector::DataInspector, plot::Heatmap, idx)
show_imagelike(inspector, plot, "H", true)
end
function show_data(inspector::DataInspector, plot::Image, idx)
show_imagelike(inspector, plot, "img", false)
end
function show_imagelike(inspector, plot, name, edge_based)
a = inspector.attributes
tt = inspector.plot
scene = parent_scene(plot)
pos = position_on_plot(plot, -1, apply_transform = false)[Vec(1, 2)] # index irrelevant
# Not on image/heatmap
if isnan(pos)
a.indicator_visible[] = false
tt.visible[] = false
return true
end
if plot.interpolate[]
i, j, z = _interpolated_getindex(plot[1][], plot[2][], plot[3][], pos)
x, y = pos
else
i, j, z = _pixelated_getindex(plot[1][], plot[2][], plot[3][], pos, edge_based)
x = i; y = j
end
# in case we hover over NaN values
if isnan(z)
a.indicator_visible[] = false
tt.visible[] = false
return true
end
if haskey(plot, :inspector_label)
ins_p = z isa Colorant ? (pos[1], pos[2], z) : Point3f(pos[1], pos[2], z)
tt.text[] = plot[:inspector_label][](plot, (i, j), ins_p)
else
tt.text[] = color2text(name, x, y, z)
end
a._color[] = if z isa AbstractFloat
interpolated_getindex(
to_colormap(plot.colormap[]), z,
extract_colorrange(plot)
)
else
z
end
proj_pos = Point2f(mouseposition_px(inspector.root))
update_tooltip_alignment!(inspector, proj_pos)
if a.enable_indicators[]
if plot.interpolate[]
if inspector.selection != plot || (length(inspector.temp_plots) != 1) ||
!(inspector.temp_plots[1] isa Scatter)
clear_temporary_plots!(inspector, plot)
p = scatter!(
scene, pos, color = a._color,
visible = a.indicator_visible,
inspectable = false, model = plot.model,
# TODO switch to Rect with 2r-1 or 2r-2 markersize to have
# just enough space to always detect the underlying image
marker=:rect, markersize = map(r -> 2r, a.range),
strokecolor = a.indicator_color,
strokewidth = a.indicator_linewidth,
depth_shift = -1f-3
)
push!(inspector.temp_plots, p)
else
p = inspector.temp_plots[1]
p[1].val[1] = pos
notify(p[1])
end
else
bbox = _pixelated_image_bbox(plot[1][], plot[2][], plot[3][], i, j, edge_based)
if inspector.selection != plot || (length(inspector.temp_plots) != 1) ||
!(inspector.temp_plots[1] isa Wireframe)
clear_temporary_plots!(inspector, plot)
p = wireframe!(
scene, bbox, color = a.indicator_color, model = plot.model,
strokewidth = a.indicator_linewidth, linestyle = a.indicator_linestyle,
visible = a.indicator_visible, inspectable = false,
depth_shift = -1f-3
)
push!(inspector.temp_plots, p)
else
p = inspector.temp_plots[1]
p[1][] = bbox
end
end
a.indicator_visible[] = true
end
tt.visible[] = true
return true
end
function _interpolated_getindex(xs, ys, img, mpos)
x0, x1 = extrema(xs)
y0, y1 = extrema(ys)
x, y = clamp.(mpos, (x0, y0), (x1, y1))
i = clamp((x - x0) / (x1 - x0) * size(img, 1) + 0.5, 1, size(img, 1))
j = clamp((y - y0) / (y1 - y0) * size(img, 2) + 0.5, 1, size(img, 2))
l = clamp(floor(Int, i), 1, size(img, 1)-1);
r = clamp(l+1, 2, size(img, 1))
b = clamp(floor(Int, j), 1, size(img, 2)-1);
t = clamp(b+1, 2, size(img, 2))
z = ((r-i) * img[l, b] + (i-l) * img[r, b]) * (t-j) +
((r-i) * img[l, t] + (i-l) * img[r, t]) * (j-b)
# float, float, value (i, j are no longer used)
return i, j, z
end
function _pixelated_getindex(xs, ys, img, mpos, edge_based)
x0, x1 = extrema(xs)
y0, y1 = extrema(ys)
x, y = clamp.(mpos, (x0, y0), (x1, y1))
i = clamp(round(Int, (x - x0) / (x1 - x0) * size(img, 1) + 0.5), 1, size(img, 1))
j = clamp(round(Int, (y - y0) / (y1 - y0) * size(img, 2) + 0.5), 1, size(img, 2))
# int, int, value
return i, j, img[i,j]
end
function _interpolated_getindex(xs::Vector, ys::Vector, img, mpos)
# x, y = mpos
# i, j, _ = _pixelated_getindex(xs, ys, img, mpos, false)
# w = (xs[i+1] - xs[i]); h = (ys[j+1] - ys[j])
# z = ((xs[i+1] - x) / w * img[i, j] + (x - xs[i]) / w * img[i+1, j]) * (ys[j+1] - y) / h +
# ((xs[i+1] - x) / w * img[i, j+1] + (x - xs[i]) / w * img[i+1, j+1]) * (y - ys[j]) / h
# return i, j, z
_interpolated_getindex(minimum(xs)..maximum(xs), minimum(ys)..maximum(ys), img, mpos)
end
function _pixelated_getindex(xs::Vector, ys::Vector, img, mpos, edge_based)
if edge_based
x, y = mpos
i = max(1, something(findfirst(v -> v >= x, xs), length(xs))-1)
j = max(1, something(findfirst(v -> v >= y, ys), length(ys))-1)
return i, j, img[i, j]
else
_pixelated_getindex(minimum(xs)..maximum(xs), minimum(ys)..maximum(ys), img, mpos, edge_based)
end
end
function _pixelated_image_bbox(xs, ys, img, i::Integer, j::Integer, edge_based)
x0, x1 = extrema(xs)
y0, y1 = extrema(ys)
nw, nh = ((x1 - x0), (y1 - y0)) ./ size(img)
Rect2f(x0 + nw * (i-1), y0 + nh * (j-1), nw, nh)
end
function _pixelated_image_bbox(xs::Vector, ys::Vector, img, i::Integer, j::Integer, edge_based)
if edge_based
Rect2f(xs[i], ys[j], xs[i+1] - xs[i], ys[j+1] - ys[j])
else
_pixelated_image_bbox(
minimum(xs)..maximum(xs), minimum(ys)..maximum(ys),
img, i, j, edge_based
)
end
end
function show_data(inspector::DataInspector, plot, idx, source=nothing)
return false
end
################################################################################
### show_data for Plot/recipe plots
################################################################################
function show_data(inspector::DataInspector, plot::BarPlot, idx, ::Lines)
return show_data(inspector, plot, div(idx-1, 6)+1)
end
function show_data(inspector::DataInspector, plot::BarPlot, idx, ::Mesh)
return show_data(inspector, plot, div(idx-1, 4)+1)
end
function show_data(inspector::DataInspector, plot::BarPlot, idx)
a = inspector.attributes
tt = inspector.plot
scene = parent_scene(plot)
pos = apply_transform_and_model(plot, plot[1][][idx])
proj_pos = shift_project(scene, to_ndim(Point3f, pos, 0))
update_tooltip_alignment!(inspector, proj_pos)
if a.enable_indicators[]
model = plot.model[]
bbox = plot.plots[1][1][][idx]
if inspector.selection != plot
clear_temporary_plots!(inspector, plot)
p = wireframe!(
scene, bbox, model = model, color = a.indicator_color,
strokewidth = a.indicator_linewidth, linestyle = a.indicator_linestyle,
visible = a.indicator_visible, inspectable = false
)
translate!(p, Vec3f(0, 0, a.depth[]))
push!(inspector.temp_plots, p)
elseif !isempty(inspector.temp_plots)
p = inspector.temp_plots[1]
p[1][] = bbox
p.model[] = model
end
a.indicator_visible[] = true
end
if haskey(plot, :inspector_label)
tt.text[] = plot[:inspector_label][](plot, idx, pos)
else
tt.text[] = position2string(pos)
end
tt.visible[] = true
return true
end
function show_data(inspector::DataInspector, plot::Arrows, idx, ::LineSegments)
return show_data(inspector, plot, div(idx+1, 2), nothing)
end
function show_data(inspector::DataInspector, plot::Arrows, idx, source)
a = inspector.attributes
tt = inspector.plot
pos = apply_transform_and_model(plot, plot[1][][idx])
mpos = Point2f(mouseposition_px(inspector.root))
update_tooltip_alignment!(inspector, mpos)
p = vec2string(pos)
v = vec2string(plot[2][][idx])
tt[1][] = mpos
if haskey(plot, :inspector_label)
tt.text[] = plot[:inspector_label][](plot, idx, pos)
else
tt.text[] = "Position:\n $p\nDirection:\n $v"
end
tt.visible[] = true
a.indicator_visible[] && (a.indicator_visible[] = false)
return true
end
# This should work if contourf would place computed levels in colors and let the
# backend handle picking colors from a colormap
function show_data(inspector::DataInspector, plot::Contourf, idx, source::Mesh)
tt = inspector.plot
idx = show_poly(inspector, plot, plot.plots[1], idx, source)
level = plot.plots[1].color[][idx]
mpos = Point2f(mouseposition_px(inspector.root))
update_tooltip_alignment!(inspector, mpos)
tt[1][] = mpos
if haskey(plot, :inspector_label)
tt.text[] = plot[:inspector_label][](plot, idx, mpos)
else
tt.text[] = @sprintf("level = %0.3f", level)
end
tt.visible[] = true
return true
end
# What should this display?
# function show_data(
# inspector::DataInspector, plot::Poly{<: Tuple{<: AbstractVector}},
# idx, source::Mesh
# )
# @info "PolyMesh"
# idx, ext = show_poly(inspectable, plot, idx, source)
# return true
# end
function show_poly(inspector, plot, poly, idx, source)
a = inspector.attributes
idx = vertexindex2poly(poly[1][], idx)
if a.enable_indicators[]
line_collection = copy(convert_arguments(PointBased(), poly[1][][idx].exterior)[1])
for int in poly[1][][idx].interiors
push!(line_collection, Point2f(NaN))
append!(line_collection, convert_arguments(PointBased(), int)[1])
end
if inspector.selection != plot
scene = parent_scene(plot)
clear_temporary_plots!(inspector, plot)
p = lines!(
scene, line_collection, color = a.indicator_color,
transformation = Transformation(source),
strokewidth = a.indicator_linewidth, linestyle = a.indicator_linestyle,
visible = a.indicator_visible, inspectable = false, depth_shift = -1f-3
)
push!(inspector.temp_plots, p)
elseif !isempty(inspector.temp_plots)
inspector.temp_plots[1][1][] = line_collection
end
a.indicator_visible[] = true
end
return idx
end
function show_data(inspector::DataInspector, plot::VolumeSlices, idx, child::Heatmap)
a = inspector.attributes
tt = inspector.plot
pos = position_on_plot(child, -1, apply_transform = false)[Vec(1, 2)] # index irrelevant
# Not on heatmap
if isnan(pos)
a.indicator_visible[] && (a.indicator_visible[] = false)
tt.visible[] = false
return true
end
i, j, val = _pixelated_getindex(child[1][], child[2][], child[3][], pos, true)
proj_pos = Point2f(mouseposition_px(inspector.root))
update_tooltip_alignment!(inspector, proj_pos)
tt[1][] = proj_pos
world_pos = apply_transform_and_model(child, pos)
if haskey(plot, :inspector_label)
tt.text[] = plot[:inspector_label][](plot, (i, j), world_pos)
else
tt.text[] = @sprintf(
"x: %0.6f\ny: %0.6f\nz: %0.6f\n%0.6f0",
world_pos[1], world_pos[2], world_pos[3], val
)
end
tt.visible[] = true
a.indicator_visible[] && (a.indicator_visible[] = false)
return true
end
function show_data(inspector::DataInspector, plot::Band, idx::Integer, mesh::Mesh)
scene = parent_scene(plot)
tt = inspector.plot
a = inspector.attributes
pos = Point2f(position_on_plot(mesh, idx, apply_transform = false)) #Point2f(mouseposition(scene))
ps1 = plot.converted[1][]
ps2 = plot.converted[2][]
# find first triangle containing the cursor position
idx = findfirst(1:length(ps1)-1) do i
point_in_triangle(ps1[i], ps1[i+1], ps2[i+1], pos) ||
point_in_triangle(ps1[i], ps2[i+1], ps2[i], pos)
end
if idx !== nothing
# (idx, idx+1) picks the quad that contains the cursor position
# Within the quad we can draw a line from ps1[idx] + f * (ps1[idx+1] - ps1[idx])
# to ps2[idx] + f * (ps2[idx+1] - ps2[idx]) which crosses through the
# cursor position. Find the parameter f that describes this line
f = point_in_quad_parameter(ps1[idx], ps1[idx+1], ps2[idx+1], ps2[idx], pos)
P1 = ps1[idx] + f * (ps1[idx+1] - ps1[idx])
P2 = ps2[idx] + f * (ps2[idx+1] - ps2[idx])
# Draw the line