我如何使自定义事件与VTK

How do I make a custom event with VTK?

本文关键字:VTK 事件 自定义 何使      更新时间:2023-10-16

我正在使用VTK制作线程软件,我需要实时更改模型本身,而我需要更改他的渲染方法。一切都很好,但是,问题从interactor->start();开始,模型数据更新得很好,但只有当我移动相机时才显示在屏幕上。此外,我还选择了一些从imagedata文件生成3D数据的方法,为此我需要关闭vtk窗口(交互器窗口),然后代码将重新打开它并将生成的新数据发送给它…

我需要这样的东西:

int force close_window = false; int refresh_interactor = false;

我设法使窗口关闭,但只使用vtkcommand::Keypressed命令,但idk我如何使用新命令:S,我尝试了vtkcommand::UserEvent,但我没有找到关于如何处理该数据的良好信息(如某种方式调用它)

我处理VTK的方式是两个线程,第一个,只是关于VTK iren循环,第二个将管理模型并检查iren是否需要更新。

在我的梦想代码应该是这样的:

=======================================================

bool VTKWindow()
{ 
    ... 
    vtkSmartPointer ator = vtkSmartPointer::New(); 
    iren = vtkSmartPointer::New(); 
    RenWindow = vtkSmartPointer::New(); 
    render->SetBackground(.1, .2, .3); 
    RenWindow->AddRenderer(renderer); 
    iren->SetRenderWindow(RenWindow);
    if(data_type == voxel_type)
    { 
        Render->AddViewProp(VoxelData); 
    }
    else
    { 
        actor->SetMapper(PolyData);
        Render->AddActor(Actor); 
    }
    RenWindow->Render(); 
    iren->Start();
}
void ManageVTK()
{ 
    while true loop... 
            if(force close_window == true)
                do some command to exit the iren loop 
            if(refresh_interactor == true)
                do some command to refresh iren 
}

对不起,英语不是我的母语,也对不起问题的格式,这是我第一次使用stackoverflow

这听起来可能很愚蠢,但是,我找到了一种解决问题的方法。我在相关链接上看到这个家伙vtkRenderWindowInteractor事件循环和线程,这几乎是相同的问题…

class VTKNewEvent : public vtkCommand{
    public:
        vtkTypeMacro(VTKNewEvent , vtkCommand);
        static VTKNewEvent * New(){
            return new VTKNewEvent ;
        }
        void Execute(vtkObject * caller, unsigned long vtkNotUsed(eventId), void * vtkNotUsed(callData)){
            vtkRenderWindowInteractor *iren = static_cast<vtkRenderWindowInteractor*>(caller);
            if (iren_close == true){
                iren->GetRenderWindow()->Finalize                   // Stop the interactor
                iren->TerminateApp();
                iren_close = false;
            }
            if (iren_update== true){
                renderJanela->Render();
                iren_update= false;
            }           
        }
};

bool VTKWindow(){
    vtkSmartPointer<VTKNewEvent > IrenRefresh= vtkSmartPointer<VTKNewEvent>::New();
...
    iren->CreateRepeatingTimer(1);//this makes that IrenRefresh will be called at every 1ms
    iren->AddObserver(vtkCommand::TimerEvent, IrenRefresh);
    iren->Start();
...
}

它很简单,但是,也许不是最好的,但它做了这项工作,我希望这个链接将帮助人们开始进入VTK世界,因为线程+渲染循环不是一个简单的工作来理解发生了什么