为了账号安全,请及时绑定邮箱和手机立即绑定

渲染在 vtkCommand 中创建的 actor(回调)

渲染在 vtkCommand 中创建的 actor(回调)

Go
湖上湖 2021-10-19 09:49:26
我正在尝试编写一个程序,该程序在按下箭头键时显示和循环显示不同文件格式的图像。我发现设置我的 reader->mapper->actor 管道然后启动我的 RenderWindowInteractor 允许渲染 actor,但是将此代码移动到 RenderWindowInteractor 的 KeyPressEvent 的回调不允许渲染 actor。一旦事件循环开始,是否需要额外的步骤来设置新的actor,或者我犯了其他错误?import vtk#Create the renderer that will display our actorsren = vtk.vtkRenderer()actor = None#Maps the data from the Nifti file 'filename' onto a vtkImageSlice actordef LoadNifti():    # load head MRI in NIFTI format    reader = vtk.vtkNIFTIImageReader()    reader.SetFileName("head.nii")    reader.Update()    # Add an ImageSliceMapper, which maps data to an ImageSlice Actor    mapper = vtk.vtkImageSliceMapper()    #Set the slice position to the camera focal point    mapper.SliceAtFocalPointOn()    mapper.SetInputConnection(reader.GetOutputPort())    # Add an ImageSlice actor, which represents a slice as an image    global actor    actor = vtk.vtkImageSlice()    actor.SetMapper(mapper)#Placeholder for when I have DICOM workingdef ShowDICOM(filename):    pass#Create a RenderWindow to display images from the RendererrenWin = vtk.vtkRenderWindow()renWin.AddRenderer(ren)#Wrap the RenderWindow in a RenderWindowInteractor to catch key and mouse eventsiren = vtk.vtkRenderWindowInteractor()iren.SetRenderWindow(renWin)def foo():    LoadNifti()    ren.AddActor(actor)##Extend the default functionality of the interactor and its style with custom listenersdef KeyPressEvent(obj, ev):    foo()    print("This verifies that the callback is triggered")iren.AddObserver("KeyPressEvent", KeyPressEvent)        #Start the program############################################################################ WHEN foo() IS PRESENT BELOW, THE ACTOR WILL RENDER IMMEDIATELY.# WHEN foo() IS ABSENT BELOW, CALLING foo() BY TRIGGERING THE KeyPressEvent# IS NOT SUFFICIENT TO HAVE THE ACTOR RENDER.###########################################################################foo()#According to the docs, the Start method will initialize iren and render renWin automaticallyiren.Start()
查看完整描述

1 回答

?
子衿沉夜

TA贡献1828条经验 获得超3个赞

好吧,您绝对可以在回调中添加或删除角色。要立即再次渲染场景,只需调用iren.Render()。


当您在没有任何可见演员和明确定义的相机的情况下启动渲染器时,ResetCamera通常需要。AutomaticResetCamera在初始化期间只调用一次,这就是为什么调用foo()beforeiren.Start()使对象可见的原因。


def foo():

    LoadNifti()

    ren.AddActor(actor)

    ren.ResetCamera() # should that be necessary

    iren.Render()


查看完整回答
反对 回复 2021-10-19
  • 1 回答
  • 0 关注
  • 346 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信