Skip to content

Commit

Permalink
Outlines for tetcontour, tetmesh
Browse files Browse the repository at this point in the history
  • Loading branch information
j-fu committed Sep 22, 2021
1 parent 2364ea9 commit a2dd72e
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 9 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.4"
version = "0.5.5"

[deps]
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Expand Down
84 changes: 84 additions & 0 deletions assets/plutovtkplot.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,23 @@ function plutovtkplot(uuid,jsdict,invalidation)
var polys=jsdict[cmd+"polys"]
var colors=jsdict[cmd+"colors"]


var outline=jsdict[cmd+"outline"]
var opoints=jsdict[cmd+"opoints"]
var opolys=jsdict[cmd+"opolys"]
var ocolors=jsdict[cmd+"ocolors"]

if (outline==1)
{
var ocolorData = vtk.Common.Core.vtkDataArray.newInstance({
name: 'Colors',
values: ocolors,
numberOfComponents: 4,
});

}


/// need to use LUT here!
var colorData = vtk.Common.Core.vtkDataArray.newInstance({
name: 'Colors',
Expand All @@ -134,8 +151,34 @@ function plutovtkplot(uuid,jsdict,invalidation)
mapper.setColorModeToDirectScalars()
win.actor.setMapper(mapper);
win.renderer.addActor(win.actor);

if (outline==1)
{
win.odataset = vtk.Common.DataModel.vtkPolyData.newInstance();
win.oactor = vtk.Rendering.Core.vtkActor.newInstance();
var omapper = vtk.Rendering.Core.vtkMapper.newInstance();
omapper.setInputData(win.odataset);
omapper.setColorModeToDirectScalars()
win.oactor.setForceTranslucent(true) // https://discourse.vtk.org/t/wireframe-not-visible-behind-transparent-surfaces/6671
win.oactor.setMapper(omapper);
win.renderer.addActor(win.oactor);
}



}


if (outline==1)
{
win.odataset.getPoints().setData(opoints, 3);
win.odataset.getPolys().setData(opolys,1);
win.odataset.getCellData().setActiveScalars('Colors');
win.odataset.getCellData().setScalars(ocolorData);
win.odataset.modified()
}


win.dataset.getPoints().setData(points, 3);
win.dataset.getPolys().setData(polys,1);
win.dataset.getPointData().setActiveScalars('Colors');
Expand Down Expand Up @@ -268,6 +311,23 @@ function plutovtkplot(uuid,jsdict,invalidation)
numberOfComponents: 3,
});
}

var outline=jsdict[cmd+"outline"]
var opoints=jsdict[cmd+"opoints"]
var opolys=jsdict[cmd+"opolys"]
var ocolors=jsdict[cmd+"ocolors"]

if (outline==1)
{
var ocolorData = vtk.Common.Core.vtkDataArray.newInstance({
name: 'Colors',
values: ocolors,
numberOfComponents: 4,
});

}



if (!win.update)
{
Expand All @@ -282,6 +342,18 @@ function plutovtkplot(uuid,jsdict,invalidation)
win.renderer.addActor(win.cellactor);
}

if (outline==1)
{
win.odataset = vtk.Common.DataModel.vtkPolyData.newInstance();
win.oactor = vtk.Rendering.Core.vtkActor.newInstance();
var omapper = vtk.Rendering.Core.vtkMapper.newInstance();
omapper.setInputData(win.odataset);
omapper.setColorModeToDirectScalars()
win.oactor.setForceTranslucent(true) // https://discourse.vtk.org/t/wireframe-not-visible-behind-transparent-surfaces/6671
win.oactor.setMapper(omapper);
win.renderer.addActor(win.oactor);
}


win.dataset = vtk.Common.DataModel.vtkPolyData.newInstance();
win.actor = vtk.Rendering.Core.vtkActor.newInstance();
Expand All @@ -304,6 +376,18 @@ function plutovtkplot(uuid,jsdict,invalidation)
win.celldataset.getCellData().setScalars(colorData);
win.celldataset.modified()
}

if (outline==1)
{
win.odataset.getPoints().setData(opoints, 3);
win.odataset.getPolys().setData(opolys,1);
win.odataset.getCellData().setActiveScalars('Colors');
win.odataset.getCellData().setScalars(ocolorData);
win.odataset.modified()
}



win.dataset.getPoints().setData(points, 3);
win.dataset.getPolys().setData(polys,1);
win.dataset.modified()
Expand Down
14 changes: 12 additions & 2 deletions examples/vtktest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ z: $(@bind zplane Slider(0:0.01:1,show_value=true,default=0.45))

# ╔═╡ 3681ef5b-c794-44da-9fe7-cedcd68b426c
tetcontour!(p3d,g[Coordinates],g[CellNodes],f;flevel=flevel,
xplane=xplane,yplane=yplane,zplane=zplane)
faces=g[BFaceNodes],
facemarkers=g[BFaceRegions],
xplane=xplane,yplane=yplane,zplane=zplane,outline=true,alpha=0.1)

# ╔═╡ 0f440c27-7ff1-4db5-b4eb-8ce1e9018ef1
g[BFaceNodes]

# ╔═╡ 606f6837-f3b7-4a52-b9c3-034799c7bf93
g3x=let
Expand All @@ -144,12 +149,15 @@ y: $(@bind gyplane Slider(0:0.01:1,show_value=true,default=0.45))
z: $(@bind gzplane Slider(0:0.01:1,show_value=true,default=0.45))
"""

# ╔═╡ ae5707d9-41b7-4937-924a-fb54b83c31db
alpha=0.15

# ╔═╡ 519d106f-3f6f-4db1-b4e4-c5e7ef176857
tetmesh!(p3dx,g3x[Coordinates],g3x[CellNodes];
markers=g3x[CellRegions],
faces=g3x[BFaceNodes],
facemarkers=g3x[BFaceRegions],
xplane=gxplane,yplane=gyplane,zplane=gzplane)
xplane=gxplane,yplane=gyplane,zplane=gzplane,alpha=alpha,outline=true)

# ╔═╡ Cell order:
# ╟─93ca4fd0-8f61-4174-b459-55f5395c0f56
Expand All @@ -173,7 +181,9 @@ tetmesh!(p3dx,g3x[Coordinates],g3x[CellNodes];
# ╠═f64729e4-d2b4-40d3-acbb-1395dbe0337d
# ╟─7be35f33-5f7a-4765-8bb6-1487e209efc8
# ╠═3681ef5b-c794-44da-9fe7-cedcd68b426c
# ╠═0f440c27-7ff1-4db5-b4eb-8ce1e9018ef1
# ╠═606f6837-f3b7-4a52-b9c3-034799c7bf93
# ╠═ecb3bb5e-6ae5-4d6e-9834-d52ce977b3fc
# ╟─90ff6ffc-84dc-45fd-8d09-9eb916397630
# ╠═ae5707d9-41b7-4937-924a-fb54b83c31db
# ╠═519d106f-3f6f-4db1-b4e4-c5e7ef176857
72 changes: 66 additions & 6 deletions src/plutovtkplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,27 @@ Plot piecewise linear function on tetrahedral mesh.
"""
function tetcontour!(p::PlutoVTKPlot, pts, tets,func;colormap=:viridis,
flevel=prevfloat(Inf), flimits=(1.0,-1.0),
xplane=prevfloat(Inf), yplane=prevfloat(Inf), zplane=prevfloat(Inf))
faces=nothing, facemarkers=nothing, facecolormap=nothing,
xplane=prevfloat(Inf), yplane=prevfloat(Inf), zplane=prevfloat(Inf),
outline=true,alpha=0.1)

p.jsdict=Dict{String,Any}("cmdcount" => 0)
command!(p,"tetcontour")

xyzmin=zeros(3)
xyzmax=ones(3)

nbregions= facemarkers==nothing ? 0 : maximum(facemarkers)

if faces!=nothing && nbregions==0
nbregions=1
facemarkers=fill(Int32(1),size(faces,2))
end

if facecolormap==nothing
facecolormap=GridVisualize.bregion_cmap(nbregions)
end



@views for idim=1:3
Expand Down Expand Up @@ -221,10 +234,10 @@ function tetcontour!(p::PlutoVTKPlot, pts, tets,func;colormap=:viridis,
[flevel]
)

faces=reshape(reinterpret(Int32,faces0),(3,length(faces0)))
cfaces=reshape(reinterpret(Int32,faces0),(3,length(faces0)))
cpts=copy(reinterpret(Float32,cpts0))
parameter!(p,"points",cpts)
parameter!(p,"polys",vtkpolys(faces))
parameter!(p,"polys",vtkpolys(cfaces))
nan_replacement=0.5*(fminmax[1]+fminmax[2])
for i=1:length(values)
if isnan(values[i])
Expand All @@ -249,6 +262,12 @@ function tetcontour!(p::PlutoVTKPlot, pts, tets,func;colormap=:viridis,
p.jsdict["levels"]=[fminmax[1],flevel,fminmax[2]]


if outline && faces!=nothing
parameter!(p,"outline",1)
outline!(p,pts,faces,facemarkers,facecolormap,nbregions,xyzmin,xyzmax;alpha=alpha)
else
parameter!(p,"outline",0)
end

axis3d!(p)
p
Expand Down Expand Up @@ -353,7 +372,40 @@ function trimesh!(p::PlutoVTKPlot,pts, tris;
p
end


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,
primepoints=hcat(xyzmin,xyzmax)
)
bregpoints=hcat([reshape(reinterpret(Float32,bregpoints0[i]),(3,length(bregpoints0[i]))) for i=1:nbregions]...)
bregfacets=vcat([vtkpolys(reshape(reinterpret(Int32,bregfacets0[i]),(3,length(bregfacets0[i]))),
offset= ( i==1 ? 0 : sum(k->length(bregpoints0[k]),1:i-1) ) )
for i=1:nbregions]...)
bfacemarkers=vcat([fill(i,length(bregfacets0[i])) for i=1:nbregions]...)

if typeof(facecolormap)==Symbol
facecmap=colorschemes[facecolormap]
else
facecmap=ColorScheme(facecolormap)
end
facergb=reinterpret(Float64,get(facecmap,bfacemarkers,(1,size(facecmap))))
nfaces=length(facergb)÷3
facergba=zeros(UInt8,nfaces*4)
irgb=0
irgba=0
for i=1:nfaces
facergba[irgba+1]=UInt8(floor(facergb[irgb+1]*255))
facergba[irgba+2]=UInt8(floor(facergb[irgb+2]*255))
facergba[irgba+3]=UInt8(floor(facergb[irgb+3]*255))
facergba[irgba+4]=UInt8(floor(alpha*255))
irgb+=3
irgba+=4
end
parameter!(p,"opolys",bregfacets)
parameter!(p,"opoints",vec(bregpoints))
parameter!(p,"ocolors",facergba)

end

"""
tetmesh!(p::PlutoVTKPlot,pts, tris;markers, colormap, faces, facemarkers, facecolormap)
Expand All @@ -363,7 +415,8 @@ Plot piecewise linear function on triangular grid given as "heatmap"
function tetmesh!(p::PlutoVTKPlot, pts, tets;
markers=nothing, colormap=nothing,
faces=nothing, facemarkers=nothing, facecolormap=nothing,
xplane=prevfloat(Inf), yplane=prevfloat(Inf), zplane=prevfloat(Inf))
xplane=prevfloat(Inf), yplane=prevfloat(Inf), zplane=prevfloat(Inf),
outline=true, alpha=0.1)


ntet=size(tets,2)
Expand Down Expand Up @@ -459,7 +512,14 @@ function tetmesh!(p::PlutoVTKPlot, pts, tets;
p.jsdict["ecolors"]=ebar_rgb
p.jsdict["elevels"]=collect(1:size(ecmap))
end


if outline && faces!=nothing
parameter!(p,"outline",1)
outline!(p,pts,faces,facemarkers,facecolormap,nbregions,xyzmin,xyzmax;alpha=alpha)
else
parameter!(p,"outline",0)
end

parameter!(p,"polys",facets)
parameter!(p,"points",vec(points))
parameter!(p,"colors",UInt8.(floor.(rgb*255)))
Expand Down

2 comments on commit a2dd72e

@j-fu
Copy link
Owner Author

@j-fu j-fu commented on a2dd72e Sep 22, 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/45371

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.5 -m "<description of version>" a2dd72e3eabcec3f01c7d108d4eeea49166849b0
git push origin v0.5.5

Please sign in to comment.