在mayavi中旋转contour3d对象

2024-09-28 20:43:58 发布

您现在位置:Python中文网/ 问答频道 /正文

在Mayavi中,我正在制作一个椭球体自由刚体旋转的动画,为此,我需要用旋转矩阵及时旋转椭球体

下面的代码是我制作动画所必须的,在make\u frame函数中,我希望通过在椭球体的所有点上应用3x3旋转矩阵来旋转椭球体

import numpy as np
import mayavi.mlab as mlab
import moviepy.editor as mpy

fig = mlab.figure(size=(500,500), bgcolor=(1,1,1))
f = mlab.gcf()
f.scene._lift()

x,y,z = np.mgrid[-3:3:100j, -3:3:100j, -3:3:100j]
a,b,c = 3,2,1
ellipsoid = mlab.contour3d(x**2/a**2 + y**2/b**2 + z**2/c**2,
                          contours=[1],opacity=0.5,
                          extent=[-a*1,a*1,-b*1,b*1,-c*1,c*1])

def rot_angle_axis(theta, k):
    K = np.array([[ 0, -k[2], k[1]],
                 [k[2],0,  -k[0]],
                 [-k[1], k[0], 0]])
    rot = np.eye(3) + np.sin(theta)*K + (1-np.cos(theta))*K@K
    return rot
def make_frame(t):
    # insert rotation code here
    return mlab.screenshot(antialiased=True)

animation = mpy.VideoClip(make_frame, duration=2)
animation.write_videofile("testanim.mp4",fps=30)

如果我能以某种方式访问椭球体的点作为(x,y,z)坐标的数组,那么我可以在每个点上应用矩阵,然后用ellipsoid.mlab_source.set(x=<transformed x>,y=,<transformed y>,z=<transformed z>)更新椭球体,但我不知道这是否可行

This answer可能接近我想要的,但目前它对我来说太高了,我不知道如何将它应用到我的mlab.contour3d对象上

有人能帮我做这个轮换吗


Tags: importmakeasnp动画矩阵frametransformed