给定 jupyter 笔记本,其场景使用 ipyvolume 渲染,并且 slider 更新场景中形状的 x、y、z 特征,3D 渲染器具有场景更改动画,该动画绘制与不对应的形状的中间 View slider 上的 View 。
如何强制 ipyvolume 在更新时仅渲染最终场景,而不渲染场景更改动画?
例如,在 jupyter 笔记本中运行以下命令并拖动 theta/phi:
import numpy as np
from numpy import pi, sin, cos, radians
import ipyvolume as ipv
import ipywidgets as widgets
def transform_xyz(theta, phi, x, y, z):
x, y, z = [np.asarray(v) for v in (x, y, z)]
shape = x.shape
points = np.matrix([x.flatten(), y.flatten(), z.flatten()])
Ry_theta = [[+cos(theta), 0, +sin(theta)],
[0, 1, 0],
[-sin(theta), 0, +cos(theta)]]
Rz_phi = [[+cos(phi), -sin(phi), 0],
[+sin(phi), +cos(phi), 0],
[0, 0, 1]]
points = np.matrix(Rz_phi)*np.matrix(Ry_theta)*points
x, y, z = [np.array(v).reshape(shape) for v in points]
return x, y, z
def torus(R=100, r=10, Rsteps=100, rsteps=100):
u = np.linspace(0, 2*pi, rsteps) # tube
v = np.linspace(0, 2*pi, Rsteps) # ring
U, V = np.meshgrid(u, v)
x = (R + r*cos(U))*cos(V)
y = (R + r*cos(U))*sin(V)
z = r*sin(U)
return x, y, z
class Scene:
def __init__(self, view=(0, 0, 0), hkl=(1,1,1)):
self.view = view
self.figure = ipv.figure()
R = 100.
r = R/30.
ipv.xlim(-R, R)
ipv.ylim(-R, R)
ipv.zlim(-R, R)
ipv.style.box_off()
self.torus_xyz = torus(R=R, r=R/10)
self.torus = ipv.plot_surface(*self.torus_xyz)
ipv.show()
self.ui = widgets.interact(self.update, theta=(-90.,90.), phi=(-180., 180.))
#self.ui.widget.children[0].continuous_update = False
#self.ui.widget.children[1].continuous_update = False
def update(self, theta, phi):
print("update with", theta, phi)
x, y, z = transform_xyz(radians(theta), radians(phi), *self.torus_xyz)
self.torus.x = x.flatten()
self.torus.y = y.flatten()
self.torus.z = z.flatten()
scene = Scene()
最佳答案
您也可以尝试将animation_exponent设置为0。Ipyvolume使用线性插值来更新数据变化的图形,指数设置此插值的速度。零指数应该会导致即时更新。
self.figure = ipv.figure(animation=0.,animation_exponent=0.)
关于python - 如何立即更新 ipyvolume 形状而无需中间 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54525890/