Skip to content

Commit

Permalink
enhance surfaceplot when using lines
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Feb 7, 2022
1 parent f2f1e87 commit 7490a7b
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 44 deletions.
1 change: 0 additions & 1 deletion .github/workflows/format-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jobs:
- uses: actions/checkout@v2
- name: Install JuliaFormatter and format
run: |
julia -e 'using Pkg; Pkg.add(PackageSpec(name="Tokenize", version="0.5.21"))'
julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
julia -e 'using JuliaFormatter; format(["src", "test"], verbose=true)'
- name: Format check
Expand Down
6 changes: 2 additions & 4 deletions src/interface/isosurface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Extract and plot isosurface from volumetric data, or implicit function.
# Usage
isosurface(x, y, z, V; $(keywords((isovalue = 0, centroid = true); add = (Z_DESCRIPTION..., PROJ_DESCRIPTION..., :canvas), remove = (:blend, :grid))))
isosurface(x, y, z, V; $(keywords((isovalue = 0, centroid = true); add = (Z_DESCRIPTION..., PROJ_DESCRIPTION..., :canvas), remove = (:blend, :grid, :name))))
# Arguments
Expand Down Expand Up @@ -59,7 +59,6 @@ function isosurface(
V::Union{Function,AbstractArray};
canvas::Type = BrailleCanvas,
color::UserColorType = KEYWORDS.color,
name::AbstractString = KEYWORDS.name,
projection::Union{MVP,Symbol} = KEYWORDS.projection,
isovalue::Number = 0,
centroid::Bool = true,
Expand Down Expand Up @@ -98,7 +97,6 @@ function isosurface!(
z::AbstractVector,
V::AbstractArray;
color::UserColorType = KEYWORDS.color,
name::AbstractString = KEYWORDS.name,
isovalue::Number = 0,
centroid::Bool = true,
legacy::Bool = false,
Expand Down Expand Up @@ -140,5 +138,5 @@ function isosurface!(
end
end
# triangles vertices or centroid
scatterplot!(plot, xs, ys, zs; color = cs, name = name)
scatterplot!(plot, xs, ys, zs; color = cs)
end
3 changes: 2 additions & 1 deletion src/interface/lineplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ This means that the two vectors must be of the same length and ordering.
# Usage
lineplot([x], y; $(keywords(; add = (:canvas,)))
lineplot(fun, [start], [stop]; kw...)
# Arguments
Expand Down Expand Up @@ -94,6 +93,8 @@ function lineplot!(
col_vec = color isa AbstractVector
name == "" || label!(plot, :r, string(name), col_vec ? first(color) : color)
if col_vec
length(x) == length(y) == length(color) ||
throw(ArgumentError("Invalid color vector"))
for i in eachindex(color)
lines!(plot, x[i], y[i], z === nothing ? z : z[i], color[i])
end
Expand Down
66 changes: 48 additions & 18 deletions src/interface/surfaceplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
surfaceplot(x, y, A; kw...)
Draws a 3D surface plot on a new canvas (masking values using `NaN`s is supported).
For plotting a slice one can pass an anonymous function which maps to a constant height: `hscale = x -> some_constant_height`.
For plotting a slice one can pass an anonymous function which maps to a constant height: `hscale = h -> some_constant_height`.
# Usage
Expand All @@ -13,9 +13,9 @@ For plotting a slice one can pass an anonymous function which maps to a constant
$(arguments(
(
A = "`Matrix` of surface heights, or `Function` evaluated as `f(x, y)`",
lines = "use `lineplot` instead of `scatterplot`",
lines = "use `lineplot` instead of `scatterplot` (for regular increasing data)",
hscale = "scale heights (`:identity`, `:aspect`, tuple of (min, max) values, or arbitrary scale function)",
); add = (Z_DESCRIPTION..., PROJ_DESCRIPTION..., :x, :y, :canvas), remove = (:blend, :grid)
); add = (Z_DESCRIPTION..., PROJ_DESCRIPTION..., :x, :y, :canvas), remove = (:blend, :grid, :name)
))
# Author(s)
Expand Down Expand Up @@ -56,7 +56,6 @@ function surfaceplot(
y::AbstractVecOrMat,
A::Union{Function,AbstractVecOrMat};
canvas::Type = BrailleCanvas,
name::AbstractString = KEYWORDS.name,
color::UserColorType = nothing, # NOTE: nothing as default (uses colormap), but allow single color
colormap = KEYWORDS.colormap,
colorbar::Bool = true,
Expand Down Expand Up @@ -94,9 +93,6 @@ function surfaceplot(
throw(ArgumentError("hscale=$hscale not understood"))
end

length(X) == length(Y) == length(Z) == length(H) ||
throw(DimensionMismatch("X, Y, Z and H must have same length"))

plot =
Plot(collect(ex), collect(ey), collect(ez), canvas; projection = projection, kw...)
surfaceplot!(
Expand All @@ -105,7 +101,6 @@ function surfaceplot(
Y,
Z,
H;
name = name,
color = color,
colormap = colormap,
colorbar = colorbar,
Expand All @@ -115,27 +110,62 @@ end

function surfaceplot!(
plot::Plot{<:Canvas},
X::AbstractVecOrMat,
Y::AbstractVecOrMat,
Z::AbstractVecOrMat,
H::AbstractVecOrMat;
name::AbstractString = KEYWORDS.name,
X::AbstractMatrix,
Y::AbstractMatrix,
Z::AbstractMatrix,
H::AbstractMatrix;
color::UserColorType = nothing,
colormap = KEYWORDS.colormap,
colorbar::Bool = true,
lines::Bool = false,
)
length(X) == length(Y) == length(Z) == length(H) ||
throw(DimensionMismatch("X, Y, Z and H must have same length"))

plot.colorbar_lim = mh, Mh = NaNMath.extrema(as_float(H))
plot.colormap = callback = colormap_callback(colormap)
plot.colorbar = colorbar && color === nothing

if color === nothing
color =
UserColorType[isfinite(h) ? callback(h, mh, Mh) : nothing for h in @view(H[:])]
if (cmapped = color === nothing)
color = map(h -> isfinite(h) ? callback(h, mh, Mh) : nothing, H)
end

callable = lines ? lineplot! : scatterplot!
callable(plot, @view(X[:]), @view(Y[:]), @view(Z[:]); color = color, name = name)
if lines
nx, ny = size(X)
@inbounds for j in axes(X, 2), i in axes(X, 1)
c = cmapped ? color[i, j] : color
scatter = false
if i < nx
lines!(plot, X[i:(i + 1), j], Y[i:(i + 1), j], Z[i:(i + 1), j], c)
else
scatter = true
end
if j < ny
lines!(plot, X[i, j:(j + 1)], Y[i, j:(j + 1)], Z[i, j:(j + 1)], c)
else
scatter = true
end
if i < nx && j < ny
lines!(
plot,
[X[i, j], X[i + 1, j + 1]],
[Y[i, j], Y[i + 1, j + 1]],
[Z[i, j], Z[i + 1, j + 1]],
c,
)
end
scatter && points!(plot, X[i, j], Y[i, j], Z[i, j], c)
end
else
scatterplot!(
plot,
@view(X[:]),
@view(Y[:]),
@view(Z[:]);
color = cmapped ? @view(color[:]) : color,
)
end
plot
end

"""
Expand Down
18 changes: 0 additions & 18 deletions test/references/surfaceplot/slice.txt

This file was deleted.

Loading

0 comments on commit 7490a7b

Please sign in to comment.