标题:关于traitsUI跟Mayavi结合画图的问题,课设程序,救救孩子!!!
取消只看楼主
杨xiao
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-11-5
 问题点数:0 回复次数:1 
关于traitsUI跟Mayavi结合画图的问题,课设程序,救救孩子!!!
问题描述:我用traitsUI跟Mayavi结合写了一个画图轮的程序,希望通过滑动条控件改变参数来改变图像形状,界面跟图像都能画出来,但是我滑动改变参数的时候就提醒我出错。。。

(这个是程序运行出的界面)

(这个是错误提示,好像是数组形状不对了)
程序:(程序有点长,但是只看一个函数就行,因为图像分六段画的,所以用了六个函数)
程序代码:
from traits.api import HasTraits, Range, Instance, on_trait_change
from traitsui.api import View, Item, Group
from mayavi.core.api import PipelineBase
from mayavi.core.ui.api import MayaviScene, SceneEditor, MlabSceneModel
import numpy as np
from numpy import arange,  cos, sin,pi
e=20;h=115.47;
def curve1(a0,r0):
    s0=(r0**2-e**2)**0.5
    c=0.01
    a1=np.linspace(0.0,a0,200)
    s1=h/2*(1-np.cos(pi/a0*a1))
    x=(s0+s1)*np.sin(a1)+e*np.cos(a1)
    y=(s0+s1)*np.cos(a1)-e*np.sin(a1)
    z=np.ones_like(a1)
    return x,y,z
def curve2(a0,r0):
    s0=(r0**2-e**2)**0.5
    b=(2*pi-a0)/5
    c=0.01
    s2=h
    a2=np.linspace(a0,a0+b,200)
    x=(s0+s2)*np.sin(a2)+e*np.cos(a2)
    y=(s0+s2)*np.cos(a2)-e*np.sin(a2)
    z=np.ones_like(a2)
    return x,y,z
def curve3(a0,r0):
    s0=(r0**2-e**2)**0.5
    b=(2*pi-a0)/5
    c=0.01
    a3=np.linspace(a0+b,a0+2*b,200)
    s3=h/2+h/4*(1+cos(pi/b*(a3-a0-b)))
    x=(s0+s3)*np.sin(a3)+e*np.cos(a3)
    y=(s0+s3)*np.cos(a3)-e*np.sin(a3)
    z=np.ones_like(a3)
    return x,y,z
def curve4(a0,r0):
    s0=(r0**2-e**2)**0.5
    b=(2*pi-a0)/5
    c=0.01
    s4=h/2
    a4=np.linspace(a0+2*b,a0+3*b,200)
    x=(s0+s4)*np.sin(a4)+e*np.cos(a4)
    y=(s0+s4)*np.cos(a4)-e*np.sin(a4)
    z=np.ones_like(a4)
    return x,y,z
def curve5(a0,r0):
    s0=(r0**2-e**2)**0.5
    b=(2*pi-a0)/5
    c=0.01
    a5=np.linspace(a0+3*b,a0+4*b,200)
    s5=h/4*(1+cos(pi/b*(a5-a0-3*b)))
    x=(s0+s5)*np.sin(a5)+e*np.cos(a5)
    y=(s0+s5)*np.cos(a5)-e*np.sin(a5)
    z=np.ones_like(a5)
    return x,y,z
def curve6(a0,r0):
    s0=(r0**2-e**2)**0.5
    b=(2*pi-a0)/5
    c=0.01
    s6=0
    a6=np.linspace(a0+4*b,a0+5*b,200)
    x=(s0+s6)*np.sin(a6)+e*np.cos(a6)
    y=(s0+s6)*np.cos(a6)-e*np.sin(a6)
    z=np.ones_like(a6)
    return x,y,z
class TL(HasTraits):
    n_meridional    = Range(0, 6, 2)
    n_longitudinal  = Range(0, 150, 90)
    # 场景模型实例
    scene = Instance(MlabSceneModel, ())
    # 管线实例
    plot = Instance(PipelineBase)
    #当场景被激活,或者参数发生改变,更新图形
    @on_trait_change('n_meridional,n_longitudinal,scene.activated')
    def update_plot(self):
        x,y,z= curve1(self.n_meridional, self.n_longitudinal)
        x1,y1,z1= curve2(self.n_meridional, self.n_longitudinal)
        x2,y2,z2= curve3(self.n_meridional, self.n_longitudinal)
        x3,y3,z3= curve4(self.n_meridional, self.n_longitudinal)
        x4,y4,z4= curve5(self.n_meridional, self.n_longitudinal)
        x5,y5,z5= curve6(self.n_meridional, self.n_longitudinal)
        if self.plot is None:#如果plot未绘制则生成plot3d
            self.plot = self.scene.mlab.plot3d(x, y, z,
                        tube_radius=0.25, colormap='Spectral')
            self.plot = self.scene.mlab.plot3d(x1, y1, z1,
                        tube_radius=0.25, colormap='Spectral')
            self.plot = self.scene.mlab.plot3d(x2, y2, z2,
                        tube_radius=0.25, colormap='Spectral')
            self.plot = self.scene.mlab.plot3d(x3, y3, z3,
                        tube_radius=0.25, colormap='Spectral')
            self.plot = self.scene.mlab.plot3d(x4, y4, z4,
                        tube_radius=0.25, colormap='Spectral')
            self.plot = self.scene.mlab.plot3d(x5, y5, z5,
                        tube_radius=0.25, colormap='Spectral')
            self.plot = self.scene.mlab.points3d(0, 0, 1,colormap='Reds',scale_factor=4)
            
        else:#如果数据有变化,将数据更新即重新赋值
            self.plot.mlab_source.set(x=x, y=y, z=z)
            self.plot.mlab_source.set(x1=x1, y1=y1, z1=z1)
            self.plot.mlab_source.set(x2=x2, y2=y2, z2=z2)
            self.plot.mlab_source.set(x3=x3, y3=y3, z3=z3)
            self.plot.mlab_source.set(x4=x4, y4=y4, z4=z4)
            self.plot.mlab_source.set(x5=x5, y5=y5, z5=z5)

 
    # 建立视图布局
    view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
                    show_label=False),
                Group('_', 'n_meridional', 'n_longitudinal'),
                resizable=True)

 
model = TL()
model.configure_traits()
                

求大神帮忙看一下


[此贴子已经被作者于2018-11-6 00:04编辑过]

搜索更多相关主题的帖子: import cos sin def plot 
2018-11-05 23:58
杨xiao
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-11-5
得分:0 
程序有点长,但是只看一个函数就行,因为图像我分六段画的,所以用了六个函数。
2018-11-05 23:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-491023-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.358328 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved