Mayavi的:动画八面体轮廓不阻塞 [英] Mayavi: animating an octahedron outline without blocking

查看:294
本文介绍了Mayavi的:动画八面体轮廓不阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动画一个八面体。这里是code为。这code的一个简化版本将在SO 发现这个不同的问题。使用code有和动画对象的样式所使用这里我试图让一个功能动画。是 - 动画唯一的问题挂断绘图窗口!

导入numpy的是NP
进口mayavi.mlab为ML
进口数学
进口时间DEF produce_verts(A,T):
    三角洲=拉姆达A,T:A * math.sin(T)
    绿党=拉姆达D:[(1 + D,0,0),(0,1 + D,0),( - 1-D,0,0),(0,-1-D,0),(0 ,0,1 + D),(0,0,-1-d)中]
    返回ZIP(*绿党(增量(A,T)))T = 0。
DT = 0.01
A = 0.5
ML.clf()
nverts = 6
X,Y,Z = produce_verts(A,T)
#每个三角形是索引的3元组。该指数是verts`的`指数。
三角形= [(I,第(i + 1)%4,j)的对于i中对于j范围(4)(4,5)]
colorval = [X [I] ** 2 + Y [I] ** 2 + Z [I] ** 2 i的范围(nverts)
目= ML.triangular_mesh(X,Y,Z,三角形,标量= colorval,不透明= 1,再presentation ='目')
MS = mesh.mlab_source
布尔= TRUE
而布尔:
    T =(T + DT)%(2 * math.pi)
    X,Y,Z = produce_verts(A,T)
    colorval = [X [I] ** 2 + Y [I] ** 2 + Z [I] ** 2 i的范围(nverts)
    MS.reset(X = X,Y = Y,Z = Z,标量= colorval)
    time.sleep(1)。
    印花T,DT
    如果T> 4:
        布尔=假


解决方案

我不认为该地块挂。这只是 DT 是如此之小和 time.sleep 是如此之大,它会尝试你的耐心。如果设置 DT 等于说,0.1,并删除 time.sleep 呼叫,则情节变得更动画。

此外,使用 MS.reset 时的尺寸的阵列的改变。当数组的大小保持不变,你会用得到更好的性能 MS.set

 导入numpy的是NP
进口mayavi.mlab为ML
进口数学
进口时间
DEF produce_verts(A,T):
    DEF三角洲(A,T):
        返回A * math.sin(T)
    高清绿党(D):
        返回[(1 + D,0,0),(0,1 + D,0),( - 1 - D,0,0),(0,-1 - D,0),
                (0,0,1 + D),(0,0,-1 - 四)]
    返回ZIP(*绿党(增量(A,T)))T = 0。
DT = 0.1
A = 0.5
ML.clf()
nverts = 6
X,Y,Z = produce_verts(A,T)
#每个三角形是索引的3元组。该指数是verts`的`指数。
三角形= [(I,第(i + 1)%4,j)的对于i中对于j范围(4)在(4,5)]
colorval = [喜** 2 +易** 2 +子** 2喜,彝,字拉链(X,Y,Z)]
网= ML.triangular_mesh(
    X,Y,Z,三角形,标量= colorval,不透明= 1,再presentation ='目')
MS = mesh.mlab_source
而真正的:
    T =(T + DT)%(2 * math.pi)
    X,Y,Z = produce_verts(A,T)
    colorval = [喜** 2 +易** 2 +子** 2喜,苡仁,紫拉链(X,Y,Z)]
    ms.set(X = X,Y = Y,Z = Z,标量= colorval)
    #time.sleep(0.1)
    印花T,DT
    如果T> 4:
        打破ML.show()

I am trying to animate an octahedron. Here is the code for that. A simpler version of this code would be found on this different question on SO. Using the code there and the style of the animate object as used here I tried to make a functional animation. The only problem is- the animation hangs up the plot window!

import numpy as np
import mayavi.mlab as ML
import math
import time

def produce_verts(A,t):
    delta=lambda A,t:A*math.sin(t)
    verts =lambda d: [(1+d,0,0), (0,1+d,0), (-1-d,0,0), (0,-1-d,0), (0,0,1+d), (0,0,-1-d)]
    return zip(*verts(delta(A,t)))

t=0.
dt=0.01
A=0.5
ML.clf()
nverts=6
x, y, z = produce_verts(A,t)
# Each triangle is a 3-tuple of indices. The indices are indices of `verts`.                                                                                                                                        
triangles = [(i, (i+1)%4, j) for i in range(4) for j in (4,5)]
colorval = [x[i]**2+y[i]**2+z[i]**2 for i in range(nverts)]
mesh=ML.triangular_mesh(x, y, z, triangles, scalars=colorval, opacity=1,representation='mesh')
MS=mesh.mlab_source
Bool=True
while Bool:
    t=(t+dt)%(2*math.pi)
    x,y,z=produce_verts(A,t)
    colorval = [x[i]**2+y[i]**2+z[i]**2 for i in range(nverts)]
    MS.reset(x=x,y=y,z=z,scalars=colorval)
    time.sleep(1.)
    print t,dt
    if t>4:
        Bool=False

解决方案

I don't think the plot is hanging. It's just that dt is so small and the time.sleep is so large that it tries your patience. If you set dt equal to, say, 0.1, and remove the time.sleep call, then the plot becomes more animated.

Also, use MS.reset when the size of the arrays change. When the size of the arrays stays the same, you'll get better performance using MS.set:

import numpy as np
import mayavi.mlab as ML
import math
import time


def produce_verts(A, t):
    def delta(A, t):
        return A * math.sin(t)
    def verts(d):
        return [(1 + d, 0, 0), (0, 1 + d, 0), (-1 - d, 0, 0), (0, -1 - d, 0),
                (0, 0, 1 + d), (0, 0, -1 - d)]
    return zip(*verts(delta(A, t)))

t = 0.
dt = 0.1
A = 0.5
ML.clf()
nverts = 6
x, y, z = produce_verts(A, t)
# Each triangle is a 3-tuple of indices. The indices are indices of `verts`.
triangles = [(i, (i + 1) % 4, j) for i in range(4) for j in (4, 5)]
colorval = [xi ** 2 + yi ** 2 + zi ** 2 for xi, yi, zi in zip(x, y, z)]
mesh = ML.triangular_mesh(
    x, y, z, triangles, scalars=colorval, opacity=1, representation='mesh')
ms = mesh.mlab_source
while True:
    t = (t + dt) % (2 * math.pi)
    x, y, z = produce_verts(A, t)
    colorval = [xi ** 2 + yi ** 2 + zi ** 2 for xi, yi, zi in zip(x, y, z)]
    ms.set(x=x, y=y, z=z, scalars=colorval)
    # time.sleep(0.1)
    print t, dt
    if t > 4:
        break

ML.show()

这篇关于Mayavi的:动画八面体轮廓不阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆