addSlider2D for 2 renderer
See original GitHub issueHi marcomusy,
I’m trying to render 2 plots with 1 slider. I found it is hard to realize that updating two or more plt.show()
or plt.pop.add()
within one plt.addSlider2D
at the same time.
I want to slice a mesh object with a plane and show the cross section (in 2D, eg. in x-y-panel) simultaneously.
It seems work by ploting the cross section in matlibplot.pyplot
, but there was some lag and delay by dragging the slider, how can I realize this effect just in vedo.plotter. Because further I want to merge 2 more mesh into one object to compare their section profile, it may be better if just use vedo.
The dataset is below if it is needed.
vedo 2021.0.3 | vtk 9.0.1 | python 3.9
Here’s my code 1, display 2 effects by using vedo.plotter
and matplotlib
.
import matplotlib.pyplot
from vedo import *
settings.immediateRendering = False
# load the mesh and pre_cut
s = Mesh('data/targets/target_AF_bs.stl')
plane_cut = s.cutWithBox([-78, 308, -71, 65, -1000, 1000]) # .c("silver").bc("Silver")
obj1 = vedo2trimesh(plane_cut)
# 8 bit RGBA color (r,g,b,transparent)(https://rgbacolorpicker.com/)
obj1.visual.face_colors = [(122, 122, 122, 100)]
txt = 'cross section of the mesh'
fig, ax = matplotlib.pyplot.subplots()
def slider(widget, event):
cut_x = widget.GetRepresentation().GetValue()
# generate cutting path by using trimesh.section -> return path2D object
mslice = obj1.section(plane_origin=(cut_x, 0, 0), plane_normal=[1, 0, 0])
# pl = Plane((cut_x, 0, -25), normal=[1, 0, 0], sx=100, sy=200, c='green', alpha=0.5)
# force path2D object -> planar
slice_2D, planar = mslice.to_planar()
# show the cutting path on object and update
plt.pop().add(mslice)
ax.set_title('Interface in x = ' + str(int(cut_x)) + "mm")
ax.autoscale_view()
ax.set_xlim(-50, 50)
ax.set_ylim(-75, 75)
# matplotlib.pyplot the path2D object
slice_2D.show()
# matplotlib.pyplot show update by dragging slider
ax.figure.canvas.draw()
matplotlib.pyplot.cla()
plt = Plotter(bg="blue9", bg2="white")
plt.addSlider2D(slider,
-77, 307,
value=0,
pos="bottom",
title="cut in x direction")
plt.show(obj1, txt, axes=1, azimuth=65, elevation=-30, roll=-80).close()
code 2: use vedo
, the old render didn’t show at=0
and didn’t remove/pop at=1
, in addition, the camera angle didn’t work. I want to obserb the cross section just in 2D (in my case Y-Z-panel), like in code 1.
"""Slice in x-direction"""
from vedo import *
settings.useDepthPeeling = True
settings.immediateRendering = False
# declare the instance of the class
plt = Plotter(shape=(1, 2), resetcam=1, sharecam=0, interactive=0, size=[1200, 500])
# load tool mesh
s = Mesh('data/targets/target_AF_bs.stl')
s1 = s.cutWithBox([-78, 308, -71, 65, -1000, 1000]).bc("silver")
def slider(widget, event):
cut_x = widget.GetRepresentation().GetValue()
# creat cut plane
pl = Plane((cut_x, 0, -25), normal=[1, 0, 0], sx=100, sy=200, c='green', alpha=0.4)
# Intersection
s2 = s1.intersectWith(pl).lineWidth(1.1).c('black')
# 1st render
plt.pop().add(pl, at=0)
# # 2nd render
plt.pop().add(s2, at=1)
plt.addSlider2D(slider,
-77, 307,
value=0,
pos="bottom-left",
title="cut in x direction")
plt.show(s1, __doc__, at=0, axes=1, interactive=1, azimuth=65, elevation=-30, roll=-80)
Thank you so much!
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Thank you so much, that’s just what i thought, I really appreciate your work!
Yes, try: