Skip to content

Commit

Permalink
fix add_arrows!
Browse files Browse the repository at this point in the history
  • Loading branch information
jake-w-liu committed Jul 18, 2024
1 parent 3af8b5b commit b3e5a4c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PlotlyGeometries"
uuid = "da4dd95e-df4f-4caf-ac06-cee3b0f5c2e6"
authors = ["Jake W. Liu"]
version = "2.2.0"
version = "2.2.1"

[deps]
BatchAssign = "e837618a-1c15-48db-beb0-68297dffdb28"
Expand Down
4 changes: 2 additions & 2 deletions docs/MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ ___
#### add arrows

```julia
add_arrows!(plt::PlotlyJS.SyncPlot, origin::Vector{<:Real}, dir::Vector{<:Real}, len::Float64=1.0, color::String=""; opc::Real=1)

add_arrows!(plt::PlotlyJS.SyncPlot, origin::Vector{<:Real}, dir::Vector{<:Real}, len::Real=1.0, color::String=""; opc::Real=1, endpoint::Bool=true, asize::Real=len)
```

Creates a 3D arrow starting from a point and pointing in a given direction.
Expand All @@ -274,6 +273,7 @@ Creates a 3D arrow starting from a point and pointing in a given direction.

##### Keywords
- `opc`: The opacity of the arrow. Default is 1.
- `asize` Size factor of the arrow cone. Default is `len`.

___

Expand Down
10 changes: 10 additions & 0 deletions main.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Pkg, Revise
Pkg.activate(".")
using PlotlyJS, PlotlyGeometries

fig = plot()
fig.plot.layout = blank_layout()
display(fig)

add_ref_axes!(fig, [0, 0, 0], [2, 1, 1])
add_arrows!(fig, [0, 0, 0], [1, 0, 0], 1)
68 changes: 45 additions & 23 deletions src/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ function grot!(geo::GenericTrace, ang::Real, axis::Vector{<:Real}, origin::Vecto
push!(pos, [geo.x[n], geo.y[n], geo.z[n]])
end

axis .= axis ./ norm(axis)
axis_norm = axis ./ norm(axis)
vrot = similar(pos)

if origin == [0] # rotation center set at the geometry center
Expand All @@ -482,9 +482,10 @@ function grot!(geo::GenericTrace, ang::Real, axis::Vector{<:Real}, origin::Vecto
@assert length(origin) == 3
end

v = similar(origin)
for n in eachindex(vrot)
v = (pos[n] .- origin)
vrot[n] = cosd(ang) * v + sind(ang) * cross(axis, v) + (1 - cosd(ang)) * dot(axis, v) * axis
v .= (pos[n] .- origin)
vrot[n] = cosd(ang) * v + sind(ang) * cross(axis_norm, v) + (1 - cosd(ang)) * dot(axis_norm, v) * axis_norm
pos[n] = vrot[n] .+ origin
end

Expand Down Expand Up @@ -517,12 +518,14 @@ function sort_pts(pts::Vector)
c = collect(combinations(1:N, 3))
thtr = 0
phir = 0

vec = similar(mid)
for n in eachindex(c)
vec = cross(pts[c[n][2]] .- pts[c[n][1]], pts[c[n][3]] .- pts[c[n][1]])
vec .= cross(pts[c[n][2]] .- pts[c[n][1]], pts[c[n][3]] .- pts[c[n][1]])
if norm(vec) == 0
continue
else
vec = vec ./ norm(vec)
vec .= vec ./ norm(vec)
thtr = acosd(vec[3])
phir = atand(vec[2], vec[1])
break
Expand Down Expand Up @@ -575,12 +578,13 @@ function sort_pts!(pts::Vector)
c = collect(combinations(1:N, 3))
thtr = 0
phir = 0
vec = similar(mid)
for n in eachindex(c)
vec = cross(pts[c[n][2]] .- pts[c[n][1]], pts[c[n][3]] .- pts[c[n][1]])
vec .= cross(pts[c[n][2]] .- pts[c[n][1]], pts[c[n][3]] .- pts[c[n][1]])
if norm(vec) == 0
continue
else
vec = vec ./ norm(vec)
vec .= vec ./ norm(vec)
thtr = acosd(vec[3])
phir = atand(vec[2], vec[1])
break
Expand All @@ -605,6 +609,7 @@ function sort_pts!(pts::Vector)
ang[n] = atan(pts_rot[n][2] - mid_rot[2], pts_rot[n][1] - mid_rot[1])
end
pts .= pts[sortperm(ang)]
return nothing
end

"""
Expand Down Expand Up @@ -676,6 +681,7 @@ function add_ref_axes!(plt::PlotlyJS.SyncPlot, origin::Vector{<:Real}=[0, 0, 0],
),
]
))
return nothing
end

"""
Expand Down Expand Up @@ -749,11 +755,12 @@ function add_ref_axes!(plt::PlotlyJS.SyncPlot, origin::Vector{<:Real}, r::Vector
),
]
))
return nothing
end

"""
add_arrows!(plt::PlotlyJS.SyncPlot, origin::Vector{<:Real}, dir::Vector{<:Real}, len::Float64=1.0, color::String=""; opc::Real=1)
add_arrows!(plt::PlotlyJS.SyncPlot, origin::Vector{<:Real}, dir::Vector{<:Real}, len::Real=1.0, color::String=""; opc::Real=1, endpoint::Bool=true, asize::Real=len)
Creates a 3D arrow starting from a point and pointing in a given direction.
# Arguments
Expand All @@ -765,33 +772,47 @@ end
# Keywords
- `opc`: The opacity of the arrow. Default is 1.
- `asize` Size factor of the arrow cone. Default is `len`.
"""
function add_arrows!(plt::PlotlyJS.SyncPlot, origin::Vector{<:Real}, dir::Vector{<:Real}, len::Float64=1.0, color::String=""; opc::Real=1)
function add_arrows!(plt::PlotlyJS.SyncPlot, origin::Vector{<:Real}, dir::Vector{<:Real}, len::Real=1.0, color::String=""; opc::Real=1, endpoint::Bool=true, asize::Real=len)
@assert length(origin) == 3
@assert length(dir) == 3
@assert len > 0
@assert asize > 0

if color == ""
@all r g b = round(Int, rand() * 255)
color = "rgb($r, $g, $b)"
end

dir .= dir ./ norm(dir) .* len

c = cone(x=[origin[1] + dir[1] / 2], y=[origin[2] + dir[2] / 2], z=[origin[3] + dir[3] / 2], u=[dir[1]], v=[dir[2]], w=[dir[3]],
colorscale=[[0, color], [1, color]],
opacity=opc,
showscale=false)
dir_norm = dir / norm(dir) * len

if endpoint == true
c = cone(x=[origin[1] + dir_norm[1]], y=[origin[2] + dir_norm[2]], z=[origin[3] + dir_norm[3]], u=[dir_norm[1]/len*asize], v=[dir_norm[2]]/len*asize, w=[dir_norm[3]/len*asize],
colorscale=[[0, color], [1, color]],
opacity=opc,
showscale=false)
l = scatter3d(x=[origin[1], origin[1] + dir_norm[1]], y=[origin[2], origin[2] + dir_norm[2]], z=[origin[3], origin[3] + dir_norm[3]],
line=attr(color=color, width=2 *dir_norm),
mode="lines",
opacity=opc,
showlegend=false)
else # origin at center
c = cone(x=[origin[1] + dir_norm[1] / 2], y=[origin[2] + dir_norm[2] / 2], z=[origin[3] + dir_norm[3] / 2], u=[dir_norm[1]/len*asize], v=[dir_norm[2]/len*asize], w=[dir_norm[3]/len*asize],
colorscale=[[0, color], [1, color]],
opacity=opc,
showscale=false)
l = scatter3d(x=[origin[1] - dir_norm[1] / 2, origin[1] + dir_norm[1] / 2], y=[origin[2] - dir_norm[2] / 2, origin[2] + dir_norm[2] / 2], z=[origin[3] - dir_norm[3] / 2, origin[3] + dir_norm[3] / 2],
line=attr(color=color, width=2 * dir_norm),
mode="lines",
opacity=opc,
showlegend=false)
end

addtraces!(plt, c)

l = scatter3d(x=[origin[1] - dir[1] / 2, origin[1] + dir[1] / 2], y=[origin[2] - dir[2] / 2, origin[2] + dir[2] / 2], z=[origin[3] - dir[3] / 2, origin[3] + dir[3] / 2],
line=attr(color=color, width=2 * norm(dir)),
mode="lines",
opacity=opc,
showlegend=false)

addtraces!(plt, l)

return nothing
end

"""
Expand Down Expand Up @@ -819,6 +840,7 @@ function add_text!(plt::PlotlyJS.SyncPlot, origin::Vector{<:Real}, text::String,
),
showlegend=false,
))
return nothing
end

"""
Expand Down

0 comments on commit b3e5a4c

Please sign in to comment.