This repository has been archived by the owner on Jul 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
conversions.jl
1218 lines (1002 loc) · 42.3 KB
/
conversions.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
################################################################################
# Type Conversions #
################################################################################
# if no plot type based conversion is defined, we try using a trait
function convert_arguments(T::PlotFunc, args...; kw...)
ct = conversion_trait(T)
try
convert_arguments(ct, args...; kw...)
catch e
if e isa MethodError
try
convert_arguments_individually(T, args...)
catch ee
if ee isa MethodError
error(
"""
`AbstractPlotting.convert_arguments` for the plot type $T and its conversion trait $ct was unsuccessful.
The signature that could not be converted was:
$(join("::" .* string.(typeof.(args)), ", "))
AbstractPlotting needs to convert all plot input arguments to types that can be consumed by the backends (typically Arrays with Float32 elements).
You can define a method for `AbstractPlotting.convert_arguments` (a type recipe) for these types or their supertypes to make this set of arguments convertible (See http://makie.juliaplots.org/stable/recipes.html).
Alternatively, you can define `AbstractPlotting.convert_single_argument` for single arguments which have types that are unknown to AbstractPlotting but which can be converted to known types and fed back to the conversion pipeline.
"""
)
else
rethrow(ee)
end
end
else
rethrow(e)
end
end
end
# in case no trait matches we try to convert each individual argument
# and reconvert the whole tuple in order to handle missings centrally, e.g.
function convert_arguments_individually(T::PlotFunc, args...)
# convert each single argument until it doesn't change type anymore
single_converted = recursively_convert_argument.(args)
# if the type of args hasn't changed this function call didn't help and we error
if typeof(single_converted) == typeof(args)
throw(MethodError(convert_arguments, (T, args...)))
end
# otherwise we try converting our newly single-converted args again because
# now a normal conversion method might work again
convert_arguments(T, single_converted...)
end
function recursively_convert_argument(x)
newx = convert_single_argument(x)
if typeof(newx) == typeof(x)
x
else
recursively_convert_argument(newx)
end
end
################################################################################
# Conversion Traits #
################################################################################
abstract type ConversionTrait end
const XYBased = Union{MeshScatter, Scatter, Lines, LineSegments}
const RangeLike = Union{AbstractRange, AbstractVector, ClosedInterval}
struct NoConversion <: ConversionTrait end
# No conversion by default
conversion_trait(::Type) = NoConversion()
convert_arguments(::NoConversion, args...) = args
struct PointBased <: ConversionTrait end
conversion_trait(x::Type{<: XYBased}) = PointBased()
abstract type SurfaceLike <: ConversionTrait end
struct ContinuousSurface <: SurfaceLike end
conversion_trait(::Type{<: Union{Surface, Image}}) = ContinuousSurface()
struct DiscreteSurface <: SurfaceLike end
conversion_trait(::Type{<: Heatmap}) = DiscreteSurface()
struct VolumeLike end
conversion_trait(::Type{<: Volume}) = VolumeLike()
################################################################################
# Single Argument Conversion #
################################################################################
# if no specific conversion is defined, we don't convert
convert_single_argument(x) = x
# replace missings with NaNs
function convert_single_argument(a::AbstractArray{<:Union{Missing, <:Real}})
[ismissing(x) ? NaN32 : convert(Float32, x) for x in a]
end
# same for points
function convert_single_argument(a::AbstractArray{<:Union{Missing, <:Point{N}}}) where N
[ismissing(x) ? Point{N, Float32}(NaN32) : Point{N, Float32}(x) for x in a]
end
################################################################################
# PointBased #
################################################################################
"""
Wrap a single point or equivalent object in a single-element array.
"""
function convert_arguments(::PointBased, position::VecTypes{N, <: Number}) where N
([convert(Point{N, Float32}, position)],)
end
function convert_arguments(::PointBased, positions::AbstractVector{<: VecTypes{N, <: Number}}) where N
(elconvert(Point{N, Float32}, positions),)
end
function convert_arguments(::PointBased, positions::SubArray{<: VecTypes, 1})
# TODO figure out a good subarray solution
(positions,)
end
"""
Enables to use scatter like a surface plot with x::Vector, y::Vector, z::Matrix
spanning z over the grid spanned by x y
"""
function convert_arguments(::PointBased, x::AbstractVector, y::AbstractVector, z::AbstractMatrix)
(vec(Point3f0.(x, y', z)),)
end
"""
convert_arguments(P, x, y, z)::(Vector)
Takes vectors `x`, `y`, and `z` and turns it into a vector of 3D points of the values
from `x`, `y`, and `z`.
`P` is the plot Type (it is optional).
"""
convert_arguments(::PointBased, x::RealVector, y::RealVector, z::RealVector) = (Point3f0.(x, y, z),)
"""
convert_arguments(P, x)::(Vector)
Takes an input GeometryPrimitive `x` and decomposes it to points.
`P` is the plot Type (it is optional).
"""
convert_arguments(p::PointBased, x::GeometryPrimitive) = convert_arguments(p, decompose(Point, x))
function convert_arguments(::PointBased, pos::AbstractMatrix{<: Number})
(to_vertices(pos),)
end
convert_arguments(P::PointBased, x::AbstractVector{<:Real}, y::AbstractVector{<:Real}) = (Point2f0.(x, y),)
convert_arguments(P::PointBased, x::AbstractVector{<:Real}, y::AbstractVector{<:Real}, z::AbstractVector{<:Real}) = (Point3f0.(x, y, z),)
"""
convert_arguments(P, y)::Vector
Takes vector `y` and generates a range from 1 to the length of `y`, for plotting on
an arbitrary `x` axis.
`P` is the plot Type (it is optional).
"""
convert_arguments(P::PointBased, y::RealVector) = convert_arguments(P, 1:length(y), y)
"""
convert_arguments(P, x, y)::(Vector)
Takes vectors `x` and `y` and turns it into a vector of 2D points of the values
from `x` and `y`.
`P` is the plot Type (it is optional).
"""
#convert_arguments(::PointBased, x::RealVector, y::RealVector) = (Point2f0.(x, y),)
convert_arguments(P::PointBased, x::ClosedInterval, y::RealVector) = convert_arguments(P, LinRange(extrema(x)..., length(y)), y)
convert_arguments(P::PointBased, x::RealVector, y::ClosedInterval) = convert_arguments(P, x, LinRange(extrema(y)..., length(x)))
"""
convert_arguments(P, x)::(Vector)
Takes an input `Rect` `x` and decomposes it to points.
`P` is the plot Type (it is optional).
"""
function convert_arguments(P::PointBased, x::Rect2D)
# TODO fix the order of decompose
return convert_arguments(P, decompose(Point2f0, x)[[1, 2, 4, 3, 1]])
end
function convert_arguments(P::PointBased, mesh::AbstractMesh)
return convert_arguments(P, decompose(Point3f0, mesh))
end
function convert_arguments(PB::PointBased, linesegments::FaceView{<:Line, P}) where {P<:AbstractPoint}
# TODO FaceView should be natively supported by backends!
return convert_arguments(PB, collect(reinterpret(P, linesegments)))
end
function convert_arguments(P::PointBased, x::Rect3D)
inds = [
1, 2, 3, 4, 5, 6, 7, 8,
1, 5, 5, 7, 7, 3, 1, 3,
4, 8, 8, 6, 2, 4, 2, 6
]
convert_arguments(P, decompose(Point3f0, x)[inds])
end
"""
convert_arguments(PB, LineString)
Takes an input `LineString` and decomposes it to points.
"""
function convert_arguments(PB::PointBased, linestring::LineString)
return convert_arguments(PB, decompose(Point, linestring))
end
"""
convert_arguments(PB, Union{Array{<:LineString}, MultiLineString})
Takes an input `Array{LineString}` or a `MultiLineString` and decomposes it to points.
"""
function convert_arguments(PB::PointBased, linestring::Union{Array{<:LineString}, MultiLineString})
arr = copy(convert_arguments(PB, linestring[1])[1])
for ls in 2:length(linestring)
push!(arr, Point2f0(NaN))
append!(arr, convert_arguments(PB, linestring[ls])[1])
end
return (arr,)
end
"""
convert_arguments(PB, Polygon)
Takes an input `Polygon` and decomposes it to points.
"""
function convert_arguments(PB::PointBased, pol::Polygon)
if isempty(pol.interiors)
return convert_arguments(PB, pol.exterior)
else
arr = copy(convert_arguments(PB, pol.exterior)[1])
push!(arr, Point2f0(NaN))
append!(arr, convert_arguments(PB, pol.interiors)[1])
return (arr,)
end
end
"""
convert_arguments(PB, Union{Array{<:Polygon}, MultiPolygon})
Takes an input `Array{Polygon}` or a `MultiPolygon` and decomposes it to points.
"""
function convert_arguments(PB::PointBased, mp::Union{Array{<:Polygon}, MultiPolygon})
arr = copy(convert_arguments(PB, mp[1])[1])
for p in 2:length(mp)
push!(arr, Point2f0(NaN))
append!(arr, convert_arguments(PB, mp[p])[1])
end
return (arr,)
end
################################################################################
# SurfaceLike #
################################################################################
function edges(v::AbstractVector)
l = length(v)
if l == 1
return [v[1] - 0.5, v[1] + 0.5]
else
# Equivalent to
# mids = 0.5 .* (v[1:end-1] .+ v[2:end])
# borders = [2v[1] - mids[1]; mids; 2v[end] - mids[end]]
borders = [0.5 * (v[max(1, i)] + v[min(end, i+1)]) for i in 0:length(v)]
borders[1] = 2borders[1] - borders[2]
borders[end] = 2borders[end] - borders[end-1]
return borders
end
end
function adjust_axes(::DiscreteSurface, x::AbstractVector{<:Number}, y::AbstractVector{<:Number}, z::AbstractMatrix)
x̂, ŷ = map((x, y), size(z)) do v, sz
return length(v) == sz ? edges(v) : v
end
return x̂, ŷ, z
end
adjust_axes(::SurfaceLike, x, y, z) = x, y, z
"""
convert_arguments(SL::SurfaceLike, x::VecOrMat, y::VecOrMat, z::Matrix)
If `SL` is `Heatmap` and `x` and `y` are vectors, infer from length of `x` and `y`
whether they represent edges or centers of the heatmap bins.
If they are centers, convert to edges. Convert eltypes to `Float32` and return
outputs as a `Tuple`.
"""
function convert_arguments(SL::SurfaceLike, x::AbstractVecOrMat{<: Number}, y::AbstractVecOrMat{<: Number}, z::AbstractMatrix{<: Union{Number, Colorant}})
return map(el32convert, adjust_axes(SL, x, y, z))
end
function convert_arguments(SL::SurfaceLike, x::AbstractVecOrMat{<: Number}, y::AbstractVecOrMat{<: Number}, z::AbstractMatrix{<:Number})
return map(el32convert, adjust_axes(SL, x, y, z))
end
convert_arguments(::SurfaceLike, x::AbstractMatrix, y::AbstractMatrix) = (x, y, zeros(size(y)))
"""
convert_arguments(P, x, y, z)::Tuple{ClosedInterval, ClosedInterval, Matrix}
Takes 2 ClosedIntervals's `x`, `y`, and an AbstractMatrix `z`, and converts the closed range to
linspaces with size(z, 1/2)
`P` is the plot Type (it is optional).
"""
function convert_arguments(P::SurfaceLike, x::ClosedInterval, y::ClosedInterval, z::AbstractMatrix)
convert_arguments(P, to_linspace(x, size(z, 1)), to_linspace(y, size(z, 2)), z)
end
"""
convert_arguments(P, Matrix)::Tuple{ClosedInterval, ClosedInterval, Matrix}
Takes an `AbstractMatrix`, converts the dimesions `n` and `m` into `ClosedInterval`,
and stores the `ClosedInterval` to `n` and `m`, plus the original matrix in a Tuple.
`P` is the plot Type (it is optional).
"""
function convert_arguments(::SurfaceLike, data::AbstractMatrix)
n, m = Float32.(size(data))
(0f0 .. n, 0f0 .. m, el32convert(data))
end
function convert_arguments(::DiscreteSurface, data::AbstractMatrix)
n, m = Float32.(size(data))
(0.5f0 .. n+0.5f0, 0.5f0 .. m+0.5f0, el32convert(data))
end
"""
convert_arguments(P, x, y, f)::(Vector, Vector, Matrix)
Takes vectors `x` and `y` and the function `f`, and applies `f` on the grid that `x` and `y` span.
This is equivalent to `f.(x, y')`.
`P` is the plot Type (it is optional).
"""
function convert_arguments(::SurfaceLike, x::AbstractVector{T1}, y::AbstractVector{T2}, f::Function) where {T1, T2}
if !applicable(f, x[1], y[1])
error("You need to pass a function with signature f(x::$T1, y::$T2). Found: $f")
end
T = typeof(f(x[1], y[1]))
z = similar(x, T, (length(x), length(y)))
z .= f.(x, y')
(x, y, z)
end
################################################################################
# VolumeLike #
################################################################################
"""
convert_arguments(P, Matrix)::Tuple{ClosedInterval, ClosedInterval, ClosedInterval, Matrix}
Takes an array of `{T, 3} where T`, converts the dimesions `n`, `m` and `k` into `ClosedInterval`,
and stores the `ClosedInterval` to `n`, `m` and `k`, plus the original array in a Tuple.
`P` is the plot Type (it is optional).
"""
function convert_arguments(::VolumeLike, data::AbstractArray{T, 3}) where T
n, m, k = Float32.(size(data))
return (0f0 .. n, 0f0 .. m, 0f0 .. k, el32convert(data))
end
function convert_arguments(::VolumeLike, x::RangeLike, y::RangeLike, z::RangeLike, data::AbstractArray{T, 3}) where T
return (x, y, z, el32convert(data))
end
"""
convert_arguments(P, x, y, z, i)::(Vector, Vector, Vector, Matrix)
Takes 3 `AbstractVector` `x`, `y`, and `z` and the `AbstractMatrix` `i`, and puts everything in a Tuple.
`P` is the plot Type (it is optional).
"""
function convert_arguments(::VolumeLike, x::AbstractVector, y::AbstractVector, z::AbstractVector, i::AbstractArray{T, 3}) where T
(x, y, z, el32convert(i))
end
"""
convert_arguments(P, x, y, z, f)::(Vector, Vector, Vector, Matrix)
Takes `AbstractVector` `x`, `y`, and `z` and the function `f`, evaluates `f` on the volume
spanned by `x`, `y` and `z`, and puts `x`, `y`, `z` and `f(x,y,z)` in a Tuple.
`P` is the plot Type (it is optional).
"""
function convert_arguments(::VolumeLike, x::AbstractVector, y::AbstractVector, z::AbstractVector, f::Function)
if !applicable(f, x[1], y[1], z[1])
error("You need to pass a function with signature f(x, y, z). Found: $f")
end
_x, _y, _z = ntuple(Val(3)) do i
A = (x, y, z)[i]
reshape(A, ntuple(j-> j != i ? 1 : length(A), Val(3)))
end
return (x, y, z, el32convert.(f.(_x, _y, _z)))
end
################################################################################
# <:LineSegments #
################################################################################
"""
Accepts a Vector of Pair of Points (e.g. `[Point(0, 0) => Point(1, 1), ...]`)
to encode e.g. linesegments or directions.
"""
function convert_arguments(::Type{<: LineSegments}, positions::AbstractVector{E}) where E <: Union{Pair{A, A}, Tuple{A, A}} where A <: VecTypes{N, T} where {N, T}
(elconvert(Point{N, Float32}, reinterpret(Point{N, T}, positions)),)
end
function convert_arguments(::Type{<: LineSegments}, x::Rect2D)
# TODO fix the order of decompose
points = decompose(Point2f0, x)
return (points[[1, 2, 2, 4, 4, 3, 3, 1]],)
end
################################################################################
# <:Text #
################################################################################
"""
convert_arguments(x)::(String)
Takes an input `AbstractString` `x` and converts it to a string.
"""
convert_arguments(::Type{<: Text}, x::AbstractString) = (String(x),)
################################################################################
# <:Mesh #
################################################################################
"""
convert_arguments(Mesh, x, y, z)::GLNormalMesh
Takes real vectors x, y, z and constructs a mesh out of those, under the assumption that
every 3 points form a triangle.
"""
function convert_arguments(
T::Type{<:Mesh},
x::RealVector, y::RealVector, z::RealVector
)
convert_arguments(T, Point3f0.(x, y, z))
end
"""
convert_arguments(Mesh, xyz::AbstractVector)::GLNormalMesh
Takes an input mesh and a vector `xyz` representing the vertices of the mesh, and
creates indices under the assumption, that each triplet in `xyz` forms a triangle.
"""
function convert_arguments(
MT::Type{<:Mesh},
xyz::AbstractVector
)
faces = connect(UInt32.(0:length(xyz)-1), GLTriangleFace)
# TODO support faceview natively
return convert_arguments(MT, xyz, collect(faces))
end
function convert_arguments(::Type{<:Mesh}, mesh::GeometryBasics.Mesh{N}) where {N}
# Make sure we have normals!
if !hasproperty(mesh, :normals)
n = normals(mesh)
# Normals can be nothing, when it's impossible to calculate the normals (e.g. 2d mesh)
if n !== nothing
mesh = GeometryBasics.pointmeta(mesh, decompose(Vec3f0, n))
end
end
return (GeometryBasics.mesh(mesh, pointtype=Point{N, Float32}, facetype=GLTriangleFace),)
end
function convert_arguments(
MT::Type{<:Mesh},
meshes::AbstractVector{<: Union{AbstractMesh, AbstractPolygon}}
)
return (meshes,)
end
function convert_arguments(
MT::Type{<:Mesh},
xyz::Union{AbstractPolygon, AbstractVector{<: AbstractPoint{2}}}
)
return convert_arguments(MT, triangle_mesh(xyz))
end
function convert_arguments(MT::Type{<:Mesh}, geom::GeometryPrimitive)
# we convert to UV mesh as default, because otherwise the uv informations get lost
# - we can still drop them, but we can't add them later on
return (GeometryBasics.uv_normal_mesh(geom),)
end
"""
convert_arguments(Mesh, x, y, z, indices)::GLNormalMesh
Takes real vectors x, y, z and constructs a triangle mesh out of those, using the
faces in `indices`, which can be integers (every 3 -> one triangle), or GeometryBasics.NgonFace{N, <: Integer}.
"""
function convert_arguments(
T::Type{<: Mesh},
x::RealVector, y::RealVector, z::RealVector,
indices::AbstractVector
)
return convert_arguments(T, Point3f0.(x, y, z), indices)
end
"""
convert_arguments(Mesh, vertices, indices)::GLNormalMesh
Takes `vertices` and `indices`, and creates a triangle mesh out of those.
See [`to_vertices`](@ref) and [`to_triangles`](@ref) for more information about
accepted types.
"""
function convert_arguments(
::Type{<:Mesh},
vertices::AbstractArray,
indices::AbstractArray
)
m = normal_mesh(to_vertices(vertices), to_triangles(indices))
(m,)
end
################################################################################
# Function Conversions #
################################################################################
function convert_arguments(P::PlotFunc, r::AbstractVector, f::Function)
ptype = plottype(P, Lines)
to_plotspec(ptype, convert_arguments(ptype, r, f.(r)))
end
function convert_arguments(P::PlotFunc, i::AbstractInterval, f::Function)
x, y = PlotUtils.adapted_grid(f, endpoints(i))
ptype = plottype(P, Lines)
to_plotspec(ptype, convert_arguments(ptype, x, y))
end
to_tuple(t::Tuple) = t
to_tuple(t) = (t,)
function convert_arguments(P::PlotFunc, f::Function, args...; kwargs...)
tmp =to_tuple(f(args...; kwargs...))
convert_arguments(P, tmp...)
end
# The following `tryrange` code was copied from Plots.jl
# https://github.com/JuliaPlots/Plots.jl/blob/15dc61feb57cba1df524ce5d69f68c2c4ea5b942/src/series.jl#L399-L416
# try some intervals over which the function may be defined
function tryrange(F::AbstractArray, vec)
rets = [tryrange(f, vec) for f in F] # get the preferred for each
maxind = maximum(indexin(rets, vec)) # get the last attempt that succeeded (most likely to fit all)
rets .= [tryrange(f, vec[maxind:maxind]) for f in F] # ensure that all functions compute there
rets[1]
end
function tryrange(F, vec)
for v in vec
try
tmp = F(v)
return v
catch
end
end
error("$F is not a Function, or is not defined at any of the values $vec")
end
################################################################################
# Helper Functions #
################################################################################
to_linspace(interval, N) = range(minimum(interval), stop = maximum(interval), length = N)
"""
Converts the elemen array type to `T1` without making a copy if the element type matches
"""
elconvert(::Type{T1}, x::AbstractArray{T2, N}) where {T1, T2, N} = convert(AbstractArray{T1, N}, x)
float32type(x::Type) = Float32
float32type(::Type{<: RGB}) = RGB{Float32}
float32type(::Type{<: RGBA}) = RGBA{Float32}
float32type(::Type{<: Colorant}) = RGBA{Float32}
float32type(x::AbstractArray{T}) where T = float32type(T)
float32type(x::T) where T = float32type(T)
el32convert(x::AbstractArray) = elconvert(float32type(x), x)
el32convert(x::Observable) = lift(el32convert, x)
el32convert(x) = convert(float32type(x), x)
function el32convert(x::AbstractArray{T, N}) where {T<:Union{Missing, <: Number}, N}
return map(x) do elem
return (ismissing(elem) ? NaN32 : convert(Float32, elem))::Float32
end::Array{Float32, N}
end
"""
to_triangles(indices)
Convert a representation of triangle point indices `indices` to its canonical representation as a `Vector{AbstractPlotting.GLTriangleFace}`. `indices` can be any of the following:
- An `AbstractVector{Int}`, containing groups of 3 1-based indices,
- An `AbstractVector{UIn32}`, containing groups of 3 0-based indices,
- An `AbstractVector` of `TriangleFace` objects,
- An `AbstractMatrix` of `Integer`s, where each row is a triangle.
"""
function to_triangles(x::AbstractVector{Int})
idx0 = UInt32.(x .- 1)
return to_triangles(idx0)
end
function to_triangles(idx0::AbstractVector{UInt32})
reinterpret(GLTriangleFace, idx0)
end
function to_triangles(faces::AbstractVector{TriangleFace{T}}) where T
elconvert(GLTriangleFace, faces)
end
function to_triangles(faces::AbstractMatrix{T}) where T <: Integer
let N = Val(size(faces, 2)), lfaces = faces
broadcast(1:size(faces, 1), N) do fidx, n
to_ndim(GLTriangleFace, ntuple(i-> lfaces[fidx, i], n), 0.0)
end
end
end
"""
to_vertices(v)
Converts a representation of vertices `v` to its canonical representation as a
`Vector{Point3f0}`. `v` can be:
- An `AbstractVector` of 3-element `Tuple`s or `StaticVector`s,
- An `AbstractVector` of `Tuple`s or `StaticVector`s, in which case exta dimensions will
be either truncated or padded with zeros as required,
- An `AbstractMatrix`"
- if `v` has 2 or 3 rows, it will treat each column as a vertex,
- otherwise if `v` has 2 or 3 columns, it will treat each row as a vertex.
"""
function to_vertices(verts::AbstractVector{<: VecTypes{3, T}}) where T
vert3f0 = T != Float32 ? Point3f0.(verts) : verts
return reinterpret(Point3f0, vert3f0)
end
function to_vertices(verts::AbstractVector{<: VecTypes})
to_vertices(to_ndim.(Point3f0, verts, 0.0))
end
function to_vertices(verts::AbstractMatrix{<: Number})
if size(verts, 1) in (2, 3)
to_vertices(verts, Val(1))
elseif size(verts, 2) in (2, 3)
to_vertices(verts, Val(2))
else
error("You are using a matrix for vertices which uses neither dimension to encode the dimension of the space. Please have either size(verts, 1/2) in the range of 2-3. Found: $(size(verts))")
end
end
function to_vertices(verts::AbstractMatrix{T}, ::Val{1}) where T <: Number
N = size(verts, 1)
if T == Float32 && N == 3
reinterpret(Point{N, T}, elconvert(T, vec(verts)))
else
let N = Val(N), lverts = verts
broadcast(1:size(verts, 2), N) do vidx, n
to_ndim(Point3f0, ntuple(i-> lverts[i, vidx], n), 0.0)
end
end
end
end
function to_vertices(verts::AbstractMatrix{T}, ::Val{2}) where T <: Number
let N = Val(size(verts, 2)), lverts = verts
broadcast(1:size(verts, 1), N) do vidx, n
to_ndim(Point3f0, ntuple(i-> lverts[vidx, i], n), 0.0)
end
end
end
################################################################################
# Attribute conversions #
################################################################################
"""
to_color(color)
Converts a `color` symbol (e.g. `:blue`) to a color RGBA.
"""
to_color(color) = convert_attribute(color, key"color"())
"""
to_colormap(cm[, N = 20])
Converts a colormap `cm` symbol (e.g. `:Spectral`) to a colormap RGB array, where `N` specifies the number of color points.
"""
to_colormap(colormap) = convert_attribute(colormap, key"colormap"())
to_rotation(rotation) = convert_attribute(rotation, key"rotation"())
to_font(font) = convert_attribute(font, key"font"())
to_align(align) = convert_attribute(align, key"align"())
to_textsize(textsize) = convert_attribute(textsize, key"textsize"())
convert_attribute(x, key::Key, ::Key) = convert_attribute(x, key)
convert_attribute(s::SceneLike, x, key::Key, ::Key) = convert_attribute(s, x, key)
convert_attribute(s::SceneLike, x, key::Key) = convert_attribute(x, key)
convert_attribute(x, key::Key) = x
convert_attribute(p, ::key"highclip") = to_color(p)
convert_attribute(p::Nothing, ::key"highclip") = p
convert_attribute(p, ::key"lowclip") = to_color(p)
convert_attribute(p::Nothing, ::key"lowclip") = p
convert_attribute(p, ::key"nan_color") = to_color(p)
struct Palette{N}
colors::SArray{Tuple{N},RGBA{Float32},1,N}
i::Ref{UInt8}
Palette(colors) = new{length(colors)}(SVector{length(colors)}(to_color.(colors)), zero(UInt8))
end
Palette(name::Union{String, Symbol}, n = 8) = Palette(to_colormap(name, n))
function convert_attribute(p::Palette{N}, ::key"color") where {N}
p.i[] = p.i[] == N ? one(UInt8) : p.i[] + one(UInt8)
p.colors[p.i[]]
end
convert_attribute(c::Colorant, ::key"color") = convert(RGBA{Float32}, c)
convert_attribute(c::Symbol, k::key"color") = convert_attribute(string(c), k)
function convert_attribute(c::String, ::key"color")
return parse(RGBA{Float32}, c)
end
# Do we really need all colors to be RGBAf0?!
convert_attribute(c::AbstractArray{<: Colorant}, k::key"color") = el32convert(c)
convert_attribute(c::AbstractArray{<: Union{Tuple{Any, Number}, Symbol}}, k::key"color") = to_color.(c)
convert_attribute(c::AbstractArray, ::key"color", ::key"heatmap") = el32convert(c)
convert_attribute(c::Tuple, k::key"color") = convert_attribute.(c, k)
convert_attribute(p::AbstractPattern, k::key"color") = p
function convert_attribute(c::Tuple{T, F}, k::key"color") where {T, F <: Number}
RGBAf0(Colors.color(to_color(c[1])), c[2])
end
convert_attribute(b::Billboard{Float32}, ::key"rotations") = to_rotation(b.rotation)
convert_attribute(b::Billboard{Vector{Float32}}, ::key"rotations") = to_rotation.(b.rotation)
convert_attribute(r::AbstractArray, ::key"rotations") = to_rotation.(r)
convert_attribute(r::StaticVector, ::key"rotations") = to_rotation(r)
convert_attribute(c, ::key"markersize", ::key"scatter") = to_2d_scale(c)
convert_attribute(c, k1::key"markersize", k2::key"meshscatter") = to_3d_scale(c)
to_2d_scale(x::Number) = Vec2f0(x)
to_2d_scale(x::VecTypes) = to_ndim(Vec2f0, x, 1)
to_2d_scale(x::Tuple{<:Number, <:Number}) = to_ndim(Vec2f0, x, 1)
to_2d_scale(x::AbstractVector) = to_2d_scale.(x)
to_3d_scale(x::Number) = Vec3f0(x)
to_3d_scale(x::VecTypes) = to_ndim(Vec3f0, x, 1)
to_3d_scale(x::AbstractVector) = to_3d_scale.(x)
convert_attribute(c::Number, ::key"glowwidth") = Float32(c)
convert_attribute(c, ::key"glowcolor") = to_color(c)
convert_attribute(c, ::key"strokecolor") = to_color(c)
convert_attribute(c::Number, ::key"strokewidth") = Float32(c)
convert_attribute(x::Nothing, ::key"linestyle") = x
"""
`AbstractVector{<:AbstractFloat}` for denoting sequences of fill/nofill. e.g.
[0.5, 0.8, 1.2] will result in 0.5 filled, 0.3 unfilled, 0.4 filled. 1.0 unit is one linewidth!
"""
convert_attribute(A::AbstractVector, ::key"linestyle") = A
"""
A `Symbol` equal to `:dash`, `:dot`, `:dashdot`, `:dashdotdot`
"""
convert_attribute(ls::Union{Symbol,AbstractString}, ::key"linestyle") = line_pattern(ls, :normal)
function convert_attribute(ls::Tuple{<:Union{Symbol,AbstractString},<:Any}, ::key"linestyle")
line_pattern(ls[1], ls[2])
end
function line_pattern(linestyle, gaps)
pattern = line_diff_pattern(linestyle, gaps)
isnothing(pattern) ? pattern : float.([0.0; cumsum(pattern)])
end
"The linestyle patterns are inspired by the LaTeX package tikZ as seen here https://tex.stackexchange.com/questions/45275/tikz-get-values-for-predefined-dash-patterns."
function line_diff_pattern(ls::Symbol, gaps = :normal)
if ls == :solid
nothing
elseif ls == :dash
line_diff_pattern("-", gaps)
elseif ls == :dot
line_diff_pattern(".", gaps)
elseif ls == :dashdot
line_diff_pattern("-.", gaps)
elseif ls == :dashdotdot
line_diff_pattern("-..", gaps)
else
error(
"""
Unkown line style: $ls. Available linestyles are:
:solid, :dash, :dot, :dashdot, :dashdotdot
or a sequence of numbers enumerating the next transparent/opaque region.
This sequence of numbers must be cumulative; 1 unit corresponds to 1 line width.
"""
)
end
end
function line_diff_pattern(ls_str::AbstractString, gaps = :normal)
dot = 1
dash = 3
check_line_pattern(ls_str)
dot_gap, dash_gap = convert_gaps(gaps)
pattern = Float64[]
for i in 1:length(ls_str)
curr_char = ls_str[i]
next_char = i == lastindex(ls_str) ? ls_str[firstindex(ls_str)] : ls_str[i+1]
# push dash or dot
if curr_char == '-'
push!(pattern, dash)
else
push!(pattern, dot)
end
# push the gap (use dot_gap only between two dots)
if (curr_char == '.') && (next_char == '.')
push!(pattern, dot_gap)
else
push!(pattern, dash_gap)
end
end
pattern
end
"Checks if the linestyle format provided as a string contains only dashes and dots"
function check_line_pattern(ls_str)
isnothing(match(r"^[.-]+$", ls_str)) &&
throw(ArgumentError("If you provide a string as linestyle, it must only consist of dashes (-) and dots (.)"))
nothing
end
function convert_gaps(gaps)
error_msg = "You provided the gaps modifier $gaps when specifying the linestyle. The modifier must be `∈ ([:normal, :dense, :loose])`, a real number or a collection of two real numbers."
if gaps isa Symbol
gaps in [:normal, :dense, :loose] || throw(ArgumentError(error_msg))
dot_gaps = (normal = 2, dense = 1, loose = 4)
dash_gaps = (normal = 3, dense = 2, loose = 6)
dot_gap = getproperty(dot_gaps, gaps)
dash_gap = getproperty(dash_gaps, gaps)
elseif gaps isa Real
dot_gap = gaps
dash_gap = gaps
elseif length(gaps) == 2 && eltype(gaps) <: Real
dot_gap, dash_gap = gaps
else
throw(ArgumentError(error_msg))
end
(dot_gap = dot_gap, dash_gap = dash_gap)
end
function convert_attribute(f::Symbol, ::key"frames")
f == :box && return ((true, true), (true, true))
f == :semi && return ((true, false), (true, false))
f == :none && return ((false, false), (false, false))
throw(MethodError("$(string(f)) is not a valid framestyle. Options are `:box`, `:semi` and `:none`"))
end
convert_attribute(f::Tuple{Tuple{Bool,Bool},Tuple{Bool,Bool}}, ::key"frames") = f
convert_attribute(c::Tuple{<: Number, <: Number}, ::key"position") = Point2f0(c[1], c[2])
convert_attribute(c::Tuple{<: Number, <: Number, <: Number}, ::key"position") = Point3f0(c)
convert_attribute(c::VecTypes{N}, ::key"position") where N = Point{N, Float32}(c)
"""
Text align, e.g.:
"""
convert_attribute(x::Tuple{Symbol, Symbol}, ::key"align") = Vec2f0(alignment2num.(x))
convert_attribute(x::Vec2f0, ::key"align") = x
const _font_cache = Dict{String, NativeFont}()
"""
font conversion
a string naming a font, e.g. helvetica
"""
function convert_attribute(x::Union{Symbol, String}, k::key"font")
str = string(x)
get!(_font_cache, str) do
str == "default" && return to_font("Dejavu Sans")
# check if the string points to a font file and load that
if isfile(str)
font = FreeTypeAbstraction.try_load(str)
if isnothing(font)
error("Could not load font file $str")
else
return font
end
end
fontpath = assetpath("fonts")
font = FreeTypeAbstraction.findfont(str; additional_fonts=fontpath)
if font === nothing
@warn("Could not find font $str, using Dejavu Sans")
if "dejavu sans" == lowercase(str)
# since we fall back to dejavu sans, we need to check for recursion
error("Recursion encountered; DejaVu Sans cannot be located in the font path $fontpath")
end
return to_font("dejavu sans")
end
return font
end
end
convert_attribute(x::Vector{String}, k::key"font") = convert_attribute.(x, k)
convert_attribute(x::NativeFont, k::key"font") = x
"""
rotation accepts:
to_rotation(b, quaternion)
to_rotation(b, tuple_float)
to_rotation(b, vec4)
"""
convert_attribute(s::Quaternion, ::key"rotation") = s
function convert_attribute(s::VecTypes{N}, ::key"rotation") where N
if N == 4
Quaternionf0(s...)
elseif N == 3
rotation_between(Vec3f0(0, 0, 1), to_ndim(Vec3f0, s, 0.0))
elseif N == 2
rotation_between(Vec3f0(0, 1, 0), to_ndim(Vec3f0, s, 0.0))
else
error("The $N dimensional vector $s can't be converted to a rotation.")
end
end
function convert_attribute(s::Tuple{VecTypes, AbstractFloat}, ::key"rotation")
qrotation(to_ndim(Vec3f0, s[1], 0.0), s[2])
end
convert_attribute(angle::AbstractFloat, ::key"rotation") = qrotation(Vec3f0(0, 0, 1), Float32(angle))
convert_attribute(r::AbstractVector, k::key"rotation") = to_rotation.(r)
convert_attribute(r::AbstractVector{<: Quaternionf0}, k::key"rotation") = r
convert_attribute(x, k::key"colorrange") = x==nothing ? nothing : Vec2f0(x)
convert_attribute(x, k::key"textsize") = Float32(x)
convert_attribute(x::AbstractVector, k::key"textsize") = convert_attribute.(x, k)
convert_attribute(x::AbstractVector{T}, k::key"textsize") where T <: Number = el32convert(x)
convert_attribute(x::AbstractVector{T}, k::key"textsize") where T <: VecTypes = elconvert(Vec2f0, x)
convert_attribute(x, k::key"linewidth") = Float32(x)
convert_attribute(x::AbstractVector, k::key"linewidth") = el32convert(x)
# ColorBrewer colormaps that support only 8 colors require special handling on the backend, so we show them here.
const colorbrewer_8color_names = String.([
:Accent,
:Dark2,
:Pastel2,
:Set2
])
const plotutils_names = String.(union(
keys(PlotUtils.ColorSchemes.colorschemes),
keys(PlotUtils.COLORSCHEME_ALIASES),
keys(PlotUtils.MISC_COLORSCHEMES)
))
const all_gradient_names = Set(vcat(plotutils_names, colorbrewer_8color_names))
"""
available_gradients()
Prints all available gradient names.
"""
function available_gradients()
println("Gradient Symbol/Strings:")
for name in sort(collect(all_gradient_names))
println(" ", name)
end