VTK:视图在用户交互后才会更新

VTK: View doesn't update until after user interaction

本文关键字:更新 交互 用户 视图 VTK      更新时间:2023-10-16

TL;博士

我有一个管道,它读取图像并使用VTK显示网格;在更改图像阅读器的输入并更新管道时,网格不会更新,直到我与窗互。

长版本

我有一个目录,其中包含一系列分割文件(即像素值对应于相应图像中的结构的 3D 图像体积),这些文件显示了结构如何随时间变化。 我在VTK中编写了一个实用程序,它允许我加载目录中的第一个图像,将标签可视化为网格,并通过更改输入图像的文件名和更新管道来使用箭头键向前或向后"前进"。 这几乎有效 - 唯一的问题是,在更新管道后,网格不会更新,直到我与窗互(例如,只需单击窗口中的任意位置即可更新网格)。

我尝试过什么:

  1. 在读卡器上调用Update()

    this->reader->Update();
    
  2. 呼吁Modified()演员:

    const auto actors = this->GetCurrentRenderer()->GetActors();
    actors->InitTraversal();
    for (vtkIdType i = 0; i < actors->GetNumberOfItems(); ++i)
    {
    actors->GetNextActor()->Modified();
    }
    
  3. 在当前渲染窗口上调用Render()

    this->GetCurrentRenderer()->Render();
    

MCVE:

注意:读卡器在KeyPressInteractorStyle::UpdateReader()中更新。

// VTK
#include <vtkSmartPointer.h>
#include <vtkNIFTIImageReader.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkCamera.h>
#include <vtkDiscreteMarchingCubes.h>
#include <vtkProperty.h>
#include <vtkInteractorStyleTrackballCamera.h>
// Define interaction style
class KeyPressInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
static KeyPressInteractorStyle* New();
vtkTypeMacro(KeyPressInteractorStyle, vtkInteractorStyleTrackballCamera);
virtual void OnKeyPress() 
{
// Get the keypress
vtkRenderWindowInteractor *rwi = this->Interactor;
std::string key = rwi->GetKeySym();
// Output the key that was pressed
std::cout << "Pressed " << key << std::endl;
// Handle an arrow key
if(key == "Down" || key == "Right")
{
this->index += 1;
this->UpdateReader();
}
// Handle an arrow key
if(key == "Up" || key == "Left")
{
this->index -= 1;
this->UpdateReader();
}
// Forward events
vtkInteractorStyleTrackballCamera::OnKeyPress();
}
void UpdateReader()
{
std::cout << "Frame: " << this->index << std::endl;
const auto fn = this->directory + std::to_string(this->index) + ".nii.gz";
std::cout << fn << std::endl;
this->reader->SetFileName( fn.c_str() );
this->reader->Update();
const auto actors = this->GetCurrentRenderer()->GetActors();
actors->InitTraversal();
for (vtkIdType i = 0; i < actors->GetNumberOfItems(); ++i)
{
actors->GetNextActor()->Modified();
}
this->GetCurrentRenderer()->Render();
}
unsigned int index = 0;
std::string directory;
vtkSmartPointer<vtkNIFTIImageReader> reader;
};
vtkStandardNewMacro(KeyPressInteractorStyle);
int
main( int argc, char ** argv )
{
std::string dn = argv[1];
const auto renderer = vtkSmartPointer<vtkRenderer>::New();
unsigned int frameid = 0;
const auto reader = vtkSmartPointer<vtkNIFTIImageReader>::New();
reader->SetFileName( (dn + std::to_string(frameid) + ".nii.gz").c_str() );
const auto cubes = vtkSmartPointer<vtkDiscreteMarchingCubes>::New();
cubes->SetInputConnection( reader->GetOutputPort() );
cubes->SetValue( 0, 1 );
const auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection( cubes->GetOutputPort() );
mapper->ScalarVisibilityOff();
const auto actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper( mapper );
renderer->AddActor( actor );
const auto window = vtkSmartPointer<vtkRenderWindow>::New();
window->AddRenderer( renderer );
const auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
const auto style = vtkSmartPointer<KeyPressInteractorStyle>::New();
style->reader = reader;
style->directory = dn;
interactor->SetInteractorStyle( style );
style->SetCurrentRenderer( renderer );
interactor->SetRenderWindow( window );
window->Render();
interactor->Start();
return EXIT_SUCCESS;
}

您实际上并没有尝试在当前渲染窗口上调用Render(),而是在当前渲染器上调用它,这是两回事。正如 vtkRenderer::Renderer() 的文档所述,

仅由 vtkRenderWindow 调用。

最终用户通过您的方式并调用 vtkRenderWindow::Render()。创建一个 图像。这是一个超类方法,它将依次调用 vtkRenderer 子类的 DeviceRender 方法。

因此,将this->GetCurrentRenderer()->Render();更改为this->GetCurrentRenderer()->GetRenderWindow()->Render();