This repository has been archived by the owner on Nov 4, 2019. It is now read-only.
forked from MakieOrg/Makie.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converts.jl
730 lines (621 loc) · 17.5 KB
/
converts.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
const VecLike{N, T} = Union{NTuple{N, T}, StaticVector{N, T}}
"""
Any GLAbstraction.Camera
"""
function to_camera(b, x::GLAbstraction.Camera)
x
end
"""
Takes a camera symbol, one of :auto, :perspective, :orthographic, :pixel
"""
function to_camera(b, x::Symbol)
if x in (:auto, :perspective, :orthographic, :pixel)
return x
else
error(":$x is not a supported camera symbol. Try one of :auto, :perspective, :orthographic, :pixel")
end
end
"""
3 Numbers for each dimension
"""
to_3floats(b, x::Tuple) = to_float.(b, x)
to_3floats(b, x::Number) = ntuple(i-> x, Val{3})
to_2floats(b, x::Tuple) = to_float.(b, x)
to_2floats(b, x::Number) = ntuple(i-> to_float(b, x), Val{2})
"""
to_scale(b, s::Number)::Vec
"""
to_scale(b, s::Number) = Vec3f0(s)
"""
to_scale(b, s::VecLike)::Point
"""
to_scale(b, s::VecLike{2}) = Vec3f0(s[1], s[2], 1)
to_scale(b, s::VecLike{3}) = Vec3f0(s)
"""
to_offset(b, s::Number)::Point
"""
to_offset(b, s::Number) = Point3f0(s)
"""
to_scale(b, s::VecLike)::Point
"""
to_offset(b, s::VecLike{2}) = Point3f0(s[1], s[2], 0)
to_offset(b, s::VecLike{3}) = Point3f0(s)
"""
to_rotation(b, vec4)
"""
to_rotation(b, s::VecLike{4}) = Vec4f0(s)
"""
to_rotation(b, quaternion)
"""
to_rotation(b, s::Quaternion) = Vec4f0(s.v1, s.v2, s.v3, s.s)
"""
to_rotation(b, tuple_float)
"""
to_rotation(b, s::Tuple{<:VecLike{3}, <: AbstractFloat}) = qrotation(s[1], s[2])
to_rotation(b, s::Tuple{<:VecLike{2}, <: AbstractFloat}) = qrotation(Vec3f0(s[1][1], s[1][2], 0), s[2])
"""
to_image(b, image)
All kinds of images
"""
to_image(b, image) = image
"""
to_bool(b, bool)
To boolean
"""
to_bool(b, bool) = Bool(bool)
"""
to_bool(b, tuple)
Accepts Tuple of Bool
"""
to_bool(b, x::Tuple) = Bool.(x)
"""
to_index_buffer(b, x::GLBuffer{UInt32})
"""
to_index_buffer(b, x::GLBuffer) = x
"""
`TOrSignal{Int}, AbstractVector{UnitRange{Int}}, TOrSignal{UnitRange{Int}}`
"""
to_index_buffer(b, x::Union{TOrSignal{Int}, VecOrSignal{UnitRange{Int}}, TOrSignal{UnitRange{Int}}}) = x
"""
`AbstractVector{<:Integer}` assumend 1-based indexing
"""
function to_index_buffer(b, x::AbstractVector{I}) where I <: Integer
gpu_mem = GLBuffer(Cuint.(to_value(x) .- 1), buffertype = GL_ELEMENT_ARRAY_BUFFER)
x = lift_node(to_node(x)) do x
val = Cuint[i-1 for i = x]
update!(gpu_mem, val)
end
gpu_mem
end
"""
`AbstractVector{<:Face{2}}` for linesegments
"""
function to_index_buffer(b, x::AbstractVector{I}) where I <: Face{2}
Face{2, GLIndex}.(x)
end
"""
`AbstractVector{UInt32}`, is assumed to be 0 based
"""
function to_index_buffer(b, x::AbstractVector{UInt32})
gpu_mem = GLBuffer(to_value(x), buffertype = GL_ELEMENT_ARRAY_BUFFER)
lift_node(to_node(x)) do x
update!(gpu_mem, x)
end
gpu_mem
end
to_index_buffer(b, x) = error(
"Not a valid index type: $(typeof(x)).
Please choose from Int, Vector{UnitRange{Int}}, Vector{Int} or a signal of either of them"
)
"""
to_positions(b, positionlike)
`NTuple{2, AbstractArray{Float}}` for 2D points
"""
function to_positions(b, x::Tuple{<: AbstractArray, <: AbstractArray})
Point{2, Float32}.(x...)
end
"""
`NTuple{3, AbstractArray{Float}}` for 3D points
"""
function to_positions(b, x::Tuple{<: AbstractArray, <: AbstractArray, <: AbstractArray})
Point{3, Float32}.(x...)
end
"""
`view(AbstractArray{Point}, idx)` for a subset of points. Can be shared (so you can plot subsets of the same data)!
"""
function to_positions(b, x::SubArray)
view(to_positions(b, x.parent), x.indexes...)
end
"""
`AbstractArray{T}` where T needs to have `length` defined and must be convertible to a Point
"""
function to_positions(b, x::AbstractArray{NTuple{N, T}}) where {N, T}
Point{N, Float32}.(x)
end
function to_positions(b, x::AbstractArray{T}) where T
N = if applicable(length, T)
length(T)
else
error("Point type needs to have length defined and needs to be convertible to GeometryTypes point (e.g. tuples, abstract arrays etc.)")
end
Point{N, Float32}.(x)
end
function to_positions(b, x::T) where T <: StaticVector
error("Please use an array of StaticVectors for positions. Found: $T")
end
function to_positions(b, x::GeometryPrimitive)
to_positions(b, decompose(Point, x))
end
function to_positions(b, x::SimpleRectangle)
# TODO fix the order of decompose
to_positions(b, decompose(Point, x)[[1, 2, 4, 3, 1]])
end
function to_positions(b, x)
error("Not a valid position type: $(typeof(x)). Please read the documentation of [`to_positions`](@ref)")
end
"""
Converts a Vec like to a position (Point)
"""
function to_position(b, x::VecLike{N}) where N
Point{N, Float32}(x)
end
"""
to_array(b, arraylike)
`AbstractArray`
"""
to_array(b, x) = collect(x)
"""
to_scalefunc(b, x)
`Function`
"""
to_scalefunc(b, x) = x # TODO implement it
"""
to_text(b, x)
All text
"""
to_text(b, x) = x# TODO implement it
"""
to_font(b, x)
All fonts
"""
to_font(b, x) = x # TODO implement it
"""
to_colornorm(b, norm, intensity)
anything that can be converted to `Vec2f0` (e.g. `Tuple`, `Vector`)
"""
to_colornorm(b, norm, intensity) = Vec2f0(norm)
"""
If colornorm is `nothing` will default to calculate the extrema from `intensity`
"""
function to_colornorm(b, norm::Void, intensity)
nan_extrema(intensity)
end
"""
`AbstractArray`
"""
to_intensity(b, x::AbstractArray) = x # TODO implement
"""
to_surface(b, x::Range)
`Range`
"""
to_surface(b, x::Range) = x
"""
to_surface(b, arraylike)
Anything that can be converted to Matrix/Vector of Float32
"""
to_surface(b, x) = Float32.(x)
const Image = Matrix{T} where T <: Colorant
const _marker_map = Dict(
:rect => '■',
:star5 => '★',
:diamond => '◆',
:hexagon => '⬢',
:cross => '✚',
:xcross => '❌',
:utriangle => '▲',
:dtriangle => '▼',
:ltriangle => '◀',
:rtriangle => '▶',
:pentagon => '⬟',
:octagon => '⯄',
:star4 => '✦',
:star6 => '🟋',
:star8 => '✷',
:vline => '┃',
:hline => '━',
:+ => '+',
:x => 'x',
:circle => '●'
)
"""
available_marker_symbols()
Displays all available marker symbols
"""
function available_marker_symbols()
println("Marker Symbols:")
for (k, v) in _marker_map
println(" ", k, " => ", v)
end
end
"""
to_spritemarker(b, x::Circle)
`GeometryTypes.Circle(Point2(...), radius)`
"""
to_spritemarker(b, x::Circle) = x
"""
to_spritemarker(b, ::Type{Circle})
`Type{GeometryTypes.Circle}`
"""
to_spritemarker(b, ::Type{Circle}) = Circle(Point2f0(0), 1f0)
"""
to_spritemarker(b, ::Type{Rectangle})
`Type{GeometryTypes.Rectangle}`
"""
to_spritemarker(b, ::Type{Rectangle}) = HyperRectangle(Vec2f0(0), Vec2f0(1))
"""
to_spritemarker(b, marker::Char)
Any `Char`, including unicode
"""
to_spritemarker(b, marker::Char) = marker
"""
Matrix of AbstractFloat will be interpreted as a distancefield (negative numbers outside shape, positive inside)
"""
to_spritemarker(b, marker::Matrix{<: AbstractFloat}) = Float32.(marker)
"""
Any AbstractMatrix{<: Colorant} or other image type
"""
to_spritemarker(b, marker::Image) = to_image(marker)
"""
A `Symbol` - Available options can be printed with `available_marker_symbols()`
"""
function to_spritemarker(b, marker::Symbol)
if haskey(_marker_map, marker)
return to_spritemarker(b, _marker_map[marker])
else
warn("Unsupported marker: $marker, using ● instead")
return '●'
end
end
to_spritemarker(b, marker::Vector{Char}) = String(marker)
"""
Vector of anything that is accepted as a single marker will give each point it's own marker.
Note that it needs to be a uniform vector with the same element type!
"""
function to_spritemarker(b, marker::Vector)
marker = map(marker) do sym
to_spritemarker(b, sym)
end
if isa(marker, Vector{Char})
to_spritemarker(b, marker)
else
marker
end
end
# function to_spritemarker(shape::Shape)
# points = Point2f0[GeometryTypes.Vec{2, Float32}(p) for p in zip(shape.x, shape.y)]
# bb = GeometryTypes.AABB(points)
# mini, maxi = minimum(bb), maximum(bb)
# w3 = maxi-mini
# origin, width = Point2f0(mini[1], mini[2]), Point2f0(w3[1], w3[2])
# map!(p -> ((p - origin) ./ width) - 0.5f0, points, points) # normalize and center
# GeometryTypes.GLNormalMesh(points)
# end
# create a marker/shape type
"""
to_static_vec(b, x)
`AbstractArray`
"""
function to_static_vec(b, x::AbstractArray)
Vec(ntuple(length(x)) do i
x[i]
end)
end
"""
Any `StaticVector`
"""
to_static_vec(b, x::StaticVector) = Vec(x)
"""
`NTuple`
"""
to_static_vec(b, x::NTuple{N}) where N = Vec(x)
"""
Abstract array of which the elements can be converted to vec
"""
function to_static_vec(b, x::AbstractArray{T}) where T <: Union{Tuple, SVector, AbstractArray}
to_static_vec.(b, x)
end
"""
to_rotations(b, x)
`Billboard()` for a rotation that will always face the camera
"""
to_rotations(b, x::Billboard) = x
"""
Any AbstractArray which elements can be converted to Vec4 (as a quaternion x, y, z, w)
"""
to_rotations(b, x::AbstractVector) = to_static_vec(b, x)
"""
to_markersize2d(b, x)
Anything that can be converted to `Vec2f0` for x, y scale
"""
to_markersize2d(b, x::Number) = Vec2f0(x)
to_markersize2d(b, x::Tuple) = Vec2f0(x)
to_markersize2d(b, x::StaticVector) = Vec2f0(x)
to_markersize2d(b, x::AbstractVector) = Vec2f0.(x)
# TODO generically implement these to share implementation with to_position etc
"""
to_markersize3d(b, x)
Anything that can be converted to `Vec3f0` for x, y, z scale
"""
to_markersize3d(b, x::Number) = Vec3f0(x)
to_markersize3d(b, x::Tuple) = Vec3f0(x)
to_markersize3d(b, x::StaticVector) = Vec3f0(x)
to_markersize3d(b, x::AbstractVector) = Vec3f0.(x)
"""
to_linestyle(b, x)
`Nothing` for no style
"""
to_linestyle(b, x::Void) = 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!
"""
to_linestyle(b, A::AbstractVector) = A
"""
A `Symbol` equal to `:dash`, `:dot`, `:dashdot`, `:dashdotdot`
"""
function to_linestyle(b, ls::Symbol)
return if ls == :dash
[0.0, 1.0, 2.0, 3.0, 4.0]
elseif ls == :dot
tick, gap = 1/2, 1/4
[0.0, tick, tick+gap, 2tick+gap, 2tick+2gap]
elseif ls == :dashdot
dtick, dgap = 1.0, 1.0
ptick, pgap = 1/2, 1/4
[0.0, dtick, dtick+dgap, dtick+dgap+ptick, dtick+dgap+ptick+pgap]
elseif ls == :dashdotdot
dtick, dgap = 1.0, 1.0
ptick, pgap = 1/2, 1/4
[0.0, dtick, dtick+dgap, dtick+dgap+ptick, dtick+dgap+ptick+pgap, dtick+dgap+ptick+pgap+ptick, dtick+dgap+ptick+pgap+ptick+pgap]
else
error("Unkown line style: $linestyle. Available: :dash, :dot, :dashdot, :dashdotdot or a sequence of numbers enumerating the next transparent/opaque region")
end
end
"""
to_normals(b, x)
Vector{Normal{3}}
"""
to_normals(b, x) = x
"""
to_faces(b, x)
Any array of NTuple/GeometryTypes.Face
"""
function to_faces(b, x::AbstractVector{NTuple{N, TI}}) where {N, TI <: Integer}
to_faces(reinterpret(Face{N, TI}, x))
end
function to_faces(b, faces::AbstractVector{<: Face})
decompose(GLTriangle, faces)
end
function to_faces(b, faces::AbstractVector{GLTriangle})
faces
end
function to_faces(b, x::Void)
x
end
function to_faces(b, x::Vector{Int})
if length(x) % 3 != 0
error("Int indices need to represent triangles, therefore need to be a multiple of three. Found: $(length(x))")
end
reinterpret(GLTriangle, UInt32.(x .- 1))
end
"""
to_mesh(b, meshlike)
`AbstractMesh`
"""
function to_mesh(b, mesh::AbstractMesh)
mesh
end
function to_mesh(b, geom::GeometryPrimitive)
GLNormalMesh(geom)
end
function to_mesh(b, verts, faces, colors, attribute_id::Node{Void})
lift_node(verts, faces) do v, f
GLPlainMesh(v, f)
end
end
function to_mesh(b, verts, faces, colors::Node{<:Colorant}, attribute_id::Node{Void})
lift_node(verts, faces) do v, f
GLNormalMesh(vertices = v, faces = f)
end
end
function to_mesh(b, verts, faces, colors::AbstractVector, attribute_id::Node{Void})
lift_node(verts, faces, colors) do v, f, c
if length(c) != length(v)
error("You need one color per vertex. Found: $(length(v)) vertices, and $(length(c)) colors")
end
GLNormalVertexcolorMesh(vertices = v, faces = f, color = c)
end
end
function to_mesh(verts, faces, colors::AbstractVector, attribute_id::AbstractVector)
lift_node(verts, faces, colors, attribute_id) do v, f, c, id
if length(id) != length(v)
error("You need one attribute per vertex. Found: $(length(v)) vertices, and $(length(id)) attributes")
end
GLNormalAttributeMesh(
vertices = v, faces = f,
attributes = c, attribute_id = id
)
end
end
"""
to_attribut_id(b, x)
Index into Mesh attributes, Vector{Integer}
"""
to_attribut_id(backend, x) = x
"""
to_float(b, x)
Any Object convertible to Floatingpoint
"""
to_float(b, x) = Float32(x)
to_color(c) = to_color(current_backend[], c)
"""
to_color(b, x)
`Colors.Colorants`
"""
to_color(b, c::Colorant) = RGBA{Float32}(c)
"""
A `Symbol` naming a color, e.g. `:black`
"""
to_color(b, c::Symbol) = to_color(b, string(c))
"""
A `String` naming a color, e.g. `:black` or html style `#rrggbb`
"""
to_color(b, c::String) = parse(RGBA{Float32}, c)
to_color(b, c::UniqueColorIter) = to_color(b, next(c))
"""
A Tuple or Array with elements that `to_color` accepts.
If Array is a Matrix it will get interpreted as an Image
"""
to_color(b, c::Union{Tuple, AbstractArray}) = to_color.(b, c)
"""
Tuple{<: ColorLike, <: AbstractFloat} for a transparent color
"""
to_color(b, c::Tuple{T, F}) where {T, F <: AbstractFloat} = RGBAf0(Colors.color(to_color(b, c[1])), c[2])
const colorbrewer_names = Symbol[
# All sequential color schemes can have between 3 and 9 colors. The available sequential color schemes are:
:Blues,
:Oranges,
:Greens,
:Reds,
:Purples,
:Greys,
:OrRd,
:GnBu,
:PuBu,
:PuRd,
:BuPu,
:BuGn,
:YlGn,
:RdPu,
:YlOrBr,
:YlGnBu,
:YlOrRd,
:PuBuGn,
# All diverging color schemes can have between 3 and 11 colors. The available diverging color schemes are:
:Spectral,
:RdYlGn,
:RdBu,
:PiYG,
:PRGn,
:RdYlBu,
:BrBG,
:RdGy,
:PuOr,
#The number of colors a qualitative color scheme can have depends on the scheme. The available qualitative color schemes are:
:Name,
:Set1,
:Set2,
:Set3,
:Dark2,
:Accent,
:Paired,
:Pastel1,
:Pastel2
]
"""
available_gradients()
Prints all available gradient names
"""
function available_gradients()
println("Gradient Symbol/Strings:")
for name in colorbrewer_names
println(" ", name)
end
end
"""
to_colormap(b, x)
An `AbstractVector{T}` with any object that [`to_color`](@ref) accepts
"""
to_colormap(b, cm::AbstractVector) = RGBAf0.(cm)
"""
Tuple(A, B) or Pair{A, B} with any object that [`to_color`](@ref) accepts
"""
function to_colormap(b, cs::Union{Tuple, Pair})
[to_color.(cs)...]
end
to_colormap(val) = to_colormap(current_backend[], val)
"""
A Symbol/String naming the gradient. For more on what names are available please see: `available_gradients()
"""
function to_colormap(b, cs::Union{String, Symbol})
cs_sym = Symbol(cs)
if cs_sym in colorbrewer_names
ColorBrewer.palette(string(cs_sym), 9)
else
#TODO integrate PlotUtils color gradients
error("There is no color gradient named: $cs")
end
end
"""
to_spatial_order(b, x)
"xy" or "yx"
"""
function to_spatial_order(b, x)
if !(x in ("yx", "xy"))
error("Spatial order must be \"yx\" or \"xy\". Found: $x")
end
x
end
"""
:xy or :yx
"""
to_spatial_order(b, x::Symbol) = to_spatial_order(b, string(x))
"""
to_interval(b, x)
`Tuple{<: Number, <: Number}`
"""
function to_interval(b, x)
if isa(x, Tuple{<: Number, <: Number})
return x
else
error("Not an accepted value for interval. Please have a look at the documentation for to_interval")
end
end
"""
Pair{<: Number, <: Number} e.g. 2 => 100
"""
to_interval(b, x::Pair{<: Number, <: Number}) = to_interval(b, (x...,))
"""
`AbstractVector` will be interpreted as an interval from minimum to maximum
"""
to_interval(b, x::AbstractVector) = to_interval(b, (minimum(x), maximum(x)))
using GLVisualize: IsoValue, Absorption, MaximumIntensityProjection, AbsorptionRGBA, IndexedAbsorptionRGBA
export IsoValue, Absorption, MaximumIntensityProjection, AbsorptionRGBA, IndexedAbsorptionRGBA
"""
to_volume_algorithm(b, x)
Enum values: `IsoValue` `Absorption` `MaximumIntensityProjection` `AbsorptionRGBA` `IndexedAbsorptionRGBA`
"""
function to_volume_algorithm(b, value)
if isa(value, GLVisualize.RaymarchAlgorithm)
return Int32(value)
elseif isa(value, Int32) && value in 0:5
return value
else
error("$value is not a valid volume algorithm. Please have a look at the documentation of `to_volume_algorithm`")
end
end
"""
Symbol/String: iso, absorption, mip, absorptionrgba, indexedabsorption
"""
function to_volume_algorithm(b, value::Union{Symbol, String})
vals = Dict(
:iso => IsoValue,
:absorption => Absorption,
:mip => MaximumIntensityProjection,
:absorptionrgba => AbsorptionRGBA,
:indexedabsorption => IndexedAbsorptionRGBA,
)
to_volume_algorithm(b, get(vals, Symbol(value)) do
error("$value not a valid volume algorithm. Needs to be in $(keys(vals))")
end)
end