Skip to content

Commit

Permalink
Adjust canvas color bar + float precision, update some docstrings.
Browse files Browse the repository at this point in the history
  • Loading branch information
j-fu committed Sep 23, 2021
1 parent a2dd72e commit d66627b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PlutoVista"
uuid = "646e1f28-b900-46d7-9d87-d554eb38a413"
authors = ["Juergen Fuhrmann <[email protected]>"]
version = "0.5.5"
version = "0.5.6"

[deps]
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Expand Down
14 changes: 7 additions & 7 deletions assets/canvascolorbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

function canvascolorbar(uuid,w,h,cbdict)
{

var hpad=0.1*h
var h0=hpad
var h1=h-hpad
var dh=h1-h0
var canvas = document.getElementById(uuid);
var ctx = canvas.getContext("2d");

ctx.clearRect(0, 0, canvas.width, canvas.height);


Expand All @@ -30,10 +30,10 @@ function canvascolorbar(uuid,w,h,cbdict)
}

ctx.fillStyle = grad;
ctx.fillRect(0,h0,w,dh);
ctx.fillRect(0,h0,0.5*w,dh);


ctx.font = "12px Arial"
ctx.font = "10px Arial"
ctx.textBaseline = "middle"
ctx.textAlign = "left"

Expand All @@ -46,13 +46,13 @@ function canvascolorbar(uuid,w,h,cbdict)
var hlev=h1-dh*(levels[i]-lmin)/(lmax-lmin)
ctx.beginPath();
ctx.moveTo(0, hlev);
ctx.lineTo(1.1*w,hlev);
ctx.lineTo(0.6*w,hlev);
ctx.stroke();
ctx.fillText(`${levels[i]}`,1.2*w,hlev)
ctx.fillText(levels[i].toPrecision(3),0.7*w,hlev)
}

}
else if (cbdict["cbar"]==2)
else if (cbdict["cbar"]==2) /// markes for gridplot
{
var cstops=cbdict["cstops"]
var colors=cbdict["colors"]
Expand Down Expand Up @@ -121,7 +121,7 @@ function canvascolorbar(uuid,w,h,cbdict)



ctx.font = "12px Arial"
ctx.font = "10px Arial"
ctx.textBaseline = "middle"
ctx.textAlign = "left"

Expand Down
2 changes: 1 addition & 1 deletion examples/plutovista.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ function func3d(;n=20)
end;

# ╔═╡ da3ba65b-71c0-4da5-b792-f57e0905aab7
g3,f3=func3d(;n=49)
g3,f3=func3d(;n=50)

# ╔═╡ 6af0b5d7-5324-43b5-8f99-6f5d35d5deba
TableOfContents()
Expand Down
4 changes: 2 additions & 2 deletions examples/vtktest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Change grid resolution: $(@bind resolution Slider(5:200))
(pts,tris,markers,edges,edgemarkers)=maketriangulation(1/resolution^2)

# ╔═╡ b8a976e3-7fef-4527-ae6a-4da31c93a04f
func=0.5*[sin(10*pts[1,i])*cos(10*pts[2,i]) for i=1:size(pts,2)]
func=0.5.+0.5*[sin(10*pts[1,i])*cos(10*pts[2,i]) for i=1:size(pts,2)]

# ╔═╡ 401b36bd-fa8f-4a9c-9556-bbc82c3ddbca
md"""
Expand All @@ -84,7 +84,7 @@ md"""Number of gridpoints: $(size(pts,2)) """
# ╔═╡ 81046dcd-3cfb-4133-943f-61b9b3cdb183
let
p=PlutoVTKPlot(resolution=(300,300))
tricontour!(p,pts,tris,func;cmap=:spring,isolines=-0.5:0.1:0.5)
tricontour!(p,pts,tris,func;cmap=:spring,isolines=-1.0e-16:0.1:1)
end

# ╔═╡ 7019ce3f-f2db-4581-8bd9-64f76231a62a
Expand Down
118 changes: 66 additions & 52 deletions src/plutovtkplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ mutable struct PlutoVTKPlot <: AbstractPlutoVistaBackend
# command list passed to javascript
jsdict::Dict{String,Any}

# size in canvas coordinates
# size in screen coordinates
w::Float64
h::Float64

# update of a already created plot ?
update::Bool

# uuid for identifying html element
uuid::UUID
PlutoVTKPlot(::Nothing)=new()
Expand All @@ -21,8 +23,7 @@ end
PlutoVTKPlot(;resolution=(300,300))
````
Create a canvas plot with given resolution in the notebook
and given "world coordinate" range.
Create a vtk plot with given resolution in the notebook.
"""
function PlutoVTKPlot(;resolution=(300,300), kwargs...)
p=PlutoVTKPlot(nothing)
Expand All @@ -34,27 +35,13 @@ function PlutoVTKPlot(;resolution=(300,300), kwargs...)
p
end

"""
axis3d!(vtkplot)
Add 3D coordinate system axes to the plot.
Sets camera handling to 3D mode.
"""
function axis3d!(p::PlutoVTKPlot)
command!(p,"axis")
parameter!(p,"cam","3D")
end

"""
axis2d!(vtkplot)
Add 2D coordinate system axes to the plot.
Sets camera handling to 2D mode.
"""
function axis2d!(p::PlutoVTKPlot)
command!(p,"axis")
parameter!(p,"cam","2D")
end

Base.show(io::IO,::MIME"text/html",p::PlutoVTKPlot)
Show plot in html. This creates a vtk.js based renderer along with a canvas
for handling the colorbar.
"""
function Base.show(io::IO, ::MIME"text/html", p::PlutoVTKPlot)
plutovtkplot = read(joinpath(@__DIR__, "..", "assets", "plutovtkplot.js"), String)
canvascolorbar = read(joinpath(@__DIR__, "..", "assets", "canvascolorbar.js"), String)
Expand All @@ -80,18 +67,38 @@ function Base.show(io::IO, ::MIME"text/html", p::PlutoVTKPlot)
plutovtkplot("$(p.uuid)",jsdict,invalidation)
canvascolorbar("$(uuidcbar)",20,$(p.h),jsdict)
</script>
$(div)
"""
p.update=true
write(io,result)
write(io,result*div)
end



"""
axis3d!(vtkplot)
Add 3D coordinate system axes to the plot.
Sets camera handling to 3D mode.
"""
function axis3d!(p::PlutoVTKPlot)
command!(p,"axis")
parameter!(p,"cam","3D")
end

"""
axis2d!(vtkplot)
Add 2D coordinate system axes to the plot.
Sets camera handling to 2D mode.
"""
function axis2d!(p::PlutoVTKPlot)
command!(p,"axis")
parameter!(p,"cam","2D")
end

"""
vtkpolys(tris; offset=0)
Set up polygon data for vtk.
Set up polygon (triangle) data for vtk.
Coding is [3, i11, i12, i13, 3 , i21, i22 ,i23, ...]
Careful: js indexing counts from zero
Careful: js indexing counts from zero.
"""
function vtkpolys(tris; offset=0)
ipoly=1
Expand All @@ -112,7 +119,8 @@ end
"""
tricontour!(p::PlutoVTKPlot,pts, tris,f; colormap, isolines)
Plot piecewise linear function on triangular grid given as "heatmap"
Plot piecewise linear function on triangular grid given as "heatmap".
Isolines can be given as a number or as a range.
"""
function tricontour!(p::PlutoVTKPlot, pts, tris,f;colormap=:viridis, isolines=0, kwargs...)

Expand Down Expand Up @@ -177,12 +185,20 @@ function tricontour!(p::PlutoVTKPlot, pts, tris,f;colormap=:viridis, isolines=0,
p
end

"""
contour!(p::PlutoVTKPlot,X,Y,f; colormap, isolines)
Plot piecewise linear function on triangular grid created from the tensor product of X and Y arrays as "heatmap".
Isolines can be given as a number or as a range.
"""
contour!(p::PlutoVTKPlot,X,Y,f; kwargs...)=tricontour!(p,triang(X,Y)...,vec(f);kwargs...)


"""
tetcontour!(p::PlutoVTKPlot,pts, tets,f; colormap, isolevels, xplane, yplane, zplane)
tetcontour!(p::PlutoVTKPlot,pts, tets,f; colormap, flevel, xplane, yplane, zplane)
Plot piecewise linear function on tetrahedral mesh.
Plot isosurface given by `flevel` and contour maps on planes given by the `*plane` parameters
for piecewise linear function on tetrahedral mesh.
"""
function tetcontour!(p::PlutoVTKPlot, pts, tets,func;colormap=:viridis,
flevel=prevfloat(Inf), flimits=(1.0,-1.0),
Expand All @@ -206,9 +222,7 @@ function tetcontour!(p::PlutoVTKPlot, pts, tets,func;colormap=:viridis,
if facecolormap==nothing
facecolormap=GridVisualize.bregion_cmap(nbregions)
end




@views for idim=1:3
xyzmin[idim]=minimum(pts[idim,:])
xyzmax[idim]=maximum(pts[idim,:])
Expand Down Expand Up @@ -276,16 +290,10 @@ end



contour!(p::PlutoVTKPlot,X,Y,f; kwargs...)=tricontour!(p,triang(X,Y)...,vec(f);kwargs...)
#contour!(p::PlutoVTKPlot,X,Y,f)=tricontour!(p,triang(X,Y)...,vec(f))




"""
trimesh!(p::PlutoVTKPlot,pts, tris;markers, colormap, edges, edgemarkers, edgecolormap)
Plot piecewise linear function on triangular grid given as "heatmap"
Plot triangular grid with optional region and boundary markers.
"""
function trimesh!(p::PlutoVTKPlot,pts, tris;
markers=nothing, colormap=nothing,
Expand Down Expand Up @@ -372,6 +380,12 @@ function trimesh!(p::PlutoVTKPlot,pts, tris;
p
end


"""
outline!(p::PlutoVTKPlot,pts,faces,facemarkers,facecolormap,nbregions,xyzmin,xyzmax;alpha=0.1)
Plot transparent outline of grid boundaries.
"""
function outline!(p::PlutoVTKPlot,pts,faces,facemarkers,facecolormap,nbregions,xyzmin,xyzmax;alpha=0.1)
bregpoints0,bregfacets0=GridVisualize.extract_visible_bfaces3D(pts,faces,facemarkers,nbregions,
xyzmax,
Expand Down Expand Up @@ -408,9 +422,9 @@ function outline!(p::PlutoVTKPlot,pts,faces,facemarkers,facecolormap,nbregions,x
end

"""
tetmesh!(p::PlutoVTKPlot,pts, tris;markers, colormap, faces, facemarkers, facecolormap)
tetmesh!(p::PlutoVTKPlot,pts, tris;markers, colormap, faces, facemarkers, facecolormap,xplane,yplane,zplane, outline, alpha)
Plot piecewise linear function on triangular grid given as "heatmap"
Plot parts of tetrahedral mesh below the planes given by the `*plane` parameters.
"""
function tetmesh!(p::PlutoVTKPlot, pts, tets;
markers=nothing, colormap=nothing,
Expand Down Expand Up @@ -529,18 +543,6 @@ function tetmesh!(p::PlutoVTKPlot, pts, tets;
end


function plot!(p::PlutoVTKPlot,x,y; kwargs...)
command!(p,"plot")
n=length(x)
points=vec(vcat(x',y',zeros(n)'))
lines=collect(UInt16,0:n)
lines[1]=n
parameter!(p,"points",points)
parameter!(p,"lines",lines)
parameter!(p,"cam","2D")
p
end




Expand All @@ -565,3 +567,15 @@ function triplot!(p::PlutoVTKPlot,pts, tris,f)
p
end


function plot!(p::PlutoVTKPlot,x,y; kwargs...)
command!(p,"plot")
n=length(x)
points=vec(vcat(x',y',zeros(n)'))
lines=collect(UInt16,0:n)
lines[1]=n
parameter!(p,"points",points)
parameter!(p,"lines",lines)
parameter!(p,"cam","2D")
p
end

2 comments on commit d66627b

@j-fu
Copy link
Owner Author

@j-fu j-fu commented on d66627b Sep 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/45439

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.6 -m "<description of version>" d66627b94d53af6a5a6edf74e2ffdc426eb28a50
git push origin v0.5.6

Please sign in to comment.