Skip to content

Commit

Permalink
Merge pull request JuliaPlots#3134 from kellertuer/quiver3
Browse files Browse the repository at this point in the history
Proposes a 3D quiver
  • Loading branch information
daschw authored Nov 13, 2020
2 parents 967bc89 + 7e8eb5a commit 5861b1f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
22 changes: 22 additions & 0 deletions src/examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,27 @@ const _examples = PlotExample[
plot!(sin, -π, π, lw=3, color=:red)
end]
),
PlotExample(
"3d quiver",
"",
[quote
using Plots

ϕs = range(-π, π, length=50)
θs = range(0, π, length=25)
θqs = range(1, π-1, length=25)

x = vec([sin(θ) * cos(ϕ) for (ϕ, θ) in Iterators.product(ϕs, θs)])
y = vec([sin(θ) * sin(ϕ) for (ϕ, θ) in Iterators.product(ϕs, θs)])
z = vec([cos(θ) for (ϕ, θ) in Iterators.product(ϕs, θs)])

u = 0.1 * vec([sin(θ) * cos(ϕ) for (ϕ, θ) in Iterators.product(ϕs, θqs)])
v = 0.1 * vec([sin(θ) * sin(ϕ) for (ϕ, θ) in Iterators.product(ϕs, θqs)])
w = 0.1 * vec([cos(θ) for (ϕ, θ) in Iterators.product(ϕs, θqs)])

quiver(x,y,z, quiver=(u,v,w))
end]
),
]

# Some constants for PlotDocs and PlotReferenceImages
Expand All @@ -1099,6 +1120,7 @@ _backend_skips = Dict(
32, # spy
49, # polar heatmap
51, # image with custom axes
52, # 3d quiver
],
)

Expand Down
42 changes: 29 additions & 13 deletions src/recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1197,35 +1197,51 @@ function quiver_using_arrows(plotattributes::AKW)
if !isa(plotattributes[:arrow], Arrow)
plotattributes[:arrow] = arrow()
end

is_3d = haskey(plotattributes,:z) && !isnothing(plotattributes[:z])
velocity = error_zipit(plotattributes[:quiver])
xorig, yorig = plotattributes[:x], plotattributes[:y]
zorig = is_3d ? plotattributes[:z] : []

# for each point, we create an arrow of velocity vi, translated to the x/y coordinates
x, y = zeros(0), zeros(0)
for i = 1:max(length(xorig), length(yorig))
is_3d && ( z = zeros(0))
for i = 1:max(length(xorig), length(yorig), is_3d ? 0 : length(zorig))
# get the starting position
xi = _cycle(xorig, i)
yi = _cycle(yorig, i)

zi = is_3d ? _cycle(zorig, i) : 0
# get the velocity
vi = _cycle(velocity, i)
vx, vy = if istuple(vi)
first(vi), last(vi)
elseif isscalar(vi)
vi, vi
elseif isa(vi, Function)
vi(xi, yi)
else
error("unexpected vi type $(typeof(vi)) for quiver: $vi")
if is_3d
vx, vy, vz = if istuple(vi)
vi[1], vi[2], vi[3]
elseif isscalar(vi)
vi, vi, vi
elseif isa(vi, Function)
vi(xi, yi, zi)
else
error("unexpected vi type $(typeof(vi)) for quiver: $vi")
end
else # 2D quiver
vx, vy = if istuple(vi)
first(vi), last(vi)
elseif isscalar(vi)
vi, vi
elseif isa(vi, Function)
vi(xi, yi)
else
error("unexpected vi type $(typeof(vi)) for quiver: $vi")
end
end

# add the points
nanappend!(x, [xi, xi + vx, NaN])
nanappend!(y, [yi, yi + vy, NaN])
is_3d && nanappend!(z, [zi, zi + vz, NaN])
end

plotattributes[:x], plotattributes[:y] = x, y
if is_3d
plotattributes[:z] = z
end
# KW[plotattributes]
end

Expand Down

0 comments on commit 5861b1f

Please sign in to comment.