Skip to content

Commit

Permalink
update for 3D support in Plots.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Feb 2, 2022
1 parent 0bb0fea commit eeba67f
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/description.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function keywords(
remove::Tuple = (),
)
all_kw = (; KEYWORDS..., extra...)
candidates = keys(extra) filter(x -> x add default, keys(KEYWORDS)) # extra does first !
candidates = keys(extra) filter(x -> x add default, keys(KEYWORDS)) # extra goes first !
kw = filter(x -> x setdiff(exclude remove, add), candidates)
@assert allunique(kw) # extra check
join((k isa Symbol ? "$k = $(all_kw[k] |> repr)" : k for k in kw), ", ")
Expand Down
6 changes: 3 additions & 3 deletions src/interface/isosurface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ julia> isosurface(-1:.1:1, -1:.1:1, -1:.1:1, torus; xlim = (-.5, .5), ylim = (-.
│⠀⠀⠀⠀⠜⠤⠁⡀⠂⢂⢈⠄⠦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡹⠠⡁⡐⠐⢀⠈⢤⢚⠀⠀⠀⠀│
│⠀⠀⠀⠀⠸⠗⠅⡀⠂⢂⢀⠂⢂⠃⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠂⡑⠐⡀⡐⠐⠀⠨⢔⠏⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠉⠄⠌⠰⠀⡀⠂⢂⠀⡐⠐⠌⠔⠐⠄⠡⠂⠢⠡⠂⢂⠀⡐⠐⣀⠐⠠⡁⡃⠁⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠈⠁⠄⠌⠀⡆⢀⠐⠐⢀⠐⠐⠀⠄⠂⠂⡀⠂⡂⡀⠂⢂⠀⡂⠐⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠥⠀⡂⡃⠇⠃⡁⡡⠨⡈⡨⠈⠬⠨⠢⢈⠠⠑⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠈⠁⠄⠌⠀⡆⢀⠐⠐⢀⠐⠐⠀⠄⠂⠂⡀⠂⡂⡀⠂⢂⠀⡂⠐⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠥⠀⡂⡃⠇⠃⡁⡡⠨⡈⡨⠈⠬⠨⠢⢈⠠⠑⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠃⠓⠀⠊⠐⠂⠊⠈⠈⠘⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⣀⠼⢄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
-0.5 │⠔⠉⠀⠀⠀⠈⠑⠤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
-0.5 │⠔⠉⠀⠀⠀⠈⠑⠤⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
└────────────────────────────────────────┘
⠀-0.5⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀0.5⠀
```
Expand Down
14 changes: 11 additions & 3 deletions src/interface/lineplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,22 @@ function lineplot!(
x::AbstractVector,
y::AbstractVector,
z::Union{AbstractVector,Nothing} = nothing;
color::UserColorType = KEYWORDS.color,
color::Union{UserColorType,AbstractVector} = KEYWORDS.color,
name::AbstractString = KEYWORDS.name,
head_tail::Union{Nothing,Symbol} = nothing,
)
color = color == :auto ? next_color!(plot) : color
name == "" || label!(plot, :r, string(name), color)
lines!(plot, x, y, z, color)
name == "" ||
label!(plot, :r, string(name), color isa AbstractVector ? color[1] : color)
if color isa AbstractVector
for i in 1:length(color)
lines!(plot, x[i], y[i], z === nothing ? z : z[i], color[i])
end
else
lines!(plot, x, y, z, color)
end
z === nothing || return plot # 3D arrows unsupported for now
color isa AbstractVector && return plot
(head_tail === nothing || length(x) == 0 || length(y) == 0) && return plot
head_tail_color = (col = crayon_256_color(color)) === nothing ? nothing : ~col
if head_tail in (:head, :both)
Expand Down
41 changes: 28 additions & 13 deletions src/interface/surfaceplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ julia> surfaceplot(-8:.5:8, -8:.5:8, sombrero)
│⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⣯⡭⠭⠒⢊⠔⢱⢍⡘⡖⣳⢃⡩⡎⠢⡑⠒⠭⢭⣽⣷⣄⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│
│⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣞⠭⠭⠥⢒⣿⣋⣵⢜⡧⣮⣙⣿⡒⠬⠭⠭⣳⣿⣿⣿⣷⣄⠀⠀⠀⠀⠀⠀│ │▄▄│
│⠀⠀⠀⠀⠴⢾⠿⠯⠛⠛⠽⢿⣯⣶⣮⡽⠛⠫⡉⠃⠙⢉⠝⠛⢯⣵⣶⣽⡿⠯⠛⠛⠽⠿⡷⠦⠀⠀⠀⠀│ │▄▄│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢿⣿⣿⢶⣶⣶⡺⣗⣶⣶⡶⣿⣿⡿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│
│⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⠛⢷⢵⢾⡷⡮⡾⠛⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│
│⠀⢀⡠⠼⠤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠢⢕⡯⠔⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│
-1 │⠊⠁⠀⠀⠀⠀⠉⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ └──┘ 0
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢿⣿⣿⢶⣶⣶⡺⣗⣶⣶⡶⣿⣿⡿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│
│⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⠛⢷⢵⢾⡷⡮⡾⠛⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│
│⠀⠀⣀⠤⠧⢄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠢⢕⡯⠔⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│
-1 │⠐⠉⠀⠀⠀⠀⠈⠑⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ └──┘ 0
└────────────────────────────────────────┘ ⠀⠀⠀⠀
⠀-1⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀1⠀ ⠀⠀⠀⠀
```
Expand All @@ -52,16 +52,17 @@ julia> surfaceplot(-8:.5:8, -8:.5:8, sombrero)
function surfaceplot(
x::AbstractVecOrMat,
y::AbstractVecOrMat,
A::Union{Function,AbstractMatrix};
A::Union{Function,AbstractVecOrMat};
canvas::Type = BrailleCanvas,
name::AbstractString = KEYWORDS.name,
projection::Union{MVP,Symbol} = KEYWORDS.projection,
color::UserColorType = nothing, # NOTE: nothing here to override colormap
colormap = KEYWORDS.colormap,
colorbar::Bool = true,
lines::Bool = false,
kwargs...,
)
X, Y = if x isa AbstractVector && y isa AbstractVector
X, Y = if x isa AbstractVector && y isa AbstractVector && !(A isa AbstractVector)
repeat(x', length(y), 1), repeat(y, 1, length(x))
else
x, y
Expand All @@ -85,26 +86,40 @@ function surfaceplot(
colorbar = colorbar,
kwargs...,
)
surfaceplot!(plot, X, Y, Z; name = name, colormap = colormap, lines = lines)
surfaceplot!(
plot,
X,
Y,
Z;
name = name,
colormap = colormap,
color = color,
lines = lines,
)
plot
end

function surfaceplot!(
plot::Plot{<:Canvas},
X::AbstractMatrix,
Y::AbstractMatrix,
Z::AbstractMatrix;
X::AbstractVecOrMat,
Y::AbstractVecOrMat,
Z::AbstractVecOrMat;
name::AbstractString = KEYWORDS.name,
colormap = KEYWORDS.colormap,
lines::Bool = false,
color::UserColorType = nothing, # NOTE: nothing here to override colormap
colormap = KEYWORDS.colormap,
kw...,
)
name == "" || label!(plot, :r, string(name))
plot.colormap = callback = colormap_callback(colormap)

mZ, MZ = NaNMath.extrema(Z)
color = UserColorType[isfinite(z) ? callback(z, mZ, MZ) : nothing for z in @view(Z[:])]
if color === nothing
color =
UserColorType[isfinite(z) ? callback(z, mZ, MZ) : nothing for z in @view(Z[:])]
end
callable = lines ? lineplot! : scatterplot!
callable(plot, @view(X[:]), @view(Y[:]), @view(Z[:]); color = color, name = name)
callable(plot, @view(X[:]), @view(Y[:]), @view(Z[:]); color = color, name = name, kw...)
plot
end

Expand Down
12 changes: 6 additions & 6 deletions src/volume.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function center_diagonal(x, y, z)
ly = My - my
lz = Mz - mz

[mx + 0.5lx, my + 0.5ly, mz + 0.5lz], (lx^2 + ly^2 + lz^2)
[mx + 0.5lx, my + 0.5ly, mz + 0.5lz], [lx, ly, lz], (lx^2 + ly^2 + lz^2)
end

function view_matrix(center, distance, elevation, azimuth, up)
Expand Down Expand Up @@ -201,7 +201,7 @@ Model - View - Projection transformation matrix.
struct MVP{T}
A::Matrix{T}
view_dir::SVector{3,T}
distance::T
len::SVector{3,T}
ortho::Bool
MVP(M::AbstractMatrix{T}, V::AbstractMatrix{T}, P::Projection) where {T} =
new{T}(P.A * V * M, [0, 0, 0], 1, P isa Orthographic)
Expand All @@ -225,7 +225,7 @@ struct MVP{T}
@assert -180 azimuth 180
@assert -90 elevation 90
ortho = projection === :orthographic
ctr, diag = center_diagonal(x, y, z)
ctr, len, diag = center_diagonal(x, y, z)
# half the diagonal (cam distance to the center)
dist = (diag / 2) / zoom / (ortho ? 1 : 2)
δ = 100eps() # avoid `NaN`s in `V` when `elevation` is close to ±90
Expand All @@ -240,7 +240,7 @@ struct MVP{T}
else
Perspective(-dist, dist, -dist, dist, 1.0, 100.0)
end
new{float(eltype(x))}(P.A * V * M, view_dir, dist, ortho)
new{float(eltype(x))}(P.A * V * M, view_dir, len, ortho)
end
end

Expand Down Expand Up @@ -304,11 +304,11 @@ If `p = (x, y)` is given, draws at screen coordinates (only correct in orthograp
"""
function draw_axes!(plot, p = [0, 0, 0], len = nothing)
T = plot.projection
l = len === nothing ? T.distance / 4 : len
l = len === nothing ? T.len ./ 4 : len

axis(p, d) = begin
e = copy(p)
e[d] += l
e[d] += l[d]
T(hcat(p, e))
end

Expand Down
4 changes: 2 additions & 2 deletions test/references/isosurface/torus.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
│⠀⠀⠀⠀⠜⠤⠁⡀⠂⢂⢈⠄⠦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡹⠠⡁⡐⠐⢀⠈⢤⢚⠀⠀⠀⠀│
│⠀⠀⠀⠀⠸⠗⠅⡀⠂⢂⢀⠂⢂⠃⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠂⡑⠐⡀⡐⠐⠀⠨⢔⠏⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠉⠄⠌⠰⠀⡀⠂⢂⠀⡐⠐⠌⠔⠐⠄⠡⠂⠢⠡⠂⢂⠀⡐⠐⣀⠐⠠⡁⡃⠁⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠈⠁⠄⠌⠀⡆⢀⠐⠐⢀⠐⠐⠀⠄⠂⠂⡀⠂⡂⡀⠂⢂⠀⡂⠐⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠥⠀⡂⡃⠇⠃⡁⡡⠨⡈⡨⠈⠬⠨⠢⢈⠠⠑⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⢀⠀⠀⠀⠈⠁⠄⠌⠀⡆⢀⠐⠐⢀⠐⠐⠀⠄⠂⠂⡀⠂⡂⡀⠂⢂⠀⡂⠐⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠥⠀⡂⡃⠇⠃⡁⡡⠨⡈⡨⠈⠬⠨⠢⢈⠠⠑⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠃⠓⠀⠊⠐⠂⠊⠈⠈⠘⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⣀⠼⢄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
-0.5 │⠔⠉⠀⠀⠀⠈⠑⠤⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
Expand Down
2 changes: 1 addition & 1 deletion test/references/surfaceplot/sombrero.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
│⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⣯⡭⠭⠒⢊⠔⢱⢍⡘⡖⣳⢃⡩⡎⠢⡑⠒⠭⢭⣽⣷⣄⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│
│⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣞⠭⠭⠥⢒⣿⣋⣵⢜⡧⣮⣙⣿⡒⠬⠭⠭⣳⣿⣿⣿⣷⣄⠀⠀⠀⠀⠀⠀│ │▄▄│
│⠀⠀⠀⠀⠴⢾⠿⠯⠛⠛⠽⢿⣯⣶⣮⡽⠛⠫⡉⠃⠙⢉⠝⠛⢯⣵⣶⣽⡿⠯⠛⠛⠽⠿⡷⠦⠀⠀⠀⠀│ │▄▄│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢿⣿⣿⢶⣶⣶⡺⣗⣶⣶⡶⣿⣿⡿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│
│⠀⠀⠀⠀⡀⠀⠀⠀⠀⠀⠀⠀⠈⢿⣿⣿⢶⣶⣶⡺⣗⣶⣶⡶⣿⣿⡿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│
│⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⠛⢷⢵⢾⡷⡮⡾⠛⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│
│⠀⠀⣀⠤⠧⢄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠢⢕⡯⠔⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│
-1 │⠐⠉⠀⠀⠀⠀⠈⠑⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ └──┘ 0
Expand Down
Loading

0 comments on commit eeba67f

Please sign in to comment.